elf_builder.h revision 6065402316da2b51eed5fc34cffbd991766bd408
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 "base/bit_utils.h"
24#include "base/casts.h"
25#include "base/unix_file/fd_file.h"
26#include "elf_utils.h"
27#include "leb128.h"
28#include "linker/error_delaying_output_stream.h"
29#include "utils/array_ref.h"
30
31namespace art {
32
33// Writes ELF file.
34//
35// The basic layout of the elf file:
36//   Elf_Ehdr                    - The ELF header.
37//   Elf_Phdr[]                  - Program headers for the linker.
38//   .rodata                     - DEX files and oat metadata.
39//   .text                       - Compiled code.
40//   .bss                        - Zero-initialized writeable section.
41//   .dynstr                     - Names for .dynsym.
42//   .dynsym                     - A few oat-specific dynamic symbols.
43//   .hash                       - Hash-table for .dynsym.
44//   .dynamic                    - Tags which let the linker locate .dynsym.
45//   .strtab                     - Names for .symtab.
46//   .symtab                     - Debug symbols.
47//   .eh_frame                   - Unwind information (CFI).
48//   .eh_frame_hdr               - Index of .eh_frame.
49//   .debug_frame                - Unwind information (CFI).
50//   .debug_frame.oat_patches    - Addresses for relocation.
51//   .debug_info                 - Debug information.
52//   .debug_info.oat_patches     - Addresses for relocation.
53//   .debug_abbrev               - Decoding information for .debug_info.
54//   .debug_str                  - Strings for .debug_info.
55//   .debug_line                 - Line number tables.
56//   .debug_line.oat_patches     - Addresses for relocation.
57//   .text.oat_patches           - Addresses for relocation.
58//   .shstrtab                   - Names of ELF sections.
59//   Elf_Shdr[]                  - Section headers.
60//
61// Some section are optional (the debug sections in particular).
62//
63// We try write the section data directly into the file without much
64// in-memory buffering.  This means we generally write sections based on the
65// dependency order (e.g. .dynamic points to .dynsym which points to .text).
66//
67// In the cases where we need to buffer, we write the larger section first
68// and buffer the smaller one (e.g. .strtab is bigger than .symtab).
69//
70// The debug sections are written last for easier stripping.
71//
72template <typename ElfTypes>
73class ElfBuilder FINAL {
74 public:
75  static constexpr size_t kMaxProgramHeaders = 16;
76  using Elf_Addr = typename ElfTypes::Addr;
77  using Elf_Off = typename ElfTypes::Off;
78  using Elf_Word = typename ElfTypes::Word;
79  using Elf_Sword = typename ElfTypes::Sword;
80  using Elf_Ehdr = typename ElfTypes::Ehdr;
81  using Elf_Shdr = typename ElfTypes::Shdr;
82  using Elf_Sym = typename ElfTypes::Sym;
83  using Elf_Phdr = typename ElfTypes::Phdr;
84  using Elf_Dyn = typename ElfTypes::Dyn;
85
86  // Base class of all sections.
87  class Section : public OutputStream {
88   public:
89    Section(ElfBuilder<ElfTypes>* owner,
90            const std::string& name,
91            Elf_Word type,
92            Elf_Word flags,
93            const Section* link,
94            Elf_Word info,
95            Elf_Word align,
96            Elf_Word entsize)
97        : OutputStream(name),
98          owner_(owner),
99          header_(),
100          section_index_(0),
101          name_(name),
102          link_(link),
103          started_(false),
104          finished_(false),
105          phdr_flags_(PF_R),
106          phdr_type_(0) {
107      DCHECK_GE(align, 1u);
108      header_.sh_type = type;
109      header_.sh_flags = flags;
110      header_.sh_info = info;
111      header_.sh_addralign = align;
112      header_.sh_entsize = entsize;
113    }
114
115    // Start writing of this section.
116    void Start() {
117      CHECK(!started_);
118      CHECK(!finished_);
119      started_ = true;
120      auto& sections = owner_->sections_;
121      // Check that the previous section is complete.
122      CHECK(sections.empty() || sections.back()->finished_);
123      // The first ELF section index is 1. Index 0 is reserved for NULL.
124      section_index_ = sections.size() + 1;
125      // Page-align if we switch between allocated and non-allocated sections,
126      // or if we change the type of allocation (e.g. executable vs non-executable).
127      if (!sections.empty()) {
128        if (header_.sh_flags != sections.back()->header_.sh_flags) {
129          header_.sh_addralign = kPageSize;
130        }
131      }
132      // Align file position.
133      if (header_.sh_type != SHT_NOBITS) {
134        header_.sh_offset = owner_->AlignFileOffset(header_.sh_addralign);
135      } else {
136        header_.sh_offset = 0;
137      }
138      // Align virtual memory address.
139      if ((header_.sh_flags & SHF_ALLOC) != 0) {
140        header_.sh_addr = owner_->AlignVirtualAddress(header_.sh_addralign);
141      } else {
142        header_.sh_addr = 0;
143      }
144      // Push this section on the list of written sections.
145      sections.push_back(this);
146    }
147
148    // Finish writing of this section.
149    void End() {
150      CHECK(started_);
151      CHECK(!finished_);
152      finished_ = true;
153      if (header_.sh_type == SHT_NOBITS) {
154        CHECK_GT(header_.sh_size, 0u);
155      } else {
156        // Use the current file position to determine section size.
157        off_t file_offset = owner_->stream_.Seek(0, kSeekCurrent);
158        CHECK_GE(file_offset, (off_t)header_.sh_offset);
159        header_.sh_size = file_offset - header_.sh_offset;
160      }
161      if ((header_.sh_flags & SHF_ALLOC) != 0) {
162        owner_->virtual_address_ += header_.sh_size;
163      }
164    }
165
166    // Returns true if the section was written to disk.
167    // (Used to check whether we have .text when writing JIT debug info)
168    bool Exists() const {
169      return finished_;
170    }
171
172    // Get the location of this section in virtual memory.
173    Elf_Addr GetAddress() const {
174      CHECK(started_);
175      return header_.sh_addr;
176    }
177
178    // Returns the size of the content of this section.
179    Elf_Word GetSize() const {
180      if (finished_) {
181        return header_.sh_size;
182      } else {
183        CHECK(started_);
184        CHECK_NE(header_.sh_type, (Elf_Word)SHT_NOBITS);
185        return owner_->stream_.Seek(0, kSeekCurrent) - header_.sh_offset;
186      }
187    }
188
189    // Write this section as "NOBITS" section. (used for the .bss section)
190    // This means that the ELF file does not contain the initial data for this section
191    // and it will be zero-initialized when the ELF file is loaded in the running program.
192    void WriteNoBitsSection(Elf_Word size) {
193      DCHECK_NE(header_.sh_flags & SHF_ALLOC, 0u);
194      header_.sh_type = SHT_NOBITS;
195      Start();
196      header_.sh_size = size;
197      End();
198    }
199
200    // This function always succeeds to simplify code.
201    // Use builder's Good() to check the actual status.
202    bool WriteFully(const void* buffer, size_t byte_count) OVERRIDE {
203      CHECK(started_);
204      CHECK(!finished_);
205      return owner_->stream_.WriteFully(buffer, byte_count);
206    }
207
208    // This function always succeeds to simplify code.
209    // Use builder's Good() to check the actual status.
210    off_t Seek(off_t offset, Whence whence) OVERRIDE {
211      // Forward the seek as-is and trust the caller to use it reasonably.
212      return owner_->stream_.Seek(offset, whence);
213    }
214
215    // This function flushes the output and returns whether it succeeded.
216    // If there was a previous failure, this does nothing and returns false, i.e. failed.
217    bool Flush() OVERRIDE {
218      return owner_->stream_.Flush();
219    }
220
221    Elf_Word GetSectionIndex() const {
222      DCHECK(started_);
223      DCHECK_NE(section_index_, 0u);
224      return section_index_;
225    }
226
227   private:
228    ElfBuilder<ElfTypes>* owner_;
229    Elf_Shdr header_;
230    Elf_Word section_index_;
231    const std::string name_;
232    const Section* const link_;
233    bool started_;
234    bool finished_;
235    Elf_Word phdr_flags_;
236    Elf_Word phdr_type_;
237
238    friend class ElfBuilder;
239
240    DISALLOW_COPY_AND_ASSIGN(Section);
241  };
242
243  class CachedSection : public Section {
244   public:
245    CachedSection(ElfBuilder<ElfTypes>* owner,
246                  const std::string& name,
247                  Elf_Word type,
248                  Elf_Word flags,
249                  const Section* link,
250                  Elf_Word info,
251                  Elf_Word align,
252                  Elf_Word entsize)
253        : Section(owner, name, type, flags, link, info, align, entsize), cache_() { }
254
255    Elf_Word Add(const void* data, size_t length) {
256      Elf_Word offset = cache_.size();
257      const uint8_t* d = reinterpret_cast<const uint8_t*>(data);
258      cache_.insert(cache_.end(), d, d + length);
259      return offset;
260    }
261
262    Elf_Word GetCacheSize() {
263      return cache_.size();
264    }
265
266    void Write() {
267      this->WriteFully(cache_.data(), cache_.size());
268      cache_.clear();
269      cache_.shrink_to_fit();
270    }
271
272    void WriteCachedSection() {
273      this->Start();
274      Write();
275      this->End();
276    }
277
278   private:
279    std::vector<uint8_t> cache_;
280  };
281
282  // Writer of .dynstr section.
283  class CachedStringSection FINAL : public CachedSection {
284   public:
285    CachedStringSection(ElfBuilder<ElfTypes>* owner,
286                        const std::string& name,
287                        Elf_Word flags,
288                        Elf_Word align)
289        : CachedSection(owner,
290                        name,
291                        SHT_STRTAB,
292                        flags,
293                        /* link */ nullptr,
294                        /* info */ 0,
295                        align,
296                        /* entsize */ 0) { }
297
298    Elf_Word Add(const std::string& name) {
299      if (CachedSection::GetCacheSize() == 0u) {
300        DCHECK(name.empty());
301      }
302      return CachedSection::Add(name.c_str(), name.length() + 1);
303    }
304  };
305
306  // Writer of .strtab and .shstrtab sections.
307  class StringSection FINAL : public Section {
308   public:
309    StringSection(ElfBuilder<ElfTypes>* owner,
310                  const std::string& name,
311                  Elf_Word flags,
312                  Elf_Word align)
313        : Section(owner,
314                  name,
315                  SHT_STRTAB,
316                  flags,
317                  /* link */ nullptr,
318                  /* info */ 0,
319                  align,
320                  /* entsize */ 0),
321          current_offset_(0) {
322    }
323
324    Elf_Word Write(const std::string& name) {
325      if (current_offset_ == 0) {
326        DCHECK(name.empty());
327      }
328      Elf_Word offset = current_offset_;
329      this->WriteFully(name.c_str(), name.length() + 1);
330      current_offset_ += name.length() + 1;
331      return offset;
332    }
333
334   private:
335    Elf_Word current_offset_;
336  };
337
338  // Writer of .dynsym and .symtab sections.
339  class SymbolSection FINAL : public CachedSection {
340   public:
341    SymbolSection(ElfBuilder<ElfTypes>* owner,
342                  const std::string& name,
343                  Elf_Word type,
344                  Elf_Word flags,
345                  Section* strtab)
346        : CachedSection(owner,
347                        name,
348                        type,
349                        flags,
350                        strtab,
351                        /* info */ 0,
352                        sizeof(Elf_Off),
353                        sizeof(Elf_Sym)) {
354      // The symbol table always has to start with NULL symbol.
355      Elf_Sym null_symbol = Elf_Sym();
356      CachedSection::Add(&null_symbol, sizeof(null_symbol));
357    }
358
359    // Buffer symbol for this section.  It will be written later.
360    // If the symbol's section is null, it will be considered absolute (SHN_ABS).
361    // (we use this in JIT to reference code which is stored outside the debug ELF file)
362    void Add(Elf_Word name,
363             const Section* section,
364             Elf_Addr addr,
365             bool is_relative,
366             Elf_Word size,
367             uint8_t binding,
368             uint8_t type,
369             uint8_t other = 0) {
370      DCHECK(section != nullptr || !is_relative);
371      Elf_Addr abs_addr = addr + (is_relative ? section->GetAddress() : 0);
372      Elf_Word section_index =
373          (section != nullptr) ? section->GetSectionIndex() : static_cast<Elf_Word>(SHN_ABS);
374      Add(name, section_index, abs_addr, size, binding, type, other);
375    }
376
377    void Add(Elf_Word name,
378             Elf_Word section_index,
379             Elf_Addr addr,
380             Elf_Word size,
381             uint8_t binding,
382             uint8_t type,
383             uint8_t other = 0) {
384      Elf_Sym sym = Elf_Sym();
385      sym.st_name = name;
386      sym.st_value = addr;
387      sym.st_size = size;
388      sym.st_other = other;
389      sym.st_shndx = section_index;
390      sym.st_info = (binding << 4) + (type & 0xf);
391      CachedSection::Add(&sym, sizeof(sym));
392    }
393  };
394
395  ElfBuilder(InstructionSet isa, OutputStream* output)
396      : isa_(isa),
397        stream_(output),
398        rodata_(this, ".rodata", SHT_PROGBITS, SHF_ALLOC, nullptr, 0, kPageSize, 0),
399        text_(this, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR, nullptr, 0, kPageSize, 0),
400        bss_(this, ".bss", SHT_NOBITS, SHF_ALLOC, nullptr, 0, kPageSize, 0),
401        dynstr_(this, ".dynstr", SHF_ALLOC, kPageSize),
402        dynsym_(this, ".dynsym", SHT_DYNSYM, SHF_ALLOC, &dynstr_),
403        hash_(this, ".hash", SHT_HASH, SHF_ALLOC, &dynsym_, 0, sizeof(Elf_Word), sizeof(Elf_Word)),
404        dynamic_(this, ".dynamic", SHT_DYNAMIC, SHF_ALLOC, &dynstr_, 0, kPageSize, sizeof(Elf_Dyn)),
405        eh_frame_(this, ".eh_frame", SHT_PROGBITS, SHF_ALLOC, nullptr, 0, kPageSize, 0),
406        eh_frame_hdr_(this, ".eh_frame_hdr", SHT_PROGBITS, SHF_ALLOC, nullptr, 0, 4, 0),
407        strtab_(this, ".strtab", 0, 1),
408        symtab_(this, ".symtab", SHT_SYMTAB, 0, &strtab_),
409        debug_frame_(this, ".debug_frame", SHT_PROGBITS, 0, nullptr, 0, sizeof(Elf_Addr), 0),
410        debug_info_(this, ".debug_info", SHT_PROGBITS, 0, nullptr, 0, 1, 0),
411        debug_line_(this, ".debug_line", SHT_PROGBITS, 0, nullptr, 0, 1, 0),
412        shstrtab_(this, ".shstrtab", 0, 1),
413        started_(false),
414        write_program_headers_(false),
415        loaded_size_(0u),
416        virtual_address_(0) {
417    text_.phdr_flags_ = PF_R | PF_X;
418    bss_.phdr_flags_ = PF_R | PF_W;
419    dynamic_.phdr_flags_ = PF_R | PF_W;
420    dynamic_.phdr_type_ = PT_DYNAMIC;
421    eh_frame_hdr_.phdr_type_ = PT_GNU_EH_FRAME;
422  }
423  ~ElfBuilder() {}
424
425  InstructionSet GetIsa() { return isa_; }
426  Section* GetRoData() { return &rodata_; }
427  Section* GetText() { return &text_; }
428  Section* GetBss() { return &bss_; }
429  StringSection* GetStrTab() { return &strtab_; }
430  SymbolSection* GetSymTab() { return &symtab_; }
431  Section* GetEhFrame() { return &eh_frame_; }
432  Section* GetEhFrameHdr() { return &eh_frame_hdr_; }
433  Section* GetDebugFrame() { return &debug_frame_; }
434  Section* GetDebugInfo() { return &debug_info_; }
435  Section* GetDebugLine() { return &debug_line_; }
436
437  // Encode patch locations as LEB128 list of deltas between consecutive addresses.
438  // (exposed publicly for tests)
439  static void EncodeOatPatches(const ArrayRef<const uintptr_t>& locations,
440                               std::vector<uint8_t>* buffer) {
441    buffer->reserve(buffer->size() + locations.size() * 2);  // guess 2 bytes per ULEB128.
442    uintptr_t address = 0;  // relative to start of section.
443    for (uintptr_t location : locations) {
444      DCHECK_GE(location, address) << "Patch locations are not in sorted order";
445      EncodeUnsignedLeb128(buffer, dchecked_integral_cast<uint32_t>(location - address));
446      address = location;
447    }
448  }
449
450  void WritePatches(const char* name, const ArrayRef<const uintptr_t>& patch_locations) {
451    std::vector<uint8_t> buffer;
452    EncodeOatPatches(patch_locations, &buffer);
453    std::unique_ptr<Section> s(new Section(this, name, SHT_OAT_PATCH, 0, nullptr, 0, 1, 0));
454    s->Start();
455    s->WriteFully(buffer.data(), buffer.size());
456    s->End();
457    other_sections_.push_back(std::move(s));
458  }
459
460  void WriteSection(const char* name, const std::vector<uint8_t>* buffer) {
461    std::unique_ptr<Section> s(new Section(this, name, SHT_PROGBITS, 0, nullptr, 0, 1, 0));
462    s->Start();
463    s->WriteFully(buffer->data(), buffer->size());
464    s->End();
465    other_sections_.push_back(std::move(s));
466  }
467
468  // Reserve space for ELF header and program headers.
469  // We do not know the number of headers until later, so
470  // it is easiest to just reserve a fixed amount of space.
471  // Program headers are required for loading by the linker.
472  // It is possible to omit them for ELF files used for debugging.
473  void Start(bool write_program_headers = true) {
474    int size = sizeof(Elf_Ehdr);
475    if (write_program_headers) {
476      size += sizeof(Elf_Phdr) * kMaxProgramHeaders;
477    }
478    stream_.Seek(size, kSeekSet);
479    started_ = true;
480    virtual_address_ += size;
481    write_program_headers_ = write_program_headers;
482  }
483
484  void End() {
485    DCHECK(started_);
486
487    // Note: loaded_size_ == 0 for tests that don't write .rodata, .text, .bss,
488    // .dynstr, dynsym, .hash and .dynamic. These tests should not read loaded_size_.
489    // TODO: Either refactor the .eh_frame creation so that it counts towards loaded_size_,
490    // or remove all support for .eh_frame. (The currently unused .eh_frame counts towards
491    // the virtual_address_ but we don't consider it for loaded_size_.)
492    CHECK(loaded_size_ == 0 || loaded_size_ == RoundUp(virtual_address_, kPageSize))
493        << loaded_size_ << " " << virtual_address_;
494
495    // Write section names and finish the section headers.
496    shstrtab_.Start();
497    shstrtab_.Write("");
498    for (auto* section : sections_) {
499      section->header_.sh_name = shstrtab_.Write(section->name_);
500      if (section->link_ != nullptr) {
501        section->header_.sh_link = section->link_->GetSectionIndex();
502      }
503    }
504    shstrtab_.End();
505
506    // Write section headers at the end of the ELF file.
507    std::vector<Elf_Shdr> shdrs;
508    shdrs.reserve(1u + sections_.size());
509    shdrs.push_back(Elf_Shdr());  // NULL at index 0.
510    for (auto* section : sections_) {
511      shdrs.push_back(section->header_);
512    }
513    Elf_Off section_headers_offset;
514    section_headers_offset = AlignFileOffset(sizeof(Elf_Off));
515    stream_.WriteFully(shdrs.data(), shdrs.size() * sizeof(shdrs[0]));
516
517    // Flush everything else before writing the program headers. This should prevent
518    // the OS from reordering writes, so that we don't end up with valid headers
519    // and partially written data if we suddenly lose power, for example.
520    stream_.Flush();
521
522    // The main ELF header.
523    Elf_Ehdr elf_header = MakeElfHeader(isa_);
524    elf_header.e_shoff = section_headers_offset;
525    elf_header.e_shnum = shdrs.size();
526    elf_header.e_shstrndx = shstrtab_.GetSectionIndex();
527
528    // Program headers (i.e. mmap instructions).
529    std::vector<Elf_Phdr> phdrs;
530    if (write_program_headers_) {
531      phdrs = MakeProgramHeaders();
532      CHECK_LE(phdrs.size(), kMaxProgramHeaders);
533      elf_header.e_phoff = sizeof(Elf_Ehdr);
534      elf_header.e_phnum = phdrs.size();
535    }
536
537    stream_.Seek(0, kSeekSet);
538    stream_.WriteFully(&elf_header, sizeof(elf_header));
539    stream_.WriteFully(phdrs.data(), phdrs.size() * sizeof(phdrs[0]));
540    stream_.Flush();
541  }
542
543  // The running program does not have access to section headers
544  // and the loader is not supposed to use them either.
545  // The dynamic sections therefore replicates some of the layout
546  // information like the address and size of .rodata and .text.
547  // It also contains other metadata like the SONAME.
548  // The .dynamic section is found using the PT_DYNAMIC program header.
549  void PrepareDynamicSection(const std::string& elf_file_path,
550                             Elf_Word rodata_size,
551                             Elf_Word text_size,
552                             Elf_Word bss_size) {
553    std::string soname(elf_file_path);
554    size_t directory_separator_pos = soname.rfind('/');
555    if (directory_separator_pos != std::string::npos) {
556      soname = soname.substr(directory_separator_pos + 1);
557    }
558
559    // Calculate addresses of .text, .bss and .dynstr.
560    DCHECK_EQ(rodata_.header_.sh_addralign, static_cast<Elf_Word>(kPageSize));
561    DCHECK_EQ(text_.header_.sh_addralign, static_cast<Elf_Word>(kPageSize));
562    DCHECK_EQ(bss_.header_.sh_addralign, static_cast<Elf_Word>(kPageSize));
563    DCHECK_EQ(dynstr_.header_.sh_addralign, static_cast<Elf_Word>(kPageSize));
564    Elf_Word rodata_address = rodata_.GetAddress();
565    Elf_Word text_address = RoundUp(rodata_address + rodata_size, kPageSize);
566    Elf_Word bss_address = RoundUp(text_address + text_size, kPageSize);
567    Elf_Word dynstr_address = RoundUp(bss_address + bss_size, kPageSize);
568
569    // Cache .dynstr, .dynsym and .hash data.
570    dynstr_.Add("");  // dynstr should start with empty string.
571    Elf_Word rodata_index = rodata_.GetSectionIndex();
572    Elf_Word oatdata = dynstr_.Add("oatdata");
573    dynsym_.Add(oatdata, rodata_index, rodata_address, rodata_size, STB_GLOBAL, STT_OBJECT);
574    if (text_size != 0u) {
575      Elf_Word text_index = rodata_index + 1u;
576      Elf_Word oatexec = dynstr_.Add("oatexec");
577      dynsym_.Add(oatexec, text_index, text_address, text_size, STB_GLOBAL, STT_OBJECT);
578      Elf_Word oatlastword = dynstr_.Add("oatlastword");
579      Elf_Word oatlastword_address = text_address + text_size - 4;
580      dynsym_.Add(oatlastword, text_index, oatlastword_address, 4, STB_GLOBAL, STT_OBJECT);
581    } else if (rodata_size != 0) {
582      // rodata_ can be size 0 for dwarf_test.
583      Elf_Word oatlastword = dynstr_.Add("oatlastword");
584      Elf_Word oatlastword_address = rodata_address + rodata_size - 4;
585      dynsym_.Add(oatlastword, rodata_index, oatlastword_address, 4, STB_GLOBAL, STT_OBJECT);
586    }
587    if (bss_size != 0u) {
588      Elf_Word bss_index = rodata_index + 1u + (text_size != 0 ? 1u : 0u);
589      Elf_Word oatbss = dynstr_.Add("oatbss");
590      dynsym_.Add(oatbss, bss_index, bss_address, bss_size, STB_GLOBAL, STT_OBJECT);
591      Elf_Word oatbsslastword = dynstr_.Add("oatbsslastword");
592      Elf_Word bsslastword_address = bss_address + bss_size - 4;
593      dynsym_.Add(oatbsslastword, bss_index, bsslastword_address, 4, STB_GLOBAL, STT_OBJECT);
594    }
595    Elf_Word soname_offset = dynstr_.Add(soname);
596
597    // We do not really need a hash-table since there is so few entries.
598    // However, the hash-table is the only way the linker can actually
599    // determine the number of symbols in .dynsym so it is required.
600    int count = dynsym_.GetCacheSize() / sizeof(Elf_Sym);  // Includes NULL.
601    std::vector<Elf_Word> hash;
602    hash.push_back(1);  // Number of buckets.
603    hash.push_back(count);  // Number of chains.
604    // Buckets.  Having just one makes it linear search.
605    hash.push_back(1);  // Point to first non-NULL symbol.
606    // Chains.  This creates linked list of symbols.
607    hash.push_back(0);  // Dummy entry for the NULL symbol.
608    for (int i = 1; i < count - 1; i++) {
609      hash.push_back(i + 1);  // Each symbol points to the next one.
610    }
611    hash.push_back(0);  // Last symbol terminates the chain.
612    hash_.Add(hash.data(), hash.size() * sizeof(hash[0]));
613
614    // Calculate addresses of .dynsym, .hash and .dynamic.
615    DCHECK_EQ(dynstr_.header_.sh_flags, dynsym_.header_.sh_flags);
616    DCHECK_EQ(dynsym_.header_.sh_flags, hash_.header_.sh_flags);
617    Elf_Word dynsym_address =
618        RoundUp(dynstr_address + dynstr_.GetCacheSize(), dynsym_.header_.sh_addralign);
619    Elf_Word hash_address =
620        RoundUp(dynsym_address + dynsym_.GetCacheSize(), hash_.header_.sh_addralign);
621    DCHECK_EQ(dynamic_.header_.sh_addralign, static_cast<Elf_Word>(kPageSize));
622    Elf_Word dynamic_address = RoundUp(hash_address + dynsym_.GetCacheSize(), kPageSize);
623
624    Elf_Dyn dyns[] = {
625      { DT_HASH, { hash_address } },
626      { DT_STRTAB, { dynstr_address } },
627      { DT_SYMTAB, { dynsym_address } },
628      { DT_SYMENT, { sizeof(Elf_Sym) } },
629      { DT_STRSZ, { dynstr_.GetCacheSize() } },
630      { DT_SONAME, { soname_offset } },
631      { DT_NULL, { 0 } },
632    };
633    dynamic_.Add(&dyns, sizeof(dyns));
634
635    loaded_size_ = RoundUp(dynamic_address + dynamic_.GetCacheSize(), kPageSize);
636  }
637
638  void WriteDynamicSection() {
639    dynstr_.WriteCachedSection();
640    dynsym_.WriteCachedSection();
641    hash_.WriteCachedSection();
642    dynamic_.WriteCachedSection();
643
644    CHECK_EQ(loaded_size_, RoundUp(dynamic_.GetAddress() + dynamic_.GetSize(), kPageSize));
645  }
646
647  Elf_Word GetLoadedSize() {
648    CHECK_NE(loaded_size_, 0u);
649    return loaded_size_;
650  }
651
652  // Returns true if all writes and seeks on the output stream succeeded.
653  bool Good() {
654    return stream_.Good();
655  }
656
657  // Returns the builder's internal stream.
658  OutputStream* GetStream() {
659    return &stream_;
660  }
661
662  off_t AlignFileOffset(size_t alignment) {
663     return stream_.Seek(RoundUp(stream_.Seek(0, kSeekCurrent), alignment), kSeekSet);
664  }
665
666  Elf_Addr AlignVirtualAddress(size_t alignment) {
667     return virtual_address_ = RoundUp(virtual_address_, alignment);
668  }
669
670 private:
671  static Elf_Ehdr MakeElfHeader(InstructionSet isa) {
672    Elf_Ehdr elf_header = Elf_Ehdr();
673    switch (isa) {
674      case kArm:
675        // Fall through.
676      case kThumb2: {
677        elf_header.e_machine = EM_ARM;
678        elf_header.e_flags = EF_ARM_EABI_VER5;
679        break;
680      }
681      case kArm64: {
682        elf_header.e_machine = EM_AARCH64;
683        elf_header.e_flags = 0;
684        break;
685      }
686      case kX86: {
687        elf_header.e_machine = EM_386;
688        elf_header.e_flags = 0;
689        break;
690      }
691      case kX86_64: {
692        elf_header.e_machine = EM_X86_64;
693        elf_header.e_flags = 0;
694        break;
695      }
696      case kMips: {
697        elf_header.e_machine = EM_MIPS;
698        elf_header.e_flags = (EF_MIPS_NOREORDER |
699                               EF_MIPS_PIC       |
700                               EF_MIPS_CPIC      |
701                               EF_MIPS_ABI_O32   |
702                               EF_MIPS_ARCH_32R2);
703        break;
704      }
705      case kMips64: {
706        elf_header.e_machine = EM_MIPS;
707        elf_header.e_flags = (EF_MIPS_NOREORDER |
708                               EF_MIPS_PIC       |
709                               EF_MIPS_CPIC      |
710                               EF_MIPS_ARCH_64R6);
711        break;
712      }
713      case kNone: {
714        LOG(FATAL) << "No instruction set";
715        break;
716      }
717      default: {
718        LOG(FATAL) << "Unknown instruction set " << isa;
719      }
720    }
721
722    elf_header.e_ident[EI_MAG0]       = ELFMAG0;
723    elf_header.e_ident[EI_MAG1]       = ELFMAG1;
724    elf_header.e_ident[EI_MAG2]       = ELFMAG2;
725    elf_header.e_ident[EI_MAG3]       = ELFMAG3;
726    elf_header.e_ident[EI_CLASS]      = (sizeof(Elf_Addr) == sizeof(Elf32_Addr))
727                                         ? ELFCLASS32 : ELFCLASS64;;
728    elf_header.e_ident[EI_DATA]       = ELFDATA2LSB;
729    elf_header.e_ident[EI_VERSION]    = EV_CURRENT;
730    elf_header.e_ident[EI_OSABI]      = ELFOSABI_LINUX;
731    elf_header.e_ident[EI_ABIVERSION] = 0;
732    elf_header.e_type = ET_DYN;
733    elf_header.e_version = 1;
734    elf_header.e_entry = 0;
735    elf_header.e_ehsize = sizeof(Elf_Ehdr);
736    elf_header.e_phentsize = sizeof(Elf_Phdr);
737    elf_header.e_shentsize = sizeof(Elf_Shdr);
738    elf_header.e_phoff = sizeof(Elf_Ehdr);
739    return elf_header;
740  }
741
742  // Create program headers based on written sections.
743  std::vector<Elf_Phdr> MakeProgramHeaders() {
744    CHECK(!sections_.empty());
745    std::vector<Elf_Phdr> phdrs;
746    {
747      // The program headers must start with PT_PHDR which is used in
748      // loaded process to determine the number of program headers.
749      Elf_Phdr phdr = Elf_Phdr();
750      phdr.p_type    = PT_PHDR;
751      phdr.p_flags   = PF_R;
752      phdr.p_offset  = phdr.p_vaddr = phdr.p_paddr = sizeof(Elf_Ehdr);
753      phdr.p_filesz  = phdr.p_memsz = 0;  // We need to fill this later.
754      phdr.p_align   = sizeof(Elf_Off);
755      phdrs.push_back(phdr);
756      // Tell the linker to mmap the start of file to memory.
757      Elf_Phdr load = Elf_Phdr();
758      load.p_type    = PT_LOAD;
759      load.p_flags   = PF_R;
760      load.p_offset  = load.p_vaddr = load.p_paddr = 0;
761      load.p_filesz  = load.p_memsz = sections_[0]->header_.sh_offset;
762      load.p_align   = kPageSize;
763      phdrs.push_back(load);
764    }
765    // Create program headers for sections.
766    for (auto* section : sections_) {
767      const Elf_Shdr& shdr = section->header_;
768      if ((shdr.sh_flags & SHF_ALLOC) != 0 && shdr.sh_size != 0) {
769        // PT_LOAD tells the linker to mmap part of the file.
770        // The linker can only mmap page-aligned sections.
771        // Single PT_LOAD may contain several ELF sections.
772        Elf_Phdr& prev = phdrs.back();
773        Elf_Phdr load = Elf_Phdr();
774        load.p_type   = PT_LOAD;
775        load.p_flags  = section->phdr_flags_;
776        load.p_offset = shdr.sh_offset;
777        load.p_vaddr  = load.p_paddr = shdr.sh_addr;
778        load.p_filesz = (shdr.sh_type != SHT_NOBITS ? shdr.sh_size : 0u);
779        load.p_memsz  = shdr.sh_size;
780        load.p_align  = shdr.sh_addralign;
781        if (prev.p_type == load.p_type &&
782            prev.p_flags == load.p_flags &&
783            prev.p_filesz == prev.p_memsz &&  // Do not merge .bss
784            load.p_filesz == load.p_memsz) {  // Do not merge .bss
785          // Merge this PT_LOAD with the previous one.
786          Elf_Word size = shdr.sh_offset + shdr.sh_size - prev.p_offset;
787          prev.p_filesz = size;
788          prev.p_memsz  = size;
789        } else {
790          // If we are adding new load, it must be aligned.
791          CHECK_EQ(shdr.sh_addralign, (Elf_Word)kPageSize);
792          phdrs.push_back(load);
793        }
794      }
795    }
796    for (auto* section : sections_) {
797      const Elf_Shdr& shdr = section->header_;
798      if ((shdr.sh_flags & SHF_ALLOC) != 0 && shdr.sh_size != 0) {
799        // Other PT_* types allow the program to locate interesting
800        // parts of memory at runtime. They must overlap with PT_LOAD.
801        if (section->phdr_type_ != 0) {
802          Elf_Phdr phdr = Elf_Phdr();
803          phdr.p_type   = section->phdr_type_;
804          phdr.p_flags  = section->phdr_flags_;
805          phdr.p_offset = shdr.sh_offset;
806          phdr.p_vaddr  = phdr.p_paddr = shdr.sh_addr;
807          phdr.p_filesz = phdr.p_memsz = shdr.sh_size;
808          phdr.p_align  = shdr.sh_addralign;
809          phdrs.push_back(phdr);
810        }
811      }
812    }
813    // Set the size of the initial PT_PHDR.
814    CHECK_EQ(phdrs[0].p_type, (Elf_Word)PT_PHDR);
815    phdrs[0].p_filesz = phdrs[0].p_memsz = phdrs.size() * sizeof(Elf_Phdr);
816
817    return phdrs;
818  }
819
820  InstructionSet isa_;
821
822  ErrorDelayingOutputStream stream_;
823
824  Section rodata_;
825  Section text_;
826  Section bss_;
827  CachedStringSection dynstr_;
828  SymbolSection dynsym_;
829  CachedSection hash_;
830  CachedSection dynamic_;
831  Section eh_frame_;
832  Section eh_frame_hdr_;
833  StringSection strtab_;
834  SymbolSection symtab_;
835  Section debug_frame_;
836  Section debug_info_;
837  Section debug_line_;
838  StringSection shstrtab_;
839  std::vector<std::unique_ptr<Section>> other_sections_;
840
841  // List of used section in the order in which they were written.
842  std::vector<Section*> sections_;
843
844  bool started_;
845  bool write_program_headers_;
846
847  // The size of the memory taken by the ELF file when loaded.
848  size_t loaded_size_;
849
850  // Used for allocation of virtual address space.
851  Elf_Addr virtual_address_;
852
853  DISALLOW_COPY_AND_ASSIGN(ElfBuilder);
854};
855
856}  // namespace art
857
858#endif  // ART_COMPILER_ELF_BUILDER_H_
859