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