blob: 2c68bb814da5559ecd4f54fafdaa78dbae85f06f [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 Marko41b175a2015-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)
60 : header_(), 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 {
83 return &header_;
David Srbecky0c5bbc12015-04-28 17:54:52 +010084 }
85
David Srbeckybc90fd02015-04-22 19:40:27 +010086 Elf_Shdr* GetHeader() {
87 return &header_;
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 Srbeckybc90fd02015-04-22 19:40:27 +0100104 Elf_Shdr header_;
David Srbecky0c5bbc12015-04-28 17:54:52 +0100105 Elf_Word section_index_;
106 const std::string name_;
David Srbeckybc90fd02015-04-22 19:40:27 +0100107 const Section* const link_;
108
109 DISALLOW_COPY_AND_ASSIGN(Section);
David Srbecky0c5bbc12015-04-28 17:54:52 +0100110 };
111
David Srbeckybc90fd02015-04-22 19:40:27 +0100112 // Writer of .dynamic section.
113 class DynamicSection FINAL : public Section {
David Srbecky0c5bbc12015-04-28 17:54:52 +0100114 public:
David Srbeckybc90fd02015-04-22 19:40:27 +0100115 void AddDynamicTag(Elf_Sword tag, Elf_Word value, const Section* section) {
116 DCHECK_NE(tag, static_cast<Elf_Sword>(DT_NULL));
117 dynamics_.push_back({tag, value, section});
David Srbecky0c5bbc12015-04-28 17:54:52 +0100118 }
119
David Srbeckybc90fd02015-04-22 19:40:27 +0100120 DynamicSection(const std::string& name, Section* link)
121 : Section(name, SHT_DYNAMIC, SHF_ALLOC,
122 link, 0, kPageSize, sizeof(Elf_Dyn)) {}
123
124 Elf_Word GetSize() const OVERRIDE {
125 return (dynamics_.size() + 1 /* DT_NULL */) * sizeof(Elf_Dyn);
David Srbecky0c5bbc12015-04-28 17:54:52 +0100126 }
127
David Srbeckybc90fd02015-04-22 19:40:27 +0100128 bool Write(File* elf_file) OVERRIDE {
129 std::vector<Elf_Dyn> buffer;
130 buffer.reserve(dynamics_.size() + 1u);
131 for (const ElfDynamicState& it : dynamics_) {
132 if (it.section_ != nullptr) {
David Srbecky0c5bbc12015-04-28 17:54:52 +0100133 // We are adding an address relative to a section.
David Srbeckybc90fd02015-04-22 19:40:27 +0100134 buffer.push_back(
135 {it.tag_, {it.value_ + it.section_->GetHeader()->sh_addr}});
David Srbecky0c5bbc12015-04-28 17:54:52 +0100136 } else {
David Srbeckybc90fd02015-04-22 19:40:27 +0100137 buffer.push_back({it.tag_, {it.value_}});
David Srbecky0c5bbc12015-04-28 17:54:52 +0100138 }
139 }
David Srbeckybc90fd02015-04-22 19:40:27 +0100140 buffer.push_back({DT_NULL, {0}});
141 return WriteArray(elf_file, buffer.data(), buffer.size());
David Srbecky0c5bbc12015-04-28 17:54:52 +0100142 }
143
144 private:
145 struct ElfDynamicState {
David Srbecky0c5bbc12015-04-28 17:54:52 +0100146 Elf_Sword tag_;
David Srbeckybc90fd02015-04-22 19:40:27 +0100147 Elf_Word value_;
148 const Section* section_;
David Srbecky0c5bbc12015-04-28 17:54:52 +0100149 };
150 std::vector<ElfDynamicState> dynamics_;
151 };
152
David Srbecky033d7452015-04-30 19:57:35 +0100153 using PatchFn = void (*)(const std::vector<uintptr_t>& patch_locations,
154 Elf_Addr buffer_address,
155 Elf_Addr base_address,
156 std::vector<uint8_t>* buffer);
157
David Srbeckybc90fd02015-04-22 19:40:27 +0100158 // Section with content based on simple memory buffer.
159 // The buffer can be optionally patched before writing.
David Srbeckybc90fd02015-04-22 19:40:27 +0100160 class RawSection FINAL : public Section {
David Srbecky0c5bbc12015-04-28 17:54:52 +0100161 public:
David Srbeckybc90fd02015-04-22 19:40:27 +0100162 RawSection(const std::string& name, Elf_Word type, Elf_Word flags,
163 const Section* link, Elf_Word info, Elf_Word align, Elf_Word entsize,
David Srbecky033d7452015-04-30 19:57:35 +0100164 PatchFn patch = nullptr, const Section* patch_base_section = nullptr)
David Srbeckybc90fd02015-04-22 19:40:27 +0100165 : Section(name, type, flags, link, info, align, entsize),
David Srbecky033d7452015-04-30 19:57:35 +0100166 patched_(false), patch_(patch), patch_base_section_(patch_base_section) {
David Srbecky0c5bbc12015-04-28 17:54:52 +0100167 }
David Srbecky0c5bbc12015-04-28 17:54:52 +0100168
David Srbeckybc90fd02015-04-22 19:40:27 +0100169 Elf_Word GetSize() const OVERRIDE {
170 return buffer_.size();
171 }
172
173 bool Write(File* elf_file) OVERRIDE {
174 if (!patch_locations_.empty()) {
David Srbecky033d7452015-04-30 19:57:35 +0100175 DCHECK(!patched_); // Do not patch twice.
176 DCHECK(patch_ != nullptr);
177 DCHECK(patch_base_section_ != nullptr);
178 patch_(patch_locations_,
179 this->GetHeader()->sh_addr,
180 patch_base_section_->GetHeader()->sh_addr,
181 &buffer_);
182 patched_ = true;
David Srbeckybc90fd02015-04-22 19:40:27 +0100183 }
184 return WriteArray(elf_file, buffer_.data(), buffer_.size());
185 }
186
187 bool IsEmpty() const {
188 return buffer_.size() == 0;
189 }
David Srbecky0c5bbc12015-04-28 17:54:52 +0100190
191 std::vector<uint8_t>* GetBuffer() {
David Srbeckybc90fd02015-04-22 19:40:27 +0100192 return &buffer_;
David Srbecky0c5bbc12015-04-28 17:54:52 +0100193 }
194
David Srbeckybc90fd02015-04-22 19:40:27 +0100195 void SetBuffer(const std::vector<uint8_t>& buffer) {
196 buffer_ = buffer;
197 }
198
199 std::vector<uintptr_t>* GetPatchLocations() {
200 return &patch_locations_;
David Srbecky0c5bbc12015-04-28 17:54:52 +0100201 }
202
203 private:
David Srbeckybc90fd02015-04-22 19:40:27 +0100204 std::vector<uint8_t> buffer_;
205 std::vector<uintptr_t> patch_locations_;
David Srbecky033d7452015-04-30 19:57:35 +0100206 bool patched_;
207 // User-provided function to do the actual patching.
208 PatchFn patch_;
209 // The section that we patch against (usually .text).
210 const Section* patch_base_section_;
David Srbecky0c5bbc12015-04-28 17:54:52 +0100211 };
212
David Srbeckybc90fd02015-04-22 19:40:27 +0100213 // Writer of .rodata section or .text section.
214 // The write is done lazily using the provided CodeOutput.
215 class OatSection FINAL : public Section {
David Srbecky0c5bbc12015-04-28 17:54:52 +0100216 public:
David Srbeckybc90fd02015-04-22 19:40:27 +0100217 OatSection(const std::string& name, Elf_Word type, Elf_Word flags,
218 const Section* link, Elf_Word info, Elf_Word align,
219 Elf_Word entsize, Elf_Word size, CodeOutput* code_output)
220 : Section(name, type, flags, link, info, align, entsize),
221 size_(size), code_output_(code_output) {
David Srbecky0c5bbc12015-04-28 17:54:52 +0100222 }
223
David Srbeckybc90fd02015-04-22 19:40:27 +0100224 Elf_Word GetSize() const OVERRIDE {
David Srbecky0c5bbc12015-04-28 17:54:52 +0100225 return size_;
226 }
227
David Srbeckybc90fd02015-04-22 19:40:27 +0100228 bool Write(File* elf_file) OVERRIDE {
229 // The BufferedOutputStream class contains the buffer as field,
230 // therefore it is too big to allocate on the stack.
231 std::unique_ptr<BufferedOutputStream> output_stream(
232 new BufferedOutputStream(new FileOutputStream(elf_file)));
233 return code_output_->Write(output_stream.get());
234 }
235
David Srbecky0c5bbc12015-04-28 17:54:52 +0100236 private:
David Srbeckybc90fd02015-04-22 19:40:27 +0100237 Elf_Word size_;
238 CodeOutput* code_output_;
239 };
240
241 // Writer of .bss section.
242 class NoBitsSection FINAL : public Section {
243 public:
244 NoBitsSection(const std::string& name, Elf_Word size)
245 : Section(name, SHT_NOBITS, SHF_ALLOC, nullptr, 0, kPageSize, 0),
246 size_(size) {
247 }
248
249 Elf_Word GetSize() const OVERRIDE {
250 return size_;
251 }
252
253 bool Write(File* elf_file ATTRIBUTE_UNUSED) OVERRIDE {
254 LOG(ERROR) << "This section should not be written to the ELF file";
255 return false;
256 }
257
258 private:
David Srbecky0c5bbc12015-04-28 17:54:52 +0100259 Elf_Word size_;
260 };
261
David Srbeckybc90fd02015-04-22 19:40:27 +0100262 // Writer of .dynstr .strtab and .shstrtab sections.
263 class StrtabSection FINAL : public Section {
264 public:
265 StrtabSection(const std::string& name, Elf_Word flags)
266 : Section(name, SHT_STRTAB, flags, nullptr, 0, 1, 1) {
267 buffer_.reserve(4 * KB);
268 // The first entry of strtab must be empty string.
269 buffer_ += '\0';
David Srbecky0c5bbc12015-04-28 17:54:52 +0100270 }
David Srbecky0c5bbc12015-04-28 17:54:52 +0100271
David Srbeckybc90fd02015-04-22 19:40:27 +0100272 Elf_Word AddName(const std::string& name) {
273 Elf_Word offset = buffer_.size();
274 buffer_ += name;
275 buffer_ += '\0';
276 return offset;
277 }
278
279 Elf_Word GetSize() const OVERRIDE {
280 return buffer_.size();
281 }
282
283 bool Write(File* elf_file) OVERRIDE {
284 return WriteArray(elf_file, buffer_.data(), buffer_.size());
285 }
286
287 private:
288 std::string buffer_;
289 };
290
291 class HashSection;
292
293 // Writer of .dynsym and .symtab sections.
294 class SymtabSection FINAL : public Section {
David Srbecky0c5bbc12015-04-28 17:54:52 +0100295 public:
296 // Add a symbol with given name to this symtab. The symbol refers to
297 // 'relative_addr' within the given section and has the given attributes.
David Srbeckybc90fd02015-04-22 19:40:27 +0100298 void AddSymbol(const std::string& name, const Section* section,
299 Elf_Addr addr, bool is_relative, Elf_Word size,
300 uint8_t binding, uint8_t type, uint8_t other = 0) {
301 CHECK(section != nullptr);
302 Elf_Word name_idx = strtab_->AddName(name);
303 symbols_.push_back({ name, section, addr, size, is_relative,
304 MakeStInfo(binding, type), other, name_idx });
David Srbecky0c5bbc12015-04-28 17:54:52 +0100305 }
306
David Srbeckybc90fd02015-04-22 19:40:27 +0100307 SymtabSection(const std::string& name, Elf_Word type, Elf_Word flags,
308 StrtabSection* strtab)
309 : Section(name, type, flags, strtab, 0, sizeof(Elf_Word), sizeof(Elf_Sym)),
310 strtab_(strtab) {
David Srbecky0c5bbc12015-04-28 17:54:52 +0100311 }
312
David Srbeckybc90fd02015-04-22 19:40:27 +0100313 bool IsEmpty() const {
314 return symbols_.empty();
315 }
David Srbecky0c5bbc12015-04-28 17:54:52 +0100316
David Srbeckybc90fd02015-04-22 19:40:27 +0100317 Elf_Word GetSize() const OVERRIDE {
318 return (1 /* NULL */ + symbols_.size()) * sizeof(Elf_Sym);
319 }
320
321 bool Write(File* elf_file) OVERRIDE {
322 std::vector<Elf_Sym> buffer;
323 buffer.reserve(1u + symbols_.size());
324 buffer.push_back(Elf_Sym()); // NULL.
325 for (const ElfSymbolState& it : symbols_) {
326 Elf_Sym sym = Elf_Sym();
327 sym.st_name = it.name_idx_;
328 if (it.is_relative_) {
329 sym.st_value = it.addr_ + it.section_->GetHeader()->sh_addr;
330 } else {
331 sym.st_value = it.addr_;
332 }
333 sym.st_size = it.size_;
334 sym.st_other = it.other_;
335 sym.st_shndx = it.section_->GetSectionIndex();
336 sym.st_info = it.info_;
337 buffer.push_back(sym);
338 }
339 return WriteArray(elf_file, buffer.data(), buffer.size());
340 }
341
342 private:
343 struct ElfSymbolState {
344 const std::string name_;
345 const Section* section_;
346 Elf_Addr addr_;
347 Elf_Word size_;
348 bool is_relative_;
349 uint8_t info_;
350 uint8_t other_;
351 Elf_Word name_idx_; // index in the strtab.
352 };
353
354 static inline constexpr uint8_t MakeStInfo(uint8_t binding, uint8_t type) {
355 return ((binding) << 4) + ((type) & 0xf);
356 }
357
358 // The symbols in the same order they will be in the symbol table.
359 std::vector<ElfSymbolState> symbols_;
360 StrtabSection* strtab_;
361
362 friend class HashSection;
363 };
364
365 // TODO: Consider removing.
366 // We use it only for the dynsym section which has only 5 symbols.
367 // We do not use it for symtab, and we probably do not have to
368 // since we use those symbols only to print backtraces.
369 class HashSection FINAL : public Section {
370 public:
371 HashSection(const std::string& name, Elf_Word flags, SymtabSection* symtab)
372 : Section(name, SHT_HASH, flags, symtab,
373 0, sizeof(Elf_Word), sizeof(Elf_Word)),
374 symtab_(symtab) {
375 }
376
377 Elf_Word GetSize() const OVERRIDE {
378 Elf_Word nbuckets = GetNumBuckets();
379 Elf_Word chain_size = symtab_->symbols_.size() + 1 /* NULL */;
380 return (2 /* header */ + nbuckets + chain_size) * sizeof(Elf_Word);
381 }
382
383 bool Write(File* const elf_file) OVERRIDE {
David Srbecky0c5bbc12015-04-28 17:54:52 +0100384 // Here is how The ELF hash table works.
385 // There are 3 arrays to worry about.
386 // * The symbol table where the symbol information is.
387 // * The bucket array which is an array of indexes into the symtab and chain.
388 // * The chain array which is also an array of indexes into the symtab and chain.
389 //
390 // Lets say the state is something like this.
391 // +--------+ +--------+ +-----------+
392 // | symtab | | bucket | | chain |
393 // | null | | 1 | | STN_UNDEF |
394 // | <sym1> | | 4 | | 2 |
395 // | <sym2> | | | | 5 |
396 // | <sym3> | | | | STN_UNDEF |
397 // | <sym4> | | | | 3 |
398 // | <sym5> | | | | STN_UNDEF |
399 // +--------+ +--------+ +-----------+
400 //
401 // The lookup process (in python psudocode) is
402 //
403 // def GetSym(name):
404 // # NB STN_UNDEF == 0
405 // indx = bucket[elfhash(name) % num_buckets]
406 // while indx != STN_UNDEF:
407 // if GetSymbolName(symtab[indx]) == name:
408 // return symtab[indx]
409 // indx = chain[indx]
410 // return SYMBOL_NOT_FOUND
411 //
412 // Between bucket and chain arrays every symtab index must be present exactly
413 // once (except for STN_UNDEF, which must be present 1 + num_bucket times).
David Srbeckybc90fd02015-04-22 19:40:27 +0100414 const auto& symbols = symtab_->symbols_;
David Srbecky0c5bbc12015-04-28 17:54:52 +0100415 // Select number of buckets.
416 // This is essentially arbitrary.
David Srbeckybc90fd02015-04-22 19:40:27 +0100417 Elf_Word nbuckets = GetNumBuckets();
418 // 1 is for the implicit NULL symbol.
419 Elf_Word chain_size = (symbols.size() + 1);
David Srbecky0c5bbc12015-04-28 17:54:52 +0100420 std::vector<Elf_Word> hash;
421 hash.push_back(nbuckets);
422 hash.push_back(chain_size);
423 uint32_t bucket_offset = hash.size();
424 uint32_t chain_offset = bucket_offset + nbuckets;
425 hash.resize(hash.size() + nbuckets + chain_size, 0);
426
427 Elf_Word* buckets = hash.data() + bucket_offset;
428 Elf_Word* chain = hash.data() + chain_offset;
429
430 // Set up the actual hash table.
David Srbeckybc90fd02015-04-22 19:40:27 +0100431 for (Elf_Word i = 0; i < symbols.size(); i++) {
David Srbecky0c5bbc12015-04-28 17:54:52 +0100432 // Add 1 since we need to have the null symbol that is not in the symbols
433 // list.
434 Elf_Word index = i + 1;
David Srbeckybc90fd02015-04-22 19:40:27 +0100435 Elf_Word hash_val = static_cast<Elf_Word>(elfhash(symbols[i].name_.c_str())) % nbuckets;
David Srbecky0c5bbc12015-04-28 17:54:52 +0100436 if (buckets[hash_val] == 0) {
437 buckets[hash_val] = index;
438 } else {
439 hash_val = buckets[hash_val];
440 CHECK_LT(hash_val, chain_size);
441 while (chain[hash_val] != 0) {
442 hash_val = chain[hash_val];
443 CHECK_LT(hash_val, chain_size);
444 }
445 chain[hash_val] = index;
446 // Check for loops. Works because if this is non-empty then there must be
447 // another cell which already contains the same symbol index as this one,
448 // which means some symbol has more then one name, which isn't allowed.
449 CHECK_EQ(chain[index], static_cast<Elf_Word>(0));
450 }
451 }
David Srbeckybc90fd02015-04-22 19:40:27 +0100452 return WriteArray(elf_file, hash.data(), hash.size());
David Srbecky0c5bbc12015-04-28 17:54:52 +0100453 }
454
455 private:
David Srbeckybc90fd02015-04-22 19:40:27 +0100456 Elf_Word GetNumBuckets() const {
457 const auto& symbols = symtab_->symbols_;
458 if (symbols.size() < 8) {
459 return 2;
460 } else if (symbols.size() < 32) {
461 return 4;
462 } else if (symbols.size() < 256) {
463 return 16;
464 } else {
465 // Have about 32 ids per bucket.
466 return RoundUp(symbols.size()/32, 2);
David Srbecky0c5bbc12015-04-28 17:54:52 +0100467 }
468 }
David Srbecky0c5bbc12015-04-28 17:54:52 +0100469
David Srbeckybc90fd02015-04-22 19:40:27 +0100470 // from bionic
471 static inline unsigned elfhash(const char *_name) {
472 const unsigned char *name = (const unsigned char *) _name;
473 unsigned h = 0, g;
David Srbecky0c5bbc12015-04-28 17:54:52 +0100474
David Srbeckybc90fd02015-04-22 19:40:27 +0100475 while (*name) {
476 h = (h << 4) + *name++;
477 g = h & 0xf0000000;
478 h ^= g;
479 h ^= g >> 24;
480 }
481 return h;
482 }
483
484 SymtabSection* symtab_;
485
486 DISALLOW_COPY_AND_ASSIGN(HashSection);
487 };
488
489 ElfBuilder(InstructionSet isa,
490 Elf_Word rodata_size, CodeOutput* rodata_writer,
491 Elf_Word text_size, CodeOutput* text_writer,
492 Elf_Word bss_size)
493 : isa_(isa),
494 dynstr_(".dynstr", SHF_ALLOC),
495 dynsym_(".dynsym", SHT_DYNSYM, SHF_ALLOC, &dynstr_),
496 hash_(".hash", SHF_ALLOC, &dynsym_),
497 rodata_(".rodata", SHT_PROGBITS, SHF_ALLOC,
498 nullptr, 0, kPageSize, 0, rodata_size, rodata_writer),
499 text_(".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR,
500 nullptr, 0, kPageSize, 0, text_size, text_writer),
501 bss_(".bss", bss_size),
502 dynamic_(".dynamic", &dynsym_),
503 strtab_(".strtab", 0),
504 symtab_(".symtab", SHT_SYMTAB, 0, &strtab_),
505 shstrtab_(".shstrtab", 0) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700506 }
507 ~ElfBuilder() {}
508
David Srbeckybc90fd02015-04-22 19:40:27 +0100509 OatSection* GetText() { return &text_; }
510 SymtabSection* GetSymtab() { return &symtab_; }
Ian Rogers0279ebb2014-10-08 17:27:48 -0700511
David Srbeckybc90fd02015-04-22 19:40:27 +0100512 bool Write(File* elf_file) {
Vladimir Marko5c42c292015-02-25 12:02:49 +0000513 // Since the .text section of an oat file contains relative references to .rodata
514 // and (optionally) .bss, we keep these 2 or 3 sections together. This creates
515 // a non-traditional layout where the .bss section is mapped independently of the
516 // .dynamic section and needs its own program header with LOAD RW.
517 //
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700518 // The basic layout of the elf file. Order may be different in final output.
519 // +-------------------------+
520 // | Elf_Ehdr |
521 // +-------------------------+
522 // | Elf_Phdr PHDR |
David Srbeckyb0a962c2015-04-28 19:43:56 +0100523 // | Elf_Phdr LOAD R | .dynsym .dynstr .hash .rodata
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700524 // | Elf_Phdr LOAD R X | .text
Vladimir Marko5c42c292015-02-25 12:02:49 +0000525 // | Elf_Phdr LOAD RW | .bss (Optional)
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700526 // | Elf_Phdr LOAD RW | .dynamic
527 // | Elf_Phdr DYNAMIC | .dynamic
David Srbeckyb0a962c2015-04-28 19:43:56 +0100528 // | Elf_Phdr LOAD R | .eh_frame .eh_frame_hdr
David Srbecky527c9c72015-04-17 21:14:10 +0100529 // | Elf_Phdr EH_FRAME R | .eh_frame_hdr
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700530 // +-------------------------+
531 // | .dynsym |
532 // | Elf_Sym STN_UNDEF |
533 // | Elf_Sym oatdata |
534 // | Elf_Sym oatexec |
535 // | Elf_Sym oatlastword |
Vladimir Marko5c42c292015-02-25 12:02:49 +0000536 // | Elf_Sym oatbss | (Optional)
537 // | Elf_Sym oatbsslastword | (Optional)
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700538 // +-------------------------+
539 // | .dynstr |
David Srbeckybc90fd02015-04-22 19:40:27 +0100540 // | names for .dynsym |
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700541 // +-------------------------+
542 // | .hash |
David Srbeckybc90fd02015-04-22 19:40:27 +0100543 // | hashtable for dynsym |
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700544 // +-------------------------+
545 // | .rodata |
546 // | oatdata..oatexec-4 |
547 // +-------------------------+
548 // | .text |
549 // | oatexec..oatlastword |
550 // +-------------------------+
551 // | .dynamic |
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700552 // | Elf_Dyn DT_HASH |
David Srbeckybc90fd02015-04-22 19:40:27 +0100553 // | Elf_Dyn DT_STRTAB |
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700554 // | Elf_Dyn DT_SYMTAB |
555 // | Elf_Dyn DT_SYMENT |
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700556 // | Elf_Dyn DT_STRSZ |
David Srbeckybc90fd02015-04-22 19:40:27 +0100557 // | Elf_Dyn DT_SONAME |
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700558 // | Elf_Dyn DT_NULL |
559 // +-------------------------+ (Optional)
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700560 // | .symtab | (Optional)
561 // | program symbols | (Optional)
David Srbeckybc90fd02015-04-22 19:40:27 +0100562 // +-------------------------+ (Optional)
563 // | .strtab | (Optional)
564 // | names for .symtab | (Optional)
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700565 // +-------------------------+ (Optional)
David Srbeckyb0a962c2015-04-28 19:43:56 +0100566 // | .eh_frame | (Optional)
567 // +-------------------------+ (Optional)
568 // | .eh_frame_hdr | (Optional)
569 // +-------------------------+ (Optional)
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700570 // | .debug_info | (Optional)
571 // +-------------------------+ (Optional)
572 // | .debug_abbrev | (Optional)
573 // +-------------------------+ (Optional)
David Srbecky527c9c72015-04-17 21:14:10 +0100574 // | .debug_str | (Optional)
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700575 // +-------------------------+ (Optional)
576 // | .debug_line | (Optional)
David Srbeckybc90fd02015-04-22 19:40:27 +0100577 // +-------------------------+
578 // | .shstrtab |
579 // | names of sections |
580 // +-------------------------+
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700581 // | Elf_Shdr null |
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700582 // | Elf_Shdr .dynsym |
583 // | Elf_Shdr .dynstr |
584 // | Elf_Shdr .hash |
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700585 // | Elf_Shdr .rodata |
Vladimir Marko5c42c292015-02-25 12:02:49 +0000586 // | Elf_Shdr .text |
587 // | Elf_Shdr .bss | (Optional)
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700588 // | Elf_Shdr .dynamic |
David Srbeckybc90fd02015-04-22 19:40:27 +0100589 // | Elf_Shdr .symtab | (Optional)
590 // | Elf_Shdr .strtab | (Optional)
David Srbeckyb0a962c2015-04-28 19:43:56 +0100591 // | Elf_Shdr .eh_frame | (Optional)
592 // | Elf_Shdr .eh_frame_hdr | (Optional)
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700593 // | Elf_Shdr .debug_info | (Optional)
594 // | Elf_Shdr .debug_abbrev | (Optional)
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700595 // | Elf_Shdr .debug_str | (Optional)
David Srbecky527c9c72015-04-17 21:14:10 +0100596 // | Elf_Shdr .debug_line | (Optional)
David Srbeckybc90fd02015-04-22 19:40:27 +0100597 // | Elf_Shdr .oat_patches | (Optional)
598 // | Elf_Shdr .shstrtab |
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700599 // +-------------------------+
David Srbeckybc90fd02015-04-22 19:40:27 +0100600 constexpr bool debug_logging_ = false;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700601
David Srbeckybc90fd02015-04-22 19:40:27 +0100602 // Create a list of all section which we want to write.
603 // This is the order in which they will be written.
604 std::vector<Section*> sections;
605 sections.push_back(&dynsym_);
606 sections.push_back(&dynstr_);
607 sections.push_back(&hash_);
David Srbeckybc90fd02015-04-22 19:40:27 +0100608 sections.push_back(&rodata_);
609 sections.push_back(&text_);
610 if (bss_.GetSize() != 0u) {
611 sections.push_back(&bss_);
Vladimir Marko5c42c292015-02-25 12:02:49 +0000612 }
David Srbeckybc90fd02015-04-22 19:40:27 +0100613 sections.push_back(&dynamic_);
614 if (!symtab_.IsEmpty()) {
615 sections.push_back(&symtab_);
616 sections.push_back(&strtab_);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700617 }
David Srbeckybc90fd02015-04-22 19:40:27 +0100618 for (Section* section : other_sections_) {
David Srbeckyb0a962c2015-04-28 19:43:56 +0100619 sections.push_back(section);
David Srbecky527c9c72015-04-17 21:14:10 +0100620 }
David Srbeckybc90fd02015-04-22 19:40:27 +0100621 sections.push_back(&shstrtab_);
622 for (size_t i = 0; i < sections.size(); i++) {
623 // The first section index is 1. Index 0 is reserved for NULL.
624 // Section index is used for relative symbols and for section links.
625 sections[i]->SetSectionIndex(i + 1);
626 // Add section name to .shstrtab.
627 Elf_Word name_offset = shstrtab_.AddName(sections[i]->GetName());
628 sections[i]->GetHeader()->sh_name = name_offset;
David Srbecky527c9c72015-04-17 21:14:10 +0100629 }
630
David Srbeckybc90fd02015-04-22 19:40:27 +0100631 // The running program does not have access to section headers
632 // and the loader is not supposed to use them either.
633 // The dynamic sections therefore replicates some of the layout
634 // information like the address and size of .rodata and .text.
635 // It also contains other metadata like the SONAME.
636 // The .dynamic section is found using the PT_DYNAMIC program header.
637 BuildDynsymSection();
638 BuildDynamicSection(elf_file->GetPath());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700639
David Srbeckybc90fd02015-04-22 19:40:27 +0100640 // We do not know the number of headers until the final stages of write.
641 // It is easiest to just reserve a fixed amount of space for them.
David Srbeckyb0a962c2015-04-28 19:43:56 +0100642 constexpr size_t kMaxProgramHeaders = 8;
David Srbeckybc90fd02015-04-22 19:40:27 +0100643 constexpr size_t kProgramHeadersOffset = sizeof(Elf_Ehdr);
644 constexpr size_t kProgramHeadersSize = sizeof(Elf_Phdr) * kMaxProgramHeaders;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700645
David Srbeckybc90fd02015-04-22 19:40:27 +0100646 // Layout of all sections - determine the final file offsets and addresses.
647 // This must be done after we have built all sections and know their size.
648 Elf_Off file_offset = kProgramHeadersOffset + kProgramHeadersSize;
649 Elf_Addr load_address = file_offset;
650 std::vector<Elf_Shdr> section_headers;
651 section_headers.reserve(1u + sections.size());
652 section_headers.push_back(Elf_Shdr()); // NULL at index 0.
653 for (auto* section : sections) {
654 Elf_Shdr* header = section->GetHeader();
655 Elf_Off alignment = header->sh_addralign > 0 ? header->sh_addralign : 1;
656 header->sh_size = section->GetSize();
657 header->sh_link = section->GetLink();
658 // Allocate memory for the section in the file.
659 if (header->sh_type != SHT_NOBITS) {
660 header->sh_offset = RoundUp(file_offset, alignment);
661 file_offset = header->sh_offset + header->sh_size;
662 }
663 // Allocate memory for the section during program execution.
664 if ((header->sh_flags & SHF_ALLOC) != 0) {
665 header->sh_addr = RoundUp(load_address, alignment);
666 load_address = header->sh_addr + header->sh_size;
667 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700668 if (debug_logging_) {
David Srbeckybc90fd02015-04-22 19:40:27 +0100669 LOG(INFO) << "Section " << section->GetName() << ":" << std::hex
670 << " offset=0x" << header->sh_offset
671 << " addr=0x" << header->sh_addr
672 << " size=0x" << header->sh_size;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700673 }
David Srbeckybc90fd02015-04-22 19:40:27 +0100674 // Collect section headers into continuous array for convenience.
675 section_headers.push_back(*header);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700676 }
David Srbeckybc90fd02015-04-22 19:40:27 +0100677 Elf_Off section_headers_offset = RoundUp(file_offset, sizeof(Elf_Word));
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700678
David Srbeckybc90fd02015-04-22 19:40:27 +0100679 // Create program headers now that we know the layout of the whole file.
680 // Each segment contains one or more sections which are mapped together.
681 // Not all sections are mapped during the execution of the program.
682 // PT_LOAD does the mapping. Other PT_* types allow the program to locate
683 // interesting parts of memory and their addresses overlap with PT_LOAD.
684 std::vector<Elf_Phdr> program_headers;
685 program_headers.push_back(MakeProgramHeader(PT_PHDR, PF_R,
686 kProgramHeadersOffset, kProgramHeadersSize, sizeof(Elf_Word)));
687 // Create the main LOAD R segment which spans all sections up to .rodata.
688 const Elf_Shdr* rodata = rodata_.GetHeader();
689 program_headers.push_back(MakeProgramHeader(PT_LOAD, PF_R,
690 0, rodata->sh_offset + rodata->sh_size, rodata->sh_addralign));
691 program_headers.push_back(MakeProgramHeader(PT_LOAD, PF_R | PF_X, text_));
692 if (bss_.GetHeader()->sh_size != 0u) {
693 program_headers.push_back(MakeProgramHeader(PT_LOAD, PF_R | PF_W, bss_));
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700694 }
David Srbeckybc90fd02015-04-22 19:40:27 +0100695 program_headers.push_back(MakeProgramHeader(PT_LOAD, PF_R | PF_W, dynamic_));
696 program_headers.push_back(MakeProgramHeader(PT_DYNAMIC, PF_R | PF_W, dynamic_));
David Srbeckyb0a962c2015-04-28 19:43:56 +0100697 const Section* eh_frame = FindSection(".eh_frame");
698 if (eh_frame != nullptr) {
699 program_headers.push_back(MakeProgramHeader(PT_LOAD, PF_R, *eh_frame));
700 const Section* eh_frame_hdr = FindSection(".eh_frame_hdr");
701 if (eh_frame_hdr != nullptr) {
702 // Check layout: eh_frame is before eh_frame_hdr and there is no gap.
703 CHECK_LE(eh_frame->GetHeader()->sh_offset, eh_frame_hdr->GetHeader()->sh_offset);
704 CHECK_EQ(eh_frame->GetHeader()->sh_offset + eh_frame->GetHeader()->sh_size,
705 eh_frame_hdr->GetHeader()->sh_offset);
706 // Extend the PT_LOAD of .eh_frame to include the .eh_frame_hdr as well.
707 program_headers.back().p_filesz += eh_frame_hdr->GetHeader()->sh_size;
708 program_headers.back().p_memsz += eh_frame_hdr->GetHeader()->sh_size;
709 program_headers.push_back(MakeProgramHeader(PT_GNU_EH_FRAME, PF_R, *eh_frame_hdr));
710 }
David Srbecky527c9c72015-04-17 21:14:10 +0100711 }
David Srbeckybc90fd02015-04-22 19:40:27 +0100712 CHECK_LE(program_headers.size(), kMaxProgramHeaders);
David Srbecky527c9c72015-04-17 21:14:10 +0100713
David Srbeckybc90fd02015-04-22 19:40:27 +0100714 // Create the main ELF header.
715 Elf_Ehdr elf_header = MakeElfHeader(isa_);
716 elf_header.e_phoff = kProgramHeadersOffset;
717 elf_header.e_shoff = section_headers_offset;
718 elf_header.e_phnum = program_headers.size();
719 elf_header.e_shnum = section_headers.size();
720 elf_header.e_shstrndx = shstrtab_.GetSectionIndex();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700721
David Srbeckybc90fd02015-04-22 19:40:27 +0100722 // Write all headers and section content to the file.
723 // Depending on the implementations of Section::Write, this
724 // might be just memory copies or some more elaborate operations.
725 if (!WriteArray(elf_file, &elf_header, 1)) {
726 LOG(INFO) << "Failed to write the ELF header";
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700727 return false;
728 }
David Srbeckybc90fd02015-04-22 19:40:27 +0100729 if (!WriteArray(elf_file, program_headers.data(), program_headers.size())) {
730 LOG(INFO) << "Failed to write the program headers";
731 return false;
732 }
733 for (Section* section : sections) {
734 const Elf_Shdr* header = section->GetHeader();
735 if (header->sh_type != SHT_NOBITS) {
736 if (!SeekTo(elf_file, header->sh_offset) || !section->Write(elf_file)) {
737 LOG(INFO) << "Failed to write section " << section->GetName();
738 return false;
739 }
740 Elf_Word current_offset = lseek(elf_file->Fd(), 0, SEEK_CUR);
741 CHECK_EQ(current_offset, header->sh_offset + header->sh_size)
742 << "The number of bytes written does not match GetSize()";
743 }
744 }
745 if (!SeekTo(elf_file, section_headers_offset) ||
746 !WriteArray(elf_file, section_headers.data(), section_headers.size())) {
747 LOG(INFO) << "Failed to write the section headers";
748 return false;
749 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700750 return true;
751 }
752
David Srbeckybc90fd02015-04-22 19:40:27 +0100753 // Adds the given section to the builder. It does not take ownership.
754 void RegisterSection(Section* section) {
755 other_sections_.push_back(section);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700756 }
757
David Srbeckybc90fd02015-04-22 19:40:27 +0100758 const Section* FindSection(const char* name) {
759 for (const auto* section : other_sections_) {
760 if (section->GetName() == name) {
761 return section;
David Srbecky527c9c72015-04-17 21:14:10 +0100762 }
763 }
764 return nullptr;
765 }
766
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700767 private:
David Srbeckybc90fd02015-04-22 19:40:27 +0100768 static bool SeekTo(File* elf_file, Elf_Word offset) {
769 DCHECK_LE(lseek(elf_file->Fd(), 0, SEEK_CUR), static_cast<off_t>(offset))
770 << "Seeking backwards";
771 if (static_cast<off_t>(offset) != lseek(elf_file->Fd(), offset, SEEK_SET)) {
772 PLOG(ERROR) << "Failed to seek in file " << elf_file->GetPath();
773 return false;
774 }
775 return true;
776 }
777
778 template<typename T>
779 static bool WriteArray(File* elf_file, const T* data, size_t count) {
780 DCHECK(data != nullptr);
781 if (!elf_file->WriteFully(data, count * sizeof(T))) {
782 PLOG(ERROR) << "Failed to write to file " << elf_file->GetPath();
783 return false;
784 }
785 return true;
786 }
787
788 // Helper - create segment header based on memory range.
789 static Elf_Phdr MakeProgramHeader(Elf_Word type, Elf_Word flags,
790 Elf_Off offset, Elf_Word size, Elf_Word align) {
791 Elf_Phdr phdr = Elf_Phdr();
792 phdr.p_type = type;
793 phdr.p_flags = flags;
794 phdr.p_offset = offset;
795 phdr.p_vaddr = offset;
796 phdr.p_paddr = offset;
797 phdr.p_filesz = size;
798 phdr.p_memsz = size;
799 phdr.p_align = align;
800 return phdr;
801 }
802
803 // Helper - create segment header based on section header.
804 static Elf_Phdr MakeProgramHeader(Elf_Word type, Elf_Word flags,
805 const Section& section) {
806 const Elf_Shdr* shdr = section.GetHeader();
807 // Only run-time allocated sections should be in segment headers.
808 CHECK_NE(shdr->sh_flags & SHF_ALLOC, 0u);
809 Elf_Phdr phdr = Elf_Phdr();
810 phdr.p_type = type;
811 phdr.p_flags = flags;
812 phdr.p_offset = shdr->sh_offset;
813 phdr.p_vaddr = shdr->sh_addr;
814 phdr.p_paddr = shdr->sh_addr;
815 phdr.p_filesz = shdr->sh_type != SHT_NOBITS ? shdr->sh_size : 0u;
816 phdr.p_memsz = shdr->sh_size;
817 phdr.p_align = shdr->sh_addralign;
818 return phdr;
819 }
820
821 static Elf_Ehdr MakeElfHeader(InstructionSet isa) {
822 Elf_Ehdr elf_header = Elf_Ehdr();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700823 switch (isa) {
824 case kArm:
825 // Fall through.
826 case kThumb2: {
David Srbeckybc90fd02015-04-22 19:40:27 +0100827 elf_header.e_machine = EM_ARM;
828 elf_header.e_flags = EF_ARM_EABI_VER5;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700829 break;
830 }
831 case kArm64: {
David Srbeckybc90fd02015-04-22 19:40:27 +0100832 elf_header.e_machine = EM_AARCH64;
833 elf_header.e_flags = 0;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700834 break;
835 }
836 case kX86: {
David Srbeckybc90fd02015-04-22 19:40:27 +0100837 elf_header.e_machine = EM_386;
838 elf_header.e_flags = 0;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700839 break;
840 }
841 case kX86_64: {
David Srbeckybc90fd02015-04-22 19:40:27 +0100842 elf_header.e_machine = EM_X86_64;
843 elf_header.e_flags = 0;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700844 break;
845 }
846 case kMips: {
David Srbeckybc90fd02015-04-22 19:40:27 +0100847 elf_header.e_machine = EM_MIPS;
848 elf_header.e_flags = (EF_MIPS_NOREORDER |
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700849 EF_MIPS_PIC |
850 EF_MIPS_CPIC |
851 EF_MIPS_ABI_O32 |
852 EF_MIPS_ARCH_32R2);
853 break;
854 }
Andreas Gampe57b34292015-01-14 15:45:59 -0800855 case kMips64: {
David Srbeckybc90fd02015-04-22 19:40:27 +0100856 elf_header.e_machine = EM_MIPS;
857 elf_header.e_flags = (EF_MIPS_NOREORDER |
Andreas Gampe57b34292015-01-14 15:45:59 -0800858 EF_MIPS_PIC |
859 EF_MIPS_CPIC |
860 EF_MIPS_ARCH_64R6);
861 break;
862 }
David Srbeckybc90fd02015-04-22 19:40:27 +0100863 case kNone: {
864 LOG(FATAL) << "No instruction set";
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700865 }
866 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700867
David Srbeckybc90fd02015-04-22 19:40:27 +0100868 elf_header.e_ident[EI_MAG0] = ELFMAG0;
869 elf_header.e_ident[EI_MAG1] = ELFMAG1;
870 elf_header.e_ident[EI_MAG2] = ELFMAG2;
871 elf_header.e_ident[EI_MAG3] = ELFMAG3;
872 elf_header.e_ident[EI_CLASS] = (sizeof(Elf_Addr) == sizeof(Elf32_Addr))
Tong Shen62d1ca32014-09-03 17:24:56 -0700873 ? ELFCLASS32 : ELFCLASS64;;
David Srbeckybc90fd02015-04-22 19:40:27 +0100874 elf_header.e_ident[EI_DATA] = ELFDATA2LSB;
875 elf_header.e_ident[EI_VERSION] = EV_CURRENT;
876 elf_header.e_ident[EI_OSABI] = ELFOSABI_LINUX;
877 elf_header.e_ident[EI_ABIVERSION] = 0;
878 elf_header.e_type = ET_DYN;
879 elf_header.e_version = 1;
880 elf_header.e_entry = 0;
881 elf_header.e_ehsize = sizeof(Elf_Ehdr);
882 elf_header.e_phentsize = sizeof(Elf_Phdr);
883 elf_header.e_shentsize = sizeof(Elf_Shdr);
884 elf_header.e_phoff = sizeof(Elf_Ehdr);
885 return elf_header;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700886 }
887
David Srbeckybc90fd02015-04-22 19:40:27 +0100888 void BuildDynamicSection(const std::string& elf_file_path) {
889 std::string soname(elf_file_path);
890 size_t directory_separator_pos = soname.rfind('/');
891 if (directory_separator_pos != std::string::npos) {
892 soname = soname.substr(directory_separator_pos + 1);
893 }
894 // NB: We must add the name before adding DT_STRSZ.
895 Elf_Word soname_offset = dynstr_.AddName(soname);
896
897 dynamic_.AddDynamicTag(DT_HASH, 0, &hash_);
898 dynamic_.AddDynamicTag(DT_STRTAB, 0, &dynstr_);
899 dynamic_.AddDynamicTag(DT_SYMTAB, 0, &dynsym_);
900 dynamic_.AddDynamicTag(DT_SYMENT, sizeof(Elf_Sym), nullptr);
901 dynamic_.AddDynamicTag(DT_STRSZ, dynstr_.GetSize(), nullptr);
902 dynamic_.AddDynamicTag(DT_SONAME, soname_offset, nullptr);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700903 }
904
David Srbeckybc90fd02015-04-22 19:40:27 +0100905 void BuildDynsymSection() {
906 dynsym_.AddSymbol("oatdata", &rodata_, 0, true,
907 rodata_.GetSize(), STB_GLOBAL, STT_OBJECT);
908 dynsym_.AddSymbol("oatexec", &text_, 0, true,
909 text_.GetSize(), STB_GLOBAL, STT_OBJECT);
910 dynsym_.AddSymbol("oatlastword", &text_, text_.GetSize() - 4,
911 true, 4, STB_GLOBAL, STT_OBJECT);
912 if (bss_.GetSize() != 0u) {
913 dynsym_.AddSymbol("oatbss", &bss_, 0, true,
914 bss_.GetSize(), STB_GLOBAL, STT_OBJECT);
915 dynsym_.AddSymbol("oatbsslastword", &bss_, bss_.GetSize() - 4,
916 true, 4, STB_GLOBAL, STT_OBJECT);
Vladimir Marko5c42c292015-02-25 12:02:49 +0000917 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700918 }
919
David Srbeckybc90fd02015-04-22 19:40:27 +0100920 InstructionSet isa_;
921 StrtabSection dynstr_;
922 SymtabSection dynsym_;
923 HashSection hash_;
924 OatSection rodata_;
925 OatSection text_;
926 NoBitsSection bss_;
927 DynamicSection dynamic_;
928 StrtabSection strtab_;
929 SymtabSection symtab_;
930 std::vector<Section*> other_sections_;
931 StrtabSection shstrtab_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700932
933 DISALLOW_COPY_AND_ASSIGN(ElfBuilder);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700934};
935
936} // namespace art
937
938#endif // ART_COMPILER_ELF_BUILDER_H_