]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/link: add peSection
authorAlex Brainman <alex.brainman@gmail.com>
Fri, 2 Jun 2017 06:53:22 +0000 (16:53 +1000)
committerAlex Brainman <alex.brainman@gmail.com>
Wed, 16 Aug 2017 05:10:22 +0000 (05:10 +0000)
Change-Id: Id3aeeaeaacf5f079fb2ddad579f2f209b7fc0e06
Reviewed-on: https://go-review.googlesource.com/55258
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/cmd/link/internal/ld/pe.go

index 6c6758644fe02d6a90f5c9ef55ae5db29e3fcc10..be0fd7af43b1a682a9139c726a66107db33e27bd 100644 (file)
@@ -7,6 +7,7 @@ package ld
 import (
        "cmd/internal/objabi"
        "cmd/internal/sys"
+       "debug/pe"
        "encoding/binary"
        "fmt"
        "os"
@@ -409,11 +410,64 @@ func (t *peStringTable) write() {
        }
 }
 
+// peSection represents section from COFF section table.
+type peSection struct {
+       name      string
+       shortName string
+       index     int // one-based index into the Section Table
+       // TODO: change all these names to start with small letters
+       VirtualSize          uint32
+       VirtualAddress       uint32
+       SizeOfRawData        uint32
+       PointerToRawData     uint32
+       PointerToRelocations uint32
+       NumberOfRelocations  uint16
+       Characteristics      uint32
+}
+
+// write writes COFF section sect into the output file.
+func (sect *peSection) write() error {
+       h := pe.SectionHeader32{
+               VirtualSize:          sect.VirtualSize,
+               SizeOfRawData:        sect.SizeOfRawData,
+               PointerToRawData:     sect.PointerToRawData,
+               PointerToRelocations: sect.PointerToRelocations,
+               NumberOfRelocations:  sect.NumberOfRelocations,
+               Characteristics:      sect.Characteristics,
+       }
+       if Linkmode != LinkExternal {
+               h.VirtualAddress = sect.VirtualAddress
+       }
+       copy(h.Name[:], sect.shortName)
+       return binary.Write(&coutbuf, binary.LittleEndian, h)
+}
+
 // peFile is used to build COFF file.
 type peFile struct {
+       sections    []*peSection
        stringTable peStringTable
 }
 
+// addSection adds section to the COFF file f.
+func (f *peFile) addSection(name string, sectsize int, filesize int) *peSection {
+       sect := &peSection{
+               name:             name,
+               shortName:        name,
+               index:            len(f.sections) + 1,
+               VirtualSize:      uint32(sectsize),
+               VirtualAddress:   uint32(nextsectoff),
+               PointerToRawData: uint32(nextfileoff),
+       }
+       nextsectoff = int(Rnd(int64(nextsectoff)+int64(sectsize), PESECTALIGN))
+       if filesize > 0 {
+               sect.SizeOfRawData = uint32(Rnd(int64(filesize), PEFILEALIGN))
+               nextfileoff += int(sect.SizeOfRawData)
+       }
+       f.sections = append(f.sections, sect)
+       pensect++
+       return sect
+}
+
 var pefile peFile
 
 func addpesectionWithLongName(ctxt *Link, shortname, longname string, sectsize int, filesize int) *IMAGE_SECTION_HEADER {