blob: f7da609e5d474c5dd5a55845d8deaf2fe70d8b8f [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 Srbecky6d8c8f02015-10-26 10:57:09 +000024#include "base/casts.h"
David Srbeckybc90fd02015-04-22 19:40:27 +010025#include "base/unix_file/fd_file.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070026#include "elf_utils.h"
David Srbecky6d8c8f02015-10-26 10:57:09 +000027#include "leb128.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000028#include "linker/error_delaying_output_stream.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000029#include "utils/array_ref.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070030
31namespace art {
32
David Srbeckybc90fd02015-04-22 19:40:27 +010033// Writes ELF file.
David Srbecky6d8c8f02015-10-26 10:57:09 +000034//
35// The basic layout of the elf file:
36// Elf_Ehdr - The ELF header.
37// Elf_Phdr[] - Program headers for the linker.
38// .rodata - DEX files and oat metadata.
39// .text - Compiled code.
40// .bss - Zero-initialized writeable section.
41// .dynstr - Names for .dynsym.
42// .dynsym - A few oat-specific dynamic symbols.
43// .hash - Hash-table for .dynsym.
44// .dynamic - Tags which let the linker locate .dynsym.
45// .strtab - Names for .symtab.
46// .symtab - Debug symbols.
47// .eh_frame - Unwind information (CFI).
48// .eh_frame_hdr - Index of .eh_frame.
49// .debug_frame - Unwind information (CFI).
50// .debug_frame.oat_patches - Addresses for relocation.
51// .debug_info - Debug information.
52// .debug_info.oat_patches - Addresses for relocation.
53// .debug_abbrev - Decoding information for .debug_info.
54// .debug_str - Strings for .debug_info.
55// .debug_line - Line number tables.
56// .debug_line.oat_patches - Addresses for relocation.
57// .text.oat_patches - Addresses for relocation.
58// .shstrtab - Names of ELF sections.
59// Elf_Shdr[] - Section headers.
60//
61// Some section are optional (the debug sections in particular).
62//
63// We try write the section data directly into the file without much
64// in-memory buffering. This means we generally write sections based on the
65// dependency order (e.g. .dynamic points to .dynsym which points to .text).
66//
67// In the cases where we need to buffer, we write the larger section first
68// and buffer the smaller one (e.g. .strtab is bigger than .symtab).
69//
70// The debug sections are written last for easier stripping.
71//
David Srbecky533c2072015-04-22 12:20:22 +010072template <typename ElfTypes>
Andreas Gampe54fc26c2014-09-04 21:47:42 -070073class ElfBuilder FINAL {
74 public:
David Srbecky6d8c8f02015-10-26 10:57:09 +000075 static constexpr size_t kMaxProgramHeaders = 16;
David Srbecky533c2072015-04-22 12:20:22 +010076 using Elf_Addr = typename ElfTypes::Addr;
David Srbeckybc90fd02015-04-22 19:40:27 +010077 using Elf_Off = typename ElfTypes::Off;
David Srbecky533c2072015-04-22 12:20:22 +010078 using Elf_Word = typename ElfTypes::Word;
79 using Elf_Sword = typename ElfTypes::Sword;
80 using Elf_Ehdr = typename ElfTypes::Ehdr;
81 using Elf_Shdr = typename ElfTypes::Shdr;
82 using Elf_Sym = typename ElfTypes::Sym;
83 using Elf_Phdr = typename ElfTypes::Phdr;
84 using Elf_Dyn = typename ElfTypes::Dyn;
85
David Srbeckybc90fd02015-04-22 19:40:27 +010086 // Base class of all sections.
David Srbecky6d8c8f02015-10-26 10:57:09 +000087 class Section : public OutputStream {
David Srbecky0c5bbc12015-04-28 17:54:52 +010088 public:
Vladimir Marko944da602016-02-19 12:27:55 +000089 Section(ElfBuilder<ElfTypes>* owner,
90 const std::string& name,
91 Elf_Word type,
92 Elf_Word flags,
93 const Section* link,
94 Elf_Word info,
95 Elf_Word align,
96 Elf_Word entsize)
97 : OutputStream(name),
98 owner_(owner),
99 header_(),
100 section_index_(0),
101 name_(name),
102 link_(link),
103 started_(false),
104 finished_(false),
105 phdr_flags_(PF_R),
106 phdr_type_(0) {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000107 DCHECK_GE(align, 1u);
David Srbecky491a7fe2015-05-28 00:59:08 +0100108 header_.sh_type = type;
109 header_.sh_flags = flags;
110 header_.sh_info = info;
111 header_.sh_addralign = align;
112 header_.sh_entsize = entsize;
David Srbecky0c5bbc12015-04-28 17:54:52 +0100113 }
David Srbecky0c5bbc12015-04-28 17:54:52 +0100114
David Srbecky6d8c8f02015-10-26 10:57:09 +0000115 // Start writing of this section.
116 void Start() {
117 CHECK(!started_);
118 CHECK(!finished_);
119 started_ = true;
120 auto& sections = owner_->sections_;
121 // Check that the previous section is complete.
122 CHECK(sections.empty() || sections.back()->finished_);
123 // The first ELF section index is 1. Index 0 is reserved for NULL.
124 section_index_ = sections.size() + 1;
David Srbecky579942f2016-01-28 20:01:28 +0000125 // Page-align if we switch between allocated and non-allocated sections,
126 // or if we change the type of allocation (e.g. executable vs non-executable).
127 if (!sections.empty()) {
128 if (header_.sh_flags != sections.back()->header_.sh_flags) {
129 header_.sh_addralign = kPageSize;
130 }
131 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000132 // Align file position.
133 if (header_.sh_type != SHT_NOBITS) {
David Srbecky579942f2016-01-28 20:01:28 +0000134 header_.sh_offset = owner_->AlignFileOffset(header_.sh_addralign);
135 } else {
136 header_.sh_offset = 0;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000137 }
138 // Align virtual memory address.
139 if ((header_.sh_flags & SHF_ALLOC) != 0) {
David Srbecky579942f2016-01-28 20:01:28 +0000140 header_.sh_addr = owner_->AlignVirtualAddress(header_.sh_addralign);
141 } else {
142 header_.sh_addr = 0;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000143 }
David Srbecky579942f2016-01-28 20:01:28 +0000144 // Push this section on the list of written sections.
145 sections.push_back(this);
David Srbecky0c5bbc12015-04-28 17:54:52 +0100146 }
147
David Srbecky6d8c8f02015-10-26 10:57:09 +0000148 // Finish writing of this section.
149 void End() {
150 CHECK(started_);
151 CHECK(!finished_);
152 finished_ = true;
153 if (header_.sh_type == SHT_NOBITS) {
154 CHECK_GT(header_.sh_size, 0u);
155 } else {
156 // Use the current file position to determine section size.
Vladimir Marko131980f2015-12-03 18:29:23 +0000157 off_t file_offset = owner_->stream_.Seek(0, kSeekCurrent);
David Srbecky6d8c8f02015-10-26 10:57:09 +0000158 CHECK_GE(file_offset, (off_t)header_.sh_offset);
159 header_.sh_size = file_offset - header_.sh_offset;
160 }
161 if ((header_.sh_flags & SHF_ALLOC) != 0) {
162 owner_->virtual_address_ += header_.sh_size;
163 }
164 }
165
David Srbecky5cc349f2015-12-18 15:04:48 +0000166 // Returns true if the section was written to disk.
167 // (Used to check whether we have .text when writing JIT debug info)
168 bool Exists() const {
169 return finished_;
170 }
171
David Srbecky6d8c8f02015-10-26 10:57:09 +0000172 // Get the location of this section in virtual memory.
173 Elf_Addr GetAddress() const {
174 CHECK(started_);
175 return header_.sh_addr;
176 }
177
178 // Returns the size of the content of this section.
179 Elf_Word GetSize() const {
David Srbeckyb851b492015-11-11 20:19:38 +0000180 if (finished_) {
181 return header_.sh_size;
182 } else {
183 CHECK(started_);
184 CHECK_NE(header_.sh_type, (Elf_Word)SHT_NOBITS);
Vladimir Marko131980f2015-12-03 18:29:23 +0000185 return owner_->stream_.Seek(0, kSeekCurrent) - header_.sh_offset;
David Srbeckyb851b492015-11-11 20:19:38 +0000186 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000187 }
188
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000189 // Write this section as "NOBITS" section. (used for the .bss section)
190 // This means that the ELF file does not contain the initial data for this section
191 // and it will be zero-initialized when the ELF file is loaded in the running program.
192 void WriteNoBitsSection(Elf_Word size) {
193 DCHECK_NE(header_.sh_flags & SHF_ALLOC, 0u);
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000194 header_.sh_type = SHT_NOBITS;
David Srbecky579942f2016-01-28 20:01:28 +0000195 Start();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000196 header_.sh_size = size;
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000197 End();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000198 }
199
200 // This function always succeeds to simplify code.
201 // Use builder's Good() to check the actual status.
202 bool WriteFully(const void* buffer, size_t byte_count) OVERRIDE {
203 CHECK(started_);
204 CHECK(!finished_);
Vladimir Marko131980f2015-12-03 18:29:23 +0000205 return owner_->stream_.WriteFully(buffer, byte_count);
David Srbecky6d8c8f02015-10-26 10:57:09 +0000206 }
207
208 // This function always succeeds to simplify code.
209 // Use builder's Good() to check the actual status.
210 off_t Seek(off_t offset, Whence whence) OVERRIDE {
211 // Forward the seek as-is and trust the caller to use it reasonably.
Vladimir Marko131980f2015-12-03 18:29:23 +0000212 return owner_->stream_.Seek(offset, whence);
David Srbecky0c5bbc12015-04-28 17:54:52 +0100213 }
214
Vladimir Marko10c13562015-11-25 14:33:36 +0000215 // This function flushes the output and returns whether it succeeded.
216 // If there was a previous failure, this does nothing and returns false, i.e. failed.
217 bool Flush() OVERRIDE {
Vladimir Marko131980f2015-12-03 18:29:23 +0000218 return owner_->stream_.Flush();
Vladimir Marko10c13562015-11-25 14:33:36 +0000219 }
220
David Srbecky0c5bbc12015-04-28 17:54:52 +0100221 Elf_Word GetSectionIndex() const {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000222 DCHECK(started_);
David Srbeckybc90fd02015-04-22 19:40:27 +0100223 DCHECK_NE(section_index_, 0u);
David Srbecky0c5bbc12015-04-28 17:54:52 +0100224 return section_index_;
225 }
226
David Srbecky0c5bbc12015-04-28 17:54:52 +0100227 private:
David Srbecky6d8c8f02015-10-26 10:57:09 +0000228 ElfBuilder<ElfTypes>* owner_;
David Srbecky491a7fe2015-05-28 00:59:08 +0100229 Elf_Shdr header_;
David Srbecky0c5bbc12015-04-28 17:54:52 +0100230 Elf_Word section_index_;
231 const std::string name_;
David Srbeckybc90fd02015-04-22 19:40:27 +0100232 const Section* const link_;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000233 bool started_;
234 bool finished_;
235 Elf_Word phdr_flags_;
236 Elf_Word phdr_type_;
David Srbeckybc90fd02015-04-22 19:40:27 +0100237
David Srbecky6d8c8f02015-10-26 10:57:09 +0000238 friend class ElfBuilder;
David Srbecky4d247f72015-11-09 11:56:52 +0000239
240 DISALLOW_COPY_AND_ASSIGN(Section);
David Srbecky0c5bbc12015-04-28 17:54:52 +0100241 };
242
Vladimir Marko944da602016-02-19 12:27:55 +0000243 class CachedSection : public Section {
244 public:
245 CachedSection(ElfBuilder<ElfTypes>* owner,
246 const std::string& name,
247 Elf_Word type,
248 Elf_Word flags,
249 const Section* link,
250 Elf_Word info,
251 Elf_Word align,
252 Elf_Word entsize)
253 : Section(owner, name, type, flags, link, info, align, entsize), cache_() { }
254
255 Elf_Word Add(const void* data, size_t length) {
256 Elf_Word offset = cache_.size();
257 const uint8_t* d = reinterpret_cast<const uint8_t*>(data);
258 cache_.insert(cache_.end(), d, d + length);
259 return offset;
260 }
261
262 Elf_Word GetCacheSize() {
263 return cache_.size();
264 }
265
266 void Write() {
267 this->WriteFully(cache_.data(), cache_.size());
268 cache_.clear();
269 cache_.shrink_to_fit();
270 }
271
272 void WriteCachedSection() {
273 this->Start();
274 Write();
275 this->End();
276 }
277
278 private:
279 std::vector<uint8_t> cache_;
280 };
281
282 // Writer of .dynstr section.
283 class CachedStringSection FINAL : public CachedSection {
284 public:
285 CachedStringSection(ElfBuilder<ElfTypes>* owner,
286 const std::string& name,
287 Elf_Word flags,
288 Elf_Word align)
289 : CachedSection(owner,
290 name,
291 SHT_STRTAB,
292 flags,
293 /* link */ nullptr,
294 /* info */ 0,
295 align,
296 /* entsize */ 0) { }
297
298 Elf_Word Add(const std::string& name) {
299 if (CachedSection::GetCacheSize() == 0u) {
300 DCHECK(name.empty());
301 }
302 return CachedSection::Add(name.c_str(), name.length() + 1);
303 }
304 };
305
306 // Writer of .strtab and .shstrtab sections.
David Srbecky6d8c8f02015-10-26 10:57:09 +0000307 class StringSection FINAL : public Section {
David Srbeckybc90fd02015-04-22 19:40:27 +0100308 public:
Vladimir Marko944da602016-02-19 12:27:55 +0000309 StringSection(ElfBuilder<ElfTypes>* owner,
310 const std::string& name,
311 Elf_Word flags,
312 Elf_Word align)
313 : Section(owner,
314 name,
315 SHT_STRTAB,
316 flags,
317 /* link */ nullptr,
318 /* info */ 0,
319 align,
320 /* entsize */ 0),
David Srbecky6d8c8f02015-10-26 10:57:09 +0000321 current_offset_(0) {
David Srbecky0c5bbc12015-04-28 17:54:52 +0100322 }
David Srbecky0c5bbc12015-04-28 17:54:52 +0100323
David Srbecky6d8c8f02015-10-26 10:57:09 +0000324 Elf_Word Write(const std::string& name) {
325 if (current_offset_ == 0) {
326 DCHECK(name.empty());
327 }
328 Elf_Word offset = current_offset_;
329 this->WriteFully(name.c_str(), name.length() + 1);
330 current_offset_ += name.length() + 1;
David Srbeckybc90fd02015-04-22 19:40:27 +0100331 return offset;
332 }
333
David Srbeckybc90fd02015-04-22 19:40:27 +0100334 private:
David Srbecky6d8c8f02015-10-26 10:57:09 +0000335 Elf_Word current_offset_;
David Srbeckybc90fd02015-04-22 19:40:27 +0100336 };
337
David Srbeckybc90fd02015-04-22 19:40:27 +0100338 // Writer of .dynsym and .symtab sections.
Vladimir Marko944da602016-02-19 12:27:55 +0000339 class SymbolSection FINAL : public CachedSection {
David Srbecky0c5bbc12015-04-28 17:54:52 +0100340 public:
Vladimir Marko944da602016-02-19 12:27:55 +0000341 SymbolSection(ElfBuilder<ElfTypes>* owner,
342 const std::string& name,
343 Elf_Word type,
344 Elf_Word flags,
345 Section* strtab)
346 : CachedSection(owner,
347 name,
348 type,
349 flags,
350 strtab,
351 /* info */ 0,
352 sizeof(Elf_Off),
353 sizeof(Elf_Sym)) {
354 // The symbol table always has to start with NULL symbol.
355 Elf_Sym null_symbol = Elf_Sym();
356 CachedSection::Add(&null_symbol, sizeof(null_symbol));
David Srbecky6d8c8f02015-10-26 10:57:09 +0000357 }
358
359 // Buffer symbol for this section. It will be written later.
David Srbecky5cc349f2015-12-18 15:04:48 +0000360 // If the symbol's section is null, it will be considered absolute (SHN_ABS).
361 // (we use this in JIT to reference code which is stored outside the debug ELF file)
Vladimir Marko944da602016-02-19 12:27:55 +0000362 void Add(Elf_Word name,
363 const Section* section,
364 Elf_Addr addr,
365 bool is_relative,
366 Elf_Word size,
367 uint8_t binding,
368 uint8_t type,
369 uint8_t other = 0) {
370 DCHECK(section != nullptr || !is_relative);
371 Elf_Addr abs_addr = addr + (is_relative ? section->GetAddress() : 0);
372 Elf_Word section_index =
373 (section != nullptr) ? section->GetSectionIndex() : static_cast<Elf_Word>(SHN_ABS);
374 Add(name, section_index, abs_addr, size, binding, type, other);
375 }
376
377 void Add(Elf_Word name,
378 Elf_Word section_index,
379 Elf_Addr addr,
380 Elf_Word size,
381 uint8_t binding,
382 uint8_t type,
383 uint8_t other = 0) {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000384 Elf_Sym sym = Elf_Sym();
385 sym.st_name = name;
Vladimir Marko944da602016-02-19 12:27:55 +0000386 sym.st_value = addr;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000387 sym.st_size = size;
388 sym.st_other = other;
Vladimir Marko944da602016-02-19 12:27:55 +0000389 sym.st_shndx = section_index;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000390 sym.st_info = (binding << 4) + (type & 0xf);
Vladimir Marko944da602016-02-19 12:27:55 +0000391 CachedSection::Add(&sym, sizeof(sym));
David Srbecky0c5bbc12015-04-28 17:54:52 +0100392 }
David Srbeckybc90fd02015-04-22 19:40:27 +0100393 };
394
David Srbecky6d8c8f02015-10-26 10:57:09 +0000395 ElfBuilder(InstructionSet isa, OutputStream* output)
Vladimir Marko131980f2015-12-03 18:29:23 +0000396 : isa_(isa),
397 stream_(output),
398 rodata_(this, ".rodata", SHT_PROGBITS, SHF_ALLOC, nullptr, 0, kPageSize, 0),
399 text_(this, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR, nullptr, 0, kPageSize, 0),
400 bss_(this, ".bss", SHT_NOBITS, SHF_ALLOC, nullptr, 0, kPageSize, 0),
401 dynstr_(this, ".dynstr", SHF_ALLOC, kPageSize),
402 dynsym_(this, ".dynsym", SHT_DYNSYM, SHF_ALLOC, &dynstr_),
403 hash_(this, ".hash", SHT_HASH, SHF_ALLOC, &dynsym_, 0, sizeof(Elf_Word), sizeof(Elf_Word)),
404 dynamic_(this, ".dynamic", SHT_DYNAMIC, SHF_ALLOC, &dynstr_, 0, kPageSize, sizeof(Elf_Dyn)),
405 eh_frame_(this, ".eh_frame", SHT_PROGBITS, SHF_ALLOC, nullptr, 0, kPageSize, 0),
406 eh_frame_hdr_(this, ".eh_frame_hdr", SHT_PROGBITS, SHF_ALLOC, nullptr, 0, 4, 0),
David Srbecky579942f2016-01-28 20:01:28 +0000407 strtab_(this, ".strtab", 0, 1),
Vladimir Marko131980f2015-12-03 18:29:23 +0000408 symtab_(this, ".symtab", SHT_SYMTAB, 0, &strtab_),
409 debug_frame_(this, ".debug_frame", SHT_PROGBITS, 0, nullptr, 0, sizeof(Elf_Addr), 0),
410 debug_info_(this, ".debug_info", SHT_PROGBITS, 0, nullptr, 0, 1, 0),
411 debug_line_(this, ".debug_line", SHT_PROGBITS, 0, nullptr, 0, 1, 0),
412 shstrtab_(this, ".shstrtab", 0, 1),
David Srbecky579942f2016-01-28 20:01:28 +0000413 started_(false),
Vladimir Marko944da602016-02-19 12:27:55 +0000414 write_program_headers_(false),
415 loaded_size_(0u),
Vladimir Marko131980f2015-12-03 18:29:23 +0000416 virtual_address_(0) {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000417 text_.phdr_flags_ = PF_R | PF_X;
418 bss_.phdr_flags_ = PF_R | PF_W;
419 dynamic_.phdr_flags_ = PF_R | PF_W;
420 dynamic_.phdr_type_ = PT_DYNAMIC;
421 eh_frame_hdr_.phdr_type_ = PT_GNU_EH_FRAME;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700422 }
423 ~ElfBuilder() {}
424
David Srbecky6d8c8f02015-10-26 10:57:09 +0000425 InstructionSet GetIsa() { return isa_; }
426 Section* GetRoData() { return &rodata_; }
427 Section* GetText() { return &text_; }
428 Section* GetBss() { return &bss_; }
429 StringSection* GetStrTab() { return &strtab_; }
430 SymbolSection* GetSymTab() { return &symtab_; }
431 Section* GetEhFrame() { return &eh_frame_; }
432 Section* GetEhFrameHdr() { return &eh_frame_hdr_; }
433 Section* GetDebugFrame() { return &debug_frame_; }
David Srbeckyb851b492015-11-11 20:19:38 +0000434 Section* GetDebugInfo() { return &debug_info_; }
435 Section* GetDebugLine() { return &debug_line_; }
Ian Rogers0279ebb2014-10-08 17:27:48 -0700436
David Srbecky6d8c8f02015-10-26 10:57:09 +0000437 // Encode patch locations as LEB128 list of deltas between consecutive addresses.
438 // (exposed publicly for tests)
Vladimir Marko10c13562015-11-25 14:33:36 +0000439 static void EncodeOatPatches(const ArrayRef<const uintptr_t>& locations,
David Srbecky6d8c8f02015-10-26 10:57:09 +0000440 std::vector<uint8_t>* buffer) {
441 buffer->reserve(buffer->size() + locations.size() * 2); // guess 2 bytes per ULEB128.
442 uintptr_t address = 0; // relative to start of section.
443 for (uintptr_t location : locations) {
444 DCHECK_GE(location, address) << "Patch locations are not in sorted order";
445 EncodeUnsignedLeb128(buffer, dchecked_integral_cast<uint32_t>(location - address));
446 address = location;
447 }
448 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700449
Vladimir Marko10c13562015-11-25 14:33:36 +0000450 void WritePatches(const char* name, const ArrayRef<const uintptr_t>& patch_locations) {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000451 std::vector<uint8_t> buffer;
Vladimir Marko10c13562015-11-25 14:33:36 +0000452 EncodeOatPatches(patch_locations, &buffer);
David Srbecky6d8c8f02015-10-26 10:57:09 +0000453 std::unique_ptr<Section> s(new Section(this, name, SHT_OAT_PATCH, 0, nullptr, 0, 1, 0));
454 s->Start();
455 s->WriteFully(buffer.data(), buffer.size());
456 s->End();
457 other_sections_.push_back(std::move(s));
458 }
David Srbecky527c9c72015-04-17 21:14:10 +0100459
David Srbecky6d8c8f02015-10-26 10:57:09 +0000460 void WriteSection(const char* name, const std::vector<uint8_t>* buffer) {
461 std::unique_ptr<Section> s(new Section(this, name, SHT_PROGBITS, 0, nullptr, 0, 1, 0));
462 s->Start();
463 s->WriteFully(buffer->data(), buffer->size());
464 s->End();
465 other_sections_.push_back(std::move(s));
466 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700467
David Srbecky579942f2016-01-28 20:01:28 +0000468 // Reserve space for ELF header and program headers.
469 // We do not know the number of headers until later, so
470 // it is easiest to just reserve a fixed amount of space.
471 // Program headers are required for loading by the linker.
472 // It is possible to omit them for ELF files used for debugging.
473 void Start(bool write_program_headers = true) {
474 int size = sizeof(Elf_Ehdr);
475 if (write_program_headers) {
476 size += sizeof(Elf_Phdr) * kMaxProgramHeaders;
477 }
Vladimir Marko131980f2015-12-03 18:29:23 +0000478 stream_.Seek(size, kSeekSet);
David Srbecky579942f2016-01-28 20:01:28 +0000479 started_ = true;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000480 virtual_address_ += size;
David Srbecky579942f2016-01-28 20:01:28 +0000481 write_program_headers_ = write_program_headers;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000482 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700483
David Srbecky6d8c8f02015-10-26 10:57:09 +0000484 void End() {
David Srbecky579942f2016-01-28 20:01:28 +0000485 DCHECK(started_);
486
Vladimir Marko944da602016-02-19 12:27:55 +0000487 // Note: loaded_size_ == 0 for tests that don't write .rodata, .text, .bss,
488 // .dynstr, dynsym, .hash and .dynamic. These tests should not read loaded_size_.
489 // TODO: Either refactor the .eh_frame creation so that it counts towards loaded_size_,
490 // or remove all support for .eh_frame. (The currently unused .eh_frame counts towards
491 // the virtual_address_ but we don't consider it for loaded_size_.)
492 CHECK(loaded_size_ == 0 || loaded_size_ == RoundUp(virtual_address_, kPageSize))
493 << loaded_size_ << " " << virtual_address_;
494
David Srbecky6d8c8f02015-10-26 10:57:09 +0000495 // Write section names and finish the section headers.
496 shstrtab_.Start();
497 shstrtab_.Write("");
498 for (auto* section : sections_) {
499 section->header_.sh_name = shstrtab_.Write(section->name_);
500 if (section->link_ != nullptr) {
501 section->header_.sh_link = section->link_->GetSectionIndex();
David Srbeckyb0a962c2015-04-28 19:43:56 +0100502 }
David Srbecky527c9c72015-04-17 21:14:10 +0100503 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000504 shstrtab_.End();
David Srbecky527c9c72015-04-17 21:14:10 +0100505
David Srbecky6d8c8f02015-10-26 10:57:09 +0000506 // Write section headers at the end of the ELF file.
507 std::vector<Elf_Shdr> shdrs;
508 shdrs.reserve(1u + sections_.size());
509 shdrs.push_back(Elf_Shdr()); // NULL at index 0.
510 for (auto* section : sections_) {
511 shdrs.push_back(section->header_);
512 }
513 Elf_Off section_headers_offset;
David Srbecky579942f2016-01-28 20:01:28 +0000514 section_headers_offset = AlignFileOffset(sizeof(Elf_Off));
Vladimir Marko131980f2015-12-03 18:29:23 +0000515 stream_.WriteFully(shdrs.data(), shdrs.size() * sizeof(shdrs[0]));
516
517 // Flush everything else before writing the program headers. This should prevent
518 // the OS from reordering writes, so that we don't end up with valid headers
519 // and partially written data if we suddenly lose power, for example.
520 stream_.Flush();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000521
David Srbecky579942f2016-01-28 20:01:28 +0000522 // The main ELF header.
David Srbeckybc90fd02015-04-22 19:40:27 +0100523 Elf_Ehdr elf_header = MakeElfHeader(isa_);
David Srbeckybc90fd02015-04-22 19:40:27 +0100524 elf_header.e_shoff = section_headers_offset;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000525 elf_header.e_shnum = shdrs.size();
David Srbeckybc90fd02015-04-22 19:40:27 +0100526 elf_header.e_shstrndx = shstrtab_.GetSectionIndex();
David Srbecky579942f2016-01-28 20:01:28 +0000527
528 // Program headers (i.e. mmap instructions).
529 std::vector<Elf_Phdr> phdrs;
530 if (write_program_headers_) {
531 phdrs = MakeProgramHeaders();
532 CHECK_LE(phdrs.size(), kMaxProgramHeaders);
533 elf_header.e_phoff = sizeof(Elf_Ehdr);
534 elf_header.e_phnum = phdrs.size();
535 }
536
Vladimir Marko131980f2015-12-03 18:29:23 +0000537 stream_.Seek(0, kSeekSet);
538 stream_.WriteFully(&elf_header, sizeof(elf_header));
539 stream_.WriteFully(phdrs.data(), phdrs.size() * sizeof(phdrs[0]));
540 stream_.Flush();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700541 }
542
David Srbecky6d8c8f02015-10-26 10:57:09 +0000543 // The running program does not have access to section headers
544 // and the loader is not supposed to use them either.
545 // The dynamic sections therefore replicates some of the layout
546 // information like the address and size of .rodata and .text.
547 // It also contains other metadata like the SONAME.
548 // The .dynamic section is found using the PT_DYNAMIC program header.
Vladimir Marko944da602016-02-19 12:27:55 +0000549 void PrepareDynamicSection(const std::string& elf_file_path,
550 Elf_Word rodata_size,
551 Elf_Word text_size,
552 Elf_Word bss_size) {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000553 std::string soname(elf_file_path);
554 size_t directory_separator_pos = soname.rfind('/');
555 if (directory_separator_pos != std::string::npos) {
556 soname = soname.substr(directory_separator_pos + 1);
557 }
558
Vladimir Marko944da602016-02-19 12:27:55 +0000559 // Calculate addresses of .text, .bss and .dynstr.
560 DCHECK_EQ(rodata_.header_.sh_addralign, static_cast<Elf_Word>(kPageSize));
561 DCHECK_EQ(text_.header_.sh_addralign, static_cast<Elf_Word>(kPageSize));
562 DCHECK_EQ(bss_.header_.sh_addralign, static_cast<Elf_Word>(kPageSize));
563 DCHECK_EQ(dynstr_.header_.sh_addralign, static_cast<Elf_Word>(kPageSize));
564 Elf_Word rodata_address = rodata_.GetAddress();
565 Elf_Word text_address = RoundUp(rodata_address + rodata_size, kPageSize);
566 Elf_Word bss_address = RoundUp(text_address + text_size, kPageSize);
567 Elf_Word dynstr_address = RoundUp(bss_address + bss_size, kPageSize);
Vladimir Marko45724f92016-02-17 17:46:10 +0000568
Vladimir Marko944da602016-02-19 12:27:55 +0000569 // Cache .dynstr, .dynsym and .hash data.
570 dynstr_.Add(""); // dynstr should start with empty string.
571 Elf_Word rodata_index = rodata_.GetSectionIndex();
572 Elf_Word oatdata = dynstr_.Add("oatdata");
573 dynsym_.Add(oatdata, rodata_index, rodata_address, rodata_size, STB_GLOBAL, STT_OBJECT);
574 if (text_size != 0u) {
575 Elf_Word text_index = rodata_index + 1u;
576 Elf_Word oatexec = dynstr_.Add("oatexec");
577 dynsym_.Add(oatexec, text_index, text_address, text_size, STB_GLOBAL, STT_OBJECT);
578 Elf_Word oatlastword = dynstr_.Add("oatlastword");
579 Elf_Word oatlastword_address = text_address + text_size - 4;
580 dynsym_.Add(oatlastword, text_index, oatlastword_address, 4, STB_GLOBAL, STT_OBJECT);
581 } else if (rodata_size != 0) {
582 // rodata_ can be size 0 for dwarf_test.
583 Elf_Word oatlastword = dynstr_.Add("oatlastword");
584 Elf_Word oatlastword_address = rodata_address + rodata_size - 4;
585 dynsym_.Add(oatlastword, rodata_index, oatlastword_address, 4, STB_GLOBAL, STT_OBJECT);
586 }
587 if (bss_size != 0u) {
588 Elf_Word bss_index = rodata_index + 1u + (text_size != 0 ? 1u : 0u);
589 Elf_Word oatbss = dynstr_.Add("oatbss");
590 dynsym_.Add(oatbss, bss_index, bss_address, bss_size, STB_GLOBAL, STT_OBJECT);
591 Elf_Word oatbsslastword = dynstr_.Add("oatbsslastword");
592 Elf_Word bsslastword_address = bss_address + bss_size - 4;
593 dynsym_.Add(oatbsslastword, bss_index, bsslastword_address, 4, STB_GLOBAL, STT_OBJECT);
594 }
595 Elf_Word soname_offset = dynstr_.Add(soname);
David Srbecky6d8c8f02015-10-26 10:57:09 +0000596
597 // We do not really need a hash-table since there is so few entries.
598 // However, the hash-table is the only way the linker can actually
599 // determine the number of symbols in .dynsym so it is required.
Vladimir Marko944da602016-02-19 12:27:55 +0000600 int count = dynsym_.GetCacheSize() / sizeof(Elf_Sym); // Includes NULL.
David Srbecky6d8c8f02015-10-26 10:57:09 +0000601 std::vector<Elf_Word> hash;
602 hash.push_back(1); // Number of buckets.
603 hash.push_back(count); // Number of chains.
604 // Buckets. Having just one makes it linear search.
605 hash.push_back(1); // Point to first non-NULL symbol.
606 // Chains. This creates linked list of symbols.
607 hash.push_back(0); // Dummy entry for the NULL symbol.
608 for (int i = 1; i < count - 1; i++) {
609 hash.push_back(i + 1); // Each symbol points to the next one.
610 }
611 hash.push_back(0); // Last symbol terminates the chain.
Vladimir Marko944da602016-02-19 12:27:55 +0000612 hash_.Add(hash.data(), hash.size() * sizeof(hash[0]));
David Srbecky6d8c8f02015-10-26 10:57:09 +0000613
Vladimir Marko944da602016-02-19 12:27:55 +0000614 // Calculate addresses of .dynsym, .hash and .dynamic.
615 DCHECK_EQ(dynstr_.header_.sh_flags, dynsym_.header_.sh_flags);
616 DCHECK_EQ(dynsym_.header_.sh_flags, hash_.header_.sh_flags);
617 Elf_Word dynsym_address =
618 RoundUp(dynstr_address + dynstr_.GetCacheSize(), dynsym_.header_.sh_addralign);
619 Elf_Word hash_address =
620 RoundUp(dynsym_address + dynsym_.GetCacheSize(), hash_.header_.sh_addralign);
621 DCHECK_EQ(dynamic_.header_.sh_addralign, static_cast<Elf_Word>(kPageSize));
622 Elf_Word dynamic_address = RoundUp(hash_address + dynsym_.GetCacheSize(), kPageSize);
623
David Srbecky6d8c8f02015-10-26 10:57:09 +0000624 Elf_Dyn dyns[] = {
Vladimir Marko944da602016-02-19 12:27:55 +0000625 { DT_HASH, { hash_address } },
626 { DT_STRTAB, { dynstr_address } },
627 { DT_SYMTAB, { dynsym_address } },
David Srbecky6d8c8f02015-10-26 10:57:09 +0000628 { DT_SYMENT, { sizeof(Elf_Sym) } },
Vladimir Marko944da602016-02-19 12:27:55 +0000629 { DT_STRSZ, { dynstr_.GetCacheSize() } },
David Srbecky6d8c8f02015-10-26 10:57:09 +0000630 { DT_SONAME, { soname_offset } },
631 { DT_NULL, { 0 } },
632 };
Vladimir Marko944da602016-02-19 12:27:55 +0000633 dynamic_.Add(&dyns, sizeof(dyns));
634
635 loaded_size_ = RoundUp(dynamic_address + dynamic_.GetCacheSize(), kPageSize);
636 }
637
638 void WriteDynamicSection() {
639 dynstr_.WriteCachedSection();
640 dynsym_.WriteCachedSection();
641 hash_.WriteCachedSection();
642 dynamic_.WriteCachedSection();
643
644 CHECK_EQ(loaded_size_, RoundUp(dynamic_.GetAddress() + dynamic_.GetSize(), kPageSize));
645 }
646
647 Elf_Word GetLoadedSize() {
648 CHECK_NE(loaded_size_, 0u);
649 return loaded_size_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700650 }
651
David Srbecky6d8c8f02015-10-26 10:57:09 +0000652 // Returns true if all writes and seeks on the output stream succeeded.
653 bool Good() {
Vladimir Marko131980f2015-12-03 18:29:23 +0000654 return stream_.Good();
655 }
656
657 // Returns the builder's internal stream.
658 OutputStream* GetStream() {
659 return &stream_;
David Srbecky527c9c72015-04-17 21:14:10 +0100660 }
661
David Srbecky579942f2016-01-28 20:01:28 +0000662 off_t AlignFileOffset(size_t alignment) {
663 return stream_.Seek(RoundUp(stream_.Seek(0, kSeekCurrent), alignment), kSeekSet);
664 }
665
666 Elf_Addr AlignVirtualAddress(size_t alignment) {
667 return virtual_address_ = RoundUp(virtual_address_, alignment);
668 }
669
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700670 private:
David Srbeckybc90fd02015-04-22 19:40:27 +0100671 static Elf_Ehdr MakeElfHeader(InstructionSet isa) {
672 Elf_Ehdr elf_header = Elf_Ehdr();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700673 switch (isa) {
674 case kArm:
675 // Fall through.
676 case kThumb2: {
David Srbeckybc90fd02015-04-22 19:40:27 +0100677 elf_header.e_machine = EM_ARM;
678 elf_header.e_flags = EF_ARM_EABI_VER5;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700679 break;
680 }
681 case kArm64: {
David Srbeckybc90fd02015-04-22 19:40:27 +0100682 elf_header.e_machine = EM_AARCH64;
683 elf_header.e_flags = 0;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700684 break;
685 }
686 case kX86: {
David Srbeckybc90fd02015-04-22 19:40:27 +0100687 elf_header.e_machine = EM_386;
688 elf_header.e_flags = 0;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700689 break;
690 }
691 case kX86_64: {
David Srbeckybc90fd02015-04-22 19:40:27 +0100692 elf_header.e_machine = EM_X86_64;
693 elf_header.e_flags = 0;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700694 break;
695 }
696 case kMips: {
David Srbeckybc90fd02015-04-22 19:40:27 +0100697 elf_header.e_machine = EM_MIPS;
698 elf_header.e_flags = (EF_MIPS_NOREORDER |
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700699 EF_MIPS_PIC |
700 EF_MIPS_CPIC |
701 EF_MIPS_ABI_O32 |
702 EF_MIPS_ARCH_32R2);
703 break;
704 }
Andreas Gampe57b34292015-01-14 15:45:59 -0800705 case kMips64: {
David Srbeckybc90fd02015-04-22 19:40:27 +0100706 elf_header.e_machine = EM_MIPS;
707 elf_header.e_flags = (EF_MIPS_NOREORDER |
Andreas Gampe57b34292015-01-14 15:45:59 -0800708 EF_MIPS_PIC |
709 EF_MIPS_CPIC |
710 EF_MIPS_ARCH_64R6);
711 break;
712 }
David Srbeckybc90fd02015-04-22 19:40:27 +0100713 case kNone: {
714 LOG(FATAL) << "No instruction set";
David Srbecky6d8c8f02015-10-26 10:57:09 +0000715 break;
716 }
717 default: {
718 LOG(FATAL) << "Unknown instruction set " << isa;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700719 }
720 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700721
David Srbeckybc90fd02015-04-22 19:40:27 +0100722 elf_header.e_ident[EI_MAG0] = ELFMAG0;
723 elf_header.e_ident[EI_MAG1] = ELFMAG1;
724 elf_header.e_ident[EI_MAG2] = ELFMAG2;
725 elf_header.e_ident[EI_MAG3] = ELFMAG3;
726 elf_header.e_ident[EI_CLASS] = (sizeof(Elf_Addr) == sizeof(Elf32_Addr))
Tong Shen62d1ca32014-09-03 17:24:56 -0700727 ? ELFCLASS32 : ELFCLASS64;;
David Srbeckybc90fd02015-04-22 19:40:27 +0100728 elf_header.e_ident[EI_DATA] = ELFDATA2LSB;
729 elf_header.e_ident[EI_VERSION] = EV_CURRENT;
730 elf_header.e_ident[EI_OSABI] = ELFOSABI_LINUX;
731 elf_header.e_ident[EI_ABIVERSION] = 0;
732 elf_header.e_type = ET_DYN;
733 elf_header.e_version = 1;
734 elf_header.e_entry = 0;
735 elf_header.e_ehsize = sizeof(Elf_Ehdr);
736 elf_header.e_phentsize = sizeof(Elf_Phdr);
737 elf_header.e_shentsize = sizeof(Elf_Shdr);
738 elf_header.e_phoff = sizeof(Elf_Ehdr);
739 return elf_header;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700740 }
741
David Srbecky6d8c8f02015-10-26 10:57:09 +0000742 // Create program headers based on written sections.
743 std::vector<Elf_Phdr> MakeProgramHeaders() {
744 CHECK(!sections_.empty());
745 std::vector<Elf_Phdr> phdrs;
746 {
747 // The program headers must start with PT_PHDR which is used in
748 // loaded process to determine the number of program headers.
749 Elf_Phdr phdr = Elf_Phdr();
750 phdr.p_type = PT_PHDR;
751 phdr.p_flags = PF_R;
752 phdr.p_offset = phdr.p_vaddr = phdr.p_paddr = sizeof(Elf_Ehdr);
753 phdr.p_filesz = phdr.p_memsz = 0; // We need to fill this later.
754 phdr.p_align = sizeof(Elf_Off);
755 phdrs.push_back(phdr);
756 // Tell the linker to mmap the start of file to memory.
757 Elf_Phdr load = Elf_Phdr();
758 load.p_type = PT_LOAD;
759 load.p_flags = PF_R;
760 load.p_offset = load.p_vaddr = load.p_paddr = 0;
761 load.p_filesz = load.p_memsz = sections_[0]->header_.sh_offset;
762 load.p_align = kPageSize;
763 phdrs.push_back(load);
David Srbeckybc90fd02015-04-22 19:40:27 +0100764 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000765 // Create program headers for sections.
766 for (auto* section : sections_) {
767 const Elf_Shdr& shdr = section->header_;
768 if ((shdr.sh_flags & SHF_ALLOC) != 0 && shdr.sh_size != 0) {
769 // PT_LOAD tells the linker to mmap part of the file.
770 // The linker can only mmap page-aligned sections.
771 // Single PT_LOAD may contain several ELF sections.
772 Elf_Phdr& prev = phdrs.back();
773 Elf_Phdr load = Elf_Phdr();
774 load.p_type = PT_LOAD;
775 load.p_flags = section->phdr_flags_;
776 load.p_offset = shdr.sh_offset;
777 load.p_vaddr = load.p_paddr = shdr.sh_addr;
778 load.p_filesz = (shdr.sh_type != SHT_NOBITS ? shdr.sh_size : 0u);
779 load.p_memsz = shdr.sh_size;
780 load.p_align = shdr.sh_addralign;
781 if (prev.p_type == load.p_type &&
782 prev.p_flags == load.p_flags &&
783 prev.p_filesz == prev.p_memsz && // Do not merge .bss
784 load.p_filesz == load.p_memsz) { // Do not merge .bss
785 // Merge this PT_LOAD with the previous one.
786 Elf_Word size = shdr.sh_offset + shdr.sh_size - prev.p_offset;
787 prev.p_filesz = size;
788 prev.p_memsz = size;
789 } else {
790 // If we are adding new load, it must be aligned.
791 CHECK_EQ(shdr.sh_addralign, (Elf_Word)kPageSize);
792 phdrs.push_back(load);
793 }
794 }
795 }
796 for (auto* section : sections_) {
797 const Elf_Shdr& shdr = section->header_;
798 if ((shdr.sh_flags & SHF_ALLOC) != 0 && shdr.sh_size != 0) {
799 // Other PT_* types allow the program to locate interesting
800 // parts of memory at runtime. They must overlap with PT_LOAD.
801 if (section->phdr_type_ != 0) {
802 Elf_Phdr phdr = Elf_Phdr();
803 phdr.p_type = section->phdr_type_;
804 phdr.p_flags = section->phdr_flags_;
805 phdr.p_offset = shdr.sh_offset;
806 phdr.p_vaddr = phdr.p_paddr = shdr.sh_addr;
807 phdr.p_filesz = phdr.p_memsz = shdr.sh_size;
808 phdr.p_align = shdr.sh_addralign;
809 phdrs.push_back(phdr);
810 }
811 }
812 }
813 // Set the size of the initial PT_PHDR.
814 CHECK_EQ(phdrs[0].p_type, (Elf_Word)PT_PHDR);
815 phdrs[0].p_filesz = phdrs[0].p_memsz = phdrs.size() * sizeof(Elf_Phdr);
David Srbeckybc90fd02015-04-22 19:40:27 +0100816
David Srbecky6d8c8f02015-10-26 10:57:09 +0000817 return phdrs;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700818 }
819
David Srbeckybc90fd02015-04-22 19:40:27 +0100820 InstructionSet isa_;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000821
Vladimir Marko131980f2015-12-03 18:29:23 +0000822 ErrorDelayingOutputStream stream_;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000823
824 Section rodata_;
825 Section text_;
826 Section bss_;
Vladimir Marko944da602016-02-19 12:27:55 +0000827 CachedStringSection dynstr_;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000828 SymbolSection dynsym_;
Vladimir Marko944da602016-02-19 12:27:55 +0000829 CachedSection hash_;
830 CachedSection dynamic_;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000831 Section eh_frame_;
832 Section eh_frame_hdr_;
833 StringSection strtab_;
834 SymbolSection symtab_;
835 Section debug_frame_;
David Srbeckyb851b492015-11-11 20:19:38 +0000836 Section debug_info_;
837 Section debug_line_;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000838 StringSection shstrtab_;
839 std::vector<std::unique_ptr<Section>> other_sections_;
840
841 // List of used section in the order in which they were written.
842 std::vector<Section*> sections_;
843
David Srbecky579942f2016-01-28 20:01:28 +0000844 bool started_;
Vladimir Marko944da602016-02-19 12:27:55 +0000845 bool write_program_headers_;
846
847 // The size of the memory taken by the ELF file when loaded.
848 size_t loaded_size_;
David Srbecky579942f2016-01-28 20:01:28 +0000849
David Srbecky6d8c8f02015-10-26 10:57:09 +0000850 // Used for allocation of virtual address space.
851 Elf_Addr virtual_address_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700852
853 DISALLOW_COPY_AND_ASSIGN(ElfBuilder);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700854};
855
856} // namespace art
857
858#endif // ART_COMPILER_ELF_BUILDER_H_