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