]> Cypherpunks repositories - gostls13.git/commit
cmd/link: permit a larger size BSS reference to a smaller DATA symbol
authorCherry Mui <cherryyz@google.com>
Thu, 26 Jun 2025 19:46:31 +0000 (15:46 -0400)
committerCherry Mui <cherryyz@google.com>
Fri, 27 Jun 2025 02:49:36 +0000 (19:49 -0700)
commit0f8ab2db177baee7b04182f5641693df3b212aa9
tree7376d6aed339c591d175c968fcd26362712f141b
parent988a20c8c5e2c9eb49f8749e5ee94ce3c964fe59
cmd/link: permit a larger size BSS reference to a smaller DATA symbol

Currently, if there is a BSS reference and a DATA symbol
definition with the same name, we pick the DATA symbol, as it
contains the right content. In this case, if the BSS reference
has a larger size, we error out, because it is not safe to access
a smaller symbol as if it has a larger size.

Sometimes code declares a global variable in Go and defines it
in assembly with content. They are expected to be of the same
size. However, in ASAN mode, we insert a red zone for the variable
on the Go side, making it have a larger size, whereas the assembly
symbol is unchanged. This causes the Go reference (BSS) has a
larger size than the assembly definition (DATA). It results in an
error currently. This code is valid and safe, so we should permit
that.

We support this case by increasing the symbol size to match the
larger size (of the BSS side). The symbol content (from the DATA
side) is kept. In some sense, we merge the two symbols. When
loading symbols, it is not easy to change its size (as the object
file may be mapped read-only), so we add it to a fixup list, and
fix it up later after all Go symbols are loaded. This is a very
rare case, so the list should not be long.

We could limit this to just ASAN mode. But it seems okay to allow
this in general. As long as the symbol has the larger size, it is
safe to access it with the larger size.

Fixes #74314.

Change-Id: I3ee6e46161d8f59500e2b81befea11e563355a57
Reviewed-on: https://go-review.googlesource.com/c/go/+/684236
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drchase@google.com>
src/cmd/cgo/internal/testsanitizers/asan_test.go
src/cmd/cgo/internal/testsanitizers/cc_test.go
src/cmd/cgo/internal/testsanitizers/testdata/asan_global_asm/asm.s [new file with mode: 0644]
src/cmd/cgo/internal/testsanitizers/testdata/asan_global_asm/main.go [new file with mode: 0644]
src/cmd/cgo/internal/testsanitizers/testdata/asan_global_asm2_fail/asm.s [new file with mode: 0644]
src/cmd/cgo/internal/testsanitizers/testdata/asan_global_asm2_fail/main.go [new file with mode: 0644]
src/cmd/link/internal/loader/loader.go