1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
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 
20 #include <vector>
21 
22 #include "arch/instruction_set.h"
23 #include "arch/mips/instruction_set_features_mips.h"
24 #include "base/array_ref.h"
25 #include "base/bit_utils.h"
26 #include "base/casts.h"
27 #include "base/unix_file/fd_file.h"
28 #include "elf_utils.h"
29 #include "leb128.h"
30 #include "linker/error_delaying_output_stream.h"
31 
32 namespace art {
33 
34 // Writes ELF file.
35 //
36 // The basic layout of the elf file:
37 //   Elf_Ehdr                    - The ELF header.
38 //   Elf_Phdr[]                  - Program headers for the linker.
39 //   .note.gnu.build-id          - Optional build ID section (SHA-1 digest).
40 //   .rodata                     - DEX files and oat metadata.
41 //   .text                       - Compiled code.
42 //   .bss                        - Zero-initialized writeable section.
43 //   .MIPS.abiflags              - MIPS specific section.
44 //   .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 //
75 template <typename ElfTypes>
76 class ElfBuilder FINAL {
77  public:
78   static constexpr size_t kMaxProgramHeaders = 16;
79   // 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 
83   using Elf_Addr = typename ElfTypes::Addr;
84   using Elf_Off = typename ElfTypes::Off;
85   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 
93   // Base class of all sections.
94   class Section : public OutputStream {
95    public:
Section(ElfBuilder<ElfTypes> * owner,const std::string & name,Elf_Word type,Elf_Word flags,const Section * link,Elf_Word info,Elf_Word align,Elf_Word entsize)96     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) {
114       DCHECK_GE(align, 1u);
115       header_.sh_type = type;
116       header_.sh_flags = flags;
117       header_.sh_info = info;
118       header_.sh_addralign = align;
119       header_.sh_entsize = entsize;
120     }
121 
122     // Start writing of this section.
Start()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;
132       // 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       }
139       // Align file position.
140       if (header_.sh_type != SHT_NOBITS) {
141         header_.sh_offset = owner_->AlignFileOffset(header_.sh_addralign);
142       } else {
143         header_.sh_offset = 0;
144       }
145       // Align virtual memory address.
146       if ((header_.sh_flags & SHF_ALLOC) != 0) {
147         header_.sh_addr = owner_->AlignVirtualAddress(header_.sh_addralign);
148       } else {
149         header_.sh_addr = 0;
150       }
151       // Push this section on the list of written sections.
152       sections.push_back(this);
153     }
154 
155     // Finish writing of this section.
End()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.
164         off_t file_offset = owner_->stream_.Seek(0, kSeekCurrent);
165         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.
GetAddress()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.
GetSize()180     Elf_Word GetSize() const {
181       if (finished_) {
182         return header_.sh_size;
183       } else {
184         CHECK(started_);
185         CHECK_NE(header_.sh_type, (Elf_Word)SHT_NOBITS);
186         return owner_->stream_.Seek(0, kSeekCurrent) - header_.sh_offset;
187       }
188     }
189 
190     // 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.
WriteNoBitsSection(Elf_Word size)193     void WriteNoBitsSection(Elf_Word size) {
194       DCHECK_NE(header_.sh_flags & SHF_ALLOC, 0u);
195       header_.sh_type = SHT_NOBITS;
196       Start();
197       header_.sh_size = size;
198       End();
199     }
200 
201     // This function always succeeds to simplify code.
202     // Use builder's Good() to check the actual status.
WriteFully(const void * buffer,size_t byte_count)203     bool WriteFully(const void* buffer, size_t byte_count) OVERRIDE {
204       CHECK(started_);
205       CHECK(!finished_);
206       return owner_->stream_.WriteFully(buffer, byte_count);
207     }
208 
209     // This function always succeeds to simplify code.
210     // Use builder's Good() to check the actual status.
Seek(off_t offset,Whence whence)211     off_t Seek(off_t offset, Whence whence) OVERRIDE {
212       // Forward the seek as-is and trust the caller to use it reasonably.
213       return owner_->stream_.Seek(offset, whence);
214     }
215 
216     // 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.
Flush()218     bool Flush() OVERRIDE {
219       return owner_->stream_.Flush();
220     }
221 
GetSectionIndex()222     Elf_Word GetSectionIndex() const {
223       DCHECK(started_);
224       DCHECK_NE(section_index_, 0u);
225       return section_index_;
226     }
227 
228    private:
229     ElfBuilder<ElfTypes>* owner_;
230     Elf_Shdr header_;
231     Elf_Word section_index_;
232     const std::string name_;
233     const Section* const link_;
234     bool started_;
235     bool finished_;
236     Elf_Word phdr_flags_;
237     Elf_Word phdr_type_;
238 
239     friend class ElfBuilder;
240 
241     DISALLOW_COPY_AND_ASSIGN(Section);
242   };
243 
244   class CachedSection : public Section {
245    public:
CachedSection(ElfBuilder<ElfTypes> * owner,const std::string & name,Elf_Word type,Elf_Word flags,const Section * link,Elf_Word info,Elf_Word align,Elf_Word entsize)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 
Add(const void * data,size_t length)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 
GetCacheSize()263     Elf_Word GetCacheSize() {
264       return cache_.size();
265     }
266 
Write()267     void Write() {
268       this->WriteFully(cache_.data(), cache_.size());
269       cache_.clear();
270       cache_.shrink_to_fit();
271     }
272 
WriteCachedSection()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:
CachedStringSection(ElfBuilder<ElfTypes> * owner,const std::string & name,Elf_Word flags,Elf_Word align)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 
Add(const std::string & name)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.
308   class StringSection FINAL : public Section {
309    public:
StringSection(ElfBuilder<ElfTypes> * owner,const std::string & name,Elf_Word flags,Elf_Word align)310     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),
322           current_offset_(0) {
323     }
324 
Write(const std::string & name)325     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;
332       return offset;
333     }
334 
335    private:
336     Elf_Word current_offset_;
337   };
338 
339   // Writer of .dynsym and .symtab sections.
340   class SymbolSection FINAL : public CachedSection {
341    public:
SymbolSection(ElfBuilder<ElfTypes> * owner,const std::string & name,Elf_Word type,Elf_Word flags,Section * strtab)342     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));
358     }
359 
360     // Buffer symbol for this section.  It will be written later.
361     // 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)
Add(Elf_Word name,const Section * section,Elf_Addr addr,Elf_Word size,uint8_t binding,uint8_t type)363     void Add(Elf_Word name,
364              const Section* section,
365              Elf_Addr addr,
366              Elf_Word size,
367              uint8_t binding,
368              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);
378     }
379 
Add(Elf_Word name,Elf_Word section_index,Elf_Addr addr,Elf_Word size,uint8_t binding,uint8_t type)380     void Add(Elf_Word name,
381              Elf_Word section_index,
382              Elf_Addr addr,
383              Elf_Word size,
384              uint8_t binding,
385              uint8_t type) {
386       Elf_Sym sym = Elf_Sym();
387       sym.st_name = name;
388       sym.st_value = addr;
389       sym.st_size = size;
390       sym.st_other = 0;
391       sym.st_shndx = section_index;
392       sym.st_info = (binding << 4) + (type & 0xf);
393       CachedSection::Add(&sym, sizeof(sym));
394     }
395   };
396 
397   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 
AbiflagsSection(ElfBuilder<ElfTypes> * owner,const std::string & name,Elf_Word type,Elf_Word flags,const Section * link,Elf_Word info,Elf_Word align,Elf_Word entsize,InstructionSet isa,const InstructionSetFeatures * features)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 
GetSize()449     Elf_Word GetSize() const {
450       return sizeof(abiflags_);
451     }
452 
Write()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 
466   class BuildIdSection FINAL : public Section {
467    public:
BuildIdSection(ElfBuilder<ElfTypes> * owner,const std::string & name,Elf_Word type,Elf_Word flags,const Section * link,Elf_Word info,Elf_Word align,Elf_Word entsize)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 
Write()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 
GetDigestStart()493     off_t GetDigestStart() {
494       CHECK_GT(digest_start_, 0);
495       return digest_start_;
496     }
497 
498    private:
WriteUint32(uint32_t v)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 
ElfBuilder(InstructionSet isa,const InstructionSetFeatures * features,OutputStream * output)509   ElfBuilder(InstructionSet isa, const InstructionSetFeatures* features, OutputStream* output)
510       : isa_(isa),
511         features_(features),
512         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),
522         strtab_(this, ".strtab", 0, 1),
523         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),
528         abiflags_(this, ".MIPS.abiflags", SHT_MIPS_ABIFLAGS, SHF_ALLOC, nullptr, 0, kPageSize, 0,
529                   isa, features),
530         build_id_(this, ".note.gnu.build-id", SHT_NOTE, SHF_ALLOC, nullptr, 0, 4, 0),
531         started_(false),
532         write_program_headers_(false),
533         loaded_size_(0u),
534         virtual_address_(0) {
535     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;
540     abiflags_.phdr_type_ = PT_MIPS_ABIFLAGS;
541     build_id_.phdr_type_ = PT_NOTE;
542   }
~ElfBuilder()543   ~ElfBuilder() {}
544 
GetIsa()545   InstructionSet GetIsa() { return isa_; }
GetRoData()546   Section* GetRoData() { return &rodata_; }
GetText()547   Section* GetText() { return &text_; }
GetBss()548   Section* GetBss() { return &bss_; }
GetStrTab()549   StringSection* GetStrTab() { return &strtab_; }
GetSymTab()550   SymbolSection* GetSymTab() { return &symtab_; }
GetEhFrame()551   Section* GetEhFrame() { return &eh_frame_; }
GetEhFrameHdr()552   Section* GetEhFrameHdr() { return &eh_frame_hdr_; }
GetDebugFrame()553   Section* GetDebugFrame() { return &debug_frame_; }
GetDebugInfo()554   Section* GetDebugInfo() { return &debug_info_; }
GetDebugLine()555   Section* GetDebugLine() { return &debug_line_; }
556 
557   // Encode patch locations as LEB128 list of deltas between consecutive addresses.
558   // (exposed publicly for tests)
EncodeOatPatches(const ArrayRef<const uintptr_t> & locations,std::vector<uint8_t> * buffer)559   static void EncodeOatPatches(const ArrayRef<const uintptr_t>& locations,
560                                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   }
569 
WritePatches(const char * name,const ArrayRef<const uintptr_t> & patch_locations)570   void WritePatches(const char* name, const ArrayRef<const uintptr_t>& patch_locations) {
571     std::vector<uint8_t> buffer;
572     EncodeOatPatches(patch_locations, &buffer);
573     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   }
579 
WriteSection(const char * name,const std::vector<uint8_t> * buffer)580   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   }
587 
588   // 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     }
598     stream_.Seek(size, kSeekSet);
599     started_ = true;
600     virtual_address_ += size;
601     write_program_headers_ = write_program_headers;
602   }
603 
End()604   void End() {
605     DCHECK(started_);
606 
607     // 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 
615     // 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();
622       }
623     }
624     shstrtab_.End();
625 
626     // 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;
634     section_headers_offset = AlignFileOffset(sizeof(Elf_Off));
635     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();
641 
642     // The main ELF header.
643     Elf_Ehdr elf_header = MakeElfHeader(isa_, features_);
644     elf_header.e_shoff = section_headers_offset;
645     elf_header.e_shnum = shdrs.size();
646     elf_header.e_shstrndx = shstrtab_.GetSectionIndex();
647 
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 
657     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();
661   }
662 
663   // 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.
PrepareDynamicSection(const std::string & elf_file_path,Elf_Word rodata_size,Elf_Word text_size,Elf_Word bss_size,Elf_Word bss_roots_offset)669   void PrepareDynamicSection(const std::string& elf_file_path,
670                              Elf_Word rodata_size,
671                              Elf_Word text_size,
672                              Elf_Word bss_size,
673                              Elf_Word bss_roots_offset) {
674     std::string soname(elf_file_path);
675     size_t directory_separator_pos = soname.rfind('/');
676     if (directory_separator_pos != std::string::npos) {
677       soname = soname.substr(directory_separator_pos + 1);
678     }
679 
680     // Calculate addresses of .text, .bss and .dynstr.
681     DCHECK_EQ(rodata_.header_.sh_addralign, static_cast<Elf_Word>(kPageSize));
682     DCHECK_EQ(text_.header_.sh_addralign, static_cast<Elf_Word>(kPageSize));
683     DCHECK_EQ(bss_.header_.sh_addralign, static_cast<Elf_Word>(kPageSize));
684     DCHECK_EQ(dynstr_.header_.sh_addralign, static_cast<Elf_Word>(kPageSize));
685     Elf_Word rodata_address = rodata_.GetAddress();
686     Elf_Word text_address = RoundUp(rodata_address + rodata_size, kPageSize);
687     Elf_Word bss_address = RoundUp(text_address + text_size, kPageSize);
688     Elf_Word abiflags_address = RoundUp(bss_address + bss_size, kPageSize);
689     Elf_Word abiflags_size = 0;
690     if (isa_ == kMips || isa_ == kMips64) {
691       abiflags_size = abiflags_.GetSize();
692     }
693     Elf_Word dynstr_address = RoundUp(abiflags_address + abiflags_size, kPageSize);
694 
695     // Cache .dynstr, .dynsym and .hash data.
696     dynstr_.Add("");  // dynstr should start with empty string.
697     Elf_Word rodata_index = rodata_.GetSectionIndex();
698     Elf_Word oatdata = dynstr_.Add("oatdata");
699     dynsym_.Add(oatdata, rodata_index, rodata_address, rodata_size, STB_GLOBAL, STT_OBJECT);
700     if (text_size != 0u) {
701       Elf_Word text_index = rodata_index + 1u;
702       Elf_Word oatexec = dynstr_.Add("oatexec");
703       dynsym_.Add(oatexec, text_index, text_address, text_size, STB_GLOBAL, STT_OBJECT);
704       Elf_Word oatlastword = dynstr_.Add("oatlastword");
705       Elf_Word oatlastword_address = text_address + text_size - 4;
706       dynsym_.Add(oatlastword, text_index, oatlastword_address, 4, STB_GLOBAL, STT_OBJECT);
707     } else if (rodata_size != 0) {
708       // rodata_ can be size 0 for dwarf_test.
709       Elf_Word oatlastword = dynstr_.Add("oatlastword");
710       Elf_Word oatlastword_address = rodata_address + rodata_size - 4;
711       dynsym_.Add(oatlastword, rodata_index, oatlastword_address, 4, STB_GLOBAL, STT_OBJECT);
712     }
713     DCHECK_LE(bss_roots_offset, bss_size);
714     if (bss_size != 0u) {
715       Elf_Word bss_index = rodata_index + 1u + (text_size != 0 ? 1u : 0u);
716       Elf_Word oatbss = dynstr_.Add("oatbss");
717       dynsym_.Add(oatbss, bss_index, bss_address, bss_roots_offset, STB_GLOBAL, STT_OBJECT);
718       // Add a symbol marking the start of the GC roots part of the .bss, if not empty.
719       if (bss_roots_offset != bss_size) {
720         DCHECK_LT(bss_roots_offset, bss_size);
721         Elf_Word bss_roots_address = bss_address + bss_roots_offset;
722         Elf_Word bss_roots_size = bss_size - bss_roots_offset;
723         Elf_Word oatbssroots = dynstr_.Add("oatbssroots");
724         dynsym_.Add(
725             oatbssroots, bss_index, bss_roots_address, bss_roots_size, STB_GLOBAL, STT_OBJECT);
726       }
727       Elf_Word oatbsslastword = dynstr_.Add("oatbsslastword");
728       Elf_Word bsslastword_address = bss_address + bss_size - 4;
729       dynsym_.Add(oatbsslastword, bss_index, bsslastword_address, 4, STB_GLOBAL, STT_OBJECT);
730     }
731     Elf_Word soname_offset = dynstr_.Add(soname);
732 
733     // We do not really need a hash-table since there is so few entries.
734     // However, the hash-table is the only way the linker can actually
735     // determine the number of symbols in .dynsym so it is required.
736     int count = dynsym_.GetCacheSize() / sizeof(Elf_Sym);  // Includes NULL.
737     std::vector<Elf_Word> hash;
738     hash.push_back(1);  // Number of buckets.
739     hash.push_back(count);  // Number of chains.
740     // Buckets.  Having just one makes it linear search.
741     hash.push_back(1);  // Point to first non-NULL symbol.
742     // Chains.  This creates linked list of symbols.
743     hash.push_back(0);  // Dummy entry for the NULL symbol.
744     for (int i = 1; i < count - 1; i++) {
745       hash.push_back(i + 1);  // Each symbol points to the next one.
746     }
747     hash.push_back(0);  // Last symbol terminates the chain.
748     hash_.Add(hash.data(), hash.size() * sizeof(hash[0]));
749 
750     // Calculate addresses of .dynsym, .hash and .dynamic.
751     DCHECK_EQ(dynstr_.header_.sh_flags, dynsym_.header_.sh_flags);
752     DCHECK_EQ(dynsym_.header_.sh_flags, hash_.header_.sh_flags);
753     Elf_Word dynsym_address =
754         RoundUp(dynstr_address + dynstr_.GetCacheSize(), dynsym_.header_.sh_addralign);
755     Elf_Word hash_address =
756         RoundUp(dynsym_address + dynsym_.GetCacheSize(), hash_.header_.sh_addralign);
757     DCHECK_EQ(dynamic_.header_.sh_addralign, static_cast<Elf_Word>(kPageSize));
758     Elf_Word dynamic_address = RoundUp(hash_address + dynsym_.GetCacheSize(), kPageSize);
759 
760     Elf_Dyn dyns[] = {
761       { DT_HASH, { hash_address } },
762       { DT_STRTAB, { dynstr_address } },
763       { DT_SYMTAB, { dynsym_address } },
764       { DT_SYMENT, { sizeof(Elf_Sym) } },
765       { DT_STRSZ, { dynstr_.GetCacheSize() } },
766       { DT_SONAME, { soname_offset } },
767       { DT_NULL, { 0 } },
768     };
769     dynamic_.Add(&dyns, sizeof(dyns));
770 
771     loaded_size_ = RoundUp(dynamic_address + dynamic_.GetCacheSize(), kPageSize);
772   }
773 
WriteDynamicSection()774   void WriteDynamicSection() {
775     dynstr_.WriteCachedSection();
776     dynsym_.WriteCachedSection();
777     hash_.WriteCachedSection();
778     dynamic_.WriteCachedSection();
779 
780     CHECK_EQ(loaded_size_, RoundUp(dynamic_.GetAddress() + dynamic_.GetSize(), kPageSize));
781   }
782 
GetLoadedSize()783   Elf_Word GetLoadedSize() {
784     CHECK_NE(loaded_size_, 0u);
785     return loaded_size_;
786   }
787 
WriteMIPSabiflagsSection()788   void WriteMIPSabiflagsSection() {
789     abiflags_.Start();
790     abiflags_.Write();
791     abiflags_.End();
792   }
793 
WriteBuildIdSection()794   void WriteBuildIdSection() {
795     build_id_.Start();
796     build_id_.Write();
797     build_id_.End();
798   }
799 
WriteBuildId(uint8_t build_id[kBuildIdLen])800   void WriteBuildId(uint8_t build_id[kBuildIdLen]) {
801     stream_.Seek(build_id_.GetDigestStart(), kSeekSet);
802     stream_.WriteFully(build_id, kBuildIdLen);
803   }
804 
805   // Returns true if all writes and seeks on the output stream succeeded.
Good()806   bool Good() {
807     return stream_.Good();
808   }
809 
810   // Returns the builder's internal stream.
GetStream()811   OutputStream* GetStream() {
812     return &stream_;
813   }
814 
AlignFileOffset(size_t alignment)815   off_t AlignFileOffset(size_t alignment) {
816      return stream_.Seek(RoundUp(stream_.Seek(0, kSeekCurrent), alignment), kSeekSet);
817   }
818 
AlignVirtualAddress(size_t alignment)819   Elf_Addr AlignVirtualAddress(size_t alignment) {
820      return virtual_address_ = RoundUp(virtual_address_, alignment);
821   }
822 
823  private:
MakeElfHeader(InstructionSet isa,const InstructionSetFeatures * features)824   static Elf_Ehdr MakeElfHeader(InstructionSet isa, const InstructionSetFeatures* features) {
825     Elf_Ehdr elf_header = Elf_Ehdr();
826     switch (isa) {
827       case kArm:
828         // Fall through.
829       case kThumb2: {
830         elf_header.e_machine = EM_ARM;
831         elf_header.e_flags = EF_ARM_EABI_VER5;
832         break;
833       }
834       case kArm64: {
835         elf_header.e_machine = EM_AARCH64;
836         elf_header.e_flags = 0;
837         break;
838       }
839       case kX86: {
840         elf_header.e_machine = EM_386;
841         elf_header.e_flags = 0;
842         break;
843       }
844       case kX86_64: {
845         elf_header.e_machine = EM_X86_64;
846         elf_header.e_flags = 0;
847         break;
848       }
849       case kMips: {
850         elf_header.e_machine = EM_MIPS;
851         elf_header.e_flags = (EF_MIPS_NOREORDER |
852                               EF_MIPS_PIC       |
853                               EF_MIPS_CPIC      |
854                               EF_MIPS_ABI_O32   |
855                               (features->AsMipsInstructionSetFeatures()->IsR6()
856                                    ? EF_MIPS_ARCH_32R6
857                                    : EF_MIPS_ARCH_32R2));
858         break;
859       }
860       case kMips64: {
861         elf_header.e_machine = EM_MIPS;
862         elf_header.e_flags = (EF_MIPS_NOREORDER |
863                               EF_MIPS_PIC       |
864                               EF_MIPS_CPIC      |
865                               EF_MIPS_ARCH_64R6);
866         break;
867       }
868       case kNone: {
869         LOG(FATAL) << "No instruction set";
870         break;
871       }
872       default: {
873         LOG(FATAL) << "Unknown instruction set " << isa;
874       }
875     }
876 
877     elf_header.e_ident[EI_MAG0]       = ELFMAG0;
878     elf_header.e_ident[EI_MAG1]       = ELFMAG1;
879     elf_header.e_ident[EI_MAG2]       = ELFMAG2;
880     elf_header.e_ident[EI_MAG3]       = ELFMAG3;
881     elf_header.e_ident[EI_CLASS]      = (sizeof(Elf_Addr) == sizeof(Elf32_Addr))
882                                          ? ELFCLASS32 : ELFCLASS64;
883     elf_header.e_ident[EI_DATA]       = ELFDATA2LSB;
884     elf_header.e_ident[EI_VERSION]    = EV_CURRENT;
885     elf_header.e_ident[EI_OSABI]      = ELFOSABI_LINUX;
886     elf_header.e_ident[EI_ABIVERSION] = 0;
887     elf_header.e_type = ET_DYN;
888     elf_header.e_version = 1;
889     elf_header.e_entry = 0;
890     elf_header.e_ehsize = sizeof(Elf_Ehdr);
891     elf_header.e_phentsize = sizeof(Elf_Phdr);
892     elf_header.e_shentsize = sizeof(Elf_Shdr);
893     elf_header.e_phoff = sizeof(Elf_Ehdr);
894     return elf_header;
895   }
896 
897   // Create program headers based on written sections.
MakeProgramHeaders()898   std::vector<Elf_Phdr> MakeProgramHeaders() {
899     CHECK(!sections_.empty());
900     std::vector<Elf_Phdr> phdrs;
901     {
902       // The program headers must start with PT_PHDR which is used in
903       // loaded process to determine the number of program headers.
904       Elf_Phdr phdr = Elf_Phdr();
905       phdr.p_type    = PT_PHDR;
906       phdr.p_flags   = PF_R;
907       phdr.p_offset  = phdr.p_vaddr = phdr.p_paddr = sizeof(Elf_Ehdr);
908       phdr.p_filesz  = phdr.p_memsz = 0;  // We need to fill this later.
909       phdr.p_align   = sizeof(Elf_Off);
910       phdrs.push_back(phdr);
911       // Tell the linker to mmap the start of file to memory.
912       Elf_Phdr load = Elf_Phdr();
913       load.p_type    = PT_LOAD;
914       load.p_flags   = PF_R;
915       load.p_offset  = load.p_vaddr = load.p_paddr = 0;
916       load.p_filesz  = load.p_memsz = sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * kMaxProgramHeaders;
917       load.p_align   = kPageSize;
918       phdrs.push_back(load);
919     }
920     // Create program headers for sections.
921     for (auto* section : sections_) {
922       const Elf_Shdr& shdr = section->header_;
923       if ((shdr.sh_flags & SHF_ALLOC) != 0 && shdr.sh_size != 0) {
924         // PT_LOAD tells the linker to mmap part of the file.
925         // The linker can only mmap page-aligned sections.
926         // Single PT_LOAD may contain several ELF sections.
927         Elf_Phdr& prev = phdrs.back();
928         Elf_Phdr load = Elf_Phdr();
929         load.p_type   = PT_LOAD;
930         load.p_flags  = section->phdr_flags_;
931         load.p_offset = shdr.sh_offset;
932         load.p_vaddr  = load.p_paddr = shdr.sh_addr;
933         load.p_filesz = (shdr.sh_type != SHT_NOBITS ? shdr.sh_size : 0u);
934         load.p_memsz  = shdr.sh_size;
935         load.p_align  = shdr.sh_addralign;
936         if (prev.p_type == load.p_type &&
937             prev.p_flags == load.p_flags &&
938             prev.p_filesz == prev.p_memsz &&  // Do not merge .bss
939             load.p_filesz == load.p_memsz) {  // Do not merge .bss
940           // Merge this PT_LOAD with the previous one.
941           Elf_Word size = shdr.sh_offset + shdr.sh_size - prev.p_offset;
942           prev.p_filesz = size;
943           prev.p_memsz  = size;
944         } else {
945           // If we are adding new load, it must be aligned.
946           CHECK_EQ(shdr.sh_addralign, (Elf_Word)kPageSize);
947           phdrs.push_back(load);
948         }
949       }
950     }
951     for (auto* section : sections_) {
952       const Elf_Shdr& shdr = section->header_;
953       if ((shdr.sh_flags & SHF_ALLOC) != 0 && shdr.sh_size != 0) {
954         // Other PT_* types allow the program to locate interesting
955         // parts of memory at runtime. They must overlap with PT_LOAD.
956         if (section->phdr_type_ != 0) {
957           Elf_Phdr phdr = Elf_Phdr();
958           phdr.p_type   = section->phdr_type_;
959           phdr.p_flags  = section->phdr_flags_;
960           phdr.p_offset = shdr.sh_offset;
961           phdr.p_vaddr  = phdr.p_paddr = shdr.sh_addr;
962           phdr.p_filesz = phdr.p_memsz = shdr.sh_size;
963           phdr.p_align  = shdr.sh_addralign;
964           phdrs.push_back(phdr);
965         }
966       }
967     }
968     // Set the size of the initial PT_PHDR.
969     CHECK_EQ(phdrs[0].p_type, (Elf_Word)PT_PHDR);
970     phdrs[0].p_filesz = phdrs[0].p_memsz = phdrs.size() * sizeof(Elf_Phdr);
971 
972     return phdrs;
973   }
974 
975   InstructionSet isa_;
976   const InstructionSetFeatures* features_;
977 
978   ErrorDelayingOutputStream stream_;
979 
980   Section rodata_;
981   Section text_;
982   Section bss_;
983   CachedStringSection dynstr_;
984   SymbolSection dynsym_;
985   CachedSection hash_;
986   CachedSection dynamic_;
987   Section eh_frame_;
988   Section eh_frame_hdr_;
989   StringSection strtab_;
990   SymbolSection symtab_;
991   Section debug_frame_;
992   Section debug_info_;
993   Section debug_line_;
994   StringSection shstrtab_;
995   AbiflagsSection abiflags_;
996   BuildIdSection build_id_;
997   std::vector<std::unique_ptr<Section>> other_sections_;
998 
999   // List of used section in the order in which they were written.
1000   std::vector<Section*> sections_;
1001 
1002   bool started_;
1003   bool write_program_headers_;
1004 
1005   // The size of the memory taken by the ELF file when loaded.
1006   size_t loaded_size_;
1007 
1008   // Used for allocation of virtual address space.
1009   Elf_Addr virtual_address_;
1010 
1011   DISALLOW_COPY_AND_ASSIGN(ElfBuilder);
1012 };
1013 
1014 }  // namespace art
1015 
1016 #endif  // ART_COMPILER_ELF_BUILDER_H_
1017