]> Cypherpunks repositories - gostls13.git/commitdiff
debug/macho: fix DWARF for section names longer than 16 chars
authorAlessandro Arzilli <alessandro.arzilli@gmail.com>
Wed, 28 Aug 2024 08:16:10 +0000 (10:16 +0200)
committerGopher Robot <gobot@golang.org>
Fri, 30 Aug 2024 17:51:51 +0000 (17:51 +0000)
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:
https://github.com/go-delve/delve/issues/3797

Change-Id: I8c4a02493b8d5c3f63c831da43f6292124edf670
Reviewed-on: https://go-review.googlesource.com/c/go/+/608995
Auto-Submit: Ian Lance Taylor <iant@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
src/debug/macho/file.go

index 7b11bb2954354357f6d1ce707cdf6f508cc78ef3..fcf28c4b25edaf6632bd2af23e24a8a901fb6b58 100644 (file)
@@ -610,15 +610,33 @@ func (f *File) Section(name string) *Section {
 // DWARF returns the DWARF debug information for the Mach-O file.
 func (f *File) DWARF() (*dwarf.Data, error) {
        dwarfSuffix := func(s *Section) string {
+               sectname := s.Name
+               var pfx int
                switch {
-               case strings.HasPrefix(s.Name, "__debug_"):
-                       return s.Name[8:]
-               case strings.HasPrefix(s.Name, "__zdebug_"):
-                       return s.Name[9:]
+               case strings.HasPrefix(sectname, "__debug_"):
+                       pfx = 8
+               case strings.HasPrefix(sectname, "__zdebug_"):
+                       pfx = 9
                default:
                        return ""
                }
-
+               // Mach-O executables truncate section names to 16 characters, mangling some DWARF sections.
+               // As of DWARFv5 these are the only problematic section names (see DWARFv5 Appendix G).
+               for _, longname := range []string{
+                       "__debug_str_offsets",
+                       "__zdebug_line_str",
+                       "__zdebug_loclists",
+                       "__zdebug_pubnames",
+                       "__zdebug_pubtypes",
+                       "__zdebug_rnglists",
+                       "__zdebug_str_offsets",
+               } {
+                       if sectname == longname[:16] {
+                               sectname = longname
+                               break
+                       }
+               }
+               return sectname[pfx:]
        }
        sectionData := func(s *Section) ([]byte, error) {
                b, err := s.Data()