@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/.gitmodules +3 -0
- package/.vscode/launch.json +16 -0
- package/.vscode/settings.json +3 -0
- package/README.md +152 -44
- package/eslint.config.mjs +32 -0
- package/package.json +9 -2
- package/src/CompoundDocument.ts +5 -5
- package/src/arch.ts +16 -13
- package/src/clr.ts +53 -58
- package/src/elf.ts +544 -545
- package/src/mach.ts +172 -121
- package/src/pe.ts +209 -213
- package/transform.ts +369 -0
- package/tsconfig.json +10 -3
- package/tsconfig.tsbuildinfo +1 -0
- package/dist/CompoundDocument.d.ts +0 -129
- package/dist/CompoundDocument.js +0 -301
- package/dist/arch.d.ts +0 -41
- package/dist/arch.js +0 -94
- package/dist/binary.d.ts +0 -397
- package/dist/binary.js +0 -802
- package/dist/binary_helpers.d.ts +0 -69
- package/dist/binary_helpers.js +0 -328
- package/dist/clr.d.ts +0 -63
- package/dist/clr.js +0 -664
- package/dist/elf.d.ts +0 -11
- package/dist/elf.js +0 -791
- package/dist/mach.d.ts +0 -543
- package/dist/mach.js +0 -1034
- package/dist/pe.d.ts +0 -399
- package/dist/pe.js +0 -489
package/src/elf.ts
CHANGED
|
@@ -2,49 +2,51 @@ import * as binary from '@isopodlabs/binary';
|
|
|
2
2
|
|
|
3
3
|
//-------------------- FILE HEADER
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
CLASSNONE
|
|
7
|
-
CLASS32
|
|
8
|
-
CLASS64
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
NONE
|
|
13
|
-
LSB
|
|
14
|
-
MSB
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
SYSV
|
|
19
|
-
HPUX
|
|
20
|
-
STANDALONE
|
|
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.
|
|
27
|
-
encoding: binary.
|
|
26
|
+
file_class: binary.asEnum(binary.UINT8, CLASS),
|
|
27
|
+
encoding: binary.asEnum(binary.UINT8, DATA),
|
|
28
28
|
version: binary.UINT8,
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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
|
-
|
|
36
|
-
NONE
|
|
37
|
-
REL
|
|
38
|
-
EXEC
|
|
39
|
-
DYN
|
|
40
|
-
CORE
|
|
41
|
-
LOOS
|
|
42
|
-
HIOS
|
|
43
|
-
LOPROC
|
|
44
|
-
HIPROC
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const EM
|
|
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
|
-
|
|
146
|
-
NONE
|
|
147
|
-
CURRENT
|
|
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
|
-
|
|
154
|
-
NULL
|
|
155
|
-
LOAD
|
|
156
|
-
DYNAMIC
|
|
157
|
-
INTERP
|
|
158
|
-
NOTE
|
|
159
|
-
SHLIB
|
|
160
|
-
PHDR
|
|
161
|
-
TLS
|
|
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
|
|
164
|
-
UNWIND
|
|
165
|
-
EH_FRAME
|
|
166
|
-
GNU_STACK
|
|
167
|
-
GNU_RELRO
|
|
168
|
-
OS_SCE
|
|
169
|
-
HIOS
|
|
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
|
|
172
|
-
HIPROC
|
|
173
|
-
}
|
|
173
|
+
LOPROC: 0x70000000,
|
|
174
|
+
HIPROC: 0x7fffffff,
|
|
175
|
+
} as const;
|
|
174
176
|
|
|
175
|
-
|
|
176
|
-
X
|
|
177
|
-
W
|
|
178
|
-
R
|
|
179
|
-
MASKPROC
|
|
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
|
-
|
|
185
|
-
UNDEF
|
|
186
|
+
const SHN = {
|
|
187
|
+
UNDEF: 0, //undefined
|
|
186
188
|
// LORESERVE = 0xff00, //lower bound of the range of reserved indexes
|
|
187
|
-
LOPROC
|
|
188
|
-
HIPROC
|
|
189
|
-
LOOS
|
|
190
|
-
HIOS
|
|
191
|
-
ABS
|
|
192
|
-
COMMON
|
|
193
|
-
HIRESERVE
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
NULL
|
|
198
|
-
PROGBITS
|
|
199
|
-
SYMTAB
|
|
200
|
-
STRTAB
|
|
201
|
-
RELA
|
|
202
|
-
HASH
|
|
203
|
-
DYNAMIC
|
|
204
|
-
NOTE
|
|
205
|
-
NOBITS
|
|
206
|
-
REL
|
|
207
|
-
SHLIB
|
|
208
|
-
DYNSYM
|
|
209
|
-
LOOS
|
|
210
|
-
HIOS
|
|
211
|
-
LOPROC
|
|
212
|
-
HIPROC
|
|
213
|
-
LOUSER
|
|
214
|
-
HIUSER
|
|
215
|
-
|
|
216
|
-
PS3_RELA
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
WRITE
|
|
221
|
-
ALLOC
|
|
222
|
-
EXECINSTR
|
|
223
|
-
MASKOS
|
|
224
|
-
MASKPROC
|
|
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
|
-
|
|
265
|
-
//Name Value
|
|
266
|
-
DT_NULL
|
|
267
|
-
DT_NEEDED
|
|
268
|
-
DT_PLTRELSZ
|
|
269
|
-
DT_PLTGOT
|
|
270
|
-
DT_HASH
|
|
271
|
-
DT_STRTAB
|
|
272
|
-
DT_SYMTAB
|
|
273
|
-
DT_RELA
|
|
274
|
-
DT_RELASZ
|
|
275
|
-
DT_RELAENT
|
|
276
|
-
DT_STRSZ
|
|
277
|
-
DT_SYMENT
|
|
278
|
-
DT_INIT
|
|
279
|
-
DT_FINI
|
|
280
|
-
DT_SONAME
|
|
281
|
-
DT_RPATH
|
|
282
|
-
DT_SYMBOLIC
|
|
283
|
-
DT_REL
|
|
284
|
-
DT_RELSZ
|
|
285
|
-
DT_RELENT
|
|
286
|
-
DT_PLTREL
|
|
287
|
-
DT_DEBUG
|
|
288
|
-
DT_TEXTREL
|
|
289
|
-
DT_JMPREL
|
|
290
|
-
DT_BIND_NOW
|
|
291
|
-
DT_INIT_ARRAY
|
|
292
|
-
DT_FINI_ARRAY
|
|
293
|
-
DT_INIT_ARRAYSZ
|
|
294
|
-
DT_FINI_ARRAYSZ
|
|
295
|
-
DT_RUNPATH
|
|
296
|
-
DT_FLAGS
|
|
297
|
-
DT_ENCODING
|
|
298
|
-
// DT_PREINIT_ARRAY
|
|
299
|
-
DT_PREINIT_ARRAYSZ
|
|
300
|
-
DT_LOOS
|
|
301
|
-
DT_HIOS
|
|
302
|
-
DT_LOPROC
|
|
303
|
-
DT_HIPROC
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
DF_ORIGIN
|
|
308
|
-
DF_SYMBOLIC
|
|
309
|
-
DF_TEXTREL
|
|
310
|
-
DF_BIND_NOW
|
|
311
|
-
DF_STATIC_TLS
|
|
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
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
// Relocs added to support TLS.
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
// TLS
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
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
|
|
617
|
-
sections
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
header: any;
|
|
621
|
-
|
|
625
|
+
segments;
|
|
626
|
+
sections;
|
|
627
|
+
header;
|
|
628
|
+
|
|
622
629
|
static check(data: Uint8Array): boolean {
|
|
623
|
-
|
|
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.
|
|
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.
|
|
693
|
+
data: binary.MappedMemory;
|
|
683
694
|
constructor(s: binary.stream) {
|
|
684
695
|
super(s);
|
|
685
|
-
const flags = (this.p_flags.R ? binary.
|
|
686
|
-
| (this.p_flags.W ? binary.
|
|
687
|
-
| (this.p_flags.X ? binary.
|
|
688
|
-
this.data = new binary.
|
|
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.
|
|
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.
|
|
715
|
+
data: binary.MappedMemory;
|
|
705
716
|
constructor(s: binary.stream) {
|
|
706
717
|
super(s);
|
|
707
|
-
const flags = binary.
|
|
708
|
-
| (this.sh_flags.WRITE ? binary.
|
|
709
|
-
| (this.sh_flags.EXECINSTR ? binary.
|
|
710
|
-
this.data = new binary.
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
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
|
-
|
|
781
|
-
|
|
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
|
-
*/
|