blob: 2ef9fa1ccb8381542590167573b53f72d5683193 [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"
David Brazdild9c90372016-09-14 16:53:55 +010024#include "base/array_ref.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010025#include "base/bit_utils.h"
David Srbecky6d8c8f02015-10-26 10:57:09 +000026#include "base/casts.h"
David Srbeckybc90fd02015-04-22 19:40:27 +010027#include "base/unix_file/fd_file.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070028#include "elf_utils.h"
David Srbecky6d8c8f02015-10-26 10:57:09 +000029#include "leb128.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000030#include "linker/error_delaying_output_stream.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.
Alexey Alexandrovab40c112016-09-19 09:33:49 -070039// .note.gnu.build-id - Optional build ID section (SHA-1 digest).
David Srbecky6d8c8f02015-10-26 10:57:09 +000040// .rodata - DEX files and oat metadata.
41// .text - Compiled code.
42// .bss - Zero-initialized writeable section.
Douglas Leung316a2182015-09-17 15:26:25 -070043// .MIPS.abiflags - MIPS specific section.
David Srbecky6d8c8f02015-10-26 10:57:09 +000044// .dynstr - Names for .dynsym.
45// .dynsym - A few oat-specific dynamic symbols.
46// .hash - Hash-table for .dynsym.
47// .dynamic - Tags which let the linker locate .dynsym.
48// .strtab - Names for .symtab.
49// .symtab - Debug symbols.
50// .eh_frame - Unwind information (CFI).
51// .eh_frame_hdr - Index of .eh_frame.
52// .debug_frame - Unwind information (CFI).
53// .debug_frame.oat_patches - Addresses for relocation.
54// .debug_info - Debug information.
55// .debug_info.oat_patches - Addresses for relocation.
56// .debug_abbrev - Decoding information for .debug_info.
57// .debug_str - Strings for .debug_info.
58// .debug_line - Line number tables.
59// .debug_line.oat_patches - Addresses for relocation.
60// .text.oat_patches - Addresses for relocation.
61// .shstrtab - Names of ELF sections.
62// Elf_Shdr[] - Section headers.
63//
64// Some section are optional (the debug sections in particular).
65//
66// We try write the section data directly into the file without much
67// in-memory buffering. This means we generally write sections based on the
68// dependency order (e.g. .dynamic points to .dynsym which points to .text).
69//
70// In the cases where we need to buffer, we write the larger section first
71// and buffer the smaller one (e.g. .strtab is bigger than .symtab).
72//
73// The debug sections are written last for easier stripping.
74//
David Srbecky533c2072015-04-22 12:20:22 +010075template <typename ElfTypes>
Andreas Gampe54fc26c2014-09-04 21:47:42 -070076class ElfBuilder FINAL {
77 public:
David Srbecky6d8c8f02015-10-26 10:57:09 +000078 static constexpr size_t kMaxProgramHeaders = 16;
Alexey Alexandrovab40c112016-09-19 09:33:49 -070079 // SHA-1 digest. Not using SHA_DIGEST_LENGTH from openssl/sha.h to avoid
80 // spreading this header dependency for just this single constant.
81 static constexpr size_t kBuildIdLen = 20;
82
David Srbecky533c2072015-04-22 12:20:22 +010083 using Elf_Addr = typename ElfTypes::Addr;
David Srbeckybc90fd02015-04-22 19:40:27 +010084 using Elf_Off = typename ElfTypes::Off;
David Srbecky533c2072015-04-22 12:20:22 +010085 using Elf_Word = typename ElfTypes::Word;
86 using Elf_Sword = typename ElfTypes::Sword;
87 using Elf_Ehdr = typename ElfTypes::Ehdr;
88 using Elf_Shdr = typename ElfTypes::Shdr;
89 using Elf_Sym = typename ElfTypes::Sym;
90 using Elf_Phdr = typename ElfTypes::Phdr;
91 using Elf_Dyn = typename ElfTypes::Dyn;
92
David Srbeckybc90fd02015-04-22 19:40:27 +010093 // Base class of all sections.
David Srbecky6d8c8f02015-10-26 10:57:09 +000094 class Section : public OutputStream {
David Srbecky0c5bbc12015-04-28 17:54:52 +010095 public:
Vladimir Marko944da602016-02-19 12:27:55 +000096 Section(ElfBuilder<ElfTypes>* owner,
97 const std::string& name,
98 Elf_Word type,
99 Elf_Word flags,
100 const Section* link,
101 Elf_Word info,
102 Elf_Word align,
103 Elf_Word entsize)
104 : OutputStream(name),
105 owner_(owner),
106 header_(),
107 section_index_(0),
108 name_(name),
109 link_(link),
110 started_(false),
111 finished_(false),
112 phdr_flags_(PF_R),
113 phdr_type_(0) {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000114 DCHECK_GE(align, 1u);
David Srbecky491a7fe2015-05-28 00:59:08 +0100115 header_.sh_type = type;
116 header_.sh_flags = flags;
117 header_.sh_info = info;
118 header_.sh_addralign = align;
119 header_.sh_entsize = entsize;
David Srbecky0c5bbc12015-04-28 17:54:52 +0100120 }
David Srbecky0c5bbc12015-04-28 17:54:52 +0100121
David Srbecky6d8c8f02015-10-26 10:57:09 +0000122 // Start writing of this section.
123 void Start() {
124 CHECK(!started_);
125 CHECK(!finished_);
126 started_ = true;
127 auto& sections = owner_->sections_;
128 // Check that the previous section is complete.
129 CHECK(sections.empty() || sections.back()->finished_);
130 // The first ELF section index is 1. Index 0 is reserved for NULL.
131 section_index_ = sections.size() + 1;
David Srbecky579942f2016-01-28 20:01:28 +0000132 // Page-align if we switch between allocated and non-allocated sections,
133 // or if we change the type of allocation (e.g. executable vs non-executable).
134 if (!sections.empty()) {
135 if (header_.sh_flags != sections.back()->header_.sh_flags) {
136 header_.sh_addralign = kPageSize;
137 }
138 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000139 // Align file position.
140 if (header_.sh_type != SHT_NOBITS) {
David Srbecky579942f2016-01-28 20:01:28 +0000141 header_.sh_offset = owner_->AlignFileOffset(header_.sh_addralign);
142 } else {
143 header_.sh_offset = 0;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000144 }
145 // Align virtual memory address.
146 if ((header_.sh_flags & SHF_ALLOC) != 0) {
David Srbecky579942f2016-01-28 20:01:28 +0000147 header_.sh_addr = owner_->AlignVirtualAddress(header_.sh_addralign);
148 } else {
149 header_.sh_addr = 0;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000150 }
David Srbecky579942f2016-01-28 20:01:28 +0000151 // Push this section on the list of written sections.
152 sections.push_back(this);
David Srbecky0c5bbc12015-04-28 17:54:52 +0100153 }
154
David Srbecky6d8c8f02015-10-26 10:57:09 +0000155 // Finish writing of this section.
156 void End() {
157 CHECK(started_);
158 CHECK(!finished_);
159 finished_ = true;
160 if (header_.sh_type == SHT_NOBITS) {
161 CHECK_GT(header_.sh_size, 0u);
162 } else {
163 // Use the current file position to determine section size.
Vladimir Marko131980f2015-12-03 18:29:23 +0000164 off_t file_offset = owner_->stream_.Seek(0, kSeekCurrent);
David Srbecky6d8c8f02015-10-26 10:57:09 +0000165 CHECK_GE(file_offset, (off_t)header_.sh_offset);
166 header_.sh_size = file_offset - header_.sh_offset;
167 }
168 if ((header_.sh_flags & SHF_ALLOC) != 0) {
169 owner_->virtual_address_ += header_.sh_size;
170 }
171 }
172
173 // 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,
Vladimir Marko944da602016-02-19 12:27:55 +0000366 Elf_Word size,
367 uint8_t binding,
David Srbecky197160d2016-03-07 17:33:57 +0000368 uint8_t type) {
369 Elf_Word section_index;
370 if (section != nullptr) {
371 DCHECK_LE(section->GetAddress(), addr);
372 DCHECK_LE(addr, section->GetAddress() + section->GetSize());
373 section_index = section->GetSectionIndex();
374 } else {
375 section_index = static_cast<Elf_Word>(SHN_ABS);
376 }
377 Add(name, section_index, addr, size, binding, type);
Vladimir Marko944da602016-02-19 12:27:55 +0000378 }
379
380 void Add(Elf_Word name,
381 Elf_Word section_index,
382 Elf_Addr addr,
383 Elf_Word size,
384 uint8_t binding,
David Srbecky197160d2016-03-07 17:33:57 +0000385 uint8_t type) {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000386 Elf_Sym sym = Elf_Sym();
387 sym.st_name = name;
Vladimir Marko944da602016-02-19 12:27:55 +0000388 sym.st_value = addr;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000389 sym.st_size = size;
David Srbecky197160d2016-03-07 17:33:57 +0000390 sym.st_other = 0;
Vladimir Marko944da602016-02-19 12:27:55 +0000391 sym.st_shndx = section_index;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000392 sym.st_info = (binding << 4) + (type & 0xf);
Vladimir Marko944da602016-02-19 12:27:55 +0000393 CachedSection::Add(&sym, sizeof(sym));
David Srbecky0c5bbc12015-04-28 17:54:52 +0100394 }
David Srbeckybc90fd02015-04-22 19:40:27 +0100395 };
396
Douglas Leung316a2182015-09-17 15:26:25 -0700397 class AbiflagsSection FINAL : public Section {
398 public:
399 // Section with Mips abiflag info.
400 static constexpr uint8_t MIPS_AFL_REG_NONE = 0; // no registers
401 static constexpr uint8_t MIPS_AFL_REG_32 = 1; // 32-bit registers
402 static constexpr uint8_t MIPS_AFL_REG_64 = 2; // 64-bit registers
403 static constexpr uint32_t MIPS_AFL_FLAGS1_ODDSPREG = 1; // Uses odd single-prec fp regs
404 static constexpr uint8_t MIPS_ABI_FP_DOUBLE = 1; // -mdouble-float
405 static constexpr uint8_t MIPS_ABI_FP_XX = 5; // -mfpxx
406 static constexpr uint8_t MIPS_ABI_FP_64A = 7; // -mips32r* -mfp64 -mno-odd-spreg
407
408 AbiflagsSection(ElfBuilder<ElfTypes>* owner,
409 const std::string& name,
410 Elf_Word type,
411 Elf_Word flags,
412 const Section* link,
413 Elf_Word info,
414 Elf_Word align,
415 Elf_Word entsize,
416 InstructionSet isa,
417 const InstructionSetFeatures* features)
418 : Section(owner, name, type, flags, link, info, align, entsize) {
419 if (isa == kMips || isa == kMips64) {
420 bool fpu32 = false; // assume mips64 values
421 uint8_t isa_rev = 6; // assume mips64 values
422 if (isa == kMips) {
423 // adjust for mips32 values
424 fpu32 = features->AsMipsInstructionSetFeatures()->Is32BitFloatingPoint();
425 isa_rev = features->AsMipsInstructionSetFeatures()->IsR6()
426 ? 6
427 : features->AsMipsInstructionSetFeatures()->IsMipsIsaRevGreaterThanEqual2()
428 ? (fpu32 ? 2 : 5)
429 : 1;
430 }
431 abiflags_.version = 0; // version of flags structure
432 abiflags_.isa_level = (isa == kMips) ? 32 : 64;
433 abiflags_.isa_rev = isa_rev;
434 abiflags_.gpr_size = (isa == kMips) ? MIPS_AFL_REG_32 : MIPS_AFL_REG_64;
435 abiflags_.cpr1_size = fpu32 ? MIPS_AFL_REG_32 : MIPS_AFL_REG_64;
436 abiflags_.cpr2_size = MIPS_AFL_REG_NONE;
437 // Set the fp_abi to MIPS_ABI_FP_64A for mips32 with 64-bit FPUs (ie: mips32 R5 and R6).
438 // Otherwise set to MIPS_ABI_FP_DOUBLE.
439 abiflags_.fp_abi = (isa == kMips && !fpu32) ? MIPS_ABI_FP_64A : MIPS_ABI_FP_DOUBLE;
440 abiflags_.isa_ext = 0;
441 abiflags_.ases = 0;
442 // To keep the code simple, we are not using odd FP reg for single floats for both
443 // mips32 and mips64 ART. Therefore we are not setting the MIPS_AFL_FLAGS1_ODDSPREG bit.
444 abiflags_.flags1 = 0;
445 abiflags_.flags2 = 0;
446 }
447 }
448
449 Elf_Word GetSize() const {
450 return sizeof(abiflags_);
451 }
452
453 void Write() {
454 this->WriteFully(&abiflags_, sizeof(abiflags_));
455 }
456
457 private:
458 struct {
459 uint16_t version; // version of this structure
460 uint8_t isa_level, isa_rev, gpr_size, cpr1_size, cpr2_size;
461 uint8_t fp_abi;
462 uint32_t isa_ext, ases, flags1, flags2;
463 } abiflags_;
464 };
465
Alexey Alexandrovab40c112016-09-19 09:33:49 -0700466 class BuildIdSection FINAL : public Section {
467 public:
468 BuildIdSection(ElfBuilder<ElfTypes>* owner,
469 const std::string& name,
470 Elf_Word type,
471 Elf_Word flags,
472 const Section* link,
473 Elf_Word info,
474 Elf_Word align,
475 Elf_Word entsize)
476 : Section(owner, name, type, flags, link, info, align, entsize),
477 digest_start_(-1) {
478 }
479
480 void Write() {
481 // The size fields are 32-bit on both 32-bit and 64-bit systems, confirmed
482 // with the 64-bit linker and libbfd code. The size of name and desc must
483 // be a multiple of 4 and it currently is.
484 this->WriteUint32(4); // namesz.
485 this->WriteUint32(kBuildIdLen); // descsz.
486 this->WriteUint32(3); // type = NT_GNU_BUILD_ID.
487 this->WriteFully("GNU", 4); // name.
488 digest_start_ = this->Seek(0, kSeekCurrent);
489 static_assert(kBuildIdLen % 4 == 0, "expecting a mutliple of 4 for build ID length");
490 this->WriteFully(std::string(kBuildIdLen, '\0').c_str(), kBuildIdLen); // desc.
491 }
492
493 off_t GetDigestStart() {
494 CHECK_GT(digest_start_, 0);
495 return digest_start_;
496 }
497
498 private:
499 bool WriteUint32(uint32_t v) {
500 return this->WriteFully(&v, sizeof(v));
501 }
502
503 // File offset where the build ID digest starts.
504 // Populated with zeros first, then updated with the actual value as the
505 // very last thing in the output file creation.
506 off_t digest_start_;
507 };
508
David Srbecky5d811202016-03-08 13:21:22 +0000509 ElfBuilder(InstructionSet isa, const InstructionSetFeatures* features, OutputStream* output)
Vladimir Marko131980f2015-12-03 18:29:23 +0000510 : isa_(isa),
David Srbecky5d811202016-03-08 13:21:22 +0000511 features_(features),
Vladimir Marko131980f2015-12-03 18:29:23 +0000512 stream_(output),
513 rodata_(this, ".rodata", SHT_PROGBITS, SHF_ALLOC, nullptr, 0, kPageSize, 0),
514 text_(this, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR, nullptr, 0, kPageSize, 0),
515 bss_(this, ".bss", SHT_NOBITS, SHF_ALLOC, nullptr, 0, kPageSize, 0),
516 dynstr_(this, ".dynstr", SHF_ALLOC, kPageSize),
517 dynsym_(this, ".dynsym", SHT_DYNSYM, SHF_ALLOC, &dynstr_),
518 hash_(this, ".hash", SHT_HASH, SHF_ALLOC, &dynsym_, 0, sizeof(Elf_Word), sizeof(Elf_Word)),
519 dynamic_(this, ".dynamic", SHT_DYNAMIC, SHF_ALLOC, &dynstr_, 0, kPageSize, sizeof(Elf_Dyn)),
520 eh_frame_(this, ".eh_frame", SHT_PROGBITS, SHF_ALLOC, nullptr, 0, kPageSize, 0),
521 eh_frame_hdr_(this, ".eh_frame_hdr", SHT_PROGBITS, SHF_ALLOC, nullptr, 0, 4, 0),
David Srbecky579942f2016-01-28 20:01:28 +0000522 strtab_(this, ".strtab", 0, 1),
Vladimir Marko131980f2015-12-03 18:29:23 +0000523 symtab_(this, ".symtab", SHT_SYMTAB, 0, &strtab_),
524 debug_frame_(this, ".debug_frame", SHT_PROGBITS, 0, nullptr, 0, sizeof(Elf_Addr), 0),
525 debug_info_(this, ".debug_info", SHT_PROGBITS, 0, nullptr, 0, 1, 0),
526 debug_line_(this, ".debug_line", SHT_PROGBITS, 0, nullptr, 0, 1, 0),
527 shstrtab_(this, ".shstrtab", 0, 1),
Douglas Leung316a2182015-09-17 15:26:25 -0700528 abiflags_(this, ".MIPS.abiflags", SHT_MIPS_ABIFLAGS, SHF_ALLOC, nullptr, 0, kPageSize, 0,
529 isa, features),
Alexey Alexandrovab40c112016-09-19 09:33:49 -0700530 build_id_(this, ".note.gnu.build-id", SHT_NOTE, SHF_ALLOC, nullptr, 0, 4, 0),
David Srbecky579942f2016-01-28 20:01:28 +0000531 started_(false),
Vladimir Marko944da602016-02-19 12:27:55 +0000532 write_program_headers_(false),
533 loaded_size_(0u),
Vladimir Marko131980f2015-12-03 18:29:23 +0000534 virtual_address_(0) {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000535 text_.phdr_flags_ = PF_R | PF_X;
536 bss_.phdr_flags_ = PF_R | PF_W;
537 dynamic_.phdr_flags_ = PF_R | PF_W;
538 dynamic_.phdr_type_ = PT_DYNAMIC;
539 eh_frame_hdr_.phdr_type_ = PT_GNU_EH_FRAME;
Douglas Leung316a2182015-09-17 15:26:25 -0700540 abiflags_.phdr_type_ = PT_MIPS_ABIFLAGS;
Alexey Alexandrovab40c112016-09-19 09:33:49 -0700541 build_id_.phdr_type_ = PT_NOTE;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700542 }
543 ~ElfBuilder() {}
544
David Srbecky6d8c8f02015-10-26 10:57:09 +0000545 InstructionSet GetIsa() { return isa_; }
546 Section* GetRoData() { return &rodata_; }
547 Section* GetText() { return &text_; }
548 Section* GetBss() { return &bss_; }
549 StringSection* GetStrTab() { return &strtab_; }
550 SymbolSection* GetSymTab() { return &symtab_; }
551 Section* GetEhFrame() { return &eh_frame_; }
552 Section* GetEhFrameHdr() { return &eh_frame_hdr_; }
553 Section* GetDebugFrame() { return &debug_frame_; }
David Srbeckyb851b492015-11-11 20:19:38 +0000554 Section* GetDebugInfo() { return &debug_info_; }
555 Section* GetDebugLine() { return &debug_line_; }
Ian Rogers0279ebb2014-10-08 17:27:48 -0700556
David Srbecky6d8c8f02015-10-26 10:57:09 +0000557 // Encode patch locations as LEB128 list of deltas between consecutive addresses.
558 // (exposed publicly for tests)
Vladimir Marko10c13562015-11-25 14:33:36 +0000559 static void EncodeOatPatches(const ArrayRef<const uintptr_t>& locations,
David Srbecky6d8c8f02015-10-26 10:57:09 +0000560 std::vector<uint8_t>* buffer) {
561 buffer->reserve(buffer->size() + locations.size() * 2); // guess 2 bytes per ULEB128.
562 uintptr_t address = 0; // relative to start of section.
563 for (uintptr_t location : locations) {
564 DCHECK_GE(location, address) << "Patch locations are not in sorted order";
565 EncodeUnsignedLeb128(buffer, dchecked_integral_cast<uint32_t>(location - address));
566 address = location;
567 }
568 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700569
Vladimir Marko10c13562015-11-25 14:33:36 +0000570 void WritePatches(const char* name, const ArrayRef<const uintptr_t>& patch_locations) {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000571 std::vector<uint8_t> buffer;
Vladimir Marko10c13562015-11-25 14:33:36 +0000572 EncodeOatPatches(patch_locations, &buffer);
David Srbecky6d8c8f02015-10-26 10:57:09 +0000573 std::unique_ptr<Section> s(new Section(this, name, SHT_OAT_PATCH, 0, nullptr, 0, 1, 0));
574 s->Start();
575 s->WriteFully(buffer.data(), buffer.size());
576 s->End();
577 other_sections_.push_back(std::move(s));
578 }
David Srbecky527c9c72015-04-17 21:14:10 +0100579
David Srbecky6d8c8f02015-10-26 10:57:09 +0000580 void WriteSection(const char* name, const std::vector<uint8_t>* buffer) {
581 std::unique_ptr<Section> s(new Section(this, name, SHT_PROGBITS, 0, nullptr, 0, 1, 0));
582 s->Start();
583 s->WriteFully(buffer->data(), buffer->size());
584 s->End();
585 other_sections_.push_back(std::move(s));
586 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700587
David Srbecky579942f2016-01-28 20:01:28 +0000588 // Reserve space for ELF header and program headers.
589 // We do not know the number of headers until later, so
590 // it is easiest to just reserve a fixed amount of space.
591 // Program headers are required for loading by the linker.
592 // It is possible to omit them for ELF files used for debugging.
593 void Start(bool write_program_headers = true) {
594 int size = sizeof(Elf_Ehdr);
595 if (write_program_headers) {
596 size += sizeof(Elf_Phdr) * kMaxProgramHeaders;
597 }
Vladimir Marko131980f2015-12-03 18:29:23 +0000598 stream_.Seek(size, kSeekSet);
David Srbecky579942f2016-01-28 20:01:28 +0000599 started_ = true;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000600 virtual_address_ += size;
David Srbecky579942f2016-01-28 20:01:28 +0000601 write_program_headers_ = write_program_headers;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000602 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700603
David Srbecky6d8c8f02015-10-26 10:57:09 +0000604 void End() {
David Srbecky579942f2016-01-28 20:01:28 +0000605 DCHECK(started_);
606
Vladimir Marko944da602016-02-19 12:27:55 +0000607 // Note: loaded_size_ == 0 for tests that don't write .rodata, .text, .bss,
608 // .dynstr, dynsym, .hash and .dynamic. These tests should not read loaded_size_.
609 // TODO: Either refactor the .eh_frame creation so that it counts towards loaded_size_,
610 // or remove all support for .eh_frame. (The currently unused .eh_frame counts towards
611 // the virtual_address_ but we don't consider it for loaded_size_.)
612 CHECK(loaded_size_ == 0 || loaded_size_ == RoundUp(virtual_address_, kPageSize))
613 << loaded_size_ << " " << virtual_address_;
614
David Srbecky6d8c8f02015-10-26 10:57:09 +0000615 // Write section names and finish the section headers.
616 shstrtab_.Start();
617 shstrtab_.Write("");
618 for (auto* section : sections_) {
619 section->header_.sh_name = shstrtab_.Write(section->name_);
620 if (section->link_ != nullptr) {
621 section->header_.sh_link = section->link_->GetSectionIndex();
David Srbeckyb0a962c2015-04-28 19:43:56 +0100622 }
David Srbecky527c9c72015-04-17 21:14:10 +0100623 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000624 shstrtab_.End();
David Srbecky527c9c72015-04-17 21:14:10 +0100625
David Srbecky6d8c8f02015-10-26 10:57:09 +0000626 // Write section headers at the end of the ELF file.
627 std::vector<Elf_Shdr> shdrs;
628 shdrs.reserve(1u + sections_.size());
629 shdrs.push_back(Elf_Shdr()); // NULL at index 0.
630 for (auto* section : sections_) {
631 shdrs.push_back(section->header_);
632 }
633 Elf_Off section_headers_offset;
David Srbecky579942f2016-01-28 20:01:28 +0000634 section_headers_offset = AlignFileOffset(sizeof(Elf_Off));
Vladimir Marko131980f2015-12-03 18:29:23 +0000635 stream_.WriteFully(shdrs.data(), shdrs.size() * sizeof(shdrs[0]));
636
637 // Flush everything else before writing the program headers. This should prevent
638 // the OS from reordering writes, so that we don't end up with valid headers
639 // and partially written data if we suddenly lose power, for example.
640 stream_.Flush();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000641
David Srbecky579942f2016-01-28 20:01:28 +0000642 // The main ELF header.
Douglas Leung316a2182015-09-17 15:26:25 -0700643 Elf_Ehdr elf_header = MakeElfHeader(isa_, features_);
David Srbeckybc90fd02015-04-22 19:40:27 +0100644 elf_header.e_shoff = section_headers_offset;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000645 elf_header.e_shnum = shdrs.size();
David Srbeckybc90fd02015-04-22 19:40:27 +0100646 elf_header.e_shstrndx = shstrtab_.GetSectionIndex();
David Srbecky579942f2016-01-28 20:01:28 +0000647
648 // Program headers (i.e. mmap instructions).
649 std::vector<Elf_Phdr> phdrs;
650 if (write_program_headers_) {
651 phdrs = MakeProgramHeaders();
652 CHECK_LE(phdrs.size(), kMaxProgramHeaders);
653 elf_header.e_phoff = sizeof(Elf_Ehdr);
654 elf_header.e_phnum = phdrs.size();
655 }
656
Vladimir Marko131980f2015-12-03 18:29:23 +0000657 stream_.Seek(0, kSeekSet);
658 stream_.WriteFully(&elf_header, sizeof(elf_header));
659 stream_.WriteFully(phdrs.data(), phdrs.size() * sizeof(phdrs[0]));
660 stream_.Flush();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700661 }
662
David Srbecky6d8c8f02015-10-26 10:57:09 +0000663 // The running program does not have access to section headers
664 // and the loader is not supposed to use them either.
665 // The dynamic sections therefore replicates some of the layout
666 // information like the address and size of .rodata and .text.
667 // It also contains other metadata like the SONAME.
668 // The .dynamic section is found using the PT_DYNAMIC program header.
Vladimir Marko944da602016-02-19 12:27:55 +0000669 void PrepareDynamicSection(const std::string& elf_file_path,
670 Elf_Word rodata_size,
671 Elf_Word text_size,
Vladimir Markoaad75c62016-10-03 08:46:48 +0000672 Elf_Word bss_size,
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100673 Elf_Word bss_methods_offset,
Vladimir Markoaad75c62016-10-03 08:46:48 +0000674 Elf_Word bss_roots_offset) {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000675 std::string soname(elf_file_path);
676 size_t directory_separator_pos = soname.rfind('/');
677 if (directory_separator_pos != std::string::npos) {
678 soname = soname.substr(directory_separator_pos + 1);
679 }
680
Vladimir Marko944da602016-02-19 12:27:55 +0000681 // Calculate addresses of .text, .bss and .dynstr.
682 DCHECK_EQ(rodata_.header_.sh_addralign, static_cast<Elf_Word>(kPageSize));
683 DCHECK_EQ(text_.header_.sh_addralign, static_cast<Elf_Word>(kPageSize));
684 DCHECK_EQ(bss_.header_.sh_addralign, static_cast<Elf_Word>(kPageSize));
685 DCHECK_EQ(dynstr_.header_.sh_addralign, static_cast<Elf_Word>(kPageSize));
686 Elf_Word rodata_address = rodata_.GetAddress();
687 Elf_Word text_address = RoundUp(rodata_address + rodata_size, kPageSize);
688 Elf_Word bss_address = RoundUp(text_address + text_size, kPageSize);
Douglas Leung316a2182015-09-17 15:26:25 -0700689 Elf_Word abiflags_address = RoundUp(bss_address + bss_size, kPageSize);
690 Elf_Word abiflags_size = 0;
691 if (isa_ == kMips || isa_ == kMips64) {
692 abiflags_size = abiflags_.GetSize();
693 }
694 Elf_Word dynstr_address = RoundUp(abiflags_address + abiflags_size, kPageSize);
Vladimir Marko45724f92016-02-17 17:46:10 +0000695
Vladimir Marko944da602016-02-19 12:27:55 +0000696 // Cache .dynstr, .dynsym and .hash data.
697 dynstr_.Add(""); // dynstr should start with empty string.
698 Elf_Word rodata_index = rodata_.GetSectionIndex();
699 Elf_Word oatdata = dynstr_.Add("oatdata");
700 dynsym_.Add(oatdata, rodata_index, rodata_address, rodata_size, STB_GLOBAL, STT_OBJECT);
701 if (text_size != 0u) {
702 Elf_Word text_index = rodata_index + 1u;
703 Elf_Word oatexec = dynstr_.Add("oatexec");
704 dynsym_.Add(oatexec, text_index, text_address, text_size, STB_GLOBAL, STT_OBJECT);
705 Elf_Word oatlastword = dynstr_.Add("oatlastword");
706 Elf_Word oatlastword_address = text_address + text_size - 4;
707 dynsym_.Add(oatlastword, text_index, oatlastword_address, 4, STB_GLOBAL, STT_OBJECT);
708 } else if (rodata_size != 0) {
709 // rodata_ can be size 0 for dwarf_test.
710 Elf_Word oatlastword = dynstr_.Add("oatlastword");
711 Elf_Word oatlastword_address = rodata_address + rodata_size - 4;
712 dynsym_.Add(oatlastword, rodata_index, oatlastword_address, 4, STB_GLOBAL, STT_OBJECT);
713 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000714 DCHECK_LE(bss_roots_offset, bss_size);
Vladimir Marko944da602016-02-19 12:27:55 +0000715 if (bss_size != 0u) {
716 Elf_Word bss_index = rodata_index + 1u + (text_size != 0 ? 1u : 0u);
717 Elf_Word oatbss = dynstr_.Add("oatbss");
Vladimir Markoaad75c62016-10-03 08:46:48 +0000718 dynsym_.Add(oatbss, bss_index, bss_address, bss_roots_offset, STB_GLOBAL, STT_OBJECT);
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100719 DCHECK_LE(bss_methods_offset, bss_roots_offset);
720 DCHECK_LE(bss_roots_offset, bss_size);
721 // Add a symbol marking the start of the methods part of the .bss, if not empty.
722 if (bss_methods_offset != bss_roots_offset) {
723 Elf_Word bss_methods_address = bss_address + bss_methods_offset;
724 Elf_Word bss_methods_size = bss_roots_offset - bss_methods_offset;
725 Elf_Word oatbssroots = dynstr_.Add("oatbssmethods");
726 dynsym_.Add(
727 oatbssroots, bss_index, bss_methods_address, bss_methods_size, STB_GLOBAL, STT_OBJECT);
728 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000729 // Add a symbol marking the start of the GC roots part of the .bss, if not empty.
730 if (bss_roots_offset != bss_size) {
Vladimir Markoaad75c62016-10-03 08:46:48 +0000731 Elf_Word bss_roots_address = bss_address + bss_roots_offset;
732 Elf_Word bss_roots_size = bss_size - bss_roots_offset;
733 Elf_Word oatbssroots = dynstr_.Add("oatbssroots");
734 dynsym_.Add(
735 oatbssroots, bss_index, bss_roots_address, bss_roots_size, STB_GLOBAL, STT_OBJECT);
736 }
Vladimir Marko944da602016-02-19 12:27:55 +0000737 Elf_Word oatbsslastword = dynstr_.Add("oatbsslastword");
738 Elf_Word bsslastword_address = bss_address + bss_size - 4;
739 dynsym_.Add(oatbsslastword, bss_index, bsslastword_address, 4, STB_GLOBAL, STT_OBJECT);
740 }
741 Elf_Word soname_offset = dynstr_.Add(soname);
David Srbecky6d8c8f02015-10-26 10:57:09 +0000742
743 // We do not really need a hash-table since there is so few entries.
744 // However, the hash-table is the only way the linker can actually
745 // determine the number of symbols in .dynsym so it is required.
Vladimir Marko944da602016-02-19 12:27:55 +0000746 int count = dynsym_.GetCacheSize() / sizeof(Elf_Sym); // Includes NULL.
David Srbecky6d8c8f02015-10-26 10:57:09 +0000747 std::vector<Elf_Word> hash;
748 hash.push_back(1); // Number of buckets.
749 hash.push_back(count); // Number of chains.
750 // Buckets. Having just one makes it linear search.
751 hash.push_back(1); // Point to first non-NULL symbol.
752 // Chains. This creates linked list of symbols.
753 hash.push_back(0); // Dummy entry for the NULL symbol.
754 for (int i = 1; i < count - 1; i++) {
755 hash.push_back(i + 1); // Each symbol points to the next one.
756 }
757 hash.push_back(0); // Last symbol terminates the chain.
Vladimir Marko944da602016-02-19 12:27:55 +0000758 hash_.Add(hash.data(), hash.size() * sizeof(hash[0]));
David Srbecky6d8c8f02015-10-26 10:57:09 +0000759
Vladimir Marko944da602016-02-19 12:27:55 +0000760 // Calculate addresses of .dynsym, .hash and .dynamic.
761 DCHECK_EQ(dynstr_.header_.sh_flags, dynsym_.header_.sh_flags);
762 DCHECK_EQ(dynsym_.header_.sh_flags, hash_.header_.sh_flags);
763 Elf_Word dynsym_address =
764 RoundUp(dynstr_address + dynstr_.GetCacheSize(), dynsym_.header_.sh_addralign);
765 Elf_Word hash_address =
766 RoundUp(dynsym_address + dynsym_.GetCacheSize(), hash_.header_.sh_addralign);
767 DCHECK_EQ(dynamic_.header_.sh_addralign, static_cast<Elf_Word>(kPageSize));
768 Elf_Word dynamic_address = RoundUp(hash_address + dynsym_.GetCacheSize(), kPageSize);
769
David Srbecky6d8c8f02015-10-26 10:57:09 +0000770 Elf_Dyn dyns[] = {
Vladimir Marko944da602016-02-19 12:27:55 +0000771 { DT_HASH, { hash_address } },
772 { DT_STRTAB, { dynstr_address } },
773 { DT_SYMTAB, { dynsym_address } },
David Srbecky6d8c8f02015-10-26 10:57:09 +0000774 { DT_SYMENT, { sizeof(Elf_Sym) } },
Vladimir Marko944da602016-02-19 12:27:55 +0000775 { DT_STRSZ, { dynstr_.GetCacheSize() } },
David Srbecky6d8c8f02015-10-26 10:57:09 +0000776 { DT_SONAME, { soname_offset } },
777 { DT_NULL, { 0 } },
778 };
Vladimir Marko944da602016-02-19 12:27:55 +0000779 dynamic_.Add(&dyns, sizeof(dyns));
780
781 loaded_size_ = RoundUp(dynamic_address + dynamic_.GetCacheSize(), kPageSize);
782 }
783
784 void WriteDynamicSection() {
785 dynstr_.WriteCachedSection();
786 dynsym_.WriteCachedSection();
787 hash_.WriteCachedSection();
788 dynamic_.WriteCachedSection();
789
790 CHECK_EQ(loaded_size_, RoundUp(dynamic_.GetAddress() + dynamic_.GetSize(), kPageSize));
791 }
792
793 Elf_Word GetLoadedSize() {
794 CHECK_NE(loaded_size_, 0u);
795 return loaded_size_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700796 }
797
Douglas Leung316a2182015-09-17 15:26:25 -0700798 void WriteMIPSabiflagsSection() {
799 abiflags_.Start();
800 abiflags_.Write();
801 abiflags_.End();
802 }
803
Alexey Alexandrovab40c112016-09-19 09:33:49 -0700804 void WriteBuildIdSection() {
805 build_id_.Start();
806 build_id_.Write();
807 build_id_.End();
808 }
809
810 void WriteBuildId(uint8_t build_id[kBuildIdLen]) {
811 stream_.Seek(build_id_.GetDigestStart(), kSeekSet);
812 stream_.WriteFully(build_id, kBuildIdLen);
813 }
814
David Srbecky6d8c8f02015-10-26 10:57:09 +0000815 // Returns true if all writes and seeks on the output stream succeeded.
816 bool Good() {
Vladimir Marko131980f2015-12-03 18:29:23 +0000817 return stream_.Good();
818 }
819
820 // Returns the builder's internal stream.
821 OutputStream* GetStream() {
822 return &stream_;
David Srbecky527c9c72015-04-17 21:14:10 +0100823 }
824
David Srbecky579942f2016-01-28 20:01:28 +0000825 off_t AlignFileOffset(size_t alignment) {
826 return stream_.Seek(RoundUp(stream_.Seek(0, kSeekCurrent), alignment), kSeekSet);
827 }
828
829 Elf_Addr AlignVirtualAddress(size_t alignment) {
830 return virtual_address_ = RoundUp(virtual_address_, alignment);
831 }
832
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700833 private:
Douglas Leung316a2182015-09-17 15:26:25 -0700834 static Elf_Ehdr MakeElfHeader(InstructionSet isa, const InstructionSetFeatures* features) {
David Srbeckybc90fd02015-04-22 19:40:27 +0100835 Elf_Ehdr elf_header = Elf_Ehdr();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700836 switch (isa) {
837 case kArm:
838 // Fall through.
839 case kThumb2: {
David Srbeckybc90fd02015-04-22 19:40:27 +0100840 elf_header.e_machine = EM_ARM;
841 elf_header.e_flags = EF_ARM_EABI_VER5;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700842 break;
843 }
844 case kArm64: {
David Srbeckybc90fd02015-04-22 19:40:27 +0100845 elf_header.e_machine = EM_AARCH64;
846 elf_header.e_flags = 0;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700847 break;
848 }
849 case kX86: {
David Srbeckybc90fd02015-04-22 19:40:27 +0100850 elf_header.e_machine = EM_386;
851 elf_header.e_flags = 0;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700852 break;
853 }
854 case kX86_64: {
David Srbeckybc90fd02015-04-22 19:40:27 +0100855 elf_header.e_machine = EM_X86_64;
856 elf_header.e_flags = 0;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700857 break;
858 }
859 case kMips: {
David Srbeckybc90fd02015-04-22 19:40:27 +0100860 elf_header.e_machine = EM_MIPS;
861 elf_header.e_flags = (EF_MIPS_NOREORDER |
Douglas Leung316a2182015-09-17 15:26:25 -0700862 EF_MIPS_PIC |
863 EF_MIPS_CPIC |
864 EF_MIPS_ABI_O32 |
Greg Kaiser81a18992016-06-16 15:55:15 -0700865 (features->AsMipsInstructionSetFeatures()->IsR6()
866 ? EF_MIPS_ARCH_32R6
867 : EF_MIPS_ARCH_32R2));
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700868 break;
869 }
Andreas Gampe57b34292015-01-14 15:45:59 -0800870 case kMips64: {
David Srbeckybc90fd02015-04-22 19:40:27 +0100871 elf_header.e_machine = EM_MIPS;
872 elf_header.e_flags = (EF_MIPS_NOREORDER |
Douglas Leung316a2182015-09-17 15:26:25 -0700873 EF_MIPS_PIC |
874 EF_MIPS_CPIC |
875 EF_MIPS_ARCH_64R6);
Andreas Gampe57b34292015-01-14 15:45:59 -0800876 break;
877 }
David Srbeckybc90fd02015-04-22 19:40:27 +0100878 case kNone: {
879 LOG(FATAL) << "No instruction set";
David Srbecky6d8c8f02015-10-26 10:57:09 +0000880 break;
881 }
882 default: {
883 LOG(FATAL) << "Unknown instruction set " << isa;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700884 }
885 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700886
David Srbeckybc90fd02015-04-22 19:40:27 +0100887 elf_header.e_ident[EI_MAG0] = ELFMAG0;
888 elf_header.e_ident[EI_MAG1] = ELFMAG1;
889 elf_header.e_ident[EI_MAG2] = ELFMAG2;
890 elf_header.e_ident[EI_MAG3] = ELFMAG3;
891 elf_header.e_ident[EI_CLASS] = (sizeof(Elf_Addr) == sizeof(Elf32_Addr))
Mathieu Chartier6beced42016-11-15 15:51:31 -0800892 ? ELFCLASS32 : ELFCLASS64;
David Srbeckybc90fd02015-04-22 19:40:27 +0100893 elf_header.e_ident[EI_DATA] = ELFDATA2LSB;
894 elf_header.e_ident[EI_VERSION] = EV_CURRENT;
895 elf_header.e_ident[EI_OSABI] = ELFOSABI_LINUX;
896 elf_header.e_ident[EI_ABIVERSION] = 0;
897 elf_header.e_type = ET_DYN;
898 elf_header.e_version = 1;
899 elf_header.e_entry = 0;
900 elf_header.e_ehsize = sizeof(Elf_Ehdr);
901 elf_header.e_phentsize = sizeof(Elf_Phdr);
902 elf_header.e_shentsize = sizeof(Elf_Shdr);
903 elf_header.e_phoff = sizeof(Elf_Ehdr);
904 return elf_header;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700905 }
906
David Srbecky6d8c8f02015-10-26 10:57:09 +0000907 // Create program headers based on written sections.
908 std::vector<Elf_Phdr> MakeProgramHeaders() {
909 CHECK(!sections_.empty());
910 std::vector<Elf_Phdr> phdrs;
911 {
912 // The program headers must start with PT_PHDR which is used in
913 // loaded process to determine the number of program headers.
914 Elf_Phdr phdr = Elf_Phdr();
915 phdr.p_type = PT_PHDR;
916 phdr.p_flags = PF_R;
917 phdr.p_offset = phdr.p_vaddr = phdr.p_paddr = sizeof(Elf_Ehdr);
918 phdr.p_filesz = phdr.p_memsz = 0; // We need to fill this later.
919 phdr.p_align = sizeof(Elf_Off);
920 phdrs.push_back(phdr);
921 // Tell the linker to mmap the start of file to memory.
922 Elf_Phdr load = Elf_Phdr();
923 load.p_type = PT_LOAD;
924 load.p_flags = PF_R;
925 load.p_offset = load.p_vaddr = load.p_paddr = 0;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000926 load.p_filesz = load.p_memsz = sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * kMaxProgramHeaders;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000927 load.p_align = kPageSize;
928 phdrs.push_back(load);
David Srbeckybc90fd02015-04-22 19:40:27 +0100929 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000930 // Create program headers for sections.
931 for (auto* section : sections_) {
932 const Elf_Shdr& shdr = section->header_;
933 if ((shdr.sh_flags & SHF_ALLOC) != 0 && shdr.sh_size != 0) {
934 // PT_LOAD tells the linker to mmap part of the file.
935 // The linker can only mmap page-aligned sections.
936 // Single PT_LOAD may contain several ELF sections.
937 Elf_Phdr& prev = phdrs.back();
938 Elf_Phdr load = Elf_Phdr();
939 load.p_type = PT_LOAD;
940 load.p_flags = section->phdr_flags_;
941 load.p_offset = shdr.sh_offset;
942 load.p_vaddr = load.p_paddr = shdr.sh_addr;
943 load.p_filesz = (shdr.sh_type != SHT_NOBITS ? shdr.sh_size : 0u);
944 load.p_memsz = shdr.sh_size;
945 load.p_align = shdr.sh_addralign;
946 if (prev.p_type == load.p_type &&
947 prev.p_flags == load.p_flags &&
948 prev.p_filesz == prev.p_memsz && // Do not merge .bss
949 load.p_filesz == load.p_memsz) { // Do not merge .bss
950 // Merge this PT_LOAD with the previous one.
951 Elf_Word size = shdr.sh_offset + shdr.sh_size - prev.p_offset;
952 prev.p_filesz = size;
953 prev.p_memsz = size;
954 } else {
955 // If we are adding new load, it must be aligned.
956 CHECK_EQ(shdr.sh_addralign, (Elf_Word)kPageSize);
957 phdrs.push_back(load);
958 }
959 }
960 }
961 for (auto* section : sections_) {
962 const Elf_Shdr& shdr = section->header_;
963 if ((shdr.sh_flags & SHF_ALLOC) != 0 && shdr.sh_size != 0) {
964 // Other PT_* types allow the program to locate interesting
965 // parts of memory at runtime. They must overlap with PT_LOAD.
966 if (section->phdr_type_ != 0) {
967 Elf_Phdr phdr = Elf_Phdr();
968 phdr.p_type = section->phdr_type_;
969 phdr.p_flags = section->phdr_flags_;
970 phdr.p_offset = shdr.sh_offset;
971 phdr.p_vaddr = phdr.p_paddr = shdr.sh_addr;
972 phdr.p_filesz = phdr.p_memsz = shdr.sh_size;
973 phdr.p_align = shdr.sh_addralign;
974 phdrs.push_back(phdr);
975 }
976 }
977 }
978 // Set the size of the initial PT_PHDR.
979 CHECK_EQ(phdrs[0].p_type, (Elf_Word)PT_PHDR);
980 phdrs[0].p_filesz = phdrs[0].p_memsz = phdrs.size() * sizeof(Elf_Phdr);
David Srbeckybc90fd02015-04-22 19:40:27 +0100981
David Srbecky6d8c8f02015-10-26 10:57:09 +0000982 return phdrs;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700983 }
984
David Srbeckybc90fd02015-04-22 19:40:27 +0100985 InstructionSet isa_;
David Srbecky5d811202016-03-08 13:21:22 +0000986 const InstructionSetFeatures* features_;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000987
Vladimir Marko131980f2015-12-03 18:29:23 +0000988 ErrorDelayingOutputStream stream_;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000989
990 Section rodata_;
991 Section text_;
992 Section bss_;
Vladimir Marko944da602016-02-19 12:27:55 +0000993 CachedStringSection dynstr_;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000994 SymbolSection dynsym_;
Vladimir Marko944da602016-02-19 12:27:55 +0000995 CachedSection hash_;
996 CachedSection dynamic_;
David Srbecky6d8c8f02015-10-26 10:57:09 +0000997 Section eh_frame_;
998 Section eh_frame_hdr_;
999 StringSection strtab_;
1000 SymbolSection symtab_;
1001 Section debug_frame_;
David Srbeckyb851b492015-11-11 20:19:38 +00001002 Section debug_info_;
1003 Section debug_line_;
David Srbecky6d8c8f02015-10-26 10:57:09 +00001004 StringSection shstrtab_;
Douglas Leung316a2182015-09-17 15:26:25 -07001005 AbiflagsSection abiflags_;
Alexey Alexandrovab40c112016-09-19 09:33:49 -07001006 BuildIdSection build_id_;
David Srbecky6d8c8f02015-10-26 10:57:09 +00001007 std::vector<std::unique_ptr<Section>> other_sections_;
1008
1009 // List of used section in the order in which they were written.
1010 std::vector<Section*> sections_;
1011
David Srbecky579942f2016-01-28 20:01:28 +00001012 bool started_;
Vladimir Marko944da602016-02-19 12:27:55 +00001013 bool write_program_headers_;
1014
1015 // The size of the memory taken by the ELF file when loaded.
1016 size_t loaded_size_;
David Srbecky579942f2016-01-28 20:01:28 +00001017
David Srbecky6d8c8f02015-10-26 10:57:09 +00001018 // Used for allocation of virtual address space.
1019 Elf_Addr virtual_address_;
Ian Rogers0279ebb2014-10-08 17:27:48 -07001020
1021 DISALLOW_COPY_AND_ASSIGN(ElfBuilder);
Andreas Gampe54fc26c2014-09-04 21:47:42 -07001022};
1023
1024} // namespace art
1025
1026#endif // ART_COMPILER_ELF_BUILDER_H_