Skip to content

Commit 4f327f2

Browse files
aarzilligopherbot
authored andcommitted
debug/macho: fix DWARF for section names longer than 16 chars
The Mach-O file format truncates section names to 16 characters maximum, which makes some sections unrecognizable to debug/dwarf. This CL works around this problem by re-expanding the truncated section names. This problem was originally reported as: go-delve/delve#3797 Change-Id: I8c4a02493b8d5c3f63c831da43f6292124edf670 Reviewed-on: https://go-review.googlesource.com/c/go/+/608995 Auto-Submit: Ian Lance Taylor <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Cherry Mui <[email protected]>
1 parent 7300b9e commit 4f327f2

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

src/debug/macho/file.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -610,15 +610,33 @@ func (f *File) Section(name string) *Section {
610610
// DWARF returns the DWARF debug information for the Mach-O file.
611611
func (f *File) DWARF() (*dwarf.Data, error) {
612612
dwarfSuffix := func(s *Section) string {
613+
sectname := s.Name
614+
var pfx int
613615
switch {
614-
case strings.HasPrefix(s.Name, "__debug_"):
615-
return s.Name[8:]
616-
case strings.HasPrefix(s.Name, "__zdebug_"):
617-
return s.Name[9:]
616+
case strings.HasPrefix(sectname, "__debug_"):
617+
pfx = 8
618+
case strings.HasPrefix(sectname, "__zdebug_"):
619+
pfx = 9
618620
default:
619621
return ""
620622
}
621-
623+
// Mach-O executables truncate section names to 16 characters, mangling some DWARF sections.
624+
// As of DWARFv5 these are the only problematic section names (see DWARFv5 Appendix G).
625+
for _, longname := range []string{
626+
"__debug_str_offsets",
627+
"__zdebug_line_str",
628+
"__zdebug_loclists",
629+
"__zdebug_pubnames",
630+
"__zdebug_pubtypes",
631+
"__zdebug_rnglists",
632+
"__zdebug_str_offsets",
633+
} {
634+
if sectname == longname[:16] {
635+
sectname = longname
636+
break
637+
}
638+
}
639+
return sectname[pfx:]
622640
}
623641
sectionData := func(s *Section) ([]byte, error) {
624642
b, err := s.Data()

0 commit comments

Comments
 (0)