1
1
mirror of https://github.com/wader/fq.git synced 2024-11-28 03:02:55 +03:00

elf: Use correct offset to dynamic linking string table

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.
This commit is contained in:
Mattias Wadman 2022-06-30 23:30:56 +02:00
parent d4fe00d65c
commit f66a359c39

View File

@ -432,6 +432,8 @@ func elfDecodeGNUHash(d *decode.D, ec elfContext, size int64, strTab string) {
type dynamicContext struct {
entries int
strTabPtr int64
strSzVal int64
strTab string
symEnt int64
}
@ -462,7 +464,8 @@ func elfReadDynamicTags(d *decode.D, ec *elfContext) dynamicContext {
return dynamicContext{
entries: entries,
strTab: string(d.BytesRange(strTabPtr, int(strSzVal/8))),
strTabPtr: strTabPtr,
strSzVal: strSzVal,
symEnt: symEnt,
}
}
@ -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) {