diff options
| author | Alex Brainman <alex.brainman@gmail.com> | 2016-04-21 11:44:05 +1000 |
|---|---|---|
| committer | Alex Brainman <alex.brainman@gmail.com> | 2016-04-21 06:35:48 +0000 |
| commit | 45522a6a93efe0fd487f6875f2b104d772a26469 (patch) | |
| tree | 10ae32fc7b6bb44235fdb64910ea832132c619b2 /src/debug/pe | |
| parent | e48434887e568fa96800a0dff36ab45bc844ea04 (diff) | |
| download | go-45522a6a93efe0fd487f6875f2b104d772a26469.tar.xz | |
debug/pe: introduce Section.Relocs
cmd/link reads PE object files when building programs with cgo.
cmd/link accesses object relocations. Add new Section.Relocs that
provides similar functionality in debug/pe.
Updates #15345
Change-Id: I34de91b7f18cf1c9e4cdb3aedd685486a625ac92
Reviewed-on: https://go-review.googlesource.com/22332
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Diffstat (limited to 'src/debug/pe')
| -rw-r--r-- | src/debug/pe/file.go | 8 | ||||
| -rw-r--r-- | src/debug/pe/section.go | 27 |
2 files changed, 35 insertions, 0 deletions
diff --git a/src/debug/pe/file.go b/src/debug/pe/file.go index 73b7c1cba2..cfd8e08a63 100644 --- a/src/debug/pe/file.go +++ b/src/debug/pe/file.go @@ -192,6 +192,14 @@ func NewFile(r io.ReaderAt) (*File, error) { s.ReaderAt = s.sr f.Sections[i] = s } + for i := range f.Sections { + var err error + f.Sections[i].Relocs, err = readRelocs(&f.Sections[i].SectionHeader, sr) + if err != nil { + return nil, err + } + } + return f, nil } diff --git a/src/debug/pe/section.go b/src/debug/pe/section.go index ded3ec4393..69fe41fd7a 100644 --- a/src/debug/pe/section.go +++ b/src/debug/pe/section.go @@ -5,6 +5,8 @@ package pe import ( + "encoding/binary" + "fmt" "io" "strconv" ) @@ -37,6 +39,30 @@ func (sh *SectionHeader32) fullName(st StringTable) (string, error) { return st.String(uint32(i)) } +// Reloc represents a PE COFF relocation. +// Each section contains its own relocation list. +type Reloc struct { + VirtualAddress uint32 + SymbolTableIndex uint32 + Type uint16 +} + +func readRelocs(sh *SectionHeader, r io.ReadSeeker) ([]Reloc, error) { + if sh.NumberOfRelocations <= 0 { + return nil, nil + } + _, err := r.Seek(int64(sh.PointerToRelocations), io.SeekStart) + if err != nil { + return nil, fmt.Errorf("fail to seek to %q section relocations: %v", sh.Name, err) + } + relocs := make([]Reloc, sh.NumberOfRelocations) + err = binary.Read(r, binary.LittleEndian, relocs) + if err != nil { + return nil, fmt.Errorf("fail to read section relocations: %v", err) + } + return relocs, nil +} + // SectionHeader is similar to SectionHeader32 with Name // field replaced by Go string. type SectionHeader struct { @@ -55,6 +81,7 @@ type SectionHeader struct { // Section provides access to PE COFF section. type Section struct { SectionHeader + Relocs []Reloc // Embed ReaderAt for ReadAt method. // Do not embed SectionReader directly |
