go: cmd/link: improve error for missing method body

The language permits omitting the method body. However, the error message for this could be clearer.

Test case (uses import "C" to force the compiler to assume that method bodies are defined elsewhere):

package main

import "C"

type S struct{}

func (S) M()

var Sink interface{}

func main() {
	Sink = S{}
}

Running go build foo.go produces

type.main.S: relocation target main.S.M not defined

This error message is accurate but conveys little information to someone unfamiliar with linker jargon like “relocation target.” We should instead say “missing function body”, though unfortunately it may be difficult to give a good file/line position for the declaration with the missing body.

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Comments: 20 (10 by maintainers)

Most upvoted comments

An example of assembly code referring to a variable is runtime/asm_amd64.s. These lines assign to the variables _rt0_amd64_lib_argc and _rt0_amd64_lib_argv.

	MOVQ	DI, _rt0_amd64_lib_argc<>(SB)
	MOVQ	SI, _rt0_amd64_lib_argv<>(SB)

Those variables are defined in the same assembly file using:

DATA _rt0_amd64_lib_argc<>(SB)/8, $0
GLOBL _rt0_amd64_lib_argc<>(SB),NOPTR, $8
DATA _rt0_amd64_lib_argv<>(SB)/8, $0
GLOBL _rt0_amd64_lib_argv<>(SB),NOPTR, $8

If those DATA and GLOBL lines are removed, you will have assembly code that refers to undefined variables.

I don’t know. I think it is important to print the undefined symbol name, i.e. the main.S.M in the original post. But I don’t know the exact wording.

This issue is not about gccgo. This is about the Go toolchain distributed from https://golang.org/dl/ , also known as “the gc toolchain”.

@deepto98 Please include plain text messages as plain text, not as an image. Images are very hard to read. Thanks.

The error in the image appears to be from the external linker. Unfortunately I don’t know that there is much that we can do about this when in external linking mode. But I also don’t know why you didn’t see the same error that I did. What operating system are you on? I am on amd64-linux.

@deepto98 thanks. The error is emitted at https://cs.opensource.google/go/go/+/master:src/cmd/link/internal/ld/errors.go;l=63 Feel free to send a CL when you ready. (But note that I’m still unsure what the best solution is.)