Skip to content

Commit 9e416c7

Browse files
committed
Add bindings for LIEF.ELF.Binary.extend() and make ELF section properties writable
1 parent 0be5d94 commit 9e416c7

11 files changed

Lines changed: 834 additions & 10 deletions

File tree

binding.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"src/abstract/segment.cpp",
1414
"src/elf/binary.cpp",
1515
"src/elf/segment.cpp",
16+
"src/elf/section.cpp",
1617
"src/pe/binary.cpp",
1718
"src/pe/section.cpp",
1819
"src/pe/optional_header.cpp",

lib/index.d.ts

Lines changed: 185 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ declare namespace LIEF {
133133
set content(value: Buffer | number[]);
134134

135135
/** Returns sections contained within this segment */
136-
sections(): Section[];
136+
sections(): ELF.Section[];
137137

138138
static readonly TYPE: {
139139
readonly PT_NULL_: 'PT_NULL_';
@@ -178,6 +178,183 @@ declare namespace LIEF {
178178
};
179179
}
180180

181+
type SectionType =
182+
| 'SHT_NULL_'
183+
| 'PROGBITS'
184+
| 'SYMTAB'
185+
| 'STRTAB'
186+
| 'RELA'
187+
| 'HASH'
188+
| 'DYNAMIC'
189+
| 'NOTE'
190+
| 'NOBITS'
191+
| 'REL'
192+
| 'SHLIB'
193+
| 'DYNSYM'
194+
| 'INIT_ARRAY'
195+
| 'FINI_ARRAY'
196+
| 'PREINIT_ARRAY'
197+
| 'GROUP'
198+
| 'SYMTAB_SHNDX'
199+
| 'RELR'
200+
| 'ANDROID_REL'
201+
| 'ANDROID_RELA'
202+
| 'LLVM_ADDRSIG'
203+
| 'ANDROID_RELR'
204+
| 'GNU_ATTRIBUTES'
205+
| 'GNU_HASH'
206+
| 'GNU_VERDEF'
207+
| 'GNU_VERNEED'
208+
| 'GNU_VERSYM'
209+
| 'ARM_EXIDX'
210+
| 'ARM_PREEMPTMAP'
211+
| 'ARM_ATTRIBUTES'
212+
| 'ARM_DEBUGOVERLAY'
213+
| 'ARM_OVERLAYSECTION'
214+
| 'HEX_ORDERED'
215+
| 'X86_64_UNWIND'
216+
| 'MIPS_LIBLIST'
217+
| 'MIPS_MSYM'
218+
| 'MIPS_CONFLICT'
219+
| 'MIPS_GPTAB'
220+
| 'MIPS_UCODE'
221+
| 'MIPS_DEBUG'
222+
| 'MIPS_REGINFO'
223+
| 'MIPS_PACKAGE'
224+
| 'MIPS_PACKSYM'
225+
| 'MIPS_RELD'
226+
| 'MIPS_IFACE'
227+
| 'MIPS_CONTENT'
228+
| 'MIPS_OPTIONS'
229+
| 'MIPS_SHDR'
230+
| 'MIPS_FDESC'
231+
| 'MIPS_EXTSYM'
232+
| 'MIPS_DENSE'
233+
| 'MIPS_PDESC'
234+
| 'MIPS_LOCSYM'
235+
| 'MIPS_AUXSYM'
236+
| 'MIPS_OPTSYM'
237+
| 'MIPS_LOCSTR'
238+
| 'MIPS_LINE'
239+
| 'MIPS_RFDESC'
240+
| 'MIPS_DELTASYM'
241+
| 'MIPS_DELTAINST'
242+
| 'MIPS_DELTACLASS'
243+
| 'MIPS_DWARF'
244+
| 'MIPS_DELTADECL'
245+
| 'MIPS_SYMBOL_LIB'
246+
| 'MIPS_EVENTS'
247+
| 'MIPS_TRANSLATE'
248+
| 'MIPS_PIXIE'
249+
| 'MIPS_XLATE'
250+
| 'MIPS_XLATE_DEBUG'
251+
| 'MIPS_WHIRL'
252+
| 'MIPS_EH_REGION'
253+
| 'MIPS_XLATE_OLD'
254+
| 'MIPS_ABIFLAGS'
255+
| 'MIPS_XHASH'
256+
| 'RISCV_ATTRIBUTES'
257+
| 'UNKNOWN';
258+
259+
/**
260+
* Represents an ELF section with ELF-specific read-write access.
261+
*/
262+
class Section {
263+
name: string;
264+
type: SectionType;
265+
flags: bigint;
266+
virtualAddress: bigint;
267+
fileOffset: bigint;
268+
size: bigint;
269+
alignment: bigint;
270+
/** Backward-compatible alias for fileOffset. */
271+
readonly offset: bigint;
272+
/**
273+
* Section content as a Buffer.
274+
* Getter returns Buffer. Setter accepts Buffer or number[].
275+
*/
276+
get content(): Buffer;
277+
set content(value: Buffer | number[]);
278+
279+
static readonly TYPE: {
280+
readonly SHT_NULL_: 'SHT_NULL_';
281+
readonly PROGBITS: 'PROGBITS';
282+
readonly SYMTAB: 'SYMTAB';
283+
readonly STRTAB: 'STRTAB';
284+
readonly RELA: 'RELA';
285+
readonly HASH: 'HASH';
286+
readonly DYNAMIC: 'DYNAMIC';
287+
readonly NOTE: 'NOTE';
288+
readonly NOBITS: 'NOBITS';
289+
readonly REL: 'REL';
290+
readonly SHLIB: 'SHLIB';
291+
readonly DYNSYM: 'DYNSYM';
292+
readonly INIT_ARRAY: 'INIT_ARRAY';
293+
readonly FINI_ARRAY: 'FINI_ARRAY';
294+
readonly PREINIT_ARRAY: 'PREINIT_ARRAY';
295+
readonly GROUP: 'GROUP';
296+
readonly SYMTAB_SHNDX: 'SYMTAB_SHNDX';
297+
readonly RELR: 'RELR';
298+
readonly ANDROID_REL: 'ANDROID_REL';
299+
readonly ANDROID_RELA: 'ANDROID_RELA';
300+
readonly LLVM_ADDRSIG: 'LLVM_ADDRSIG';
301+
readonly ANDROID_RELR: 'ANDROID_RELR';
302+
readonly GNU_ATTRIBUTES: 'GNU_ATTRIBUTES';
303+
readonly GNU_HASH: 'GNU_HASH';
304+
readonly GNU_VERDEF: 'GNU_VERDEF';
305+
readonly GNU_VERNEED: 'GNU_VERNEED';
306+
readonly GNU_VERSYM: 'GNU_VERSYM';
307+
readonly ARM_EXIDX: 'ARM_EXIDX';
308+
readonly ARM_PREEMPTMAP: 'ARM_PREEMPTMAP';
309+
readonly ARM_ATTRIBUTES: 'ARM_ATTRIBUTES';
310+
readonly ARM_DEBUGOVERLAY: 'ARM_DEBUGOVERLAY';
311+
readonly ARM_OVERLAYSECTION: 'ARM_OVERLAYSECTION';
312+
readonly HEX_ORDERED: 'HEX_ORDERED';
313+
readonly X86_64_UNWIND: 'X86_64_UNWIND';
314+
readonly MIPS_LIBLIST: 'MIPS_LIBLIST';
315+
readonly MIPS_MSYM: 'MIPS_MSYM';
316+
readonly MIPS_CONFLICT: 'MIPS_CONFLICT';
317+
readonly MIPS_GPTAB: 'MIPS_GPTAB';
318+
readonly MIPS_UCODE: 'MIPS_UCODE';
319+
readonly MIPS_DEBUG: 'MIPS_DEBUG';
320+
readonly MIPS_REGINFO: 'MIPS_REGINFO';
321+
readonly MIPS_PACKAGE: 'MIPS_PACKAGE';
322+
readonly MIPS_PACKSYM: 'MIPS_PACKSYM';
323+
readonly MIPS_RELD: 'MIPS_RELD';
324+
readonly MIPS_IFACE: 'MIPS_IFACE';
325+
readonly MIPS_CONTENT: 'MIPS_CONTENT';
326+
readonly MIPS_OPTIONS: 'MIPS_OPTIONS';
327+
readonly MIPS_SHDR: 'MIPS_SHDR';
328+
readonly MIPS_FDESC: 'MIPS_FDESC';
329+
readonly MIPS_EXTSYM: 'MIPS_EXTSYM';
330+
readonly MIPS_DENSE: 'MIPS_DENSE';
331+
readonly MIPS_PDESC: 'MIPS_PDESC';
332+
readonly MIPS_LOCSYM: 'MIPS_LOCSYM';
333+
readonly MIPS_AUXSYM: 'MIPS_AUXSYM';
334+
readonly MIPS_OPTSYM: 'MIPS_OPTSYM';
335+
readonly MIPS_LOCSTR: 'MIPS_LOCSTR';
336+
readonly MIPS_LINE: 'MIPS_LINE';
337+
readonly MIPS_RFDESC: 'MIPS_RFDESC';
338+
readonly MIPS_DELTASYM: 'MIPS_DELTASYM';
339+
readonly MIPS_DELTAINST: 'MIPS_DELTAINST';
340+
readonly MIPS_DELTACLASS: 'MIPS_DELTACLASS';
341+
readonly MIPS_DWARF: 'MIPS_DWARF';
342+
readonly MIPS_DELTADECL: 'MIPS_DELTADECL';
343+
readonly MIPS_SYMBOL_LIB: 'MIPS_SYMBOL_LIB';
344+
readonly MIPS_EVENTS: 'MIPS_EVENTS';
345+
readonly MIPS_TRANSLATE: 'MIPS_TRANSLATE';
346+
readonly MIPS_PIXIE: 'MIPS_PIXIE';
347+
readonly MIPS_XLATE: 'MIPS_XLATE';
348+
readonly MIPS_XLATE_DEBUG: 'MIPS_XLATE_DEBUG';
349+
readonly MIPS_WHIRL: 'MIPS_WHIRL';
350+
readonly MIPS_EH_REGION: 'MIPS_EH_REGION';
351+
readonly MIPS_XLATE_OLD: 'MIPS_XLATE_OLD';
352+
readonly MIPS_ABIFLAGS: 'MIPS_ABIFLAGS';
353+
readonly MIPS_XHASH: 'MIPS_XHASH';
354+
readonly RISCV_ATTRIBUTES: 'RISCV_ATTRIBUTES';
355+
};
356+
}
357+
181358
/**
182359
* ELF-specific binary class.
183360
* Used for Linux/Unix executable manipulation.
@@ -197,7 +374,7 @@ declare namespace LIEF {
197374
overlay: Buffer;
198375

199376
// Methods inherited from abstract Binary
200-
sections(): Section[];
377+
sections(): ELF.Section[];
201378
symbols(): Symbol[];
202379
relocations(): Relocation[];
203380
getSymbol(name: string): Symbol | null;
@@ -211,12 +388,17 @@ declare namespace LIEF {
211388
* @param name - Section name (e.g. ".text", ".data", ".rodata")
212389
* @returns The section, or null if not found.
213390
*/
214-
getSection(name: string): Section | null;
391+
getSection(name: string): ELF.Section | null;
215392
/**
216393
* Get a segment by its type name (e.g. LIEF.ELF.Segment.TYPE.LOAD).
217394
* Returns the first segment matching the given type, or null if not found.
218395
*/
219396
getSegment(type: SegmentType): Segment | null;
397+
extend(segment: Segment, size: bigint | number): Segment | null;
398+
lastOffsetSection(): bigint;
399+
lastOffsetSegment(): bigint;
400+
nextVirtualAddress(): bigint;
401+
pageSize(): bigint;
220402
}
221403
}
222404

lib/index.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,86 @@ binding.ELF.Segment.TYPE = Object.freeze({
7373
HP_CORE_UTSNAME: 'HP_CORE_UTSNAME',
7474
});
7575

76+
// Add ELF.Section.TYPE constants
77+
// These mirror LIEF::ELF::Section::TYPE names exposed by the native binding.
78+
binding.ELF.Section.TYPE = Object.freeze({
79+
SHT_NULL_: 'SHT_NULL_',
80+
PROGBITS: 'PROGBITS',
81+
SYMTAB: 'SYMTAB',
82+
STRTAB: 'STRTAB',
83+
RELA: 'RELA',
84+
HASH: 'HASH',
85+
DYNAMIC: 'DYNAMIC',
86+
NOTE: 'NOTE',
87+
NOBITS: 'NOBITS',
88+
REL: 'REL',
89+
SHLIB: 'SHLIB',
90+
DYNSYM: 'DYNSYM',
91+
INIT_ARRAY: 'INIT_ARRAY',
92+
FINI_ARRAY: 'FINI_ARRAY',
93+
PREINIT_ARRAY: 'PREINIT_ARRAY',
94+
GROUP: 'GROUP',
95+
SYMTAB_SHNDX: 'SYMTAB_SHNDX',
96+
RELR: 'RELR',
97+
ANDROID_REL: 'ANDROID_REL',
98+
ANDROID_RELA: 'ANDROID_RELA',
99+
LLVM_ADDRSIG: 'LLVM_ADDRSIG',
100+
ANDROID_RELR: 'ANDROID_RELR',
101+
GNU_ATTRIBUTES: 'GNU_ATTRIBUTES',
102+
GNU_HASH: 'GNU_HASH',
103+
GNU_VERDEF: 'GNU_VERDEF',
104+
GNU_VERNEED: 'GNU_VERNEED',
105+
GNU_VERSYM: 'GNU_VERSYM',
106+
ARM_EXIDX: 'ARM_EXIDX',
107+
ARM_PREEMPTMAP: 'ARM_PREEMPTMAP',
108+
ARM_ATTRIBUTES: 'ARM_ATTRIBUTES',
109+
ARM_DEBUGOVERLAY: 'ARM_DEBUGOVERLAY',
110+
ARM_OVERLAYSECTION: 'ARM_OVERLAYSECTION',
111+
HEX_ORDERED: 'HEX_ORDERED',
112+
X86_64_UNWIND: 'X86_64_UNWIND',
113+
MIPS_LIBLIST: 'MIPS_LIBLIST',
114+
MIPS_MSYM: 'MIPS_MSYM',
115+
MIPS_CONFLICT: 'MIPS_CONFLICT',
116+
MIPS_GPTAB: 'MIPS_GPTAB',
117+
MIPS_UCODE: 'MIPS_UCODE',
118+
MIPS_DEBUG: 'MIPS_DEBUG',
119+
MIPS_REGINFO: 'MIPS_REGINFO',
120+
MIPS_PACKAGE: 'MIPS_PACKAGE',
121+
MIPS_PACKSYM: 'MIPS_PACKSYM',
122+
MIPS_RELD: 'MIPS_RELD',
123+
MIPS_IFACE: 'MIPS_IFACE',
124+
MIPS_CONTENT: 'MIPS_CONTENT',
125+
MIPS_OPTIONS: 'MIPS_OPTIONS',
126+
MIPS_SHDR: 'MIPS_SHDR',
127+
MIPS_FDESC: 'MIPS_FDESC',
128+
MIPS_EXTSYM: 'MIPS_EXTSYM',
129+
MIPS_DENSE: 'MIPS_DENSE',
130+
MIPS_PDESC: 'MIPS_PDESC',
131+
MIPS_LOCSYM: 'MIPS_LOCSYM',
132+
MIPS_AUXSYM: 'MIPS_AUXSYM',
133+
MIPS_OPTSYM: 'MIPS_OPTSYM',
134+
MIPS_LOCSTR: 'MIPS_LOCSTR',
135+
MIPS_LINE: 'MIPS_LINE',
136+
MIPS_RFDESC: 'MIPS_RFDESC',
137+
MIPS_DELTASYM: 'MIPS_DELTASYM',
138+
MIPS_DELTAINST: 'MIPS_DELTAINST',
139+
MIPS_DELTACLASS: 'MIPS_DELTACLASS',
140+
MIPS_DWARF: 'MIPS_DWARF',
141+
MIPS_DELTADECL: 'MIPS_DELTADECL',
142+
MIPS_SYMBOL_LIB: 'MIPS_SYMBOL_LIB',
143+
MIPS_EVENTS: 'MIPS_EVENTS',
144+
MIPS_TRANSLATE: 'MIPS_TRANSLATE',
145+
MIPS_PIXIE: 'MIPS_PIXIE',
146+
MIPS_XLATE: 'MIPS_XLATE',
147+
MIPS_XLATE_DEBUG: 'MIPS_XLATE_DEBUG',
148+
MIPS_WHIRL: 'MIPS_WHIRL',
149+
MIPS_EH_REGION: 'MIPS_EH_REGION',
150+
MIPS_XLATE_OLD: 'MIPS_XLATE_OLD',
151+
MIPS_ABIFLAGS: 'MIPS_ABIFLAGS',
152+
MIPS_XHASH: 'MIPS_XHASH',
153+
RISCV_ATTRIBUTES: 'RISCV_ATTRIBUTES',
154+
});
155+
76156
// Add MachO.Header.CPU_TYPE constants
77157
// These mirror LIEF::MachO::Header::CPU_TYPE enum values
78158
binding.MachO.Header.CPU_TYPE = Object.freeze({

0 commit comments

Comments
 (0)