Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

cmd/cgo: enable reproducible builds when LTO is enabled #53528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Foxboron wants to merge 1 commit into golang:master
base: master
Choose a base branch
Loading
from Foxboron:morten/repro-lto
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/cmd/go/internal/work/exec.go
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package work

import (
"bufio"
"bytes"
"context"
"crypto/sha256"
Expand Down Expand Up @@ -2508,6 +2509,38 @@ func (b *Builder) gfortran(a *Action, p *load.Package, workdir, out string, flag
return b.ccompile(a, p, out, flags, ffile, b.gfortranCmd(p.Dir, workdir))
}

// bareLineSymbols returns any C #line macros inserted by cmd/cgo with just a name and no path.
func bareLineSymbols(file string) ([][]byte, error) {
var symbols [][]byte
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
bufr := bufio.NewReader(f)
for {
line, _, err := bufr.ReadLine()
if err == io.EOF {
break
} else if err != nil {
return nil, fmt.Errorf("failed to read line macros from cgo2.c file: %v", err)
}
if !bytes.HasPrefix(line, []byte("#line")) {
continue
}
s := bytes.Split(line, []byte(` `))
if len(s) != 3 {
continue
}
symbol := bytes.Trim(s[2], `"`)
if !bytes.HasPrefix(symbol, []byte("cgo")) {
continue
}
symbols = append(symbols, symbol)
}
return symbols, nil
}

// ccompile runs the given C or C++ compiler and creates an object from a single source file.
func (b *Builder) ccompile(a *Action, p *load.Package, outfile string, flags []string, file string, compiler []string) error {
file = mkAbs(p.Dir, file)
Expand Down Expand Up @@ -2548,6 +2581,22 @@ func (b *Builder) ccompile(a *Action, p *load.Package, outfile string, flags []s
if p, ok := a.nonGoOverlay[overlayPath]; ok {
overlayPath = p
}

// GCC embeds the working directory to bare symbols in line macros
// when LTO is used on cgo2.c files.
// See go.dev/issue/53528
if strings.HasSuffix(file, "cgo2.c") && !cfg.BuildN {
lineSymbols, err := bareLineSymbols(overlayPath)
if err != nil {
return err
}
// The complexity of checking for duplicate symbols isn't worth it
// as duplicate prefix-maps doesn't affect the output.
for _, line := range lineSymbols {
flags = append(flags, fmt.Sprintf("-fdebug-prefix-map=%s=/tmp/go-build/%s", line, line))
}
}

output, err := b.runOut(a, filepath.Dir(overlayPath), b.cCompilerEnv(), compiler, flags, "-o", outfile, "-c", filepath.Base(overlayPath))
if len(output) > 0 {
// On FreeBSD 11, when we pass -g to clang 3.8 it
Expand Down
33 changes: 33 additions & 0 deletions src/cmd/go/testdata/script/build_issue413974.txt
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Regression test for https://go.dev/issue/53528:
# cgo build should reproduce binaries with LTO enable

[!cgo] skip
[short] skip 'links cgo binaries'

env GOFLAGS=-ldflags=-linkmode=external
env CGO_CFLAGS=-flto

go build -o main.exe
mv main.exe main1.exe

env GOCACHE=$WORK${/}gocache
mkdir $GOCACHE
go build -o main.exe
mv main.exe main2.exe

cmp -q main2.exe main1.exe

-- go.mod --
module main

go 1.18
-- main.go --
package main

import "C"

var _ C.int

func main() {
println("Hello World")
}

AltStyle によって変換されたページ (->オリジナル) /