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 // Tool to pack and unpack R_ARM_RELATIVE relocations in a shared library.
7 // Packing removes R_ARM_RELATIVE relocations from .rel.dyn and writes them
8 // in a more compact form to .android.rel.dyn. Unpacking does the reverse.
10 // Invoke with -v to trace actions taken when packing or unpacking.
11 // Invoke with -p to pad removed relocations with R_ARM_NONE. Suppresses
12 // shrinking of .rel.dyn.
13 // See PrintUsage() below for full usage details.
15 // NOTE: Breaks with libelf 0.152, which is buggy. libelf 0.158 works.
17 // TODO(simonb): Extend for 64-bit target libraries.
25 #include <sys/types.h>
34 void PrintUsage(const char* argv0) {
35 std::string temporary = argv0;
36 const size_t last_slash = temporary.find_last_of("/");
37 if (last_slash != temporary.npos) {
38 temporary.erase(0, last_slash + 1);
40 const char* basename = temporary.c_str();
43 "Usage: %s [-u] [-v] [-p] file\n"
44 "Pack or unpack R_ARM_RELATIVE relocations in a shared library.\n\n"
45 " -u, --unpack unpack previously packed R_ARM_RELATIVE relocations\n"
46 " -v, --verbose trace object file modifications (for debugging)\n"
47 " -p, --pad do not shrink .rel.dyn, but pad (for debugging)\n\n"
48 "Extracts R_ARM_RELATIVE relocations from the .rel.dyn section, packs\n"
49 "them into a more compact format, and stores the packed relocations in\n"
50 ".android.rel.dyn. Expands .android.rel.dyn to hold the packed data,\n"
51 "and shrinks .rel.dyn by the amount of unpacked data removed from it.\n\n"
52 "Before being packed, a shared library needs to be prepared by adding\n"
53 "a null .android.rel.dyn section. A complete packing process is:\n\n"
54 " echo -n 'NULL' >/tmp/small\n"
55 " arm-linux-gnueabi-objcopy \\\n"
56 " --add-section .android.rel.dyn=/tmp/small \\\n"
57 " libchrome.<version>.so\n"
59 " %s libchrome.<version>.so\n\n"
60 "To unpack and restore the shared library to its original state:\n\n"
61 " %s -u libchrome.<version>.so\n"
62 " arm-linux-gnueabi-objcopy \\\n"
63 " --remove-section=.android.rel.dyn libchrome.<version>.so\n\n"
64 "Debug sections are not handled, so packing should not be used on\n"
65 "shared libraries compiled for debugging or otherwise unstripped.\n",
66 basename, basename, basename);
69 int main(int argc, char* argv[]) {
70 bool is_unpacking = false;
71 bool is_verbose = false;
72 bool is_padding = false;
74 static const option options[] = {
75 {"unpack", 0, 0, 'u'}, {"verbose", 0, 0, 'v'}, {"pad", 0, 0, 'p'},
76 {"help", 0, 0, 'h'}, {NULL, 0, 0, 0}
78 bool has_options = true;
80 int c = getopt_long(argc, argv, "uvph", options, NULL);
95 LOG("Try '%s --help' for more information.\n", argv[0]);
104 if (optind != argc - 1) {
105 LOG("Try '%s --help' for more information.\n", argv[0]);
109 if (elf_version(EV_CURRENT) == EV_NONE) {
110 LOG("WARNING: Elf Library is out of date!\n");
113 const char* file = argv[argc - 1];
114 const int fd = open(file, O_RDWR);
116 LOG("%s: %s\n", file, strerror(errno));
120 relocation_packer::Logger::SetVerbose(is_verbose);
122 relocation_packer::ElfFile elf_file(fd);
123 elf_file.SetPadding(is_padding);
127 status = elf_file.UnpackRelocations();
129 status = elf_file.PackRelocations();
134 LOG("ERROR: %s: failed to pack/unpack file\n", file);