Skip to content

Commit 74a9d28

Browse files
florinpapagopherbot
authored andcommitted
debug/elf: retrieve values for dynamic section tags
Add functionality to retrieve values for .dynamic entries that don't correspond to entries in the string table. Fixes #56892 Change-Id: I6edabc8ca331c819e442d06e19b7f4df8343372b Reviewed-on: https://go-review.googlesource.com/c/go/+/452617 Auto-Submit: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent a42bb79 commit 74a9d28

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

api/next/56892.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pkg debug/elf, method (*File) DynValue(DynTag) ([]uint64, error) #56892

src/debug/elf/file.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,6 +1642,40 @@ func (f *File) DynString(tag DynTag) ([]string, error) {
16421642
return all, nil
16431643
}
16441644

1645+
// DynValue returns the values listed for the given tag in the file's dynamic
1646+
// section.
1647+
func (f *File) DynValue(tag DynTag) ([]uint64, error) {
1648+
ds := f.SectionByType(SHT_DYNAMIC)
1649+
if ds == nil {
1650+
return nil, nil
1651+
}
1652+
d, err := ds.Data()
1653+
if err != nil {
1654+
return nil, err
1655+
}
1656+
1657+
// Parse the .dynamic section as a string of bytes.
1658+
var vals []uint64
1659+
for len(d) > 0 {
1660+
var t DynTag
1661+
var v uint64
1662+
switch f.Class {
1663+
case ELFCLASS32:
1664+
t = DynTag(f.ByteOrder.Uint32(d[0:4]))
1665+
v = uint64(f.ByteOrder.Uint32(d[4:8]))
1666+
d = d[8:]
1667+
case ELFCLASS64:
1668+
t = DynTag(f.ByteOrder.Uint64(d[0:8]))
1669+
v = f.ByteOrder.Uint64(d[8:16])
1670+
d = d[16:]
1671+
}
1672+
if t == tag {
1673+
vals = append(vals, v)
1674+
}
1675+
}
1676+
return vals, nil
1677+
}
1678+
16451679
type nobitsSectionReader struct{}
16461680

16471681
func (*nobitsSectionReader) ReadAt(p []byte, off int64) (n int, err error) {

src/debug/elf/file_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,3 +1224,21 @@ func TestIssue10996(t *testing.T) {
12241224
t.Fatalf("opening invalid ELF file unexpectedly succeeded")
12251225
}
12261226
}
1227+
1228+
func TestDynValue(t *testing.T) {
1229+
const testdata = "testdata/gcc-amd64-linux-exec"
1230+
f, err := Open(testdata)
1231+
if err != nil {
1232+
t.Fatalf("could not read %s: %v", testdata, err)
1233+
}
1234+
defer f.Close()
1235+
1236+
vals, err := f.DynValue(DT_VERNEEDNUM)
1237+
if err != nil {
1238+
t.Fatalf("DynValue(DT_VERNEEDNUM): got unexpected error %v", err)
1239+
}
1240+
1241+
if len(vals) != 1 || vals[0] != 1 {
1242+
t.Errorf("DynValue(DT_VERNEEDNUM): got %v, want [1]", vals)
1243+
}
1244+
}

0 commit comments

Comments
 (0)