OLD | NEW |
(Empty) | |
| 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 // TODO(simonb): Extend for 64-bit target libraries. |
| 6 |
| 7 #include "elf_file.h" |
| 8 |
| 9 #include <stdlib.h> |
| 10 #include <sys/types.h> |
| 11 #include <unistd.h> |
| 12 #include <string> |
| 13 #include <vector> |
| 14 |
| 15 #include "debug.h" |
| 16 #include "libelf.h" |
| 17 #include "packer.h" |
| 18 |
| 19 namespace relocation_packer { |
| 20 |
| 21 // Stub identifier written to 'null out' packed data, "NULL". |
| 22 static const Elf32_Word kStubIdentifier = 0x4c4c554eu; |
| 23 |
| 24 // Out-of-band dynamic tags used to indicate the offset and size of the |
| 25 // .android.rel.dyn section. |
| 26 static const Elf32_Sword DT_ANDROID_ARM_REL_OFFSET = DT_LOPROC; |
| 27 static const Elf32_Sword DT_ANDROID_ARM_REL_SIZE = DT_LOPROC + 1; |
| 28 |
| 29 namespace { |
| 30 |
| 31 // Get section data. Checks that the section has exactly one data entry, |
| 32 // so that the section size and the data size are the same. True in |
| 33 // practice for all sections we resize when packing or unpacking. Done |
| 34 // by ensuring that a call to elf_getdata(section, data) returns NULL as |
| 35 // the next data entry. |
| 36 Elf_Data* GetSectionData(Elf_Scn* section) { |
| 37 Elf_Data* data = elf_getdata(section, NULL); |
| 38 CHECK(data && elf_getdata(section, data) == NULL); |
| 39 return data; |
| 40 } |
| 41 |
| 42 // Rewrite section data. Allocates new data and makes it the data element's |
| 43 // buffer. Relies on program exit to free allocated data. |
| 44 void RewriteSectionData(Elf_Data* data, |
| 45 const void* section_data, |
| 46 size_t size) { |
| 47 CHECK(size == data->d_size); |
| 48 uint8_t* area = new uint8_t[size]; |
| 49 memcpy(area, section_data, size); |
| 50 data->d_buf = area; |
| 51 } |
| 52 |
| 53 // Verbose ELF header logging. |
| 54 void VerboseLogElfHeader(const Elf32_Ehdr* elf_header) { |
| 55 VLOG("e_phoff = %u\n", elf_header->e_phoff); |
| 56 VLOG("e_shoff = %u\n", elf_header->e_shoff); |
| 57 VLOG("e_ehsize = %u\n", elf_header->e_ehsize); |
| 58 VLOG("e_phentsize = %u\n", elf_header->e_phentsize); |
| 59 VLOG("e_phnum = %u\n", elf_header->e_phnum); |
| 60 VLOG("e_shnum = %u\n", elf_header->e_shnum); |
| 61 VLOG("e_shstrndx = %u\n", elf_header->e_shstrndx); |
| 62 } |
| 63 |
| 64 // Verbose ELF program header logging. |
| 65 void VerboseLogProgramHeader(size_t program_header_index, |
| 66 const Elf32_Phdr* program_header) { |
| 67 std::string type; |
| 68 switch (program_header->p_type) { |
| 69 case PT_NULL: type = "NULL"; break; |
| 70 case PT_LOAD: type = "LOAD"; break; |
| 71 case PT_DYNAMIC: type = "DYNAMIC"; break; |
| 72 case PT_INTERP: type = "INTERP"; break; |
| 73 case PT_NOTE: type = "NOTE"; break; |
| 74 case PT_SHLIB: type = "SHLIB"; break; |
| 75 case PT_PHDR: type = "PHDR"; break; |
| 76 case PT_TLS: type = "TLS"; break; |
| 77 default: type = "(OTHER)"; break; |
| 78 } |
| 79 VLOG("phdr %lu : %s\n", program_header_index, type.c_str()); |
| 80 VLOG(" p_offset = %u\n", program_header->p_offset); |
| 81 VLOG(" p_vaddr = %u\n", program_header->p_vaddr); |
| 82 VLOG(" p_paddr = %u\n", program_header->p_paddr); |
| 83 VLOG(" p_filesz = %u\n", program_header->p_filesz); |
| 84 VLOG(" p_memsz = %u\n", program_header->p_memsz); |
| 85 } |
| 86 |
| 87 // Verbose ELF section header logging. |
| 88 void VerboseLogSectionHeader(const std::string& section_name, |
| 89 const Elf32_Shdr* section_header) { |
| 90 VLOG("section %s\n", section_name.c_str()); |
| 91 VLOG(" sh_addr = %u\n", section_header->sh_addr); |
| 92 VLOG(" sh_offset = %u\n", section_header->sh_offset); |
| 93 VLOG(" sh_size = %u\n", section_header->sh_size); |
| 94 } |
| 95 |
| 96 // Verbose ELF section data logging. |
| 97 void VerboseLogSectionData(const Elf_Data* data) { |
| 98 VLOG(" data\n"); |
| 99 VLOG(" d_buf = %p\n", data->d_buf); |
| 100 VLOG(" d_off = %lu\n", data->d_off); |
| 101 VLOG(" d_size = %lu\n", data->d_size); |
| 102 } |
| 103 |
| 104 } // namespace |
| 105 |
| 106 // Load the complete ELF file into a memory image in libelf, and identify |
| 107 // the .rel.dyn, .dynamic, and .android.rel.dyn sections. No-op if the |
| 108 // ELF file has already been loaded. |
| 109 bool ElfFile::Load() { |
| 110 if (elf_) |
| 111 return true; |
| 112 |
| 113 elf_ = elf_begin(fd_, ELF_C_RDWR, NULL); |
| 114 CHECK(elf_); |
| 115 |
| 116 if (elf_kind(elf_) != ELF_K_ELF) { |
| 117 LOG("ERROR: File not in ELF format\n"); |
| 118 return false; |
| 119 } |
| 120 |
| 121 Elf32_Ehdr* elf_header = elf32_getehdr(elf_); |
| 122 if (!elf_header) { |
| 123 LOG("ERROR: Failed to load ELF header\n"); |
| 124 return false; |
| 125 } |
| 126 if (elf_header->e_machine != EM_ARM) { |
| 127 LOG("ERROR: File is not an arm32 ELF file\n"); |
| 128 return false; |
| 129 } |
| 130 |
| 131 // Require that our endianness matches that of the target, and that both |
| 132 // are little-endian. Safe for all current build/target combinations. |
| 133 const int endian = static_cast<int>(elf_header->e_ident[5]); |
| 134 CHECK(endian == ELFDATA2LSB); |
| 135 CHECK(__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__); |
| 136 |
| 137 VLOG("endian = %u\n", endian); |
| 138 VerboseLogElfHeader(elf_header); |
| 139 |
| 140 const Elf32_Phdr* elf_program_header = elf32_getphdr(elf_); |
| 141 CHECK(elf_program_header); |
| 142 |
| 143 const Elf32_Phdr* dynamic_program_header = NULL; |
| 144 for (size_t i = 0; i < elf_header->e_phnum; ++i) { |
| 145 const Elf32_Phdr* program_header = &elf_program_header[i]; |
| 146 VerboseLogProgramHeader(i, program_header); |
| 147 |
| 148 if (program_header->p_type == PT_DYNAMIC) { |
| 149 CHECK(dynamic_program_header == NULL); |
| 150 dynamic_program_header = program_header; |
| 151 } |
| 152 } |
| 153 CHECK(dynamic_program_header != NULL); |
| 154 |
| 155 size_t string_index; |
| 156 elf_getshdrstrndx(elf_, &string_index); |
| 157 |
| 158 // Notes of the .rel.dyn, .android.rel.dyn, and .dynamic sections. Found |
| 159 // while iterating sections, and later stored in class attributes. |
| 160 Elf_Scn* found_rel_dyn_section = NULL; |
| 161 Elf_Scn* found_android_rel_dyn_section = NULL; |
| 162 Elf_Scn* found_dynamic_section = NULL; |
| 163 |
| 164 // Flag set if we encounter any .debug* section. We do not adjust any |
| 165 // offsets or addresses of any debug data, so if we find one of these then |
| 166 // the resulting output shared object should still run, but might not be |
| 167 // usable for debugging, disassembly, and so on. Provides a warning if |
| 168 // this occurs. |
| 169 bool has_debug_section = false; |
| 170 |
| 171 Elf_Scn* section = NULL; |
| 172 while ((section = elf_nextscn(elf_, section)) != NULL) { |
| 173 const Elf32_Shdr* section_header = elf32_getshdr(section); |
| 174 std::string name = elf_strptr(elf_, string_index, section_header->sh_name); |
| 175 VerboseLogSectionHeader(name, section_header); |
| 176 |
| 177 // Note special sections as we encounter them. |
| 178 if (name == ".rel.dyn") { |
| 179 found_rel_dyn_section = section; |
| 180 } |
| 181 if (name == ".android.rel.dyn") { |
| 182 found_android_rel_dyn_section = section; |
| 183 } |
| 184 if (section_header->sh_offset == dynamic_program_header->p_offset) { |
| 185 found_dynamic_section = section; |
| 186 } |
| 187 |
| 188 // If we find a section named .debug*, set the debug warning flag. |
| 189 if (std::string(name).find(".debug") == 0) { |
| 190 has_debug_section = true; |
| 191 } |
| 192 |
| 193 Elf_Data* data = NULL; |
| 194 while ((data = elf_getdata(section, data)) != NULL) { |
| 195 VerboseLogSectionData(data); |
| 196 } |
| 197 } |
| 198 |
| 199 // Loading failed if we did not find the required special sections. |
| 200 if (!found_rel_dyn_section) { |
| 201 LOG("ERROR: Missing .rel.dyn section\n"); |
| 202 return false; |
| 203 } |
| 204 if (!found_dynamic_section) { |
| 205 LOG("ERROR: Missing .dynamic section\n"); |
| 206 return false; |
| 207 } |
| 208 if (!found_android_rel_dyn_section) { |
| 209 LOG("ERROR: Missing .android.rel.dyn section " |
| 210 "(to fix, run with --help and follow the pre-packing instructions)\n"); |
| 211 return false; |
| 212 } |
| 213 |
| 214 if (has_debug_section) { |
| 215 LOG("WARNING: found .debug section(s), and ignored them\n"); |
| 216 } |
| 217 |
| 218 rel_dyn_section_ = found_rel_dyn_section; |
| 219 dynamic_section_ = found_dynamic_section; |
| 220 android_rel_dyn_section_ = found_android_rel_dyn_section; |
| 221 return true; |
| 222 } |
| 223 |
| 224 namespace { |
| 225 |
| 226 // Helper for ResizeSection(). Adjust the main ELF header for the hole. |
| 227 void AdjustElfHeaderForHole(Elf32_Ehdr* elf_header, |
| 228 Elf32_Off hole_start, |
| 229 int32_t hole_size) { |
| 230 if (elf_header->e_phoff > hole_start) { |
| 231 elf_header->e_phoff += hole_size; |
| 232 VLOG("e_phoff adjusted to %u\n", elf_header->e_phoff); |
| 233 } |
| 234 if (elf_header->e_shoff > hole_start) { |
| 235 elf_header->e_shoff += hole_size; |
| 236 VLOG("e_shoff adjusted to %u\n", elf_header->e_shoff); |
| 237 } |
| 238 } |
| 239 |
| 240 // Helper for ResizeSection(). Adjust all program headers for the hole. |
| 241 void AdjustProgramHeadersForHole(Elf32_Phdr* elf_program_header, |
| 242 size_t program_header_count, |
| 243 Elf32_Off hole_start, |
| 244 int32_t hole_size) { |
| 245 for (size_t i = 0; i < program_header_count; ++i) { |
| 246 Elf32_Phdr* program_header = &elf_program_header[i]; |
| 247 |
| 248 if (program_header->p_offset > hole_start) { |
| 249 // The hole start is past this segment, so adjust offsets and addrs. |
| 250 program_header->p_offset += hole_size; |
| 251 VLOG("phdr %lu p_offset adjusted to %u\n", i, program_header->p_offset); |
| 252 |
| 253 // Only adjust vaddr and paddr if this program header has them. |
| 254 if (program_header->p_vaddr != 0) { |
| 255 program_header->p_vaddr += hole_size; |
| 256 VLOG("phdr %lu p_vaddr adjusted to %u\n", i, program_header->p_vaddr); |
| 257 } |
| 258 if (program_header->p_paddr != 0) { |
| 259 program_header->p_paddr += hole_size; |
| 260 VLOG("phdr %lu p_paddr adjusted to %u\n", i, program_header->p_paddr); |
| 261 } |
| 262 } else if (program_header->p_offset + |
| 263 program_header->p_filesz > hole_start) { |
| 264 // The hole start is within this segment, so adjust file and in-memory |
| 265 // sizes, but leave offsets and addrs unchanged. |
| 266 program_header->p_filesz += hole_size; |
| 267 VLOG("phdr %lu p_filesz adjusted to %u\n", i, program_header->p_filesz); |
| 268 program_header->p_memsz += hole_size; |
| 269 VLOG("phdr %lu p_memsz adjusted to %u\n", i, program_header->p_memsz); |
| 270 } |
| 271 } |
| 272 } |
| 273 |
| 274 // Helper for ResizeSection(). Adjust all section headers for the hole. |
| 275 void AdjustSectionHeadersForHole(Elf* elf, |
| 276 Elf32_Off hole_start, |
| 277 int32_t hole_size) { |
| 278 size_t string_index; |
| 279 elf_getshdrstrndx(elf, &string_index); |
| 280 |
| 281 Elf_Scn* section = NULL; |
| 282 while ((section = elf_nextscn(elf, section)) != NULL) { |
| 283 Elf32_Shdr* section_header = elf32_getshdr(section); |
| 284 std::string name = elf_strptr(elf, string_index, section_header->sh_name); |
| 285 |
| 286 if (section_header->sh_offset > hole_start) { |
| 287 section_header->sh_offset += hole_size; |
| 288 VLOG("section %s sh_offset" |
| 289 " adjusted to %u\n", name.c_str(), section_header->sh_offset); |
| 290 // Only adjust section addr if this section has one. |
| 291 if (section_header->sh_addr != 0) { |
| 292 section_header->sh_addr += hole_size; |
| 293 VLOG("section %s sh_addr" |
| 294 " adjusted to %u\n", name.c_str(), section_header->sh_addr); |
| 295 } |
| 296 } |
| 297 } |
| 298 } |
| 299 |
| 300 // Helper for ResizeSection(). Adjust the .dynamic section for the hole. |
| 301 void AdjustDynamicSectionForHole(Elf_Scn* dynamic_section, |
| 302 bool is_rel_dyn_resize, |
| 303 Elf32_Off hole_start, |
| 304 int32_t hole_size) { |
| 305 Elf_Data* data = GetSectionData(dynamic_section); |
| 306 |
| 307 const Elf32_Dyn* dynamic_base = reinterpret_cast<Elf32_Dyn*>(data->d_buf); |
| 308 std::vector<Elf32_Dyn> dynamics( |
| 309 dynamic_base, |
| 310 dynamic_base + data->d_size / sizeof(dynamics[0])); |
| 311 |
| 312 for (size_t i = 0; i < dynamics.size(); ++i) { |
| 313 Elf32_Dyn* dynamic = &dynamics[i]; |
| 314 const Elf32_Sword tag = dynamic->d_tag; |
| 315 // Any tags that hold offsets are adjustment candidates. |
| 316 const bool is_adjustable = (tag == DT_PLTGOT || |
| 317 tag == DT_HASH || |
| 318 tag == DT_STRTAB || |
| 319 tag == DT_SYMTAB || |
| 320 tag == DT_RELA || |
| 321 tag == DT_INIT || |
| 322 tag == DT_FINI || |
| 323 tag == DT_REL || |
| 324 tag == DT_JMPREL || |
| 325 tag == DT_INIT_ARRAY || |
| 326 tag == DT_FINI_ARRAY || |
| 327 tag == DT_ANDROID_ARM_REL_OFFSET); |
| 328 if (is_adjustable && dynamic->d_un.d_ptr > hole_start) { |
| 329 dynamic->d_un.d_ptr += hole_size; |
| 330 VLOG("dynamic[%lu] %u" |
| 331 " d_ptr adjusted to %u\n", i, dynamic->d_tag, dynamic->d_un.d_ptr); |
| 332 } |
| 333 |
| 334 // If we are specifically resizing .rel.dyn, we need to make some added |
| 335 // adjustments to tags that indicate the counts of R_ARM_RELATIVE |
| 336 // relocations in the shared object. |
| 337 if (is_rel_dyn_resize) { |
| 338 // DT_RELSZ is the overall size of relocations. Adjust by hole size. |
| 339 if (tag == DT_RELSZ) { |
| 340 dynamic->d_un.d_val += hole_size; |
| 341 VLOG("dynamic[%lu] %u" |
| 342 " d_val adjusted to %u\n", i, dynamic->d_tag, dynamic->d_un.d_val); |
| 343 } |
| 344 |
| 345 // The crazy linker does not use DT_RELCOUNT, but we keep it updated |
| 346 // anyway. In practice the section hole is always equal to the size |
| 347 // of R_ARM_RELATIVE relocations, and DT_RELCOUNT is the count of |
| 348 // relative relocations. So closing a hole on packing reduces |
| 349 // DT_RELCOUNT to zero, and opening a hole on unpacking restores it to |
| 350 // its pre-packed value. |
| 351 if (tag == DT_RELCOUNT) { |
| 352 dynamic->d_un.d_val += hole_size / sizeof(Elf32_Rel); |
| 353 VLOG("dynamic[%lu] %u" |
| 354 " d_val adjusted to %u\n", i, dynamic->d_tag, dynamic->d_un.d_val); |
| 355 } |
| 356 |
| 357 // DT_RELENT doesn't change, but make sure it is what we expect. |
| 358 if (tag == DT_RELENT) { |
| 359 CHECK(dynamic->d_un.d_val == sizeof(Elf32_Rel)); |
| 360 } |
| 361 } |
| 362 } |
| 363 |
| 364 void* section_data = &dynamics[0]; |
| 365 size_t bytes = dynamics.size() * sizeof(dynamics[0]); |
| 366 RewriteSectionData(data, section_data, bytes); |
| 367 } |
| 368 |
| 369 // Helper for ResizeSection(). Adjust the .dynsym section for the hole. |
| 370 // We need to adjust the values for the symbols represented in it. |
| 371 void AdjustDynSymSectionForHole(Elf_Scn* dynsym_section, |
| 372 Elf32_Off hole_start, |
| 373 int32_t hole_size) { |
| 374 Elf_Data* data = GetSectionData(dynsym_section); |
| 375 |
| 376 const Elf32_Sym* dynsym_base = reinterpret_cast<Elf32_Sym*>(data->d_buf); |
| 377 std::vector<Elf32_Sym> dynsyms |
| 378 (dynsym_base, |
| 379 dynsym_base + data->d_size / sizeof(dynsyms[0])); |
| 380 |
| 381 for (size_t i = 0; i < dynsyms.size(); ++i) { |
| 382 Elf32_Sym* dynsym = &dynsyms[i]; |
| 383 const int type = static_cast<int>(ELF32_ST_TYPE(dynsym->st_info)); |
| 384 const bool is_adjustable = (type == STT_OBJECT || |
| 385 type == STT_FUNC || |
| 386 type == STT_SECTION || |
| 387 type == STT_FILE || |
| 388 type == STT_COMMON || |
| 389 type == STT_TLS); |
| 390 if (is_adjustable && dynsym->st_value > hole_start) { |
| 391 dynsym->st_value += hole_size; |
| 392 VLOG("dynsym[%lu] type=%u" |
| 393 " st_value adjusted to %u\n", i, type, dynsym->st_value); |
| 394 } |
| 395 } |
| 396 |
| 397 void* section_data = &dynsyms[0]; |
| 398 size_t bytes = dynsyms.size() * sizeof(dynsyms[0]); |
| 399 RewriteSectionData(data, section_data, bytes); |
| 400 } |
| 401 |
| 402 // Helper for ResizeSection(). Adjust the .rel.plt section for the hole. |
| 403 // We need to adjust the offset of every relocation inside it that falls |
| 404 // beyond the hole start. |
| 405 void AdjustRelPltSectionForHole(Elf_Scn* relplt_section, |
| 406 Elf32_Off hole_start, |
| 407 int32_t hole_size) { |
| 408 Elf_Data* data = GetSectionData(relplt_section); |
| 409 |
| 410 const Elf32_Rel* relplt_base = reinterpret_cast<Elf32_Rel*>(data->d_buf); |
| 411 std::vector<Elf32_Rel> relplts( |
| 412 relplt_base, |
| 413 relplt_base + data->d_size / sizeof(relplts[0])); |
| 414 |
| 415 for (size_t i = 0; i < relplts.size(); ++i) { |
| 416 Elf32_Rel* relplt = &relplts[i]; |
| 417 if (relplt->r_offset > hole_start) { |
| 418 relplt->r_offset += hole_size; |
| 419 VLOG("relplt[%lu] r_offset adjusted to %u\n", i, relplt->r_offset); |
| 420 } |
| 421 } |
| 422 |
| 423 void* section_data = &relplts[0]; |
| 424 size_t bytes = relplts.size() * sizeof(relplts[0]); |
| 425 RewriteSectionData(data, section_data, bytes); |
| 426 } |
| 427 |
| 428 // Helper for ResizeSection(). Adjust the .symtab section for the hole. |
| 429 // We want to adjust the value of every symbol in it that falls beyond |
| 430 // the hole start. |
| 431 void AdjustSymTabSectionForHole(Elf_Scn* symtab_section, |
| 432 Elf32_Off hole_start, |
| 433 int32_t hole_size) { |
| 434 Elf_Data* data = GetSectionData(symtab_section); |
| 435 |
| 436 const Elf32_Sym* symtab_base = reinterpret_cast<Elf32_Sym*>(data->d_buf); |
| 437 std::vector<Elf32_Sym> symtab( |
| 438 symtab_base, |
| 439 symtab_base + data->d_size / sizeof(symtab[0])); |
| 440 |
| 441 for (size_t i = 0; i < symtab.size(); ++i) { |
| 442 Elf32_Sym* sym = &symtab[i]; |
| 443 if (sym->st_value > hole_start) { |
| 444 sym->st_value += hole_size; |
| 445 VLOG("symtab[%lu] value adjusted to %u\n", i, sym->st_value); |
| 446 } |
| 447 } |
| 448 |
| 449 void* section_data = &symtab[0]; |
| 450 size_t bytes = symtab.size() * sizeof(symtab[0]); |
| 451 RewriteSectionData(data, section_data, bytes); |
| 452 } |
| 453 |
| 454 // Resize a section. If the new size is larger than the current size, open |
| 455 // up a hole by increasing file offsets that come after the hole. If smaller |
| 456 // than the current size, remove the hole by decreasing those offsets. |
| 457 void ResizeSection(Elf* elf, Elf_Scn* section, size_t new_size) { |
| 458 Elf32_Shdr* section_header = elf32_getshdr(section); |
| 459 if (section_header->sh_size == new_size) |
| 460 return; |
| 461 |
| 462 // Note if we are resizing the real .rel.dyn. If yes, then we have to |
| 463 // massage d_un.d_val in the dynamic section where d_tag is DT_RELSZ and |
| 464 // DT_RELCOUNT. |
| 465 size_t string_index; |
| 466 elf_getshdrstrndx(elf, &string_index); |
| 467 const std::string section_name = |
| 468 elf_strptr(elf, string_index, section_header->sh_name); |
| 469 const bool is_rel_dyn_resize = section_name == ".rel.dyn"; |
| 470 |
| 471 // Require that the section size and the data size are the same. True |
| 472 // in practice for all sections we resize when packing or unpacking. |
| 473 Elf_Data* data = GetSectionData(section); |
| 474 CHECK(data->d_off == 0 && data->d_size == section_header->sh_size); |
| 475 |
| 476 // Require that the section is not zero-length (that is, has allocated |
| 477 // data that we can validly expand). |
| 478 CHECK(data->d_size && data->d_buf); |
| 479 |
| 480 const Elf32_Off hole_start = section_header->sh_offset; |
| 481 const int32_t hole_size = new_size - data->d_size; |
| 482 |
| 483 VLOG_IF(hole_size > 0, "expand section size = %lu\n", data->d_size); |
| 484 VLOG_IF(hole_size < 0, "shrink section size = %lu\n", data->d_size); |
| 485 |
| 486 // Resize the data and the section header. |
| 487 data->d_size += hole_size; |
| 488 section_header->sh_size += hole_size; |
| 489 |
| 490 Elf32_Ehdr* elf_header = elf32_getehdr(elf); |
| 491 Elf32_Phdr* elf_program_header = elf32_getphdr(elf); |
| 492 |
| 493 // Add the hole size to all offsets in the ELF file that are after the |
| 494 // start of the hole. If the hole size is positive we are expanding the |
| 495 // section to create a new hole; if negative, we are closing up a hole. |
| 496 |
| 497 // Start with the main ELF header. |
| 498 AdjustElfHeaderForHole(elf_header, hole_start, hole_size); |
| 499 |
| 500 // Adjust all program headers. |
| 501 AdjustProgramHeadersForHole(elf_program_header, |
| 502 elf_header->e_phnum, |
| 503 hole_start, |
| 504 hole_size); |
| 505 |
| 506 // Adjust all section headers. |
| 507 AdjustSectionHeadersForHole(elf, hole_start, hole_size); |
| 508 |
| 509 // We use the dynamic program header entry to locate the dynamic section. |
| 510 const Elf32_Phdr* dynamic_program_header = NULL; |
| 511 |
| 512 // Find the dynamic program header entry. |
| 513 for (size_t i = 0; i < elf_header->e_phnum; ++i) { |
| 514 Elf32_Phdr* program_header = &elf_program_header[i]; |
| 515 |
| 516 if (program_header->p_type == PT_DYNAMIC) { |
| 517 dynamic_program_header = program_header; |
| 518 } |
| 519 } |
| 520 CHECK(dynamic_program_header); |
| 521 |
| 522 // Sections requiring special attention, and the .android.rel.dyn offset. |
| 523 Elf_Scn* dynamic_section = NULL; |
| 524 Elf_Scn* dynsym_section = NULL; |
| 525 Elf_Scn* relplt_section = NULL; |
| 526 Elf_Scn* symtab_section = NULL; |
| 527 Elf32_Off android_rel_dyn_offset = 0; |
| 528 |
| 529 // Find these sections, and the .android.rel.dyn offset. |
| 530 section = NULL; |
| 531 while ((section = elf_nextscn(elf, section)) != NULL) { |
| 532 Elf32_Shdr* section_header = elf32_getshdr(section); |
| 533 std::string name = elf_strptr(elf, string_index, section_header->sh_name); |
| 534 |
| 535 if (section_header->sh_offset == dynamic_program_header->p_offset) { |
| 536 dynamic_section = section; |
| 537 } |
| 538 if (name == ".dynsym") { |
| 539 dynsym_section = section; |
| 540 } |
| 541 if (name == ".rel.plt") { |
| 542 relplt_section = section; |
| 543 } |
| 544 if (name == ".symtab") { |
| 545 symtab_section = section; |
| 546 } |
| 547 |
| 548 // Note .android.rel.dyn offset. |
| 549 if (name == ".android.rel.dyn") { |
| 550 android_rel_dyn_offset = section_header->sh_offset; |
| 551 } |
| 552 } |
| 553 CHECK(dynamic_section != NULL); |
| 554 CHECK(dynsym_section != NULL); |
| 555 CHECK(relplt_section != NULL); |
| 556 CHECK(android_rel_dyn_offset != 0); |
| 557 |
| 558 // Adjust the .dynamic section for the hole. Because we have to edit the |
| 559 // current contents of .dynamic we disallow resizing it. |
| 560 CHECK(section != dynamic_section); |
| 561 AdjustDynamicSectionForHole(dynamic_section, |
| 562 is_rel_dyn_resize, |
| 563 hole_start, |
| 564 hole_size); |
| 565 |
| 566 // Adjust the .dynsym section for the hole. |
| 567 AdjustDynSymSectionForHole(dynsym_section, hole_start, hole_size); |
| 568 |
| 569 // Adjust the .rel.plt section for the hole. |
| 570 AdjustRelPltSectionForHole(relplt_section, hole_start, hole_size); |
| 571 |
| 572 // If present, adjust the .symtab section for the hole. If the shared |
| 573 // library was stripped then .symtab will be absent. |
| 574 if (symtab_section) |
| 575 AdjustSymTabSectionForHole(symtab_section, hole_start, hole_size); |
| 576 } |
| 577 |
| 578 // Replace the first free (unused) slot in a dynamics vector with the given |
| 579 // value. The vector always ends with a free (unused) element, so the slot |
| 580 // found cannot be the last one in the vector. |
| 581 void AddDynamicEntry(Elf32_Dyn dyn, |
| 582 std::vector<Elf32_Dyn>* dynamics) { |
| 583 // Loop until the penultimate entry. We cannot replace the end sentinel. |
| 584 for (size_t i = 0; i < dynamics->size() - 1; ++i) { |
| 585 Elf32_Dyn &slot = dynamics->at(i); |
| 586 if (slot.d_tag == DT_NULL) { |
| 587 slot = dyn; |
| 588 VLOG("dynamic[%lu] overwritten with %u\n", i, dyn.d_tag); |
| 589 return; |
| 590 } |
| 591 } |
| 592 |
| 593 // No free dynamics vector slot was found. |
| 594 LOG("FATAL: No spare dynamic vector slots found " |
| 595 "(to fix, increase gold's --spare-dynamic-tags value)\n"); |
| 596 NOTREACHED(); |
| 597 } |
| 598 |
| 599 // Remove the element in the dynamics vector that matches the given tag with |
| 600 // unused slot data. Shuffle the following elements up, and ensure that the |
| 601 // last is the null sentinel. |
| 602 void RemoveDynamicEntry(Elf32_Sword tag, |
| 603 std::vector<Elf32_Dyn>* dynamics) { |
| 604 // Loop until the penultimate entry, and never match the end sentinel. |
| 605 for (size_t i = 0; i < dynamics->size() - 1; ++i) { |
| 606 Elf32_Dyn &slot = dynamics->at(i); |
| 607 if (slot.d_tag == tag) { |
| 608 for ( ; i < dynamics->size() - 1; ++i) { |
| 609 dynamics->at(i) = dynamics->at(i + 1); |
| 610 VLOG("dynamic[%lu] overwritten with dynamic[%lu]\n", i, i + 1); |
| 611 } |
| 612 CHECK(dynamics->at(i).d_tag == DT_NULL); |
| 613 return; |
| 614 } |
| 615 } |
| 616 |
| 617 // No matching dynamics vector entry was found. |
| 618 NOTREACHED(); |
| 619 } |
| 620 |
| 621 // Apply R_ARM_RELATIVE relocations to the file data to which they refer. |
| 622 // This relocates data into the area it will occupy after the hole in |
| 623 // .rel.dyn is added or removed. |
| 624 void AdjustRelocationTargets(Elf* elf, |
| 625 Elf32_Off hole_start, |
| 626 size_t hole_size, |
| 627 const std::vector<Elf32_Rel>& relocations) { |
| 628 Elf_Scn* section = NULL; |
| 629 while ((section = elf_nextscn(elf, section)) != NULL) { |
| 630 const Elf32_Shdr* section_header = elf32_getshdr(section); |
| 631 |
| 632 // Identify this section's start and end addresses. |
| 633 const Elf32_Addr section_start = section_header->sh_addr; |
| 634 const Elf32_Addr section_end = section_start + section_header->sh_size; |
| 635 |
| 636 Elf_Data* data = GetSectionData(section); |
| 637 |
| 638 // Ignore sections with no effective data. |
| 639 if (data->d_buf == NULL) |
| 640 continue; |
| 641 |
| 642 // Create a copy-on-write pointer to the section's data. |
| 643 uint8_t* area = reinterpret_cast<uint8_t*>(data->d_buf); |
| 644 |
| 645 for (size_t i = 0; i < relocations.size(); ++i) { |
| 646 const Elf32_Rel* relocation = &relocations[i]; |
| 647 CHECK(ELF32_R_TYPE(relocation->r_info) == R_ARM_RELATIVE); |
| 648 |
| 649 // See if this relocation points into the current section. |
| 650 if (relocation->r_offset >= section_start && |
| 651 relocation->r_offset < section_end) { |
| 652 Elf32_Addr byte_offset = relocation->r_offset - section_start; |
| 653 Elf32_Off* target = reinterpret_cast<Elf32_Off*>(area + byte_offset); |
| 654 |
| 655 // Is the relocation's target after the hole's start? |
| 656 if (*target > hole_start) { |
| 657 |
| 658 // Copy on first write. Recompute target to point into the newly |
| 659 // allocated buffer. |
| 660 if (area == data->d_buf) { |
| 661 area = new uint8_t[data->d_size]; |
| 662 memcpy(area, data->d_buf, data->d_size); |
| 663 target = reinterpret_cast<Elf32_Off*>(area + byte_offset); |
| 664 } |
| 665 |
| 666 *target += hole_size; |
| 667 VLOG("relocation[%lu] target adjusted to %u\n", i, *target); |
| 668 } |
| 669 } |
| 670 } |
| 671 |
| 672 // If we applied any relocation to this section, write it back. |
| 673 if (area != data->d_buf) { |
| 674 RewriteSectionData(data, area, data->d_size); |
| 675 delete [] area; |
| 676 } |
| 677 } |
| 678 } |
| 679 |
| 680 // Adjust relocations so that the offset that they indicate will be correct |
| 681 // after the hole in .rel.dyn is added or removed (in effect, relocate the |
| 682 // relocations). |
| 683 void AdjustRelocations(Elf32_Off hole_start, |
| 684 size_t hole_size, |
| 685 std::vector<Elf32_Rel>* relocations) { |
| 686 for (size_t i = 0; i < relocations->size(); ++i) { |
| 687 Elf32_Rel* relocation = &relocations->at(i); |
| 688 if (relocation->r_offset > hole_start) { |
| 689 relocation->r_offset += hole_size; |
| 690 VLOG("relocation[%lu] offset adjusted to %u\n", i, relocation->r_offset); |
| 691 } |
| 692 } |
| 693 } |
| 694 |
| 695 } // namespace |
| 696 |
| 697 // Remove R_ARM_RELATIVE entries from .rel.dyn and write as packed data |
| 698 // into .android.rel.dyn. |
| 699 bool ElfFile::PackRelocations() { |
| 700 // Load the ELF file into libelf. |
| 701 if (!Load()) { |
| 702 LOG("ERROR: Failed to load as ELF (elf_error=%d)\n", elf_errno()); |
| 703 return false; |
| 704 } |
| 705 |
| 706 // Retrieve the current .rel.dyn section data. |
| 707 Elf_Data* data = GetSectionData(rel_dyn_section_); |
| 708 |
| 709 // Convert data to a vector of Elf32 relocations. |
| 710 const Elf32_Rel* relocations_base = reinterpret_cast<Elf32_Rel*>(data->d_buf); |
| 711 std::vector<Elf32_Rel> relocations( |
| 712 relocations_base, |
| 713 relocations_base + data->d_size / sizeof(relocations[0])); |
| 714 |
| 715 std::vector<Elf32_Rel> relative_relocations; |
| 716 std::vector<Elf32_Rel> other_relocations; |
| 717 |
| 718 // Filter relocations into those that are R_ARM_RELATIVE and others. |
| 719 for (size_t i = 0; i < relocations.size(); ++i) { |
| 720 const Elf32_Rel& relocation = relocations[i]; |
| 721 if (ELF32_R_TYPE(relocation.r_info) == R_ARM_RELATIVE) { |
| 722 CHECK(ELF32_R_SYM(relocation.r_info) == 0); |
| 723 relative_relocations.push_back(relocation); |
| 724 } else { |
| 725 other_relocations.push_back(relocation); |
| 726 } |
| 727 } |
| 728 VLOG("R_ARM_RELATIVE: %lu entries\n", relative_relocations.size()); |
| 729 VLOG("Other : %lu entries\n", other_relocations.size()); |
| 730 VLOG("Total : %lu entries\n", relocations.size()); |
| 731 |
| 732 // If no relative relocations then we have nothing packable. Perhaps |
| 733 // the shared object has already been packed? |
| 734 if (relative_relocations.empty()) { |
| 735 LOG("ERROR: No R_ARM_RELATIVE relocations found (already packed?)\n"); |
| 736 return false; |
| 737 } |
| 738 |
| 739 // Pre-calculate the size of the hole we will close up when we rewrite |
| 740 // .reldyn. We have to adjust all relocation addresses to account for this. |
| 741 Elf32_Shdr* section_header = elf32_getshdr(rel_dyn_section_); |
| 742 const Elf32_Off hole_start = section_header->sh_offset; |
| 743 const size_t hole_size = |
| 744 relative_relocations.size() * sizeof(relative_relocations[0]); |
| 745 |
| 746 // Unless padding, pre-apply R_ARM_RELATIVE relocations to account for the |
| 747 // hole, and pre-adjust all relocation offsets accordingly. |
| 748 if (!is_padding_rel_dyn_) { |
| 749 // Apply relocations to all R_ARM_RELATIVE data to relocate it into the |
| 750 // area it will occupy once the hole in .rel.dyn is removed. |
| 751 AdjustRelocationTargets(elf_, hole_start, -hole_size, relative_relocations); |
| 752 // Relocate the relocations. |
| 753 AdjustRelocations(hole_start, -hole_size, &relative_relocations); |
| 754 AdjustRelocations(hole_start, -hole_size, &other_relocations); |
| 755 } |
| 756 |
| 757 // Pack R_ARM_RELATIVE relocations. |
| 758 const size_t initial_bytes = |
| 759 relative_relocations.size() * sizeof(relative_relocations[0]); |
| 760 LOG("Unpacked R_ARM_RELATIVE: %lu bytes\n", initial_bytes); |
| 761 std::vector<uint8_t> packed; |
| 762 RelocationPacker packer; |
| 763 packer.PackRelativeRelocations(relative_relocations, &packed); |
| 764 const void* packed_data = &packed[0]; |
| 765 const size_t packed_bytes = packed.size() * sizeof(packed[0]); |
| 766 LOG("Packed R_ARM_RELATIVE: %lu bytes\n", packed_bytes); |
| 767 |
| 768 // If we have insufficient R_ARM_RELATIVE relocations to form a run then |
| 769 // packing fails. |
| 770 if (packed.empty()) { |
| 771 LOG("Too few R_ARM_RELATIVE relocations to pack\n"); |
| 772 return false; |
| 773 } |
| 774 |
| 775 // Run a loopback self-test as a check that packing is lossless. |
| 776 std::vector<Elf32_Rel> unpacked; |
| 777 packer.UnpackRelativeRelocations(packed, &unpacked); |
| 778 CHECK(unpacked.size() == relative_relocations.size()); |
| 779 for (size_t i = 0; i < unpacked.size(); ++i) { |
| 780 CHECK(unpacked[i].r_offset == relative_relocations[i].r_offset); |
| 781 CHECK(unpacked[i].r_info == relative_relocations[i].r_info); |
| 782 } |
| 783 |
| 784 // Make sure packing saved some space. |
| 785 if (packed_bytes >= initial_bytes) { |
| 786 LOG("Packing R_ARM_RELATIVE relocations saves no space\n"); |
| 787 return false; |
| 788 } |
| 789 |
| 790 // If padding, add R_ARM_NONE relocations to other_relocations to make it |
| 791 // the same size as the the original relocations we read in. This makes |
| 792 // the ResizeSection() below a no-op. |
| 793 if (is_padding_rel_dyn_) { |
| 794 const Elf32_Rel r_arm_none = {R_ARM_NONE, 0}; |
| 795 const size_t required = relocations.size() - other_relocations.size(); |
| 796 std::vector<Elf32_Rel> padding(required, r_arm_none); |
| 797 other_relocations.insert( |
| 798 other_relocations.end(), padding.begin(), padding.end()); |
| 799 } |
| 800 |
| 801 // Rewrite the current .rel.dyn section to be only the non-R_ARM_RELATIVE |
| 802 // relocations, then shrink it to size. |
| 803 const void* section_data = &other_relocations[0]; |
| 804 const size_t bytes = other_relocations.size() * sizeof(other_relocations[0]); |
| 805 ResizeSection(elf_, rel_dyn_section_, bytes); |
| 806 RewriteSectionData(data, section_data, bytes); |
| 807 |
| 808 // Rewrite the current .android.rel.dyn section to hold the packed |
| 809 // R_ARM_RELATIVE relocations. |
| 810 data = GetSectionData(android_rel_dyn_section_); |
| 811 ResizeSection(elf_, android_rel_dyn_section_, packed_bytes); |
| 812 RewriteSectionData(data, packed_data, packed_bytes); |
| 813 |
| 814 // Rewrite .dynamic to include two new tags describing .android.rel.dyn. |
| 815 data = GetSectionData(dynamic_section_); |
| 816 const Elf32_Dyn* dynamic_base = reinterpret_cast<Elf32_Dyn*>(data->d_buf); |
| 817 std::vector<Elf32_Dyn> dynamics( |
| 818 dynamic_base, |
| 819 dynamic_base + data->d_size / sizeof(dynamics[0])); |
| 820 section_header = elf32_getshdr(android_rel_dyn_section_); |
| 821 // Use two of the spare slots to describe the .android.rel.dyn section. |
| 822 const Elf32_Dyn offset_dyn |
| 823 = {DT_ANDROID_ARM_REL_OFFSET, {section_header->sh_offset}}; |
| 824 AddDynamicEntry(offset_dyn, &dynamics); |
| 825 const Elf32_Dyn size_dyn |
| 826 = {DT_ANDROID_ARM_REL_SIZE, {section_header->sh_size}}; |
| 827 AddDynamicEntry(size_dyn, &dynamics); |
| 828 const void* dynamics_data = &dynamics[0]; |
| 829 const size_t dynamics_bytes = dynamics.size() * sizeof(dynamics[0]); |
| 830 RewriteSectionData(data, dynamics_data, dynamics_bytes); |
| 831 |
| 832 Flush(); |
| 833 return true; |
| 834 } |
| 835 |
| 836 // Find packed R_ARM_RELATIVE relocations in .android.rel.dyn, unpack them, |
| 837 // and rewrite the .rel.dyn section in so_file to contain unpacked data. |
| 838 bool ElfFile::UnpackRelocations() { |
| 839 // Load the ELF file into libelf. |
| 840 if (!Load()) { |
| 841 LOG("ERROR: Failed to load as ELF (elf_error=%d)\n", elf_errno()); |
| 842 return false; |
| 843 } |
| 844 |
| 845 // Retrieve the current .android.rel.dyn section data. |
| 846 Elf_Data* data = GetSectionData(android_rel_dyn_section_); |
| 847 |
| 848 // Convert data to a vector of bytes. |
| 849 const uint8_t* packed_base = reinterpret_cast<uint8_t*>(data->d_buf); |
| 850 std::vector<uint8_t> packed( |
| 851 packed_base, |
| 852 packed_base + data->d_size / sizeof(packed[0])); |
| 853 |
| 854 // Properly packed data must begin with "APR1". |
| 855 if (packed.empty() || |
| 856 packed[0] != 'A' || packed[1] != 'P' || |
| 857 packed[2] != 'R' || packed[3] != '1') { |
| 858 LOG("ERROR: Packed R_ARM_RELATIVE relocations not found (not packed?)\n"); |
| 859 return false; |
| 860 } |
| 861 |
| 862 // Unpack the data to re-materialize the R_ARM_RELATIVE relocations. |
| 863 const size_t packed_bytes = packed.size() * sizeof(packed[0]); |
| 864 LOG("Packed R_ARM_RELATIVE: %lu bytes\n", packed_bytes); |
| 865 std::vector<Elf32_Rel> relative_relocations; |
| 866 RelocationPacker packer; |
| 867 packer.UnpackRelativeRelocations(packed, &relative_relocations); |
| 868 const size_t unpacked_bytes = |
| 869 relative_relocations.size() * sizeof(relative_relocations[0]); |
| 870 LOG("Unpacked R_ARM_RELATIVE: %lu bytes\n", unpacked_bytes); |
| 871 |
| 872 // Retrieve the current .rel.dyn section data. |
| 873 data = GetSectionData(rel_dyn_section_); |
| 874 |
| 875 // Interpret data as Elf32 relocations. |
| 876 const Elf32_Rel* relocations_base = reinterpret_cast<Elf32_Rel*>(data->d_buf); |
| 877 std::vector<Elf32_Rel> relocations( |
| 878 relocations_base, |
| 879 relocations_base + data->d_size / sizeof(relocations[0])); |
| 880 |
| 881 std::vector<Elf32_Rel> other_relocations; |
| 882 size_t padding = 0; |
| 883 |
| 884 // Filter relocations to locate any that are R_ARM_NONE. These will occur |
| 885 // if padding was turned on for packing. |
| 886 for (size_t i = 0; i < relocations.size(); ++i) { |
| 887 const Elf32_Rel& relocation = relocations[i]; |
| 888 if (ELF32_R_TYPE(relocation.r_info) != R_ARM_NONE) { |
| 889 other_relocations.push_back(relocation); |
| 890 } else { |
| 891 ++padding; |
| 892 } |
| 893 } |
| 894 LOG("R_ARM_RELATIVE: %lu entries\n", relative_relocations.size()); |
| 895 LOG("Other : %lu entries\n", other_relocations.size()); |
| 896 |
| 897 // If we found the same number of R_ARM_NONE entries in .rel.dyn as we |
| 898 // hold as unpacked relative relocations, then this is a padded file. |
| 899 const bool is_padded = padding == relative_relocations.size(); |
| 900 |
| 901 // Pre-calculate the size of the hole we will open up when we rewrite |
| 902 // .reldyn. We have to adjust all relocation addresses to account for this. |
| 903 Elf32_Shdr* section_header = elf32_getshdr(rel_dyn_section_); |
| 904 const Elf32_Off hole_start = section_header->sh_offset; |
| 905 const size_t hole_size = |
| 906 relative_relocations.size() * sizeof(relative_relocations[0]); |
| 907 |
| 908 // Unless padded, pre-apply R_ARM_RELATIVE relocations to account for the |
| 909 // hole, and pre-adjust all relocation offsets accordingly. |
| 910 if (!is_padded) { |
| 911 // Apply relocations to all R_ARM_RELATIVE data to relocate it into the |
| 912 // area it will occupy once the hole in .rel.dyn is opened. |
| 913 AdjustRelocationTargets(elf_, hole_start, hole_size, relative_relocations); |
| 914 // Relocate the relocations. |
| 915 AdjustRelocations(hole_start, hole_size, &relative_relocations); |
| 916 AdjustRelocations(hole_start, hole_size, &other_relocations); |
| 917 } |
| 918 |
| 919 // Rewrite the current .rel.dyn section to be the R_ARM_RELATIVE relocations |
| 920 // followed by other relocations. This is the usual order in which we find |
| 921 // them after linking, so this action will normally put the entire .rel.dyn |
| 922 // section back to its pre-split-and-packed state. |
| 923 relocations.assign(relative_relocations.begin(), relative_relocations.end()); |
| 924 relocations.insert(relocations.end(), |
| 925 other_relocations.begin(), other_relocations.end()); |
| 926 const void* section_data = &relocations[0]; |
| 927 const size_t bytes = relocations.size() * sizeof(relocations[0]); |
| 928 LOG("Total : %lu entries\n", relocations.size()); |
| 929 ResizeSection(elf_, rel_dyn_section_, bytes); |
| 930 RewriteSectionData(data, section_data, bytes); |
| 931 |
| 932 // Nearly empty the current .android.rel.dyn section. Leaves a four-byte |
| 933 // stub so that some data remains allocated to the section. This is a |
| 934 // convenience which allows us to re-pack this file again without |
| 935 // having to remove the section and then add a new small one with objcopy. |
| 936 // The way we resize sections relies on there being some data in a section. |
| 937 data = GetSectionData(android_rel_dyn_section_); |
| 938 ResizeSection(elf_, android_rel_dyn_section_, sizeof(kStubIdentifier)); |
| 939 RewriteSectionData(data, &kStubIdentifier, sizeof(kStubIdentifier)); |
| 940 |
| 941 // Rewrite .dynamic to remove two tags describing .android.rel.dyn. |
| 942 data = GetSectionData(dynamic_section_); |
| 943 const Elf32_Dyn* dynamic_base = reinterpret_cast<Elf32_Dyn*>(data->d_buf); |
| 944 std::vector<Elf32_Dyn> dynamics( |
| 945 dynamic_base, |
| 946 dynamic_base + data->d_size / sizeof(dynamics[0])); |
| 947 RemoveDynamicEntry(DT_ANDROID_ARM_REL_SIZE, &dynamics); |
| 948 RemoveDynamicEntry(DT_ANDROID_ARM_REL_OFFSET, &dynamics); |
| 949 const void* dynamics_data = &dynamics[0]; |
| 950 const size_t dynamics_bytes = dynamics.size() * sizeof(dynamics[0]); |
| 951 RewriteSectionData(data, dynamics_data, dynamics_bytes); |
| 952 |
| 953 Flush(); |
| 954 return true; |
| 955 } |
| 956 |
| 957 // Flush rewritten shared object file data. |
| 958 void ElfFile::Flush() { |
| 959 // Flag all ELF data held in memory as needing to be written back to the |
| 960 // file, and tell libelf that we have controlled the file layout. |
| 961 elf_flagelf(elf_, ELF_C_SET, ELF_F_DIRTY); |
| 962 elf_flagelf(elf_, ELF_C_SET, ELF_F_LAYOUT); |
| 963 |
| 964 // Write ELF data back to disk. |
| 965 const off_t file_bytes = elf_update(elf_, ELF_C_WRITE); |
| 966 CHECK(file_bytes > 0); |
| 967 VLOG("elf_update returned: %lu\n", file_bytes); |
| 968 |
| 969 // Clean up libelf, and truncate the output file to the number of bytes |
| 970 // written by elf_update(). |
| 971 elf_end(elf_); |
| 972 elf_ = NULL; |
| 973 const int truncate = ftruncate(fd_, file_bytes); |
| 974 CHECK(truncate == 0); |
| 975 } |
| 976 |
| 977 } // namespace relocation_packer |
OLD | NEW |