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