@isopodlabs/binary_libs 0.1.1 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/elf.js ADDED
@@ -0,0 +1,778 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.ELFFile = void 0;
27
+ const binary = __importStar(require("@isopodlabs/binary"));
28
+ //-------------------- FILE HEADER
29
+ const CLASS = {
30
+ CLASSNONE: 0, // Invalid class
31
+ CLASS32: 1, // 32-bit objects
32
+ CLASS64: 2, // 64-bit objects
33
+ };
34
+ const DATA = {
35
+ NONE: 0, // Invalid data encoding
36
+ LSB: 1, // little endian
37
+ MSB: 2, // big endian
38
+ };
39
+ const OSABI = {
40
+ SYSV: 0, // System V ABI
41
+ HPUX: 1, // HP-UX operating system
42
+ STANDALONE: 255, // Standalone (embedded)application
43
+ };
44
+ const Ident = {
45
+ //enum {MAGIC = '\177ELF'};
46
+ magic: binary.UINT32_LE,
47
+ file_class: binary.asEnum(binary.UINT8, CLASS),
48
+ encoding: binary.asEnum(binary.UINT8, DATA),
49
+ version: binary.UINT8,
50
+ _: binary.If(obj => obj.file_class === 'CLASS64', {
51
+ //64 bit only
52
+ osabi: binary.asEnum(binary.UINT8, OSABI),
53
+ abiversion: binary.UINT8,
54
+ pad: binary.ArrayType(7, binary.UINT8),
55
+ })
56
+ };
57
+ const ET = {
58
+ NONE: 0, // No file type
59
+ REL: 1, // Relocatable file
60
+ EXEC: 2, // Executable file
61
+ DYN: 3, // Shared object file
62
+ CORE: 4, // Core file
63
+ LOOS: 0xfe00, // Environment-specific use
64
+ HIOS: 0xfeff, // Environment-specific use
65
+ LOPROC: 0xff00, // Processor-specific
66
+ HIPROC: 0xffff, // Processor-specific
67
+ };
68
+ const EM = {
69
+ NONE: 0, // e_machine
70
+ M32: 1, // AT&T WE 32100
71
+ SPARC: 2, // Sun SPARC
72
+ '386': 3, // Intel 80386
73
+ '68K': 4, // Motorola 68000
74
+ '88K': 5, // Motorola 88000
75
+ '486': 6, // Intel 80486
76
+ '860': 7, // Intel i860
77
+ MIPS: 8, // MIPS RS3000 Big-Endian
78
+ S370: 9, // IBM System/370 Processor
79
+ MIPS_RS3_LE: 10, // MIPS RS3000 Little-Endian
80
+ RS6000: 11, // RS6000
81
+ UNKNOWN12: 12,
82
+ UNKNOWN13: 13,
83
+ UNKNOWN14: 14,
84
+ PA_RISC: 15, // PA-RISC
85
+ nCUBE: 16, // nCUBE
86
+ VPP500: 17, // Fujitsu VPP500
87
+ SPARC32PLUS: 18, // Sun SPARC 32+
88
+ '960': 19, // Intel 80960
89
+ PPC: 20, // PowerPC
90
+ PPC64: 21, // 64-bit PowerPC
91
+ S390: 22, // IBM System/390 Processor
92
+ SPE: 23,
93
+ UNKNOWN24: 24,
94
+ UNKNOWN25: 25,
95
+ UNKNOWN26: 26,
96
+ UNKNOWN27: 27,
97
+ UNKNOWN28: 28,
98
+ UNKNOWN29: 29,
99
+ UNKNOWN30: 30,
100
+ UNKNOWN31: 31,
101
+ UNKNOWN32: 32,
102
+ UNKNOWN33: 33,
103
+ UNKNOWN34: 34,
104
+ UNKNOWN35: 35,
105
+ V800: 36, // NEX V800
106
+ FR20: 37, // Fujitsu FR20
107
+ RH32: 38, // TRW RH-32
108
+ RCE: 39, // Motorola RCE
109
+ ARM: 40, // Advanced RISC Marchines ARM
110
+ ALPHA: 41, // Digital Alpha
111
+ SH: 42, // Hitachi SH
112
+ SPARCV9: 43, // Sun SPARC V9 (64-bit)
113
+ TRICORE: 44, // Siemens Tricore embedded processor
114
+ ARC: 45, // Argonaut RISC Core, Argonaut Technologies Inc.
115
+ H8_300: 46, // Hitachi H8/300
116
+ H8_300H: 47, // Hitachi H8/300H
117
+ H8S: 48, // Hitachi H8S
118
+ H8_500: 49, // Hitachi H8/500
119
+ IA_64: 50, // Intel IA64
120
+ MIPS_X: 51, // Stanford MIPS-X
121
+ COLDFIRE: 52, // Motorola ColdFire
122
+ '68HC12': 53, // Motorola M68HC12
123
+ MMA: 54, // Fujitsu MMA Mulimedia Accelerator
124
+ PCP: 55, // Siemens PCP
125
+ NCPU: 56, // Sony nCPU embedded RISC processor
126
+ NDR1: 57, // Denso NDR1 microprocessor
127
+ STARCORE: 58, // Motorola Star*Core processor
128
+ ME16: 59, // Toyota ME16 processor
129
+ ST100: 60, // STMicroelectronics ST100 processor
130
+ TINYJ: 61, // Advanced Logic Corp. TinyJ embedded processor family
131
+ AMD64: 62, // AMDs x86-64 architecture
132
+ PDSP: 63, // Sony DSP Processor
133
+ UNKNOWN64: 64,
134
+ UNKNOWN65: 65,
135
+ FX66: 66, // Siemens FX66 microcontroller
136
+ ST9PLUS: 67, // STMicroelectronics ST9+8/16 bit microcontroller
137
+ ST7: 68, // STMicroelectronics ST7 8-bit microcontroller
138
+ '68HC16': 69, // Motorola MC68HC16 Microcontroller
139
+ '68HC11': 70, // Motorola MC68HC11 Microcontroller
140
+ '68HC08': 71, // Motorola MC68HC08 Microcontroller
141
+ '68HC05': 72, // Motorola MC68HC05 Microcontroller
142
+ SVX: 73, // Silicon Graphics SVx
143
+ ST19: 74, // STMicroelectronics ST19 8-bit microcontroller
144
+ VAX: 75, // Digital VAX
145
+ CRIS: 76, // Axis Communications 32-bit embedded processor
146
+ JAVELIN: 77, // Infineon Technologies 32-bit embedded processor
147
+ FIREPATH: 78, // Element 14 64-bit DSP Processor
148
+ ZSP: 79, // LSI Logic 16-bit DSP Processor
149
+ MMIX: 80, // Donald Knuth's educational 64-bit processor
150
+ HUANY: 81, // Harvard University machine-independent object files
151
+ PRISM: 82, // SiTera Prism
152
+ AVR: 83, // Atmel AVR 8-bit microcontroller
153
+ FR30: 84, // Fujitsu FR30
154
+ D10V: 85, // Mitsubishi D10V
155
+ D30V: 86, // Mitsubishi D30V
156
+ V850: 87, // NEC v850
157
+ M32R: 88, // Mitsubishi M32R
158
+ MN10300: 89, // Matsushita MN10300
159
+ MN10200: 90, // Matsushita MN10200
160
+ PJ: 91, // picoJava
161
+ OPENRISC: 92, // OpenRISC 32-bit embedded processor
162
+ ARC_A5: 93, // ARC Cores Tangent-A5
163
+ XTENSA: 94, // Tensilica Xtensa architecture
164
+ };
165
+ const EV = {
166
+ NONE: 0, // Invalid version
167
+ CURRENT: 1, // Current version
168
+ };
169
+ //-------------------- PROGRAM HEADER
170
+ const PT = {
171
+ NULL: 0, //Unused - nables the program header table to contain ignored entries
172
+ LOAD: 1, //loadable segment, described by p_filesz and p_memsz. The bytes from the file are mapped to the beginning of the memory segment
173
+ DYNAMIC: 2, //dynamic linking information
174
+ INTERP: 3, //null-terminated path name to invoke as an interpreter
175
+ NOTE: 4, //auxiliary information
176
+ SHLIB: 5, //Reserved but has unspecified semantics
177
+ PHDR: 6, //program header table in the file and in the memory image of the program
178
+ TLS: 7, //thread-local storage template
179
+ //OS-specific semantics.
180
+ LOOS: 0x60000000,
181
+ UNWIND: 0x6464e550, //stack unwind tables.
182
+ EH_FRAME: 0x6474e550, //stack unwind table - equivalent to UNWIND
183
+ GNU_STACK: 0x6474e551, //stack flags
184
+ GNU_RELRO: 0x6474e552, //read only after relocation
185
+ OS_SCE: 0x6fffff00,
186
+ HIOS: 0x6fffffff,
187
+ //processor-specific semantics.
188
+ LOPROC: 0x70000000,
189
+ HIPROC: 0x7fffffff,
190
+ };
191
+ const PF = {
192
+ X: 0x1, //Execute
193
+ W: 0x2, //Write
194
+ R: 0x4, //Read
195
+ MASKPROC: 0xf0000000, //Unspecified
196
+ };
197
+ //-------------------- SECTIONS
198
+ const SHN = {
199
+ UNDEF: 0, //undefined
200
+ // LORESERVE = 0xff00, //lower bound of the range of reserved indexes
201
+ LOPROC: 0xff00, //reserved for processor-specific semantics
202
+ HIPROC: 0xff1f, // "
203
+ LOOS: 0xff20, //Environment-specific use
204
+ HIOS: 0xff3f, //Environment-specific use
205
+ ABS: 0xfff1, //
206
+ COMMON: 0xfff2, //common symbols
207
+ HIRESERVE: 0xffff, //upper bound of the range of reserved indexes
208
+ };
209
+ const SHT = {
210
+ NULL: 0, //section header as inactive
211
+ PROGBITS: 1, //information defined by the program
212
+ SYMTAB: 2, //symbol table
213
+ STRTAB: 3, //string table
214
+ RELA: 4, //relocation entries with explicit addends
215
+ HASH: 5, //hash table
216
+ DYNAMIC: 6, //information for dynamic linking
217
+ NOTE: 7, //marks the file in some way
218
+ NOBITS: 8, //occupies no space in file
219
+ REL: 9, //relocation entries without explicit addends
220
+ SHLIB: 10, //reserved
221
+ DYNSYM: 11, //symbol table for linking only
222
+ LOOS: 0x60000000, //Environment-specific use
223
+ HIOS: 0x6fffffff, //Environment-specific use
224
+ LOPROC: 0x70000000, //Processor- specific
225
+ HIPROC: 0x7fffffff, //Processor- specific
226
+ LOUSER: 0x80000000,
227
+ HIUSER: 0xffffffff,
228
+ PS3_RELA: 0x70000000 + 0xa4,
229
+ };
230
+ const SHF = {
231
+ WRITE: 0x1, //contains writable data
232
+ ALLOC: 0x2, //occupies memory during xecution
233
+ EXECINSTR: 0x4, //contains executable machine instructions
234
+ MASKOS: 0x0f000000, //environment-specific use
235
+ MASKPROC: 0xf0000000, //processor-specific semantics
236
+ };
237
+ //-------------------- SYMBOLS
238
+ const ST_INFO = binary.BitFields({
239
+ type: [4, binary.Enum({
240
+ NOTYPE: 0, //The symbol's type is not specified
241
+ OBJECT: 1, //associated with a data object
242
+ FUNC: 2, //associated with a function
243
+ SECTION: 3, //associated with a section
244
+ FILE: 4, //name of the source file
245
+ LOOS: 10, //environment-specific use
246
+ HIOS: 12,
247
+ LOPROC: 13,
248
+ HIPROC: 15,
249
+ })],
250
+ binding: [4, binary.Enum({
251
+ LOCAL: 0, //not visible outside the object file containing their definition
252
+ GLOBAL: 1, //visible to all object files being combined
253
+ WEAK: 2, //like global symbols, but lower precedence
254
+ LOOS: 10, //environment-specific use
255
+ HIOS: 12,
256
+ LOPROC: 13,
257
+ HIPROC: 15,
258
+ })],
259
+ });
260
+ const ST_OTHER = binary.BitFields({
261
+ visibility: [2, binary.Enum({
262
+ DEFAULT: 0,
263
+ HIDDEN: 1,
264
+ PROTECTED: 2,
265
+ })],
266
+ other: 6,
267
+ });
268
+ //-------------------- DYNAMIC
269
+ const DT_TAG = {
270
+ //Name Value d_un Executable Shared Object
271
+ DT_NULL: 0, //ignored mandatory mandatory
272
+ DT_NEEDED: 1, //d_val optional optional
273
+ DT_PLTRELSZ: 2, //d_val optional optional
274
+ DT_PLTGOT: 3, //d_ptr optional optional
275
+ DT_HASH: 4, //d_ptr mandatory mandatory
276
+ DT_STRTAB: 5, //d_ptr mandatory mandatory
277
+ DT_SYMTAB: 6, //d_ptr mandatory mandatory
278
+ DT_RELA: 7, //d_ptr mandatory optional
279
+ DT_RELASZ: 8, //d_val mandatory optional
280
+ DT_RELAENT: 9, //d_val mandatory optional
281
+ DT_STRSZ: 10, //d_val mandatory mandatory
282
+ DT_SYMENT: 11, //d_val mandatory mandatory
283
+ DT_INIT: 12, //d_ptr optional optional
284
+ DT_FINI: 13, //d_ptr optional optional
285
+ DT_SONAME: 14, //d_val ignored optional
286
+ DT_RPATH: 15, //d_val optional ignored (LEVEL2)
287
+ DT_SYMBOLIC: 16, //ignored ignored optional (LEVEL2)
288
+ DT_REL: 17, //d_ptr mandatory optional
289
+ DT_RELSZ: 18, //d_val mandatory optional
290
+ DT_RELENT: 19, //d_val mandatory optional
291
+ DT_PLTREL: 20, //d_val optional optional
292
+ DT_DEBUG: 21, //d_ptr optional ignored
293
+ DT_TEXTREL: 22, //ignored optional optional (LEVEL2)
294
+ DT_JMPREL: 23, //d_ptr optional optional
295
+ DT_BIND_NOW: 24, //ignored optional optional (LEVEL2)
296
+ DT_INIT_ARRAY: 25, //d_ptr optional optional
297
+ DT_FINI_ARRAY: 26, //d_ptr optional optional
298
+ DT_INIT_ARRAYSZ: 27, //d_val optional optional
299
+ DT_FINI_ARRAYSZ: 28, //d_val optional optional
300
+ DT_RUNPATH: 29, //d_val optional optional
301
+ DT_FLAGS: 30, //d_val optional optional
302
+ DT_ENCODING: 32, //unspecified unspecified unspecified
303
+ // DT_PREINIT_ARRAY: 32, //d_ptr optional ignored
304
+ DT_PREINIT_ARRAYSZ: 33, //d_val optional ignored
305
+ DT_LOOS: 0x6000000D, //unspecified unspecified unspecified
306
+ DT_HIOS: 0x6ffff000, //unspecified unspecified unspecified
307
+ DT_LOPROC: 0x70000000, //unspecified unspecified unspecified
308
+ DT_HIPROC: 0x7fffffff, //unspecified unspecified unspecified
309
+ };
310
+ /*
311
+ const DT_FLAGS = {
312
+ DF_ORIGIN: 0x1,
313
+ DF_SYMBOLIC: 0x2,
314
+ DF_TEXTREL: 0x4,
315
+ DF_BIND_NOW: 0x8,
316
+ DF_STATIC_TLS: 0x10,
317
+ } as const;
318
+ */
319
+ //-------------------- RELOC
320
+ const RELOC = {
321
+ '386': {
322
+ R_386_NONE: 0,
323
+ R_386_32: 1,
324
+ R_386_PC32: 2,
325
+ R_386_GOT32: 3,
326
+ R_386_PLT32: 4,
327
+ R_386_COPY: 5,
328
+ R_386_GLOB_DAT: 6,
329
+ R_386_JMP_SLOT: 7,
330
+ R_386_RELATIVE: 8,
331
+ R_386_GOTOFF: 9,
332
+ R_386_GOTPC: 10,
333
+ R_386_32PLT: 11,
334
+ R_386_TLS_GD_PLT: 12,
335
+ R_386_TLS_LDM_PLT: 13,
336
+ R_386_TLS_TPOFF: 14,
337
+ R_386_TLS_IE: 15,
338
+ R_386_TLS_GOTIE: 16,
339
+ R_386_TLS_LE: 17,
340
+ R_386_TLS_GD: 18,
341
+ R_386_TLS_LDM: 19,
342
+ R_386_16: 20,
343
+ R_386_PC16: 21,
344
+ R_386_8: 22,
345
+ R_386_PC8: 23,
346
+ R_386_UNKNOWN24: 24,
347
+ R_386_UNKNOWN25: 25,
348
+ R_386_UNKNOWN26: 26,
349
+ R_386_UNKNOWN27: 27,
350
+ R_386_UNKNOWN28: 28,
351
+ R_386_UNKNOWN29: 29,
352
+ R_386_UNKNOWN30: 30,
353
+ R_386_UNKNOWN31: 31,
354
+ R_386_TLS_LDO_32: 32,
355
+ R_386_UNKNOWN33: 33,
356
+ R_386_UNKNOWN34: 34,
357
+ R_386_TLS_DTPMOD32: 35,
358
+ R_386_TLS_DTPOFF32: 36,
359
+ R_386_UNKNOWN37: 37,
360
+ R_386_SIZE32: 38,
361
+ R_386_NUM: 39
362
+ },
363
+ 'PPC': {
364
+ R_PPC_NONE: 0,
365
+ R_PPC_ADDR32: 1,
366
+ R_PPC_ADDR24: 2,
367
+ R_PPC_ADDR16: 3,
368
+ R_PPC_ADDR16_LO: 4,
369
+ R_PPC_ADDR16_HI: 5,
370
+ R_PPC_ADDR16_HA: 6,
371
+ R_PPC_ADDR14: 7,
372
+ R_PPC_ADDR14_BRTAKEN: 8,
373
+ R_PPC_ADDR14_BRNTAKEN: 9,
374
+ R_PPC_REL24: 10,
375
+ R_PPC_REL14: 11,
376
+ R_PPC_REL14_BRTAKEN: 12,
377
+ R_PPC_REL14_BRNTAKEN: 13,
378
+ R_PPC_GOT16: 14,
379
+ R_PPC_GOT16_LO: 15,
380
+ R_PPC_GOT16_HI: 16,
381
+ R_PPC_GOT16_HA: 17,
382
+ R_PPC_PLTREL24: 18,
383
+ R_PPC_COPY: 19,
384
+ R_PPC_GLOB_DAT: 20,
385
+ R_PPC_JMP_SLOT: 21,
386
+ R_PPC_RELATIVE: 22,
387
+ R_PPC_LOCAL24PC: 23,
388
+ R_PPC_UADDR32: 24,
389
+ R_PPC_UADDR16: 25,
390
+ R_PPC_REL32: 26,
391
+ R_PPC_PLT32: 27,
392
+ R_PPC_PLTREL32: 28,
393
+ R_PPC_PLT16_LO: 29,
394
+ R_PPC_PLT16_HI: 30,
395
+ R_PPC_PLT16_HA: 31,
396
+ R_PPC_SDAREL16: 32,
397
+ R_PPC_SECTOFF: 33,
398
+ R_PPC_SECTOFF_LO: 34,
399
+ R_PPC_SECTOFF_HI: 35,
400
+ R_PPC_SECTOFF_HA: 36,
401
+ R_PPC_ADDR30: 37,
402
+ // Relocs added to support TLS.
403
+ R_PPC_TLS: 67,
404
+ R_PPC_DTPMOD32: 68,
405
+ R_PPC_TPREL16: 69,
406
+ R_PPC_TPREL16_LO: 70,
407
+ R_PPC_TPREL16_HI: 71,
408
+ R_PPC_TPREL16_HA: 72,
409
+ R_PPC_TPREL32: 73,
410
+ R_PPC_DTPREL16: 74,
411
+ R_PPC_DTPREL16_LO: 75,
412
+ R_PPC_DTPREL16_HI: 76,
413
+ R_PPC_DTPREL16_HA: 77,
414
+ R_PPC_DTPREL32: 78,
415
+ R_PPC_GOT_TLSGD16: 79,
416
+ R_PPC_GOT_TLSGD16_LO: 80,
417
+ R_PPC_GOT_TLSGD16_HI: 81,
418
+ R_PPC_GOT_TLSGD16_HA: 82,
419
+ R_PPC_GOT_TLSLD16: 83,
420
+ R_PPC_GOT_TLSLD16_LO: 84,
421
+ R_PPC_GOT_TLSLD16_HI: 85,
422
+ R_PPC_GOT_TLSLD16_HA: 86,
423
+ R_PPC_GOT_TPREL16: 87,
424
+ R_PPC_GOT_TPREL16_LO: 88,
425
+ R_PPC_GOT_TPREL16_HI: 89,
426
+ R_PPC_GOT_TPREL16_HA: 90,
427
+ R_PPC_GOT_DTPREL16: 91,
428
+ R_PPC_GOT_DTPREL16_LO: 92,
429
+ R_PPC_GOT_DTPREL16_HI: 93,
430
+ R_PPC_GOT_DTPREL16_HA: 94,
431
+ // The remaining relocs are from the Embedded ELF ABI and are not in the SVR4 ELF ABI
432
+ R_PPC_EMB_NADDR32: 101,
433
+ R_PPC_EMB_NADDR16: 102,
434
+ R_PPC_EMB_NADDR16_LO: 103,
435
+ R_PPC_EMB_NADDR16_HI: 104,
436
+ R_PPC_EMB_NADDR16_HA: 105,
437
+ R_PPC_EMB_SDAI16: 106,
438
+ R_PPC_EMB_SDA2I16: 107,
439
+ R_PPC_EMB_SDA2REL: 108,
440
+ R_PPC_EMB_SDA21: 109,
441
+ R_PPC_EMB_MRKREF: 110,
442
+ R_PPC_EMB_RELSEC16: 111,
443
+ R_PPC_EMB_RELST_LO: 112,
444
+ R_PPC_EMB_RELST_HI: 113,
445
+ R_PPC_EMB_RELST_HA: 114,
446
+ R_PPC_EMB_BIT_FLD: 115,
447
+ R_PPC_EMB_RELSDA: 116,
448
+ },
449
+ 'PPC64': {
450
+ R_PPC64_NONE: 0,
451
+ R_PPC64_ADDR32: 1,
452
+ R_PPC64_ADDR24: 2,
453
+ R_PPC64_ADDR16: 3,
454
+ R_PPC64_ADDR16_LO: 4,
455
+ R_PPC64_ADDR16_HI: 5,
456
+ R_PPC64_ADDR16_HA: 6,
457
+ R_PPC64_ADDR14: 7,
458
+ R_PPC64_ADDR14_BRTAKEN: 8,
459
+ R_PPC64_ADDR14_BRNTAKEN: 9,
460
+ R_PPC64_REL24: 10,
461
+ R_PPC64_REL14: 11,
462
+ R_PPC64_REL14_BRTAKEN: 12,
463
+ R_PPC64_REL14_BRNTAKEN: 13,
464
+ R_PPC64_GOT16: 14,
465
+ R_PPC64_GOT16_LO: 15,
466
+ R_PPC64_GOT16_HI: 16,
467
+ R_PPC64_GOT16_HA: 17,
468
+ // 18 unused. = 32-bit reloc is R_PPC_PLTREL24.
469
+ R_PPC64_COPY: 19,
470
+ R_PPC64_GLOB_DAT: 20,
471
+ R_PPC64_JMP_SLOT: 21,
472
+ R_PPC64_RELATIVE: 22,
473
+ // 23 unused. = 32-bit reloc is R_PPC_LOCAL24PC.
474
+ R_PPC64_UADDR32: 24,
475
+ R_PPC64_UADDR16: 25,
476
+ R_PPC64_REL32: 26,
477
+ R_PPC64_PLT32: 27,
478
+ R_PPC64_PLTREL32: 28,
479
+ R_PPC64_PLT16_LO: 29,
480
+ R_PPC64_PLT16_HI: 30,
481
+ R_PPC64_PLT16_HA: 31,
482
+ // 32 unused. = 32-bit reloc is R_PPC_SDAREL16.
483
+ R_PPC64_SECTOFF: 33,
484
+ R_PPC64_SECTOFF_LO: 34,
485
+ R_PPC64_SECTOFF_HI: 35,
486
+ R_PPC64_SECTOFF_HA: 36,
487
+ R_PPC64_REL30: 37,
488
+ R_PPC64_ADDR64: 38,
489
+ R_PPC64_ADDR16_HIGHER: 39,
490
+ R_PPC64_ADDR16_HIGHERA: 40,
491
+ R_PPC64_ADDR16_HIGHEST: 41,
492
+ R_PPC64_ADDR16_HIGHESTA: 42,
493
+ R_PPC64_UADDR64: 43,
494
+ R_PPC64_REL64: 44,
495
+ R_PPC64_PLT64: 45,
496
+ R_PPC64_PLTREL64: 46,
497
+ R_PPC64_TOC16: 47,
498
+ R_PPC64_TOC16_LO: 48,
499
+ R_PPC64_TOC16_HI: 49,
500
+ R_PPC64_TOC16_HA: 50,
501
+ R_PPC64_TOC: 51,
502
+ R_PPC64_PLTGOT16: 52,
503
+ R_PPC64_PLTGOT16_LO: 53,
504
+ R_PPC64_PLTGOT16_HI: 54,
505
+ R_PPC64_PLTGOT16_HA: 55,
506
+ // The following relocs were added in the 64-bit PowerPC ELF ABI revision 1.2.
507
+ R_PPC64_ADDR16_DS: 56,
508
+ R_PPC64_ADDR16_LO_DS: 57,
509
+ R_PPC64_GOT16_DS: 58,
510
+ R_PPC64_GOT16_LO_DS: 59,
511
+ R_PPC64_PLT16_LO_DS: 60,
512
+ R_PPC64_SECTOFF_DS: 61,
513
+ R_PPC64_SECTOFF_LO_DS: 62,
514
+ R_PPC64_TOC16_DS: 63,
515
+ R_PPC64_TOC16_LO_DS: 64,
516
+ R_PPC64_PLTGOT16_DS: 65,
517
+ R_PPC64_PLTGOT16_LO_DS: 66,
518
+ // Relocs added to support TLS. PowerPC64 ELF ABI revision 1.5.
519
+ R_PPC64_TLS: 67,
520
+ R_PPC64_DTPMOD64: 68,
521
+ R_PPC64_TPREL16: 69,
522
+ R_PPC64_TPREL16_LO: 70,
523
+ R_PPC64_TPREL16_HI: 71,
524
+ R_PPC64_TPREL16_HA: 72,
525
+ R_PPC64_TPREL64: 73,
526
+ R_PPC64_DTPREL16: 74,
527
+ R_PPC64_DTPREL16_LO: 75,
528
+ R_PPC64_DTPREL16_HI: 76,
529
+ R_PPC64_DTPREL16_HA: 77,
530
+ R_PPC64_DTPREL64: 78,
531
+ R_PPC64_GOT_TLSGD16: 79,
532
+ R_PPC64_GOT_TLSGD16_LO: 80,
533
+ R_PPC64_GOT_TLSGD16_HI: 81,
534
+ R_PPC64_GOT_TLSGD16_HA: 82,
535
+ R_PPC64_GOT_TLSLD16: 83,
536
+ R_PPC64_GOT_TLSLD16_LO: 84,
537
+ R_PPC64_GOT_TLSLD16_HI: 85,
538
+ R_PPC64_GOT_TLSLD16_HA: 86,
539
+ R_PPC64_GOT_TPREL16_DS: 87,
540
+ R_PPC64_GOT_TPREL16_LO_DS: 88,
541
+ R_PPC64_GOT_TPREL16_HI: 89,
542
+ R_PPC64_GOT_TPREL16_HA: 90,
543
+ R_PPC64_GOT_DTPREL16_DS: 91,
544
+ R_PPC64_GOT_DTPREL16_LO_DS: 92,
545
+ R_PPC64_GOT_DTPREL16_HI: 93,
546
+ R_PPC64_GOT_DTPREL16_HA: 94,
547
+ R_PPC64_TPREL16_DS: 95,
548
+ R_PPC64_TPREL16_LO_DS: 96,
549
+ R_PPC64_TPREL16_HIGHER: 97,
550
+ R_PPC64_TPREL16_HIGHERA: 98,
551
+ R_PPC64_TPREL16_HIGHEST: 99,
552
+ R_PPC64_TPREL16_HIGHESTA: 100,
553
+ R_PPC64_DTPREL16_DS: 101,
554
+ R_PPC64_DTPREL16_LO_DS: 102,
555
+ R_PPC64_DTPREL16_HIGHER: 103,
556
+ R_PPC64_DTPREL16_HIGHERA: 104,
557
+ R_PPC64_DTPREL16_HIGHEST: 105,
558
+ R_PPC64_DTPREL16_HIGHESTA: 106,
559
+ // These are GNU extensions to enable C++ vtable garbage collection.
560
+ R_PPC64_GNU_VTINHERIT: 253,
561
+ R_PPC64_GNU_VTENTRY: 254,
562
+ },
563
+ 'AMD64': {
564
+ R_AMD64_NONE: 0, // No reloc
565
+ R_AMD64_64: 1, // Direct 64 bit
566
+ R_AMD64_PC32: 2, // PC relative 32 bit signed
567
+ R_AMD64_GOT32: 3, // 32 bit GOT entry
568
+ R_AMD64_PLT32: 4, // 32 bit PLT address
569
+ R_AMD64_COPY: 5, // Copy symbol at runtime
570
+ R_AMD64_GLOB_DAT: 6, // Create GOT entry
571
+ R_AMD64_JUMP_SLOT: 7, // Create PLT entry
572
+ R_AMD64_RELATIVE: 8, // Adjust by program base
573
+ R_AMD64_GOTPCREL: 9, // 32 bit signed PC relative offset to GOT
574
+ R_AMD64_32: 10, // Direct 32 bit zero extended
575
+ R_AMD64_32S: 11, // Direct 32 bit sign extended
576
+ R_AMD64_16: 12, // Direct 16 bit zero extended
577
+ R_AMD64_PC16: 13, // 16 bit sign extended pc relative
578
+ R_AMD64_8: 14, // Direct 8 bit sign extended
579
+ R_AMD64_PC8: 15, // 8 bit sign extended pc relative
580
+ // TLS relocations
581
+ R_AMD64_DTPMOD64: 16, // ID of module containing symbol
582
+ R_AMD64_DTPOFF64: 17, // Offset in module's TLS block
583
+ R_AMD64_TPOFF64: 18, // Offset in initial TLS block
584
+ R_AMD64_TLSGD: 19, // 32 bit signed PC relative offset to two GOT entries for GD symbol
585
+ R_AMD64_TLSLD: 20, // 32 bit signed PC relative offset to two GOT entries for LD symbol
586
+ R_AMD64_DTPOFF32: 21, // Offset in TLS block
587
+ R_AMD64_GOTTPOFF: 22, // 32 bit signed PC relative offset to GOT entry for IE symbol
588
+ R_AMD64_TPOFF32: 23, // Offset in initial TLS block
589
+ R_AMD64_PC64: 24, // 64-bit PC relative
590
+ R_AMD64_GOTOFF64: 25, // 64-bit GOT offset
591
+ R_AMD64_GOTPC32: 26, // 32-bit PC relative offset to GOT
592
+ R_AMD64_GOT64: 27, // 64-bit GOT entry offset
593
+ R_AMD64_GOTPCREL64: 28, // 64-bit PC relative offset to GOT entry
594
+ R_AMD64_GOTPC64: 29, // 64-bit PC relative offset to GOT
595
+ R_AMD64_GOTPLT64: 30, // Like GOT64, indicates that PLT entry needed
596
+ R_AMD64_PLTOFF64: 31, // 64-bit GOT relative offset to PLT entry
597
+ R_AMD64_SIZE32: 32,
598
+ R_AMD64_SIZE64: 33,
599
+ R_AMD64_GOTPC32_TLSDESC: 34, // 32-bit PC relative to TLS descriptor in GOT
600
+ R_AMD64_TLSDESC_CALL: 35, // Relaxable call through TLS descriptor
601
+ R_AMD64_TLSDESC: 36, // 2 by 64-bit TLS descriptor
602
+ R_AMD64_IRELATIVE: 37, // Adjust indirectly by program base
603
+ // GNU vtable garbage collection extensions.
604
+ R_AMD64_GNU_VTINHERIT: 250,
605
+ R_AMD64_GNU_VTENTRY: 251
606
+ }
607
+ };
608
+ //-----------------------------------------------------------------------------
609
+ // ELF
610
+ //-----------------------------------------------------------------------------
611
+ function Pair(top, bottom, be) {
612
+ return be ? { top, bottom } : { bottom, top };
613
+ }
614
+ function readDataAs(data, type) {
615
+ if (data)
616
+ return binary.RemainingArrayType(type).get(new binary.stream(data.data));
617
+ }
618
+ class ELFFile {
619
+ segments;
620
+ sections;
621
+ header;
622
+ static check(data) {
623
+ return binary.utils.decodeText(data.subarray(0, 4), 'utf8') === '\x7fELF';
624
+ }
625
+ getDynamic;
626
+ getRel;
627
+ getRelA;
628
+ getSymbols;
629
+ getDynamicSymbols;
630
+ constructor(data) {
631
+ const s = new binary.stream(data);
632
+ const ident = binary.read(s, Ident);
633
+ if (ident.magic != binary.utils.stringCode("\x7fELF"))
634
+ throw new Error('Not an ELF file');
635
+ const be = ident.encoding == 'MSB';
636
+ const bits = ident.file_class == 'CLASS32' ? 32 : 64;
637
+ const Half = binary.UINT(16, be);
638
+ const Sword = binary.INT(32, be);
639
+ const Word = binary.UINT(32, be);
640
+ const Addr = binary.asHex(binary.UINT(bits, be));
641
+ const Off = binary.UINT(bits, be);
642
+ const Xword = binary.INT(bits, be);
643
+ const Sxword = binary.UINT(bits, be);
644
+ const PairHalf = binary.UINT(bits == 32 ? 8 : 32, be);
645
+ const Ehdr = {
646
+ e_type: binary.asEnum(Half, ET), //Object file type (ET_..)
647
+ e_machine: binary.asEnum(Half, EM), //specifies the required architecture (EM_...)
648
+ e_version: binary.asEnum(Word, EV), //object file version (EV_...)
649
+ e_entry: Addr, //run address
650
+ e_phoff: Off, //program header table's file offset
651
+ e_shoff: Off, //section header table's file offset
652
+ e_flags: Word, //processor-specific flags (EF_...)
653
+ e_ehsize: Half, //ELF header's size
654
+ e_phentsize: Half, //size of each entry in the program header table
655
+ e_phnum: Half, //number of entries in the program header table
656
+ e_shentsize: Half, //size of each section header
657
+ e_shnum: Half, //number of entries in the section header table
658
+ e_shstrndx: Half, //section header table index of section name string table
659
+ };
660
+ class Phdr extends binary.ReadClass(bits == 32 ? {
661
+ p_type: binary.asEnum(Word, PT), //kind of segment this array element describes
662
+ p_offset: Off, //offset from the beginning of the file at which the first byte of the segment resides
663
+ p_vaddr: Addr, //virtual address at which the first byte of the segment resides in memory
664
+ p_paddr: Addr, //segment's physical address (when relevant)
665
+ p_filesz: Word, //number of bytes in the file image of the segment
666
+ p_memsz: Word, //number of bytes in the memory image of the segment
667
+ p_flags: binary.asFlags(Word, PF),
668
+ p_align: Word,
669
+ } : {
670
+ p_type: binary.asEnum(Word, PT),
671
+ p_flags: binary.asFlags(Word, PF),
672
+ p_offset: Off,
673
+ p_vaddr: Addr,
674
+ p_paddr: Addr,
675
+ p_filesz: Xword,
676
+ p_memsz: Xword,
677
+ p_align: Xword,
678
+ }) {
679
+ data;
680
+ constructor(s) {
681
+ super(s);
682
+ const flags = (this.p_flags.R ? 1 /* binary.MEM.READ */ : 0)
683
+ | (this.p_flags.W ? 2 /* binary.MEM.WRITE */ : 0)
684
+ | (this.p_flags.X ? 4 /* binary.MEM.EXECUTE */ : 0);
685
+ this.data = new binary.MappedMemory(s.buffer_at(Number(this.p_offset), Number(this.p_filesz)), +this.p_vaddr, flags);
686
+ }
687
+ }
688
+ class Shdr extends binary.ReadClass({
689
+ sh_name: Word, //name of the section
690
+ sh_type: binary.asEnum(Word, SHT), //categorizes the section's contents and semantics
691
+ sh_flags: binary.asFlags(Xword, SHF), //miscellaneous attributes
692
+ sh_addr: Addr, //address
693
+ sh_offset: Off, //file offset to first byte in section
694
+ sh_size: Off, //section's size in bytes
695
+ sh_link: Word, //section header table index link
696
+ sh_info: Word, //extra information
697
+ sh_addralign: Off, //address alignment constraints
698
+ sh_entsize: Off, //size in bytes of each entry (when appropriate)
699
+ }) {
700
+ data;
701
+ constructor(s) {
702
+ super(s);
703
+ const flags = 1 /* binary.MEM.READ */
704
+ | (this.sh_flags.WRITE ? 2 /* binary.MEM.WRITE */ : 0)
705
+ | (this.sh_flags.EXECINSTR ? 4 /* binary.MEM.EXECUTE */ : 0);
706
+ this.data = new binary.MappedMemory(s.buffer_at(Number(this.sh_offset), Number(this.sh_size)), +this.sh_addr, flags);
707
+ }
708
+ }
709
+ const h = binary.read(s, Ehdr);
710
+ this.header = h;
711
+ s.seek(Number(h.e_phoff));
712
+ const ph = Array.from({ length: h.e_phnum }, () => new Phdr(s));
713
+ s.seek(Number(h.e_shoff));
714
+ const sh = Array.from({ length: h.e_shnum }, () => new Shdr(s));
715
+ //set segment names
716
+ this.segments = ph.map(i => [`${i.p_type} @ 0x${i.p_offset.toString(16)}`, i]);
717
+ //set section names
718
+ const shnames = sh[h.e_shstrndx].data.data;
719
+ this.sections = sh.map(i => [binary.utils.decodeTextTo0(shnames.subarray(i.sh_name), 'utf8'), i]);
720
+ const Dyn = {
721
+ d_tag: binary.asEnum(Sword, DT_TAG),
722
+ d_val: Xword,
723
+ };
724
+ const Rel = {
725
+ r_offset: Addr,
726
+ r_info: Pair(binary.asEnum(PairHalf, RELOC[h.e_machine]), PairHalf, be),
727
+ };
728
+ const Rela = {
729
+ ...Rel,
730
+ r_addend: Sxword,
731
+ };
732
+ this.getDynamic = () => readDataAs(ph.find(i => i.p_type === 'DYNAMIC')?.data, Dyn);
733
+ this.getRel = () => readDataAs(sh.find(i => i.sh_type === 'REL')?.data, Rel);
734
+ this.getRelA = () => readDataAs(sh.find(i => i.sh_type === 'RELA')?.data, Rela);
735
+ const Sym = {
736
+ data: binary.DontRead(),
737
+ st_name: Word, //index into the object file's symbol string table
738
+ ...(bits === 32 ? {
739
+ st_value: Addr, //value of the associated symbol
740
+ st_size: Word, //associated size
741
+ st_info: binary.as(binary.UINT8, ST_INFO), //symbol's type and binding attributes
742
+ st_other: binary.as(binary.UINT8, ST_OTHER),
743
+ st_shndx: binary.asEnum(Half, SHN), //section header table index
744
+ } : {
745
+ st_info: binary.as(binary.UINT8, ST_INFO),
746
+ st_other: binary.as(binary.UINT8, ST_OTHER),
747
+ st_shndx: binary.asEnum(Half, SHN),
748
+ st_value: Addr,
749
+ st_size: Off,
750
+ })
751
+ };
752
+ function getSymbols(name) {
753
+ const sect = sh.find(i => i.sh_type === name);
754
+ if (sect) {
755
+ const names = sh[sect.sh_link].data.data;
756
+ const syms = readDataAs(sect.data, Sym);
757
+ return syms.filter(sym => sym.st_name).map(sym => {
758
+ if (+sym.st_shndx) {
759
+ const section = sh[+sym.st_shndx];
760
+ const offset = Number(sym.st_value.value) - Number(section.sh_addr.value);
761
+ const flags = sym.st_info.type === 'FUNC' ? section.data.flags : section.data.flags & ~4 /* binary.MEM.EXECUTE */;
762
+ sym.data = new binary.MappedMemory(section.data.data.subarray(offset, offset + Number(sym.st_size)), Number(sym.st_value.value), flags);
763
+ }
764
+ return [binary.utils.decodeTextTo0(names.subarray(sym.st_name), 'utf8'), sym];
765
+ });
766
+ }
767
+ }
768
+ this.getSymbols = () => getSymbols('SYMTAB');
769
+ this.getDynamicSymbols = () => getSymbols('DYNSYM');
770
+ }
771
+ getSegmentByType(type) {
772
+ return this.segments.find(i => i[1].p_type === type)?.[1];
773
+ }
774
+ getSectionByType(type) {
775
+ return this.sections.find(i => i[1].sh_type === type)?.[1];
776
+ }
777
+ }
778
+ exports.ELFFile = ELFFile;