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