@isopodlabs/binary_libs 0.1.1 → 0.1.2

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