blob: 6843f5bae4e25fc7d5998d7470c417398f9ced56 [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
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 Ivanovf8ff6b12015-01-27 19:32:56 -080044static constexpr size_t kPreserveAlignment = 4096;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080045
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 Ivanovf8ff6b12015-01-27 19:32:56 -080051static Elf_Data* GetSectionData(Elf_Scn* section) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080052 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 Ivanovf8ff6b12015-01-27 19:32:56 -080059static void RewriteSectionData(Elf_Scn* section,
60 const void* section_data,
61 size_t size) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080062 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 Ivanovf8ff6b12015-01-27 19:32:56 -080070template <typename Ehdr>
71static void VerboseLogElfHeader(const Ehdr* elf_header) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080072 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 Ivanovf8ff6b12015-01-27 19:32:56 -080082template <typename Phdr>
83static void VerboseLogProgramHeader(size_t program_header_index,
84 const Phdr* program_header) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080085 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 Ivanovf8ff6b12015-01-27 19:32:56 -0800108template <typename Shdr>
109static void VerboseLogSectionHeader(const std::string& section_name,
110 const Shdr* section_header) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800111 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 Ivanovf8ff6b12015-01-27 19:32:56 -0800115 VLOG(1) << " sh_entsize = " << section_header->sh_entsize;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800116 VLOG(1) << " sh_addralign = " << section_header->sh_addralign;
117}
118
119// Verbose ELF section data logging.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800120static void VerboseLogSectionData(const Elf_Data* data) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800121 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 Ivanov87a06172015-02-06 10:56:28 -0800128// 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 Ivanovf8ff6b12015-01-27 19:32:56 -0800131template <typename ELF>
132bool ElfFile<ELF>::Load() {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800133 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 Ivanovf8ff6b12015-01-27 19:32:56 -0800144 auto elf_header = ELF::getehdr(elf);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800145 if (!elf_header) {
146 LOG(ERROR) << "Failed to load ELF header: " << elf_errmsg(elf_errno());
147 return false;
148 }
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800149
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800150 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 Ivanov87a06172015-02-06 10:56:28 -0800161 const int file_class = elf_header->e_ident[EI_CLASS];
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800162 VLOG(1) << "endian = " << endian << ", file class = " << file_class;
163 VerboseLogElfHeader(elf_header);
164
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800165 auto elf_program_header = ELF::getphdr(elf);
166 CHECK(elf_program_header != nullptr);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800167
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800168 const typename ELF::Phdr* dynamic_program_header = NULL;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800169 for (size_t i = 0; i < elf_header->e_phnum; ++i) {
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800170 auto program_header = &elf_program_header[i];
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800171 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 Ivanovf8ff6b12015-01-27 19:32:56 -0800178 CHECK(dynamic_program_header != nullptr);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800179
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 Ivanovf8ff6b12015-01-27 19:32:56 -0800186 Elf_Scn* found_relocations_section = nullptr;
187 Elf_Scn* found_dynamic_section = nullptr;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800188
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 Ivanovf8ff6b12015-01-27 19:32:56 -0800195 while ((section = elf_nextscn(elf, section)) != nullptr) {
196 auto section_header = ELF::getshdr(section);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800197 std::string name = elf_strptr(elf, string_index, section_header->sh_name);
198 VerboseLogSectionHeader(name, section_header);
199
200 // Note relocation section types.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800201 if (section_header->sh_type == SHT_REL || section_header->sh_type == SHT_ANDROID_REL) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800202 has_rel_relocations = true;
203 }
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800204 if (section_header->sh_type == SHT_RELA || section_header->sh_type == SHT_ANDROID_RELA) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800205 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 Ivanovf8ff6b12015-01-27 19:32:56 -0800213
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800214 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 Ivanov87a06172015-02-06 10:56:28 -0800233 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 Ivanov87a06172015-02-06 10:56:28 -0800252 relocations_type_ = has_rel_relocations ? REL : RELA;
253 return true;
254}
255
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800256// Helper for ResizeSection(). Adjust the main ELF header for the hole.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800257template <typename ELF>
258static void AdjustElfHeaderForHole(typename ELF::Ehdr* elf_header,
259 typename ELF::Off hole_start,
260 ssize_t hole_size) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800261 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 Ivanovf8ff6b12015-01-27 19:32:56 -0800272template <typename ELF>
273static void AdjustSectionHeadersForHole(Elf* elf,
274 typename ELF::Off hole_start,
275 ssize_t hole_size) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800276 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 Ivanovf8ff6b12015-01-27 19:32:56 -0800281 auto section_header = ELF::getshdr(section);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800282 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 Ivanovf8ff6b12015-01-27 19:32:56 -0800288 } else {
289 section_header->sh_addr -= hole_size;
290 VLOG(1) << "section " << name
291 << " sh_addr adjusted to " << section_header->sh_addr;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800292 }
293 }
294}
295
296// Helper for ResizeSection(). Adjust the offsets of any program headers
297// that have offsets currently beyond the hole start.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800298template <typename ELF>
299static void AdjustProgramHeaderOffsets(typename ELF::Phdr* program_headers,
300 size_t count,
301 typename ELF::Off hole_start,
302 ssize_t hole_size) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800303 for (size_t i = 0; i < count; ++i) {
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800304 typename ELF::Phdr* program_header = &program_headers[i];
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800305
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 Ivanovf8ff6b12015-01-27 19:32:56 -0800311 } 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 Ivanov87a06172015-02-06 10:56:28 -0800317 }
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 Ivanovf8ff6b12015-01-27 19:32:56 -0800323template <typename ELF>
324static typename ELF::Phdr* FindLoadSegmentForHole(typename ELF::Phdr* program_headers,
325 size_t count,
326 typename ELF::Off hole_start) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800327 for (size_t i = 0; i < count; ++i) {
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800328 typename ELF::Phdr* program_header = &program_headers[i];
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800329
330 if (program_header->p_type == PT_LOAD &&
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800331 program_header->p_offset <= hole_start &&
332 (program_header->p_offset + program_header->p_filesz) >= hole_start ) {
333 return program_header;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800334 }
335 }
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800336 LOG(FATAL) << "Cannot locate a LOAD segment with hole_start=0x" << std::hex << hole_start;
337 NOTREACHED();
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800338
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800339 return nullptr;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800340}
341
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800342// Helper for ResizeSection(). Rewrite program headers.
343template <typename ELF>
344static 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 Ivanov87a06172015-02-06 10:56:28 -0800348 CHECK(elf_header);
349
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800350 typename ELF::Phdr* elf_program_header = ELF::getphdr(elf);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800351 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 Ivanovf8ff6b12015-01-27 19:32:56 -0800357 typename ELF::Phdr* target_load_header =
358 FindLoadSegmentForHole<ELF>(elf_program_header, program_header_count, hole_start);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800359
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800360 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 Ivanov87a06172015-02-06 10:56:28 -0800364
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800365 // Adjust the offsets and p_vaddrs
366 AdjustProgramHeaderOffsets<ELF>(elf_program_header,
367 program_header_count,
368 hole_start,
369 hole_size);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800370}
371
372// Helper for ResizeSection(). Locate and return the dynamic section.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800373template <typename ELF>
374static Elf_Scn* GetDynamicSection(Elf* elf) {
375 const typename ELF::Ehdr* elf_header = ELF::getehdr(elf);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800376 CHECK(elf_header);
377
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800378 const typename ELF::Phdr* elf_program_header = ELF::getphdr(elf);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800379 CHECK(elf_program_header);
380
381 // Find the program header that describes the dynamic section.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800382 const typename ELF::Phdr* dynamic_program_header = NULL;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800383 for (size_t i = 0; i < elf_header->e_phnum; ++i) {
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800384 const typename ELF::Phdr* program_header = &elf_program_header[i];
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800385
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 Ivanovf8ff6b12015-01-27 19:32:56 -0800396 typename ELF::Shdr* section_header = ELF::getshdr(section);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800397
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 Ivanovf8ff6b12015-01-27 19:32:56 -0800408template <typename ELF>
409void 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 Ivanov87a06172015-02-06 10:56:28 -0800414 Elf_Data* data = GetSectionData(dynamic_section);
415
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800416 auto dynamic_base = reinterpret_cast<typename ELF::Dyn*>(data->d_buf);
417 std::vector<typename ELF::Dyn> dynamics(
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800418 dynamic_base,
419 dynamic_base + data->d_size / sizeof(dynamics[0]));
420
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800421 if (hole_size > 0) { // expanding
422 hole_start += hole_size;
423 }
424
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800425 for (size_t i = 0; i < dynamics.size(); ++i) {
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800426 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 Ivanovbb25bbe2015-04-20 17:41:28 -0700442 tag == DT_VERSYM ||
443 tag == DT_VERNEED ||
444 tag == DT_VERDEF ||
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800445 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 Ivanov87a06172015-02-06 10:56:28 -0800453
454 // DT_RELSZ or DT_RELASZ indicate the overall size of relocations.
455 // Only one will be present. Adjust by hole size.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800456 if (tag == DT_RELSZ || tag == DT_RELASZ || tag == DT_ANDROID_RELSZ || tag == DT_ANDROID_RELASZ) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800457 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 Ivanovf8ff6b12015-01-27 19:32:56 -0800462 // Ignore DT_RELCOUNT and DT_RELACOUNT: (1) nobody uses them and
463 // technically (2) the relative relocation count is not changed.
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800464
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800465 // DT_RELENT and DT_RELAENT don't change, ignore them as well.
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800466 }
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 Ivanovf8ff6b12015-01-27 19:32:56 -0800476template <typename ELF>
477void 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 Ivanov87a06172015-02-06 10:56:28 -0800480
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800481 size_t string_index;
482 elf_getshdrstrndx(elf, &string_index);
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800483 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 Ivanov87a06172015-02-06 10:56:28 -0800489
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 Ivanovf8ff6b12015-01-27 19:32:56 -0800499 const auto hole_start = section_header->sh_offset;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800500 const ssize_t hole_size = new_size - data->d_size;
501
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800502 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 Ivanov87a06172015-02-06 10:56:28 -0800514
515 // Resize the data and the section header.
516 data->d_size += hole_size;
517 section_header->sh_size += hole_size;
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800518 section_header->sh_entsize = new_entsize;
519 section_header->sh_type = new_sh_type;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800520
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 Ivanovf8ff6b12015-01-27 19:32:56 -0800526 typename ELF::Ehdr* elf_header = ELF::getehdr(elf);
527 AdjustElfHeaderForHole<ELF>(elf_header, hole_start, hole_size);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800528
529 // Adjust all section headers.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800530 AdjustSectionHeadersForHole<ELF>(elf, hole_start, hole_size);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800531
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800532 // 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 Ivanov87a06172015-02-06 10:56:28 -0800535
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800536 Elf_Scn* dynamic_section = GetDynamicSection<ELF>(elf);
537 AdjustDynamicSectionForHole(dynamic_section, hole_start, hole_size, relocations_type);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800538}
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 Ivanovf8ff6b12015-01-27 19:32:56 -0800543template <typename ELF>
544static size_t FindDynamicEntry(typename ELF::Sword tag,
545 std::vector<typename ELF::Dyn>* dynamics) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800546 // Loop until the penultimate entry. We exclude the end sentinel.
547 for (size_t i = 0; i < dynamics->size() - 1; ++i) {
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800548 if (dynamics->at(i).d_tag == tag) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800549 return i;
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800550 }
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800551 }
552
553 // The tag was not found.
554 return dynamics->size();
555}
556
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800557// Replace dynamic entry.
558template <typename ELF>
559static 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 Ivanov87a06172015-02-06 10:56:28 -0800563 if (slot == dynamics->size()) {
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800564 LOG(FATAL) << "Dynamic slot is not found for tag=" << tag;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800565 }
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 Ivanov87a06172015-02-06 10:56:28 -0800572// Remove relative entries from dynamic relocations and write as packed
573// data into android packed relocations.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800574template <typename ELF>
575bool ElfFile<ELF>::PackRelocations() {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800576 // 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 Ivanovf8ff6b12015-01-27 19:32:56 -0800584 // we always pack rela, because packed format is pretty much the same
585 std::vector<typename ELF::Rela> relocations;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800586
587 if (relocations_type_ == REL) {
588 // Convert data to a vector of relocations.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800589 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 Ivanovbb25bbe2015-04-20 17:41:28 -0700592 VLOG(1) << "Relocations : REL";
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800593 } else if (relocations_type_ == RELA) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800594 // Convert data to a vector of relocations with addends.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800595 const typename ELF::Rela* relocations_base = reinterpret_cast<typename ELF::Rela*>(data->d_buf);
596 relocations = std::vector<typename ELF::Rela>(
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800597 relocations_base,
598 relocations_base + data->d_size / sizeof(relocations[0]));
599
Dmitriy Ivanovbb25bbe2015-04-20 17:41:28 -0700600 VLOG(1) << "Relocations : RELA";
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800601 } else {
602 NOTREACHED();
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800603 }
604
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800605 return PackTypedRelocations(&relocations);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800606}
607
608// Helper for PackRelocations(). Rel type is one of ELF::Rel or ELF::Rela.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800609template <typename ELF>
610bool ElfFile<ELF>::PackTypedRelocations(std::vector<typename ELF::Rela>* relocations) {
611 typedef typename ELF::Rela Rela;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800612
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800613 // If no relocations then we have nothing packable. Perhaps
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800614 // the shared object has already been packed?
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800615 if (relocations->empty()) {
616 LOG(ERROR) << "No relocations found (already packed?)";
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800617 return false;
618 }
619
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800620 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 Ivanov87a06172015-02-06 10:56:28 -0800623
Dmitriy Ivanovbb25bbe2015-04-20 17:41:28 -0700624 VLOG(1) << "Unpacked : " << initial_bytes << " bytes";
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800625 std::vector<uint8_t> packed;
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800626 RelocationPacker<ELF> packer;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800627
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800628 // 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 Ivanovbb25bbe2015-04-20 17:41:28 -0700631 VLOG(1) << "Packed (no padding): " << packed_bytes_estimate << " bytes";
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800632
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800633 if (packed.empty()) {
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800634 LOG(INFO) << "Too few relocations to pack";
Dmitriy Ivanovbb25bbe2015-04-20 17:41:28 -0700635 return true;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800636 }
637
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800638 // 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 Ivanovbb25bbe2015-04-20 17:41:28 -0700652 return true;
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800653 }
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 Ivanov87a06172015-02-06 10:56:28 -0800670 // Run a loopback self-test as a check that packing is lossless.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800671 std::vector<Rela> unpacked;
672 packer.UnpackRelocations(packed, &unpacked);
673 CHECK(unpacked.size() == relocations->size());
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800674 CHECK(!memcmp(&unpacked[0],
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800675 &relocations->at(0),
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800676 unpacked.size() * sizeof(unpacked[0])));
677
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800678 // 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 Ivanov87a06172015-02-06 10:56:28 -0800683
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800684 // TODO (dimitry): fix string table and replace .rel.dyn/plt with .android.rel.dyn/plt
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800685
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800686 // Rewrite .dynamic and rename relocation tags describing the packed android
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800687 // relocations.
688 Elf_Data* data = GetSectionData(dynamic_section_);
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800689 const typename ELF::Dyn* dynamic_base = reinterpret_cast<typename ELF::Dyn*>(data->d_buf);
690 std::vector<typename ELF::Dyn> dynamics(
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800691 dynamic_base,
692 dynamic_base + data->d_size / sizeof(dynamics[0]));
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800693 section_header = ELF::getshdr(relocations_section_);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800694 {
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800695 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 Ivanov87a06172015-02-06 10:56:28 -0800699 }
700 {
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800701 typename ELF::Dyn dyn;
702 dyn.d_tag = relocations_type_ == REL ? DT_ANDROID_RELSZ : DT_ANDROID_RELASZ;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800703 dyn.d_un.d_val = section_header->sh_size;
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800704 ReplaceDynamicEntry<ELF>(relocations_type_ == REL ? DT_RELSZ : DT_RELASZ, dyn, &dynamics);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800705 }
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800706
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800707 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 Ivanovf8ff6b12015-01-27 19:32:56 -0800718template <typename ELF>
719bool ElfFile<ELF>::UnpackRelocations() {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800720 // Load the ELF file into libelf.
721 if (!Load()) {
722 LOG(ERROR) << "Failed to load as ELF";
723 return false;
724 }
725
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800726 typename ELF::Shdr* section_header = ELF::getshdr(relocations_section_);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800727 // Retrieve the current packed android relocations section data.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800728 Elf_Data* data = GetSectionData(relocations_section_);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800729
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 Ivanovf8ff6b12015-01-27 19:32:56 -0800736 if ((section_header->sh_type == SHT_ANDROID_RELA || section_header->sh_type == SHT_ANDROID_REL) &&
737 packed.size() > 3 &&
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800738 packed[0] == 'A' &&
739 packed[1] == 'P' &&
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800740 (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 Ivanov87a06172015-02-06 10:56:28 -0800746 }
747
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800748 return UnpackTypedRelocations(packed);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800749}
750
751// Helper for UnpackRelocations(). Rel type is one of ELF::Rel or ELF::Rela.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800752template <typename ELF>
753bool ElfFile<ELF>::UnpackTypedRelocations(const std::vector<uint8_t>& packed) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800754 // Unpack the data to re-materialize the relative relocations.
755 const size_t packed_bytes = packed.size() * sizeof(packed[0]);
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800756 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 Ivanov87a06172015-02-06 10:56:28 -0800765
766 // Retrieve the current dynamic relocations section data.
767 Elf_Data* data = GetSectionData(relocations_section_);
768
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800769 LOG(INFO) << "Relocations : " << unpacked_relocations.size() << " entries";
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800770
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 Ivanov87a06172015-02-06 10:56:28 -0800774
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800775 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 Ivanov87a06172015-02-06 10:56:28 -0800781 if (!is_padded) {
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800782 LOG(INFO) << "Expansion : " << unpacked_bytes - packed_bytes << " bytes";
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800783 }
784
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800785 // 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 Ivanov87a06172015-02-06 10:56:28 -0800797
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800798 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 Ivanov87a06172015-02-06 10:56:28 -0800801
802 // Rewrite .dynamic to remove two tags describing packed android relocations.
803 data = GetSectionData(dynamic_section_);
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800804 const typename ELF::Dyn* dynamic_base = reinterpret_cast<typename ELF::Dyn*>(data->d_buf);
805 std::vector<typename ELF::Dyn> dynamics(
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800806 dynamic_base,
807 dynamic_base + data->d_size / sizeof(dynamics[0]));
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800808 {
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 Ivanov87a06172015-02-06 10:56:28 -0800824 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 Ivanovf8ff6b12015-01-27 19:32:56 -0800833template <typename ELF>
834void ElfFile<ELF>::Flush() {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800835 // 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 Ivanovf8ff6b12015-01-27 19:32:56 -0800842 if (file_bytes == -1) {
843 LOG(ERROR) << "elf_update failed: " << elf_errmsg(elf_errno());
844 }
845
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800846 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 Ivanovf8ff6b12015-01-27 19:32:56 -0800857template <typename ELF>
858void 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
870template <typename ELF>
871void 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
882template class ElfFile<ELF32_traits>;
883template class ElfFile<ELF64_traits>;
884
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800885} // namespace relocation_packer