Path: blob/main/contrib/llvm-project/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOX86_64.h
35294 views
//===-- RuntimeDyldMachOX86_64.h ---- MachO/X86_64 specific code. -*- C++ -*-=//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOX86_64_H9#define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOX86_64_H1011#include "../RuntimeDyldMachO.h"1213#define DEBUG_TYPE "dyld"1415namespace llvm {1617class RuntimeDyldMachOX86_6418: public RuntimeDyldMachOCRTPBase<RuntimeDyldMachOX86_64> {19public:2021typedef uint64_t TargetPtrT;2223RuntimeDyldMachOX86_64(RuntimeDyld::MemoryManager &MM,24JITSymbolResolver &Resolver)25: RuntimeDyldMachOCRTPBase(MM, Resolver) {}2627unsigned getMaxStubSize() const override { return 8; }2829Align getStubAlignment() override { return Align(8); }3031Expected<relocation_iterator>32processRelocationRef(unsigned SectionID, relocation_iterator RelI,33const ObjectFile &BaseObjT,34ObjSectionToIDMap &ObjSectionToID,35StubMap &Stubs) override {36const MachOObjectFile &Obj =37static_cast<const MachOObjectFile &>(BaseObjT);38MachO::any_relocation_info RelInfo =39Obj.getRelocation(RelI->getRawDataRefImpl());40uint32_t RelType = Obj.getAnyRelocationType(RelInfo);4142if (RelType == MachO::X86_64_RELOC_SUBTRACTOR)43return processSubtractRelocation(SectionID, RelI, Obj, ObjSectionToID);4445assert(!Obj.isRelocationScattered(RelInfo) &&46"Scattered relocations not supported on X86_64");4748RelocationEntry RE(getRelocationEntry(SectionID, Obj, RelI));49RE.Addend = memcpyAddend(RE);50RelocationValueRef Value;51if (auto ValueOrErr = getRelocationValueRef(Obj, RelI, RE, ObjSectionToID))52Value = *ValueOrErr;53else54return ValueOrErr.takeError();5556bool IsExtern = Obj.getPlainRelocationExternal(RelInfo);57if (!IsExtern && RE.IsPCRel)58makeValueAddendPCRel(Value, RelI, 1 << RE.Size);5960switch (RelType) {61UNIMPLEMENTED_RELOC(MachO::X86_64_RELOC_TLV);62default:63if (RelType > MachO::X86_64_RELOC_TLV)64return make_error<RuntimeDyldError>(("MachO X86_64 relocation type " +65Twine(RelType) +66" is out of range").str());67break;68}6970if (RE.RelType == MachO::X86_64_RELOC_GOT ||71RE.RelType == MachO::X86_64_RELOC_GOT_LOAD)72processGOTRelocation(RE, Value, Stubs);73else {74RE.Addend = Value.Offset;75if (Value.SymbolName)76addRelocationForSymbol(RE, Value.SymbolName);77else78addRelocationForSection(RE, Value.SectionID);79}8081return ++RelI;82}8384void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {85LLVM_DEBUG(dumpRelocationToResolve(RE, Value));86const SectionEntry &Section = Sections[RE.SectionID];87uint8_t *LocalAddress = Section.getAddressWithOffset(RE.Offset);8889// If the relocation is PC-relative, the value to be encoded is the90// pointer difference.91if (RE.IsPCRel) {92// FIXME: It seems this value needs to be adjusted by 4 for an effective93// PC address. Is that expected? Only for branches, perhaps?94uint64_t FinalAddress = Section.getLoadAddressWithOffset(RE.Offset);95Value -= FinalAddress + 4;96}9798switch (RE.RelType) {99default:100llvm_unreachable("Invalid relocation type!");101case MachO::X86_64_RELOC_SIGNED_1:102case MachO::X86_64_RELOC_SIGNED_2:103case MachO::X86_64_RELOC_SIGNED_4:104case MachO::X86_64_RELOC_SIGNED:105case MachO::X86_64_RELOC_UNSIGNED:106case MachO::X86_64_RELOC_BRANCH:107writeBytesUnaligned(Value + RE.Addend, LocalAddress, 1 << RE.Size);108break;109case MachO::X86_64_RELOC_SUBTRACTOR: {110uint64_t SectionABase = Sections[RE.Sections.SectionA].getLoadAddress();111uint64_t SectionBBase = Sections[RE.Sections.SectionB].getLoadAddress();112assert((Value == SectionABase || Value == SectionBBase) &&113"Unexpected SUBTRACTOR relocation value.");114Value = SectionABase - SectionBBase + RE.Addend;115writeBytesUnaligned(Value, LocalAddress, 1 << RE.Size);116break;117}118}119}120121Error finalizeSection(const ObjectFile &Obj, unsigned SectionID,122const SectionRef &Section) {123return Error::success();124}125126private:127void processGOTRelocation(const RelocationEntry &RE,128RelocationValueRef &Value, StubMap &Stubs) {129SectionEntry &Section = Sections[RE.SectionID];130assert(RE.IsPCRel);131assert(RE.Size == 2);132Value.Offset -= RE.Addend;133RuntimeDyldMachO::StubMap::const_iterator i = Stubs.find(Value);134uint8_t *Addr;135if (i != Stubs.end()) {136Addr = Section.getAddressWithOffset(i->second);137} else {138Stubs[Value] = Section.getStubOffset();139uint8_t *GOTEntry = Section.getAddressWithOffset(Section.getStubOffset());140RelocationEntry GOTRE(RE.SectionID, Section.getStubOffset(),141MachO::X86_64_RELOC_UNSIGNED, Value.Offset, false,1423);143if (Value.SymbolName)144addRelocationForSymbol(GOTRE, Value.SymbolName);145else146addRelocationForSection(GOTRE, Value.SectionID);147Section.advanceStubOffset(8);148Addr = GOTEntry;149}150RelocationEntry TargetRE(RE.SectionID, RE.Offset,151MachO::X86_64_RELOC_UNSIGNED, RE.Addend, true, 2);152resolveRelocation(TargetRE, (uint64_t)Addr);153}154155Expected<relocation_iterator>156processSubtractRelocation(unsigned SectionID, relocation_iterator RelI,157const MachOObjectFile &BaseObj,158ObjSectionToIDMap &ObjSectionToID) {159const MachOObjectFile &Obj =160static_cast<const MachOObjectFile&>(BaseObj);161MachO::any_relocation_info RE =162Obj.getRelocation(RelI->getRawDataRefImpl());163164unsigned Size = Obj.getAnyRelocationLength(RE);165uint64_t Offset = RelI->getOffset();166uint8_t *LocalAddress = Sections[SectionID].getAddressWithOffset(Offset);167unsigned NumBytes = 1 << Size;168int64_t Addend =169SignExtend64(readBytesUnaligned(LocalAddress, NumBytes), NumBytes * 8);170171unsigned SectionBID = ~0U;172uint64_t SectionBOffset = 0;173174MachO::any_relocation_info RelInfo =175Obj.getRelocation(RelI->getRawDataRefImpl());176177bool AIsExternal = BaseObj.getPlainRelocationExternal(RelInfo);178179if (AIsExternal) {180Expected<StringRef> SubtrahendNameOrErr = RelI->getSymbol()->getName();181if (!SubtrahendNameOrErr)182return SubtrahendNameOrErr.takeError();183auto SubtrahendI = GlobalSymbolTable.find(*SubtrahendNameOrErr);184SectionBID = SubtrahendI->second.getSectionID();185SectionBOffset = SubtrahendI->second.getOffset();186} else {187SectionRef SecB = Obj.getAnyRelocationSection(RelInfo);188bool IsCode = SecB.isText();189Expected<unsigned> SectionBIDOrErr =190findOrEmitSection(Obj, SecB, IsCode, ObjSectionToID);191if (!SectionBIDOrErr)192return SectionBIDOrErr.takeError();193SectionBID = *SectionBIDOrErr;194Addend += SecB.getAddress();195}196197++RelI;198199unsigned SectionAID = ~0U;200uint64_t SectionAOffset = 0;201202RelInfo = Obj.getRelocation(RelI->getRawDataRefImpl());203204bool BIsExternal = BaseObj.getPlainRelocationExternal(RelInfo);205if (BIsExternal) {206Expected<StringRef> MinuendNameOrErr = RelI->getSymbol()->getName();207if (!MinuendNameOrErr)208return MinuendNameOrErr.takeError();209auto MinuendI = GlobalSymbolTable.find(*MinuendNameOrErr);210SectionAID = MinuendI->second.getSectionID();211SectionAOffset = MinuendI->second.getOffset();212} else {213SectionRef SecA = Obj.getAnyRelocationSection(RelInfo);214bool IsCode = SecA.isText();215Expected<unsigned> SectionAIDOrErr =216findOrEmitSection(Obj, SecA, IsCode, ObjSectionToID);217if (!SectionAIDOrErr)218return SectionAIDOrErr.takeError();219SectionAID = *SectionAIDOrErr;220Addend -= SecA.getAddress();221}222223RelocationEntry R(SectionID, Offset, MachO::X86_64_RELOC_SUBTRACTOR, (uint64_t)Addend,224SectionAID, SectionAOffset, SectionBID, SectionBOffset,225false, Size);226227addRelocationForSection(R, SectionAID);228229return ++RelI;230}231232};233}234235#undef DEBUG_TYPE236237#endif238239240