|
| 1 | +[short] skip |
| 2 | +[!cgo] skip |
| 3 | + |
| 4 | +# Set up fresh GOCACHE |
| 5 | +env GOCACHE=$WORK/gocache |
| 6 | +mkdir $GOCACHE |
| 7 | + |
| 8 | +# 1. unset GOROOT_FINAL, Build a simple binary with cgo by origin go. |
| 9 | +# The DW_AT_comp_dir of runtime/cgo should have a prefix with origin goroot. |
| 10 | +env GOROOT_FINAL= |
| 11 | +# If using "go run", it is no debuginfo in binary. So use "go build". |
| 12 | +# And we can check the stderr to judge if the cache of "runtime/cgo" |
| 13 | +# was used or not. |
| 14 | +go build -o binary.exe |
| 15 | +exec ./binary.exe $TESTGO_GOROOT |
| 16 | +stdout 'cgo DW_AT_comp_dir is right in binary' |
| 17 | + |
| 18 | + |
| 19 | +# 2. GOROOT_FINAL will be changed, the runtime/cgo will be rebuild. |
| 20 | +env GOROOT_FINAL=$WORK/gorootfinal |
| 21 | +go build -x -o binary.exe |
| 22 | +stderr '(clang|gcc)( |\.exe).*gcc_.*\.c' |
| 23 | +exec ./binary.exe $GOROOT_FINAL |
| 24 | +stdout 'cgo DW_AT_comp_dir is right in binary' |
| 25 | + |
| 26 | + |
| 27 | +[!symlink] skip |
| 28 | + |
| 29 | +# Symlink the compiler to another path |
| 30 | +env GOROOT=$WORK/goroot |
| 31 | +symlink $GOROOT -> $TESTGO_GOROOT |
| 32 | + |
| 33 | +# 3. GOROOT_FINAL is same with 2, build with the other go |
| 34 | +# the runtime/cgo will not be rebuild. |
| 35 | +go build -x -o binary.exe |
| 36 | +! stderr '(clang|gcc)( |\.exe).*gcc_.*\.c' |
| 37 | +exec ./binary.exe $GOROOT_FINAL |
| 38 | +stdout 'cgo DW_AT_comp_dir is right in binary' |
| 39 | + |
| 40 | + |
| 41 | +# 4. unset GOROOT_FINAL, build with the other go |
| 42 | +# the runtime/cgo will be rebuild. |
| 43 | +env GOROOT_FINAL= |
| 44 | +go build -x -o binary.exe |
| 45 | +stderr '(clang|gcc)( |\.exe).*gcc_.*\.c' |
| 46 | +exec ./binary.exe $GOROOT |
| 47 | +stdout 'cgo DW_AT_comp_dir is right in binary' |
| 48 | + |
| 49 | +-- go.mod -- |
| 50 | +module main |
| 51 | + |
| 52 | +go 1.18 |
| 53 | +-- main.go -- |
| 54 | +package main |
| 55 | + |
| 56 | +import "C" |
| 57 | +import ( |
| 58 | + "debug/dwarf" |
| 59 | + "fmt" |
| 60 | + "log" |
| 61 | + "os" |
| 62 | + "path/filepath" |
| 63 | + "strings" |
| 64 | +) |
| 65 | + |
| 66 | +var _ C.int |
| 67 | + |
| 68 | +func main() { |
| 69 | + dwarfData, err := readDWARF(os.Args[0]) |
| 70 | + if err != nil { |
| 71 | + log.Fatal(err) |
| 72 | + } |
| 73 | + goroot := filepath.Join(os.Args[1], "src") |
| 74 | + dwarfReader := dwarfData.Reader() |
| 75 | + cgopackage := filepath.Join("runtime", "cgo") |
| 76 | + var hascgo bool |
| 77 | + for { |
| 78 | + e, err := dwarfReader.Next() |
| 79 | + if err != nil { |
| 80 | + log.Fatal(err) |
| 81 | + } |
| 82 | + if e == nil { |
| 83 | + break |
| 84 | + } |
| 85 | + field := e.AttrField(dwarf.AttrCompDir) |
| 86 | + if field == nil { |
| 87 | + continue |
| 88 | + } |
| 89 | + compdir := field.Val.(string) |
| 90 | + if strings.HasSuffix(compdir, cgopackage) { |
| 91 | + hascgo = true |
| 92 | + if !strings.HasPrefix(compdir, goroot) { |
| 93 | + fmt.Printf("cgo DW_AT_comp_dir %s contains incorrect path in binary.\n", compdir) |
| 94 | + return |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + if hascgo { |
| 99 | + fmt.Println("cgo DW_AT_comp_dir is right in binary") |
| 100 | + } else { |
| 101 | + fmt.Println("binary does not contain cgo") |
| 102 | + } |
| 103 | +} |
| 104 | +-- read_darwin.go -- |
| 105 | +package main |
| 106 | + |
| 107 | +import ( |
| 108 | + "debug/dwarf" |
| 109 | + "debug/macho" |
| 110 | +) |
| 111 | + |
| 112 | +func readDWARF(exePath string) (*dwarf.Data, error) { |
| 113 | + machoFile, err := macho.Open(exePath) |
| 114 | + if err != nil { |
| 115 | + return nil, err |
| 116 | + } |
| 117 | + defer machoFile.Close() |
| 118 | + return machoFile.DWARF() |
| 119 | +} |
| 120 | +-- read_elf.go -- |
| 121 | +// +build android dragonfly freebsd illumos linux netbsd openbsd solaris |
| 122 | + |
| 123 | +package main |
| 124 | + |
| 125 | +import ( |
| 126 | + "debug/dwarf" |
| 127 | + "debug/elf" |
| 128 | +) |
| 129 | + |
| 130 | +func readDWARF(exePath string) (*dwarf.Data, error) { |
| 131 | + elfFile, err := elf.Open(exePath) |
| 132 | + if err != nil { |
| 133 | + return nil, err |
| 134 | + } |
| 135 | + defer elfFile.Close() |
| 136 | + return elfFile.DWARF() |
| 137 | +} |
| 138 | +-- read_windows.go -- |
| 139 | +package main |
| 140 | + |
| 141 | +import ( |
| 142 | + "debug/dwarf" |
| 143 | + "debug/pe" |
| 144 | +) |
| 145 | + |
| 146 | +func readDWARF(exePath string) (*dwarf.Data, error) { |
| 147 | + peFile, err := pe.Open(exePath) |
| 148 | + if err != nil { |
| 149 | + return nil, err |
| 150 | + } |
| 151 | + defer peFile.Close() |
| 152 | + return peFile.DWARF() |
| 153 | +} |
0 commit comments