File tree Expand file tree Collapse file tree 3 files changed +53
-0
lines changed Expand file tree Collapse file tree 3 files changed +53
-0
lines changed Original file line number Diff line number Diff line change
1
+ pkg debug/elf, method (*File) DynValue(DynTag) ([]uint64, error) #56892
Original file line number Diff line number Diff line change @@ -1642,6 +1642,40 @@ func (f *File) DynString(tag DynTag) ([]string, error) {
1642
1642
return all , nil
1643
1643
}
1644
1644
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
+
1645
1679
type nobitsSectionReader struct {}
1646
1680
1647
1681
func (* nobitsSectionReader ) ReadAt (p []byte , off int64 ) (n int , err error ) {
Original file line number Diff line number Diff line change @@ -1224,3 +1224,21 @@ func TestIssue10996(t *testing.T) {
1224
1224
t .Fatalf ("opening invalid ELF file unexpectedly succeeded" )
1225
1225
}
1226
1226
}
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
+ }
You can’t perform that action at this time.
0 commit comments