]> Cypherpunks repositories - gostls13.git/commitdiff
debug/pe: introduce Section.Relocs
authorAlex Brainman <alex.brainman@gmail.com>
Thu, 21 Apr 2016 01:44:05 +0000 (11:44 +1000)
committerAlex Brainman <alex.brainman@gmail.com>
Thu, 21 Apr 2016 06:35:48 +0000 (06:35 +0000)
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>
src/debug/pe/file.go
src/debug/pe/section.go

index 73b7c1cba2405d51a4e969b7114bc522853ffbc5..cfd8e08a6384a276cf0de95f0a5f95f1905c5bf1 100644 (file)
@@ -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
 }
 
index ded3ec439376c5649e81986bb3da464960b06e74..69fe41fd7a4d85d01f0ee92194e4df66f5913c58 100644 (file)
@@ -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