Home | History | Annotate | Download | only in linker
      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_LINKER_ELF_BUILDER_H_
     18 #define ART_COMPILER_LINKER_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/leb128.h"
     28 #include "base/unix_file/fd_file.h"
     29 #include "elf_utils.h"
     30 #include "linker/error_delaying_output_stream.h"
     31 
     32 namespace art {
     33 namespace linker {
     34 
     35 // Writes ELF file.
     36 //
     37 // The basic layout of the elf file:
     38 //   Elf_Ehdr                    - The ELF header.
     39 //   Elf_Phdr[]                  - Program headers for the linker.
     40 //   .note.gnu.build-id          - Optional build ID section (SHA-1 digest).
     41 //   .rodata                     - Oat metadata.
     42 //   .text                       - Compiled code.
     43 //   .bss                        - Zero-initialized writeable section.
     44 //   .dex                        - Reserved NOBITS space for dex-related data.
     45 //   .MIPS.abiflags              - MIPS specific section.
     46 //   .dynstr                     - Names for .dynsym.
     47 //   .dynsym                     - A few oat-specific dynamic symbols.
     48 //   .hash                       - Hash-table for .dynsym.
     49 //   .dynamic                    - Tags which let the linker locate .dynsym.
     50 //   .strtab                     - Names for .symtab.
     51 //   .symtab                     - Debug symbols.
     52 //   .eh_frame                   - Unwind information (CFI).
     53 //   .eh_frame_hdr               - Index of .eh_frame.
     54 //   .debug_frame                - Unwind information (CFI).
     55 //   .debug_frame.oat_patches    - Addresses for relocation.
     56 //   .debug_info                 - Debug information.
     57 //   .debug_info.oat_patches     - Addresses for relocation.
     58 //   .debug_abbrev               - Decoding information for .debug_info.
     59 //   .debug_str                  - Strings for .debug_info.
     60 //   .debug_line                 - Line number tables.
     61 //   .debug_line.oat_patches     - Addresses for relocation.
     62 //   .text.oat_patches           - Addresses for relocation.
     63 //   .shstrtab                   - Names of ELF sections.
     64 //   Elf_Shdr[]                  - Section headers.
     65 //
     66 // Some section are optional (the debug sections in particular).
     67 //
     68 // We try write the section data directly into the file without much
     69 // in-memory buffering.  This means we generally write sections based on the
     70 // dependency order (e.g. .dynamic points to .dynsym which points to .text).
     71 //
     72 // In the cases where we need to buffer, we write the larger section first
     73 // and buffer the smaller one (e.g. .strtab is bigger than .symtab).
     74 //
     75 // The debug sections are written last for easier stripping.
     76 //
     77 template <typename ElfTypes>
     78 class ElfBuilder FINAL {
     79  public:
     80   static constexpr size_t kMaxProgramHeaders = 16;
     81   // SHA-1 digest.  Not using SHA_DIGEST_LENGTH from openssl/sha.h to avoid
     82   // spreading this header dependency for just this single constant.
     83   static constexpr size_t kBuildIdLen = 20;
     84 
     85   using Elf_Addr = typename ElfTypes::Addr;
     86   using Elf_Off = typename ElfTypes::Off;
     87   using Elf_Word = typename ElfTypes::Word;
     88   using Elf_Sword = typename ElfTypes::Sword;
     89   using Elf_Ehdr = typename ElfTypes::Ehdr;
     90   using Elf_Shdr = typename ElfTypes::Shdr;
     91   using Elf_Sym = typename ElfTypes::Sym;
     92   using Elf_Phdr = typename ElfTypes::Phdr;
     93   using Elf_Dyn = typename ElfTypes::Dyn;
     94 
     95   // Base class of all sections.
     96   class Section : public OutputStream {
     97    public:
     98     Section(ElfBuilder<ElfTypes>* owner,
     99             const std::string& name,
    100             Elf_Word type,
    101             Elf_Word flags,
    102             const Section* link,
    103             Elf_Word info,
    104             Elf_Word align,
    105             Elf_Word entsize)
    106         : OutputStream(name),
    107           owner_(owner),
    108           header_(),
    109           section_index_(0),
    110           name_(name),
    111           link_(link),
    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     // Allocate chunk of virtual memory for this section from the owning ElfBuilder.
    123     // This must be done at the start for all SHF_ALLOC sections (i.e. mmaped by linker).
    124     // It is fine to allocate section but never call Start/End() (e.g. the .bss section).
    125     void AllocateVirtualMemory(Elf_Word size) {
    126       AllocateVirtualMemory(owner_->virtual_address_, size);
    127     }
    128 
    129     void AllocateVirtualMemory(Elf_Addr addr, Elf_Word size) {
    130       CHECK_NE(header_.sh_flags & SHF_ALLOC, 0u);
    131       Elf_Word align = AddSection();
    132       CHECK_EQ(header_.sh_addr, 0u);
    133       header_.sh_addr = RoundUp(addr, align);
    134       CHECK(header_.sh_size == 0u || header_.sh_size == size);
    135       header_.sh_size = size;
    136       CHECK_LE(owner_->virtual_address_, header_.sh_addr);
    137       owner_->virtual_address_ = header_.sh_addr + header_.sh_size;
    138     }
    139 
    140     // Start writing file data of this section.
    141     void Start() {
    142       CHECK(owner_->current_section_ == nullptr);
    143       Elf_Word align = AddSection();
    144       CHECK_EQ(header_.sh_offset, 0u);
    145       header_.sh_offset = owner_->AlignFileOffset(align);
    146       owner_->current_section_ = this;
    147     }
    148 
    149     // Finish writing file data of this section.
    150     void End() {
    151       CHECK(owner_->current_section_ == this);
    152       Elf_Word position = GetPosition();
    153       CHECK(header_.sh_size == 0u || header_.sh_size == position);
    154       header_.sh_size = position;
    155       owner_->current_section_ = nullptr;
    156     }
    157 
    158     // Get the number of bytes written so far.
    159     // Only valid while writing the section.
    160     Elf_Word GetPosition() const {
    161       CHECK(owner_->current_section_ == this);
    162       off_t file_offset = owner_->stream_.Seek(0, kSeekCurrent);
    163       DCHECK_GE(file_offset, (off_t)header_.sh_offset);
    164       return file_offset - header_.sh_offset;
    165     }
    166 
    167     // Get the location of this section in virtual memory.
    168     Elf_Addr GetAddress() const {
    169       DCHECK_NE(header_.sh_flags & SHF_ALLOC, 0u);
    170       DCHECK_NE(header_.sh_addr, 0u);
    171       return header_.sh_addr;
    172     }
    173 
    174     // This function always succeeds to simplify code.
    175     // Use builder's Good() to check the actual status.
    176     bool WriteFully(const void* buffer, size_t byte_count) OVERRIDE {
    177       CHECK(owner_->current_section_ == this);
    178       return owner_->stream_.WriteFully(buffer, byte_count);
    179     }
    180 
    181     // This function always succeeds to simplify code.
    182     // Use builder's Good() to check the actual status.
    183     off_t Seek(off_t offset, Whence whence) OVERRIDE {
    184       // Forward the seek as-is and trust the caller to use it reasonably.
    185       return owner_->stream_.Seek(offset, whence);
    186     }
    187 
    188     // This function flushes the output and returns whether it succeeded.
    189     // If there was a previous failure, this does nothing and returns false, i.e. failed.
    190     bool Flush() OVERRIDE {
    191       return owner_->stream_.Flush();
    192     }
    193 
    194     Elf_Word GetSectionIndex() const {
    195       DCHECK_NE(section_index_, 0u);
    196       return section_index_;
    197     }
    198 
    199     // Returns true if this section has been added.
    200     bool Exists() const {
    201       return section_index_ != 0;
    202     }
    203 
    204    protected:
    205     // Add this section to the list of generated ELF sections (if not there already).
    206     // It also ensures the alignment is sufficient to generate valid program headers,
    207     // since that depends on the previous section. It returns the required alignment.
    208     Elf_Word AddSection() {
    209       if (section_index_ == 0) {
    210         std::vector<Section*>& sections = owner_->sections_;
    211         Elf_Word last = sections.empty() ? PF_R : sections.back()->phdr_flags_;
    212         if (phdr_flags_ != last) {
    213           header_.sh_addralign = kPageSize;  // Page-align if R/W/X flags changed.
    214         }
    215         sections.push_back(this);
    216         section_index_ = sections.size();  // First ELF section has index 1.
    217       }
    218       return owner_->write_program_headers_ ? header_.sh_addralign : 1;
    219     }
    220 
    221     ElfBuilder<ElfTypes>* owner_;
    222     Elf_Shdr header_;
    223     Elf_Word section_index_;
    224     const std::string name_;
    225     const Section* const link_;
    226     Elf_Word phdr_flags_;
    227     Elf_Word phdr_type_;
    228 
    229     friend class ElfBuilder;
    230 
    231     DISALLOW_COPY_AND_ASSIGN(Section);
    232   };
    233 
    234   class CachedSection : public Section {
    235    public:
    236     CachedSection(ElfBuilder<ElfTypes>* owner,
    237                   const std::string& name,
    238                   Elf_Word type,
    239                   Elf_Word flags,
    240                   const Section* link,
    241                   Elf_Word info,
    242                   Elf_Word align,
    243                   Elf_Word entsize)
    244         : Section(owner, name, type, flags, link, info, align, entsize), cache_() { }
    245 
    246     Elf_Word Add(const void* data, size_t length) {
    247       Elf_Word offset = cache_.size();
    248       const uint8_t* d = reinterpret_cast<const uint8_t*>(data);
    249       cache_.insert(cache_.end(), d, d + length);
    250       return offset;
    251     }
    252 
    253     Elf_Word GetCacheSize() {
    254       return cache_.size();
    255     }
    256 
    257     void Write() {
    258       this->WriteFully(cache_.data(), cache_.size());
    259       cache_.clear();
    260       cache_.shrink_to_fit();
    261     }
    262 
    263     void WriteCachedSection() {
    264       this->Start();
    265       Write();
    266       this->End();
    267     }
    268 
    269    private:
    270     std::vector<uint8_t> cache_;
    271   };
    272 
    273   // Writer of .dynstr section.
    274   class CachedStringSection FINAL : public CachedSection {
    275    public:
    276     CachedStringSection(ElfBuilder<ElfTypes>* owner,
    277                         const std::string& name,
    278                         Elf_Word flags,
    279                         Elf_Word align)
    280         : CachedSection(owner,
    281                         name,
    282                         SHT_STRTAB,
    283                         flags,
    284                         /* link */ nullptr,
    285                         /* info */ 0,
    286                         align,
    287                         /* entsize */ 0) { }
    288 
    289     Elf_Word Add(const std::string& name) {
    290       if (CachedSection::GetCacheSize() == 0u) {
    291         DCHECK(name.empty());
    292       }
    293       return CachedSection::Add(name.c_str(), name.length() + 1);
    294     }
    295   };
    296 
    297   // Writer of .strtab and .shstrtab sections.
    298   class StringSection FINAL : public Section {
    299    public:
    300     StringSection(ElfBuilder<ElfTypes>* owner,
    301                   const std::string& name,
    302                   Elf_Word flags,
    303                   Elf_Word align)
    304         : Section(owner,
    305                   name,
    306                   SHT_STRTAB,
    307                   flags,
    308                   /* link */ nullptr,
    309                   /* info */ 0,
    310                   align,
    311                   /* entsize */ 0),
    312           current_offset_(0),
    313           last_offset_(0) {
    314     }
    315 
    316     Elf_Word Write(const std::string& name) {
    317       if (current_offset_ == 0) {
    318         DCHECK(name.empty());
    319       } else if (name == last_name_) {
    320         return last_offset_;  // Very simple string de-duplication.
    321       }
    322       last_name_ = name;
    323       last_offset_ = current_offset_;
    324       this->WriteFully(name.c_str(), name.length() + 1);
    325       current_offset_ += name.length() + 1;
    326       return last_offset_;
    327     }
    328 
    329    private:
    330     Elf_Word current_offset_;
    331     std::string last_name_;
    332     Elf_Word last_offset_;
    333   };
    334 
    335   // Writer of .dynsym and .symtab sections.
    336   class SymbolSection FINAL : public Section {
    337    public:
    338     SymbolSection(ElfBuilder<ElfTypes>* owner,
    339                   const std::string& name,
    340                   Elf_Word type,
    341                   Elf_Word flags,
    342                   Section* strtab)
    343         : Section(owner,
    344                   name,
    345                   type,
    346                   flags,
    347                   strtab,
    348                   /* info */ 1,
    349                   sizeof(Elf_Off),
    350                   sizeof(Elf_Sym)) {
    351       syms_.push_back(Elf_Sym());  // The symbol table always has to start with NULL symbol.
    352     }
    353 
    354     // Buffer symbol for this section.  It will be written later.
    355     // If the symbol's section is null, it will be considered absolute (SHN_ABS).
    356     // (we use this in JIT to reference code which is stored outside the debug ELF file)
    357     void Add(Elf_Word name,
    358              const Section* section,
    359              Elf_Addr addr,
    360              Elf_Word size,
    361              uint8_t binding,
    362              uint8_t type) {
    363       Elf_Word section_index;
    364       if (section != nullptr) {
    365         DCHECK_LE(section->GetAddress(), addr);
    366         DCHECK_LE(addr, section->GetAddress() + section->header_.sh_size);
    367         section_index = section->GetSectionIndex();
    368       } else {
    369         section_index = static_cast<Elf_Word>(SHN_ABS);
    370       }
    371       Add(name, section_index, addr, size, binding, type);
    372     }
    373 
    374     // Buffer symbol for this section.  It will be written later.
    375     void Add(Elf_Word name,
    376              Elf_Word section_index,
    377              Elf_Addr addr,
    378              Elf_Word size,
    379              uint8_t binding,
    380              uint8_t type) {
    381       Elf_Sym sym = Elf_Sym();
    382       sym.st_name = name;
    383       sym.st_value = addr;
    384       sym.st_size = size;
    385       sym.st_other = 0;
    386       sym.st_shndx = section_index;
    387       sym.st_info = (binding << 4) + (type & 0xf);
    388       syms_.push_back(sym);
    389 
    390       // The sh_info file must be set to index one-past the last local symbol.
    391       if (binding == STB_LOCAL) {
    392         this->header_.sh_info = syms_.size();
    393       }
    394     }
    395 
    396     Elf_Word GetCacheSize() { return syms_.size() * sizeof(Elf_Sym); }
    397 
    398     void WriteCachedSection() {
    399       this->Start();
    400       this->WriteFully(syms_.data(), syms_.size() * sizeof(Elf_Sym));
    401       this->End();
    402     }
    403 
    404    private:
    405     std::vector<Elf_Sym> syms_;  // Buffered/cached content of the whole section.
    406   };
    407 
    408   class AbiflagsSection FINAL : public Section {
    409    public:
    410     // Section with Mips abiflag info.
    411     static constexpr uint8_t MIPS_AFL_REG_NONE =         0;  // no registers
    412     static constexpr uint8_t MIPS_AFL_REG_32 =           1;  // 32-bit registers
    413     static constexpr uint8_t MIPS_AFL_REG_64 =           2;  // 64-bit registers
    414     static constexpr uint32_t MIPS_AFL_FLAGS1_ODDSPREG = 1;  // Uses odd single-prec fp regs
    415     static constexpr uint8_t MIPS_ABI_FP_DOUBLE =        1;  // -mdouble-float
    416     static constexpr uint8_t MIPS_ABI_FP_XX =            5;  // -mfpxx
    417     static constexpr uint8_t MIPS_ABI_FP_64A =           7;  // -mips32r* -mfp64 -mno-odd-spreg
    418 
    419     AbiflagsSection(ElfBuilder<ElfTypes>* owner,
    420                     const std::string& name,
    421                     Elf_Word type,
    422                     Elf_Word flags,
    423                     const Section* link,
    424                     Elf_Word info,
    425                     Elf_Word align,
    426                     Elf_Word entsize,
    427                     InstructionSet isa,
    428                     const InstructionSetFeatures* features)
    429         : Section(owner, name, type, flags, link, info, align, entsize) {
    430       if (isa == InstructionSet::kMips || isa == InstructionSet::kMips64) {
    431         bool fpu32 = false;    // assume mips64 values
    432         uint8_t isa_rev = 6;   // assume mips64 values
    433         if (isa == InstructionSet::kMips) {
    434           // adjust for mips32 values
    435           fpu32 = features->AsMipsInstructionSetFeatures()->Is32BitFloatingPoint();
    436           isa_rev = features->AsMipsInstructionSetFeatures()->IsR6()
    437               ? 6
    438               : features->AsMipsInstructionSetFeatures()->IsMipsIsaRevGreaterThanEqual2()
    439                   ? (fpu32 ? 2 : 5)
    440                   : 1;
    441         }
    442         abiflags_.version = 0;  // version of flags structure
    443         abiflags_.isa_level = (isa == InstructionSet::kMips) ? 32 : 64;
    444         abiflags_.isa_rev = isa_rev;
    445         abiflags_.gpr_size = (isa == InstructionSet::kMips) ? MIPS_AFL_REG_32 : MIPS_AFL_REG_64;
    446         abiflags_.cpr1_size = fpu32 ? MIPS_AFL_REG_32 : MIPS_AFL_REG_64;
    447         abiflags_.cpr2_size = MIPS_AFL_REG_NONE;
    448         // Set the fp_abi to MIPS_ABI_FP_64A for mips32 with 64-bit FPUs (ie: mips32 R5 and R6).
    449         // Otherwise set to MIPS_ABI_FP_DOUBLE.
    450         abiflags_.fp_abi =
    451             (isa == InstructionSet::kMips && !fpu32) ? MIPS_ABI_FP_64A : MIPS_ABI_FP_DOUBLE;
    452         abiflags_.isa_ext = 0;
    453         abiflags_.ases = 0;
    454         // To keep the code simple, we are not using odd FP reg for single floats for both
    455         // mips32 and mips64 ART. Therefore we are not setting the MIPS_AFL_FLAGS1_ODDSPREG bit.
    456         abiflags_.flags1 = 0;
    457         abiflags_.flags2 = 0;
    458       }
    459     }
    460 
    461     Elf_Word GetSize() const {
    462       return sizeof(abiflags_);
    463     }
    464 
    465     void Write() {
    466       this->WriteFully(&abiflags_, sizeof(abiflags_));
    467     }
    468 
    469    private:
    470     struct {
    471       uint16_t version;  // version of this structure
    472       uint8_t  isa_level, isa_rev, gpr_size, cpr1_size, cpr2_size;
    473       uint8_t  fp_abi;
    474       uint32_t isa_ext, ases, flags1, flags2;
    475     } abiflags_;
    476   };
    477 
    478   class BuildIdSection FINAL : public Section {
    479    public:
    480     BuildIdSection(ElfBuilder<ElfTypes>* owner,
    481                    const std::string& name,
    482                    Elf_Word type,
    483                    Elf_Word flags,
    484                    const Section* link,
    485                    Elf_Word info,
    486                    Elf_Word align,
    487                    Elf_Word entsize)
    488         : Section(owner, name, type, flags, link, info, align, entsize),
    489           digest_start_(-1) {
    490     }
    491 
    492     Elf_Word GetSize() {
    493       return 16 + kBuildIdLen;
    494     }
    495 
    496     void Write() {
    497       // The size fields are 32-bit on both 32-bit and 64-bit systems, confirmed
    498       // with the 64-bit linker and libbfd code. The size of name and desc must
    499       // be a multiple of 4 and it currently is.
    500       this->WriteUint32(4);  // namesz.
    501       this->WriteUint32(kBuildIdLen);  // descsz.
    502       this->WriteUint32(3);  // type = NT_GNU_BUILD_ID.
    503       this->WriteFully("GNU", 4);  // name.
    504       digest_start_ = this->Seek(0, kSeekCurrent);
    505       static_assert(kBuildIdLen % 4 == 0, "expecting a mutliple of 4 for build ID length");
    506       this->WriteFully(std::string(kBuildIdLen, '\0').c_str(), kBuildIdLen);  // desc.
    507       DCHECK_EQ(this->GetPosition(), GetSize());
    508     }
    509 
    510     off_t GetDigestStart() {
    511       CHECK_GT(digest_start_, 0);
    512       return digest_start_;
    513     }
    514 
    515    private:
    516     bool WriteUint32(uint32_t v) {
    517       return this->WriteFully(&v, sizeof(v));
    518     }
    519 
    520     // File offset where the build ID digest starts.
    521     // Populated with zeros first, then updated with the actual value as the
    522     // very last thing in the output file creation.
    523     off_t digest_start_;
    524   };
    525 
    526   ElfBuilder(InstructionSet isa, const InstructionSetFeatures* features, OutputStream* output)
    527       : isa_(isa),
    528         features_(features),
    529         stream_(output),
    530         rodata_(this, ".rodata", SHT_PROGBITS, SHF_ALLOC, nullptr, 0, kPageSize, 0),
    531         text_(this, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR, nullptr, 0, kPageSize, 0),
    532         bss_(this, ".bss", SHT_NOBITS, SHF_ALLOC, nullptr, 0, kPageSize, 0),
    533         dex_(this, ".dex", SHT_NOBITS, SHF_ALLOC, nullptr, 0, kPageSize, 0),
    534         dynstr_(this, ".dynstr", SHF_ALLOC, kPageSize),
    535         dynsym_(this, ".dynsym", SHT_DYNSYM, SHF_ALLOC, &dynstr_),
    536         hash_(this, ".hash", SHT_HASH, SHF_ALLOC, &dynsym_, 0, sizeof(Elf_Word), sizeof(Elf_Word)),
    537         dynamic_(this, ".dynamic", SHT_DYNAMIC, SHF_ALLOC, &dynstr_, 0, kPageSize, sizeof(Elf_Dyn)),
    538         eh_frame_(this, ".eh_frame", SHT_PROGBITS, SHF_ALLOC, nullptr, 0, kPageSize, 0),
    539         eh_frame_hdr_(this, ".eh_frame_hdr", SHT_PROGBITS, SHF_ALLOC, nullptr, 0, 4, 0),
    540         strtab_(this, ".strtab", 0, 1),
    541         symtab_(this, ".symtab", SHT_SYMTAB, 0, &strtab_),
    542         debug_frame_(this, ".debug_frame", SHT_PROGBITS, 0, nullptr, 0, sizeof(Elf_Addr), 0),
    543         debug_info_(this, ".debug_info", SHT_PROGBITS, 0, nullptr, 0, 1, 0),
    544         debug_line_(this, ".debug_line", SHT_PROGBITS, 0, nullptr, 0, 1, 0),
    545         shstrtab_(this, ".shstrtab", 0, 1),
    546         abiflags_(this, ".MIPS.abiflags", SHT_MIPS_ABIFLAGS, SHF_ALLOC, nullptr, 0, kPageSize, 0,
    547                   isa, features),
    548         build_id_(this, ".note.gnu.build-id", SHT_NOTE, SHF_ALLOC, nullptr, 0, 4, 0),
    549         current_section_(nullptr),
    550         started_(false),
    551         write_program_headers_(false),
    552         loaded_size_(0u),
    553         virtual_address_(0) {
    554     text_.phdr_flags_ = PF_R | PF_X;
    555     bss_.phdr_flags_ = PF_R | PF_W;
    556     dex_.phdr_flags_ = PF_R;
    557     dynamic_.phdr_flags_ = PF_R | PF_W;
    558     dynamic_.phdr_type_ = PT_DYNAMIC;
    559     eh_frame_hdr_.phdr_type_ = PT_GNU_EH_FRAME;
    560     abiflags_.phdr_type_ = PT_MIPS_ABIFLAGS;
    561     build_id_.phdr_type_ = PT_NOTE;
    562   }
    563   ~ElfBuilder() {}
    564 
    565   InstructionSet GetIsa() { return isa_; }
    566   BuildIdSection* GetBuildId() { return &build_id_; }
    567   Section* GetRoData() { return &rodata_; }
    568   Section* GetText() { return &text_; }
    569   Section* GetBss() { return &bss_; }
    570   Section* GetDex() { return &dex_; }
    571   StringSection* GetStrTab() { return &strtab_; }
    572   SymbolSection* GetSymTab() { return &symtab_; }
    573   Section* GetEhFrame() { return &eh_frame_; }
    574   Section* GetEhFrameHdr() { return &eh_frame_hdr_; }
    575   Section* GetDebugFrame() { return &debug_frame_; }
    576   Section* GetDebugInfo() { return &debug_info_; }
    577   Section* GetDebugLine() { return &debug_line_; }
    578 
    579   // Encode patch locations as LEB128 list of deltas between consecutive addresses.
    580   // (exposed publicly for tests)
    581   static void EncodeOatPatches(const ArrayRef<const uintptr_t>& locations,
    582                                std::vector<uint8_t>* buffer) {
    583     buffer->reserve(buffer->size() + locations.size() * 2);  // guess 2 bytes per ULEB128.
    584     uintptr_t address = 0;  // relative to start of section.
    585     for (uintptr_t location : locations) {
    586       DCHECK_GE(location, address) << "Patch locations are not in sorted order";
    587       EncodeUnsignedLeb128(buffer, dchecked_integral_cast<uint32_t>(location - address));
    588       address = location;
    589     }
    590   }
    591 
    592   void WritePatches(const char* name, const ArrayRef<const uintptr_t>& patch_locations) {
    593     std::vector<uint8_t> buffer;
    594     EncodeOatPatches(patch_locations, &buffer);
    595     std::unique_ptr<Section> s(new Section(this, name, SHT_OAT_PATCH, 0, nullptr, 0, 1, 0));
    596     s->Start();
    597     s->WriteFully(buffer.data(), buffer.size());
    598     s->End();
    599     other_sections_.push_back(std::move(s));
    600   }
    601 
    602   void WriteSection(const char* name, const std::vector<uint8_t>* buffer) {
    603     std::unique_ptr<Section> s(new Section(this, name, SHT_PROGBITS, 0, nullptr, 0, 1, 0));
    604     s->Start();
    605     s->WriteFully(buffer->data(), buffer->size());
    606     s->End();
    607     other_sections_.push_back(std::move(s));
    608   }
    609 
    610   // Reserve space for ELF header and program headers.
    611   // We do not know the number of headers until later, so
    612   // it is easiest to just reserve a fixed amount of space.
    613   // Program headers are required for loading by the linker.
    614   // It is possible to omit them for ELF files used for debugging.
    615   void Start(bool write_program_headers = true) {
    616     int size = sizeof(Elf_Ehdr);
    617     if (write_program_headers) {
    618       size += sizeof(Elf_Phdr) * kMaxProgramHeaders;
    619     }
    620     stream_.Seek(size, kSeekSet);
    621     started_ = true;
    622     virtual_address_ += size;
    623     write_program_headers_ = write_program_headers;
    624   }
    625 
    626   void End() {
    627     DCHECK(started_);
    628 
    629     // Note: loaded_size_ == 0 for tests that don't write .rodata, .text, .bss,
    630     // .dynstr, dynsym, .hash and .dynamic. These tests should not read loaded_size_.
    631     // TODO: Either refactor the .eh_frame creation so that it counts towards loaded_size_,
    632     // or remove all support for .eh_frame. (The currently unused .eh_frame counts towards
    633     // the virtual_address_ but we don't consider it for loaded_size_.)
    634     CHECK(loaded_size_ == 0 || loaded_size_ == RoundUp(virtual_address_, kPageSize))
    635         << loaded_size_ << " " << virtual_address_;
    636 
    637     // Write section names and finish the section headers.
    638     shstrtab_.Start();
    639     shstrtab_.Write("");
    640     for (auto* section : sections_) {
    641       section->header_.sh_name = shstrtab_.Write(section->name_);
    642       if (section->link_ != nullptr) {
    643         section->header_.sh_link = section->link_->GetSectionIndex();
    644       }
    645       if (section->header_.sh_offset == 0) {
    646         section->header_.sh_type = SHT_NOBITS;
    647       }
    648     }
    649     shstrtab_.End();
    650 
    651     // Write section headers at the end of the ELF file.
    652     std::vector<Elf_Shdr> shdrs;
    653     shdrs.reserve(1u + sections_.size());
    654     shdrs.push_back(Elf_Shdr());  // NULL at index 0.
    655     for (auto* section : sections_) {
    656       shdrs.push_back(section->header_);
    657     }
    658     Elf_Off section_headers_offset;
    659     section_headers_offset = AlignFileOffset(sizeof(Elf_Off));
    660     stream_.WriteFully(shdrs.data(), shdrs.size() * sizeof(shdrs[0]));
    661 
    662     // Flush everything else before writing the program headers. This should prevent
    663     // the OS from reordering writes, so that we don't end up with valid headers
    664     // and partially written data if we suddenly lose power, for example.
    665     stream_.Flush();
    666 
    667     // The main ELF header.
    668     Elf_Ehdr elf_header = MakeElfHeader(isa_, features_);
    669     elf_header.e_shoff = section_headers_offset;
    670     elf_header.e_shnum = shdrs.size();
    671     elf_header.e_shstrndx = shstrtab_.GetSectionIndex();
    672 
    673     // Program headers (i.e. mmap instructions).
    674     std::vector<Elf_Phdr> phdrs;
    675     if (write_program_headers_) {
    676       phdrs = MakeProgramHeaders();
    677       CHECK_LE(phdrs.size(), kMaxProgramHeaders);
    678       elf_header.e_phoff = sizeof(Elf_Ehdr);
    679       elf_header.e_phnum = phdrs.size();
    680     }
    681 
    682     stream_.Seek(0, kSeekSet);
    683     stream_.WriteFully(&elf_header, sizeof(elf_header));
    684     stream_.WriteFully(phdrs.data(), phdrs.size() * sizeof(phdrs[0]));
    685     stream_.Flush();
    686   }
    687 
    688   // The running program does not have access to section headers
    689   // and the loader is not supposed to use them either.
    690   // The dynamic sections therefore replicates some of the layout
    691   // information like the address and size of .rodata and .text.
    692   // It also contains other metadata like the SONAME.
    693   // The .dynamic section is found using the PT_DYNAMIC program header.
    694   void PrepareDynamicSection(const std::string& elf_file_path,
    695                              Elf_Word rodata_size,
    696                              Elf_Word text_size,
    697                              Elf_Word bss_size,
    698                              Elf_Word bss_methods_offset,
    699                              Elf_Word bss_roots_offset,
    700                              Elf_Word dex_size) {
    701     std::string soname(elf_file_path);
    702     size_t directory_separator_pos = soname.rfind('/');
    703     if (directory_separator_pos != std::string::npos) {
    704       soname = soname.substr(directory_separator_pos + 1);
    705     }
    706 
    707     // Allocate all pre-dynamic sections.
    708     rodata_.AllocateVirtualMemory(rodata_size);
    709     text_.AllocateVirtualMemory(text_size);
    710     if (bss_size != 0) {
    711       bss_.AllocateVirtualMemory(bss_size);
    712     }
    713     if (dex_size != 0) {
    714       dex_.AllocateVirtualMemory(dex_size);
    715     }
    716     if (isa_ == InstructionSet::kMips || isa_ == InstructionSet::kMips64) {
    717       abiflags_.AllocateVirtualMemory(abiflags_.GetSize());
    718     }
    719 
    720     // Cache .dynstr, .dynsym and .hash data.
    721     dynstr_.Add("");  // dynstr should start with empty string.
    722     Elf_Word oatdata = dynstr_.Add("oatdata");
    723     dynsym_.Add(oatdata, &rodata_, rodata_.GetAddress(), rodata_size, STB_GLOBAL, STT_OBJECT);
    724     if (text_size != 0u) {
    725       // The runtime does not care about the size of this symbol (it uses the "lastword" symbol).
    726       // We use size 0 (meaning "unknown size" in ELF) to prevent overlap with the debug symbols.
    727       Elf_Word oatexec = dynstr_.Add("oatexec");
    728       dynsym_.Add(oatexec, &text_, text_.GetAddress(), /* size */ 0, STB_GLOBAL, STT_OBJECT);
    729       Elf_Word oatlastword = dynstr_.Add("oatlastword");
    730       Elf_Word oatlastword_address = text_.GetAddress() + text_size - 4;
    731       dynsym_.Add(oatlastword, &text_, oatlastword_address, 4, STB_GLOBAL, STT_OBJECT);
    732     } else if (rodata_size != 0) {
    733       // rodata_ can be size 0 for dwarf_test.
    734       Elf_Word oatlastword = dynstr_.Add("oatlastword");
    735       Elf_Word oatlastword_address = rodata_.GetAddress() + rodata_size - 4;
    736       dynsym_.Add(oatlastword, &rodata_, oatlastword_address, 4, STB_GLOBAL, STT_OBJECT);
    737     }
    738     DCHECK_LE(bss_roots_offset, bss_size);
    739     if (bss_size != 0u) {
    740       Elf_Word oatbss = dynstr_.Add("oatbss");
    741       dynsym_.Add(oatbss, &bss_, bss_.GetAddress(), bss_roots_offset, STB_GLOBAL, STT_OBJECT);
    742       DCHECK_LE(bss_methods_offset, bss_roots_offset);
    743       DCHECK_LE(bss_roots_offset, bss_size);
    744       // Add a symbol marking the start of the methods part of the .bss, if not empty.
    745       if (bss_methods_offset != bss_roots_offset) {
    746         Elf_Word bss_methods_address = bss_.GetAddress() + bss_methods_offset;
    747         Elf_Word bss_methods_size = bss_roots_offset - bss_methods_offset;
    748         Elf_Word oatbssroots = dynstr_.Add("oatbssmethods");
    749         dynsym_.Add(
    750             oatbssroots, &bss_, bss_methods_address, bss_methods_size, STB_GLOBAL, STT_OBJECT);
    751       }
    752       // Add a symbol marking the start of the GC roots part of the .bss, if not empty.
    753       if (bss_roots_offset != bss_size) {
    754         Elf_Word bss_roots_address = bss_.GetAddress() + bss_roots_offset;
    755         Elf_Word bss_roots_size = bss_size - bss_roots_offset;
    756         Elf_Word oatbssroots = dynstr_.Add("oatbssroots");
    757         dynsym_.Add(
    758             oatbssroots, &bss_, bss_roots_address, bss_roots_size, STB_GLOBAL, STT_OBJECT);
    759       }
    760       Elf_Word oatbsslastword = dynstr_.Add("oatbsslastword");
    761       Elf_Word bsslastword_address = bss_.GetAddress() + bss_size - 4;
    762       dynsym_.Add(oatbsslastword, &bss_, bsslastword_address, 4, STB_GLOBAL, STT_OBJECT);
    763     }
    764     if (dex_size != 0u) {
    765       Elf_Word oatdex = dynstr_.Add("oatdex");
    766       dynsym_.Add(oatdex, &dex_, dex_.GetAddress(), /* size */ 0, STB_GLOBAL, STT_OBJECT);
    767       Elf_Word oatdexlastword = dynstr_.Add("oatdexlastword");
    768       Elf_Word oatdexlastword_address = dex_.GetAddress() + dex_size - 4;
    769       dynsym_.Add(oatdexlastword, &dex_, oatdexlastword_address, 4, STB_GLOBAL, STT_OBJECT);
    770     }
    771 
    772     Elf_Word soname_offset = dynstr_.Add(soname);
    773 
    774     // We do not really need a hash-table since there is so few entries.
    775     // However, the hash-table is the only way the linker can actually
    776     // determine the number of symbols in .dynsym so it is required.
    777     int count = dynsym_.GetCacheSize() / sizeof(Elf_Sym);  // Includes NULL.
    778     std::vector<Elf_Word> hash;
    779     hash.push_back(1);  // Number of buckets.
    780     hash.push_back(count);  // Number of chains.
    781     // Buckets.  Having just one makes it linear search.
    782     hash.push_back(1);  // Point to first non-NULL symbol.
    783     // Chains.  This creates linked list of symbols.
    784     hash.push_back(0);  // Dummy entry for the NULL symbol.
    785     for (int i = 1; i < count - 1; i++) {
    786       hash.push_back(i + 1);  // Each symbol points to the next one.
    787     }
    788     hash.push_back(0);  // Last symbol terminates the chain.
    789     hash_.Add(hash.data(), hash.size() * sizeof(hash[0]));
    790 
    791     // Allocate all remaining sections.
    792     dynstr_.AllocateVirtualMemory(dynstr_.GetCacheSize());
    793     dynsym_.AllocateVirtualMemory(dynsym_.GetCacheSize());
    794     hash_.AllocateVirtualMemory(hash_.GetCacheSize());
    795 
    796     Elf_Dyn dyns[] = {
    797       { DT_HASH, { hash_.GetAddress() } },
    798       { DT_STRTAB, { dynstr_.GetAddress() } },
    799       { DT_SYMTAB, { dynsym_.GetAddress() } },
    800       { DT_SYMENT, { sizeof(Elf_Sym) } },
    801       { DT_STRSZ, { dynstr_.GetCacheSize() } },
    802       { DT_SONAME, { soname_offset } },
    803       { DT_NULL, { 0 } },
    804     };
    805     dynamic_.Add(&dyns, sizeof(dyns));
    806     dynamic_.AllocateVirtualMemory(dynamic_.GetCacheSize());
    807 
    808     loaded_size_ = RoundUp(virtual_address_, kPageSize);
    809   }
    810 
    811   void WriteDynamicSection() {
    812     dynstr_.WriteCachedSection();
    813     dynsym_.WriteCachedSection();
    814     hash_.WriteCachedSection();
    815     dynamic_.WriteCachedSection();
    816   }
    817 
    818   Elf_Word GetLoadedSize() {
    819     CHECK_NE(loaded_size_, 0u);
    820     return loaded_size_;
    821   }
    822 
    823   void WriteMIPSabiflagsSection() {
    824     abiflags_.Start();
    825     abiflags_.Write();
    826     abiflags_.End();
    827   }
    828 
    829   void WriteBuildIdSection() {
    830     build_id_.Start();
    831     build_id_.Write();
    832     build_id_.End();
    833   }
    834 
    835   void WriteBuildId(uint8_t build_id[kBuildIdLen]) {
    836     stream_.Seek(build_id_.GetDigestStart(), kSeekSet);
    837     stream_.WriteFully(build_id, kBuildIdLen);
    838   }
    839 
    840   // Returns true if all writes and seeks on the output stream succeeded.
    841   bool Good() {
    842     return stream_.Good();
    843   }
    844 
    845   // Returns the builder's internal stream.
    846   OutputStream* GetStream() {
    847     return &stream_;
    848   }
    849 
    850   off_t AlignFileOffset(size_t alignment) {
    851      return stream_.Seek(RoundUp(stream_.Seek(0, kSeekCurrent), alignment), kSeekSet);
    852   }
    853 
    854  private:
    855   static Elf_Ehdr MakeElfHeader(InstructionSet isa, const InstructionSetFeatures* features) {
    856     Elf_Ehdr elf_header = Elf_Ehdr();
    857     switch (isa) {
    858       case InstructionSet::kArm:
    859         // Fall through.
    860       case InstructionSet::kThumb2: {
    861         elf_header.e_machine = EM_ARM;
    862         elf_header.e_flags = EF_ARM_EABI_VER5;
    863         break;
    864       }
    865       case InstructionSet::kArm64: {
    866         elf_header.e_machine = EM_AARCH64;
    867         elf_header.e_flags = 0;
    868         break;
    869       }
    870       case InstructionSet::kX86: {
    871         elf_header.e_machine = EM_386;
    872         elf_header.e_flags = 0;
    873         break;
    874       }
    875       case InstructionSet::kX86_64: {
    876         elf_header.e_machine = EM_X86_64;
    877         elf_header.e_flags = 0;
    878         break;
    879       }
    880       case InstructionSet::kMips: {
    881         elf_header.e_machine = EM_MIPS;
    882         elf_header.e_flags = (EF_MIPS_NOREORDER |
    883                               EF_MIPS_PIC       |
    884                               EF_MIPS_CPIC      |
    885                               EF_MIPS_ABI_O32   |
    886                               (features->AsMipsInstructionSetFeatures()->IsR6()
    887                                    ? EF_MIPS_ARCH_32R6
    888                                    : EF_MIPS_ARCH_32R2));
    889         break;
    890       }
    891       case InstructionSet::kMips64: {
    892         elf_header.e_machine = EM_MIPS;
    893         elf_header.e_flags = (EF_MIPS_NOREORDER |
    894                               EF_MIPS_PIC       |
    895                               EF_MIPS_CPIC      |
    896                               EF_MIPS_ARCH_64R6);
    897         break;
    898       }
    899       case InstructionSet::kNone: {
    900         LOG(FATAL) << "No instruction set";
    901         break;
    902       }
    903       default: {
    904         LOG(FATAL) << "Unknown instruction set " << isa;
    905       }
    906     }
    907 
    908     elf_header.e_ident[EI_MAG0]       = ELFMAG0;
    909     elf_header.e_ident[EI_MAG1]       = ELFMAG1;
    910     elf_header.e_ident[EI_MAG2]       = ELFMAG2;
    911     elf_header.e_ident[EI_MAG3]       = ELFMAG3;
    912     elf_header.e_ident[EI_CLASS]      = (sizeof(Elf_Addr) == sizeof(Elf32_Addr))
    913                                          ? ELFCLASS32 : ELFCLASS64;
    914     elf_header.e_ident[EI_DATA]       = ELFDATA2LSB;
    915     elf_header.e_ident[EI_VERSION]    = EV_CURRENT;
    916     elf_header.e_ident[EI_OSABI]      = ELFOSABI_LINUX;
    917     elf_header.e_ident[EI_ABIVERSION] = 0;
    918     elf_header.e_type = ET_DYN;
    919     elf_header.e_version = 1;
    920     elf_header.e_entry = 0;
    921     elf_header.e_ehsize = sizeof(Elf_Ehdr);
    922     elf_header.e_phentsize = sizeof(Elf_Phdr);
    923     elf_header.e_shentsize = sizeof(Elf_Shdr);
    924     return elf_header;
    925   }
    926 
    927   // Create program headers based on written sections.
    928   std::vector<Elf_Phdr> MakeProgramHeaders() {
    929     CHECK(!sections_.empty());
    930     std::vector<Elf_Phdr> phdrs;
    931     {
    932       // The program headers must start with PT_PHDR which is used in
    933       // loaded process to determine the number of program headers.
    934       Elf_Phdr phdr = Elf_Phdr();
    935       phdr.p_type    = PT_PHDR;
    936       phdr.p_flags   = PF_R;
    937       phdr.p_offset  = phdr.p_vaddr = phdr.p_paddr = sizeof(Elf_Ehdr);
    938       phdr.p_filesz  = phdr.p_memsz = 0;  // We need to fill this later.
    939       phdr.p_align   = sizeof(Elf_Off);
    940       phdrs.push_back(phdr);
    941       // Tell the linker to mmap the start of file to memory.
    942       Elf_Phdr load = Elf_Phdr();
    943       load.p_type    = PT_LOAD;
    944       load.p_flags   = PF_R;
    945       load.p_offset  = load.p_vaddr = load.p_paddr = 0;
    946       load.p_filesz  = load.p_memsz = sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * kMaxProgramHeaders;
    947       load.p_align   = kPageSize;
    948       phdrs.push_back(load);
    949     }
    950     // Create program headers for sections.
    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         DCHECK(shdr.sh_addr != 0u) << "Allocate virtual memory for the section";
    955         // PT_LOAD tells the linker to mmap part of the file.
    956         // The linker can only mmap page-aligned sections.
    957         // Single PT_LOAD may contain several ELF sections.
    958         Elf_Phdr& prev = phdrs.back();
    959         Elf_Phdr load = Elf_Phdr();
    960         load.p_type   = PT_LOAD;
    961         load.p_flags  = section->phdr_flags_;
    962         load.p_offset = shdr.sh_offset;
    963         load.p_vaddr  = load.p_paddr = shdr.sh_addr;
    964         load.p_filesz = (shdr.sh_type != SHT_NOBITS ? shdr.sh_size : 0u);
    965         load.p_memsz  = shdr.sh_size;
    966         load.p_align  = shdr.sh_addralign;
    967         if (prev.p_type == load.p_type &&
    968             prev.p_flags == load.p_flags &&
    969             prev.p_filesz == prev.p_memsz &&  // Do not merge .bss
    970             load.p_filesz == load.p_memsz) {  // Do not merge .bss
    971           // Merge this PT_LOAD with the previous one.
    972           Elf_Word size = shdr.sh_offset + shdr.sh_size - prev.p_offset;
    973           prev.p_filesz = size;
    974           prev.p_memsz  = size;
    975         } else {
    976           // If we are adding new load, it must be aligned.
    977           CHECK_EQ(shdr.sh_addralign, (Elf_Word)kPageSize);
    978           phdrs.push_back(load);
    979         }
    980       }
    981     }
    982     for (auto* section : sections_) {
    983       const Elf_Shdr& shdr = section->header_;
    984       if ((shdr.sh_flags & SHF_ALLOC) != 0 && shdr.sh_size != 0) {
    985         // Other PT_* types allow the program to locate interesting
    986         // parts of memory at runtime. They must overlap with PT_LOAD.
    987         if (section->phdr_type_ != 0) {
    988           Elf_Phdr phdr = Elf_Phdr();
    989           phdr.p_type   = section->phdr_type_;
    990           phdr.p_flags  = section->phdr_flags_;
    991           phdr.p_offset = shdr.sh_offset;
    992           phdr.p_vaddr  = phdr.p_paddr = shdr.sh_addr;
    993           phdr.p_filesz = phdr.p_memsz = shdr.sh_size;
    994           phdr.p_align  = shdr.sh_addralign;
    995           phdrs.push_back(phdr);
    996         }
    997       }
    998     }
    999     // Set the size of the initial PT_PHDR.
   1000     CHECK_EQ(phdrs[0].p_type, (Elf_Word)PT_PHDR);
   1001     phdrs[0].p_filesz = phdrs[0].p_memsz = phdrs.size() * sizeof(Elf_Phdr);
   1002 
   1003     return phdrs;
   1004   }
   1005 
   1006   InstructionSet isa_;
   1007   const InstructionSetFeatures* features_;
   1008 
   1009   ErrorDelayingOutputStream stream_;
   1010 
   1011   Section rodata_;
   1012   Section text_;
   1013   Section bss_;
   1014   Section dex_;
   1015   CachedStringSection dynstr_;
   1016   SymbolSection dynsym_;
   1017   CachedSection hash_;
   1018   CachedSection dynamic_;
   1019   Section eh_frame_;
   1020   Section eh_frame_hdr_;
   1021   StringSection strtab_;
   1022   SymbolSection symtab_;
   1023   Section debug_frame_;
   1024   Section debug_info_;
   1025   Section debug_line_;
   1026   StringSection shstrtab_;
   1027   AbiflagsSection abiflags_;
   1028   BuildIdSection build_id_;
   1029   std::vector<std::unique_ptr<Section>> other_sections_;
   1030 
   1031   // List of used section in the order in which they were written.
   1032   std::vector<Section*> sections_;
   1033   Section* current_section_;  // The section which is currently being written.
   1034 
   1035   bool started_;
   1036   bool write_program_headers_;
   1037 
   1038   // The size of the memory taken by the ELF file when loaded.
   1039   size_t loaded_size_;
   1040 
   1041   // Used for allocation of virtual address space.
   1042   Elf_Addr virtual_address_;
   1043 
   1044   DISALLOW_COPY_AND_ASSIGN(ElfBuilder);
   1045 };
   1046 
   1047 }  // namespace linker
   1048 }  // namespace art
   1049 
   1050 #endif  // ART_COMPILER_LINKER_ELF_BUILDER_H_
   1051