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 | |
Rafael Espindola | a2e40fb | 2013-05-05 20:43:10 +0000 | [diff] [blame] | 154 | StringRef RuntimeDyldELF::getEHFrameSection() { |
| 155 | for (int i = 0, e = Sections.size(); i != e; ++i) { |
| 156 | if (Sections[i].Name == ".eh_frame") |
| 157 | return StringRef((const char*)Sections[i].Address, Sections[i].Size); |
| 158 | } |
| 159 | return StringRef(); |
| 160 | } |
| 161 | |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 162 | ObjectImage *RuntimeDyldELF::createObjectImage(ObjectBuffer *Buffer) { |
| 163 | if (Buffer->getBufferSize() < ELF::EI_NIDENT) |
| 164 | llvm_unreachable("Unexpected ELF object size"); |
| 165 | std::pair<unsigned char, unsigned char> Ident = std::make_pair( |
| 166 | (uint8_t)Buffer->getBufferStart()[ELF::EI_CLASS], |
| 167 | (uint8_t)Buffer->getBufferStart()[ELF::EI_DATA]); |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 168 | error_code ec; |
| 169 | |
| 170 | if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB) { |
Michael J. Spencer | ac97f5c | 2013-01-15 07:44:25 +0000 | [diff] [blame] | 171 | DyldELFObject<ELFType<support::little, 4, false> > *Obj = |
| 172 | new DyldELFObject<ELFType<support::little, 4, false> >( |
| 173 | Buffer->getMemBuffer(), ec); |
| 174 | return new ELFObjectImage<ELFType<support::little, 4, false> >(Buffer, Obj); |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 175 | } |
| 176 | else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB) { |
Michael J. Spencer | ac97f5c | 2013-01-15 07:44:25 +0000 | [diff] [blame] | 177 | DyldELFObject<ELFType<support::big, 4, false> > *Obj = |
| 178 | new DyldELFObject<ELFType<support::big, 4, false> >( |
| 179 | Buffer->getMemBuffer(), ec); |
| 180 | return new ELFObjectImage<ELFType<support::big, 4, false> >(Buffer, Obj); |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 181 | } |
| 182 | else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB) { |
Michael J. Spencer | ac97f5c | 2013-01-15 07:44:25 +0000 | [diff] [blame] | 183 | DyldELFObject<ELFType<support::big, 8, true> > *Obj = |
| 184 | new DyldELFObject<ELFType<support::big, 8, true> >( |
| 185 | Buffer->getMemBuffer(), ec); |
| 186 | return new ELFObjectImage<ELFType<support::big, 8, true> >(Buffer, Obj); |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 187 | } |
| 188 | else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) { |
Michael J. Spencer | ac97f5c | 2013-01-15 07:44:25 +0000 | [diff] [blame] | 189 | DyldELFObject<ELFType<support::little, 8, true> > *Obj = |
| 190 | new DyldELFObject<ELFType<support::little, 8, true> >( |
| 191 | Buffer->getMemBuffer(), ec); |
| 192 | return new ELFObjectImage<ELFType<support::little, 8, true> >(Buffer, Obj); |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 193 | } |
| 194 | else |
| 195 | llvm_unreachable("Unexpected ELF format"); |
| 196 | } |
| 197 | |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 198 | RuntimeDyldELF::~RuntimeDyldELF() { |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 199 | } |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 200 | |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 201 | void RuntimeDyldELF::resolveX86_64Relocation(const SectionEntry &Section, |
| 202 | uint64_t Offset, |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 203 | uint64_t Value, |
| 204 | uint32_t Type, |
| 205 | int64_t Addend) { |
| 206 | switch (Type) { |
| 207 | default: |
| 208 | llvm_unreachable("Relocation type not implemented yet!"); |
| 209 | break; |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 210 | case ELF::R_X86_64_64: { |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 211 | uint64_t *Target = reinterpret_cast<uint64_t*>(Section.Address + Offset); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 212 | *Target = Value + Addend; |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 213 | DEBUG(dbgs() << "Writing " << format("%p", (Value + Addend)) |
| 214 | << " at " << format("%p\n",Target)); |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 215 | break; |
| 216 | } |
| 217 | case ELF::R_X86_64_32: |
| 218 | case ELF::R_X86_64_32S: { |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 219 | Value += Addend; |
Andrew Kaylor | d83a547 | 2012-07-27 20:30:12 +0000 | [diff] [blame] | 220 | assert((Type == ELF::R_X86_64_32 && (Value <= UINT32_MAX)) || |
Michael J. Spencer | 4d9c539 | 2013-01-04 20:36:28 +0000 | [diff] [blame] | 221 | (Type == ELF::R_X86_64_32S && |
Andrew Kaylor | d83a547 | 2012-07-27 20:30:12 +0000 | [diff] [blame] | 222 | ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN))); |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 223 | uint32_t TruncatedAddr = (Value & 0xFFFFFFFF); |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 224 | uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset); |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 225 | *Target = TruncatedAddr; |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 226 | DEBUG(dbgs() << "Writing " << format("%p", TruncatedAddr) |
| 227 | << " at " << format("%p\n",Target)); |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 228 | break; |
| 229 | } |
| 230 | case ELF::R_X86_64_PC32: { |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 231 | // Get the placeholder value from the generated object since |
| 232 | // a previous relocation attempt may have overwritten the loaded version |
| 233 | uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress |
| 234 | + Offset); |
| 235 | uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset); |
| 236 | uint64_t FinalAddress = Section.LoadAddress + Offset; |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 237 | int64_t RealOffset = *Placeholder + Value + Addend - FinalAddress; |
Andrew Kaylor | d83a547 | 2012-07-27 20:30:12 +0000 | [diff] [blame] | 238 | assert(RealOffset <= INT32_MAX && RealOffset >= INT32_MIN); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 239 | int32_t TruncOffset = (RealOffset & 0xFFFFFFFF); |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 240 | *Target = TruncOffset; |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 241 | break; |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 246 | void RuntimeDyldELF::resolveX86Relocation(const SectionEntry &Section, |
| 247 | uint64_t Offset, |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 248 | uint32_t Value, |
| 249 | uint32_t Type, |
| 250 | int32_t Addend) { |
| 251 | switch (Type) { |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 252 | case ELF::R_386_32: { |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 253 | // Get the placeholder value from the generated object since |
| 254 | // a previous relocation attempt may have overwritten the loaded version |
| 255 | uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress |
| 256 | + Offset); |
| 257 | uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset); |
| 258 | *Target = *Placeholder + Value + Addend; |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 259 | break; |
| 260 | } |
| 261 | case ELF::R_386_PC32: { |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 262 | // Get the placeholder value from the generated object since |
| 263 | // a previous relocation attempt may have overwritten the loaded version |
| 264 | uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress |
| 265 | + Offset); |
| 266 | uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset); |
| 267 | uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 268 | uint32_t RealOffset = *Placeholder + Value + Addend - FinalAddress; |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 269 | *Target = RealOffset; |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 270 | break; |
| 271 | } |
| 272 | default: |
| 273 | // There are other relocation types, but it appears these are the |
Andrew Kaylor | e2e73bd | 2012-07-27 18:39:47 +0000 | [diff] [blame] | 274 | // only ones currently used by the LLVM ELF object writer |
Craig Topper | 8581438 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 275 | llvm_unreachable("Relocation type not implemented yet!"); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 276 | break; |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | |
Tim Northover | 85829bb | 2013-05-04 20:13:59 +0000 | [diff] [blame] | 280 | void RuntimeDyldELF::resolveAArch64Relocation(const SectionEntry &Section, |
| 281 | uint64_t Offset, |
| 282 | uint64_t Value, |
| 283 | uint32_t Type, |
| 284 | int64_t Addend) { |
| 285 | uint32_t *TargetPtr = reinterpret_cast<uint32_t*>(Section.Address + Offset); |
| 286 | uint64_t FinalAddress = Section.LoadAddress + Offset; |
| 287 | |
| 288 | DEBUG(dbgs() << "resolveAArch64Relocation, LocalAddress: 0x" |
| 289 | << format("%llx", Section.Address + Offset) |
| 290 | << " FinalAddress: 0x" << format("%llx",FinalAddress) |
| 291 | << " Value: 0x" << format("%llx",Value) |
| 292 | << " Type: 0x" << format("%x",Type) |
| 293 | << " Addend: 0x" << format("%llx",Addend) |
| 294 | << "\n"); |
| 295 | |
| 296 | switch (Type) { |
| 297 | default: |
| 298 | llvm_unreachable("Relocation type not implemented yet!"); |
| 299 | break; |
Tim Northover | d52eaae | 2013-05-04 20:14:14 +0000 | [diff] [blame] | 300 | case ELF::R_AARCH64_ABS64: { |
| 301 | uint64_t *TargetPtr = reinterpret_cast<uint64_t*>(Section.Address + Offset); |
| 302 | *TargetPtr = Value + Addend; |
| 303 | break; |
| 304 | } |
Tim Northover | 675b9e9 | 2013-05-19 15:39:03 +0000 | [diff] [blame] | 305 | case ELF::R_AARCH64_PREL32: { |
Tim Northover | 85829bb | 2013-05-04 20:13:59 +0000 | [diff] [blame] | 306 | uint64_t Result = Value + Addend - FinalAddress; |
Michael J. Spencer | 081a194 | 2013-08-08 22:27:13 +0000 | [diff] [blame^] | 307 | assert(static_cast<int64_t>(Result) >= INT32_MIN && |
Tim Northover | 85829bb | 2013-05-04 20:13:59 +0000 | [diff] [blame] | 308 | static_cast<int64_t>(Result) <= UINT32_MAX); |
| 309 | *TargetPtr = static_cast<uint32_t>(Result & 0xffffffffU); |
| 310 | break; |
| 311 | } |
Tim Northover | 4a9b6b7 | 2013-05-04 20:14:09 +0000 | [diff] [blame] | 312 | case ELF::R_AARCH64_CALL26: // fallthrough |
| 313 | case ELF::R_AARCH64_JUMP26: { |
| 314 | // Operation: S+A-P. Set Call or B immediate value to bits fff_fffc of the |
| 315 | // calculation. |
| 316 | uint64_t BranchImm = Value + Addend - FinalAddress; |
| 317 | |
| 318 | // "Check that -2^27 <= result < 2^27". |
Michael J. Spencer | 081a194 | 2013-08-08 22:27:13 +0000 | [diff] [blame^] | 319 | assert(-(1LL << 27) <= static_cast<int64_t>(BranchImm) && |
Tim Northover | 4a9b6b7 | 2013-05-04 20:14:09 +0000 | [diff] [blame] | 320 | static_cast<int64_t>(BranchImm) < (1LL << 27)); |
Tim Northover | 675b9e9 | 2013-05-19 15:39:03 +0000 | [diff] [blame] | 321 | |
| 322 | // AArch64 code is emitted with .rela relocations. The data already in any |
| 323 | // bits affected by the relocation on entry is garbage. |
| 324 | *TargetPtr &= 0xfc000000U; |
Tim Northover | 4a9b6b7 | 2013-05-04 20:14:09 +0000 | [diff] [blame] | 325 | // Immediate goes in bits 25:0 of B and BL. |
| 326 | *TargetPtr |= static_cast<uint32_t>(BranchImm & 0xffffffcU) >> 2; |
| 327 | break; |
| 328 | } |
Tim Northover | 654c2d6 | 2013-05-04 20:14:04 +0000 | [diff] [blame] | 329 | case ELF::R_AARCH64_MOVW_UABS_G3: { |
| 330 | uint64_t Result = Value + Addend; |
Tim Northover | 675b9e9 | 2013-05-19 15:39:03 +0000 | [diff] [blame] | 331 | |
| 332 | // AArch64 code is emitted with .rela relocations. The data already in any |
| 333 | // bits affected by the relocation on entry is garbage. |
Tim Northover | 107b2f2 | 2013-07-25 12:42:52 +0000 | [diff] [blame] | 334 | *TargetPtr &= 0xffe0001fU; |
Tim Northover | 654c2d6 | 2013-05-04 20:14:04 +0000 | [diff] [blame] | 335 | // Immediate goes in bits 20:5 of MOVZ/MOVK instruction |
| 336 | *TargetPtr |= Result >> (48 - 5); |
Tim Northover | 6711fc2 | 2013-07-01 19:23:10 +0000 | [diff] [blame] | 337 | // Shift must be "lsl #48", in bits 22:21 |
| 338 | assert((*TargetPtr >> 21 & 0x3) == 3 && "invalid shift for relocation"); |
Tim Northover | 654c2d6 | 2013-05-04 20:14:04 +0000 | [diff] [blame] | 339 | break; |
| 340 | } |
| 341 | case ELF::R_AARCH64_MOVW_UABS_G2_NC: { |
| 342 | uint64_t Result = Value + Addend; |
Tim Northover | 675b9e9 | 2013-05-19 15:39:03 +0000 | [diff] [blame] | 343 | |
Tim Northover | 675b9e9 | 2013-05-19 15:39:03 +0000 | [diff] [blame] | 344 | // AArch64 code is emitted with .rela relocations. The data already in any |
| 345 | // bits affected by the relocation on entry is garbage. |
Tim Northover | 107b2f2 | 2013-07-25 12:42:52 +0000 | [diff] [blame] | 346 | *TargetPtr &= 0xffe0001fU; |
Tim Northover | 654c2d6 | 2013-05-04 20:14:04 +0000 | [diff] [blame] | 347 | // Immediate goes in bits 20:5 of MOVZ/MOVK instruction |
| 348 | *TargetPtr |= ((Result & 0xffff00000000ULL) >> (32 - 5)); |
Tim Northover | 6711fc2 | 2013-07-01 19:23:10 +0000 | [diff] [blame] | 349 | // Shift must be "lsl #32", in bits 22:21 |
| 350 | assert((*TargetPtr >> 21 & 0x3) == 2 && "invalid shift for relocation"); |
Tim Northover | 654c2d6 | 2013-05-04 20:14:04 +0000 | [diff] [blame] | 351 | break; |
| 352 | } |
| 353 | case ELF::R_AARCH64_MOVW_UABS_G1_NC: { |
| 354 | uint64_t Result = Value + Addend; |
Tim Northover | 675b9e9 | 2013-05-19 15:39:03 +0000 | [diff] [blame] | 355 | |
| 356 | // AArch64 code is emitted with .rela relocations. The data already in any |
| 357 | // bits affected by the relocation on entry is garbage. |
Tim Northover | 107b2f2 | 2013-07-25 12:42:52 +0000 | [diff] [blame] | 358 | *TargetPtr &= 0xffe0001fU; |
Tim Northover | 654c2d6 | 2013-05-04 20:14:04 +0000 | [diff] [blame] | 359 | // Immediate goes in bits 20:5 of MOVZ/MOVK instruction |
| 360 | *TargetPtr |= ((Result & 0xffff0000U) >> (16 - 5)); |
Tim Northover | 6711fc2 | 2013-07-01 19:23:10 +0000 | [diff] [blame] | 361 | // Shift must be "lsl #16", in bits 22:2 |
| 362 | assert((*TargetPtr >> 21 & 0x3) == 1 && "invalid shift for relocation"); |
Tim Northover | 654c2d6 | 2013-05-04 20:14:04 +0000 | [diff] [blame] | 363 | break; |
| 364 | } |
| 365 | case ELF::R_AARCH64_MOVW_UABS_G0_NC: { |
| 366 | uint64_t Result = Value + Addend; |
Tim Northover | 675b9e9 | 2013-05-19 15:39:03 +0000 | [diff] [blame] | 367 | |
| 368 | // AArch64 code is emitted with .rela relocations. The data already in any |
| 369 | // bits affected by the relocation on entry is garbage. |
Tim Northover | 107b2f2 | 2013-07-25 12:42:52 +0000 | [diff] [blame] | 370 | *TargetPtr &= 0xffe0001fU; |
Tim Northover | 654c2d6 | 2013-05-04 20:14:04 +0000 | [diff] [blame] | 371 | // Immediate goes in bits 20:5 of MOVZ/MOVK instruction |
| 372 | *TargetPtr |= ((Result & 0xffffU) << 5); |
Tim Northover | 6711fc2 | 2013-07-01 19:23:10 +0000 | [diff] [blame] | 373 | // Shift must be "lsl #0", in bits 22:21. |
| 374 | assert((*TargetPtr >> 21 & 0x3) == 0 && "invalid shift for relocation"); |
Tim Northover | 654c2d6 | 2013-05-04 20:14:04 +0000 | [diff] [blame] | 375 | break; |
| 376 | } |
Tim Northover | 85829bb | 2013-05-04 20:13:59 +0000 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 380 | void RuntimeDyldELF::resolveARMRelocation(const SectionEntry &Section, |
| 381 | uint64_t Offset, |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 382 | uint32_t Value, |
| 383 | uint32_t Type, |
| 384 | int32_t Addend) { |
| 385 | // TODO: Add Thumb relocations. |
Tim Northover | e274b47 | 2013-05-28 19:48:19 +0000 | [diff] [blame] | 386 | uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress + |
| 387 | Offset); |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 388 | uint32_t* TargetPtr = (uint32_t*)(Section.Address + Offset); |
| 389 | uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 390 | Value += Addend; |
| 391 | |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 392 | DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: " |
| 393 | << Section.Address + Offset |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 394 | << " FinalAddress: " << format("%p",FinalAddress) |
| 395 | << " Value: " << format("%x",Value) |
| 396 | << " Type: " << format("%x",Type) |
| 397 | << " Addend: " << format("%x",Addend) |
| 398 | << "\n"); |
| 399 | |
| 400 | switch(Type) { |
| 401 | default: |
| 402 | llvm_unreachable("Not implemented relocation type!"); |
| 403 | |
Michael J. Spencer | 4d9c539 | 2013-01-04 20:36:28 +0000 | [diff] [blame] | 404 | // Write a 32bit value to relocation address, taking into account the |
Tim Northover | 565ebde | 2012-10-03 16:29:42 +0000 | [diff] [blame] | 405 | // implicit addend encoded in the target. |
Tim Northover | e274b47 | 2013-05-28 19:48:19 +0000 | [diff] [blame] | 406 | case ELF::R_ARM_TARGET1: |
| 407 | case ELF::R_ARM_ABS32: |
| 408 | *TargetPtr = *Placeholder + Value; |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 409 | break; |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 410 | // Write first 16 bit of 32 bit value to the mov instruction. |
| 411 | // Last 4 bit should be shifted. |
Tim Northover | e274b47 | 2013-05-28 19:48:19 +0000 | [diff] [blame] | 412 | case ELF::R_ARM_MOVW_ABS_NC: |
Tim Northover | 565ebde | 2012-10-03 16:29:42 +0000 | [diff] [blame] | 413 | // We are not expecting any other addend in the relocation address. |
Michael J. Spencer | 4d9c539 | 2013-01-04 20:36:28 +0000 | [diff] [blame] | 414 | // Using 0x000F0FFF because MOVW has its 16 bit immediate split into 2 |
Tim Northover | 565ebde | 2012-10-03 16:29:42 +0000 | [diff] [blame] | 415 | // non-contiguous fields. |
Tim Northover | e274b47 | 2013-05-28 19:48:19 +0000 | [diff] [blame] | 416 | assert((*Placeholder & 0x000F0FFF) == 0); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 417 | Value = Value & 0xFFFF; |
Tim Northover | e274b47 | 2013-05-28 19:48:19 +0000 | [diff] [blame] | 418 | *TargetPtr = *Placeholder | (Value & 0xFFF); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 419 | *TargetPtr |= ((Value >> 12) & 0xF) << 16; |
| 420 | break; |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 421 | // Write last 16 bit of 32 bit value to the mov instruction. |
| 422 | // Last 4 bit should be shifted. |
Tim Northover | e274b47 | 2013-05-28 19:48:19 +0000 | [diff] [blame] | 423 | case ELF::R_ARM_MOVT_ABS: |
Tim Northover | 565ebde | 2012-10-03 16:29:42 +0000 | [diff] [blame] | 424 | // We are not expecting any other addend in the relocation address. |
| 425 | // Use 0x000F0FFF for the same reason as R_ARM_MOVW_ABS_NC. |
Tim Northover | e274b47 | 2013-05-28 19:48:19 +0000 | [diff] [blame] | 426 | assert((*Placeholder & 0x000F0FFF) == 0); |
| 427 | |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 428 | Value = (Value >> 16) & 0xFFFF; |
Tim Northover | e274b47 | 2013-05-28 19:48:19 +0000 | [diff] [blame] | 429 | *TargetPtr = *Placeholder | (Value & 0xFFF); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 430 | *TargetPtr |= ((Value >> 12) & 0xF) << 16; |
| 431 | break; |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 432 | // Write 24 bit relative value to the branch instruction. |
| 433 | case ELF::R_ARM_PC24 : // Fall through. |
| 434 | case ELF::R_ARM_CALL : // Fall through. |
Tim Northover | e274b47 | 2013-05-28 19:48:19 +0000 | [diff] [blame] | 435 | case ELF::R_ARM_JUMP24: { |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 436 | int32_t RelValue = static_cast<int32_t>(Value - FinalAddress - 8); |
| 437 | RelValue = (RelValue & 0x03FFFFFC) >> 2; |
Tim Northover | e274b47 | 2013-05-28 19:48:19 +0000 | [diff] [blame] | 438 | assert((*TargetPtr & 0xFFFFFF) == 0xFFFFFE); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 439 | *TargetPtr &= 0xFF000000; |
| 440 | *TargetPtr |= RelValue; |
| 441 | break; |
| 442 | } |
Tim Northover | e274b47 | 2013-05-28 19:48:19 +0000 | [diff] [blame] | 443 | case ELF::R_ARM_PRIVATE_0: |
| 444 | // This relocation is reserved by the ARM ELF ABI for internal use. We |
| 445 | // appropriate it here to act as an R_ARM_ABS32 without any addend for use |
| 446 | // in the stubs created during JIT (which can't put an addend into the |
| 447 | // original object file). |
| 448 | *TargetPtr = Value; |
| 449 | break; |
| 450 | } |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 453 | void RuntimeDyldELF::resolveMIPSRelocation(const SectionEntry &Section, |
| 454 | uint64_t Offset, |
Akira Hatanaka | b862f09 | 2012-08-20 17:53:24 +0000 | [diff] [blame] | 455 | uint32_t Value, |
| 456 | uint32_t Type, |
| 457 | int32_t Addend) { |
Akira Hatanaka | 3af1c9d | 2013-07-24 01:58:40 +0000 | [diff] [blame] | 458 | uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress + |
| 459 | Offset); |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 460 | uint32_t* TargetPtr = (uint32_t*)(Section.Address + Offset); |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 461 | Value += Addend; |
| 462 | |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 463 | DEBUG(dbgs() << "resolveMipselocation, LocalAddress: " |
| 464 | << Section.Address + Offset |
| 465 | << " FinalAddress: " |
| 466 | << format("%p",Section.LoadAddress + Offset) |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 467 | << " Value: " << format("%x",Value) |
| 468 | << " Type: " << format("%x",Type) |
| 469 | << " Addend: " << format("%x",Addend) |
| 470 | << "\n"); |
| 471 | |
| 472 | switch(Type) { |
| 473 | default: |
| 474 | llvm_unreachable("Not implemented relocation type!"); |
| 475 | break; |
| 476 | case ELF::R_MIPS_32: |
Akira Hatanaka | 3af1c9d | 2013-07-24 01:58:40 +0000 | [diff] [blame] | 477 | *TargetPtr = Value + (*Placeholder); |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 478 | break; |
| 479 | case ELF::R_MIPS_26: |
Akira Hatanaka | 3af1c9d | 2013-07-24 01:58:40 +0000 | [diff] [blame] | 480 | *TargetPtr = ((*Placeholder) & 0xfc000000) | (( Value & 0x0fffffff) >> 2); |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 481 | break; |
| 482 | case ELF::R_MIPS_HI16: |
| 483 | // 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] | 484 | Value += ((*Placeholder) & 0x0000ffff) << 16; |
| 485 | *TargetPtr = ((*Placeholder) & 0xffff0000) | |
| 486 | (((Value + 0x8000) >> 16) & 0xffff); |
| 487 | break; |
| 488 | case ELF::R_MIPS_LO16: |
| 489 | Value += ((*Placeholder) & 0x0000ffff); |
| 490 | *TargetPtr = ((*Placeholder) & 0xffff0000) | (Value & 0xffff); |
| 491 | break; |
| 492 | case ELF::R_MIPS_UNUSED1: |
| 493 | // Similar to ELF::R_ARM_PRIVATE_0, R_MIPS_UNUSED1 and R_MIPS_UNUSED2 |
| 494 | // are used for internal JIT purpose. These relocations are similar to |
| 495 | // R_MIPS_HI16 and R_MIPS_LO16, but they do not take any addend into |
| 496 | // account. |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 497 | *TargetPtr = ((*TargetPtr) & 0xffff0000) | |
| 498 | (((Value + 0x8000) >> 16) & 0xffff); |
| 499 | break; |
Akira Hatanaka | 3af1c9d | 2013-07-24 01:58:40 +0000 | [diff] [blame] | 500 | case ELF::R_MIPS_UNUSED2: |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 501 | *TargetPtr = ((*TargetPtr) & 0xffff0000) | (Value & 0xffff); |
| 502 | break; |
| 503 | } |
| 504 | } |
| 505 | |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 506 | // Return the .TOC. section address to R_PPC64_TOC relocations. |
| 507 | uint64_t RuntimeDyldELF::findPPC64TOC() const { |
| 508 | // The TOC consists of sections .got, .toc, .tocbss, .plt in that |
| 509 | // order. The TOC starts where the first of these sections starts. |
| 510 | SectionList::const_iterator it = Sections.begin(); |
| 511 | SectionList::const_iterator ite = Sections.end(); |
| 512 | for (; it != ite; ++it) { |
| 513 | if (it->Name == ".got" || |
| 514 | it->Name == ".toc" || |
| 515 | it->Name == ".tocbss" || |
| 516 | it->Name == ".plt") |
| 517 | break; |
| 518 | } |
| 519 | if (it == ite) { |
| 520 | // This may happen for |
| 521 | // * references to TOC base base (sym@toc, .odp relocation) without |
| 522 | // a .toc directive. |
| 523 | // In this case just use the first section (which is usually |
| 524 | // the .odp) since the code won't reference the .toc base |
| 525 | // directly. |
| 526 | it = Sections.begin(); |
| 527 | } |
| 528 | assert (it != ite); |
| 529 | // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000 |
| 530 | // thus permitting a full 64 Kbytes segment. |
| 531 | return it->LoadAddress + 0x8000; |
| 532 | } |
| 533 | |
| 534 | // Returns the sections and offset associated with the ODP entry referenced |
| 535 | // by Symbol. |
| 536 | void RuntimeDyldELF::findOPDEntrySection(ObjectImage &Obj, |
| 537 | ObjSectionToIDMap &LocalSections, |
| 538 | RelocationValueRef &Rel) { |
| 539 | // Get the ELF symbol value (st_value) to compare with Relocation offset in |
| 540 | // .opd entries |
| 541 | |
| 542 | error_code err; |
| 543 | for (section_iterator si = Obj.begin_sections(), |
| 544 | se = Obj.end_sections(); si != se; si.increment(err)) { |
Rafael Espindola | 15e5c46 | 2013-06-03 19:37:34 +0000 | [diff] [blame] | 545 | section_iterator RelSecI = si->getRelocatedSection(); |
| 546 | if (RelSecI == Obj.end_sections()) |
| 547 | continue; |
| 548 | |
| 549 | StringRef RelSectionName; |
| 550 | check(RelSecI->getName(RelSectionName)); |
| 551 | if (RelSectionName != ".opd") |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 552 | continue; |
| 553 | |
| 554 | for (relocation_iterator i = si->begin_relocations(), |
| 555 | e = si->end_relocations(); i != e;) { |
| 556 | check(err); |
| 557 | |
| 558 | // The R_PPC64_ADDR64 relocation indicates the first field |
| 559 | // of a .opd entry |
| 560 | uint64_t TypeFunc; |
| 561 | check(i->getType(TypeFunc)); |
| 562 | if (TypeFunc != ELF::R_PPC64_ADDR64) { |
| 563 | i.increment(err); |
| 564 | continue; |
| 565 | } |
| 566 | |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 567 | uint64_t TargetSymbolOffset; |
Rafael Espindola | 6c1202c | 2013-06-05 01:33:53 +0000 | [diff] [blame] | 568 | symbol_iterator TargetSymbol = i->getSymbol(); |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 569 | check(i->getOffset(TargetSymbolOffset)); |
Rafael Espindola | 167957f | 2013-05-09 03:39:05 +0000 | [diff] [blame] | 570 | int64_t Addend; |
| 571 | check(getELFRelocationAddend(*i, Addend)); |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 572 | |
| 573 | i = i.increment(err); |
| 574 | if (i == e) |
| 575 | break; |
| 576 | check(err); |
| 577 | |
| 578 | // Just check if following relocation is a R_PPC64_TOC |
| 579 | uint64_t TypeTOC; |
| 580 | check(i->getType(TypeTOC)); |
| 581 | if (TypeTOC != ELF::R_PPC64_TOC) |
| 582 | continue; |
| 583 | |
| 584 | // Finally compares the Symbol value and the target symbol offset |
| 585 | // to check if this .opd entry refers to the symbol the relocation |
| 586 | // points to. |
| 587 | if (Rel.Addend != (intptr_t)TargetSymbolOffset) |
| 588 | continue; |
| 589 | |
| 590 | section_iterator tsi(Obj.end_sections()); |
Rafael Espindola | 6c1202c | 2013-06-05 01:33:53 +0000 | [diff] [blame] | 591 | check(TargetSymbol->getSection(tsi)); |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 592 | Rel.SectionID = findOrEmitSection(Obj, (*tsi), true, LocalSections); |
Rafael Espindola | 167957f | 2013-05-09 03:39:05 +0000 | [diff] [blame] | 593 | Rel.Addend = (intptr_t)Addend; |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 594 | return; |
| 595 | } |
| 596 | } |
| 597 | llvm_unreachable("Attempting to get address of ODP entry!"); |
| 598 | } |
| 599 | |
| 600 | // Relocation masks following the #lo(value), #hi(value), #higher(value), |
| 601 | // and #highest(value) macros defined in section 4.5.1. Relocation Types |
| 602 | // in PPC-elf64abi document. |
| 603 | // |
| 604 | static inline |
| 605 | uint16_t applyPPClo (uint64_t value) |
| 606 | { |
| 607 | return value & 0xffff; |
| 608 | } |
| 609 | |
| 610 | static inline |
| 611 | uint16_t applyPPChi (uint64_t value) |
| 612 | { |
| 613 | return (value >> 16) & 0xffff; |
| 614 | } |
| 615 | |
| 616 | static inline |
| 617 | uint16_t applyPPChigher (uint64_t value) |
| 618 | { |
| 619 | return (value >> 32) & 0xffff; |
| 620 | } |
| 621 | |
| 622 | static inline |
| 623 | uint16_t applyPPChighest (uint64_t value) |
| 624 | { |
| 625 | return (value >> 48) & 0xffff; |
| 626 | } |
| 627 | |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 628 | void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntry &Section, |
| 629 | uint64_t Offset, |
| 630 | uint64_t Value, |
| 631 | uint32_t Type, |
| 632 | int64_t Addend) { |
| 633 | uint8_t* LocalAddress = Section.Address + Offset; |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 634 | switch (Type) { |
| 635 | default: |
| 636 | llvm_unreachable("Relocation type not implemented yet!"); |
| 637 | break; |
| 638 | case ELF::R_PPC64_ADDR16_LO : |
| 639 | writeInt16BE(LocalAddress, applyPPClo (Value + Addend)); |
| 640 | break; |
| 641 | case ELF::R_PPC64_ADDR16_HI : |
| 642 | writeInt16BE(LocalAddress, applyPPChi (Value + Addend)); |
| 643 | break; |
| 644 | case ELF::R_PPC64_ADDR16_HIGHER : |
| 645 | writeInt16BE(LocalAddress, applyPPChigher (Value + Addend)); |
| 646 | break; |
| 647 | case ELF::R_PPC64_ADDR16_HIGHEST : |
| 648 | writeInt16BE(LocalAddress, applyPPChighest (Value + Addend)); |
| 649 | break; |
| 650 | case ELF::R_PPC64_ADDR14 : { |
| 651 | assert(((Value + Addend) & 3) == 0); |
| 652 | // Preserve the AA/LK bits in the branch instruction |
| 653 | uint8_t aalk = *(LocalAddress+3); |
| 654 | writeInt16BE(LocalAddress + 2, (aalk & 3) | ((Value + Addend) & 0xfffc)); |
| 655 | } break; |
Adhemerval Zanella | 7b44988 | 2013-01-04 19:08:13 +0000 | [diff] [blame] | 656 | case ELF::R_PPC64_ADDR32 : { |
| 657 | int32_t Result = static_cast<int32_t>(Value + Addend); |
| 658 | if (SignExtend32<32>(Result) != Result) |
Adhemerval Zanella | a1db5de | 2013-01-09 17:08:15 +0000 | [diff] [blame] | 659 | llvm_unreachable("Relocation R_PPC64_ADDR32 overflow"); |
Adhemerval Zanella | 7b44988 | 2013-01-04 19:08:13 +0000 | [diff] [blame] | 660 | writeInt32BE(LocalAddress, Result); |
| 661 | } break; |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 662 | case ELF::R_PPC64_REL24 : { |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 663 | uint64_t FinalAddress = (Section.LoadAddress + Offset); |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 664 | int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend); |
| 665 | if (SignExtend32<24>(delta) != delta) |
| 666 | llvm_unreachable("Relocation R_PPC64_REL24 overflow"); |
| 667 | // Generates a 'bl <address>' instruction |
| 668 | writeInt32BE(LocalAddress, 0x48000001 | (delta & 0x03FFFFFC)); |
| 669 | } break; |
Adhemerval Zanella | a1db5de | 2013-01-09 17:08:15 +0000 | [diff] [blame] | 670 | case ELF::R_PPC64_REL32 : { |
| 671 | uint64_t FinalAddress = (Section.LoadAddress + Offset); |
| 672 | int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend); |
| 673 | if (SignExtend32<32>(delta) != delta) |
| 674 | llvm_unreachable("Relocation R_PPC64_REL32 overflow"); |
| 675 | writeInt32BE(LocalAddress, delta); |
| 676 | } break; |
Adhemerval Zanella | f51d7e7 | 2013-05-06 17:21:23 +0000 | [diff] [blame] | 677 | case ELF::R_PPC64_REL64: { |
| 678 | uint64_t FinalAddress = (Section.LoadAddress + Offset); |
| 679 | uint64_t Delta = Value - FinalAddress + Addend; |
| 680 | writeInt64BE(LocalAddress, Delta); |
| 681 | } break; |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 682 | case ELF::R_PPC64_ADDR64 : |
| 683 | writeInt64BE(LocalAddress, Value + Addend); |
| 684 | break; |
| 685 | case ELF::R_PPC64_TOC : |
| 686 | writeInt64BE(LocalAddress, findPPC64TOC()); |
| 687 | break; |
| 688 | case ELF::R_PPC64_TOC16 : { |
| 689 | uint64_t TOCStart = findPPC64TOC(); |
| 690 | Value = applyPPClo((Value + Addend) - TOCStart); |
| 691 | writeInt16BE(LocalAddress, applyPPClo(Value)); |
| 692 | } break; |
| 693 | case ELF::R_PPC64_TOC16_DS : { |
| 694 | uint64_t TOCStart = findPPC64TOC(); |
| 695 | Value = ((Value + Addend) - TOCStart); |
| 696 | writeInt16BE(LocalAddress, applyPPClo(Value)); |
| 697 | } break; |
| 698 | } |
| 699 | } |
| 700 | |
Richard Sandiford | 6fc2ad6 | 2013-05-03 14:15:35 +0000 | [diff] [blame] | 701 | void RuntimeDyldELF::resolveSystemZRelocation(const SectionEntry &Section, |
| 702 | uint64_t Offset, |
| 703 | uint64_t Value, |
| 704 | uint32_t Type, |
| 705 | int64_t Addend) { |
| 706 | uint8_t *LocalAddress = Section.Address + Offset; |
| 707 | switch (Type) { |
| 708 | default: |
| 709 | llvm_unreachable("Relocation type not implemented yet!"); |
| 710 | break; |
| 711 | case ELF::R_390_PC16DBL: |
| 712 | case ELF::R_390_PLT16DBL: { |
| 713 | int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset); |
| 714 | assert(int16_t(Delta / 2) * 2 == Delta && "R_390_PC16DBL overflow"); |
| 715 | writeInt16BE(LocalAddress, Delta / 2); |
| 716 | break; |
| 717 | } |
| 718 | case ELF::R_390_PC32DBL: |
| 719 | case ELF::R_390_PLT32DBL: { |
| 720 | int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset); |
| 721 | assert(int32_t(Delta / 2) * 2 == Delta && "R_390_PC32DBL overflow"); |
| 722 | writeInt32BE(LocalAddress, Delta / 2); |
| 723 | break; |
| 724 | } |
| 725 | case ELF::R_390_PC32: { |
| 726 | int64_t Delta = (Value + Addend) - (Section.LoadAddress + Offset); |
| 727 | assert(int32_t(Delta) == Delta && "R_390_PC32 overflow"); |
| 728 | writeInt32BE(LocalAddress, Delta); |
| 729 | break; |
| 730 | } |
| 731 | case ELF::R_390_64: |
| 732 | writeInt64BE(LocalAddress, Value + Addend); |
| 733 | break; |
| 734 | } |
| 735 | } |
| 736 | |
Rafael Espindola | 87b5017 | 2013-04-29 17:24:34 +0000 | [diff] [blame] | 737 | void RuntimeDyldELF::resolveRelocation(const RelocationEntry &RE, |
| 738 | uint64_t Value) { |
| 739 | const SectionEntry &Section = Sections[RE.SectionID]; |
| 740 | return resolveRelocation(Section, RE.Offset, Value, RE.RelType, RE.Addend); |
| 741 | } |
| 742 | |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 743 | void RuntimeDyldELF::resolveRelocation(const SectionEntry &Section, |
| 744 | uint64_t Offset, |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 745 | uint64_t Value, |
| 746 | uint32_t Type, |
| 747 | int64_t Addend) { |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 748 | switch (Arch) { |
| 749 | case Triple::x86_64: |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 750 | resolveX86_64Relocation(Section, Offset, Value, Type, Addend); |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 751 | break; |
| 752 | case Triple::x86: |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 753 | resolveX86Relocation(Section, Offset, |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 754 | (uint32_t)(Value & 0xffffffffL), Type, |
| 755 | (uint32_t)(Addend & 0xffffffffL)); |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 756 | break; |
Tim Northover | 85829bb | 2013-05-04 20:13:59 +0000 | [diff] [blame] | 757 | case Triple::aarch64: |
| 758 | resolveAArch64Relocation(Section, Offset, Value, Type, Addend); |
| 759 | break; |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 760 | case Triple::arm: // Fall through. |
| 761 | case Triple::thumb: |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 762 | resolveARMRelocation(Section, Offset, |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 763 | (uint32_t)(Value & 0xffffffffL), Type, |
| 764 | (uint32_t)(Addend & 0xffffffffL)); |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 765 | break; |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 766 | case Triple::mips: // Fall through. |
| 767 | case Triple::mipsel: |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 768 | resolveMIPSRelocation(Section, Offset, |
Akira Hatanaka | b862f09 | 2012-08-20 17:53:24 +0000 | [diff] [blame] | 769 | (uint32_t)(Value & 0xffffffffL), Type, |
| 770 | (uint32_t)(Addend & 0xffffffffL)); |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 771 | break; |
Bill Schmidt | f38cc38 | 2013-07-26 01:35:43 +0000 | [diff] [blame] | 772 | case Triple::ppc64: // Fall through. |
| 773 | case Triple::ppc64le: |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 774 | resolvePPC64Relocation(Section, Offset, Value, Type, Addend); |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 775 | break; |
Richard Sandiford | 6fc2ad6 | 2013-05-03 14:15:35 +0000 | [diff] [blame] | 776 | case Triple::systemz: |
| 777 | resolveSystemZRelocation(Section, Offset, Value, Type, Addend); |
| 778 | break; |
Craig Topper | 8581438 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 779 | default: llvm_unreachable("Unsupported CPU type!"); |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 780 | } |
| 781 | } |
| 782 | |
Rafael Espindola | efa91f6 | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 783 | void RuntimeDyldELF::processRelocationRef(unsigned SectionID, |
Rafael Espindola | ca0e736 | 2013-04-29 19:03:21 +0000 | [diff] [blame] | 784 | RelocationRef RelI, |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 785 | ObjectImage &Obj, |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 786 | ObjSectionToIDMap &ObjSectionToID, |
Eli Bendersky | d98c9e9 | 2012-05-01 06:58:59 +0000 | [diff] [blame] | 787 | const SymbolTableMap &Symbols, |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 788 | StubMap &Stubs) { |
Rafael Espindola | efa91f6 | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 789 | uint64_t RelType; |
Rafael Espindola | ca0e736 | 2013-04-29 19:03:21 +0000 | [diff] [blame] | 790 | Check(RelI.getType(RelType)); |
Rafael Espindola | efa91f6 | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 791 | int64_t Addend; |
Rafael Espindola | 167957f | 2013-05-09 03:39:05 +0000 | [diff] [blame] | 792 | Check(getELFRelocationAddend(RelI, Addend)); |
Rafael Espindola | 6c1202c | 2013-06-05 01:33:53 +0000 | [diff] [blame] | 793 | symbol_iterator Symbol = RelI.getSymbol(); |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 794 | |
| 795 | // Obtain the symbol name which is referenced in the relocation |
| 796 | StringRef TargetName; |
Rafael Espindola | 0962b16 | 2013-06-05 02:55:01 +0000 | [diff] [blame] | 797 | if (Symbol != Obj.end_symbols()) |
| 798 | Symbol->getName(TargetName); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 799 | DEBUG(dbgs() << "\t\tRelType: " << RelType |
| 800 | << " Addend: " << Addend |
| 801 | << " TargetName: " << TargetName |
| 802 | << "\n"); |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 803 | RelocationValueRef Value; |
| 804 | // First search for the symbol in the local symbol table |
Rafael Espindola | 0962b16 | 2013-06-05 02:55:01 +0000 | [diff] [blame] | 805 | SymbolTableMap::const_iterator lsi = Symbols.end(); |
| 806 | SymbolRef::Type SymType = SymbolRef::ST_Unknown; |
| 807 | if (Symbol != Obj.end_symbols()) { |
| 808 | lsi = Symbols.find(TargetName.data()); |
| 809 | Symbol->getType(SymType); |
| 810 | } |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 811 | if (lsi != Symbols.end()) { |
| 812 | Value.SectionID = lsi->second.first; |
Ulrich Weigand | 03f018a | 2013-04-05 13:29:04 +0000 | [diff] [blame] | 813 | Value.Addend = lsi->second.second + Addend; |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 814 | } else { |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 815 | // Search for the symbol in the global symbol table |
Rafael Espindola | 0962b16 | 2013-06-05 02:55:01 +0000 | [diff] [blame] | 816 | SymbolTableMap::const_iterator gsi = GlobalSymbolTable.end(); |
| 817 | if (Symbol != Obj.end_symbols()) |
| 818 | gsi = GlobalSymbolTable.find(TargetName.data()); |
Eli Bendersky | d98c9e9 | 2012-05-01 06:58:59 +0000 | [diff] [blame] | 819 | if (gsi != GlobalSymbolTable.end()) { |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 820 | Value.SectionID = gsi->second.first; |
Ulrich Weigand | 03f018a | 2013-04-05 13:29:04 +0000 | [diff] [blame] | 821 | Value.Addend = gsi->second.second + Addend; |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 822 | } else { |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 823 | switch (SymType) { |
| 824 | case SymbolRef::ST_Debug: { |
| 825 | // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously |
| 826 | // and can be changed by another developers. Maybe best way is add |
| 827 | // a new symbol type ST_Section to SymbolRef and use it. |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 828 | section_iterator si(Obj.end_sections()); |
Rafael Espindola | 6c1202c | 2013-06-05 01:33:53 +0000 | [diff] [blame] | 829 | Symbol->getSection(si); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 830 | if (si == Obj.end_sections()) |
| 831 | llvm_unreachable("Symbol section not found, bad object file format!"); |
| 832 | DEBUG(dbgs() << "\t\tThis is section symbol\n"); |
Andrew Kaylor | fa8cd9d | 2012-10-12 23:53:16 +0000 | [diff] [blame] | 833 | // Default to 'true' in case isText fails (though it never does). |
| 834 | bool isCode = true; |
| 835 | si->isText(isCode); |
Michael J. Spencer | 4d9c539 | 2013-01-04 20:36:28 +0000 | [diff] [blame] | 836 | Value.SectionID = findOrEmitSection(Obj, |
| 837 | (*si), |
| 838 | isCode, |
Andrew Kaylor | fa8cd9d | 2012-10-12 23:53:16 +0000 | [diff] [blame] | 839 | ObjSectionToID); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 840 | Value.Addend = Addend; |
| 841 | break; |
| 842 | } |
| 843 | case SymbolRef::ST_Unknown: { |
| 844 | Value.SymbolName = TargetName.data(); |
| 845 | Value.Addend = Addend; |
| 846 | break; |
| 847 | } |
| 848 | default: |
| 849 | llvm_unreachable("Unresolved symbol type!"); |
| 850 | break; |
| 851 | } |
| 852 | } |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 853 | } |
Rafael Espindola | efa91f6 | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 854 | uint64_t Offset; |
Rafael Espindola | ca0e736 | 2013-04-29 19:03:21 +0000 | [diff] [blame] | 855 | Check(RelI.getOffset(Offset)); |
Rafael Espindola | efa91f6 | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 856 | |
| 857 | DEBUG(dbgs() << "\t\tSectionID: " << SectionID |
| 858 | << " Offset: " << Offset |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 859 | << "\n"); |
Tim Northover | 4a9b6b7 | 2013-05-04 20:14:09 +0000 | [diff] [blame] | 860 | if (Arch == Triple::aarch64 && |
| 861 | (RelType == ELF::R_AARCH64_CALL26 || |
| 862 | RelType == ELF::R_AARCH64_JUMP26)) { |
| 863 | // This is an AArch64 branch relocation, need to use a stub function. |
| 864 | DEBUG(dbgs() << "\t\tThis is an AArch64 branch relocation."); |
| 865 | SectionEntry &Section = Sections[SectionID]; |
| 866 | |
| 867 | // Look for an existing stub. |
| 868 | StubMap::const_iterator i = Stubs.find(Value); |
| 869 | if (i != Stubs.end()) { |
| 870 | resolveRelocation(Section, Offset, |
| 871 | (uint64_t)Section.Address + i->second, RelType, 0); |
| 872 | DEBUG(dbgs() << " Stub function found\n"); |
| 873 | } else { |
| 874 | // Create a new stub function. |
| 875 | DEBUG(dbgs() << " Create a new stub function\n"); |
| 876 | Stubs[Value] = Section.StubOffset; |
| 877 | uint8_t *StubTargetAddr = createStubFunction(Section.Address + |
| 878 | Section.StubOffset); |
| 879 | |
| 880 | RelocationEntry REmovz_g3(SectionID, |
| 881 | StubTargetAddr - Section.Address, |
| 882 | ELF::R_AARCH64_MOVW_UABS_G3, Value.Addend); |
| 883 | RelocationEntry REmovk_g2(SectionID, |
| 884 | StubTargetAddr - Section.Address + 4, |
| 885 | ELF::R_AARCH64_MOVW_UABS_G2_NC, Value.Addend); |
| 886 | RelocationEntry REmovk_g1(SectionID, |
| 887 | StubTargetAddr - Section.Address + 8, |
| 888 | ELF::R_AARCH64_MOVW_UABS_G1_NC, Value.Addend); |
| 889 | RelocationEntry REmovk_g0(SectionID, |
| 890 | StubTargetAddr - Section.Address + 12, |
| 891 | ELF::R_AARCH64_MOVW_UABS_G0_NC, Value.Addend); |
| 892 | |
| 893 | if (Value.SymbolName) { |
| 894 | addRelocationForSymbol(REmovz_g3, Value.SymbolName); |
| 895 | addRelocationForSymbol(REmovk_g2, Value.SymbolName); |
| 896 | addRelocationForSymbol(REmovk_g1, Value.SymbolName); |
| 897 | addRelocationForSymbol(REmovk_g0, Value.SymbolName); |
| 898 | } else { |
| 899 | addRelocationForSection(REmovz_g3, Value.SectionID); |
| 900 | addRelocationForSection(REmovk_g2, Value.SectionID); |
| 901 | addRelocationForSection(REmovk_g1, Value.SectionID); |
| 902 | addRelocationForSection(REmovk_g0, Value.SectionID); |
| 903 | } |
| 904 | resolveRelocation(Section, Offset, |
| 905 | (uint64_t)Section.Address + Section.StubOffset, |
| 906 | RelType, 0); |
| 907 | Section.StubOffset += getMaxStubSize(); |
| 908 | } |
| 909 | } else if (Arch == Triple::arm && |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 910 | (RelType == ELF::R_ARM_PC24 || |
| 911 | RelType == ELF::R_ARM_CALL || |
| 912 | RelType == ELF::R_ARM_JUMP24)) { |
| 913 | // This is an ARM branch relocation, need to use a stub function. |
| 914 | DEBUG(dbgs() << "\t\tThis is an ARM branch relocation."); |
Rafael Espindola | efa91f6 | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 915 | SectionEntry &Section = Sections[SectionID]; |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 916 | |
Eric Christopher | bf261f1 | 2012-10-23 17:19:15 +0000 | [diff] [blame] | 917 | // Look for an existing stub. |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 918 | StubMap::const_iterator i = Stubs.find(Value); |
| 919 | if (i != Stubs.end()) { |
Rafael Espindola | efa91f6 | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 920 | resolveRelocation(Section, Offset, |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 921 | (uint64_t)Section.Address + i->second, RelType, 0); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 922 | DEBUG(dbgs() << " Stub function found\n"); |
| 923 | } else { |
| 924 | // Create a new stub function. |
| 925 | DEBUG(dbgs() << " Create a new stub function\n"); |
| 926 | Stubs[Value] = Section.StubOffset; |
| 927 | uint8_t *StubTargetAddr = createStubFunction(Section.Address + |
| 928 | Section.StubOffset); |
Rafael Espindola | efa91f6 | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 929 | RelocationEntry RE(SectionID, StubTargetAddr - Section.Address, |
Tim Northover | e274b47 | 2013-05-28 19:48:19 +0000 | [diff] [blame] | 930 | ELF::R_ARM_PRIVATE_0, Value.Addend); |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 931 | if (Value.SymbolName) |
| 932 | addRelocationForSymbol(RE, Value.SymbolName); |
| 933 | else |
| 934 | addRelocationForSection(RE, Value.SectionID); |
| 935 | |
Rafael Espindola | efa91f6 | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 936 | resolveRelocation(Section, Offset, |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 937 | (uint64_t)Section.Address + Section.StubOffset, |
| 938 | RelType, 0); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 939 | Section.StubOffset += getMaxStubSize(); |
| 940 | } |
Akira Hatanaka | ade0474 | 2012-12-03 23:12:19 +0000 | [diff] [blame] | 941 | } else if ((Arch == Triple::mipsel || Arch == Triple::mips) && |
| 942 | RelType == ELF::R_MIPS_26) { |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 943 | // This is an Mips branch relocation, need to use a stub function. |
| 944 | DEBUG(dbgs() << "\t\tThis is a Mips branch relocation."); |
Rafael Espindola | efa91f6 | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 945 | SectionEntry &Section = Sections[SectionID]; |
| 946 | uint8_t *Target = Section.Address + Offset; |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 947 | uint32_t *TargetAddress = (uint32_t *)Target; |
| 948 | |
| 949 | // Extract the addend from the instruction. |
| 950 | uint32_t Addend = ((*TargetAddress) & 0x03ffffff) << 2; |
| 951 | |
| 952 | Value.Addend += Addend; |
| 953 | |
| 954 | // Look up for existing stub. |
| 955 | StubMap::const_iterator i = Stubs.find(Value); |
| 956 | if (i != Stubs.end()) { |
Rafael Espindola | efa91f6 | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 957 | resolveRelocation(Section, Offset, |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 958 | (uint64_t)Section.Address + i->second, RelType, 0); |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 959 | DEBUG(dbgs() << " Stub function found\n"); |
| 960 | } else { |
| 961 | // Create a new stub function. |
| 962 | DEBUG(dbgs() << " Create a new stub function\n"); |
| 963 | Stubs[Value] = Section.StubOffset; |
| 964 | uint8_t *StubTargetAddr = createStubFunction(Section.Address + |
| 965 | Section.StubOffset); |
| 966 | |
| 967 | // Creating Hi and Lo relocations for the filled stub instructions. |
Rafael Espindola | efa91f6 | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 968 | RelocationEntry REHi(SectionID, |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 969 | StubTargetAddr - Section.Address, |
Akira Hatanaka | 3af1c9d | 2013-07-24 01:58:40 +0000 | [diff] [blame] | 970 | ELF::R_MIPS_UNUSED1, Value.Addend); |
Rafael Espindola | efa91f6 | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 971 | RelocationEntry RELo(SectionID, |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 972 | StubTargetAddr - Section.Address + 4, |
Akira Hatanaka | 3af1c9d | 2013-07-24 01:58:40 +0000 | [diff] [blame] | 973 | ELF::R_MIPS_UNUSED2, Value.Addend); |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 974 | |
| 975 | if (Value.SymbolName) { |
| 976 | addRelocationForSymbol(REHi, Value.SymbolName); |
| 977 | addRelocationForSymbol(RELo, Value.SymbolName); |
| 978 | } else { |
| 979 | addRelocationForSection(REHi, Value.SectionID); |
| 980 | addRelocationForSection(RELo, Value.SectionID); |
| 981 | } |
| 982 | |
Rafael Espindola | efa91f6 | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 983 | resolveRelocation(Section, Offset, |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 984 | (uint64_t)Section.Address + Section.StubOffset, |
| 985 | RelType, 0); |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 986 | Section.StubOffset += getMaxStubSize(); |
| 987 | } |
Bill Schmidt | f38cc38 | 2013-07-26 01:35:43 +0000 | [diff] [blame] | 988 | } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) { |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 989 | if (RelType == ELF::R_PPC64_REL24) { |
| 990 | // A PPC branch relocation will need a stub function if the target is |
| 991 | // an external symbol (Symbol::ST_Unknown) or if the target address |
| 992 | // is not within the signed 24-bits branch address. |
Rafael Espindola | efa91f6 | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 993 | SectionEntry &Section = Sections[SectionID]; |
| 994 | uint8_t *Target = Section.Address + Offset; |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 995 | bool RangeOverflow = false; |
| 996 | if (SymType != SymbolRef::ST_Unknown) { |
| 997 | // A function call may points to the .opd entry, so the final symbol value |
| 998 | // in calculated based in the relocation values in .opd section. |
| 999 | findOPDEntrySection(Obj, ObjSectionToID, Value); |
| 1000 | uint8_t *RelocTarget = Sections[Value.SectionID].Address + Value.Addend; |
| 1001 | int32_t delta = static_cast<int32_t>(Target - RelocTarget); |
| 1002 | // If it is within 24-bits branch range, just set the branch target |
| 1003 | if (SignExtend32<24>(delta) == delta) { |
Rafael Espindola | efa91f6 | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 1004 | RelocationEntry RE(SectionID, Offset, RelType, Value.Addend); |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 1005 | if (Value.SymbolName) |
| 1006 | addRelocationForSymbol(RE, Value.SymbolName); |
| 1007 | else |
| 1008 | addRelocationForSection(RE, Value.SectionID); |
| 1009 | } else { |
| 1010 | RangeOverflow = true; |
| 1011 | } |
| 1012 | } |
| 1013 | if (SymType == SymbolRef::ST_Unknown || RangeOverflow == true) { |
| 1014 | // It is an external symbol (SymbolRef::ST_Unknown) or within a range |
| 1015 | // larger than 24-bits. |
| 1016 | StubMap::const_iterator i = Stubs.find(Value); |
| 1017 | if (i != Stubs.end()) { |
| 1018 | // Symbol function stub already created, just relocate to it |
Rafael Espindola | efa91f6 | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 1019 | resolveRelocation(Section, Offset, |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 1020 | (uint64_t)Section.Address + i->second, RelType, 0); |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 1021 | DEBUG(dbgs() << " Stub function found\n"); |
| 1022 | } else { |
| 1023 | // Create a new stub function. |
| 1024 | DEBUG(dbgs() << " Create a new stub function\n"); |
| 1025 | Stubs[Value] = Section.StubOffset; |
| 1026 | uint8_t *StubTargetAddr = createStubFunction(Section.Address + |
| 1027 | Section.StubOffset); |
Rafael Espindola | efa91f6 | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 1028 | RelocationEntry RE(SectionID, StubTargetAddr - Section.Address, |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 1029 | ELF::R_PPC64_ADDR64, Value.Addend); |
| 1030 | |
| 1031 | // Generates the 64-bits address loads as exemplified in section |
| 1032 | // 4.5.1 in PPC64 ELF ABI. |
Rafael Espindola | efa91f6 | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 1033 | RelocationEntry REhst(SectionID, |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 1034 | StubTargetAddr - Section.Address + 2, |
| 1035 | ELF::R_PPC64_ADDR16_HIGHEST, Value.Addend); |
Rafael Espindola | efa91f6 | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 1036 | RelocationEntry REhr(SectionID, |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 1037 | StubTargetAddr - Section.Address + 6, |
| 1038 | ELF::R_PPC64_ADDR16_HIGHER, Value.Addend); |
Rafael Espindola | efa91f6 | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 1039 | RelocationEntry REh(SectionID, |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 1040 | StubTargetAddr - Section.Address + 14, |
| 1041 | ELF::R_PPC64_ADDR16_HI, Value.Addend); |
Rafael Espindola | efa91f6 | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 1042 | RelocationEntry REl(SectionID, |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 1043 | StubTargetAddr - Section.Address + 18, |
| 1044 | ELF::R_PPC64_ADDR16_LO, Value.Addend); |
| 1045 | |
| 1046 | if (Value.SymbolName) { |
| 1047 | addRelocationForSymbol(REhst, Value.SymbolName); |
| 1048 | addRelocationForSymbol(REhr, Value.SymbolName); |
| 1049 | addRelocationForSymbol(REh, Value.SymbolName); |
| 1050 | addRelocationForSymbol(REl, Value.SymbolName); |
| 1051 | } else { |
| 1052 | addRelocationForSection(REhst, Value.SectionID); |
| 1053 | addRelocationForSection(REhr, Value.SectionID); |
| 1054 | addRelocationForSection(REh, Value.SectionID); |
| 1055 | addRelocationForSection(REl, Value.SectionID); |
| 1056 | } |
| 1057 | |
Rafael Espindola | efa91f6 | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 1058 | resolveRelocation(Section, Offset, |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 1059 | (uint64_t)Section.Address + Section.StubOffset, |
| 1060 | RelType, 0); |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 1061 | if (SymType == SymbolRef::ST_Unknown) |
| 1062 | // Restore the TOC for external calls |
| 1063 | writeInt32BE(Target+4, 0xE8410028); // ld r2,40(r1) |
| 1064 | Section.StubOffset += getMaxStubSize(); |
| 1065 | } |
| 1066 | } |
| 1067 | } else { |
Rafael Espindola | efa91f6 | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 1068 | RelocationEntry RE(SectionID, Offset, RelType, Value.Addend); |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 1069 | // Extra check to avoid relocation againt empty symbols (usually |
| 1070 | // the R_PPC64_TOC). |
| 1071 | if (Value.SymbolName && !TargetName.empty()) |
| 1072 | addRelocationForSymbol(RE, Value.SymbolName); |
| 1073 | else |
| 1074 | addRelocationForSection(RE, Value.SectionID); |
| 1075 | } |
Richard Sandiford | 6fc2ad6 | 2013-05-03 14:15:35 +0000 | [diff] [blame] | 1076 | } else if (Arch == Triple::systemz && |
| 1077 | (RelType == ELF::R_390_PLT32DBL || |
| 1078 | RelType == ELF::R_390_GOTENT)) { |
| 1079 | // Create function stubs for both PLT and GOT references, regardless of |
| 1080 | // whether the GOT reference is to data or code. The stub contains the |
| 1081 | // full address of the symbol, as needed by GOT references, and the |
| 1082 | // executable part only adds an overhead of 8 bytes. |
| 1083 | // |
| 1084 | // We could try to conserve space by allocating the code and data |
| 1085 | // parts of the stub separately. However, as things stand, we allocate |
| 1086 | // a stub for every relocation, so using a GOT in JIT code should be |
| 1087 | // no less space efficient than using an explicit constant pool. |
| 1088 | DEBUG(dbgs() << "\t\tThis is a SystemZ indirect relocation."); |
| 1089 | SectionEntry &Section = Sections[SectionID]; |
| 1090 | |
| 1091 | // Look for an existing stub. |
| 1092 | StubMap::const_iterator i = Stubs.find(Value); |
| 1093 | uintptr_t StubAddress; |
| 1094 | if (i != Stubs.end()) { |
| 1095 | StubAddress = uintptr_t(Section.Address) + i->second; |
| 1096 | DEBUG(dbgs() << " Stub function found\n"); |
| 1097 | } else { |
| 1098 | // Create a new stub function. |
| 1099 | DEBUG(dbgs() << " Create a new stub function\n"); |
| 1100 | |
| 1101 | uintptr_t BaseAddress = uintptr_t(Section.Address); |
| 1102 | uintptr_t StubAlignment = getStubAlignment(); |
| 1103 | StubAddress = (BaseAddress + Section.StubOffset + |
| 1104 | StubAlignment - 1) & -StubAlignment; |
| 1105 | unsigned StubOffset = StubAddress - BaseAddress; |
| 1106 | |
| 1107 | Stubs[Value] = StubOffset; |
| 1108 | createStubFunction((uint8_t *)StubAddress); |
| 1109 | RelocationEntry RE(SectionID, StubOffset + 8, |
| 1110 | ELF::R_390_64, Value.Addend - Addend); |
| 1111 | if (Value.SymbolName) |
| 1112 | addRelocationForSymbol(RE, Value.SymbolName); |
| 1113 | else |
| 1114 | addRelocationForSection(RE, Value.SectionID); |
| 1115 | Section.StubOffset = StubOffset + getMaxStubSize(); |
| 1116 | } |
| 1117 | |
| 1118 | if (RelType == ELF::R_390_GOTENT) |
| 1119 | resolveRelocation(Section, Offset, StubAddress + 8, |
| 1120 | ELF::R_390_PC32DBL, Addend); |
| 1121 | else |
| 1122 | resolveRelocation(Section, Offset, StubAddress, RelType, Addend); |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 1123 | } else { |
Rafael Espindola | efa91f6 | 2013-04-29 14:44:23 +0000 | [diff] [blame] | 1124 | RelocationEntry RE(SectionID, Offset, RelType, Value.Addend); |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 1125 | if (Value.SymbolName) |
| 1126 | addRelocationForSymbol(RE, Value.SymbolName); |
| 1127 | else |
| 1128 | addRelocationForSection(RE, Value.SectionID); |
| 1129 | } |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 1132 | bool RuntimeDyldELF::isCompatibleFormat(const ObjectBuffer *Buffer) const { |
| 1133 | if (Buffer->getBufferSize() < strlen(ELF::ElfMagic)) |
| 1134 | return false; |
| 1135 | return (memcmp(Buffer->getBufferStart(), ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0; |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 1136 | } |
| 1137 | } // namespace llvm |