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