Skip to content

Commit d77d4f5

Browse files
committed
cmd/link: unexport all peSection fields
Change-Id: I83e168f0d1dd1897a0c02c0f1233e1054e93fb0f Reviewed-on: https://go-review.googlesource.com/59791 Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 3bd7971 commit d77d4f5

File tree

1 file changed

+69
-70
lines changed
  • src/cmd/link/internal/ld

1 file changed

+69
-70
lines changed

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

Lines changed: 69 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -290,36 +290,35 @@ func (t *peStringTable) write() {
290290

291291
// peSection represents section from COFF section table.
292292
type peSection struct {
293-
name string
294-
shortName string
295-
index int // one-based index into the Section Table
296-
// TODO: change all these names to start with small letters
297-
VirtualSize uint32
298-
VirtualAddress uint32
299-
SizeOfRawData uint32
300-
PointerToRawData uint32
301-
PointerToRelocations uint32
302-
NumberOfRelocations uint16
303-
Characteristics uint32
293+
name string
294+
shortName string
295+
index int // one-based index into the Section Table
296+
virtualSize uint32
297+
virtualAddress uint32
298+
sizeOfRawData uint32
299+
pointerToRawData uint32
300+
pointerToRelocations uint32
301+
numberOfRelocations uint16
302+
characteristics uint32
304303
}
305304

306305
// checkOffset verifies COFF section sect offset in the file.
307306
func (sect *peSection) checkOffset(off int64) {
308-
if off != int64(sect.PointerToRawData) {
309-
Errorf(nil, "%s.PointerToRawData = %#x, want %#x", sect.name, uint64(int64(sect.PointerToRawData)), uint64(off))
307+
if off != int64(sect.pointerToRawData) {
308+
Errorf(nil, "%s.PointerToRawData = %#x, want %#x", sect.name, uint64(int64(sect.pointerToRawData)), uint64(off))
310309
errorexit()
311310
}
312311
}
313312

314313
// checkSegment verifies COFF section sect matches address
315314
// and file offset provided in segment seg.
316315
func (sect *peSection) checkSegment(seg *Segment) {
317-
if seg.Vaddr-PEBASE != uint64(sect.VirtualAddress) {
318-
Errorf(nil, "%s.VirtualAddress = %#x, want %#x", sect.name, uint64(int64(sect.VirtualAddress)), uint64(int64(seg.Vaddr-PEBASE)))
316+
if seg.Vaddr-PEBASE != uint64(sect.virtualAddress) {
317+
Errorf(nil, "%s.VirtualAddress = %#x, want %#x", sect.name, uint64(int64(sect.virtualAddress)), uint64(int64(seg.Vaddr-PEBASE)))
319318
errorexit()
320319
}
321-
if seg.Fileoff != uint64(sect.PointerToRawData) {
322-
Errorf(nil, "%s.PointerToRawData = %#x, want %#x", sect.name, uint64(int64(sect.PointerToRawData)), uint64(int64(seg.Fileoff)))
320+
if seg.Fileoff != uint64(sect.pointerToRawData) {
321+
Errorf(nil, "%s.PointerToRawData = %#x, want %#x", sect.name, uint64(int64(sect.pointerToRawData)), uint64(int64(seg.Fileoff)))
323322
errorexit()
324323
}
325324
}
@@ -328,21 +327,21 @@ func (sect *peSection) checkSegment(seg *Segment) {
328327
// as necessary to make section sect.SizeOfRawData bytes long.
329328
// It assumes that n bytes are already written to the file.
330329
func (sect *peSection) pad(n uint32) {
331-
strnput("", int(sect.SizeOfRawData-n))
330+
strnput("", int(sect.sizeOfRawData-n))
332331
}
333332

334333
// write writes COFF section sect into the output file.
335334
func (sect *peSection) write() error {
336335
h := pe.SectionHeader32{
337-
VirtualSize: sect.VirtualSize,
338-
SizeOfRawData: sect.SizeOfRawData,
339-
PointerToRawData: sect.PointerToRawData,
340-
PointerToRelocations: sect.PointerToRelocations,
341-
NumberOfRelocations: sect.NumberOfRelocations,
342-
Characteristics: sect.Characteristics,
336+
VirtualSize: sect.virtualSize,
337+
SizeOfRawData: sect.sizeOfRawData,
338+
PointerToRawData: sect.pointerToRawData,
339+
PointerToRelocations: sect.pointerToRelocations,
340+
NumberOfRelocations: sect.numberOfRelocations,
341+
Characteristics: sect.characteristics,
343342
}
344343
if Linkmode != LinkExternal {
345-
h.VirtualAddress = sect.VirtualAddress
344+
h.VirtualAddress = sect.virtualAddress
346345
}
347346
copy(h.Name[:], sect.shortName)
348347
return binary.Write(&coutbuf, binary.LittleEndian, h)
@@ -353,7 +352,7 @@ func (sect *peSection) write() error {
353352
// This updates the corresponding PE section table entry
354353
// with the relocation offset and count.
355354
func (sect *peSection) emitRelocations(relocfn func() int) {
356-
sect.PointerToRelocations = uint32(coutbuf.Offset())
355+
sect.pointerToRelocations = uint32(coutbuf.Offset())
357356
// first entry: extended relocs
358357
Lputl(0) // placeholder for number of relocation + 1
359358
Lputl(0)
@@ -362,16 +361,16 @@ func (sect *peSection) emitRelocations(relocfn func() int) {
362361
n := relocfn() + 1
363362

364363
cpos := coutbuf.Offset()
365-
Cseek(int64(sect.PointerToRelocations))
364+
Cseek(int64(sect.pointerToRelocations))
366365
Lputl(uint32(n))
367366
Cseek(cpos)
368367
if n > 0x10000 {
369368
n = 0x10000
370-
sect.Characteristics |= IMAGE_SCN_LNK_NRELOC_OVFL
369+
sect.characteristics |= IMAGE_SCN_LNK_NRELOC_OVFL
371370
} else {
372-
sect.PointerToRelocations += 10 // skip the extend reloc entry
371+
sect.pointerToRelocations += 10 // skip the extend reloc entry
373372
}
374-
sect.NumberOfRelocations = uint16(n - 1)
373+
sect.numberOfRelocations = uint16(n - 1)
375374
}
376375

377376
// peFile is used to build COFF file.
@@ -395,14 +394,14 @@ func (f *peFile) addSection(name string, sectsize int, filesize int) *peSection
395394
name: name,
396395
shortName: name,
397396
index: len(f.sections) + 1,
398-
VirtualSize: uint32(sectsize),
399-
VirtualAddress: f.nextSectOffset,
400-
PointerToRawData: f.nextFileOffset,
397+
virtualSize: uint32(sectsize),
398+
virtualAddress: f.nextSectOffset,
399+
pointerToRawData: f.nextFileOffset,
401400
}
402401
f.nextSectOffset = uint32(Rnd(int64(f.nextSectOffset)+int64(sectsize), PESECTALIGN))
403402
if filesize > 0 {
404-
sect.SizeOfRawData = uint32(Rnd(int64(filesize), PEFILEALIGN))
405-
f.nextFileOffset += sect.SizeOfRawData
403+
sect.sizeOfRawData = uint32(Rnd(int64(filesize), PEFILEALIGN))
404+
f.nextFileOffset += sect.sizeOfRawData
406405
}
407406
f.sections = append(f.sections, sect)
408407
return sect
@@ -424,7 +423,7 @@ func (f *peFile) addDWARFSection(name string, size int) *peSection {
424423
off := f.stringTable.add(name)
425424
h := f.addSection(name, size, size)
426425
h.shortName = fmt.Sprintf("/%d", off)
427-
h.Characteristics = IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_DISCARDABLE
426+
h.characteristics = IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_DISCARDABLE
428427
return h
429428
}
430429

@@ -439,8 +438,8 @@ func (f *peFile) addDWARF() {
439438
for _, sect := range Segdwarf.Sections {
440439
h := f.addDWARFSection(sect.Name, int(sect.Length))
441440
fileoff := sect.Vaddr - Segdwarf.Vaddr + Segdwarf.Fileoff
442-
if uint64(h.PointerToRawData) != fileoff {
443-
Exitf("%s.PointerToRawData = %#x, want %#x", sect.Name, h.PointerToRawData, fileoff)
441+
if uint64(h.pointerToRawData) != fileoff {
442+
Exitf("%s.PointerToRawData = %#x, want %#x", sect.Name, h.pointerToRawData, fileoff)
444443
}
445444
}
446445
}
@@ -462,9 +461,9 @@ func (f *peFile) addInitArray(ctxt *Link) *peSection {
462461
size = 8
463462
}
464463
sect := f.addSection(".ctors", size, size)
465-
sect.Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ
466-
sect.SizeOfRawData = uint32(size)
467-
Cseek(int64(sect.PointerToRawData))
464+
sect.characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ
465+
sect.sizeOfRawData = uint32(size)
466+
Cseek(int64(sect.pointerToRawData))
468467
sect.checkOffset(coutbuf.Offset())
469468

470469
init_entry := ctxt.Syms.Lookup(*flagEntrySymbol, 0)
@@ -698,7 +697,7 @@ func (f *peFile) writeSymbolTableAndStringTable(ctxt *Link) {
698697
// We do not really need .symtab for go.o, and if we have one, ld
699698
// will also include it in the exe, and that will confuse windows.
700699
h = f.addSection(".symtab", size, size)
701-
h.Characteristics = IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_DISCARDABLE
700+
h.characteristics = IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_DISCARDABLE
702701
h.checkOffset(f.symtabOffset)
703702
}
704703

@@ -758,26 +757,26 @@ func (f *peFile) writeOptionalHeader(ctxt *Link) {
758757
oh64.Magic = 0x20b // PE32+
759758
} else {
760759
oh.Magic = 0x10b // PE32
761-
oh.BaseOfData = f.dataSect.VirtualAddress
760+
oh.BaseOfData = f.dataSect.virtualAddress
762761
}
763762

764763
// Fill out both oh64 and oh. We only use one. Oh well.
765764
oh64.MajorLinkerVersion = 3
766765
oh.MajorLinkerVersion = 3
767766
oh64.MinorLinkerVersion = 0
768767
oh.MinorLinkerVersion = 0
769-
oh64.SizeOfCode = f.textSect.SizeOfRawData
770-
oh.SizeOfCode = f.textSect.SizeOfRawData
771-
oh64.SizeOfInitializedData = f.dataSect.SizeOfRawData
772-
oh.SizeOfInitializedData = f.dataSect.SizeOfRawData
768+
oh64.SizeOfCode = f.textSect.sizeOfRawData
769+
oh.SizeOfCode = f.textSect.sizeOfRawData
770+
oh64.SizeOfInitializedData = f.dataSect.sizeOfRawData
771+
oh.SizeOfInitializedData = f.dataSect.sizeOfRawData
773772
oh64.SizeOfUninitializedData = 0
774773
oh.SizeOfUninitializedData = 0
775774
if Linkmode != LinkExternal {
776775
oh64.AddressOfEntryPoint = uint32(Entryvalue(ctxt) - PEBASE)
777776
oh.AddressOfEntryPoint = uint32(Entryvalue(ctxt) - PEBASE)
778777
}
779-
oh64.BaseOfCode = f.textSect.VirtualAddress
780-
oh.BaseOfCode = f.textSect.VirtualAddress
778+
oh64.BaseOfCode = f.textSect.virtualAddress
779+
oh.BaseOfCode = f.textSect.virtualAddress
781780
oh64.ImageBase = PEBASE
782781
oh.ImageBase = PEBASE
783782
oh64.SectionAlignment = uint32(PESECTALIGN)
@@ -1096,15 +1095,15 @@ func addimports(ctxt *Link, datsect *peSection) {
10961095
n = uint64(coutbuf.Offset()) - uint64(startoff)
10971096

10981097
isect := pefile.addSection(".idata", int(n), int(n))
1099-
isect.Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE
1098+
isect.characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE
11001099
isect.checkOffset(startoff)
11011100
isect.pad(uint32(n))
11021101
endoff := coutbuf.Offset()
11031102

11041103
// write FirstThunks (allocated in .data section)
1105-
ftbase := uint64(dynamic.Value) - uint64(datsect.VirtualAddress) - PEBASE
1104+
ftbase := uint64(dynamic.Value) - uint64(datsect.virtualAddress) - PEBASE
11061105

1107-
Cseek(int64(uint64(datsect.PointerToRawData) + ftbase))
1106+
Cseek(int64(uint64(datsect.pointerToRawData) + ftbase))
11081107
for d := dr; d != nil; d = d.next {
11091108
for m = d.ms; m != nil; m = m.next {
11101109
if pe64 != 0 {
@@ -1125,11 +1124,11 @@ func addimports(ctxt *Link, datsect *peSection) {
11251124
Cseek(startoff)
11261125

11271126
for d := dr; d != nil; d = d.next {
1128-
Lputl(uint32(uint64(isect.VirtualAddress) + oftbase + d.thunkoff))
1127+
Lputl(uint32(uint64(isect.virtualAddress) + oftbase + d.thunkoff))
11291128
Lputl(0)
11301129
Lputl(0)
1131-
Lputl(uint32(uint64(isect.VirtualAddress) + d.nameoff))
1132-
Lputl(uint32(uint64(datsect.VirtualAddress) + ftbase + d.thunkoff))
1130+
Lputl(uint32(uint64(isect.virtualAddress) + d.nameoff))
1131+
Lputl(uint32(uint64(datsect.virtualAddress) + ftbase + d.thunkoff))
11331132
}
11341133

11351134
Lputl(0) //end
@@ -1139,8 +1138,8 @@ func addimports(ctxt *Link, datsect *peSection) {
11391138
Lputl(0)
11401139

11411140
// update data directory
1142-
pefile.dataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress = isect.VirtualAddress
1143-
pefile.dataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].Size = isect.VirtualSize
1141+
pefile.dataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress = isect.virtualAddress
1142+
pefile.dataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].Size = isect.virtualSize
11441143
pefile.dataDirectory[IMAGE_DIRECTORY_ENTRY_IAT].VirtualAddress = uint32(dynamic.Value - PEBASE)
11451144
pefile.dataDirectory[IMAGE_DIRECTORY_ENTRY_IAT].Size = uint32(dynamic.Size)
11461145

@@ -1184,11 +1183,11 @@ func addexports(ctxt *Link) {
11841183
}
11851184

11861185
sect := pefile.addSection(".edata", size, size)
1187-
sect.Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ
1186+
sect.characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ
11881187
sect.checkOffset(coutbuf.Offset())
1189-
va := int(sect.VirtualAddress)
1188+
va := int(sect.virtualAddress)
11901189
pefile.dataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress = uint32(va)
1191-
pefile.dataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size = sect.VirtualSize
1190+
pefile.dataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size = sect.virtualSize
11921191

11931192
vaName := va + binary.Size(&e) + nexport*4
11941193
vaAddr := va + binary.Size(&e)
@@ -1260,7 +1259,7 @@ func addpersrc(ctxt *Link) {
12601259
}
12611260

12621261
h := pefile.addSection(".rsrc", int(rsrcsym.Size), int(rsrcsym.Size))
1263-
h.Characteristics = IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE | IMAGE_SCN_CNT_INITIALIZED_DATA
1262+
h.characteristics = IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE | IMAGE_SCN_CNT_INITIALIZED_DATA
12641263
h.checkOffset(coutbuf.Offset())
12651264

12661265
// relocation
@@ -1270,7 +1269,7 @@ func addpersrc(ctxt *Link) {
12701269
for ri := 0; ri < len(rsrcsym.R); ri++ {
12711270
r = &rsrcsym.R[ri]
12721271
p = rsrcsym.P[r.Off:]
1273-
val = uint32(int64(h.VirtualAddress) + r.Add)
1272+
val = uint32(int64(h.virtualAddress) + r.Add)
12741273

12751274
// 32-bit little-endian
12761275
p[0] = byte(val)
@@ -1284,9 +1283,9 @@ func addpersrc(ctxt *Link) {
12841283
h.pad(uint32(rsrcsym.Size))
12851284

12861285
// update data directory
1287-
pefile.dataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress = h.VirtualAddress
1286+
pefile.dataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress = h.virtualAddress
12881287

1289-
pefile.dataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].Size = h.VirtualSize
1288+
pefile.dataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].Size = h.virtualSize
12901289
}
12911290

12921291
func Asmbpe(ctxt *Link) {
@@ -1297,30 +1296,30 @@ func Asmbpe(ctxt *Link) {
12971296
}
12981297

12991298
t := pefile.addSection(".text", int(Segtext.Length), int(Segtext.Length))
1300-
t.Characteristics = IMAGE_SCN_CNT_CODE | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ
1299+
t.characteristics = IMAGE_SCN_CNT_CODE | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ
13011300
if Linkmode == LinkExternal {
13021301
// some data symbols (e.g. masks) end up in the .text section, and they normally
13031302
// expect larger alignment requirement than the default text section alignment.
1304-
t.Characteristics |= IMAGE_SCN_ALIGN_32BYTES
1303+
t.characteristics |= IMAGE_SCN_ALIGN_32BYTES
13051304
}
13061305
t.checkSegment(&Segtext)
13071306
pefile.textSect = t
13081307

13091308
var d *peSection
13101309
if Linkmode != LinkExternal {
13111310
d = pefile.addSection(".data", int(Segdata.Length), int(Segdata.Filelen))
1312-
d.Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE
1311+
d.characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE
13131312
d.checkSegment(&Segdata)
13141313
pefile.dataSect = d
13151314
} else {
13161315
d = pefile.addSection(".data", int(Segdata.Filelen), int(Segdata.Filelen))
1317-
d.Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE | IMAGE_SCN_ALIGN_32BYTES
1316+
d.characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE | IMAGE_SCN_ALIGN_32BYTES
13181317
d.checkSegment(&Segdata)
13191318
pefile.dataSect = d
13201319

13211320
b := pefile.addSection(".bss", int(Segdata.Length-Segdata.Filelen), 0)
1322-
b.Characteristics = IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE | IMAGE_SCN_ALIGN_32BYTES
1323-
b.PointerToRawData = 0
1321+
b.characteristics = IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE | IMAGE_SCN_ALIGN_32BYTES
1322+
b.pointerToRawData = 0
13241323
pefile.bssSect = b
13251324
}
13261325

0 commit comments

Comments
 (0)