-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathelfdump.nim
77 lines (70 loc) · 2.73 KB
/
elfdump.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
let doc = """
ELF Dump
Usage:
elfdump <filename>
elfdump (-h | --help)
elfdump (-v | --version)
Options:
-h --help Show this screen.
-v --version Show version.
"""
import docopt
import strformat
import elfconsts
import elfparse
let args = docopt(doc, version = "ELF Dump 0.1")
let elf_parsed =
($args["<filename>"])
.read_file()
.to_elf()
echo "ELF Header"
echo fmt" Class: {ElfClass(elf_parsed.eh.ident.class)}"
echo fmt" Endianness: {ElfData(elf_parsed.eh.ident.data)}"
echo fmt" Version: {elf_parsed.eh.ident.version}"
echo fmt" OS/ABI: {ElfOsAbi(elf_parsed.eh.ident.os_abi)}"
echo fmt" ABI Version: {elf_parsed.eh.ident.abi_version}"
echo fmt" Type: {ElfFileType(elf_parsed.eh.typ)}"
echo fmt" Machine: {ElfMachine(elf_parsed.eh.machine)}"
echo fmt" Entry Point: 0x{elf_parsed.eh.entry_point:X}"
echo fmt" Flags: 0x{elf_parsed.eh.flags:X}"
echo fmt" Header Size: {elf_parsed.eh.eh_size} (bytes)"
echo ""
echo fmt" Program Headers"
echo fmt" Offset: {elf_parsed.eh.ph_offset} (bytes)"
echo fmt" Count: {elf_parsed.eh.ph_count}"
echo fmt" Entry Size: {elf_parsed.eh.ph_entry_size} (bytes)"
echo ""
echo fmt" Section Headers"
echo fmt" Offset: {elf_parsed.eh.sh_offset} (bytes)"
echo fmt" Count: {elf_parsed.eh.sh_count}"
echo fmt" Entry Size: {elf_parsed.eh.sh_entry_size} (bytes)"
echo fmt" String Table Index: {elf_parsed.eh.sh_strtab_idx}"
echo ""
echo "Program Segment Headers"
echo fmt" Type Offset Virt Addr Phys Addr File Size Mem Size Align Flags"
for i, ph in elf_parsed.ph_table:
echo fmt" {i:>2} | ",
fmt"{SegmentType(ph.typ):<13} | ",
fmt"{ph.offset:>8} | ",
fmt"{ph.v_addr:>16X} | ",
fmt"{ph.p_addr:>16X} | ",
fmt"{ph.file_size:>8} | ",
fmt"{ph.memory_size:>8} | ",
fmt"{ph.align:>8} | ",
fmt"{cast[SegmentFlags](ph.flags)}"
echo ""
echo "Section Headers"
echo fmt" Name Type Offset Virt Addr Size Align Entry Link Info Flags"
echo fmt" Size"
for i, sh in elf_parsed.sh_table:
echo fmt" {i:>2} | ",
fmt"{$cast[cstring](addr elf_parsed.str_table[sh.name_offset]):<18} | ",
fmt"{SectionType(sh.typ):<13} | ",
fmt"{sh.offset:>8} | ",
fmt"{sh.v_addr:>16X} | ",
fmt"{sh.size:>8} | ",
fmt"{sh.align:>8} | ",
fmt"{sh.entry_size:>4} | ",
fmt"{sh.link:>4} | ",
fmt"{sh.info:>4} | ",
fmt"{cast[SectionFlags](sh.flags)}"