OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // ELF shared object file updates handler. | 5 // ELF shared object file updates handler. |
6 // | 6 // |
7 // Provides functions to remove relative relocations from the .rel.dyn | 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, | 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. | 9 // and unpack to return the file to its pre-packed state. |
10 // | 10 // |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 | 60 |
61 #include "elf.h" | 61 #include "elf.h" |
62 #include "libelf.h" | 62 #include "libelf.h" |
63 #include "packer.h" | 63 #include "packer.h" |
64 | 64 |
65 namespace relocation_packer { | 65 namespace relocation_packer { |
66 | 66 |
67 // An ElfFile reads shared objects, and shuttles relative relocations | 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 | 68 // between .rel.dyn or .rela.dyn and .android.rel.dyn or .android.rela.dyn |
69 // sections. | 69 // sections. |
| 70 template <typename ELF> |
70 class ElfFile { | 71 class ElfFile { |
71 public: | 72 public: |
72 explicit ElfFile(int fd) | 73 explicit ElfFile(int fd) |
73 : fd_(fd), is_padding_relocations_(false), elf_(NULL), | 74 : fd_(fd), is_padding_relocations_(false), elf_(NULL), |
74 relocations_section_(NULL), dynamic_section_(NULL), | 75 relocations_section_(NULL), dynamic_section_(NULL), |
75 android_relocations_section_(NULL), relocations_type_(NONE) {} | 76 relocations_type_(NONE) {} |
76 ~ElfFile() {} | 77 ~ElfFile() {} |
77 | 78 |
78 // Set padding mode. When padding, PackRelocations() will not shrink | 79 // Set padding mode. When padding, PackRelocations() will not shrink |
79 // the .rel.dyn or .rela.dyn section, but instead replace relative with | 80 // the .rel.dyn or .rela.dyn section, but instead replace relative with |
80 // NONE-type entries. | 81 // NONE-type entries. |
81 // |flag| is true to pad .rel.dyn or .rela.dyn, false to shrink it. | 82 // |flag| is true to pad .rel.dyn or .rela.dyn, false to shrink it. |
82 inline void SetPadding(bool flag) { is_padding_relocations_ = flag; } | 83 inline void SetPadding(bool flag) { is_padding_relocations_ = flag; } |
83 | 84 |
84 // Transfer relative relocations from .rel.dyn or .rela.dyn to a packed | 85 // Transfer relative relocations from .rel.dyn or .rela.dyn to a packed |
85 // representation in .android.rel.dyn or .android.rela.dyn. Returns true | 86 // representation in .android.rel.dyn or .android.rela.dyn. Returns true |
86 // on success. | 87 // on success. |
87 bool PackRelocations(); | 88 bool PackRelocations(); |
88 | 89 |
89 // Transfer relative relocations from a packed representation in | 90 // Transfer relative relocations from a packed representation in |
90 // .android.rel.dyn or .android.rela.dyn to .rel.dyn or .rela.dyn. Returns | 91 // .android.rel.dyn or .android.rela.dyn to .rel.dyn or .rela.dyn. Returns |
91 // true on success. | 92 // true on success. |
92 bool UnpackRelocations(); | 93 bool UnpackRelocations(); |
93 | 94 |
94 private: | 95 private: |
| 96 enum relocations_type_t { |
| 97 NONE = 0, REL, RELA |
| 98 }; |
| 99 |
95 // Load a new ElfFile from a filedescriptor. If flushing, the file must | 100 // Load a new ElfFile from a filedescriptor. If flushing, the file must |
96 // be open for read/write. Returns true on successful ELF file load. | 101 // be open for read/write. Returns true on successful ELF file load. |
97 // |fd| is an open file descriptor for the shared object. | 102 // |fd| is an open file descriptor for the shared object. |
98 bool Load(); | 103 bool Load(); |
99 | 104 |
100 // Templated packer, helper for PackRelocations(). Rel type is one of | 105 // Templated packer, helper for PackRelocations(). Rel type is one of |
101 // ELF::Rel or ELF::Rela. | 106 // ELF::Rel or ELF::Rela. |
102 template <typename Rel> | 107 bool PackTypedRelocations(std::vector<typename ELF::Rela>* relocations); |
103 bool PackTypedRelocations(const std::vector<Rel>& relocations); | |
104 | 108 |
105 // Templated unpacker, helper for UnpackRelocations(). Rel type is one of | 109 // Templated unpacker, helper for UnpackRelocations(). Rel type is one of |
106 // ELF::Rel or ELF::Rela. | 110 // ELF::Rel or ELF::Rela. |
107 template <typename Rel> | |
108 bool UnpackTypedRelocations(const std::vector<uint8_t>& packed); | 111 bool UnpackTypedRelocations(const std::vector<uint8_t>& packed); |
109 | 112 |
110 // Write ELF file changes. | 113 // Write ELF file changes. |
111 void Flush(); | 114 void Flush(); |
112 | 115 |
| 116 void AdjustRelativeRelocationTargets(typename ELF::Off hole_start, |
| 117 ssize_t hole_size, |
| 118 std::vector<typename ELF::Rela>* relocati
ons); |
| 119 |
| 120 static void ResizeSection(Elf* elf, Elf_Scn* section, size_t new_size, |
| 121 typename ELF::Word new_sh_type, relocations_type_t r
elocations_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, si
ze_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_v
ector); |
| 133 |
| 134 |
113 // File descriptor opened on the shared object. | 135 // File descriptor opened on the shared object. |
114 int fd_; | 136 int fd_; |
115 | 137 |
116 // If set, pad rather than shrink .rel.dyn or .rela.dyn. Primarily for | 138 // If set, pad rather than shrink .rel.dyn or .rela.dyn. Primarily for |
117 // debugging, allows packing to be checked without affecting load addresses. | 139 // debugging, allows packing to be checked without affecting load addresses. |
118 bool is_padding_relocations_; | 140 bool is_padding_relocations_; |
119 | 141 |
120 // Libelf handle, assigned by Load(). | 142 // Libelf handle, assigned by Load(). |
121 Elf* elf_; | 143 Elf* elf_; |
122 | 144 |
123 // Sections that we manipulate, assigned by Load(). | 145 // Sections that we manipulate, assigned by Load(). |
124 Elf_Scn* relocations_section_; | 146 Elf_Scn* relocations_section_; |
125 Elf_Scn* dynamic_section_; | 147 Elf_Scn* dynamic_section_; |
126 Elf_Scn* android_relocations_section_; | |
127 | 148 |
128 // Relocation type found, assigned by Load(). | 149 // Relocation type found, assigned by Load(). |
129 enum { NONE = 0, REL, RELA } relocations_type_; | 150 relocations_type_t relocations_type_; |
130 }; | 151 }; |
131 | 152 |
132 } // namespace relocation_packer | 153 } // namespace relocation_packer |
133 | 154 |
134 #endif // TOOLS_RELOCATION_PACKER_SRC_ELF_FILE_H_ | 155 #endif // TOOLS_RELOCATION_PACKER_SRC_ELF_FILE_H_ |
OLD | NEW |