aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/internal/obj
diff options
context:
space:
mode:
authorMatthew Dempsky <mdempsky@google.com>2023-08-28 05:13:03 -0700
committerGopher Robot <gobot@golang.org>2023-09-01 15:52:41 +0000
commitc9bb7ce2d7132debe50f024c50ed4ee1460d6af5 (patch)
tree60a390801d54850fd742783abd0ec7a2bbdcb728 /src/cmd/internal/obj
parent06f420fc19f8af6a1935e4edbfd1af361f663190 (diff)
downloadgo-c9bb7ce2d7132debe50f024c50ed4ee1460d6af5.tar.xz
cmd/internal/obj: simplify filename handling
The old Go object file format used linker symbols like "gofile..foo" to record references to the filename "foo". But the current object file format has a dedicated section for file names, so we don't need these useless prefixes anymore. Also, change DWARF generation to pass around the src.Pos directly, rather than the old file symbols, which it just turned back into a file index before writing out anyway. Finally, directly record the FileIndex into src.PosBase, so that we can skip the map lookups. Change-Id: Ia4a5ebfa95da271f2522e45befdb9f137c16d373 Reviewed-on: https://go-review.googlesource.com/c/go/+/523378 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Auto-Submit: Matthew Dempsky <mdempsky@google.com>
Diffstat (limited to 'src/cmd/internal/obj')
-rw-r--r--src/cmd/internal/obj/dwarf.go49
-rw-r--r--src/cmd/internal/obj/line.go18
-rw-r--r--src/cmd/internal/obj/line_test.go12
-rw-r--r--src/cmd/internal/obj/plist.go2
-rw-r--r--src/cmd/internal/obj/sym.go4
5 files changed, 33 insertions, 52 deletions
diff --git a/src/cmd/internal/obj/dwarf.go b/src/cmd/internal/obj/dwarf.go
index 825f0133f1..f5caa08f0a 100644
--- a/src/cmd/internal/obj/dwarf.go
+++ b/src/cmd/internal/obj/dwarf.go
@@ -48,7 +48,7 @@ func (ctxt *Link) generateDebugLinesSymbol(s, lines *LSym) {
line := int64(1)
pc := s.Func().Text.Pc
var lastpc int64 // last PC written to line table, not last PC in func
- name := ""
+ fileIndex := 1
prologue, wrotePrologue := false, false
// Walk the progs, generating the DWARF table.
for p := s.Func().Text; p != nil; p = p.Link {
@@ -58,15 +58,15 @@ func (ctxt *Link) generateDebugLinesSymbol(s, lines *LSym) {
continue
}
newStmt := p.Pos.IsStmt() != src.PosNotStmt
- newName, newLine := ctxt.getFileSymbolAndLine(p.Pos)
+ newFileIndex, newLine := ctxt.getFileIndexAndLine(p.Pos)
+ newFileIndex++ // 1 indexing for the table
// Output debug info.
wrote := false
- if name != newName {
- newFile := ctxt.PosTable.FileIndex(newName) + 1 // 1 indexing for the table.
+ if newFileIndex != fileIndex {
dctxt.AddUint8(lines, dwarf.DW_LNS_set_file)
- dwarf.Uleb128put(dctxt, lines, int64(newFile))
- name = newName
+ dwarf.Uleb128put(dctxt, lines, int64(newFileIndex))
+ fileIndex = newFileIndex
wrote = true
}
if prologue && !wrotePrologue {
@@ -258,16 +258,6 @@ func (c dwCtxt) AddDWARFAddrSectionOffset(s dwarf.Sym, t interface{}, ofs int64)
r.Type = objabi.R_DWARFSECREF
}
-func (c dwCtxt) AddFileRef(s dwarf.Sym, f interface{}) {
- ls := s.(*LSym)
- rsym := f.(*LSym)
- fidx := c.Link.PosTable.FileIndex(rsym.Name)
- // Note the +1 here -- the value we're writing is going to be an
- // index into the DWARF line table file section, whose entries
- // are numbered starting at 1, not 0.
- ls.WriteInt(c.Link, ls.Size, 4, int64(fidx+1))
-}
-
func (c dwCtxt) CurrentOffset(s dwarf.Sym) int64 {
ls := s.(*LSym)
return ls.Size
@@ -329,17 +319,13 @@ func (s *LSym) Length(dwarfContext interface{}) int64 {
return s.Size
}
-// fileSymbol returns a symbol corresponding to the source file of the
-// first instruction (prog) of the specified function. This will
-// presumably be the file in which the function is defined.
-func (ctxt *Link) fileSymbol(fn *LSym) *LSym {
- p := fn.Func().Text
- if p != nil {
- f, _ := ctxt.getFileSymbolAndLine(p.Pos)
- fsym := ctxt.Lookup(f)
- return fsym
+// textPos returns the source position of the first instruction (prog)
+// of the specified function.
+func textPos(fn *LSym) src.XPos {
+ if p := fn.Func().Text; p != nil {
+ return p.Pos
}
- return nil
+ return src.NoXPos
}
// populateDWARF fills in the DWARF Debugging Information Entries for
@@ -362,17 +348,19 @@ func (ctxt *Link) populateDWARF(curfn Func, s *LSym) {
}
var err error
dwctxt := dwCtxt{ctxt}
- filesym := ctxt.fileSymbol(s)
+ startPos := ctxt.InnermostPos(textPos(s))
+ if !startPos.IsKnown() || startPos.RelLine() != uint(s.Func().StartLine) {
+ panic("bad startPos")
+ }
fnstate := &dwarf.FnState{
Name: s.Name,
Info: info,
- Filesym: filesym,
Loc: loc,
Ranges: ranges,
Absfn: absfunc,
StartPC: s,
Size: s.Size,
- StartLine: s.Func().StartLine,
+ StartPos: startPos,
External: !s.Static(),
Scopes: scopes,
InlCalls: inlcalls,
@@ -434,13 +422,12 @@ func (ctxt *Link) DwarfAbstractFunc(curfn Func, s *LSym) {
s.NewFuncInfo()
}
scopes, _ := ctxt.DebugInfo(s, absfn, curfn)
- _, startLine := ctxt.getFileSymbolAndLine(curfn.Pos())
dwctxt := dwCtxt{ctxt}
fnstate := dwarf.FnState{
Name: s.Name,
Info: absfn,
Absfn: absfn,
- StartLine: startLine,
+ StartPos: ctxt.InnermostPos(curfn.Pos()),
External: !s.Static(),
Scopes: scopes,
UseBASEntries: ctxt.UseBASEntries,
diff --git a/src/cmd/internal/obj/line.go b/src/cmd/internal/obj/line.go
index 20f03d9853..988640f6a4 100644
--- a/src/cmd/internal/obj/line.go
+++ b/src/cmd/internal/obj/line.go
@@ -14,22 +14,14 @@ func (ctxt *Link) AddImport(pkg string, fingerprint goobj.FingerprintType) {
ctxt.Imports = append(ctxt.Imports, goobj.ImportedPkg{Pkg: pkg, Fingerprint: fingerprint})
}
-// getFileSymbolAndLine returns the relative file symbol and relative line
-// number for a position (i.e., as adjusted by a //line directive). This is the
-// file/line visible in the final binary (pcfile, pcln, etc).
-func (ctxt *Link) getFileSymbolAndLine(xpos src.XPos) (f string, l int32) {
- pos := ctxt.InnermostPos(xpos)
- if !pos.IsKnown() {
- pos = src.Pos{}
- }
- return pos.SymFilename(), int32(pos.RelLine())
-}
-
// getFileIndexAndLine returns the relative file index (local to the CU), and
// the relative line number for a position (i.e., as adjusted by a //line
// directive). This is the file/line visible in the final binary (pcfile, pcln,
// etc).
func (ctxt *Link) getFileIndexAndLine(xpos src.XPos) (int, int32) {
- f, l := ctxt.getFileSymbolAndLine(xpos)
- return ctxt.PosTable.FileIndex(f), l
+ pos := ctxt.InnermostPos(xpos)
+ if !pos.IsKnown() {
+ pos = src.Pos{}
+ }
+ return pos.FileIndex(), int32(pos.RelLine())
}
diff --git a/src/cmd/internal/obj/line_test.go b/src/cmd/internal/obj/line_test.go
index d3bb4e2639..de7ef1a22e 100644
--- a/src/cmd/internal/obj/line_test.go
+++ b/src/cmd/internal/obj/line_test.go
@@ -31,9 +31,15 @@ func TestGetFileSymbolAndLine(t *testing.T) {
}
for _, test := range tests {
- f, l := ctxt.getFileSymbolAndLine(ctxt.PosTable.XPos(test.pos))
- got := fmt.Sprintf("%s:%d", f, l)
- if got != src.FileSymPrefix+test.want {
+ fileIndex, line := ctxt.getFileIndexAndLine(ctxt.PosTable.XPos(test.pos))
+
+ file := "??"
+ if fileIndex >= 0 {
+ file = ctxt.PosTable.FileTable()[fileIndex]
+ }
+ got := fmt.Sprintf("%s:%d", file, line)
+
+ if got != test.want {
t.Errorf("ctxt.getFileSymbolAndLine(%v) = %q, want %q", test.pos, got, test.want)
}
}
diff --git a/src/cmd/internal/obj/plist.go b/src/cmd/internal/obj/plist.go
index cd6e2313ad..9cf6a20bdb 100644
--- a/src/cmd/internal/obj/plist.go
+++ b/src/cmd/internal/obj/plist.go
@@ -198,7 +198,7 @@ func (ctxt *Link) InitTextSym(s *LSym, flag int, start src.XPos) {
// startLine should be the same line number that would be displayed via
// pcln, etc for the declaration (i.e., relative line number, as
// adjusted by //line).
- _, startLine := ctxt.getFileSymbolAndLine(start)
+ _, startLine := ctxt.getFileIndexAndLine(start)
s.Func().FuncID = objabi.GetFuncID(s.Name, flag&WRAPPER != 0 || flag&ABIWRAPPER != 0)
s.Func().FuncFlag = ctxt.toFuncFlag(flag)
diff --git a/src/cmd/internal/obj/sym.go b/src/cmd/internal/obj/sym.go
index 63d7d22e33..f27d4ef4fc 100644
--- a/src/cmd/internal/obj/sym.go
+++ b/src/cmd/internal/obj/sym.go
@@ -449,10 +449,6 @@ func (ctxt *Link) traverseFuncAux(flag traverseFlag, fsym *LSym, fn func(parent
if call.Func != nil {
fn(fsym, call.Func)
}
- f, _ := ctxt.getFileSymbolAndLine(call.Pos)
- if filesym := ctxt.Lookup(f); filesym != nil {
- fn(fsym, filesym)
- }
}
auxsyms := []*LSym{fninfo.dwarfRangesSym, fninfo.dwarfLocSym, fninfo.dwarfDebugLinesSym, fninfo.dwarfInfoSym, fninfo.WasmImportSym, fninfo.sehUnwindInfoSym}