-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacho_test.go
157 lines (142 loc) · 4.13 KB
/
macho_test.go
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright 2025 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build darwin
package ld
import (
"debug/macho"
"fmt"
"internal/testenv"
"os"
"path/filepath"
"testing"
)
func TestMachoSectionsReadOnly(t *testing.T) {
t.Parallel()
testenv.MustHaveGoBuild(t)
const (
prog = `package main; func main() {}`
progC = `package main; import "C"; func main() {}`
)
tests := []struct {
name string
args []string
prog string
wantSecsRO []string
mustHaveCGO bool
mustInternalLink bool
}{
{
name: "linkmode-internal",
args: []string{"-ldflags", "-linkmode=internal"},
prog: prog,
mustInternalLink: true,
wantSecsRO: []string{"__got", "__rodata", "__itablink", "__typelink", "__gosymtab", "__gopclntab"},
},
{
name: "linkmode-external",
args: []string{"-ldflags", "-linkmode=external"},
prog: prog,
mustHaveCGO: true,
wantSecsRO: []string{"__got", "__rodata", "__itablink", "__typelink", "__gopclntab"},
},
{
name: "cgo-linkmode-internal",
args: []string{"-ldflags", "-linkmode=external"},
prog: progC,
mustHaveCGO: true,
mustInternalLink: true,
wantSecsRO: []string{"__got", "__rodata", "__itablink", "__typelink", "__gopclntab"},
},
{
name: "cgo-linkmode-external",
args: []string{"-ldflags", "-linkmode=external"},
prog: progC,
mustHaveCGO: true,
wantSecsRO: []string{"__got", "__rodata", "__itablink", "__typelink", "__gopclntab"},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if test.mustInternalLink {
testenv.MustInternalLink(t, test.mustHaveCGO)
}
if test.mustHaveCGO {
testenv.MustHaveCGO(t)
}
var (
dir = t.TempDir()
src = filepath.Join(dir, fmt.Sprintf("macho_%s.go", test.name))
binFile = filepath.Join(dir, test.name)
)
if err := os.WriteFile(src, []byte(test.prog), 0666); err != nil {
t.Fatal(err)
}
cmdArgs := append([]string{"build", "-o", binFile}, append(test.args, src)...)
cmd := testenv.Command(t, testenv.GoToolPath(t), cmdArgs...)
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("failed to build %v: %v:\n%s", cmd.Args, err, out)
}
fi, err := os.Open(binFile)
if err != nil {
t.Fatalf("failed to open built file: %v", err)
}
defer fi.Close()
machoFile, err := macho.NewFile(fi)
if err != nil {
t.Fatalf("failed to parse macho file: %v", err)
}
defer machoFile.Close()
// Load segments
segs := make(map[string]*macho.Segment)
for _, l := range machoFile.Loads {
if s, ok := l.(*macho.Segment); ok {
segs[s.Name] = s
}
}
for _, wsroname := range test.wantSecsRO {
// Now walk the sections. Section should be part of
// some segment that is readonly.
var wsro *macho.Section
foundRO := false
for _, s := range machoFile.Sections {
if s.Name == wsroname {
seg := segs[s.Seg]
if seg == nil {
t.Fatalf("test %s: can't locate segment for %q section",
test.name, wsroname)
}
if seg.Flag == 0x10 { // SG_READ_ONLY
foundRO = true
wsro = s
break
}
}
}
if wsro == nil {
t.Fatalf("test %s: can't locate %q section",
test.name, wsroname)
continue
}
if !foundRO {
// Things went off the rails. Write out some
// useful information for a human looking at the
// test failure.
t.Logf("test %s: %q section not in readonly segment",
wsro.Name, test.name)
t.Logf("section %s location: st=0x%x en=0x%x\n",
wsro.Name, wsro.Addr, wsro.Addr+wsro.Size)
t.Logf("sec %s found in this segment: ", wsro.Seg)
t.Logf("\nall segments: \n")
for _, l := range machoFile.Loads {
if s, ok := l.(*macho.Segment); ok {
t.Logf("cmd=%s fl=%d st=0x%x en=0x%x\n",
s.Cmd, s.Flag, s.Addr, s.Addr+s.Filesz)
}
}
t.Fatalf("test %s failed", test.name)
}
}
})
}
}