blob: 730d780f6b8dffb68722d0d306215bdf030044de [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 Srbeckyf8980872015-05-22 17:04:47 +0100171 RawSection(const std::string& name, Elf_Word type)
172 : RawSection(name, type, 0, nullptr, 0, 1, 0, nullptr, nullptr) {
173 }
174
David Srbeckybc90fd02015-04-22 19:40:27 +0100175 Elf_Word GetSize() const OVERRIDE {
176 return buffer_.size();
177 }
178
179 bool Write(File* elf_file) OVERRIDE {
180 if (!patch_locations_.empty()) {
David Srbecky033d7452015-04-30 19:57:35 +0100181 DCHECK(!patched_); // Do not patch twice.
182 DCHECK(patch_ != nullptr);
183 DCHECK(patch_base_section_ != nullptr);
184 patch_(patch_locations_,
185 this->GetHeader()->sh_addr,
186 patch_base_section_->GetHeader()->sh_addr,
187 &buffer_);
188 patched_ = true;
David Srbeckybc90fd02015-04-22 19:40:27 +0100189 }
190 return WriteArray(elf_file, buffer_.data(), buffer_.size());
191 }
192
193 bool IsEmpty() const {
194 return buffer_.size() == 0;
195 }
David Srbecky0c5bbc12015-04-28 17:54:52 +0100196
197 std::vector<uint8_t>* GetBuffer() {
David Srbeckybc90fd02015-04-22 19:40:27 +0100198 return &buffer_;
David Srbecky0c5bbc12015-04-28 17:54:52 +0100199 }
200
David Srbeckybc90fd02015-04-22 19:40:27 +0100201 void SetBuffer(const std::vector<uint8_t>& buffer) {
202 buffer_ = buffer;
203 }
204
205 std::vector<uintptr_t>* GetPatchLocations() {
206 return &patch_locations_;
David Srbecky0c5bbc12015-04-28 17:54:52 +0100207 }
208
209 private:
David Srbeckybc90fd02015-04-22 19:40:27 +0100210 std::vector<uint8_t> buffer_;
211 std::vector<uintptr_t> patch_locations_;
David Srbecky033d7452015-04-30 19:57:35 +0100212 bool patched_;
213 // User-provided function to do the actual patching.
214 PatchFn patch_;
215 // The section that we patch against (usually .text).
216 const Section* patch_base_section_;
David Srbecky0c5bbc12015-04-28 17:54:52 +0100217 };
218
David Srbeckybc90fd02015-04-22 19:40:27 +0100219 // Writer of .rodata section or .text section.
220 // The write is done lazily using the provided CodeOutput.
221 class OatSection FINAL : public Section {
David Srbecky0c5bbc12015-04-28 17:54:52 +0100222 public:
David Srbeckybc90fd02015-04-22 19:40:27 +0100223 OatSection(const std::string& name, Elf_Word type, Elf_Word flags,
224 const Section* link, Elf_Word info, Elf_Word align,
225 Elf_Word entsize, Elf_Word size, CodeOutput* code_output)
226 : Section(name, type, flags, link, info, align, entsize),
227 size_(size), code_output_(code_output) {
David Srbecky0c5bbc12015-04-28 17:54:52 +0100228 }
229
David Srbeckybc90fd02015-04-22 19:40:27 +0100230 Elf_Word GetSize() const OVERRIDE {
David Srbecky0c5bbc12015-04-28 17:54:52 +0100231 return size_;
232 }
233
David Srbeckybc90fd02015-04-22 19:40:27 +0100234 bool Write(File* elf_file) OVERRIDE {
235 // The BufferedOutputStream class contains the buffer as field,
236 // therefore it is too big to allocate on the stack.
237 std::unique_ptr<BufferedOutputStream> output_stream(
238 new BufferedOutputStream(new FileOutputStream(elf_file)));
239 return code_output_->Write(output_stream.get());
240 }
241
David Srbecky0c5bbc12015-04-28 17:54:52 +0100242 private:
David Srbeckybc90fd02015-04-22 19:40:27 +0100243 Elf_Word size_;
244 CodeOutput* code_output_;
245 };
246
247 // Writer of .bss section.
248 class NoBitsSection FINAL : public Section {
249 public:
250 NoBitsSection(const std::string& name, Elf_Word size)
251 : Section(name, SHT_NOBITS, SHF_ALLOC, nullptr, 0, kPageSize, 0),
252 size_(size) {
253 }
254
255 Elf_Word GetSize() const OVERRIDE {
256 return size_;
257 }
258
259 bool Write(File* elf_file ATTRIBUTE_UNUSED) OVERRIDE {
260 LOG(ERROR) << "This section should not be written to the ELF file";
261 return false;
262 }
263
264 private:
David Srbecky0c5bbc12015-04-28 17:54:52 +0100265 Elf_Word size_;
266 };
267
David Srbeckybc90fd02015-04-22 19:40:27 +0100268 // Writer of .dynstr .strtab and .shstrtab sections.
269 class StrtabSection FINAL : public Section {
270 public:
271 StrtabSection(const std::string& name, Elf_Word flags)
272 : Section(name, SHT_STRTAB, flags, nullptr, 0, 1, 1) {
273 buffer_.reserve(4 * KB);
274 // The first entry of strtab must be empty string.
275 buffer_ += '\0';
David Srbecky0c5bbc12015-04-28 17:54:52 +0100276 }
David Srbecky0c5bbc12015-04-28 17:54:52 +0100277
David Srbeckybc90fd02015-04-22 19:40:27 +0100278 Elf_Word AddName(const std::string& name) {
279 Elf_Word offset = buffer_.size();
280 buffer_ += name;
281 buffer_ += '\0';
282 return offset;
283 }
284
285 Elf_Word GetSize() const OVERRIDE {
286 return buffer_.size();
287 }
288
289 bool Write(File* elf_file) OVERRIDE {
290 return WriteArray(elf_file, buffer_.data(), buffer_.size());
291 }
292
293 private:
294 std::string buffer_;
295 };
296
297 class HashSection;
298
299 // Writer of .dynsym and .symtab sections.
300 class SymtabSection FINAL : public Section {
David Srbecky0c5bbc12015-04-28 17:54:52 +0100301 public:
302 // Add a symbol with given name to this symtab. The symbol refers to
303 // 'relative_addr' within the given section and has the given attributes.
David Srbeckybc90fd02015-04-22 19:40:27 +0100304 void AddSymbol(const std::string& name, const Section* section,
305 Elf_Addr addr, bool is_relative, Elf_Word size,
306 uint8_t binding, uint8_t type, uint8_t other = 0) {
307 CHECK(section != nullptr);
308 Elf_Word name_idx = strtab_->AddName(name);
309 symbols_.push_back({ name, section, addr, size, is_relative,
310 MakeStInfo(binding, type), other, name_idx });
David Srbecky0c5bbc12015-04-28 17:54:52 +0100311 }
312
David Srbeckybc90fd02015-04-22 19:40:27 +0100313 SymtabSection(const std::string& name, Elf_Word type, Elf_Word flags,
314 StrtabSection* strtab)
315 : Section(name, type, flags, strtab, 0, sizeof(Elf_Word), sizeof(Elf_Sym)),
316 strtab_(strtab) {
David Srbecky0c5bbc12015-04-28 17:54:52 +0100317 }
318
David Srbeckybc90fd02015-04-22 19:40:27 +0100319 bool IsEmpty() const {
320 return symbols_.empty();
321 }
David Srbecky0c5bbc12015-04-28 17:54:52 +0100322
David Srbeckybc90fd02015-04-22 19:40:27 +0100323 Elf_Word GetSize() const OVERRIDE {
324 return (1 /* NULL */ + symbols_.size()) * sizeof(Elf_Sym);
325 }
326
327 bool Write(File* elf_file) OVERRIDE {
328 std::vector<Elf_Sym> buffer;
329 buffer.reserve(1u + symbols_.size());
330 buffer.push_back(Elf_Sym()); // NULL.
331 for (const ElfSymbolState& it : symbols_) {
332 Elf_Sym sym = Elf_Sym();
333 sym.st_name = it.name_idx_;
334 if (it.is_relative_) {
335 sym.st_value = it.addr_ + it.section_->GetHeader()->sh_addr;
336 } else {
337 sym.st_value = it.addr_;
338 }
339 sym.st_size = it.size_;
340 sym.st_other = it.other_;
341 sym.st_shndx = it.section_->GetSectionIndex();
342 sym.st_info = it.info_;
343 buffer.push_back(sym);
344 }
345 return WriteArray(elf_file, buffer.data(), buffer.size());
346 }
347
348 private:
349 struct ElfSymbolState {
350 const std::string name_;
351 const Section* section_;
352 Elf_Addr addr_;
353 Elf_Word size_;
354 bool is_relative_;
355 uint8_t info_;
356 uint8_t other_;
357 Elf_Word name_idx_; // index in the strtab.
358 };
359
360 static inline constexpr uint8_t MakeStInfo(uint8_t binding, uint8_t type) {
361 return ((binding) << 4) + ((type) & 0xf);
362 }
363
364 // The symbols in the same order they will be in the symbol table.
365 std::vector<ElfSymbolState> symbols_;
366 StrtabSection* strtab_;
367
368 friend class HashSection;
369 };
370
371 // TODO: Consider removing.
372 // We use it only for the dynsym section which has only 5 symbols.
373 // We do not use it for symtab, and we probably do not have to
374 // since we use those symbols only to print backtraces.
375 class HashSection FINAL : public Section {
376 public:
377 HashSection(const std::string& name, Elf_Word flags, SymtabSection* symtab)
378 : Section(name, SHT_HASH, flags, symtab,
379 0, sizeof(Elf_Word), sizeof(Elf_Word)),
380 symtab_(symtab) {
381 }
382
383 Elf_Word GetSize() const OVERRIDE {
384 Elf_Word nbuckets = GetNumBuckets();
385 Elf_Word chain_size = symtab_->symbols_.size() + 1 /* NULL */;
386 return (2 /* header */ + nbuckets + chain_size) * sizeof(Elf_Word);
387 }
388
389 bool Write(File* const elf_file) OVERRIDE {
David Srbecky0c5bbc12015-04-28 17:54:52 +0100390 // Here is how The ELF hash table works.
391 // There are 3 arrays to worry about.
392 // * The symbol table where the symbol information is.
393 // * The bucket array which is an array of indexes into the symtab and chain.
394 // * The chain array which is also an array of indexes into the symtab and chain.
395 //
396 // Lets say the state is something like this.
397 // +--------+ +--------+ +-----------+
398 // | symtab | | bucket | | chain |
399 // | null | | 1 | | STN_UNDEF |
400 // | <sym1> | | 4 | | 2 |
401 // | <sym2> | | | | 5 |
402 // | <sym3> | | | | STN_UNDEF |
403 // | <sym4> | | | | 3 |
404 // | <sym5> | | | | STN_UNDEF |
405 // +--------+ +--------+ +-----------+
406 //
407 // The lookup process (in python psudocode) is
408 //
409 // def GetSym(name):
410 // # NB STN_UNDEF == 0
411 // indx = bucket[elfhash(name) % num_buckets]
412 // while indx != STN_UNDEF:
413 // if GetSymbolName(symtab[indx]) == name:
414 // return symtab[indx]
415 // indx = chain[indx]
416 // return SYMBOL_NOT_FOUND
417 //
418 // Between bucket and chain arrays every symtab index must be present exactly
419 // once (except for STN_UNDEF, which must be present 1 + num_bucket times).
David Srbeckybc90fd02015-04-22 19:40:27 +0100420 const auto& symbols = symtab_->symbols_;
David Srbecky0c5bbc12015-04-28 17:54:52 +0100421 // Select number of buckets.
422 // This is essentially arbitrary.
David Srbeckybc90fd02015-04-22 19:40:27 +0100423 Elf_Word nbuckets = GetNumBuckets();
424 // 1 is for the implicit NULL symbol.
425 Elf_Word chain_size = (symbols.size() + 1);
David Srbecky0c5bbc12015-04-28 17:54:52 +0100426 std::vector<Elf_Word> hash;
427 hash.push_back(nbuckets);
428 hash.push_back(chain_size);
429 uint32_t bucket_offset = hash.size();
430 uint32_t chain_offset = bucket_offset + nbuckets;
431 hash.resize(hash.size() + nbuckets + chain_size, 0);
432
433 Elf_Word* buckets = hash.data() + bucket_offset;
434 Elf_Word* chain = hash.data() + chain_offset;
435
436 // Set up the actual hash table.
David Srbeckybc90fd02015-04-22 19:40:27 +0100437 for (Elf_Word i = 0; i < symbols.size(); i++) {
David Srbecky0c5bbc12015-04-28 17:54:52 +0100438 // Add 1 since we need to have the null symbol that is not in the symbols
439 // list.
440 Elf_Word index = i + 1;
David Srbeckybc90fd02015-04-22 19:40:27 +0100441 Elf_Word hash_val = static_cast<Elf_Word>(elfhash(symbols[i].name_.c_str())) % nbuckets;
David Srbecky0c5bbc12015-04-28 17:54:52 +0100442 if (buckets[hash_val] == 0) {
443 buckets[hash_val] = index;
444 } else {
445 hash_val = buckets[hash_val];
446 CHECK_LT(hash_val, chain_size);
447 while (chain[hash_val] != 0) {
448 hash_val = chain[hash_val];
449 CHECK_LT(hash_val, chain_size);
450 }
451 chain[hash_val] = index;
452 // Check for loops. Works because if this is non-empty then there must be
453 // another cell which already contains the same symbol index as this one,
454 // which means some symbol has more then one name, which isn't allowed.
455 CHECK_EQ(chain[index], static_cast<Elf_Word>(0));
456 }
457 }
David Srbeckybc90fd02015-04-22 19:40:27 +0100458 return WriteArray(elf_file, hash.data(), hash.size());
David Srbecky0c5bbc12015-04-28 17:54:52 +0100459 }
460
461 private:
David Srbeckybc90fd02015-04-22 19:40:27 +0100462 Elf_Word GetNumBuckets() const {
463 const auto& symbols = symtab_->symbols_;
464 if (symbols.size() < 8) {
465 return 2;
466 } else if (symbols.size() < 32) {
467 return 4;
468 } else if (symbols.size() < 256) {
469 return 16;
470 } else {
471 // Have about 32 ids per bucket.
472 return RoundUp(symbols.size()/32, 2);
David Srbecky0c5bbc12015-04-28 17:54:52 +0100473 }
474 }
David Srbecky0c5bbc12015-04-28 17:54:52 +0100475
David Srbeckybc90fd02015-04-22 19:40:27 +0100476 // from bionic
477 static inline unsigned elfhash(const char *_name) {
478 const unsigned char *name = (const unsigned char *) _name;
479 unsigned h = 0, g;
David Srbecky0c5bbc12015-04-28 17:54:52 +0100480
David Srbeckybc90fd02015-04-22 19:40:27 +0100481 while (*name) {
482 h = (h << 4) + *name++;
483 g = h & 0xf0000000;
484 h ^= g;
485 h ^= g >> 24;
486 }
487 return h;
488 }
489
490 SymtabSection* symtab_;
491
492 DISALLOW_COPY_AND_ASSIGN(HashSection);
493 };
494
495 ElfBuilder(InstructionSet isa,
496 Elf_Word rodata_size, CodeOutput* rodata_writer,
497 Elf_Word text_size, CodeOutput* text_writer,
498 Elf_Word bss_size)
499 : isa_(isa),
500 dynstr_(".dynstr", SHF_ALLOC),
501 dynsym_(".dynsym", SHT_DYNSYM, SHF_ALLOC, &dynstr_),
502 hash_(".hash", SHF_ALLOC, &dynsym_),
503 rodata_(".rodata", SHT_PROGBITS, SHF_ALLOC,
504 nullptr, 0, kPageSize, 0, rodata_size, rodata_writer),
505 text_(".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR,
506 nullptr, 0, kPageSize, 0, text_size, text_writer),
507 bss_(".bss", bss_size),
508 dynamic_(".dynamic", &dynsym_),
509 strtab_(".strtab", 0),
510 symtab_(".symtab", SHT_SYMTAB, 0, &strtab_),
511 shstrtab_(".shstrtab", 0) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700512 }
513 ~ElfBuilder() {}
514
David Srbeckybc90fd02015-04-22 19:40:27 +0100515 OatSection* GetText() { return &text_; }
516 SymtabSection* GetSymtab() { return &symtab_; }
Ian Rogers0279ebb2014-10-08 17:27:48 -0700517
David Srbeckybc90fd02015-04-22 19:40:27 +0100518 bool Write(File* elf_file) {
Vladimir Marko5c42c292015-02-25 12:02:49 +0000519 // Since the .text section of an oat file contains relative references to .rodata
520 // and (optionally) .bss, we keep these 2 or 3 sections together. This creates
521 // a non-traditional layout where the .bss section is mapped independently of the
522 // .dynamic section and needs its own program header with LOAD RW.
523 //
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700524 // The basic layout of the elf file. Order may be different in final output.
525 // +-------------------------+
526 // | Elf_Ehdr |
527 // +-------------------------+
528 // | Elf_Phdr PHDR |
David Srbeckyb0a962c2015-04-28 19:43:56 +0100529 // | Elf_Phdr LOAD R | .dynsym .dynstr .hash .rodata
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700530 // | Elf_Phdr LOAD R X | .text
Vladimir Marko5c42c292015-02-25 12:02:49 +0000531 // | Elf_Phdr LOAD RW | .bss (Optional)
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700532 // | Elf_Phdr LOAD RW | .dynamic
533 // | Elf_Phdr DYNAMIC | .dynamic
David Srbeckyb0a962c2015-04-28 19:43:56 +0100534 // | Elf_Phdr LOAD R | .eh_frame .eh_frame_hdr
David Srbecky527c9c72015-04-17 21:14:10 +0100535 // | Elf_Phdr EH_FRAME R | .eh_frame_hdr
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700536 // +-------------------------+
537 // | .dynsym |
538 // | Elf_Sym STN_UNDEF |
539 // | Elf_Sym oatdata |
540 // | Elf_Sym oatexec |
541 // | Elf_Sym oatlastword |
Vladimir Marko5c42c292015-02-25 12:02:49 +0000542 // | Elf_Sym oatbss | (Optional)
543 // | Elf_Sym oatbsslastword | (Optional)
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700544 // +-------------------------+
545 // | .dynstr |
David Srbeckybc90fd02015-04-22 19:40:27 +0100546 // | names for .dynsym |
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700547 // +-------------------------+
548 // | .hash |
David Srbeckybc90fd02015-04-22 19:40:27 +0100549 // | hashtable for dynsym |
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700550 // +-------------------------+
551 // | .rodata |
552 // | oatdata..oatexec-4 |
553 // +-------------------------+
554 // | .text |
555 // | oatexec..oatlastword |
556 // +-------------------------+
557 // | .dynamic |
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700558 // | Elf_Dyn DT_HASH |
David Srbeckybc90fd02015-04-22 19:40:27 +0100559 // | Elf_Dyn DT_STRTAB |
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700560 // | Elf_Dyn DT_SYMTAB |
561 // | Elf_Dyn DT_SYMENT |
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700562 // | Elf_Dyn DT_STRSZ |
David Srbeckybc90fd02015-04-22 19:40:27 +0100563 // | Elf_Dyn DT_SONAME |
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700564 // | Elf_Dyn DT_NULL |
565 // +-------------------------+ (Optional)
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700566 // | .symtab | (Optional)
567 // | program symbols | (Optional)
David Srbeckybc90fd02015-04-22 19:40:27 +0100568 // +-------------------------+ (Optional)
569 // | .strtab | (Optional)
570 // | names for .symtab | (Optional)
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700571 // +-------------------------+ (Optional)
David Srbeckyb0a962c2015-04-28 19:43:56 +0100572 // | .eh_frame | (Optional)
573 // +-------------------------+ (Optional)
574 // | .eh_frame_hdr | (Optional)
575 // +-------------------------+ (Optional)
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700576 // | .debug_info | (Optional)
577 // +-------------------------+ (Optional)
578 // | .debug_abbrev | (Optional)
579 // +-------------------------+ (Optional)
David Srbecky527c9c72015-04-17 21:14:10 +0100580 // | .debug_str | (Optional)
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700581 // +-------------------------+ (Optional)
582 // | .debug_line | (Optional)
David Srbeckybc90fd02015-04-22 19:40:27 +0100583 // +-------------------------+
584 // | .shstrtab |
585 // | names of sections |
586 // +-------------------------+
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700587 // | Elf_Shdr null |
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700588 // | Elf_Shdr .dynsym |
589 // | Elf_Shdr .dynstr |
590 // | Elf_Shdr .hash |
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700591 // | Elf_Shdr .rodata |
Vladimir Marko5c42c292015-02-25 12:02:49 +0000592 // | Elf_Shdr .text |
593 // | Elf_Shdr .bss | (Optional)
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700594 // | Elf_Shdr .dynamic |
David Srbeckybc90fd02015-04-22 19:40:27 +0100595 // | Elf_Shdr .symtab | (Optional)
596 // | Elf_Shdr .strtab | (Optional)
David Srbeckyb0a962c2015-04-28 19:43:56 +0100597 // | Elf_Shdr .eh_frame | (Optional)
598 // | Elf_Shdr .eh_frame_hdr | (Optional)
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700599 // | Elf_Shdr .debug_info | (Optional)
600 // | Elf_Shdr .debug_abbrev | (Optional)
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700601 // | Elf_Shdr .debug_str | (Optional)
David Srbecky527c9c72015-04-17 21:14:10 +0100602 // | Elf_Shdr .debug_line | (Optional)
David Srbeckybc90fd02015-04-22 19:40:27 +0100603 // | Elf_Shdr .oat_patches | (Optional)
604 // | Elf_Shdr .shstrtab |
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700605 // +-------------------------+
David Srbeckybc90fd02015-04-22 19:40:27 +0100606 constexpr bool debug_logging_ = false;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700607
David Srbeckybc90fd02015-04-22 19:40:27 +0100608 // Create a list of all section which we want to write.
609 // This is the order in which they will be written.
610 std::vector<Section*> sections;
611 sections.push_back(&dynsym_);
612 sections.push_back(&dynstr_);
613 sections.push_back(&hash_);
David Srbeckybc90fd02015-04-22 19:40:27 +0100614 sections.push_back(&rodata_);
615 sections.push_back(&text_);
616 if (bss_.GetSize() != 0u) {
617 sections.push_back(&bss_);
Vladimir Marko5c42c292015-02-25 12:02:49 +0000618 }
David Srbeckybc90fd02015-04-22 19:40:27 +0100619 sections.push_back(&dynamic_);
620 if (!symtab_.IsEmpty()) {
621 sections.push_back(&symtab_);
622 sections.push_back(&strtab_);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700623 }
David Srbeckybc90fd02015-04-22 19:40:27 +0100624 for (Section* section : other_sections_) {
David Srbeckyb0a962c2015-04-28 19:43:56 +0100625 sections.push_back(section);
David Srbecky527c9c72015-04-17 21:14:10 +0100626 }
David Srbeckybc90fd02015-04-22 19:40:27 +0100627 sections.push_back(&shstrtab_);
628 for (size_t i = 0; i < sections.size(); i++) {
629 // The first section index is 1. Index 0 is reserved for NULL.
630 // Section index is used for relative symbols and for section links.
631 sections[i]->SetSectionIndex(i + 1);
632 // Add section name to .shstrtab.
633 Elf_Word name_offset = shstrtab_.AddName(sections[i]->GetName());
634 sections[i]->GetHeader()->sh_name = name_offset;
David Srbecky527c9c72015-04-17 21:14:10 +0100635 }
636
David Srbeckybc90fd02015-04-22 19:40:27 +0100637 // The running program does not have access to section headers
638 // and the loader is not supposed to use them either.
639 // The dynamic sections therefore replicates some of the layout
640 // information like the address and size of .rodata and .text.
641 // It also contains other metadata like the SONAME.
642 // The .dynamic section is found using the PT_DYNAMIC program header.
643 BuildDynsymSection();
644 BuildDynamicSection(elf_file->GetPath());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700645
David Srbeckybc90fd02015-04-22 19:40:27 +0100646 // We do not know the number of headers until the final stages of write.
647 // It is easiest to just reserve a fixed amount of space for them.
David Srbeckyb0a962c2015-04-28 19:43:56 +0100648 constexpr size_t kMaxProgramHeaders = 8;
David Srbeckybc90fd02015-04-22 19:40:27 +0100649 constexpr size_t kProgramHeadersOffset = sizeof(Elf_Ehdr);
650 constexpr size_t kProgramHeadersSize = sizeof(Elf_Phdr) * kMaxProgramHeaders;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700651
David Srbeckybc90fd02015-04-22 19:40:27 +0100652 // Layout of all sections - determine the final file offsets and addresses.
653 // This must be done after we have built all sections and know their size.
654 Elf_Off file_offset = kProgramHeadersOffset + kProgramHeadersSize;
655 Elf_Addr load_address = file_offset;
656 std::vector<Elf_Shdr> section_headers;
657 section_headers.reserve(1u + sections.size());
658 section_headers.push_back(Elf_Shdr()); // NULL at index 0.
659 for (auto* section : sections) {
660 Elf_Shdr* header = section->GetHeader();
661 Elf_Off alignment = header->sh_addralign > 0 ? header->sh_addralign : 1;
662 header->sh_size = section->GetSize();
663 header->sh_link = section->GetLink();
664 // Allocate memory for the section in the file.
665 if (header->sh_type != SHT_NOBITS) {
666 header->sh_offset = RoundUp(file_offset, alignment);
667 file_offset = header->sh_offset + header->sh_size;
668 }
669 // Allocate memory for the section during program execution.
670 if ((header->sh_flags & SHF_ALLOC) != 0) {
671 header->sh_addr = RoundUp(load_address, alignment);
672 load_address = header->sh_addr + header->sh_size;
673 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700674 if (debug_logging_) {
David Srbeckybc90fd02015-04-22 19:40:27 +0100675 LOG(INFO) << "Section " << section->GetName() << ":" << std::hex
676 << " offset=0x" << header->sh_offset
677 << " addr=0x" << header->sh_addr
678 << " size=0x" << header->sh_size;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700679 }
David Srbeckybc90fd02015-04-22 19:40:27 +0100680 // Collect section headers into continuous array for convenience.
681 section_headers.push_back(*header);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700682 }
David Srbeckybc90fd02015-04-22 19:40:27 +0100683 Elf_Off section_headers_offset = RoundUp(file_offset, sizeof(Elf_Word));
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700684
David Srbeckybc90fd02015-04-22 19:40:27 +0100685 // Create program headers now that we know the layout of the whole file.
686 // Each segment contains one or more sections which are mapped together.
687 // Not all sections are mapped during the execution of the program.
688 // PT_LOAD does the mapping. Other PT_* types allow the program to locate
689 // interesting parts of memory and their addresses overlap with PT_LOAD.
690 std::vector<Elf_Phdr> program_headers;
691 program_headers.push_back(MakeProgramHeader(PT_PHDR, PF_R,
692 kProgramHeadersOffset, kProgramHeadersSize, sizeof(Elf_Word)));
693 // Create the main LOAD R segment which spans all sections up to .rodata.
694 const Elf_Shdr* rodata = rodata_.GetHeader();
695 program_headers.push_back(MakeProgramHeader(PT_LOAD, PF_R,
696 0, rodata->sh_offset + rodata->sh_size, rodata->sh_addralign));
697 program_headers.push_back(MakeProgramHeader(PT_LOAD, PF_R | PF_X, text_));
698 if (bss_.GetHeader()->sh_size != 0u) {
699 program_headers.push_back(MakeProgramHeader(PT_LOAD, PF_R | PF_W, bss_));
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700700 }
David Srbeckybc90fd02015-04-22 19:40:27 +0100701 program_headers.push_back(MakeProgramHeader(PT_LOAD, PF_R | PF_W, dynamic_));
702 program_headers.push_back(MakeProgramHeader(PT_DYNAMIC, PF_R | PF_W, dynamic_));
David Srbeckyb0a962c2015-04-28 19:43:56 +0100703 const Section* eh_frame = FindSection(".eh_frame");
704 if (eh_frame != nullptr) {
705 program_headers.push_back(MakeProgramHeader(PT_LOAD, PF_R, *eh_frame));
706 const Section* eh_frame_hdr = FindSection(".eh_frame_hdr");
707 if (eh_frame_hdr != nullptr) {
708 // Check layout: eh_frame is before eh_frame_hdr and there is no gap.
709 CHECK_LE(eh_frame->GetHeader()->sh_offset, eh_frame_hdr->GetHeader()->sh_offset);
710 CHECK_EQ(eh_frame->GetHeader()->sh_offset + eh_frame->GetHeader()->sh_size,
711 eh_frame_hdr->GetHeader()->sh_offset);
712 // Extend the PT_LOAD of .eh_frame to include the .eh_frame_hdr as well.
713 program_headers.back().p_filesz += eh_frame_hdr->GetHeader()->sh_size;
714 program_headers.back().p_memsz += eh_frame_hdr->GetHeader()->sh_size;
715 program_headers.push_back(MakeProgramHeader(PT_GNU_EH_FRAME, PF_R, *eh_frame_hdr));
716 }
David Srbecky527c9c72015-04-17 21:14:10 +0100717 }
David Srbeckybc90fd02015-04-22 19:40:27 +0100718 CHECK_LE(program_headers.size(), kMaxProgramHeaders);
David Srbecky527c9c72015-04-17 21:14:10 +0100719
David Srbeckybc90fd02015-04-22 19:40:27 +0100720 // Create the main ELF header.
721 Elf_Ehdr elf_header = MakeElfHeader(isa_);
722 elf_header.e_phoff = kProgramHeadersOffset;
723 elf_header.e_shoff = section_headers_offset;
724 elf_header.e_phnum = program_headers.size();
725 elf_header.e_shnum = section_headers.size();
726 elf_header.e_shstrndx = shstrtab_.GetSectionIndex();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700727
David Srbeckybc90fd02015-04-22 19:40:27 +0100728 // Write all headers and section content to the file.
729 // Depending on the implementations of Section::Write, this
730 // might be just memory copies or some more elaborate operations.
731 if (!WriteArray(elf_file, &elf_header, 1)) {
732 LOG(INFO) << "Failed to write the ELF header";
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700733 return false;
734 }
David Srbeckybc90fd02015-04-22 19:40:27 +0100735 if (!WriteArray(elf_file, program_headers.data(), program_headers.size())) {
736 LOG(INFO) << "Failed to write the program headers";
737 return false;
738 }
739 for (Section* section : sections) {
740 const Elf_Shdr* header = section->GetHeader();
741 if (header->sh_type != SHT_NOBITS) {
742 if (!SeekTo(elf_file, header->sh_offset) || !section->Write(elf_file)) {
743 LOG(INFO) << "Failed to write section " << section->GetName();
744 return false;
745 }
746 Elf_Word current_offset = lseek(elf_file->Fd(), 0, SEEK_CUR);
747 CHECK_EQ(current_offset, header->sh_offset + header->sh_size)
748 << "The number of bytes written does not match GetSize()";
749 }
750 }
751 if (!SeekTo(elf_file, section_headers_offset) ||
752 !WriteArray(elf_file, section_headers.data(), section_headers.size())) {
753 LOG(INFO) << "Failed to write the section headers";
754 return false;
755 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700756 return true;
757 }
758
David Srbeckybc90fd02015-04-22 19:40:27 +0100759 // Adds the given section to the builder. It does not take ownership.
760 void RegisterSection(Section* section) {
761 other_sections_.push_back(section);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700762 }
763
David Srbeckybc90fd02015-04-22 19:40:27 +0100764 const Section* FindSection(const char* name) {
765 for (const auto* section : other_sections_) {
766 if (section->GetName() == name) {
767 return section;
David Srbecky527c9c72015-04-17 21:14:10 +0100768 }
769 }
770 return nullptr;
771 }
772
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700773 private:
David Srbeckybc90fd02015-04-22 19:40:27 +0100774 static bool SeekTo(File* elf_file, Elf_Word offset) {
775 DCHECK_LE(lseek(elf_file->Fd(), 0, SEEK_CUR), static_cast<off_t>(offset))
776 << "Seeking backwards";
777 if (static_cast<off_t>(offset) != lseek(elf_file->Fd(), offset, SEEK_SET)) {
778 PLOG(ERROR) << "Failed to seek in file " << elf_file->GetPath();
779 return false;
780 }
781 return true;
782 }
783
784 template<typename T>
785 static bool WriteArray(File* elf_file, const T* data, size_t count) {
David Srbeckyf8980872015-05-22 17:04:47 +0100786 if (count != 0) {
787 DCHECK(data != nullptr);
788 if (!elf_file->WriteFully(data, count * sizeof(T))) {
789 PLOG(ERROR) << "Failed to write to file " << elf_file->GetPath();
790 return false;
791 }
David Srbeckybc90fd02015-04-22 19:40:27 +0100792 }
793 return true;
794 }
795
796 // Helper - create segment header based on memory range.
797 static Elf_Phdr MakeProgramHeader(Elf_Word type, Elf_Word flags,
798 Elf_Off offset, Elf_Word size, Elf_Word align) {
799 Elf_Phdr phdr = Elf_Phdr();
800 phdr.p_type = type;
801 phdr.p_flags = flags;
802 phdr.p_offset = offset;
803 phdr.p_vaddr = offset;
804 phdr.p_paddr = offset;
805 phdr.p_filesz = size;
806 phdr.p_memsz = size;
807 phdr.p_align = align;
808 return phdr;
809 }
810
811 // Helper - create segment header based on section header.
812 static Elf_Phdr MakeProgramHeader(Elf_Word type, Elf_Word flags,
813 const Section& section) {
814 const Elf_Shdr* shdr = section.GetHeader();
815 // Only run-time allocated sections should be in segment headers.
816 CHECK_NE(shdr->sh_flags & SHF_ALLOC, 0u);
817 Elf_Phdr phdr = Elf_Phdr();
818 phdr.p_type = type;
819 phdr.p_flags = flags;
820 phdr.p_offset = shdr->sh_offset;
821 phdr.p_vaddr = shdr->sh_addr;
822 phdr.p_paddr = shdr->sh_addr;
823 phdr.p_filesz = shdr->sh_type != SHT_NOBITS ? shdr->sh_size : 0u;
824 phdr.p_memsz = shdr->sh_size;
825 phdr.p_align = shdr->sh_addralign;
826 return phdr;
827 }
828
829 static Elf_Ehdr MakeElfHeader(InstructionSet isa) {
830 Elf_Ehdr elf_header = Elf_Ehdr();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700831 switch (isa) {
832 case kArm:
833 // Fall through.
834 case kThumb2: {
David Srbeckybc90fd02015-04-22 19:40:27 +0100835 elf_header.e_machine = EM_ARM;
836 elf_header.e_flags = EF_ARM_EABI_VER5;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700837 break;
838 }
839 case kArm64: {
David Srbeckybc90fd02015-04-22 19:40:27 +0100840 elf_header.e_machine = EM_AARCH64;
841 elf_header.e_flags = 0;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700842 break;
843 }
844 case kX86: {
David Srbeckybc90fd02015-04-22 19:40:27 +0100845 elf_header.e_machine = EM_386;
846 elf_header.e_flags = 0;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700847 break;
848 }
849 case kX86_64: {
David Srbeckybc90fd02015-04-22 19:40:27 +0100850 elf_header.e_machine = EM_X86_64;
851 elf_header.e_flags = 0;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700852 break;
853 }
854 case kMips: {
David Srbeckybc90fd02015-04-22 19:40:27 +0100855 elf_header.e_machine = EM_MIPS;
856 elf_header.e_flags = (EF_MIPS_NOREORDER |
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700857 EF_MIPS_PIC |
858 EF_MIPS_CPIC |
859 EF_MIPS_ABI_O32 |
860 EF_MIPS_ARCH_32R2);
861 break;
862 }
Andreas Gampe57b34292015-01-14 15:45:59 -0800863 case kMips64: {
David Srbeckybc90fd02015-04-22 19:40:27 +0100864 elf_header.e_machine = EM_MIPS;
865 elf_header.e_flags = (EF_MIPS_NOREORDER |
Andreas Gampe57b34292015-01-14 15:45:59 -0800866 EF_MIPS_PIC |
867 EF_MIPS_CPIC |
868 EF_MIPS_ARCH_64R6);
869 break;
870 }
David Srbeckybc90fd02015-04-22 19:40:27 +0100871 case kNone: {
872 LOG(FATAL) << "No instruction set";
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700873 }
874 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700875
David Srbeckybc90fd02015-04-22 19:40:27 +0100876 elf_header.e_ident[EI_MAG0] = ELFMAG0;
877 elf_header.e_ident[EI_MAG1] = ELFMAG1;
878 elf_header.e_ident[EI_MAG2] = ELFMAG2;
879 elf_header.e_ident[EI_MAG3] = ELFMAG3;
880 elf_header.e_ident[EI_CLASS] = (sizeof(Elf_Addr) == sizeof(Elf32_Addr))
Tong Shen62d1ca32014-09-03 17:24:56 -0700881 ? ELFCLASS32 : ELFCLASS64;;
David Srbeckybc90fd02015-04-22 19:40:27 +0100882 elf_header.e_ident[EI_DATA] = ELFDATA2LSB;
883 elf_header.e_ident[EI_VERSION] = EV_CURRENT;
884 elf_header.e_ident[EI_OSABI] = ELFOSABI_LINUX;
885 elf_header.e_ident[EI_ABIVERSION] = 0;
886 elf_header.e_type = ET_DYN;
887 elf_header.e_version = 1;
888 elf_header.e_entry = 0;
889 elf_header.e_ehsize = sizeof(Elf_Ehdr);
890 elf_header.e_phentsize = sizeof(Elf_Phdr);
891 elf_header.e_shentsize = sizeof(Elf_Shdr);
892 elf_header.e_phoff = sizeof(Elf_Ehdr);
893 return elf_header;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700894 }
895
David Srbeckybc90fd02015-04-22 19:40:27 +0100896 void BuildDynamicSection(const std::string& elf_file_path) {
897 std::string soname(elf_file_path);
898 size_t directory_separator_pos = soname.rfind('/');
899 if (directory_separator_pos != std::string::npos) {
900 soname = soname.substr(directory_separator_pos + 1);
901 }
902 // NB: We must add the name before adding DT_STRSZ.
903 Elf_Word soname_offset = dynstr_.AddName(soname);
904
905 dynamic_.AddDynamicTag(DT_HASH, 0, &hash_);
906 dynamic_.AddDynamicTag(DT_STRTAB, 0, &dynstr_);
907 dynamic_.AddDynamicTag(DT_SYMTAB, 0, &dynsym_);
908 dynamic_.AddDynamicTag(DT_SYMENT, sizeof(Elf_Sym), nullptr);
909 dynamic_.AddDynamicTag(DT_STRSZ, dynstr_.GetSize(), nullptr);
910 dynamic_.AddDynamicTag(DT_SONAME, soname_offset, nullptr);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700911 }
912
David Srbeckybc90fd02015-04-22 19:40:27 +0100913 void BuildDynsymSection() {
914 dynsym_.AddSymbol("oatdata", &rodata_, 0, true,
915 rodata_.GetSize(), STB_GLOBAL, STT_OBJECT);
916 dynsym_.AddSymbol("oatexec", &text_, 0, true,
917 text_.GetSize(), STB_GLOBAL, STT_OBJECT);
918 dynsym_.AddSymbol("oatlastword", &text_, text_.GetSize() - 4,
919 true, 4, STB_GLOBAL, STT_OBJECT);
920 if (bss_.GetSize() != 0u) {
921 dynsym_.AddSymbol("oatbss", &bss_, 0, true,
922 bss_.GetSize(), STB_GLOBAL, STT_OBJECT);
923 dynsym_.AddSymbol("oatbsslastword", &bss_, bss_.GetSize() - 4,
924 true, 4, STB_GLOBAL, STT_OBJECT);
Vladimir Marko5c42c292015-02-25 12:02:49 +0000925 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700926 }
927
David Srbeckybc90fd02015-04-22 19:40:27 +0100928 InstructionSet isa_;
929 StrtabSection dynstr_;
930 SymtabSection dynsym_;
931 HashSection hash_;
932 OatSection rodata_;
933 OatSection text_;
934 NoBitsSection bss_;
935 DynamicSection dynamic_;
936 StrtabSection strtab_;
937 SymtabSection symtab_;
938 std::vector<Section*> other_sections_;
939 StrtabSection shstrtab_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700940
941 DISALLOW_COPY_AND_ASSIGN(ElfBuilder);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700942};
943
944} // namespace art
945
946#endif // ART_COMPILER_ELF_BUILDER_H_