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

     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.
    11  go build -o hello.exe .
    12  grep -q gopath[/\\]src hello.exe
    13  go run ./list-dwarf hello.exe
    14  stdout gopath[/\\]src
    15  
    16  # Check that the source path does not appear when -trimpath is used.
    17  [aix] stop # can't inspect XCOFF binaries
    18  go build -trimpath -o hello.exe .
    19  ! grep -q gopath[/\\]src hello.exe
    20  go 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.
    26  mkdir $WORK/overlay
    27  cp hello.go $WORK/overlay/hello.go
    28  mkdir hello_overlay
    29  cp hello.c hello_overlay/hello.c
    30  go build -overlay overlay.json -o hello_overlay.exe ./hello_overlay
    31  grep -q gopath[/\\]src hello_overlay.exe
    32  ! grep -q $WORK[/\\]overlay hello_overlay.exe
    33  go run ./list-dwarf hello_overlay.exe
    34  stdout gopath[/\\]src
    35  ! stdout $WORK[/\\]overlay
    36  
    37  # Check that the source path does not appear when -trimpath is used.
    38  go 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
    41  go run ./list-dwarf hello_overlay.exe
    42  ! stdout gopath/src
    43  ! stdout $WORK[/\\]overlay
    44  
    45  -- go.mod --
    46  module m
    47  
    48  go 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  
    58  void say_hello() { puts("Hello, world!\n"); }
    59  
    60  -- hello.go --
    61  package main
    62  
    63  // void say_hello();
    64  import "C"
    65  
    66  func main() {
    67  	C.say_hello()
    68  }
    69  
    70  -- list-dwarf/list-dwarf.go --
    71  package main
    72  
    73  import (
    74  	"debug/dwarf"
    75  	"fmt"
    76  	"io"
    77  	"log"
    78  	"os"
    79  	"sort"
    80  )
    81  
    82  func 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  
    92  func 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 --
   136  package main
   137  
   138  import (
   139  	"debug/dwarf"
   140  	"debug/macho"
   141  )
   142  
   143  func 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  
   154  package main
   155  
   156  import (
   157  	"debug/dwarf"
   158  	"debug/elf"
   159  )
   160  
   161  func 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 --
   170  package main
   171  
   172  import (
   173  	"debug/dwarf"
   174  	"debug/pe"
   175  )
   176  
   177  func 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  }
   185  

View as plain text