Skip to content

Commit e31144f

Browse files
committed
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/36974 Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 64c0246 commit e31144f

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

src/cmd/link/internal/amd64/obj.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func archinit(ctxt *ld.Link) {
152152
*ld.FlagDataAddr = 0
153153
}
154154
if *ld.FlagRound == -1 {
155-
*ld.FlagRound = ld.PESECTALIGN
155+
*ld.FlagRound = int(ld.PESECTALIGN)
156156
}
157157
}
158158

src/cmd/link/internal/ld/pe.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,17 @@ type IMAGE_EXPORT_DIRECTORY struct {
101101

102102
const (
103103
PEBASE = 0x00400000
104+
)
104105

106+
var (
105107
// SectionAlignment must be greater than or equal to FileAlignment.
106108
// The default is the page size for the architecture.
107-
PESECTALIGN = 0x1000
109+
PESECTALIGN int64 = 0x1000
108110

109111
// FileAlignment should be a power of 2 between 512 and 64 K, inclusive.
110112
// The default is 512. If the SectionAlignment is less than
111113
// the architecture's page size, then FileAlignment must match SectionAlignment.
112-
PEFILEALIGN = 2 << 8
114+
PEFILEALIGN int64 = 2 << 8
113115
)
114116

115117
const (
@@ -435,8 +437,17 @@ func Peinit(ctxt *Link) {
435437
dd = oh.DataDirectory[:]
436438
}
437439

440+
if Linkmode == LinkExternal {
441+
PESECTALIGN = 0
442+
PEFILEALIGN = 0
443+
}
444+
438445
PEFILEHEADR = int32(Rnd(int64(len(dosstub)+binary.Size(&fh)+l+binary.Size(&sh)), PEFILEALIGN))
439-
PESECTHEADR = int32(Rnd(int64(PEFILEHEADR), PESECTALIGN))
446+
if Linkmode != LinkExternal {
447+
PESECTHEADR = int32(Rnd(int64(PEFILEHEADR), PESECTALIGN))
448+
} else {
449+
PESECTHEADR = 0
450+
}
440451
nextsectoff = int(PESECTHEADR)
441452
nextfileoff = int(PEFILEHEADR)
442453

@@ -1218,10 +1229,10 @@ func Asmbpe(ctxt *Link) {
12181229
oh.BaseOfCode = t.VirtualAddress
12191230
oh64.ImageBase = PEBASE
12201231
oh.ImageBase = PEBASE
1221-
oh64.SectionAlignment = PESECTALIGN
1222-
oh.SectionAlignment = PESECTALIGN
1223-
oh64.FileAlignment = PEFILEALIGN
1224-
oh.FileAlignment = PEFILEALIGN
1232+
oh64.SectionAlignment = uint32(PESECTALIGN)
1233+
oh.SectionAlignment = uint32(PESECTALIGN)
1234+
oh64.FileAlignment = uint32(PEFILEALIGN)
1235+
oh.FileAlignment = uint32(PEFILEALIGN)
12251236
oh64.MajorOperatingSystemVersion = 4
12261237
oh.MajorOperatingSystemVersion = 4
12271238
oh64.MinorOperatingSystemVersion = 0

src/cmd/link/internal/x86/obj.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func archinit(ctxt *ld.Link) {
144144
*ld.FlagDataAddr = 0
145145
}
146146
if *ld.FlagRound == -1 {
147-
*ld.FlagRound = ld.PESECTALIGN
147+
*ld.FlagRound = int(ld.PESECTALIGN)
148148
}
149149
}
150150

0 commit comments

Comments
 (0)