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

     1  package test
     2  
     3  import (
     4  	"debug/elf"
     5  	"fmt"
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/bazelbuild/rules_go/go/tools/bazel"
    10  )
    11  
    12  func openELF(dir, bin string) (*elf.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 elf.NewFile(f)
    24  }
    25  
    26  func TestPIE(t *testing.T) {
    27  	e, err := openELF("tests/core/go_binary", "hello_pie_bin")
    28  	if err != nil {
    29  		t.Fatal(err)
    30  	}
    31  
    32  	// PIE binaries are implemented as shared libraries.
    33  	if e.Type != elf.ET_DYN {
    34  		t.Error("ELF binary is not position-independent.")
    35  	}
    36  }
    37  
    38  func TestNoPIE(t *testing.T) {
    39  	e, err := openELF("tests/core/go_binary", "hello_nopie_bin")
    40  	if err != nil {
    41  		t.Fatal(err)
    42  	}
    43  
    44  	// PIE binaries are implemented as shared libraries.
    45  	if e.Type != elf.ET_EXEC {
    46  		t.Error("ELF binary is not position-dependent.")
    47  	}
    48  }