diff --git a/lib/MC/MachObjectWriter.cpp b/lib/MC/MachObjectWriter.cpp index 4b0077efb0c..824928a0206 100644 --- a/lib/MC/MachObjectWriter.cpp +++ b/lib/MC/MachObjectWriter.cpp @@ -34,6 +34,7 @@ static unsigned getFixupKindLog2Size(unsigned Kind) { case FK_Data_2: return 1; case X86::reloc_pcrel_4byte: case X86::reloc_riprel_4byte: + case X86::reloc_riprel_4byte_movq_load: case FK_Data_4: return 2; case FK_Data_8: return 3; } @@ -46,10 +47,16 @@ static bool isFixupKindPCRel(unsigned Kind) { case X86::reloc_pcrel_1byte: case X86::reloc_pcrel_4byte: case X86::reloc_riprel_4byte: + case X86::reloc_riprel_4byte_movq_load: return true; } } +static bool isFixupKindRIPRel(unsigned Kind) { + return Kind == X86::reloc_riprel_4byte || + Kind == X86::reloc_riprel_4byte_movq_load; +} + namespace { class MachObjectWriterImpl { @@ -125,6 +132,19 @@ class MachObjectWriterImpl { RIT_LocalDifference = 4 }; + /// X86_64 uses its own relocation types. + enum RelocationInfoTypeX86_64 { + RIT_X86_64_Unsigned = 0, + RIT_X86_64_Signed = 1, + RIT_X86_64_Branch = 2, + RIT_X86_64_GOTLoad = 3, + RIT_X86_64_GOT = 4, + RIT_X86_64_Subtractor = 5, + RIT_X86_64_Signed1 = 6, + RIT_X86_64_Signed2 = 7, + RIT_X86_64_Signed4 = 8 + }; + /// MachSymbolData - Helper struct for containing some precomputed information /// on symbols. struct MachSymbolData { @@ -417,6 +437,194 @@ public: Write32(Address); } + void RecordX86_64Relocation(const MCAssembler &Asm, + const MCDataFragment &Fragment, + const MCAsmFixup &Fixup, MCValue Target, + uint64_t &FixedValue) { + unsigned IsPCRel = isFixupKindPCRel(Fixup.Kind); + unsigned IsRIPRel = isFixupKindRIPRel(Fixup.Kind); + unsigned Log2Size = getFixupKindLog2Size(Fixup.Kind); + + // See . + uint32_t Address = Fragment.getOffset() + Fixup.Offset; + int64_t Value = 0; + unsigned Index = 0; + unsigned IsExtern = 0; + unsigned Type = 0; + + Value = Target.getConstant(); + + if (IsPCRel) { + // Compensate for the relocation offset, Darwin x86_64 relocations only + // have the addend and appear to have attempted to define it to be the + // actual expression addend without the PCrel bias. However, instructions + // with data following the relocation are not accomodated for (see comment + // below regarding SIGNED{1,2,4}), so it isn't exactly that either. + Value += 1 << Log2Size; + } + + if (Target.isAbsolute()) { // constant + // SymbolNum of 0 indicates the absolute section. + Type = RIT_X86_64_Unsigned; + Index = 0; + + // FIXME: I believe this is broken, I don't think the linker can + // understand it. I think it would require a local relocation, but I'm not + // sure if that would work either. The official way to get an absolute + // PCrel relocation is to use an absolute symbol (which we don't support + // yet). + if (IsPCRel) { + IsExtern = 1; + Type = RIT_X86_64_Branch; + } + } else if (Target.getSymB()) { // A - B + constant + const MCSymbol *A = &Target.getSymA()->getSymbol(); + MCSymbolData &A_SD = Asm.getSymbolData(*A); + const MCSymbolData *A_Base = Asm.getAtom(&A_SD); + + const MCSymbol *B = &Target.getSymB()->getSymbol(); + MCSymbolData &B_SD = Asm.getSymbolData(*B); + const MCSymbolData *B_Base = Asm.getAtom(&B_SD); + + // Neither symbol can be modified. + if (Target.getSymA()->getKind() != MCSymbolRefExpr::VK_None || + Target.getSymB()->getKind() != MCSymbolRefExpr::VK_None) + llvm_report_error("unsupported relocation of modified symbol"); + + // We don't support PCrel relocations of differences. Darwin 'as' doesn't + // implement most of these correctly. + if (IsPCRel) + llvm_report_error("unsupported pc-relative relocation of difference"); + + // We don't currently support any situation where one or both of the + // symbols would require a local relocation. This is almost certainly + // unused and may not be possible to encode correctly. + if (!A_Base || !B_Base) + llvm_report_error("unsupported local relocations in difference"); + + // Darwin 'as' doesn't emit correct relocations for this (it ends up with + // a single SIGNED relocation); reject it for now. + if (A_Base == B_Base) + llvm_report_error("unsupported relocation with identical base"); + + Value += A_SD.getAddress() - A_Base->getAddress(); + Value -= B_SD.getAddress() - B_Base->getAddress(); + + Index = A_Base->getIndex(); + IsExtern = 1; + Type = RIT_X86_64_Unsigned; + + MachRelocationEntry MRE; + MRE.Word0 = Address; + MRE.Word1 = ((Index << 0) | + (IsPCRel << 24) | + (Log2Size << 25) | + (IsExtern << 27) | + (Type << 28)); + Relocations[Fragment.getParent()].push_back(MRE); + + Index = B_Base->getIndex(); + IsExtern = 1; + Type = RIT_X86_64_Subtractor; + } else { + const MCSymbol *Symbol = &Target.getSymA()->getSymbol(); + MCSymbolData &SD = Asm.getSymbolData(*Symbol); + const MCSymbolData *Base = Asm.getAtom(&SD); + + // x86_64 almost always uses external relocations, except when there is no + // symbol to use as a base address (a local symbol with no preceeding + // non-local symbol). + if (Base) { + Index = Base->getIndex(); + IsExtern = 1; + + // Add the local offset, if needed. + if (Base != &SD) + Value += SD.getAddress() - Base->getAddress(); + } else { + // The index is the section ordinal. + // + // FIXME: O(N) + Index = 1; + MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end(); + for (; it != ie; ++it, ++Index) + if (&*it == SD.getFragment()->getParent()) + break; + assert(it != ie && "Unable to find section index!"); + IsExtern = 0; + Value += SD.getAddress(); + + if (IsPCRel) + Value -= Address + (1 << Log2Size); + } + + MCSymbolRefExpr::VariantKind Modifier = Target.getSymA()->getKind(); + if (IsPCRel) { + if (IsRIPRel) { + if (Modifier == MCSymbolRefExpr::VK_GOTPCREL) { + // x86_64 distinguishes movq foo@GOTPCREL so that the linker can + // rewrite the movq to an leaq at link time if the symbol ends up in + // the same linkage unit. + if (unsigned(Fixup.Kind) == X86::reloc_riprel_4byte_movq_load) + Type = RIT_X86_64_GOTLoad; + else + Type = RIT_X86_64_GOT; + } else if (Modifier != MCSymbolRefExpr::VK_None) + llvm_report_error("unsupported symbol modifier in relocation"); + else + Type = RIT_X86_64_Signed; + } else { + if (Modifier != MCSymbolRefExpr::VK_None) + llvm_report_error("unsupported symbol modifier in branch " + "relocation"); + + 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() + (1 << 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; + else if (Modifier != MCSymbolRefExpr::VK_None) + llvm_report_error("unsupported symbol modifier in relocation"); + else + Type = RIT_X86_64_Unsigned; + } + } + + // x86_64 always writes custom values into the fixups. + FixedValue = Value; + + // struct relocation_info (8 bytes) + MachRelocationEntry MRE; + MRE.Word0 = Address; + MRE.Word1 = ((Index << 0) | + (IsPCRel << 24) | + (Log2Size << 25) | + (IsExtern << 27) | + (Type << 28)); + Relocations[Fragment.getParent()].push_back(MRE); + } + void RecordScatteredRelocation(const MCAssembler &Asm, const MCFragment &Fragment, const MCAsmFixup &Fixup, MCValue Target, @@ -479,6 +687,11 @@ public: const MCDataFragment &Fragment, const MCAsmFixup &Fixup, MCValue Target, uint64_t &FixedValue) { + if (Is64Bit) { + RecordX86_64Relocation(Asm, Fragment, Fixup, Target, FixedValue); + return; + } + unsigned IsPCRel = isFixupKindPCRel(Fixup.Kind); unsigned Log2Size = getFixupKindLog2Size(Fixup.Kind); diff --git a/test/MC/MachO/darwin-x86_64-diff-relocs.s b/test/MC/MachO/darwin-x86_64-diff-relocs.s new file mode 100644 index 00000000000..38fa074fde2 --- /dev/null +++ b/test/MC/MachO/darwin-x86_64-diff-relocs.s @@ -0,0 +1,329 @@ +// RUN: llvm-mc -triple x86_64-apple-darwin10 %s -filetype=obj -o - | macho-dump --dump-section-data | FileCheck %s + + .text + +// FIXME: llvm-mc doesn't handle this in a way we can make compatible with 'as', +// currently, because of how we handle assembler variables. +// +// See improve handling of absolute symbols + +// _baz = 4 + +_foo: + xorl %eax,%eax +_g0: + xorl %eax,%eax +L0: + jmp 4 +// jmp _baz + +// FIXME: Darwin 'as' for historical reasons widens this jump, but doesn't emit +// a relocation. It seems like 'as' widens any jump that is not to a temporary, +// which is inherited from the x86_32 behavior, even though x86_64 could do +// better. +// jmp _g0 + + jmp L0 + jmp _g1 + +// FIXME: Darwin 'as' gets this wrong as well, even though it could get it right +// given the other things we do on x86_64. It is using a short jump here. This +// is probably fallout of the hack that exists for x86_32. +// jmp L1 + +// FIXME: We don't support this, and would currently get it wrong, it should be a jump to an absolute address. +// jmp L0 - _g0 + +// jmp _g1 - _g0 +// FIXME: Darwin 'as' comes up with 'SIGNED' here instead of 'BRANCH'. +// jmp _g1 - L1 +// FIXME: Darwin 'as' gets this completely wrong. It ends up with a single +// branch relocation. Fallout from the other delta hack? +// jmp L1 - _g0 + + jmp _g2 + jmp L2 + jmp _g3 + jmp L3 +// FIXME: Darwin 'as' gets this completely wrong. It ends up with a single +// branch relocation. Fallout from the other delta hack? +// jmp L2 - _g3 +// jmp _g3 - _g2 +// FIXME: Darwin 'as' comes up with 'SIGNED' here instead of 'BRANCH'. +// jmp _g3 - L3 +// FIXME: Darwin 'as' gets this completely wrong. It ends up with a single +// branch relocation. Fallout from the other delta hack? +// jmp L3 - _g2 + + movl %eax,4(%rip) +// movl %eax,_baz(%rip) + movl %eax,_g0(%rip) + movl %eax,L0(%rip) + movl %eax,_g1(%rip) + movl %eax,L1(%rip) + +// FIXME: Darwin 'as' gets most of these wrong, and there is an ambiguity in ATT +// syntax in what they should mean in the first place (absolute or +// rip-relative address). +// movl %eax,L0 - _g0(%rip) +// movl %eax,_g1 - _g0(%rip) +// movl %eax,_g1 - L1(%rip) +// movl %eax,L1 - _g0(%rip) + + movl %eax,_g2(%rip) + movl %eax,L2(%rip) + movl %eax,_g3(%rip) + movl %eax,L3(%rip) + +// FIXME: Darwin 'as' gets most of these wrong, and there is an ambiguity in ATT +// syntax in what they should mean in the first place (absolute or +// rip-relative address). +// movl %eax,L2 - _g2(%rip) +// movl %eax,_g3 - _g2(%rip) +// movl %eax,_g3 - L3(%rip) +// movl %eax,L3 - _g2(%rip) + +_g1: + xorl %eax,%eax +L1: + xorl %eax,%eax + + .data +_g2: + xorl %eax,%eax +L2: + .quad 4 +// .quad _baz + .quad _g2 + .quad L2 + .quad _g3 + .quad L3 + .quad L2 - _g2 + .quad _g3 - _g2 + .quad L3 - _g2 + .quad L3 - _g3 + + .quad _g0 + .quad L0 + .quad _g1 + .quad L1 + .quad L0 - _g0 + .quad _g1 - _g0 + .quad L1 - _g0 + .quad L1 - _g1 + +_g3: + xorl %eax,%eax +L3: + xorl %eax,%eax + +// CHECK: ('cputype', 16777223) +// CHECK: ('cpusubtype', 3) +// CHECK: ('filetype', 1) +// CHECK: ('num_load_commands', 1) +// CHECK: ('load_commands_size', 336) +// CHECK: ('flag', 0) +// CHECK: ('reserved', 0) +// CHECK: ('load_commands', [ +// CHECK: # Load Command 0 +// CHECK: (('command', 25) +// CHECK: ('size', 232) +// 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', 236) +// CHECK: ('file_offset', 368) +// CHECK: ('file_size', 236) +// CHECK: ('maxprot', 7) +// CHECK: ('initprot', 7) +// CHECK: ('num_sections', 2) +// CHECK: ('flags', 0) +// CHECK: ('sections', [ +// CHECK: # Section 0 +// 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', 0) +// CHECK: ('size', 94) +// CHECK: ('offset', 368) +// CHECK: ('alignment', 0) +// CHECK: ('reloc_offset', 604) +// CHECK: ('num_reloc', 12) +// CHECK: ('flags', 0x80000400) +// CHECK: ('reserved1', 0) +// CHECK: ('reserved2', 0) +// CHECK: ('reserved3', 0) +// CHECK: ), +// CHECK: ('_relocations', [ + +// FIXME: Unfortunately, we do not get these relocations in exactly the same +// order as Darwin 'as'. It turns out that 'as' *usually* ends up emitting +// them in reverse address order, but sometimes it allocates some +// additional relocations late so these end up preceed the other entries. I +// haven't figured out the exact criteria for this yet. + +// CHECK: (('word-0', 0x56), +// CHECK: ('word-1', 0x1d000004)), +// CHECK: (('word-0', 0x50), +// CHECK: ('word-1', 0x1d000004)), +// CHECK: (('word-0', 0x4a), +// CHECK: ('word-1', 0x1d000003)), +// CHECK: (('word-0', 0x44), +// CHECK: ('word-1', 0x1d000003)), +// CHECK: (('word-0', 0x3e), +// CHECK: ('word-1', 0x1d000002)), +// CHECK: (('word-0', 0x38), +// CHECK: ('word-1', 0x1d000002)), +// CHECK: (('word-0', 0x20), +// CHECK: ('word-1', 0x2d000004)), +// CHECK: (('word-0', 0x1b), +// CHECK: ('word-1', 0x2d000004)), +// CHECK: (('word-0', 0x16), +// CHECK: ('word-1', 0x2d000003)), +// CHECK: (('word-0', 0x11), +// CHECK: ('word-1', 0x2d000003)), +// CHECK: (('word-0', 0xc), +// CHECK: ('word-1', 0x2d000002)), +// CHECK: (('word-0', 0x5), +// CHECK: ('word-1', 0x2d000000)), +// CHECK: ]) +// CHECK: # Section 1 +// CHECK: (('section_name', '__data\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') +// CHECK: ('segment_name', '__DATA\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') +// CHECK: ('address', 94) +// CHECK: ('size', 142) +// CHECK: ('offset', 462) +// CHECK: ('alignment', 0) +// CHECK: ('reloc_offset', 700) +// CHECK: ('num_reloc', 16) +// CHECK: ('flags', 0x400) +// CHECK: ('reserved1', 0) +// CHECK: ('reserved2', 0) +// CHECK: ('reserved3', 0) +// CHECK: ), +// CHECK: ('_relocations', [ +// CHECK: # Relocation 0 +// CHECK: (('word-0', 0x7a), +// CHECK: ('word-1', 0x5e000001)), +// CHECK: # Relocation 1 +// CHECK: (('word-0', 0x7a), +// CHECK: ('word-1', 0xe000002)), +// CHECK: # Relocation 2 +// CHECK: (('word-0', 0x72), +// CHECK: ('word-1', 0x5e000001)), +// CHECK: # Relocation 3 +// CHECK: (('word-0', 0x72), +// CHECK: ('word-1', 0xe000002)), +// CHECK: # Relocation 4 +// CHECK: (('word-0', 0x62), +// CHECK: ('word-1', 0xe000002)), +// CHECK: # Relocation 5 +// CHECK: (('word-0', 0x5a), +// CHECK: ('word-1', 0xe000002)), +// CHECK: # Relocation 6 +// CHECK: (('word-0', 0x52), +// CHECK: ('word-1', 0xe000001)), +// CHECK: # Relocation 7 +// CHECK: (('word-0', 0x4a), +// CHECK: ('word-1', 0xe000001)), +// CHECK: # Relocation 8 +// CHECK: (('word-0', 0x3a), +// CHECK: ('word-1', 0x5e000003)), +// CHECK: # Relocation 9 +// CHECK: (('word-0', 0x3a), +// CHECK: ('word-1', 0xe000004)), +// CHECK: # Relocation 10 +// CHECK: (('word-0', 0x32), +// CHECK: ('word-1', 0x5e000003)), +// CHECK: # Relocation 11 +// CHECK: (('word-0', 0x32), +// CHECK: ('word-1', 0xe000004)), +// CHECK: # Relocation 12 +// CHECK: (('word-0', 0x22), +// CHECK: ('word-1', 0xe000004)), +// CHECK: # Relocation 13 +// CHECK: (('word-0', 0x1a), +// CHECK: ('word-1', 0xe000004)), +// CHECK: # Relocation 14 +// CHECK: (('word-0', 0x12), +// CHECK: ('word-1', 0xe000003)), +// CHECK: # Relocation 15 +// CHECK: (('word-0', 0xa), +// CHECK: ('word-1', 0xe000003)), +// CHECK: ]) +// CHECK: ]) +// CHECK: ), +// CHECK: # Load Command 1 +// CHECK: (('command', 2) +// CHECK: ('size', 24) +// CHECK: ('symoff', 828) +// CHECK: ('nsyms', 5) +// CHECK: ('stroff', 908) +// CHECK: ('strsize', 24) +// CHECK: ('_string_data', '\x00_foo\x00_g0\x00_g1\x00_g2\x00_g3\x00\x00\x00') +// CHECK: ('_symbols', [ +// CHECK: # Symbol 0 +// CHECK: (('n_strx', 1) +// CHECK: ('n_type', 0xe) +// CHECK: ('n_sect', 1) +// CHECK: ('n_desc', 0) +// CHECK: ('n_value', 0) +// CHECK: ('_string', '_foo') +// CHECK: ), +// CHECK: # Symbol 1 +// CHECK: (('n_strx', 6) +// CHECK: ('n_type', 0xe) +// CHECK: ('n_sect', 1) +// CHECK: ('n_desc', 0) +// CHECK: ('n_value', 2) +// CHECK: ('_string', '_g0') +// CHECK: ), +// CHECK: # Symbol 2 +// CHECK: (('n_strx', 10) +// CHECK: ('n_type', 0xe) +// CHECK: ('n_sect', 1) +// CHECK: ('n_desc', 0) +// CHECK: ('n_value', 90) +// CHECK: ('_string', '_g1') +// CHECK: ), +// CHECK: # Symbol 3 +// CHECK: (('n_strx', 14) +// CHECK: ('n_type', 0xe) +// CHECK: ('n_sect', 2) +// CHECK: ('n_desc', 0) +// CHECK: ('n_value', 94) +// CHECK: ('_string', '_g2') +// CHECK: ), +// CHECK: # Symbol 4 +// CHECK: (('n_strx', 18) +// CHECK: ('n_type', 0xe) +// CHECK: ('n_sect', 2) +// CHECK: ('n_desc', 0) +// CHECK: ('n_value', 232) +// CHECK: ('_string', '_g3') +// CHECK: ), +// CHECK: ]) +// CHECK: ), +// CHECK: # Load Command 2 +// CHECK: (('command', 11) +// CHECK: ('size', 80) +// CHECK: ('ilocalsym', 0) +// CHECK: ('nlocalsym', 5) +// CHECK: ('iextdefsym', 5) +// CHECK: ('nextdefsym', 0) +// CHECK: ('iundefsym', 5) +// CHECK: ('nundefsym', 0) +// CHECK: ('tocoff', 0) +// CHECK: ('ntoc', 0) +// CHECK: ('modtaboff', 0) +// CHECK: ('nmodtab', 0) +// CHECK: ('extrefsymoff', 0) +// CHECK: ('nextrefsyms', 0) +// CHECK: ('indirectsymoff', 0) +// CHECK: ('nindirectsyms', 0) +// CHECK: ('extreloff', 0) +// CHECK: ('nextrel', 0) +// CHECK: ('locreloff', 0) +// CHECK: ('nlocrel', 0) +// CHECK: ('_indirect_symbols', [ +// CHECK: ]) +// CHECK: ), +// CHECK: ]) diff --git a/test/MC/MachO/darwin-x86_64-reloc-offsets.s b/test/MC/MachO/darwin-x86_64-reloc-offsets.s new file mode 100644 index 00000000000..ab6820e4a82 --- /dev/null +++ b/test/MC/MachO/darwin-x86_64-reloc-offsets.s @@ -0,0 +1,343 @@ +// RUN: llvm-mc -triple x86_64-apple-darwin10 %s -filetype=obj -o - | macho-dump --dump-section-data | FileCheck %s + + .data + + .org 0x10 +L0: + .long 0 + .long 0 + .long 0 + .long 0 + +_d: + .long 0 +L1: + .long 0 + + .text + +// These generate normal x86_64 (external) relocations. They could all use +// SIGNED, but don't for pedantic compatibility with Darwin 'as'. + + // SIGNED1 + movb $0x12, _d(%rip) + + // SIGNED + movb $0x12, _d + 1(%rip) + + // SIGNED4 + movl $0x12345678, _d(%rip) + + // SIGNED + movl $0x12345678, _d + 1(%rip) + + // SIGNED2 + movl $0x12345678, _d + 2(%rip) + + // SIGNED1 + movl $0x12345678, _d + 3(%rip) + + // SIGNED + movl $0x12345678, _d + 4(%rip) + + movb %al, _d(%rip) + movb %al, _d + 1(%rip) + movl %eax, _d(%rip) + movl %eax, _d + 1(%rip) + movl %eax, _d + 2(%rip) + movl %eax, _d + 3(%rip) + movl %eax, _d + 4(%rip) + +// These have to use local relocations. Since that uses an offset into the +// section in x86_64 (as opposed to a scattered relocation), and since the +// linker can only decode this to an atom + offset by scanning the section, +// it is not possible to correctly encode these without SIGNED. This is +// ultimately due to a design flaw in the x86_64 relocation format, it is +// not possible to encode an address (L + ) which is outside the +// atom containing L. + + // SIGNED1 + movb $0x12, L0(%rip) + + // SIGNED + movb $0x12, L0 + 1(%rip) + + // SIGNED4 + movl $0x12345678, L0(%rip) + + // SIGNED + movl $0x12345678, L0 + 1(%rip) + + // SIGNED2 + movl $0x12345678, L0 + 2(%rip) + + // SIGNED1 + movl $0x12345678, L0 + 3(%rip) + + // SIGNED + movl $0x12345678, L0 + 4(%rip) + + movb %al, L0(%rip) + movb %al, L0 + 1(%rip) + movl %eax, L0(%rip) + movl %eax, L0 + 1(%rip) + movl %eax, L0 + 2(%rip) + movl %eax, L0 + 3(%rip) + movl %eax, L0 + 4(%rip) + + // SIGNED1 + movb $0x12, L1(%rip) + + // SIGNED + movb $0x12, L1 + 1(%rip) + + // SIGNED4 + movl $0x12345678, L1(%rip) + + // SIGNED + movl $0x12345678, L1 + 1(%rip) + + // SIGNED2 + movl $0x12345678, L1 + 2(%rip) + + // SIGNED1 + movl $0x12345678, L1 + 3(%rip) + + // SIGNED + movl $0x12345678, L1 + 4(%rip) + + movb %al, L1(%rip) + movb %al, L1 + 1(%rip) + movl %eax, L1(%rip) + movl %eax, L1 + 1(%rip) + movl %eax, L1 + 2(%rip) + movl %eax, L1 + 3(%rip) + movl %eax, L1 + 4(%rip) + +// CHECK: ('cputype', 16777223) +// CHECK: ('cpusubtype', 3) +// CHECK: ('filetype', 1) +// CHECK: ('num_load_commands', 1) +// CHECK: ('load_commands_size', 336) +// CHECK: ('flag', 0) +// CHECK: ('reserved', 0) +// CHECK: ('load_commands', [ +// CHECK: # Load Command 0 +// CHECK: (('command', 25) +// CHECK: ('size', 232) +// 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', 358) +// CHECK: ('file_offset', 368) +// CHECK: ('file_size', 358) +// CHECK: ('maxprot', 7) +// CHECK: ('initprot', 7) +// CHECK: ('num_sections', 2) +// CHECK: ('flags', 0) +// CHECK: ('sections', [ +// CHECK: # Section 0 +// 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', 0) +// CHECK: ('size', 318) +// CHECK: ('offset', 368) +// CHECK: ('alignment', 0) +// CHECK: ('reloc_offset', 728) +// CHECK: ('num_reloc', 42) +// CHECK: ('flags', 0x80000400) +// CHECK: ('reserved1', 0) +// CHECK: ('reserved2', 0) +// CHECK: ('reserved3', 0) +// CHECK: ), +// CHECK: ('_relocations', [ +// CHECK: # Relocation 0 +// CHECK: (('word-0', 0x13a), +// CHECK: ('word-1', 0x1d000000)), +// CHECK: # Relocation 1 +// CHECK: (('word-0', 0x134), +// CHECK: ('word-1', 0x1d000000)), +// CHECK: # Relocation 2 +// CHECK: (('word-0', 0x12e), +// CHECK: ('word-1', 0x1d000000)), +// CHECK: # Relocation 3 +// CHECK: (('word-0', 0x128), +// CHECK: ('word-1', 0x1d000000)), +// CHECK: # Relocation 4 +// CHECK: (('word-0', 0x122), +// CHECK: ('word-1', 0x1d000000)), +// CHECK: # Relocation 5 +// CHECK: (('word-0', 0x11c), +// CHECK: ('word-1', 0x1d000000)), +// CHECK: # Relocation 6 +// CHECK: (('word-0', 0x116), +// CHECK: ('word-1', 0x1d000000)), +// CHECK: # Relocation 7 +// CHECK: (('word-0', 0x10c), +// CHECK: ('word-1', 0x1d000000)), +// CHECK: # Relocation 8 +// CHECK: (('word-0', 0x102), +// CHECK: ('word-1', 0x6d000000)), +// CHECK: # Relocation 9 +// CHECK: (('word-0', 0xf8), +// CHECK: ('word-1', 0x7d000000)), +// CHECK: # Relocation 10 +// CHECK: (('word-0', 0xee), +// CHECK: ('word-1', 0x1d000000)), +// CHECK: # Relocation 11 +// CHECK: (('word-0', 0xe4), +// CHECK: ('word-1', 0x8d000000)), +// CHECK: # Relocation 12 +// CHECK: (('word-0', 0xdd), +// CHECK: ('word-1', 0x1d000000)), +// CHECK: # Relocation 13 +// CHECK: (('word-0', 0xd6), +// CHECK: ('word-1', 0x6d000000)), +// CHECK: # Relocation 14 +// CHECK: (('word-0', 0xd0), +// CHECK: ('word-1', 0x15000002)), +// CHECK: # Relocation 15 +// CHECK: (('word-0', 0xca), +// CHECK: ('word-1', 0x15000002)), +// CHECK: # Relocation 16 +// CHECK: (('word-0', 0xc4), +// CHECK: ('word-1', 0x15000002)), +// CHECK: # Relocation 17 +// CHECK: (('word-0', 0xbe), +// CHECK: ('word-1', 0x15000002)), +// CHECK: # Relocation 18 +// CHECK: (('word-0', 0xb8), +// CHECK: ('word-1', 0x15000002)), +// CHECK: # Relocation 19 +// CHECK: (('word-0', 0xb2), +// CHECK: ('word-1', 0x15000002)), +// CHECK: # Relocation 20 +// CHECK: (('word-0', 0xac), +// CHECK: ('word-1', 0x15000002)), +// CHECK: # Relocation 21 +// CHECK: (('word-0', 0xa2), +// CHECK: ('word-1', 0x15000002)), +// CHECK: # Relocation 22 +// CHECK: (('word-0', 0x98), +// CHECK: ('word-1', 0x65000002)), +// CHECK: # Relocation 23 +// CHECK: (('word-0', 0x8e), +// CHECK: ('word-1', 0x75000002)), +// CHECK: # Relocation 24 +// CHECK: (('word-0', 0x84), +// CHECK: ('word-1', 0x15000002)), +// CHECK: # Relocation 25 +// CHECK: (('word-0', 0x7a), +// CHECK: ('word-1', 0x85000002)), +// CHECK: # Relocation 26 +// CHECK: (('word-0', 0x73), +// CHECK: ('word-1', 0x15000002)), +// CHECK: # Relocation 27 +// CHECK: (('word-0', 0x6c), +// CHECK: ('word-1', 0x65000002)), +// CHECK: # Relocation 28 +// CHECK: (('word-0', 0x66), +// CHECK: ('word-1', 0x1d000000)), +// CHECK: # Relocation 29 +// CHECK: (('word-0', 0x60), +// CHECK: ('word-1', 0x1d000000)), +// CHECK: # Relocation 30 +// CHECK: (('word-0', 0x5a), +// CHECK: ('word-1', 0x1d000000)), +// CHECK: # Relocation 31 +// CHECK: (('word-0', 0x54), +// CHECK: ('word-1', 0x1d000000)), +// CHECK: # Relocation 32 +// CHECK: (('word-0', 0x4e), +// CHECK: ('word-1', 0x1d000000)), +// CHECK: # Relocation 33 +// CHECK: (('word-0', 0x48), +// CHECK: ('word-1', 0x1d000000)), +// CHECK: # Relocation 34 +// CHECK: (('word-0', 0x42), +// CHECK: ('word-1', 0x1d000000)), +// CHECK: # Relocation 35 +// CHECK: (('word-0', 0x38), +// CHECK: ('word-1', 0x1d000000)), +// CHECK: # Relocation 36 +// CHECK: (('word-0', 0x2e), +// CHECK: ('word-1', 0x6d000000)), +// CHECK: # Relocation 37 +// CHECK: (('word-0', 0x24), +// CHECK: ('word-1', 0x7d000000)), +// CHECK: # Relocation 38 +// CHECK: (('word-0', 0x1a), +// CHECK: ('word-1', 0x1d000000)), +// CHECK: # Relocation 39 +// CHECK: (('word-0', 0x10), +// CHECK: ('word-1', 0x8d000000)), +// CHECK: # Relocation 40 +// CHECK: (('word-0', 0x9), +// CHECK: ('word-1', 0x1d000000)), +// CHECK: # Relocation 41 +// CHECK: (('word-0', 0x2), +// CHECK: ('word-1', 0x6d000000)), +// CHECK: ]) +// CHECK: ('_section_data', '\xc6\x05\xff\xff\xff\xff\x12\xc6\x05\x00\x00\x00\x00\x12\xc7\x05\xfc\xff\xff\xffxV4\x12\xc7\x05\xfd\xff\xff\xffxV4\x12\xc7\x05\xfe\xff\xff\xffxV4\x12\xc7\x05\xff\xff\xff\xffxV4\x12\xc7\x05\x00\x00\x00\x00xV4\x12\x88\x05\x00\x00\x00\x00\x88\x05\x01\x00\x00\x00\x89\x05\x00\x00\x00\x00\x89\x05\x01\x00\x00\x00\x89\x05\x02\x00\x00\x00\x89\x05\x03\x00\x00\x00\x89\x05\x04\x00\x00\x00\xc6\x05\xdd\x00\x00\x00\x12\xc6\x05\xd7\x00\x00\x00\x12\xc7\x05\xcc\x00\x00\x00xV4\x12\xc7\x05\xc3\x00\x00\x00xV4\x12\xc7\x05\xba\x00\x00\x00xV4\x12\xc7\x05\xb1\x00\x00\x00xV4\x12\xc7\x05\xa8\x00\x00\x00xV4\x12\x88\x05\x9e\x00\x00\x00\x88\x05\x99\x00\x00\x00\x89\x05\x92\x00\x00\x00\x89\x05\x8d\x00\x00\x00\x89\x05\x88\x00\x00\x00\x89\x05\x83\x00\x00\x00\x89\x05~\x00\x00\x00\xc6\x05\x03\x00\x00\x00\x12\xc6\x05\x04\x00\x00\x00\x12\xc7\x05\x00\x00\x00\x00xV4\x12\xc7\x05\x01\x00\x00\x00xV4\x12\xc7\x05\x02\x00\x00\x00xV4\x12\xc7\x05\x03\x00\x00\x00xV4\x12\xc7\x05\x04\x00\x00\x00xV4\x12\x88\x05\x04\x00\x00\x00\x88\x05\x05\x00\x00\x00\x89\x05\x04\x00\x00\x00\x89\x05\x05\x00\x00\x00\x89\x05\x06\x00\x00\x00\x89\x05\x07\x00\x00\x00\x89\x05\x08\x00\x00\x00') +// CHECK: # Section 1 +// CHECK: (('section_name', '__data\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') +// CHECK: ('segment_name', '__DATA\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') +// CHECK: ('address', 318) +// CHECK: ('size', 40) +// CHECK: ('offset', 686) +// CHECK: ('alignment', 0) +// CHECK: ('reloc_offset', 0) +// CHECK: ('num_reloc', 0) +// CHECK: ('flags', 0x0) +// CHECK: ('reserved1', 0) +// CHECK: ('reserved2', 0) +// CHECK: ('reserved3', 0) +// CHECK: ), +// CHECK: ('_relocations', [ +// CHECK: ]) +// CHECK: ('_section_data', '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') +// CHECK: ]) +// CHECK: ), +// CHECK: # Load Command 1 +// CHECK: (('command', 2) +// CHECK: ('size', 24) +// CHECK: ('symoff', 1064) +// CHECK: ('nsyms', 1) +// CHECK: ('stroff', 1080) +// CHECK: ('strsize', 4) +// CHECK: ('_string_data', '\x00_d\x00') +// CHECK: ('_symbols', [ +// CHECK: # Symbol 0 +// CHECK: (('n_strx', 1) +// CHECK: ('n_type', 0xe) +// CHECK: ('n_sect', 2) +// CHECK: ('n_desc', 0) +// CHECK: ('n_value', 350) +// CHECK: ('_string', '_d') +// CHECK: ), +// CHECK: ]) +// CHECK: ), +// CHECK: # Load Command 2 +// CHECK: (('command', 11) +// CHECK: ('size', 80) +// CHECK: ('ilocalsym', 0) +// CHECK: ('nlocalsym', 1) +// CHECK: ('iextdefsym', 1) +// CHECK: ('nextdefsym', 0) +// CHECK: ('iundefsym', 1) +// CHECK: ('nundefsym', 0) +// CHECK: ('tocoff', 0) +// CHECK: ('ntoc', 0) +// CHECK: ('modtaboff', 0) +// CHECK: ('nmodtab', 0) +// CHECK: ('extrefsymoff', 0) +// CHECK: ('nextrefsyms', 0) +// CHECK: ('indirectsymoff', 0) +// CHECK: ('nindirectsyms', 0) +// CHECK: ('extreloff', 0) +// CHECK: ('nextrel', 0) +// CHECK: ('locreloff', 0) +// CHECK: ('nlocrel', 0) +// CHECK: ('_indirect_symbols', [ +// CHECK: ]) +// CHECK: ), +// CHECK: ]) diff --git a/test/MC/MachO/darwin-x86_64-reloc.s b/test/MC/MachO/darwin-x86_64-reloc.s new file mode 100644 index 00000000000..6b325b085ec --- /dev/null +++ b/test/MC/MachO/darwin-x86_64-reloc.s @@ -0,0 +1,229 @@ +// RUN: llvm-mc -triple x86_64-apple-darwin9 %s -filetype=obj -o - | macho-dump --dump-section-data | FileCheck %s + +// These examples are taken from . + + .text +_foo: + ret + +_baz: + call _foo + call _foo+4 + movq _foo@GOTPCREL(%rip), %rax + pushq _foo@GOTPCREL(%rip) + movl _foo(%rip), %eax + movl _foo+4(%rip), %eax + movb $0x12, _foo(%rip) + movl $0x12345678, _foo(%rip) + .quad _foo +_bar: + .quad _foo+4 + .quad _foo - _bar + .quad _foo - _bar + 4 + .long _foo - _bar + leaq L1(%rip), %rax + leaq L0(%rip), %rax + addl $6,L0(%rip) + addw $500,L0(%rip) + addl $500,L0(%rip) + +_prev: + .space 12,0x90 + .quad L1 +L0: + .quad L0 +L_pc: + .quad _foo - L_pc + .quad _foo - L1 +L1: + .quad L1 - _prev + +// CHECK: ('cputype', 16777223) +// CHECK: ('cpusubtype', 3) +// CHECK: ('filetype', 1) +// CHECK: ('num_load_commands', 1) +// CHECK: ('load_commands_size', 256) +// CHECK: ('flag', 0) +// CHECK: ('reserved', 0) +// CHECK: ('load_commands', [ +// CHECK: # Load Command 0 +// CHECK: (('command', 25) +// CHECK: ('size', 152) +// 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', 181) +// CHECK: ('file_offset', 288) +// CHECK: ('file_size', 181) +// CHECK: ('maxprot', 7) +// CHECK: ('initprot', 7) +// CHECK: ('num_sections', 1) +// CHECK: ('flags', 0) +// CHECK: ('sections', [ +// CHECK: # Section 0 +// 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', 0) +// CHECK: ('size', 181) +// CHECK: ('offset', 288) +// CHECK: ('alignment', 0) +// CHECK: ('reloc_offset', 472) +// CHECK: ('num_reloc', 27) +// CHECK: ('flags', 0x80000400) +// CHECK: ('reserved1', 0) +// CHECK: ('reserved2', 0) +// CHECK: ('reserved3', 0) +// CHECK: ), +// CHECK: ('_relocations', [ +// CHECK: # Relocation 0 +// CHECK: (('word-0', 0xa5), +// CHECK: ('word-1', 0x5e000003)), +// CHECK: # Relocation 1 +// CHECK: (('word-0', 0xa5), +// CHECK: ('word-1', 0xe000000)), +// CHECK: # Relocation 2 +// CHECK: (('word-0', 0x9d), +// CHECK: ('word-1', 0x5e000003)), +// CHECK: # Relocation 3 +// CHECK: (('word-0', 0x9d), +// CHECK: ('word-1', 0xe000000)), +// CHECK: # Relocation 4 +// CHECK: (('word-0', 0x95), +// CHECK: ('word-1', 0xe000003)), +// CHECK: # Relocation 5 +// CHECK: (('word-0', 0x8d), +// CHECK: ('word-1', 0xe000003)), +// CHECK: # Relocation 6 +// CHECK: (('word-0', 0x79), +// CHECK: ('word-1', 0x8d000003)), +// CHECK: # Relocation 7 +// CHECK: (('word-0', 0x71), +// CHECK: ('word-1', 0x7d000003)), +// CHECK: # Relocation 8 +// CHECK: (('word-0', 0x69), +// CHECK: ('word-1', 0x6d000003)), +// CHECK: # Relocation 9 +// CHECK: (('word-0', 0x63), +// CHECK: ('word-1', 0x1d000003)), +// CHECK: # Relocation 10 +// CHECK: (('word-0', 0x5c), +// CHECK: ('word-1', 0x1d000003)), +// CHECK: # Relocation 11 +// CHECK: (('word-0', 0x55), +// CHECK: ('word-1', 0x5c000002)), +// CHECK: # Relocation 12 +// CHECK: (('word-0', 0x55), +// CHECK: ('word-1', 0xc000000)), +// CHECK: # Relocation 13 +// CHECK: (('word-0', 0x4d), +// CHECK: ('word-1', 0x5e000002)), +// CHECK: # Relocation 14 +// CHECK: (('word-0', 0x4d), +// CHECK: ('word-1', 0xe000000)), +// CHECK: # Relocation 15 +// CHECK: (('word-0', 0x45), +// CHECK: ('word-1', 0x5e000002)), +// CHECK: # Relocation 16 +// CHECK: (('word-0', 0x45), +// CHECK: ('word-1', 0xe000000)), +// CHECK: # Relocation 17 +// CHECK: (('word-0', 0x3d), +// CHECK: ('word-1', 0xe000000)), +// CHECK: # Relocation 18 +// CHECK: (('word-0', 0x35), +// CHECK: ('word-1', 0xe000000)), +// CHECK: # Relocation 19 +// CHECK: (('word-0', 0x2d), +// CHECK: ('word-1', 0x8d000000)), +// CHECK: # Relocation 20 +// CHECK: (('word-0', 0x26), +// CHECK: ('word-1', 0x6d000000)), +// CHECK: # Relocation 21 +// CHECK: (('word-0', 0x20), +// CHECK: ('word-1', 0x1d000000)), +// CHECK: # Relocation 22 +// CHECK: (('word-0', 0x1a), +// CHECK: ('word-1', 0x1d000000)), +// CHECK: # Relocation 23 +// CHECK: (('word-0', 0x14), +// CHECK: ('word-1', 0x4d000000)), +// CHECK: # Relocation 24 +// CHECK: (('word-0', 0xe), +// CHECK: ('word-1', 0x3d000000)), +// CHECK: # Relocation 25 +// CHECK: (('word-0', 0x7), +// CHECK: ('word-1', 0x2d000000)), +// CHECK: # Relocation 26 +// 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') +// CHECK: ]) +// CHECK: ), +// CHECK: # Load Command 1 +// CHECK: (('command', 2) +// CHECK: ('size', 24) +// CHECK: ('symoff', 688) +// CHECK: ('nsyms', 4) +// CHECK: ('stroff', 752) +// CHECK: ('strsize', 24) +// CHECK: ('_string_data', '\x00_foo\x00_baz\x00_bar\x00_prev\x00\x00\x00') +// CHECK: ('_symbols', [ +// CHECK: # Symbol 0 +// CHECK: (('n_strx', 1) +// CHECK: ('n_type', 0xe) +// CHECK: ('n_sect', 1) +// CHECK: ('n_desc', 0) +// CHECK: ('n_value', 0) +// CHECK: ('_string', '_foo') +// CHECK: ), +// CHECK: # Symbol 1 +// CHECK: (('n_strx', 6) +// CHECK: ('n_type', 0xe) +// CHECK: ('n_sect', 1) +// CHECK: ('n_desc', 0) +// CHECK: ('n_value', 1) +// CHECK: ('_string', '_baz') +// CHECK: ), +// CHECK: # Symbol 2 +// CHECK: (('n_strx', 11) +// CHECK: ('n_type', 0xe) +// CHECK: ('n_sect', 1) +// CHECK: ('n_desc', 0) +// CHECK: ('n_value', 61) +// CHECK: ('_string', '_bar') +// CHECK: ), +// CHECK: # Symbol 3 +// CHECK: (('n_strx', 16) +// CHECK: ('n_type', 0xe) +// CHECK: ('n_sect', 1) +// CHECK: ('n_desc', 0) +// CHECK: ('n_value', 129) +// CHECK: ('_string', '_prev') +// CHECK: ), +// CHECK: ]) +// CHECK: ), +// CHECK: # Load Command 2 +// CHECK: (('command', 11) +// CHECK: ('size', 80) +// CHECK: ('ilocalsym', 0) +// CHECK: ('nlocalsym', 4) +// CHECK: ('iextdefsym', 4) +// CHECK: ('nextdefsym', 0) +// CHECK: ('iundefsym', 4) +// CHECK: ('nundefsym', 0) +// CHECK: ('tocoff', 0) +// CHECK: ('ntoc', 0) +// CHECK: ('modtaboff', 0) +// CHECK: ('nmodtab', 0) +// CHECK: ('extrefsymoff', 0) +// CHECK: ('nextrefsyms', 0) +// CHECK: ('indirectsymoff', 0) +// CHECK: ('nindirectsyms', 0) +// CHECK: ('extreloff', 0) +// CHECK: ('nextrel', 0) +// CHECK: ('locreloff', 0) +// CHECK: ('nlocrel', 0) +// CHECK: ('_indirect_symbols', [ +// CHECK: ]) +// CHECK: ), +// CHECK: ])