...

Text file src/cmd/go/testdata/script/build_trimpath_cgo.txt

Documentation: cmd/go/testdata/script

     1# This test builds a cgo binary and verifies the source directory path
     2# does not appear in the binary, either literally or in compressed DWARF.
     3# TODO(golang.org/issue/36072): ideally we should build a binary from identical
     4# sources in different directories and verify the binary and all intermediate
     5# files are identical.
     6
     7[short] skip
     8[!cgo] skip
     9
    10# Check that the source path appears when -trimpath is not used.
    11go build -o hello.exe .
    12grep -q gopath[/\\]src hello.exe
    13go run ./list-dwarf hello.exe
    14stdout gopath[/\\]src
    15
    16# Check that the source path does not appear when -trimpath is used.
    17[GOOS:aix] stop # can't inspect XCOFF binaries
    18go build -trimpath -o hello.exe .
    19! grep -q gopath[/\\]src hello.exe
    20go run ./list-dwarf hello.exe
    21! stdout gopath/src
    22
    23
    24# Do the above, with the cgo (but not .c) sources in an overlay
    25# Check that the source path appears when -trimpath is not used.
    26mkdir $WORK/overlay
    27cp hello.go $WORK/overlay/hello.go
    28mkdir hello_overlay
    29cp hello.c hello_overlay/hello.c
    30go build -overlay overlay.json -o hello_overlay.exe ./hello_overlay
    31grep -q gopath[/\\]src hello_overlay.exe
    32! grep -q $WORK[/\\]overlay hello_overlay.exe
    33go run ./list-dwarf hello_overlay.exe
    34stdout gopath[/\\]src
    35! stdout $WORK[/\\]overlay
    36
    37# Check that the source path does not appear when -trimpath is used.
    38go build -overlay overlay.json -trimpath -o hello_overlay.exe ./hello_overlay
    39! grep -q gopath[/\\]src hello_overlay.exe
    40! grep -q $WORK[/\\]overlay hello_overlay.exe
    41go run ./list-dwarf hello_overlay.exe
    42! stdout gopath/src
    43! stdout $WORK[/\\]overlay
    44
    45-- go.mod --
    46module m
    47
    48go 1.14
    49-- overlay.json --
    50{
    51	"Replace": {
    52		"hello_overlay/hello.go": "../../overlay/hello.go"
    53	}
    54}
    55-- hello.c --
    56#include <stdio.h>
    57
    58void say_hello() { puts("Hello, world!\n"); }
    59
    60-- hello.go --
    61package main
    62
    63// void say_hello();
    64import "C"
    65
    66func main() {
    67	C.say_hello()
    68}
    69
    70-- list-dwarf/list-dwarf.go --
    71package main
    72
    73import (
    74	"debug/dwarf"
    75	"fmt"
    76	"io"
    77	"log"
    78	"os"
    79	"sort"
    80)
    81
    82func main() {
    83	files, err := run(os.Args[1])
    84	if err != nil {
    85		log.Fatal(err)
    86	}
    87	for _, file := range files {
    88		fmt.Println(file)
    89	}
    90}
    91
    92func run(exePath string) ([]string, error) {
    93	dwarfData, err := readDWARF(exePath)
    94	if err != nil {
    95		return nil, err
    96	}
    97
    98	dwarfReader := dwarfData.Reader()
    99	files := make(map[string]bool)
   100	for {
   101		e, err := dwarfReader.Next()
   102		if err != nil {
   103			return nil, err
   104		}
   105		if e == nil {
   106			break
   107		}
   108		lr, err := dwarfData.LineReader(e)
   109		if err != nil {
   110			return nil, err
   111		}
   112		if lr == nil {
   113			continue
   114		}
   115
   116		var le dwarf.LineEntry
   117		for {
   118			if err := lr.Next(&le); err != nil {
   119				if err == io.EOF {
   120					break
   121				}
   122				return nil, err
   123			}
   124			files[le.File.Name] = true
   125		}
   126	}
   127
   128	sortedFiles := make([]string, 0, len(files))
   129	for file := range files {
   130		sortedFiles = append(sortedFiles, file)
   131	}
   132	sort.Strings(sortedFiles)
   133	return sortedFiles, nil
   134}
   135-- list-dwarf/read_darwin.go --
   136package main
   137
   138import (
   139	"debug/dwarf"
   140	"debug/macho"
   141)
   142
   143func readDWARF(exePath string) (*dwarf.Data, error) {
   144	machoFile, err := macho.Open(exePath)
   145	if err != nil {
   146		return nil, err
   147	}
   148	defer machoFile.Close()
   149	return machoFile.DWARF()
   150}
   151-- list-dwarf/read_elf.go --
   152// +build android dragonfly freebsd illumos linux netbsd openbsd solaris
   153
   154package main
   155
   156import (
   157	"debug/dwarf"
   158	"debug/elf"
   159)
   160
   161func readDWARF(exePath string) (*dwarf.Data, error) {
   162	elfFile, err := elf.Open(exePath)
   163	if err != nil {
   164		return nil, err
   165	}
   166	defer elfFile.Close()
   167	return elfFile.DWARF()
   168}
   169-- list-dwarf/read_windows.go --
   170package main
   171
   172import (
   173	"debug/dwarf"
   174	"debug/pe"
   175)
   176
   177func readDWARF(exePath string) (*dwarf.Data, error) {
   178	peFile, err := pe.Open(exePath)
   179	if err != nil {
   180		return nil, err
   181	}
   182	defer peFile.Close()
   183	return peFile.DWARF()
   184}

View as plain text