blob: 3f784e4f3b3e8713395d143818dee017c23c8aa1 [file] [log] [blame]
Dmitriy Ivanov87a06172015-02-06 10:56:28 -08001// 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 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.
14//
15// NOTE: Breaks with libelf 0.152, which is buggy. libelf 0.158 works.
16
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>
25
26#include "debug.h"
27#include "elf_file.h"
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080028#include "elf_traits.h"
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080029#include "libelf.h"
30
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080031#include "nativehelper/ScopedFd.h"
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080032
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -080033static void PrintUsage(const char* argv0) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080034 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);
38 }
39 const char* basename = temporary.c_str();
40
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);
48
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080049 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");
52}
53
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080054int main(int argc, char* argv[]) {
55 bool is_unpacking = false;
56 bool is_verbose = false;
57 bool is_padding = false;
58
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}
62 };
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;
88 }
89 }
90 if (optind != argc - 1) {
91 LOG(INFO) << "Try '" << argv[0] << " --help' for more information.";
92 return 1;
93 }
94
95 if (elf_version(EV_CURRENT) == EV_NONE) {
96 LOG(WARNING) << "Elf Library is out of date!";
97 }
98
Dmitriy Ivanov87a06172015-02-06 10:56:28 -080099 const char* file = argv[argc - 1];
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800100 ScopedFd fd(open(file, O_RDWR));
101 if (fd.get() == -1) {
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800102 LOG(ERROR) << file << ": " << strerror(errno);
103 return 1;
104 }
105
106 if (is_verbose)
107 relocation_packer::Logger::SetVerbose(1);
108
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800109 // 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;
115 }
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800116
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800117 if (TEMP_FAILURE_RETRY(lseek(fd.get(), 0, SEEK_SET)) != 0) {
118 LOG(ERROR) << file << ": lseek to 0 failed:" << strerror(errno);
119 return 1;
120 }
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800121
Dmitriy Ivanovf8ff6b12015-01-27 19:32:56 -0800122 bool status = false;
123
124 if (e_ident[EI_CLASS] == ELFCLASS32) {
125 relocation_packer::ElfFile<ELF32_traits> elf_file(fd.get());
126 elf_file.SetPadding(is_padding);
127
128 if (is_unpacking) {
129 status = elf_file.UnpackRelocations();
130 } else {
131 status = elf_file.PackRelocations();
132 }
133 } else if (e_ident[EI_CLASS] == ELFCLASS64) {
134 relocation_packer::ElfFile<ELF64_traits> elf_file(fd.get());
135 elf_file.SetPadding(is_padding);
136
137 if (is_unpacking) {
138 status = elf_file.UnpackRelocations();
139 } else {
140 status = elf_file.PackRelocations();
141 }
142 } else {
143 LOG(ERROR) << file << ": unknown ELFCLASS: " << e_ident[EI_CLASS];
144 return 1;
145 }
Dmitriy Ivanov87a06172015-02-06 10:56:28 -0800146
147 if (!status) {
148 LOG(ERROR) << file << ": failed to pack/unpack file";
149 return 1;
150 }
151
152 return 0;
153}