1 // Copyright 2014 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // Implementation notes: 6 // 7 // We need to remove a piece from the ELF shared library. However, we also 8 // want to avoid fixing DWARF cfi data and relative relocation addresses. 9 // So after packing we shift offets and starting address of the RX segment 10 // while preserving code/data vaddrs location. 11 // This requires some fixups for symtab/hash/gnu_hash dynamic section addresses. 12 13 #include "elf_file.h" 14 15 #include <stdlib.h> 16 #include <sys/types.h> 17 #include <unistd.h> 18 #include <algorithm> 19 #include <string> 20 #include <vector> 21 22 #include "debug.h" 23 #include "elf_traits.h" 24 #include "libelf.h" 25 #include "packer.h" 26 27 namespace relocation_packer { 28 29 // Out-of-band dynamic tags used to indicate the offset and size of the 30 // android packed relocations section. 31 static constexpr int32_t DT_ANDROID_REL = DT_LOOS + 2; 32 static constexpr int32_t DT_ANDROID_RELSZ = DT_LOOS + 3; 33 34 static constexpr int32_t DT_ANDROID_RELA = DT_LOOS + 4; 35 static constexpr int32_t DT_ANDROID_RELASZ = DT_LOOS + 5; 36 37 static constexpr uint32_t SHT_ANDROID_REL = SHT_LOOS + 1; 38 static constexpr uint32_t SHT_ANDROID_RELA = SHT_LOOS + 2; 39 40 static const size_t kPageSize = 4096; 41 42 // Alignment to preserve, in bytes. This must be at least as large as the 43 // largest d_align and sh_addralign values found in the loaded file. 44 // Out of caution for RELRO page alignment, we preserve to a complete target 45 // page. See http://www.airs.com/blog/archives/189. 46 static const size_t kPreserveAlignment = kPageSize; 47 48 // Get section data. Checks that the section has exactly one data entry, 49 // so that the section size and the data size are the same. True in 50 // practice for all sections we resize when packing or unpacking. Done 51 // by ensuring that a call to elf_getdata(section, data) returns NULL as 52 // the next data entry. 53 static Elf_Data* GetSectionData(Elf_Scn* section) { 54 Elf_Data* data = elf_getdata(section, NULL); 55 CHECK(data && elf_getdata(section, data) == NULL); 56 return data; 57 } 58 59 // Rewrite section data. Allocates new data and makes it the data element's 60 // buffer. Relies on program exit to free allocated data. 61 static void RewriteSectionData(Elf_Scn* section, 62 const void* section_data, 63 size_t size) { 64 Elf_Data* data = GetSectionData(section); 65 CHECK(size == data->d_size); 66 uint8_t* area = new uint8_t[size]; 67 memcpy(area, section_data, size); 68 data->d_buf = area; 69 } 70 71 // Verbose ELF header logging. 72 template <typename Ehdr> 73 static void VerboseLogElfHeader(const Ehdr* elf_header) { 74 VLOG(1) << "e_phoff = " << elf_header->e_phoff; 75 VLOG(1) << "e_shoff = " << elf_header->e_shoff; 76 VLOG(1) << "e_ehsize = " << elf_header->e_ehsize; 77 VLOG(1) << "e_phentsize = " << elf_header->e_phentsize; 78 VLOG(1) << "e_phnum = " << elf_header->e_phnum; 79 VLOG(1) << "e_shnum = " << elf_header->e_shnum; 80 VLOG(1) << "e_shstrndx = " << elf_header->e_shstrndx; 81 } 82 83 // Verbose ELF program header logging. 84 template <typename Phdr> 85 static void VerboseLogProgramHeader(size_t program_header_index, 86 const Phdr* program_header) { 87 std::string type; 88 switch (program_header->p_type) { 89 case PT_NULL: type = "NULL"; break; 90 case PT_LOAD: type = "LOAD"; break; 91 case PT_DYNAMIC: type = "DYNAMIC"; break; 92 case PT_INTERP: type = "INTERP"; break; 93 case PT_PHDR: type = "PHDR"; break; 94 case PT_GNU_RELRO: type = "GNU_RELRO"; break; 95 case PT_GNU_STACK: type = "GNU_STACK"; break; 96 case PT_ARM_EXIDX: type = "EXIDX"; break; 97 default: type = "(OTHER)"; break; 98 } 99 VLOG(1) << "phdr[" << program_header_index << "] : " << type; 100 VLOG(1) << " p_offset = " << program_header->p_offset; 101 VLOG(1) << " p_vaddr = " << program_header->p_vaddr; 102 VLOG(1) << " p_paddr = " << program_header->p_paddr; 103 VLOG(1) << " p_filesz = " << program_header->p_filesz; 104 VLOG(1) << " p_memsz = " << program_header->p_memsz; 105 VLOG(1) << " p_flags = " << program_header->p_flags; 106 VLOG(1) << " p_align = " << program_header->p_align; 107 } 108 109 // Verbose ELF section header logging. 110 template <typename Shdr> 111 static void VerboseLogSectionHeader(const std::string& section_name, 112 const Shdr* section_header) { 113 VLOG(1) << "section " << section_name; 114 VLOG(1) << " sh_addr = " << section_header->sh_addr; 115 VLOG(1) << " sh_offset = " << section_header->sh_offset; 116 VLOG(1) << " sh_size = " << section_header->sh_size; 117 VLOG(1) << " sh_entsize = " << section_header->sh_entsize; 118 VLOG(1) << " sh_addralign = " << section_header->sh_addralign; 119 } 120 121 // Verbose ELF section data logging. 122 static void VerboseLogSectionData(const Elf_Data* data) { 123 VLOG(1) << " data"; 124 VLOG(1) << " d_buf = " << data->d_buf; 125 VLOG(1) << " d_off = " << data->d_off; 126 VLOG(1) << " d_size = " << data->d_size; 127 VLOG(1) << " d_align = " << data->d_align; 128 } 129 130 // Load the complete ELF file into a memory image in libelf, and identify 131 // the .rel.dyn or .rela.dyn, .dynamic, and .android.rel.dyn or 132 // .android.rela.dyn sections. No-op if the ELF file has already been loaded. 133 template <typename ELF> 134 bool ElfFile<ELF>::Load() { 135 if (elf_) 136 return true; 137 138 Elf* elf = elf_begin(fd_, ELF_C_RDWR, NULL); 139 CHECK(elf); 140 141 if (elf_kind(elf) != ELF_K_ELF) { 142 LOG(ERROR) << "File not in ELF format"; 143 return false; 144 } 145 146 auto elf_header = ELF::getehdr(elf); 147 if (!elf_header) { 148 LOG(ERROR) << "Failed to load ELF header: " << elf_errmsg(elf_errno()); 149 return false; 150 } 151 152 if (elf_header->e_type != ET_DYN) { 153 LOG(ERROR) << "ELF file is not a shared object"; 154 return false; 155 } 156 157 // Require that our endianness matches that of the target, and that both 158 // are little-endian. Safe for all current build/target combinations. 159 const int endian = elf_header->e_ident[EI_DATA]; 160 CHECK(endian == ELFDATA2LSB); 161 CHECK(__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__); 162 163 const int file_class = elf_header->e_ident[EI_CLASS]; 164 VLOG(1) << "endian = " << endian << ", file class = " << file_class; 165 VerboseLogElfHeader(elf_header); 166 167 auto elf_program_header = ELF::getphdr(elf); 168 CHECK(elf_program_header != nullptr); 169 170 const typename ELF::Phdr* dynamic_program_header = NULL; 171 for (size_t i = 0; i < elf_header->e_phnum; ++i) { 172 auto program_header = &elf_program_header[i]; 173 VerboseLogProgramHeader(i, program_header); 174 175 if (program_header->p_type == PT_DYNAMIC) { 176 CHECK(dynamic_program_header == NULL); 177 dynamic_program_header = program_header; 178 } 179 } 180 CHECK(dynamic_program_header != nullptr); 181 182 size_t string_index; 183 elf_getshdrstrndx(elf, &string_index); 184 185 // Notes of the dynamic relocations, packed relocations, and .dynamic 186 // sections. Found while iterating sections, and later stored in class 187 // attributes. 188 Elf_Scn* found_relocations_section = nullptr; 189 Elf_Scn* found_dynamic_section = nullptr; 190 191 // Notes of relocation section types seen. We require one or the other of 192 // these; both is unsupported. 193 bool has_rel_relocations = false; 194 bool has_rela_relocations = false; 195 bool has_android_relocations = false; 196 197 Elf_Scn* section = NULL; 198 while ((section = elf_nextscn(elf, section)) != nullptr) { 199 auto section_header = ELF::getshdr(section); 200 std::string name = elf_strptr(elf, string_index, section_header->sh_name); 201 VerboseLogSectionHeader(name, section_header); 202 203 // Note relocation section types. 204 if (section_header->sh_type == SHT_REL || section_header->sh_type == SHT_ANDROID_REL) { 205 has_rel_relocations = true; 206 } 207 if (section_header->sh_type == SHT_RELA || section_header->sh_type == SHT_ANDROID_RELA) { 208 has_rela_relocations = true; 209 } 210 211 // Note special sections as we encounter them. 212 if ((name == ".rel.dyn" || name == ".rela.dyn") && 213 section_header->sh_size > 0) { 214 found_relocations_section = section; 215 216 // Note if relocation section is already packed 217 has_android_relocations = 218 section_header->sh_type == SHT_ANDROID_REL || 219 section_header->sh_type == SHT_ANDROID_RELA; 220 } 221 222 if (section_header->sh_offset == dynamic_program_header->p_offset) { 223 found_dynamic_section = section; 224 } 225 226 // Ensure we preserve alignment, repeated later for the data block(s). 227 CHECK(section_header->sh_addralign <= kPreserveAlignment); 228 229 Elf_Data* data = NULL; 230 while ((data = elf_getdata(section, data)) != NULL) { 231 CHECK(data->d_align <= kPreserveAlignment); 232 VerboseLogSectionData(data); 233 } 234 } 235 236 // Loading failed if we did not find the required special sections. 237 if (!found_relocations_section) { 238 LOG(ERROR) << "Missing or empty .rel.dyn or .rela.dyn section"; 239 return false; 240 } 241 if (!found_dynamic_section) { 242 LOG(ERROR) << "Missing .dynamic section"; 243 return false; 244 } 245 246 // Loading failed if we could not identify the relocations type. 247 if (!has_rel_relocations && !has_rela_relocations) { 248 LOG(ERROR) << "No relocations sections found"; 249 return false; 250 } 251 if (has_rel_relocations && has_rela_relocations) { 252 LOG(ERROR) << "Multiple relocations sections with different types found, " 253 << "not currently supported"; 254 return false; 255 } 256 257 elf_ = elf; 258 relocations_section_ = found_relocations_section; 259 dynamic_section_ = found_dynamic_section; 260 relocations_type_ = has_rel_relocations ? REL : RELA; 261 has_android_relocations_ = has_android_relocations; 262 return true; 263 } 264 265 // Helper for ResizeSection(). Adjust the main ELF header for the hole. 266 template <typename ELF> 267 static void AdjustElfHeaderForHole(typename ELF::Ehdr* elf_header, 268 typename ELF::Off hole_start, 269 ssize_t hole_size) { 270 if (elf_header->e_phoff > hole_start) { 271 elf_header->e_phoff += hole_size; 272 VLOG(1) << "e_phoff adjusted to " << elf_header->e_phoff; 273 } 274 if (elf_header->e_shoff > hole_start) { 275 elf_header->e_shoff += hole_size; 276 VLOG(1) << "e_shoff adjusted to " << elf_header->e_shoff; 277 } 278 } 279 280 // Helper for ResizeSection(). Adjust all section headers for the hole. 281 template <typename ELF> 282 static void AdjustSectionHeadersForHole(Elf* elf, 283 typename ELF::Off hole_start, 284 ssize_t hole_size) { 285 size_t string_index; 286 elf_getshdrstrndx(elf, &string_index); 287 288 Elf_Scn* section = NULL; 289 while ((section = elf_nextscn(elf, section)) != NULL) { 290 auto section_header = ELF::getshdr(section); 291 std::string name = elf_strptr(elf, string_index, section_header->sh_name); 292 293 if (section_header->sh_offset > hole_start) { 294 section_header->sh_offset += hole_size; 295 VLOG(1) << "section " << name 296 << " sh_offset adjusted to " << section_header->sh_offset; 297 } else { 298 section_header->sh_addr -= hole_size; 299 VLOG(1) << "section " << name 300 << " sh_addr adjusted to " << section_header->sh_addr; 301 } 302 } 303 } 304 305 // Helpers for ResizeSection(). On packing, reduce p_align for LOAD segments 306 // to 4kb if larger. On unpacking, restore p_align for LOAD segments if 307 // packing reduced it to 4kb. Return true if p_align was changed. 308 template <typename ELF> 309 static bool ClampLoadSegmentAlignment(typename ELF::Phdr* program_header) { 310 CHECK(program_header->p_type == PT_LOAD); 311 312 // If large, reduce p_align for a LOAD segment to page size on packing. 313 if (program_header->p_align > kPageSize) { 314 program_header->p_align = kPageSize; 315 return true; 316 } 317 return false; 318 } 319 320 template <typename ELF> 321 static bool RestoreLoadSegmentAlignment(typename ELF::Phdr* program_headers, 322 size_t count, 323 typename ELF::Phdr* program_header) { 324 CHECK(program_header->p_type == PT_LOAD); 325 326 // If p_align was reduced on packing, restore it to its previous value 327 // on unpacking. We do this by searching for a different LOAD segment 328 // and setting p_align to that of the other LOAD segment found. 329 // 330 // Relies on the following observations: 331 // - a packable ELF executable has more than one LOAD segment; 332 // - before packing all LOAD segments have the same p_align; 333 // - on packing we reduce only one LOAD segment's p_align. 334 if (program_header->p_align == kPageSize) { 335 for (size_t i = 0; i < count; ++i) { 336 typename ELF::Phdr* other_header = &program_headers[i]; 337 if (other_header->p_type == PT_LOAD && other_header != program_header) { 338 program_header->p_align = other_header->p_align; 339 return true; 340 } 341 } 342 LOG(WARNING) << "Cannot find a LOAD segment from which to restore p_align"; 343 } 344 return false; 345 } 346 347 template <typename ELF> 348 static bool AdjustLoadSegmentAlignment(typename ELF::Phdr* program_headers, 349 size_t count, 350 typename ELF::Phdr* program_header, 351 ssize_t hole_size) { 352 CHECK(program_header->p_type == PT_LOAD); 353 354 bool status = false; 355 if (hole_size < 0) { 356 status = ClampLoadSegmentAlignment<ELF>(program_header); 357 } else if (hole_size > 0) { 358 status = RestoreLoadSegmentAlignment<ELF>(program_headers, 359 count, 360 program_header); 361 } 362 return status; 363 } 364 365 // Helper for ResizeSection(). Adjust the offsets of any program headers 366 // that have offsets currently beyond the hole start, and adjust the 367 // virtual and physical addrs (and perhaps alignment) of the others. 368 template <typename ELF> 369 static void AdjustProgramHeaderFields(typename ELF::Phdr* program_headers, 370 size_t count, 371 typename ELF::Off hole_start, 372 ssize_t hole_size) { 373 int alignment_changes = 0; 374 for (size_t i = 0; i < count; ++i) { 375 typename ELF::Phdr* program_header = &program_headers[i]; 376 377 // Do not adjust PT_GNU_STACK - it confuses gdb and results 378 // in incorrect unwinding if the executable is stripped after 379 // packing. 380 if (program_header->p_type == PT_GNU_STACK) { 381 continue; 382 } 383 384 if (program_header->p_offset > hole_start) { 385 // The hole start is past this segment, so adjust offset. 386 program_header->p_offset += hole_size; 387 VLOG(1) << "phdr[" << i 388 << "] p_offset adjusted to "<< program_header->p_offset; 389 } else { 390 program_header->p_vaddr -= hole_size; 391 program_header->p_paddr -= hole_size; 392 393 // If packing, clamp LOAD segment alignment to 4kb to prevent strip 394 // from adjusting it unnecessarily if run on a packed file. If 395 // unpacking, attempt to restore a reduced alignment to its previous 396 // value. Ensure that we do this on at most one LOAD segment. 397 if (program_header->p_type == PT_LOAD) { 398 alignment_changes += AdjustLoadSegmentAlignment<ELF>(program_headers, 399 count, 400 program_header, 401 hole_size); 402 LOG_IF(FATAL, alignment_changes > 1) 403 << "Changed p_align on more than one LOAD segment"; 404 } 405 406 VLOG(1) << "phdr[" << i 407 << "] p_vaddr adjusted to "<< program_header->p_vaddr 408 << "; p_paddr adjusted to "<< program_header->p_paddr 409 << "; p_align adjusted to "<< program_header->p_align; 410 } 411 } 412 } 413 414 // Helper for ResizeSection(). Find the first loadable segment in the 415 // file. We expect it to map from file offset zero. 416 template <typename ELF> 417 static typename ELF::Phdr* FindLoadSegmentForHole(typename ELF::Phdr* program_headers, 418 size_t count, 419 typename ELF::Off hole_start) { 420 for (size_t i = 0; i < count; ++i) { 421 typename ELF::Phdr* program_header = &program_headers[i]; 422 423 if (program_header->p_type == PT_LOAD && 424 program_header->p_offset <= hole_start && 425 (program_header->p_offset + program_header->p_filesz) >= hole_start ) { 426 return program_header; 427 } 428 } 429 LOG(FATAL) << "Cannot locate a LOAD segment with hole_start=0x" << std::hex << hole_start; 430 NOTREACHED(); 431 432 return nullptr; 433 } 434 435 // Helper for ResizeSection(). Rewrite program headers. 436 template <typename ELF> 437 static void RewriteProgramHeadersForHole(Elf* elf, 438 typename ELF::Off hole_start, 439 ssize_t hole_size) { 440 const typename ELF::Ehdr* elf_header = ELF::getehdr(elf); 441 CHECK(elf_header); 442 443 typename ELF::Phdr* elf_program_header = ELF::getphdr(elf); 444 CHECK(elf_program_header); 445 446 const size_t program_header_count = elf_header->e_phnum; 447 448 // Locate the segment that we can overwrite to form the new LOAD entry, 449 // and the segment that we are going to split into two parts. 450 typename ELF::Phdr* target_load_header = 451 FindLoadSegmentForHole<ELF>(elf_program_header, program_header_count, hole_start); 452 453 VLOG(1) << "phdr[" << target_load_header - elf_program_header << "] adjust"; 454 // Adjust PT_LOAD program header memsz and filesz 455 target_load_header->p_filesz += hole_size; 456 target_load_header->p_memsz += hole_size; 457 458 // Adjust the offsets and p_vaddrs 459 AdjustProgramHeaderFields<ELF>(elf_program_header, 460 program_header_count, 461 hole_start, 462 hole_size); 463 } 464 465 // Helper for ResizeSection(). Locate and return the dynamic section. 466 template <typename ELF> 467 static Elf_Scn* GetDynamicSection(Elf* elf) { 468 const typename ELF::Ehdr* elf_header = ELF::getehdr(elf); 469 CHECK(elf_header); 470 471 const typename ELF::Phdr* elf_program_header = ELF::getphdr(elf); 472 CHECK(elf_program_header); 473 474 // Find the program header that describes the dynamic section. 475 const typename ELF::Phdr* dynamic_program_header = NULL; 476 for (size_t i = 0; i < elf_header->e_phnum; ++i) { 477 const typename ELF::Phdr* program_header = &elf_program_header[i]; 478 479 if (program_header->p_type == PT_DYNAMIC) { 480 dynamic_program_header = program_header; 481 } 482 } 483 CHECK(dynamic_program_header); 484 485 // Now find the section with the same offset as this program header. 486 Elf_Scn* dynamic_section = NULL; 487 Elf_Scn* section = NULL; 488 while ((section = elf_nextscn(elf, section)) != NULL) { 489 typename ELF::Shdr* section_header = ELF::getshdr(section); 490 491 if (section_header->sh_offset == dynamic_program_header->p_offset) { 492 dynamic_section = section; 493 } 494 } 495 CHECK(dynamic_section != NULL); 496 497 return dynamic_section; 498 } 499 500 // Helper for ResizeSection(). Adjust the .dynamic section for the hole. 501 template <typename ELF> 502 void ElfFile<ELF>::AdjustDynamicSectionForHole(Elf_Scn* dynamic_section, 503 typename ELF::Off hole_start, 504 ssize_t hole_size, 505 relocations_type_t relocations_type) { 506 CHECK(relocations_type != NONE); 507 Elf_Data* data = GetSectionData(dynamic_section); 508 509 auto dynamic_base = reinterpret_cast<typename ELF::Dyn*>(data->d_buf); 510 std::vector<typename ELF::Dyn> dynamics( 511 dynamic_base, 512 dynamic_base + data->d_size / sizeof(dynamics[0])); 513 514 if (hole_size > 0) { // expanding 515 hole_start += hole_size; 516 } 517 518 for (size_t i = 0; i < dynamics.size(); ++i) { 519 typename ELF::Dyn* dynamic = &dynamics[i]; 520 const typename ELF::Sword tag = dynamic->d_tag; 521 522 // Any tags that hold offsets are adjustment candidates. 523 const bool is_adjustable = (tag == DT_PLTGOT || 524 tag == DT_HASH || 525 tag == DT_GNU_HASH || 526 tag == DT_STRTAB || 527 tag == DT_SYMTAB || 528 tag == DT_RELA || 529 tag == DT_INIT || 530 tag == DT_FINI || 531 tag == DT_REL || 532 tag == DT_JMPREL || 533 tag == DT_INIT_ARRAY || 534 tag == DT_FINI_ARRAY || 535 tag == DT_VERSYM || 536 tag == DT_VERNEED || 537 tag == DT_VERDEF || 538 tag == DT_ANDROID_REL|| 539 tag == DT_ANDROID_RELA); 540 541 if (is_adjustable && dynamic->d_un.d_ptr <= hole_start) { 542 dynamic->d_un.d_ptr -= hole_size; 543 VLOG(1) << "dynamic[" << i << "] " << dynamic->d_tag 544 << " d_ptr adjusted to " << dynamic->d_un.d_ptr; 545 } 546 547 // DT_RELSZ or DT_RELASZ indicate the overall size of relocations. 548 // Only one will be present. Adjust by hole size. 549 if (tag == DT_RELSZ || tag == DT_RELASZ || tag == DT_ANDROID_RELSZ || tag == DT_ANDROID_RELASZ) { 550 dynamic->d_un.d_val += hole_size; 551 VLOG(1) << "dynamic[" << i << "] " << dynamic->d_tag 552 << " d_val adjusted to " << dynamic->d_un.d_val; 553 } 554 555 // Special case: DT_MIPS_RLD_MAP2 stores the difference between dynamic 556 // entry address and the address of the _r_debug (used by GDB) 557 // since the dynamic section and target address are on the 558 // different sides of the hole it needs to be adjusted accordingly 559 if (tag == DT_MIPS_RLD_MAP2) { 560 dynamic->d_un.d_val += hole_size; 561 VLOG(1) << "dynamic[" << i << "] " << dynamic->d_tag 562 << " d_val adjusted to " << dynamic->d_un.d_val; 563 } 564 565 // Ignore DT_RELCOUNT and DT_RELACOUNT: (1) nobody uses them and 566 // technically (2) the relative relocation count is not changed. 567 568 // DT_RELENT and DT_RELAENT don't change, ignore them as well. 569 } 570 571 void* section_data = &dynamics[0]; 572 size_t bytes = dynamics.size() * sizeof(dynamics[0]); 573 RewriteSectionData(dynamic_section, section_data, bytes); 574 } 575 576 // Resize a section. If the new size is larger than the current size, open 577 // up a hole by increasing file offsets that come after the hole. If smaller 578 // than the current size, remove the hole by decreasing those offsets. 579 template <typename ELF> 580 void ElfFile<ELF>::ResizeSection(Elf* elf, Elf_Scn* section, size_t new_size, 581 typename ELF::Word new_sh_type, 582 relocations_type_t relocations_type) { 583 584 size_t string_index; 585 elf_getshdrstrndx(elf, &string_index); 586 auto section_header = ELF::getshdr(section); 587 std::string name = elf_strptr(elf, string_index, section_header->sh_name); 588 589 if (section_header->sh_size == new_size) { 590 return; 591 } 592 593 // Require that the section size and the data size are the same. True 594 // in practice for all sections we resize when packing or unpacking. 595 Elf_Data* data = GetSectionData(section); 596 CHECK(data->d_off == 0 && data->d_size == section_header->sh_size); 597 598 // Require that the section is not zero-length (that is, has allocated 599 // data that we can validly expand). 600 CHECK(data->d_size && data->d_buf); 601 602 const auto hole_start = section_header->sh_offset; 603 const ssize_t hole_size = new_size - data->d_size; 604 605 VLOG_IF(1, (hole_size > 0)) << "expand section (" << name << ") size: " << 606 data->d_size << " -> " << (data->d_size + hole_size); 607 VLOG_IF(1, (hole_size < 0)) << "shrink section (" << name << ") size: " << 608 data->d_size << " -> " << (data->d_size + hole_size); 609 610 // libelf overrides sh_entsize for known sh_types, so it does not matter what we set 611 // for SHT_REL/SHT_RELA. 612 typename ELF::Xword new_entsize = 613 (new_sh_type == SHT_ANDROID_REL || new_sh_type == SHT_ANDROID_RELA) ? 1 : 0; 614 615 VLOG(1) << "Update section (" << name << ") entry size: " << 616 section_header->sh_entsize << " -> " << new_entsize; 617 618 // Resize the data and the section header. 619 data->d_size += hole_size; 620 section_header->sh_size += hole_size; 621 section_header->sh_entsize = new_entsize; 622 section_header->sh_type = new_sh_type; 623 624 // Add the hole size to all offsets in the ELF file that are after the 625 // start of the hole. If the hole size is positive we are expanding the 626 // section to create a new hole; if negative, we are closing up a hole. 627 628 // Start with the main ELF header. 629 typename ELF::Ehdr* elf_header = ELF::getehdr(elf); 630 AdjustElfHeaderForHole<ELF>(elf_header, hole_start, hole_size); 631 632 // Adjust all section headers. 633 AdjustSectionHeadersForHole<ELF>(elf, hole_start, hole_size); 634 635 // Rewrite the program headers to either split or coalesce segments, 636 // and adjust dynamic entries to match. 637 RewriteProgramHeadersForHole<ELF>(elf, hole_start, hole_size); 638 639 Elf_Scn* dynamic_section = GetDynamicSection<ELF>(elf); 640 AdjustDynamicSectionForHole(dynamic_section, hole_start, hole_size, relocations_type); 641 } 642 643 // Find the first slot in a dynamics array with the given tag. The array 644 // always ends with a free (unused) element, and which we exclude from the 645 // search. Returns dynamics->size() if not found. 646 template <typename ELF> 647 static size_t FindDynamicEntry(typename ELF::Sword tag, 648 std::vector<typename ELF::Dyn>* dynamics) { 649 // Loop until the penultimate entry. We exclude the end sentinel. 650 for (size_t i = 0; i < dynamics->size() - 1; ++i) { 651 if (dynamics->at(i).d_tag == tag) { 652 return i; 653 } 654 } 655 656 // The tag was not found. 657 return dynamics->size(); 658 } 659 660 // Replace dynamic entry. 661 template <typename ELF> 662 static void ReplaceDynamicEntry(typename ELF::Sword tag, 663 const typename ELF::Dyn& dyn, 664 std::vector<typename ELF::Dyn>* dynamics) { 665 const size_t slot = FindDynamicEntry<ELF>(tag, dynamics); 666 if (slot == dynamics->size()) { 667 LOG(FATAL) << "Dynamic slot is not found for tag=" << tag; 668 } 669 670 // Replace this entry with the one supplied. 671 dynamics->at(slot) = dyn; 672 VLOG(1) << "dynamic[" << slot << "] overwritten with " << dyn.d_tag; 673 } 674 675 // Remove relative entries from dynamic relocations and write as packed 676 // data into android packed relocations. 677 template <typename ELF> 678 bool ElfFile<ELF>::PackRelocations() { 679 // Load the ELF file into libelf. 680 if (!Load()) { 681 LOG(ERROR) << "Failed to load as ELF"; 682 return false; 683 } 684 685 // Retrieve the current dynamic relocations section data. 686 Elf_Data* data = GetSectionData(relocations_section_); 687 // we always pack rela, because packed format is pretty much the same 688 std::vector<typename ELF::Rela> relocations; 689 690 if (relocations_type_ == REL) { 691 // Convert data to a vector of relocations. 692 const typename ELF::Rel* relocations_base = reinterpret_cast<typename ELF::Rel*>(data->d_buf); 693 ConvertRelArrayToRelaVector(relocations_base, 694 data->d_size / sizeof(typename ELF::Rel), &relocations); 695 VLOG(1) << "Relocations : REL"; 696 } else if (relocations_type_ == RELA) { 697 // Convert data to a vector of relocations with addends. 698 const typename ELF::Rela* relocations_base = reinterpret_cast<typename ELF::Rela*>(data->d_buf); 699 relocations = std::vector<typename ELF::Rela>( 700 relocations_base, 701 relocations_base + data->d_size / sizeof(relocations[0])); 702 703 VLOG(1) << "Relocations : RELA"; 704 } else { 705 NOTREACHED(); 706 } 707 708 return PackTypedRelocations(&relocations); 709 } 710 711 // Helper for PackRelocations(). Rel type is one of ELF::Rel or ELF::Rela. 712 template <typename ELF> 713 bool ElfFile<ELF>::PackTypedRelocations(std::vector<typename ELF::Rela>* relocations) { 714 typedef typename ELF::Rela Rela; 715 716 if (has_android_relocations_) { 717 LOG(INFO) << "Relocation table is already packed"; 718 return true; 719 } 720 721 // If no relocations then we have nothing packable. Perhaps 722 // the shared object has already been packed? 723 if (relocations->empty()) { 724 LOG(ERROR) << "No relocations found"; 725 return false; 726 } 727 728 const size_t rel_size = 729 relocations_type_ == RELA ? sizeof(typename ELF::Rela) : sizeof(typename ELF::Rel); 730 const size_t initial_bytes = relocations->size() * rel_size; 731 732 VLOG(1) << "Unpacked : " << initial_bytes << " bytes"; 733 std::vector<uint8_t> packed; 734 RelocationPacker<ELF> packer; 735 736 // Pack relocations: dry run to estimate memory savings. 737 packer.PackRelocations(*relocations, &packed); 738 const size_t packed_bytes_estimate = packed.size() * sizeof(packed[0]); 739 VLOG(1) << "Packed (no padding): " << packed_bytes_estimate << " bytes"; 740 741 if (packed.empty()) { 742 VLOG(1) << "Too few relocations to pack"; 743 return true; 744 } 745 746 // Pre-calculate the size of the hole we will close up when we rewrite 747 // dynamic relocations. We have to adjust relocation addresses to 748 // account for this. 749 typename ELF::Shdr* section_header = ELF::getshdr(relocations_section_); 750 ssize_t hole_size = initial_bytes - packed_bytes_estimate; 751 752 // hole_size needs to be page_aligned. 753 hole_size -= hole_size % kPreserveAlignment; 754 755 VLOG(1) << "Compaction : " << hole_size << " bytes"; 756 757 // Adjusting for alignment may have removed any packing benefit. 758 if (hole_size == 0) { 759 VLOG(1) << "Too few relocations to pack after alignment"; 760 return true; 761 } 762 763 if (hole_size <= 0) { 764 VLOG(1) << "Packing relocations saves no space"; 765 return true; 766 } 767 768 size_t data_padding_bytes = is_padding_relocations_ ? 769 initial_bytes - packed_bytes_estimate : 770 initial_bytes - hole_size - packed_bytes_estimate; 771 772 // pad data 773 std::vector<uint8_t> padding(data_padding_bytes, 0); 774 packed.insert(packed.end(), padding.begin(), padding.end()); 775 776 const void* packed_data = &packed[0]; 777 778 // Run a loopback self-test as a check that packing is lossless. 779 std::vector<Rela> unpacked; 780 packer.UnpackRelocations(packed, &unpacked); 781 CHECK(unpacked.size() == relocations->size()); 782 CHECK(!memcmp(&unpacked[0], 783 &relocations->at(0), 784 unpacked.size() * sizeof(unpacked[0]))); 785 786 // Rewrite the current dynamic relocations section with packed one then shrink it to size. 787 const size_t bytes = packed.size() * sizeof(packed[0]); 788 ResizeSection(elf_, relocations_section_, bytes, 789 relocations_type_ == REL ? SHT_ANDROID_REL : SHT_ANDROID_RELA, relocations_type_); 790 RewriteSectionData(relocations_section_, packed_data, bytes); 791 792 // TODO (dimitry): fix string table and replace .rel.dyn/plt with .android.rel.dyn/plt 793 794 // Rewrite .dynamic and rename relocation tags describing the packed android 795 // relocations. 796 Elf_Data* data = GetSectionData(dynamic_section_); 797 const typename ELF::Dyn* dynamic_base = reinterpret_cast<typename ELF::Dyn*>(data->d_buf); 798 std::vector<typename ELF::Dyn> dynamics( 799 dynamic_base, 800 dynamic_base + data->d_size / sizeof(dynamics[0])); 801 section_header = ELF::getshdr(relocations_section_); 802 { 803 typename ELF::Dyn dyn; 804 dyn.d_tag = relocations_type_ == REL ? DT_ANDROID_REL : DT_ANDROID_RELA; 805 dyn.d_un.d_ptr = section_header->sh_addr; 806 ReplaceDynamicEntry<ELF>(relocations_type_ == REL ? DT_REL : DT_RELA, dyn, &dynamics); 807 } 808 { 809 typename ELF::Dyn dyn; 810 dyn.d_tag = relocations_type_ == REL ? DT_ANDROID_RELSZ : DT_ANDROID_RELASZ; 811 dyn.d_un.d_val = section_header->sh_size; 812 ReplaceDynamicEntry<ELF>(relocations_type_ == REL ? DT_RELSZ : DT_RELASZ, dyn, &dynamics); 813 } 814 815 const void* dynamics_data = &dynamics[0]; 816 const size_t dynamics_bytes = dynamics.size() * sizeof(dynamics[0]); 817 RewriteSectionData(dynamic_section_, dynamics_data, dynamics_bytes); 818 819 Flush(); 820 return true; 821 } 822 823 // Find packed relative relocations in the packed android relocations 824 // section, unpack them, and rewrite the dynamic relocations section to 825 // contain unpacked data. 826 template <typename ELF> 827 bool ElfFile<ELF>::UnpackRelocations() { 828 // Load the ELF file into libelf. 829 if (!Load()) { 830 LOG(ERROR) << "Failed to load as ELF"; 831 return false; 832 } 833 834 typename ELF::Shdr* section_header = ELF::getshdr(relocations_section_); 835 // Retrieve the current packed android relocations section data. 836 Elf_Data* data = GetSectionData(relocations_section_); 837 838 // Convert data to a vector of bytes. 839 const uint8_t* packed_base = reinterpret_cast<uint8_t*>(data->d_buf); 840 std::vector<uint8_t> packed( 841 packed_base, 842 packed_base + data->d_size / sizeof(packed[0])); 843 844 if ((section_header->sh_type == SHT_ANDROID_RELA || section_header->sh_type == SHT_ANDROID_REL) && 845 packed.size() > 3 && 846 packed[0] == 'A' && 847 packed[1] == 'P' && 848 packed[2] == 'S' && 849 packed[3] == '2') { 850 LOG(INFO) << "Relocations : " << (relocations_type_ == REL ? "REL" : "RELA"); 851 } else { 852 LOG(ERROR) << "Packed relocations not found (not packed?)"; 853 return false; 854 } 855 856 return UnpackTypedRelocations(packed); 857 } 858 859 // Helper for UnpackRelocations(). Rel type is one of ELF::Rel or ELF::Rela. 860 template <typename ELF> 861 bool ElfFile<ELF>::UnpackTypedRelocations(const std::vector<uint8_t>& packed) { 862 // Unpack the data to re-materialize the relative relocations. 863 const size_t packed_bytes = packed.size() * sizeof(packed[0]); 864 LOG(INFO) << "Packed : " << packed_bytes << " bytes"; 865 std::vector<typename ELF::Rela> unpacked_relocations; 866 RelocationPacker<ELF> packer; 867 packer.UnpackRelocations(packed, &unpacked_relocations); 868 869 const size_t relocation_entry_size = 870 relocations_type_ == REL ? sizeof(typename ELF::Rel) : sizeof(typename ELF::Rela); 871 const size_t unpacked_bytes = unpacked_relocations.size() * relocation_entry_size; 872 LOG(INFO) << "Unpacked : " << unpacked_bytes << " bytes"; 873 874 // Retrieve the current dynamic relocations section data. 875 Elf_Data* data = GetSectionData(relocations_section_); 876 877 LOG(INFO) << "Relocations : " << unpacked_relocations.size() << " entries"; 878 879 // If we found the same number of null relocation entries in the dynamic 880 // relocations section as we hold as unpacked relative relocations, then 881 // this is a padded file. 882 883 const bool is_padded = packed_bytes == unpacked_bytes; 884 885 // Unless padded, pre-apply relative relocations to account for the 886 // hole, and pre-adjust all relocation offsets accordingly. 887 typename ELF::Shdr* section_header = ELF::getshdr(relocations_section_); 888 889 if (!is_padded) { 890 LOG(INFO) << "Expansion : " << unpacked_bytes - packed_bytes << " bytes"; 891 } 892 893 // Rewrite the current dynamic relocations section with unpacked version of 894 // relocations. 895 const void* section_data = nullptr; 896 std::vector<typename ELF::Rel> unpacked_rel_relocations; 897 if (relocations_type_ == RELA) { 898 section_data = &unpacked_relocations[0]; 899 } else if (relocations_type_ == REL) { 900 ConvertRelaVectorToRelVector(unpacked_relocations, &unpacked_rel_relocations); 901 section_data = &unpacked_rel_relocations[0]; 902 } else { 903 NOTREACHED(); 904 } 905 906 ResizeSection(elf_, relocations_section_, unpacked_bytes, 907 relocations_type_ == REL ? SHT_REL : SHT_RELA, relocations_type_); 908 RewriteSectionData(relocations_section_, section_data, unpacked_bytes); 909 910 // Rewrite .dynamic to remove two tags describing packed android relocations. 911 data = GetSectionData(dynamic_section_); 912 const typename ELF::Dyn* dynamic_base = reinterpret_cast<typename ELF::Dyn*>(data->d_buf); 913 std::vector<typename ELF::Dyn> dynamics( 914 dynamic_base, 915 dynamic_base + data->d_size / sizeof(dynamics[0])); 916 { 917 typename ELF::Dyn dyn; 918 dyn.d_tag = relocations_type_ == REL ? DT_REL : DT_RELA; 919 dyn.d_un.d_ptr = section_header->sh_addr; 920 ReplaceDynamicEntry<ELF>(relocations_type_ == REL ? DT_ANDROID_REL : DT_ANDROID_RELA, 921 dyn, &dynamics); 922 } 923 924 { 925 typename ELF::Dyn dyn; 926 dyn.d_tag = relocations_type_ == REL ? DT_RELSZ : DT_RELASZ; 927 dyn.d_un.d_val = section_header->sh_size; 928 ReplaceDynamicEntry<ELF>(relocations_type_ == REL ? DT_ANDROID_RELSZ : DT_ANDROID_RELASZ, 929 dyn, &dynamics); 930 } 931 932 const void* dynamics_data = &dynamics[0]; 933 const size_t dynamics_bytes = dynamics.size() * sizeof(dynamics[0]); 934 RewriteSectionData(dynamic_section_, dynamics_data, dynamics_bytes); 935 936 Flush(); 937 return true; 938 } 939 940 // Flush rewritten shared object file data. 941 template <typename ELF> 942 void ElfFile<ELF>::Flush() { 943 // Flag all ELF data held in memory as needing to be written back to the 944 // file, and tell libelf that we have controlled the file layout. 945 elf_flagelf(elf_, ELF_C_SET, ELF_F_DIRTY); 946 elf_flagelf(elf_, ELF_C_SET, ELF_F_LAYOUT); 947 948 // Write ELF data back to disk. 949 const off_t file_bytes = elf_update(elf_, ELF_C_WRITE); 950 if (file_bytes == -1) { 951 LOG(ERROR) << "elf_update failed: " << elf_errmsg(elf_errno()); 952 } 953 954 CHECK(file_bytes > 0); 955 VLOG(1) << "elf_update returned: " << file_bytes; 956 957 // Clean up libelf, and truncate the output file to the number of bytes 958 // written by elf_update(). 959 elf_end(elf_); 960 elf_ = NULL; 961 const int truncate = ftruncate(fd_, file_bytes); 962 CHECK(truncate == 0); 963 } 964 965 template <typename ELF> 966 void ElfFile<ELF>::ConvertRelArrayToRelaVector(const typename ELF::Rel* rel_array, 967 size_t rel_array_size, 968 std::vector<typename ELF::Rela>* rela_vector) { 969 for (size_t i = 0; i<rel_array_size; ++i) { 970 typename ELF::Rela rela; 971 rela.r_offset = rel_array[i].r_offset; 972 rela.r_info = rel_array[i].r_info; 973 rela.r_addend = 0; 974 rela_vector->push_back(rela); 975 } 976 } 977 978 template <typename ELF> 979 void ElfFile<ELF>::ConvertRelaVectorToRelVector(const std::vector<typename ELF::Rela>& rela_vector, 980 std::vector<typename ELF::Rel>* rel_vector) { 981 for (auto rela : rela_vector) { 982 typename ELF::Rel rel; 983 rel.r_offset = rela.r_offset; 984 rel.r_info = rela.r_info; 985 CHECK(rela.r_addend == 0); 986 rel_vector->push_back(rel); 987 } 988 } 989 990 template class ElfFile<ELF32_traits>; 991 template class ElfFile<ELF64_traits>; 992 993 } // namespace relocation_packer 994