Commit 7188e002 authored by Alex Brainman's avatar Alex Brainman

cmd/link: introduce and use peFile.nextSectOffset and nextFileOffset

Change-Id: Iecff99e85e2cca1127dca79747bb0d5362cd4125
Reviewed-on: https://go-review.googlesource.com/56319Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
parent 44211c14
...@@ -322,10 +322,6 @@ var PEFILEHEADR int32 ...@@ -322,10 +322,6 @@ var PEFILEHEADR int32
var pe64 int var pe64 int
var nextsectoff int
var nextfileoff int
var fh IMAGE_FILE_HEADER var fh IMAGE_FILE_HEADER
var oh IMAGE_OPTIONAL_HEADER var oh IMAGE_OPTIONAL_HEADER
...@@ -446,11 +442,13 @@ func (sect *peSection) write() error { ...@@ -446,11 +442,13 @@ func (sect *peSection) write() error {
// peFile is used to build COFF file. // peFile is used to build COFF file.
type peFile struct { type peFile struct {
sections []*peSection sections []*peSection
stringTable peStringTable stringTable peStringTable
textSect *peSection textSect *peSection
dataSect *peSection dataSect *peSection
bssSect *peSection bssSect *peSection
nextSectOffset uint32
nextFileOffset uint32
} }
// addSection adds section to the COFF file f. // addSection adds section to the COFF file f.
...@@ -460,13 +458,13 @@ func (f *peFile) addSection(name string, sectsize int, filesize int) *peSection ...@@ -460,13 +458,13 @@ func (f *peFile) addSection(name string, sectsize int, filesize int) *peSection
shortName: name, shortName: name,
index: len(f.sections) + 1, index: len(f.sections) + 1,
VirtualSize: uint32(sectsize), VirtualSize: uint32(sectsize),
VirtualAddress: uint32(nextsectoff), VirtualAddress: f.nextSectOffset,
PointerToRawData: uint32(nextfileoff), PointerToRawData: f.nextFileOffset,
} }
nextsectoff = int(Rnd(int64(nextsectoff)+int64(sectsize), PESECTALIGN)) f.nextSectOffset = uint32(Rnd(int64(f.nextSectOffset)+int64(sectsize), PESECTALIGN))
if filesize > 0 { if filesize > 0 {
sect.SizeOfRawData = uint32(Rnd(int64(filesize), PEFILEALIGN)) sect.SizeOfRawData = uint32(Rnd(int64(filesize), PEFILEALIGN))
nextfileoff += int(sect.SizeOfRawData) f.nextFileOffset += sect.SizeOfRawData
} }
f.sections = append(f.sections, sect) f.sections = append(f.sections, sect)
return sect return sect
...@@ -524,8 +522,8 @@ func Peinit(ctxt *Link) { ...@@ -524,8 +522,8 @@ func Peinit(ctxt *Link) {
} else { } else {
PESECTHEADR = 0 PESECTHEADR = 0
} }
nextsectoff = int(PESECTHEADR) pefile.nextSectOffset = uint32(PESECTHEADR)
nextfileoff = int(PEFILEHEADR) pefile.nextFileOffset = uint32(PEFILEHEADR)
if Linkmode == LinkInternal { if Linkmode == LinkInternal {
// some mingw libs depend on this symbol, for example, FindPESectionByName // some mingw libs depend on this symbol, for example, FindPESectionByName
...@@ -695,7 +693,7 @@ func addimports(ctxt *Link, datsect *peSection) { ...@@ -695,7 +693,7 @@ func addimports(ctxt *Link, datsect *peSection) {
var m *Imp var m *Imp
for d := dr; d != nil; d = d.next { for d := dr; d != nil; d = d.next {
for m = d.ms; m != nil; m = m.next { for m = d.ms; m != nil; m = m.next {
m.off = uint64(nextsectoff) + uint64(coutbuf.Offset()) - uint64(startoff) m.off = uint64(pefile.nextSectOffset) + uint64(coutbuf.Offset()) - uint64(startoff)
Wputl(0) // hint Wputl(0) // hint
strput(m.s.Extname) strput(m.s.Extname)
} }
...@@ -1255,7 +1253,7 @@ func Asmbpe(ctxt *Link) { ...@@ -1255,7 +1253,7 @@ func Asmbpe(ctxt *Link) {
c = addinitarray(ctxt) c = addinitarray(ctxt)
} }
Cseek(int64(nextfileoff)) Cseek(int64(pefile.nextFileOffset))
if Linkmode != LinkExternal { if Linkmode != LinkExternal {
addimports(ctxt, d) addimports(ctxt, d)
addexports(ctxt) addexports(ctxt)
...@@ -1324,8 +1322,8 @@ func Asmbpe(ctxt *Link) { ...@@ -1324,8 +1322,8 @@ func Asmbpe(ctxt *Link) {
oh.MajorSubsystemVersion = 4 oh.MajorSubsystemVersion = 4
oh64.MinorSubsystemVersion = 0 oh64.MinorSubsystemVersion = 0
oh.MinorSubsystemVersion = 0 oh.MinorSubsystemVersion = 0
oh64.SizeOfImage = uint32(nextsectoff) oh64.SizeOfImage = pefile.nextSectOffset
oh.SizeOfImage = uint32(nextsectoff) oh.SizeOfImage = pefile.nextSectOffset
oh64.SizeOfHeaders = uint32(PEFILEHEADR) oh64.SizeOfHeaders = uint32(PEFILEHEADR)
oh.SizeOfHeaders = uint32(PEFILEHEADR) oh.SizeOfHeaders = uint32(PEFILEHEADR)
if windowsgui { if windowsgui {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment