Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Side by Side Diff: tools/relocation_packer/src/elf_file.h

Issue 410933004: Extend relocation packing to cover arm64. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Script 'golden' test data generation Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 ARM relative relocations from the .rel.dyn 7 // Provides functions to remove relative relocations from the .rel.dyn
8 // section and pack them in .android.rel.dyn, and unpack to return the file 8 // or .rela.dyn sections and pack into .android.rel.dyn or .android.rela.dyn,
9 // to its pre-packed state. 9 // and unpack to return the file to its pre-packed state.
10 // 10 //
11 // Files to be packed or unpacked must include an existing .android.rel.dyn 11 // Files to be packed or unpacked must include an existing .android.rel.dyn
12 // section. A standard libchrome.<version>.so will not contain this section, 12 // or android.rela.dyn section. A standard libchrome.<version>.so will not
13 // so the following can be used to add one: 13 // contain this section, so the following can be used to add one:
14 // 14 //
15 // echo -n 'NULL' >/tmp/small 15 // echo -n 'NULL' >/tmp/small
16 // arm-linux-gnueabi-objcopy 16 // if file libchrome.<version>.so | grep -q 'ELF 32'; then
17 // --add-section .android.rel.dyn=/tmp/small 17 // arm-linux-gnueabi-objcopy
18 // libchrome.<version>.so libchrome.<version>.so.packed 18 // --add-section .android.rel.dyn=/tmp/small
19 // libchrome.<version>.so libchrome.<version>.so.packed
20 // else
21 // arm-linux-gnueabi-objcopy
22 // --add-section .android.rela.dyn=/tmp/small
23 // libchrome.<version>.so libchrome.<version>.so.packed
24 // fi
19 // rm /tmp/small 25 // rm /tmp/small
20 // 26 //
21 // To use, open the file and pass the file descriptor to the constructor, 27 // To use, open the file and pass the file descriptor to the constructor,
22 // then pack or unpack as desired. Packing or unpacking will flush the file 28 // then pack or unpack as desired. Packing or unpacking will flush the file
23 // descriptor on success. Example: 29 // descriptor on success. Example:
24 // 30 //
25 // int fd = open(..., O_RDWR); 31 // int fd = open(..., O_RDWR);
26 // ElfFile elf_file(fd); 32 // ElfFile elf_file(fd);
27 // bool status; 33 // bool status;
28 // if (is_packing) 34 // if (is_packing)
29 // status = elf_file.PackRelocations(); 35 // status = elf_file.PackRelocations();
30 // else 36 // else
31 // status = elf_file.UnpackRelocations(); 37 // status = elf_file.UnpackRelocations();
32 // close(fd); 38 // close(fd);
33 // 39 //
34 // SetPadding() causes PackRelocations() to pad .rel.dyn with NONE-type 40 // SetPadding() causes PackRelocations() to pad .rel.dyn or .rela.dyn with
35 // entries rather than cutting a hole out of the shared object file. This 41 // NONE-type entries rather than cutting a hole out of the shared object
36 // keeps all load addresses and offsets constant, and enables easier 42 // file. This keeps all load addresses and offsets constant, and enables
37 // debugging and testing. 43 // easier debugging and testing.
38 // 44 //
39 // A packed shared object file has all of its ARM relative relocations 45 // A packed shared object file has all of its relative relocations
40 // removed from .rel.dyn, and replaced as packed data in .android.rel.dyn. 46 // removed from .rel.dyn or .rela.dyn, and replaced as packed data in
41 // The resulting file is shorter than its non-packed original. 47 // .android.rel.dyn or .android.rela.dyn respectively. The resulting file
48 // is shorter than its non-packed original.
42 // 49 //
43 // Unpacking a packed file restores the file to its non-packed state, by 50 // Unpacking a packed file restores the file to its non-packed state, by
44 // expanding the packed data in android.rel.dyn, combining the ARM relative 51 // expanding the packed data in .android.rel.dyn or .android.rela.dyn,
45 // relocations with the data already in .rel.dyn, and then writing back the 52 // combining the relative relocations with the data already in .rel.dyn
46 // now expanded .rel.dyn section. 53 // or .rela.dyn, and then writing back the now expanded section.
47 54
48 #ifndef TOOLS_RELOCATION_PACKER_SRC_ELF_FILE_H_ 55 #ifndef TOOLS_RELOCATION_PACKER_SRC_ELF_FILE_H_
49 #define TOOLS_RELOCATION_PACKER_SRC_ELF_FILE_H_ 56 #define TOOLS_RELOCATION_PACKER_SRC_ELF_FILE_H_
50 57
51 #include <string.h> 58 #include <string.h>
59 #include <vector>
52 60
53 #include "elf.h" 61 #include "elf.h"
54 #include "libelf.h" 62 #include "libelf.h"
55 #include "packer.h" 63 #include "packer.h"
56 64
57 namespace relocation_packer { 65 namespace relocation_packer {
58 66
59 // An ElfFile reads shared objects, and shuttles ARM relative relocations 67 // An ElfFile reads shared objects, and shuttles relative relocations
60 // between .rel.dyn and .android.rel.dyn sections. 68 // between .rel.dyn or .rela.dyn and .android.rel.dyn or .android.rela.dyn
69 // sections.
61 class ElfFile { 70 class ElfFile {
62 public: 71 public:
63 explicit ElfFile(int fd) { memset(this, 0, sizeof(*this)); fd_ = fd; } 72 explicit ElfFile(int fd) { memset(this, 0, sizeof(*this)); fd_ = fd; }
64 ~ElfFile() {} 73 ~ElfFile() {}
65 74
66 // Set padding mode. When padding, PackRelocations() will not shrink 75 // Set padding mode. When padding, PackRelocations() will not shrink
67 // the .rel.dyn section, but instead replace ARM relative with 76 // the .rel.dyn or .rela.dyn section, but instead replace relative with
68 // NONE-type entries. 77 // NONE-type entries.
69 // |flag| is true to pad .rel.dyn, false to shrink it. 78 // |flag| is true to pad .rel.dyn or .rela.dyn, false to shrink it.
70 inline void SetPadding(bool flag) { is_padding_rel_dyn_ = flag; } 79 inline void SetPadding(bool flag) { is_padding_relocations_ = flag; }
71 80
72 // Transfer ARM relative relocations from .rel.dyn to a packed 81 // Transfer relative relocations from .rel.dyn or .rela.dyn to a packed
73 // representation in .android.rel.dyn. Returns true on success. 82 // representation in .android.rel.dyn or .android.rela.dyn. Returns true
83 // on success.
74 bool PackRelocations(); 84 bool PackRelocations();
75 85
76 // Transfer ARM relative relocations from a packed representation in 86 // Transfer relative relocations from a packed representation in
77 // .android.rel.dyn to .rel.dyn. Returns true on success. 87 // .android.rel.dyn or .android.rela.dyn to .rel.dyn or .rela.dyn. Returns
88 // true on success.
78 bool UnpackRelocations(); 89 bool UnpackRelocations();
79 90
80 private: 91 private:
81 // Load a new ElfFile from a filedescriptor. If flushing, the file must 92 // Load a new ElfFile from a filedescriptor. If flushing, the file must
82 // be open for read/write. Returns true on successful ELF file load. 93 // be open for read/write. Returns true on successful ELF file load.
83 // |fd| is an open file descriptor for the shared object. 94 // |fd| is an open file descriptor for the shared object.
84 bool Load(); 95 bool Load();
85 96
97 // Templated packer, helper for PackRelocations(). Rel type is one of
98 // ELF::Rel or ELF::Rela.
99 template <typename Rel>
100 bool PackTypedRelocations(const std::vector<Rel>& relocations,
101 Elf_Data* data);
102
103 // Templated unpacker, helper for UnpackRelocations(). Rel type is one of
104 // ELF::Rel or ELF::Rela.
105 template <typename Rel>
106 bool UnpackTypedRelocations(const std::vector<uint8_t>& packed,
107 Elf_Data* data);
108
86 // Write ELF file changes. 109 // Write ELF file changes.
87 void Flush(); 110 void Flush();
88 111
89 // If set, pad rather than shrink .rel.dyn. Primarily for debugging, 112 // If set, pad rather than shrink .rel.dyn or .rela.dyn. Primarily for
90 // allows packing to be checked without affecting load addresses. 113 // debugging, allows packing to be checked without affecting load addresses.
91 bool is_padding_rel_dyn_; 114 bool is_padding_relocations_;
92 115
93 // File descriptor opened on the shared object. 116 // File descriptor opened on the shared object.
94 int fd_; 117 int fd_;
95 118
96 // Libelf handle, assigned by Load(). 119 // Libelf handle, assigned by Load().
97 Elf* elf_; 120 Elf* elf_;
98 121
99 // Sections that we manipulate, assigned by Load(). 122 // Sections that we manipulate, assigned by Load().
100 Elf_Scn* rel_dyn_section_; 123 Elf_Scn* relocations_section_;
101 Elf_Scn* dynamic_section_; 124 Elf_Scn* dynamic_section_;
102 Elf_Scn* android_rel_dyn_section_; 125 Elf_Scn* android_relocations_section_;
126
127 // Relocation type found, assigned by Load().
128 enum { NONE = 0, REL, RELA } relocations_type_;
103 }; 129 };
104 130
105 } // namespace relocation_packer 131 } // namespace relocation_packer
106 132
107 #endif // TOOLS_RELOCATION_PACKER_SRC_ELF_FILE_H_ 133 #endif // TOOLS_RELOCATION_PACKER_SRC_ELF_FILE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698