2016-05-25 04:24:43 +08:00
|
|
|
//===- Relocations.h -------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLD_ELF_RELOCATIONS_H
|
|
|
|
#define LLD_ELF_RELOCATIONS_H
|
|
|
|
|
2017-10-03 05:00:41 +08:00
|
|
|
#include "lld/Common/LLVM.h"
|
2017-04-05 18:30:09 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
2016-05-25 04:24:43 +08:00
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
namespace elf {
|
2017-11-04 05:21:47 +08:00
|
|
|
class Symbol;
|
2017-02-24 00:49:07 +08:00
|
|
|
class InputSection;
|
2017-02-23 10:28:28 +08:00
|
|
|
class InputSectionBase;
|
2017-02-24 23:07:30 +08:00
|
|
|
class OutputSection;
|
2018-03-10 01:54:43 +08:00
|
|
|
class SectionBase;
|
2016-05-25 04:24:43 +08:00
|
|
|
|
2017-10-12 06:49:24 +08:00
|
|
|
// Represents a relocation type, such as R_X86_64_PC32 or R_ARM_THM_CALL.
|
|
|
|
typedef uint32_t RelType;
|
|
|
|
|
2016-10-21 12:52:11 +08:00
|
|
|
// List of target-independent relocation types. Relocations read
|
|
|
|
// from files are converted to these types so that the main code
|
|
|
|
// doesn't have to know about architecture-specific details.
|
2016-05-25 04:24:43 +08:00
|
|
|
enum RelExpr {
|
2017-10-12 11:14:06 +08:00
|
|
|
R_INVALID,
|
2016-05-25 04:24:43 +08:00
|
|
|
R_ABS,
|
2018-02-16 18:01:17 +08:00
|
|
|
R_ADDEND,
|
2017-05-18 17:12:21 +08:00
|
|
|
R_ARM_SBREL,
|
2016-05-25 04:24:43 +08:00
|
|
|
R_GOT,
|
|
|
|
R_GOTONLY_PC,
|
2016-09-01 07:24:11 +08:00
|
|
|
R_GOTONLY_PC_FROM_END,
|
2016-05-25 04:24:43 +08:00
|
|
|
R_GOTREL,
|
2016-09-01 07:24:11 +08:00
|
|
|
R_GOTREL_FROM_END,
|
2016-05-25 04:24:43 +08:00
|
|
|
R_GOT_FROM_END,
|
|
|
|
R_GOT_OFF,
|
|
|
|
R_GOT_PAGE_PC,
|
|
|
|
R_GOT_PC,
|
|
|
|
R_HINT,
|
2016-11-17 05:01:02 +08:00
|
|
|
R_MIPS_GOTREL,
|
2017-03-21 05:03:43 +08:00
|
|
|
R_MIPS_GOT_GP,
|
|
|
|
R_MIPS_GOT_GP_PC,
|
2017-03-26 12:10:43 +08:00
|
|
|
R_MIPS_GOT_LOCAL_PAGE,
|
|
|
|
R_MIPS_GOT_OFF,
|
|
|
|
R_MIPS_GOT_OFF32,
|
2016-06-23 23:26:31 +08:00
|
|
|
R_MIPS_TLSGD,
|
|
|
|
R_MIPS_TLSLD,
|
2016-05-25 04:24:43 +08:00
|
|
|
R_NEG_TLS,
|
2017-02-23 14:22:28 +08:00
|
|
|
R_NONE,
|
2016-05-25 04:24:43 +08:00
|
|
|
R_PAGE_PC,
|
|
|
|
R_PC,
|
|
|
|
R_PLT,
|
2016-06-05 03:11:14 +08:00
|
|
|
R_PLT_PAGE_PC,
|
2017-03-26 12:10:43 +08:00
|
|
|
R_PLT_PC,
|
2016-05-25 04:24:43 +08:00
|
|
|
R_PPC_OPD,
|
|
|
|
R_PPC_PLT_OPD,
|
|
|
|
R_PPC_TOC,
|
2016-05-25 22:31:37 +08:00
|
|
|
R_RELAX_GOT_PC,
|
[ELF] - Implemented support for test/binop relaxations from latest ABI.
Patch implements next relaxation from latest ABI:
"Convert memory operand of test and binop into immediate operand, where binop is one of adc, add, and, cmp, or,
sbb, sub, xor instructions, when position-independent code is disabled."
It is described in System V Application Binary Interface AMD64 Architecture Processor
Supplement Draft Version 0.99.8 (https://github.com/hjl-tools/x86-psABI/wiki/x86-64-psABI-r249.pdf,
B.2 "B.2 Optimize GOTPCRELX Relocations").
Differential revision: http://reviews.llvm.org/D20793
llvm-svn: 271405
2016-06-02 00:45:30 +08:00
|
|
|
R_RELAX_GOT_PC_NOPIC,
|
2016-05-25 04:24:43 +08:00
|
|
|
R_RELAX_TLS_GD_TO_IE,
|
2016-06-05 07:33:31 +08:00
|
|
|
R_RELAX_TLS_GD_TO_IE_ABS,
|
2017-03-26 12:10:43 +08:00
|
|
|
R_RELAX_TLS_GD_TO_IE_END,
|
2016-06-05 07:33:31 +08:00
|
|
|
R_RELAX_TLS_GD_TO_IE_PAGE_PC,
|
2016-05-25 04:24:43 +08:00
|
|
|
R_RELAX_TLS_GD_TO_LE,
|
2016-06-05 07:22:34 +08:00
|
|
|
R_RELAX_TLS_GD_TO_LE_NEG,
|
2016-05-25 04:24:43 +08:00
|
|
|
R_RELAX_TLS_IE_TO_LE,
|
|
|
|
R_RELAX_TLS_LD_TO_LE,
|
|
|
|
R_SIZE,
|
|
|
|
R_TLS,
|
2016-06-03 03:49:53 +08:00
|
|
|
R_TLSDESC,
|
2016-10-20 17:59:26 +08:00
|
|
|
R_TLSDESC_CALL,
|
2017-03-26 12:10:43 +08:00
|
|
|
R_TLSDESC_PAGE,
|
2016-05-25 04:24:43 +08:00
|
|
|
R_TLSGD,
|
|
|
|
R_TLSGD_PC,
|
|
|
|
R_TLSLD,
|
2016-10-21 12:52:11 +08:00
|
|
|
R_TLSLD_PC,
|
2016-05-25 04:24:43 +08:00
|
|
|
};
|
|
|
|
|
2016-12-01 13:43:48 +08:00
|
|
|
// Build a bitmask with one bit set for each RelExpr.
|
|
|
|
//
|
|
|
|
// Constexpr function arguments can't be used in static asserts, so we
|
|
|
|
// use template arguments to build the mask.
|
|
|
|
// But function template partial specializations don't exist (needed
|
|
|
|
// for base case of the recursion), so we need a dummy struct.
|
|
|
|
template <RelExpr... Exprs> struct RelExprMaskBuilder {
|
|
|
|
static inline uint64_t build() { return 0; }
|
|
|
|
};
|
|
|
|
|
|
|
|
// Specialization for recursive case.
|
|
|
|
template <RelExpr Head, RelExpr... Tail>
|
|
|
|
struct RelExprMaskBuilder<Head, Tail...> {
|
|
|
|
static inline uint64_t build() {
|
|
|
|
static_assert(0 <= Head && Head < 64,
|
|
|
|
"RelExpr is too large for 64-bit mask!");
|
|
|
|
return (uint64_t(1) << Head) | RelExprMaskBuilder<Tail...>::build();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Return true if `Expr` is one of `Exprs`.
|
|
|
|
// There are fewer than 64 RelExpr's, so we can represent any set of
|
|
|
|
// RelExpr's as a constant bit mask and test for membership with a
|
|
|
|
// couple cheap bitwise operations.
|
|
|
|
template <RelExpr... Exprs> bool isRelExprOneOf(RelExpr Expr) {
|
2017-07-18 19:55:35 +08:00
|
|
|
assert(0 <= Expr && (int)Expr < 64 &&
|
|
|
|
"RelExpr is too large for 64-bit mask!");
|
2016-12-01 13:43:48 +08:00
|
|
|
return (uint64_t(1) << Expr) & RelExprMaskBuilder<Exprs...>::build();
|
|
|
|
}
|
|
|
|
|
2016-10-21 12:52:11 +08:00
|
|
|
// Architecture-neutral representation of relocation.
|
2016-09-08 04:37:34 +08:00
|
|
|
struct Relocation {
|
2016-05-25 04:24:43 +08:00
|
|
|
RelExpr Expr;
|
2017-10-12 06:49:24 +08:00
|
|
|
RelType Type;
|
2016-05-25 04:24:43 +08:00
|
|
|
uint64_t Offset;
|
2017-02-16 08:12:34 +08:00
|
|
|
int64_t Addend;
|
2017-11-04 05:21:47 +08:00
|
|
|
Symbol *Sym;
|
2016-05-25 04:24:43 +08:00
|
|
|
};
|
|
|
|
|
2017-02-23 10:28:28 +08:00
|
|
|
template <class ELFT> void scanRelocations(InputSectionBase &);
|
2016-07-02 16:50:03 +08:00
|
|
|
|
2017-04-05 18:30:09 +08:00
|
|
|
class ThunkSection;
|
|
|
|
class Thunk;
|
2017-10-27 16:56:20 +08:00
|
|
|
struct InputSectionDescription;
|
2017-04-05 18:30:09 +08:00
|
|
|
|
2017-05-17 15:10:59 +08:00
|
|
|
class ThunkCreator {
|
2017-04-05 18:30:09 +08:00
|
|
|
public:
|
|
|
|
// Return true if Thunks have been added to OutputSections
|
2017-07-28 03:22:43 +08:00
|
|
|
bool createThunks(ArrayRef<OutputSection *> OutputSections);
|
2017-04-05 18:30:09 +08:00
|
|
|
|
2017-06-16 21:10:08 +08:00
|
|
|
// The number of completed passes of createThunks this permits us
|
|
|
|
// to do one time initialization on Pass 0 and put a limit on the
|
|
|
|
// number of times it can be called to prevent infinite loops.
|
|
|
|
uint32_t Pass = 0;
|
|
|
|
|
2017-04-05 18:30:09 +08:00
|
|
|
private:
|
2017-10-27 16:56:20 +08:00
|
|
|
void mergeThunks(ArrayRef<OutputSection *> OutputSections);
|
2017-10-27 17:04:11 +08:00
|
|
|
|
|
|
|
ThunkSection *getISDThunkSec(OutputSection *OS, InputSection *IS,
|
|
|
|
InputSectionDescription *ISD, uint32_t Type,
|
|
|
|
uint64_t Src);
|
|
|
|
|
2017-09-12 17:17:39 +08:00
|
|
|
ThunkSection *getISThunkSec(InputSection *IS);
|
2017-10-27 16:56:20 +08:00
|
|
|
|
2017-10-27 16:58:28 +08:00
|
|
|
void createInitialThunkSections(ArrayRef<OutputSection *> OutputSections);
|
|
|
|
|
2017-10-27 16:56:20 +08:00
|
|
|
void forEachInputSectionDescription(
|
2017-07-28 03:22:43 +08:00
|
|
|
ArrayRef<OutputSection *> OutputSections,
|
2017-10-27 16:56:20 +08:00
|
|
|
std::function<void(OutputSection *, InputSectionDescription *)> Fn);
|
|
|
|
|
2017-11-04 08:31:04 +08:00
|
|
|
std::pair<Thunk *, bool> getThunk(Symbol &Sym, RelType Type, uint64_t Src);
|
2017-10-27 16:58:28 +08:00
|
|
|
|
2017-10-27 16:56:20 +08:00
|
|
|
ThunkSection *addThunkSection(OutputSection *OS, InputSectionDescription *,
|
|
|
|
uint64_t Off);
|
2017-10-27 17:07:10 +08:00
|
|
|
|
|
|
|
bool normalizeExistingThunk(Relocation &Rel, uint64_t Src);
|
2017-10-27 16:58:28 +08:00
|
|
|
|
2017-07-05 17:53:33 +08:00
|
|
|
// Record all the available Thunks for a Symbol
|
2018-03-10 01:54:43 +08:00
|
|
|
llvm::DenseMap<std::pair<SectionBase *, uint64_t>, std::vector<Thunk *>>
|
|
|
|
ThunkedSymbolsBySection;
|
2017-11-04 05:21:47 +08:00
|
|
|
llvm::DenseMap<Symbol *, std::vector<Thunk *>> ThunkedSymbols;
|
2017-04-05 18:30:09 +08:00
|
|
|
|
2017-06-16 21:10:08 +08:00
|
|
|
// Find a Thunk from the Thunks symbol definition, we can use this to find
|
|
|
|
// the Thunk from a relocation to the Thunks symbol definition.
|
2017-11-04 05:21:47 +08:00
|
|
|
llvm::DenseMap<Symbol *, Thunk *> Thunks;
|
2017-06-16 21:10:08 +08:00
|
|
|
|
2017-07-05 17:53:33 +08:00
|
|
|
// Track InputSections that have an inline ThunkSection placed in front
|
|
|
|
// an inline ThunkSection may have control fall through to the section below
|
|
|
|
// so we need to make sure that there is only one of them.
|
|
|
|
// The Mips LA25 Thunk is an example of an inline ThunkSection.
|
2017-04-05 18:30:09 +08:00
|
|
|
llvm::DenseMap<InputSection *, ThunkSection *> ThunkedSections;
|
|
|
|
};
|
2016-07-21 01:58:07 +08:00
|
|
|
|
2017-02-16 08:12:34 +08:00
|
|
|
// Return a int64_t to make sure we get the sign extension out of the way as
|
|
|
|
// early as possible.
|
2016-07-02 16:50:03 +08:00
|
|
|
template <class ELFT>
|
2017-02-16 08:12:34 +08:00
|
|
|
static inline int64_t getAddend(const typename ELFT::Rel &Rel) {
|
2016-07-02 16:50:03 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
template <class ELFT>
|
2017-02-16 08:12:34 +08:00
|
|
|
static inline int64_t getAddend(const typename ELFT::Rela &Rel) {
|
2016-07-02 16:50:03 +08:00
|
|
|
return Rel.r_addend;
|
|
|
|
}
|
2017-07-18 19:55:35 +08:00
|
|
|
} // namespace elf
|
|
|
|
} // namespace lld
|
2016-05-25 04:24:43 +08:00
|
|
|
|
|
|
|
#endif
|