elf_file.h revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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.
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.
70class 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() {}
77
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; }
83
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();
88
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();
93
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();
99
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,
104                            Elf_Data* data);
105
106  // Templated unpacker, helper for UnpackRelocations().  Rel type is one of
107  // ELF::Rel or ELF::Rela.
108  template <typename Rel>
109  bool UnpackTypedRelocations(const std::vector<uint8_t>& packed,
110                              Elf_Data* data);
111
112  // Write ELF file changes.
113  void Flush();
114
115  // File descriptor opened on the shared object.
116  int fd_;
117
118  // If set, pad rather than shrink .rel.dyn or .rela.dyn.  Primarily for
119  // debugging, allows packing to be checked without affecting load addresses.
120  bool is_padding_relocations_;
121
122  // Libelf handle, assigned by Load().
123  Elf* elf_;
124
125  // Sections that we manipulate, assigned by Load().
126  Elf_Scn* relocations_section_;
127  Elf_Scn* dynamic_section_;
128  Elf_Scn* android_relocations_section_;
129
130  // Relocation type found, assigned by Load().
131  enum { NONE = 0, REL, RELA } relocations_type_;
132};
133
134}  // namespace relocation_packer
135
136#endif  // TOOLS_RELOCATION_PACKER_SRC_ELF_FILE_H_
137