xref: /freebsd/contrib/llvm-project/lld/ELF/Target.h (revision 74626c16)
10b57cec5SDimitry Andric //===- Target.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_TARGET_H
100b57cec5SDimitry Andric #define LLD_ELF_TARGET_H
110b57cec5SDimitry Andric 
1281ad6265SDimitry Andric #include "Config.h"
130b57cec5SDimitry Andric #include "InputSection.h"
140b57cec5SDimitry Andric #include "lld/Common/ErrorHandler.h"
1506c3fb27SDimitry Andric #include "llvm/ADT/StringExtras.h"
160b57cec5SDimitry Andric #include "llvm/Object/ELF.h"
175f757f3fSDimitry Andric #include "llvm/Object/ELFTypes.h"
18bdd1243dSDimitry Andric #include "llvm/Support/Compiler.h"
190b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h"
200b57cec5SDimitry Andric #include <array>
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric namespace lld {
230b57cec5SDimitry Andric std::string toString(elf::RelType type);
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric namespace elf {
260b57cec5SDimitry Andric class Defined;
270b57cec5SDimitry Andric class InputFile;
280b57cec5SDimitry Andric class Symbol;
290b57cec5SDimitry Andric 
300b57cec5SDimitry Andric class TargetInfo {
310b57cec5SDimitry Andric public:
calcEFlags()320b57cec5SDimitry Andric   virtual uint32_t calcEFlags() const { return 0; }
330b57cec5SDimitry Andric   virtual RelExpr getRelExpr(RelType type, const Symbol &s,
340b57cec5SDimitry Andric                              const uint8_t *loc) const = 0;
getDynRel(RelType type)350b57cec5SDimitry Andric   virtual RelType getDynRel(RelType type) const { return 0; }
writeGotPltHeader(uint8_t * buf)360b57cec5SDimitry Andric   virtual void writeGotPltHeader(uint8_t *buf) const {}
writeGotHeader(uint8_t * buf)370b57cec5SDimitry Andric   virtual void writeGotHeader(uint8_t *buf) const {}
writeGotPlt(uint8_t * buf,const Symbol & s)380b57cec5SDimitry Andric   virtual void writeGotPlt(uint8_t *buf, const Symbol &s) const {};
writeIgotPlt(uint8_t * buf,const Symbol & s)39480093f4SDimitry Andric   virtual void writeIgotPlt(uint8_t *buf, const Symbol &s) const {}
400b57cec5SDimitry Andric   virtual int64_t getImplicitAddend(const uint8_t *buf, RelType type) const;
getTlsGdRelaxSkip(RelType type)410b57cec5SDimitry Andric   virtual int getTlsGdRelaxSkip(RelType type) const { return 1; }
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric   // If lazy binding is supported, the first entry of the PLT has code
440b57cec5SDimitry Andric   // to call the dynamic linker to resolve PLT entries the first time
450b57cec5SDimitry Andric   // they are called. This function writes that code.
writePltHeader(uint8_t * buf)460b57cec5SDimitry Andric   virtual void writePltHeader(uint8_t *buf) const {}
470b57cec5SDimitry Andric 
writePlt(uint8_t * buf,const Symbol & sym,uint64_t pltEntryAddr)48480093f4SDimitry Andric   virtual void writePlt(uint8_t *buf, const Symbol &sym,
49480093f4SDimitry Andric                         uint64_t pltEntryAddr) const {}
writeIplt(uint8_t * buf,const Symbol & sym,uint64_t pltEntryAddr)50480093f4SDimitry Andric   virtual void writeIplt(uint8_t *buf, const Symbol &sym,
51480093f4SDimitry Andric                          uint64_t pltEntryAddr) const {
52480093f4SDimitry Andric     // All but PPC32 and PPC64 use the same format for .plt and .iplt entries.
53480093f4SDimitry Andric     writePlt(buf, sym, pltEntryAddr);
54480093f4SDimitry Andric   }
writeIBTPlt(uint8_t * buf,size_t numEntries)55480093f4SDimitry Andric   virtual void writeIBTPlt(uint8_t *buf, size_t numEntries) const {}
addPltHeaderSymbols(InputSection & isec)560b57cec5SDimitry Andric   virtual void addPltHeaderSymbols(InputSection &isec) const {}
addPltSymbols(InputSection & isec,uint64_t off)570b57cec5SDimitry Andric   virtual void addPltSymbols(InputSection &isec, uint64_t off) const {}
580b57cec5SDimitry Andric 
590b57cec5SDimitry Andric   // Returns true if a relocation only uses the low bits of a value such that
600b57cec5SDimitry Andric   // all those bits are in the same page. For example, if the relocation
610b57cec5SDimitry Andric   // only uses the low 12 bits in a system with 4k pages. If this is true, the
620b57cec5SDimitry Andric   // bits will always have the same value at runtime and we don't have to emit
630b57cec5SDimitry Andric   // a dynamic relocation.
640b57cec5SDimitry Andric   virtual bool usesOnlyLowPageBits(RelType type) const;
650b57cec5SDimitry Andric 
660b57cec5SDimitry Andric   // Decide whether a Thunk is needed for the relocation from File
670b57cec5SDimitry Andric   // targeting S.
680b57cec5SDimitry Andric   virtual bool needsThunk(RelExpr expr, RelType relocType,
690b57cec5SDimitry Andric                           const InputFile *file, uint64_t branchAddr,
70480093f4SDimitry Andric                           const Symbol &s, int64_t a) const;
710b57cec5SDimitry Andric 
720b57cec5SDimitry Andric   // On systems with range extensions we place collections of Thunks at
730b57cec5SDimitry Andric   // regular spacings that enable the majority of branches reach the Thunks.
740b57cec5SDimitry Andric   // a value of 0 means range extension thunks are not supported.
getThunkSectionSpacing()750b57cec5SDimitry Andric   virtual uint32_t getThunkSectionSpacing() const { return 0; }
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric   // The function with a prologue starting at Loc was compiled with
780b57cec5SDimitry Andric   // -fsplit-stack and it calls a function compiled without. Adjust the prologue
790b57cec5SDimitry Andric   // to do the right thing. See https://gcc.gnu.org/wiki/SplitStacks.
800b57cec5SDimitry Andric   // The symbols st_other flags are needed on PowerPC64 for determining the
810b57cec5SDimitry Andric   // offset to the split-stack prologue.
820b57cec5SDimitry Andric   virtual bool adjustPrologueForCrossSplitStack(uint8_t *loc, uint8_t *end,
830b57cec5SDimitry Andric                                                 uint8_t stOther) const;
840b57cec5SDimitry Andric 
850b57cec5SDimitry Andric   // Return true if we can reach dst from src with RelType type.
860b57cec5SDimitry Andric   virtual bool inBranchRange(RelType type, uint64_t src,
870b57cec5SDimitry Andric                              uint64_t dst) const;
880b57cec5SDimitry Andric 
895ffd83dbSDimitry Andric   virtual void relocate(uint8_t *loc, const Relocation &rel,
905ffd83dbSDimitry Andric                         uint64_t val) const = 0;
relocateNoSym(uint8_t * loc,RelType type,uint64_t val)915ffd83dbSDimitry Andric   void relocateNoSym(uint8_t *loc, RelType type, uint64_t val) const {
925ffd83dbSDimitry Andric     relocate(loc, Relocation{R_NONE, type, 0, 0, nullptr}, val);
935ffd83dbSDimitry Andric   }
94bdd1243dSDimitry Andric   virtual void relocateAlloc(InputSectionBase &sec, uint8_t *buf) const;
955ffd83dbSDimitry Andric 
96753f127fSDimitry Andric   // Do a linker relaxation pass and return true if we changed something.
relaxOnce(int pass)97753f127fSDimitry Andric   virtual bool relaxOnce(int pass) const { return false; }
9874626c16SDimitry Andric   // Do finalize relaxation after collecting relaxation infos.
finalizeRelax(int passes)9974626c16SDimitry Andric   virtual void finalizeRelax(int passes) const {}
100753f127fSDimitry Andric 
applyJumpInstrMod(uint8_t * loc,JumpModType type,JumpModType val)1015ffd83dbSDimitry Andric   virtual void applyJumpInstrMod(uint8_t *loc, JumpModType type,
1025ffd83dbSDimitry Andric                                  JumpModType val) const {}
1030b57cec5SDimitry Andric 
1040b57cec5SDimitry Andric   virtual ~TargetInfo();
1050b57cec5SDimitry Andric 
1065ffd83dbSDimitry Andric   // This deletes a jump insn at the end of the section if it is a fall thru to
1075ffd83dbSDimitry Andric   // the next section.  Further, if there is a conditional jump and a direct
1085ffd83dbSDimitry Andric   // jump consecutively, it tries to flip the conditional jump to convert the
1095ffd83dbSDimitry Andric   // direct jump into a fall thru and delete it.  Returns true if a jump
1105ffd83dbSDimitry Andric   // instruction can be deleted.
deleteFallThruJmpInsn(InputSection & is,InputFile * file,InputSection * nextIS)1115ffd83dbSDimitry Andric   virtual bool deleteFallThruJmpInsn(InputSection &is, InputFile *file,
1125ffd83dbSDimitry Andric                                      InputSection *nextIS) const {
1135ffd83dbSDimitry Andric     return false;
1145ffd83dbSDimitry Andric   }
1155ffd83dbSDimitry Andric 
1160b57cec5SDimitry Andric   unsigned defaultCommonPageSize = 4096;
1170b57cec5SDimitry Andric   unsigned defaultMaxPageSize = 4096;
1180b57cec5SDimitry Andric 
1190b57cec5SDimitry Andric   uint64_t getImageBase() const;
1200b57cec5SDimitry Andric 
1210b57cec5SDimitry Andric   // True if _GLOBAL_OFFSET_TABLE_ is relative to .got.plt, false if .got.
122349cc55cSDimitry Andric   bool gotBaseSymInGotPlt = false;
1230b57cec5SDimitry Andric 
124349cc55cSDimitry Andric   static constexpr RelType noneRel = 0;
1250b57cec5SDimitry Andric   RelType copyRel;
1260b57cec5SDimitry Andric   RelType gotRel;
1270b57cec5SDimitry Andric   RelType pltRel;
1280b57cec5SDimitry Andric   RelType relativeRel;
1290b57cec5SDimitry Andric   RelType iRelativeRel;
1300b57cec5SDimitry Andric   RelType symbolicRel;
1310b57cec5SDimitry Andric   RelType tlsDescRel;
1320b57cec5SDimitry Andric   RelType tlsGotRel;
1330b57cec5SDimitry Andric   RelType tlsModuleIndexRel;
1340b57cec5SDimitry Andric   RelType tlsOffsetRel;
135fe6060f1SDimitry Andric   unsigned gotEntrySize = config->wordsize;
1360b57cec5SDimitry Andric   unsigned pltEntrySize;
1370b57cec5SDimitry Andric   unsigned pltHeaderSize;
138480093f4SDimitry Andric   unsigned ipltEntrySize;
1390b57cec5SDimitry Andric 
1400b57cec5SDimitry Andric   // At least on x86_64 positions 1 and 2 are used by the first plt entry
1410b57cec5SDimitry Andric   // to support lazy loading.
1420b57cec5SDimitry Andric   unsigned gotPltHeaderEntriesNum = 3;
1430b57cec5SDimitry Andric 
1440b57cec5SDimitry Andric   // On PPC ELF V2 abi, the first entry in the .got is the .TOC.
1450b57cec5SDimitry Andric   unsigned gotHeaderEntriesNum = 0;
1460b57cec5SDimitry Andric 
14706c3fb27SDimitry Andric   // On PPC ELF V2 abi, the dynamic section needs DT_PPC64_OPT (DT_LOPROC + 3)
14806c3fb27SDimitry Andric   // to be set to 0x2 if there can be multiple TOC's. Although we do not emit
14906c3fb27SDimitry Andric   // multiple TOC's, there can be a mix of TOC and NOTOC addressing which
15006c3fb27SDimitry Andric   // is functionally equivalent.
15106c3fb27SDimitry Andric   int ppc64DynamicSectionOpt = 0;
15206c3fb27SDimitry Andric 
1530b57cec5SDimitry Andric   bool needsThunks = false;
1540b57cec5SDimitry Andric 
1550b57cec5SDimitry Andric   // A 4-byte field corresponding to one or more trap instructions, used to pad
1560b57cec5SDimitry Andric   // executable OutputSections.
1570b57cec5SDimitry Andric   std::array<uint8_t, 4> trapInstr;
1580b57cec5SDimitry Andric 
1595ffd83dbSDimitry Andric   // Stores the NOP instructions of different sizes for the target and is used
1605ffd83dbSDimitry Andric   // to pad sections that are relaxed.
161bdd1243dSDimitry Andric   std::optional<std::vector<std::vector<uint8_t>>> nopInstrs;
1625ffd83dbSDimitry Andric 
1630b57cec5SDimitry Andric   // If a target needs to rewrite calls to __morestack to instead call
1640b57cec5SDimitry Andric   // __morestack_non_split when a split-stack enabled caller calls a
1650b57cec5SDimitry Andric   // non-split-stack callee this will return true. Otherwise returns false.
1660b57cec5SDimitry Andric   bool needsMoreStackNonSplit = true;
1670b57cec5SDimitry Andric 
168e8d8bef9SDimitry Andric   virtual RelExpr adjustTlsExpr(RelType type, RelExpr expr) const;
169e8d8bef9SDimitry Andric   virtual RelExpr adjustGotPcExpr(RelType type, int64_t addend,
170e8d8bef9SDimitry Andric                                   const uint8_t *loc) const;
1710b57cec5SDimitry Andric 
1720b57cec5SDimitry Andric protected:
1730b57cec5SDimitry Andric   // On FreeBSD x86_64 the first page cannot be mmaped.
174480093f4SDimitry Andric   // On Linux this is controlled by vm.mmap_min_addr. At least on some x86_64
175480093f4SDimitry Andric   // installs this is set to 65536, so the first 15 pages cannot be used.
1760b57cec5SDimitry Andric   // Given that, the smallest value that can be used in here is 0x10000.
1770b57cec5SDimitry Andric   uint64_t defaultImageBase = 0x10000;
1780b57cec5SDimitry Andric };
1790b57cec5SDimitry Andric 
1800b57cec5SDimitry Andric TargetInfo *getAArch64TargetInfo();
1810b57cec5SDimitry Andric TargetInfo *getAMDGPUTargetInfo();
1820b57cec5SDimitry Andric TargetInfo *getARMTargetInfo();
1830b57cec5SDimitry Andric TargetInfo *getAVRTargetInfo();
1840b57cec5SDimitry Andric TargetInfo *getHexagonTargetInfo();
18506c3fb27SDimitry Andric TargetInfo *getLoongArchTargetInfo();
1860b57cec5SDimitry Andric TargetInfo *getMSP430TargetInfo();
1870b57cec5SDimitry Andric TargetInfo *getPPC64TargetInfo();
1880b57cec5SDimitry Andric TargetInfo *getPPCTargetInfo();
1890b57cec5SDimitry Andric TargetInfo *getRISCVTargetInfo();
1900b57cec5SDimitry Andric TargetInfo *getSPARCV9TargetInfo();
19174626c16SDimitry Andric TargetInfo *getSystemZTargetInfo();
1920b57cec5SDimitry Andric TargetInfo *getX86TargetInfo();
1930b57cec5SDimitry Andric TargetInfo *getX86_64TargetInfo();
1940b57cec5SDimitry Andric template <class ELFT> TargetInfo *getMipsTargetInfo();
1950b57cec5SDimitry Andric 
1960b57cec5SDimitry Andric struct ErrorPlace {
1970b57cec5SDimitry Andric   InputSectionBase *isec;
1980b57cec5SDimitry Andric   std::string loc;
199349cc55cSDimitry Andric   std::string srcLoc;
2000b57cec5SDimitry Andric };
2010b57cec5SDimitry Andric 
2020b57cec5SDimitry Andric // Returns input section and corresponding source string for the given location.
2030b57cec5SDimitry Andric ErrorPlace getErrorPlace(const uint8_t *loc);
2040b57cec5SDimitry Andric 
getErrorLocation(const uint8_t * loc)2050b57cec5SDimitry Andric static inline std::string getErrorLocation(const uint8_t *loc) {
2060b57cec5SDimitry Andric   return getErrorPlace(loc).loc;
2070b57cec5SDimitry Andric }
2080b57cec5SDimitry Andric 
20906c3fb27SDimitry Andric void processArmCmseSymbols();
21006c3fb27SDimitry Andric 
2110b57cec5SDimitry Andric void writePPC32GlinkSection(uint8_t *buf, size_t numEntries);
2120b57cec5SDimitry Andric 
2130b57cec5SDimitry Andric unsigned getPPCDFormOp(unsigned secondaryOp);
2148a4dda33SDimitry Andric unsigned getPPCDSFormOp(unsigned secondaryOp);
2150b57cec5SDimitry Andric 
2160b57cec5SDimitry Andric // In the PowerPC64 Elf V2 abi a function can have 2 entry points.  The first
2170b57cec5SDimitry Andric // is a global entry point (GEP) which typically is used to initialize the TOC
2180b57cec5SDimitry Andric // pointer in general purpose register 2.  The second is a local entry
2190b57cec5SDimitry Andric // point (LEP) which bypasses the TOC pointer initialization code. The
2200b57cec5SDimitry Andric // offset between GEP and LEP is encoded in a function's st_other flags.
2210b57cec5SDimitry Andric // This function will return the offset (in bytes) from the global entry-point
2220b57cec5SDimitry Andric // to the local entry-point.
2230b57cec5SDimitry Andric unsigned getPPC64GlobalEntryToLocalEntryOffset(uint8_t stOther);
2240b57cec5SDimitry Andric 
225e8d8bef9SDimitry Andric // Write a prefixed instruction, which is a 4-byte prefix followed by a 4-byte
226e8d8bef9SDimitry Andric // instruction (regardless of endianness). Therefore, the prefix is always in
227e8d8bef9SDimitry Andric // lower memory than the instruction.
228e8d8bef9SDimitry Andric void writePrefixedInstruction(uint8_t *loc, uint64_t insn);
229e8d8bef9SDimitry Andric 
2305ffd83dbSDimitry Andric void addPPC64SaveRestore();
2310b57cec5SDimitry Andric uint64_t getPPC64TocBase();
2320b57cec5SDimitry Andric uint64_t getAArch64Page(uint64_t expr);
23306c3fb27SDimitry Andric template <typename ELFT> void writeARMCmseImportLib();
234297eecfbSDimitry Andric uint64_t getLoongArchPageDelta(uint64_t dest, uint64_t pc, RelType type);
235753f127fSDimitry Andric void riscvFinalizeRelax(int passes);
236bdd1243dSDimitry Andric void mergeRISCVAttributesSections();
23706c3fb27SDimitry Andric void addArmInputSectionMappingSymbols();
23806c3fb27SDimitry Andric void addArmSyntheticSectionMappingSymbol(Defined *);
23906c3fb27SDimitry Andric void sortArmMappingSymbols();
24006c3fb27SDimitry Andric void convertArmInstructionstoBE8(InputSection *sec, uint8_t *buf);
2415f757f3fSDimitry Andric void createTaggedSymbols(const SmallVector<ELFFileBase *, 0> &files);
24274626c16SDimitry Andric void initSymbolAnchors();
2430b57cec5SDimitry Andric 
244bdd1243dSDimitry Andric LLVM_LIBRARY_VISIBILITY extern const TargetInfo *target;
2450b57cec5SDimitry Andric TargetInfo *getTarget();
2460b57cec5SDimitry Andric 
2470b57cec5SDimitry Andric template <class ELFT> bool isMipsPIC(const Defined *sym);
2480b57cec5SDimitry Andric 
2495ffd83dbSDimitry Andric void reportRangeError(uint8_t *loc, const Relocation &rel, const Twine &v,
2505ffd83dbSDimitry Andric                       int64_t min, uint64_t max);
251e8d8bef9SDimitry Andric void reportRangeError(uint8_t *loc, int64_t v, int n, const Symbol &sym,
252e8d8bef9SDimitry Andric                       const Twine &msg);
2530b57cec5SDimitry Andric 
2540b57cec5SDimitry Andric // Make sure that V can be represented as an N bit signed integer.
checkInt(uint8_t * loc,int64_t v,int n,const Relocation & rel)2555ffd83dbSDimitry Andric inline void checkInt(uint8_t *loc, int64_t v, int n, const Relocation &rel) {
2560b57cec5SDimitry Andric   if (v != llvm::SignExtend64(v, n))
2575ffd83dbSDimitry Andric     reportRangeError(loc, rel, Twine(v), llvm::minIntN(n), llvm::maxIntN(n));
2580b57cec5SDimitry Andric }
2590b57cec5SDimitry Andric 
2600b57cec5SDimitry Andric // Make sure that V can be represented as an N bit unsigned integer.
checkUInt(uint8_t * loc,uint64_t v,int n,const Relocation & rel)2615ffd83dbSDimitry Andric inline void checkUInt(uint8_t *loc, uint64_t v, int n, const Relocation &rel) {
2620b57cec5SDimitry Andric   if ((v >> n) != 0)
2635ffd83dbSDimitry Andric     reportRangeError(loc, rel, Twine(v), 0, llvm::maxUIntN(n));
2640b57cec5SDimitry Andric }
2650b57cec5SDimitry Andric 
2660b57cec5SDimitry Andric // Make sure that V can be represented as an N bit signed or unsigned integer.
checkIntUInt(uint8_t * loc,uint64_t v,int n,const Relocation & rel)2675ffd83dbSDimitry Andric inline void checkIntUInt(uint8_t *loc, uint64_t v, int n,
2685ffd83dbSDimitry Andric                          const Relocation &rel) {
2690b57cec5SDimitry Andric   // For the error message we should cast V to a signed integer so that error
2700b57cec5SDimitry Andric   // messages show a small negative value rather than an extremely large one
2710b57cec5SDimitry Andric   if (v != (uint64_t)llvm::SignExtend64(v, n) && (v >> n) != 0)
2725ffd83dbSDimitry Andric     reportRangeError(loc, rel, Twine((int64_t)v), llvm::minIntN(n),
2730b57cec5SDimitry Andric                      llvm::maxUIntN(n));
2740b57cec5SDimitry Andric }
2750b57cec5SDimitry Andric 
checkAlignment(uint8_t * loc,uint64_t v,int n,const Relocation & rel)2765ffd83dbSDimitry Andric inline void checkAlignment(uint8_t *loc, uint64_t v, int n,
2775ffd83dbSDimitry Andric                            const Relocation &rel) {
2780b57cec5SDimitry Andric   if ((v & (n - 1)) != 0)
2790b57cec5SDimitry Andric     error(getErrorLocation(loc) + "improper alignment for relocation " +
2805ffd83dbSDimitry Andric           lld::toString(rel.type) + ": 0x" + llvm::utohexstr(v) +
2810b57cec5SDimitry Andric           " is not aligned to " + Twine(n) + " bytes");
2820b57cec5SDimitry Andric }
2830b57cec5SDimitry Andric 
2840b57cec5SDimitry Andric // Endianness-aware read/write.
read16(const void * p)2850b57cec5SDimitry Andric inline uint16_t read16(const void *p) {
2860b57cec5SDimitry Andric   return llvm::support::endian::read16(p, config->endianness);
2870b57cec5SDimitry Andric }
2880b57cec5SDimitry Andric 
read32(const void * p)2890b57cec5SDimitry Andric inline uint32_t read32(const void *p) {
2900b57cec5SDimitry Andric   return llvm::support::endian::read32(p, config->endianness);
2910b57cec5SDimitry Andric }
2920b57cec5SDimitry Andric 
read64(const void * p)2930b57cec5SDimitry Andric inline uint64_t read64(const void *p) {
2940b57cec5SDimitry Andric   return llvm::support::endian::read64(p, config->endianness);
2950b57cec5SDimitry Andric }
2960b57cec5SDimitry Andric 
write16(void * p,uint16_t v)2970b57cec5SDimitry Andric inline void write16(void *p, uint16_t v) {
2980b57cec5SDimitry Andric   llvm::support::endian::write16(p, v, config->endianness);
2990b57cec5SDimitry Andric }
3000b57cec5SDimitry Andric 
write32(void * p,uint32_t v)3010b57cec5SDimitry Andric inline void write32(void *p, uint32_t v) {
3020b57cec5SDimitry Andric   llvm::support::endian::write32(p, v, config->endianness);
3030b57cec5SDimitry Andric }
3040b57cec5SDimitry Andric 
write64(void * p,uint64_t v)3050b57cec5SDimitry Andric inline void write64(void *p, uint64_t v) {
3060b57cec5SDimitry Andric   llvm::support::endian::write64(p, v, config->endianness);
3070b57cec5SDimitry Andric }
3081db9f3b2SDimitry Andric 
3091db9f3b2SDimitry Andric // Overwrite a ULEB128 value and keep the original length.
overwriteULEB128(uint8_t * bufLoc,uint64_t val)3101db9f3b2SDimitry Andric inline uint64_t overwriteULEB128(uint8_t *bufLoc, uint64_t val) {
3111db9f3b2SDimitry Andric   while (*bufLoc & 0x80) {
3121db9f3b2SDimitry Andric     *bufLoc++ = 0x80 | (val & 0x7f);
3131db9f3b2SDimitry Andric     val >>= 7;
3141db9f3b2SDimitry Andric   }
3151db9f3b2SDimitry Andric   *bufLoc = val;
3161db9f3b2SDimitry Andric   return val;
3171db9f3b2SDimitry Andric }
3180b57cec5SDimitry Andric } // namespace elf
3190b57cec5SDimitry Andric } // namespace lld
3200b57cec5SDimitry Andric 
3211fd87a68SDimitry Andric #ifdef __clang__
3221fd87a68SDimitry Andric #pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
3231fd87a68SDimitry Andric #endif
3241fd87a68SDimitry Andric #define invokeELFT(f, ...)                                                     \
3251fd87a68SDimitry Andric   switch (config->ekind) {                                                     \
3265f757f3fSDimitry Andric   case lld::elf::ELF32LEKind:                                                  \
3275f757f3fSDimitry Andric     f<llvm::object::ELF32LE>(__VA_ARGS__);                                     \
3281fd87a68SDimitry Andric     break;                                                                     \
3295f757f3fSDimitry Andric   case lld::elf::ELF32BEKind:                                                  \
3305f757f3fSDimitry Andric     f<llvm::object::ELF32BE>(__VA_ARGS__);                                     \
3311fd87a68SDimitry Andric     break;                                                                     \
3325f757f3fSDimitry Andric   case lld::elf::ELF64LEKind:                                                  \
3335f757f3fSDimitry Andric     f<llvm::object::ELF64LE>(__VA_ARGS__);                                     \
3341fd87a68SDimitry Andric     break;                                                                     \
3355f757f3fSDimitry Andric   case lld::elf::ELF64BEKind:                                                  \
3365f757f3fSDimitry Andric     f<llvm::object::ELF64BE>(__VA_ARGS__);                                     \
3371fd87a68SDimitry Andric     break;                                                                     \
3381fd87a68SDimitry Andric   default:                                                                     \
3391fd87a68SDimitry Andric     llvm_unreachable("unknown config->ekind");                                 \
3401fd87a68SDimitry Andric   }
3411fd87a68SDimitry Andric 
3420b57cec5SDimitry Andric #endif
343