Skip to content

Commit 8f3d6e7

Browse files
committed
Add support for retrieving by section name to --string-dump
1 parent e3f1c63 commit 8f3d6e7

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

ELFSage/Commands/Read.lean

+20-10
Original file line numberDiff line numberDiff line change
@@ -85,20 +85,28 @@ def printStringsForSectionIdx (elffile : RawELFFile) (idx : Nat) :=
8585
| .some ⟨_, sec⟩ => for byte in sec.section_body do
8686
if byte == 0 then IO.print '\n' else IO.print (Char.ofNat byte.toNat)
8787

88-
def printHexForSectionIdx (elffile : RawELFFile) (idx : Nat) :=
89-
match elffile.getRawSectionHeaderTableEntries[idx]? with
90-
| .none => IO.println s!"There doesn't appear to be a section header {idx}"
91-
| .some ⟨_, sec⟩ => dumpBytesAsHex sec.section_body
92-
93-
def printHexForSectionName (elffile : RawELFFile) (name : String) :=
88+
def msecByName (elffile : RawELFFile) (name : String) : IO (Option (RawSectionHeaderTableEntry × InterpretedSection)) :=
9489
match elffile.getSectionHeaderStringTable? with
95-
| .error err => IO.println err
90+
| .error err => IO.println err *> return none
9691
| .ok (_,shstrtab_sec) =>
9792
let shstrtab : ELFStringTable := ⟨shstrtab_sec.section_body⟩
9893
let offset := shstrtab.stringToOffset name
9994
let findPred : RawSectionHeaderTableEntry × InterpretedSection → Bool := (λent => SectionHeaderTableEntry.sh_name ent.fst == offset)
100-
let msec := elffile.getRawSectionHeaderTableEntries.find? findPred
101-
match msec with
95+
return (elffile.getRawSectionHeaderTableEntries.find? findPred)
96+
97+
def printStringsForSectionName (elffile : RawELFFile) (name : String) := do
98+
match (← msecByName elffile name) with
99+
| .none => IO.println s!"There doesn't appear to be a section header named {name}"
100+
| .some ⟨_, sec⟩ => for byte in sec.section_body do
101+
if byte == 0 then IO.print '\n' else IO.print (Char.ofNat byte.toNat)
102+
103+
def printHexForSectionIdx (elffile : RawELFFile) (idx : Nat) :=
104+
match elffile.getRawSectionHeaderTableEntries[idx]? with
105+
| .none => IO.println s!"There doesn't appear to be a section header {idx}"
106+
| .some ⟨_, sec⟩ => dumpBytesAsHex sec.section_body
107+
108+
def printHexForSectionName (elffile : RawELFFile) (name : String) := do
109+
match (← msecByName elffile name) with
102110
| .none => IO.println s!"There doesn't appear to be a section header named {name}"
103111
| .some ⟨_, sec⟩ => dumpBytesAsHex sec.section_body
104112

@@ -250,8 +258,10 @@ def runReadCmd (p : Cli.Parsed): IO UInt32 := do
250258
printSymbolsForSectionType elffile symtab
251259
--TODO fallback to DYNSYM when SYMTAB isn't present
252260
| "string-dump" => match flag.as? Nat with
253-
| none => IO.println "couldn't parse section number provided for string dump"
254261
| some idx => printStringsForSectionIdx elffile idx
262+
| none => match flag.as? String with
263+
| some name => printStringsForSectionName elffile name
264+
| none => IO.println "couldn't parse section number provided for string dump"
255265
| "hex-dump" => match flag.as? Nat with
256266
| some idx => printHexForSectionIdx elffile idx
257267
| none => match flag.as? String with

0 commit comments

Comments
 (0)