blob: d06bd639213db3991aec58bcfe88b7f2a56eba32 [file] [log] [blame]
Dmitriy Ivanov87a06172015-02-06 10:56:28 -08001// 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 Ivanovf8ff6b12015-01-27 19:32:56 -08008// 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 Ivanov87a06172015-02-06 10:56:28 -080012
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
27namespace relocation_packer {
28
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080029// Out-of-band dynamic tags used to indicate the offset and size of the
30// android packed relocations section.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080031static constexpr int32_t DT_ANDROID_REL = DT_LOOS + 2;
32static constexpr int32_t DT_ANDROID_RELSZ = DT_LOOS + 3;
33
34static constexpr int32_t DT_ANDROID_RELA = DT_LOOS + 4;
35static constexpr int32_t DT_ANDROID_RELASZ = DT_LOOS + 5;
36
37static constexpr uint32_t SHT_ANDROID_REL = SHT_LOOS + 1;
38static constexpr uint32_t SHT_ANDROID_RELA = SHT_LOOS + 2;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080039
Dmitriy Ivanovb2939692015-04-27 18:53:27 -070040static const size_t kPageSize = 4096;
41
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080042// Alignment to preserve, in bytes. This must be at least as large as the
43// largest d_align and sh_addralign values found in the loaded file.
44// Out of caution for RELRO page alignment, we preserve to a complete target
45// page. See http://www.airs.com/blog/archives/189.
Dmitriy Ivanovb2939692015-04-27 18:53:27 -070046static const size_t kPreserveAlignment = kPageSize;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080047
48// Get section data. Checks that the section has exactly one data entry,
49// so that the section size and the data size are the same. True in
50// practice for all sections we resize when packing or unpacking. Done
51// by ensuring that a call to elf_getdata(section, data) returns NULL as
52// the next data entry.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080053static Elf_Data* GetSectionData(Elf_Scn* section) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080054 Elf_Data* data = elf_getdata(section, NULL);
55 CHECK(data && elf_getdata(section, data) == NULL);
56 return data;
57}
58
59// Rewrite section data. Allocates new data and makes it the data element's
60// buffer. Relies on program exit to free allocated data.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080061static void RewriteSectionData(Elf_Scn* section,
62 const void* section_data,
63 size_t size) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080064 Elf_Data* data = GetSectionData(section);
65 CHECK(size == data->d_size);
66 uint8_t* area = new uint8_t[size];
67 memcpy(area, section_data, size);
68 data->d_buf = area;
69}
70
71// Verbose ELF header logging.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080072template <typename Ehdr>
73static void VerboseLogElfHeader(const Ehdr* elf_header) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080074 VLOG(1) << "e_phoff = " << elf_header->e_phoff;
75 VLOG(1) << "e_shoff = " << elf_header->e_shoff;
76 VLOG(1) << "e_ehsize = " << elf_header->e_ehsize;
77 VLOG(1) << "e_phentsize = " << elf_header->e_phentsize;
78 VLOG(1) << "e_phnum = " << elf_header->e_phnum;
79 VLOG(1) << "e_shnum = " << elf_header->e_shnum;
80 VLOG(1) << "e_shstrndx = " << elf_header->e_shstrndx;
81}
82
83// Verbose ELF program header logging.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080084template <typename Phdr>
85static void VerboseLogProgramHeader(size_t program_header_index,
86 const Phdr* program_header) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080087 std::string type;
88 switch (program_header->p_type) {
89 case PT_NULL: type = "NULL"; break;
90 case PT_LOAD: type = "LOAD"; break;
91 case PT_DYNAMIC: type = "DYNAMIC"; break;
92 case PT_INTERP: type = "INTERP"; break;
93 case PT_PHDR: type = "PHDR"; break;
94 case PT_GNU_RELRO: type = "GNU_RELRO"; break;
95 case PT_GNU_STACK: type = "GNU_STACK"; break;
96 case PT_ARM_EXIDX: type = "EXIDX"; break;
97 default: type = "(OTHER)"; break;
98 }
99 VLOG(1) << "phdr[" << program_header_index << "] : " << type;
100 VLOG(1) << " p_offset = " << program_header->p_offset;
101 VLOG(1) << " p_vaddr = " << program_header->p_vaddr;
102 VLOG(1) << " p_paddr = " << program_header->p_paddr;
103 VLOG(1) << " p_filesz = " << program_header->p_filesz;
104 VLOG(1) << " p_memsz = " << program_header->p_memsz;
105 VLOG(1) << " p_flags = " << program_header->p_flags;
106 VLOG(1) << " p_align = " << program_header->p_align;
107}
108
109// Verbose ELF section header logging.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800110template <typename Shdr>
111static void VerboseLogSectionHeader(const std::string& section_name,
112 const Shdr* section_header) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800113 VLOG(1) << "section " << section_name;
114 VLOG(1) << " sh_addr = " << section_header->sh_addr;
115 VLOG(1) << " sh_offset = " << section_header->sh_offset;
116 VLOG(1) << " sh_size = " << section_header->sh_size;
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800117 VLOG(1) << " sh_entsize = " << section_header->sh_entsize;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800118 VLOG(1) << " sh_addralign = " << section_header->sh_addralign;
119}
120
121// Verbose ELF section data logging.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800122static void VerboseLogSectionData(const Elf_Data* data) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800123 VLOG(1) << " data";
124 VLOG(1) << " d_buf = " << data->d_buf;
125 VLOG(1) << " d_off = " << data->d_off;
126 VLOG(1) << " d_size = " << data->d_size;
127 VLOG(1) << " d_align = " << data->d_align;
128}
129
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800130// Load the complete ELF file into a memory image in libelf, and identify
131// the .rel.dyn or .rela.dyn, .dynamic, and .android.rel.dyn or
132// .android.rela.dyn sections. No-op if the ELF file has already been loaded.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800133template <typename ELF>
134bool ElfFile<ELF>::Load() {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800135 if (elf_)
136 return true;
137
138 Elf* elf = elf_begin(fd_, ELF_C_RDWR, NULL);
139 CHECK(elf);
140
141 if (elf_kind(elf) != ELF_K_ELF) {
142 LOG(ERROR) << "File not in ELF format";
143 return false;
144 }
145
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800146 auto elf_header = ELF::getehdr(elf);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800147 if (!elf_header) {
148 LOG(ERROR) << "Failed to load ELF header: " << elf_errmsg(elf_errno());
149 return false;
150 }
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800151
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800152 if (elf_header->e_type != ET_DYN) {
153 LOG(ERROR) << "ELF file is not a shared object";
154 return false;
155 }
156
157 // Require that our endianness matches that of the target, and that both
158 // are little-endian. Safe for all current build/target combinations.
159 const int endian = elf_header->e_ident[EI_DATA];
160 CHECK(endian == ELFDATA2LSB);
161 CHECK(__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__);
162
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800163 const int file_class = elf_header->e_ident[EI_CLASS];
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800164 VLOG(1) << "endian = " << endian << ", file class = " << file_class;
165 VerboseLogElfHeader(elf_header);
166
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800167 auto elf_program_header = ELF::getphdr(elf);
168 CHECK(elf_program_header != nullptr);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800169
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800170 const typename ELF::Phdr* dynamic_program_header = NULL;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800171 for (size_t i = 0; i < elf_header->e_phnum; ++i) {
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800172 auto program_header = &elf_program_header[i];
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800173 VerboseLogProgramHeader(i, program_header);
174
175 if (program_header->p_type == PT_DYNAMIC) {
176 CHECK(dynamic_program_header == NULL);
177 dynamic_program_header = program_header;
178 }
179 }
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800180 CHECK(dynamic_program_header != nullptr);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800181
182 size_t string_index;
183 elf_getshdrstrndx(elf, &string_index);
184
185 // Notes of the dynamic relocations, packed relocations, and .dynamic
186 // sections. Found while iterating sections, and later stored in class
187 // attributes.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800188 Elf_Scn* found_relocations_section = nullptr;
189 Elf_Scn* found_dynamic_section = nullptr;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800190
191 // Notes of relocation section types seen. We require one or the other of
192 // these; both is unsupported.
193 bool has_rel_relocations = false;
194 bool has_rela_relocations = false;
Dmitriy Ivanovf15ceeb2015-04-21 15:03:04 -0700195 bool has_android_relocations = false;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800196
197 Elf_Scn* section = NULL;
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800198 while ((section = elf_nextscn(elf, section)) != nullptr) {
199 auto section_header = ELF::getshdr(section);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800200 std::string name = elf_strptr(elf, string_index, section_header->sh_name);
201 VerboseLogSectionHeader(name, section_header);
202
203 // Note relocation section types.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800204 if (section_header->sh_type == SHT_REL || section_header->sh_type == SHT_ANDROID_REL) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800205 has_rel_relocations = true;
206 }
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800207 if (section_header->sh_type == SHT_RELA || section_header->sh_type == SHT_ANDROID_RELA) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800208 has_rela_relocations = true;
209 }
210
211 // Note special sections as we encounter them.
212 if ((name == ".rel.dyn" || name == ".rela.dyn") &&
213 section_header->sh_size > 0) {
214 found_relocations_section = section;
Dmitriy Ivanovf15ceeb2015-04-21 15:03:04 -0700215
216 // Note if relocation section is already packed
217 has_android_relocations =
218 section_header->sh_type == SHT_ANDROID_REL ||
219 section_header->sh_type == SHT_ANDROID_RELA;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800220 }
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800221
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800222 if (section_header->sh_offset == dynamic_program_header->p_offset) {
223 found_dynamic_section = section;
224 }
225
226 // Ensure we preserve alignment, repeated later for the data block(s).
227 CHECK(section_header->sh_addralign <= kPreserveAlignment);
228
229 Elf_Data* data = NULL;
230 while ((data = elf_getdata(section, data)) != NULL) {
231 CHECK(data->d_align <= kPreserveAlignment);
232 VerboseLogSectionData(data);
233 }
234 }
235
236 // Loading failed if we did not find the required special sections.
237 if (!found_relocations_section) {
238 LOG(ERROR) << "Missing or empty .rel.dyn or .rela.dyn section";
239 return false;
240 }
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800241 if (!found_dynamic_section) {
242 LOG(ERROR) << "Missing .dynamic section";
243 return false;
244 }
245
246 // Loading failed if we could not identify the relocations type.
247 if (!has_rel_relocations && !has_rela_relocations) {
248 LOG(ERROR) << "No relocations sections found";
249 return false;
250 }
251 if (has_rel_relocations && has_rela_relocations) {
252 LOG(ERROR) << "Multiple relocations sections with different types found, "
253 << "not currently supported";
254 return false;
255 }
256
257 elf_ = elf;
258 relocations_section_ = found_relocations_section;
259 dynamic_section_ = found_dynamic_section;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800260 relocations_type_ = has_rel_relocations ? REL : RELA;
Dmitriy Ivanovf15ceeb2015-04-21 15:03:04 -0700261 has_android_relocations_ = has_android_relocations;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800262 return true;
263}
264
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800265// Helper for ResizeSection(). Adjust the main ELF header for the hole.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800266template <typename ELF>
267static void AdjustElfHeaderForHole(typename ELF::Ehdr* elf_header,
268 typename ELF::Off hole_start,
269 ssize_t hole_size) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800270 if (elf_header->e_phoff > hole_start) {
271 elf_header->e_phoff += hole_size;
272 VLOG(1) << "e_phoff adjusted to " << elf_header->e_phoff;
273 }
274 if (elf_header->e_shoff > hole_start) {
275 elf_header->e_shoff += hole_size;
276 VLOG(1) << "e_shoff adjusted to " << elf_header->e_shoff;
277 }
278}
279
280// Helper for ResizeSection(). Adjust all section headers for the hole.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800281template <typename ELF>
282static void AdjustSectionHeadersForHole(Elf* elf,
283 typename ELF::Off hole_start,
284 ssize_t hole_size) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800285 size_t string_index;
286 elf_getshdrstrndx(elf, &string_index);
287
288 Elf_Scn* section = NULL;
289 while ((section = elf_nextscn(elf, section)) != NULL) {
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800290 auto section_header = ELF::getshdr(section);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800291 std::string name = elf_strptr(elf, string_index, section_header->sh_name);
292
293 if (section_header->sh_offset > hole_start) {
294 section_header->sh_offset += hole_size;
295 VLOG(1) << "section " << name
296 << " sh_offset adjusted to " << section_header->sh_offset;
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800297 } else {
298 section_header->sh_addr -= hole_size;
299 VLOG(1) << "section " << name
300 << " sh_addr adjusted to " << section_header->sh_addr;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800301 }
302 }
303}
304
305// Helper for ResizeSection(). Adjust the offsets of any program headers
306// that have offsets currently beyond the hole start.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800307template <typename ELF>
308static void AdjustProgramHeaderOffsets(typename ELF::Phdr* program_headers,
309 size_t count,
310 typename ELF::Off hole_start,
311 ssize_t hole_size) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800312 for (size_t i = 0; i < count; ++i) {
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800313 typename ELF::Phdr* program_header = &program_headers[i];
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800314
315 if (program_header->p_offset > hole_start) {
316 // The hole start is past this segment, so adjust offset.
317 program_header->p_offset += hole_size;
318 VLOG(1) << "phdr[" << i
319 << "] p_offset adjusted to "<< program_header->p_offset;
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800320 } else {
321 program_header->p_vaddr -= hole_size;
322 program_header->p_paddr -= hole_size;
Dmitriy Ivanovb2939692015-04-27 18:53:27 -0700323 if (program_header->p_align > kPageSize) {
324 program_header->p_align = kPageSize;
325 }
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800326 VLOG(1) << "phdr[" << i
327 << "] p_vaddr adjusted to "<< program_header->p_vaddr
Dmitriy Ivanovb2939692015-04-27 18:53:27 -0700328 << "; p_paddr adjusted to "<< program_header->p_paddr
329 << "; p_align adjusted to "<< program_header->p_align;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800330 }
331 }
332}
333
334// Helper for ResizeSection(). Find the first loadable segment in the
335// file. We expect it to map from file offset zero.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800336template <typename ELF>
337static typename ELF::Phdr* FindLoadSegmentForHole(typename ELF::Phdr* program_headers,
338 size_t count,
339 typename ELF::Off hole_start) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800340 for (size_t i = 0; i < count; ++i) {
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800341 typename ELF::Phdr* program_header = &program_headers[i];
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800342
343 if (program_header->p_type == PT_LOAD &&
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800344 program_header->p_offset <= hole_start &&
345 (program_header->p_offset + program_header->p_filesz) >= hole_start ) {
346 return program_header;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800347 }
348 }
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800349 LOG(FATAL) << "Cannot locate a LOAD segment with hole_start=0x" << std::hex << hole_start;
350 NOTREACHED();
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800351
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800352 return nullptr;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800353}
354
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800355// Helper for ResizeSection(). Rewrite program headers.
356template <typename ELF>
357static void RewriteProgramHeadersForHole(Elf* elf,
358 typename ELF::Off hole_start,
359 ssize_t hole_size) {
360 const typename ELF::Ehdr* elf_header = ELF::getehdr(elf);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800361 CHECK(elf_header);
362
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800363 typename ELF::Phdr* elf_program_header = ELF::getphdr(elf);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800364 CHECK(elf_program_header);
365
366 const size_t program_header_count = elf_header->e_phnum;
367
368 // Locate the segment that we can overwrite to form the new LOAD entry,
369 // and the segment that we are going to split into two parts.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800370 typename ELF::Phdr* target_load_header =
371 FindLoadSegmentForHole<ELF>(elf_program_header, program_header_count, hole_start);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800372
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800373 VLOG(1) << "phdr[" << target_load_header - elf_program_header << "] adjust";
374 // Adjust PT_LOAD program header memsz and filesz
375 target_load_header->p_filesz += hole_size;
376 target_load_header->p_memsz += hole_size;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800377
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800378 // Adjust the offsets and p_vaddrs
379 AdjustProgramHeaderOffsets<ELF>(elf_program_header,
380 program_header_count,
381 hole_start,
382 hole_size);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800383}
384
385// Helper for ResizeSection(). Locate and return the dynamic section.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800386template <typename ELF>
387static Elf_Scn* GetDynamicSection(Elf* elf) {
388 const typename ELF::Ehdr* elf_header = ELF::getehdr(elf);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800389 CHECK(elf_header);
390
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800391 const typename ELF::Phdr* elf_program_header = ELF::getphdr(elf);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800392 CHECK(elf_program_header);
393
394 // Find the program header that describes the dynamic section.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800395 const typename ELF::Phdr* dynamic_program_header = NULL;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800396 for (size_t i = 0; i < elf_header->e_phnum; ++i) {
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800397 const typename ELF::Phdr* program_header = &elf_program_header[i];
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800398
399 if (program_header->p_type == PT_DYNAMIC) {
400 dynamic_program_header = program_header;
401 }
402 }
403 CHECK(dynamic_program_header);
404
405 // Now find the section with the same offset as this program header.
406 Elf_Scn* dynamic_section = NULL;
407 Elf_Scn* section = NULL;
408 while ((section = elf_nextscn(elf, section)) != NULL) {
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800409 typename ELF::Shdr* section_header = ELF::getshdr(section);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800410
411 if (section_header->sh_offset == dynamic_program_header->p_offset) {
412 dynamic_section = section;
413 }
414 }
415 CHECK(dynamic_section != NULL);
416
417 return dynamic_section;
418}
419
420// Helper for ResizeSection(). Adjust the .dynamic section for the hole.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800421template <typename ELF>
422void ElfFile<ELF>::AdjustDynamicSectionForHole(Elf_Scn* dynamic_section,
423 typename ELF::Off hole_start,
424 ssize_t hole_size,
425 relocations_type_t relocations_type) {
426 CHECK(relocations_type != NONE);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800427 Elf_Data* data = GetSectionData(dynamic_section);
428
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800429 auto dynamic_base = reinterpret_cast<typename ELF::Dyn*>(data->d_buf);
430 std::vector<typename ELF::Dyn> dynamics(
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800431 dynamic_base,
432 dynamic_base + data->d_size / sizeof(dynamics[0]));
433
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800434 if (hole_size > 0) { // expanding
435 hole_start += hole_size;
436 }
437
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800438 for (size_t i = 0; i < dynamics.size(); ++i) {
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800439 typename ELF::Dyn* dynamic = &dynamics[i];
440 const typename ELF::Sword tag = dynamic->d_tag;
441
442 // Any tags that hold offsets are adjustment candidates.
443 const bool is_adjustable = (tag == DT_PLTGOT ||
444 tag == DT_HASH ||
445 tag == DT_GNU_HASH ||
446 tag == DT_STRTAB ||
447 tag == DT_SYMTAB ||
448 tag == DT_RELA ||
449 tag == DT_INIT ||
450 tag == DT_FINI ||
451 tag == DT_REL ||
452 tag == DT_JMPREL ||
453 tag == DT_INIT_ARRAY ||
454 tag == DT_FINI_ARRAY ||
Dmitriy Ivanovbb25bbe2015-04-20 17:41:28 -0700455 tag == DT_VERSYM ||
456 tag == DT_VERNEED ||
457 tag == DT_VERDEF ||
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800458 tag == DT_ANDROID_REL||
459 tag == DT_ANDROID_RELA);
460
461 if (is_adjustable && dynamic->d_un.d_ptr <= hole_start) {
462 dynamic->d_un.d_ptr -= hole_size;
463 VLOG(1) << "dynamic[" << i << "] " << dynamic->d_tag
464 << " d_ptr adjusted to " << dynamic->d_un.d_ptr;
465 }
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800466
467 // DT_RELSZ or DT_RELASZ indicate the overall size of relocations.
468 // Only one will be present. Adjust by hole size.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800469 if (tag == DT_RELSZ || tag == DT_RELASZ || tag == DT_ANDROID_RELSZ || tag == DT_ANDROID_RELASZ) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800470 dynamic->d_un.d_val += hole_size;
471 VLOG(1) << "dynamic[" << i << "] " << dynamic->d_tag
472 << " d_val adjusted to " << dynamic->d_un.d_val;
473 }
474
Dmitriy Ivanov18c935c2015-04-29 19:34:51 -0700475 // Special case: DT_MIPS_RLD_MAP2 stores the difference between dynamic
476 // entry address and the address of the _r_debug (used by GDB)
477 // since the dynamic section and target address are on the
478 // different sides of the hole it needs to be adjusted accordingly
479 if (tag == DT_MIPS_RLD_MAP2) {
480 dynamic->d_un.d_val += hole_size;
481 VLOG(1) << "dynamic[" << i << "] " << dynamic->d_tag
482 << " d_val adjusted to " << dynamic->d_un.d_val;
483 }
484
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800485 // Ignore DT_RELCOUNT and DT_RELACOUNT: (1) nobody uses them and
486 // technically (2) the relative relocation count is not changed.
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800487
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800488 // DT_RELENT and DT_RELAENT don't change, ignore them as well.
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800489 }
490
491 void* section_data = &dynamics[0];
492 size_t bytes = dynamics.size() * sizeof(dynamics[0]);
493 RewriteSectionData(dynamic_section, section_data, bytes);
494}
495
496// Resize a section. If the new size is larger than the current size, open
497// up a hole by increasing file offsets that come after the hole. If smaller
498// than the current size, remove the hole by decreasing those offsets.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800499template <typename ELF>
500void ElfFile<ELF>::ResizeSection(Elf* elf, Elf_Scn* section, size_t new_size,
501 typename ELF::Word new_sh_type,
502 relocations_type_t relocations_type) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800503
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800504 size_t string_index;
505 elf_getshdrstrndx(elf, &string_index);
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800506 auto section_header = ELF::getshdr(section);
507 std::string name = elf_strptr(elf, string_index, section_header->sh_name);
508
509 if (section_header->sh_size == new_size) {
510 return;
511 }
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800512
513 // Require that the section size and the data size are the same. True
514 // in practice for all sections we resize when packing or unpacking.
515 Elf_Data* data = GetSectionData(section);
516 CHECK(data->d_off == 0 && data->d_size == section_header->sh_size);
517
518 // Require that the section is not zero-length (that is, has allocated
519 // data that we can validly expand).
520 CHECK(data->d_size && data->d_buf);
521
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800522 const auto hole_start = section_header->sh_offset;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800523 const ssize_t hole_size = new_size - data->d_size;
524
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800525 VLOG_IF(1, (hole_size > 0)) << "expand section (" << name << ") size: " <<
526 data->d_size << " -> " << (data->d_size + hole_size);
527 VLOG_IF(1, (hole_size < 0)) << "shrink section (" << name << ") size: " <<
528 data->d_size << " -> " << (data->d_size + hole_size);
529
530 // libelf overrides sh_entsize for known sh_types, so it does not matter what we set
531 // for SHT_REL/SHT_RELA.
532 typename ELF::Xword new_entsize =
533 (new_sh_type == SHT_ANDROID_REL || new_sh_type == SHT_ANDROID_RELA) ? 1 : 0;
534
535 VLOG(1) << "Update section (" << name << ") entry size: " <<
536 section_header->sh_entsize << " -> " << new_entsize;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800537
538 // Resize the data and the section header.
539 data->d_size += hole_size;
540 section_header->sh_size += hole_size;
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800541 section_header->sh_entsize = new_entsize;
542 section_header->sh_type = new_sh_type;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800543
544 // Add the hole size to all offsets in the ELF file that are after the
545 // start of the hole. If the hole size is positive we are expanding the
546 // section to create a new hole; if negative, we are closing up a hole.
547
548 // Start with the main ELF header.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800549 typename ELF::Ehdr* elf_header = ELF::getehdr(elf);
550 AdjustElfHeaderForHole<ELF>(elf_header, hole_start, hole_size);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800551
552 // Adjust all section headers.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800553 AdjustSectionHeadersForHole<ELF>(elf, hole_start, hole_size);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800554
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800555 // Rewrite the program headers to either split or coalesce segments,
556 // and adjust dynamic entries to match.
557 RewriteProgramHeadersForHole<ELF>(elf, hole_start, hole_size);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800558
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800559 Elf_Scn* dynamic_section = GetDynamicSection<ELF>(elf);
560 AdjustDynamicSectionForHole(dynamic_section, hole_start, hole_size, relocations_type);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800561}
562
563// Find the first slot in a dynamics array with the given tag. The array
564// always ends with a free (unused) element, and which we exclude from the
565// search. Returns dynamics->size() if not found.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800566template <typename ELF>
567static size_t FindDynamicEntry(typename ELF::Sword tag,
568 std::vector<typename ELF::Dyn>* dynamics) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800569 // Loop until the penultimate entry. We exclude the end sentinel.
570 for (size_t i = 0; i < dynamics->size() - 1; ++i) {
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800571 if (dynamics->at(i).d_tag == tag) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800572 return i;
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800573 }
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800574 }
575
576 // The tag was not found.
577 return dynamics->size();
578}
579
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800580// Replace dynamic entry.
581template <typename ELF>
582static void ReplaceDynamicEntry(typename ELF::Sword tag,
583 const typename ELF::Dyn& dyn,
584 std::vector<typename ELF::Dyn>* dynamics) {
585 const size_t slot = FindDynamicEntry<ELF>(tag, dynamics);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800586 if (slot == dynamics->size()) {
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800587 LOG(FATAL) << "Dynamic slot is not found for tag=" << tag;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800588 }
589
590 // Replace this entry with the one supplied.
591 dynamics->at(slot) = dyn;
592 VLOG(1) << "dynamic[" << slot << "] overwritten with " << dyn.d_tag;
593}
594
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800595// Remove relative entries from dynamic relocations and write as packed
596// data into android packed relocations.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800597template <typename ELF>
598bool ElfFile<ELF>::PackRelocations() {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800599 // Load the ELF file into libelf.
600 if (!Load()) {
601 LOG(ERROR) << "Failed to load as ELF";
602 return false;
603 }
604
605 // Retrieve the current dynamic relocations section data.
606 Elf_Data* data = GetSectionData(relocations_section_);
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800607 // we always pack rela, because packed format is pretty much the same
608 std::vector<typename ELF::Rela> relocations;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800609
610 if (relocations_type_ == REL) {
611 // Convert data to a vector of relocations.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800612 const typename ELF::Rel* relocations_base = reinterpret_cast<typename ELF::Rel*>(data->d_buf);
613 ConvertRelArrayToRelaVector(relocations_base,
614 data->d_size / sizeof(typename ELF::Rel), &relocations);
Dmitriy Ivanovbb25bbe2015-04-20 17:41:28 -0700615 VLOG(1) << "Relocations : REL";
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800616 } else if (relocations_type_ == RELA) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800617 // Convert data to a vector of relocations with addends.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800618 const typename ELF::Rela* relocations_base = reinterpret_cast<typename ELF::Rela*>(data->d_buf);
619 relocations = std::vector<typename ELF::Rela>(
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800620 relocations_base,
621 relocations_base + data->d_size / sizeof(relocations[0]));
622
Dmitriy Ivanovbb25bbe2015-04-20 17:41:28 -0700623 VLOG(1) << "Relocations : RELA";
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800624 } else {
625 NOTREACHED();
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800626 }
627
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800628 return PackTypedRelocations(&relocations);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800629}
630
631// Helper for PackRelocations(). Rel type is one of ELF::Rel or ELF::Rela.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800632template <typename ELF>
633bool ElfFile<ELF>::PackTypedRelocations(std::vector<typename ELF::Rela>* relocations) {
634 typedef typename ELF::Rela Rela;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800635
Dmitriy Ivanovf15ceeb2015-04-21 15:03:04 -0700636 if (has_android_relocations_) {
Dmitriy Ivanovb0b93382015-04-24 12:39:14 -0700637 LOG(INFO) << "Relocation table is already packed";
638 return true;
Dmitriy Ivanovf15ceeb2015-04-21 15:03:04 -0700639 }
640
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800641 // If no relocations then we have nothing packable. Perhaps
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800642 // the shared object has already been packed?
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800643 if (relocations->empty()) {
Dmitriy Ivanovf15ceeb2015-04-21 15:03:04 -0700644 LOG(ERROR) << "No relocations found";
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800645 return false;
646 }
647
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800648 const size_t rel_size =
649 relocations_type_ == RELA ? sizeof(typename ELF::Rela) : sizeof(typename ELF::Rel);
650 const size_t initial_bytes = relocations->size() * rel_size;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800651
Dmitriy Ivanovbb25bbe2015-04-20 17:41:28 -0700652 VLOG(1) << "Unpacked : " << initial_bytes << " bytes";
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800653 std::vector<uint8_t> packed;
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800654 RelocationPacker<ELF> packer;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800655
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800656 // Pack relocations: dry run to estimate memory savings.
657 packer.PackRelocations(*relocations, &packed);
658 const size_t packed_bytes_estimate = packed.size() * sizeof(packed[0]);
Dmitriy Ivanovbb25bbe2015-04-20 17:41:28 -0700659 VLOG(1) << "Packed (no padding): " << packed_bytes_estimate << " bytes";
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800660
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800661 if (packed.empty()) {
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800662 LOG(INFO) << "Too few relocations to pack";
Dmitriy Ivanovbb25bbe2015-04-20 17:41:28 -0700663 return true;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800664 }
665
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800666 // Pre-calculate the size of the hole we will close up when we rewrite
667 // dynamic relocations. We have to adjust relocation addresses to
668 // account for this.
669 typename ELF::Shdr* section_header = ELF::getshdr(relocations_section_);
670 ssize_t hole_size = initial_bytes - packed_bytes_estimate;
671
672 // hole_size needs to be page_aligned.
673 hole_size -= hole_size % kPreserveAlignment;
674
675 LOG(INFO) << "Compaction : " << hole_size << " bytes";
676
677 // Adjusting for alignment may have removed any packing benefit.
678 if (hole_size == 0) {
679 LOG(INFO) << "Too few relocations to pack after alignment";
Dmitriy Ivanovbb25bbe2015-04-20 17:41:28 -0700680 return true;
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800681 }
682
683 if (hole_size <= 0) {
684 LOG(INFO) << "Packing relocations saves no space";
Dmitriy Ivanovadfcb972015-04-23 13:47:39 -0700685 return true;
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800686 }
687
688 size_t data_padding_bytes = is_padding_relocations_ ?
689 initial_bytes - packed_bytes_estimate :
690 initial_bytes - hole_size - packed_bytes_estimate;
691
692 // pad data
693 std::vector<uint8_t> padding(data_padding_bytes, 0);
694 packed.insert(packed.end(), padding.begin(), padding.end());
695
696 const void* packed_data = &packed[0];
697
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800698 // Run a loopback self-test as a check that packing is lossless.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800699 std::vector<Rela> unpacked;
700 packer.UnpackRelocations(packed, &unpacked);
701 CHECK(unpacked.size() == relocations->size());
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800702 CHECK(!memcmp(&unpacked[0],
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800703 &relocations->at(0),
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800704 unpacked.size() * sizeof(unpacked[0])));
705
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800706 // Rewrite the current dynamic relocations section with packed one then shrink it to size.
707 const size_t bytes = packed.size() * sizeof(packed[0]);
708 ResizeSection(elf_, relocations_section_, bytes,
709 relocations_type_ == REL ? SHT_ANDROID_REL : SHT_ANDROID_RELA, relocations_type_);
710 RewriteSectionData(relocations_section_, packed_data, bytes);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800711
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800712 // TODO (dimitry): fix string table and replace .rel.dyn/plt with .android.rel.dyn/plt
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800713
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800714 // Rewrite .dynamic and rename relocation tags describing the packed android
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800715 // relocations.
716 Elf_Data* data = GetSectionData(dynamic_section_);
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800717 const typename ELF::Dyn* dynamic_base = reinterpret_cast<typename ELF::Dyn*>(data->d_buf);
718 std::vector<typename ELF::Dyn> dynamics(
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800719 dynamic_base,
720 dynamic_base + data->d_size / sizeof(dynamics[0]));
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800721 section_header = ELF::getshdr(relocations_section_);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800722 {
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800723 typename ELF::Dyn dyn;
724 dyn.d_tag = relocations_type_ == REL ? DT_ANDROID_REL : DT_ANDROID_RELA;
725 dyn.d_un.d_ptr = section_header->sh_addr;
726 ReplaceDynamicEntry<ELF>(relocations_type_ == REL ? DT_REL : DT_RELA, dyn, &dynamics);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800727 }
728 {
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800729 typename ELF::Dyn dyn;
730 dyn.d_tag = relocations_type_ == REL ? DT_ANDROID_RELSZ : DT_ANDROID_RELASZ;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800731 dyn.d_un.d_val = section_header->sh_size;
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800732 ReplaceDynamicEntry<ELF>(relocations_type_ == REL ? DT_RELSZ : DT_RELASZ, dyn, &dynamics);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800733 }
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800734
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800735 const void* dynamics_data = &dynamics[0];
736 const size_t dynamics_bytes = dynamics.size() * sizeof(dynamics[0]);
737 RewriteSectionData(dynamic_section_, dynamics_data, dynamics_bytes);
738
739 Flush();
740 return true;
741}
742
743// Find packed relative relocations in the packed android relocations
744// section, unpack them, and rewrite the dynamic relocations section to
745// contain unpacked data.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800746template <typename ELF>
747bool ElfFile<ELF>::UnpackRelocations() {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800748 // Load the ELF file into libelf.
749 if (!Load()) {
750 LOG(ERROR) << "Failed to load as ELF";
751 return false;
752 }
753
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800754 typename ELF::Shdr* section_header = ELF::getshdr(relocations_section_);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800755 // Retrieve the current packed android relocations section data.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800756 Elf_Data* data = GetSectionData(relocations_section_);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800757
758 // Convert data to a vector of bytes.
759 const uint8_t* packed_base = reinterpret_cast<uint8_t*>(data->d_buf);
760 std::vector<uint8_t> packed(
761 packed_base,
762 packed_base + data->d_size / sizeof(packed[0]));
763
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800764 if ((section_header->sh_type == SHT_ANDROID_RELA || section_header->sh_type == SHT_ANDROID_REL) &&
765 packed.size() > 3 &&
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800766 packed[0] == 'A' &&
767 packed[1] == 'P' &&
Dmitriy Ivanovf15ceeb2015-04-21 15:03:04 -0700768 packed[2] == 'S' &&
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800769 packed[3] == '2') {
770 LOG(INFO) << "Relocations : " << (relocations_type_ == REL ? "REL" : "RELA");
771 } else {
772 LOG(ERROR) << "Packed relocations not found (not packed?)";
773 return false;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800774 }
775
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800776 return UnpackTypedRelocations(packed);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800777}
778
779// Helper for UnpackRelocations(). Rel type is one of ELF::Rel or ELF::Rela.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800780template <typename ELF>
781bool ElfFile<ELF>::UnpackTypedRelocations(const std::vector<uint8_t>& packed) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800782 // Unpack the data to re-materialize the relative relocations.
783 const size_t packed_bytes = packed.size() * sizeof(packed[0]);
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800784 LOG(INFO) << "Packed : " << packed_bytes << " bytes";
785 std::vector<typename ELF::Rela> unpacked_relocations;
786 RelocationPacker<ELF> packer;
787 packer.UnpackRelocations(packed, &unpacked_relocations);
788
789 const size_t relocation_entry_size =
790 relocations_type_ == REL ? sizeof(typename ELF::Rel) : sizeof(typename ELF::Rela);
791 const size_t unpacked_bytes = unpacked_relocations.size() * relocation_entry_size;
792 LOG(INFO) << "Unpacked : " << unpacked_bytes << " bytes";
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800793
794 // Retrieve the current dynamic relocations section data.
795 Elf_Data* data = GetSectionData(relocations_section_);
796
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800797 LOG(INFO) << "Relocations : " << unpacked_relocations.size() << " entries";
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800798
799 // If we found the same number of null relocation entries in the dynamic
800 // relocations section as we hold as unpacked relative relocations, then
801 // this is a padded file.
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800802
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800803 const bool is_padded = packed_bytes == unpacked_bytes;
804
805 // Unless padded, pre-apply relative relocations to account for the
806 // hole, and pre-adjust all relocation offsets accordingly.
807 typename ELF::Shdr* section_header = ELF::getshdr(relocations_section_);
808
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800809 if (!is_padded) {
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800810 LOG(INFO) << "Expansion : " << unpacked_bytes - packed_bytes << " bytes";
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800811 }
812
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800813 // Rewrite the current dynamic relocations section with unpacked version of
814 // relocations.
815 const void* section_data = nullptr;
816 std::vector<typename ELF::Rel> unpacked_rel_relocations;
817 if (relocations_type_ == RELA) {
818 section_data = &unpacked_relocations[0];
819 } else if (relocations_type_ == REL) {
820 ConvertRelaVectorToRelVector(unpacked_relocations, &unpacked_rel_relocations);
821 section_data = &unpacked_rel_relocations[0];
822 } else {
823 NOTREACHED();
824 }
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800825
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800826 ResizeSection(elf_, relocations_section_, unpacked_bytes,
827 relocations_type_ == REL ? SHT_REL : SHT_RELA, relocations_type_);
828 RewriteSectionData(relocations_section_, section_data, unpacked_bytes);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800829
830 // Rewrite .dynamic to remove two tags describing packed android relocations.
831 data = GetSectionData(dynamic_section_);
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800832 const typename ELF::Dyn* dynamic_base = reinterpret_cast<typename ELF::Dyn*>(data->d_buf);
833 std::vector<typename ELF::Dyn> dynamics(
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800834 dynamic_base,
835 dynamic_base + data->d_size / sizeof(dynamics[0]));
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800836 {
837 typename ELF::Dyn dyn;
838 dyn.d_tag = relocations_type_ == REL ? DT_REL : DT_RELA;
839 dyn.d_un.d_ptr = section_header->sh_addr;
840 ReplaceDynamicEntry<ELF>(relocations_type_ == REL ? DT_ANDROID_REL : DT_ANDROID_RELA,
841 dyn, &dynamics);
842 }
843
844 {
845 typename ELF::Dyn dyn;
846 dyn.d_tag = relocations_type_ == REL ? DT_RELSZ : DT_RELASZ;
847 dyn.d_un.d_val = section_header->sh_size;
848 ReplaceDynamicEntry<ELF>(relocations_type_ == REL ? DT_ANDROID_RELSZ : DT_ANDROID_RELASZ,
849 dyn, &dynamics);
850 }
851
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800852 const void* dynamics_data = &dynamics[0];
853 const size_t dynamics_bytes = dynamics.size() * sizeof(dynamics[0]);
854 RewriteSectionData(dynamic_section_, dynamics_data, dynamics_bytes);
855
856 Flush();
857 return true;
858}
859
860// Flush rewritten shared object file data.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800861template <typename ELF>
862void ElfFile<ELF>::Flush() {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800863 // Flag all ELF data held in memory as needing to be written back to the
864 // file, and tell libelf that we have controlled the file layout.
865 elf_flagelf(elf_, ELF_C_SET, ELF_F_DIRTY);
866 elf_flagelf(elf_, ELF_C_SET, ELF_F_LAYOUT);
867
868 // Write ELF data back to disk.
869 const off_t file_bytes = elf_update(elf_, ELF_C_WRITE);
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800870 if (file_bytes == -1) {
871 LOG(ERROR) << "elf_update failed: " << elf_errmsg(elf_errno());
872 }
873
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800874 CHECK(file_bytes > 0);
875 VLOG(1) << "elf_update returned: " << file_bytes;
876
877 // Clean up libelf, and truncate the output file to the number of bytes
878 // written by elf_update().
879 elf_end(elf_);
880 elf_ = NULL;
881 const int truncate = ftruncate(fd_, file_bytes);
882 CHECK(truncate == 0);
883}
884
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800885template <typename ELF>
886void ElfFile<ELF>::ConvertRelArrayToRelaVector(const typename ELF::Rel* rel_array,
887 size_t rel_array_size,
888 std::vector<typename ELF::Rela>* rela_vector) {
889 for (size_t i = 0; i<rel_array_size; ++i) {
890 typename ELF::Rela rela;
891 rela.r_offset = rel_array[i].r_offset;
892 rela.r_info = rel_array[i].r_info;
893 rela.r_addend = 0;
894 rela_vector->push_back(rela);
895 }
896}
897
898template <typename ELF>
899void ElfFile<ELF>::ConvertRelaVectorToRelVector(const std::vector<typename ELF::Rela>& rela_vector,
900 std::vector<typename ELF::Rel>* rel_vector) {
901 for (auto rela : rela_vector) {
902 typename ELF::Rel rel;
903 rel.r_offset = rela.r_offset;
904 rel.r_info = rela.r_info;
905 CHECK(rela.r_addend == 0);
906 rel_vector->push_back(rel);
907 }
908}
909
910template class ElfFile<ELF32_traits>;
911template class ElfFile<ELF64_traits>;
912
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800913} // namespace relocation_packer