]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/link: improve error message and debugging
authorCherry Mui <cherryyz@google.com>
Wed, 15 Mar 2023 21:38:35 +0000 (17:38 -0400)
committerCherry Mui <cherryyz@google.com>
Tue, 21 Mar 2023 18:44:09 +0000 (18:44 +0000)
Correct an error message to missing section, not unreachable
symbol.

Also, under -v >= 2, dump symbol info on error for debugging.

Updates #58966.

Change-Id: I0f832c517d64f4b672b313a8b9be2d028744f945
Reviewed-on: https://go-review.googlesource.com/c/go/+/476735
Run-TryBot: Cherry Mui <cherryyz@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
src/cmd/go/testdata/script/link_external_undef.txt
src/cmd/link/internal/ld/data.go
src/cmd/link/internal/ld/main.go

index d86b3a374e919d744f04227e6ec47e9744d06013..f3205054599695c5d4eb8266f1287878dec84fd2 100644 (file)
@@ -7,7 +7,7 @@
 
 ! go build -ldflags='-linkmode=external' .
 ! stderr 'panic'
-stderr '^.*unreachable sym in relocation.*'
+stderr '^.*undefined symbol in relocation.*'
 
 -- go.mod --
 
index 488cfa7a44ad995e83b748acb91f5a9e9616cfdb..bd8d17b11023f4576c8d7d7f179327f3dd912ac4 100644 (file)
@@ -444,14 +444,21 @@ func (st *relocSymState) relocsym(s loader.Sym, P []byte) {
                        if weak && !ldr.AttrReachable(rs) {
                                continue
                        }
-                       if ldr.SymSect(rs) == nil {
-                               st.err.Errorf(s, "unreachable sym in relocation: %s", ldr.SymName(rs))
+                       sect := ldr.SymSect(rs)
+                       if sect == nil {
+                               if rst == sym.SDYNIMPORT {
+                                       st.err.Errorf(s, "cannot target DYNIMPORT sym in section-relative reloc: %s", ldr.SymName(rs))
+                               } else if rst == sym.SUNDEFEXT {
+                                       st.err.Errorf(s, "undefined symbol in relocation: %s", ldr.SymName(rs))
+                               } else {
+                                       st.err.Errorf(s, "missing section for relocation target %s", ldr.SymName(rs))
+                               }
                                continue
                        }
 
                        // The method offset tables using this relocation expect the offset to be relative
                        // to the start of the first text section, even if there are multiple.
-                       if ldr.SymSect(rs).Name == ".text" {
+                       if sect.Name == ".text" {
                                o = ldr.SymValue(rs) - int64(Segtext.Sections[0].Vaddr) + r.Add()
                        } else {
                                o = ldr.SymValue(rs) - int64(ldr.SymSect(rs).Vaddr) + r.Add()
index 8511e5de63179bc7349c838d06d776d37310a9c3..9042a4db32228177c90fe1ce64ee10263452571c 100644 (file)
@@ -159,6 +159,14 @@ func Main(arch *sys.Arch, theArch Arch) {
                // dump symbol info on crash
                defer func() { ctxt.loader.Dump() }()
        }
+       if ctxt.Debugvlog > 1 {
+               // dump symbol info on error
+               AtExit(func() {
+                       if nerrors > 0 {
+                               ctxt.loader.Dump()
+                       }
+               })
+       }
 
        switch *flagHeadType {
        case "":