Skip to content

Commit 2747f92

Browse files
committed
debug/macho: support reading imported symbols without LC_DYSYMTAB
Currently, the ImportedSymbols method requires an LC_DYSYMTAB load command to exist. However, a Mach-O object file may not have an LC_DYSYMTAB load command, e.g. the one produced by "ld -r". Support this case by just reading the symbol table and gathers undefined symbols. Updates #61229. Change-Id: I8b4761ac7d99e1f1f378e883e9be75ee4049ffbb Reviewed-on: https://go-review.googlesource.com/c/go/+/692995 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: David Chase <[email protected]>
1 parent 025d369 commit 2747f92

File tree

3 files changed

+50
-8
lines changed

3 files changed

+50
-8
lines changed

src/debug/macho/file.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -719,15 +719,28 @@ func (f *File) DWARF() (*dwarf.Data, error) {
719719
// referred to by the binary f that are expected to be
720720
// satisfied by other libraries at dynamic load time.
721721
func (f *File) ImportedSymbols() ([]string, error) {
722-
if f.Dysymtab == nil || f.Symtab == nil {
722+
if f.Symtab == nil {
723723
return nil, &FormatError{0, "missing symbol table", nil}
724724
}
725725

726726
st := f.Symtab
727727
dt := f.Dysymtab
728728
var all []string
729-
for _, s := range st.Syms[dt.Iundefsym : dt.Iundefsym+dt.Nundefsym] {
730-
all = append(all, s.Name)
729+
if dt != nil {
730+
for _, s := range st.Syms[dt.Iundefsym : dt.Iundefsym+dt.Nundefsym] {
731+
all = append(all, s.Name)
732+
}
733+
} else {
734+
// From Darwin's include/mach-o/nlist.h
735+
const (
736+
N_TYPE = 0x0e
737+
N_UNDF = 0x0
738+
)
739+
for _, s := range st.Syms {
740+
if s.Type&N_TYPE == N_UNDF && s.Sect == 0 {
741+
all = append(all, s.Name)
742+
}
743+
}
731744
}
732745
return all, nil
733746
}

src/debug/macho/file_test.go

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@ import (
99
"internal/obscuretestdata"
1010
"io"
1111
"reflect"
12+
"slices"
1213
"testing"
1314
)
1415

1516
type fileTest struct {
16-
file string
17-
hdr FileHeader
18-
loads []any
19-
sections []*SectionHeader
20-
relocations map[string][]Reloc
17+
file string
18+
hdr FileHeader
19+
loads []any
20+
sections []*SectionHeader
21+
relocations map[string][]Reloc
22+
importedSyms []string
2123
}
2224

2325
var fileTests = []fileTest{
@@ -46,6 +48,7 @@ var fileTests = []fileTest{
4648
{"__jump_table", "__IMPORT", 0x3000, 0xa, 0x2000, 0x6, 0x0, 0x0, 0x4000008},
4749
},
4850
nil,
51+
nil,
4952
},
5053
{
5154
"testdata/gcc-amd64-darwin-exec.base64",
@@ -74,6 +77,7 @@ var fileTests = []fileTest{
7477
{"__la_symbol_ptr", "__DATA", 0x100001058, 0x10, 0x1058, 0x2, 0x0, 0x0, 0x7},
7578
},
7679
nil,
80+
nil,
7781
},
7882
{
7983
"testdata/gcc-amd64-darwin-exec-debug.base64",
@@ -102,6 +106,7 @@ var fileTests = []fileTest{
102106
{"__debug_str", "__DWARF", 0x10000215c, 0x60, 0x115c, 0x0, 0x0, 0x0, 0x0},
103107
},
104108
nil,
109+
nil,
105110
},
106111
{
107112
"testdata/clang-386-darwin-exec-with-rpath.base64",
@@ -126,6 +131,7 @@ var fileTests = []fileTest{
126131
},
127132
nil,
128133
nil,
134+
nil,
129135
},
130136
{
131137
"testdata/clang-amd64-darwin-exec-with-rpath.base64",
@@ -150,6 +156,7 @@ var fileTests = []fileTest{
150156
},
151157
nil,
152158
nil,
159+
nil,
153160
},
154161
{
155162
"testdata/clang-386-darwin.obj.base64",
@@ -185,6 +192,7 @@ var fileTests = []fileTest{
185192
},
186193
},
187194
},
195+
nil,
188196
},
189197
{
190198
"testdata/clang-amd64-darwin.obj.base64",
@@ -221,6 +229,15 @@ var fileTests = []fileTest{
221229
},
222230
},
223231
},
232+
[]string{"_printf"},
233+
},
234+
{
235+
"testdata/clang-amd64-darwin-ld-r.obj.base64",
236+
FileHeader{0xfeedfacf, CpuAmd64, 0x3, 0x1, 0x4, 0x1c0, 0x2000},
237+
nil,
238+
nil,
239+
nil,
240+
[]string{"_printf"},
224241
},
225242
}
226243

@@ -345,6 +362,17 @@ func TestOpen(t *testing.T) {
345362
}
346363
}
347364
}
365+
366+
if tt.importedSyms != nil {
367+
ss, err := f.ImportedSymbols()
368+
if err != nil {
369+
t.Errorf("open %s: fail to read imported symbols: %v", tt.file, err)
370+
}
371+
want := tt.importedSyms
372+
if !slices.Equal(ss, want) {
373+
t.Errorf("open %s: imported symbols differ:\n\thave %v\n\twant %v", tt.file, ss, want)
374+
}
375+
}
348376
}
349377
}
350378

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
z/rt/gcAAAEDAAAAAQAAAAQAAADAAQAAACAAAAAAAAAZAAAAiAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJgAAAAAAAAAAAIAAAAAAACYAAAAAAAAAAcAAAAHAAAABAAAAAAAAABfX3RleHQAAAAAAAAAAAAAX19URVhUAAAAAAAAAAAAAAAAAAAAAAAAKgAAAAAAAAAAAgAABAAAAJgCAAACAAAAAAQAgAAAAAAAAAAAAAAAAF9fY3N0cmluZwAAAAAAAABfX1RFWFQAAAAAAAAAAAAAKgAAAAAAAAAOAAAAAAAAACoCAAAAAAAAAAAAAAAAAAACAAAAAAAAAAAAAAAAAAAAX19laF9mcmFtZQAAAAAAAF9fVEVYVAAAAAAAAAAAAAA4AAAAAAAAAEAAAAAAAAAAOAIAAAMAAACoAgAABAAAAAAAAAAAAAAAAAAAAAAAAABfX2NvbXBhY3RfdW53aW5kX19MRAAAAAAAAAAAAAAAAHgAAAAAAAAAIAAAAAAAAAB4AgAAAwAAAMgCAAABAAAAAAAAAgAAAAAAAAAAAAAAAAIAAAAYAAAA0AIAAAUAAAAgAwAAKAAAACQAAAAQAAAAAAwKAAAAAAApAAAAEAAAANACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABVSInlSIPsEEiNPQAAAADHRfw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