Skip to content

Commit

Permalink
elf: Use correct offset to dynamic linking string table
Browse files Browse the repository at this point in the history
String table used for "needed" tags (the string seen with ldd etc) was using address as
offset which seem to work as it's usually the same as correct offset. But this is not true
for some ELFs. Then we have to look up offset by finding the section by address and then use
its offset for the string table. Hope this is the correct way.

Not sure how to produce a small test for this.
  • Loading branch information
wader committed Jun 30, 2022
1 parent d4fe00d commit f66a359
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions format/elf/elf.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,9 +431,11 @@ func elfDecodeGNUHash(d *decode.D, ec elfContext, size int64, strTab string) {
}

type dynamicContext struct {
entries int
strTab string
symEnt int64
entries int
strTabPtr int64
strSzVal int64
strTab string
symEnt int64
}

func elfReadDynamicTags(d *decode.D, ec *elfContext) dynamicContext {
Expand Down Expand Up @@ -461,9 +463,10 @@ func elfReadDynamicTags(d *decode.D, ec *elfContext) dynamicContext {
}

return dynamicContext{
entries: entries,
strTab: string(d.BytesRange(strTabPtr, int(strSzVal/8))),
symEnt: symEnt,
entries: entries,
strTabPtr: strTabPtr,
strSzVal: strSzVal,
symEnt: symEnt,
}
}

Expand Down Expand Up @@ -559,6 +562,19 @@ func elfReadSectionHeaders(d *decode.D, ec *elfContext) {
ec.sections = append(ec.sections, sh)
}

// for dynamic linking sections find offset to string table by looking up
// section by address using string stable address
for i := range ec.sections {
sh := &ec.sections[i]
if sh.typ != SHT_DYNAMIC {
continue
}
if i, ok := ec.sectionIndexByAddr(sh.dc.strTabPtr); ok {
strTabSh := ec.sections[i]
sh.dc.strTab = string(d.BytesRange(strTabSh.offset, int(sh.dc.strSzVal/8)))
}
}

ec.strTabMap = map[string]string{}
var shStrTab string
if ec.shStrNdx >= len(ec.sections) {
Expand Down

0 comments on commit f66a359

Please sign in to comment.