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