Skip to content

Commit 8adc1e0

Browse files
committed
cmd/link: skip symbol references when looking for missing symbols
ErrorUnresolved attempts to find the missing symbol in another ABI, in order to provide more friendly error messages. However, in doing so it checks the same ABI and can find the symbol reference for the symbol that it is currently reporting the unresolved error for. Avoid this by ignoring SXREF symbols, which is the same behaviour used when linking is performed. Fixes #33979 Change-Id: I3cb2477b2ad4baa7c2007323b983eb29404b0aac Reviewed-on: https://go-review.googlesource.com/c/go/+/192597 TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Austin Clements <[email protected]> Run-TryBot: Joel Sing <[email protected]>
1 parent 3a6cd4c commit 8adc1e0

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func (ctxt *Link) ErrorUnresolved(s *sym.Symbol, r *sym.Reloc) {
136136
if v == -1 {
137137
continue
138138
}
139-
if rs := ctxt.Syms.ROLookup(r.Sym.Name, v); rs != nil && rs.Type != sym.Sxxx {
139+
if rs := ctxt.Syms.ROLookup(r.Sym.Name, v); rs != nil && rs.Type != sym.Sxxx && rs.Type != sym.SXREF {
140140
haveABI = abi
141141
}
142142
}

src/cmd/link/link_test.go

+79
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,85 @@ main.x: relocation target main.zero not defined
172172
}
173173
}
174174

175+
func TestIssue33979(t *testing.T) {
176+
testenv.MustHaveGoBuild(t)
177+
testenv.MustHaveCGO(t)
178+
179+
tmpdir, err := ioutil.TempDir("", "unresolved-")
180+
if err != nil {
181+
t.Fatalf("failed to create temp dir: %v", err)
182+
}
183+
defer os.RemoveAll(tmpdir)
184+
185+
write := func(name, content string) {
186+
err := ioutil.WriteFile(filepath.Join(tmpdir, name), []byte(content), 0666)
187+
if err != nil {
188+
t.Fatal(err)
189+
}
190+
}
191+
192+
run := func(name string, args ...string) string {
193+
cmd := exec.Command(name, args...)
194+
cmd.Dir = tmpdir
195+
out, err := cmd.CombinedOutput()
196+
if err != nil {
197+
t.Fatalf("'go %s' failed: %v, output: %s", strings.Join(args, " "), err, out)
198+
}
199+
return string(out)
200+
}
201+
runGo := func(args ...string) string {
202+
return run(testenv.GoToolPath(t), args...)
203+
}
204+
205+
// Test object with undefined reference that was not generated
206+
// by Go, resulting in an SXREF symbol being loaded during linking.
207+
// Because of issue #33979, the SXREF symbol would be found during
208+
// error reporting, resulting in confusing error messages.
209+
210+
write("main.go", `package main
211+
func main() {
212+
x()
213+
}
214+
func x()
215+
`)
216+
// The following assembly must work on all architectures.
217+
write("x.s", `
218+
TEXT ·x(SB),0,$0
219+
CALL foo(SB)
220+
RET
221+
`)
222+
write("x.c", `
223+
void undefined();
224+
225+
void foo() {
226+
undefined();
227+
}
228+
`)
229+
230+
cc := strings.TrimSpace(runGo("env", "CC"))
231+
cflags := strings.Fields(runGo("env", "GOGCCFLAGS"))
232+
233+
// Compile, assemble and pack the Go and C code.
234+
runGo("tool", "asm", "-gensymabis", "-o", "symabis", "x.s")
235+
runGo("tool", "compile", "-symabis", "symabis", "-p", "main", "-o", "x1.o", "main.go")
236+
runGo("tool", "asm", "-o", "x2.o", "x.s")
237+
run(cc, append(cflags, "-c", "-o", "x3.o", "x.c")...)
238+
runGo("tool", "pack", "c", "x.a", "x1.o", "x2.o", "x3.o")
239+
240+
// Now attempt to link using the internal linker.
241+
cmd := exec.Command(testenv.GoToolPath(t), "tool", "link", "-linkmode=internal", "x.a")
242+
cmd.Dir = tmpdir
243+
out, err := cmd.CombinedOutput()
244+
if err == nil {
245+
t.Fatalf("expected link to fail, but it succeeded")
246+
}
247+
got := string(out)
248+
want := "main(.text): relocation target undefined not defined\n"
249+
if !strings.Contains(got, want) {
250+
t.Fatalf("got:\n%swant:\n%s", got, want)
251+
}
252+
}
253+
175254
func TestBuildForTvOS(t *testing.T) {
176255
testenv.MustHaveCGO(t)
177256
testenv.MustHaveGoBuild(t)

0 commit comments

Comments
 (0)