Commit e31144f1 authored by Alex Brainman's avatar Alex Brainman

cmd/link: set pe section and file alignment to 0 during external linking

This is what gcc does when it generates object files.
And it is easier to count everything, when it starts from 0.
Make go linker do the same.

gcc also does not output IMAGE_OPTIONAL_HEADER or
PE64_IMAGE_OPTIONAL_HEADER for object files.
Perhaps we should do the same, but not in this CL.

For #10776.

Change-Id: I9789c337648623b6cfaa7d18d1ac9cef32e180dc
Reviewed-on: https://go-review.googlesource.com/36974Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
parent 64c02460
...@@ -152,7 +152,7 @@ func archinit(ctxt *ld.Link) { ...@@ -152,7 +152,7 @@ func archinit(ctxt *ld.Link) {
*ld.FlagDataAddr = 0 *ld.FlagDataAddr = 0
} }
if *ld.FlagRound == -1 { if *ld.FlagRound == -1 {
*ld.FlagRound = ld.PESECTALIGN *ld.FlagRound = int(ld.PESECTALIGN)
} }
} }
......
...@@ -101,15 +101,17 @@ type IMAGE_EXPORT_DIRECTORY struct { ...@@ -101,15 +101,17 @@ type IMAGE_EXPORT_DIRECTORY struct {
const ( const (
PEBASE = 0x00400000 PEBASE = 0x00400000
)
var (
// SectionAlignment must be greater than or equal to FileAlignment. // SectionAlignment must be greater than or equal to FileAlignment.
// The default is the page size for the architecture. // The default is the page size for the architecture.
PESECTALIGN = 0x1000 PESECTALIGN int64 = 0x1000
// FileAlignment should be a power of 2 between 512 and 64 K, inclusive. // FileAlignment should be a power of 2 between 512 and 64 K, inclusive.
// The default is 512. If the SectionAlignment is less than // The default is 512. If the SectionAlignment is less than
// the architecture's page size, then FileAlignment must match SectionAlignment. // the architecture's page size, then FileAlignment must match SectionAlignment.
PEFILEALIGN = 2 << 8 PEFILEALIGN int64 = 2 << 8
) )
const ( const (
...@@ -435,8 +437,17 @@ func Peinit(ctxt *Link) { ...@@ -435,8 +437,17 @@ func Peinit(ctxt *Link) {
dd = oh.DataDirectory[:] dd = oh.DataDirectory[:]
} }
if Linkmode == LinkExternal {
PESECTALIGN = 0
PEFILEALIGN = 0
}
PEFILEHEADR = int32(Rnd(int64(len(dosstub)+binary.Size(&fh)+l+binary.Size(&sh)), PEFILEALIGN)) PEFILEHEADR = int32(Rnd(int64(len(dosstub)+binary.Size(&fh)+l+binary.Size(&sh)), PEFILEALIGN))
if Linkmode != LinkExternal {
PESECTHEADR = int32(Rnd(int64(PEFILEHEADR), PESECTALIGN)) PESECTHEADR = int32(Rnd(int64(PEFILEHEADR), PESECTALIGN))
} else {
PESECTHEADR = 0
}
nextsectoff = int(PESECTHEADR) nextsectoff = int(PESECTHEADR)
nextfileoff = int(PEFILEHEADR) nextfileoff = int(PEFILEHEADR)
...@@ -1218,10 +1229,10 @@ func Asmbpe(ctxt *Link) { ...@@ -1218,10 +1229,10 @@ func Asmbpe(ctxt *Link) {
oh.BaseOfCode = t.VirtualAddress oh.BaseOfCode = t.VirtualAddress
oh64.ImageBase = PEBASE oh64.ImageBase = PEBASE
oh.ImageBase = PEBASE oh.ImageBase = PEBASE
oh64.SectionAlignment = PESECTALIGN oh64.SectionAlignment = uint32(PESECTALIGN)
oh.SectionAlignment = PESECTALIGN oh.SectionAlignment = uint32(PESECTALIGN)
oh64.FileAlignment = PEFILEALIGN oh64.FileAlignment = uint32(PEFILEALIGN)
oh.FileAlignment = PEFILEALIGN oh.FileAlignment = uint32(PEFILEALIGN)
oh64.MajorOperatingSystemVersion = 4 oh64.MajorOperatingSystemVersion = 4
oh.MajorOperatingSystemVersion = 4 oh.MajorOperatingSystemVersion = 4
oh64.MinorOperatingSystemVersion = 0 oh64.MinorOperatingSystemVersion = 0
......
...@@ -144,7 +144,7 @@ func archinit(ctxt *ld.Link) { ...@@ -144,7 +144,7 @@ func archinit(ctxt *ld.Link) {
*ld.FlagDataAddr = 0 *ld.FlagDataAddr = 0
} }
if *ld.FlagRound == -1 { if *ld.FlagRound == -1 {
*ld.FlagRound = ld.PESECTALIGN *ld.FlagRound = int(ld.PESECTALIGN)
} }
} }
......
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