]> Cypherpunks.ru repositories - gostls13.git/commit
cmd/compile,cmd/link: resolve cgo symbols to the correct Go ABI
authorAustin Clements <austin@google.com>
Sun, 11 Apr 2021 19:44:25 +0000 (15:44 -0400)
committerAustin Clements <austin@google.com>
Tue, 13 Apr 2021 20:07:47 +0000 (20:07 +0000)
commit69262d48717771cedb1da86563eb3f1b094b4e92
tree9f5a1720a907b8b18ed796b28e68f716011bc293
parent48531da9e706a9f3cf5e08319202e97022fb6813
cmd/compile,cmd/link: resolve cgo symbols to the correct Go ABI

Currently, Go functions exported to cgo have some confusion around
ABIs that leads to crashes. The cmd/cgo-generated C code references an
exported Go wrapper function (which calls the underlying exported user
function). The linker resolves this reference to the ABI0 entry-point
to that Go wrapper function because all host object references are
currently assumed to be to version 0 of a symbol. This gets passed via
crosscall2 and winds its way to cgocallbackg1, which puts this ABI0
entry-point into a function value and calls it. Unfortunately,
function values always use the ABIInternal calling convention, so
calling this ABI0 entry-point goes poorly.

Fix this by threading definition ABIs through the cgo export mechanism
so the linker can resolve host object references (which have no
concept of multiple ABIs) to the correct Go symbol. This involves a
few pieces:

- The compiler extends the cgo_export_{static,dynamic} directives that
  get passed on to the linker with symbol definition ABIs.

- The linker parses the ABIs in the cgo_export_{static,dynamic}
  directives to look up the right symbol to apply export attributes to
  and put in the dynexp list.

- For internal linking, the linker's Loader structure tracks the right
  symbol (in particular the right ABI) to resolve host object
  references to, and we use this in all of the host object loaders.

- For external linking, we mangle only the non-ABIInternal symbols
  now, so the external linker is able to resolve the correct reference
  from host objects to Go symbols.

Updates #40724.

Change-Id: I70a0b1610596768c3f473745fa1a3e630afbf1a8
Reviewed-on: https://go-review.googlesource.com/c/go/+/309341
Trust: Austin Clements <austin@google.com>
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
Reviewed-by: Than McIntosh <thanm@google.com>
src/cmd/compile/internal/ssagen/abi.go
src/cmd/link/internal/ld/go.go
src/cmd/link/internal/ld/macho.go
src/cmd/link/internal/ld/symtab.go
src/cmd/link/internal/loadelf/ldelf.go
src/cmd/link/internal/loader/loader.go
src/cmd/link/internal/loadmacho/ldmacho.go
src/cmd/link/internal/loadpe/ldpe.go
src/cmd/link/internal/loadxcoff/ldxcoff.go