Skip to content

Commit 03a849f

Browse files
committed
cgo/out: include path to line macros to avoid build tmp directory being embedded
When the external linker is used the work directory of the compiler is embedded into compressed sections of the LTO objects. Making them unreproducible between builds. The culprit seems to be fact that the line macros uses bare symbol names, and GCC prepends each symbol with current directory when compiling. These files are included when building the final build id stamped into the binary, thus they should be identical between builds. Reproduction of the issue: $ CGO_CFLAGS='-flto' go build -work -trimpath -ldflags '-linkmode=external' main.go WORK=/tmp/go-build2408155205 $ cd /tmp/go-build2408155205/b001 $ objdump -h ./_x002.o | grep GoStringLen 7 .gnu.lto__GoStringLen.0.6b7d5558e3553715 0000013b 0000000000000000 0000000000000000 0000009c 2**0 $ objcopy ./_x002.o /dev/null --dump-section .gnu.lto__GoStringLen.0.6b7d5558e3553715=GoStringLen.zst $ unzstd ./GoStringLen.zst $ strings GoStringLen /tmp/go-build2408155205/b001 cgo-builtin-prolog
1 parent d15515a commit 03a849f

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

src/cmd/cgo/out.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,7 +1471,7 @@ func (p *Package) cgoType(e ast.Expr) *Type {
14711471
}
14721472

14731473
const gccProlog = `
1474-
#line 1 "cgo-gcc-prolog"
1474+
#line 1 "/cgo-gcc-prolog"
14751475
/*
14761476
If x and y are not equal, the type will be invalid
14771477
(have a negative array count) and an inscrutable error will come
@@ -1535,7 +1535,7 @@ const noTsanProlog = `
15351535
// it is more important to avoid false positives, which reduce confidence
15361536
// in the tool, than to avoid false negatives.
15371537
const yesTsanProlog = `
1538-
#line 1 "cgo-tsan-prolog"
1538+
#line 1 "/cgo-tsan-prolog"
15391539
#define CGO_NO_SANITIZE_THREAD __attribute__ ((no_sanitize_thread))
15401540
15411541
long long _cgo_sync __attribute__ ((common));
@@ -1577,7 +1577,7 @@ extern void __msan_unpoison(const volatile void *, size_t);
15771577
var msanProlog = noMsanProlog
15781578

15791579
const builtinProlog = `
1580-
#line 1 "cgo-builtin-prolog"
1580+
#line 1 "/cgo-builtin-prolog"
15811581
#include <stddef.h>
15821582
15831583
/* Define intgo when compiling with GCC. */
@@ -1766,7 +1766,7 @@ func (p *Package) cPrologGccgo() string {
17661766
}
17671767

17681768
const cPrologGccgo = `
1769-
#line 1 "cgo-c-prolog-gccgo"
1769+
#line 1 "/cgo-c-prolog-gccgo"
17701770
#include <stdint.h>
17711771
#include <stdlib.h>
17721772
#include <string.h>
@@ -1873,7 +1873,7 @@ void localCgoCheckResult(Eface val) {
18731873
// error if a Go file with a cgo comment #include's the export header
18741874
// generated by a different package.
18751875
const builtinExportProlog = `
1876-
#line 1 "cgo-builtin-export-prolog"
1876+
#line 1 "/cgo-builtin-export-prolog"
18771877
18781878
#include <stddef.h>
18791879
@@ -1906,7 +1906,7 @@ func (p *Package) gccExportHeaderProlog() string {
19061906
// difference, as Go code never wants to refer to the latter meaning.
19071907
const gccExportHeaderProlog = `
19081908
/* Start of boilerplate cgo prologue. */
1909-
#line 1 "cgo-gcc-export-header-prolog"
1909+
#line 1 "/cgo-gcc-export-header-prolog"
19101910
19111911
#ifndef GO_CGO_PROLOGUE_H
19121912
#define GO_CGO_PROLOGUE_H
@@ -1968,7 +1968,7 @@ const gccExportHeaderEpilog = `
19681968
// We use weak declarations, and test the addresses, so that this code
19691969
// works with older versions of gccgo.
19701970
const gccgoExportFileProlog = `
1971-
#line 1 "cgo-gccgo-export-file-prolog"
1971+
#line 1 "/cgo-gccgo-export-file-prolog"
19721972
extern _Bool runtime_iscgo __attribute__ ((weak));
19731973
19741974
static void GoInit(void) __attribute__ ((constructor));

0 commit comments

Comments
 (0)