blob: 943e2a897d3105d0c0b30b02d5974889a156cb68 [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"
David Srbecky5d811202016-03-08 13:21:22 +000023#include "arch/mips/instruction_set_features_mips.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010024#include "base/bit_utils.h"
David Srbecky6d8c8f02015-10-26 10:57:09 +000025#include "base/casts.h"
David Srbeckybc90fd02015-04-22 19:40:27 +010026#include "base/unix_file/fd_file.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070027#include "elf_utils.h"
David Srbecky6d8c8f02015-10-26 10:57:09 +000028#include "leb128.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000029#include "linker/error_delaying_output_stream.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000030#include "utils/array_ref.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070031
32namespace art {
33
David Srbeckybc90fd02015-04-22 19:40:27 +010034// Writes ELF file.
David Srbecky6d8c8f02015-10-26 10:57:09 +000035//
36// The basic layout of the elf file:
37// Elf_Ehdr - The ELF header.
38// Elf_Phdr[] - Program headers for the linker.
39// .rodata - DEX files and oat metadata.
40// .text - Compiled code.
41// .bss - Zero-initialized writeable section.
42// .dynstr - Names for .dynsym.
43// .dynsym - A few oat-specific dynamic symbols.
44// .hash - Hash-table for .dynsym.
45// .dynamic - Tags which let the linker locate .dynsym.
46// .strtab - Names for .symtab.
47// .symtab - Debug symbols.
48// .eh_frame - Unwind information (CFI).
49// .eh_frame_hdr - Index of .eh_frame.
50// .debug_frame - Unwind information (CFI).
51// .debug_frame.oat_patches - Addresses for relocation.
52// .debug_info - Debug information.
53// .debug_info.oat_patches - Addresses for relocation.
54// .debug_abbrev - Decoding information for .debug_info.
55// .debug_str - Strings for .debug_info.
56// .debug_line - Line number tables.
57// .debug_line.oat_patches - Addresses for relocation.
58// .text.oat_patches - Addresses for relocation.
59// .shstrtab - Names of ELF sections.
60// Elf_Shdr[] - Section headers.
61//
62// Some section are optional (the debug sections in particular).
63//
64// We try write the section data directly into the file without much
65// in-memory buffering. This means we generally write sections based on the
66// dependency order (e.g. .dynamic points to .dynsym which points to .text).
67//
68// In the cases where we need to buffer, we write the larger section first
69// and buffer the smaller one (e.g. .strtab is bigger than .symtab).
70//
71// The debug sections are written last for easier stripping.
72//
David Srbecky533c2072015-04-22 12:20:22 +010073template <typename ElfTypes>
Andreas Gampe54fc26c2014-09-04 21:47:42 -070074class ElfBuilder FINAL {
75 public:
David Srbecky6d8c8f02015-10-26 10:57:09 +000076 static constexpr size_t kMaxProgramHeaders = 16;
David Srbecky533c2072015-04-22 12:20:22 +010077 using Elf_Addr = typename ElfTypes::Addr;
David Srbeckybc90fd02015-04-22 19:40:27 +010078 using Elf_Off = typename ElfTypes::Off;
David Srbecky533c2072015-04-22 12:20:22 +010079 using Elf_Word = typename ElfTypes::Word;
80 using Elf_Sword = typename ElfTypes::Sword;
81 using Elf_Ehdr = typename ElfTypes::Ehdr;
82 using Elf_Shdr = typename ElfTypes::Shdr;
83 using Elf_Sym = typename ElfTypes::Sym;
84 using Elf_Phdr = typename ElfTypes::Phdr;
85 using Elf_Dyn = typename ElfTypes::Dyn;
86
David Srbeckybc90fd02015-04-22 19:40:27 +010087 // Base class of all sections.
David Srbecky6d8c8f02015-10-26 10:57:09 +000088 class Section : public OutputStream {
David Srbecky0c5bbc12015-04-28 17:54:52 +010089 public:
Vladimir Marko944da602016-02-19 12:27:55 +000090 Section(ElfBuilder<ElfTypes>* owner,
91 const std::string& name,
92 Elf_Word type,
93 Elf_Word flags,
94 const Section* link,
95 Elf_Word info,
96 Elf_Word align,
97 Elf_Word entsize)
98 : OutputStream(name),
99 owner_(owner),
100 header_(),
101 section_index_(0),
102 name_(name),
103 link_(link),
104 started_(false),
105 finished_(false),
106 phdr_flags_(PF_R),
107 phdr_type_(0) {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000108 DCHECK_GE(align, 1u);
David Srbecky491a7fe2015-05-28 00:59:08 +0100109 header_.sh_type = type;
110 header_.sh_flags = flags;
111 header_.sh_info = info;
112 header_.sh_addralign = align;
113 header_.sh_entsize = entsize;
David Srbecky0c5bbc12015-04-28 17:54:52 +0100114 }
David Srbecky0c5bbc12015-04-28 17:54:52 +0100115
David Srbecky6d8c8f02015-10-26 10:57:09 +0000116 // Start writing of this section.
117 void Start() {
118 CHECK(!started_);
119 CHECK(!finished_);
120 started_ = true;
121 auto& sections = owner_->sections_;
122 // Check that the previous section is complete.
123 CHECK(sections.empty() || sections.back()->finished_);
124 // The first ELF section index is 1. Index 0 is reserved for NULL.
125 section_index_ = sections.size() + 1;
David Srbecky579942f2016-01-28 20:01:28 +0000126 // Page-align if we switch between allocated and non-allocated sections,
127 // or if we change the type of allocation (e.g. executable vs non-executable).
128 if (!sections.empty()) {
129 if (header_.sh_flags != sections.back()->header_.sh_flags) {
130 header_.sh_addralign = kPageSize;
131 }
132 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000133 // Align file position.
134 if (header_.sh_type != SHT_NOBITS) {
David Srbecky579942f2016-01-28 20:01:28 +0000135 header_.sh_offset = owner_->AlignFileOffset(header_.sh_addralign);
136 } else {
137 header_.sh_offset = 0;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000138 }
139 // Align virtual memory address.
140 if ((header_.sh_flags & SHF_ALLOC) != 0) {
David Srbecky579942f2016-01-28 20:01:28 +0000141 header_.sh_addr = owner_->AlignVirtualAddress(header_.sh_addralign);
142 } else {
143 header_.sh_addr = 0;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000144 }
David Srbecky579942f2016-01-28 20:01:28 +0000145 // Push this section on the list of written sections.
146 sections.push_back(this);
David Srbecky0c5bbc12015-04-28 17:54:52 +0100147 }
148
David Srbecky6d8c8f02015-10-26 10:57:09 +0000149 // Finish writing of this section.
150 void End() {
151 CHECK(started_);
152 CHECK(!finished_);
153 finished_ = true;
154 if (header_.sh_type == SHT_NOBITS) {
155 CHECK_GT(header_.sh_size, 0u);
156 } else {
157 // Use the current file position to determine section size.
Vladimir Marko131980f2015-12-03 18:29:23 +0000158 off_t file_offset = owner_->stream_.Seek(0, kSeekCurrent);
David Srbecky6d8c8f02015-10-26 10:57:09 +0000159 CHECK_GE(file_offset, (off_t)header_.sh_offset);
160 header_.sh_size = file_offset - header_.sh_offset;
161 }
162 if ((header_.sh_flags & SHF_ALLOC) != 0) {
163 owner_->virtual_address_ += header_.sh_size;
164 }
165 }
166
David Srbecky5cc349f2015-12-18 15:04:48 +0000167 // Returns true if the section was written to disk.
168 // (Used to check whether we have .text when writing JIT debug info)
169 bool Exists() const {
170 return finished_;
171 }
172
David Srbecky6d8c8f02015-10-26 10:57:09 +0000173 // Get the location of this section in virtual memory.
174 Elf_Addr GetAddress() const {
175 CHECK(started_);
176 return header_.sh_addr;
177 }
178
179 // Returns the size of the content of this section.
180 Elf_Word GetSize() const {
David Srbeckyb851b492015-11-11 20:19:38 +0000181 if (finished_) {
182 return header_.sh_size;
183 } else {
184 CHECK(started_);
185 CHECK_NE(header_.sh_type, (Elf_Word)SHT_NOBITS);
Vladimir Marko131980f2015-12-03 18:29:23 +0000186 return owner_->stream_.Seek(0, kSeekCurrent) - header_.sh_offset;
David Srbeckyb851b492015-11-11 20:19:38 +0000187 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000188 }
189
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000190 // Write this section as "NOBITS" section. (used for the .bss section)
191 // This means that the ELF file does not contain the initial data for this section
192 // and it will be zero-initialized when the ELF file is loaded in the running program.
193 void WriteNoBitsSection(Elf_Word size) {
194 DCHECK_NE(header_.sh_flags & SHF_ALLOC, 0u);
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000195 header_.sh_type = SHT_NOBITS;
David Srbecky579942f2016-01-28 20:01:28 +0000196 Start();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000197 header_.sh_size = size;
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000198 End();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000199 }
200
201 // This function always succeeds to simplify code.
202 // Use builder's Good() to check the actual status.
203 bool WriteFully(const void* buffer, size_t byte_count) OVERRIDE {
204 CHECK(started_);
205 CHECK(!finished_);
Vladimir Marko131980f2015-12-03 18:29:23 +0000206 return owner_->stream_.WriteFully(buffer, byte_count);
David Srbecky6d8c8f02015-10-26 10:57:09 +0000207 }
208
209 // This function always succeeds to simplify code.
210 // Use builder's Good() to check the actual status.
211 off_t Seek(off_t offset, Whence whence) OVERRIDE {
212 // Forward the seek as-is and trust the caller to use it reasonably.
Vladimir Marko131980f2015-12-03 18:29:23 +0000213 return owner_->stream_.Seek(offset, whence);
David Srbecky0c5bbc12015-04-28 17:54:52 +0100214 }
215
Vladimir Marko10c13562015-11-25 14:33:36 +0000216 // This function flushes the output and returns whether it succeeded.
217 // If there was a previous failure, this does nothing and returns false, i.e. failed.
218 bool Flush() OVERRIDE {
Vladimir Marko131980f2015-12-03 18:29:23 +0000219 return owner_->stream_.Flush();
Vladimir Marko10c13562015-11-25 14:33:36 +0000220 }
221
David Srbecky0c5bbc12015-04-28 17:54:52 +0100222 Elf_Word GetSectionIndex() const {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000223 DCHECK(started_);
David Srbeckybc90fd02015-04-22 19:40:27 +0100224 DCHECK_NE(section_index_, 0u);
David Srbecky0c5bbc12015-04-28 17:54:52 +0100225 return section_index_;
226 }
227
David Srbecky0c5bbc12015-04-28 17:54:52 +0100228 private:
David Srbecky6d8c8f02015-10-26 10:57:09 +0000229 ElfBuilder<ElfTypes>* owner_;
David Srbecky491a7fe2015-05-28 00:59:08 +0100230 Elf_Shdr header_;
David Srbecky0c5bbc12015-04-28 17:54:52 +0100231 Elf_Word section_index_;
232 const std::string name_;
David Srbeckybc90fd02015-04-22 19:40:27 +0100233 const Section* const link_;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000234 bool started_;
235 bool finished_;
236 Elf_Word phdr_flags_;
237 Elf_Word phdr_type_;
David Srbeckybc90fd02015-04-22 19:40:27 +0100238
David Srbecky6d8c8f02015-10-26 10:57:09 +0000239 friend class ElfBuilder;
David Srbecky4d247f72015-11-09 11:56:52 +0000240
241 DISALLOW_COPY_AND_ASSIGN(Section);
David Srbecky0c5bbc12015-04-28 17:54:52 +0100242 };
243
Vladimir Marko944da602016-02-19 12:27:55 +0000244 class CachedSection : public Section {
245 public:
246 CachedSection(ElfBuilder<ElfTypes>* owner,
247 const std::string& name,
248 Elf_Word type,
249 Elf_Word flags,
250 const Section* link,
251 Elf_Word info,
252 Elf_Word align,
253 Elf_Word entsize)
254 : Section(owner, name, type, flags, link, info, align, entsize), cache_() { }
255
256 Elf_Word Add(const void* data, size_t length) {
257 Elf_Word offset = cache_.size();
258 const uint8_t* d = reinterpret_cast<const uint8_t*>(data);
259 cache_.insert(cache_.end(), d, d + length);
260 return offset;
261 }
262
263 Elf_Word GetCacheSize() {
264 return cache_.size();
265 }
266
267 void Write() {
268 this->WriteFully(cache_.data(), cache_.size());
269 cache_.clear();
270 cache_.shrink_to_fit();
271 }
272
273 void WriteCachedSection() {
274 this->Start();
275 Write();
276 this->End();
277 }
278
279 private:
280 std::vector<uint8_t> cache_;
281 };
282
283 // Writer of .dynstr section.
284 class CachedStringSection FINAL : public CachedSection {
285 public:
286 CachedStringSection(ElfBuilder<ElfTypes>* owner,
287 const std::string& name,
288 Elf_Word flags,
289 Elf_Word align)
290 : CachedSection(owner,
291 name,
292 SHT_STRTAB,
293 flags,
294 /* link */ nullptr,
295 /* info */ 0,
296 align,
297 /* entsize */ 0) { }
298
299 Elf_Word Add(const std::string& name) {
300 if (CachedSection::GetCacheSize() == 0u) {
301 DCHECK(name.empty());
302 }
303 return CachedSection::Add(name.c_str(), name.length() + 1);
304 }
305 };
306
307 // Writer of .strtab and .shstrtab sections.
David Srbecky6d8c8f02015-10-26 10:57:09 +0000308 class StringSection FINAL : public Section {
David Srbeckybc90fd02015-04-22 19:40:27 +0100309 public:
Vladimir Marko944da602016-02-19 12:27:55 +0000310 StringSection(ElfBuilder<ElfTypes>* owner,
311 const std::string& name,
312 Elf_Word flags,
313 Elf_Word align)
314 : Section(owner,
315 name,
316 SHT_STRTAB,
317 flags,
318 /* link */ nullptr,
319 /* info */ 0,
320 align,
321 /* entsize */ 0),
David Srbecky6d8c8f02015-10-26 10:57:09 +0000322 current_offset_(0) {
David Srbecky0c5bbc12015-04-28 17:54:52 +0100323 }
David Srbecky0c5bbc12015-04-28 17:54:52 +0100324
David Srbecky6d8c8f02015-10-26 10:57:09 +0000325 Elf_Word Write(const std::string& name) {
326 if (current_offset_ == 0) {
327 DCHECK(name.empty());
328 }
329 Elf_Word offset = current_offset_;
330 this->WriteFully(name.c_str(), name.length() + 1);
331 current_offset_ += name.length() + 1;
David Srbeckybc90fd02015-04-22 19:40:27 +0100332 return offset;
333 }
334
David Srbeckybc90fd02015-04-22 19:40:27 +0100335 private:
David Srbecky6d8c8f02015-10-26 10:57:09 +0000336 Elf_Word current_offset_;
David Srbeckybc90fd02015-04-22 19:40:27 +0100337 };
338
David Srbeckybc90fd02015-04-22 19:40:27 +0100339 // Writer of .dynsym and .symtab sections.
Vladimir Marko944da602016-02-19 12:27:55 +0000340 class SymbolSection FINAL : public CachedSection {
David Srbecky0c5bbc12015-04-28 17:54:52 +0100341 public:
Vladimir Marko944da602016-02-19 12:27:55 +0000342 SymbolSection(ElfBuilder<ElfTypes>* owner,
343 const std::string& name,
344 Elf_Word type,
345 Elf_Word flags,
346 Section* strtab)
347 : CachedSection(owner,
348 name,
349 type,
350 flags,
351 strtab,
352 /* info */ 0,
353 sizeof(Elf_Off),
354 sizeof(Elf_Sym)) {
355 // The symbol table always has to start with NULL symbol.
356 Elf_Sym null_symbol = Elf_Sym();
357 CachedSection::Add(&null_symbol, sizeof(null_symbol));
David Srbecky6d8c8f02015-10-26 10:57:09 +0000358 }
359
360 // Buffer symbol for this section. It will be written later.
David Srbecky5cc349f2015-12-18 15:04:48 +0000361 // If the symbol's section is null, it will be considered absolute (SHN_ABS).
362 // (we use this in JIT to reference code which is stored outside the debug ELF file)
Vladimir Marko944da602016-02-19 12:27:55 +0000363 void Add(Elf_Word name,
364 const Section* section,
365 Elf_Addr addr,
366 bool is_relative,
367 Elf_Word size,
368 uint8_t binding,
369 uint8_t type,
370 uint8_t other = 0) {
371 DCHECK(section != nullptr || !is_relative);
372 Elf_Addr abs_addr = addr + (is_relative ? section->GetAddress() : 0);
373 Elf_Word section_index =
374 (section != nullptr) ? section->GetSectionIndex() : static_cast<Elf_Word>(SHN_ABS);
375 Add(name, section_index, abs_addr, size, binding, type, other);
376 }
377
378 void Add(Elf_Word name,
379 Elf_Word section_index,
380 Elf_Addr addr,
381 Elf_Word size,
382 uint8_t binding,
383 uint8_t type,
384 uint8_t other = 0) {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000385 Elf_Sym sym = Elf_Sym();
386 sym.st_name = name;
Vladimir Marko944da602016-02-19 12:27:55 +0000387 sym.st_value = addr;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000388 sym.st_size = size;
389 sym.st_other = other;
Vladimir Marko944da602016-02-19 12:27:55 +0000390 sym.st_shndx = section_index;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000391 sym.st_info = (binding << 4) + (type & 0xf);
Vladimir Marko944da602016-02-19 12:27:55 +0000392 CachedSection::Add(&sym, sizeof(sym));
David Srbecky0c5bbc12015-04-28 17:54:52 +0100393 }
David Srbeckybc90fd02015-04-22 19:40:27 +0100394 };
395
David Srbecky5d811202016-03-08 13:21:22 +0000396 ElfBuilder(InstructionSet isa, const InstructionSetFeatures* features, OutputStream* output)
Vladimir Marko131980f2015-12-03 18:29:23 +0000397 : isa_(isa),
David Srbecky5d811202016-03-08 13:21:22 +0000398 features_(features),
Vladimir Marko131980f2015-12-03 18:29:23 +0000399 stream_(output),
400 rodata_(this, ".rodata", SHT_PROGBITS, SHF_ALLOC, nullptr, 0, kPageSize, 0),
401 text_(this, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR, nullptr, 0, kPageSize, 0),
402 bss_(this, ".bss", SHT_NOBITS, SHF_ALLOC, nullptr, 0, kPageSize, 0),
403 dynstr_(this, ".dynstr", SHF_ALLOC, kPageSize),
404 dynsym_(this, ".dynsym", SHT_DYNSYM, SHF_ALLOC, &dynstr_),
405 hash_(this, ".hash", SHT_HASH, SHF_ALLOC, &dynsym_, 0, sizeof(Elf_Word), sizeof(Elf_Word)),
406 dynamic_(this, ".dynamic", SHT_DYNAMIC, SHF_ALLOC, &dynstr_, 0, kPageSize, sizeof(Elf_Dyn)),
407 eh_frame_(this, ".eh_frame", SHT_PROGBITS, SHF_ALLOC, nullptr, 0, kPageSize, 0),
408 eh_frame_hdr_(this, ".eh_frame_hdr", SHT_PROGBITS, SHF_ALLOC, nullptr, 0, 4, 0),
David Srbecky579942f2016-01-28 20:01:28 +0000409 strtab_(this, ".strtab", 0, 1),
Vladimir Marko131980f2015-12-03 18:29:23 +0000410 symtab_(this, ".symtab", SHT_SYMTAB, 0, &strtab_),
411 debug_frame_(this, ".debug_frame", SHT_PROGBITS, 0, nullptr, 0, sizeof(Elf_Addr), 0),
412 debug_info_(this, ".debug_info", SHT_PROGBITS, 0, nullptr, 0, 1, 0),
413 debug_line_(this, ".debug_line", SHT_PROGBITS, 0, nullptr, 0, 1, 0),
414 shstrtab_(this, ".shstrtab", 0, 1),
David Srbecky579942f2016-01-28 20:01:28 +0000415 started_(false),
Vladimir Marko944da602016-02-19 12:27:55 +0000416 write_program_headers_(false),
417 loaded_size_(0u),
Vladimir Marko131980f2015-12-03 18:29:23 +0000418 virtual_address_(0) {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000419 text_.phdr_flags_ = PF_R | PF_X;
420 bss_.phdr_flags_ = PF_R | PF_W;
421 dynamic_.phdr_flags_ = PF_R | PF_W;
422 dynamic_.phdr_type_ = PT_DYNAMIC;
423 eh_frame_hdr_.phdr_type_ = PT_GNU_EH_FRAME;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700424 }
425 ~ElfBuilder() {}
426
David Srbecky6d8c8f02015-10-26 10:57:09 +0000427 InstructionSet GetIsa() { return isa_; }
428 Section* GetRoData() { return &rodata_; }
429 Section* GetText() { return &text_; }
430 Section* GetBss() { return &bss_; }
431 StringSection* GetStrTab() { return &strtab_; }
432 SymbolSection* GetSymTab() { return &symtab_; }
433 Section* GetEhFrame() { return &eh_frame_; }
434 Section* GetEhFrameHdr() { return &eh_frame_hdr_; }
435 Section* GetDebugFrame() { return &debug_frame_; }
David Srbeckyb851b492015-11-11 20:19:38 +0000436 Section* GetDebugInfo() { return &debug_info_; }
437 Section* GetDebugLine() { return &debug_line_; }
Ian Rogers0279ebb2014-10-08 17:27:48 -0700438
David Srbecky6d8c8f02015-10-26 10:57:09 +0000439 // Encode patch locations as LEB128 list of deltas between consecutive addresses.
440 // (exposed publicly for tests)
Vladimir Marko10c13562015-11-25 14:33:36 +0000441 static void EncodeOatPatches(const ArrayRef<const uintptr_t>& locations,
David Srbecky6d8c8f02015-10-26 10:57:09 +0000442 std::vector<uint8_t>* buffer) {
443 buffer->reserve(buffer->size() + locations.size() * 2); // guess 2 bytes per ULEB128.
444 uintptr_t address = 0; // relative to start of section.
445 for (uintptr_t location : locations) {
446 DCHECK_GE(location, address) << "Patch locations are not in sorted order";
447 EncodeUnsignedLeb128(buffer, dchecked_integral_cast<uint32_t>(location - address));
448 address = location;
449 }
450 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700451
Vladimir Marko10c13562015-11-25 14:33:36 +0000452 void WritePatches(const char* name, const ArrayRef<const uintptr_t>& patch_locations) {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000453 std::vector<uint8_t> buffer;
Vladimir Marko10c13562015-11-25 14:33:36 +0000454 EncodeOatPatches(patch_locations, &buffer);
David Srbecky6d8c8f02015-10-26 10:57:09 +0000455 std::unique_ptr<Section> s(new Section(this, name, SHT_OAT_PATCH, 0, nullptr, 0, 1, 0));
456 s->Start();
457 s->WriteFully(buffer.data(), buffer.size());
458 s->End();
459 other_sections_.push_back(std::move(s));
460 }
David Srbecky527c9c72015-04-17 21:14:10 +0100461
David Srbecky6d8c8f02015-10-26 10:57:09 +0000462 void WriteSection(const char* name, const std::vector<uint8_t>* buffer) {
463 std::unique_ptr<Section> s(new Section(this, name, SHT_PROGBITS, 0, nullptr, 0, 1, 0));
464 s->Start();
465 s->WriteFully(buffer->data(), buffer->size());
466 s->End();
467 other_sections_.push_back(std::move(s));
468 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700469
David Srbecky579942f2016-01-28 20:01:28 +0000470 // Reserve space for ELF header and program headers.
471 // We do not know the number of headers until later, so
472 // it is easiest to just reserve a fixed amount of space.
473 // Program headers are required for loading by the linker.
474 // It is possible to omit them for ELF files used for debugging.
475 void Start(bool write_program_headers = true) {
476 int size = sizeof(Elf_Ehdr);
477 if (write_program_headers) {
478 size += sizeof(Elf_Phdr) * kMaxProgramHeaders;
479 }
Vladimir Marko131980f2015-12-03 18:29:23 +0000480 stream_.Seek(size, kSeekSet);
David Srbecky579942f2016-01-28 20:01:28 +0000481 started_ = true;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000482 virtual_address_ += size;
David Srbecky579942f2016-01-28 20:01:28 +0000483 write_program_headers_ = write_program_headers;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000484 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700485
David Srbecky6d8c8f02015-10-26 10:57:09 +0000486 void End() {
David Srbecky579942f2016-01-28 20:01:28 +0000487 DCHECK(started_);
488
Vladimir Marko944da602016-02-19 12:27:55 +0000489 // Note: loaded_size_ == 0 for tests that don't write .rodata, .text, .bss,
490 // .dynstr, dynsym, .hash and .dynamic. These tests should not read loaded_size_.
491 // TODO: Either refactor the .eh_frame creation so that it counts towards loaded_size_,
492 // or remove all support for .eh_frame. (The currently unused .eh_frame counts towards
493 // the virtual_address_ but we don't consider it for loaded_size_.)
494 CHECK(loaded_size_ == 0 || loaded_size_ == RoundUp(virtual_address_, kPageSize))
495 << loaded_size_ << " " << virtual_address_;
496
David Srbecky6d8c8f02015-10-26 10:57:09 +0000497 // Write section names and finish the section headers.
498 shstrtab_.Start();
499 shstrtab_.Write("");
500 for (auto* section : sections_) {
501 section->header_.sh_name = shstrtab_.Write(section->name_);
502 if (section->link_ != nullptr) {
503 section->header_.sh_link = section->link_->GetSectionIndex();
David Srbeckyb0a962c2015-04-28 19:43:56 +0100504 }
David Srbecky527c9c72015-04-17 21:14:10 +0100505 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000506 shstrtab_.End();
David Srbecky527c9c72015-04-17 21:14:10 +0100507
David Srbecky6d8c8f02015-10-26 10:57:09 +0000508 // Write section headers at the end of the ELF file.
509 std::vector<Elf_Shdr> shdrs;
510 shdrs.reserve(1u + sections_.size());
511 shdrs.push_back(Elf_Shdr()); // NULL at index 0.
512 for (auto* section : sections_) {
513 shdrs.push_back(section->header_);
514 }
515 Elf_Off section_headers_offset;
David Srbecky579942f2016-01-28 20:01:28 +0000516 section_headers_offset = AlignFileOffset(sizeof(Elf_Off));
Vladimir Marko131980f2015-12-03 18:29:23 +0000517 stream_.WriteFully(shdrs.data(), shdrs.size() * sizeof(shdrs[0]));
518
519 // Flush everything else before writing the program headers. This should prevent
520 // the OS from reordering writes, so that we don't end up with valid headers
521 // and partially written data if we suddenly lose power, for example.
522 stream_.Flush();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000523
David Srbecky579942f2016-01-28 20:01:28 +0000524 // The main ELF header.
David Srbeckybc90fd02015-04-22 19:40:27 +0100525 Elf_Ehdr elf_header = MakeElfHeader(isa_);
David Srbeckybc90fd02015-04-22 19:40:27 +0100526 elf_header.e_shoff = section_headers_offset;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000527 elf_header.e_shnum = shdrs.size();
David Srbeckybc90fd02015-04-22 19:40:27 +0100528 elf_header.e_shstrndx = shstrtab_.GetSectionIndex();
David Srbecky579942f2016-01-28 20:01:28 +0000529
530 // Program headers (i.e. mmap instructions).
531 std::vector<Elf_Phdr> phdrs;
532 if (write_program_headers_) {
533 phdrs = MakeProgramHeaders();
534 CHECK_LE(phdrs.size(), kMaxProgramHeaders);
535 elf_header.e_phoff = sizeof(Elf_Ehdr);
536 elf_header.e_phnum = phdrs.size();
537 }
538
Vladimir Marko131980f2015-12-03 18:29:23 +0000539 stream_.Seek(0, kSeekSet);
540 stream_.WriteFully(&elf_header, sizeof(elf_header));
541 stream_.WriteFully(phdrs.data(), phdrs.size() * sizeof(phdrs[0]));
542 stream_.Flush();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700543 }
544
David Srbecky6d8c8f02015-10-26 10:57:09 +0000545 // The running program does not have access to section headers
546 // and the loader is not supposed to use them either.
547 // The dynamic sections therefore replicates some of the layout
548 // information like the address and size of .rodata and .text.
549 // It also contains other metadata like the SONAME.
550 // The .dynamic section is found using the PT_DYNAMIC program header.
Vladimir Marko944da602016-02-19 12:27:55 +0000551 void PrepareDynamicSection(const std::string& elf_file_path,
552 Elf_Word rodata_size,
553 Elf_Word text_size,
554 Elf_Word bss_size) {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000555 std::string soname(elf_file_path);
556 size_t directory_separator_pos = soname.rfind('/');
557 if (directory_separator_pos != std::string::npos) {
558 soname = soname.substr(directory_separator_pos + 1);
559 }
560
Vladimir Marko944da602016-02-19 12:27:55 +0000561 // Calculate addresses of .text, .bss and .dynstr.
562 DCHECK_EQ(rodata_.header_.sh_addralign, static_cast<Elf_Word>(kPageSize));
563 DCHECK_EQ(text_.header_.sh_addralign, static_cast<Elf_Word>(kPageSize));
564 DCHECK_EQ(bss_.header_.sh_addralign, static_cast<Elf_Word>(kPageSize));
565 DCHECK_EQ(dynstr_.header_.sh_addralign, static_cast<Elf_Word>(kPageSize));
566 Elf_Word rodata_address = rodata_.GetAddress();
567 Elf_Word text_address = RoundUp(rodata_address + rodata_size, kPageSize);
568 Elf_Word bss_address = RoundUp(text_address + text_size, kPageSize);
569 Elf_Word dynstr_address = RoundUp(bss_address + bss_size, kPageSize);
Vladimir Marko45724f92016-02-17 17:46:10 +0000570
Vladimir Marko944da602016-02-19 12:27:55 +0000571 // Cache .dynstr, .dynsym and .hash data.
572 dynstr_.Add(""); // dynstr should start with empty string.
573 Elf_Word rodata_index = rodata_.GetSectionIndex();
574 Elf_Word oatdata = dynstr_.Add("oatdata");
575 dynsym_.Add(oatdata, rodata_index, rodata_address, rodata_size, STB_GLOBAL, STT_OBJECT);
576 if (text_size != 0u) {
577 Elf_Word text_index = rodata_index + 1u;
578 Elf_Word oatexec = dynstr_.Add("oatexec");
579 dynsym_.Add(oatexec, text_index, text_address, text_size, STB_GLOBAL, STT_OBJECT);
580 Elf_Word oatlastword = dynstr_.Add("oatlastword");
581 Elf_Word oatlastword_address = text_address + text_size - 4;
582 dynsym_.Add(oatlastword, text_index, oatlastword_address, 4, STB_GLOBAL, STT_OBJECT);
583 } else if (rodata_size != 0) {
584 // rodata_ can be size 0 for dwarf_test.
585 Elf_Word oatlastword = dynstr_.Add("oatlastword");
586 Elf_Word oatlastword_address = rodata_address + rodata_size - 4;
587 dynsym_.Add(oatlastword, rodata_index, oatlastword_address, 4, STB_GLOBAL, STT_OBJECT);
588 }
589 if (bss_size != 0u) {
590 Elf_Word bss_index = rodata_index + 1u + (text_size != 0 ? 1u : 0u);
591 Elf_Word oatbss = dynstr_.Add("oatbss");
592 dynsym_.Add(oatbss, bss_index, bss_address, bss_size, STB_GLOBAL, STT_OBJECT);
593 Elf_Word oatbsslastword = dynstr_.Add("oatbsslastword");
594 Elf_Word bsslastword_address = bss_address + bss_size - 4;
595 dynsym_.Add(oatbsslastword, bss_index, bsslastword_address, 4, STB_GLOBAL, STT_OBJECT);
596 }
597 Elf_Word soname_offset = dynstr_.Add(soname);
David Srbecky6d8c8f02015-10-26 10:57:09 +0000598
599 // We do not really need a hash-table since there is so few entries.
600 // However, the hash-table is the only way the linker can actually
601 // determine the number of symbols in .dynsym so it is required.
Vladimir Marko944da602016-02-19 12:27:55 +0000602 int count = dynsym_.GetCacheSize() / sizeof(Elf_Sym); // Includes NULL.
David Srbecky6d8c8f02015-10-26 10:57:09 +0000603 std::vector<Elf_Word> hash;
604 hash.push_back(1); // Number of buckets.
605 hash.push_back(count); // Number of chains.
606 // Buckets. Having just one makes it linear search.
607 hash.push_back(1); // Point to first non-NULL symbol.
608 // Chains. This creates linked list of symbols.
609 hash.push_back(0); // Dummy entry for the NULL symbol.
610 for (int i = 1; i < count - 1; i++) {
611 hash.push_back(i + 1); // Each symbol points to the next one.
612 }
613 hash.push_back(0); // Last symbol terminates the chain.
Vladimir Marko944da602016-02-19 12:27:55 +0000614 hash_.Add(hash.data(), hash.size() * sizeof(hash[0]));
David Srbecky6d8c8f02015-10-26 10:57:09 +0000615
Vladimir Marko944da602016-02-19 12:27:55 +0000616 // Calculate addresses of .dynsym, .hash and .dynamic.
617 DCHECK_EQ(dynstr_.header_.sh_flags, dynsym_.header_.sh_flags);
618 DCHECK_EQ(dynsym_.header_.sh_flags, hash_.header_.sh_flags);
619 Elf_Word dynsym_address =
620 RoundUp(dynstr_address + dynstr_.GetCacheSize(), dynsym_.header_.sh_addralign);
621 Elf_Word hash_address =
622 RoundUp(dynsym_address + dynsym_.GetCacheSize(), hash_.header_.sh_addralign);
623 DCHECK_EQ(dynamic_.header_.sh_addralign, static_cast<Elf_Word>(kPageSize));
624 Elf_Word dynamic_address = RoundUp(hash_address + dynsym_.GetCacheSize(), kPageSize);
625
David Srbecky6d8c8f02015-10-26 10:57:09 +0000626 Elf_Dyn dyns[] = {
Vladimir Marko944da602016-02-19 12:27:55 +0000627 { DT_HASH, { hash_address } },
628 { DT_STRTAB, { dynstr_address } },
629 { DT_SYMTAB, { dynsym_address } },
David Srbecky6d8c8f02015-10-26 10:57:09 +0000630 { DT_SYMENT, { sizeof(Elf_Sym) } },
Vladimir Marko944da602016-02-19 12:27:55 +0000631 { DT_STRSZ, { dynstr_.GetCacheSize() } },
David Srbecky6d8c8f02015-10-26 10:57:09 +0000632 { DT_SONAME, { soname_offset } },
633 { DT_NULL, { 0 } },
634 };
Vladimir Marko944da602016-02-19 12:27:55 +0000635 dynamic_.Add(&dyns, sizeof(dyns));
636
637 loaded_size_ = RoundUp(dynamic_address + dynamic_.GetCacheSize(), kPageSize);
638 }
639
640 void WriteDynamicSection() {
641 dynstr_.WriteCachedSection();
642 dynsym_.WriteCachedSection();
643 hash_.WriteCachedSection();
644 dynamic_.WriteCachedSection();
645
646 CHECK_EQ(loaded_size_, RoundUp(dynamic_.GetAddress() + dynamic_.GetSize(), kPageSize));
647 }
648
649 Elf_Word GetLoadedSize() {
650 CHECK_NE(loaded_size_, 0u);
651 return loaded_size_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700652 }
653
David Srbecky6d8c8f02015-10-26 10:57:09 +0000654 // Returns true if all writes and seeks on the output stream succeeded.
655 bool Good() {
Vladimir Marko131980f2015-12-03 18:29:23 +0000656 return stream_.Good();
657 }
658
659 // Returns the builder's internal stream.
660 OutputStream* GetStream() {
661 return &stream_;
David Srbecky527c9c72015-04-17 21:14:10 +0100662 }
663
David Srbecky579942f2016-01-28 20:01:28 +0000664 off_t AlignFileOffset(size_t alignment) {
665 return stream_.Seek(RoundUp(stream_.Seek(0, kSeekCurrent), alignment), kSeekSet);
666 }
667
668 Elf_Addr AlignVirtualAddress(size_t alignment) {
669 return virtual_address_ = RoundUp(virtual_address_, alignment);
670 }
671
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700672 private:
David Srbeckybc90fd02015-04-22 19:40:27 +0100673 static Elf_Ehdr MakeElfHeader(InstructionSet isa) {
674 Elf_Ehdr elf_header = Elf_Ehdr();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700675 switch (isa) {
676 case kArm:
677 // Fall through.
678 case kThumb2: {
David Srbeckybc90fd02015-04-22 19:40:27 +0100679 elf_header.e_machine = EM_ARM;
680 elf_header.e_flags = EF_ARM_EABI_VER5;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700681 break;
682 }
683 case kArm64: {
David Srbeckybc90fd02015-04-22 19:40:27 +0100684 elf_header.e_machine = EM_AARCH64;
685 elf_header.e_flags = 0;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700686 break;
687 }
688 case kX86: {
David Srbeckybc90fd02015-04-22 19:40:27 +0100689 elf_header.e_machine = EM_386;
690 elf_header.e_flags = 0;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700691 break;
692 }
693 case kX86_64: {
David Srbeckybc90fd02015-04-22 19:40:27 +0100694 elf_header.e_machine = EM_X86_64;
695 elf_header.e_flags = 0;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700696 break;
697 }
698 case kMips: {
David Srbeckybc90fd02015-04-22 19:40:27 +0100699 elf_header.e_machine = EM_MIPS;
700 elf_header.e_flags = (EF_MIPS_NOREORDER |
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700701 EF_MIPS_PIC |
702 EF_MIPS_CPIC |
703 EF_MIPS_ABI_O32 |
704 EF_MIPS_ARCH_32R2);
705 break;
706 }
Andreas Gampe57b34292015-01-14 15:45:59 -0800707 case kMips64: {
David Srbeckybc90fd02015-04-22 19:40:27 +0100708 elf_header.e_machine = EM_MIPS;
709 elf_header.e_flags = (EF_MIPS_NOREORDER |
Andreas Gampe57b34292015-01-14 15:45:59 -0800710 EF_MIPS_PIC |
711 EF_MIPS_CPIC |
712 EF_MIPS_ARCH_64R6);
713 break;
714 }
David Srbeckybc90fd02015-04-22 19:40:27 +0100715 case kNone: {
716 LOG(FATAL) << "No instruction set";
David Srbecky6d8c8f02015-10-26 10:57:09 +0000717 break;
718 }
719 default: {
720 LOG(FATAL) << "Unknown instruction set " << isa;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700721 }
722 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700723
David Srbeckybc90fd02015-04-22 19:40:27 +0100724 elf_header.e_ident[EI_MAG0] = ELFMAG0;
725 elf_header.e_ident[EI_MAG1] = ELFMAG1;
726 elf_header.e_ident[EI_MAG2] = ELFMAG2;
727 elf_header.e_ident[EI_MAG3] = ELFMAG3;
728 elf_header.e_ident[EI_CLASS] = (sizeof(Elf_Addr) == sizeof(Elf32_Addr))
Tong Shen62d1ca32014-09-03 17:24:56 -0700729 ? ELFCLASS32 : ELFCLASS64;;
David Srbeckybc90fd02015-04-22 19:40:27 +0100730 elf_header.e_ident[EI_DATA] = ELFDATA2LSB;
731 elf_header.e_ident[EI_VERSION] = EV_CURRENT;
732 elf_header.e_ident[EI_OSABI] = ELFOSABI_LINUX;
733 elf_header.e_ident[EI_ABIVERSION] = 0;
734 elf_header.e_type = ET_DYN;
735 elf_header.e_version = 1;
736 elf_header.e_entry = 0;
737 elf_header.e_ehsize = sizeof(Elf_Ehdr);
738 elf_header.e_phentsize = sizeof(Elf_Phdr);
739 elf_header.e_shentsize = sizeof(Elf_Shdr);
740 elf_header.e_phoff = sizeof(Elf_Ehdr);
741 return elf_header;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700742 }
743
David Srbecky6d8c8f02015-10-26 10:57:09 +0000744 // Create program headers based on written sections.
745 std::vector<Elf_Phdr> MakeProgramHeaders() {
746 CHECK(!sections_.empty());
747 std::vector<Elf_Phdr> phdrs;
748 {
749 // The program headers must start with PT_PHDR which is used in
750 // loaded process to determine the number of program headers.
751 Elf_Phdr phdr = Elf_Phdr();
752 phdr.p_type = PT_PHDR;
753 phdr.p_flags = PF_R;
754 phdr.p_offset = phdr.p_vaddr = phdr.p_paddr = sizeof(Elf_Ehdr);
755 phdr.p_filesz = phdr.p_memsz = 0; // We need to fill this later.
756 phdr.p_align = sizeof(Elf_Off);
757 phdrs.push_back(phdr);
758 // Tell the linker to mmap the start of file to memory.
759 Elf_Phdr load = Elf_Phdr();
760 load.p_type = PT_LOAD;
761 load.p_flags = PF_R;
762 load.p_offset = load.p_vaddr = load.p_paddr = 0;
763 load.p_filesz = load.p_memsz = sections_[0]->header_.sh_offset;
764 load.p_align = kPageSize;
765 phdrs.push_back(load);
David Srbeckybc90fd02015-04-22 19:40:27 +0100766 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000767 // Create program headers for sections.
768 for (auto* section : sections_) {
769 const Elf_Shdr& shdr = section->header_;
770 if ((shdr.sh_flags & SHF_ALLOC) != 0 && shdr.sh_size != 0) {
771 // PT_LOAD tells the linker to mmap part of the file.
772 // The linker can only mmap page-aligned sections.
773 // Single PT_LOAD may contain several ELF sections.
774 Elf_Phdr& prev = phdrs.back();
775 Elf_Phdr load = Elf_Phdr();
776 load.p_type = PT_LOAD;
777 load.p_flags = section->phdr_flags_;
778 load.p_offset = shdr.sh_offset;
779 load.p_vaddr = load.p_paddr = shdr.sh_addr;
780 load.p_filesz = (shdr.sh_type != SHT_NOBITS ? shdr.sh_size : 0u);
781 load.p_memsz = shdr.sh_size;
782 load.p_align = shdr.sh_addralign;
783 if (prev.p_type == load.p_type &&
784 prev.p_flags == load.p_flags &&
785 prev.p_filesz == prev.p_memsz && // Do not merge .bss
786 load.p_filesz == load.p_memsz) { // Do not merge .bss
787 // Merge this PT_LOAD with the previous one.
788 Elf_Word size = shdr.sh_offset + shdr.sh_size - prev.p_offset;
789 prev.p_filesz = size;
790 prev.p_memsz = size;
791 } else {
792 // If we are adding new load, it must be aligned.
793 CHECK_EQ(shdr.sh_addralign, (Elf_Word)kPageSize);
794 phdrs.push_back(load);
795 }
796 }
797 }
798 for (auto* section : sections_) {
799 const Elf_Shdr& shdr = section->header_;
800 if ((shdr.sh_flags & SHF_ALLOC) != 0 && shdr.sh_size != 0) {
801 // Other PT_* types allow the program to locate interesting
802 // parts of memory at runtime. They must overlap with PT_LOAD.
803 if (section->phdr_type_ != 0) {
804 Elf_Phdr phdr = Elf_Phdr();
805 phdr.p_type = section->phdr_type_;
806 phdr.p_flags = section->phdr_flags_;
807 phdr.p_offset = shdr.sh_offset;
808 phdr.p_vaddr = phdr.p_paddr = shdr.sh_addr;
809 phdr.p_filesz = phdr.p_memsz = shdr.sh_size;
810 phdr.p_align = shdr.sh_addralign;
811 phdrs.push_back(phdr);
812 }
813 }
814 }
815 // Set the size of the initial PT_PHDR.
816 CHECK_EQ(phdrs[0].p_type, (Elf_Word)PT_PHDR);
817 phdrs[0].p_filesz = phdrs[0].p_memsz = phdrs.size() * sizeof(Elf_Phdr);
David Srbeckybc90fd02015-04-22 19:40:27 +0100818
David Srbecky6d8c8f02015-10-26 10:57:09 +0000819 return phdrs;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700820 }
821
David Srbeckybc90fd02015-04-22 19:40:27 +0100822 InstructionSet isa_;
David Srbecky5d811202016-03-08 13:21:22 +0000823 const InstructionSetFeatures* features_;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000824
Vladimir Marko131980f2015-12-03 18:29:23 +0000825 ErrorDelayingOutputStream stream_;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000826
827 Section rodata_;
828 Section text_;
829 Section bss_;
Vladimir Marko944da602016-02-19 12:27:55 +0000830 CachedStringSection dynstr_;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000831 SymbolSection dynsym_;
Vladimir Marko944da602016-02-19 12:27:55 +0000832 CachedSection hash_;
833 CachedSection dynamic_;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000834 Section eh_frame_;
835 Section eh_frame_hdr_;
836 StringSection strtab_;
837 SymbolSection symtab_;
838 Section debug_frame_;
David Srbeckyb851b492015-11-11 20:19:38 +0000839 Section debug_info_;
840 Section debug_line_;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000841 StringSection shstrtab_;
842 std::vector<std::unique_ptr<Section>> other_sections_;
843
844 // List of used section in the order in which they were written.
845 std::vector<Section*> sections_;
846
David Srbecky579942f2016-01-28 20:01:28 +0000847 bool started_;
Vladimir Marko944da602016-02-19 12:27:55 +0000848 bool write_program_headers_;
849
850 // The size of the memory taken by the ELF file when loaded.
851 size_t loaded_size_;
David Srbecky579942f2016-01-28 20:01:28 +0000852
David Srbecky6d8c8f02015-10-26 10:57:09 +0000853 // Used for allocation of virtual address space.
854 Elf_Addr virtual_address_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700855
856 DISALLOW_COPY_AND_ASSIGN(ElfBuilder);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700857};
858
859} // namespace art
860
861#endif // ART_COMPILER_ELF_BUILDER_H_