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