diff --git a/lib/MC/MachObjectWriter.cpp b/lib/MC/MachObjectWriter.cpp index 3ca67080053..00c14ef9de1 100644 --- a/lib/MC/MachObjectWriter.cpp +++ b/lib/MC/MachObjectWriter.cpp @@ -609,10 +609,31 @@ public: Type = RIT_X86_64_GOTLoad; else Type = RIT_X86_64_GOT; - } else if (Modifier != MCSymbolRefExpr::VK_None) + } else if (Modifier != MCSymbolRefExpr::VK_None) { report_fatal_error("unsupported symbol modifier in relocation"); - else + } else { Type = RIT_X86_64_Signed; + + // The Darwin x86_64 relocation format has a problem where it cannot + // encode an address (L + ) which is outside the atom + // containing L. Generally, this shouldn't occur but it does + // happen when we have a RIPrel instruction with data following the + // relocation entry (e.g., movb $012, L0(%rip)). Even with the PCrel + // adjustment Darwin x86_64 uses, the offset is still negative and + // the linker has no way to recognize this. + // + // To work around this, Darwin uses several special relocation types + // to indicate the offsets. However, the specification or + // implementation of these seems to also be incomplete; they should + // adjust the addend as well based on the actual encoded instruction + // (the additional bias), but instead appear to just look at the + // final offset. + switch (-(Target.getConstant() + (1LL << Log2Size))) { + case 1: Type = RIT_X86_64_Signed1; break; + case 2: Type = RIT_X86_64_Signed2; break; + case 4: Type = RIT_X86_64_Signed4; break; + } + } } else { if (Modifier != MCSymbolRefExpr::VK_None) report_fatal_error("unsupported symbol modifier in branch " @@ -620,27 +641,6 @@ public: Type = RIT_X86_64_Branch; } - - // The Darwin x86_64 relocation format has a problem where it cannot - // encode an address (L + ) which is outside the atom - // containing L. Generally, this shouldn't occur but it does happen - // when we have a RIPrel instruction with data following the relocation - // entry (e.g., movb $012, L0(%rip)). Even with the PCrel adjustment - // Darwin x86_64 uses, the offset is still negative and the linker has - // no way to recognize this. - // - // To work around this, Darwin uses several special relocation types to - // indicate the offsets. However, the specification or implementation of - // these seems to also be incomplete; they should adjust the addend as - // well based on the actual encoded instruction (the additional bias), - // but instead appear to just look at the final offset. - if (IsRIPRel) { - switch (-(Target.getConstant() + (1LL << Log2Size))) { - case 1: Type = RIT_X86_64_Signed1; break; - case 2: Type = RIT_X86_64_Signed2; break; - case 4: Type = RIT_X86_64_Signed4; break; - } - } } else { if (Modifier == MCSymbolRefExpr::VK_GOT) { Type = RIT_X86_64_GOT; diff --git a/test/MC/MachO/darwin-x86_64-reloc.s b/test/MC/MachO/darwin-x86_64-reloc.s index 248c507b42a..19b434565a2 100644 --- a/test/MC/MachO/darwin-x86_64-reloc.s +++ b/test/MC/MachO/darwin-x86_64-reloc.s @@ -84,6 +84,9 @@ L6: .quad f6 .quad L6 + .text + cmpq $0, _foo@GOTPCREL(%rip) + // CHECK: ('cputype', 16777223) // CHECK: ('cpusubtype', 3) // CHECK: ('filetype', 1) @@ -97,9 +100,9 @@ L6: // CHECK: ('size', 392) // CHECK: ('segment_name', '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') // CHECK: ('vm_addr', 0) -// CHECK: ('vm_size', 303) +// CHECK: ('vm_size', 311) // CHECK: ('file_offset', 528) -// CHECK: ('file_size', 303) +// CHECK: ('file_size', 311) // CHECK: ('maxprot', 7) // CHECK: ('initprot', 7) // CHECK: ('num_sections', 4) @@ -112,7 +115,7 @@ L6: // CHECK: ('size', 40) // CHECK: ('offset', 528) // CHECK: ('alignment', 0) -// CHECK: ('reloc_offset', 832) +// CHECK: ('reloc_offset', 840) // CHECK: ('num_reloc', 5) // CHECK: ('flags', 0x0) // CHECK: ('reserved1', 0) @@ -136,16 +139,16 @@ L6: // CHECK: (('word-0', 0x4), // CHECK: ('word-1', 0x4d000008)), // CHECK: ]) -// CHECK: ('_section_data', "\x00\x00\x00\x00\x04\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x17\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'\x01\x00\x00\x00\x00\x00\x00") +// CHECK: ('_section_data', '\x00\x00\x00\x00\x04\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x1f\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00/\x01\x00\x00\x00\x00\x00\x00') // CHECK: # Section 1 // CHECK: (('section_name', '__text\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') // CHECK: ('segment_name', '__TEXT\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') // CHECK: ('address', 40) -// CHECK: ('size', 215) +// CHECK: ('size', 223) // CHECK: ('offset', 568) // CHECK: ('alignment', 0) -// CHECK: ('reloc_offset', 872) -// CHECK: ('num_reloc', 31) +// CHECK: ('reloc_offset', 880) +// CHECK: ('num_reloc', 32) // CHECK: ('flags', 0x80000400) // CHECK: ('reserved1', 0) // CHECK: ('reserved2', 0) @@ -153,108 +156,111 @@ L6: // CHECK: ), // CHECK: ('_relocations', [ // CHECK: # Relocation 0 +// CHECK: (('word-0', 0xda), +// CHECK: ('word-1', 0x4d000000)), +// CHECK: # Relocation 1 // CHECK: (('word-0', 0xd3), // CHECK: ('word-1', 0x15000004)), -// CHECK: # Relocation 1 +// CHECK: # Relocation 2 // CHECK: (('word-0', 0xcd), // CHECK: ('word-1', 0x1d000006)), -// CHECK: # Relocation 2 +// CHECK: # Relocation 3 // CHECK: (('word-0', 0xc7), // CHECK: ('word-1', 0x15000004)), -// CHECK: # Relocation 3 +// CHECK: # Relocation 4 // CHECK: (('word-0', 0xc1), // CHECK: ('word-1', 0x15000001)), -// CHECK: # Relocation 4 -// CHECK: (('word-0', 0xa5), -// CHECK: ('word-1', 0x5e000003)), // CHECK: # Relocation 5 // CHECK: (('word-0', 0xa5), -// CHECK: ('word-1', 0xe000000)), -// CHECK: # Relocation 6 -// CHECK: (('word-0', 0x9d), // CHECK: ('word-1', 0x5e000003)), +// CHECK: # Relocation 6 +// CHECK: (('word-0', 0xa5), +// CHECK: ('word-1', 0xe000000)), // CHECK: # Relocation 7 // CHECK: (('word-0', 0x9d), -// CHECK: ('word-1', 0xe000000)), +// CHECK: ('word-1', 0x5e000003)), // CHECK: # Relocation 8 +// CHECK: (('word-0', 0x9d), +// CHECK: ('word-1', 0xe000000)), +// CHECK: # Relocation 9 // CHECK: (('word-0', 0x95), // CHECK: ('word-1', 0xe000003)), -// CHECK: # Relocation 9 +// CHECK: # Relocation 10 // CHECK: (('word-0', 0x8d), // CHECK: ('word-1', 0xe000003)), -// CHECK: # Relocation 10 +// CHECK: # Relocation 11 // CHECK: (('word-0', 0x79), // CHECK: ('word-1', 0x8d000003)), -// CHECK: # Relocation 11 +// CHECK: # Relocation 12 // CHECK: (('word-0', 0x71), // CHECK: ('word-1', 0x7d000003)), -// CHECK: # Relocation 12 +// CHECK: # Relocation 13 // CHECK: (('word-0', 0x69), // CHECK: ('word-1', 0x6d000003)), -// CHECK: # Relocation 13 +// CHECK: # Relocation 14 // CHECK: (('word-0', 0x63), // CHECK: ('word-1', 0x1d000003)), -// CHECK: # Relocation 14 +// CHECK: # Relocation 15 // CHECK: (('word-0', 0x5c), // CHECK: ('word-1', 0x1d000003)), -// CHECK: # Relocation 15 -// CHECK: (('word-0', 0x55), -// CHECK: ('word-1', 0x5c000002)), // CHECK: # Relocation 16 // CHECK: (('word-0', 0x55), -// CHECK: ('word-1', 0xc000000)), +// CHECK: ('word-1', 0x5c000002)), // CHECK: # Relocation 17 -// CHECK: (('word-0', 0x4d), -// CHECK: ('word-1', 0x5e000002)), +// CHECK: (('word-0', 0x55), +// CHECK: ('word-1', 0xc000000)), // CHECK: # Relocation 18 // CHECK: (('word-0', 0x4d), -// CHECK: ('word-1', 0xe000000)), -// CHECK: # Relocation 19 -// CHECK: (('word-0', 0x45), // CHECK: ('word-1', 0x5e000002)), +// CHECK: # Relocation 19 +// CHECK: (('word-0', 0x4d), +// CHECK: ('word-1', 0xe000000)), // CHECK: # Relocation 20 // CHECK: (('word-0', 0x45), -// CHECK: ('word-1', 0xe000000)), +// CHECK: ('word-1', 0x5e000002)), // CHECK: # Relocation 21 -// CHECK: (('word-0', 0x3d), +// CHECK: (('word-0', 0x45), // CHECK: ('word-1', 0xe000000)), // CHECK: # Relocation 22 -// CHECK: (('word-0', 0x35), +// CHECK: (('word-0', 0x3d), // CHECK: ('word-1', 0xe000000)), // CHECK: # Relocation 23 +// CHECK: (('word-0', 0x35), +// CHECK: ('word-1', 0xe000000)), +// CHECK: # Relocation 24 // CHECK: (('word-0', 0x2d), // CHECK: ('word-1', 0x8d000000)), -// CHECK: # Relocation 24 +// CHECK: # Relocation 25 // CHECK: (('word-0', 0x26), // CHECK: ('word-1', 0x6d000000)), -// CHECK: # Relocation 25 +// CHECK: # Relocation 26 // CHECK: (('word-0', 0x20), // CHECK: ('word-1', 0x1d000000)), -// CHECK: # Relocation 26 +// CHECK: # Relocation 27 // CHECK: (('word-0', 0x1a), // CHECK: ('word-1', 0x1d000000)), -// CHECK: # Relocation 27 +// CHECK: # Relocation 28 // CHECK: (('word-0', 0x14), // CHECK: ('word-1', 0x4d000000)), -// CHECK: # Relocation 28 +// CHECK: # Relocation 29 // CHECK: (('word-0', 0xe), // CHECK: ('word-1', 0x3d000000)), -// CHECK: # Relocation 29 +// CHECK: # Relocation 30 // CHECK: (('word-0', 0x7), // CHECK: ('word-1', 0x2d000000)), -// CHECK: # Relocation 30 +// CHECK: # Relocation 31 // CHECK: (('word-0', 0x2), // CHECK: ('word-1', 0x2d000000)), // CHECK: ]) -// CHECK: ('_section_data', '\xc3\xe8\x00\x00\x00\x00\xe8\x04\x00\x00\x00H\x8b\x05\x00\x00\x00\x00\xff5\x00\x00\x00\x00\x8b\x05\x00\x00\x00\x00\x8b\x05\x04\x00\x00\x00\xc6\x05\xff\xff\xff\xff\x12\xc7\x05\xfc\xff\xff\xffxV4\x12\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00H\x8d\x05,\x00\x00\x00H\x8d\x05\x14\x00\x00\x00\x83\x05\x13\x00\x00\x00\x06f\x81\x05\x12\x00\x00\x00\xf4\x01\x81\x05\x10\x00\x00\x00\xf4\x01\x00\x00\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90,\x00\x00\x00\x00\x00\x00\x00\x14\x00\x00\x00\x00\x00\x00\x00\xe4\xff\xff\xff\xff\xff\xff\xff\xd4\xff\xff\xff\xff\xff\xff\xff,\x00\x00\x00\x00\x00\x00\x00\x83\xc0\x00\x03\x04%\x03\x00\x00\x00\x8b\x05\x1f\xff\xff\xff\x8b\x05$\x00\x00\x00\x8b\x05\x00\x00\x00\x00\x8b\x05(\x00\x00\x00') +// CHECK: ('_section_data', '\xc3\xe8\x00\x00\x00\x00\xe8\x04\x00\x00\x00H\x8b\x05\x00\x00\x00\x00\xff5\x00\x00\x00\x00\x8b\x05\x00\x00\x00\x00\x8b\x05\x04\x00\x00\x00\xc6\x05\xff\xff\xff\xff\x12\xc7\x05\xfc\xff\xff\xffxV4\x12\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00H\x8d\x05,\x00\x00\x00H\x8d\x05\x14\x00\x00\x00\x83\x05\x13\x00\x00\x00\x06f\x81\x05\x12\x00\x00\x00\xf4\x01\x81\x05\x10\x00\x00\x00\xf4\x01\x00\x00\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90,\x00\x00\x00\x00\x00\x00\x00\x14\x00\x00\x00\x00\x00\x00\x00\xe4\xff\xff\xff\xff\xff\xff\xff\xd4\xff\xff\xff\xff\xff\xff\xff,\x00\x00\x00\x00\x00\x00\x00\x83\xc0\x00\x03\x04%\x03\x00\x00\x00\x8b\x05\x1f\xff\xff\xff\x8b\x05,\x00\x00\x00\x8b\x05\x00\x00\x00\x00\x8b\x050\x00\x00\x00H\x83=\xff\xff\xff\xff\x00') // CHECK: # Section 2 // CHECK: (('section_name', '__debug_frame\x00\x00\x00') // CHECK: ('segment_name', '__DWARF\x00\x00\x00\x00\x00\x00\x00\x00\x00') -// CHECK: ('address', 255) +// CHECK: ('address', 263) // CHECK: ('size', 16) -// CHECK: ('offset', 783) +// CHECK: ('offset', 791) // CHECK: ('alignment', 0) -// CHECK: ('reloc_offset', 1120) +// CHECK: ('reloc_offset', 1136) // CHECK: ('num_reloc', 2) // CHECK: ('flags', 0x2000000) // CHECK: ('reserved1', 0) @@ -273,9 +279,9 @@ L6: // CHECK: # Section 3 // CHECK: (('section_name', '__literal8\x00\x00\x00\x00\x00\x00') // CHECK: ('segment_name', '__TEXT\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') -// CHECK: ('address', 271) +// CHECK: ('address', 279) // CHECK: ('size', 32) -// CHECK: ('offset', 799) +// CHECK: ('offset', 807) // CHECK: ('alignment', 0) // CHECK: ('reloc_offset', 0) // CHECK: ('num_reloc', 0) @@ -292,9 +298,9 @@ L6: // CHECK: # Load Command 1 // CHECK: (('command', 2) // CHECK: ('size', 24) -// CHECK: ('symoff', 1136) +// CHECK: ('symoff', 1152) // CHECK: ('nsyms', 9) -// CHECK: ('stroff', 1280) +// CHECK: ('stroff', 1296) // CHECK: ('strsize', 52) // CHECK: ('_string_data', '\x00_foobar\x00_ext_foo\x00_foo\x00_baz\x00_bar\x00_prev\x00_f2\x00_f3\x00f6\x00\x00\x00') // CHECK: ('_symbols', [ @@ -351,7 +357,7 @@ L6: // CHECK: ('n_type', 0xe) // CHECK: ('n_sect', 4) // CHECK: ('n_desc', 0) -// CHECK: ('n_value', 287) +// CHECK: ('n_value', 295) // CHECK: ('_string', 'f6') // CHECK: ), // CHECK: # Symbol 7