github.com/0xKiwi/rules_go@v0.24.3/tests/core/go_binary/pie_darwin_test.go (about)

     1  package test
     2  
     3  import (
     4  	"debug/macho"
     5  	"fmt"
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/bazelbuild/rules_go/go/tools/bazel"
    10  )
    11  
    12  func openMachO(dir, bin string) (*macho.File, error) {
    13  	bin, ok := bazel.FindBinary(dir, bin)
    14  	if !ok {
    15  		return nil, fmt.Errorf("could not find binary: %s", bin)
    16  	}
    17  
    18  	f, err := os.Open(bin)
    19  	if err != nil {
    20  		return nil, err
    21  	}
    22  
    23  	return macho.NewFile(f)
    24  }
    25  
    26  func TestPIE(t *testing.T) {
    27  	m, err := openMachO("tests/core/go_binary", "hello_pie_bin")
    28  	if err != nil {
    29  		t.Fatal(err)
    30  	}
    31  
    32  	if m.Flags&macho.FlagPIE == 0 {
    33  		t.Error("ELF binary is not position-independent.")
    34  	}
    35  }
    36  
    37  func TestNoPIE(t *testing.T) {
    38  	m, err := openMachO("tests/core/go_binary", "hello_nopie_bin")
    39  	if err != nil {
    40  		t.Fatal(err)
    41  	}
    42  
    43  	if m.Flags&macho.FlagPIE != 0 {
    44  		t.Error("ELF binary is not position-dependent.")
    45  	}
    46  }