@isopodlabs/binary_libs 0.0.1 → 0.1.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 CHANGED
@@ -2,49 +2,51 @@ import * as binary from '@isopodlabs/binary';
2
2
 
3
3
  //-------------------- FILE HEADER
4
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
- }
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
22
 
23
23
  const Ident = {
24
24
  //enum {MAGIC = '\177ELF'};
25
25
  magic: binary.UINT32_LE,
26
- file_class: binary.as(binary.UINT8, binary.Enum(CLASS)),
27
- encoding: binary.as(binary.UINT8, binary.Enum(DATA)),
26
+ file_class: binary.asEnum(binary.UINT8, CLASS),
27
+ encoding: binary.asEnum(binary.UINT8, DATA),
28
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),
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
+ })
33
35
  };
34
36
 
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> = {
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 = {
48
50
  NONE: 0, // e_machine
49
51
  M32: 1, // AT&T WE 32100
50
52
  SPARC: 2, // Sun SPARC
@@ -140,89 +142,89 @@ const EM: Record<string, number> = {
140
142
  OPENRISC: 92, // OpenRISC 32-bit embedded processor
141
143
  ARC_A5: 93, // ARC Cores Tangent-A5
142
144
  XTENSA: 94, // Tensilica Xtensa architecture
143
- }
145
+ } as const;
144
146
 
145
- enum EV {
146
- NONE = 0, // Invalid version
147
- CURRENT = 1, // Current version
148
- }
147
+ const EV = {
148
+ NONE: 0, // Invalid version
149
+ CURRENT: 1, // Current version
150
+ } as const;
149
151
 
150
152
 
151
153
  //-------------------- PROGRAM HEADER
152
154
 
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
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
162
164
  //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,
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,
170
172
  //processor-specific semantics.
171
- LOPROC = 0x70000000,
172
- HIPROC = 0x7fffffff,
173
- }
173
+ LOPROC: 0x70000000,
174
+ HIPROC: 0x7fffffff,
175
+ } as const;
174
176
 
175
- enum PF {
176
- X = 0x1, //Execute
177
- W = 0x2, //Write
178
- R = 0x4, //Read
179
- MASKPROC = 0xf0000000, //Unspecified
180
- }
177
+ const PF = {
178
+ X: 0x1, //Execute
179
+ W: 0x2, //Write
180
+ R: 0x4, //Read
181
+ MASKPROC: 0xf0000000, //Unspecified
182
+ } as const;
181
183
 
182
184
  //-------------------- SECTIONS
183
185
 
184
- enum SHN {
185
- UNDEF = 0, //undefined
186
+ const SHN = {
187
+ UNDEF: 0, //undefined
186
188
  // 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
- }
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;
226
228
 
227
229
  //-------------------- SYMBOLS
228
230
 
@@ -261,369 +263,379 @@ const ST_OTHER = binary.BitFields({
261
263
 
262
264
  //-------------------- DYNAMIC
263
265
 
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
-
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
+ */
314
316
  //-------------------- RELOC
315
317
 
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
- }
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
+ };
610
611
 
611
612
  //-----------------------------------------------------------------------------
612
613
  // ELF
613
614
  //-----------------------------------------------------------------------------
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
+
615
624
  export class ELFFile {
616
- segments: [string, any][] = [];
617
- sections: [string, any][] = [];
618
- symbols?: [string, any][];
619
- dynamic_symbols?: [string, any][];
620
- header: any;
621
-
625
+ segments;
626
+ sections;
627
+ header;
628
+
622
629
  static check(data: Uint8Array): boolean {
623
- const x = binary.utils.decodeText(data.subarray(0, 4), 'utf8');
624
- return x == '\x7fELF';
630
+ return binary.utils.decodeText(data.subarray(0, 4), 'utf8') === '\x7fELF';
625
631
  }
626
632
 
633
+ getDynamic;
634
+ getRel;
635
+ getRelA;
636
+ getSymbols;
637
+ getDynamicSymbols;
638
+
627
639
  constructor(data: Uint8Array) {
628
640
  const s = new binary.stream(data);
629
641
  const ident = binary.read(s, Ident);
@@ -633,16 +645,15 @@ export class ELFFile {
633
645
  const be = ident.encoding == 'MSB';
634
646
  const bits = ident.file_class == 'CLASS32' ? 32 : 64;
635
647
 
636
- const Addr = binary.as(binary.UINT(bits, be), binary.hex);
637
- const Off = binary.UINT(bits, be);
638
648
  const Half = binary.UINT(16, be);
639
649
  const Sword = binary.INT(32, be);
640
650
  const Word = binary.UINT(32, be);
651
+
652
+ const Addr = binary.asHex(binary.UINT(bits, be));
653
+ const Off = binary.UINT(bits, be);
641
654
  const Xword = binary.INT(bits, be);
642
655
  const Sxword = binary.UINT(bits, be);
643
-
644
656
  const PairHalf= binary.UINT(bits == 32 ? 8 : 32, be);
645
- const Pair = be ? {top: PairHalf, bottom: PairHalf} : {bottom: PairHalf, top: PairHalf};
646
657
 
647
658
  const Ehdr = {
648
659
  e_type: binary.asEnum(Half, ET), //Object file type (ET_..)
@@ -660,7 +671,7 @@ export class ELFFile {
660
671
  e_shstrndx: Half, //section header table index of section name string table
661
672
  };
662
673
 
663
- class Phdr extends binary.ReadStruct(bits == 32 ? {
674
+ class Phdr extends binary.ReadClass(bits == 32 ? {
664
675
  p_type: binary.asEnum(Word, PT), //kind of segment this array element describes
665
676
  p_offset: Off, //offset from the beginning of the file at which the first byte of the segment resides
666
677
  p_vaddr: Addr, //virtual address at which the first byte of the segment resides in memory
@@ -679,17 +690,17 @@ export class ELFFile {
679
690
  p_memsz: Xword,
680
691
  p_align: Xword,
681
692
  }) {
682
- data: binary.utils.MappedMemory;
693
+ data: binary.MappedMemory;
683
694
  constructor(s: binary.stream) {
684
695
  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);
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);
689
700
  }
690
701
  }
691
702
 
692
- class Shdr extends binary.ReadStruct({
703
+ class Shdr extends binary.ReadClass({
693
704
  sh_name: Word, //name of the section
694
705
  sh_type: binary.asEnum(Word, SHT), //categorizes the section's contents and semantics
695
706
  sh_flags: binary.asFlags(Xword, SHF), //miscellaneous attributes
@@ -701,50 +712,16 @@ export class ELFFile {
701
712
  sh_addralign: Off, //address alignment constraints
702
713
  sh_entsize: Off, //size in bytes of each entry (when appropriate)
703
714
  }) {
704
- data: binary.utils.MappedMemory;
715
+ data: binary.MappedMemory;
705
716
  constructor(s: binary.stream) {
706
717
  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);
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);
711
722
  }
712
723
  }
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
-
724
+
748
725
  const h = binary.read(s, Ehdr);
749
726
  this.header = h;
750
727
 
@@ -755,49 +732,71 @@ export class ELFFile {
755
732
  const sh = Array.from({length: h.e_shnum}, () => new Shdr(s));
756
733
 
757
734
  //set segment names
758
- for (const i of ph)
759
- this.segments.push([`${i.p_type} @ 0x${i.p_offset.toString(16)}`, i]);
735
+ this.segments = ph.map(i => [`${i.p_type} @ 0x${i.p_offset.toString(16)}`, i] as [string, typeof i]);
760
736
 
761
737
  //set section names
762
738
  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
- }
739
+ this.sections = sh.map(i => [binary.utils.decodeTextTo0(shnames.subarray(i.sh_name), 'utf8'), i] as [string, typeof i]);
767
740
 
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;
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
+ });
776
789
  }
777
790
  }
791
+
792
+ this.getSymbols = () => getSymbols('SYMTAB');
793
+ this.getDynamicSymbols = () => getSymbols('DYNSYM');
778
794
  }
779
795
 
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
- })
796
+ getSegmentByType(type: keyof typeof PT) {
797
+ return this.segments.find(i => i[1].p_type === type)?.[1];
791
798
  }
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);
799
+ getSectionByType(type: keyof typeof SHT) {
800
+ return this.sections.find(i => i[1].sh_type === type)?.[1];
801
801
  }
802
802
  }
803
- */