blob: 8a63a4882f2b01163ca22161f3cf28b17653a126 [file] [log] [blame]
Andreas Gampe54fc26c2014-09-04 21:47:42 -07001/*
David Srbeckybc90fd02015-04-22 19:40:27 +01002 * Copyright (C) 2015 The Android Open Source Project
Andreas Gampe54fc26c2014-09-04 21:47:42 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_ELF_BUILDER_H_
18#define ART_COMPILER_ELF_BUILDER_H_
19
David Srbeckybc90fd02015-04-22 19:40:27 +010020#include <vector>
21
Ian Rogersd582fa42014-11-05 23:46:43 -080022#include "arch/instruction_set.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010023#include "base/bit_utils.h"
David Srbeckybc90fd02015-04-22 19:40:27 +010024#include "base/unix_file/fd_file.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070025#include "buffered_output_stream.h"
26#include "elf_utils.h"
27#include "file_output_stream.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070028
29namespace art {
30
Andreas Gampe54fc26c2014-09-04 21:47:42 -070031class CodeOutput {
32 public:
33 virtual bool Write(OutputStream* out) = 0;
34 virtual ~CodeOutput() {}
35};
36
David Srbeckybc90fd02015-04-22 19:40:27 +010037// Writes ELF file.
38// The main complication is that the sections often want to reference
39// each other. We solve this by writing the ELF file in two stages:
40// * Sections are asked about their size, and overall layout is calculated.
41// * Sections do the actual writes which may use offsets of other sections.
David Srbecky533c2072015-04-22 12:20:22 +010042template <typename ElfTypes>
Andreas Gampe54fc26c2014-09-04 21:47:42 -070043class ElfBuilder FINAL {
44 public:
David Srbecky533c2072015-04-22 12:20:22 +010045 using Elf_Addr = typename ElfTypes::Addr;
David Srbeckybc90fd02015-04-22 19:40:27 +010046 using Elf_Off = typename ElfTypes::Off;
David Srbecky533c2072015-04-22 12:20:22 +010047 using Elf_Word = typename ElfTypes::Word;
48 using Elf_Sword = typename ElfTypes::Sword;
49 using Elf_Ehdr = typename ElfTypes::Ehdr;
50 using Elf_Shdr = typename ElfTypes::Shdr;
51 using Elf_Sym = typename ElfTypes::Sym;
52 using Elf_Phdr = typename ElfTypes::Phdr;
53 using Elf_Dyn = typename ElfTypes::Dyn;
54
David Srbeckybc90fd02015-04-22 19:40:27 +010055 // Base class of all sections.
56 class Section {
David Srbecky0c5bbc12015-04-28 17:54:52 +010057 public:
David Srbeckybc90fd02015-04-22 19:40:27 +010058 Section(const std::string& name, Elf_Word type, Elf_Word flags,
59 const Section* link, Elf_Word info, Elf_Word align, Elf_Word entsize)
David Srbecky90688ae2015-05-19 16:30:51 +010060 : header_(new Elf_Shdr()), section_index_(0), name_(name), link_(link) {
61 header_->sh_type = type;
62 header_->sh_flags = flags;
63 header_->sh_info = info;
64 header_->sh_addralign = align;
65 header_->sh_entsize = entsize;
David Srbecky0c5bbc12015-04-28 17:54:52 +010066 }
David Srbeckybc90fd02015-04-22 19:40:27 +010067 virtual ~Section() {}
David Srbecky0c5bbc12015-04-28 17:54:52 +010068
David Srbeckybc90fd02015-04-22 19:40:27 +010069 // Returns the size of the content of this section. It is used to
70 // calculate file offsets of all sections before doing any writes.
71 virtual Elf_Word GetSize() const = 0;
72
73 // Write the content of this section to the given file.
74 // This must write exactly the number of bytes returned by GetSize().
75 // Offsets of all sections are known when this method is called.
76 virtual bool Write(File* elf_file) = 0;
David Srbecky0c5bbc12015-04-28 17:54:52 +010077
78 Elf_Word GetLink() const {
David Srbeckybc90fd02015-04-22 19:40:27 +010079 return (link_ != nullptr) ? link_->GetSectionIndex() : 0;
David Srbecky0c5bbc12015-04-28 17:54:52 +010080 }
81
David Srbeckybc90fd02015-04-22 19:40:27 +010082 const Elf_Shdr* GetHeader() const {
David Srbecky90688ae2015-05-19 16:30:51 +010083 return header_.get();
David Srbecky0c5bbc12015-04-28 17:54:52 +010084 }
85
David Srbeckybc90fd02015-04-22 19:40:27 +010086 Elf_Shdr* GetHeader() {
David Srbecky90688ae2015-05-19 16:30:51 +010087 return header_.get();
David Srbecky0c5bbc12015-04-28 17:54:52 +010088 }
89
90 Elf_Word GetSectionIndex() const {
David Srbeckybc90fd02015-04-22 19:40:27 +010091 DCHECK_NE(section_index_, 0u);
David Srbecky0c5bbc12015-04-28 17:54:52 +010092 return section_index_;
93 }
94
95 void SetSectionIndex(Elf_Word section_index) {
96 section_index_ = section_index;
97 }
98
99 const std::string& GetName() const {
100 return name_;
101 }
102
103 private:
David Srbecky90688ae2015-05-19 16:30:51 +0100104 // Elf_Shdr is somewhat large so allocate it on the heap.
105 // Otherwise we get in trouble with stack frame sizes.
106 std::unique_ptr<Elf_Shdr> header_;
David Srbecky0c5bbc12015-04-28 17:54:52 +0100107 Elf_Word section_index_;
108 const std::string name_;
David Srbeckybc90fd02015-04-22 19:40:27 +0100109 const Section* const link_;
110
111 DISALLOW_COPY_AND_ASSIGN(Section);
David Srbecky0c5bbc12015-04-28 17:54:52 +0100112 };
113
David Srbeckybc90fd02015-04-22 19:40:27 +0100114 // Writer of .dynamic section.
115 class DynamicSection FINAL : public Section {
David Srbecky0c5bbc12015-04-28 17:54:52 +0100116 public:
David Srbeckybc90fd02015-04-22 19:40:27 +0100117 void AddDynamicTag(Elf_Sword tag, Elf_Word value, const Section* section) {
118 DCHECK_NE(tag, static_cast<Elf_Sword>(DT_NULL));
119 dynamics_.push_back({tag, value, section});
David Srbecky0c5bbc12015-04-28 17:54:52 +0100120 }
121
David Srbeckybc90fd02015-04-22 19:40:27 +0100122 DynamicSection(const std::string& name, Section* link)
123 : Section(name, SHT_DYNAMIC, SHF_ALLOC,
124 link, 0, kPageSize, sizeof(Elf_Dyn)) {}
125
126 Elf_Word GetSize() const OVERRIDE {
127 return (dynamics_.size() + 1 /* DT_NULL */) * sizeof(Elf_Dyn);
David Srbecky0c5bbc12015-04-28 17:54:52 +0100128 }
129
David Srbeckybc90fd02015-04-22 19:40:27 +0100130 bool Write(File* elf_file) OVERRIDE {
131 std::vector<Elf_Dyn> buffer;
132 buffer.reserve(dynamics_.size() + 1u);
133 for (const ElfDynamicState& it : dynamics_) {
134 if (it.section_ != nullptr) {
David Srbecky0c5bbc12015-04-28 17:54:52 +0100135 // We are adding an address relative to a section.
David Srbeckybc90fd02015-04-22 19:40:27 +0100136 buffer.push_back(
137 {it.tag_, {it.value_ + it.section_->GetHeader()->sh_addr}});
David Srbecky0c5bbc12015-04-28 17:54:52 +0100138 } else {
David Srbeckybc90fd02015-04-22 19:40:27 +0100139 buffer.push_back({it.tag_, {it.value_}});
David Srbecky0c5bbc12015-04-28 17:54:52 +0100140 }
141 }
David Srbeckybc90fd02015-04-22 19:40:27 +0100142 buffer.push_back({DT_NULL, {0}});
143 return WriteArray(elf_file, buffer.data(), buffer.size());
David Srbecky0c5bbc12015-04-28 17:54:52 +0100144 }
145
146 private:
147 struct ElfDynamicState {
David Srbecky0c5bbc12015-04-28 17:54:52 +0100148 Elf_Sword tag_;
David Srbeckybc90fd02015-04-22 19:40:27 +0100149 Elf_Word value_;
150 const Section* section_;
David Srbecky0c5bbc12015-04-28 17:54:52 +0100151 };
152 std::vector<ElfDynamicState> dynamics_;
153 };
154
David Srbecky033d7452015-04-30 19:57:35 +0100155 using PatchFn = void (*)(const std::vector<uintptr_t>& patch_locations,
156 Elf_Addr buffer_address,
157 Elf_Addr base_address,
158 std::vector<uint8_t>* buffer);
159
David Srbeckybc90fd02015-04-22 19:40:27 +0100160 // Section with content based on simple memory buffer.
161 // The buffer can be optionally patched before writing.
David Srbeckybc90fd02015-04-22 19:40:27 +0100162 class RawSection FINAL : public Section {
David Srbecky0c5bbc12015-04-28 17:54:52 +0100163 public:
David Srbeckybc90fd02015-04-22 19:40:27 +0100164 RawSection(const std::string& name, Elf_Word type, Elf_Word flags,
165 const Section* link, Elf_Word info, Elf_Word align, Elf_Word entsize,
David Srbecky033d7452015-04-30 19:57:35 +0100166 PatchFn patch = nullptr, const Section* patch_base_section = nullptr)
David Srbeckybc90fd02015-04-22 19:40:27 +0100167 : Section(name, type, flags, link, info, align, entsize),
David Srbecky033d7452015-04-30 19:57:35 +0100168 patched_(false), patch_(patch), patch_base_section_(patch_base_section) {
David Srbecky0c5bbc12015-04-28 17:54:52 +0100169 }
David Srbecky0c5bbc12015-04-28 17:54:52 +0100170
David Srbeckybc90fd02015-04-22 19:40:27 +0100171 Elf_Word GetSize() const OVERRIDE {
172 return buffer_.size();
173 }
174
175 bool Write(File* elf_file) OVERRIDE {
176 if (!patch_locations_.empty()) {
David Srbecky033d7452015-04-30 19:57:35 +0100177 DCHECK(!patched_); // Do not patch twice.
178 DCHECK(patch_ != nullptr);
179 DCHECK(patch_base_section_ != nullptr);
180 patch_(patch_locations_,
181 this->GetHeader()->sh_addr,
182 patch_base_section_->GetHeader()->sh_addr,
183 &buffer_);
184 patched_ = true;
David Srbeckybc90fd02015-04-22 19:40:27 +0100185 }
186 return WriteArray(elf_file, buffer_.data(), buffer_.size());
187 }
188
189 bool IsEmpty() const {
190 return buffer_.size() == 0;
191 }
David Srbecky0c5bbc12015-04-28 17:54:52 +0100192
193 std::vector<uint8_t>* GetBuffer() {
David Srbeckybc90fd02015-04-22 19:40:27 +0100194 return &buffer_;
David Srbecky0c5bbc12015-04-28 17:54:52 +0100195 }
196
David Srbeckybc90fd02015-04-22 19:40:27 +0100197 void SetBuffer(const std::vector<uint8_t>& buffer) {
198 buffer_ = buffer;
199 }
200
201 std::vector<uintptr_t>* GetPatchLocations() {
202 return &patch_locations_;
David Srbecky0c5bbc12015-04-28 17:54:52 +0100203 }
204
205 private:
David Srbeckybc90fd02015-04-22 19:40:27 +0100206 std::vector<uint8_t> buffer_;
207 std::vector<uintptr_t> patch_locations_;
David Srbecky033d7452015-04-30 19:57:35 +0100208 bool patched_;
209 // User-provided function to do the actual patching.
210 PatchFn patch_;
211 // The section that we patch against (usually .text).
212 const Section* patch_base_section_;
David Srbecky0c5bbc12015-04-28 17:54:52 +0100213 };
214
David Srbeckybc90fd02015-04-22 19:40:27 +0100215 // Writer of .rodata section or .text section.
216 // The write is done lazily using the provided CodeOutput.
217 class OatSection FINAL : public Section {
David Srbecky0c5bbc12015-04-28 17:54:52 +0100218 public:
David Srbeckybc90fd02015-04-22 19:40:27 +0100219 OatSection(const std::string& name, Elf_Word type, Elf_Word flags,
220 const Section* link, Elf_Word info, Elf_Word align,
221 Elf_Word entsize, Elf_Word size, CodeOutput* code_output)
222 : Section(name, type, flags, link, info, align, entsize),
223 size_(size), code_output_(code_output) {
David Srbecky0c5bbc12015-04-28 17:54:52 +0100224 }
225
David Srbeckybc90fd02015-04-22 19:40:27 +0100226 Elf_Word GetSize() const OVERRIDE {
David Srbecky0c5bbc12015-04-28 17:54:52 +0100227 return size_;
228 }
229
David Srbeckybc90fd02015-04-22 19:40:27 +0100230 bool Write(File* elf_file) OVERRIDE {
231 // The BufferedOutputStream class contains the buffer as field,
232 // therefore it is too big to allocate on the stack.
233 std::unique_ptr<BufferedOutputStream> output_stream(
234 new BufferedOutputStream(new FileOutputStream(elf_file)));
235 return code_output_->Write(output_stream.get());
236 }
237
David Srbecky0c5bbc12015-04-28 17:54:52 +0100238 private:
David Srbeckybc90fd02015-04-22 19:40:27 +0100239 Elf_Word size_;
240 CodeOutput* code_output_;
241 };
242
243 // Writer of .bss section.
244 class NoBitsSection FINAL : public Section {
245 public:
246 NoBitsSection(const std::string& name, Elf_Word size)
247 : Section(name, SHT_NOBITS, SHF_ALLOC, nullptr, 0, kPageSize, 0),
248 size_(size) {
249 }
250
251 Elf_Word GetSize() const OVERRIDE {
252 return size_;
253 }
254
255 bool Write(File* elf_file ATTRIBUTE_UNUSED) OVERRIDE {
256 LOG(ERROR) << "This section should not be written to the ELF file";
257 return false;
258 }
259
260 private:
David Srbecky0c5bbc12015-04-28 17:54:52 +0100261 Elf_Word size_;
262 };
263
David Srbeckybc90fd02015-04-22 19:40:27 +0100264 // Writer of .dynstr .strtab and .shstrtab sections.
265 class StrtabSection FINAL : public Section {
266 public:
267 StrtabSection(const std::string& name, Elf_Word flags)
268 : Section(name, SHT_STRTAB, flags, nullptr, 0, 1, 1) {
269 buffer_.reserve(4 * KB);
270 // The first entry of strtab must be empty string.
271 buffer_ += '\0';
David Srbecky0c5bbc12015-04-28 17:54:52 +0100272 }
David Srbecky0c5bbc12015-04-28 17:54:52 +0100273
David Srbeckybc90fd02015-04-22 19:40:27 +0100274 Elf_Word AddName(const std::string& name) {
275 Elf_Word offset = buffer_.size();
276 buffer_ += name;
277 buffer_ += '\0';
278 return offset;
279 }
280
281 Elf_Word GetSize() const OVERRIDE {
282 return buffer_.size();
283 }
284
285 bool Write(File* elf_file) OVERRIDE {
286 return WriteArray(elf_file, buffer_.data(), buffer_.size());
287 }
288
289 private:
290 std::string buffer_;
291 };
292
293 class HashSection;
294
295 // Writer of .dynsym and .symtab sections.
296 class SymtabSection FINAL : public Section {
David Srbecky0c5bbc12015-04-28 17:54:52 +0100297 public:
298 // Add a symbol with given name to this symtab. The symbol refers to
299 // 'relative_addr' within the given section and has the given attributes.
David Srbeckybc90fd02015-04-22 19:40:27 +0100300 void AddSymbol(const std::string& name, const Section* section,
301 Elf_Addr addr, bool is_relative, Elf_Word size,
302 uint8_t binding, uint8_t type, uint8_t other = 0) {
303 CHECK(section != nullptr);
304 Elf_Word name_idx = strtab_->AddName(name);
305 symbols_.push_back({ name, section, addr, size, is_relative,
306 MakeStInfo(binding, type), other, name_idx });
David Srbecky0c5bbc12015-04-28 17:54:52 +0100307 }
308
David Srbeckybc90fd02015-04-22 19:40:27 +0100309 SymtabSection(const std::string& name, Elf_Word type, Elf_Word flags,
310 StrtabSection* strtab)
311 : Section(name, type, flags, strtab, 0, sizeof(Elf_Word), sizeof(Elf_Sym)),
312 strtab_(strtab) {
David Srbecky0c5bbc12015-04-28 17:54:52 +0100313 }
314
David Srbeckybc90fd02015-04-22 19:40:27 +0100315 bool IsEmpty() const {
316 return symbols_.empty();
317 }
David Srbecky0c5bbc12015-04-28 17:54:52 +0100318
David Srbeckybc90fd02015-04-22 19:40:27 +0100319 Elf_Word GetSize() const OVERRIDE {
320 return (1 /* NULL */ + symbols_.size()) * sizeof(Elf_Sym);
321 }
322
323 bool Write(File* elf_file) OVERRIDE {
324 std::vector<Elf_Sym> buffer;
325 buffer.reserve(1u + symbols_.size());
326 buffer.push_back(Elf_Sym()); // NULL.
327 for (const ElfSymbolState& it : symbols_) {
328 Elf_Sym sym = Elf_Sym();
329 sym.st_name = it.name_idx_;
330 if (it.is_relative_) {
331 sym.st_value = it.addr_ + it.section_->GetHeader()->sh_addr;
332 } else {
333 sym.st_value = it.addr_;
334 }
335 sym.st_size = it.size_;
336 sym.st_other = it.other_;
337 sym.st_shndx = it.section_->GetSectionIndex();
338 sym.st_info = it.info_;
339 buffer.push_back(sym);
340 }
341 return WriteArray(elf_file, buffer.data(), buffer.size());
342 }
343
344 private:
345 struct ElfSymbolState {
346 const std::string name_;
347 const Section* section_;
348 Elf_Addr addr_;
349 Elf_Word size_;
350 bool is_relative_;
351 uint8_t info_;
352 uint8_t other_;
353 Elf_Word name_idx_; // index in the strtab.
354 };
355
356 static inline constexpr uint8_t MakeStInfo(uint8_t binding, uint8_t type) {
357 return ((binding) << 4) + ((type) & 0xf);
358 }
359
360 // The symbols in the same order they will be in the symbol table.
361 std::vector<ElfSymbolState> symbols_;
362 StrtabSection* strtab_;
363
364 friend class HashSection;
365 };
366
367 // TODO: Consider removing.
368 // We use it only for the dynsym section which has only 5 symbols.
369 // We do not use it for symtab, and we probably do not have to
370 // since we use those symbols only to print backtraces.
371 class HashSection FINAL : public Section {
372 public:
373 HashSection(const std::string& name, Elf_Word flags, SymtabSection* symtab)
374 : Section(name, SHT_HASH, flags, symtab,
375 0, sizeof(Elf_Word), sizeof(Elf_Word)),
376 symtab_(symtab) {
377 }
378
379 Elf_Word GetSize() const OVERRIDE {
380 Elf_Word nbuckets = GetNumBuckets();
381 Elf_Word chain_size = symtab_->symbols_.size() + 1 /* NULL */;
382 return (2 /* header */ + nbuckets + chain_size) * sizeof(Elf_Word);
383 }
384
385 bool Write(File* const elf_file) OVERRIDE {
David Srbecky0c5bbc12015-04-28 17:54:52 +0100386 // Here is how The ELF hash table works.
387 // There are 3 arrays to worry about.
388 // * The symbol table where the symbol information is.
389 // * The bucket array which is an array of indexes into the symtab and chain.
390 // * The chain array which is also an array of indexes into the symtab and chain.
391 //
392 // Lets say the state is something like this.
393 // +--------+ +--------+ +-----------+
394 // | symtab | | bucket | | chain |
395 // | null | | 1 | | STN_UNDEF |
396 // | <sym1> | | 4 | | 2 |
397 // | <sym2> | | | | 5 |
398 // | <sym3> | | | | STN_UNDEF |
399 // | <sym4> | | | | 3 |
400 // | <sym5> | | | | STN_UNDEF |
401 // +--------+ +--------+ +-----------+
402 //
403 // The lookup process (in python psudocode) is
404 //
405 // def GetSym(name):
406 // # NB STN_UNDEF == 0
407 // indx = bucket[elfhash(name) % num_buckets]
408 // while indx != STN_UNDEF:
409 // if GetSymbolName(symtab[indx]) == name:
410 // return symtab[indx]
411 // indx = chain[indx]
412 // return SYMBOL_NOT_FOUND
413 //
414 // Between bucket and chain arrays every symtab index must be present exactly
415 // once (except for STN_UNDEF, which must be present 1 + num_bucket times).
David Srbeckybc90fd02015-04-22 19:40:27 +0100416 const auto& symbols = symtab_->symbols_;
David Srbecky0c5bbc12015-04-28 17:54:52 +0100417 // Select number of buckets.
418 // This is essentially arbitrary.
David Srbeckybc90fd02015-04-22 19:40:27 +0100419 Elf_Word nbuckets = GetNumBuckets();
420 // 1 is for the implicit NULL symbol.
421 Elf_Word chain_size = (symbols.size() + 1);
David Srbecky0c5bbc12015-04-28 17:54:52 +0100422 std::vector<Elf_Word> hash;
423 hash.push_back(nbuckets);
424 hash.push_back(chain_size);
425 uint32_t bucket_offset = hash.size();
426 uint32_t chain_offset = bucket_offset + nbuckets;
427 hash.resize(hash.size() + nbuckets + chain_size, 0);
428
429 Elf_Word* buckets = hash.data() + bucket_offset;
430 Elf_Word* chain = hash.data() + chain_offset;
431
432 // Set up the actual hash table.
David Srbeckybc90fd02015-04-22 19:40:27 +0100433 for (Elf_Word i = 0; i < symbols.size(); i++) {
David Srbecky0c5bbc12015-04-28 17:54:52 +0100434 // Add 1 since we need to have the null symbol that is not in the symbols
435 // list.
436 Elf_Word index = i + 1;
David Srbeckybc90fd02015-04-22 19:40:27 +0100437 Elf_Word hash_val = static_cast<Elf_Word>(elfhash(symbols[i].name_.c_str())) % nbuckets;
David Srbecky0c5bbc12015-04-28 17:54:52 +0100438 if (buckets[hash_val] == 0) {
439 buckets[hash_val] = index;
440 } else {
441 hash_val = buckets[hash_val];
442 CHECK_LT(hash_val, chain_size);
443 while (chain[hash_val] != 0) {
444 hash_val = chain[hash_val];
445 CHECK_LT(hash_val, chain_size);
446 }
447 chain[hash_val] = index;
448 // Check for loops. Works because if this is non-empty then there must be
449 // another cell which already contains the same symbol index as this one,
450 // which means some symbol has more then one name, which isn't allowed.
451 CHECK_EQ(chain[index], static_cast<Elf_Word>(0));
452 }
453 }
David Srbeckybc90fd02015-04-22 19:40:27 +0100454 return WriteArray(elf_file, hash.data(), hash.size());
David Srbecky0c5bbc12015-04-28 17:54:52 +0100455 }
456
457 private:
David Srbeckybc90fd02015-04-22 19:40:27 +0100458 Elf_Word GetNumBuckets() const {
459 const auto& symbols = symtab_->symbols_;
460 if (symbols.size() < 8) {
461 return 2;
462 } else if (symbols.size() < 32) {
463 return 4;
464 } else if (symbols.size() < 256) {
465 return 16;
466 } else {
467 // Have about 32 ids per bucket.
468 return RoundUp(symbols.size()/32, 2);
David Srbecky0c5bbc12015-04-28 17:54:52 +0100469 }
470 }
David Srbecky0c5bbc12015-04-28 17:54:52 +0100471
David Srbeckybc90fd02015-04-22 19:40:27 +0100472 // from bionic
473 static inline unsigned elfhash(const char *_name) {
474 const unsigned char *name = (const unsigned char *) _name;
475 unsigned h = 0, g;
David Srbecky0c5bbc12015-04-28 17:54:52 +0100476
David Srbeckybc90fd02015-04-22 19:40:27 +0100477 while (*name) {
478 h = (h << 4) + *name++;
479 g = h & 0xf0000000;
480 h ^= g;
481 h ^= g >> 24;
482 }
483 return h;
484 }
485
486 SymtabSection* symtab_;
487
488 DISALLOW_COPY_AND_ASSIGN(HashSection);
489 };
490
491 ElfBuilder(InstructionSet isa,
492 Elf_Word rodata_size, CodeOutput* rodata_writer,
493 Elf_Word text_size, CodeOutput* text_writer,
494 Elf_Word bss_size)
495 : isa_(isa),
496 dynstr_(".dynstr", SHF_ALLOC),
497 dynsym_(".dynsym", SHT_DYNSYM, SHF_ALLOC, &dynstr_),
498 hash_(".hash", SHF_ALLOC, &dynsym_),
499 rodata_(".rodata", SHT_PROGBITS, SHF_ALLOC,
500 nullptr, 0, kPageSize, 0, rodata_size, rodata_writer),
501 text_(".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR,
502 nullptr, 0, kPageSize, 0, text_size, text_writer),
503 bss_(".bss", bss_size),
504 dynamic_(".dynamic", &dynsym_),
505 strtab_(".strtab", 0),
506 symtab_(".symtab", SHT_SYMTAB, 0, &strtab_),
507 shstrtab_(".shstrtab", 0) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700508 }
509 ~ElfBuilder() {}
510
David Srbeckybc90fd02015-04-22 19:40:27 +0100511 OatSection* GetText() { return &text_; }
512 SymtabSection* GetSymtab() { return &symtab_; }
Ian Rogers0279ebb2014-10-08 17:27:48 -0700513
David Srbeckybc90fd02015-04-22 19:40:27 +0100514 bool Write(File* elf_file) {
Vladimir Marko5c42c292015-02-25 12:02:49 +0000515 // Since the .text section of an oat file contains relative references to .rodata
516 // and (optionally) .bss, we keep these 2 or 3 sections together. This creates
517 // a non-traditional layout where the .bss section is mapped independently of the
518 // .dynamic section and needs its own program header with LOAD RW.
519 //
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700520 // The basic layout of the elf file. Order may be different in final output.
521 // +-------------------------+
522 // | Elf_Ehdr |
523 // +-------------------------+
524 // | Elf_Phdr PHDR |
David Srbeckyb0a962c2015-04-28 19:43:56 +0100525 // | Elf_Phdr LOAD R | .dynsym .dynstr .hash .rodata
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700526 // | Elf_Phdr LOAD R X | .text
Vladimir Marko5c42c292015-02-25 12:02:49 +0000527 // | Elf_Phdr LOAD RW | .bss (Optional)
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700528 // | Elf_Phdr LOAD RW | .dynamic
529 // | Elf_Phdr DYNAMIC | .dynamic
David Srbeckyb0a962c2015-04-28 19:43:56 +0100530 // | Elf_Phdr LOAD R | .eh_frame .eh_frame_hdr
David Srbecky527c9c72015-04-17 21:14:10 +0100531 // | Elf_Phdr EH_FRAME R | .eh_frame_hdr
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700532 // +-------------------------+
533 // | .dynsym |
534 // | Elf_Sym STN_UNDEF |
535 // | Elf_Sym oatdata |
536 // | Elf_Sym oatexec |
537 // | Elf_Sym oatlastword |
Vladimir Marko5c42c292015-02-25 12:02:49 +0000538 // | Elf_Sym oatbss | (Optional)
539 // | Elf_Sym oatbsslastword | (Optional)
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700540 // +-------------------------+
541 // | .dynstr |
David Srbeckybc90fd02015-04-22 19:40:27 +0100542 // | names for .dynsym |
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700543 // +-------------------------+
544 // | .hash |
David Srbeckybc90fd02015-04-22 19:40:27 +0100545 // | hashtable for dynsym |
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700546 // +-------------------------+
547 // | .rodata |
548 // | oatdata..oatexec-4 |
549 // +-------------------------+
550 // | .text |
551 // | oatexec..oatlastword |
552 // +-------------------------+
553 // | .dynamic |
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700554 // | Elf_Dyn DT_HASH |
David Srbeckybc90fd02015-04-22 19:40:27 +0100555 // | Elf_Dyn DT_STRTAB |
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700556 // | Elf_Dyn DT_SYMTAB |
557 // | Elf_Dyn DT_SYMENT |
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700558 // | Elf_Dyn DT_STRSZ |
David Srbeckybc90fd02015-04-22 19:40:27 +0100559 // | Elf_Dyn DT_SONAME |
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700560 // | Elf_Dyn DT_NULL |
561 // +-------------------------+ (Optional)
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700562 // | .symtab | (Optional)
563 // | program symbols | (Optional)
David Srbeckybc90fd02015-04-22 19:40:27 +0100564 // +-------------------------+ (Optional)
565 // | .strtab | (Optional)
566 // | names for .symtab | (Optional)
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700567 // +-------------------------+ (Optional)
David Srbeckyb0a962c2015-04-28 19:43:56 +0100568 // | .eh_frame | (Optional)
569 // +-------------------------+ (Optional)
570 // | .eh_frame_hdr | (Optional)
571 // +-------------------------+ (Optional)
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700572 // | .debug_info | (Optional)
573 // +-------------------------+ (Optional)
574 // | .debug_abbrev | (Optional)
575 // +-------------------------+ (Optional)
David Srbecky527c9c72015-04-17 21:14:10 +0100576 // | .debug_str | (Optional)
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700577 // +-------------------------+ (Optional)
578 // | .debug_line | (Optional)
David Srbeckybc90fd02015-04-22 19:40:27 +0100579 // +-------------------------+
580 // | .shstrtab |
581 // | names of sections |
582 // +-------------------------+
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700583 // | Elf_Shdr null |
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700584 // | Elf_Shdr .dynsym |
585 // | Elf_Shdr .dynstr |
586 // | Elf_Shdr .hash |
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700587 // | Elf_Shdr .rodata |
Vladimir Marko5c42c292015-02-25 12:02:49 +0000588 // | Elf_Shdr .text |
589 // | Elf_Shdr .bss | (Optional)
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700590 // | Elf_Shdr .dynamic |
David Srbeckybc90fd02015-04-22 19:40:27 +0100591 // | Elf_Shdr .symtab | (Optional)
592 // | Elf_Shdr .strtab | (Optional)
David Srbeckyb0a962c2015-04-28 19:43:56 +0100593 // | Elf_Shdr .eh_frame | (Optional)
594 // | Elf_Shdr .eh_frame_hdr | (Optional)
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700595 // | Elf_Shdr .debug_info | (Optional)
596 // | Elf_Shdr .debug_abbrev | (Optional)
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700597 // | Elf_Shdr .debug_str | (Optional)
David Srbecky527c9c72015-04-17 21:14:10 +0100598 // | Elf_Shdr .debug_line | (Optional)
David Srbeckybc90fd02015-04-22 19:40:27 +0100599 // | Elf_Shdr .oat_patches | (Optional)
600 // | Elf_Shdr .shstrtab |
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700601 // +-------------------------+
David Srbeckybc90fd02015-04-22 19:40:27 +0100602 constexpr bool debug_logging_ = false;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700603
David Srbeckybc90fd02015-04-22 19:40:27 +0100604 // Create a list of all section which we want to write.
605 // This is the order in which they will be written.
606 std::vector<Section*> sections;
607 sections.push_back(&dynsym_);
608 sections.push_back(&dynstr_);
609 sections.push_back(&hash_);
David Srbeckybc90fd02015-04-22 19:40:27 +0100610 sections.push_back(&rodata_);
611 sections.push_back(&text_);
612 if (bss_.GetSize() != 0u) {
613 sections.push_back(&bss_);
Vladimir Marko5c42c292015-02-25 12:02:49 +0000614 }
David Srbeckybc90fd02015-04-22 19:40:27 +0100615 sections.push_back(&dynamic_);
616 if (!symtab_.IsEmpty()) {
617 sections.push_back(&symtab_);
618 sections.push_back(&strtab_);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700619 }
David Srbeckybc90fd02015-04-22 19:40:27 +0100620 for (Section* section : other_sections_) {
David Srbeckyb0a962c2015-04-28 19:43:56 +0100621 sections.push_back(section);
David Srbecky527c9c72015-04-17 21:14:10 +0100622 }
David Srbeckybc90fd02015-04-22 19:40:27 +0100623 sections.push_back(&shstrtab_);
624 for (size_t i = 0; i < sections.size(); i++) {
625 // The first section index is 1. Index 0 is reserved for NULL.
626 // Section index is used for relative symbols and for section links.
627 sections[i]->SetSectionIndex(i + 1);
628 // Add section name to .shstrtab.
629 Elf_Word name_offset = shstrtab_.AddName(sections[i]->GetName());
630 sections[i]->GetHeader()->sh_name = name_offset;
David Srbecky527c9c72015-04-17 21:14:10 +0100631 }
632
David Srbeckybc90fd02015-04-22 19:40:27 +0100633 // The running program does not have access to section headers
634 // and the loader is not supposed to use them either.
635 // The dynamic sections therefore replicates some of the layout
636 // information like the address and size of .rodata and .text.
637 // It also contains other metadata like the SONAME.
638 // The .dynamic section is found using the PT_DYNAMIC program header.
639 BuildDynsymSection();
640 BuildDynamicSection(elf_file->GetPath());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700641
David Srbeckybc90fd02015-04-22 19:40:27 +0100642 // We do not know the number of headers until the final stages of write.
643 // It is easiest to just reserve a fixed amount of space for them.
David Srbeckyb0a962c2015-04-28 19:43:56 +0100644 constexpr size_t kMaxProgramHeaders = 8;
David Srbeckybc90fd02015-04-22 19:40:27 +0100645 constexpr size_t kProgramHeadersOffset = sizeof(Elf_Ehdr);
646 constexpr size_t kProgramHeadersSize = sizeof(Elf_Phdr) * kMaxProgramHeaders;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700647
David Srbeckybc90fd02015-04-22 19:40:27 +0100648 // Layout of all sections - determine the final file offsets and addresses.
649 // This must be done after we have built all sections and know their size.
650 Elf_Off file_offset = kProgramHeadersOffset + kProgramHeadersSize;
651 Elf_Addr load_address = file_offset;
652 std::vector<Elf_Shdr> section_headers;
653 section_headers.reserve(1u + sections.size());
654 section_headers.push_back(Elf_Shdr()); // NULL at index 0.
655 for (auto* section : sections) {
656 Elf_Shdr* header = section->GetHeader();
657 Elf_Off alignment = header->sh_addralign > 0 ? header->sh_addralign : 1;
658 header->sh_size = section->GetSize();
659 header->sh_link = section->GetLink();
660 // Allocate memory for the section in the file.
661 if (header->sh_type != SHT_NOBITS) {
662 header->sh_offset = RoundUp(file_offset, alignment);
663 file_offset = header->sh_offset + header->sh_size;
664 }
665 // Allocate memory for the section during program execution.
666 if ((header->sh_flags & SHF_ALLOC) != 0) {
667 header->sh_addr = RoundUp(load_address, alignment);
668 load_address = header->sh_addr + header->sh_size;
669 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700670 if (debug_logging_) {
David Srbeckybc90fd02015-04-22 19:40:27 +0100671 LOG(INFO) << "Section " << section->GetName() << ":" << std::hex
672 << " offset=0x" << header->sh_offset
673 << " addr=0x" << header->sh_addr
674 << " size=0x" << header->sh_size;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700675 }
David Srbeckybc90fd02015-04-22 19:40:27 +0100676 // Collect section headers into continuous array for convenience.
677 section_headers.push_back(*header);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700678 }
David Srbeckybc90fd02015-04-22 19:40:27 +0100679 Elf_Off section_headers_offset = RoundUp(file_offset, sizeof(Elf_Word));
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700680
David Srbeckybc90fd02015-04-22 19:40:27 +0100681 // Create program headers now that we know the layout of the whole file.
682 // Each segment contains one or more sections which are mapped together.
683 // Not all sections are mapped during the execution of the program.
684 // PT_LOAD does the mapping. Other PT_* types allow the program to locate
685 // interesting parts of memory and their addresses overlap with PT_LOAD.
686 std::vector<Elf_Phdr> program_headers;
687 program_headers.push_back(MakeProgramHeader(PT_PHDR, PF_R,
688 kProgramHeadersOffset, kProgramHeadersSize, sizeof(Elf_Word)));
689 // Create the main LOAD R segment which spans all sections up to .rodata.
690 const Elf_Shdr* rodata = rodata_.GetHeader();
691 program_headers.push_back(MakeProgramHeader(PT_LOAD, PF_R,
692 0, rodata->sh_offset + rodata->sh_size, rodata->sh_addralign));
693 program_headers.push_back(MakeProgramHeader(PT_LOAD, PF_R | PF_X, text_));
694 if (bss_.GetHeader()->sh_size != 0u) {
695 program_headers.push_back(MakeProgramHeader(PT_LOAD, PF_R | PF_W, bss_));
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700696 }
David Srbeckybc90fd02015-04-22 19:40:27 +0100697 program_headers.push_back(MakeProgramHeader(PT_LOAD, PF_R | PF_W, dynamic_));
698 program_headers.push_back(MakeProgramHeader(PT_DYNAMIC, PF_R | PF_W, dynamic_));
David Srbeckyb0a962c2015-04-28 19:43:56 +0100699 const Section* eh_frame = FindSection(".eh_frame");
700 if (eh_frame != nullptr) {
701 program_headers.push_back(MakeProgramHeader(PT_LOAD, PF_R, *eh_frame));
702 const Section* eh_frame_hdr = FindSection(".eh_frame_hdr");
703 if (eh_frame_hdr != nullptr) {
704 // Check layout: eh_frame is before eh_frame_hdr and there is no gap.
705 CHECK_LE(eh_frame->GetHeader()->sh_offset, eh_frame_hdr->GetHeader()->sh_offset);
706 CHECK_EQ(eh_frame->GetHeader()->sh_offset + eh_frame->GetHeader()->sh_size,
707 eh_frame_hdr->GetHeader()->sh_offset);
708 // Extend the PT_LOAD of .eh_frame to include the .eh_frame_hdr as well.
709 program_headers.back().p_filesz += eh_frame_hdr->GetHeader()->sh_size;
710 program_headers.back().p_memsz += eh_frame_hdr->GetHeader()->sh_size;
711 program_headers.push_back(MakeProgramHeader(PT_GNU_EH_FRAME, PF_R, *eh_frame_hdr));
712 }
David Srbecky527c9c72015-04-17 21:14:10 +0100713 }
David Srbeckybc90fd02015-04-22 19:40:27 +0100714 CHECK_LE(program_headers.size(), kMaxProgramHeaders);
David Srbecky527c9c72015-04-17 21:14:10 +0100715
David Srbeckybc90fd02015-04-22 19:40:27 +0100716 // Create the main ELF header.
717 Elf_Ehdr elf_header = MakeElfHeader(isa_);
718 elf_header.e_phoff = kProgramHeadersOffset;
719 elf_header.e_shoff = section_headers_offset;
720 elf_header.e_phnum = program_headers.size();
721 elf_header.e_shnum = section_headers.size();
722 elf_header.e_shstrndx = shstrtab_.GetSectionIndex();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700723
David Srbeckybc90fd02015-04-22 19:40:27 +0100724 // Write all headers and section content to the file.
725 // Depending on the implementations of Section::Write, this
726 // might be just memory copies or some more elaborate operations.
727 if (!WriteArray(elf_file, &elf_header, 1)) {
728 LOG(INFO) << "Failed to write the ELF header";
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700729 return false;
730 }
David Srbeckybc90fd02015-04-22 19:40:27 +0100731 if (!WriteArray(elf_file, program_headers.data(), program_headers.size())) {
732 LOG(INFO) << "Failed to write the program headers";
733 return false;
734 }
735 for (Section* section : sections) {
736 const Elf_Shdr* header = section->GetHeader();
737 if (header->sh_type != SHT_NOBITS) {
738 if (!SeekTo(elf_file, header->sh_offset) || !section->Write(elf_file)) {
739 LOG(INFO) << "Failed to write section " << section->GetName();
740 return false;
741 }
742 Elf_Word current_offset = lseek(elf_file->Fd(), 0, SEEK_CUR);
743 CHECK_EQ(current_offset, header->sh_offset + header->sh_size)
744 << "The number of bytes written does not match GetSize()";
745 }
746 }
747 if (!SeekTo(elf_file, section_headers_offset) ||
748 !WriteArray(elf_file, section_headers.data(), section_headers.size())) {
749 LOG(INFO) << "Failed to write the section headers";
750 return false;
751 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700752 return true;
753 }
754
David Srbeckybc90fd02015-04-22 19:40:27 +0100755 // Adds the given section to the builder. It does not take ownership.
756 void RegisterSection(Section* section) {
757 other_sections_.push_back(section);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700758 }
759
David Srbeckybc90fd02015-04-22 19:40:27 +0100760 const Section* FindSection(const char* name) {
761 for (const auto* section : other_sections_) {
762 if (section->GetName() == name) {
763 return section;
David Srbecky527c9c72015-04-17 21:14:10 +0100764 }
765 }
766 return nullptr;
767 }
768
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700769 private:
David Srbeckybc90fd02015-04-22 19:40:27 +0100770 static bool SeekTo(File* elf_file, Elf_Word offset) {
771 DCHECK_LE(lseek(elf_file->Fd(), 0, SEEK_CUR), static_cast<off_t>(offset))
772 << "Seeking backwards";
773 if (static_cast<off_t>(offset) != lseek(elf_file->Fd(), offset, SEEK_SET)) {
774 PLOG(ERROR) << "Failed to seek in file " << elf_file->GetPath();
775 return false;
776 }
777 return true;
778 }
779
780 template<typename T>
781 static bool WriteArray(File* elf_file, const T* data, size_t count) {
782 DCHECK(data != nullptr);
783 if (!elf_file->WriteFully(data, count * sizeof(T))) {
784 PLOG(ERROR) << "Failed to write to file " << elf_file->GetPath();
785 return false;
786 }
787 return true;
788 }
789
790 // Helper - create segment header based on memory range.
791 static Elf_Phdr MakeProgramHeader(Elf_Word type, Elf_Word flags,
792 Elf_Off offset, Elf_Word size, Elf_Word align) {
793 Elf_Phdr phdr = Elf_Phdr();
794 phdr.p_type = type;
795 phdr.p_flags = flags;
796 phdr.p_offset = offset;
797 phdr.p_vaddr = offset;
798 phdr.p_paddr = offset;
799 phdr.p_filesz = size;
800 phdr.p_memsz = size;
801 phdr.p_align = align;
802 return phdr;
803 }
804
805 // Helper - create segment header based on section header.
806 static Elf_Phdr MakeProgramHeader(Elf_Word type, Elf_Word flags,
807 const Section& section) {
808 const Elf_Shdr* shdr = section.GetHeader();
809 // Only run-time allocated sections should be in segment headers.
810 CHECK_NE(shdr->sh_flags & SHF_ALLOC, 0u);
811 Elf_Phdr phdr = Elf_Phdr();
812 phdr.p_type = type;
813 phdr.p_flags = flags;
814 phdr.p_offset = shdr->sh_offset;
815 phdr.p_vaddr = shdr->sh_addr;
816 phdr.p_paddr = shdr->sh_addr;
817 phdr.p_filesz = shdr->sh_type != SHT_NOBITS ? shdr->sh_size : 0u;
818 phdr.p_memsz = shdr->sh_size;
819 phdr.p_align = shdr->sh_addralign;
820 return phdr;
821 }
822
823 static Elf_Ehdr MakeElfHeader(InstructionSet isa) {
824 Elf_Ehdr elf_header = Elf_Ehdr();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700825 switch (isa) {
826 case kArm:
827 // Fall through.
828 case kThumb2: {
David Srbeckybc90fd02015-04-22 19:40:27 +0100829 elf_header.e_machine = EM_ARM;
830 elf_header.e_flags = EF_ARM_EABI_VER5;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700831 break;
832 }
833 case kArm64: {
David Srbeckybc90fd02015-04-22 19:40:27 +0100834 elf_header.e_machine = EM_AARCH64;
835 elf_header.e_flags = 0;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700836 break;
837 }
838 case kX86: {
David Srbeckybc90fd02015-04-22 19:40:27 +0100839 elf_header.e_machine = EM_386;
840 elf_header.e_flags = 0;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700841 break;
842 }
843 case kX86_64: {
David Srbeckybc90fd02015-04-22 19:40:27 +0100844 elf_header.e_machine = EM_X86_64;
845 elf_header.e_flags = 0;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700846 break;
847 }
848 case kMips: {
David Srbeckybc90fd02015-04-22 19:40:27 +0100849 elf_header.e_machine = EM_MIPS;
850 elf_header.e_flags = (EF_MIPS_NOREORDER |
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700851 EF_MIPS_PIC |
852 EF_MIPS_CPIC |
853 EF_MIPS_ABI_O32 |
854 EF_MIPS_ARCH_32R2);
855 break;
856 }
Andreas Gampe57b34292015-01-14 15:45:59 -0800857 case kMips64: {
David Srbeckybc90fd02015-04-22 19:40:27 +0100858 elf_header.e_machine = EM_MIPS;
859 elf_header.e_flags = (EF_MIPS_NOREORDER |
Andreas Gampe57b34292015-01-14 15:45:59 -0800860 EF_MIPS_PIC |
861 EF_MIPS_CPIC |
862 EF_MIPS_ARCH_64R6);
863 break;
864 }
David Srbeckybc90fd02015-04-22 19:40:27 +0100865 case kNone: {
866 LOG(FATAL) << "No instruction set";
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700867 }
868 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700869
David Srbeckybc90fd02015-04-22 19:40:27 +0100870 elf_header.e_ident[EI_MAG0] = ELFMAG0;
871 elf_header.e_ident[EI_MAG1] = ELFMAG1;
872 elf_header.e_ident[EI_MAG2] = ELFMAG2;
873 elf_header.e_ident[EI_MAG3] = ELFMAG3;
874 elf_header.e_ident[EI_CLASS] = (sizeof(Elf_Addr) == sizeof(Elf32_Addr))
Tong Shen62d1ca32014-09-03 17:24:56 -0700875 ? ELFCLASS32 : ELFCLASS64;;
David Srbeckybc90fd02015-04-22 19:40:27 +0100876 elf_header.e_ident[EI_DATA] = ELFDATA2LSB;
877 elf_header.e_ident[EI_VERSION] = EV_CURRENT;
878 elf_header.e_ident[EI_OSABI] = ELFOSABI_LINUX;
879 elf_header.e_ident[EI_ABIVERSION] = 0;
880 elf_header.e_type = ET_DYN;
881 elf_header.e_version = 1;
882 elf_header.e_entry = 0;
883 elf_header.e_ehsize = sizeof(Elf_Ehdr);
884 elf_header.e_phentsize = sizeof(Elf_Phdr);
885 elf_header.e_shentsize = sizeof(Elf_Shdr);
886 elf_header.e_phoff = sizeof(Elf_Ehdr);
887 return elf_header;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700888 }
889
David Srbeckybc90fd02015-04-22 19:40:27 +0100890 void BuildDynamicSection(const std::string& elf_file_path) {
891 std::string soname(elf_file_path);
892 size_t directory_separator_pos = soname.rfind('/');
893 if (directory_separator_pos != std::string::npos) {
894 soname = soname.substr(directory_separator_pos + 1);
895 }
896 // NB: We must add the name before adding DT_STRSZ.
897 Elf_Word soname_offset = dynstr_.AddName(soname);
898
899 dynamic_.AddDynamicTag(DT_HASH, 0, &hash_);
900 dynamic_.AddDynamicTag(DT_STRTAB, 0, &dynstr_);
901 dynamic_.AddDynamicTag(DT_SYMTAB, 0, &dynsym_);
902 dynamic_.AddDynamicTag(DT_SYMENT, sizeof(Elf_Sym), nullptr);
903 dynamic_.AddDynamicTag(DT_STRSZ, dynstr_.GetSize(), nullptr);
904 dynamic_.AddDynamicTag(DT_SONAME, soname_offset, nullptr);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700905 }
906
David Srbeckybc90fd02015-04-22 19:40:27 +0100907 void BuildDynsymSection() {
908 dynsym_.AddSymbol("oatdata", &rodata_, 0, true,
909 rodata_.GetSize(), STB_GLOBAL, STT_OBJECT);
910 dynsym_.AddSymbol("oatexec", &text_, 0, true,
911 text_.GetSize(), STB_GLOBAL, STT_OBJECT);
912 dynsym_.AddSymbol("oatlastword", &text_, text_.GetSize() - 4,
913 true, 4, STB_GLOBAL, STT_OBJECT);
914 if (bss_.GetSize() != 0u) {
915 dynsym_.AddSymbol("oatbss", &bss_, 0, true,
916 bss_.GetSize(), STB_GLOBAL, STT_OBJECT);
917 dynsym_.AddSymbol("oatbsslastword", &bss_, bss_.GetSize() - 4,
918 true, 4, STB_GLOBAL, STT_OBJECT);
Vladimir Marko5c42c292015-02-25 12:02:49 +0000919 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700920 }
921
David Srbeckybc90fd02015-04-22 19:40:27 +0100922 InstructionSet isa_;
923 StrtabSection dynstr_;
924 SymtabSection dynsym_;
925 HashSection hash_;
926 OatSection rodata_;
927 OatSection text_;
928 NoBitsSection bss_;
929 DynamicSection dynamic_;
930 StrtabSection strtab_;
931 SymtabSection symtab_;
932 std::vector<Section*> other_sections_;
933 StrtabSection shstrtab_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700934
935 DISALLOW_COPY_AND_ASSIGN(ElfBuilder);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700936};
937
938} // namespace art
939
940#endif // ART_COMPILER_ELF_BUILDER_H_