xref: /freebsd/contrib/llvm-project/lld/ELF/Relocations.h (revision 480093f4440d54b30b3025afeac24b48f2ba7a2e)
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"
140b57cec5SDimitry Andric #include <map>
150b57cec5SDimitry Andric #include <vector>
160b57cec5SDimitry Andric 
170b57cec5SDimitry Andric namespace lld {
180b57cec5SDimitry Andric namespace elf {
190b57cec5SDimitry Andric class Symbol;
200b57cec5SDimitry Andric class InputSection;
210b57cec5SDimitry Andric class InputSectionBase;
220b57cec5SDimitry Andric class OutputSection;
230b57cec5SDimitry Andric class SectionBase;
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric // Represents a relocation type, such as R_X86_64_PC32 or R_ARM_THM_CALL.
260b57cec5SDimitry Andric using RelType = 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,
430b57cec5SDimitry Andric   R_NEG_TLS,
440b57cec5SDimitry Andric   R_NONE,
450b57cec5SDimitry Andric   R_PC,
460b57cec5SDimitry Andric   R_PLT,
470b57cec5SDimitry Andric   R_PLT_PC,
480b57cec5SDimitry Andric   R_RELAX_GOT_PC,
490b57cec5SDimitry Andric   R_RELAX_GOT_PC_NOPIC,
500b57cec5SDimitry Andric   R_RELAX_TLS_GD_TO_IE,
510b57cec5SDimitry Andric   R_RELAX_TLS_GD_TO_IE_ABS,
520b57cec5SDimitry Andric   R_RELAX_TLS_GD_TO_IE_GOT_OFF,
530b57cec5SDimitry Andric   R_RELAX_TLS_GD_TO_IE_GOTPLT,
540b57cec5SDimitry Andric   R_RELAX_TLS_GD_TO_LE,
550b57cec5SDimitry Andric   R_RELAX_TLS_GD_TO_LE_NEG,
560b57cec5SDimitry Andric   R_RELAX_TLS_IE_TO_LE,
570b57cec5SDimitry Andric   R_RELAX_TLS_LD_TO_LE,
580b57cec5SDimitry Andric   R_RELAX_TLS_LD_TO_LE_ABS,
590b57cec5SDimitry Andric   R_SIZE,
600b57cec5SDimitry Andric   R_TLS,
610b57cec5SDimitry Andric   R_TLSDESC,
620b57cec5SDimitry Andric   R_TLSDESC_CALL,
630b57cec5SDimitry Andric   R_TLSDESC_PC,
640b57cec5SDimitry Andric   R_TLSGD_GOT,
650b57cec5SDimitry Andric   R_TLSGD_GOTPLT,
660b57cec5SDimitry Andric   R_TLSGD_PC,
670b57cec5SDimitry Andric   R_TLSIE_HINT,
680b57cec5SDimitry Andric   R_TLSLD_GOT,
690b57cec5SDimitry Andric   R_TLSLD_GOTPLT,
700b57cec5SDimitry Andric   R_TLSLD_GOT_OFF,
710b57cec5SDimitry Andric   R_TLSLD_HINT,
720b57cec5SDimitry Andric   R_TLSLD_PC,
730b57cec5SDimitry Andric 
740b57cec5SDimitry Andric   // The following is abstract relocation types used for only one target.
750b57cec5SDimitry Andric   //
760b57cec5SDimitry Andric   // Even though RelExpr is intended to be a target-neutral representation
770b57cec5SDimitry Andric   // of a relocation type, there are some relocations whose semantics are
780b57cec5SDimitry Andric   // unique to a target. Such relocation are marked with R_<TARGET_NAME>.
790b57cec5SDimitry Andric   R_AARCH64_GOT_PAGE_PC,
800b57cec5SDimitry Andric   R_AARCH64_PAGE_PC,
810b57cec5SDimitry Andric   R_AARCH64_RELAX_TLS_GD_TO_IE_PAGE_PC,
820b57cec5SDimitry Andric   R_AARCH64_TLSDESC_PAGE,
830b57cec5SDimitry Andric   R_ARM_SBREL,
840b57cec5SDimitry Andric   R_MIPS_GOTREL,
850b57cec5SDimitry Andric   R_MIPS_GOT_GP,
860b57cec5SDimitry Andric   R_MIPS_GOT_GP_PC,
870b57cec5SDimitry Andric   R_MIPS_GOT_LOCAL_PAGE,
880b57cec5SDimitry Andric   R_MIPS_GOT_OFF,
890b57cec5SDimitry Andric   R_MIPS_GOT_OFF32,
900b57cec5SDimitry Andric   R_MIPS_TLSGD,
910b57cec5SDimitry Andric   R_MIPS_TLSLD,
920b57cec5SDimitry Andric   R_PPC32_PLTREL,
930b57cec5SDimitry Andric   R_PPC64_CALL,
940b57cec5SDimitry Andric   R_PPC64_CALL_PLT,
950b57cec5SDimitry Andric   R_PPC64_RELAX_TOC,
960b57cec5SDimitry Andric   R_PPC64_TOCBASE,
970b57cec5SDimitry Andric   R_RISCV_ADD,
980b57cec5SDimitry Andric   R_RISCV_PC_INDIRECT,
990b57cec5SDimitry Andric };
1000b57cec5SDimitry Andric 
1010b57cec5SDimitry Andric // Architecture-neutral representation of relocation.
1020b57cec5SDimitry Andric struct Relocation {
1030b57cec5SDimitry Andric   RelExpr expr;
1040b57cec5SDimitry Andric   RelType type;
1050b57cec5SDimitry Andric   uint64_t offset;
1060b57cec5SDimitry Andric   int64_t addend;
1070b57cec5SDimitry Andric   Symbol *sym;
1080b57cec5SDimitry Andric };
1090b57cec5SDimitry Andric 
1100b57cec5SDimitry Andric // This function writes undefined symbol diagnostics to an internal buffer.
1110b57cec5SDimitry Andric // Call reportUndefinedSymbols() after calling scanRelocations() to emit
1120b57cec5SDimitry Andric // the diagnostics.
1130b57cec5SDimitry Andric template <class ELFT> void scanRelocations(InputSectionBase &);
1140b57cec5SDimitry Andric 
1150b57cec5SDimitry Andric template <class ELFT> void reportUndefinedSymbols();
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric class ThunkSection;
1180b57cec5SDimitry Andric class Thunk;
1190b57cec5SDimitry Andric struct InputSectionDescription;
1200b57cec5SDimitry Andric 
1210b57cec5SDimitry Andric class ThunkCreator {
1220b57cec5SDimitry Andric public:
1230b57cec5SDimitry Andric   // Return true if Thunks have been added to OutputSections
1240b57cec5SDimitry Andric   bool createThunks(ArrayRef<OutputSection *> outputSections);
1250b57cec5SDimitry Andric 
1260b57cec5SDimitry Andric   // The number of completed passes of createThunks this permits us
1270b57cec5SDimitry Andric   // to do one time initialization on Pass 0 and put a limit on the
1280b57cec5SDimitry Andric   // number of times it can be called to prevent infinite loops.
1290b57cec5SDimitry Andric   uint32_t pass = 0;
1300b57cec5SDimitry Andric 
1310b57cec5SDimitry Andric private:
1320b57cec5SDimitry Andric   void mergeThunks(ArrayRef<OutputSection *> outputSections);
1330b57cec5SDimitry Andric 
1340b57cec5SDimitry Andric   ThunkSection *getISDThunkSec(OutputSection *os, InputSection *isec,
1350b57cec5SDimitry Andric                                InputSectionDescription *isd, uint32_t type,
1360b57cec5SDimitry Andric                                uint64_t src);
1370b57cec5SDimitry Andric 
1380b57cec5SDimitry Andric   ThunkSection *getISThunkSec(InputSection *isec);
1390b57cec5SDimitry Andric 
1400b57cec5SDimitry Andric   void createInitialThunkSections(ArrayRef<OutputSection *> outputSections);
1410b57cec5SDimitry Andric 
1420b57cec5SDimitry Andric   std::pair<Thunk *, bool> getThunk(InputSection *isec, Relocation &rel,
1430b57cec5SDimitry Andric                                     uint64_t src);
1440b57cec5SDimitry Andric 
1450b57cec5SDimitry Andric   ThunkSection *addThunkSection(OutputSection *os, InputSectionDescription *,
1460b57cec5SDimitry Andric                                 uint64_t off);
1470b57cec5SDimitry Andric 
1480b57cec5SDimitry Andric   bool normalizeExistingThunk(Relocation &rel, uint64_t src);
1490b57cec5SDimitry Andric 
150*480093f4SDimitry Andric   // Record all the available Thunks for a (Symbol, addend) pair, where Symbol
151*480093f4SDimitry Andric   // is represented as a (section, offset) pair. There may be multiple
152*480093f4SDimitry Andric   // relocations sharing the same (section, offset + addend) pair. We may revert
153*480093f4SDimitry Andric   // a relocation back to its original non-Thunk target, and restore the
154*480093f4SDimitry Andric   // original addend, so we cannot fold offset + addend. A nested pair is used
155*480093f4SDimitry Andric   // because DenseMapInfo is not specialized for std::tuple.
156*480093f4SDimitry Andric   llvm::DenseMap<std::pair<std::pair<SectionBase *, uint64_t>, int64_t>,
157*480093f4SDimitry Andric                  std::vector<Thunk *>>
158*480093f4SDimitry Andric       thunkedSymbolsBySectionAndAddend;
159*480093f4SDimitry Andric   llvm::DenseMap<std::pair<Symbol *, int64_t>, std::vector<Thunk *>>
160*480093f4SDimitry Andric       thunkedSymbols;
1610b57cec5SDimitry Andric 
1620b57cec5SDimitry Andric   // Find a Thunk from the Thunks symbol definition, we can use this to find
1630b57cec5SDimitry Andric   // the Thunk from a relocation to the Thunks symbol definition.
1640b57cec5SDimitry Andric   llvm::DenseMap<Symbol *, Thunk *> thunks;
1650b57cec5SDimitry Andric 
1660b57cec5SDimitry Andric   // Track InputSections that have an inline ThunkSection placed in front
1670b57cec5SDimitry Andric   // an inline ThunkSection may have control fall through to the section below
1680b57cec5SDimitry Andric   // so we need to make sure that there is only one of them.
1690b57cec5SDimitry Andric   // The Mips LA25 Thunk is an example of an inline ThunkSection.
1700b57cec5SDimitry Andric   llvm::DenseMap<InputSection *, ThunkSection *> thunkedSections;
1710b57cec5SDimitry Andric };
1720b57cec5SDimitry Andric 
1730b57cec5SDimitry Andric // Return a int64_t to make sure we get the sign extension out of the way as
1740b57cec5SDimitry Andric // early as possible.
1750b57cec5SDimitry Andric template <class ELFT>
1760b57cec5SDimitry Andric static inline int64_t getAddend(const typename ELFT::Rel &rel) {
1770b57cec5SDimitry Andric   return 0;
1780b57cec5SDimitry Andric }
1790b57cec5SDimitry Andric template <class ELFT>
1800b57cec5SDimitry Andric static inline int64_t getAddend(const typename ELFT::Rela &rel) {
1810b57cec5SDimitry Andric   return rel.r_addend;
1820b57cec5SDimitry Andric }
1830b57cec5SDimitry Andric } // namespace elf
1840b57cec5SDimitry Andric } // namespace lld
1850b57cec5SDimitry Andric 
1860b57cec5SDimitry Andric #endif
187