4bc09b6e5eebbd67ef68c8e23ba4936dcb72f592
[platform/framework/web/crosswalk.git] / src / tools / relocation_packer / src / main.cc
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 // Tool to pack and unpack R_ARM_RELATIVE relocations in a shared library.
6 //
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.
9 //
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.
14 //
15 // NOTE: Breaks with libelf 0.152, which is buggy.  libelf 0.158 works.
16
17 // TODO(simonb): Extend for 64-bit target libraries.
18
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <getopt.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27 #include <string>
28
29 #include "debug.h"
30 #include "elf_file.h"
31 #include "libelf.h"
32 #include "packer.h"
33
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);
39   }
40   const char* basename = temporary.c_str();
41
42   printf(
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"
58       "    rm /tmp/small\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);
67 }
68
69 int main(int argc, char* argv[]) {
70   bool is_unpacking = false;
71   bool is_verbose = false;
72   bool is_padding = false;
73
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}
77   };
78   bool has_options = true;
79   while (has_options) {
80     int c = getopt_long(argc, argv, "uvph", options, NULL);
81     switch (c) {
82       case 'u':
83         is_unpacking = true;
84         break;
85       case 'v':
86         is_verbose = true;
87         break;
88       case 'p':
89         is_padding = true;
90         break;
91       case 'h':
92         PrintUsage(argv[0]);
93         return 0;
94       case '?':
95         LOG("Try '%s --help' for more information.\n", argv[0]);
96         return 1;
97       case -1:
98         has_options = false;
99         break;
100       default:
101         NOTREACHED();
102     }
103   }
104   if (optind != argc - 1) {
105     LOG("Try '%s --help' for more information.\n", argv[0]);
106     return 1;
107   }
108
109   if (elf_version(EV_CURRENT) == EV_NONE) {
110     LOG("WARNING: Elf Library is out of date!\n");
111   }
112
113   const char* file = argv[argc - 1];
114   const int fd = open(file, O_RDWR);
115   if (fd == -1) {
116     LOG("%s: %s\n", file, strerror(errno));
117     return 1;
118   }
119
120   relocation_packer::Logger::SetVerbose(is_verbose);
121
122   relocation_packer::ElfFile elf_file(fd);
123   elf_file.SetPadding(is_padding);
124
125   bool status;
126   if (is_unpacking)
127     status = elf_file.UnpackRelocations();
128   else
129     status = elf_file.PackRelocations();
130
131   close(fd);
132
133   if (!status) {
134     LOG("ERROR: %s: failed to pack/unpack file\n", file);
135     return 1;
136   }
137
138   return 0;
139 }