Chromium Code Reviews| 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 R_ARM_RELATIVE relocations from the .rel.dyn | 7 // Provides functions to remove ARM relative relocations from the .rel.dyn |
| 8 // section and pack them in .android.rel.dyn, and unpack to return the file | 8 // section and pack them in .android.rel.dyn, and unpack to return the file |
| 9 // to its pre-packed state. | 9 // 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 // section. A standard libchrome.<version>.so will not contain this section, |
| 13 // so the following can be used to add one: | 13 // 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 // arm-linux-gnueabi-objcopy |
| 17 // --add-section .android.rel.dyn=/tmp/small | 17 // --add-section .android.rel.dyn=/tmp/small |
| 18 // libchrome.<version>.so libchrome.<version>.so.packed | 18 // libchrome.<version>.so libchrome.<version>.so.packed |
| 19 // rm /tmp/small | 19 // rm /tmp/small |
| 20 // | 20 // |
| 21 // To use, open the file and pass the file descriptor to the constructor, | 21 // 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 | 22 // then pack or unpack as desired. Packing or unpacking will flush the file |
| 23 // descriptor on success. Example: | 23 // descriptor on success. Example: |
| 24 // | 24 // |
| 25 // int fd = open(..., O_RDWR); | 25 // int fd = open(..., O_RDWR); |
| 26 // ElfFile elf_file(fd); | 26 // ElfFile elf_file(fd); |
| 27 // bool status; | 27 // bool status; |
| 28 // if (is_packing) | 28 // if (is_packing) |
| 29 // status = elf_file.PackRelocations(); | 29 // status = elf_file.PackRelocations(); |
| 30 // else | 30 // else |
| 31 // status = elf_file.UnpackRelocations(); | 31 // status = elf_file.UnpackRelocations(); |
| 32 // close(fd); | 32 // close(fd); |
| 33 // | 33 // |
| 34 // SetPadding() causes PackRelocations() to pad .rel.dyn with R_ARM_NONE | 34 // SetPadding() causes PackRelocations() to pad .rel.dyn with NONE-type |
| 35 // entries rather than cutting a hole out of the shared object file. This | 35 // entries rather than cutting a hole out of the shared object file. This |
| 36 // keeps all load addresses and offsets constant, and enables easier | 36 // keeps all load addresses and offsets constant, and enables easier |
| 37 // debugging and testing. | 37 // debugging and testing. |
| 38 // | 38 // |
| 39 // A packed shared object file has all of its R_ARM_RELATIVE relocations | 39 // A packed shared object file has all of its ARM relative relocations |
| 40 // removed from .rel.dyn, and replaced as packed data in .android.rel.dyn. | 40 // removed from .rel.dyn, and replaced as packed data in .android.rel.dyn. |
| 41 // The resulting file is shorter than its non-packed original. | 41 // The resulting file is shorter than its non-packed original. |
| 42 // | 42 // |
| 43 // Unpacking a packed file restores the file to its non-packed state, by | 43 // Unpacking a packed file restores the file to its non-packed state, by |
| 44 // expanding the packed data in android.rel.dyn, combining the R_ARM_RELATIVE | 44 // expanding the packed data in android.rel.dyn, combining the ARM relative |
| 45 // relocations with the data already in .rel.dyn, and then writing back the | 45 // relocations with the data already in .rel.dyn, and then writing back the |
| 46 // now expanded .rel.dyn section. | 46 // now expanded .rel.dyn section. |
| 47 | 47 |
| 48 #ifndef TOOLS_RELOCATION_PACKER_SRC_ELF_FILE_H_ | 48 #ifndef TOOLS_RELOCATION_PACKER_SRC_ELF_FILE_H_ |
| 49 #define TOOLS_RELOCATION_PACKER_SRC_ELF_FILE_H_ | 49 #define TOOLS_RELOCATION_PACKER_SRC_ELF_FILE_H_ |
| 50 | 50 |
| 51 #include <string.h> | 51 #include <string.h> |
| 52 | 52 |
| 53 #include "elf.h" | 53 #include "elf.h" |
| 54 #include "libelf.h" | 54 #include "libelf.h" |
| 55 #include "packer.h" | 55 #include "packer.h" |
| 56 | 56 |
| 57 namespace relocation_packer { | 57 namespace relocation_packer { |
| 58 | 58 |
| 59 // An ElfFile reads shared objects, and shuttles R_ARM_RELATIVE relocations | 59 // An ElfFile reads shared objects, and shuttles ARM relative relocations |
|
rmcilroy
2014/07/18 14:05:17
Should we remove the "ARM" from here too if we are
simonb (inactive)
2014/07/21 12:15:49
It's still only ARM, just arm32 and arm64. I'd ra
| |
| 60 // between .rel.dyn and .android.rel.dyn sections. | 60 // between .rel.dyn and .android.rel.dyn sections. |
| 61 class ElfFile { | 61 class ElfFile { |
| 62 public: | 62 public: |
| 63 explicit ElfFile(int fd) { memset(this, 0, sizeof(*this)); fd_ = fd; } | 63 explicit ElfFile(int fd) { memset(this, 0, sizeof(*this)); fd_ = fd; } |
| 64 ~ElfFile() {} | 64 ~ElfFile() {} |
| 65 | 65 |
| 66 // Set padding mode. When padding, PackRelocations() will not shrink | 66 // Set padding mode. When padding, PackRelocations() will not shrink |
| 67 // the .rel.dyn section, but instead replace R_ARM_RELATIVE with | 67 // the .rel.dyn section, but instead replace ARM relative with |
| 68 // R_ARM_NONE entries. | 68 // NONE-type entries. |
| 69 // |flag| is true to pad .rel.dyn, false to shrink it. | 69 // |flag| is true to pad .rel.dyn, false to shrink it. |
| 70 inline void SetPadding(bool flag) { is_padding_rel_dyn_ = flag; } | 70 inline void SetPadding(bool flag) { is_padding_rel_dyn_ = flag; } |
| 71 | 71 |
| 72 // Transfer R_ARM_RELATIVE relocations from .rel.dyn to a packed | 72 // Transfer ARM relative relocations from .rel.dyn to a packed |
| 73 // representation in .android.rel.dyn. Returns true on success. | 73 // representation in .android.rel.dyn. Returns true on success. |
| 74 bool PackRelocations(); | 74 bool PackRelocations(); |
| 75 | 75 |
| 76 // Transfer R_ARM_RELATIVE relocations from a packed representation in | 76 // Transfer ARM relative relocations from a packed representation in |
| 77 // .android.rel.dyn to .rel.dyn. Returns true on success. | 77 // .android.rel.dyn to .rel.dyn. Returns true on success. |
| 78 bool UnpackRelocations(); | 78 bool UnpackRelocations(); |
| 79 | 79 |
| 80 private: | 80 private: |
| 81 // Load a new ElfFile from a filedescriptor. If flushing, the file must | 81 // 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. | 82 // be open for read/write. Returns true on successful ELF file load. |
| 83 // |fd| is an open file descriptor for the shared object. | 83 // |fd| is an open file descriptor for the shared object. |
| 84 bool Load(); | 84 bool Load(); |
| 85 | 85 |
| 86 // Write ELF file changes. | 86 // Write ELF file changes. |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 98 | 98 |
| 99 // Sections that we manipulate, assigned by Load(). | 99 // Sections that we manipulate, assigned by Load(). |
| 100 Elf_Scn* rel_dyn_section_; | 100 Elf_Scn* rel_dyn_section_; |
| 101 Elf_Scn* dynamic_section_; | 101 Elf_Scn* dynamic_section_; |
| 102 Elf_Scn* android_rel_dyn_section_; | 102 Elf_Scn* android_rel_dyn_section_; |
| 103 }; | 103 }; |
| 104 | 104 |
| 105 } // namespace relocation_packer | 105 } // namespace relocation_packer |
| 106 | 106 |
| 107 #endif // TOOLS_RELOCATION_PACKER_SRC_ELF_FILE_H_ | 107 #endif // TOOLS_RELOCATION_PACKER_SRC_ELF_FILE_H_ |
| OLD | NEW |