@isopodlabs/binary_libs 0.0.1

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