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.
7 // Invoke with -v to trace actions taken when packing or unpacking.
8 // Invoke with -p to pad removed relocations with R_*_NONE. Suppresses
9 // shrinking of .rel.dyn.
10 // See PrintUsage() below for full usage details.
12 // NOTE: Breaks with libelf 0.152, which is buggy. libelf 0.158 works.
19 #include <sys/types.h>
25 #include "elf_traits.h"
28 #include "nativehelper/ScopedFd.h"
30 static void PrintUsage(const char* argv0
) {
31 std::string temporary
= argv0
;
32 const size_t last_slash
= temporary
.find_last_of("/");
33 if (last_slash
!= temporary
.npos
) {
34 temporary
.erase(0, last_slash
+ 1);
36 const char* basename
= temporary
.c_str();
39 "Usage: %s [-u] [-v] [-p] file\n\n"
40 "Pack or unpack relative relocations in a shared library.\n\n"
41 " -u, --unpack unpack previously packed relative relocations\n"
42 " -v, --verbose trace object file modifications (for debugging)\n"
43 " -p, --pad do not shrink relocations, but pad (for debugging)\n\n",
47 "Debug sections are not handled, so packing should not be used on\n"
48 "shared libraries compiled for debugging or otherwise unstripped.\n");
51 int main(int argc
, char* argv
[]) {
52 bool is_unpacking
= false;
53 bool is_verbose
= false;
54 bool is_padding
= false;
56 static const option options
[] = {
57 {"unpack", 0, 0, 'u'}, {"verbose", 0, 0, 'v'}, {"pad", 0, 0, 'p'},
58 {"help", 0, 0, 'h'}, {NULL
, 0, 0, 0}
60 bool has_options
= true;
62 int c
= getopt_long(argc
, argv
, "uvph", options
, NULL
);
77 LOG(INFO
) << "Try '" << argv
[0] << " --help' for more information.";
87 if (optind
!= argc
- 1) {
88 LOG(INFO
) << "Try '" << argv
[0] << " --help' for more information.";
92 if (elf_version(EV_CURRENT
) == EV_NONE
) {
93 LOG(WARNING
) << "Elf Library is out of date!";
96 const char* file
= argv
[argc
- 1];
97 ScopedFd
fd(open(file
, O_RDWR
));
99 LOG(ERROR
) << file
<< ": " << strerror(errno
);
104 relocation_packer::Logger::SetVerbose(1);
106 // We need to detect elf class in order to create
107 // correct implementation
108 uint8_t e_ident
[EI_NIDENT
];
109 if (TEMP_FAILURE_RETRY(read(fd
.get(), e_ident
, EI_NIDENT
) != EI_NIDENT
)) {
110 LOG(ERROR
) << file
<< ": failed to read elf header:" << strerror(errno
);
114 if (TEMP_FAILURE_RETRY(lseek(fd
.get(), 0, SEEK_SET
)) != 0) {
115 LOG(ERROR
) << file
<< ": lseek to 0 failed:" << strerror(errno
);
121 if (e_ident
[EI_CLASS
] == ELFCLASS32
) {
122 relocation_packer::ElfFile
<ELF32_traits
> elf_file(fd
.get());
123 elf_file
.SetPadding(is_padding
);
126 status
= elf_file
.UnpackRelocations();
128 status
= elf_file
.PackRelocations();
130 } else if (e_ident
[EI_CLASS
] == ELFCLASS64
) {
131 relocation_packer::ElfFile
<ELF64_traits
> elf_file(fd
.get());
132 elf_file
.SetPadding(is_padding
);
135 status
= elf_file
.UnpackRelocations();
137 status
= elf_file
.PackRelocations();
140 LOG(ERROR
) << file
<< ": unknown ELFCLASS: " << e_ident
[EI_CLASS
];
145 LOG(ERROR
) << file
<< ": failed to pack/unpack file";