@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/dist/elf.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import * as binary from '@isopodlabs/binary';
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: binary.MappedMemory;
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: binary.hex<number | bigint>;
50
- p_paddr: binary.hex<number | bigint>;
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: binary.MappedMemory;
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: binary.hex<number | bigint>;
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: binary.hex<number | bigint>;
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: binary.hex<number | bigint>;
89
- r_info: never;
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: binary.hex<number | bigint>;
94
- r_info: never;
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: binary.hex<number | bigint>;
112
+ st_value: bin.hex<number | bigint>;
107
113
  st_size: number | bigint;
108
- data: binary.MappedMemory | undefined;
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: binary.hex<number | bigint>;
127
+ st_value: bin.hex<number | bigint>;
122
128
  st_size: number | bigint;
123
- data: binary.MappedMemory | undefined;
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: binary.MappedMemory;
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: binary.hex<number | bigint>;
133
- p_paddr: binary.hex<number | bigint>;
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: binary.MappedMemory;
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: binary.hex<number | bigint>;
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 binary = __importStar(require("@isopodlabs/binary"));
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: binary.UINT32_LE,
47
- file_class: binary.asEnum(binary.UINT8, CLASS),
48
- encoding: binary.asEnum(binary.UINT8, DATA),
49
- version: binary.UINT8,
50
- _: binary.If(s => s.obj.file_class === 'CLASS64', {
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: binary.asEnum(binary.UINT8, OSABI),
53
- abiversion: binary.UINT8,
54
- pad: binary.ArrayType(7, binary.UINT8),
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 = binary.BitFields({
239
- type: [4, binary.Enum({
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, binary.Enum({
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 = binary.BitFields({
261
- visibility: [2, binary.Enum({
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 binary.RemainingArrayType(type).get(new binary.stream(data.data));
616
+ return bin.RemainingArrayType(type).get(new bin.stream(data.data));
617
617
  }
618
618
  class ELFFile {
619
619
  static check(data) {
620
- return binary.utils.decodeText(data.subarray(0, 4), 'utf8') === '\x7fELF';
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 binary.stream(data);
632
- const ident = binary.read(s, Ident);
633
- if (ident.magic != binary.utils.stringCode("\x7fELF"))
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 = binary.UINT(16, be);
638
- const Sword = binary.INT(32, be);
639
- const Word = binary.UINT(32, be);
640
- const Addr = binary.asHex(binary.UINT(bits, be));
641
- const Off = binary.UINT(bits, be);
642
- const Xword = binary.INT(bits, be);
643
- const Sxword = binary.UINT(bits, be);
644
- const PairHalf = binary.UINT(bits == 32 ? 8 : 32, be);
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: binary.asEnum(Half, ET), //Object file type (ET_..)
647
- e_machine: binary.asEnum(Half, EM), //specifies the required architecture (EM_...)
648
- e_version: binary.asEnum(Word, EV), //object file version (EV_...)
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 binary.ReadClass(bits == 32 ? {
661
- p_type: binary.asEnum(Word, PT), //kind of segment this array element describes
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: binary.asFlags(Word, PF),
667
+ p_flags: bin.asFlags(Word, PF),
668
668
  p_align: Word,
669
669
  } : {
670
- p_type: binary.asEnum(Word, PT),
671
- p_flags: binary.asFlags(Word, PF),
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 = binary.MappedMemory.RELATIVE
683
- | (this.p_flags.R ? binary.MappedMemory.READ : 0)
684
- | (this.p_flags.W ? binary.MappedMemory.WRITE : 0)
685
- | (this.p_flags.X ? binary.MappedMemory.EXECUTE : 0);
686
- this.data = new binary.MappedMemory(s.buffer_at(Number(this.p_offset), Number(this.p_filesz)), Number(this.p_vaddr), flags);
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 binary.ReadClass({
689
+ class Shdr extends bin.ReadClass({
690
690
  sh_name: Word, //name of the section
691
- sh_type: binary.asEnum(Word, SHT), //categorizes the section's contents and semantics
692
- sh_flags: binary.asFlags(Xword, SHF), //miscellaneous attributes
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 = binary.MappedMemory.RELATIVE | binary.MappedMemory.READ
705
- | (this.sh_flags.WRITE ? binary.MappedMemory.WRITE : 0)
706
- | (this.sh_flags.EXECINSTR ? binary.MappedMemory.EXECUTE : 0);
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 binary.MappedMemory(buffer, Number(this.sh_addr), flags);
708
+ this.data = new bin.MappedMemory(buffer, BigInt(this.sh_addr.value), flags);
709
709
  }
710
710
  }
711
- const h = binary.read(s, Ehdr);
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 => [binary.utils.decodeTextTo0(shnames.subarray(i.sh_name), 'utf8'), i]);
721
+ this.sections = sh.map(i => [bin.utils.decodeTextTo0(shnames.subarray(i.sh_name), 'utf8'), i]);
722
722
  const Dyn = {
723
- d_tag: binary.asEnum(Sword, DT_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(binary.asEnum(PairHalf, RELOC[h.e_machine]), PairHalf, be),
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: binary.DontRead(),
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: binary.as(binary.UINT8, ST_INFO), //symbol's type and binding attributes
744
- st_other: binary.as(binary.UINT8, ST_OTHER),
745
- st_shndx: binary.asEnum(Half, SHN), //section header table index
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: binary.as(binary.UINT8, ST_INFO),
748
- st_other: binary.as(binary.UINT8, ST_OTHER),
749
- st_shndx: binary.asEnum(Half, SHN),
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 & ~binary.MappedMemory.EXECUTE;
764
- sym.data = new binary.MappedMemory(section.data.data.subarray(offset, offset + Number(sym.st_size)), Number(sym.st_value.value), flags);
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 [binary.utils.decodeTextTo0(names.subarray(sym.st_name), 'utf8'), sym];
766
+ return [bin.utils.decodeTextTo0(names.subarray(sym.st_name), 'utf8'), sym];
767
767
  });
768
768
  }
769
769
  }