blob: fd1e5d33c438af6122e7c1777b519730d11309d7 [file] [log] [blame]
Rafael Espindola01205f72015-09-22 18:19:461//===- Target.h -------------------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:563// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Rafael Espindola01205f72015-09-22 18:19:466//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLD_ELF_TARGET_H
10#define LLD_ELF_TARGET_H
11
Fangrui Song27bb7992022-02-08 05:53:3412#include "Config.h"
Rafael Espindola22ef9562016-04-13 01:40:1913#include "InputSection.h"
Bob Haarmanb8a59c82017-10-25 22:28:3814#include "lld/Common/ErrorHandler.h"
Elliot Goodrichb0abd482023-06-17 12:18:2315#include "llvm/ADT/StringExtras.h"
Igor Kudrin15cd9ff2015-11-06 07:43:0316#include "llvm/Object/ELF.h"
Mitch Phillipsca35a192023-07-31 15:07:2617#include "llvm/Object/ELFTypes.h"
Fangrui Song961439c2022-10-02 20:23:5218#include "llvm/Support/Compiler.h"
Fangrui Songf66d0ce2018-08-06 23:50:2619#include "llvm/Support/MathExtras.h"
Simon Atanasyanb0486052018-11-14 21:05:2020#include <array>
Simon Atanasyan49829a12015-09-29 05:34:0321
Rafael Espindola01205f72015-09-22 18:19:4622namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:5423namespace elf {
Rui Ueyama7957b082017-11-07 00:04:2224class Defined;
Simon Atanasyan13f6da12016-03-31 21:26:2325class InputFile;
Rui Ueyamaf52496e2017-11-03 21:21:4726class Symbol;
Rafael Espindola01205f72015-09-22 18:19:4627
Fangrui Song58a971f2024-11-16 19:58:1028std::string toStr(Ctx &, RelType type);
29
Rafael Espindola01205f72015-09-22 18:19:4630class TargetInfo {
31public:
Fangrui Songc3e49982024-09-29 04:48:2632 TargetInfo(Ctx &ctx) : ctx(ctx) {}
Konstantin Zhuravlyove7f17342017-10-24 17:01:4033 virtual uint32_t calcEFlags() const { return 0; }
Rui Ueyama3837f422019-07-10 05:00:3734 virtual RelExpr getRelExpr(RelType type, const Symbol &s,
35 const uint8_t *loc) const = 0;
36 virtual RelType getDynRel(RelType type) const { return 0; }
37 virtual void writeGotPltHeader(uint8_t *buf) const {}
38 virtual void writeGotHeader(uint8_t *buf) const {}
39 virtual void writeGotPlt(uint8_t *buf, const Symbol &s) const {};
Fangrui Song2d077d62020-01-09 19:59:2840 virtual void writeIgotPlt(uint8_t *buf, const Symbol &s) const {}
Rui Ueyama3837f422019-07-10 05:00:3741 virtual int64_t getImplicitAddend(const uint8_t *buf, RelType type) const;
42 virtual int getTlsGdRelaxSkip(RelType type) const { return 1; }
Rui Ueyama69c30ed2016-01-29 03:00:3043
44 // If lazy binding is supported, the first entry of the PLT has code
45 // to call the dynamic linker to resolve PLT entries the first time
46 // they are called. This function writes that code.
Rui Ueyama3837f422019-07-10 05:00:3747 virtual void writePltHeader(uint8_t *buf) const {}
Rui Ueyama69c30ed2016-01-29 03:00:3048
Fangrui Song37b28082019-12-17 21:43:0449 virtual void writePlt(uint8_t *buf, const Symbol &sym,
50 uint64_t pltEntryAddr) const {}
51 virtual void writeIplt(uint8_t *buf, const Symbol &sym,
52 uint64_t pltEntryAddr) const {
53 // All but PPC32 and PPC64 use the same format for .plt and .iplt entries.
54 writePlt(buf, sym, pltEntryAddr);
Fangrui Song891a8652019-12-14 22:17:3555 }
Fangrui Song7cd429f2019-12-11 02:05:3656 virtual void writeIBTPlt(uint8_t *buf, size_t numEntries) const {}
Rui Ueyama3837f422019-07-10 05:00:3757 virtual void addPltHeaderSymbols(InputSection &isec) const {}
58 virtual void addPltSymbols(InputSection &isec, uint64_t off) const {}
Rui Ueyama67533a22017-10-11 22:49:2459
Rafael Espindolab8ff59a2016-04-28 14:34:3960 // Returns true if a relocation only uses the low bits of a value such that
Fangrui Song1f3e2b22018-04-27 05:50:4061 // all those bits are in the same page. For example, if the relocation
Rafael Espindolab8ff59a2016-04-28 14:34:3962 // only uses the low 12 bits in a system with 4k pages. If this is true, the
63 // bits will always have the same value at runtime and we don't have to emit
64 // a dynamic relocation.
Rui Ueyama3837f422019-07-10 05:00:3765 virtual bool usesOnlyLowPageBits(RelType type) const;
Rui Ueyama2b0edc22016-01-08 02:41:3566
Peter Smithfb05cd92016-07-08 16:10:2767 // Decide whether a Thunk is needed for the relocation from File
Peter Smith3a52eb02017-02-01 10:26:0368 // targeting S.
Rui Ueyama3837f422019-07-10 05:00:3769 virtual bool needsThunk(RelExpr expr, RelType relocType,
70 const InputFile *file, uint64_t branchAddr,
Fangrui Songbf535ac2019-11-23 08:57:5471 const Symbol &s, int64_t a) const;
Sterling Augustine4fd84c182018-07-17 23:16:0272
Peter Smitha8656c622018-08-20 09:37:5073 // On systems with range extensions we place collections of Thunks at
74 // regular spacings that enable the majority of branches reach the Thunks.
75 // a value of 0 means range extension thunks are not supported.
76 virtual uint32_t getThunkSectionSpacing() const { return 0; }
77
Sterling Augustine4fd84c182018-07-17 23:16:0278 // The function with a prologue starting at Loc was compiled with
79 // -fsplit-stack and it calls a function compiled without. Adjust the prologue
80 // to do the right thing. See https://gcc.gnu.org/wiki/SplitStacks.
Sean Fertile4b5ec7f2018-10-16 17:13:0181 // The symbols st_other flags are needed on PowerPC64 for determining the
82 // offset to the split-stack prologue.
Rui Ueyama3837f422019-07-10 05:00:3783 virtual bool adjustPrologueForCrossSplitStack(uint8_t *loc, uint8_t *end,
84 uint8_t stOther) const;
Sterling Augustine4fd84c182018-07-17 23:16:0285
Fangrui Song47cfe8f2019-07-16 05:50:4586 // Return true if we can reach dst from src with RelType type.
Rui Ueyama3837f422019-07-10 05:00:3787 virtual bool inBranchRange(RelType type, uint64_t src,
88 uint64_t dst) const;
Rui Ueyama67533a22017-10-11 22:49:2489
Fangrui Songdeb58192020-01-23 05:39:1690 virtual void relocate(uint8_t *loc, const Relocation &rel,
91 uint64_t val) const = 0;
92 void relocateNoSym(uint8_t *loc, RelType type, uint64_t val) const {
93 relocate(loc, Relocation{R_NONE, type, 0, 0, nullptr}, val);
94 }
Fangrui Song685b2122022-10-17 18:01:1095 virtual void relocateAlloc(InputSectionBase &sec, uint8_t *buf) const;
Rui Ueyama67533a22017-10-11 22:49:2496
Fangrui Song6611d582022-07-07 17:16:0997 // Do a linker relaxation pass and return true if we changed something.
98 virtual bool relaxOnce(int pass) const { return false; }
Jinyang He06a728f2024-02-06 01:09:1399 // Do finalize relaxation after collecting relaxation infos.
100 virtual void finalizeRelax(int passes) const {}
Fangrui Song6611d582022-07-07 17:16:09101
Sriraman Tallam94317872020-04-07 13:48:18102 virtual void applyJumpInstrMod(uint8_t *loc, JumpModType type,
103 JumpModType val) const {}
104
Rafael Espindola01205f72015-09-22 18:19:46105 virtual ~TargetInfo();
106
Sriraman Tallam94317872020-04-07 13:48:18107 // This deletes a jump insn at the end of the section if it is a fall thru to
108 // the next section. Further, if there is a conditional jump and a direct
109 // jump consecutively, it tries to flip the conditional jump to convert the
110 // direct jump into a fall thru and delete it. Returns true if a jump
111 // instruction can be deleted.
112 virtual bool deleteFallThruJmpInsn(InputSection &is, InputFile *file,
113 InputSection *nextIS) const {
114 return false;
115 }
116
Fangrui Songc3e49982024-09-29 04:48:26117 Ctx &ctx;
Rui Ueyama3837f422019-07-10 05:00:37118 unsigned defaultCommonPageSize = 4096;
119 unsigned defaultMaxPageSize = 4096;
Hal Finkel736c7412015-10-15 07:49:07120
Rui Ueyamac6946332019-03-28 17:05:09121 uint64_t getImageBase() const;
Hal Finkel736c7412015-10-15 07:49:07122
Peter Smith3d044f52018-03-19 06:52:51123 // True if _GLOBAL_OFFSET_TABLE_ is relative to .got.plt, false if .got.
Fangrui Song40cd4db2021-09-25 22:06:09124 bool gotBaseSymInGotPlt = false;
Peter Smith113a59e2017-06-26 10:22:17125
Fangrui Songd23fd8a2021-09-25 22:16:44126 static constexpr RelType noneRel = 0;
Fangrui Songc35214c2024-10-08 06:07:50127 RelType copyRel = 0;
128 RelType gotRel = 0;
129 RelType pltRel = 0;
130 RelType relativeRel = 0;
131 RelType iRelativeRel = 0;
132 RelType symbolicRel = 0;
133 RelType tlsDescRel = 0;
134 RelType tlsGotRel = 0;
135 RelType tlsModuleIndexRel = 0;
136 RelType tlsOffsetRel = 0;
Fangrui Song777329d2024-09-23 00:48:48137 unsigned gotEntrySize = ctx.arg.wordsize;
Fangrui Songc35214c2024-10-08 06:07:50138 unsigned pltEntrySize = 0;
139 unsigned pltHeaderSize = 0;
140 unsigned ipltEntrySize = 0;
Rafael Espindola4ee6cb32016-05-09 18:12:15141
142 // At least on x86_64 positions 1 and 2 are used by the first plt entry
143 // to support lazy loading.
Rui Ueyama3837f422019-07-10 05:00:37144 unsigned gotPltHeaderEntriesNum = 3;
Rafael Espindola4ee6cb32016-05-09 18:12:15145
Zaara Syeda52ed6eb2018-03-19 17:40:14146 // On PPC ELF V2 abi, the first entry in the .got is the .TOC.
Rui Ueyama3837f422019-07-10 05:00:37147 unsigned gotHeaderEntriesNum = 0;
Zaara Syeda52ed6eb2018-03-19 17:40:14148
Stefan Pintilie658f23f2023-06-02 18:46:25149 // On PPC ELF V2 abi, the dynamic section needs DT_PPC64_OPT (DT_LOPROC + 3)
150 // to be set to 0x2 if there can be multiple TOC's. Although we do not emit
151 // multiple TOC's, there can be a mix of TOC and NOTOC addressing which
152 // is functionally equivalent.
153 int ppc64DynamicSectionOpt = 0;
154
Rui Ueyama3837f422019-07-10 05:00:37155 bool needsThunks = false;
Rafael Espindola0f7ceda2016-07-20 17:58:07156
James Henderson9d9a6632017-04-07 10:36:42157 // A 4-byte field corresponding to one or more trap instructions, used to pad
158 // executable OutputSections.
Fangrui Songc35214c2024-10-08 06:07:50159 std::array<uint8_t, 4> trapInstr = {};
James Henderson9d9a6632017-04-07 10:36:42160
Sriraman Tallam94317872020-04-07 13:48:18161 // Stores the NOP instructions of different sizes for the target and is used
162 // to pad sections that are relaxed.
Fangrui Song4191fda2022-11-27 03:19:15163 std::optional<std::vector<std::vector<uint8_t>>> nopInstrs;
Sriraman Tallam94317872020-04-07 13:48:18164
Sean Fertile4b5ec7f2018-10-16 17:13:01165 // If a target needs to rewrite calls to __morestack to instead call
166 // __morestack_non_split when a split-stack enabled caller calls a
167 // non-split-stack callee this will return true. Otherwise returns false.
Rui Ueyama3837f422019-07-10 05:00:37168 bool needsMoreStackNonSplit = true;
Sean Fertile4b5ec7f2018-10-16 17:13:01169
Fangrui Song50564ca2020-11-25 17:00:55170 virtual RelExpr adjustTlsExpr(RelType type, RelExpr expr) const;
Fangrui Song572d1832020-11-25 16:43:26171 virtual RelExpr adjustGotPcExpr(RelType type, int64_t addend,
172 const uint8_t *loc) const;
James Hendersonb5ca92e2017-10-10 10:09:35173
174protected:
175 // On FreeBSD x86_64 the first page cannot be mmaped.
Nico Weber4138fc92019-10-31 02:17:52176 // On Linux this is controlled by vm.mmap_min_addr. At least on some x86_64
177 // installs this is set to 65536, so the first 15 pages cannot be used.
James Hendersonb5ca92e2017-10-10 10:09:35178 // Given that, the smallest value that can be used in here is 0x10000.
Rui Ueyama3837f422019-07-10 05:00:37179 uint64_t defaultImageBase = 0x10000;
Rafael Espindola01205f72015-09-22 18:19:46180};
181
Fangrui Songe1a073c2024-10-08 06:14:02182void setAArch64TargetInfo(Ctx &);
183void setAMDGPUTargetInfo(Ctx &);
184void setARMTargetInfo(Ctx &);
185void setAVRTargetInfo(Ctx &);
186void setHexagonTargetInfo(Ctx &);
187void setLoongArchTargetInfo(Ctx &);
188void setMSP430TargetInfo(Ctx &);
189void setMipsTargetInfo(Ctx &);
190void setPPC64TargetInfo(Ctx &);
191void setPPCTargetInfo(Ctx &);
192void setRISCVTargetInfo(Ctx &);
193void setSPARCV9TargetInfo(Ctx &);
194void setSystemZTargetInfo(Ctx &);
195void setX86TargetInfo(Ctx &);
196void setX86_64TargetInfo(Ctx &);
Rui Ueyama21c0a9c2017-06-16 17:32:43197
George Rimar89481f32018-03-21 09:19:34198struct ErrorPlace {
Rui Ueyama3837f422019-07-10 05:00:37199 InputSectionBase *isec;
200 std::string loc;
Fangrui Song2b1e3242021-10-28 16:38:45201 std::string srcLoc;
George Rimar89481f32018-03-21 09:19:34202};
203
204// Returns input section and corresponding source string for the given location.
Fangrui Songa5225162024-09-29 02:23:56205ErrorPlace getErrorPlace(Ctx &ctx, const uint8_t *loc);
George Rimar89481f32018-03-21 09:19:34206
Fangrui Songcfd32892024-10-07 02:36:21207static inline std::string getErrorLoc(Ctx &ctx, const uint8_t *loc) {
Fangrui Songa5225162024-09-29 02:23:56208 return getErrorPlace(ctx, loc).loc;
George Rimar89481f32018-03-21 09:19:34209}
Rui Ueyama21c0a9c2017-06-16 17:32:43210
Fangrui Song079b8322024-09-29 23:06:47211void processArmCmseSymbols(Ctx &);
Amilendra Kodithuwakku9acbab62023-07-06 09:45:10212
Fangrui Songb3e0bd32024-10-06 07:31:51213template <class ELFT> uint32_t calcMipsEFlags(Ctx &);
Fangrui Songc4dc5ed2024-11-24 19:43:40214uint8_t getMipsFpAbiFlag(Ctx &, InputFile *file, uint8_t oldFlag,
215 uint8_t newFlag);
Fangrui Songb3e0bd32024-10-06 07:31:51216bool isMipsN32Abi(Ctx &, const InputFile &f);
217bool isMicroMips(Ctx &);
218bool isMipsR6(Ctx &);
219
Fangrui Song6d03a692024-10-06 07:14:12220void writePPC32GlinkSection(Ctx &, uint8_t *buf, size_t numEntries);
Fangrui Song82442ad2019-06-06 17:03:00221
Rui Ueyama3837f422019-07-10 05:00:37222unsigned getPPCDFormOp(unsigned secondaryOp);
Amy Kwan698b45a2023-08-30 16:54:12223unsigned getPPCDSFormOp(unsigned secondaryOp);
Fangrui Song912251e2019-05-07 04:26:05224
225// In the PowerPC64 Elf V2 abi a function can have 2 entry points. The first
226// is a global entry point (GEP) which typically is used to initialize the TOC
Sean Fertilee0e586b2018-09-20 00:26:47227// pointer in general purpose register 2. The second is a local entry
228// point (LEP) which bypasses the TOC pointer initialization code. The
229// offset between GEP and LEP is encoded in a function's st_other flags.
230// This function will return the offset (in bytes) from the global entry-point
231// to the local entry-point.
Fangrui Songd69cc052024-11-15 06:30:29232unsigned getPPC64GlobalEntryToLocalEntryOffset(Ctx &, uint8_t stOther);
Sean Fertilee0e586b2018-09-20 00:26:47233
Victor Huang91cce1a2020-07-20 17:40:41234// Write a prefixed instruction, which is a 4-byte prefix followed by a 4-byte
235// instruction (regardless of endianness). Therefore, the prefix is always in
236// lower memory than the instruction.
Fangrui Song6d03a692024-10-06 07:14:12237void writePrefixedInst(Ctx &, uint8_t *loc, uint64_t insn);
Victor Huang91cce1a2020-07-20 17:40:41238
Fangrui Song6d03a692024-10-06 07:14:12239void addPPC64SaveRestore(Ctx &);
240uint64_t getPPC64TocBase(Ctx &ctx);
Rui Ueyama3837f422019-07-10 05:00:37241uint64_t getAArch64Page(uint64_t expr);
Fangrui Song4fadf412024-10-08 06:29:11242bool isAArch64BTILandingPad(Ctx &, Symbol &s, int64_t a);
Fangrui Song6d03a692024-10-06 07:14:12243template <typename ELFT> void writeARMCmseImportLib(Ctx &);
Lu Weining38394a32024-01-10 10:03:52244uint64_t getLoongArchPageDelta(uint64_t dest, uint64_t pc, RelType type);
Fangrui Song6611d582022-07-07 17:16:09245void riscvFinalizeRelax(int passes);
Fangrui Song079b8322024-09-29 23:06:47246void mergeRISCVAttributesSections(Ctx &);
Fangrui Song6d03a692024-10-06 07:14:12247void addArmInputSectionMappingSymbols(Ctx &);
Simi Pallipurathf1467632023-06-22 12:55:59248void addArmSyntheticSectionMappingSymbol(Defined *);
Fangrui Song4092c0d2024-11-18 17:08:29249void sortArmMappingSymbols(Ctx &);
250void convertArmInstructionstoBE8(Ctx &, InputSection *sec, uint8_t *buf);
Fangrui Song6d03a692024-10-06 07:14:12251void createTaggedSymbols(Ctx &);
252void initSymbolAnchors(Ctx &);
Hal Finkel6f97c2b2015-10-16 21:55:40253
Fangrui Songe1a073c2024-10-08 06:14:02254void setTarget(Ctx &);
Rui Ueyama21c0a9c2017-06-16 17:32:43255
Rui Ueyama3837f422019-07-10 05:00:37256template <class ELFT> bool isMipsPIC(const Defined *sym);
Rui Ueyama7957b082017-11-07 00:04:22257
Fangrui Song47e66732024-11-16 18:12:08258const ELFSyncStream &operator<<(const ELFSyncStream &, RelType);
259
Fangrui Songc490d342024-09-29 23:15:32260void reportRangeError(Ctx &, uint8_t *loc, const Relocation &rel,
261 const Twine &v, int64_t min, uint64_t max);
Fangrui Song29783f72024-09-29 02:17:18262void reportRangeError(Ctx &ctx, uint8_t *loc, int64_t v, int n,
263 const Symbol &sym, const Twine &msg);
Alexander Richardsond2481be2017-12-11 20:47:21264
Rui Ueyamaf001ead2018-03-29 22:40:52265// Make sure that V can be represented as an N bit signed integer.
Fangrui Song2c5dd032024-10-13 18:14:40266inline void checkInt(Ctx &ctx, uint8_t *loc, int64_t v, int n,
267 const Relocation &rel) {
Rui Ueyama3837f422019-07-10 05:00:37268 if (v != llvm::SignExtend64(v, n))
Fangrui Songc490d342024-09-29 23:15:32269 reportRangeError(ctx, loc, rel, Twine(v), llvm::minIntN(n),
270 llvm::maxIntN(n));
Rafael Espindola01205f72015-09-22 18:19:46271}
Rui Ueyamace039262017-01-06 10:04:08272
Rui Ueyamaf001ead2018-03-29 22:40:52273// Make sure that V can be represented as an N bit unsigned integer.
Fangrui Song2c5dd032024-10-13 18:14:40274inline void checkUInt(Ctx &ctx, uint8_t *loc, uint64_t v, int n,
275 const Relocation &rel) {
Rui Ueyama3837f422019-07-10 05:00:37276 if ((v >> n) != 0)
Fangrui Songc490d342024-09-29 23:15:32277 reportRangeError(ctx, loc, rel, Twine(v), 0, llvm::maxUIntN(n));
Rui Ueyama21c0a9c2017-06-16 17:32:43278}
279
Rui Ueyamaf001ead2018-03-29 22:40:52280// Make sure that V can be represented as an N bit signed or unsigned integer.
Fangrui Song2c5dd032024-10-13 18:14:40281inline void checkIntUInt(Ctx &ctx, uint8_t *loc, uint64_t v, int n,
Fangrui Songdeb58192020-01-23 05:39:16282 const Relocation &rel) {
Rui Ueyamaf001ead2018-03-29 22:40:52283 // For the error message we should cast V to a signed integer so that error
284 // messages show a small negative value rather than an extremely large one
Rui Ueyama3837f422019-07-10 05:00:37285 if (v != (uint64_t)llvm::SignExtend64(v, n) && (v >> n) != 0)
Fangrui Songc490d342024-09-29 23:15:32286 reportRangeError(ctx, loc, rel, Twine((int64_t)v), llvm::minIntN(n),
Rui Ueyama3837f422019-07-10 05:00:37287 llvm::maxUIntN(n));
Rui Ueyama21c0a9c2017-06-16 17:32:43288}
289
Fangrui Song2c5dd032024-10-13 18:14:40290inline void checkAlignment(Ctx &ctx, uint8_t *loc, uint64_t v, int n,
Fangrui Songdeb58192020-01-23 05:39:16291 const Relocation &rel) {
Rui Ueyama3837f422019-07-10 05:00:37292 if ((v & (n - 1)) != 0)
Fangrui Song47e66732024-11-16 18:12:08293 Err(ctx) << getErrorLoc(ctx, loc) << "improper alignment for relocation "
294 << rel.type << ": 0x" << llvm::utohexstr(v)
Fangrui Song1cd62752024-11-24 20:13:01295 << " is not aligned to " << n << " bytes";
Rui Ueyama21c0a9c2017-06-16 17:32:43296}
Fangrui Song0c483022018-03-09 18:03:22297
298// Endianness-aware read/write.
Fangrui Song002ca632024-10-13 17:47:18299inline uint16_t read16(Ctx &ctx, const void *p) {
Fangrui Song777329d2024-09-23 00:48:48300 return llvm::support::endian::read16(p, ctx.arg.endianness);
Fangrui Song0c483022018-03-09 18:03:22301}
302
Fangrui Song38dfcd92024-10-13 17:37:47303inline uint32_t read32(Ctx &ctx, const void *p) {
Fangrui Song777329d2024-09-23 00:48:48304 return llvm::support::endian::read32(p, ctx.arg.endianness);
Fangrui Song0c483022018-03-09 18:03:22305}
306
Fangrui Song002ca632024-10-13 17:47:18307inline uint64_t read64(Ctx &ctx, const void *p) {
Fangrui Song777329d2024-09-23 00:48:48308 return llvm::support::endian::read64(p, ctx.arg.endianness);
Fangrui Song0c483022018-03-09 18:03:22309}
310
Fangrui Song002ca632024-10-13 17:47:18311inline void write16(Ctx &ctx, void *p, uint16_t v) {
Fangrui Song777329d2024-09-23 00:48:48312 llvm::support::endian::write16(p, v, ctx.arg.endianness);
Fangrui Song0c483022018-03-09 18:03:22313}
314
Fangrui Song38dfcd92024-10-13 17:37:47315inline void write32(Ctx &ctx, void *p, uint32_t v) {
Fangrui Song777329d2024-09-23 00:48:48316 llvm::support::endian::write32(p, v, ctx.arg.endianness);
Fangrui Song0c483022018-03-09 18:03:22317}
318
Fangrui Song002ca632024-10-13 17:47:18319inline void write64(Ctx &ctx, void *p, uint64_t v) {
Fangrui Song777329d2024-09-23 00:48:48320 llvm::support::endian::write64(p, v, ctx.arg.endianness);
Fangrui Song0c483022018-03-09 18:03:22321}
Fangrui Song3fa17952024-01-09 04:24:00322
323// Overwrite a ULEB128 value and keep the original length.
324inline uint64_t overwriteULEB128(uint8_t *bufLoc, uint64_t val) {
325 while (*bufLoc & 0x80) {
326 *bufLoc++ = 0x80 | (val & 0x7f);
327 val >>= 7;
328 }
329 *bufLoc = val;
330 return val;
331}
Rui Ueyama21c0a9c2017-06-16 17:32:43332} // namespace elf
George Rimar67c60722017-07-18 11:55:35333} // namespace lld
Rafael Espindola01205f72015-09-22 18:19:46334
Fangrui Song7518d382022-02-01 17:47:56335#ifdef __clang__
336#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
337#endif
338#define invokeELFT(f, ...) \
Fangrui Song777329d2024-09-23 00:48:48339 switch (ctx.arg.ekind) { \
Mitch Phillipsca35a192023-07-31 15:07:26340 case lld::elf::ELF32LEKind: \
341 f<llvm::object::ELF32LE>(__VA_ARGS__); \
Fangrui Song7518d382022-02-01 17:47:56342 break; \
Mitch Phillipsca35a192023-07-31 15:07:26343 case lld::elf::ELF32BEKind: \
344 f<llvm::object::ELF32BE>(__VA_ARGS__); \
Fangrui Song7518d382022-02-01 17:47:56345 break; \
Mitch Phillipsca35a192023-07-31 15:07:26346 case lld::elf::ELF64LEKind: \
347 f<llvm::object::ELF64LE>(__VA_ARGS__); \
Fangrui Song7518d382022-02-01 17:47:56348 break; \
Mitch Phillipsca35a192023-07-31 15:07:26349 case lld::elf::ELF64BEKind: \
350 f<llvm::object::ELF64BE>(__VA_ARGS__); \
Fangrui Song7518d382022-02-01 17:47:56351 break; \
352 default: \
Fangrui Song777329d2024-09-23 00:48:48353 llvm_unreachable("unknown ctx.arg.ekind"); \
Fangrui Song7518d382022-02-01 17:47:56354 }
355
Rafael Espindola01205f72015-09-22 18:19:46356#endif