Skip to content

macOS DWARF oddities #303

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

Closed
bjorn3 opened this issue Jan 26, 2019 · 1 comment · Fixed by #759
Closed

macOS DWARF oddities #303

bjorn3 opened this issue Jan 26, 2019 · 1 comment · Fixed by #759
Labels
A-debuginfo Area: Debugging information at runtime in generated code. C-bug Category: This is a bug. O-macos Operating system: MacOS

Comments

@bjorn3
Copy link
Member

bjorn3 commented Jan 26, 2019

After applying the following patch, because macOS doesn't use .debug_* relocations.

diff --git a/src/debuginfo.rs b/src/debuginfo.rs
index 6da47d2..4c943c4 100644
--- a/src/debuginfo.rs
+++ b/src/debuginfo.rs
@@ -740,15 +740,7 @@ impl<'a, 'tcx> Writer for WriterRelocate<'a, 'tcx> {
     }
 
     fn write_offset(&mut self, val: usize, section: SectionId, size: u8) -> Result<()> {
-        let offset = self.len() as u32;
-        let name = self.ctx.section_name(section);
-        self.relocs.push(DebugReloc {
-            offset,
-            size,
-            name,
-            addend: val as i64,
-        });
-        self.write_word(0, size)
+        self.write_word(val as u64, size)
     }
 
     fn write_offset_at(
@@ -758,14 +750,7 @@ impl<'a, 'tcx> Writer for WriterRelocate<'a, 'tcx> {
         section: SectionId,
         size: u8,
     ) -> Result<()> {
-        let name = self.ctx.section_name(section);
-        self.relocs.push(DebugReloc {
-            offset: offset as u32,
-            size,
-            name,
-            addend: val as i64,
-        });
-        self.write_word_at(offset, 0, size)
+        self.write_word_at(offset, val as u64, size)
     }
 }

The resulting DWARF has wrong addresses, even though the original object files had relocations for the .debug_* sections:

dwarfdump --verify target/out/mini_core_hello_world.dSYM
----------------------------------------------------------------------
 File: target/out/mini_core_hello_world.dSYM/Contents/Resources/DWARF/mini_core_hello_world (x86_64)
----------------------------------------------------------------------
Verifying Compile Unit Header chain... ok
Verifying .debug_info... 
error: Range is not in parent
  TAG_subprogram {0x00000066}: [0x0000000100000ade - 0x0000000100000c6e) 
error: Range is not in parent
  TAG_subprogram {0x0000019d}: [0x0000000100000d2b - 0x0000000100001b9e) 
error: Range is not in parent
  TAG_subprogram {0x000007f8}: [0x0000000100001bbf - 0x0000000100001bcd) 
error: Range is not in parent
  TAG_subprogram {0x00000835}: [0x0000000100001b9e - 0x0000000100001bbf) 
error: Range is not in parent
  TAG_subprogram {0x0000086e}: [0x0000000100001cac - 0x0000000100001d2e) 
error: Range is not in parent
  TAG_subprogram {0x0000090c}: [0x0000000100001bcd - 0x0000000100001c52) 
error: Range is not in parent
  TAG_subprogram {0x00000a13}: [0x0000000100001eab - 0x0000000100001f30) 
error: Range is not in parent
  TAG_subprogram {0x00000a96}: [0x0000000100001d2e - 0x0000000100001eab) 
error: Range is not in parent
  TAG_subprogram {0x00000b2d}: [0x0000000100001c52 - 0x0000000100001cac) 
error: Range is not in parent
  TAG_subprogram {0x00000bae}: [0x0000000100001f58 - 0x0000000100001f65) 
error: Range is not in parent
  TAG_subprogram {0x00000beb}: [0x0000000100001f6b - 0x0000000100001f90) 
11 error(s) were found.
Verifying .apple_names... ok
Verifying .apple_types... ok
Verifying .apple_namespaces... ok
Verifying .apple_objc... ok
Verifying .debug_frame... ok
Verifying .debug_line... ok

$ dwarfdump target/out/mini_core_hello_world.dSYM
----------------------------------------------------------------------
 File: target/out/mini_core_hello_world.dSYM/Contents/Resources/DWARF/mini_core_hello_world (x86_64)
----------------------------------------------------------------------
.debug_info contents:

0x00000000: Compile Unit: length = 0x00000b7a  version = 0x0004  abbr_offset = 0x00000000  addr_size = 0x08  (next CU at 0x00000b7e)

0x0000000b: TAG_compile_unit [1] *
             AT_producer( "cranelift fn (rustc version unknown version)" )
             AT_language( DW_LANG_Rust )
             AT_name( "example/mini_core_hello_world.rs" )
             AT_comp_dir( "/Users/bjorn/Documents/rustc_codegen_cranelift" )
             AT_stmt_list( 0x00000000 )
             AT_low_pc( 0x0000000100000ad0 )
             AT_ranges( 0x00000000
                [0x0000000100000ad0 - 0x0000000100000ade)
                [0x0000000100000ad0 - 0x0000000100000ade)
                [0x0000000100000ad0 - 0x0000000100000aeb)
                [0x0000000100000ad0 - 0x0000000100000af1)
                [0x0000000100000ad0 - 0x0000000100000b2a)
                [0x0000000100000ad0 - 0x0000000100000b52)
                [0x0000000100000ad0 - 0x0000000100000b55)
                [0x0000000100000ad0 - 0x0000000100000b55)
                [0x0000000100000ad0 - 0x0000000100000b72)
                [0x0000000100000ad0 - 0x0000000100000c4d)
                [0x0000000100000ad0 - 0x0000000100000c60)
                [0x0000000100000ad0 - 0x0000000100001943)
                 End )
[...]

$ objdump -r target/out/mini_core.dummy_name.rcgu.o

target/out/mini_core.dummy_name.rcgu.o:	file format Mach-O 64-bit x86-64

RELOCATION RECORDS FOR [__text]:
000000000000034f X86_64_RELOC_GOT_LOAD __ZN9mini_core12MY_TINY_HEAP17h06b8b6ee678156b1E@GOTPCREL

RELOCATION RECORDS FOR [__debug_info]:
0000000000000032 X86_64_RELOC_UNSIGNED __ZN68_$LT$$RF$$u27$a$u20$bool$u20$as$u20$mini_core..BitOr$LT$bool$GT$$GT$5bitor17h06c4c08238ea11b3E
00000000000000a8 X86_64_RELOC_UNSIGNED __ZN39_$LT$bool$u20$as$u20$mini_core..Not$GT$3not17h0dfb0e7a03f21bafE
00000000000000f3 X86_64_RELOC_UNSIGNED __ZN37_$LT$u8$u20$as$u20$mini_core..Mul$GT$3mul17hbadd8ec33043e277E
0000000000000174 X86_64_RELOC_UNSIGNED __ZN37_$LT$u8$u20$as$u20$mini_core..Add$GT$3add17hf2b580c713c9aa22E
00000000000001e8 X86_64_RELOC_UNSIGNED __ZN45_$LT$char$u20$as$u20$mini_core..PartialEq$GT$2ne17h6591d7d822cd694dE
000000000000025e X86_64_RELOC_UNSIGNED __ZN40_$LT$isize$u20$as$u20$mini_core..Neg$GT$3neg17ha9990a7b7c09dbf8E
00000000000002ba X86_64_RELOC_UNSIGNED __ZN45_$LT$char$u20$as$u20$mini_core..PartialEq$GT$2eq17h85da6c7efdb2221eE
0000000000000323 X86_64_RELOC_UNSIGNED __ZN40_$LT$usize$u20$as$u20$mini_core..Sub$GT$3sub17h1819823fa4db3789E
00000000000003a5 X86_64_RELOC_UNSIGNED __ZN9mini_core5panic17h1b8c87c5633941c5E
00000000000003e2 X86_64_RELOC_UNSIGNED __ZN43_$LT$u8$u20$as$u20$mini_core..PartialEq$GT$2eq17h77b456aa7ccf9ab8E
0000000000000451 X86_64_RELOC_UNSIGNED __ZN41_$LT$bool$u20$as$u20$mini_core..BitOr$GT$5bitor17hc51993c0e2f70499E
00000000000004ba X86_64_RELOC_UNSIGNED _rust_eh_personality
00000000000004dc X86_64_RELOC_UNSIGNED __ZN43_$LT$u8$u20$as$u20$mini_core..PartialEq$GT$2ne17h40203ccd00452a56E
0000000000000545 X86_64_RELOC_UNSIGNED __ZN9mini_core8allocate17h122bb7e874c2cf63E

RELOCATION RECORDS FOR [__debug_line]:
0000000000000038 X86_64_RELOC_UNSIGNED __ZN68_$LT$$RF$$u27$a$u20$bool$u20$as$u20$mini_core..BitOr$LT$bool$GT$$GT$5bitor17h06c4c08238ea11b3E
0000000000000068 X86_64_RELOC_UNSIGNED __ZN39_$LT$bool$u20$as$u20$mini_core..Not$GT$3not17h0dfb0e7a03f21bafE
0000000000000096 X86_64_RELOC_UNSIGNED __ZN37_$LT$u8$u20$as$u20$mini_core..Mul$GT$3mul17hbadd8ec33043e277E
00000000000000e0 X86_64_RELOC_UNSIGNED __ZN37_$LT$u8$u20$as$u20$mini_core..Add$GT$3add17hf2b580c713c9aa22E
000000000000012a X86_64_RELOC_UNSIGNED __ZN45_$LT$char$u20$as$u20$mini_core..PartialEq$GT$2ne17h6591d7d822cd694dE
000000000000015d X86_64_RELOC_UNSIGNED __ZN40_$LT$isize$u20$as$u20$mini_core..Neg$GT$3neg17ha9990a7b7c09dbf8E
000000000000018f X86_64_RELOC_UNSIGNED __ZN45_$LT$char$u20$as$u20$mini_core..PartialEq$GT$2eq17h85da6c7efdb2221eE
00000000000001c2 X86_64_RELOC_UNSIGNED __ZN40_$LT$usize$u20$as$u20$mini_core..Sub$GT$3sub17h1819823fa4db3789E
0000000000000205 X86_64_RELOC_UNSIGNED __ZN9mini_core5panic17h1b8c87c5633941c5E
0000000000000222 X86_64_RELOC_UNSIGNED __ZN43_$LT$u8$u20$as$u20$mini_core..PartialEq$GT$2eq17h77b456aa7ccf9ab8E
0000000000000259 X86_64_RELOC_UNSIGNED __ZN41_$LT$bool$u20$as$u20$mini_core..BitOr$GT$5bitor17hc51993c0e2f70499E
0000000000000287 X86_64_RELOC_UNSIGNED _rust_eh_personality
00000000000002a4 X86_64_RELOC_UNSIGNED __ZN43_$LT$u8$u20$as$u20$mini_core..PartialEq$GT$2ne17h40203ccd00452a56E
00000000000002db X86_64_RELOC_UNSIGNED __ZN9mini_core8allocate17h122bb7e874c2cf63E

RELOCATION RECORDS FOR [__debug_ranges]:
0000000000000000 X86_64_RELOC_UNSIGNED __ZN68_$LT$$RF$$u27$a$u20$bool$u20$as$u20$mini_core..BitOr$LT$bool$GT$$GT$5bitor17h06c4c08238ea11b3E
0000000000000008 X86_64_RELOC_UNSIGNED __ZN68_$LT$$RF$$u27$a$u20$bool$u20$as$u20$mini_core..BitOr$LT$bool$GT$$GT$5bitor17h06c4c08238ea11b3E
0000000000000010 X86_64_RELOC_UNSIGNED __ZN39_$LT$bool$u20$as$u20$mini_core..Not$GT$3not17h0dfb0e7a03f21bafE
0000000000000018 X86_64_RELOC_UNSIGNED __ZN39_$LT$bool$u20$as$u20$mini_core..Not$GT$3not17h0dfb0e7a03f21bafE
0000000000000020 X86_64_RELOC_UNSIGNED __ZN37_$LT$u8$u20$as$u20$mini_core..Mul$GT$3mul17hbadd8ec33043e277E
0000000000000028 X86_64_RELOC_UNSIGNED __ZN37_$LT$u8$u20$as$u20$mini_core..Mul$GT$3mul17hbadd8ec33043e277E
0000000000000030 X86_64_RELOC_UNSIGNED __ZN37_$LT$u8$u20$as$u20$mini_core..Add$GT$3add17hf2b580c713c9aa22E
0000000000000038 X86_64_RELOC_UNSIGNED __ZN37_$LT$u8$u20$as$u20$mini_core..Add$GT$3add17hf2b580c713c9aa22E
0000000000000040 X86_64_RELOC_UNSIGNED __ZN45_$LT$char$u20$as$u20$mini_core..PartialEq$GT$2ne17h6591d7d822cd694dE
0000000000000048 X86_64_RELOC_UNSIGNED __ZN45_$LT$char$u20$as$u20$mini_core..PartialEq$GT$2ne17h6591d7d822cd694dE
0000000000000050 X86_64_RELOC_UNSIGNED __ZN40_$LT$isize$u20$as$u20$mini_core..Neg$GT$3neg17ha9990a7b7c09dbf8E
0000000000000058 X86_64_RELOC_UNSIGNED __ZN40_$LT$isize$u20$as$u20$mini_core..Neg$GT$3neg17ha9990a7b7c09dbf8E
0000000000000060 X86_64_RELOC_UNSIGNED __ZN45_$LT$char$u20$as$u20$mini_core..PartialEq$GT$2eq17h85da6c7efdb2221eE
0000000000000068 X86_64_RELOC_UNSIGNED __ZN45_$LT$char$u20$as$u20$mini_core..PartialEq$GT$2eq17h85da6c7efdb2221eE
0000000000000070 X86_64_RELOC_UNSIGNED __ZN40_$LT$usize$u20$as$u20$mini_core..Sub$GT$3sub17h1819823fa4db3789E
0000000000000078 X86_64_RELOC_UNSIGNED __ZN40_$LT$usize$u20$as$u20$mini_core..Sub$GT$3sub17h1819823fa4db3789E
0000000000000080 X86_64_RELOC_UNSIGNED __ZN9mini_core5panic17h1b8c87c5633941c5E
0000000000000088 X86_64_RELOC_UNSIGNED __ZN9mini_core5panic17h1b8c87c5633941c5E
0000000000000090 X86_64_RELOC_UNSIGNED __ZN43_$LT$u8$u20$as$u20$mini_core..PartialEq$GT$2eq17h77b456aa7ccf9ab8E
0000000000000098 X86_64_RELOC_UNSIGNED __ZN43_$LT$u8$u20$as$u20$mini_core..PartialEq$GT$2eq17h77b456aa7ccf9ab8E
00000000000000a0 X86_64_RELOC_UNSIGNED __ZN41_$LT$bool$u20$as$u20$mini_core..BitOr$GT$5bitor17hc51993c0e2f70499E
00000000000000a8 X86_64_RELOC_UNSIGNED __ZN41_$LT$bool$u20$as$u20$mini_core..BitOr$GT$5bitor17hc51993c0e2f70499E
00000000000000b0 X86_64_RELOC_UNSIGNED _rust_eh_personality
00000000000000b8 X86_64_RELOC_UNSIGNED _rust_eh_personality
00000000000000c0 X86_64_RELOC_UNSIGNED __ZN43_$LT$u8$u20$as$u20$mini_core..PartialEq$GT$2ne17h40203ccd00452a56E
00000000000000c8 X86_64_RELOC_UNSIGNED __ZN43_$LT$u8$u20$as$u20$mini_core..PartialEq$GT$2ne17h40203ccd00452a56E
00000000000000d0 X86_64_RELOC_UNSIGNED __ZN9mini_core8allocate17h122bb7e874c2cf63E
00000000000000d8 X86_64_RELOC_UNSIGNED __ZN9mini_core8allocate17h122bb7e874c2cf63E

And .debug_line has all sequences start at 0x0000000100000ad0

Originally posted by @bjorn3 in https://github.com/bjorn3/rustc_codegen_cranelift/pull/291#issuecomment-455791860

@bjorn3
Copy link
Member Author

bjorn3 commented Jan 26, 2019

Excerpt of https://github.com/bjorn3/rustc_codegen_cranelift/pull/291#issuecomment-455835684 by @philipc:

MachO relocations use implicit addends, so you need to write the addend instead of the 0. This will apply to other targets too (eg ELF EM_386). faerie doesn't handle this case for us (and I think this assert in cranelift is the same problem).

@bjorn3 bjorn3 added C-bug Category: This is a bug. A-debuginfo Area: Debugging information at runtime in generated code. labels Jan 26, 2019
@bjorn3 bjorn3 added the O-macos Operating system: MacOS label Aug 18, 2019
bjorn3 added a commit that referenced this issue Oct 19, 2019
This only fixes it when using object::write as backend, and not when using faerie.

There were two problems:

* object::write doesn't replace .debug_info with __debug_info, unlike faerie
* machO requires section relative relocations, and not symbol relative
  relocations. When using symbol relative relocations, the linker
  interprets the relocations as section relative. Thus writing the wrong
  values to the debug sections.

Fixes #303
@bjorn3 bjorn3 mentioned this issue Oct 19, 2019
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-debuginfo Area: Debugging information at runtime in generated code. C-bug Category: This is a bug. O-macos Operating system: MacOS
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant