aboutsummaryrefslogtreecommitdiff
path: root/tests/core/go_binary/pie_linux_test.go
blob: 8b1355006009fcf7c1bac843f0807fb378b3e749 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package test

import (
	"debug/elf"
	"fmt"
	"os"
	"testing"

	"github.com/bazelbuild/rules_go/go/tools/bazel"
)

func openELF(dir, bin string) (*elf.File, error) {
	bin, ok := bazel.FindBinary(dir, bin)
	if !ok {
		return nil, fmt.Errorf("could not find binary: %s", bin)
	}

	f, err := os.Open(bin)
	if err != nil {
		return nil, err
	}

	return elf.NewFile(f)
}

func TestPIE(t *testing.T) {
	e, err := openELF("tests/core/go_binary", "hello_pie_bin")
	if err != nil {
		t.Fatal(err)
	}

	// PIE binaries are implemented as shared libraries.
	if e.Type != elf.ET_DYN {
		t.Error("ELF binary is not position-independent.")
	}
}

func TestNoPIE(t *testing.T) {
	e, err := openELF("tests/core/go_binary", "hello_nopie_bin")
	if err != nil {
		t.Fatal(err)
	}

	// PIE binaries are implemented as shared libraries.
	if e.Type != elf.ET_EXEC {
		t.Error("ELF binary is not position-dependent.")
	}
}