Port Android relocation packer to chromium build
[chromium-blink-merge.git] / third_party / android_platform / bionic / tools / relocation_packer / src / main.cc
blob3f784e4f3b3e8713395d143818dee017c23c8aa1
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 relative relocations in a shared library.
6 //
7 // Packing removes 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_*_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 #include <errno.h>
18 #include <fcntl.h>
19 #include <getopt.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24 #include <string>
26 #include "debug.h"
27 #include "elf_file.h"
28 #include "elf_traits.h"
29 #include "libelf.h"
31 #include "nativehelper/ScopedFd.h"
33 static void PrintUsage(const char* argv0) {
34 std::string temporary = argv0;
35 const size_t last_slash = temporary.find_last_of("/");
36 if (last_slash != temporary.npos) {
37 temporary.erase(0, last_slash + 1);
39 const char* basename = temporary.c_str();
41 printf(
42 "Usage: %s [-u] [-v] [-p] file\n\n"
43 "Pack or unpack relative relocations in a shared library.\n\n"
44 " -u, --unpack unpack previously packed relative relocations\n"
45 " -v, --verbose trace object file modifications (for debugging)\n"
46 " -p, --pad do not shrink relocations, but pad (for debugging)\n\n",
47 basename);
49 printf(
50 "Debug sections are not handled, so packing should not be used on\n"
51 "shared libraries compiled for debugging or otherwise unstripped.\n");
54 int main(int argc, char* argv[]) {
55 bool is_unpacking = false;
56 bool is_verbose = false;
57 bool is_padding = false;
59 static const option options[] = {
60 {"unpack", 0, 0, 'u'}, {"verbose", 0, 0, 'v'}, {"pad", 0, 0, 'p'},
61 {"help", 0, 0, 'h'}, {NULL, 0, 0, 0}
63 bool has_options = true;
64 while (has_options) {
65 int c = getopt_long(argc, argv, "uvph", options, NULL);
66 switch (c) {
67 case 'u':
68 is_unpacking = true;
69 break;
70 case 'v':
71 is_verbose = true;
72 break;
73 case 'p':
74 is_padding = true;
75 break;
76 case 'h':
77 PrintUsage(argv[0]);
78 return 0;
79 case '?':
80 LOG(INFO) << "Try '" << argv[0] << " --help' for more information.";
81 return 1;
82 case -1:
83 has_options = false;
84 break;
85 default:
86 NOTREACHED();
87 return 1;
90 if (optind != argc - 1) {
91 LOG(INFO) << "Try '" << argv[0] << " --help' for more information.";
92 return 1;
95 if (elf_version(EV_CURRENT) == EV_NONE) {
96 LOG(WARNING) << "Elf Library is out of date!";
99 const char* file = argv[argc - 1];
100 ScopedFd fd(open(file, O_RDWR));
101 if (fd.get() == -1) {
102 LOG(ERROR) << file << ": " << strerror(errno);
103 return 1;
106 if (is_verbose)
107 relocation_packer::Logger::SetVerbose(1);
109 // We need to detect elf class in order to create
110 // correct implementation
111 uint8_t e_ident[EI_NIDENT];
112 if (TEMP_FAILURE_RETRY(read(fd.get(), e_ident, EI_NIDENT) != EI_NIDENT)) {
113 LOG(ERROR) << file << ": failed to read elf header:" << strerror(errno);
114 return 1;
117 if (TEMP_FAILURE_RETRY(lseek(fd.get(), 0, SEEK_SET)) != 0) {
118 LOG(ERROR) << file << ": lseek to 0 failed:" << strerror(errno);
119 return 1;
122 bool status = false;
124 if (e_ident[EI_CLASS] == ELFCLASS32) {
125 relocation_packer::ElfFile<ELF32_traits> elf_file(fd.get());
126 elf_file.SetPadding(is_padding);
128 if (is_unpacking) {
129 status = elf_file.UnpackRelocations();
130 } else {
131 status = elf_file.PackRelocations();
133 } else if (e_ident[EI_CLASS] == ELFCLASS64) {
134 relocation_packer::ElfFile<ELF64_traits> elf_file(fd.get());
135 elf_file.SetPadding(is_padding);
137 if (is_unpacking) {
138 status = elf_file.UnpackRelocations();
139 } else {
140 status = elf_file.PackRelocations();
142 } else {
143 LOG(ERROR) << file << ": unknown ELFCLASS: " << e_ident[EI_CLASS];
144 return 1;
147 if (!status) {
148 LOG(ERROR) << file << ": failed to pack/unpack file";
149 return 1;
152 return 0;