blob: 73c31923fcc823662f996832b8debb731f62f259 [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// ELF shared object file updates handler.
6//
7// Provides functions to remove relative relocations from the .rel.dyn
8// or .rela.dyn sections and pack into .android.rel.dyn or .android.rela.dyn,
9// and unpack to return the file to its pre-packed state.
10//
11// Files to be packed or unpacked must include an existing .android.rel.dyn
12// or android.rela.dyn section. A standard libchrome.<version>.so will not
13// contain this section, so the following can be used to add one:
14//
15// echo -n 'NULL' >/tmp/small
16// if file libchrome.<version>.so | grep -q 'ELF 32'; then
17// arm-linux-androideabi-objcopy
18// --add-section .android.rel.dyn=/tmp/small
19// libchrome.<version>.so libchrome.<version>.so.packed
20// else
21// aarch64-linux-android-objcopy
22// --add-section .android.rela.dyn=/tmp/small
23// libchrome.<version>.so libchrome.<version>.so.packed
24// fi
25// rm /tmp/small
26//
27// To use, open the file and pass the file descriptor to the constructor,
28// then pack or unpack as desired. Packing or unpacking will flush the file
29// descriptor on success. Example:
30//
31// int fd = open(..., O_RDWR);
32// ElfFile elf_file(fd);
33// bool status;
34// if (is_packing)
35// status = elf_file.PackRelocations();
36// else
37// status = elf_file.UnpackRelocations();
38// close(fd);
39//
40// SetPadding() causes PackRelocations() to pad .rel.dyn or .rela.dyn with
41// NONE-type entries rather than cutting a hole out of the shared object
42// file. This keeps all load addresses and offsets constant, and enables
43// easier debugging and testing.
44//
45// A packed shared object file has all of its relative relocations
46// removed from .rel.dyn or .rela.dyn, and replaced as packed data in
47// .android.rel.dyn or .android.rela.dyn respectively. The resulting file
48// is shorter than its non-packed original.
49//
50// Unpacking a packed file restores the file to its non-packed state, by
51// expanding the packed data in .android.rel.dyn or .android.rela.dyn,
52// combining the relative relocations with the data already in .rel.dyn
53// or .rela.dyn, and then writing back the now expanded section.
54
55#ifndef TOOLS_RELOCATION_PACKER_SRC_ELF_FILE_H_
56#define TOOLS_RELOCATION_PACKER_SRC_ELF_FILE_H_
57
58#include <string.h>
59#include <vector>
60
61#include "elf.h"
62#include "libelf.h"
63#include "packer.h"
64
65namespace relocation_packer {
66
67// An ElfFile reads shared objects, and shuttles relative relocations
68// between .rel.dyn or .rela.dyn and .android.rel.dyn or .android.rela.dyn
69// sections.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080070template <typename ELF>
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080071class ElfFile {
72 public:
73 explicit ElfFile(int fd)
74 : fd_(fd), is_padding_relocations_(false), elf_(NULL),
75 relocations_section_(NULL), dynamic_section_(NULL),
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080076 relocations_type_(NONE) {}
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080077 ~ElfFile() {}
78
79 // Set padding mode. When padding, PackRelocations() will not shrink
80 // the .rel.dyn or .rela.dyn section, but instead replace relative with
81 // NONE-type entries.
82 // |flag| is true to pad .rel.dyn or .rela.dyn, false to shrink it.
83 inline void SetPadding(bool flag) { is_padding_relocations_ = flag; }
84
85 // Transfer relative relocations from .rel.dyn or .rela.dyn to a packed
86 // representation in .android.rel.dyn or .android.rela.dyn. Returns true
87 // on success.
88 bool PackRelocations();
89
90 // Transfer relative relocations from a packed representation in
91 // .android.rel.dyn or .android.rela.dyn to .rel.dyn or .rela.dyn. Returns
92 // true on success.
93 bool UnpackRelocations();
94
95 private:
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080096 enum relocations_type_t {
97 NONE = 0, REL, RELA
98 };
99
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800100 // Load a new ElfFile from a filedescriptor. If flushing, the file must
101 // be open for read/write. Returns true on successful ELF file load.
102 // |fd| is an open file descriptor for the shared object.
103 bool Load();
104
105 // Templated packer, helper for PackRelocations(). Rel type is one of
106 // ELF::Rel or ELF::Rela.
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800107 bool PackTypedRelocations(std::vector<typename ELF::Rela>* relocations);
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800108
109 // Templated unpacker, helper for UnpackRelocations(). Rel type is one of
110 // ELF::Rel or ELF::Rela.
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800111 bool UnpackTypedRelocations(const std::vector<uint8_t>& packed);
112
113 // Write ELF file changes.
114 void Flush();
115
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800116 void AdjustRelativeRelocationTargets(typename ELF::Off hole_start,
117 ssize_t hole_size,
118 std::vector<typename ELF::Rela>* relocations);
119
120 static void ResizeSection(Elf* elf, Elf_Scn* section, size_t new_size,
121 typename ELF::Word new_sh_type, relocations_type_t relocations_type);
122
123 static void AdjustDynamicSectionForHole(Elf_Scn* dynamic_section,
124 typename ELF::Off hole_start,
125 ssize_t hole_size,
126 relocations_type_t relocations_type);
127
128 static void ConvertRelArrayToRelaVector(const typename ELF::Rel* rel_array, size_t rel_array_size,
129 std::vector<typename ELF::Rela>* rela_vector);
130
131 static void ConvertRelaVectorToRelVector(const std::vector<typename ELF::Rela>& rela_vector,
132 std::vector<typename ELF::Rel>* rel_vector);
133
134
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800135 // File descriptor opened on the shared object.
136 int fd_;
137
138 // If set, pad rather than shrink .rel.dyn or .rela.dyn. Primarily for
139 // debugging, allows packing to be checked without affecting load addresses.
140 bool is_padding_relocations_;
141
142 // Libelf handle, assigned by Load().
143 Elf* elf_;
144
145 // Sections that we manipulate, assigned by Load().
146 Elf_Scn* relocations_section_;
147 Elf_Scn* dynamic_section_;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800148
149 // Relocation type found, assigned by Load().
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800150 relocations_type_t relocations_type_;
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800151};
152
153} // namespace relocation_packer
154
155#endif // TOOLS_RELOCATION_PACKER_SRC_ELF_FILE_H_