master
1//! Executable and Linkable Format.
2
3const std = @import("std.zig");
4const Io = std.Io;
5const math = std.math;
6const mem = std.mem;
7const assert = std.debug.assert;
8const Endian = std.builtin.Endian;
9const native_endian = @import("builtin").target.cpu.arch.endian();
10
11pub const AT_NULL = 0;
12pub const AT_IGNORE = 1;
13pub const AT_EXECFD = 2;
14pub const AT_PHDR = 3;
15pub const AT_PHENT = 4;
16pub const AT_PHNUM = 5;
17pub const AT_PAGESZ = 6;
18pub const AT_BASE = 7;
19pub const AT_FLAGS = 8;
20pub const AT_ENTRY = 9;
21pub const AT_NOTELF = 10;
22pub const AT_UID = 11;
23pub const AT_EUID = 12;
24pub const AT_GID = 13;
25pub const AT_EGID = 14;
26pub const AT_CLKTCK = 17;
27pub const AT_PLATFORM = 15;
28pub const AT_HWCAP = 16;
29pub const AT_FPUCW = 18;
30pub const AT_DCACHEBSIZE = 19;
31pub const AT_ICACHEBSIZE = 20;
32pub const AT_UCACHEBSIZE = 21;
33pub const AT_IGNOREPPC = 22;
34pub const AT_SECURE = 23;
35pub const AT_BASE_PLATFORM = 24;
36pub const AT_RANDOM = 25;
37pub const AT_HWCAP2 = 26;
38pub const AT_EXECFN = 31;
39pub const AT_SYSINFO = 32;
40pub const AT_SYSINFO_EHDR = 33;
41pub const AT_L1I_CACHESHAPE = 34;
42pub const AT_L1D_CACHESHAPE = 35;
43pub const AT_L2_CACHESHAPE = 36;
44pub const AT_L3_CACHESHAPE = 37;
45pub const AT_L1I_CACHESIZE = 40;
46pub const AT_L1I_CACHEGEOMETRY = 41;
47pub const AT_L1D_CACHESIZE = 42;
48pub const AT_L1D_CACHEGEOMETRY = 43;
49pub const AT_L2_CACHESIZE = 44;
50pub const AT_L2_CACHEGEOMETRY = 45;
51pub const AT_L3_CACHESIZE = 46;
52pub const AT_L3_CACHEGEOMETRY = 47;
53pub const AT_MINSIGSTKSZ = 51;
54
55pub const DT_NULL = 0;
56pub const DT_NEEDED = 1;
57pub const DT_PLTRELSZ = 2;
58pub const DT_PLTGOT = 3;
59pub const DT_HASH = 4;
60pub const DT_STRTAB = 5;
61pub const DT_SYMTAB = 6;
62pub const DT_RELA = 7;
63pub const DT_RELASZ = 8;
64pub const DT_RELAENT = 9;
65pub const DT_STRSZ = 10;
66pub const DT_SYMENT = 11;
67pub const DT_INIT = 12;
68pub const DT_FINI = 13;
69pub const DT_SONAME = 14;
70pub const DT_RPATH = 15;
71pub const DT_SYMBOLIC = 16;
72pub const DT_REL = 17;
73pub const DT_RELSZ = 18;
74pub const DT_RELENT = 19;
75pub const DT_PLTREL = 20;
76pub const DT_DEBUG = 21;
77pub const DT_TEXTREL = 22;
78pub const DT_JMPREL = 23;
79pub const DT_BIND_NOW = 24;
80pub const DT_INIT_ARRAY = 25;
81pub const DT_FINI_ARRAY = 26;
82pub const DT_INIT_ARRAYSZ = 27;
83pub const DT_FINI_ARRAYSZ = 28;
84pub const DT_RUNPATH = 29;
85pub const DT_FLAGS = 30;
86pub const DT_ENCODING = 32;
87pub const DT_PREINIT_ARRAY = 32;
88pub const DT_PREINIT_ARRAYSZ = 33;
89pub const DT_SYMTAB_SHNDX = 34;
90pub const DT_RELRSZ = 35;
91pub const DT_RELR = 36;
92pub const DT_RELRENT = 37;
93pub const DT_NUM = 38;
94pub const DT_LOOS = 0x6000000d;
95pub const DT_HIOS = 0x6ffff000;
96pub const DT_LOPROC = 0x70000000;
97pub const DT_HIPROC = 0x7fffffff;
98pub const DT_PROCNUM = DT_MIPS_NUM;
99
100pub const DT_VALRNGLO = 0x6ffffd00;
101pub const DT_GNU_PRELINKED = 0x6ffffdf5;
102pub const DT_GNU_CONFLICTSZ = 0x6ffffdf6;
103pub const DT_GNU_LIBLISTSZ = 0x6ffffdf7;
104pub const DT_CHECKSUM = 0x6ffffdf8;
105pub const DT_PLTPADSZ = 0x6ffffdf9;
106pub const DT_MOVEENT = 0x6ffffdfa;
107pub const DT_MOVESZ = 0x6ffffdfb;
108pub const DT_FEATURE_1 = 0x6ffffdfc;
109pub const DT_POSFLAG_1 = 0x6ffffdfd;
110
111pub const DT_SYMINSZ = 0x6ffffdfe;
112pub const DT_SYMINENT = 0x6ffffdff;
113pub const DT_VALRNGHI = 0x6ffffdff;
114pub const DT_VALNUM = 12;
115
116pub const DT_ADDRRNGLO = 0x6ffffe00;
117pub const DT_GNU_HASH = 0x6ffffef5;
118pub const DT_TLSDESC_PLT = 0x6ffffef6;
119pub const DT_TLSDESC_GOT = 0x6ffffef7;
120pub const DT_GNU_CONFLICT = 0x6ffffef8;
121pub const DT_GNU_LIBLIST = 0x6ffffef9;
122pub const DT_CONFIG = 0x6ffffefa;
123pub const DT_DEPAUDIT = 0x6ffffefb;
124pub const DT_AUDIT = 0x6ffffefc;
125pub const DT_PLTPAD = 0x6ffffefd;
126pub const DT_MOVETAB = 0x6ffffefe;
127pub const DT_SYMINFO = 0x6ffffeff;
128pub const DT_ADDRRNGHI = 0x6ffffeff;
129pub const DT_ADDRNUM = 11;
130
131pub const DT_VERSYM = 0x6ffffff0;
132
133pub const DT_RELACOUNT = 0x6ffffff9;
134pub const DT_RELCOUNT = 0x6ffffffa;
135
136pub const DT_FLAGS_1 = 0x6ffffffb;
137pub const DT_VERDEF = 0x6ffffffc;
138
139pub const DT_VERDEFNUM = 0x6ffffffd;
140pub const DT_VERNEED = 0x6ffffffe;
141
142pub const DT_VERNEEDNUM = 0x6fffffff;
143pub const DT_VERSIONTAGNUM = 16;
144
145pub const DT_AUXILIARY = 0x7ffffffd;
146pub const DT_FILTER = 0x7fffffff;
147pub const DT_EXTRANUM = 3;
148
149pub const DT_SPARC_REGISTER = 0x70000001;
150pub const DT_SPARC_NUM = 2;
151
152pub const DT_MIPS_RLD_VERSION = 0x70000001;
153pub const DT_MIPS_TIME_STAMP = 0x70000002;
154pub const DT_MIPS_ICHECKSUM = 0x70000003;
155pub const DT_MIPS_IVERSION = 0x70000004;
156pub const DT_MIPS_FLAGS = 0x70000005;
157pub const DT_MIPS_BASE_ADDRESS = 0x70000006;
158pub const DT_MIPS_MSYM = 0x70000007;
159pub const DT_MIPS_CONFLICT = 0x70000008;
160pub const DT_MIPS_LIBLIST = 0x70000009;
161pub const DT_MIPS_LOCAL_GOTNO = 0x7000000a;
162pub const DT_MIPS_CONFLICTNO = 0x7000000b;
163pub const DT_MIPS_LIBLISTNO = 0x70000010;
164pub const DT_MIPS_SYMTABNO = 0x70000011;
165pub const DT_MIPS_UNREFEXTNO = 0x70000012;
166pub const DT_MIPS_GOTSYM = 0x70000013;
167pub const DT_MIPS_HIPAGENO = 0x70000014;
168pub const DT_MIPS_RLD_MAP = 0x70000016;
169pub const DT_MIPS_DELTA_CLASS = 0x70000017;
170pub const DT_MIPS_DELTA_CLASS_NO = 0x70000018;
171
172pub const DT_MIPS_DELTA_INSTANCE = 0x70000019;
173pub const DT_MIPS_DELTA_INSTANCE_NO = 0x7000001a;
174
175pub const DT_MIPS_DELTA_RELOC = 0x7000001b;
176pub const DT_MIPS_DELTA_RELOC_NO = 0x7000001c;
177
178pub const DT_MIPS_DELTA_SYM = 0x7000001d;
179
180pub const DT_MIPS_DELTA_SYM_NO = 0x7000001e;
181
182pub const DT_MIPS_DELTA_CLASSSYM = 0x70000020;
183
184pub const DT_MIPS_DELTA_CLASSSYM_NO = 0x70000021;
185
186pub const DT_MIPS_CXX_FLAGS = 0x70000022;
187pub const DT_MIPS_PIXIE_INIT = 0x70000023;
188pub const DT_MIPS_SYMBOL_LIB = 0x70000024;
189pub const DT_MIPS_LOCALPAGE_GOTIDX = 0x70000025;
190pub const DT_MIPS_LOCAL_GOTIDX = 0x70000026;
191pub const DT_MIPS_HIDDEN_GOTIDX = 0x70000027;
192pub const DT_MIPS_PROTECTED_GOTIDX = 0x70000028;
193pub const DT_MIPS_OPTIONS = 0x70000029;
194pub const DT_MIPS_INTERFACE = 0x7000002a;
195pub const DT_MIPS_DYNSTR_ALIGN = 0x7000002b;
196pub const DT_MIPS_INTERFACE_SIZE = 0x7000002c;
197pub const DT_MIPS_RLD_TEXT_RESOLVE_ADDR = 0x7000002d;
198
199pub const DT_MIPS_PERF_SUFFIX = 0x7000002e;
200
201pub const DT_MIPS_COMPACT_SIZE = 0x7000002f;
202pub const DT_MIPS_GP_VALUE = 0x70000030;
203pub const DT_MIPS_AUX_DYNAMIC = 0x70000031;
204
205pub const DT_MIPS_PLTGOT = 0x70000032;
206
207pub const DT_MIPS_RWPLT = 0x70000034;
208pub const DT_MIPS_RLD_MAP_REL = 0x70000035;
209pub const DT_MIPS_NUM = 0x36;
210
211pub const DT_ALPHA_PLTRO = (DT_LOPROC + 0);
212pub const DT_ALPHA_NUM = 1;
213
214pub const DT_PPC_GOT = (DT_LOPROC + 0);
215pub const DT_PPC_OPT = (DT_LOPROC + 1);
216pub const DT_PPC_NUM = 2;
217
218pub const DT_PPC64_GLINK = (DT_LOPROC + 0);
219pub const DT_PPC64_OPD = (DT_LOPROC + 1);
220pub const DT_PPC64_OPDSZ = (DT_LOPROC + 2);
221pub const DT_PPC64_OPT = (DT_LOPROC + 3);
222pub const DT_PPC64_NUM = 4;
223
224pub const DT_IA_64_PLT_RESERVE = (DT_LOPROC + 0);
225pub const DT_IA_64_NUM = 1;
226
227pub const DT_NIOS2_GP = 0x70000002;
228
229pub const DF_ORIGIN = 0x00000001;
230pub const DF_SYMBOLIC = 0x00000002;
231pub const DF_TEXTREL = 0x00000004;
232pub const DF_BIND_NOW = 0x00000008;
233pub const DF_STATIC_TLS = 0x00000010;
234
235pub const DF_1_NOW = 0x00000001;
236pub const DF_1_GLOBAL = 0x00000002;
237pub const DF_1_GROUP = 0x00000004;
238pub const DF_1_NODELETE = 0x00000008;
239pub const DF_1_LOADFLTR = 0x00000010;
240pub const DF_1_INITFIRST = 0x00000020;
241pub const DF_1_NOOPEN = 0x00000040;
242pub const DF_1_ORIGIN = 0x00000080;
243pub const DF_1_DIRECT = 0x00000100;
244pub const DF_1_TRANS = 0x00000200;
245pub const DF_1_INTERPOSE = 0x00000400;
246pub const DF_1_NODEFLIB = 0x00000800;
247pub const DF_1_NODUMP = 0x00001000;
248pub const DF_1_CONFALT = 0x00002000;
249pub const DF_1_ENDFILTEE = 0x00004000;
250pub const DF_1_DISPRELDNE = 0x00008000;
251pub const DF_1_DISPRELPND = 0x00010000;
252pub const DF_1_NODIRECT = 0x00020000;
253pub const DF_1_IGNMULDEF = 0x00040000;
254pub const DF_1_NOKSYMS = 0x00080000;
255pub const DF_1_NOHDR = 0x00100000;
256pub const DF_1_EDITED = 0x00200000;
257pub const DF_1_NORELOC = 0x00400000;
258pub const DF_1_SYMINTPOSE = 0x00800000;
259pub const DF_1_GLOBAUDIT = 0x01000000;
260pub const DF_1_SINGLETON = 0x02000000;
261pub const DF_1_STUB = 0x04000000;
262pub const DF_1_PIE = 0x08000000;
263
264pub const Versym = packed struct(u16) {
265 VERSION: u15,
266 HIDDEN: bool,
267
268 pub const LOCAL: Versym = @bitCast(@intFromEnum(VER_NDX.LOCAL));
269 pub const GLOBAL: Versym = @bitCast(@intFromEnum(VER_NDX.GLOBAL));
270};
271
272pub const VER_NDX = enum(u16) {
273 /// Symbol is local
274 LOCAL = 0,
275 /// Symbol is global
276 GLOBAL = 1,
277 /// Beginning of reserved entries
278 LORESERVE = 0xff00,
279 /// Symbol is to be eliminated
280 ELIMINATE = 0xff01,
281 UNSPECIFIED = 0xffff,
282 _,
283};
284
285/// Version definition of the file itself
286pub const VER_FLG_BASE = 1;
287/// Weak version identifier
288pub const VER_FLG_WEAK = 2;
289
290/// Deprecated, use `@intFromEnum(std.elf.PT.NULL)`
291pub const PT_NULL = @intFromEnum(std.elf.PT.NULL);
292/// Deprecated, use `@intFromEnum(std.elf.PT.LOAD)`
293pub const PT_LOAD = @intFromEnum(std.elf.PT.LOAD);
294/// Deprecated, use `@intFromEnum(std.elf.PT.DYNAMIC)`
295pub const PT_DYNAMIC = @intFromEnum(std.elf.PT.DYNAMIC);
296/// Deprecated, use `@intFromEnum(std.elf.PT.INTERP)`
297pub const PT_INTERP = @intFromEnum(std.elf.PT.INTERP);
298/// Deprecated, use `@intFromEnum(std.elf.PT.NOTE)`
299pub const PT_NOTE = @intFromEnum(std.elf.PT.NOTE);
300/// Deprecated, use `@intFromEnum(std.elf.PT.SHLIB)`
301pub const PT_SHLIB = @intFromEnum(std.elf.PT.SHLIB);
302/// Deprecated, use `@intFromEnum(std.elf.PT.PHDR)`
303pub const PT_PHDR = @intFromEnum(std.elf.PT.PHDR);
304/// Deprecated, use `@intFromEnum(std.elf.PT.TLS)`
305pub const PT_TLS = @intFromEnum(std.elf.PT.TLS);
306/// Deprecated, use `std.elf.PT.NUM`.
307pub const PT_NUM = PT.NUM;
308/// Deprecated, use `@intFromEnum(std.elf.PT.LOOS)`
309pub const PT_LOOS = @intFromEnum(std.elf.PT.LOOS);
310/// Deprecated, use `@intFromEnum(std.elf.PT.GNU_EH_FRAME)`
311pub const PT_GNU_EH_FRAME = @intFromEnum(std.elf.PT.GNU_EH_FRAME);
312/// Deprecated, use `@intFromEnum(std.elf.PT.GNU_STACK)`
313pub const PT_GNU_STACK = @intFromEnum(std.elf.PT.GNU_STACK);
314/// Deprecated, use `@intFromEnum(std.elf.PT.GNU_RELRO)`
315pub const PT_GNU_RELRO = @intFromEnum(std.elf.PT.GNU_RELRO);
316/// Deprecated, use `@intFromEnum(std.elf.PT.LOSUNW)`
317pub const PT_LOSUNW = @intFromEnum(std.elf.PT.LOSUNW);
318/// Deprecated, use `@intFromEnum(std.elf.PT.SUNWBSS)`
319pub const PT_SUNWBSS = @intFromEnum(std.elf.PT.SUNWBSS);
320/// Deprecated, use `@intFromEnum(std.elf.PT.SUNWSTACK)`
321pub const PT_SUNWSTACK = @intFromEnum(std.elf.PT.SUNWSTACK);
322/// Deprecated, use `@intFromEnum(std.elf.PT.HISUNW)`
323pub const PT_HISUNW = @intFromEnum(std.elf.PT.HISUNW);
324/// Deprecated, use `@intFromEnum(std.elf.PT.HIOS)`
325pub const PT_HIOS = @intFromEnum(std.elf.PT.HIOS);
326/// Deprecated, use `@intFromEnum(std.elf.PT.LOPROC)`
327pub const PT_LOPROC = @intFromEnum(std.elf.PT.LOPROC);
328/// Deprecated, use `@intFromEnum(std.elf.PT.HIPROC)`
329pub const PT_HIPROC = @intFromEnum(std.elf.PT.HIPROC);
330
331pub const PN_XNUM = 0xffff;
332
333/// Deprecated, use `@intFromEnum(std.elf.SHT.NULL)`
334pub const SHT_NULL = @intFromEnum(std.elf.SHT.NULL);
335/// Deprecated, use `@intFromEnum(std.elf.SHT.PROGBITS)`
336pub const SHT_PROGBITS = @intFromEnum(std.elf.SHT.PROGBITS);
337/// Deprecated, use `@intFromEnum(std.elf.SHT.SYMTAB)`
338pub const SHT_SYMTAB = @intFromEnum(std.elf.SHT.SYMTAB);
339/// Deprecated, use `@intFromEnum(std.elf.SHT.STRTAB)`
340pub const SHT_STRTAB = @intFromEnum(std.elf.SHT.STRTAB);
341/// Deprecated, use `@intFromEnum(std.elf.SHT.RELA)`
342pub const SHT_RELA = @intFromEnum(std.elf.SHT.RELA);
343/// Deprecated, use `@intFromEnum(std.elf.SHT.HASH)`
344pub const SHT_HASH = @intFromEnum(std.elf.SHT.HASH);
345/// Deprecated, use `@intFromEnum(std.elf.SHT.DYNAMIC)`
346pub const SHT_DYNAMIC = @intFromEnum(std.elf.SHT.DYNAMIC);
347/// Deprecated, use `@intFromEnum(std.elf.SHT.NOTE)`
348pub const SHT_NOTE = @intFromEnum(std.elf.SHT.NOTE);
349/// Deprecated, use `@intFromEnum(std.elf.SHT.NOBITS)`
350pub const SHT_NOBITS = @intFromEnum(std.elf.SHT.NOBITS);
351/// Deprecated, use `@intFromEnum(std.elf.SHT.REL)`
352pub const SHT_REL = @intFromEnum(std.elf.SHT.REL);
353/// Deprecated, use `@intFromEnum(std.elf.SHT.SHLIB)`
354pub const SHT_SHLIB = @intFromEnum(std.elf.SHT.SHLIB);
355/// Deprecated, use `@intFromEnum(std.elf.SHT.DYNSYM)`
356pub const SHT_DYNSYM = @intFromEnum(std.elf.SHT.DYNSYM);
357/// Deprecated, use `@intFromEnum(std.elf.SHT.INIT_ARRAY)`
358pub const SHT_INIT_ARRAY = @intFromEnum(std.elf.SHT.INIT_ARRAY);
359/// Deprecated, use `@intFromEnum(std.elf.SHT.FINI_ARRAY)`
360pub const SHT_FINI_ARRAY = @intFromEnum(std.elf.SHT.FINI_ARRAY);
361/// Deprecated, use `@intFromEnum(std.elf.SHT.PREINIT_ARRAY)`
362pub const SHT_PREINIT_ARRAY = @intFromEnum(std.elf.SHT.PREINIT_ARRAY);
363/// Deprecated, use `@intFromEnum(std.elf.SHT.GROUP)`
364pub const SHT_GROUP = @intFromEnum(std.elf.SHT.GROUP);
365/// Deprecated, use `@intFromEnum(std.elf.SHT.SYMTAB_SHNDX)`
366pub const SHT_SYMTAB_SHNDX = @intFromEnum(std.elf.SHT.SYMTAB_SHNDX);
367/// Deprecated, use `@intFromEnum(std.elf.SHT.RELR)`
368pub const SHT_RELR = @intFromEnum(std.elf.SHT.RELR);
369/// Deprecated, use `std.elf.SHT.NUM`.
370pub const SHT_NUM = SHT.NUM;
371/// Deprecated, use `@intFromEnum(std.elf.SHT.LOOS)`
372pub const SHT_LOOS = @intFromEnum(std.elf.SHT.LOOS);
373/// Deprecated, use `@intFromEnum(std.elf.SHT.LLVM_ADDRSIG)`
374pub const SHT_LLVM_ADDRSIG = @intFromEnum(std.elf.SHT.LLVM_ADDRSIG);
375/// Deprecated, use `@intFromEnum(std.elf.SHT.GNU_HASH)`
376pub const SHT_GNU_HASH = @intFromEnum(std.elf.SHT.GNU_HASH);
377/// Deprecated, use `@intFromEnum(std.elf.SHT.GNU_VERDEF)`
378pub const SHT_GNU_VERDEF = @intFromEnum(std.elf.SHT.GNU_VERDEF);
379/// Deprecated, use `@intFromEnum(std.elf.SHT.GNU_VERNEED)`
380pub const SHT_GNU_VERNEED = @intFromEnum(std.elf.SHT.GNU_VERNEED);
381/// Deprecated, use `@intFromEnum(std.elf.SHT.GNU_VERSYM)`
382pub const SHT_GNU_VERSYM = @intFromEnum(std.elf.SHT.GNU_VERSYM);
383/// Deprecated, use `@intFromEnum(std.elf.SHT.HIOS)`
384pub const SHT_HIOS = @intFromEnum(std.elf.SHT.HIOS);
385/// Deprecated, use `@intFromEnum(std.elf.SHT.LOPROC)`
386pub const SHT_LOPROC = @intFromEnum(std.elf.SHT.LOPROC);
387/// Deprecated, use `@intFromEnum(std.elf.SHT.X86_64_UNWIND)`
388pub const SHT_X86_64_UNWIND = @intFromEnum(std.elf.SHT.X86_64_UNWIND);
389/// Deprecated, use `@intFromEnum(std.elf.SHT.HIPROC)`
390pub const SHT_HIPROC = @intFromEnum(std.elf.SHT.HIPROC);
391/// Deprecated, use `@intFromEnum(std.elf.SHT.LOUSER)`
392pub const SHT_LOUSER = @intFromEnum(std.elf.SHT.LOUSER);
393/// Deprecated, use `@intFromEnum(std.elf.SHT.HIUSER)`
394pub const SHT_HIUSER = @intFromEnum(std.elf.SHT.HIUSER);
395
396// Note type for .note.gnu.build_id
397pub const NT_GNU_BUILD_ID = 3;
398
399/// Deprecated, use `@intFromEnum(std.elf.STB.LOCAL)`
400pub const STB_LOCAL = @intFromEnum(STB.LOCAL);
401/// Deprecated, use `@intFromEnum(std.elf.STB.GLOBAL)`
402pub const STB_GLOBAL = @intFromEnum(STB.GLOBAL);
403/// Deprecated, use `@intFromEnum(std.elf.STB.WEAK)`
404pub const STB_WEAK = @intFromEnum(STB.WEAK);
405/// Deprecated, use `std.elf.STB.NUM`
406pub const STB_NUM = STB.NUM;
407/// Deprecated, use `@intFromEnum(std.elf.STB.LOOS)`
408pub const STB_LOOS = @intFromEnum(STB.LOOS);
409/// Deprecated, use `@intFromEnum(std.elf.STB.GNU_UNIQUE)`
410pub const STB_GNU_UNIQUE = @intFromEnum(STB.GNU_UNIQUE);
411/// Deprecated, use `@intFromEnum(std.elf.STB.HIOS)`
412pub const STB_HIOS = @intFromEnum(STB.HIOS);
413/// Deprecated, use `@intFromEnum(std.elf.STB.LOPROC)`
414pub const STB_LOPROC = @intFromEnum(STB.LOPROC);
415/// Deprecated, use `@intFromEnum(std.elf.STB.HIPROC)`
416pub const STB_HIPROC = @intFromEnum(STB.HIPROC);
417
418/// Deprecated, use `@intFromEnum(std.elf.STB.MIPS_SPLIT_COMMON)`
419pub const STB_MIPS_SPLIT_COMMON = @intFromEnum(STB.MIBS_SPLIT_COMMON);
420
421/// Deprecated, use `@intFromEnum(std.elf.STT.NOTYPE)`
422pub const STT_NOTYPE = @intFromEnum(STT.NOTYPE);
423/// Deprecated, use `@intFromEnum(std.elf.STT.OBJECT)`
424pub const STT_OBJECT = @intFromEnum(STT.OBJECT);
425/// Deprecated, use `@intFromEnum(std.elf.STT.FUNC)`
426pub const STT_FUNC = @intFromEnum(STT.FUNC);
427/// Deprecated, use `@intFromEnum(std.elf.STT.SECTION)`
428pub const STT_SECTION = @intFromEnum(STT.SECTION);
429/// Deprecated, use `@intFromEnum(std.elf.STT.FILE)`
430pub const STT_FILE = @intFromEnum(STT.FILE);
431/// Deprecated, use `@intFromEnum(std.elf.STT.COMMON)`
432pub const STT_COMMON = @intFromEnum(STT.COMMON);
433/// Deprecated, use `@intFromEnum(std.elf.STT.TLS)`
434pub const STT_TLS = @intFromEnum(STT.TLS);
435/// Deprecated, use `std.elf.STT.NUM`
436pub const STT_NUM = STT.NUM;
437/// Deprecated, use `@intFromEnum(std.elf.STT.LOOS)`
438pub const STT_LOOS = @intFromEnum(STT.LOOS);
439/// Deprecated, use `@intFromEnum(std.elf.STT.GNU_IFUNC)`
440pub const STT_GNU_IFUNC = @intFromEnum(STT.GNU_IFUNC);
441/// Deprecated, use `@intFromEnum(std.elf.STT.HIOS)`
442pub const STT_HIOS = @intFromEnum(STT.HIOS);
443/// Deprecated, use `@intFromEnum(std.elf.STT.LOPROC)`
444pub const STT_LOPROC = @intFromEnum(STT.LOPROC);
445/// Deprecated, use `@intFromEnum(std.elf.STT.HIPROC)`
446pub const STT_HIPROC = @intFromEnum(STT.HIPROC);
447
448/// Deprecated, use `@intFromEnum(std.elf.STT.SPARC_REGISTER)`
449pub const STT_SPARC_REGISTER = @intFromEnum(STT.SPARC_REGISTER);
450
451/// Deprecated, use `@intFromEnum(std.elf.STT.PARISC_MILLICODE)`
452pub const STT_PARISC_MILLICODE = @intFromEnum(STT.PARISC_MILLICODE);
453
454/// Deprecated, use `@intFromEnum(std.elf.STT.HP_OPAQUE)`
455pub const STT_HP_OPAQUE = @intFromEnum(STT.HP_OPAQUE);
456/// Deprecated, use `@intFromEnum(std.elf.STT.HP_STUB)`
457pub const STT_HP_STUB = @intFromEnum(STT.HP_STUB);
458
459/// Deprecated, use `@intFromEnum(std.elf.STT.ARM_TFUNC)`
460pub const STT_ARM_TFUNC = @intFromEnum(STT.ARM_TFUNC);
461/// Deprecated, use `@intFromEnum(std.elf.STT.ARM_16BIT)`
462pub const STT_ARM_16BIT = @intFromEnum(STT.ARM_16BIT);
463
464pub const PT = enum(Word) {
465 /// Program header table entry unused
466 NULL = 0,
467 /// Loadable program segment
468 LOAD = 1,
469 /// Dynamic linking information
470 DYNAMIC = 2,
471 /// Program interpreter
472 INTERP = 3,
473 /// Auxiliary information
474 NOTE = 4,
475 /// Reserved
476 SHLIB = 5,
477 /// Entry for header table itself
478 PHDR = 6,
479 /// Thread-local storage segment
480 TLS = 7,
481 _,
482
483 /// Number of defined types
484 pub const NUM = @typeInfo(PT).@"enum".fields.len;
485
486 /// Start of OS-specific
487 pub const LOOS: PT = @enumFromInt(0x60000000);
488 /// End of OS-specific
489 pub const HIOS: PT = @enumFromInt(0x6fffffff);
490
491 /// GCC .eh_frame_hdr segment
492 pub const GNU_EH_FRAME: PT = @enumFromInt(0x6474e550);
493 /// Indicates stack executability
494 pub const GNU_STACK: PT = @enumFromInt(0x6474e551);
495 /// Read-only after relocation
496 pub const GNU_RELRO: PT = @enumFromInt(0x6474e552);
497
498 pub const LOSUNW: PT = @enumFromInt(0x6ffffffa);
499 pub const HISUNW: PT = @enumFromInt(0x6fffffff);
500
501 /// Sun specific segment
502 pub const SUNWBSS: PT = @enumFromInt(0x6ffffffa);
503 /// Stack segment
504 pub const SUNWSTACK: PT = @enumFromInt(0x6ffffffb);
505
506 /// Start of processor-specific
507 pub const LOPROC: PT = @enumFromInt(0x70000000);
508 /// End of processor-specific
509 pub const HIPROC: PT = @enumFromInt(0x7fffffff);
510};
511
512pub const SHT = enum(Word) {
513 /// Section header table entry unused
514 NULL = 0,
515 /// Program data
516 PROGBITS = 1,
517 /// Symbol table
518 SYMTAB = 2,
519 /// String table
520 STRTAB = 3,
521 /// Relocation entries with addends
522 RELA = 4,
523 /// Symbol hash table
524 HASH = 5,
525 /// Dynamic linking information
526 DYNAMIC = 6,
527 /// Notes
528 NOTE = 7,
529 /// Program space with no data (bss)
530 NOBITS = 8,
531 /// Relocation entries, no addends
532 REL = 9,
533 /// Reserved
534 SHLIB = 10,
535 /// Dynamic linker symbol table
536 DYNSYM = 11,
537 /// Array of constructors
538 INIT_ARRAY = 14,
539 /// Array of destructors
540 FINI_ARRAY = 15,
541 /// Array of pre-constructors
542 PREINIT_ARRAY = 16,
543 /// Section group
544 GROUP = 17,
545 /// Extended section indices
546 SYMTAB_SHNDX = 18,
547 /// RELR relative relocations
548 RELR = 19,
549 _,
550
551 /// Number of defined types
552 pub const NUM = @typeInfo(SHT).@"enum".fields.len;
553
554 /// Start of OS-specific
555 pub const LOOS: SHT = @enumFromInt(0x60000000);
556 /// End of OS-specific
557 pub const HIOS: SHT = @enumFromInt(0x6fffffff);
558
559 /// LLVM address-significance table
560 pub const LLVM_ADDRSIG: SHT = @enumFromInt(0x6fff4c03);
561
562 /// GNU hash table
563 pub const GNU_HASH: SHT = @enumFromInt(0x6ffffff6);
564 /// GNU version definition table
565 pub const GNU_VERDEF: SHT = @enumFromInt(0x6ffffffd);
566 /// GNU needed versions table
567 pub const GNU_VERNEED: SHT = @enumFromInt(0x6ffffffe);
568 /// GNU symbol version table
569 pub const GNU_VERSYM: SHT = @enumFromInt(0x6fffffff);
570
571 /// Start of processor-specific
572 pub const LOPROC: SHT = @enumFromInt(0x70000000);
573 /// End of processor-specific
574 pub const HIPROC: SHT = @enumFromInt(0x7fffffff);
575
576 /// Unwind information
577 pub const X86_64_UNWIND: SHT = @enumFromInt(0x70000001);
578
579 /// Start of application-specific
580 pub const LOUSER: SHT = @enumFromInt(0x80000000);
581 /// End of application-specific
582 pub const HIUSER: SHT = @enumFromInt(0xffffffff);
583};
584
585pub const STB = enum(u4) {
586 /// Local symbol
587 LOCAL = 0,
588 /// Global symbol
589 GLOBAL = 1,
590 /// Weak symbol
591 WEAK = 2,
592 _,
593
594 /// Number of defined types
595 pub const NUM = @typeInfo(STB).@"enum".fields.len;
596
597 /// Start of OS-specific
598 pub const LOOS: STB = @enumFromInt(10);
599 /// End of OS-specific
600 pub const HIOS: STB = @enumFromInt(12);
601
602 /// Unique symbol
603 pub const GNU_UNIQUE: STB = @enumFromInt(@intFromEnum(LOOS) + 0);
604
605 /// Start of processor-specific
606 pub const LOPROC: STB = @enumFromInt(13);
607 /// End of processor-specific
608 pub const HIPROC: STB = @enumFromInt(15);
609
610 pub const MIPS_SPLIT_COMMON: STB = @enumFromInt(@intFromEnum(LOPROC) + 0);
611};
612
613pub const STT = enum(u4) {
614 /// Symbol type is unspecified
615 NOTYPE = 0,
616 /// Symbol is a data object
617 OBJECT = 1,
618 /// Symbol is a code object
619 FUNC = 2,
620 /// Symbol associated with a section
621 SECTION = 3,
622 /// Symbol's name is file name
623 FILE = 4,
624 /// Symbol is a common data object
625 COMMON = 5,
626 /// Symbol is thread-local data object
627 TLS = 6,
628 _,
629
630 /// Number of defined types
631 pub const NUM = @typeInfo(STT).@"enum".fields.len;
632
633 /// Start of OS-specific
634 pub const LOOS: STT = @enumFromInt(10);
635 /// End of OS-specific
636 pub const HIOS: STT = @enumFromInt(12);
637
638 /// Symbol is indirect code object
639 pub const GNU_IFUNC: STT = @enumFromInt(@intFromEnum(LOOS) + 0);
640
641 pub const HP_OPAQUE: STT = @enumFromInt(@intFromEnum(LOOS) + 1);
642 pub const HP_STUB: STT = @enumFromInt(@intFromEnum(LOOS) + 2);
643
644 /// Start of processor-specific
645 pub const LOPROC: STT = @enumFromInt(13);
646 /// End of processor-specific
647 pub const HIPROC: STT = @enumFromInt(15);
648
649 pub const SPARC_REGISTER: STT = @enumFromInt(@intFromEnum(LOPROC) + 0);
650
651 pub const PARISC_MILLICODE: STT = @enumFromInt(@intFromEnum(LOPROC) + 0);
652
653 pub const ARM_TFUNC: STT = @enumFromInt(@intFromEnum(LOPROC) + 0);
654 pub const ARM_16BIT: STT = @enumFromInt(@intFromEnum(HIPROC) + 2);
655};
656
657pub const STV = enum(u3) {
658 DEFAULT = 0,
659 INTERNAL = 1,
660 HIDDEN = 2,
661 PROTECTED = 3,
662};
663
664pub const MAGIC = "\x7fELF";
665
666/// File types
667pub const ET = enum(u16) {
668 /// No file type
669 NONE = 0,
670
671 /// Relocatable file
672 REL = 1,
673
674 /// Executable file
675 EXEC = 2,
676
677 /// Shared object file
678 DYN = 3,
679
680 /// Core file
681 CORE = 4,
682
683 _,
684
685 /// Beginning of OS-specific codes
686 pub const LOOS = 0xfe00;
687
688 /// End of OS-specific codes
689 pub const HIOS = 0xfeff;
690
691 /// Beginning of processor-specific codes
692 pub const LOPROC = 0xff00;
693
694 /// End of processor-specific codes
695 pub const HIPROC = 0xffff;
696};
697
698/// All integers are native endian.
699pub const Header = struct {
700 is_64: bool,
701 endian: Endian,
702 os_abi: OSABI,
703 /// The meaning of this value depends on `os_abi`.
704 abi_version: u8,
705 type: ET,
706 machine: EM,
707 entry: u64,
708 phoff: u64,
709 shoff: u64,
710 phentsize: u16,
711 phnum: u16,
712 shentsize: u16,
713 shnum: u16,
714 shstrndx: u16,
715
716 pub fn iterateProgramHeaders(h: *const Header, file_reader: *Io.File.Reader) ProgramHeaderIterator {
717 return .{
718 .is_64 = h.is_64,
719 .endian = h.endian,
720 .phnum = h.phnum,
721 .phoff = h.phoff,
722 .file_reader = file_reader,
723 };
724 }
725
726 pub fn iterateProgramHeadersBuffer(h: *const Header, buf: []const u8) ProgramHeaderBufferIterator {
727 return .{
728 .is_64 = h.is_64,
729 .endian = h.endian,
730 .phnum = h.phnum,
731 .phoff = h.phoff,
732 .buf = buf,
733 };
734 }
735
736 pub fn iterateSectionHeaders(h: *const Header, file_reader: *Io.File.Reader) SectionHeaderIterator {
737 return .{
738 .is_64 = h.is_64,
739 .endian = h.endian,
740 .shnum = h.shnum,
741 .shoff = h.shoff,
742 .file_reader = file_reader,
743 };
744 }
745
746 pub fn iterateSectionHeadersBuffer(h: *const Header, buf: []const u8) SectionHeaderBufferIterator {
747 return .{
748 .is_64 = h.is_64,
749 .endian = h.endian,
750 .shnum = h.shnum,
751 .shoff = h.shoff,
752 .buf = buf,
753 };
754 }
755
756 pub fn iterateDynamicSection(
757 h: *const Header,
758 file_reader: *Io.File.Reader,
759 offset: u64,
760 size: u64,
761 ) DynamicSectionIterator {
762 return .{
763 .is_64 = h.is_64,
764 .endian = h.endian,
765 .offset = offset,
766 .end_offset = offset + size,
767 .file_reader = file_reader,
768 };
769 }
770
771 pub fn iterateDynamicSectionBuffer(
772 h: *const Header,
773 buf: []const u8,
774 offset: u64,
775 size: u64,
776 ) DynamicSectionBufferIterator {
777 return .{
778 .is_64 = h.is_64,
779 .endian = h.endian,
780 .offset = offset,
781 .end_offset = offset + size,
782 .buf = buf,
783 };
784 }
785
786 pub const ReadError = Io.Reader.Error || error{
787 InvalidElfMagic,
788 InvalidElfVersion,
789 InvalidElfClass,
790 InvalidElfEndian,
791 };
792
793 /// If this function fails, seek position of `r` is unchanged.
794 pub fn read(r: *Io.Reader) ReadError!Header {
795 const buf = try r.peek(@sizeOf(Elf64_Ehdr));
796
797 if (!mem.eql(u8, buf[0..4], MAGIC)) return error.InvalidElfMagic;
798 if (buf[EI.VERSION] != 1) return error.InvalidElfVersion;
799
800 const endian: Endian = switch (buf[EI.DATA]) {
801 ELFDATA2LSB => .little,
802 ELFDATA2MSB => .big,
803 else => return error.InvalidElfEndian,
804 };
805
806 return switch (buf[EI.CLASS]) {
807 ELFCLASS32 => .init(try r.takeStruct(Elf32_Ehdr, endian), endian),
808 ELFCLASS64 => .init(try r.takeStruct(Elf64_Ehdr, endian), endian),
809 else => return error.InvalidElfClass,
810 };
811 }
812
813 pub fn init(hdr: anytype, endian: Endian) Header {
814 // Converting integers to exhaustive enums using `@enumFromInt` could cause a panic.
815 comptime assert(!@typeInfo(OSABI).@"enum".is_exhaustive);
816 return .{
817 .is_64 = switch (@TypeOf(hdr)) {
818 Elf32_Ehdr => false,
819 Elf64_Ehdr => true,
820 else => @compileError("bad type"),
821 },
822 .endian = endian,
823 .os_abi = @enumFromInt(hdr.e_ident[EI.OSABI]),
824 .abi_version = hdr.e_ident[EI.ABIVERSION],
825 .type = hdr.e_type,
826 .machine = hdr.e_machine,
827 .entry = hdr.e_entry,
828 .phoff = hdr.e_phoff,
829 .shoff = hdr.e_shoff,
830 .phentsize = hdr.e_phentsize,
831 .phnum = hdr.e_phnum,
832 .shentsize = hdr.e_shentsize,
833 .shnum = hdr.e_shnum,
834 .shstrndx = hdr.e_shstrndx,
835 };
836 }
837};
838
839pub const ProgramHeaderIterator = struct {
840 is_64: bool,
841 endian: Endian,
842 phnum: u16,
843 phoff: u64,
844
845 file_reader: *Io.File.Reader,
846 index: usize = 0,
847
848 pub fn next(it: *ProgramHeaderIterator) !?Elf64_Phdr {
849 if (it.index >= it.phnum) return null;
850 defer it.index += 1;
851
852 const size: u64 = if (it.is_64) @sizeOf(Elf64_Phdr) else @sizeOf(Elf32_Phdr);
853 const offset = it.phoff + size * it.index;
854 try it.file_reader.seekTo(offset);
855
856 return try takeProgramHeader(&it.file_reader.interface, it.is_64, it.endian);
857 }
858};
859
860pub const ProgramHeaderBufferIterator = struct {
861 is_64: bool,
862 endian: Endian,
863 phnum: u16,
864 phoff: u64,
865
866 buf: []const u8,
867 index: usize = 0,
868
869 pub fn next(it: *ProgramHeaderBufferIterator) !?Elf64_Phdr {
870 if (it.index >= it.phnum) return null;
871 defer it.index += 1;
872
873 const size: u64 = if (it.is_64) @sizeOf(Elf64_Phdr) else @sizeOf(Elf32_Phdr);
874 const offset = it.phoff + size * it.index;
875 var reader = Io.Reader.fixed(it.buf[offset..]);
876
877 return try takeProgramHeader(&reader, it.is_64, it.endian);
878 }
879};
880
881pub fn takeProgramHeader(reader: *Io.Reader, is_64: bool, endian: Endian) !Elf64_Phdr {
882 if (is_64) {
883 const phdr = try reader.takeStruct(Elf64_Phdr, endian);
884 return phdr;
885 }
886
887 const phdr = try reader.takeStruct(Elf32_Phdr, endian);
888 return .{
889 .p_type = phdr.p_type,
890 .p_offset = phdr.p_offset,
891 .p_vaddr = phdr.p_vaddr,
892 .p_paddr = phdr.p_paddr,
893 .p_filesz = phdr.p_filesz,
894 .p_memsz = phdr.p_memsz,
895 .p_flags = phdr.p_flags,
896 .p_align = phdr.p_align,
897 };
898}
899
900pub const SectionHeaderIterator = struct {
901 is_64: bool,
902 endian: Endian,
903 shnum: u16,
904 shoff: u64,
905
906 file_reader: *Io.File.Reader,
907 index: usize = 0,
908
909 pub fn next(it: *SectionHeaderIterator) !?Elf64_Shdr {
910 if (it.index >= it.shnum) return null;
911 defer it.index += 1;
912
913 const size: u64 = if (it.is_64) @sizeOf(Elf64_Shdr) else @sizeOf(Elf32_Shdr);
914 const offset = it.shoff + size * it.index;
915 try it.file_reader.seekTo(offset);
916
917 return try takeSectionHeader(&it.file_reader.interface, it.is_64, it.endian);
918 }
919};
920
921pub const SectionHeaderBufferIterator = struct {
922 is_64: bool,
923 endian: Endian,
924 shnum: u16,
925 shoff: u64,
926
927 buf: []const u8,
928 index: usize = 0,
929
930 pub fn next(it: *SectionHeaderBufferIterator) !?Elf64_Shdr {
931 if (it.index >= it.shnum) return null;
932 defer it.index += 1;
933
934 const size: u64 = if (it.is_64) @sizeOf(Elf64_Shdr) else @sizeOf(Elf32_Shdr);
935 const offset = it.shoff + size * it.index;
936 if (offset > it.buf.len) return error.EndOfStream;
937 var reader = Io.Reader.fixed(it.buf[@intCast(offset)..]);
938
939 return try takeSectionHeader(&reader, it.is_64, it.endian);
940 }
941};
942
943pub fn takeSectionHeader(reader: *Io.Reader, is_64: bool, endian: Endian) !Elf64_Shdr {
944 if (is_64) {
945 const shdr = try reader.takeStruct(Elf64_Shdr, endian);
946 return shdr;
947 }
948
949 const shdr = try reader.takeStruct(Elf32_Shdr, endian);
950 return .{
951 .sh_name = shdr.sh_name,
952 .sh_type = shdr.sh_type,
953 .sh_flags = shdr.sh_flags,
954 .sh_addr = shdr.sh_addr,
955 .sh_offset = shdr.sh_offset,
956 .sh_size = shdr.sh_size,
957 .sh_link = shdr.sh_link,
958 .sh_info = shdr.sh_info,
959 .sh_addralign = shdr.sh_addralign,
960 .sh_entsize = shdr.sh_entsize,
961 };
962}
963
964pub const DynamicSectionIterator = struct {
965 is_64: bool,
966 endian: Endian,
967 offset: u64,
968 end_offset: u64,
969
970 file_reader: *Io.File.Reader,
971
972 pub fn next(it: *DynamicSectionIterator) !?Elf64_Dyn {
973 if (it.offset >= it.end_offset) return null;
974 const size: u64 = if (it.is_64) @sizeOf(Elf64_Dyn) else @sizeOf(Elf32_Dyn);
975 defer it.offset += size;
976 try it.file_reader.seekTo(it.offset);
977 return try takeDynamicSection(&it.file_reader.interface, it.is_64, it.endian);
978 }
979};
980
981pub const DynamicSectionBufferIterator = struct {
982 is_64: bool,
983 endian: Endian,
984 offset: u64,
985 end_offset: u64,
986
987 buf: []const u8,
988
989 pub fn next(it: *DynamicSectionBufferIterator) !?Elf64_Dyn {
990 if (it.offset >= it.end_offset) return null;
991 const size: u64 = if (it.is_64) @sizeOf(Elf64_Dyn) else @sizeOf(Elf32_Dyn);
992 defer it.offset += size;
993 var reader: std.Io.Reader = .fixed(it.buf[it.offset..]);
994 return try takeDynamicSection(&reader, it.is_64, it.endian);
995 }
996};
997
998pub fn takeDynamicSection(reader: *Io.Reader, is_64: bool, endian: Endian) !Elf64_Dyn {
999 if (is_64) {
1000 const dyn = try reader.takeStruct(Elf64_Dyn, endian);
1001 return dyn;
1002 }
1003
1004 const dyn = try reader.takeStruct(Elf32_Dyn, endian);
1005 return .{
1006 .d_tag = dyn.d_tag,
1007 .d_val = dyn.d_val,
1008 };
1009}
1010
1011pub const EI = struct {
1012 pub const CLASS = 4;
1013 pub const DATA = 5;
1014 pub const VERSION = 6;
1015 pub const OSABI = 7;
1016 pub const ABIVERSION = 8;
1017 pub const PAD = 9;
1018 pub const NIDENT = 16;
1019};
1020
1021/// Deprecated, use `std.elf.EI.CLASS`
1022pub const EI_CLASS = EI.CLASS;
1023/// Deprecated, use `std.elf.EI.DATA`
1024pub const EI_DATA = EI.DATA;
1025/// Deprecated, use `std.elf.EI.VERSION`
1026pub const EI_VERSION = EI.VERSION;
1027/// Deprecated, use `std.elf.EI.OSABI`
1028pub const EI_OSABI = EI.OSABI;
1029/// Deprecated, use `std.elf.EI.ABIVERSION`
1030pub const EI_ABIVERSION = EI.ABIVERSION;
1031/// Deprecated, use `std.elf.EI.PAD`
1032pub const EI_PAD = EI.PAD;
1033/// Deprecated, use `std.elf.EI.NIDENT`
1034pub const EI_NIDENT = EI.NIDENT;
1035
1036pub const Half = u16;
1037pub const Word = u32;
1038pub const Sword = i32;
1039pub const Xword = u64;
1040pub const Sxword = i64;
1041pub const Section = u16;
1042pub const Elf32 = struct {
1043 pub const Addr = u32;
1044 pub const Off = u32;
1045 pub const Ehdr = extern struct {
1046 ident: [EI.NIDENT]u8,
1047 type: ET,
1048 machine: EM,
1049 version: Word,
1050 entry: Elf32.Addr,
1051 phoff: Elf32.Off,
1052 shoff: Elf32.Off,
1053 flags: Word,
1054 ehsize: Half,
1055 phentsize: Half,
1056 phnum: Half,
1057 shentsize: Half,
1058 shnum: Half,
1059 shstrndx: Half,
1060 };
1061 pub const Phdr = extern struct {
1062 type: PT,
1063 offset: Elf32.Off,
1064 vaddr: Elf32.Addr,
1065 paddr: Elf32.Addr,
1066 filesz: Word,
1067 memsz: Word,
1068 flags: PF,
1069 @"align": Word,
1070 };
1071 pub const Shdr = extern struct {
1072 name: Word,
1073 type: SHT,
1074 flags: packed struct { shf: SHF },
1075 addr: Elf32.Addr,
1076 offset: Elf32.Off,
1077 size: Word,
1078 link: Word,
1079 info: Word,
1080 addralign: Word,
1081 entsize: Word,
1082 };
1083 pub const Chdr = extern struct {
1084 type: COMPRESS,
1085 size: Word,
1086 addralign: Word,
1087 };
1088 pub const Sym = extern struct {
1089 name: Word,
1090 value: Elf32.Addr,
1091 size: Word,
1092 info: Info,
1093 other: Other,
1094 shndx: Section,
1095
1096 pub const Info = packed struct(u8) {
1097 type: STT,
1098 bind: STB,
1099 };
1100
1101 pub const Other = packed struct(u8) {
1102 visibility: STV,
1103 unused: u5 = 0,
1104 };
1105 };
1106 pub const Rel = extern struct {
1107 offset: Elf32.Addr,
1108 info: Info,
1109 addend: u0 = 0,
1110
1111 pub const Info = packed struct(u32) {
1112 type: u8,
1113 sym: u24,
1114 };
1115 };
1116 pub const Rela = extern struct {
1117 offset: Elf32.Addr,
1118 info: Info,
1119 addend: i32,
1120
1121 pub const Info = Elf32.Rel.Info;
1122 };
1123 comptime {
1124 assert(@sizeOf(Elf32.Ehdr) == 52);
1125 assert(@sizeOf(Elf32.Phdr) == 32);
1126 assert(@sizeOf(Elf32.Shdr) == 40);
1127 assert(@sizeOf(Elf32.Sym) == 16);
1128 assert(@sizeOf(Elf32.Rel) == 8);
1129 assert(@sizeOf(Elf32.Rela) == 12);
1130 }
1131};
1132pub const Elf64 = struct {
1133 pub const Addr = u64;
1134 pub const Off = u64;
1135 pub const Ehdr = extern struct {
1136 ident: [EI.NIDENT]u8,
1137 type: ET,
1138 machine: EM,
1139 version: Word,
1140 entry: Elf64.Addr,
1141 phoff: Elf64.Off,
1142 shoff: Elf64.Off,
1143 flags: Word,
1144 ehsize: Half,
1145 phentsize: Half,
1146 phnum: Half,
1147 shentsize: Half,
1148 shnum: Half,
1149 shstrndx: Half,
1150 };
1151 pub const Phdr = extern struct {
1152 type: PT,
1153 flags: PF,
1154 offset: Elf64.Off,
1155 vaddr: Elf64.Addr,
1156 paddr: Elf64.Addr,
1157 filesz: Xword,
1158 memsz: Xword,
1159 @"align": Xword,
1160 };
1161 pub const Shdr = extern struct {
1162 name: Word,
1163 type: SHT,
1164 flags: packed struct { shf: SHF, unused: Word = 0 },
1165 addr: Elf64.Addr,
1166 offset: Elf64.Off,
1167 size: Xword,
1168 link: Word,
1169 info: Word,
1170 addralign: Xword,
1171 entsize: Xword,
1172 };
1173 pub const Chdr = extern struct {
1174 type: COMPRESS,
1175 reserved: Word = 0,
1176 size: Xword,
1177 addralign: Xword,
1178 };
1179 pub const Sym = extern struct {
1180 name: Word,
1181 info: Info,
1182 other: Other,
1183 shndx: Section,
1184 value: Elf64.Addr,
1185 size: Xword,
1186
1187 pub const Info = Elf32.Sym.Info;
1188 pub const Other = Elf32.Sym.Other;
1189 };
1190 pub const Rel = extern struct {
1191 offset: Elf64.Addr,
1192 info: Info,
1193 addend: u0 = 0,
1194
1195 pub const Info = packed struct(u64) {
1196 type: u32,
1197 sym: u32,
1198 };
1199 };
1200 pub const Rela = extern struct {
1201 offset: Elf64.Addr,
1202 info: Info,
1203 addend: i64,
1204
1205 pub const Info = Elf64.Rel.Info;
1206 };
1207 comptime {
1208 assert(@sizeOf(Elf64.Ehdr) == 64);
1209 assert(@sizeOf(Elf64.Phdr) == 56);
1210 assert(@sizeOf(Elf64.Shdr) == 64);
1211 assert(@sizeOf(Elf64.Sym) == 24);
1212 assert(@sizeOf(Elf64.Rel) == 16);
1213 assert(@sizeOf(Elf64.Rela) == 24);
1214 }
1215};
1216pub const ElfN = switch (@sizeOf(usize)) {
1217 4 => Elf32,
1218 8 => Elf64,
1219 else => @compileError("expected pointer size of 32 or 64"),
1220};
1221
1222/// Deprecated, use `std.elf.Xword`
1223pub const Elf32_Xword = Xword;
1224/// Deprecated, use `std.elf.Sxword`
1225pub const Elf32_Sxword = Sxword;
1226/// Deprecated, use `std.elf.Xword`
1227pub const Elf64_Xword = Xword;
1228/// Deprecated, use `std.elf.Sxword`
1229pub const Elf64_Sxword = i64;
1230/// Deprecated, use `std.elf.Elf32.Addr`
1231pub const Elf32_Addr = u32;
1232/// Deprecated, use `std.elf.Elf64.Addr`
1233pub const Elf64_Addr = u64;
1234/// Deprecated, use `std.elf.Elf32.Off`
1235pub const Elf32_Off = u32;
1236/// Deprecated, use `std.elf.Elf64.Off`
1237pub const Elf64_Off = u64;
1238/// Deprecated, use `std.elf.Section`
1239pub const Elf32_Section = u16;
1240/// Deprecated, use `std.elf.Section`
1241pub const Elf64_Section = u16;
1242/// Deprecated, use `std.elf.Elf32.Ehdr`
1243pub const Elf32_Ehdr = extern struct {
1244 e_ident: [EI_NIDENT]u8,
1245 e_type: ET,
1246 e_machine: EM,
1247 e_version: Word,
1248 e_entry: Elf32_Addr,
1249 e_phoff: Elf32_Off,
1250 e_shoff: Elf32_Off,
1251 e_flags: Word,
1252 e_ehsize: Half,
1253 e_phentsize: Half,
1254 e_phnum: Half,
1255 e_shentsize: Half,
1256 e_shnum: Half,
1257 e_shstrndx: Half,
1258};
1259/// Deprecated, use `std.elf.Elf64.Ehdr`
1260pub const Elf64_Ehdr = extern struct {
1261 e_ident: [EI.NIDENT]u8,
1262 e_type: ET,
1263 e_machine: EM,
1264 e_version: Word,
1265 e_entry: Elf64_Addr,
1266 e_phoff: Elf64_Off,
1267 e_shoff: Elf64_Off,
1268 e_flags: Word,
1269 e_ehsize: Half,
1270 e_phentsize: Half,
1271 e_phnum: Half,
1272 e_shentsize: Half,
1273 e_shnum: Half,
1274 e_shstrndx: Half,
1275};
1276/// Deprecated, use `std.elf.Elf32.Phdr`
1277pub const Elf32_Phdr = extern struct {
1278 p_type: Word,
1279 p_offset: Elf32_Off,
1280 p_vaddr: Elf32_Addr,
1281 p_paddr: Elf32_Addr,
1282 p_filesz: Word,
1283 p_memsz: Word,
1284 p_flags: Word,
1285 p_align: Word,
1286};
1287/// Deprecated, use `std.elf.Elf64.Phdr`
1288pub const Elf64_Phdr = extern struct {
1289 p_type: Word,
1290 p_flags: Word,
1291 p_offset: Elf64_Off,
1292 p_vaddr: Elf64_Addr,
1293 p_paddr: Elf64_Addr,
1294 p_filesz: Elf64_Xword,
1295 p_memsz: Elf64_Xword,
1296 p_align: Elf64_Xword,
1297};
1298/// Deprecated, use `std.elf.Elf32.Shdr`
1299pub const Elf32_Shdr = extern struct {
1300 sh_name: Word,
1301 sh_type: Word,
1302 sh_flags: Word,
1303 sh_addr: Elf32_Addr,
1304 sh_offset: Elf32_Off,
1305 sh_size: Word,
1306 sh_link: Word,
1307 sh_info: Word,
1308 sh_addralign: Word,
1309 sh_entsize: Word,
1310};
1311/// Deprecated, use `std.elf.Elf64.Shdr`
1312pub const Elf64_Shdr = extern struct {
1313 sh_name: Word,
1314 sh_type: Word,
1315 sh_flags: Elf64_Xword,
1316 sh_addr: Elf64_Addr,
1317 sh_offset: Elf64_Off,
1318 sh_size: Elf64_Xword,
1319 sh_link: Word,
1320 sh_info: Word,
1321 sh_addralign: Elf64_Xword,
1322 sh_entsize: Elf64_Xword,
1323};
1324/// Deprecated, use `std.elf.Elf32.Chdr`
1325pub const Elf32_Chdr = extern struct {
1326 ch_type: COMPRESS,
1327 ch_size: Word,
1328 ch_addralign: Word,
1329};
1330/// Deprecated, use `std.elf.Elf64.Chdr`
1331pub const Elf64_Chdr = extern struct {
1332 ch_type: COMPRESS,
1333 ch_reserved: Word = 0,
1334 ch_size: Elf64_Xword,
1335 ch_addralign: Elf64_Xword,
1336};
1337/// Deprecated, use `std.elf.Elf32.Sym`
1338pub const Elf32_Sym = extern struct {
1339 st_name: Word,
1340 st_value: Elf32_Addr,
1341 st_size: Word,
1342 st_info: u8,
1343 st_other: u8,
1344 st_shndx: Elf32_Section,
1345
1346 pub inline fn st_type(self: @This()) u4 {
1347 return @truncate(self.st_info);
1348 }
1349 pub inline fn st_bind(self: @This()) u4 {
1350 return @truncate(self.st_info >> 4);
1351 }
1352};
1353/// Deprecated, use `std.elf.Elf64.Sym`
1354pub const Elf64_Sym = extern struct {
1355 st_name: Word,
1356 st_info: u8,
1357 st_other: u8,
1358 st_shndx: Elf64_Section,
1359 st_value: Elf64_Addr,
1360 st_size: Elf64_Xword,
1361
1362 pub inline fn st_type(self: @This()) u4 {
1363 return @truncate(self.st_info);
1364 }
1365 pub inline fn st_bind(self: @This()) u4 {
1366 return @truncate(self.st_info >> 4);
1367 }
1368};
1369pub const Elf32_Syminfo = extern struct {
1370 si_boundto: Half,
1371 si_flags: Half,
1372};
1373pub const Elf64_Syminfo = extern struct {
1374 si_boundto: Half,
1375 si_flags: Half,
1376};
1377pub const Elf32_Rel = extern struct {
1378 r_offset: Elf32_Addr,
1379 r_info: Word,
1380
1381 pub inline fn r_sym(self: @This()) u24 {
1382 return @truncate(self.r_info >> 8);
1383 }
1384 pub inline fn r_type(self: @This()) u8 {
1385 return @truncate(self.r_info);
1386 }
1387};
1388pub const Elf64_Rel = extern struct {
1389 r_offset: Elf64_Addr,
1390 r_info: Elf64_Xword,
1391
1392 pub inline fn r_sym(self: @This()) u32 {
1393 return @truncate(self.r_info >> 32);
1394 }
1395 pub inline fn r_type(self: @This()) u32 {
1396 return @truncate(self.r_info);
1397 }
1398};
1399pub const Elf32_Rela = extern struct {
1400 r_offset: Elf32_Addr,
1401 r_info: Word,
1402 r_addend: Sword,
1403
1404 pub inline fn r_sym(self: @This()) u24 {
1405 return @truncate(self.r_info >> 8);
1406 }
1407 pub inline fn r_type(self: @This()) u8 {
1408 return @truncate(self.r_info);
1409 }
1410};
1411pub const Elf64_Rela = extern struct {
1412 r_offset: Elf64_Addr,
1413 r_info: Elf64_Xword,
1414 r_addend: Elf64_Sxword,
1415
1416 pub inline fn r_sym(self: @This()) u32 {
1417 return @truncate(self.r_info >> 32);
1418 }
1419 pub inline fn r_type(self: @This()) u32 {
1420 return @truncate(self.r_info);
1421 }
1422};
1423pub const Elf32_Relr = Word;
1424pub const Elf64_Relr = Elf64_Xword;
1425pub const Elf32_Dyn = extern struct {
1426 d_tag: Sword,
1427 d_val: Elf32_Addr,
1428};
1429pub const Elf64_Dyn = extern struct {
1430 d_tag: Elf64_Sxword,
1431 d_val: Elf64_Addr,
1432};
1433pub const Verdef = extern struct {
1434 version: Half,
1435 flags: Half,
1436 ndx: VER_NDX,
1437 cnt: Half,
1438 hash: Word,
1439 aux: Word,
1440 next: Word,
1441};
1442pub const Verdaux = extern struct {
1443 name: Word,
1444 next: Word,
1445};
1446pub const Elf32_Verneed = extern struct {
1447 vn_version: Half,
1448 vn_cnt: Half,
1449 vn_file: Word,
1450 vn_aux: Word,
1451 vn_next: Word,
1452};
1453pub const Elf64_Verneed = extern struct {
1454 vn_version: Half,
1455 vn_cnt: Half,
1456 vn_file: Word,
1457 vn_aux: Word,
1458 vn_next: Word,
1459};
1460pub const Vernaux = extern struct {
1461 hash: Word,
1462 flags: Half,
1463 other: Half,
1464 name: Word,
1465 next: Word,
1466};
1467pub const Elf32_auxv_t = extern struct {
1468 a_type: u32,
1469 a_un: extern union {
1470 a_val: u32,
1471 },
1472};
1473pub const Elf64_auxv_t = extern struct {
1474 a_type: u64,
1475 a_un: extern union {
1476 a_val: u64,
1477 },
1478};
1479pub const Elf32_Nhdr = extern struct {
1480 n_namesz: Word,
1481 n_descsz: Word,
1482 n_type: Word,
1483};
1484pub const Elf64_Nhdr = extern struct {
1485 n_namesz: Word,
1486 n_descsz: Word,
1487 n_type: Word,
1488};
1489pub const Elf32_Move = extern struct {
1490 m_value: Elf32_Xword,
1491 m_info: Word,
1492 m_poffset: Word,
1493 m_repeat: Half,
1494 m_stride: Half,
1495};
1496pub const Elf64_Move = extern struct {
1497 m_value: Elf64_Xword,
1498 m_info: Elf64_Xword,
1499 m_poffset: Elf64_Xword,
1500 m_repeat: Half,
1501 m_stride: Half,
1502};
1503pub const Elf32_gptab = extern union {
1504 gt_header: extern struct {
1505 gt_current_g_value: Word,
1506 gt_unused: Word,
1507 },
1508 gt_entry: extern struct {
1509 gt_g_value: Word,
1510 gt_bytes: Word,
1511 },
1512};
1513pub const Elf32_RegInfo = extern struct {
1514 ri_gprmask: Word,
1515 ri_cprmask: [4]Word,
1516 ri_gp_value: Sword,
1517};
1518pub const Elf_Options = extern struct {
1519 kind: u8,
1520 size: u8,
1521 section: Elf32_Section,
1522 info: Word,
1523};
1524pub const Elf_Options_Hw = extern struct {
1525 hwp_flags1: Word,
1526 hwp_flags2: Word,
1527};
1528pub const Elf32_Lib = extern struct {
1529 l_name: Word,
1530 l_time_stamp: Word,
1531 l_checksum: Word,
1532 l_version: Word,
1533 l_flags: Word,
1534};
1535pub const Elf64_Lib = extern struct {
1536 l_name: Word,
1537 l_time_stamp: Word,
1538 l_checksum: Word,
1539 l_version: Word,
1540 l_flags: Word,
1541};
1542pub const Elf32_Conflict = Elf32_Addr;
1543pub const Elf_MIPS_ABIFlags_v0 = extern struct {
1544 version: Half,
1545 isa_level: u8,
1546 isa_rev: u8,
1547 gpr_size: u8,
1548 cpr1_size: u8,
1549 cpr2_size: u8,
1550 fp_abi: u8,
1551 isa_ext: Word,
1552 ases: Word,
1553 flags1: Word,
1554 flags2: Word,
1555};
1556
1557pub const Auxv = switch (@sizeOf(usize)) {
1558 4 => Elf32_auxv_t,
1559 8 => Elf64_auxv_t,
1560 else => @compileError("expected pointer size of 32 or 64"),
1561};
1562/// Deprecated, use `std.elf.ElfN.Ehdr`
1563pub const Ehdr = switch (@sizeOf(usize)) {
1564 4 => Elf32_Ehdr,
1565 8 => Elf64_Ehdr,
1566 else => @compileError("expected pointer size of 32 or 64"),
1567};
1568/// Deprecated, use `std.elf.ElfN.Phdr`
1569pub const Phdr = switch (@sizeOf(usize)) {
1570 4 => Elf32_Phdr,
1571 8 => Elf64_Phdr,
1572 else => @compileError("expected pointer size of 32 or 64"),
1573};
1574pub const Dyn = switch (@sizeOf(usize)) {
1575 4 => Elf32_Dyn,
1576 8 => Elf64_Dyn,
1577 else => @compileError("expected pointer size of 32 or 64"),
1578};
1579pub const Rel = switch (@sizeOf(usize)) {
1580 4 => Elf32_Rel,
1581 8 => Elf64_Rel,
1582 else => @compileError("expected pointer size of 32 or 64"),
1583};
1584pub const Rela = switch (@sizeOf(usize)) {
1585 4 => Elf32_Rela,
1586 8 => Elf64_Rela,
1587 else => @compileError("expected pointer size of 32 or 64"),
1588};
1589pub const Relr = switch (@sizeOf(usize)) {
1590 4 => Elf32_Relr,
1591 8 => Elf64_Relr,
1592 else => @compileError("expected pointer size of 32 or 64"),
1593};
1594pub const Shdr = switch (@sizeOf(usize)) {
1595 4 => Elf32_Shdr,
1596 8 => Elf64_Shdr,
1597 else => @compileError("expected pointer size of 32 or 64"),
1598};
1599/// Deprecated, use `std.elf.ElfN.Chdr`
1600pub const Chdr = switch (@sizeOf(usize)) {
1601 4 => Elf32_Chdr,
1602 8 => Elf64_Chdr,
1603 else => @compileError("expected pointer size of 32 or 64"),
1604};
1605/// Deprecated, use `std.elf.ElfN.Sym`
1606pub const Sym = switch (@sizeOf(usize)) {
1607 4 => Elf32_Sym,
1608 8 => Elf64_Sym,
1609 else => @compileError("expected pointer size of 32 or 64"),
1610};
1611/// Deprecated, use `std.elf.ElfN.Addr`
1612pub const Addr = ElfN.Addr;
1613
1614/// Deprecated, use `@intFromEnum(std.elf.CLASS.NONE)`
1615pub const ELFCLASSNONE = @intFromEnum(CLASS.NONE);
1616/// Deprecated, use `@intFromEnum(std.elf.CLASS.@"32")`
1617pub const ELFCLASS32 = @intFromEnum(CLASS.@"32");
1618/// Deprecated, use `@intFromEnum(std.elf.CLASS.@"64")`
1619pub const ELFCLASS64 = @intFromEnum(CLASS.@"64");
1620/// Deprecated, use `@intFromEnum(std.elf.CLASS.NUM)`
1621pub const ELFCLASSNUM = CLASS.NUM;
1622pub const CLASS = enum(u8) {
1623 NONE = 0,
1624 @"32" = 1,
1625 @"64" = 2,
1626 _,
1627
1628 pub const NUM = @typeInfo(CLASS).@"enum".fields.len;
1629
1630 pub fn ElfN(comptime class: CLASS) type {
1631 return switch (class) {
1632 .NONE, _ => comptime unreachable,
1633 .@"32" => Elf32,
1634 .@"64" => Elf64,
1635 };
1636 }
1637};
1638
1639/// Deprecated, use `@intFromEnum(std.elf.DATA.NONE)`
1640pub const ELFDATANONE = @intFromEnum(DATA.NONE);
1641/// Deprecated, use `@intFromEnum(std.elf.DATA.@"2LSB")`
1642pub const ELFDATA2LSB = @intFromEnum(DATA.@"2LSB");
1643/// Deprecated, use `@intFromEnum(std.elf.DATA.@"2MSB")`
1644pub const ELFDATA2MSB = @intFromEnum(DATA.@"2MSB");
1645/// Deprecated, use `@intFromEnum(std.elf.DATA.NUM)`
1646pub const ELFDATANUM = DATA.NUM;
1647pub const DATA = enum(u8) {
1648 NONE = 0,
1649 @"2LSB" = 1,
1650 @"2MSB" = 2,
1651 _,
1652
1653 pub const NUM = @typeInfo(DATA).@"enum".fields.len;
1654};
1655
1656pub const OSABI = enum(u8) {
1657 /// UNIX System V ABI
1658 NONE = 0,
1659 /// HP-UX operating system
1660 HPUX = 1,
1661 /// NetBSD
1662 NETBSD = 2,
1663 /// GNU (Hurd/Linux)
1664 GNU = 3,
1665 /// Solaris
1666 SOLARIS = 6,
1667 /// AIX
1668 AIX = 7,
1669 /// IRIX
1670 IRIX = 8,
1671 /// FreeBSD
1672 FREEBSD = 9,
1673 /// TRU64 UNIX
1674 TRU64 = 10,
1675 /// Novell Modesto
1676 MODESTO = 11,
1677 /// OpenBSD
1678 OPENBSD = 12,
1679 /// OpenVMS
1680 OPENVMS = 13,
1681 /// Hewlett-Packard Non-Stop Kernel
1682 NSK = 14,
1683 /// AROS
1684 AROS = 15,
1685 /// FenixOS
1686 FENIXOS = 16,
1687 /// Nuxi CloudABI
1688 CLOUDABI = 17,
1689 /// Stratus Technologies OpenVOS
1690 OPENVOS = 18,
1691 /// NVIDIA CUDA architecture (not gABI assigned)
1692 CUDA = 51,
1693 /// AMD HSA Runtime (not gABI assigned)
1694 AMDGPU_HSA = 64,
1695 /// AMD PAL Runtime (not gABI assigned)
1696 AMDGPU_PAL = 65,
1697 /// AMD Mesa3D Runtime (not gABI assigned)
1698 AMDGPU_MESA3D = 66,
1699 /// ARM (not gABI assigned)
1700 ARM = 97,
1701 /// Standalone (embedded) application (not gABI assigned)
1702 STANDALONE = 255,
1703
1704 _,
1705};
1706
1707/// Machine architectures.
1708///
1709/// See current registered ELF machine architectures at:
1710/// http://www.sco.com/developers/gabi/latest/ch4.eheader.html
1711pub const EM = enum(u16) {
1712 /// No machine
1713 NONE = 0,
1714 /// AT&T WE 32100
1715 M32 = 1,
1716 /// SUN SPARC
1717 SPARC = 2,
1718 /// Intel 80386
1719 @"386" = 3,
1720 /// Motorola m68k family
1721 @"68K" = 4,
1722 /// Motorola m88k family
1723 @"88K" = 5,
1724 /// Intel MCU
1725 IAMCU = 6,
1726 /// Intel 80860
1727 @"860" = 7,
1728 /// MIPS R3000 (officially, big-endian only)
1729 MIPS = 8,
1730 /// IBM System/370
1731 S370 = 9,
1732 /// MIPS R3000 (and R4000) little-endian, Oct 4 1993 Draft (deprecated)
1733 MIPS_RS3_LE = 10,
1734 /// Old version of Sparc v9, from before the ABI (not gABI assigned)
1735 OLD_SPARCV9 = 11,
1736 /// HPPA
1737 PARISC = 15,
1738 /// Fujitsu VPP500 (also old version of PowerPC, which was not gABI assigned)
1739 VPP500 = 17,
1740 /// Sun's "v8plus"
1741 SPARC32PLUS = 18,
1742 /// Intel 80960
1743 @"960" = 19,
1744 /// PowerPC
1745 PPC = 20,
1746 /// 64-bit PowerPC
1747 PPC64 = 21,
1748 /// IBM S/390
1749 S390 = 22,
1750 /// Sony/Toshiba/IBM SPU
1751 SPU = 23,
1752 /// NEC V800 series
1753 V800 = 36,
1754 /// Fujitsu FR20
1755 FR20 = 37,
1756 /// TRW RH32
1757 RH32 = 38,
1758 /// Motorola M*Core, aka RCE (also old Fujitsu MMA, which was not gABI assigned)
1759 MCORE = 39,
1760 /// ARM
1761 ARM = 40,
1762 /// Digital Alpha
1763 OLD_ALPHA = 41,
1764 /// Renesas (formerly Hitachi) / SuperH SH
1765 SH = 42,
1766 /// SPARC v9 64-bit
1767 SPARCV9 = 43,
1768 /// Siemens Tricore embedded processor
1769 TRICORE = 44,
1770 /// ARC Cores
1771 ARC = 45,
1772 /// Renesas (formerly Hitachi) H8/300
1773 H8_300 = 46,
1774 /// Renesas (formerly Hitachi) H8/300H
1775 H8_300H = 47,
1776 /// Renesas (formerly Hitachi) H8S
1777 H8S = 48,
1778 /// Renesas (formerly Hitachi) H8/500
1779 H8_500 = 49,
1780 /// Intel IA-64 Processor
1781 IA_64 = 50,
1782 /// Stanford MIPS-X
1783 MIPS_X = 51,
1784 /// Motorola Coldfire
1785 COLDFIRE = 52,
1786 /// Motorola M68HC12
1787 @"68HC12" = 53,
1788 /// Fujitsu Multimedia Accelerator
1789 MMA = 54,
1790 /// Siemens PCP
1791 PCP = 55,
1792 /// Sony nCPU embedded RISC processor
1793 NCPU = 56,
1794 /// Denso NDR1 microprocessor
1795 NDR1 = 57,
1796 /// Motorola Star*Core processor
1797 STARCORE = 58,
1798 /// Toyota ME16 processor
1799 ME16 = 59,
1800 /// STMicroelectronics ST100 processor
1801 ST100 = 60,
1802 /// Advanced Logic Corp. TinyJ embedded processor
1803 TINYJ = 61,
1804 /// Advanced Micro Devices X86-64 processor
1805 X86_64 = 62,
1806 /// Sony DSP Processor
1807 PDSP = 63,
1808 /// Digital Equipment Corp. PDP-10
1809 PDP10 = 64,
1810 /// Digital Equipment Corp. PDP-11
1811 PDP11 = 65,
1812 /// Siemens FX66 microcontroller
1813 FX66 = 66,
1814 /// STMicroelectronics ST9+ 8/16 bit microcontroller
1815 ST9PLUS = 67,
1816 /// STMicroelectronics ST7 8-bit microcontroller
1817 ST7 = 68,
1818 /// Motorola MC68HC16 Microcontroller
1819 @"68HC16" = 69,
1820 /// Motorola MC68HC11 Microcontroller
1821 @"68HC11" = 70,
1822 /// Motorola MC68HC08 Microcontroller
1823 @"68HC08" = 71,
1824 /// Motorola MC68HC05 Microcontroller
1825 @"68HC05" = 72,
1826 /// Silicon Graphics SVx
1827 SVX = 73,
1828 /// STMicroelectronics ST19 8-bit cpu
1829 ST19 = 74,
1830 /// Digital VAX
1831 VAX = 75,
1832 /// Axis Communications 32-bit embedded processor
1833 CRIS = 76,
1834 /// Infineon Technologies 32-bit embedded cpu
1835 JAVELIN = 77,
1836 /// Element 14 64-bit DSP processor
1837 FIREPATH = 78,
1838 /// LSI Logic's 16-bit DSP processor
1839 ZSP = 79,
1840 /// Donald Knuth's educational 64-bit processor
1841 MMIX = 80,
1842 /// Harvard's machine-independent format
1843 HUANY = 81,
1844 /// SiTera Prism
1845 PRISM = 82,
1846 /// Atmel AVR 8-bit microcontroller
1847 AVR = 83,
1848 /// Fujitsu FR30
1849 FR30 = 84,
1850 /// Mitsubishi D10V
1851 D10V = 85,
1852 /// Mitsubishi D30V
1853 D30V = 86,
1854 /// Renesas V850 (formerly NEC V850)
1855 V850 = 87,
1856 /// Renesas M32R (formerly Mitsubishi M32R)
1857 M32R = 88,
1858 /// Matsushita MN10300
1859 MN10300 = 89,
1860 /// Matsushita MN10200
1861 MN10200 = 90,
1862 /// picoJava
1863 PJ = 91,
1864 /// OpenRISC 1000 32-bit embedded processor
1865 OR1K = 92,
1866 /// ARC International ARCompact processor
1867 ARC_COMPACT = 93,
1868 /// Tensilica Xtensa Architecture
1869 XTENSA = 94,
1870 /// Alphamosaic VideoCore processor (also old Sunplus S+core7 backend magic number, which was not gABI assigned)
1871 VIDEOCORE = 95,
1872 /// Thompson Multimedia General Purpose Processor
1873 TMM_GPP = 96,
1874 /// National Semiconductor 32000 series
1875 NS32K = 97,
1876 /// Tenor Network TPC processor
1877 TPC = 98,
1878 /// Trebia SNP 1000 processor (also old value for picoJava, which was not gABI assigned)
1879 SNP1K = 99,
1880 /// STMicroelectronics ST200 microcontroller
1881 ST200 = 100,
1882 /// Ubicom IP2022 micro controller
1883 IP2K = 101,
1884 /// MAX Processor
1885 MAX = 102,
1886 /// National Semiconductor CompactRISC
1887 CR = 103,
1888 /// Fujitsu F2MC16
1889 F2MC16 = 104,
1890 /// TI msp430 micro controller
1891 MSP430 = 105,
1892 /// ADI Blackfin
1893 BLACKFIN = 106,
1894 /// S1C33 Family of Seiko Epson processors
1895 SE_C33 = 107,
1896 /// Sharp embedded microprocessor
1897 SEP = 108,
1898 /// Arca RISC Microprocessor
1899 ARCA = 109,
1900 /// Microprocessor series from PKU-Unity Ltd. and MPRC of Peking University
1901 UNICORE = 110,
1902 /// eXcess: 16/32/64-bit configurable embedded CPU
1903 EXCESS = 111,
1904 /// Icera Semiconductor Inc. Deep Execution Processor
1905 DXP = 112,
1906 /// Altera Nios II soft-core processor
1907 ALTERA_NIOS2 = 113,
1908 /// National Semiconductor CRX
1909 CRX = 114,
1910 /// Motorola XGATE embedded processor (also old value for National Semiconductor CompactRISC, which was not gABI assigned)
1911 XGATE = 115,
1912 /// Infineon C16x/XC16x processor
1913 C166 = 116,
1914 /// Renesas M16C series microprocessors
1915 M16C = 117,
1916 /// Microchip Technology dsPIC30F Digital Signal Controller
1917 DSPIC30F = 118,
1918 /// Freescale Communication Engine RISC core
1919 CE = 119,
1920 /// Renesas M32C series microprocessors
1921 M32C = 120,
1922 /// Altium TSK3000 core
1923 TSK3000 = 131,
1924 /// Freescale RS08 embedded processor
1925 RS08 = 132,
1926 /// Analog Devices SHARC family of 32-bit DSP processors
1927 SHARC = 133,
1928 /// Cyan Technology eCOG2 microprocessor
1929 ECOG2 = 134,
1930 /// Sunplus S+core (and S+core7) RISC processor
1931 SCORE = 135,
1932 /// New Japan Radio (NJR) 24-bit DSP Processor
1933 DSP24 = 136,
1934 /// Broadcom VideoCore III processor
1935 VIDEOCORE3 = 137,
1936 /// RISC processor for Lattice FPGA architecture
1937 LATTICEMICO32 = 138,
1938 /// Seiko Epson C17 family
1939 SE_C17 = 139,
1940 /// Texas Instruments TMS320C6000 DSP family
1941 TI_C6000 = 140,
1942 /// Texas Instruments TMS320C2000 DSP family
1943 TI_C2000 = 141,
1944 /// Texas Instruments TMS320C55x DSP family
1945 TI_C5500 = 142,
1946 /// Texas Instruments Application Specific RISC Processor, 32bit fetch
1947 TI_ARP32 = 143,
1948 /// Texas Instruments Programmable Realtime Unit
1949 TI_PRU = 144,
1950 /// STMicroelectronics 64bit VLIW Data Signal Processor
1951 MMDSP_PLUS = 160,
1952 /// Cypress M8C microprocessor
1953 CYPRESS_M8C = 161,
1954 /// Renesas R32C series microprocessors
1955 R32C = 162,
1956 /// NXP Semiconductors TriMedia architecture family
1957 TRIMEDIA = 163,
1958 /// QUALCOMM DSP6 Processor
1959 QDSP6 = 164,
1960 /// Intel 8051 and variants
1961 @"8051" = 165,
1962 /// STMicroelectronics STxP7x family
1963 STXP7X = 166,
1964 /// Andes Technology compact code size embedded RISC processor family
1965 NDS32 = 167,
1966 /// Cyan Technology eCOG1X family
1967 ECOG1X = 168,
1968 /// Dallas Semiconductor MAXQ30 Core Micro-controllers
1969 MAXQ30 = 169,
1970 /// New Japan Radio (NJR) 16-bit DSP Processor
1971 XIMO16 = 170,
1972 /// M2000 Reconfigurable RISC Microprocessor
1973 MANIK = 171,
1974 /// Cray Inc. NV2 vector architecture
1975 CRAYNV2 = 172,
1976 /// Renesas RX family
1977 RX = 173,
1978 /// Imagination Technologies Meta processor architecture
1979 METAG = 174,
1980 /// MCST Elbrus general purpose hardware architecture
1981 MCST_ELBRUS = 175,
1982 /// Cyan Technology eCOG16 family
1983 ECOG16 = 176,
1984 /// National Semiconductor CompactRISC 16-bit processor
1985 CR16 = 177,
1986 /// Freescale Extended Time Processing Unit
1987 ETPU = 178,
1988 /// Infineon Technologies SLE9X core
1989 SLE9X = 179,
1990 /// Intel L10M
1991 L10M = 180,
1992 /// Intel K10M
1993 K10M = 181,
1994 /// ARM 64-bit architecture
1995 AARCH64 = 183,
1996 /// Atmel Corporation 32-bit microprocessor family
1997 AVR32 = 185,
1998 /// STMicroeletronics STM8 8-bit microcontroller
1999 STM8 = 186,
2000 /// Tilera TILE64 multicore architecture family
2001 TILE64 = 187,
2002 /// Tilera TILEPro multicore architecture family
2003 TILEPRO = 188,
2004 /// Xilinx MicroBlaze 32-bit RISC soft processor core
2005 MICROBLAZE = 189,
2006 /// NVIDIA CUDA architecture
2007 CUDA = 190,
2008 /// Tilera TILE-Gx multicore architecture family
2009 TILEGX = 191,
2010 /// CloudShield architecture family
2011 CLOUDSHIELD = 192,
2012 /// KIPO-KAIST Core-A 1st generation processor family
2013 COREA_1ST = 193,
2014 /// KIPO-KAIST Core-A 2nd generation processor family
2015 COREA_2ND = 194,
2016 /// Synopsys ARCompact V2
2017 ARC_COMPACT2 = 195,
2018 /// Open8 8-bit RISC soft processor core
2019 OPEN8 = 196,
2020 /// Renesas RL78 family
2021 RL78 = 197,
2022 /// Broadcom VideoCore V processor
2023 VIDEOCORE5 = 198,
2024 /// Renesas 78K0R
2025 @"78K0R" = 199,
2026 /// Freescale 56800EX Digital Signal Controller (DSC)
2027 @"56800EX" = 200,
2028 /// Beyond BA1 CPU architecture
2029 BA1 = 201,
2030 /// Beyond BA2 CPU architecture
2031 BA2 = 202,
2032 /// XMOS xCORE processor family
2033 XCORE = 203,
2034 /// Microchip 8-bit PIC(r) family
2035 MCHP_PIC = 204,
2036 /// Intel Graphics Technology
2037 INTELGT = 205,
2038 /// KM211 KM32 32-bit processor
2039 KM32 = 210,
2040 /// KM211 KMX32 32-bit processor
2041 KMX32 = 211,
2042 /// KM211 KMX16 16-bit processor
2043 KMX16 = 212,
2044 /// KM211 KMX8 8-bit processor
2045 KMX8 = 213,
2046 /// KM211 KVARC processor
2047 KVARC = 214,
2048 /// Paneve CDP architecture family
2049 CDP = 215,
2050 /// Cognitive Smart Memory Processor
2051 COGE = 216,
2052 /// Bluechip Systems CoolEngine
2053 COOL = 217,
2054 /// Nanoradio Optimized RISC
2055 NORC = 218,
2056 /// CSR Kalimba architecture family
2057 CSR_KALIMBA = 219,
2058 /// Zilog Z80
2059 Z80 = 220,
2060 /// Controls and Data Services VISIUMcore processor
2061 VISIUM = 221,
2062 /// FTDI Chip FT32 high performance 32-bit RISC architecture
2063 FT32 = 222,
2064 /// Moxie processor family
2065 MOXIE = 223,
2066 /// AMD GPU architecture
2067 AMDGPU = 224,
2068 /// RISC-V
2069 RISCV = 243,
2070 /// Lanai 32-bit processor
2071 LANAI = 244,
2072 /// CEVA Processor Architecture Family
2073 CEVA = 245,
2074 /// CEVA X2 Processor Family
2075 CEVA_X2 = 246,
2076 /// Linux BPF - in-kernel virtual machine
2077 BPF = 247,
2078 /// Graphcore Intelligent Processing Unit
2079 GRAPHCORE_IPU = 248,
2080 /// Imagination Technologies
2081 IMG1 = 249,
2082 /// Netronome Flow Processor
2083 NFP = 250,
2084 /// NEC Vector Engine
2085 VE = 251,
2086 /// C-SKY processor family
2087 CSKY = 252,
2088 /// Synopsys ARCv2.3 64-bit
2089 ARC_COMPACT3_64 = 253,
2090 /// MOS Technology MCS 6502 processor
2091 MCS6502 = 254,
2092 /// Synopsys ARCv2.3 32-bit
2093 ARC_COMPACT3 = 255,
2094 /// Kalray VLIW core of the MPPA processor family
2095 KVX = 256,
2096 /// WDC 65816/65C816
2097 @"65816" = 257,
2098 /// LoongArch
2099 LOONGARCH = 258,
2100 /// ChipON KungFu32
2101 KF32 = 259,
2102 /// LAPIS nX-U16/U8
2103 U16_U8CORE = 260,
2104 /// Tachyum
2105 TACHYUM = 261,
2106 /// NXP 56800EF Digital Signal Controller (DSC)
2107 @"56800EF" = 262,
2108 /// Solana Bytecode Format
2109 SBF = 263,
2110 /// AMD/Xilinx AIEngine architecture
2111 AIENGINE = 264,
2112 /// SiMa MLA
2113 SIMA_MLA = 265,
2114 /// Cambricon BANG
2115 BANG = 266,
2116 /// Loongson LoongGPU
2117 LOONGGPU = 267,
2118 /// Wuxi Institute of Advanced Technology SW64
2119 SW64 = 268,
2120 /// AVR
2121 AVR_OLD = 0x1057,
2122 /// MSP430
2123 MSP430_OLD = 0x1059,
2124 /// Morpho MT
2125 MT = 0x2530,
2126 /// FR30
2127 CYGNUS_FR30 = 0x3330,
2128 /// WebAssembly (as used by LLVM)
2129 WEBASSEMBLY = 0x4157,
2130 /// Infineon Technologies 16-bit microcontroller with C166-V2 core
2131 XC16X = 0x4688,
2132 /// Freescale S12Z
2133 S12Z = 0x4def,
2134 /// DLX
2135 DLX = 0x5aa5,
2136 /// FRV
2137 CYGNUS_FRV = 0x5441,
2138 /// D10V
2139 CYGNUS_D10V = 0x7650,
2140 /// D30V
2141 CYGNUS_D30V = 0x7676,
2142 /// Ubicom IP2xxx
2143 IP2K_OLD = 0x8217,
2144 /// Cygnus PowerPC ELF
2145 CYGNUS_POWERPC = 0x9025,
2146 /// Alpha
2147 ALPHA = 0x9026,
2148 /// Cygnus M32R ELF
2149 CYGNUS_M32R = 0x9041,
2150 /// V850
2151 CYGNUS_V850 = 0x9080,
2152 /// Old S/390
2153 S390_OLD = 0xa390,
2154 /// Old unofficial value for Xtensa
2155 XTENSA_OLD = 0xabc7,
2156 /// Xstormy16
2157 XSTORMY16 = 0xad45,
2158 /// MN10300
2159 CYGNUS_MN10300 = 0xbeef,
2160 /// MN10200
2161 CYGNUS_MN10200 = 0xdead,
2162 /// Renesas M32C and M16C
2163 M32C_OLD = 0xfeb0,
2164 /// Vitesse IQ2000
2165 IQ2000 = 0xfeba,
2166 /// NIOS
2167 NIOS32 = 0xfebb,
2168 /// Toshiba MeP
2169 CYGNUS_MEP = 0xf00d,
2170 /// Old unofficial value for Moxie
2171 MOXIE_OLD = 0xfeed,
2172 /// Old MicroBlaze
2173 MICROBLAZE_OLD = 0xbaab,
2174 /// Adapteva's Epiphany architecture
2175 ADAPTEVA_EPIPHANY = 0x1223,
2176
2177 /// Parallax Propeller (P1)
2178 /// This value is an unofficial ELF value used in: https://github.com/parallaxinc/propgcc
2179 PROPELLER = 0x5072,
2180
2181 /// Parallax Propeller 2 (P2)
2182 /// This value is an unofficial ELF value used in: https://github.com/ne75/llvm-project
2183 PROPELLER2 = 300,
2184
2185 _,
2186};
2187
2188pub const GRP_COMDAT = 1;
2189
2190/// Section data should be writable during execution.
2191pub const SHF_WRITE = 0x1;
2192
2193/// Section occupies memory during program execution.
2194pub const SHF_ALLOC = 0x2;
2195
2196/// Section contains executable machine instructions.
2197pub const SHF_EXECINSTR = 0x4;
2198
2199/// The data in this section may be merged.
2200pub const SHF_MERGE = 0x10;
2201
2202/// The data in this section is null-terminated strings.
2203pub const SHF_STRINGS = 0x20;
2204
2205/// A field in this section holds a section header table index.
2206pub const SHF_INFO_LINK = 0x40;
2207
2208/// Adds special ordering requirements for link editors.
2209pub const SHF_LINK_ORDER = 0x80;
2210
2211/// This section requires special OS-specific processing to avoid incorrect
2212/// behavior.
2213pub const SHF_OS_NONCONFORMING = 0x100;
2214
2215/// This section is a member of a section group.
2216pub const SHF_GROUP = 0x200;
2217
2218/// This section holds Thread-Local Storage.
2219pub const SHF_TLS = 0x400;
2220
2221/// Identifies a section containing compressed data.
2222pub const SHF_COMPRESSED = 0x800;
2223
2224/// Not to be GCed by the linker
2225pub const SHF_GNU_RETAIN = 0x200000;
2226
2227/// This section is excluded from the final executable or shared library.
2228pub const SHF_EXCLUDE = 0x80000000;
2229
2230/// Start of target-specific flags.
2231pub const SHF_MASKOS = 0x0ff00000;
2232
2233/// Bits indicating processor-specific flags.
2234pub const SHF_MASKPROC = 0xf0000000;
2235
2236/// All sections with the "d" flag are grouped together by the linker to form
2237/// the data section and the dp register is set to the start of the section by
2238/// the boot code.
2239pub const XCORE_SHF_DP_SECTION = 0x10000000;
2240
2241/// All sections with the "c" flag are grouped together by the linker to form
2242/// the constant pool and the cp register is set to the start of the constant
2243/// pool by the boot code.
2244pub const XCORE_SHF_CP_SECTION = 0x20000000;
2245
2246/// If an object file section does not have this flag set, then it may not hold
2247/// more than 2GB and can be freely referred to in objects using smaller code
2248/// models. Otherwise, only objects using larger code models can refer to them.
2249/// For example, a medium code model object can refer to data in a section that
2250/// sets this flag besides being able to refer to data in a section that does
2251/// not set it; likewise, a small code model object can refer only to code in a
2252/// section that does not set this flag.
2253pub const SHF_X86_64_LARGE = 0x10000000;
2254
2255/// All sections with the GPREL flag are grouped into a global data area
2256/// for faster accesses
2257pub const SHF_HEX_GPREL = 0x10000000;
2258
2259/// Section contains text/data which may be replicated in other sections.
2260/// Linker must retain only one copy.
2261pub const SHF_MIPS_NODUPES = 0x01000000;
2262
2263/// Linker must generate implicit hidden weak names.
2264pub const SHF_MIPS_NAMES = 0x02000000;
2265
2266/// Section data local to process.
2267pub const SHF_MIPS_LOCAL = 0x04000000;
2268
2269/// Do not strip this section.
2270pub const SHF_MIPS_NOSTRIP = 0x08000000;
2271
2272/// Section must be part of global data area.
2273pub const SHF_MIPS_GPREL = 0x10000000;
2274
2275/// This section should be merged.
2276pub const SHF_MIPS_MERGE = 0x20000000;
2277
2278/// Address size to be inferred from section entry size.
2279pub const SHF_MIPS_ADDR = 0x40000000;
2280
2281/// Section data is string data by default.
2282pub const SHF_MIPS_STRING = 0x80000000;
2283
2284/// Make code section unreadable when in execute-only mode
2285pub const SHF_ARM_PURECODE = 0x2000000;
2286
2287pub const SHF = packed struct(Word) {
2288 /// Section data should be writable during execution.
2289 WRITE: bool = false,
2290 /// Section occupies memory during program execution.
2291 ALLOC: bool = false,
2292 /// Section contains executable machine instructions.
2293 EXECINSTR: bool = false,
2294 unused3: u1 = 0,
2295 /// The data in this section may be merged.
2296 MERGE: bool = false,
2297 /// The data in this section is null-terminated strings.
2298 STRINGS: bool = false,
2299 /// A field in this section holds a section header table index.
2300 INFO_LINK: bool = false,
2301 /// Adds special ordering requirements for link editors.
2302 LINK_ORDER: bool = false,
2303 /// This section requires special OS-specific processing to avoid incorrect behavior.
2304 OS_NONCONFORMING: bool = false,
2305 /// This section is a member of a section group.
2306 GROUP: bool = false,
2307 /// This section holds Thread-Local Storage.
2308 TLS: bool = false,
2309 /// Identifies a section containing compressed data.
2310 COMPRESSED: bool = false,
2311 unused12: u8 = 0,
2312 OS: packed union {
2313 MASK: u8,
2314 GNU: packed struct(u8) {
2315 unused0: u1 = 0,
2316 /// Not to be GCed by the linker
2317 RETAIN: bool = false,
2318 unused2: u6 = 0,
2319 },
2320 MIPS: packed struct(u8) {
2321 unused0: u4 = 0,
2322 /// Section contains text/data which may be replicated in other sections.
2323 /// Linker must retain only one copy.
2324 NODUPES: bool = false,
2325 /// Linker must generate implicit hidden weak names.
2326 NAMES: bool = false,
2327 /// Section data local to process.
2328 LOCAL: bool = false,
2329 /// Do not strip this section.
2330 NOSTRIP: bool = false,
2331 },
2332 ARM: packed struct(u8) {
2333 unused0: u5 = 0,
2334 /// Make code section unreadable when in execute-only mode
2335 PURECODE: bool = false,
2336 unused6: u2 = 0,
2337 },
2338 } = .{ .MASK = 0 },
2339 PROC: packed union {
2340 MASK: u4,
2341 XCORE: packed struct(u4) {
2342 /// All sections with the "d" flag are grouped together by the linker to form
2343 /// the data section and the dp register is set to the start of the section by
2344 /// the boot code.
2345 DP_SECTION: bool = false,
2346 /// All sections with the "c" flag are grouped together by the linker to form
2347 /// the constant pool and the cp register is set to the start of the constant
2348 /// pool by the boot code.
2349 CP_SECTION: bool = false,
2350 unused2: u1 = 0,
2351 /// This section is excluded from the final executable or shared library.
2352 EXCLUDE: bool = false,
2353 },
2354 X86_64: packed struct(u4) {
2355 /// If an object file section does not have this flag set, then it may not hold
2356 /// more than 2GB and can be freely referred to in objects using smaller code
2357 /// models. Otherwise, only objects using larger code models can refer to them.
2358 /// For example, a medium code model object can refer to data in a section that
2359 /// sets this flag besides being able to refer to data in a section that does
2360 /// not set it; likewise, a small code model object can refer only to code in a
2361 /// section that does not set this flag.
2362 LARGE: bool = false,
2363 unused1: u2 = 0,
2364 /// This section is excluded from the final executable or shared library.
2365 EXCLUDE: bool = false,
2366 },
2367 HEX: packed struct(u4) {
2368 /// All sections with the GPREL flag are grouped into a global data area
2369 /// for faster accesses
2370 GPREL: bool = false,
2371 unused1: u2 = 0,
2372 /// This section is excluded from the final executable or shared library.
2373 EXCLUDE: bool = false,
2374 },
2375 MIPS: packed struct(u4) {
2376 /// All sections with the GPREL flag are grouped into a global data area
2377 /// for faster accesses
2378 GPREL: bool = false,
2379 /// This section should be merged.
2380 MERGE: bool = false,
2381 /// Address size to be inferred from section entry size.
2382 ADDR: bool = false,
2383 /// Section data is string data by default.
2384 STRING: bool = false,
2385 },
2386 } = .{ .MASK = 0 },
2387};
2388
2389/// Execute
2390pub const PF_X = 1;
2391
2392/// Write
2393pub const PF_W = 2;
2394
2395/// Read
2396pub const PF_R = 4;
2397
2398/// Bits for operating system-specific semantics.
2399pub const PF_MASKOS = 0x0ff00000;
2400
2401/// Bits for processor-specific semantics.
2402pub const PF_MASKPROC = 0xf0000000;
2403
2404pub const PF = packed struct(Word) {
2405 X: bool = false,
2406 W: bool = false,
2407 R: bool = false,
2408 unused3: u17 = 0,
2409 OS: packed union {
2410 MASK: u8,
2411 } = .{ .MASK = 0 },
2412 PROC: packed union {
2413 MASK: u4,
2414 } = .{ .MASK = 0 },
2415};
2416
2417/// Undefined section
2418pub const SHN_UNDEF = 0;
2419/// Start of reserved indices
2420pub const SHN_LORESERVE = 0xff00;
2421/// Start of processor-specific
2422pub const SHN_LOPROC = 0xff00;
2423/// End of processor-specific
2424pub const SHN_HIPROC = 0xff1f;
2425pub const SHN_LIVEPATCH = 0xff20;
2426/// Associated symbol is absolute
2427pub const SHN_ABS = 0xfff1;
2428/// Associated symbol is common
2429pub const SHN_COMMON = 0xfff2;
2430/// End of reserved indices
2431pub const SHN_HIRESERVE = 0xffff;
2432
2433// Legal values for ch_type (compression algorithm).
2434pub const COMPRESS = enum(u32) {
2435 ZLIB = 1,
2436 ZSTD = 2,
2437 LOOS = 0x60000000,
2438 HIOS = 0x6fffffff,
2439 LOPROC = 0x70000000,
2440 HIPROC = 0x7fffffff,
2441 _,
2442};
2443
2444/// AMD x86-64 relocations.
2445pub const R_X86_64 = enum(u32) {
2446 /// No reloc
2447 NONE = 0,
2448 /// Direct 64 bit
2449 @"64" = 1,
2450 /// PC relative 32 bit signed
2451 PC32 = 2,
2452 /// 32 bit GOT entry
2453 GOT32 = 3,
2454 /// 32 bit PLT address
2455 PLT32 = 4,
2456 /// Copy symbol at runtime
2457 COPY = 5,
2458 /// Create GOT entry
2459 GLOB_DAT = 6,
2460 /// Create PLT entry
2461 JUMP_SLOT = 7,
2462 /// Adjust by program base
2463 RELATIVE = 8,
2464 /// 32 bit signed PC relative offset to GOT
2465 GOTPCREL = 9,
2466 /// Direct 32 bit zero extended
2467 @"32" = 10,
2468 /// Direct 32 bit sign extended
2469 @"32S" = 11,
2470 /// Direct 16 bit zero extended
2471 @"16" = 12,
2472 /// 16 bit sign extended pc relative
2473 PC16 = 13,
2474 /// Direct 8 bit sign extended
2475 @"8" = 14,
2476 /// 8 bit sign extended pc relative
2477 PC8 = 15,
2478 /// ID of module containing symbol
2479 DTPMOD64 = 16,
2480 /// Offset in module's TLS block
2481 DTPOFF64 = 17,
2482 /// Offset in initial TLS block
2483 TPOFF64 = 18,
2484 /// 32 bit signed PC relative offset to two GOT entries for GD symbol
2485 TLSGD = 19,
2486 /// 32 bit signed PC relative offset to two GOT entries for LD symbol
2487 TLSLD = 20,
2488 /// Offset in TLS block
2489 DTPOFF32 = 21,
2490 /// 32 bit signed PC relative offset to GOT entry for IE symbol
2491 GOTTPOFF = 22,
2492 /// Offset in initial TLS block
2493 TPOFF32 = 23,
2494 /// PC relative 64 bit
2495 PC64 = 24,
2496 /// 64 bit offset to GOT
2497 GOTOFF64 = 25,
2498 /// 32 bit signed pc relative offset to GOT
2499 GOTPC32 = 26,
2500 /// 64 bit GOT entry offset
2501 GOT64 = 27,
2502 /// 64 bit PC relative offset to GOT entry
2503 GOTPCREL64 = 28,
2504 /// 64 bit PC relative offset to GOT
2505 GOTPC64 = 29,
2506 /// Like GOT64, says PLT entry needed
2507 GOTPLT64 = 30,
2508 /// 64-bit GOT relative offset to PLT entry
2509 PLTOFF64 = 31,
2510 /// Size of symbol plus 32-bit addend
2511 SIZE32 = 32,
2512 /// Size of symbol plus 64-bit addend
2513 SIZE64 = 33,
2514 /// GOT offset for TLS descriptor
2515 GOTPC32_TLSDESC = 34,
2516 /// Marker for call through TLS descriptor
2517 TLSDESC_CALL = 35,
2518 /// TLS descriptor
2519 TLSDESC = 36,
2520 /// Adjust indirectly by program base
2521 IRELATIVE = 37,
2522 /// 64-bit adjust by program base
2523 RELATIVE64 = 38,
2524 /// 39 Reserved was PC32_BND
2525 /// 40 Reserved was PLT32_BND
2526 /// Load from 32 bit signed pc relative offset to GOT entry without REX prefix, relaxable
2527 GOTPCRELX = 41,
2528 /// Load from 32 bit signed PC relative offset to GOT entry with REX prefix, relaxable
2529 REX_GOTPCRELX = 42,
2530 _,
2531};
2532
2533/// AArch64 relocations.
2534pub const R_AARCH64 = enum(u32) {
2535 /// No relocation.
2536 NONE = 0,
2537 /// ILP32 AArch64 relocs.
2538 /// Direct 32 bit.
2539 P32_ABS32 = 1,
2540 /// Copy symbol at runtime.
2541 P32_COPY = 180,
2542 /// Create GOT entry.
2543 P32_GLOB_DAT = 181,
2544 /// Create PLT entry.
2545 P32_JUMP_SLOT = 182,
2546 /// Adjust by program base.
2547 P32_RELATIVE = 183,
2548 /// Module number, 32 bit.
2549 P32_TLS_DTPMOD = 184,
2550 /// Module-relative offset, 32 bit.
2551 P32_TLS_DTPREL = 185,
2552 /// TP-relative offset, 32 bit.
2553 P32_TLS_TPREL = 186,
2554 /// TLS Descriptor.
2555 P32_TLSDESC = 187,
2556 /// STT_GNU_IFUNC relocation.
2557 P32_IRELATIVE = 188,
2558 /// LP64 AArch64 relocs.
2559 /// Direct 64 bit.
2560 ABS64 = 257,
2561 /// Direct 32 bit.
2562 ABS32 = 258,
2563 /// Direct 16-bit.
2564 ABS16 = 259,
2565 /// PC-relative 64-bit.
2566 PREL64 = 260,
2567 /// PC-relative 32-bit.
2568 PREL32 = 261,
2569 /// PC-relative 16-bit.
2570 PREL16 = 262,
2571 /// Dir. MOVZ imm. from bits 15:0.
2572 MOVW_UABS_G0 = 263,
2573 /// Likewise for MOVK; no check.
2574 MOVW_UABS_G0_NC = 264,
2575 /// Dir. MOVZ imm. from bits 31:16.
2576 MOVW_UABS_G1 = 265,
2577 /// Likewise for MOVK; no check.
2578 MOVW_UABS_G1_NC = 266,
2579 /// Dir. MOVZ imm. from bits 47:32.
2580 MOVW_UABS_G2 = 267,
2581 /// Likewise for MOVK; no check.
2582 MOVW_UABS_G2_NC = 268,
2583 /// Dir. MOV{K,Z} imm. from 63:48.
2584 MOVW_UABS_G3 = 269,
2585 /// Dir. MOV{N,Z} imm. from 15:0.
2586 MOVW_SABS_G0 = 270,
2587 /// Dir. MOV{N,Z} imm. from 31:16.
2588 MOVW_SABS_G1 = 271,
2589 /// Dir. MOV{N,Z} imm. from 47:32.
2590 MOVW_SABS_G2 = 272,
2591 /// PC-rel. LD imm. from bits 20:2.
2592 LD_PREL_LO19 = 273,
2593 /// PC-rel. ADR imm. from bits 20:0.
2594 ADR_PREL_LO21 = 274,
2595 /// Page-rel. ADRP imm. from 32:12.
2596 ADR_PREL_PG_HI21 = 275,
2597 /// Likewise; no overflow check.
2598 ADR_PREL_PG_HI21_NC = 276,
2599 /// Dir. ADD imm. from bits 11:0.
2600 ADD_ABS_LO12_NC = 277,
2601 /// Likewise for LD/ST; no check.
2602 LDST8_ABS_LO12_NC = 278,
2603 /// PC-rel. TBZ/TBNZ imm. from 15:2.
2604 TSTBR14 = 279,
2605 /// PC-rel. cond. br. imm. from 20:2.
2606 CONDBR19 = 280,
2607 /// PC-rel. B imm. from bits 27:2.
2608 JUMP26 = 282,
2609 /// Likewise for CALL.
2610 CALL26 = 283,
2611 /// Dir. ADD imm. from bits 11:1.
2612 LDST16_ABS_LO12_NC = 284,
2613 /// Likewise for bits 11:2.
2614 LDST32_ABS_LO12_NC = 285,
2615 /// Likewise for bits 11:3.
2616 LDST64_ABS_LO12_NC = 286,
2617 /// PC-rel. MOV{N,Z} imm. from 15:0.
2618 MOVW_PREL_G0 = 287,
2619 /// Likewise for MOVK; no check.
2620 MOVW_PREL_G0_NC = 288,
2621 /// PC-rel. MOV{N,Z} imm. from 31:16.
2622 MOVW_PREL_G1 = 289,
2623 /// Likewise for MOVK; no check.
2624 MOVW_PREL_G1_NC = 290,
2625 /// PC-rel. MOV{N,Z} imm. from 47:32.
2626 MOVW_PREL_G2 = 291,
2627 /// Likewise for MOVK; no check.
2628 MOVW_PREL_G2_NC = 292,
2629 /// PC-rel. MOV{N,Z} imm. from 63:48.
2630 MOVW_PREL_G3 = 293,
2631 /// Dir. ADD imm. from bits 11:4.
2632 LDST128_ABS_LO12_NC = 299,
2633 /// GOT-rel. off. MOV{N,Z} imm. 15:0.
2634 MOVW_GOTOFF_G0 = 300,
2635 /// Likewise for MOVK; no check.
2636 MOVW_GOTOFF_G0_NC = 301,
2637 /// GOT-rel. o. MOV{N,Z} imm. 31:16.
2638 MOVW_GOTOFF_G1 = 302,
2639 /// Likewise for MOVK; no check.
2640 MOVW_GOTOFF_G1_NC = 303,
2641 /// GOT-rel. o. MOV{N,Z} imm. 47:32.
2642 MOVW_GOTOFF_G2 = 304,
2643 /// Likewise for MOVK; no check.
2644 MOVW_GOTOFF_G2_NC = 305,
2645 /// GOT-rel. o. MOV{N,Z} imm. 63:48.
2646 MOVW_GOTOFF_G3 = 306,
2647 /// GOT-relative 64-bit.
2648 GOTREL64 = 307,
2649 /// GOT-relative 32-bit.
2650 GOTREL32 = 308,
2651 /// PC-rel. GOT off. load imm. 20:2.
2652 GOT_LD_PREL19 = 309,
2653 /// GOT-rel. off. LD/ST imm. 14:3.
2654 LD64_GOTOFF_LO15 = 310,
2655 /// P-page-rel. GOT off. ADRP 32:12.
2656 ADR_GOT_PAGE = 311,
2657 /// Dir. GOT off. LD/ST imm. 11:3.
2658 LD64_GOT_LO12_NC = 312,
2659 /// GOT-page-rel. GOT off. LD/ST 14:3
2660 LD64_GOTPAGE_LO15 = 313,
2661 /// PC-relative ADR imm. 20:0.
2662 TLSGD_ADR_PREL21 = 512,
2663 /// page-rel. ADRP imm. 32:12.
2664 TLSGD_ADR_PAGE21 = 513,
2665 /// direct ADD imm. from 11:0.
2666 TLSGD_ADD_LO12_NC = 514,
2667 /// GOT-rel. MOV{N,Z} 31:16.
2668 TLSGD_MOVW_G1 = 515,
2669 /// GOT-rel. MOVK imm. 15:0.
2670 TLSGD_MOVW_G0_NC = 516,
2671 /// Like 512; local dynamic model.
2672 TLSLD_ADR_PREL21 = 517,
2673 /// Like 513; local dynamic model.
2674 TLSLD_ADR_PAGE21 = 518,
2675 /// Like 514; local dynamic model.
2676 TLSLD_ADD_LO12_NC = 519,
2677 /// Like 515; local dynamic model.
2678 TLSLD_MOVW_G1 = 520,
2679 /// Like 516; local dynamic model.
2680 TLSLD_MOVW_G0_NC = 521,
2681 /// TLS PC-rel. load imm. 20:2.
2682 TLSLD_LD_PREL19 = 522,
2683 /// TLS DTP-rel. MOV{N,Z} 47:32.
2684 TLSLD_MOVW_DTPREL_G2 = 523,
2685 /// TLS DTP-rel. MOV{N,Z} 31:16.
2686 TLSLD_MOVW_DTPREL_G1 = 524,
2687 /// Likewise; MOVK; no check.
2688 TLSLD_MOVW_DTPREL_G1_NC = 525,
2689 /// TLS DTP-rel. MOV{N,Z} 15:0.
2690 TLSLD_MOVW_DTPREL_G0 = 526,
2691 /// Likewise; MOVK; no check.
2692 TLSLD_MOVW_DTPREL_G0_NC = 527,
2693 /// DTP-rel. ADD imm. from 23:12.
2694 TLSLD_ADD_DTPREL_HI12 = 528,
2695 /// DTP-rel. ADD imm. from 11:0.
2696 TLSLD_ADD_DTPREL_LO12 = 529,
2697 /// Likewise; no ovfl. check.
2698 TLSLD_ADD_DTPREL_LO12_NC = 530,
2699 /// DTP-rel. LD/ST imm. 11:0.
2700 TLSLD_LDST8_DTPREL_LO12 = 531,
2701 /// Likewise; no check.
2702 TLSLD_LDST8_DTPREL_LO12_NC = 532,
2703 /// DTP-rel. LD/ST imm. 11:1.
2704 TLSLD_LDST16_DTPREL_LO12 = 533,
2705 /// Likewise; no check.
2706 TLSLD_LDST16_DTPREL_LO12_NC = 534,
2707 /// DTP-rel. LD/ST imm. 11:2.
2708 TLSLD_LDST32_DTPREL_LO12 = 535,
2709 /// Likewise; no check.
2710 TLSLD_LDST32_DTPREL_LO12_NC = 536,
2711 /// DTP-rel. LD/ST imm. 11:3.
2712 TLSLD_LDST64_DTPREL_LO12 = 537,
2713 /// Likewise; no check.
2714 TLSLD_LDST64_DTPREL_LO12_NC = 538,
2715 /// GOT-rel. MOV{N,Z} 31:16.
2716 TLSIE_MOVW_GOTTPREL_G1 = 539,
2717 /// GOT-rel. MOVK 15:0.
2718 TLSIE_MOVW_GOTTPREL_G0_NC = 540,
2719 /// Page-rel. ADRP 32:12.
2720 TLSIE_ADR_GOTTPREL_PAGE21 = 541,
2721 /// Direct LD off. 11:3.
2722 TLSIE_LD64_GOTTPREL_LO12_NC = 542,
2723 /// PC-rel. load imm. 20:2.
2724 TLSIE_LD_GOTTPREL_PREL19 = 543,
2725 /// TLS TP-rel. MOV{N,Z} 47:32.
2726 TLSLE_MOVW_TPREL_G2 = 544,
2727 /// TLS TP-rel. MOV{N,Z} 31:16.
2728 TLSLE_MOVW_TPREL_G1 = 545,
2729 /// Likewise; MOVK; no check.
2730 TLSLE_MOVW_TPREL_G1_NC = 546,
2731 /// TLS TP-rel. MOV{N,Z} 15:0.
2732 TLSLE_MOVW_TPREL_G0 = 547,
2733 /// Likewise; MOVK; no check.
2734 TLSLE_MOVW_TPREL_G0_NC = 548,
2735 /// TP-rel. ADD imm. 23:12.
2736 TLSLE_ADD_TPREL_HI12 = 549,
2737 /// TP-rel. ADD imm. 11:0.
2738 TLSLE_ADD_TPREL_LO12 = 550,
2739 /// Likewise; no ovfl. check.
2740 TLSLE_ADD_TPREL_LO12_NC = 551,
2741 /// TP-rel. LD/ST off. 11:0.
2742 TLSLE_LDST8_TPREL_LO12 = 552,
2743 /// Likewise; no ovfl. check.
2744 TLSLE_LDST8_TPREL_LO12_NC = 553,
2745 /// TP-rel. LD/ST off. 11:1.
2746 TLSLE_LDST16_TPREL_LO12 = 554,
2747 /// Likewise; no check.
2748 TLSLE_LDST16_TPREL_LO12_NC = 555,
2749 /// TP-rel. LD/ST off. 11:2.
2750 TLSLE_LDST32_TPREL_LO12 = 556,
2751 /// Likewise; no check.
2752 TLSLE_LDST32_TPREL_LO12_NC = 557,
2753 /// TP-rel. LD/ST off. 11:3.
2754 TLSLE_LDST64_TPREL_LO12 = 558,
2755 /// Likewise; no check.
2756 TLSLE_LDST64_TPREL_LO12_NC = 559,
2757 /// PC-rel. load immediate 20:2.
2758 TLSDESC_LD_PREL19 = 560,
2759 /// PC-rel. ADR immediate 20:0.
2760 TLSDESC_ADR_PREL21 = 561,
2761 /// Page-rel. ADRP imm. 32:12.
2762 TLSDESC_ADR_PAGE21 = 562,
2763 /// Direct LD off. from 11:3.
2764 TLSDESC_LD64_LO12 = 563,
2765 /// Direct ADD imm. from 11:0.
2766 TLSDESC_ADD_LO12 = 564,
2767 /// GOT-rel. MOV{N,Z} imm. 31:16.
2768 TLSDESC_OFF_G1 = 565,
2769 /// GOT-rel. MOVK imm. 15:0; no ck.
2770 TLSDESC_OFF_G0_NC = 566,
2771 /// Relax LDR.
2772 TLSDESC_LDR = 567,
2773 /// Relax ADD.
2774 TLSDESC_ADD = 568,
2775 /// Relax BLR.
2776 TLSDESC_CALL = 569,
2777 /// TP-rel. LD/ST off. 11:4.
2778 TLSLE_LDST128_TPREL_LO12 = 570,
2779 /// Likewise; no check.
2780 TLSLE_LDST128_TPREL_LO12_NC = 571,
2781 /// DTP-rel. LD/ST imm. 11:4.
2782 TLSLD_LDST128_DTPREL_LO12 = 572,
2783 /// Likewise; no check.
2784 TLSLD_LDST128_DTPREL_LO12_NC = 573,
2785 /// Copy symbol at runtime.
2786 COPY = 1024,
2787 /// Create GOT entry.
2788 GLOB_DAT = 1025,
2789 /// Create PLT entry.
2790 JUMP_SLOT = 1026,
2791 /// Adjust by program base.
2792 RELATIVE = 1027,
2793 /// Module number, 64 bit.
2794 TLS_DTPMOD = 1028,
2795 /// Module-relative offset, 64 bit.
2796 TLS_DTPREL = 1029,
2797 /// TP-relative offset, 64 bit.
2798 TLS_TPREL = 1030,
2799 /// TLS Descriptor.
2800 TLSDESC = 1031,
2801 /// STT_GNU_IFUNC relocation.
2802 IRELATIVE = 1032,
2803 _,
2804};
2805
2806/// RISC-V relocations.
2807pub const R_RISCV = enum(u32) {
2808 NONE = 0,
2809 @"32" = 1,
2810 @"64" = 2,
2811 RELATIVE = 3,
2812 COPY = 4,
2813 JUMP_SLOT = 5,
2814 TLS_DTPMOD32 = 6,
2815 TLS_DTPMOD64 = 7,
2816 TLS_DTPREL32 = 8,
2817 TLS_DTPREL64 = 9,
2818 TLS_TPREL32 = 10,
2819 TLS_TPREL64 = 11,
2820 TLSDESC = 12,
2821 BRANCH = 16,
2822 JAL = 17,
2823 CALL = 18,
2824 CALL_PLT = 19,
2825 GOT_HI20 = 20,
2826 TLS_GOT_HI20 = 21,
2827 TLS_GD_HI20 = 22,
2828 PCREL_HI20 = 23,
2829 PCREL_LO12_I = 24,
2830 PCREL_LO12_S = 25,
2831 HI20 = 26,
2832 LO12_I = 27,
2833 LO12_S = 28,
2834 TPREL_HI20 = 29,
2835 TPREL_LO12_I = 30,
2836 TPREL_LO12_S = 31,
2837 TPREL_ADD = 32,
2838 ADD8 = 33,
2839 ADD16 = 34,
2840 ADD32 = 35,
2841 ADD64 = 36,
2842 SUB8 = 37,
2843 SUB16 = 38,
2844 SUB32 = 39,
2845 SUB64 = 40,
2846 GNU_VTINHERIT = 41,
2847 GNU_VTENTRY = 42,
2848 ALIGN = 43,
2849 RVC_BRANCH = 44,
2850 RVC_JUMP = 45,
2851 RVC_LUI = 46,
2852 GPREL_I = 47,
2853 GPREL_S = 48,
2854 TPREL_I = 49,
2855 TPREL_S = 50,
2856 RELAX = 51,
2857 SUB6 = 52,
2858 SET6 = 53,
2859 SET8 = 54,
2860 SET16 = 55,
2861 SET32 = 56,
2862 @"32_PCREL" = 57,
2863 IRELATIVE = 58,
2864 PLT32 = 59,
2865 SET_ULEB128 = 60,
2866 SUB_ULEB128 = 61,
2867 _,
2868};
2869
2870/// PowerPC64 relocations.
2871pub const R_PPC64 = enum(u32) {
2872 NONE = 0,
2873 ADDR32 = 1,
2874 ADDR24 = 2,
2875 ADDR16 = 3,
2876 ADDR16_LO = 4,
2877 ADDR16_HI = 5,
2878 ADDR16_HA = 6,
2879 ADDR14 = 7,
2880 ADDR14_BRTAKEN = 8,
2881 ADDR14_BRNTAKEN = 9,
2882 REL24 = 10,
2883 REL14 = 11,
2884 REL14_BRTAKEN = 12,
2885 REL14_BRNTAKEN = 13,
2886 GOT16 = 14,
2887 GOT16_LO = 15,
2888 GOT16_HI = 16,
2889 GOT16_HA = 17,
2890 COPY = 19,
2891 GLOB_DAT = 20,
2892 JMP_SLOT = 21,
2893 RELATIVE = 22,
2894 REL32 = 26,
2895 PLT16_LO = 29,
2896 PLT16_HI = 30,
2897 PLT16_HA = 31,
2898 ADDR64 = 38,
2899 ADDR16_HIGHER = 39,
2900 ADDR16_HIGHERA = 40,
2901 ADDR16_HIGHEST = 41,
2902 ADDR16_HIGHESTA = 42,
2903 REL64 = 44,
2904 TOC16 = 47,
2905 TOC16_LO = 48,
2906 TOC16_HI = 49,
2907 TOC16_HA = 50,
2908 TOC = 51,
2909 ADDR16_DS = 56,
2910 ADDR16_LO_DS = 57,
2911 GOT16_DS = 58,
2912 GOT16_LO_DS = 59,
2913 PLT16_LO_DS = 60,
2914 TOC16_DS = 63,
2915 TOC16_LO_DS = 64,
2916 TLS = 67,
2917 DTPMOD64 = 68,
2918 TPREL16 = 69,
2919 TPREL16_LO = 70,
2920 TPREL16_HI = 71,
2921 TPREL16_HA = 72,
2922 TPREL64 = 73,
2923 DTPREL16 = 74,
2924 DTPREL16_LO = 75,
2925 DTPREL16_HI = 76,
2926 DTPREL16_HA = 77,
2927 DTPREL64 = 78,
2928 GOT_TLSGD16 = 79,
2929 GOT_TLSGD16_LO = 80,
2930 GOT_TLSGD16_HI = 81,
2931 GOT_TLSGD16_HA = 82,
2932 GOT_TLSLD16 = 83,
2933 GOT_TLSLD16_LO = 84,
2934 GOT_TLSLD16_HI = 85,
2935 GOT_TLSLD16_HA = 86,
2936 GOT_TPREL16_DS = 87,
2937 GOT_TPREL16_LO_DS = 88,
2938 GOT_TPREL16_HI = 89,
2939 GOT_TPREL16_HA = 90,
2940 GOT_DTPREL16_DS = 91,
2941 GOT_DTPREL16_LO_DS = 92,
2942 GOT_DTPREL16_HI = 93,
2943 GOT_DTPREL16_HA = 94,
2944 TPREL16_DS = 95,
2945 TPREL16_LO_DS = 96,
2946 TPREL16_HIGHER = 97,
2947 TPREL16_HIGHERA = 98,
2948 TPREL16_HIGHEST = 99,
2949 TPREL16_HIGHESTA = 100,
2950 DTPREL16_DS = 101,
2951 DTPREL16_LO_DS = 102,
2952 DTPREL16_HIGHER = 103,
2953 DTPREL16_HIGHERA = 104,
2954 DTPREL16_HIGHEST = 105,
2955 DTPREL16_HIGHESTA = 106,
2956 TLSGD = 107,
2957 TLSLD = 108,
2958 ADDR16_HIGH = 110,
2959 ADDR16_HIGHA = 111,
2960 TPREL16_HIGH = 112,
2961 TPREL16_HIGHA = 113,
2962 DTPREL16_HIGH = 114,
2963 DTPREL16_HIGHA = 115,
2964 REL24_NOTOC = 116,
2965 PLTSEQ = 119,
2966 PLTCALL = 120,
2967 PLTSEQ_NOTOC = 121,
2968 PLTCALL_NOTOC = 122,
2969 PCREL_OPT = 123,
2970 PCREL34 = 132,
2971 GOT_PCREL34 = 133,
2972 PLT_PCREL34 = 134,
2973 PLT_PCREL34_NOTOC = 135,
2974 TPREL34 = 146,
2975 DTPREL34 = 147,
2976 GOT_TLSGD_PCREL34 = 148,
2977 GOT_TLSLD_PCREL34 = 149,
2978 GOT_TPREL_PCREL34 = 150,
2979 IRELATIVE = 248,
2980 REL16 = 249,
2981 REL16_LO = 250,
2982 REL16_HI = 251,
2983 REL16_HA = 252,
2984 _,
2985};
2986
2987pub const ar_hdr = extern struct {
2988 /// Member file name, sometimes / terminated.
2989 ar_name: [16]u8,
2990
2991 /// File date, decimal seconds since Epoch.
2992 ar_date: [12]u8,
2993
2994 /// User ID, in ASCII format.
2995 ar_uid: [6]u8,
2996
2997 /// Group ID, in ASCII format.
2998 ar_gid: [6]u8,
2999
3000 /// File mode, in ASCII octal.
3001 ar_mode: [8]u8,
3002
3003 /// File size, in ASCII decimal.
3004 ar_size: [10]u8,
3005
3006 /// Always contains ARFMAG.
3007 ar_fmag: [2]u8,
3008
3009 pub fn date(self: ar_hdr) std.fmt.ParseIntError!u64 {
3010 const value = mem.trimEnd(u8, &self.ar_date, &[_]u8{0x20});
3011 return std.fmt.parseInt(u64, value, 10);
3012 }
3013
3014 pub fn size(self: ar_hdr) std.fmt.ParseIntError!u32 {
3015 const value = mem.trimEnd(u8, &self.ar_size, &[_]u8{0x20});
3016 return std.fmt.parseInt(u32, value, 10);
3017 }
3018
3019 pub fn isStrtab(self: ar_hdr) bool {
3020 return mem.eql(u8, &self.ar_name, STRNAME);
3021 }
3022
3023 pub fn isSymtab(self: ar_hdr) bool {
3024 return mem.eql(u8, &self.ar_name, SYMNAME);
3025 }
3026
3027 pub fn isSymtab64(self: ar_hdr) bool {
3028 return mem.eql(u8, &self.ar_name, SYM64NAME);
3029 }
3030
3031 pub fn isSymdef(self: ar_hdr) bool {
3032 return mem.eql(u8, &self.ar_name, SYMDEFNAME);
3033 }
3034
3035 pub fn isSymdefSorted(self: ar_hdr) bool {
3036 return mem.eql(u8, &self.ar_name, SYMDEFSORTEDNAME);
3037 }
3038
3039 pub fn name(self: *const ar_hdr) ?[]const u8 {
3040 const value = &self.ar_name;
3041 if (value[0] == '/') return null;
3042 const sentinel = mem.indexOfScalar(u8, value, '/') orelse value.len;
3043 return value[0..sentinel];
3044 }
3045
3046 pub fn nameOffset(self: ar_hdr) std.fmt.ParseIntError!?u32 {
3047 const value = &self.ar_name;
3048 if (value[0] != '/') return null;
3049 const trimmed = mem.trimEnd(u8, value, &[_]u8{0x20});
3050 return try std.fmt.parseInt(u32, trimmed[1..], 10);
3051 }
3052};
3053
3054fn genSpecialMemberName(comptime name: []const u8) *const [16]u8 {
3055 assert(name.len <= 16);
3056 const padding = 16 - name.len;
3057 return name ++ &[_]u8{0x20} ** padding;
3058}
3059
3060// Archive files start with the ARMAG identifying string. Then follows a
3061// `struct ar_hdr', and as many bytes of member file data as its `ar_size'
3062// member indicates, for each member file.
3063/// String that begins an archive file.
3064pub const ARMAG = "!<arch>\n";
3065/// String in ar_fmag at the end of each header.
3066pub const ARFMAG = "`\n";
3067/// 32-bit symtab identifier
3068pub const SYMNAME = genSpecialMemberName("/");
3069/// Strtab identifier
3070pub const STRNAME = genSpecialMemberName("//");
3071/// 64-bit symtab identifier
3072pub const SYM64NAME = genSpecialMemberName("/SYM64/");
3073pub const SYMDEFNAME = genSpecialMemberName("__.SYMDEF");
3074pub const SYMDEFSORTEDNAME = genSpecialMemberName("__.SYMDEF SORTED");
3075
3076pub const gnu_hash = struct {
3077
3078 // See https://flapenguin.me/elf-dt-gnu-hash
3079
3080 pub const Header = extern struct {
3081 nbuckets: u32,
3082 symoffset: u32,
3083 bloom_size: u32,
3084 bloom_shift: u32,
3085 };
3086
3087 pub const ChainEntry = packed struct(u32) {
3088 end_of_chain: bool,
3089 /// Contains the top bits of the hash value.
3090 hash: u31,
3091 };
3092
3093 /// Calculate the hash value for a name
3094 pub fn calculate(name: []const u8) u32 {
3095 var hash: u32 = 5381;
3096
3097 for (name) |char| {
3098 hash = (hash << 5) +% hash +% char;
3099 }
3100
3101 return hash;
3102 }
3103
3104 test calculate {
3105 try std.testing.expectEqual(0x00001505, calculate(""));
3106 try std.testing.expectEqual(0x156b2bb8, calculate("printf"));
3107 try std.testing.expectEqual(0x7c967e3f, calculate("exit"));
3108 try std.testing.expectEqual(0xbac212a0, calculate("syscall"));
3109 try std.testing.expectEqual(0x8ae9f18e, calculate("flapenguin.me"));
3110 }
3111};