2017-04-25 07:21:38 +08:00
|
|
|
//===- ELF.cpp - ELF object file implementation ---------------------------===//
|
2013-08-09 06:27:13 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2013-08-09 06:27:13 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Object/ELF.h"
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/ELF.h"
|
2017-10-25 11:37:12 +08:00
|
|
|
#include "llvm/Support/LEB128.h"
|
2013-08-09 06:27:13 +08:00
|
|
|
|
2017-04-25 07:21:38 +08:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace object;
|
2013-08-09 06:27:13 +08:00
|
|
|
|
2017-05-02 22:04:52 +08:00
|
|
|
#define STRINGIFY_ENUM_CASE(ns, name) \
|
|
|
|
case ns::name: \
|
|
|
|
return #name;
|
|
|
|
|
|
|
|
#define ELF_RELOC(name, value) STRINGIFY_ENUM_CASE(ELF, name)
|
2013-08-09 06:27:13 +08:00
|
|
|
|
2017-04-25 07:21:38 +08:00
|
|
|
StringRef llvm::object::getELFRelocationTypeName(uint32_t Machine,
|
|
|
|
uint32_t Type) {
|
2013-08-09 06:27:13 +08:00
|
|
|
switch (Machine) {
|
|
|
|
case ELF::EM_X86_64:
|
|
|
|
switch (Type) {
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/ELFRelocs/x86_64.def"
|
2013-08-09 06:27:13 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ELF::EM_386:
|
2015-11-04 19:21:50 +08:00
|
|
|
case ELF::EM_IAMCU:
|
2013-08-09 06:27:13 +08:00
|
|
|
switch (Type) {
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/ELFRelocs/i386.def"
|
2013-08-09 06:27:13 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ELF::EM_MIPS:
|
|
|
|
switch (Type) {
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/ELFRelocs/Mips.def"
|
2013-08-09 06:27:13 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ELF::EM_AARCH64:
|
|
|
|
switch (Type) {
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/ELFRelocs/AArch64.def"
|
2013-08-09 06:27:13 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ELF::EM_ARM:
|
|
|
|
switch (Type) {
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/ELFRelocs/ARM.def"
|
2013-08-09 06:27:13 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2017-09-13 09:49:49 +08:00
|
|
|
case ELF::EM_ARC_COMPACT:
|
|
|
|
case ELF::EM_ARC_COMPACT2:
|
|
|
|
switch (Type) {
|
|
|
|
#include "llvm/BinaryFormat/ELFRelocs/ARC.def"
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2016-09-28 21:23:42 +08:00
|
|
|
case ELF::EM_AVR:
|
|
|
|
switch (Type) {
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/ELFRelocs/AVR.def"
|
2016-09-28 21:23:42 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2013-08-09 06:27:13 +08:00
|
|
|
case ELF::EM_HEXAGON:
|
|
|
|
switch (Type) {
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/ELFRelocs/Hexagon.def"
|
2013-08-09 06:27:13 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2016-03-02 05:21:42 +08:00
|
|
|
case ELF::EM_LANAI:
|
|
|
|
switch (Type) {
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/ELFRelocs/Lanai.def"
|
2016-03-02 05:21:42 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2013-08-09 06:27:13 +08:00
|
|
|
case ELF::EM_PPC:
|
|
|
|
switch (Type) {
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/ELFRelocs/PowerPC.def"
|
2013-08-09 06:27:13 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ELF::EM_PPC64:
|
|
|
|
switch (Type) {
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/ELFRelocs/PowerPC64.def"
|
2013-08-09 06:27:13 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2016-11-02 00:59:37 +08:00
|
|
|
case ELF::EM_RISCV:
|
|
|
|
switch (Type) {
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/ELFRelocs/RISCV.def"
|
2016-11-02 00:59:37 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2013-08-09 06:27:13 +08:00
|
|
|
case ELF::EM_S390:
|
|
|
|
switch (Type) {
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/ELFRelocs/SystemZ.def"
|
2013-08-09 06:27:13 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2014-01-26 11:21:28 +08:00
|
|
|
case ELF::EM_SPARC:
|
|
|
|
case ELF::EM_SPARC32PLUS:
|
|
|
|
case ELF::EM_SPARCV9:
|
|
|
|
switch (Type) {
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/ELFRelocs/Sparc.def"
|
2014-01-26 11:21:28 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2016-06-18 06:38:08 +08:00
|
|
|
case ELF::EM_AMDGPU:
|
|
|
|
switch (Type) {
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/ELFRelocs/AMDGPU.def"
|
2016-06-18 06:38:08 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2017-12-20 06:05:25 +08:00
|
|
|
break;
|
2016-07-16 06:27:55 +08:00
|
|
|
case ELF::EM_BPF:
|
|
|
|
switch (Type) {
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/ELFRelocs/BPF.def"
|
2016-07-16 06:27:55 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2016-06-18 06:38:08 +08:00
|
|
|
break;
|
2018-11-15 20:29:43 +08:00
|
|
|
case ELF::EM_MSP430:
|
|
|
|
switch (Type) {
|
|
|
|
#include "llvm/BinaryFormat/ELFRelocs/MSP430.def"
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2013-08-09 06:27:13 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return "Unknown";
|
|
|
|
}
|
|
|
|
|
2014-11-22 04:16:07 +08:00
|
|
|
#undef ELF_RELOC
|
2017-05-02 22:04:52 +08:00
|
|
|
|
2018-12-14 15:46:58 +08:00
|
|
|
uint32_t llvm::object::getELFRelativeRelocationType(uint32_t Machine) {
|
2018-06-29 05:07:34 +08:00
|
|
|
switch (Machine) {
|
|
|
|
case ELF::EM_X86_64:
|
|
|
|
return ELF::R_X86_64_RELATIVE;
|
|
|
|
case ELF::EM_386:
|
|
|
|
case ELF::EM_IAMCU:
|
|
|
|
return ELF::R_386_RELATIVE;
|
|
|
|
case ELF::EM_MIPS:
|
|
|
|
break;
|
|
|
|
case ELF::EM_AARCH64:
|
|
|
|
return ELF::R_AARCH64_RELATIVE;
|
|
|
|
case ELF::EM_ARM:
|
|
|
|
return ELF::R_ARM_RELATIVE;
|
|
|
|
case ELF::EM_ARC_COMPACT:
|
|
|
|
case ELF::EM_ARC_COMPACT2:
|
|
|
|
return ELF::R_ARC_RELATIVE;
|
|
|
|
case ELF::EM_AVR:
|
|
|
|
break;
|
|
|
|
case ELF::EM_HEXAGON:
|
|
|
|
return ELF::R_HEX_RELATIVE;
|
|
|
|
case ELF::EM_LANAI:
|
|
|
|
break;
|
|
|
|
case ELF::EM_PPC:
|
|
|
|
break;
|
|
|
|
case ELF::EM_PPC64:
|
|
|
|
return ELF::R_PPC64_RELATIVE;
|
|
|
|
case ELF::EM_RISCV:
|
|
|
|
return ELF::R_RISCV_RELATIVE;
|
|
|
|
case ELF::EM_S390:
|
|
|
|
return ELF::R_390_RELATIVE;
|
|
|
|
case ELF::EM_SPARC:
|
|
|
|
case ELF::EM_SPARC32PLUS:
|
|
|
|
case ELF::EM_SPARCV9:
|
|
|
|
return ELF::R_SPARC_RELATIVE;
|
|
|
|
case ELF::EM_AMDGPU:
|
|
|
|
break;
|
|
|
|
case ELF::EM_BPF:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-05-02 22:04:52 +08:00
|
|
|
StringRef llvm::object::getELFSectionTypeName(uint32_t Machine, unsigned Type) {
|
|
|
|
switch (Machine) {
|
|
|
|
case ELF::EM_ARM:
|
|
|
|
switch (Type) {
|
|
|
|
STRINGIFY_ENUM_CASE(ELF, SHT_ARM_EXIDX);
|
|
|
|
STRINGIFY_ENUM_CASE(ELF, SHT_ARM_PREEMPTMAP);
|
|
|
|
STRINGIFY_ENUM_CASE(ELF, SHT_ARM_ATTRIBUTES);
|
|
|
|
STRINGIFY_ENUM_CASE(ELF, SHT_ARM_DEBUGOVERLAY);
|
|
|
|
STRINGIFY_ENUM_CASE(ELF, SHT_ARM_OVERLAYSECTION);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ELF::EM_HEXAGON:
|
|
|
|
switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_HEX_ORDERED); }
|
|
|
|
break;
|
|
|
|
case ELF::EM_X86_64:
|
|
|
|
switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_X86_64_UNWIND); }
|
|
|
|
break;
|
|
|
|
case ELF::EM_MIPS:
|
|
|
|
case ELF::EM_MIPS_RS3_LE:
|
|
|
|
switch (Type) {
|
|
|
|
STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_REGINFO);
|
|
|
|
STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_OPTIONS);
|
|
|
|
STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_DWARF);
|
2019-02-21 18:19:08 +08:00
|
|
|
STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_ABIFLAGS);
|
2017-05-02 22:04:52 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (Type) {
|
|
|
|
STRINGIFY_ENUM_CASE(ELF, SHT_NULL);
|
|
|
|
STRINGIFY_ENUM_CASE(ELF, SHT_PROGBITS);
|
|
|
|
STRINGIFY_ENUM_CASE(ELF, SHT_SYMTAB);
|
|
|
|
STRINGIFY_ENUM_CASE(ELF, SHT_STRTAB);
|
|
|
|
STRINGIFY_ENUM_CASE(ELF, SHT_RELA);
|
|
|
|
STRINGIFY_ENUM_CASE(ELF, SHT_HASH);
|
|
|
|
STRINGIFY_ENUM_CASE(ELF, SHT_DYNAMIC);
|
|
|
|
STRINGIFY_ENUM_CASE(ELF, SHT_NOTE);
|
|
|
|
STRINGIFY_ENUM_CASE(ELF, SHT_NOBITS);
|
|
|
|
STRINGIFY_ENUM_CASE(ELF, SHT_REL);
|
|
|
|
STRINGIFY_ENUM_CASE(ELF, SHT_SHLIB);
|
|
|
|
STRINGIFY_ENUM_CASE(ELF, SHT_DYNSYM);
|
|
|
|
STRINGIFY_ENUM_CASE(ELF, SHT_INIT_ARRAY);
|
|
|
|
STRINGIFY_ENUM_CASE(ELF, SHT_FINI_ARRAY);
|
|
|
|
STRINGIFY_ENUM_CASE(ELF, SHT_PREINIT_ARRAY);
|
|
|
|
STRINGIFY_ENUM_CASE(ELF, SHT_GROUP);
|
|
|
|
STRINGIFY_ENUM_CASE(ELF, SHT_SYMTAB_SHNDX);
|
2018-06-29 05:07:34 +08:00
|
|
|
STRINGIFY_ENUM_CASE(ELF, SHT_RELR);
|
2017-10-28 01:49:40 +08:00
|
|
|
STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_REL);
|
|
|
|
STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_RELA);
|
2018-06-29 05:07:34 +08:00
|
|
|
STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_RELR);
|
2017-06-15 02:52:12 +08:00
|
|
|
STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_ODRTAB);
|
2018-01-31 00:29:29 +08:00
|
|
|
STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_LINKER_OPTIONS);
|
[MC] Add assembler support for .cg_profile.
Object FIle Representation
At codegen time this is emitted into the ELF file a pair of symbol indices and a weight. In assembly it looks like:
.cg_profile a, b, 32
.cg_profile freq, a, 11
.cg_profile freq, b, 20
When writing an ELF file these are put into a SHT_LLVM_CALL_GRAPH_PROFILE (0x6fff4c02) section as (uint32_t, uint32_t, uint64_t) tuples as (from symbol index, to symbol index, weight).
Differential Revision: https://reviews.llvm.org/D44965
llvm-svn: 333823
2018-06-03 00:33:01 +08:00
|
|
|
STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_CALL_GRAPH_PROFILE);
|
2018-07-18 06:17:18 +08:00
|
|
|
STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_ADDRSIG);
|
[ELF] Implement Dependent Libraries Feature
This patch implements a limited form of autolinking primarily designed to allow
either the --dependent-library compiler option, or "comment lib" pragmas (
https://docs.microsoft.com/en-us/cpp/preprocessor/comment-c-cpp?view=vs-2017) in
C/C++ e.g. #pragma comment(lib, "foo"), to cause an ELF linker to automatically
add the specified library to the link when processing the input file generated
by the compiler.
Currently this extension is unique to LLVM and LLD. However, care has been taken
to design this feature so that it could be supported by other ELF linkers.
The design goals were to provide:
- A simple linking model for developers to reason about.
- The ability to to override autolinking from the linker command line.
- Source code compatibility, where possible, with "comment lib" pragmas in other
environments (MSVC in particular).
Dependent library support is implemented differently for ELF platforms than on
the other platforms. Primarily this difference is that on ELF we pass the
dependent library specifiers directly to the linker without manipulating them.
This is in contrast to other platforms where they are mapped to a specific
linker option by the compiler. This difference is a result of the greater
variety of ELF linkers and the fact that ELF linkers tend to handle libraries in
a more complicated fashion than on other platforms. This forces us to defer
handling the specifiers to the linker.
In order to achieve a level of source code compatibility with other platforms
we have restricted this feature to work with libraries that meet the following
"reasonable" requirements:
1. There are no competing defined symbols in a given set of libraries, or
if they exist, the program owner doesn't care which is linked to their
program.
2. There may be circular dependencies between libraries.
The binary representation is a mergeable string section (SHF_MERGE,
SHF_STRINGS), called .deplibs, with custom type SHT_LLVM_DEPENDENT_LIBRARIES
(0x6fff4c04). The compiler forms this section by concatenating the arguments of
the "comment lib" pragmas and --dependent-library options in the order they are
encountered. Partial (-r, -Ur) links are handled by concatenating .deplibs
sections with the normal mergeable string section rules. As an example, #pragma
comment(lib, "foo") would result in:
.section ".deplibs","MS",@llvm_dependent_libraries,1
.asciz "foo"
For LTO, equivalent information to the contents of a the .deplibs section can be
retrieved by the LLD for bitcode input files.
LLD processes the dependent library specifiers in the following way:
1. Dependent libraries which are found from the specifiers in .deplibs sections
of relocatable object files are added when the linker decides to include that
file (which could itself be in a library) in the link. Dependent libraries
behave as if they were appended to the command line after all other options. As
a consequence the set of dependent libraries are searched last to resolve
symbols.
2. It is an error if a file cannot be found for a given specifier.
3. Any command line options in effect at the end of the command line parsing apply
to the dependent libraries, e.g. --whole-archive.
4. The linker tries to add a library or relocatable object file from each of the
strings in a .deplibs section by; first, handling the string as if it was
specified on the command line; second, by looking for the string in each of the
library search paths in turn; third, by looking for a lib<string>.a or
lib<string>.so (depending on the current mode of the linker) in each of the
library search paths.
5. A new command line option --no-dependent-libraries tells LLD to ignore the
dependent libraries.
Rationale for the above points:
1. Adding the dependent libraries last makes the process simple to understand
from a developers perspective. All linkers are able to implement this scheme.
2. Error-ing for libraries that are not found seems like better behavior than
failing the link during symbol resolution.
3. It seems useful for the user to be able to apply command line options which
will affect all of the dependent libraries. There is a potential problem of
surprise for developers, who might not realize that these options would apply
to these "invisible" input files; however, despite the potential for surprise,
this is easy for developers to reason about and gives developers the control
that they may require.
4. This algorithm takes into account all of the different ways that ELF linkers
find input files. The different search methods are tried by the linker in most
obvious to least obvious order.
5. I considered adding finer grained control over which dependent libraries were
ignored (e.g. MSVC has /nodefaultlib:<library>); however, I concluded that this
is not necessary: if finer control is required developers can fall back to using
the command line directly.
RFC thread: http://lists.llvm.org/pipermail/llvm-dev/2019-March/131004.html.
Differential Revision: https://reviews.llvm.org/D60274
llvm-svn: 360984
2019-05-17 11:44:15 +08:00
|
|
|
STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_DEPENDENT_LIBRARIES);
|
2019-05-29 11:29:01 +08:00
|
|
|
STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_SYMPART);
|
2017-05-02 22:04:52 +08:00
|
|
|
STRINGIFY_ENUM_CASE(ELF, SHT_GNU_ATTRIBUTES);
|
|
|
|
STRINGIFY_ENUM_CASE(ELF, SHT_GNU_HASH);
|
|
|
|
STRINGIFY_ENUM_CASE(ELF, SHT_GNU_verdef);
|
|
|
|
STRINGIFY_ENUM_CASE(ELF, SHT_GNU_verneed);
|
|
|
|
STRINGIFY_ENUM_CASE(ELF, SHT_GNU_versym);
|
|
|
|
default:
|
|
|
|
return "Unknown";
|
|
|
|
}
|
|
|
|
}
|
2017-10-25 11:37:12 +08:00
|
|
|
|
2018-06-29 05:07:34 +08:00
|
|
|
template <class ELFT>
|
|
|
|
Expected<std::vector<typename ELFT::Rela>>
|
|
|
|
ELFFile<ELFT>::decode_relrs(Elf_Relr_Range relrs) const {
|
|
|
|
// This function decodes the contents of an SHT_RELR packed relocation
|
|
|
|
// section.
|
|
|
|
//
|
|
|
|
// Proposal for adding SHT_RELR sections to generic-abi is here:
|
|
|
|
// https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg
|
|
|
|
//
|
|
|
|
// The encoded sequence of Elf64_Relr entries in a SHT_RELR section looks
|
|
|
|
// like [ AAAAAAAA BBBBBBB1 BBBBBBB1 ... AAAAAAAA BBBBBB1 ... ]
|
|
|
|
//
|
|
|
|
// i.e. start with an address, followed by any number of bitmaps. The address
|
|
|
|
// entry encodes 1 relocation. The subsequent bitmap entries encode up to 63
|
|
|
|
// relocations each, at subsequent offsets following the last address entry.
|
|
|
|
//
|
|
|
|
// The bitmap entries must have 1 in the least significant bit. The assumption
|
|
|
|
// here is that an address cannot have 1 in lsb. Odd addresses are not
|
|
|
|
// supported.
|
|
|
|
//
|
|
|
|
// Excluding the least significant bit in the bitmap, each non-zero bit in
|
|
|
|
// the bitmap represents a relocation to be applied to a corresponding machine
|
|
|
|
// word that follows the base address word. The second least significant bit
|
|
|
|
// represents the machine word immediately following the initial address, and
|
|
|
|
// each bit that follows represents the next word, in linear order. As such,
|
|
|
|
// a single bitmap can encode up to 31 relocations in a 32-bit object, and
|
|
|
|
// 63 relocations in a 64-bit object.
|
|
|
|
//
|
|
|
|
// This encoding has a couple of interesting properties:
|
|
|
|
// 1. Looking at any entry, it is clear whether it's an address or a bitmap:
|
|
|
|
// even means address, odd means bitmap.
|
|
|
|
// 2. Just a simple list of addresses is a valid encoding.
|
|
|
|
|
|
|
|
Elf_Rela Rela;
|
|
|
|
Rela.r_info = 0;
|
|
|
|
Rela.r_addend = 0;
|
2018-12-14 15:46:58 +08:00
|
|
|
Rela.setType(getRelativeRelocationType(), false);
|
2018-06-29 05:07:34 +08:00
|
|
|
std::vector<Elf_Rela> Relocs;
|
|
|
|
|
|
|
|
// Word type: uint32_t for Elf32, and uint64_t for Elf64.
|
|
|
|
typedef typename ELFT::uint Word;
|
|
|
|
|
|
|
|
// Word size in number of bytes.
|
|
|
|
const size_t WordSize = sizeof(Word);
|
|
|
|
|
|
|
|
// Number of bits used for the relocation offsets bitmap.
|
|
|
|
// These many relative relocations can be encoded in a single entry.
|
|
|
|
const size_t NBits = 8*WordSize - 1;
|
|
|
|
|
|
|
|
Word Base = 0;
|
|
|
|
for (const Elf_Relr &R : relrs) {
|
|
|
|
Word Entry = R;
|
|
|
|
if ((Entry&1) == 0) {
|
|
|
|
// Even entry: encodes the offset for next relocation.
|
|
|
|
Rela.r_offset = Entry;
|
|
|
|
Relocs.push_back(Rela);
|
|
|
|
// Set base offset for subsequent bitmap entries.
|
|
|
|
Base = Entry + WordSize;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Odd entry: encodes bitmap for relocations starting at base.
|
|
|
|
Word Offset = Base;
|
|
|
|
while (Entry != 0) {
|
|
|
|
Entry >>= 1;
|
|
|
|
if ((Entry&1) != 0) {
|
|
|
|
Rela.r_offset = Offset;
|
|
|
|
Relocs.push_back(Rela);
|
|
|
|
}
|
|
|
|
Offset += WordSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Advance base offset by NBits words.
|
|
|
|
Base += NBits * WordSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Relocs;
|
|
|
|
}
|
|
|
|
|
2017-10-25 11:37:12 +08:00
|
|
|
template <class ELFT>
|
|
|
|
Expected<std::vector<typename ELFT::Rela>>
|
|
|
|
ELFFile<ELFT>::android_relas(const Elf_Shdr *Sec) const {
|
|
|
|
// This function reads relocations in Android's packed relocation format,
|
|
|
|
// which is based on SLEB128 and delta encoding.
|
|
|
|
Expected<ArrayRef<uint8_t>> ContentsOrErr = getSectionContents(Sec);
|
|
|
|
if (!ContentsOrErr)
|
|
|
|
return ContentsOrErr.takeError();
|
|
|
|
const uint8_t *Cur = ContentsOrErr->begin();
|
|
|
|
const uint8_t *End = ContentsOrErr->end();
|
|
|
|
if (ContentsOrErr->size() < 4 || Cur[0] != 'A' || Cur[1] != 'P' ||
|
|
|
|
Cur[2] != 'S' || Cur[3] != '2')
|
|
|
|
return createError("invalid packed relocation header");
|
|
|
|
Cur += 4;
|
|
|
|
|
|
|
|
const char *ErrStr = nullptr;
|
|
|
|
auto ReadSLEB = [&]() -> int64_t {
|
|
|
|
if (ErrStr)
|
|
|
|
return 0;
|
|
|
|
unsigned Len;
|
|
|
|
int64_t Result = decodeSLEB128(Cur, &Len, End, &ErrStr);
|
|
|
|
Cur += Len;
|
|
|
|
return Result;
|
|
|
|
};
|
|
|
|
|
|
|
|
uint64_t NumRelocs = ReadSLEB();
|
|
|
|
uint64_t Offset = ReadSLEB();
|
|
|
|
uint64_t Addend = 0;
|
|
|
|
|
|
|
|
if (ErrStr)
|
|
|
|
return createError(ErrStr);
|
|
|
|
|
|
|
|
std::vector<Elf_Rela> Relocs;
|
|
|
|
Relocs.reserve(NumRelocs);
|
|
|
|
while (NumRelocs) {
|
|
|
|
uint64_t NumRelocsInGroup = ReadSLEB();
|
|
|
|
if (NumRelocsInGroup > NumRelocs)
|
|
|
|
return createError("relocation group unexpectedly large");
|
|
|
|
NumRelocs -= NumRelocsInGroup;
|
|
|
|
|
|
|
|
uint64_t GroupFlags = ReadSLEB();
|
|
|
|
bool GroupedByInfo = GroupFlags & ELF::RELOCATION_GROUPED_BY_INFO_FLAG;
|
|
|
|
bool GroupedByOffsetDelta = GroupFlags & ELF::RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG;
|
|
|
|
bool GroupedByAddend = GroupFlags & ELF::RELOCATION_GROUPED_BY_ADDEND_FLAG;
|
|
|
|
bool GroupHasAddend = GroupFlags & ELF::RELOCATION_GROUP_HAS_ADDEND_FLAG;
|
|
|
|
|
|
|
|
uint64_t GroupOffsetDelta;
|
|
|
|
if (GroupedByOffsetDelta)
|
|
|
|
GroupOffsetDelta = ReadSLEB();
|
|
|
|
|
|
|
|
uint64_t GroupRInfo;
|
|
|
|
if (GroupedByInfo)
|
|
|
|
GroupRInfo = ReadSLEB();
|
|
|
|
|
|
|
|
if (GroupedByAddend && GroupHasAddend)
|
|
|
|
Addend += ReadSLEB();
|
|
|
|
|
2018-08-16 01:58:22 +08:00
|
|
|
if (!GroupHasAddend)
|
|
|
|
Addend = 0;
|
|
|
|
|
2017-10-25 11:37:12 +08:00
|
|
|
for (uint64_t I = 0; I != NumRelocsInGroup; ++I) {
|
|
|
|
Elf_Rela R;
|
|
|
|
Offset += GroupedByOffsetDelta ? GroupOffsetDelta : ReadSLEB();
|
|
|
|
R.r_offset = Offset;
|
|
|
|
R.r_info = GroupedByInfo ? GroupRInfo : ReadSLEB();
|
2018-08-16 01:58:22 +08:00
|
|
|
if (GroupHasAddend && !GroupedByAddend)
|
|
|
|
Addend += ReadSLEB();
|
|
|
|
R.r_addend = Addend;
|
2017-10-25 11:37:12 +08:00
|
|
|
Relocs.push_back(R);
|
|
|
|
|
|
|
|
if (ErrStr)
|
|
|
|
return createError(ErrStr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ErrStr)
|
|
|
|
return createError(ErrStr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Relocs;
|
|
|
|
}
|
|
|
|
|
2018-07-25 19:09:20 +08:00
|
|
|
template <class ELFT>
|
2019-03-02 12:20:28 +08:00
|
|
|
std::string ELFFile<ELFT>::getDynamicTagAsString(unsigned Arch,
|
2018-07-25 19:09:20 +08:00
|
|
|
uint64_t Type) const {
|
|
|
|
#define DYNAMIC_STRINGIFY_ENUM(tag, value) \
|
|
|
|
case value: \
|
|
|
|
return #tag;
|
|
|
|
|
|
|
|
#define DYNAMIC_TAG(n, v)
|
|
|
|
switch (Arch) {
|
|
|
|
case ELF::EM_HEXAGON:
|
|
|
|
switch (Type) {
|
|
|
|
#define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
|
|
|
|
#include "llvm/BinaryFormat/DynamicTags.def"
|
|
|
|
#undef HEXAGON_DYNAMIC_TAG
|
|
|
|
}
|
2019-03-13 18:38:17 +08:00
|
|
|
break;
|
2018-07-25 19:09:20 +08:00
|
|
|
|
|
|
|
case ELF::EM_MIPS:
|
|
|
|
switch (Type) {
|
|
|
|
#define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
|
|
|
|
#include "llvm/BinaryFormat/DynamicTags.def"
|
|
|
|
#undef MIPS_DYNAMIC_TAG
|
|
|
|
}
|
2019-03-13 18:38:17 +08:00
|
|
|
break;
|
2018-07-25 19:09:20 +08:00
|
|
|
|
|
|
|
case ELF::EM_PPC64:
|
|
|
|
switch (Type) {
|
|
|
|
#define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
|
|
|
|
#include "llvm/BinaryFormat/DynamicTags.def"
|
|
|
|
#undef PPC64_DYNAMIC_TAG
|
|
|
|
}
|
2019-03-13 18:38:17 +08:00
|
|
|
break;
|
2018-07-25 19:09:20 +08:00
|
|
|
}
|
|
|
|
#undef DYNAMIC_TAG
|
|
|
|
switch (Type) {
|
|
|
|
// Now handle all dynamic tags except the architecture specific ones
|
|
|
|
#define MIPS_DYNAMIC_TAG(name, value)
|
|
|
|
#define HEXAGON_DYNAMIC_TAG(name, value)
|
|
|
|
#define PPC64_DYNAMIC_TAG(name, value)
|
|
|
|
// Also ignore marker tags such as DT_HIOS (maps to DT_VERNEEDNUM), etc.
|
|
|
|
#define DYNAMIC_TAG_MARKER(name, value)
|
|
|
|
#define DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
|
|
|
|
#include "llvm/BinaryFormat/DynamicTags.def"
|
|
|
|
#undef DYNAMIC_TAG
|
|
|
|
#undef MIPS_DYNAMIC_TAG
|
|
|
|
#undef HEXAGON_DYNAMIC_TAG
|
|
|
|
#undef PPC64_DYNAMIC_TAG
|
|
|
|
#undef DYNAMIC_TAG_MARKER
|
|
|
|
#undef DYNAMIC_STRINGIFY_ENUM
|
|
|
|
default:
|
2019-03-02 12:20:28 +08:00
|
|
|
return "<unknown:>0x" + utohexstr(Type, true);
|
2018-07-25 19:09:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2019-03-02 12:20:28 +08:00
|
|
|
std::string ELFFile<ELFT>::getDynamicTagAsString(uint64_t Type) const {
|
2018-07-25 19:09:20 +08:00
|
|
|
return getDynamicTagAsString(getHeader()->e_machine, Type);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
Expected<typename ELFT::DynRange> ELFFile<ELFT>::dynamicEntries() const {
|
|
|
|
ArrayRef<Elf_Dyn> Dyn;
|
|
|
|
size_t DynSecSize = 0;
|
|
|
|
|
|
|
|
auto ProgramHeadersOrError = program_headers();
|
|
|
|
if (!ProgramHeadersOrError)
|
|
|
|
return ProgramHeadersOrError.takeError();
|
|
|
|
|
|
|
|
for (const Elf_Phdr &Phdr : *ProgramHeadersOrError) {
|
|
|
|
if (Phdr.p_type == ELF::PT_DYNAMIC) {
|
|
|
|
Dyn = makeArrayRef(
|
|
|
|
reinterpret_cast<const Elf_Dyn *>(base() + Phdr.p_offset),
|
|
|
|
Phdr.p_filesz / sizeof(Elf_Dyn));
|
|
|
|
DynSecSize = Phdr.p_filesz;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we can't find the dynamic section in the program headers, we just fall
|
|
|
|
// back on the sections.
|
|
|
|
if (Dyn.empty()) {
|
|
|
|
auto SectionsOrError = sections();
|
|
|
|
if (!SectionsOrError)
|
|
|
|
return SectionsOrError.takeError();
|
|
|
|
|
|
|
|
for (const Elf_Shdr &Sec : *SectionsOrError) {
|
|
|
|
if (Sec.sh_type == ELF::SHT_DYNAMIC) {
|
|
|
|
Expected<ArrayRef<Elf_Dyn>> DynOrError =
|
|
|
|
getSectionContentsAsArray<Elf_Dyn>(&Sec);
|
|
|
|
if (!DynOrError)
|
|
|
|
return DynOrError.takeError();
|
|
|
|
Dyn = *DynOrError;
|
|
|
|
DynSecSize = Sec.sh_size;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Dyn.data())
|
|
|
|
return ArrayRef<Elf_Dyn>();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Dyn.empty())
|
|
|
|
return createError("invalid empty dynamic section");
|
|
|
|
|
|
|
|
if (DynSecSize % sizeof(Elf_Dyn) != 0)
|
|
|
|
return createError("malformed dynamic section");
|
|
|
|
|
|
|
|
if (Dyn.back().d_tag != ELF::DT_NULL)
|
|
|
|
return createError("dynamic sections must be DT_NULL terminated");
|
|
|
|
|
|
|
|
return Dyn;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
Expected<const uint8_t *> ELFFile<ELFT>::toMappedAddr(uint64_t VAddr) const {
|
|
|
|
auto ProgramHeadersOrError = program_headers();
|
|
|
|
if (!ProgramHeadersOrError)
|
|
|
|
return ProgramHeadersOrError.takeError();
|
|
|
|
|
|
|
|
llvm::SmallVector<Elf_Phdr *, 4> LoadSegments;
|
|
|
|
|
|
|
|
for (const Elf_Phdr &Phdr : *ProgramHeadersOrError)
|
|
|
|
if (Phdr.p_type == ELF::PT_LOAD)
|
|
|
|
LoadSegments.push_back(const_cast<Elf_Phdr *>(&Phdr));
|
|
|
|
|
|
|
|
const Elf_Phdr *const *I =
|
|
|
|
std::upper_bound(LoadSegments.begin(), LoadSegments.end(), VAddr,
|
|
|
|
[](uint64_t VAddr, const Elf_Phdr_Impl<ELFT> *Phdr) {
|
|
|
|
return VAddr < Phdr->p_vaddr;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (I == LoadSegments.begin())
|
|
|
|
return createError("Virtual address is not in any segment");
|
|
|
|
--I;
|
|
|
|
const Elf_Phdr &Phdr = **I;
|
|
|
|
uint64_t Delta = VAddr - Phdr.p_vaddr;
|
|
|
|
if (Delta >= Phdr.p_filesz)
|
|
|
|
return createError("Virtual address is not in any segment");
|
|
|
|
return base() + Phdr.p_offset + Delta;
|
|
|
|
}
|
|
|
|
|
2017-10-25 11:37:12 +08:00
|
|
|
template class llvm::object::ELFFile<ELF32LE>;
|
|
|
|
template class llvm::object::ELFFile<ELF32BE>;
|
|
|
|
template class llvm::object::ELFFile<ELF64LE>;
|
|
|
|
template class llvm::object::ELFFile<ELF64BE>;
|