xref: /freebsd/contrib/llvm-project/lld/ELF/Relocations.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===- Relocations.h -------------------------------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #ifndef LLD_ELF_RELOCATIONS_H
100b57cec5SDimitry Andric #define LLD_ELF_RELOCATIONS_H
110b57cec5SDimitry Andric 
120b57cec5SDimitry Andric #include "lld/Common/LLVM.h"
130b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h"
1404eeddc0SDimitry Andric #include "llvm/ADT/STLExtras.h"
150b57cec5SDimitry Andric #include <vector>
160b57cec5SDimitry Andric 
17bdd1243dSDimitry Andric namespace lld::elf {
180b57cec5SDimitry Andric class Symbol;
190b57cec5SDimitry Andric class InputSection;
200b57cec5SDimitry Andric class InputSectionBase;
210b57cec5SDimitry Andric class OutputSection;
220b57cec5SDimitry Andric class SectionBase;
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric // Represents a relocation type, such as R_X86_64_PC32 or R_ARM_THM_CALL.
250b57cec5SDimitry Andric using RelType = uint32_t;
265ffd83dbSDimitry Andric using JumpModType = uint32_t;
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric // List of target-independent relocation types. Relocations read
290b57cec5SDimitry Andric // from files are converted to these types so that the main code
300b57cec5SDimitry Andric // doesn't have to know about architecture-specific details.
310b57cec5SDimitry Andric enum RelExpr {
320b57cec5SDimitry Andric   R_ABS,
330b57cec5SDimitry Andric   R_ADDEND,
340b57cec5SDimitry Andric   R_DTPREL,
350b57cec5SDimitry Andric   R_GOT,
360b57cec5SDimitry Andric   R_GOT_OFF,
370b57cec5SDimitry Andric   R_GOT_PC,
380b57cec5SDimitry Andric   R_GOTONLY_PC,
390b57cec5SDimitry Andric   R_GOTPLTONLY_PC,
400b57cec5SDimitry Andric   R_GOTPLT,
410b57cec5SDimitry Andric   R_GOTPLTREL,
420b57cec5SDimitry Andric   R_GOTREL,
4374626c16SDimitry Andric   R_GOTPLT_GOTREL,
4474626c16SDimitry Andric   R_GOTPLT_PC,
450b57cec5SDimitry Andric   R_NONE,
460b57cec5SDimitry Andric   R_PC,
470b57cec5SDimitry Andric   R_PLT,
480b57cec5SDimitry Andric   R_PLT_PC,
49349cc55cSDimitry Andric   R_PLT_GOTPLT,
5074626c16SDimitry Andric   R_PLT_GOTREL,
51753f127fSDimitry Andric   R_RELAX_HINT,
520b57cec5SDimitry Andric   R_RELAX_GOT_PC,
530b57cec5SDimitry Andric   R_RELAX_GOT_PC_NOPIC,
540b57cec5SDimitry Andric   R_RELAX_TLS_GD_TO_IE,
550b57cec5SDimitry Andric   R_RELAX_TLS_GD_TO_IE_ABS,
560b57cec5SDimitry Andric   R_RELAX_TLS_GD_TO_IE_GOT_OFF,
570b57cec5SDimitry Andric   R_RELAX_TLS_GD_TO_IE_GOTPLT,
580b57cec5SDimitry Andric   R_RELAX_TLS_GD_TO_LE,
590b57cec5SDimitry Andric   R_RELAX_TLS_GD_TO_LE_NEG,
600b57cec5SDimitry Andric   R_RELAX_TLS_IE_TO_LE,
610b57cec5SDimitry Andric   R_RELAX_TLS_LD_TO_LE,
620b57cec5SDimitry Andric   R_RELAX_TLS_LD_TO_LE_ABS,
630b57cec5SDimitry Andric   R_SIZE,
64e8d8bef9SDimitry Andric   R_TPREL,
65e8d8bef9SDimitry Andric   R_TPREL_NEG,
660b57cec5SDimitry Andric   R_TLSDESC,
670b57cec5SDimitry Andric   R_TLSDESC_CALL,
680b57cec5SDimitry Andric   R_TLSDESC_PC,
69349cc55cSDimitry Andric   R_TLSDESC_GOTPLT,
700b57cec5SDimitry Andric   R_TLSGD_GOT,
710b57cec5SDimitry Andric   R_TLSGD_GOTPLT,
720b57cec5SDimitry Andric   R_TLSGD_PC,
730b57cec5SDimitry Andric   R_TLSIE_HINT,
740b57cec5SDimitry Andric   R_TLSLD_GOT,
750b57cec5SDimitry Andric   R_TLSLD_GOTPLT,
760b57cec5SDimitry Andric   R_TLSLD_GOT_OFF,
770b57cec5SDimitry Andric   R_TLSLD_HINT,
780b57cec5SDimitry Andric   R_TLSLD_PC,
790b57cec5SDimitry Andric 
800b57cec5SDimitry Andric   // The following is abstract relocation types used for only one target.
810b57cec5SDimitry Andric   //
820b57cec5SDimitry Andric   // Even though RelExpr is intended to be a target-neutral representation
830b57cec5SDimitry Andric   // of a relocation type, there are some relocations whose semantics are
840b57cec5SDimitry Andric   // unique to a target. Such relocation are marked with R_<TARGET_NAME>.
850b57cec5SDimitry Andric   R_AARCH64_GOT_PAGE_PC,
86e8d8bef9SDimitry Andric   R_AARCH64_GOT_PAGE,
870b57cec5SDimitry Andric   R_AARCH64_PAGE_PC,
880b57cec5SDimitry Andric   R_AARCH64_RELAX_TLS_GD_TO_IE_PAGE_PC,
890b57cec5SDimitry Andric   R_AARCH64_TLSDESC_PAGE,
90*0fca6ea1SDimitry Andric   R_AARCH64_AUTH,
915ffd83dbSDimitry Andric   R_ARM_PCA,
920b57cec5SDimitry Andric   R_ARM_SBREL,
930b57cec5SDimitry Andric   R_MIPS_GOTREL,
940b57cec5SDimitry Andric   R_MIPS_GOT_GP,
950b57cec5SDimitry Andric   R_MIPS_GOT_GP_PC,
960b57cec5SDimitry Andric   R_MIPS_GOT_LOCAL_PAGE,
970b57cec5SDimitry Andric   R_MIPS_GOT_OFF,
980b57cec5SDimitry Andric   R_MIPS_GOT_OFF32,
990b57cec5SDimitry Andric   R_MIPS_TLSGD,
1000b57cec5SDimitry Andric   R_MIPS_TLSLD,
1010b57cec5SDimitry Andric   R_PPC32_PLTREL,
1020b57cec5SDimitry Andric   R_PPC64_CALL,
1030b57cec5SDimitry Andric   R_PPC64_CALL_PLT,
1040b57cec5SDimitry Andric   R_PPC64_RELAX_TOC,
1050b57cec5SDimitry Andric   R_PPC64_TOCBASE,
106e8d8bef9SDimitry Andric   R_PPC64_RELAX_GOT_PC,
1070b57cec5SDimitry Andric   R_RISCV_ADD,
1085f757f3fSDimitry Andric   R_RISCV_LEB128,
1090b57cec5SDimitry Andric   R_RISCV_PC_INDIRECT,
11006c3fb27SDimitry Andric   // Same as R_PC but with page-aligned semantics.
11106c3fb27SDimitry Andric   R_LOONGARCH_PAGE_PC,
11206c3fb27SDimitry Andric   // Same as R_PLT_PC but with page-aligned semantics.
11306c3fb27SDimitry Andric   R_LOONGARCH_PLT_PAGE_PC,
11406c3fb27SDimitry Andric   // In addition to having page-aligned semantics, LoongArch GOT relocs are
11506c3fb27SDimitry Andric   // also reused for TLS, making the semantics differ from other architectures.
11606c3fb27SDimitry Andric   R_LOONGARCH_GOT,
11706c3fb27SDimitry Andric   R_LOONGARCH_GOT_PAGE_PC,
11806c3fb27SDimitry Andric   R_LOONGARCH_TLSGD_PAGE_PC,
119*0fca6ea1SDimitry Andric   R_LOONGARCH_TLSDESC_PAGE_PC,
1200b57cec5SDimitry Andric };
1210b57cec5SDimitry Andric 
1220b57cec5SDimitry Andric // Architecture-neutral representation of relocation.
1230b57cec5SDimitry Andric struct Relocation {
1240b57cec5SDimitry Andric   RelExpr expr;
1250b57cec5SDimitry Andric   RelType type;
1260b57cec5SDimitry Andric   uint64_t offset;
1270b57cec5SDimitry Andric   int64_t addend;
1280b57cec5SDimitry Andric   Symbol *sym;
1290b57cec5SDimitry Andric };
1300b57cec5SDimitry Andric 
1315ffd83dbSDimitry Andric // Manipulate jump instructions with these modifiers.  These are used to relax
1325ffd83dbSDimitry Andric // jump instruction opcodes at basic block boundaries and are particularly
1335ffd83dbSDimitry Andric // useful when basic block sections are enabled.
1345ffd83dbSDimitry Andric struct JumpInstrMod {
1355ffd83dbSDimitry Andric   uint64_t offset;
13604eeddc0SDimitry Andric   JumpModType original;
1375ffd83dbSDimitry Andric   unsigned size;
1385ffd83dbSDimitry Andric };
1395ffd83dbSDimitry Andric 
1400b57cec5SDimitry Andric // This function writes undefined symbol diagnostics to an internal buffer.
1410b57cec5SDimitry Andric // Call reportUndefinedSymbols() after calling scanRelocations() to emit
1420b57cec5SDimitry Andric // the diagnostics.
143bdd1243dSDimitry Andric template <class ELFT> void scanRelocations();
144*0fca6ea1SDimitry Andric template <class ELFT> void checkNoCrossRefs();
14581ad6265SDimitry Andric void reportUndefinedSymbols();
1460eae32dcSDimitry Andric void postScanRelocations();
1475f757f3fSDimitry Andric void addGotEntry(Symbol &sym);
1480b57cec5SDimitry Andric 
1495ffd83dbSDimitry Andric void hexagonTLSSymbolUpdate(ArrayRef<OutputSection *> outputSections);
1505ffd83dbSDimitry Andric bool hexagonNeedsTLSSymbol(ArrayRef<OutputSection *> outputSections);
1515ffd83dbSDimitry Andric 
1520b57cec5SDimitry Andric class ThunkSection;
1530b57cec5SDimitry Andric class Thunk;
154e8d8bef9SDimitry Andric class InputSectionDescription;
1550b57cec5SDimitry Andric 
1560b57cec5SDimitry Andric class ThunkCreator {
1570b57cec5SDimitry Andric public:
1580b57cec5SDimitry Andric   // Return true if Thunks have been added to OutputSections
159753f127fSDimitry Andric   bool createThunks(uint32_t pass, ArrayRef<OutputSection *> outputSections);
1600b57cec5SDimitry Andric 
1610b57cec5SDimitry Andric private:
1620b57cec5SDimitry Andric   void mergeThunks(ArrayRef<OutputSection *> outputSections);
1630b57cec5SDimitry Andric 
1640b57cec5SDimitry Andric   ThunkSection *getISDThunkSec(OutputSection *os, InputSection *isec,
165fe6060f1SDimitry Andric                                InputSectionDescription *isd,
166fe6060f1SDimitry Andric                                const Relocation &rel, uint64_t src);
1670b57cec5SDimitry Andric 
1680b57cec5SDimitry Andric   ThunkSection *getISThunkSec(InputSection *isec);
1690b57cec5SDimitry Andric 
1700b57cec5SDimitry Andric   void createInitialThunkSections(ArrayRef<OutputSection *> outputSections);
1710b57cec5SDimitry Andric 
1720b57cec5SDimitry Andric   std::pair<Thunk *, bool> getThunk(InputSection *isec, Relocation &rel,
1730b57cec5SDimitry Andric                                     uint64_t src);
1740b57cec5SDimitry Andric 
1750b57cec5SDimitry Andric   ThunkSection *addThunkSection(OutputSection *os, InputSectionDescription *,
1760b57cec5SDimitry Andric                                 uint64_t off);
1770b57cec5SDimitry Andric 
1780b57cec5SDimitry Andric   bool normalizeExistingThunk(Relocation &rel, uint64_t src);
1790b57cec5SDimitry Andric 
180480093f4SDimitry Andric   // Record all the available Thunks for a (Symbol, addend) pair, where Symbol
181480093f4SDimitry Andric   // is represented as a (section, offset) pair. There may be multiple
182480093f4SDimitry Andric   // relocations sharing the same (section, offset + addend) pair. We may revert
183480093f4SDimitry Andric   // a relocation back to its original non-Thunk target, and restore the
184480093f4SDimitry Andric   // original addend, so we cannot fold offset + addend. A nested pair is used
185480093f4SDimitry Andric   // because DenseMapInfo is not specialized for std::tuple.
186480093f4SDimitry Andric   llvm::DenseMap<std::pair<std::pair<SectionBase *, uint64_t>, int64_t>,
187480093f4SDimitry Andric                  std::vector<Thunk *>>
188480093f4SDimitry Andric       thunkedSymbolsBySectionAndAddend;
189480093f4SDimitry Andric   llvm::DenseMap<std::pair<Symbol *, int64_t>, std::vector<Thunk *>>
190480093f4SDimitry Andric       thunkedSymbols;
1910b57cec5SDimitry Andric 
1920b57cec5SDimitry Andric   // Find a Thunk from the Thunks symbol definition, we can use this to find
1930b57cec5SDimitry Andric   // the Thunk from a relocation to the Thunks symbol definition.
1940b57cec5SDimitry Andric   llvm::DenseMap<Symbol *, Thunk *> thunks;
1950b57cec5SDimitry Andric 
1960b57cec5SDimitry Andric   // Track InputSections that have an inline ThunkSection placed in front
1970b57cec5SDimitry Andric   // an inline ThunkSection may have control fall through to the section below
1980b57cec5SDimitry Andric   // so we need to make sure that there is only one of them.
1990b57cec5SDimitry Andric   // The Mips LA25 Thunk is an example of an inline ThunkSection.
2000b57cec5SDimitry Andric   llvm::DenseMap<InputSection *, ThunkSection *> thunkedSections;
201753f127fSDimitry Andric 
202753f127fSDimitry Andric   // The number of completed passes of createThunks this permits us
203753f127fSDimitry Andric   // to do one time initialization on Pass 0 and put a limit on the
204753f127fSDimitry Andric   // number of times it can be called to prevent infinite loops.
205753f127fSDimitry Andric   uint32_t pass = 0;
2060b57cec5SDimitry Andric };
2070b57cec5SDimitry Andric 
2080b57cec5SDimitry Andric // Return a int64_t to make sure we get the sign extension out of the way as
2090b57cec5SDimitry Andric // early as possible.
2100b57cec5SDimitry Andric template <class ELFT>
2110b57cec5SDimitry Andric static inline int64_t getAddend(const typename ELFT::Rel &rel) {
2120b57cec5SDimitry Andric   return 0;
2130b57cec5SDimitry Andric }
2140b57cec5SDimitry Andric template <class ELFT>
2150b57cec5SDimitry Andric static inline int64_t getAddend(const typename ELFT::Rela &rel) {
2160b57cec5SDimitry Andric   return rel.r_addend;
2170b57cec5SDimitry Andric }
218fe6060f1SDimitry Andric 
219fe6060f1SDimitry Andric template <typename RelTy>
220fe6060f1SDimitry Andric ArrayRef<RelTy> sortRels(ArrayRef<RelTy> rels, SmallVector<RelTy, 0> &storage) {
221fe6060f1SDimitry Andric   auto cmp = [](const RelTy &a, const RelTy &b) {
222fe6060f1SDimitry Andric     return a.r_offset < b.r_offset;
223fe6060f1SDimitry Andric   };
224fe6060f1SDimitry Andric   if (!llvm::is_sorted(rels, cmp)) {
225fe6060f1SDimitry Andric     storage.assign(rels.begin(), rels.end());
226fe6060f1SDimitry Andric     llvm::stable_sort(storage, cmp);
227fe6060f1SDimitry Andric     rels = storage;
228fe6060f1SDimitry Andric   }
229fe6060f1SDimitry Andric   return rels;
230fe6060f1SDimitry Andric }
2315f757f3fSDimitry Andric 
2325f757f3fSDimitry Andric // Returns true if Expr refers a GOT entry. Note that this function returns
2335f757f3fSDimitry Andric // false for TLS variables even though they need GOT, because TLS variables uses
2345f757f3fSDimitry Andric // GOT differently than the regular variables.
2355f757f3fSDimitry Andric bool needsGot(RelExpr expr);
236bdd1243dSDimitry Andric } // namespace lld::elf
2370b57cec5SDimitry Andric 
2380b57cec5SDimitry Andric #endif
239