Skip to content

Commit 2bb6e16

Browse files
rscpull[bot]
authored andcommitted
cmd/internal/objabi, cmd/link: introduce SymKind helper methods
These will be necessary when we start using the new FIPS symbols. Split into a separate CL so that these refactoring changes can be tested separate from any FIPS-specific changes. Passes golang.org/x/tools/cmd/toolstash/buildall. Change-Id: I73e5873fcb677f1f572f0668b4dc6f3951d822bc Reviewed-on: https://go-review.googlesource.com/c/go/+/625996 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Cherry Mui <[email protected]> Auto-Submit: Russ Cox <[email protected]>
1 parent 7ef0e9e commit 2bb6e16

File tree

26 files changed

+133
-87
lines changed

26 files changed

+133
-87
lines changed

src/cmd/internal/obj/dwarf.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ func isDwarf64(ctxt *Link) bool {
293293
}
294294

295295
func (ctxt *Link) dwarfSym(s *LSym) (dwarfInfoSym, dwarfLocSym, dwarfRangesSym, dwarfAbsFnSym, dwarfDebugLines *LSym) {
296-
if s.Type != objabi.STEXT {
296+
if !s.Type.IsText() {
297297
ctxt.Diag("dwarfSym of non-TEXT %v", s)
298298
}
299299
fn := s.Func()

src/cmd/internal/obj/objfile.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -896,15 +896,15 @@ func (ctxt *Link) writeSymDebugNamed(s *LSym, name string) {
896896
fmt.Fprintf(ctxt.Bso, "asm ")
897897
}
898898
fmt.Fprintf(ctxt.Bso, "size=%d", s.Size)
899-
if s.Type == objabi.STEXT {
899+
if s.Type.IsText() {
900900
fn := s.Func()
901901
fmt.Fprintf(ctxt.Bso, " args=%#x locals=%#x funcid=%#x align=%#x", uint64(fn.Args), uint64(fn.Locals), uint64(fn.FuncID), uint64(fn.Align))
902902
if s.Leaf() {
903903
fmt.Fprintf(ctxt.Bso, " leaf")
904904
}
905905
}
906906
fmt.Fprintf(ctxt.Bso, "\n")
907-
if s.Type == objabi.STEXT {
907+
if s.Type.IsText() {
908908
for p := s.Func().Text; p != nil; p = p.Link {
909909
fmt.Fprintf(ctxt.Bso, "\t%#04x ", uint(int(p.Pc)))
910910
if ctxt.Debugasm > 1 {

src/cmd/internal/obj/plist.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ func (ctxt *Link) GloblPos(s *LSym, size int64, flag int, pos src.XPos) {
250250
if flag&RODATA != 0 {
251251
s.Type = objabi.SRODATA
252252
} else if flag&NOPTR != 0 {
253-
if s.Type == objabi.SDATA {
253+
if s.Type.IsDATA() {
254254
s.Type = objabi.SNOPTRDATA
255255
} else {
256256
s.Type = objabi.SNOPTRBSS

src/cmd/internal/obj/sym.go

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -137,58 +137,67 @@ func (ctxt *Link) LookupInit(name string, init func(s *LSym)) *LSym {
137137
return s
138138
}
139139

140+
func (ctxt *Link) rodataKind() (suffix string, typ objabi.SymKind) {
141+
return "", objabi.SRODATA
142+
}
143+
140144
func (ctxt *Link) Float32Sym(f float32) *LSym {
145+
suffix, typ := ctxt.rodataKind()
141146
i := math.Float32bits(f)
142-
name := fmt.Sprintf("$f32.%08x", i)
147+
name := fmt.Sprintf("$f32.%08x%s", i, suffix)
143148
return ctxt.LookupInit(name, func(s *LSym) {
144149
s.Size = 4
145150
s.WriteFloat32(ctxt, 0, f)
146-
s.Type = objabi.SRODATA
151+
s.Type = typ
147152
s.Set(AttrLocal, true)
148153
s.Set(AttrContentAddressable, true)
149154
ctxt.constSyms = append(ctxt.constSyms, s)
150155
})
151156
}
152157

153158
func (ctxt *Link) Float64Sym(f float64) *LSym {
159+
suffix, typ := ctxt.rodataKind()
154160
i := math.Float64bits(f)
155-
name := fmt.Sprintf("$f64.%016x", i)
161+
name := fmt.Sprintf("$f64.%016x%s", i, suffix)
156162
return ctxt.LookupInit(name, func(s *LSym) {
157163
s.Size = 8
158164
s.WriteFloat64(ctxt, 0, f)
159-
s.Type = objabi.SRODATA
165+
s.Type = typ
160166
s.Set(AttrLocal, true)
161167
s.Set(AttrContentAddressable, true)
162168
ctxt.constSyms = append(ctxt.constSyms, s)
163169
})
164170
}
165171

166172
func (ctxt *Link) Int32Sym(i int64) *LSym {
167-
name := fmt.Sprintf("$i32.%08x", uint64(i))
173+
suffix, typ := ctxt.rodataKind()
174+
name := fmt.Sprintf("$i32.%08x%s", uint64(i), suffix)
168175
return ctxt.LookupInit(name, func(s *LSym) {
169176
s.Size = 4
170177
s.WriteInt(ctxt, 0, 4, i)
171-
s.Type = objabi.SRODATA
178+
s.Type = typ
172179
s.Set(AttrLocal, true)
173180
s.Set(AttrContentAddressable, true)
174181
ctxt.constSyms = append(ctxt.constSyms, s)
175182
})
176183
}
177184

178185
func (ctxt *Link) Int64Sym(i int64) *LSym {
179-
name := fmt.Sprintf("$i64.%016x", uint64(i))
186+
suffix, typ := ctxt.rodataKind()
187+
name := fmt.Sprintf("$i64.%016x%s", uint64(i), suffix)
180188
return ctxt.LookupInit(name, func(s *LSym) {
181189
s.Size = 8
182190
s.WriteInt(ctxt, 0, 8, i)
183-
s.Type = objabi.SRODATA
191+
s.Type = typ
184192
s.Set(AttrLocal, true)
185193
s.Set(AttrContentAddressable, true)
186194
ctxt.constSyms = append(ctxt.constSyms, s)
187195
})
188196
}
189197

190198
func (ctxt *Link) Int128Sym(hi, lo int64) *LSym {
191-
name := fmt.Sprintf("$i128.%016x%016x", uint64(hi), uint64(lo))
199+
suffix, typ := ctxt.rodataKind()
200+
name := fmt.Sprintf("$i128.%016x%016x%s", uint64(hi), uint64(lo), suffix)
192201
return ctxt.LookupInit(name, func(s *LSym) {
193202
s.Size = 16
194203
if ctxt.Arch.ByteOrder == binary.LittleEndian {
@@ -198,7 +207,7 @@ func (ctxt *Link) Int128Sym(hi, lo int64) *LSym {
198207
s.WriteInt(ctxt, 0, 8, hi)
199208
s.WriteInt(ctxt, 8, 8, lo)
200209
}
201-
s.Type = objabi.SRODATA
210+
s.Type = typ
202211
s.Set(AttrLocal, true)
203212
s.Set(AttrContentAddressable, true)
204213
ctxt.constSyms = append(ctxt.constSyms, s)
@@ -406,7 +415,7 @@ func (ctxt *Link) traverseSyms(flag traverseFlag, fn func(*LSym)) {
406415
}
407416
if flag&traverseAux != 0 {
408417
fnNoNil(s.Gotype)
409-
if s.Type == objabi.STEXT {
418+
if s.Type.IsText() {
410419
f := func(parent *LSym, aux *LSym) {
411420
fn(aux)
412421
}
@@ -415,7 +424,7 @@ func (ctxt *Link) traverseSyms(flag traverseFlag, fn func(*LSym)) {
415424
fnNoNil(v.dwarfInfoSym)
416425
}
417426
}
418-
if flag&traversePcdata != 0 && s.Type == objabi.STEXT {
427+
if flag&traversePcdata != 0 && s.Type.IsText() {
419428
fi := s.Func().Pcln
420429
fnNoNil(fi.Pcsp)
421430
fnNoNil(fi.Pcfile)
@@ -491,7 +500,7 @@ func (ctxt *Link) traverseAuxSyms(flag traverseFlag, fn func(parent *LSym, aux *
491500
fn(s, s.Gotype)
492501
}
493502
}
494-
if s.Type == objabi.STEXT {
503+
if s.Type.IsText() {
495504
ctxt.traverseFuncAux(flag, s, fn, files)
496505
} else if v := s.VarInfo(); v != nil && v.dwarfInfoSym != nil {
497506
fn(s, v.dwarfInfoSym)

src/cmd/internal/objabi/symkind.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,19 @@ const (
7979
SSEHUNWINDINFO
8080
// Update cmd/link/internal/sym/AbiSymKindToSymKind for new SymKind values.
8181
)
82+
83+
// IsText reports whether t is one of the text kinds.
84+
func (t SymKind) IsText() bool {
85+
return t == STEXT || t == STEXTFIPS
86+
}
87+
88+
// IsDATA reports whether t is one of the DATA kinds (SDATA or SDATAFIPS,
89+
// excluding NOPTRDATA, RODATA, BSS, and so on).
90+
func (t SymKind) IsDATA() bool {
91+
return t == SDATA || t == SDATAFIPS
92+
}
93+
94+
// IsFIPS reports whether t is one fo the FIPS kinds.
95+
func (t SymKind) IsFIPS() bool {
96+
return t == STEXTFIPS || t == SRODATAFIPS || t == SNOPTRDATAFIPS || t == SDATAFIPS
97+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ func adddynrel(target *ld.Target, ldr *loader.Loader, syms *ld.ArchSyms, s loade
263263
return true
264264

265265
case objabi.R_PCREL:
266-
if targType == sym.SDYNIMPORT && ldr.SymType(s) == sym.STEXT && target.IsDarwin() {
266+
if targType == sym.SDYNIMPORT && ldr.SymType(s).IsText() && target.IsDarwin() {
267267
// Loading the address of a dynamic symbol. Rewrite to use GOT.
268268
// turn LEAQ symbol address to MOVQ of GOT entry
269269
if r.Add() != 0 {
@@ -287,7 +287,7 @@ func adddynrel(target *ld.Target, ldr *loader.Loader, syms *ld.ArchSyms, s loade
287287
}
288288

289289
case objabi.R_ADDR:
290-
if ldr.SymType(s) == sym.STEXT && target.IsElf() {
290+
if ldr.SymType(s).IsText() && target.IsElf() {
291291
su := ldr.MakeSymbolUpdater(s)
292292
if target.IsSolaris() {
293293
addpltsym(target, ldr, syms, targ)
@@ -349,7 +349,7 @@ func adddynrel(target *ld.Target, ldr *loader.Loader, syms *ld.ArchSyms, s loade
349349
// linking, in which case the relocation will be
350350
// prepared in the 'reloc' phase and passed to the
351351
// external linker in the 'asmb' phase.
352-
if ldr.SymType(s) != sym.SDATA && ldr.SymType(s) != sym.SRODATA {
352+
if t := ldr.SymType(s); !t.IsDATA() && !t.IsRODATA() {
353353
break
354354
}
355355
}

src/cmd/link/internal/arm/asm.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ func adddynrel(target *ld.Target, ldr *loader.Loader, syms *ld.ArchSyms, s loade
224224
return true
225225

226226
case objabi.R_ADDR:
227-
if ldr.SymType(s) != sym.SDATA {
227+
if !ldr.SymType(s).IsDATA() {
228228
break
229229
}
230230
if target.IsElf() {

src/cmd/link/internal/arm64/asm.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ func adddynrel(target *ld.Target, ldr *loader.Loader, syms *ld.ArchSyms, s loade
305305
return true
306306

307307
case objabi.R_ADDRARM64:
308-
if targType == sym.SDYNIMPORT && ldr.SymType(s) == sym.STEXT && target.IsDarwin() {
308+
if targType == sym.SDYNIMPORT && ldr.SymType(s).IsText() && target.IsDarwin() {
309309
// Loading the address of a dynamic symbol. Rewrite to use GOT.
310310
// turn MOVD $sym (adrp+add) into MOVD sym@GOT (adrp+ldr)
311311
if r.Add() != 0 {
@@ -339,7 +339,7 @@ func adddynrel(target *ld.Target, ldr *loader.Loader, syms *ld.ArchSyms, s loade
339339
}
340340

341341
case objabi.R_ADDR:
342-
if ldr.SymType(s) == sym.STEXT && target.IsElf() {
342+
if ldr.SymType(s).IsText() && target.IsElf() {
343343
// The code is asking for the address of an external
344344
// function. We provide it with the address of the
345345
// correspondent GOT symbol.
@@ -394,7 +394,7 @@ func adddynrel(target *ld.Target, ldr *loader.Loader, syms *ld.ArchSyms, s loade
394394
// linking, in which case the relocation will be
395395
// prepared in the 'reloc' phase and passed to the
396396
// external linker in the 'asmb' phase.
397-
if ldr.SymType(s) != sym.SDATA && ldr.SymType(s) != sym.SRODATA {
397+
if t := ldr.SymType(s); !t.IsDATA() && !t.IsRODATA() {
398398
break
399399
}
400400
}
@@ -1278,7 +1278,7 @@ func gensymlate(ctxt *ld.Link, ldr *loader.Loader) {
12781278
continue
12791279
}
12801280
t := ldr.SymType(s)
1281-
if t == sym.STEXT {
1281+
if t.IsText() {
12821282
// Except for Duff's devices (handled above), we don't
12831283
// target the middle of a function.
12841284
continue

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (d *deadcodePass) init() {
5050
n := d.ldr.NDef()
5151
for i := 1; i < n; i++ {
5252
s := loader.Sym(i)
53-
if d.ldr.SymType(s) == sym.STEXT && d.ldr.SymSize(s) == 0 {
53+
if d.ldr.SymType(s).IsText() && d.ldr.SymSize(s) == 0 {
5454
// Zero-sized text symbol is a function deadcoded by the
5555
// compiler. It doesn't really get compiled, and its
5656
// metadata may be missing.

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1965,8 +1965,9 @@ func dwarfGenerateDebugInfo(ctxt *Link) {
19651965
continue
19661966
}
19671967
t := d.ldr.SymType(idx)
1968-
switch t {
1969-
case sym.SRODATA, sym.SDATA, sym.SNOPTRDATA, sym.STYPE, sym.SBSS, sym.SNOPTRBSS, sym.STLSBSS:
1968+
switch {
1969+
case t.IsRODATA(), t.IsDATA(), t.IsNOPTRDATA(),
1970+
t == sym.STYPE, t == sym.SBSS, t == sym.SNOPTRBSS, t == sym.STLSBSS:
19701971
// ok
19711972
default:
19721973
continue

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2414,7 +2414,7 @@ func elfadddynsym(ldr *loader.Loader, target *Target, syms *ArchSyms, s loader.S
24142414
/* type */
24152415
var t uint8
24162416

2417-
if cgoexp && st == sym.STEXT {
2417+
if cgoexp && st.IsText() {
24182418
t = elf.ST_INFO(elf.STB_GLOBAL, elf.STT_FUNC)
24192419
} else {
24202420
t = elf.ST_INFO(elf.STB_GLOBAL, elf.STT_OBJECT)
@@ -2464,9 +2464,9 @@ func elfadddynsym(ldr *loader.Loader, target *Target, syms *ArchSyms, s loader.S
24642464
var t uint8
24652465

24662466
// TODO(mwhudson): presumably the behavior should actually be the same on both arm and 386.
2467-
if target.Arch.Family == sys.I386 && cgoexp && st == sym.STEXT {
2467+
if target.Arch.Family == sys.I386 && cgoexp && st.IsText() {
24682468
t = elf.ST_INFO(elf.STB_GLOBAL, elf.STT_FUNC)
2469-
} else if target.Arch.Family == sys.ARM && cgoeDynamic && st == sym.STEXT {
2469+
} else if target.Arch.Family == sys.ARM && cgoeDynamic && st.IsText() {
24702470
t = elf.ST_INFO(elf.STB_GLOBAL, elf.STT_FUNC)
24712471
} else {
24722472
t = elf.ST_INFO(elf.STB_GLOBAL, elf.STT_OBJECT)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ func PrepareAddmoduledata(ctxt *Link) (*loader.SymbolBuilder, loader.Sym) {
221221
return nil, 0
222222
}
223223
amd := ctxt.loader.LookupOrCreateSym("runtime.addmoduledata", 0)
224-
if ctxt.loader.SymType(amd) == sym.STEXT && ctxt.BuildMode != BuildModePlugin {
224+
if ctxt.loader.SymType(amd).IsText() && ctxt.BuildMode != BuildModePlugin {
225225
// we're linking a module containing the runtime -> no need for
226226
// an init function
227227
return nil, 0

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2748,7 +2748,7 @@ func Entryvalue(ctxt *Link) int64 {
27482748
if st == 0 {
27492749
return *FlagTextAddr
27502750
}
2751-
if !ctxt.IsAIX() && st != sym.STEXT {
2751+
if !ctxt.IsAIX() && !st.IsText() {
27522752
ldr.Errorf(s, "entry not text")
27532753
}
27542754
return ldr.SymValue(s)
@@ -2768,7 +2768,7 @@ func (ctxt *Link) callgraph() {
27682768
if rs == 0 {
27692769
continue
27702770
}
2771-
if r.Type().IsDirectCall() && ldr.SymType(rs) == sym.STEXT {
2771+
if r.Type().IsDirectCall() && ldr.SymType(rs).IsText() {
27722772
ctxt.Logf("%s calls %s\n", ldr.SymName(s), ldr.SymName(rs))
27732773
}
27742774
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@ func collectmachosyms(ctxt *Link) {
866866
if !*FlagS {
867867
if !ctxt.DynlinkingGo() {
868868
s := ldr.Lookup("runtime.text", 0)
869-
if ldr.SymType(s) == sym.STEXT {
869+
if ldr.SymType(s).IsText() {
870870
addsym(s)
871871
}
872872
}
@@ -880,7 +880,7 @@ func collectmachosyms(ctxt *Link) {
880880
}
881881
if !ctxt.DynlinkingGo() {
882882
s := ldr.Lookup("runtime.etext", 0)
883-
if ldr.SymType(s) == sym.STEXT {
883+
if ldr.SymType(s).IsText() {
884884
addsym(s)
885885
}
886886
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ func (f *peFile) mapToPESection(ldr *loader.Loader, s loader.Sym, linkmode LinkM
736736
if linkmode != LinkExternal {
737737
return f.dataSect.index, int64(v), nil
738738
}
739-
if ldr.SymType(s) == sym.SDATA {
739+
if ldr.SymType(s).IsDATA() {
740740
return f.dataSect.index, int64(v), nil
741741
}
742742
// Note: although address of runtime.edata (type sym.SDATA) is at the start of .bss section
@@ -793,8 +793,8 @@ func (f *peFile) writeSymbols(ctxt *Link) {
793793
name = mangleABIName(ctxt, ldr, s, name)
794794

795795
var peSymType uint16 = IMAGE_SYM_TYPE_NULL
796-
switch t {
797-
case sym.STEXT, sym.SDYNIMPORT, sym.SHOSTOBJ, sym.SUNDEFEXT:
796+
switch {
797+
case t.IsText(), t == sym.SDYNIMPORT, t == sym.SHOSTOBJ, t == sym.SUNDEFEXT:
798798
// Microsoft's PE documentation is contradictory. It says that the symbol's complex type
799799
// is stored in the pesym.Type most significant byte, but MSVC, LLVM, and mingw store it
800800
// in the 4 high bits of the less significant byte. Also, the PE documentation says that
@@ -828,11 +828,11 @@ func (f *peFile) writeSymbols(ctxt *Link) {
828828

829829
// Add special runtime.text and runtime.etext symbols.
830830
s := ldr.Lookup("runtime.text", 0)
831-
if ldr.SymType(s) == sym.STEXT {
831+
if ldr.SymType(s).IsText() {
832832
addsym(s)
833833
}
834834
s = ldr.Lookup("runtime.etext", 0)
835-
if ldr.SymType(s) == sym.STEXT {
835+
if ldr.SymType(s).IsText() {
836836
addsym(s)
837837
}
838838

0 commit comments

Comments
 (0)