lld: apply patch: [mach-o] Extend LC_DATA_IN_CODE support to x86_64

This applies 91864f82c7d7bd1a151fdfd076a3a67a2893b868 from LLVM trunk to
the embedded LLD.

Once Zig upgrades to LLD 10, there will be no difference between Zig's
fork and upstream, and Zig's fork can be dropped.
This commit is contained in:
Andrew Kelley 2019-09-10 13:25:20 -04:00
parent 016a59e3a1
commit e92d74958c
No known key found for this signature in database
GPG key ID: 7C5F548F728501A9

View file

@ -181,6 +181,20 @@ public:
FindAddressForAtom addressForAtom, FindAddressForAtom addressForAtom,
normalized::Relocations &relocs) override; normalized::Relocations &relocs) override;
bool isDataInCodeTransition(Reference::KindValue refKind) override {
return refKind == modeCode || refKind == modeData;
}
Reference::KindValue dataInCodeTransitionStart(
const MachODefinedAtom &atom) override {
return modeData;
}
Reference::KindValue dataInCodeTransitionEnd(
const MachODefinedAtom &atom) override {
return modeCode;
}
private: private:
static const Registry::KindStrings _sKindStrings[]; static const Registry::KindStrings _sKindStrings[];
static const StubInfo _sStubInfo; static const StubInfo _sStubInfo;
@ -188,6 +202,9 @@ private:
enum X86_64Kind: Reference::KindValue { enum X86_64Kind: Reference::KindValue {
invalid, /// for error condition invalid, /// for error condition
modeCode, /// Content starting at this offset is code.
modeData, /// Content starting at this offset is data.
// Kinds found in mach-o .o files: // Kinds found in mach-o .o files:
branch32, /// ex: call _foo branch32, /// ex: call _foo
ripRel32, /// ex: movq _foo(%rip), %rax ripRel32, /// ex: movq _foo(%rip), %rax
@ -242,24 +259,34 @@ private:
}; };
const Registry::KindStrings ArchHandler_x86_64::_sKindStrings[] = { const Registry::KindStrings ArchHandler_x86_64::_sKindStrings[] = {
LLD_KIND_STRING_ENTRY(invalid), LLD_KIND_STRING_ENTRY(branch32), LLD_KIND_STRING_ENTRY(invalid),
LLD_KIND_STRING_ENTRY(ripRel32), LLD_KIND_STRING_ENTRY(ripRel32Minus1), LLD_KIND_STRING_ENTRY(modeCode),
LLD_KIND_STRING_ENTRY(ripRel32Minus2), LLD_KIND_STRING_ENTRY(ripRel32Minus4), LLD_KIND_STRING_ENTRY(modeData),
LLD_KIND_STRING_ENTRY(branch32),
LLD_KIND_STRING_ENTRY(ripRel32),
LLD_KIND_STRING_ENTRY(ripRel32Minus1),
LLD_KIND_STRING_ENTRY(ripRel32Minus2),
LLD_KIND_STRING_ENTRY(ripRel32Minus4),
LLD_KIND_STRING_ENTRY(ripRel32Anon), LLD_KIND_STRING_ENTRY(ripRel32Anon),
LLD_KIND_STRING_ENTRY(ripRel32Minus1Anon), LLD_KIND_STRING_ENTRY(ripRel32Minus1Anon),
LLD_KIND_STRING_ENTRY(ripRel32Minus2Anon), LLD_KIND_STRING_ENTRY(ripRel32Minus2Anon),
LLD_KIND_STRING_ENTRY(ripRel32Minus4Anon), LLD_KIND_STRING_ENTRY(ripRel32Minus4Anon),
LLD_KIND_STRING_ENTRY(ripRel32GotLoad), LLD_KIND_STRING_ENTRY(ripRel32GotLoad),
LLD_KIND_STRING_ENTRY(ripRel32GotLoadNowLea), LLD_KIND_STRING_ENTRY(ripRel32GotLoadNowLea),
LLD_KIND_STRING_ENTRY(ripRel32Got), LLD_KIND_STRING_ENTRY(ripRel32Tlv), LLD_KIND_STRING_ENTRY(ripRel32Got),
LLD_KIND_STRING_ENTRY(ripRel32Tlv),
LLD_KIND_STRING_ENTRY(lazyPointer), LLD_KIND_STRING_ENTRY(lazyPointer),
LLD_KIND_STRING_ENTRY(lazyImmediateLocation), LLD_KIND_STRING_ENTRY(lazyImmediateLocation),
LLD_KIND_STRING_ENTRY(pointer64), LLD_KIND_STRING_ENTRY(pointer64Anon), LLD_KIND_STRING_ENTRY(pointer64),
LLD_KIND_STRING_ENTRY(delta32), LLD_KIND_STRING_ENTRY(delta64), LLD_KIND_STRING_ENTRY(pointer64Anon),
LLD_KIND_STRING_ENTRY(delta32Anon), LLD_KIND_STRING_ENTRY(delta64Anon), LLD_KIND_STRING_ENTRY(delta32),
LLD_KIND_STRING_ENTRY(delta64),
LLD_KIND_STRING_ENTRY(delta32Anon),
LLD_KIND_STRING_ENTRY(delta64Anon),
LLD_KIND_STRING_ENTRY(negDelta64), LLD_KIND_STRING_ENTRY(negDelta64),
LLD_KIND_STRING_ENTRY(negDelta32), LLD_KIND_STRING_ENTRY(negDelta32),
LLD_KIND_STRING_ENTRY(imageOffset), LLD_KIND_STRING_ENTRY(imageOffsetGot), LLD_KIND_STRING_ENTRY(imageOffset),
LLD_KIND_STRING_ENTRY(imageOffsetGot),
LLD_KIND_STRING_ENTRY(unwindFDEToFunction), LLD_KIND_STRING_ENTRY(unwindFDEToFunction),
LLD_KIND_STRING_ENTRY(unwindInfoToEhFrame), LLD_KIND_STRING_ENTRY(unwindInfoToEhFrame),
LLD_KIND_STRING_ENTRY(tlvInitSectionOffset), LLD_KIND_STRING_ENTRY(tlvInitSectionOffset),
@ -601,6 +628,8 @@ void ArchHandler_x86_64::applyFixupFinal(
case negDelta32: case negDelta32:
*loc32 = fixupAddress - targetAddress + ref.addend(); *loc32 = fixupAddress - targetAddress + ref.addend();
return; return;
case modeCode:
case modeData:
case lazyPointer: case lazyPointer:
// Do nothing // Do nothing
return; return;
@ -720,6 +749,8 @@ void ArchHandler_x86_64::applyFixupRelocatable(const Reference &ref,
case unwindInfoToEhFrame: case unwindInfoToEhFrame:
llvm_unreachable("fixup implies __unwind_info"); llvm_unreachable("fixup implies __unwind_info");
return; return;
case modeCode:
case modeData:
case unwindFDEToFunction: case unwindFDEToFunction:
// Do nothing for now // Do nothing for now
return; return;
@ -743,6 +774,9 @@ void ArchHandler_x86_64::appendSectionRelocations(
assert(ref.kindArch() == Reference::KindArch::x86_64); assert(ref.kindArch() == Reference::KindArch::x86_64);
uint32_t sectionOffset = atomSectionOffset + ref.offsetInAtom(); uint32_t sectionOffset = atomSectionOffset + ref.offsetInAtom();
switch (static_cast<X86_64Kind>(ref.kindValue())) { switch (static_cast<X86_64Kind>(ref.kindValue())) {
case modeCode:
case modeData:
return;
case branch32: case branch32:
appendReloc(relocs, sectionOffset, symbolIndexForAtom(*ref.target()), 0, appendReloc(relocs, sectionOffset, symbolIndexForAtom(*ref.target()), 0,
X86_64_RELOC_BRANCH | rPcRel | rExtern | rLength4); X86_64_RELOC_BRANCH | rPcRel | rExtern | rLength4);