Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read/macho: support Go's compression format #697

Merged
merged 3 commits into from
Jun 25, 2024

Conversation

ajwerner
Copy link
Contributor

This PR adds support in the object crate for Go's format to compress Mach-O debug info sections. It contains two changes:

  • The MachOFile now can match section names which contain both the __ and __z prefix instead of a ..
  • The MachOSection now detects the presence of the __z prefix and attempts to offer decompression when it detects the section is compressed using the Go compression scheme.

A third commit adds testing. This third commit will need to be rebased to update the testfiles commit once gimli-rs/object-testfiles#14 is merged into the upstream.

Fixes #696.

src/read/macho/section.rs Outdated Show resolved Hide resolved
@ajwerner
Copy link
Contributor Author

TFTR!

I also manually tried this out with gimli's dwarfdump and it works.

Before:

$  cargo run --bin dwarfdump -- ../object-testfiles/macho/go-x86_64
.debug_info

.debug_types

.debug_pubnames

.debug_aranges

.debug_pubtypes

After:

$ git diff
diff --git a/Cargo.toml b/Cargo.toml
index 8eed4b2..491566f 100644
--- a/Cargo.toml
+++ b/Cargo.toml
diff --git a/Cargo.toml b/Cargo.toml
index 8eed4b2..05faeb6 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -59,3 +59,6 @@ split-debuginfo = "packed"
 members = ["crates/examples"]
 default-members = [".", "crates/examples"]
 resolver = "2"
+
+[patch.crates-io]
+object = { path = "/Users/ajwerner/src/github.com/gimli-rs/object" }
$ cargo run --bin dwarfdump -- ../object-testfiles/macho/go-x86_64


.debug_info

UNIT<.debug_info+0x00000000>: length = 0xc99, format = Dwarf32, version = 4, address_size = 8, abbrev_offset = 0x0
< 0><0x0000000b>  DW_TAG_compile_unit
                    DW_AT_name                  internal/fmtsort
                    DW_AT_language              DW_LANG_Go
                    DW_AT_stmt_list             <.debug_line+0x00000000>
                    DW_AT_low_pc                0x01074de0
                    DW_AT_ranges                <rnglist at .debug_ranges+0x00000000>
                        [ 0] <offset-pair 0x00000000, 0x0000178c> [0x01074de0, 0x0107656c]
                    DW_AT_comp_dir              .
                    DW_AT_producer              Go cmd/compile go1.22.4; -shared regabi
                    Unknown DwAt: 10501         fmtsort
< 1><0x00000060>    DW_TAG_subprogram
                      DW_AT_name                  reflect.Value.MapRange
                      DW_AT_inline                DW_INL_inlined
                      DW_AT_decl_line             0x000007f5
                      DW_AT_external              yes
< 2><0x0000007c>      DW_TAG_formal_parameter
                        DW_AT_name                  v
                        DW_AT_variable_parameter    no
                        DW_AT_type                  <.debug_info+0x000928d2>
< 2><0x00000084>      DW_TAG_null

src/read/macho/file.rs Outdated Show resolved Hide resolved
src/read/macho/file.rs Outdated Show resolved Hide resolved
tests/read/macho.rs Show resolved Hide resolved
src/read/macho/section.rs Outdated Show resolved Hide resolved
Copy link
Contributor Author

@ajwerner ajwerner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TFTR!

src/read/macho/file.rs Outdated Show resolved Hide resolved
tests/read/macho.rs Show resolved Hide resolved
src/read/macho/section.rs Outdated Show resolved Hide resolved
src/read/macho/file.rs Outdated Show resolved Hide resolved
src/read/gnu_compression.rs Show resolved Hide resolved
src/read/gnu_compression.rs Outdated Show resolved Hide resolved
src/read/macho/file.rs Outdated Show resolved Hide resolved
src/read/gnu_compression.rs Outdated Show resolved Hide resolved
Copy link
Contributor Author

@ajwerner ajwerner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review!

src/read/gnu_compression.rs Outdated Show resolved Hide resolved
src/read/gnu_compression.rs Outdated Show resolved Hide resolved
src/read/gnu_compression.rs Show resolved Hide resolved
src/read/macho/file.rs Outdated Show resolved Hide resolved
When using compression, sections in Mach-O produced by the go
compiler have a __zdebug_ prefix (only the debug sections are
compressed).
Copy link
Contributor

@philipc philipc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is good to merge after a cargo fmt.

This commit extracts the GNU-style section compression logic from the
read::elf::section to a module underneath read, and then uses it also
in read::macho. This is the style of compression created by the go
compiler for Mach-O executables.
This commit updates the submodule for the testfiles to include the
Mach-O binaries produced by the Go compiler and then exercises the
logic in the previous commits. The new testfiles commit is due to
gimli-rs/object-testfiles#14.
@ajwerner
Copy link
Contributor Author

I think this is good to merge after a cargo fmt.

Sorry for the delay, done. Thank you for the reviews.

@philipc philipc merged commit f54ea55 into gimli-rs:master Jun 25, 2024
10 checks passed
@ajwerner ajwerner deleted the go-compilation branch June 25, 2024 13:22
mstange added a commit to mstange/samply that referenced this pull request Jul 2, 2024
… Go.

Object now does this internally, thanks to github.com/gimli-rs/object/pull/697 .
mstange added a commit to mstange/samply that referenced this pull request Jul 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support go's Mach-O dwarf compression
3 participants