aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/link/internal/ld/lib.go
AgeCommit message (Collapse)Author
2024-01-25all: prealloc slice with possible minimum capabilitiesShulhan
2024-01-22cmd/link,runtime: merge minfunc const info internal/abiqiulaidongfeng
For #59670 Change-Id: If2b05b1ba30b607b518577b0e11ba5a0b07999c5 GitHub-Last-Rev: a664aa18b5ef674dc2d05c1f7533e1974d265894 GitHub-Pull-Request: golang/go#64906 Reviewed-on: https://go-review.googlesource.com/c/go/+/553276 Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Keith Randall <khr@google.com> Auto-Submit: Keith Randall <khr@golang.org> Reviewed-by: Cherry Mui <cherryyz@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
2023-11-20all: add floating point option for ARM targetsLudi Rehak
This change introduces new options to set the floating point mode on ARM targets. The GOARM version number can optionally be followed by ',hardfloat' or ',softfloat' to select whether to use hardware instructions or software emulation for floating point computations, respectively. For example, GOARM=7,softfloat. Previously, software floating point support was limited to GOARM=5. With these options, software floating point is now extended to all ARM versions, including GOARM=6 and 7. This change also extends hardware floating point to GOARM=5. GOARM=5 defaults to softfloat and GOARM=6 and 7 default to hardfloat. For #61588 Change-Id: I23dc86fbd0733b262004a2ed001e1032cf371e94 Reviewed-on: https://go-review.googlesource.com/c/go/+/514907 Run-TryBot: Michael Knyszek <mknyszek@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com> Reviewed-by: Keith Randall <khr@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Keith Randall <khr@google.com> Auto-Submit: Michael Knyszek <mknyszek@google.com>
2023-11-09cmd/internal/link: merge .pdata and .xdata sections from host object filesqmuntal
The Go linker doesn't currently merge .pdata/.xdata sections from the host object files generated by the C compiler when using internal linking. This means that the stack can't be unwind in C -> Go. This CL fixes that and adds a test to ensure that the stack can be unwind in C -> Go and Go -> C transitions, which was not well tested. Updates #57302 Change-Id: Ie86a5e6e30b80978277e66ccc2c48550e51263c8 Reviewed-on: https://go-review.googlesource.com/c/go/+/534555 Reviewed-by: Heschi Kreinick <heschi@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Than McIntosh <thanm@google.com>
2023-08-18cmd/link: don't mangle string symbol namesCherry Mui
String symbol names could contain weird characters as we put the string literal into the symbol name. So it may appear to need mangling. However, as string symbols are grouped into a single "go:string.*" symbol, the individual symbol names actually don't matter. So don't mangle them. Also make the mangling code more defensive in case of weird symbol names. Fixes #62098. Change-Id: I533012567a9fffab69debda934f426421c7abb04 Reviewed-on: https://go-review.googlesource.com/c/go/+/520856 Reviewed-by: Than McIntosh <thanm@google.com> Run-TryBot: Cherry Mui <cherryyz@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
2023-08-16cmd/link: suppress -bind_at_load deprecation warning for ld-primeCherry Mui
ld-prime emits a deprecation warning for -bind_at_load. The flag is needed for plugins to not deadlock (#38824) when linking with older darwin linker. It is supposedly not needed with newer linker when chained fixups are used. For now, we always pass it, and suppress the warning. For #61229. Change-Id: I4b8a6f864a460c40dc38adbb533f664f7fd5343c Reviewed-on: https://go-review.googlesource.com/c/go/+/508696 Reviewed-by: Than McIntosh <thanm@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Cherry Mui <cherryyz@google.com>
2023-07-21cmd/link: print dsymutil and strip commands in -v modeCherry Mui
If -v is specified, print dsymutil and strip commands. Change-Id: Icaff2b41ab582d8c58a4ec65438c2986d88def9f Reviewed-on: https://go-review.googlesource.com/c/go/+/506758 Reviewed-by: Than McIntosh <thanm@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Cherry Mui <cherryyz@google.com>
2023-07-21cmd/link: rationalize -s and -w flags with Mach-O external linkingCherry Mui
Currently, on Mach-O in external linking mode, the handling of -s and -w flags are a bit mixed: neither flag disables the symbol table, and both flags disable DWARF. This CL makes it do what is documented: -s disables symbol table, and -w disables DWARF. For the Darwin system linker, the -s flag (strip symbol table) is obsolete. So we strip it afterwards. We already use the strip command to strip the debug STAB symbols if we need to combine DWARF. With this CL we'll use an additional flag to strip more symbols. And we now also use strip if -s is specified and we don't need to combine DWARF. Change-Id: I9bed24fd388f2bd5b0ffa4ec2db46a4a2f6b1016 Reviewed-on: https://go-review.googlesource.com/c/go/+/493136 Reviewed-by: Than McIntosh <thanm@google.com> Run-TryBot: Cherry Mui <cherryyz@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
2023-07-20cmd/link: pass flags to external linker in deterministic orderCherry Mui
Currently we may pass C linker flags in nondeterministic order, as on ELf systems we pass --export-dynamic-symbol for symbols from a map. This is usally not a big problem because even if the flags are passed in nondeterministic order the resulting binary is probably still deterministic. This CL makes it pass them in a deterministic order to be extra sure. This also helps build systems where e.g. there is a build cache for the C linking action. Change-Id: I930524dd2c3387f49d62be7ad2cef937cb2c2238 Reviewed-on: https://go-review.googlesource.com/c/go/+/509215 Reviewed-by: Than McIntosh <thanm@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Cherry Mui <cherryyz@google.com>
2023-06-09cmd/link: mangle certain instantiated function name in plugin modeCherry Mui
In plugin mode, we mangle the type symbol name so it doesn't contain characters that may confuse the external linker. With generics, instantiated function name includes type names, so it may also contain such characters and so also needs to be mangled. Fixes #58800. Change-Id: Ibb08c95b89b8a815ccef98193d3a025e9d4756cc Reviewed-on: https://go-review.googlesource.com/c/go/+/500095 Reviewed-by: Than McIntosh <thanm@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Cherry Mui <cherryyz@google.com>
2023-05-12cmd/link: fix checks for supported linker flags with relative paths.James Bartlett
The existing way of checking for supported linker flags causes false negatives when there are relative paths passed to go tool link. This fixes the issue by calling the external linker in the current working directory, instead of in a temporary directory. Fixes #59952 Change-Id: I173bb8b44902f30dacefde1c202586f87667ab70 Reviewed-on: https://go-review.googlesource.com/c/go/+/491796 Run-TryBot: Cherry Mui <cherryyz@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Cherry Mui <cherryyz@google.com>
2023-05-05cmd/link: remove elfsetstring out of the loaderCherry Mui
Currently, we pass elfsetstring to the loader as a callback, for a special case of Addstring. This is only used for ELF when adding strings to the section header string table. Move the logic to the caller instead, so the loader would not have this special case. Change-Id: Icfb91f380fe4ba435985c3019681597932f58242 Reviewed-on: https://go-review.googlesource.com/c/go/+/492718 Run-TryBot: Cherry Mui <cherryyz@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Than McIntosh <thanm@google.com>
2023-05-02cmd/link: work around dsymutils not cleaning temp fileCherry Mui
Some versions of dsymutils, notably the one in clang 14.0.3, which is shipped in some versions of Xcode, have a bug that it creates a temporary directory but doesn't clean it up at exit. The temporary directory is created in DSYMUTIL_REPRODUCER_PATH (if set, otherwise TMPDIR). Work around the issue by setting DSYMUTIL_REPRODUCER_PATH to the linker's temporary directory, so the linker will clean it up at exit anyway. Fixes #59026. Change-Id: Ie3e90a2d6a01f90040dc2eac91e8e536ccdda5a2 Reviewed-on: https://go-review.googlesource.com/c/go/+/490818 Reviewed-by: Than McIntosh <thanm@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Cherry Mui <cherryyz@google.com>
2023-05-02cmd/link: generate .xdata PE sectionqmuntal
This CL adds a .xdata section to the PE file generated by the Go linker. It is also the first CL of the SEH chain that adds effective support for unwinding the Go stack, as demonstrated by the newly added tests. The .xdata section is a standard PE section that contains an array of unwind data info structures. This structures are used to record the effects a function has on the stack pointer, and where the nonvolatile registers are saved on the stack [1]. Note that this CL still does not support unwinding the cgo stack. Updates #57302 [1] https://learn.microsoft.com/en-us/cpp/build/exception-handling-x64#struct-unwind_info Change-Id: I6f305a51ed130b758ff9ca7b90c091e50a109a6f Reviewed-on: https://go-review.googlesource.com/c/go/+/457455 Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Davis Goodin <dagood@microsoft.com> Run-TryBot: Quim Muntal <quimmuntal@gmail.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Than McIntosh <thanm@google.com>
2023-05-02cmd/link: generate .pdata PE sectionqmuntal
This CL adds a .pdata section to the PE file generated by the Go linker. The .pdata section is a standard section [1] that contains an array of function table entries that are used for stack unwinding. The table entries layout is taken from [2]. This CL just generates the table entries without any unwinding information, which is enough to start doing some E2E tests between the Go linker and the Win32 APIs. The goal of the .pdata table is to allow Windows retrieve unwind information for a function at a given PC. It does so by doing a binary search on the table, looking for an entry that meets BeginAddress >= PC < EndAddress. Each table entry takes 12 bytes and only non-leaf functions with frame pointer needs an entry on the .pdata table. The result is that PE binaries will be ~0.7% bigger due to the unwind information, a reasonable amount considering the benefits in debuggability. Updates #57302 [1] https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#the-pdata-section [2] https://learn.microsoft.com/en-us/cpp/build/exception-handling-x64#struct-runtime_function Change-Id: If675d10c64452946dbab76709da20569651e3e9f Reviewed-on: https://go-review.googlesource.com/c/go/+/461738 TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Alex Brainman <alex.brainman@gmail.com> Reviewed-by: Than McIntosh <thanm@google.com> Run-TryBot: Quim Muntal <quimmuntal@gmail.com> Reviewed-by: Cherry Mui <cherryyz@google.com>
2023-04-14cmd/link/internal/ld: disable execute-only for external linking on openbsd/arm64Joel Sing
The Go arm64 assembler places constants into the text section of a binary. OpenBSD 7.3 enabled xonly by default on OpenBSD/arm64. This means that any externally linked Go binary now segfaults. Disable execute-only when invoking the external linker on openbsd/arm64, in order to work around this issue. Updates #59615 Change-Id: I1a291293da3c6e4409b21873d066ea15e9bfe280 Reviewed-on: https://go-review.googlesource.com/c/go/+/484555 TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Aaron Bieber <deftly@gmail.com> Run-TryBot: Joel Sing <joel@sing.id.au> Reviewed-by: Than McIntosh <thanm@google.com>
2023-04-14cmd/link: establish dependable package initialization orderKeith Randall
(This is a retry of CL 462035 which was reverted at 474976. The only change from that CL is the aix fix SRODATA->SNOPTRDATA at inittask.go:141) As described here: https://github.com/golang/go/issues/31636#issuecomment-493271830 "Find the lexically earliest package that is not initialized yet, but has had all its dependencies initialized, initialize that package, and repeat." Simplify the runtime a bit, by just computing the ordering required in the linker and giving a list to the runtime. Update #31636 Fixes #57411 RELNOTE=yes Change-Id: I28c09451d6aa677d7394c179d23c2c02c503fc56 Reviewed-on: https://go-review.googlesource.com/c/go/+/478916 Reviewed-by: Than McIntosh <thanm@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Run-TryBot: Keith Randall <khr@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org>
2023-03-28Revert "cmd/link: use path from "cc --print-prog-name ar" for c-archive ↵Than McIntosh
buildmode" This reverts commit https://go-review.git.corp.google.com/c/go/+/479775 Reason for revert: fails with ios-arm64-corellium builder Change-Id: Iae61b994a39ff6c70af8a302f7a46de0097edf3e Reviewed-on: https://go-review.googlesource.com/c/go/+/479917 Auto-Submit: Than McIntosh <thanm@google.com> Run-TryBot: Than McIntosh <thanm@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com>
2023-03-28cmd/link: use path from "cc --print-prog-name ar" for c-archive buildmodeThan McIntosh
When external linking with -buildmode=c-archive, the Go linker eventually invokes the "ar" tool to create the final archive library. Prior to this patch, if the '-extar' flag was not in use, we would just run "ar". This works well in most cases but breaks down if we're doing cross-compilation targeting Windows (macos system "ar" apparently doesn't create the windows symdef section correctly). To fix the problem, capture the output of "cc --print-prog-name ar" and invoke "ar" using the path returned by that command. Fixes #59221. Change-Id: I9de66e98947c42633b16fde7208c2958d62fe7cc Reviewed-on: https://go-review.googlesource.com/c/go/+/479775 Reviewed-by: Cherry Mui <cherryyz@google.com> Run-TryBot: Than McIntosh <thanm@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
2023-03-14cmd/go,cmd/link: prefer external linking when strange cgo flags seenThan McIntosh
This patch changes the Go command to examine the set of compiler flags feeding into the C compiler when packages that use cgo are built. If any of a specific set of strange/dangerous flags are in use, then the Go command generates a token file ("preferlinkext") and embeds it into the compiled package's archive. When the Go linker reads the archives of the packages feeding into the link and detects a "preferlinkext" token, it will then use external linking for the program by default (although this default can be overridden with an explicit "-linkmode" flag). The intent here is to avoid having to teach the Go linker's host object reader to grok/understand the various odd symbols/sections/types that can result from boutique flag use, but rather to just boot the objects in question over to the C linker instead. Updates #58619. Updates #58620. Updates #58848. Change-Id: I56382dd305de8dac3841a7a7e664277826061eaa Reviewed-on: https://go-review.googlesource.com/c/go/+/475375 Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Bryan Mills <bcmills@google.com> Run-TryBot: Than McIntosh <thanm@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
2023-03-09Revert "cmd/link: establish dependable package initialization order"Keith Randall
This reverts commit ce2a609909d9de3391a99a00fe140506f724f933. aka CL 462035 Reason for revert: this CL is causing some problems in some internal Google programs. Change-Id: I4476b8d8d2c3d7b5703d1d85c93baebb4b4e5d26 Reviewed-on: https://go-review.googlesource.com/c/go/+/474976 Reviewed-by: Cherry Mui <cherryyz@google.com> Run-TryBot: Keith Randall <khr@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Than McIntosh <thanm@google.com>
2023-03-03cmd/link: establish dependable package initialization orderKeith Randall
As described here: https://github.com/golang/go/issues/31636#issuecomment-493271830 "Find the lexically earliest package that is not initialized yet, but has had all its dependencies initialized, initialize that package, and repeat." Simplify the runtime a bit, by just computing the ordering required in the linker and giving a list to the runtime. Update #31636 Fixes #57411 RELNOTE=yes Change-Id: I1e4d3878ebe6e8953527aedb730824971d722cac Reviewed-on: https://go-review.googlesource.com/c/go/+/462035 Reviewed-by: Than McIntosh <thanm@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Run-TryBot: Keith Randall <khr@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org>
2023-02-27cmd/link: check for power10 support on external linker for PPC64Paul E. Murphy
The external linker will need to support the new PC relative relocations when they are generated by Go in a future patch. If it does not, many unhelpful relocation errors will be generated by the external linker. Use the -mcpu=power10 option as a surrogate for -mpcrel. It most cases, it should indicate whether the underlying linker has support for resolving PC relative relocations. Change-Id: I84b151ce04512ccaeb17835aaf44105a5f6b515b Reviewed-on: https://go-review.googlesource.com/c/go/+/469576 TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Paul Murphy <murp@ibm.com> Reviewed-by: Archana Ravindar <aravind5@in.ibm.com> Reviewed-by: Than McIntosh <thanm@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com>
2023-02-23Revert "cmd/link: default to external linking with cgo std packages"Than McIntosh
This reverts CL 470298. Reason for revert: causes issues with Google internal testing. Updates #58619. Updates #58620. Change-Id: Ic6601820ba8758ef96b71e32d9ffc549c36d5c98 Reviewed-on: https://go-review.googlesource.com/c/go/+/470835 Reviewed-by: Russ Cox <rsc@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Than McIntosh <thanm@google.com> Reviewed-by: Than McIntosh <thanm@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com>
2023-02-22cmd/link: default to external linking with cgo std packagesCherry Mui
Previously, when a program depends on cgo packages in the standard library (e.g. net, os/user) but not otherwise use cgo, we default to internal linking mode. As we shipped pre-built cgo-using packages in Go distributions, we don't require a C compiler to build those packages. Then, by using internal linking we can link programs using those packages without requiring a C toolchain. As of Go 1.20, we stopped shipping those pre-built packages. If a user doesn't have a C toolchain, they will use the non-cgo version of the package. If they have a C toolchain, they can get cgo-using packages but they can link with the external linker as well. So there is no strong need to be able to link the cgo version of the packages without a C toolchain. This CL makes it default to external linking mode. Fixes #58619. Fixes #58620. Change-Id: I62d3744c2b82ce734813c0e303e417d85dd29868 Reviewed-on: https://go-review.googlesource.com/c/go/+/470298 Run-TryBot: Cherry Mui <cherryyz@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Than McIntosh <thanm@google.com>
2023-02-16cmd/link: don't switch to gold on ARM LinuxIan Lance Taylor
The bug in GNU ld appears to have been fixed in GNU binutils 2.28 by GNU binutils revision 5522f910cb539905d6adfdceab208ddfa5e84557. (This may have been accidental as the ChangeLog for the fix makes no reference to it; the fix is from https://sourceware.org/bugzilla/show_bug.cgi?id=19962). Continue using gold on arm64, at least for now, because as reported in issue #22040 GNU ld still fails there. For #15696 For #22040 Change-Id: I5534bb8b5680daf536a7941aba5c701e8a4138ab Reviewed-on: https://go-review.googlesource.com/c/go/+/468655 Reviewed-by: Ian Lance Taylor <iant@google.com> Run-TryBot: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Than McIntosh <thanm@google.com> Run-TryBot: Ian Lance Taylor <iant@golang.org>
2023-02-10cmd/link: try libssp_nonshared.a when looking for "__stack_chk_fail_local"Than McIntosh
Update the code that tries to satisfy unresolved references to "__stack_chk_fail_local" to look for "libssp_nonshared.a" in addition to "libc_nonshared.a" (the former archive is the correct place on Alpine). Updates #52919. Updates #54313. Updates #57261. Fixes #58385. Change-Id: Id6cd3ebb4d5388df50a838e6efa5e5b683545b01 Reviewed-on: https://go-review.googlesource.com/c/go/+/466936 Run-TryBot: Than McIntosh <thanm@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
2023-01-31cmd/link: add debug print in hostobjCopyCherry Mui
In external linking mode, when the external linker fails to handle something in a host object file, it usually reports the name of the host object which is a copied file named 000NNN.o. This is often not helpful to determine what file it is. Add some debug print so at least in -v mode it is more helpful. Change-Id: Ibe762bff6a25640d16ee0dc082736ba5161b7522 Reviewed-on: https://go-review.googlesource.com/c/go/+/458735 Reviewed-by: Than McIntosh <thanm@google.com> Run-TryBot: Cherry Mui <cherryyz@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
2023-01-31cmd/link: rename ELF struct fields to remove "elf" prefixIan Lance Taylor
While we're here rename setupplt to setupPLT. This is a pure naming change with no semantic change. Change-Id: Ib0312fb6568475b620dab7632438b4d25e4d9cc0 Reviewed-on: https://go-review.googlesource.com/c/go/+/463744 Reviewed-by: Than McIntosh <thanm@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> Auto-Submit: Ian Lance Taylor <iant@golang.org>
2023-01-31cmd/link: load host archive libc_nonshared.a for -fstack-protectorThan McIntosh
For internal linking, at the point where we finish reading libgcc.a, if the symbol "__stack_chk_local" is still undefined, then read in the host archive libc_nonshared.a as well. Updates #57261. Change-Id: I0b1e485aa50aa7940db8cabcb3b9a7959bf99ce7 Reviewed-on: https://go-review.googlesource.com/c/go/+/456856 Reviewed-by: Cherry Mui <cherryyz@google.com> Run-TryBot: Than McIntosh <thanm@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
2023-01-30cmd/link: refactor ELF hooks into ELFArch structIan Lance Taylor
This is a pure cleanup to bring the ELF hooks together. Change-Id: I01d5227c70f30e4a659dcd7904e7c247266e95b1 Reviewed-on: https://go-review.googlesource.com/c/go/+/463981 Auto-Submit: Ian Lance Taylor <iant@golang.org> Reviewed-by: Than McIntosh <thanm@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Joel Sing <joel@sing.id.au>
2023-01-30all: fix problematic commentscui fliter
Change-Id: If092ae7c72b66f172ae32fa6c7294a7ac250362e Reviewed-on: https://go-review.googlesource.com/c/go/+/463995 Reviewed-by: Cherry Mui <cherryyz@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Than McIntosh <thanm@google.com> Run-TryBot: Than McIntosh <thanm@google.com>
2023-01-25cmd/link: don't export all symbols for ELF external linkingIan Lance Taylor
Since this may add a large number of --export-dynamic-symbol options, use a response file if the command line gets large. Fixes #53579 Change-Id: Ic226bf372bf1e177a3dae886d1c48f4ce3569c0e Reviewed-on: https://go-review.googlesource.com/c/go/+/414654 Reviewed-by: Michael Pratt <mpratt@google.com> Reviewed-by: Joedian Reid <joedian@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> Auto-Submit: Ian Lance Taylor <iant@golang.org> Reviewed-by: Than McIntosh <thanm@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
2023-01-24cmd/link: remove importcycles functionJeremy Quirke
The importcycles method has not been useful since April 2016 when a large code deletion was performed. The compiler itself provides some protection against import cycles, and the linker does import cycle detection in linksetup -> postorder. For #57400 Change-Id: I3095bdb3f16a82ba25681bf4a20ceaa3c9613921 GitHub-Last-Rev: 87a46153b136db67675874ffe46e5881b9c756ce GitHub-Pull-Request: golang/go#57462 Reviewed-on: https://go-review.googlesource.com/c/go/+/459475 TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Than McIntosh <thanm@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Run-TryBot: Cherry Mui <cherryyz@google.com>
2022-12-06cmd/link: pass -Wl,--no-insert-timestamp to external linker on windowsThan McIntosh
Pass -Wl,--no-insert-timestamp to the external linker on windows, so as to suppress generation of the PE file header data/time stamp. This is in order to make it possible to get reproducible CGO builds on windows (note that we already zero the timestamp field in question for internal linkage). Updates #35006. Change-Id: I3d69cf1fd32e099bd9bb4b0431a4c5f43e4b08f3 Reviewed-on: https://go-review.googlesource.com/c/go/+/455535 Run-TryBot: Than McIntosh <thanm@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
2022-11-19cmd/link: revise DLL import symbol handlingThan McIntosh
This patch reworks the handling of DLL import symbols in the PE host object loader to ensure that the Go linker can deal with them properly during internal linking. Prior to this point the strategy was to immediately treat an import symbol reference of the form "__imp__XXX" as if it were a reference to the corresponding DYNIMPORT symbol XXX, except for certain special cases. This worked for the most part, but ran into problems in situations where the target ("XXX") wasn't a previously created DYNIMPORT symbol (and when these problems happened, the root cause was not always easy to see). The new strategy is to not do any renaming or forwarding immediately, but to delay handling until host object loading is complete. At that point we make a scan through the newly introduced text+data sections looking at the relocations that target import symbols, forwarding the references to the corresponding DYNIMPORT sym where appropriate and where there are direct refs to the DYNIMPORT syms, tagging them for stub generation later on. Updates #35006. Updates #53540. Change-Id: I2d42b39141ae150a9f82ecc334001749ae8a3b4a Reviewed-on: https://go-review.googlesource.com/c/go/+/451738 Reviewed-by: Cherry Mui <cherryyz@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: David Chase <drchase@google.com> Run-TryBot: Than McIntosh <thanm@google.com>
2022-11-19cmd/link: add capturehostobjs debugging flagThan McIntosh
Add a new debugging flag "-capturehostobjs" that instructs the linker to capture copies of all object files loaded in during the host object loading portion of CGO internal linking. The intent is to make it easier to analyze the objects after the fact (as opposed to having to dig around inside archives, which can be a "find needle in haystack" exercise). Change-Id: I7023a5b72b1b899ea9b3bd6501f069d1f21bbaf0 Reviewed-on: https://go-review.googlesource.com/c/go/+/451737 Run-TryBot: Than McIntosh <thanm@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: David Chase <drchase@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com>
2022-11-19cmd/link: improved host archive debug trace outputThan McIntosh
When ctxt.Debugvlog > 1, produce additional trace output to describe which object files are being pulled out of host archive libraries and why they were pulled (e.g. which symbol had a reference to something in a library). Intended to make it easier to debug problems with cgo internal linking. Change-Id: Icd64aff244b9145162a00cb51642ef32f26adfba Reviewed-on: https://go-review.googlesource.com/c/go/+/451736 Reviewed-by: Cherry Mui <cherryyz@google.com> Run-TryBot: Than McIntosh <thanm@google.com> Reviewed-by: David Chase <drchase@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
2022-11-11cmd/link: exit if runtime.a is missingCherry Mui
Many stages in the linker assume some runtime symbols exist. Error out if the runtime package cannot be found. Fixes #56685. Change-Id: I39b2663f6d7419688e30e6d8650e5eb8fc43a2b1 Reviewed-on: https://go-review.googlesource.com/c/go/+/449638 Reviewed-by: Than McIntosh <thanm@google.com> Run-TryBot: Cherry Mui <cherryyz@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
2022-10-14cmd/link: return correct default linker for the platformDmitri Goutnik
If no external linker was passed with -extld, link currently assumes that it is "gcc" which is not correct for platforms that use clang toolchain. Return "clang" for platforms that use it, this fixes dir tests on freebsd/riscv64. For #53466 Change-Id: Ie3bce1b9581839d0b3b2129908355cd30ae9a713 Reviewed-on: https://go-review.googlesource.com/c/go/+/432756 Run-TryBot: Cherry Mui <cherryyz@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Joel Sing <joel@sing.id.au> Reviewed-by: Mikaël Urankar <mikael.urankar@gmail.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Joedian Reid <joedian@golang.org>
2022-10-04runtime/race: use internal linking mode for amd64 subarch packagesFlorian Zenker
CL 424034 introduced two new packages that trigger external linking mode where internal linking mode is sufficient. Change-Id: I81583210331fe4151d631b5efd7fc5d3c8f11f39 Reviewed-on: https://go-review.googlesource.com/c/go/+/435256 Run-TryBot: Than McIntosh <thanm@google.com> Reviewed-by: Than McIntosh <thanm@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
2022-09-20all: replace package ioutil with os and io in srcAndy Pan
For #45557 Change-Id: I56824135d86452603dd4ed4bab0e24c201bb0683 Reviewed-on: https://go-review.googlesource.com/c/go/+/426257 Run-TryBot: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Run-TryBot: Andy Pan <panjf2000@gmail.com> Reviewed-by: Cherry Mui <cherryyz@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com>
2022-09-16cmd/link: suppress -no_pie deprecation warning on darwinCherry Mui
Apparently the new darwin linker starts to emit a warning about -no_pie deprecation. Maybe we want to switch to PIE by default. For now, suppress the warning. This also makes it easier for backporting to previous releases. For #54482. Change-Id: I1a3b74c237a9d00ec3b030fc3a9940a31e5cd37e Reviewed-on: https://go-review.googlesource.com/c/go/+/430937 Run-TryBot: Cherry Mui <cherryyz@google.com> Reviewed-by: Than McIntosh <thanm@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
2022-09-15cmd/link: stop passing -pagezero_size to darwin linkerCherry Mui
We added -pagezero_size in CL 72730, where it was intented for iOS. The current code passes it only on macOS/AMD64 instead. It is not really necessary there. Also, the new darwin linker starts to emit a warning about deprecation of the flag. Stop passing it. For #54482. Change-Id: If9db7a1645c37d4284e48f075856912df8d8c1a0 Reviewed-on: https://go-review.googlesource.com/c/go/+/430936 TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Than McIntosh <thanm@google.com> Run-TryBot: Cherry Mui <cherryyz@google.com>
2022-09-13cmd/link: generate an error if the entry symbol cannot be foundJoel Sing
If the entry symbol cannot be found (for example, a new port is being brought up and no rt0 code has been provided), the linker will currently panic. Rather than panicing, generate an error that aids in debugging: missing entry symbol "_rt0_arm64_openbsd" Change-Id: I9cc38eaab48f730d596ca7fa9e9e3d68250ae4d3 Reviewed-on: https://go-review.googlesource.com/c/go/+/430555 Auto-Submit: Jenny Rakoczy <jenny@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Jenny Rakoczy <jenny@golang.org> Run-TryBot: Cherry Mui <cherryyz@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com>
2022-08-09all: use ":" for compiler generated symbolsCuong Manh Le
As it can't appear in user package paths. There is a hack for handling "go:buildid" and "type:*" on windows/386. Previously, windows/386 requires underscore prefix on external symbols, but that's only applied for SHOSTOBJ/SUNDEFEXT or cgo export symbols. "go.buildid" is STEXT, "type.*" is STYPE, thus they are not prefixed with underscore. In external linking mode, the external linker can't resolve them as external symbols. But we are lucky that they have "." in their name, so the external linker see them as Forwarder RVA exports. See: - https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#export-address-table - https://sourceware.org/git/?p=binutils-gdb.git;a=blob;f=ld/pe-dll.c;h=e7b82ba6ffadf74dc1b9ee71dc13d48336941e51;hb=HEAD#l972) This CL changes "." to ":" in symbols name, so theses symbols can not be found by external linker anymore. So a hacky way is adding the underscore prefix for these 2 symbols. I don't have enough knowledge to verify whether adding the underscore for all STEXT/STYPE symbols are fine, even if it could be, that would be done in future CL. Fixes #37762 Change-Id: I92eaaf24c0820926a36e0530fdb07b07af1fcc35 Reviewed-on: https://go-review.googlesource.com/c/go/+/317917 Reviewed-by: Than McIntosh <thanm@google.com> Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com> Reviewed-by: Cherry Mui <cherryyz@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
2022-08-08cmd/link: detect glibc vs musl ldso at link timeRuss Cox
Doing the test at link time lets us distribute one Linux toolchain that works on both glibc-based and musl-based Linux systems. The old way built a toolchain that only ran on one or the other. Fixes #54197. Change-Id: Iaae8c274c78e1091eee828a720b49646be9bfffe Reviewed-on: https://go-review.googlesource.com/c/go/+/420774 Auto-Submit: Russ Cox <rsc@golang.org> Reviewed-by: Cherry Mui <cherryyz@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Russ Cox <rsc@golang.org>
2022-07-07cmd/link: explicitly disable PIE for windows/amd64 -race modeThan McIntosh
Turn off PIE explicitly for windows/amd64 when -race is in effect, since at the moment the race detector runtime doesn't seem to handle PIE binaries correctly. Note that newer C compilers on windows produce PIE binaries by default, so the Go linker needs to explicitly turn off PIE when invoking the external linker in this case. Updates #53539. Change-Id: Ib990621f22cf61a5fa383584bab81d3dfd7552e8 Reviewed-on: https://go-review.googlesource.com/c/go/+/415676 Reviewed-by: Cherry Mui <cherryyz@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Than McIntosh <thanm@google.com> Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2022-06-27cmd/link: link against libsynchronization.a for -race on windowsThan McIntosh
As of LLVM rev 41cb504b7c4b18ac15830107431a0c1eec73a6b2, the race detector runtime now refers to things in the windows synchronization library, hence when doing windows internal linking, at that library to the list of host archives that we visit. The tsan code that makes the reference is here: https://github.com/llvm/llvm-project/blob/41cb504b7c4b18ac15830107431a0c1eec73a6b2/compiler-rt/lib/sanitizer_common/sanitizer_win.cpp#L48 https://github.com/llvm/llvm-project/blob/41cb504b7c4b18ac15830107431a0c1eec73a6b2/compiler-rt/lib/sanitizer_common/sanitizer_win.cpp#L834 Note that libsynchronization.a is not guaranteed to be available on all windows systems, so in the external linking case, check for its existence before adding "-lsynchronization" to the external linker args. Updates #53539. Change-Id: I433c95c869915693d59e9c1082d5b8a11da1fc8c Reviewed-on: https://go-review.googlesource.com/c/go/+/413817 Reviewed-by: Cherry Mui <cherryyz@google.com> Run-TryBot: Than McIntosh <thanm@google.com>
2022-06-22cmd/go, cmd/link: support failure to create _cgo_import.goIan Lance Taylor
For a package that uses cgo, the file _cgo_import.go is created to record information required for internal linking: the non-Go dynamic symbols and libraries that the package depends on. Generating this information sometimes fails, because it can require recreating all the dependencies of all transitively imported packages. And the information is rarely needed, since by default we use external linking when there are packages outside of the standard library that use cgo. With this CL, if generating _cgo_import.go fails, we don't report an error. Instead, we mark the package as requiring external linking, by adding an empty file named "dynimportfail" into the generated archive. If the linker sees a file with that name, it rejects an attempt to use internal linking. Fixes #52863 Change-Id: Ie586e6753a5b67e49bb14533cd7603d9defcf0ea Reviewed-on: https://go-review.googlesource.com/c/go/+/413460 Run-TryBot: Ian Lance Taylor <iant@golang.org> Reviewed-by: Bryan Mills <bcmills@google.com> Auto-Submit: Ian Lance Taylor <iant@golang.org> Reviewed-by: Cherry Mui <cherryyz@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>