Jim Grosbach | e0934be | 2012-01-16 23:50:58 +0000 | [diff] [blame] | 1 | //===-- RuntimeDyldELF.cpp - Run-time dynamic linker for MC-JIT -*- C++ -*-===// |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Implementation of ELF support for the MC-JIT runtime dynamic linker. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #define DEBUG_TYPE "dyld" |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 15 | #include "RuntimeDyldELF.h" |
| 16 | #include "JITRegistrar.h" |
| 17 | #include "ObjectImageCommon.h" |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/IntervalMap.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/STLExtras.h" |
| 20 | #include "llvm/ADT/StringRef.h" |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/Triple.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 22 | #include "llvm/ExecutionEngine/ObjectBuffer.h" |
| 23 | #include "llvm/ExecutionEngine/ObjectImage.h" |
Michael J. Spencer | 081a194 | 2013-08-08 22:27:13 +0000 | [diff] [blame] | 24 | #include "llvm/Object/ELFObjectFile.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 25 | #include "llvm/Object/ObjectFile.h" |
| 26 | #include "llvm/Support/ELF.h" |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 27 | #include "llvm/Support/MemoryBuffer.h" |
| 28 | |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | using namespace llvm::object; |
| 31 | |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 32 | namespace { |
| 33 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 34 | static inline error_code check(error_code Err) { |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 35 | if (Err) { |
| 36 | report_fatal_error(Err.message()); |
| 37 | } |
| 38 | return Err; |
| 39 | } |
| 40 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 41 | template <class ELFT> class DyldELFObject : public ELFObjectFile<ELFT> { |
Rafael Espindola | 4323907 | 2013-04-17 21:20:55 +0000 | [diff] [blame] | 42 | LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 43 | |
Michael J. Spencer | ac97f5c | 2013-01-15 07:44:25 +0000 | [diff] [blame] | 44 | typedef Elf_Shdr_Impl<ELFT> Elf_Shdr; |
| 45 | typedef Elf_Sym_Impl<ELFT> Elf_Sym; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 46 | typedef Elf_Rel_Impl<ELFT, false> Elf_Rel; |
| 47 | typedef Elf_Rel_Impl<ELFT, true> Elf_Rela; |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 48 | |
Michael J. Spencer | ac97f5c | 2013-01-15 07:44:25 +0000 | [diff] [blame] | 49 | typedef Elf_Ehdr_Impl<ELFT> Elf_Ehdr; |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 50 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 51 | typedef typename ELFDataTypeTypedefHelper<ELFT>::value_type addr_type; |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 52 | |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 53 | public: |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 54 | DyldELFObject(MemoryBuffer *Wrapper, error_code &ec); |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 55 | |
| 56 | void updateSectionAddress(const SectionRef &Sec, uint64_t Addr); |
| 57 | void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr); |
| 58 | |
Andrew Kaylor | 2e31987 | 2012-07-27 17:52:42 +0000 | [diff] [blame] | 59 | // Methods for type inquiry through isa, cast and dyn_cast |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 60 | static inline bool classof(const Binary *v) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 61 | return (isa<ELFObjectFile<ELFT>>(v) && |
| 62 | classof(cast<ELFObjectFile<ELFT>>(v))); |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 63 | } |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 64 | static inline bool classof(const ELFObjectFile<ELFT> *v) { |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 65 | return v->isDyldType(); |
| 66 | } |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 69 | template <class ELFT> class ELFObjectImage : public ObjectImageCommon { |
| 70 | protected: |
| 71 | DyldELFObject<ELFT> *DyldObj; |
| 72 | bool Registered; |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 73 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 74 | public: |
| 75 | ELFObjectImage(ObjectBuffer *Input, DyldELFObject<ELFT> *Obj) |
| 76 | : ObjectImageCommon(Input, Obj), DyldObj(Obj), Registered(false) {} |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 77 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 78 | virtual ~ELFObjectImage() { |
| 79 | if (Registered) |
| 80 | deregisterWithDebugger(); |
| 81 | } |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 82 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 83 | // Subclasses can override these methods to update the image with loaded |
| 84 | // addresses for sections and common symbols |
| 85 | void updateSectionAddress(const SectionRef &Sec, uint64_t Addr) override { |
| 86 | DyldObj->updateSectionAddress(Sec, Addr); |
| 87 | } |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 88 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 89 | void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr) override { |
| 90 | DyldObj->updateSymbolAddress(Sym, Addr); |
| 91 | } |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 92 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 93 | void registerWithDebugger() override { |
| 94 | JITRegistrar::getGDBRegistrar().registerObject(*Buffer); |
| 95 | Registered = true; |
| 96 | } |
| 97 | void deregisterWithDebugger() override { |
| 98 | JITRegistrar::getGDBRegistrar().deregisterObject(*Buffer); |
| 99 | } |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 100 | }; |
| 101 | |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 102 | // The MemoryBuffer passed into this constructor is just a wrapper around the |
| 103 | // actual memory. Ultimately, the Binary parent class will take ownership of |
| 104 | // this MemoryBuffer object but not the underlying memory. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 105 | template <class ELFT> |
Michael J. Spencer | ac97f5c | 2013-01-15 07:44:25 +0000 | [diff] [blame] | 106 | DyldELFObject<ELFT>::DyldELFObject(MemoryBuffer *Wrapper, error_code &ec) |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 107 | : ELFObjectFile<ELFT>(Wrapper, ec) { |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 108 | this->isDyldELFObject = true; |
| 109 | } |
| 110 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 111 | template <class ELFT> |
Michael J. Spencer | ac97f5c | 2013-01-15 07:44:25 +0000 | [diff] [blame] | 112 | void DyldELFObject<ELFT>::updateSectionAddress(const SectionRef &Sec, |
| 113 | uint64_t Addr) { |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 114 | DataRefImpl ShdrRef = Sec.getRawDataRefImpl(); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 115 | Elf_Shdr *shdr = |
| 116 | const_cast<Elf_Shdr *>(reinterpret_cast<const Elf_Shdr *>(ShdrRef.p)); |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 117 | |
| 118 | // This assumes the address passed in matches the target address bitness |
| 119 | // The template-based type cast handles everything else. |
| 120 | shdr->sh_addr = static_cast<addr_type>(Addr); |
| 121 | } |
| 122 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 123 | template <class ELFT> |
Michael J. Spencer | ac97f5c | 2013-01-15 07:44:25 +0000 | [diff] [blame] | 124 | void DyldELFObject<ELFT>::updateSymbolAddress(const SymbolRef &SymRef, |
| 125 | uint64_t Addr) { |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 126 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 127 | Elf_Sym *sym = const_cast<Elf_Sym *>( |
| 128 | ELFObjectFile<ELFT>::getSymbol(SymRef.getRawDataRefImpl())); |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 129 | |
| 130 | // This assumes the address passed in matches the target address bitness |
| 131 | // The template-based type cast handles everything else. |
| 132 | sym->st_value = static_cast<addr_type>(Addr); |
| 133 | } |
| 134 | |
| 135 | } // namespace |
| 136 | |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 137 | namespace llvm { |
| 138 | |
Andrew Kaylor | 528f6d7 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 139 | void RuntimeDyldELF::registerEHFrames() { |
| 140 | if (!MemMgr) |
| 141 | return; |
| 142 | for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) { |
| 143 | SID EHFrameSID = UnregisteredEHFrameSections[i]; |
| 144 | uint8_t *EHFrameAddr = Sections[EHFrameSID].Address; |
| 145 | uint64_t EHFrameLoadAddr = Sections[EHFrameSID].LoadAddress; |
| 146 | size_t EHFrameSize = Sections[EHFrameSID].Size; |
| 147 | MemMgr->registerEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize); |
Andrew Kaylor | 43507d0 | 2013-10-16 00:14:21 +0000 | [diff] [blame] | 148 | RegisteredEHFrameSections.push_back(EHFrameSID); |
Rafael Espindola | a2e40fb | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 149 | } |
Andrew Kaylor | 528f6d7 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 150 | UnregisteredEHFrameSections.clear(); |
Rafael Espindola | a2e40fb | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Andrew Kaylor | 43507d0 | 2013-10-16 00:14:21 +0000 | [diff] [blame] | 153 | void RuntimeDyldELF::deregisterEHFrames() { |
| 154 | if (!MemMgr) |
| 155 | return; |
| 156 | for (int i = 0, e = RegisteredEHFrameSections.size(); i != e; ++i) { |
| 157 | SID EHFrameSID = RegisteredEHFrameSections[i]; |
| 158 | uint8_t *EHFrameAddr = Sections[EHFrameSID].Address; |
| 159 | uint64_t EHFrameLoadAddr = Sections[EHFrameSID].LoadAddress; |
| 160 | size_t EHFrameSize = Sections[EHFrameSID].Size; |
| 161 | MemMgr->deregisterEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize); |
| 162 | } |
| 163 | RegisteredEHFrameSections.clear(); |
| 164 | } |
| 165 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 166 | ObjectImage * |
| 167 | RuntimeDyldELF::createObjectImageFromFile(object::ObjectFile *ObjFile) { |
| 168 | if (!ObjFile) |
| 169 | return NULL; |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 170 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 171 | error_code ec; |
| 172 | MemoryBuffer *Buffer = |
| 173 | MemoryBuffer::getMemBuffer(ObjFile->getData(), "", false); |
| 174 | |
| 175 | if (ObjFile->getBytesInAddress() == 4 && ObjFile->isLittleEndian()) { |
| 176 | DyldELFObject<ELFType<support::little, 2, false>> *Obj = |
| 177 | new DyldELFObject<ELFType<support::little, 2, false>>(Buffer, ec); |
| 178 | return new ELFObjectImage<ELFType<support::little, 2, false>>(NULL, Obj); |
| 179 | } else if (ObjFile->getBytesInAddress() == 4 && !ObjFile->isLittleEndian()) { |
| 180 | DyldELFObject<ELFType<support::big, 2, false>> *Obj = |
| 181 | new DyldELFObject<ELFType<support::big, 2, false>>(Buffer, ec); |
| 182 | return new ELFObjectImage<ELFType<support::big, 2, false>>(NULL, Obj); |
| 183 | } else if (ObjFile->getBytesInAddress() == 8 && !ObjFile->isLittleEndian()) { |
| 184 | DyldELFObject<ELFType<support::big, 2, true>> *Obj = |
| 185 | new DyldELFObject<ELFType<support::big, 2, true>>(Buffer, ec); |
| 186 | return new ELFObjectImage<ELFType<support::big, 2, true>>(NULL, Obj); |
| 187 | } else if (ObjFile->getBytesInAddress() == 8 && ObjFile->isLittleEndian()) { |
| 188 | DyldELFObject<ELFType<support::little, 2, true>> *Obj = |
| 189 | new DyldELFObject<ELFType<support::little, 2, true>>(Buffer, ec); |
| 190 | return new ELFObjectImage<ELFType<support::little, 2, true>>(NULL, Obj); |
| 191 | } else |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 192 | llvm_unreachable("Unexpected ELF format"); |
| 193 | } |
| 194 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 195 | ObjectImage *RuntimeDyldELF::createObjectImage(ObjectBuffer *Buffer) { |
| 196 | if (Buffer->getBufferSize() < ELF::EI_NIDENT) |
| 197 | llvm_unreachable("Unexpected ELF object size"); |
| 198 | std::pair<unsigned char, unsigned char> Ident = |
| 199 | std::make_pair((uint8_t)Buffer->getBufferStart()[ELF::EI_CLASS], |
| 200 | (uint8_t)Buffer->getBufferStart()[ELF::EI_DATA]); |
| 201 | error_code ec; |
| 202 | |
| 203 | if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB) { |
| 204 | DyldELFObject<ELFType<support::little, 4, false>> *Obj = |
| 205 | new DyldELFObject<ELFType<support::little, 4, false>>( |
| 206 | Buffer->getMemBuffer(), ec); |
| 207 | return new ELFObjectImage<ELFType<support::little, 4, false>>(Buffer, Obj); |
| 208 | } else if (Ident.first == ELF::ELFCLASS32 && |
| 209 | Ident.second == ELF::ELFDATA2MSB) { |
| 210 | DyldELFObject<ELFType<support::big, 4, false>> *Obj = |
| 211 | new DyldELFObject<ELFType<support::big, 4, false>>( |
| 212 | Buffer->getMemBuffer(), ec); |
| 213 | return new ELFObjectImage<ELFType<support::big, 4, false>>(Buffer, Obj); |
| 214 | } else if (Ident.first == ELF::ELFCLASS64 && |
| 215 | Ident.second == ELF::ELFDATA2MSB) { |
| 216 | DyldELFObject<ELFType<support::big, 8, true>> *Obj = |
| 217 | new DyldELFObject<ELFType<support::big, 8, true>>( |
| 218 | Buffer->getMemBuffer(), ec); |
| 219 | return new ELFObjectImage<ELFType<support::big, 8, true>>(Buffer, Obj); |
| 220 | } else if (Ident.first == ELF::ELFCLASS64 && |
| 221 | Ident.second == ELF::ELFDATA2LSB) { |
| 222 | DyldELFObject<ELFType<support::little, 8, true>> *Obj = |
| 223 | new DyldELFObject<ELFType<support::little, 8, true>>( |
| 224 | Buffer->getMemBuffer(), ec); |
| 225 | return new ELFObjectImage<ELFType<support::little, 8, true>>(Buffer, Obj); |
| 226 | } else |
| 227 | llvm_unreachable("Unexpected ELF format"); |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 228 | } |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 229 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 230 | RuntimeDyldELF::~RuntimeDyldELF() {} |
| 231 | |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 232 | void RuntimeDyldELF::resolveX86_64Relocation(const SectionEntry &Section, |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 233 | uint64_t Offset, uint64_t Value, |
| 234 | uint32_t Type, int64_t Addend, |
Andrew Kaylor | ff9fa05 | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 235 | uint64_t SymOffset) { |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 236 | switch (Type) { |
| 237 | default: |
| 238 | llvm_unreachable("Relocation type not implemented yet!"); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 239 | break; |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 240 | case ELF::R_X86_64_64: { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 241 | uint64_t *Target = reinterpret_cast<uint64_t *>(Section.Address + Offset); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 242 | *Target = Value + Addend; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 243 | DEBUG(dbgs() << "Writing " << format("%p", (Value + Addend)) << " at " |
| 244 | << format("%p\n", Target)); |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 245 | break; |
| 246 | } |
| 247 | case ELF::R_X86_64_32: |
| 248 | case ELF::R_X86_64_32S: { |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 249 | Value += Addend; |
Andrew Kaylor | d83a547 | 2012-07-27 20:30:12 +0000 | [diff] [blame] | 250 | assert((Type == ELF::R_X86_64_32 && (Value <= UINT32_MAX)) || |
Michael J. Spencer | 4d9c539 | 2013-01-04 20:36:28 +0000 | [diff] [blame] | 251 | (Type == ELF::R_X86_64_32S && |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 252 | ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN))); |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 253 | uint32_t TruncatedAddr = (Value & 0xFFFFFFFF); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 254 | uint32_t *Target = reinterpret_cast<uint32_t *>(Section.Address + Offset); |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 255 | *Target = TruncatedAddr; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 256 | DEBUG(dbgs() << "Writing " << format("%p", TruncatedAddr) << " at " |
| 257 | << format("%p\n", Target)); |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 258 | break; |
| 259 | } |
Andrew Kaylor | ff9fa05 | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 260 | case ELF::R_X86_64_GOTPCREL: { |
| 261 | // findGOTEntry returns the 'G + GOT' part of the relocation calculation |
| 262 | // based on the load/target address of the GOT (not the current/local addr). |
| 263 | uint64_t GOTAddr = findGOTEntry(Value, SymOffset); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 264 | uint32_t *Target = reinterpret_cast<uint32_t *>(Section.Address + Offset); |
| 265 | uint64_t FinalAddress = Section.LoadAddress + Offset; |
Andrew Kaylor | ff9fa05 | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 266 | // The processRelocationRef method combines the symbol offset and the addend |
| 267 | // and in most cases that's what we want. For this relocation type, we need |
| 268 | // the raw addend, so we subtract the symbol offset to get it. |
| 269 | int64_t RealOffset = GOTAddr + Addend - SymOffset - FinalAddress; |
| 270 | assert(RealOffset <= INT32_MAX && RealOffset >= INT32_MIN); |
| 271 | int32_t TruncOffset = (RealOffset & 0xFFFFFFFF); |
| 272 | *Target = TruncOffset; |
| 273 | break; |
| 274 | } |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 275 | case ELF::R_X86_64_PC32: { |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 276 | // Get the placeholder value from the generated object since |
| 277 | // a previous relocation attempt may have overwritten the loaded version |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 278 | uint32_t *Placeholder = |
| 279 | reinterpret_cast<uint32_t *>(Section.ObjAddress + Offset); |
| 280 | uint32_t *Target = reinterpret_cast<uint32_t *>(Section.Address + Offset); |
| 281 | uint64_t FinalAddress = Section.LoadAddress + Offset; |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 282 | int64_t RealOffset = *Placeholder + Value + Addend - FinalAddress; |
Andrew Kaylor | d83a547 | 2012-07-27 20:30:12 +0000 | [diff] [blame] | 283 | assert(RealOffset <= INT32_MAX && RealOffset >= INT32_MIN); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 284 | int32_t TruncOffset = (RealOffset & 0xFFFFFFFF); |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 285 | *Target = TruncOffset; |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 286 | break; |
| 287 | } |
Andrew Kaylor | ff9fa05 | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 288 | case ELF::R_X86_64_PC64: { |
| 289 | // Get the placeholder value from the generated object since |
| 290 | // a previous relocation attempt may have overwritten the loaded version |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 291 | uint64_t *Placeholder = |
| 292 | reinterpret_cast<uint64_t *>(Section.ObjAddress + Offset); |
| 293 | uint64_t *Target = reinterpret_cast<uint64_t *>(Section.Address + Offset); |
| 294 | uint64_t FinalAddress = Section.LoadAddress + Offset; |
Andrew Kaylor | ff9fa05 | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 295 | *Target = *Placeholder + Value + Addend - FinalAddress; |
| 296 | break; |
| 297 | } |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 301 | void RuntimeDyldELF::resolveX86Relocation(const SectionEntry &Section, |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 302 | uint64_t Offset, uint32_t Value, |
| 303 | uint32_t Type, int32_t Addend) { |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 304 | switch (Type) { |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 305 | case ELF::R_386_32: { |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 306 | // Get the placeholder value from the generated object since |
| 307 | // a previous relocation attempt may have overwritten the loaded version |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 308 | uint32_t *Placeholder = |
| 309 | reinterpret_cast<uint32_t *>(Section.ObjAddress + Offset); |
| 310 | uint32_t *Target = reinterpret_cast<uint32_t *>(Section.Address + Offset); |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 311 | *Target = *Placeholder + Value + Addend; |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 312 | break; |
| 313 | } |
| 314 | case ELF::R_386_PC32: { |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 315 | // Get the placeholder value from the generated object since |
| 316 | // a previous relocation attempt may have overwritten the loaded version |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 317 | uint32_t *Placeholder = |
| 318 | reinterpret_cast<uint32_t *>(Section.ObjAddress + Offset); |
| 319 | uint32_t *Target = reinterpret_cast<uint32_t *>(Section.Address + Offset); |
| 320 | uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 321 | uint32_t RealOffset = *Placeholder + Value + Addend - FinalAddress; |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 322 | *Target = RealOffset; |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 323 | break; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 324 | } |
| 325 | default: |
| 326 | // There are other relocation types, but it appears these are the |
| 327 | // only ones currently used by the LLVM ELF object writer |
| 328 | llvm_unreachable("Relocation type not implemented yet!"); |
| 329 | break; |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
Tim Northover | 85829bb | 2013-05-04 20:13:59 +0000 | [diff] [blame] | 333 | void RuntimeDyldELF::resolveAArch64Relocation(const SectionEntry &Section, |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 334 | uint64_t Offset, uint64_t Value, |
| 335 | uint32_t Type, int64_t Addend) { |
| 336 | uint32_t *TargetPtr = reinterpret_cast<uint32_t *>(Section.Address + Offset); |
Tim Northover | 85829bb | 2013-05-04 20:13:59 +0000 | [diff] [blame] | 337 | uint64_t FinalAddress = Section.LoadAddress + Offset; |
| 338 | |
| 339 | DEBUG(dbgs() << "resolveAArch64Relocation, LocalAddress: 0x" |
| 340 | << format("%llx", Section.Address + Offset) |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 341 | << " FinalAddress: 0x" << format("%llx", FinalAddress) |
| 342 | << " Value: 0x" << format("%llx", Value) << " Type: 0x" |
| 343 | << format("%x", Type) << " Addend: 0x" << format("%llx", Addend) |
Tim Northover | 85829bb | 2013-05-04 20:13:59 +0000 | [diff] [blame] | 344 | << "\n"); |
| 345 | |
| 346 | switch (Type) { |
| 347 | default: |
| 348 | llvm_unreachable("Relocation type not implemented yet!"); |
| 349 | break; |
Tim Northover | d52eaae | 2013-05-04 20:14:14 +0000 | [diff] [blame] | 350 | case ELF::R_AARCH64_ABS64: { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 351 | uint64_t *TargetPtr = |
| 352 | reinterpret_cast<uint64_t *>(Section.Address + Offset); |
Tim Northover | d52eaae | 2013-05-04 20:14:14 +0000 | [diff] [blame] | 353 | *TargetPtr = Value + Addend; |
| 354 | break; |
| 355 | } |
Tim Northover | 675b9e9 | 2013-05-19 15:39:03 +0000 | [diff] [blame] | 356 | case ELF::R_AARCH64_PREL32: { |
Tim Northover | 85829bb | 2013-05-04 20:13:59 +0000 | [diff] [blame] | 357 | uint64_t Result = Value + Addend - FinalAddress; |
Michael J. Spencer | 081a194 | 2013-08-08 22:27:13 +0000 | [diff] [blame] | 358 | assert(static_cast<int64_t>(Result) >= INT32_MIN && |
Tim Northover | 85829bb | 2013-05-04 20:13:59 +0000 | [diff] [blame] | 359 | static_cast<int64_t>(Result) <= UINT32_MAX); |
| 360 | *TargetPtr = static_cast<uint32_t>(Result & 0xffffffffU); |
| 361 | break; |
| 362 | } |
Tim Northover | 4a9b6b7 | 2013-05-04 20:14:09 +0000 | [diff] [blame] | 363 | case ELF::R_AARCH64_CALL26: // fallthrough |
| 364 | case ELF::R_AARCH64_JUMP26: { |
| 365 | // Operation: S+A-P. Set Call or B immediate value to bits fff_fffc of the |
| 366 | // calculation. |
| 367 | uint64_t BranchImm = Value + Addend - FinalAddress; |
| 368 | |
| 369 | // "Check that -2^27 <= result < 2^27". |
Michael J. Spencer | 081a194 | 2013-08-08 22:27:13 +0000 | [diff] [blame] | 370 | assert(-(1LL << 27) <= static_cast<int64_t>(BranchImm) && |
Tim Northover | 4a9b6b7 | 2013-05-04 20:14:09 +0000 | [diff] [blame] | 371 | static_cast<int64_t>(BranchImm) < (1LL << 27)); |
Tim Northover | 675b9e9 | 2013-05-19 15:39:03 +0000 | [diff] [blame] | 372 | |
| 373 | // AArch64 code is emitted with .rela relocations. The data already in any |
| 374 | // bits affected by the relocation on entry is garbage. |
| 375 | *TargetPtr &= 0xfc000000U; |
Tim Northover | 4a9b6b7 | 2013-05-04 20:14:09 +0000 | [diff] [blame] | 376 | // Immediate goes in bits 25:0 of B and BL. |
| 377 | *TargetPtr |= static_cast<uint32_t>(BranchImm & 0xffffffcU) >> 2; |
| 378 | break; |
| 379 | } |
Tim Northover | 654c2d6 | 2013-05-04 20:14:04 +0000 | [diff] [blame] | 380 | case ELF::R_AARCH64_MOVW_UABS_G3: { |
| 381 | uint64_t Result = Value + Addend; |
Tim Northover | 675b9e9 | 2013-05-19 15:39:03 +0000 | [diff] [blame] | 382 | |
| 383 | // AArch64 code is emitted with .rela relocations. The data already in any |
| 384 | // bits affected by the relocation on entry is garbage. |
Tim Northover | 107b2f2 | 2013-07-25 12:42:52 +0000 | [diff] [blame] | 385 | *TargetPtr &= 0xffe0001fU; |
Tim Northover | 654c2d6 | 2013-05-04 20:14:04 +0000 | [diff] [blame] | 386 | // Immediate goes in bits 20:5 of MOVZ/MOVK instruction |
| 387 | *TargetPtr |= Result >> (48 - 5); |
Tim Northover | 6711fc2 | 2013-07-01 19:23:10 +0000 | [diff] [blame] | 388 | // Shift must be "lsl #48", in bits 22:21 |
| 389 | assert((*TargetPtr >> 21 & 0x3) == 3 && "invalid shift for relocation"); |
Tim Northover | 654c2d6 | 2013-05-04 20:14:04 +0000 | [diff] [blame] | 390 | break; |
| 391 | } |
| 392 | case ELF::R_AARCH64_MOVW_UABS_G2_NC: { |
| 393 | uint64_t Result = Value + Addend; |
Tim Northover | 675b9e9 | 2013-05-19 15:39:03 +0000 | [diff] [blame] | 394 | |
Tim Northover | 675b9e9 | 2013-05-19 15:39:03 +0000 | [diff] [blame] | 395 | // AArch64 code is emitted with .rela relocations. The data already in any |
| 396 | // bits affected by the relocation on entry is garbage. |
Tim Northover | 107b2f2 | 2013-07-25 12:42:52 +0000 | [diff] [blame] | 397 | *TargetPtr &= 0xffe0001fU; |
Tim Northover | 654c2d6 | 2013-05-04 20:14:04 +0000 | [diff] [blame] | 398 | // Immediate goes in bits 20:5 of MOVZ/MOVK instruction |
| 399 | *TargetPtr |= ((Result & 0xffff00000000ULL) >> (32 - 5)); |
Tim Northover | 6711fc2 | 2013-07-01 19:23:10 +0000 | [diff] [blame] | 400 | // Shift must be "lsl #32", in bits 22:21 |
| 401 | assert((*TargetPtr >> 21 & 0x3) == 2 && "invalid shift for relocation"); |
Tim Northover | 654c2d6 | 2013-05-04 20:14:04 +0000 | [diff] [blame] | 402 | break; |
| 403 | } |
| 404 | case ELF::R_AARCH64_MOVW_UABS_G1_NC: { |
| 405 | uint64_t Result = Value + Addend; |
Tim Northover | 675b9e9 | 2013-05-19 15:39:03 +0000 | [diff] [blame] | 406 | |
| 407 | // AArch64 code is emitted with .rela relocations. The data already in any |
| 408 | // bits affected by the relocation on entry is garbage. |
Tim Northover | 107b2f2 | 2013-07-25 12:42:52 +0000 | [diff] [blame] | 409 | *TargetPtr &= 0xffe0001fU; |
Tim Northover | 654c2d6 | 2013-05-04 20:14:04 +0000 | [diff] [blame] | 410 | // Immediate goes in bits 20:5 of MOVZ/MOVK instruction |
| 411 | *TargetPtr |= ((Result & 0xffff0000U) >> (16 - 5)); |
Tim Northover | 6711fc2 | 2013-07-01 19:23:10 +0000 | [diff] [blame] | 412 | // Shift must be "lsl #16", in bits 22:2 |
| 413 | assert((*TargetPtr >> 21 & 0x3) == 1 && "invalid shift for relocation"); |
Tim Northover | 654c2d6 | 2013-05-04 20:14:04 +0000 | [diff] [blame] | 414 | break; |
| 415 | } |
| 416 | case ELF::R_AARCH64_MOVW_UABS_G0_NC: { |
| 417 | uint64_t Result = Value + Addend; |
Tim Northover | 675b9e9 | 2013-05-19 15:39:03 +0000 | [diff] [blame] | 418 | |
| 419 | // AArch64 code is emitted with .rela relocations. The data already in any |
| 420 | // bits affected by the relocation on entry is garbage. |
Tim Northover | 107b2f2 | 2013-07-25 12:42:52 +0000 | [diff] [blame] | 421 | *TargetPtr &= 0xffe0001fU; |
Tim Northover | 654c2d6 | 2013-05-04 20:14:04 +0000 | [diff] [blame] | 422 | // Immediate goes in bits 20:5 of MOVZ/MOVK instruction |
| 423 | *TargetPtr |= ((Result & 0xffffU) << 5); |
Tim Northover | 6711fc2 | 2013-07-01 19:23:10 +0000 | [diff] [blame] | 424 | // Shift must be "lsl #0", in bits 22:21. |
| 425 | assert((*TargetPtr >> 21 & 0x3) == 0 && "invalid shift for relocation"); |
Tim Northover | 654c2d6 | 2013-05-04 20:14:04 +0000 | [diff] [blame] | 426 | break; |
| 427 | } |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 428 | case ELF::R_AARCH64_ADR_PREL_PG_HI21: { |
| 429 | // Operation: Page(S+A) - Page(P) |
| 430 | uint64_t Result = |
| 431 | ((Value + Addend) & ~0xfffULL) - (FinalAddress & ~0xfffULL); |
| 432 | |
| 433 | // Check that -2^32 <= X < 2^32 |
| 434 | assert(static_cast<int64_t>(Result) >= (-1LL << 32) && |
| 435 | static_cast<int64_t>(Result) < (1LL << 32) && |
| 436 | "overflow check failed for relocation"); |
| 437 | |
| 438 | // AArch64 code is emitted with .rela relocations. The data already in any |
| 439 | // bits affected by the relocation on entry is garbage. |
| 440 | *TargetPtr &= 0x9f00001fU; |
| 441 | // Immediate goes in bits 30:29 + 5:23 of ADRP instruction, taken |
| 442 | // from bits 32:12 of X. |
| 443 | *TargetPtr |= ((Result & 0x3000U) << (29 - 12)); |
| 444 | *TargetPtr |= ((Result & 0x1ffffc000ULL) >> (14 - 5)); |
| 445 | break; |
| 446 | } |
| 447 | case ELF::R_AARCH64_LDST32_ABS_LO12_NC: { |
| 448 | // Operation: S + A |
| 449 | uint64_t Result = Value + Addend; |
| 450 | |
| 451 | // AArch64 code is emitted with .rela relocations. The data already in any |
| 452 | // bits affected by the relocation on entry is garbage. |
| 453 | *TargetPtr &= 0xffc003ffU; |
| 454 | // Immediate goes in bits 21:10 of LD/ST instruction, taken |
| 455 | // from bits 11:2 of X |
| 456 | *TargetPtr |= ((Result & 0xffc) << (10 - 2)); |
| 457 | break; |
| 458 | } |
| 459 | case ELF::R_AARCH64_LDST64_ABS_LO12_NC: { |
| 460 | // Operation: S + A |
| 461 | uint64_t Result = Value + Addend; |
| 462 | |
| 463 | // AArch64 code is emitted with .rela relocations. The data already in any |
| 464 | // bits affected by the relocation on entry is garbage. |
| 465 | *TargetPtr &= 0xffc003ffU; |
| 466 | // Immediate goes in bits 21:10 of LD/ST instruction, taken |
| 467 | // from bits 11:3 of X |
| 468 | *TargetPtr |= ((Result & 0xff8) << (10 - 3)); |
| 469 | break; |
| 470 | } |
Tim Northover | 85829bb | 2013-05-04 20:13:59 +0000 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 474 | void RuntimeDyldELF::resolveARMRelocation(const SectionEntry &Section, |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 475 | uint64_t Offset, uint32_t Value, |
| 476 | uint32_t Type, int32_t Addend) { |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 477 | // TODO: Add Thumb relocations. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 478 | uint32_t *Placeholder = |
| 479 | reinterpret_cast<uint32_t *>(Section.ObjAddress + Offset); |
| 480 | uint32_t *TargetPtr = (uint32_t *)(Section.Address + Offset); |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 481 | uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 482 | Value += Addend; |
| 483 | |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 484 | DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: " |
| 485 | << Section.Address + Offset |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 486 | << " FinalAddress: " << format("%p", FinalAddress) << " Value: " |
| 487 | << format("%x", Value) << " Type: " << format("%x", Type) |
| 488 | << " Addend: " << format("%x", Addend) << "\n"); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 489 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 490 | switch (Type) { |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 491 | default: |
| 492 | llvm_unreachable("Not implemented relocation type!"); |
| 493 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 494 | case ELF::R_ARM_NONE: |
| 495 | break; |
Michael J. Spencer | 4d9c539 | 2013-01-04 20:36:28 +0000 | [diff] [blame] | 496 | // Write a 32bit value to relocation address, taking into account the |
Tim Northover | 565ebde | 2012-10-03 16:29:42 +0000 | [diff] [blame] | 497 | // implicit addend encoded in the target. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 498 | case ELF::R_ARM_PREL31: |
Tim Northover | e274b47 | 2013-05-28 19:48:19 +0000 | [diff] [blame] | 499 | case ELF::R_ARM_TARGET1: |
| 500 | case ELF::R_ARM_ABS32: |
| 501 | *TargetPtr = *Placeholder + Value; |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 502 | break; |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 503 | // Write first 16 bit of 32 bit value to the mov instruction. |
| 504 | // Last 4 bit should be shifted. |
Tim Northover | e274b47 | 2013-05-28 19:48:19 +0000 | [diff] [blame] | 505 | case ELF::R_ARM_MOVW_ABS_NC: |
Tim Northover | 565ebde | 2012-10-03 16:29:42 +0000 | [diff] [blame] | 506 | // We are not expecting any other addend in the relocation address. |
Michael J. Spencer | 4d9c539 | 2013-01-04 20:36:28 +0000 | [diff] [blame] | 507 | // Using 0x000F0FFF because MOVW has its 16 bit immediate split into 2 |
Tim Northover | 565ebde | 2012-10-03 16:29:42 +0000 | [diff] [blame] | 508 | // non-contiguous fields. |
Tim Northover | e274b47 | 2013-05-28 19:48:19 +0000 | [diff] [blame] | 509 | assert((*Placeholder & 0x000F0FFF) == 0); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 510 | Value = Value & 0xFFFF; |
Tim Northover | e274b47 | 2013-05-28 19:48:19 +0000 | [diff] [blame] | 511 | *TargetPtr = *Placeholder | (Value & 0xFFF); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 512 | *TargetPtr |= ((Value >> 12) & 0xF) << 16; |
| 513 | break; |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 514 | // Write last 16 bit of 32 bit value to the mov instruction. |
| 515 | // Last 4 bit should be shifted. |
Tim Northover | e274b47 | 2013-05-28 19:48:19 +0000 | [diff] [blame] | 516 | case ELF::R_ARM_MOVT_ABS: |
Tim Northover | 565ebde | 2012-10-03 16:29:42 +0000 | [diff] [blame] | 517 | // We are not expecting any other addend in the relocation address. |
| 518 | // Use 0x000F0FFF for the same reason as R_ARM_MOVW_ABS_NC. |
Tim Northover | e274b47 | 2013-05-28 19:48:19 +0000 | [diff] [blame] | 519 | assert((*Placeholder & 0x000F0FFF) == 0); |
| 520 | |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 521 | Value = (Value >> 16) & 0xFFFF; |
Tim Northover | e274b47 | 2013-05-28 19:48:19 +0000 | [diff] [blame] | 522 | *TargetPtr = *Placeholder | (Value & 0xFFF); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 523 | *TargetPtr |= ((Value >> 12) & 0xF) << 16; |
| 524 | break; |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 525 | // Write 24 bit relative value to the branch instruction. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 526 | case ELF::R_ARM_PC24: // Fall through. |
| 527 | case ELF::R_ARM_CALL: // Fall through. |
Tim Northover | e274b47 | 2013-05-28 19:48:19 +0000 | [diff] [blame] | 528 | case ELF::R_ARM_JUMP24: { |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 529 | int32_t RelValue = static_cast<int32_t>(Value - FinalAddress - 8); |
| 530 | RelValue = (RelValue & 0x03FFFFFC) >> 2; |
Tim Northover | e274b47 | 2013-05-28 19:48:19 +0000 | [diff] [blame] | 531 | assert((*TargetPtr & 0xFFFFFF) == 0xFFFFFE); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 532 | *TargetPtr &= 0xFF000000; |
| 533 | *TargetPtr |= RelValue; |
| 534 | break; |
| 535 | } |
Tim Northover | e274b47 | 2013-05-28 19:48:19 +0000 | [diff] [blame] | 536 | case ELF::R_ARM_PRIVATE_0: |
| 537 | // This relocation is reserved by the ARM ELF ABI for internal use. We |
| 538 | // appropriate it here to act as an R_ARM_ABS32 without any addend for use |
| 539 | // in the stubs created during JIT (which can't put an addend into the |
| 540 | // original object file). |
| 541 | *TargetPtr = Value; |
| 542 | break; |
| 543 | } |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 546 | void RuntimeDyldELF::resolveMIPSRelocation(const SectionEntry &Section, |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 547 | uint64_t Offset, uint32_t Value, |
| 548 | uint32_t Type, int32_t Addend) { |
| 549 | uint32_t *Placeholder = |
| 550 | reinterpret_cast<uint32_t *>(Section.ObjAddress + Offset); |
| 551 | uint32_t *TargetPtr = (uint32_t *)(Section.Address + Offset); |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 552 | Value += Addend; |
| 553 | |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 554 | DEBUG(dbgs() << "resolveMipselocation, LocalAddress: " |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 555 | << Section.Address + Offset << " FinalAddress: " |
| 556 | << format("%p", Section.LoadAddress + Offset) << " Value: " |
| 557 | << format("%x", Value) << " Type: " << format("%x", Type) |
| 558 | << " Addend: " << format("%x", Addend) << "\n"); |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 559 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 560 | switch (Type) { |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 561 | default: |
| 562 | llvm_unreachable("Not implemented relocation type!"); |
| 563 | break; |
| 564 | case ELF::R_MIPS_32: |
Akira Hatanaka | 3af1c9d | 2013-07-24 01:58:40 +0000 | [diff] [blame] | 565 | *TargetPtr = Value + (*Placeholder); |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 566 | break; |
| 567 | case ELF::R_MIPS_26: |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 568 | *TargetPtr = ((*Placeholder) & 0xfc000000) | ((Value & 0x0fffffff) >> 2); |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 569 | break; |
| 570 | case ELF::R_MIPS_HI16: |
| 571 | // Get the higher 16-bits. Also add 1 if bit 15 is 1. |
Akira Hatanaka | 3af1c9d | 2013-07-24 01:58:40 +0000 | [diff] [blame] | 572 | Value += ((*Placeholder) & 0x0000ffff) << 16; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 573 | *TargetPtr = |
| 574 | ((*Placeholder) & 0xffff0000) | (((Value + 0x8000) >> 16) & 0xffff); |
Akira Hatanaka | 3af1c9d | 2013-07-24 01:58:40 +0000 | [diff] [blame] | 575 | break; |
| 576 | case ELF::R_MIPS_LO16: |
| 577 | Value += ((*Placeholder) & 0x0000ffff); |
| 578 | *TargetPtr = ((*Placeholder) & 0xffff0000) | (Value & 0xffff); |
| 579 | break; |
| 580 | case ELF::R_MIPS_UNUSED1: |
| 581 | // Similar to ELF::R_ARM_PRIVATE_0, R_MIPS_UNUSED1 and R_MIPS_UNUSED2 |
| 582 | // are used for internal JIT purpose. These relocations are similar to |
| 583 | // R_MIPS_HI16 and R_MIPS_LO16, but they do not take any addend into |
| 584 | // account. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 585 | *TargetPtr = |
| 586 | ((*TargetPtr) & 0xffff0000) | (((Value + 0x8000) >> 16) & 0xffff); |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 587 | break; |
Akira Hatanaka | 3af1c9d | 2013-07-24 01:58:40 +0000 | [diff] [blame] | 588 | case ELF::R_MIPS_UNUSED2: |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 589 | *TargetPtr = ((*TargetPtr) & 0xffff0000) | (Value & 0xffff); |
| 590 | break; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 591 | } |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 594 | // Return the .TOC. section address to R_PPC64_TOC relocations. |
| 595 | uint64_t RuntimeDyldELF::findPPC64TOC() const { |
| 596 | // The TOC consists of sections .got, .toc, .tocbss, .plt in that |
| 597 | // order. The TOC starts where the first of these sections starts. |
| 598 | SectionList::const_iterator it = Sections.begin(); |
| 599 | SectionList::const_iterator ite = Sections.end(); |
| 600 | for (; it != ite; ++it) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 601 | if (it->Name == ".got" || it->Name == ".toc" || it->Name == ".tocbss" || |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 602 | it->Name == ".plt") |
| 603 | break; |
| 604 | } |
| 605 | if (it == ite) { |
| 606 | // This may happen for |
| 607 | // * references to TOC base base (sym@toc, .odp relocation) without |
| 608 | // a .toc directive. |
| 609 | // In this case just use the first section (which is usually |
| 610 | // the .odp) since the code won't reference the .toc base |
| 611 | // directly. |
| 612 | it = Sections.begin(); |
| 613 | } |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 614 | assert(it != ite); |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 615 | // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000 |
| 616 | // thus permitting a full 64 Kbytes segment. |
| 617 | return it->LoadAddress + 0x8000; |
| 618 | } |
| 619 | |
| 620 | // Returns the sections and offset associated with the ODP entry referenced |
| 621 | // by Symbol. |
| 622 | void RuntimeDyldELF::findOPDEntrySection(ObjectImage &Obj, |
| 623 | ObjSectionToIDMap &LocalSections, |
| 624 | RelocationValueRef &Rel) { |
| 625 | // Get the ELF symbol value (st_value) to compare with Relocation offset in |
| 626 | // .opd entries |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 627 | for (section_iterator si = Obj.begin_sections(), se = Obj.end_sections(); |
| 628 | si != se; ++si) { |
Rafael Espindola | 15e5c46 | 2013-06-03 19:37:34 +0000 | [diff] [blame] | 629 | section_iterator RelSecI = si->getRelocatedSection(); |
| 630 | if (RelSecI == Obj.end_sections()) |
| 631 | continue; |
| 632 | |
| 633 | StringRef RelSectionName; |
| 634 | check(RelSecI->getName(RelSectionName)); |
| 635 | if (RelSectionName != ".opd") |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 636 | continue; |
| 637 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 638 | for (relocation_iterator i = si->relocation_begin(), |
| 639 | e = si->relocation_end(); |
| 640 | i != e;) { |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 641 | // The R_PPC64_ADDR64 relocation indicates the first field |
| 642 | // of a .opd entry |
| 643 | uint64_t TypeFunc; |
| 644 | check(i->getType(TypeFunc)); |
| 645 | if (TypeFunc != ELF::R_PPC64_ADDR64) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 646 | ++i; |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 647 | continue; |
| 648 | } |
| 649 | |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 650 | uint64_t TargetSymbolOffset; |
Rafael Espindola | 6c1202c | 2013-06-05 01:33:53 +0000 | [diff] [blame] | 651 | symbol_iterator TargetSymbol = i->getSymbol(); |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 652 | check(i->getOffset(TargetSymbolOffset)); |
Rafael Espindola | 167957f | 2013-05-09 03:39:05 +0000 | [diff] [blame] | 653 | int64_t Addend; |
| 654 | check(getELFRelocationAddend(*i, Addend)); |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 655 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 656 | ++i; |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 657 | if (i == e) |
| 658 | break; |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 659 | |
| 660 | // Just check if following relocation is a R_PPC64_TOC |
| 661 | uint64_t TypeTOC; |
| 662 | check(i->getType(TypeTOC)); |
| 663 | if (TypeTOC != ELF::R_PPC64_TOC) |
| 664 | continue; |
| 665 | |
| 666 | // Finally compares the Symbol value and the target symbol offset |
| 667 | // to check if this .opd entry refers to the symbol the relocation |
| 668 | // points to. |
Andrew Kaylor | ff9fa05 | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 669 | if (Rel.Addend != (int64_t)TargetSymbolOffset) |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 670 | continue; |
| 671 | |
| 672 | section_iterator tsi(Obj.end_sections()); |
Rafael Espindola | 6c1202c | 2013-06-05 01:33:53 +0000 | [diff] [blame] | 673 | check(TargetSymbol->getSection(tsi)); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 674 | bool IsCode = false; |
| 675 | tsi->isText(IsCode); |
| 676 | Rel.SectionID = findOrEmitSection(Obj, (*tsi), IsCode, LocalSections); |
Rafael Espindola | 167957f | 2013-05-09 03:39:05 +0000 | [diff] [blame] | 677 | Rel.Addend = (intptr_t)Addend; |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 678 | return; |
| 679 | } |
| 680 | } |
| 681 | llvm_unreachable("Attempting to get address of ODP entry!"); |
| 682 | } |
| 683 | |
| 684 | // Relocation masks following the #lo(value), #hi(value), #higher(value), |
| 685 | // and #highest(value) macros defined in section 4.5.1. Relocation Types |
| 686 | // in PPC-elf64abi document. |
| 687 | // |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 688 | static inline uint16_t applyPPClo(uint64_t value) { return value & 0xffff; } |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 689 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 690 | static inline uint16_t applyPPChi(uint64_t value) { |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 691 | return (value >> 16) & 0xffff; |
| 692 | } |
| 693 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 694 | static inline uint16_t applyPPChigher(uint64_t value) { |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 695 | return (value >> 32) & 0xffff; |
| 696 | } |
| 697 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 698 | static inline uint16_t applyPPChighest(uint64_t value) { |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 699 | return (value >> 48) & 0xffff; |
| 700 | } |
| 701 | |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 702 | void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntry &Section, |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 703 | uint64_t Offset, uint64_t Value, |
| 704 | uint32_t Type, int64_t Addend) { |
| 705 | uint8_t *LocalAddress = Section.Address + Offset; |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 706 | switch (Type) { |
| 707 | default: |
| 708 | llvm_unreachable("Relocation type not implemented yet!"); |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 709 | break; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 710 | case ELF::R_PPC64_ADDR16_LO: |
| 711 | writeInt16BE(LocalAddress, applyPPClo(Value + Addend)); |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 712 | break; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 713 | case ELF::R_PPC64_ADDR16_HI: |
| 714 | writeInt16BE(LocalAddress, applyPPChi(Value + Addend)); |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 715 | break; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 716 | case ELF::R_PPC64_ADDR16_HIGHER: |
| 717 | writeInt16BE(LocalAddress, applyPPChigher(Value + Addend)); |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 718 | break; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 719 | case ELF::R_PPC64_ADDR16_HIGHEST: |
| 720 | writeInt16BE(LocalAddress, applyPPChighest(Value + Addend)); |
| 721 | break; |
| 722 | case ELF::R_PPC64_ADDR14: { |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 723 | assert(((Value + Addend) & 3) == 0); |
| 724 | // Preserve the AA/LK bits in the branch instruction |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 725 | uint8_t aalk = *(LocalAddress + 3); |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 726 | writeInt16BE(LocalAddress + 2, (aalk & 3) | ((Value + Addend) & 0xfffc)); |
| 727 | } break; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 728 | case ELF::R_PPC64_ADDR32: { |
Adhemerval Zanella | 7b44988 | 2013-01-04 19:08:13 +0000 | [diff] [blame] | 729 | int32_t Result = static_cast<int32_t>(Value + Addend); |
| 730 | if (SignExtend32<32>(Result) != Result) |
Adhemerval Zanella | a1db5de | 2013-01-09 17:08:15 +0000 | [diff] [blame] | 731 | llvm_unreachable("Relocation R_PPC64_ADDR32 overflow"); |
Adhemerval Zanella | 7b44988 | 2013-01-04 19:08:13 +0000 | [diff] [blame] | 732 | writeInt32BE(LocalAddress, Result); |
| 733 | } break; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 734 | case ELF::R_PPC64_REL24: { |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 735 | uint64_t FinalAddress = (Section.LoadAddress + Offset); |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 736 | int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend); |
| 737 | if (SignExtend32<24>(delta) != delta) |
| 738 | llvm_unreachable("Relocation R_PPC64_REL24 overflow"); |
| 739 | // Generates a 'bl <address>' instruction |
| 740 | writeInt32BE(LocalAddress, 0x48000001 | (delta & 0x03FFFFFC)); |
| 741 | } break; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 742 | case ELF::R_PPC64_REL32: { |
Adhemerval Zanella | a1db5de | 2013-01-09 17:08:15 +0000 | [diff] [blame] | 743 | uint64_t FinalAddress = (Section.LoadAddress + Offset); |
| 744 | int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend); |
| 745 | if (SignExtend32<32>(delta) != delta) |
| 746 | llvm_unreachable("Relocation R_PPC64_REL32 overflow"); |
| 747 | writeInt32BE(LocalAddress, delta); |
| 748 | } break; |
Adhemerval Zanella | f51d7e7 | 2013-05-06 17:21:23 +0000 | [diff] [blame] | 749 | case ELF::R_PPC64_REL64: { |
| 750 | uint64_t FinalAddress = (Section.LoadAddress + Offset); |
| 751 | uint64_t Delta = Value - FinalAddress + Addend; |
| 752 | writeInt64BE(LocalAddress, Delta); |
| 753 | } break; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 754 | case ELF::R_PPC64_ADDR64: |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 755 | writeInt64BE(LocalAddress, Value + Addend); |
| 756 | break; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 757 | case ELF::R_PPC64_TOC: |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 758 | writeInt64BE(LocalAddress, findPPC64TOC()); |
| 759 | break; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 760 | case ELF::R_PPC64_TOC16: { |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 761 | uint64_t TOCStart = findPPC64TOC(); |
| 762 | Value = applyPPClo((Value + Addend) - TOCStart); |
| 763 | writeInt16BE(LocalAddress, applyPPClo(Value)); |
| 764 | } break; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 765 | case ELF::R_PPC64_TOC16_DS: { |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 766 | uint64_t TOCStart = findPPC64TOC(); |
| 767 | Value = ((Value + Addend) - TOCStart); |
| 768 | writeInt16BE(LocalAddress, applyPPClo(Value)); |
| 769 | } break; |
| 770 | } |
| 771 | } |
| 772 | |
Richard Sandiford | 6fc2ad6 | 2013-05-03 14:15:35 +0000 | [diff] [blame] | 773 | void RuntimeDyldELF::resolveSystemZRelocation(const SectionEntry &Section, |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 774 | uint64_t Offset, uint64_t Value, |
| 775 | uint32_t Type, int64_t Addend) { |
Richard Sandiford | 6fc2ad6 | 2013-05-03 14:15:35 +0000 | [diff] [blame] | 776 | uint8_t *LocalAddress = Section.Address + Offset; |
| 777 | switch (Type) { |
| 778 | default: |
| 779 | llvm_unreachable("Relocation type not implemented yet!"); |
| 780 | break; |
| 781 | case ELF::R_390_PC16DBL: |
| 782 | case ELF::R_390_PLT16DBL: { |
| 783 | int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset); |
| 784 | assert(int16_t(Delta / 2) * 2 == Delta && "R_390_PC16DBL overflow"); |
| 785 | writeInt16BE(LocalAddress, Delta / 2); |
| 786 | break; |
| 787 | } |
| 788 | case ELF::R_390_PC32DBL: |
| 789 | case ELF::R_390_PLT32DBL: { |
| 790 | int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset); |
| 791 | assert(int32_t(Delta / 2) * 2 == Delta && "R_390_PC32DBL overflow"); |
| 792 | writeInt32BE(LocalAddress, Delta / 2); |
| 793 | break; |
| 794 | } |
| 795 | case ELF::R_390_PC32: { |
| 796 | int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset); |
| 797 | assert(int32_t(Delta) == Delta && "R_390_PC32 overflow"); |
| 798 | writeInt32BE(LocalAddress, Delta); |
| 799 | break; |
| 800 | } |
| 801 | case ELF::R_390_64: |
| 802 | writeInt64BE(LocalAddress, Value + Addend); |
| 803 | break; |
| 804 | } |
| 805 | } |
| 806 | |
Andrew Kaylor | 32bd10b | 2013-08-19 19:38:06 +0000 | [diff] [blame] | 807 | // The target location for the relocation is described by RE.SectionID and |
| 808 | // RE.Offset. RE.SectionID can be used to find the SectionEntry. Each |
| 809 | // SectionEntry has three members describing its location. |
| 810 | // SectionEntry::Address is the address at which the section has been loaded |
| 811 | // into memory in the current (host) process. SectionEntry::LoadAddress is the |
| 812 | // address that the section will have in the target process. |
| 813 | // SectionEntry::ObjAddress is the address of the bits for this section in the |
| 814 | // original emitted object image (also in the current address space). |
| 815 | // |
| 816 | // Relocations will be applied as if the section were loaded at |
| 817 | // SectionEntry::LoadAddress, but they will be applied at an address based |
| 818 | // on SectionEntry::Address. SectionEntry::ObjAddress will be used to refer to |
| 819 | // Target memory contents if they are required for value calculations. |
| 820 | // |
| 821 | // The Value parameter here is the load address of the symbol for the |
| 822 | // relocation to be applied. For relocations which refer to symbols in the |
| 823 | // current object Value will be the LoadAddress of the section in which |
| 824 | // the symbol resides (RE.Addend provides additional information about the |
| 825 | // symbol location). For external symbols, Value will be the address of the |
| 826 | // symbol in the target address space. |
Rafael Espindola | 87b5017 | 2013-04-29 17:24:34 +0000 | [diff] [blame] | 827 | void RuntimeDyldELF::resolveRelocation(const RelocationEntry &RE, |
Andrew Kaylor | 32bd10b | 2013-08-19 19:38:06 +0000 | [diff] [blame] | 828 | uint64_t Value) { |
Rafael Espindola | 87b5017 | 2013-04-29 17:24:34 +0000 | [diff] [blame] | 829 | const SectionEntry &Section = Sections[RE.SectionID]; |
Andrew Kaylor | ff9fa05 | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 830 | return resolveRelocation(Section, RE.Offset, Value, RE.RelType, RE.Addend, |
| 831 | RE.SymOffset); |
Rafael Espindola | 87b5017 | 2013-04-29 17:24:34 +0000 | [diff] [blame] | 832 | } |
| 833 | |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 834 | void RuntimeDyldELF::resolveRelocation(const SectionEntry &Section, |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 835 | uint64_t Offset, uint64_t Value, |
| 836 | uint32_t Type, int64_t Addend, |
Andrew Kaylor | ff9fa05 | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 837 | uint64_t SymOffset) { |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 838 | switch (Arch) { |
| 839 | case Triple::x86_64: |
Andrew Kaylor | ff9fa05 | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 840 | resolveX86_64Relocation(Section, Offset, Value, Type, Addend, SymOffset); |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 841 | break; |
| 842 | case Triple::x86: |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 843 | resolveX86Relocation(Section, Offset, (uint32_t)(Value & 0xffffffffL), Type, |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 844 | (uint32_t)(Addend & 0xffffffffL)); |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 845 | break; |
Tim Northover | 85829bb | 2013-05-04 20:13:59 +0000 | [diff] [blame] | 846 | case Triple::aarch64: |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 847 | case Triple::aarch64_be: |
Tim Northover | 85829bb | 2013-05-04 20:13:59 +0000 | [diff] [blame] | 848 | resolveAArch64Relocation(Section, Offset, Value, Type, Addend); |
| 849 | break; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 850 | case Triple::arm: // Fall through. |
| 851 | case Triple::armeb: |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 852 | case Triple::thumb: |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 853 | case Triple::thumbeb: |
| 854 | resolveARMRelocation(Section, Offset, (uint32_t)(Value & 0xffffffffL), Type, |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 855 | (uint32_t)(Addend & 0xffffffffL)); |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 856 | break; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 857 | case Triple::mips: // Fall through. |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 858 | case Triple::mipsel: |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 859 | resolveMIPSRelocation(Section, Offset, (uint32_t)(Value & 0xffffffffL), |
| 860 | Type, (uint32_t)(Addend & 0xffffffffL)); |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 861 | break; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 862 | case Triple::ppc64: // Fall through. |
Bill Schmidt | f38cc38 | 2013-07-26 01:35:43 +0000 | [diff] [blame] | 863 | case Triple::ppc64le: |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 864 | resolvePPC64Relocation(Section, Offset, Value, Type, Addend); |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 865 | break; |
Richard Sandiford | 6fc2ad6 | 2013-05-03 14:15:35 +0000 | [diff] [blame] | 866 | case Triple::systemz: |
| 867 | resolveSystemZRelocation(Section, Offset, Value, Type, Addend); |
| 868 | break; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 869 | default: |
| 870 | llvm_unreachable("Unsupported CPU type!"); |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 871 | } |
| 872 | } |
| 873 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 874 | relocation_iterator RuntimeDyldELF::processRelocationRef( |
| 875 | unsigned SectionID, relocation_iterator RelI, ObjectImage &Obj, |
| 876 | ObjSectionToIDMap &ObjSectionToID, const SymbolTableMap &Symbols, |
| 877 | StubMap &Stubs) { |
Rafael Espindola | efa91f6 | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 878 | uint64_t RelType; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 879 | Check(RelI->getType(RelType)); |
Rafael Espindola | efa91f6 | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 880 | int64_t Addend; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 881 | Check(getELFRelocationAddend(*RelI, Addend)); |
| 882 | symbol_iterator Symbol = RelI->getSymbol(); |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 883 | |
| 884 | // Obtain the symbol name which is referenced in the relocation |
| 885 | StringRef TargetName; |
Rafael Espindola | 0962b16 | 2013-06-05 02:55:01 +0000 | [diff] [blame] | 886 | if (Symbol != Obj.end_symbols()) |
| 887 | Symbol->getName(TargetName); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 888 | DEBUG(dbgs() << "\t\tRelType: " << RelType << " Addend: " << Addend |
| 889 | << " TargetName: " << TargetName << "\n"); |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 890 | RelocationValueRef Value; |
| 891 | // First search for the symbol in the local symbol table |
Rafael Espindola | 0962b16 | 2013-06-05 02:55:01 +0000 | [diff] [blame] | 892 | SymbolTableMap::const_iterator lsi = Symbols.end(); |
| 893 | SymbolRef::Type SymType = SymbolRef::ST_Unknown; |
| 894 | if (Symbol != Obj.end_symbols()) { |
| 895 | lsi = Symbols.find(TargetName.data()); |
| 896 | Symbol->getType(SymType); |
| 897 | } |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 898 | if (lsi != Symbols.end()) { |
| 899 | Value.SectionID = lsi->second.first; |
Andrew Kaylor | ff9fa05 | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 900 | Value.Offset = lsi->second.second; |
Ulrich Weigand | 03f018a | 2013-04-05 13:29:04 +0000 | [diff] [blame] | 901 | Value.Addend = lsi->second.second + Addend; |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 902 | } else { |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 903 | // Search for the symbol in the global symbol table |
Rafael Espindola | 0962b16 | 2013-06-05 02:55:01 +0000 | [diff] [blame] | 904 | SymbolTableMap::const_iterator gsi = GlobalSymbolTable.end(); |
| 905 | if (Symbol != Obj.end_symbols()) |
| 906 | gsi = GlobalSymbolTable.find(TargetName.data()); |
Eli Bendersky | d98c9e9 | 2012-05-01 06:58:59 +0000 | [diff] [blame] | 907 | if (gsi != GlobalSymbolTable.end()) { |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 908 | Value.SectionID = gsi->second.first; |
Andrew Kaylor | ff9fa05 | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 909 | Value.Offset = gsi->second.second; |
Ulrich Weigand | 03f018a | 2013-04-05 13:29:04 +0000 | [diff] [blame] | 910 | Value.Addend = gsi->second.second + Addend; |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 911 | } else { |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 912 | switch (SymType) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 913 | case SymbolRef::ST_Debug: { |
| 914 | // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously |
| 915 | // and can be changed by another developers. Maybe best way is add |
| 916 | // a new symbol type ST_Section to SymbolRef and use it. |
| 917 | section_iterator si(Obj.end_sections()); |
| 918 | Symbol->getSection(si); |
| 919 | if (si == Obj.end_sections()) |
| 920 | llvm_unreachable("Symbol section not found, bad object file format!"); |
| 921 | DEBUG(dbgs() << "\t\tThis is section symbol\n"); |
| 922 | // Default to 'true' in case isText fails (though it never does). |
| 923 | bool isCode = true; |
| 924 | si->isText(isCode); |
| 925 | Value.SectionID = findOrEmitSection(Obj, (*si), isCode, ObjSectionToID); |
| 926 | Value.Addend = Addend; |
| 927 | break; |
| 928 | } |
| 929 | case SymbolRef::ST_Data: |
| 930 | case SymbolRef::ST_Unknown: { |
| 931 | Value.SymbolName = TargetName.data(); |
| 932 | Value.Addend = Addend; |
Richard Mitton | b0f7929 | 2013-08-16 18:54:26 +0000 | [diff] [blame] | 933 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 934 | // Absolute relocations will have a zero symbol ID (STN_UNDEF), which |
| 935 | // will manifest here as a NULL symbol name. |
| 936 | // We can set this as a valid (but empty) symbol name, and rely |
| 937 | // on addRelocationForSymbol to handle this. |
| 938 | if (!Value.SymbolName) |
| 939 | Value.SymbolName = ""; |
| 940 | break; |
| 941 | } |
| 942 | default: |
| 943 | llvm_unreachable("Unresolved symbol type!"); |
| 944 | break; |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 945 | } |
| 946 | } |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 947 | } |
Rafael Espindola | efa91f6 | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 948 | uint64_t Offset; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 949 | Check(RelI->getOffset(Offset)); |
Rafael Espindola | efa91f6 | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 950 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 951 | DEBUG(dbgs() << "\t\tSectionID: " << SectionID << " Offset: " << Offset |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 952 | << "\n"); |
Tim Northover | 4a9b6b7 | 2013-05-04 20:14:09 +0000 | [diff] [blame] | 953 | if (Arch == Triple::aarch64 && |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 954 | (RelType == ELF::R_AARCH64_CALL26 || RelType == ELF::R_AARCH64_JUMP26)) { |
Tim Northover | 4a9b6b7 | 2013-05-04 20:14:09 +0000 | [diff] [blame] | 955 | // This is an AArch64 branch relocation, need to use a stub function. |
| 956 | DEBUG(dbgs() << "\t\tThis is an AArch64 branch relocation."); |
| 957 | SectionEntry &Section = Sections[SectionID]; |
| 958 | |
| 959 | // Look for an existing stub. |
| 960 | StubMap::const_iterator i = Stubs.find(Value); |
| 961 | if (i != Stubs.end()) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 962 | resolveRelocation(Section, Offset, (uint64_t)Section.Address + i->second, |
| 963 | RelType, 0); |
Tim Northover | 4a9b6b7 | 2013-05-04 20:14:09 +0000 | [diff] [blame] | 964 | DEBUG(dbgs() << " Stub function found\n"); |
| 965 | } else { |
| 966 | // Create a new stub function. |
| 967 | DEBUG(dbgs() << " Create a new stub function\n"); |
| 968 | Stubs[Value] = Section.StubOffset; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 969 | uint8_t *StubTargetAddr = |
| 970 | createStubFunction(Section.Address + Section.StubOffset); |
Tim Northover | 4a9b6b7 | 2013-05-04 20:14:09 +0000 | [diff] [blame] | 971 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 972 | RelocationEntry REmovz_g3(SectionID, StubTargetAddr - Section.Address, |
Tim Northover | 4a9b6b7 | 2013-05-04 20:14:09 +0000 | [diff] [blame] | 973 | ELF::R_AARCH64_MOVW_UABS_G3, Value.Addend); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 974 | RelocationEntry REmovk_g2(SectionID, StubTargetAddr - Section.Address + 4, |
Tim Northover | 4a9b6b7 | 2013-05-04 20:14:09 +0000 | [diff] [blame] | 975 | ELF::R_AARCH64_MOVW_UABS_G2_NC, Value.Addend); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 976 | RelocationEntry REmovk_g1(SectionID, StubTargetAddr - Section.Address + 8, |
Tim Northover | 4a9b6b7 | 2013-05-04 20:14:09 +0000 | [diff] [blame] | 977 | ELF::R_AARCH64_MOVW_UABS_G1_NC, Value.Addend); |
| 978 | RelocationEntry REmovk_g0(SectionID, |
| 979 | StubTargetAddr - Section.Address + 12, |
| 980 | ELF::R_AARCH64_MOVW_UABS_G0_NC, Value.Addend); |
| 981 | |
| 982 | if (Value.SymbolName) { |
| 983 | addRelocationForSymbol(REmovz_g3, Value.SymbolName); |
| 984 | addRelocationForSymbol(REmovk_g2, Value.SymbolName); |
| 985 | addRelocationForSymbol(REmovk_g1, Value.SymbolName); |
| 986 | addRelocationForSymbol(REmovk_g0, Value.SymbolName); |
| 987 | } else { |
| 988 | addRelocationForSection(REmovz_g3, Value.SectionID); |
| 989 | addRelocationForSection(REmovk_g2, Value.SectionID); |
| 990 | addRelocationForSection(REmovk_g1, Value.SectionID); |
| 991 | addRelocationForSection(REmovk_g0, Value.SectionID); |
| 992 | } |
| 993 | resolveRelocation(Section, Offset, |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 994 | (uint64_t)Section.Address + Section.StubOffset, RelType, |
| 995 | 0); |
Tim Northover | 4a9b6b7 | 2013-05-04 20:14:09 +0000 | [diff] [blame] | 996 | Section.StubOffset += getMaxStubSize(); |
| 997 | } |
| 998 | } else if (Arch == Triple::arm && |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 999 | (RelType == ELF::R_ARM_PC24 || RelType == ELF::R_ARM_CALL || |
| 1000 | RelType == ELF::R_ARM_JUMP24)) { |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 1001 | // This is an ARM branch relocation, need to use a stub function. |
| 1002 | DEBUG(dbgs() << "\t\tThis is an ARM branch relocation."); |
Rafael Espindola | efa91f6 | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 1003 | SectionEntry &Section = Sections[SectionID]; |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 1004 | |
Eric Christopher | bf261f1 | 2012-10-23 17:19:15 +0000 | [diff] [blame] | 1005 | // Look for an existing stub. |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 1006 | StubMap::const_iterator i = Stubs.find(Value); |
| 1007 | if (i != Stubs.end()) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1008 | resolveRelocation(Section, Offset, (uint64_t)Section.Address + i->second, |
| 1009 | RelType, 0); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 1010 | DEBUG(dbgs() << " Stub function found\n"); |
| 1011 | } else { |
| 1012 | // Create a new stub function. |
| 1013 | DEBUG(dbgs() << " Create a new stub function\n"); |
| 1014 | Stubs[Value] = Section.StubOffset; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1015 | uint8_t *StubTargetAddr = |
| 1016 | createStubFunction(Section.Address + Section.StubOffset); |
Rafael Espindola | efa91f6 | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 1017 | RelocationEntry RE(SectionID, StubTargetAddr - Section.Address, |
Tim Northover | e274b47 | 2013-05-28 19:48:19 +0000 | [diff] [blame] | 1018 | ELF::R_ARM_PRIVATE_0, Value.Addend); |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 1019 | if (Value.SymbolName) |
| 1020 | addRelocationForSymbol(RE, Value.SymbolName); |
| 1021 | else |
| 1022 | addRelocationForSection(RE, Value.SectionID); |
| 1023 | |
Rafael Espindola | efa91f6 | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 1024 | resolveRelocation(Section, Offset, |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1025 | (uint64_t)Section.Address + Section.StubOffset, RelType, |
| 1026 | 0); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 1027 | Section.StubOffset += getMaxStubSize(); |
| 1028 | } |
Akira Hatanaka | ade0474 | 2012-12-03 23:12:19 +0000 | [diff] [blame] | 1029 | } else if ((Arch == Triple::mipsel || Arch == Triple::mips) && |
| 1030 | RelType == ELF::R_MIPS_26) { |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 1031 | // This is an Mips branch relocation, need to use a stub function. |
| 1032 | DEBUG(dbgs() << "\t\tThis is a Mips branch relocation."); |
Rafael Espindola | efa91f6 | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 1033 | SectionEntry &Section = Sections[SectionID]; |
| 1034 | uint8_t *Target = Section.Address + Offset; |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 1035 | uint32_t *TargetAddress = (uint32_t *)Target; |
| 1036 | |
| 1037 | // Extract the addend from the instruction. |
| 1038 | uint32_t Addend = ((*TargetAddress) & 0x03ffffff) << 2; |
| 1039 | |
| 1040 | Value.Addend += Addend; |
| 1041 | |
| 1042 | // Look up for existing stub. |
| 1043 | StubMap::const_iterator i = Stubs.find(Value); |
| 1044 | if (i != Stubs.end()) { |
Petar Jovanovic | 0ff917e | 2013-11-21 00:52:34 +0000 | [diff] [blame] | 1045 | RelocationEntry RE(SectionID, Offset, RelType, i->second); |
| 1046 | addRelocationForSection(RE, SectionID); |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 1047 | DEBUG(dbgs() << " Stub function found\n"); |
| 1048 | } else { |
| 1049 | // Create a new stub function. |
| 1050 | DEBUG(dbgs() << " Create a new stub function\n"); |
| 1051 | Stubs[Value] = Section.StubOffset; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1052 | uint8_t *StubTargetAddr = |
| 1053 | createStubFunction(Section.Address + Section.StubOffset); |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 1054 | |
| 1055 | // Creating Hi and Lo relocations for the filled stub instructions. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1056 | RelocationEntry REHi(SectionID, StubTargetAddr - Section.Address, |
Akira Hatanaka | 3af1c9d | 2013-07-24 01:58:40 +0000 | [diff] [blame] | 1057 | ELF::R_MIPS_UNUSED1, Value.Addend); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1058 | RelocationEntry RELo(SectionID, StubTargetAddr - Section.Address + 4, |
Akira Hatanaka | 3af1c9d | 2013-07-24 01:58:40 +0000 | [diff] [blame] | 1059 | ELF::R_MIPS_UNUSED2, Value.Addend); |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 1060 | |
| 1061 | if (Value.SymbolName) { |
| 1062 | addRelocationForSymbol(REHi, Value.SymbolName); |
| 1063 | addRelocationForSymbol(RELo, Value.SymbolName); |
| 1064 | } else { |
| 1065 | addRelocationForSection(REHi, Value.SectionID); |
| 1066 | addRelocationForSection(RELo, Value.SectionID); |
| 1067 | } |
| 1068 | |
Petar Jovanovic | 0ff917e | 2013-11-21 00:52:34 +0000 | [diff] [blame] | 1069 | RelocationEntry RE(SectionID, Offset, RelType, Section.StubOffset); |
| 1070 | addRelocationForSection(RE, SectionID); |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 1071 | Section.StubOffset += getMaxStubSize(); |
| 1072 | } |
Bill Schmidt | f38cc38 | 2013-07-26 01:35:43 +0000 | [diff] [blame] | 1073 | } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) { |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 1074 | if (RelType == ELF::R_PPC64_REL24) { |
| 1075 | // A PPC branch relocation will need a stub function if the target is |
| 1076 | // an external symbol (Symbol::ST_Unknown) or if the target address |
| 1077 | // is not within the signed 24-bits branch address. |
Rafael Espindola | efa91f6 | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 1078 | SectionEntry &Section = Sections[SectionID]; |
| 1079 | uint8_t *Target = Section.Address + Offset; |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 1080 | bool RangeOverflow = false; |
| 1081 | if (SymType != SymbolRef::ST_Unknown) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1082 | // A function call may points to the .opd entry, so the final symbol |
| 1083 | // value |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 1084 | // in calculated based in the relocation values in .opd section. |
| 1085 | findOPDEntrySection(Obj, ObjSectionToID, Value); |
| 1086 | uint8_t *RelocTarget = Sections[Value.SectionID].Address + Value.Addend; |
| 1087 | int32_t delta = static_cast<int32_t>(Target - RelocTarget); |
| 1088 | // If it is within 24-bits branch range, just set the branch target |
| 1089 | if (SignExtend32<24>(delta) == delta) { |
Rafael Espindola | efa91f6 | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 1090 | RelocationEntry RE(SectionID, Offset, RelType, Value.Addend); |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 1091 | if (Value.SymbolName) |
| 1092 | addRelocationForSymbol(RE, Value.SymbolName); |
| 1093 | else |
| 1094 | addRelocationForSection(RE, Value.SectionID); |
| 1095 | } else { |
| 1096 | RangeOverflow = true; |
| 1097 | } |
| 1098 | } |
| 1099 | if (SymType == SymbolRef::ST_Unknown || RangeOverflow == true) { |
| 1100 | // It is an external symbol (SymbolRef::ST_Unknown) or within a range |
| 1101 | // larger than 24-bits. |
| 1102 | StubMap::const_iterator i = Stubs.find(Value); |
| 1103 | if (i != Stubs.end()) { |
| 1104 | // Symbol function stub already created, just relocate to it |
Rafael Espindola | efa91f6 | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 1105 | resolveRelocation(Section, Offset, |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 1106 | (uint64_t)Section.Address + i->second, RelType, 0); |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 1107 | DEBUG(dbgs() << " Stub function found\n"); |
| 1108 | } else { |
| 1109 | // Create a new stub function. |
| 1110 | DEBUG(dbgs() << " Create a new stub function\n"); |
| 1111 | Stubs[Value] = Section.StubOffset; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1112 | uint8_t *StubTargetAddr = |
| 1113 | createStubFunction(Section.Address + Section.StubOffset); |
Rafael Espindola | efa91f6 | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 1114 | RelocationEntry RE(SectionID, StubTargetAddr - Section.Address, |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 1115 | ELF::R_PPC64_ADDR64, Value.Addend); |
| 1116 | |
| 1117 | // Generates the 64-bits address loads as exemplified in section |
| 1118 | // 4.5.1 in PPC64 ELF ABI. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1119 | RelocationEntry REhst(SectionID, StubTargetAddr - Section.Address + 2, |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 1120 | ELF::R_PPC64_ADDR16_HIGHEST, Value.Addend); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1121 | RelocationEntry REhr(SectionID, StubTargetAddr - Section.Address + 6, |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 1122 | ELF::R_PPC64_ADDR16_HIGHER, Value.Addend); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1123 | RelocationEntry REh(SectionID, StubTargetAddr - Section.Address + 14, |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 1124 | ELF::R_PPC64_ADDR16_HI, Value.Addend); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1125 | RelocationEntry REl(SectionID, StubTargetAddr - Section.Address + 18, |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 1126 | ELF::R_PPC64_ADDR16_LO, Value.Addend); |
| 1127 | |
| 1128 | if (Value.SymbolName) { |
| 1129 | addRelocationForSymbol(REhst, Value.SymbolName); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1130 | addRelocationForSymbol(REhr, Value.SymbolName); |
| 1131 | addRelocationForSymbol(REh, Value.SymbolName); |
| 1132 | addRelocationForSymbol(REl, Value.SymbolName); |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 1133 | } else { |
| 1134 | addRelocationForSection(REhst, Value.SectionID); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1135 | addRelocationForSection(REhr, Value.SectionID); |
| 1136 | addRelocationForSection(REh, Value.SectionID); |
| 1137 | addRelocationForSection(REl, Value.SectionID); |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
Rafael Espindola | efa91f6 | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 1140 | resolveRelocation(Section, Offset, |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 1141 | (uint64_t)Section.Address + Section.StubOffset, |
| 1142 | RelType, 0); |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 1143 | Section.StubOffset += getMaxStubSize(); |
| 1144 | } |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1145 | if (SymType == SymbolRef::ST_Unknown) |
| 1146 | // Restore the TOC for external calls |
| 1147 | writeInt32BE(Target + 4, 0xE8410028); // ld r2,40(r1) |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 1148 | } |
| 1149 | } else { |
Rafael Espindola | efa91f6 | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 1150 | RelocationEntry RE(SectionID, Offset, RelType, Value.Addend); |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 1151 | // Extra check to avoid relocation againt empty symbols (usually |
| 1152 | // the R_PPC64_TOC). |
Richard Mitton | b0f7929 | 2013-08-16 18:54:26 +0000 | [diff] [blame] | 1153 | if (SymType != SymbolRef::ST_Unknown && TargetName.empty()) |
| 1154 | Value.SymbolName = NULL; |
| 1155 | |
| 1156 | if (Value.SymbolName) |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 1157 | addRelocationForSymbol(RE, Value.SymbolName); |
| 1158 | else |
| 1159 | addRelocationForSection(RE, Value.SectionID); |
| 1160 | } |
Richard Sandiford | 6fc2ad6 | 2013-05-03 14:15:35 +0000 | [diff] [blame] | 1161 | } else if (Arch == Triple::systemz && |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1162 | (RelType == ELF::R_390_PLT32DBL || RelType == ELF::R_390_GOTENT)) { |
Richard Sandiford | 6fc2ad6 | 2013-05-03 14:15:35 +0000 | [diff] [blame] | 1163 | // Create function stubs for both PLT and GOT references, regardless of |
| 1164 | // whether the GOT reference is to data or code. The stub contains the |
| 1165 | // full address of the symbol, as needed by GOT references, and the |
| 1166 | // executable part only adds an overhead of 8 bytes. |
| 1167 | // |
| 1168 | // We could try to conserve space by allocating the code and data |
| 1169 | // parts of the stub separately. However, as things stand, we allocate |
| 1170 | // a stub for every relocation, so using a GOT in JIT code should be |
| 1171 | // no less space efficient than using an explicit constant pool. |
| 1172 | DEBUG(dbgs() << "\t\tThis is a SystemZ indirect relocation."); |
| 1173 | SectionEntry &Section = Sections[SectionID]; |
| 1174 | |
| 1175 | // Look for an existing stub. |
| 1176 | StubMap::const_iterator i = Stubs.find(Value); |
| 1177 | uintptr_t StubAddress; |
| 1178 | if (i != Stubs.end()) { |
| 1179 | StubAddress = uintptr_t(Section.Address) + i->second; |
| 1180 | DEBUG(dbgs() << " Stub function found\n"); |
| 1181 | } else { |
| 1182 | // Create a new stub function. |
| 1183 | DEBUG(dbgs() << " Create a new stub function\n"); |
| 1184 | |
| 1185 | uintptr_t BaseAddress = uintptr_t(Section.Address); |
| 1186 | uintptr_t StubAlignment = getStubAlignment(); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1187 | StubAddress = (BaseAddress + Section.StubOffset + StubAlignment - 1) & |
| 1188 | -StubAlignment; |
Richard Sandiford | 6fc2ad6 | 2013-05-03 14:15:35 +0000 | [diff] [blame] | 1189 | unsigned StubOffset = StubAddress - BaseAddress; |
| 1190 | |
| 1191 | Stubs[Value] = StubOffset; |
| 1192 | createStubFunction((uint8_t *)StubAddress); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1193 | RelocationEntry RE(SectionID, StubOffset + 8, ELF::R_390_64, |
| 1194 | Value.Addend - Addend); |
Richard Sandiford | 6fc2ad6 | 2013-05-03 14:15:35 +0000 | [diff] [blame] | 1195 | if (Value.SymbolName) |
| 1196 | addRelocationForSymbol(RE, Value.SymbolName); |
| 1197 | else |
| 1198 | addRelocationForSection(RE, Value.SectionID); |
| 1199 | Section.StubOffset = StubOffset + getMaxStubSize(); |
| 1200 | } |
| 1201 | |
| 1202 | if (RelType == ELF::R_390_GOTENT) |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1203 | resolveRelocation(Section, Offset, StubAddress + 8, ELF::R_390_PC32DBL, |
| 1204 | Addend); |
Richard Sandiford | 6fc2ad6 | 2013-05-03 14:15:35 +0000 | [diff] [blame] | 1205 | else |
| 1206 | resolveRelocation(Section, Offset, StubAddress, RelType, Addend); |
Andrew Kaylor | ff9fa05 | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 1207 | } else if (Arch == Triple::x86_64 && RelType == ELF::R_X86_64_PLT32) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1208 | // The way the PLT relocations normally work is that the linker allocates |
| 1209 | // the |
Andrew Kaylor | ff9fa05 | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 1210 | // PLT and this relocation makes a PC-relative call into the PLT. The PLT |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1211 | // entry will then jump to an address provided by the GOT. On first call, |
| 1212 | // the |
| 1213 | // GOT address will point back into PLT code that resolves the symbol. After |
Andrew Kaylor | ff9fa05 | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 1214 | // the first call, the GOT entry points to the actual function. |
| 1215 | // |
| 1216 | // For local functions we're ignoring all of that here and just replacing |
| 1217 | // the PLT32 relocation type with PC32, which will translate the relocation |
| 1218 | // into a PC-relative call directly to the function. For external symbols we |
| 1219 | // can't be sure the function will be within 2^32 bytes of the call site, so |
| 1220 | // we need to create a stub, which calls into the GOT. This case is |
| 1221 | // equivalent to the usual PLT implementation except that we use the stub |
| 1222 | // mechanism in RuntimeDyld (which puts stubs at the end of the section) |
| 1223 | // rather than allocating a PLT section. |
| 1224 | if (Value.SymbolName) { |
| 1225 | // This is a call to an external function. |
| 1226 | // Look for an existing stub. |
| 1227 | SectionEntry &Section = Sections[SectionID]; |
| 1228 | StubMap::const_iterator i = Stubs.find(Value); |
| 1229 | uintptr_t StubAddress; |
| 1230 | if (i != Stubs.end()) { |
| 1231 | StubAddress = uintptr_t(Section.Address) + i->second; |
| 1232 | DEBUG(dbgs() << " Stub function found\n"); |
| 1233 | } else { |
| 1234 | // Create a new stub function (equivalent to a PLT entry). |
| 1235 | DEBUG(dbgs() << " Create a new stub function\n"); |
| 1236 | |
| 1237 | uintptr_t BaseAddress = uintptr_t(Section.Address); |
| 1238 | uintptr_t StubAlignment = getStubAlignment(); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1239 | StubAddress = (BaseAddress + Section.StubOffset + StubAlignment - 1) & |
| 1240 | -StubAlignment; |
Andrew Kaylor | ff9fa05 | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 1241 | unsigned StubOffset = StubAddress - BaseAddress; |
| 1242 | Stubs[Value] = StubOffset; |
| 1243 | createStubFunction((uint8_t *)StubAddress); |
| 1244 | |
| 1245 | // Create a GOT entry for the external function. |
| 1246 | GOTEntries.push_back(Value); |
| 1247 | |
| 1248 | // Make our stub function a relative call to the GOT entry. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1249 | RelocationEntry RE(SectionID, StubOffset + 2, ELF::R_X86_64_GOTPCREL, |
| 1250 | -4); |
Andrew Kaylor | ff9fa05 | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 1251 | addRelocationForSymbol(RE, Value.SymbolName); |
| 1252 | |
| 1253 | // Bump our stub offset counter |
| 1254 | Section.StubOffset = StubOffset + getMaxStubSize(); |
| 1255 | } |
| 1256 | |
| 1257 | // Make the target call a call into the stub table. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1258 | resolveRelocation(Section, Offset, StubAddress, ELF::R_X86_64_PC32, |
| 1259 | Addend); |
Andrew Kaylor | ff9fa05 | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 1260 | } else { |
| 1261 | RelocationEntry RE(SectionID, Offset, ELF::R_X86_64_PC32, Value.Addend, |
| 1262 | Value.Offset); |
| 1263 | addRelocationForSection(RE, Value.SectionID); |
| 1264 | } |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 1265 | } else { |
Andrew Kaylor | ff9fa05 | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 1266 | if (Arch == Triple::x86_64 && RelType == ELF::R_X86_64_GOTPCREL) { |
| 1267 | GOTEntries.push_back(Value); |
| 1268 | } |
| 1269 | RelocationEntry RE(SectionID, Offset, RelType, Value.Addend, Value.Offset); |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 1270 | if (Value.SymbolName) |
| 1271 | addRelocationForSymbol(RE, Value.SymbolName); |
| 1272 | else |
| 1273 | addRelocationForSection(RE, Value.SectionID); |
| 1274 | } |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1275 | return ++RelI; |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
Andrew Kaylor | ff9fa05 | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 1278 | void RuntimeDyldELF::updateGOTEntries(StringRef Name, uint64_t Addr) { |
Andrew Kaylor | 9cffedb | 2013-10-05 01:52:09 +0000 | [diff] [blame] | 1279 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1280 | SmallVectorImpl<std::pair<SID, GOTRelocations>>::iterator it; |
| 1281 | SmallVectorImpl<std::pair<SID, GOTRelocations>>::iterator end = GOTs.end(); |
Andrew Kaylor | 9cffedb | 2013-10-05 01:52:09 +0000 | [diff] [blame] | 1282 | |
| 1283 | for (it = GOTs.begin(); it != end; ++it) { |
| 1284 | GOTRelocations &GOTEntries = it->second; |
| 1285 | for (int i = 0, e = GOTEntries.size(); i != e; ++i) { |
| 1286 | if (GOTEntries[i].SymbolName != 0 && GOTEntries[i].SymbolName == Name) { |
| 1287 | GOTEntries[i].Offset = Addr; |
| 1288 | } |
Andrew Kaylor | ff9fa05 | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 1289 | } |
| 1290 | } |
| 1291 | } |
| 1292 | |
| 1293 | size_t RuntimeDyldELF::getGOTEntrySize() { |
| 1294 | // We don't use the GOT in all of these cases, but it's essentially free |
| 1295 | // to put them all here. |
| 1296 | size_t Result = 0; |
| 1297 | switch (Arch) { |
| 1298 | case Triple::x86_64: |
| 1299 | case Triple::aarch64: |
| 1300 | case Triple::ppc64: |
| 1301 | case Triple::ppc64le: |
| 1302 | case Triple::systemz: |
| 1303 | Result = sizeof(uint64_t); |
| 1304 | break; |
| 1305 | case Triple::x86: |
| 1306 | case Triple::arm: |
| 1307 | case Triple::thumb: |
| 1308 | case Triple::mips: |
| 1309 | case Triple::mipsel: |
| 1310 | Result = sizeof(uint32_t); |
| 1311 | break; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1312 | default: |
| 1313 | llvm_unreachable("Unsupported CPU type!"); |
Andrew Kaylor | ff9fa05 | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 1314 | } |
| 1315 | return Result; |
| 1316 | } |
| 1317 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1318 | uint64_t RuntimeDyldELF::findGOTEntry(uint64_t LoadAddress, uint64_t Offset) { |
Andrew Kaylor | ff9fa05 | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 1319 | |
Andrew Kaylor | 9cffedb | 2013-10-05 01:52:09 +0000 | [diff] [blame] | 1320 | const size_t GOTEntrySize = getGOTEntrySize(); |
Andrew Kaylor | ff9fa05 | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 1321 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1322 | SmallVectorImpl<std::pair<SID, GOTRelocations>>::const_iterator it; |
| 1323 | SmallVectorImpl<std::pair<SID, GOTRelocations>>::const_iterator end = |
| 1324 | GOTs.end(); |
Andrew Kaylor | 9cffedb | 2013-10-05 01:52:09 +0000 | [diff] [blame] | 1325 | |
Andrew Kaylor | ff9fa05 | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 1326 | int GOTIndex = -1; |
Andrew Kaylor | 9cffedb | 2013-10-05 01:52:09 +0000 | [diff] [blame] | 1327 | for (it = GOTs.begin(); it != end; ++it) { |
| 1328 | SID GOTSectionID = it->first; |
| 1329 | const GOTRelocations &GOTEntries = it->second; |
| 1330 | |
| 1331 | // Find the matching entry in our vector. |
| 1332 | uint64_t SymbolOffset = 0; |
| 1333 | for (int i = 0, e = GOTEntries.size(); i != e; ++i) { |
| 1334 | if (GOTEntries[i].SymbolName == 0) { |
| 1335 | if (getSectionLoadAddress(GOTEntries[i].SectionID) == LoadAddress && |
| 1336 | GOTEntries[i].Offset == Offset) { |
| 1337 | GOTIndex = i; |
| 1338 | SymbolOffset = GOTEntries[i].Offset; |
| 1339 | break; |
| 1340 | } |
| 1341 | } else { |
| 1342 | // GOT entries for external symbols use the addend as the address when |
| 1343 | // the external symbol has been resolved. |
| 1344 | if (GOTEntries[i].Offset == LoadAddress) { |
| 1345 | GOTIndex = i; |
| 1346 | // Don't use the Addend here. The relocation handler will use it. |
| 1347 | break; |
| 1348 | } |
Andrew Kaylor | ff9fa05 | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 1349 | } |
| 1350 | } |
Andrew Kaylor | 9cffedb | 2013-10-05 01:52:09 +0000 | [diff] [blame] | 1351 | |
| 1352 | if (GOTIndex != -1) { |
| 1353 | if (GOTEntrySize == sizeof(uint64_t)) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1354 | uint64_t *LocalGOTAddr = (uint64_t *)getSectionAddress(GOTSectionID); |
Andrew Kaylor | 9cffedb | 2013-10-05 01:52:09 +0000 | [diff] [blame] | 1355 | // Fill in this entry with the address of the symbol being referenced. |
| 1356 | LocalGOTAddr[GOTIndex] = LoadAddress + SymbolOffset; |
| 1357 | } else { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1358 | uint32_t *LocalGOTAddr = (uint32_t *)getSectionAddress(GOTSectionID); |
Andrew Kaylor | 9cffedb | 2013-10-05 01:52:09 +0000 | [diff] [blame] | 1359 | // Fill in this entry with the address of the symbol being referenced. |
| 1360 | LocalGOTAddr[GOTIndex] = (uint32_t)(LoadAddress + SymbolOffset); |
| 1361 | } |
| 1362 | |
| 1363 | // Calculate the load address of this entry |
| 1364 | return getSectionLoadAddress(GOTSectionID) + (GOTIndex * GOTEntrySize); |
| 1365 | } |
Andrew Kaylor | ff9fa05 | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 1366 | } |
Andrew Kaylor | 9cffedb | 2013-10-05 01:52:09 +0000 | [diff] [blame] | 1367 | |
Andrew Kaylor | ff9fa05 | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 1368 | assert(GOTIndex != -1 && "Unable to find requested GOT entry."); |
Andrew Kaylor | 9cffedb | 2013-10-05 01:52:09 +0000 | [diff] [blame] | 1369 | return 0; |
Andrew Kaylor | ff9fa05 | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 1370 | } |
| 1371 | |
Andrew Kaylor | 528f6d7 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 1372 | void RuntimeDyldELF::finalizeLoad(ObjSectionToIDMap &SectionMap) { |
| 1373 | // If necessary, allocate the global offset table |
Andrew Kaylor | 9cffedb | 2013-10-05 01:52:09 +0000 | [diff] [blame] | 1374 | if (MemMgr) { |
| 1375 | // Allocate the GOT if necessary |
| 1376 | size_t numGOTEntries = GOTEntries.size(); |
| 1377 | if (numGOTEntries != 0) { |
| 1378 | // Allocate memory for the section |
| 1379 | unsigned SectionID = Sections.size(); |
| 1380 | size_t TotalSize = numGOTEntries * getGOTEntrySize(); |
| 1381 | uint8_t *Addr = MemMgr->allocateDataSection(TotalSize, getGOTEntrySize(), |
| 1382 | SectionID, ".got", false); |
| 1383 | if (!Addr) |
| 1384 | report_fatal_error("Unable to allocate memory for GOT!"); |
| 1385 | |
| 1386 | GOTs.push_back(std::make_pair(SectionID, GOTEntries)); |
| 1387 | Sections.push_back(SectionEntry(".got", Addr, TotalSize, 0)); |
| 1388 | // For now, initialize all GOT entries to zero. We'll fill them in as |
| 1389 | // needed when GOT-based relocations are applied. |
| 1390 | memset(Addr, 0, TotalSize); |
| 1391 | } |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1392 | } else { |
Andrew Kaylor | 9cffedb | 2013-10-05 01:52:09 +0000 | [diff] [blame] | 1393 | report_fatal_error("Unable to allocate memory for GOT!"); |
Andrew Kaylor | ff9fa05 | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 1394 | } |
Andrew Kaylor | 528f6d7 | 2013-10-11 21:25:48 +0000 | [diff] [blame] | 1395 | |
| 1396 | // Look for and record the EH frame section. |
| 1397 | ObjSectionToIDMap::iterator i, e; |
| 1398 | for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) { |
| 1399 | const SectionRef &Section = i->first; |
| 1400 | StringRef Name; |
| 1401 | Section.getName(Name); |
| 1402 | if (Name == ".eh_frame") { |
| 1403 | UnregisteredEHFrameSections.push_back(i->second); |
| 1404 | break; |
| 1405 | } |
| 1406 | } |
Andrew Kaylor | ff9fa05 | 2013-08-19 23:27:43 +0000 | [diff] [blame] | 1407 | } |
| 1408 | |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 1409 | bool RuntimeDyldELF::isCompatibleFormat(const ObjectBuffer *Buffer) const { |
| 1410 | if (Buffer->getBufferSize() < strlen(ELF::ElfMagic)) |
| 1411 | return false; |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1412 | return (memcmp(Buffer->getBufferStart(), ELF::ElfMagic, |
| 1413 | strlen(ELF::ElfMagic))) == 0; |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 1414 | } |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1415 | |
| 1416 | bool RuntimeDyldELF::isCompatibleFile(const object::ObjectFile *Obj) const { |
| 1417 | return Obj->isELF(); |
| 1418 | } |
| 1419 | |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 1420 | } // namespace llvm |