Unregister from GCM when the only GCM app is removed
[chromium-blink-merge.git] / tools / relocation_packer / src / elf_file.h
blob6550274976590581ff7a91a65894ec619a5b0ead
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
3 // found in the LICENSE file.
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.
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:
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
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:
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);
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.
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.
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.
55 #ifndef TOOLS_RELOCATION_PACKER_SRC_ELF_FILE_H_
56 #define TOOLS_RELOCATION_PACKER_SRC_ELF_FILE_H_
58 #include <string.h>
59 #include <vector>
61 #include "elf.h"
62 #include "libelf.h"
63 #include "packer.h"
65 namespace relocation_packer {
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.
70 class ElfFile {
71 public:
72 explicit ElfFile(int fd)
73 : fd_(fd), is_padding_relocations_(false), elf_(NULL),
74 relocations_section_(NULL), dynamic_section_(NULL),
75 android_relocations_section_(NULL), relocations_type_(NONE) {}
76 ~ElfFile() {}
78 // Set padding mode. When padding, PackRelocations() will not shrink
79 // the .rel.dyn or .rela.dyn section, but instead replace relative with
80 // NONE-type entries.
81 // |flag| is true to pad .rel.dyn or .rela.dyn, false to shrink it.
82 inline void SetPadding(bool flag) { is_padding_relocations_ = flag; }
84 // 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 // on success.
87 bool PackRelocations();
89 // Transfer relative relocations from a packed representation in
90 // .android.rel.dyn or .android.rela.dyn to .rel.dyn or .rela.dyn. Returns
91 // true on success.
92 bool UnpackRelocations();
94 private:
95 // 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.
97 // |fd| is an open file descriptor for the shared object.
98 bool Load();
100 // Templated packer, helper for PackRelocations(). Rel type is one of
101 // ELF::Rel or ELF::Rela.
102 template <typename Rel>
103 bool PackTypedRelocations(const std::vector<Rel>& relocations);
105 // Templated unpacker, helper for UnpackRelocations(). Rel type is one of
106 // ELF::Rel or ELF::Rela.
107 template <typename Rel>
108 bool UnpackTypedRelocations(const std::vector<uint8_t>& packed);
110 // Write ELF file changes.
111 void Flush();
113 // File descriptor opened on the shared object.
114 int fd_;
116 // If set, pad rather than shrink .rel.dyn or .rela.dyn. Primarily for
117 // debugging, allows packing to be checked without affecting load addresses.
118 bool is_padding_relocations_;
120 // Libelf handle, assigned by Load().
121 Elf* elf_;
123 // Sections that we manipulate, assigned by Load().
124 Elf_Scn* relocations_section_;
125 Elf_Scn* dynamic_section_;
126 Elf_Scn* android_relocations_section_;
128 // Relocation type found, assigned by Load().
129 enum { NONE = 0, REL, RELA } relocations_type_;
132 } // namespace relocation_packer
134 #endif // TOOLS_RELOCATION_PACKER_SRC_ELF_FILE_H_