@isopodlabs/binary_libs 1.2.0 → 1.3.0
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/LICENSE.txt +21 -0
- package/README.md +4 -1
- package/dist/CompoundDocument.d.ts +10 -10
- package/dist/CompoundDocument.js +45 -43
- package/dist/arch.js +2 -2
- package/dist/clr.d.ts +75 -54
- package/dist/clr.js +126 -114
- package/dist/cvinfo.d.ts +26 -0
- package/dist/cvinfo.js +930 -0
- package/dist/elf.d.ts +26 -20
- package/dist/elf.js +61 -61
- package/dist/mach.d.ts +176 -146
- package/dist/mach.js +101 -101
- package/dist/pe.d.ts +162 -132
- package/dist/pe.js +178 -172
- package/package.json +46 -43
- package/dist/CompoundDocument.js.map +0 -1
- package/dist/arch.js.map +0 -1
- package/dist/clr.js.map +0 -1
- package/dist/elf.js.map +0 -1
- package/dist/mach.js.map +0 -1
- package/dist/pe.js.map +0 -1
package/dist/elf.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as bin from '@isopodlabs/binary';
|
|
2
2
|
declare const PT: {
|
|
3
3
|
readonly NULL: 0;
|
|
4
4
|
readonly LOAD: 1;
|
|
@@ -42,22 +42,22 @@ declare const SHT: {
|
|
|
42
42
|
export declare class ELFFile {
|
|
43
43
|
static check(data: Uint8Array): boolean;
|
|
44
44
|
segments: [string, {
|
|
45
|
-
data:
|
|
45
|
+
data: bin.MappedMemory;
|
|
46
46
|
p_type: string;
|
|
47
47
|
p_flags: Record<string, bigint | boolean> | Record<string, number | boolean>;
|
|
48
48
|
p_offset: number | bigint;
|
|
49
|
-
p_vaddr:
|
|
50
|
-
p_paddr:
|
|
49
|
+
p_vaddr: bin.hex<number | bigint>;
|
|
50
|
+
p_paddr: bin.hex<number | bigint>;
|
|
51
51
|
p_filesz: number | bigint;
|
|
52
52
|
p_memsz: number | bigint;
|
|
53
53
|
p_align: number | bigint;
|
|
54
54
|
}][];
|
|
55
55
|
sections: [string, {
|
|
56
|
-
data:
|
|
56
|
+
data: bin.MappedMemory;
|
|
57
57
|
sh_name: number;
|
|
58
58
|
sh_type: string;
|
|
59
59
|
sh_flags: Record<string, bigint | boolean> | Record<string, number | boolean>;
|
|
60
|
-
sh_addr:
|
|
60
|
+
sh_addr: bin.hex<number | bigint>;
|
|
61
61
|
sh_offset: number | bigint;
|
|
62
62
|
sh_size: number | bigint;
|
|
63
63
|
sh_link: number;
|
|
@@ -69,7 +69,7 @@ export declare class ELFFile {
|
|
|
69
69
|
e_type: string;
|
|
70
70
|
e_machine: string;
|
|
71
71
|
e_version: string;
|
|
72
|
-
e_entry:
|
|
72
|
+
e_entry: bin.hex<number | bigint>;
|
|
73
73
|
e_phoff: number | bigint;
|
|
74
74
|
e_shoff: number | bigint;
|
|
75
75
|
e_flags: number;
|
|
@@ -85,13 +85,19 @@ export declare class ELFFile {
|
|
|
85
85
|
d_val: number | bigint;
|
|
86
86
|
}[] | undefined;
|
|
87
87
|
getRel: () => {
|
|
88
|
-
r_offset:
|
|
89
|
-
r_info:
|
|
88
|
+
r_offset: bin.hex<number | bigint>;
|
|
89
|
+
r_info: {
|
|
90
|
+
top: any;
|
|
91
|
+
bottom: any;
|
|
92
|
+
};
|
|
90
93
|
}[] | undefined;
|
|
91
94
|
getRelA: () => {
|
|
92
95
|
r_addend: number | bigint;
|
|
93
|
-
r_offset:
|
|
94
|
-
r_info:
|
|
96
|
+
r_offset: bin.hex<number | bigint>;
|
|
97
|
+
r_info: {
|
|
98
|
+
top: any;
|
|
99
|
+
bottom: any;
|
|
100
|
+
};
|
|
95
101
|
}[] | undefined;
|
|
96
102
|
getSymbols: () => [string, {
|
|
97
103
|
st_info: {
|
|
@@ -103,9 +109,9 @@ export declare class ELFFile {
|
|
|
103
109
|
other: number | bigint;
|
|
104
110
|
};
|
|
105
111
|
st_shndx: string;
|
|
106
|
-
st_value:
|
|
112
|
+
st_value: bin.hex<number | bigint>;
|
|
107
113
|
st_size: number | bigint;
|
|
108
|
-
data:
|
|
114
|
+
data: bin.MappedMemory | undefined;
|
|
109
115
|
st_name: number;
|
|
110
116
|
}][] | undefined;
|
|
111
117
|
getDynamicSymbols: () => [string, {
|
|
@@ -118,29 +124,29 @@ export declare class ELFFile {
|
|
|
118
124
|
other: number | bigint;
|
|
119
125
|
};
|
|
120
126
|
st_shndx: string;
|
|
121
|
-
st_value:
|
|
127
|
+
st_value: bin.hex<number | bigint>;
|
|
122
128
|
st_size: number | bigint;
|
|
123
|
-
data:
|
|
129
|
+
data: bin.MappedMemory | undefined;
|
|
124
130
|
st_name: number;
|
|
125
131
|
}][] | undefined;
|
|
126
132
|
constructor(data: Uint8Array);
|
|
127
133
|
getSegmentByType(type: keyof typeof PT): {
|
|
128
|
-
data:
|
|
134
|
+
data: bin.MappedMemory;
|
|
129
135
|
p_type: string;
|
|
130
136
|
p_flags: Record<string, bigint | boolean> | Record<string, number | boolean>;
|
|
131
137
|
p_offset: number | bigint;
|
|
132
|
-
p_vaddr:
|
|
133
|
-
p_paddr:
|
|
138
|
+
p_vaddr: bin.hex<number | bigint>;
|
|
139
|
+
p_paddr: bin.hex<number | bigint>;
|
|
134
140
|
p_filesz: number | bigint;
|
|
135
141
|
p_memsz: number | bigint;
|
|
136
142
|
p_align: number | bigint;
|
|
137
143
|
} | undefined;
|
|
138
144
|
getSectionByType(type: keyof typeof SHT): {
|
|
139
|
-
data:
|
|
145
|
+
data: bin.MappedMemory;
|
|
140
146
|
sh_name: number;
|
|
141
147
|
sh_type: string;
|
|
142
148
|
sh_flags: Record<string, bigint | boolean> | Record<string, number | boolean>;
|
|
143
|
-
sh_addr:
|
|
149
|
+
sh_addr: bin.hex<number | bigint>;
|
|
144
150
|
sh_offset: number | bigint;
|
|
145
151
|
sh_size: number | bigint;
|
|
146
152
|
sh_link: number;
|
package/dist/elf.js
CHANGED
|
@@ -24,7 +24,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
26
|
exports.ELFFile = void 0;
|
|
27
|
-
const
|
|
27
|
+
const bin = __importStar(require("@isopodlabs/binary"));
|
|
28
28
|
//-------------------- FILE HEADER
|
|
29
29
|
const CLASS = {
|
|
30
30
|
CLASSNONE: 0, // Invalid class
|
|
@@ -43,15 +43,15 @@ const OSABI = {
|
|
|
43
43
|
};
|
|
44
44
|
const Ident = {
|
|
45
45
|
//enum {MAGIC = '\177ELF'};
|
|
46
|
-
magic:
|
|
47
|
-
file_class:
|
|
48
|
-
encoding:
|
|
49
|
-
version:
|
|
50
|
-
_:
|
|
46
|
+
magic: bin.UINT32_LE,
|
|
47
|
+
file_class: bin.asEnum(bin.UINT8, CLASS),
|
|
48
|
+
encoding: bin.asEnum(bin.UINT8, DATA),
|
|
49
|
+
version: bin.UINT8,
|
|
50
|
+
_: bin.If(s => s.obj.file_class === 'CLASS64', {
|
|
51
51
|
//64 bit only
|
|
52
|
-
osabi:
|
|
53
|
-
abiversion:
|
|
54
|
-
pad:
|
|
52
|
+
osabi: bin.asEnum(bin.UINT8, OSABI),
|
|
53
|
+
abiversion: bin.UINT8,
|
|
54
|
+
pad: bin.ArrayType(7, bin.UINT8),
|
|
55
55
|
})
|
|
56
56
|
};
|
|
57
57
|
const ET = {
|
|
@@ -235,8 +235,8 @@ const SHF = {
|
|
|
235
235
|
MASKPROC: 0xf0000000, //processor-specific semantics
|
|
236
236
|
};
|
|
237
237
|
//-------------------- SYMBOLS
|
|
238
|
-
const ST_INFO =
|
|
239
|
-
type: [4,
|
|
238
|
+
const ST_INFO = bin.BitFields({
|
|
239
|
+
type: [4, bin.Enum({
|
|
240
240
|
NOTYPE: 0, //The symbol's type is not specified
|
|
241
241
|
OBJECT: 1, //associated with a data object
|
|
242
242
|
FUNC: 2, //associated with a function
|
|
@@ -247,7 +247,7 @@ const ST_INFO = binary.BitFields({
|
|
|
247
247
|
LOPROC: 13,
|
|
248
248
|
HIPROC: 15,
|
|
249
249
|
})],
|
|
250
|
-
binding: [4,
|
|
250
|
+
binding: [4, bin.Enum({
|
|
251
251
|
LOCAL: 0, //not visible outside the object file containing their definition
|
|
252
252
|
GLOBAL: 1, //visible to all object files being combined
|
|
253
253
|
WEAK: 2, //like global symbols, but lower precedence
|
|
@@ -257,8 +257,8 @@ const ST_INFO = binary.BitFields({
|
|
|
257
257
|
HIPROC: 15,
|
|
258
258
|
})],
|
|
259
259
|
});
|
|
260
|
-
const ST_OTHER =
|
|
261
|
-
visibility: [2,
|
|
260
|
+
const ST_OTHER = bin.BitFields({
|
|
261
|
+
visibility: [2, bin.Enum({
|
|
262
262
|
DEFAULT: 0,
|
|
263
263
|
HIDDEN: 1,
|
|
264
264
|
PROTECTED: 2,
|
|
@@ -613,11 +613,11 @@ function Pair(top, bottom, be) {
|
|
|
613
613
|
}
|
|
614
614
|
function readDataAs(data, type) {
|
|
615
615
|
if (data)
|
|
616
|
-
return
|
|
616
|
+
return bin.RemainingArrayType(type).get(new bin.stream(data.data));
|
|
617
617
|
}
|
|
618
618
|
class ELFFile {
|
|
619
619
|
static check(data) {
|
|
620
|
-
return
|
|
620
|
+
return bin.utils.decodeText(data.subarray(0, 4), 'utf8') === '\x7fELF';
|
|
621
621
|
}
|
|
622
622
|
segments;
|
|
623
623
|
sections;
|
|
@@ -628,24 +628,24 @@ class ELFFile {
|
|
|
628
628
|
getSymbols;
|
|
629
629
|
getDynamicSymbols;
|
|
630
630
|
constructor(data) {
|
|
631
|
-
const s = new
|
|
632
|
-
const ident =
|
|
633
|
-
if (ident.magic !=
|
|
631
|
+
const s = new bin.stream(data);
|
|
632
|
+
const ident = bin.read(s, Ident);
|
|
633
|
+
if (ident.magic != bin.utils.stringCode("\x7fELF"))
|
|
634
634
|
throw new Error('Not an ELF file');
|
|
635
635
|
const be = ident.encoding == 'MSB';
|
|
636
636
|
const bits = ident.file_class == 'CLASS32' ? 32 : 64;
|
|
637
|
-
const Half =
|
|
638
|
-
const Sword =
|
|
639
|
-
const Word =
|
|
640
|
-
const Addr =
|
|
641
|
-
const Off =
|
|
642
|
-
const Xword =
|
|
643
|
-
const Sxword =
|
|
644
|
-
const PairHalf =
|
|
637
|
+
const Half = bin.UINT(16, be);
|
|
638
|
+
const Sword = bin.INT(32, be);
|
|
639
|
+
const Word = bin.UINT(32, be);
|
|
640
|
+
const Addr = bin.asHex(bin.UINT(bits, be));
|
|
641
|
+
const Off = bin.UINT(bits, be);
|
|
642
|
+
const Xword = bin.INT(bits, be);
|
|
643
|
+
const Sxword = bin.UINT(bits, be);
|
|
644
|
+
const PairHalf = bin.UINT(bits == 32 ? 8 : 32, be);
|
|
645
645
|
const Ehdr = {
|
|
646
|
-
e_type:
|
|
647
|
-
e_machine:
|
|
648
|
-
e_version:
|
|
646
|
+
e_type: bin.asEnum(Half, ET), //Object file type (ET_..)
|
|
647
|
+
e_machine: bin.asEnum(Half, EM), //specifies the required architecture (EM_...)
|
|
648
|
+
e_version: bin.asEnum(Word, EV), //object file version (EV_...)
|
|
649
649
|
e_entry: Addr, //run address
|
|
650
650
|
e_phoff: Off, //program header table's file offset
|
|
651
651
|
e_shoff: Off, //section header table's file offset
|
|
@@ -657,18 +657,18 @@ class ELFFile {
|
|
|
657
657
|
e_shnum: Half, //number of entries in the section header table
|
|
658
658
|
e_shstrndx: Half, //section header table index of section name string table
|
|
659
659
|
};
|
|
660
|
-
class Phdr extends
|
|
661
|
-
p_type:
|
|
660
|
+
class Phdr extends bin.ReadClass(bits == 32 ? {
|
|
661
|
+
p_type: bin.asEnum(Word, PT), //kind of segment this array element describes
|
|
662
662
|
p_offset: Off, //offset from the beginning of the file at which the first byte of the segment resides
|
|
663
663
|
p_vaddr: Addr, //virtual address at which the first byte of the segment resides in memory
|
|
664
664
|
p_paddr: Addr, //segment's physical address (when relevant)
|
|
665
665
|
p_filesz: Word, //number of bytes in the file image of the segment
|
|
666
666
|
p_memsz: Word, //number of bytes in the memory image of the segment
|
|
667
|
-
p_flags:
|
|
667
|
+
p_flags: bin.asFlags(Word, PF),
|
|
668
668
|
p_align: Word,
|
|
669
669
|
} : {
|
|
670
|
-
p_type:
|
|
671
|
-
p_flags:
|
|
670
|
+
p_type: bin.asEnum(Word, PT),
|
|
671
|
+
p_flags: bin.asFlags(Word, PF),
|
|
672
672
|
p_offset: Off,
|
|
673
673
|
p_vaddr: Addr,
|
|
674
674
|
p_paddr: Addr,
|
|
@@ -679,17 +679,17 @@ class ELFFile {
|
|
|
679
679
|
data;
|
|
680
680
|
constructor(s) {
|
|
681
681
|
super(s);
|
|
682
|
-
const flags =
|
|
683
|
-
| (this.p_flags.R ?
|
|
684
|
-
| (this.p_flags.W ?
|
|
685
|
-
| (this.p_flags.X ?
|
|
686
|
-
this.data = new
|
|
682
|
+
const flags = bin.MappedMemory.RELATIVE
|
|
683
|
+
| (this.p_flags.R ? bin.MappedMemory.READ : 0)
|
|
684
|
+
| (this.p_flags.W ? bin.MappedMemory.WRITE : 0)
|
|
685
|
+
| (this.p_flags.X ? bin.MappedMemory.EXECUTE : 0);
|
|
686
|
+
this.data = new bin.MappedMemory(s.buffer_at(Number(this.p_offset), Number(this.p_filesz)), BigInt(this.p_vaddr.value), flags);
|
|
687
687
|
}
|
|
688
688
|
}
|
|
689
|
-
class Shdr extends
|
|
689
|
+
class Shdr extends bin.ReadClass({
|
|
690
690
|
sh_name: Word, //name of the section
|
|
691
|
-
sh_type:
|
|
692
|
-
sh_flags:
|
|
691
|
+
sh_type: bin.asEnum(Word, SHT), //categorizes the section's contents and semantics
|
|
692
|
+
sh_flags: bin.asFlags(Xword, SHF), //miscellaneous attributes
|
|
693
693
|
sh_addr: Addr, //address
|
|
694
694
|
sh_offset: Off, //file offset to first byte in section
|
|
695
695
|
sh_size: Off, //section's size in bytes
|
|
@@ -701,14 +701,14 @@ class ELFFile {
|
|
|
701
701
|
data;
|
|
702
702
|
constructor(s) {
|
|
703
703
|
super(s);
|
|
704
|
-
const flags =
|
|
705
|
-
| (this.sh_flags.WRITE ?
|
|
706
|
-
| (this.sh_flags.EXECINSTR ?
|
|
704
|
+
const flags = bin.MappedMemory.RELATIVE | bin.MappedMemory.READ
|
|
705
|
+
| (this.sh_flags.WRITE ? bin.MappedMemory.WRITE : 0)
|
|
706
|
+
| (this.sh_flags.EXECINSTR ? bin.MappedMemory.EXECUTE : 0);
|
|
707
707
|
const buffer = this.sh_type === 'NOBITS' ? new Uint8Array(0) : s.buffer_at(Number(this.sh_offset), Number(this.sh_size));
|
|
708
|
-
this.data = new
|
|
708
|
+
this.data = new bin.MappedMemory(buffer, BigInt(this.sh_addr.value), flags);
|
|
709
709
|
}
|
|
710
710
|
}
|
|
711
|
-
const h =
|
|
711
|
+
const h = bin.read(s, Ehdr);
|
|
712
712
|
this.header = h;
|
|
713
713
|
s.seek(Number(h.e_phoff));
|
|
714
714
|
const ph = Array.from({ length: h.e_phnum }, () => new Phdr(s));
|
|
@@ -718,14 +718,14 @@ class ELFFile {
|
|
|
718
718
|
this.segments = ph.map(i => [`${i.p_type} @ 0x${i.p_offset.toString(16)}`, i]);
|
|
719
719
|
//set section names
|
|
720
720
|
const shnames = sh[h.e_shstrndx].data.data;
|
|
721
|
-
this.sections = sh.map(i => [
|
|
721
|
+
this.sections = sh.map(i => [bin.utils.decodeTextTo0(shnames.subarray(i.sh_name), 'utf8'), i]);
|
|
722
722
|
const Dyn = {
|
|
723
|
-
d_tag:
|
|
723
|
+
d_tag: bin.asEnum(Sword, DT_TAG),
|
|
724
724
|
d_val: Xword,
|
|
725
725
|
};
|
|
726
726
|
const Rel = {
|
|
727
727
|
r_offset: Addr,
|
|
728
|
-
r_info: Pair(
|
|
728
|
+
r_info: Pair(bin.asEnum(PairHalf, RELOC[h.e_machine]), PairHalf, be),
|
|
729
729
|
};
|
|
730
730
|
const Rela = {
|
|
731
731
|
...Rel,
|
|
@@ -735,18 +735,18 @@ class ELFFile {
|
|
|
735
735
|
this.getRel = () => readDataAs(sh.find(i => i.sh_type === 'REL')?.data, Rel);
|
|
736
736
|
this.getRelA = () => readDataAs(sh.find(i => i.sh_type === 'RELA')?.data, Rela);
|
|
737
737
|
const Sym = {
|
|
738
|
-
data:
|
|
738
|
+
data: bin.DontRead(),
|
|
739
739
|
st_name: Word, //index into the object file's symbol string table
|
|
740
740
|
...(bits === 32 ? {
|
|
741
741
|
st_value: Addr, //value of the associated symbol
|
|
742
742
|
st_size: Word, //associated size
|
|
743
|
-
st_info:
|
|
744
|
-
st_other:
|
|
745
|
-
st_shndx:
|
|
743
|
+
st_info: bin.as(bin.UINT8, ST_INFO), //symbol's type and binding attributes
|
|
744
|
+
st_other: bin.as(bin.UINT8, ST_OTHER),
|
|
745
|
+
st_shndx: bin.asEnum(Half, SHN), //section header table index
|
|
746
746
|
} : {
|
|
747
|
-
st_info:
|
|
748
|
-
st_other:
|
|
749
|
-
st_shndx:
|
|
747
|
+
st_info: bin.as(bin.UINT8, ST_INFO),
|
|
748
|
+
st_other: bin.as(bin.UINT8, ST_OTHER),
|
|
749
|
+
st_shndx: bin.asEnum(Half, SHN),
|
|
750
750
|
st_value: Addr,
|
|
751
751
|
st_size: Off,
|
|
752
752
|
})
|
|
@@ -760,10 +760,10 @@ class ELFFile {
|
|
|
760
760
|
if (+sym.st_shndx) {
|
|
761
761
|
const section = sh[+sym.st_shndx];
|
|
762
762
|
const offset = Number(sym.st_value.value) - Number(section.sh_addr.value);
|
|
763
|
-
const flags = sym.st_info.type === 'FUNC' ? section.data.flags : section.data.flags & ~
|
|
764
|
-
sym.data = new
|
|
763
|
+
const flags = sym.st_info.type === 'FUNC' ? section.data.flags : section.data.flags & ~bin.MappedMemory.EXECUTE;
|
|
764
|
+
sym.data = new bin.MappedMemory(section.data.data.subarray(offset, offset + Number(sym.st_size)), BigInt(sym.st_value.value), flags);
|
|
765
765
|
}
|
|
766
|
-
return [
|
|
766
|
+
return [bin.utils.decodeTextTo0(names.subarray(sym.st_name), 'utf8'), sym];
|
|
767
767
|
});
|
|
768
768
|
}
|
|
769
769
|
}
|