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/OwningPtr.h" |
| 19 | #include "llvm/ADT/StringRef.h" |
| 20 | #include "llvm/ADT/STLExtras.h" |
| 21 | #include "llvm/ADT/IntervalMap.h" |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 22 | #include "llvm/Object/ObjectFile.h" |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 23 | #include "llvm/ExecutionEngine/ObjectImage.h" |
| 24 | #include "llvm/ExecutionEngine/ObjectBuffer.h" |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 25 | #include "llvm/Support/ELF.h" |
| 26 | #include "llvm/ADT/Triple.h" |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 27 | #include "llvm/Object/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 | |
| 33 | template<support::endianness target_endianness, bool is64Bits> |
| 34 | class DyldELFObject : public ELFObjectFile<target_endianness, is64Bits> { |
| 35 | LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits) |
| 36 | |
| 37 | typedef Elf_Shdr_Impl<target_endianness, is64Bits> Elf_Shdr; |
| 38 | typedef Elf_Sym_Impl<target_endianness, is64Bits> Elf_Sym; |
| 39 | typedef Elf_Rel_Impl<target_endianness, is64Bits, false> Elf_Rel; |
| 40 | typedef Elf_Rel_Impl<target_endianness, is64Bits, true> Elf_Rela; |
| 41 | |
Michael J. Spencer | 2c38a66 | 2012-09-10 19:04:02 +0000 | [diff] [blame] | 42 | typedef Elf_Ehdr_Impl<target_endianness, is64Bits> Elf_Ehdr; |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 43 | |
| 44 | typedef typename ELFDataTypeTypedefHelper< |
| 45 | target_endianness, is64Bits>::value_type addr_type; |
| 46 | |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 47 | public: |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 48 | DyldELFObject(MemoryBuffer *Wrapper, error_code &ec); |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 49 | |
| 50 | void updateSectionAddress(const SectionRef &Sec, uint64_t Addr); |
| 51 | void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr); |
| 52 | |
Andrew Kaylor | 2e31987 | 2012-07-27 17:52:42 +0000 | [diff] [blame] | 53 | // Methods for type inquiry through isa, cast and dyn_cast |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 54 | static inline bool classof(const Binary *v) { |
| 55 | return (isa<ELFObjectFile<target_endianness, is64Bits> >(v) |
| 56 | && classof(cast<ELFObjectFile<target_endianness, is64Bits> >(v))); |
| 57 | } |
| 58 | static inline bool classof( |
| 59 | const ELFObjectFile<target_endianness, is64Bits> *v) { |
| 60 | return v->isDyldType(); |
| 61 | } |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | template<support::endianness target_endianness, bool is64Bits> |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 65 | class ELFObjectImage : public ObjectImageCommon { |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 66 | protected: |
| 67 | DyldELFObject<target_endianness, is64Bits> *DyldObj; |
| 68 | bool Registered; |
| 69 | |
| 70 | public: |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 71 | ELFObjectImage(ObjectBuffer *Input, |
| 72 | DyldELFObject<target_endianness, is64Bits> *Obj) |
| 73 | : ObjectImageCommon(Input, Obj), |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 74 | DyldObj(Obj), |
| 75 | Registered(false) {} |
| 76 | |
| 77 | virtual ~ELFObjectImage() { |
| 78 | if (Registered) |
| 79 | deregisterWithDebugger(); |
| 80 | } |
| 81 | |
| 82 | // Subclasses can override these methods to update the image with loaded |
| 83 | // addresses for sections and common symbols |
| 84 | virtual void updateSectionAddress(const SectionRef &Sec, uint64_t Addr) |
| 85 | { |
| 86 | DyldObj->updateSectionAddress(Sec, Addr); |
| 87 | } |
| 88 | |
| 89 | virtual void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr) |
| 90 | { |
| 91 | DyldObj->updateSymbolAddress(Sym, Addr); |
| 92 | } |
| 93 | |
| 94 | virtual void registerWithDebugger() |
| 95 | { |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 96 | JITRegistrar::getGDBRegistrar().registerObject(*Buffer); |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 97 | Registered = true; |
| 98 | } |
| 99 | virtual void deregisterWithDebugger() |
| 100 | { |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 101 | JITRegistrar::getGDBRegistrar().deregisterObject(*Buffer); |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 102 | } |
| 103 | }; |
| 104 | |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 105 | // The MemoryBuffer passed into this constructor is just a wrapper around the |
| 106 | // actual memory. Ultimately, the Binary parent class will take ownership of |
| 107 | // this MemoryBuffer object but not the underlying memory. |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 108 | template<support::endianness target_endianness, bool is64Bits> |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 109 | DyldELFObject<target_endianness, is64Bits>::DyldELFObject(MemoryBuffer *Wrapper, |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 110 | error_code &ec) |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 111 | : ELFObjectFile<target_endianness, is64Bits>(Wrapper, ec) { |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 112 | this->isDyldELFObject = true; |
| 113 | } |
| 114 | |
| 115 | template<support::endianness target_endianness, bool is64Bits> |
| 116 | void DyldELFObject<target_endianness, is64Bits>::updateSectionAddress( |
| 117 | const SectionRef &Sec, |
| 118 | uint64_t Addr) { |
| 119 | DataRefImpl ShdrRef = Sec.getRawDataRefImpl(); |
| 120 | Elf_Shdr *shdr = const_cast<Elf_Shdr*>( |
| 121 | reinterpret_cast<const Elf_Shdr *>(ShdrRef.p)); |
| 122 | |
| 123 | // This assumes the address passed in matches the target address bitness |
| 124 | // The template-based type cast handles everything else. |
| 125 | shdr->sh_addr = static_cast<addr_type>(Addr); |
| 126 | } |
| 127 | |
| 128 | template<support::endianness target_endianness, bool is64Bits> |
| 129 | void DyldELFObject<target_endianness, is64Bits>::updateSymbolAddress( |
| 130 | const SymbolRef &SymRef, |
| 131 | uint64_t Addr) { |
| 132 | |
| 133 | Elf_Sym *sym = const_cast<Elf_Sym*>( |
| 134 | ELFObjectFile<target_endianness, is64Bits>:: |
| 135 | getSymbol(SymRef.getRawDataRefImpl())); |
| 136 | |
| 137 | // This assumes the address passed in matches the target address bitness |
| 138 | // The template-based type cast handles everything else. |
| 139 | sym->st_value = static_cast<addr_type>(Addr); |
| 140 | } |
| 141 | |
| 142 | } // namespace |
| 143 | |
| 144 | |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 145 | namespace llvm { |
| 146 | |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 147 | ObjectImage *RuntimeDyldELF::createObjectImage(ObjectBuffer *Buffer) { |
| 148 | if (Buffer->getBufferSize() < ELF::EI_NIDENT) |
| 149 | llvm_unreachable("Unexpected ELF object size"); |
| 150 | std::pair<unsigned char, unsigned char> Ident = std::make_pair( |
| 151 | (uint8_t)Buffer->getBufferStart()[ELF::EI_CLASS], |
| 152 | (uint8_t)Buffer->getBufferStart()[ELF::EI_DATA]); |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 153 | error_code ec; |
| 154 | |
| 155 | if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB) { |
| 156 | DyldELFObject<support::little, false> *Obj = |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 157 | new DyldELFObject<support::little, false>(Buffer->getMemBuffer(), ec); |
| 158 | return new ELFObjectImage<support::little, false>(Buffer, Obj); |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 159 | } |
| 160 | else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB) { |
| 161 | DyldELFObject<support::big, false> *Obj = |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 162 | new DyldELFObject<support::big, false>(Buffer->getMemBuffer(), ec); |
| 163 | return new ELFObjectImage<support::big, false>(Buffer, Obj); |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 164 | } |
| 165 | else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB) { |
| 166 | DyldELFObject<support::big, true> *Obj = |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 167 | new DyldELFObject<support::big, true>(Buffer->getMemBuffer(), ec); |
| 168 | return new ELFObjectImage<support::big, true>(Buffer, Obj); |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 169 | } |
| 170 | else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) { |
| 171 | DyldELFObject<support::little, true> *Obj = |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 172 | new DyldELFObject<support::little, true>(Buffer->getMemBuffer(), ec); |
| 173 | return new ELFObjectImage<support::little, true>(Buffer, Obj); |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 174 | } |
| 175 | else |
| 176 | llvm_unreachable("Unexpected ELF format"); |
| 177 | } |
| 178 | |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 179 | RuntimeDyldELF::~RuntimeDyldELF() { |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 180 | } |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 181 | |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 182 | void RuntimeDyldELF::resolveX86_64Relocation(uint8_t *LocalAddress, |
| 183 | uint64_t FinalAddress, |
| 184 | uint64_t Value, |
| 185 | uint32_t Type, |
| 186 | int64_t Addend) { |
| 187 | switch (Type) { |
| 188 | default: |
| 189 | llvm_unreachable("Relocation type not implemented yet!"); |
| 190 | break; |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 191 | case ELF::R_X86_64_64: { |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 192 | uint64_t *Target = (uint64_t*)(LocalAddress); |
| 193 | *Target = Value + Addend; |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 194 | break; |
| 195 | } |
| 196 | case ELF::R_X86_64_32: |
| 197 | case ELF::R_X86_64_32S: { |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 198 | Value += Addend; |
Andrew Kaylor | d83a547 | 2012-07-27 20:30:12 +0000 | [diff] [blame] | 199 | assert((Type == ELF::R_X86_64_32 && (Value <= UINT32_MAX)) || |
| 200 | (Type == ELF::R_X86_64_32S && |
| 201 | ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN))); |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 202 | uint32_t TruncatedAddr = (Value & 0xFFFFFFFF); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 203 | uint32_t *Target = reinterpret_cast<uint32_t*>(LocalAddress); |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 204 | *Target = TruncatedAddr; |
| 205 | break; |
| 206 | } |
| 207 | case ELF::R_X86_64_PC32: { |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 208 | uint32_t *Placeholder = reinterpret_cast<uint32_t*>(LocalAddress); |
| 209 | int64_t RealOffset = *Placeholder + Value + Addend - FinalAddress; |
Andrew Kaylor | d83a547 | 2012-07-27 20:30:12 +0000 | [diff] [blame] | 210 | assert(RealOffset <= INT32_MAX && RealOffset >= INT32_MIN); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 211 | int32_t TruncOffset = (RealOffset & 0xFFFFFFFF); |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 212 | *Placeholder = TruncOffset; |
| 213 | break; |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 218 | void RuntimeDyldELF::resolveX86Relocation(uint8_t *LocalAddress, |
| 219 | uint32_t FinalAddress, |
| 220 | uint32_t Value, |
| 221 | uint32_t Type, |
| 222 | int32_t Addend) { |
| 223 | switch (Type) { |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 224 | case ELF::R_386_32: { |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 225 | uint32_t *Target = (uint32_t*)(LocalAddress); |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 226 | uint32_t Placeholder = *Target; |
| 227 | *Target = Placeholder + Value + Addend; |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 228 | break; |
| 229 | } |
| 230 | case ELF::R_386_PC32: { |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 231 | uint32_t *Placeholder = reinterpret_cast<uint32_t*>(LocalAddress); |
| 232 | uint32_t RealOffset = *Placeholder + Value + Addend - FinalAddress; |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 233 | *Placeholder = RealOffset; |
| 234 | break; |
| 235 | } |
| 236 | default: |
| 237 | // There are other relocation types, but it appears these are the |
Andrew Kaylor | e2e73bd | 2012-07-27 18:39:47 +0000 | [diff] [blame] | 238 | // only ones currently used by the LLVM ELF object writer |
Craig Topper | 8581438 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 239 | llvm_unreachable("Relocation type not implemented yet!"); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 240 | break; |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 244 | void RuntimeDyldELF::resolveARMRelocation(uint8_t *LocalAddress, |
| 245 | uint32_t FinalAddress, |
| 246 | uint32_t Value, |
| 247 | uint32_t Type, |
| 248 | int32_t Addend) { |
| 249 | // TODO: Add Thumb relocations. |
| 250 | uint32_t* TargetPtr = (uint32_t*)LocalAddress; |
| 251 | Value += Addend; |
| 252 | |
| 253 | DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: " << LocalAddress |
| 254 | << " FinalAddress: " << format("%p",FinalAddress) |
| 255 | << " Value: " << format("%x",Value) |
| 256 | << " Type: " << format("%x",Type) |
| 257 | << " Addend: " << format("%x",Addend) |
| 258 | << "\n"); |
| 259 | |
| 260 | switch(Type) { |
| 261 | default: |
| 262 | llvm_unreachable("Not implemented relocation type!"); |
| 263 | |
Tim Northover | 565ebde | 2012-10-03 16:29:42 +0000 | [diff] [blame] | 264 | // Write a 32bit value to relocation address, taking into account the |
| 265 | // implicit addend encoded in the target. |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 266 | case ELF::R_ARM_ABS32 : |
Tim Northover | 565ebde | 2012-10-03 16:29:42 +0000 | [diff] [blame] | 267 | *TargetPtr += Value; |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 268 | break; |
| 269 | |
| 270 | // Write first 16 bit of 32 bit value to the mov instruction. |
| 271 | // Last 4 bit should be shifted. |
| 272 | case ELF::R_ARM_MOVW_ABS_NC : |
Tim Northover | 565ebde | 2012-10-03 16:29:42 +0000 | [diff] [blame] | 273 | // We are not expecting any other addend in the relocation address. |
| 274 | // Using 0x000F0FFF because MOVW has its 16 bit immediate split into 2 |
| 275 | // non-contiguous fields. |
| 276 | assert((*TargetPtr & 0x000F0FFF) == 0); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 277 | Value = Value & 0xFFFF; |
| 278 | *TargetPtr |= Value & 0xFFF; |
| 279 | *TargetPtr |= ((Value >> 12) & 0xF) << 16; |
| 280 | break; |
| 281 | |
| 282 | // Write last 16 bit of 32 bit value to the mov instruction. |
| 283 | // Last 4 bit should be shifted. |
| 284 | case ELF::R_ARM_MOVT_ABS : |
Tim Northover | 565ebde | 2012-10-03 16:29:42 +0000 | [diff] [blame] | 285 | // We are not expecting any other addend in the relocation address. |
| 286 | // Use 0x000F0FFF for the same reason as R_ARM_MOVW_ABS_NC. |
| 287 | assert((*TargetPtr & 0x000F0FFF) == 0); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 288 | Value = (Value >> 16) & 0xFFFF; |
| 289 | *TargetPtr |= Value & 0xFFF; |
| 290 | *TargetPtr |= ((Value >> 12) & 0xF) << 16; |
| 291 | break; |
| 292 | |
| 293 | // Write 24 bit relative value to the branch instruction. |
| 294 | case ELF::R_ARM_PC24 : // Fall through. |
| 295 | case ELF::R_ARM_CALL : // Fall through. |
| 296 | case ELF::R_ARM_JUMP24 : |
| 297 | int32_t RelValue = static_cast<int32_t>(Value - FinalAddress - 8); |
| 298 | RelValue = (RelValue & 0x03FFFFFC) >> 2; |
| 299 | *TargetPtr &= 0xFF000000; |
| 300 | *TargetPtr |= RelValue; |
| 301 | break; |
| 302 | } |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 305 | void RuntimeDyldELF::resolveMIPSRelocation(uint8_t *LocalAddress, |
Akira Hatanaka | b862f09 | 2012-08-20 17:53:24 +0000 | [diff] [blame] | 306 | uint32_t FinalAddress, |
| 307 | uint32_t Value, |
| 308 | uint32_t Type, |
| 309 | int32_t Addend) { |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 310 | uint32_t* TargetPtr = (uint32_t*)LocalAddress; |
| 311 | Value += Addend; |
| 312 | |
| 313 | DEBUG(dbgs() << "resolveMipselocation, LocalAddress: " << LocalAddress |
| 314 | << " FinalAddress: " << format("%p",FinalAddress) |
| 315 | << " Value: " << format("%x",Value) |
| 316 | << " Type: " << format("%x",Type) |
| 317 | << " Addend: " << format("%x",Addend) |
| 318 | << "\n"); |
| 319 | |
| 320 | switch(Type) { |
| 321 | default: |
| 322 | llvm_unreachable("Not implemented relocation type!"); |
| 323 | break; |
| 324 | case ELF::R_MIPS_32: |
| 325 | *TargetPtr = Value + (*TargetPtr); |
| 326 | break; |
| 327 | case ELF::R_MIPS_26: |
| 328 | *TargetPtr = ((*TargetPtr) & 0xfc000000) | (( Value & 0x0fffffff) >> 2); |
| 329 | break; |
| 330 | case ELF::R_MIPS_HI16: |
| 331 | // Get the higher 16-bits. Also add 1 if bit 15 is 1. |
| 332 | Value += ((*TargetPtr) & 0x0000ffff) << 16; |
| 333 | *TargetPtr = ((*TargetPtr) & 0xffff0000) | |
| 334 | (((Value + 0x8000) >> 16) & 0xffff); |
| 335 | break; |
| 336 | case ELF::R_MIPS_LO16: |
| 337 | Value += ((*TargetPtr) & 0x0000ffff); |
| 338 | *TargetPtr = ((*TargetPtr) & 0xffff0000) | (Value & 0xffff); |
| 339 | break; |
| 340 | } |
| 341 | } |
| 342 | |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 343 | void RuntimeDyldELF::resolveRelocation(uint8_t *LocalAddress, |
| 344 | uint64_t FinalAddress, |
| 345 | uint64_t Value, |
| 346 | uint32_t Type, |
| 347 | int64_t Addend) { |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 348 | switch (Arch) { |
| 349 | case Triple::x86_64: |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 350 | resolveX86_64Relocation(LocalAddress, FinalAddress, Value, Type, Addend); |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 351 | break; |
| 352 | case Triple::x86: |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 353 | resolveX86Relocation(LocalAddress, (uint32_t)(FinalAddress & 0xffffffffL), |
| 354 | (uint32_t)(Value & 0xffffffffL), Type, |
| 355 | (uint32_t)(Addend & 0xffffffffL)); |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 356 | break; |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 357 | case Triple::arm: // Fall through. |
| 358 | case Triple::thumb: |
| 359 | resolveARMRelocation(LocalAddress, (uint32_t)(FinalAddress & 0xffffffffL), |
| 360 | (uint32_t)(Value & 0xffffffffL), Type, |
| 361 | (uint32_t)(Addend & 0xffffffffL)); |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 362 | break; |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 363 | case Triple::mips: // Fall through. |
| 364 | case Triple::mipsel: |
| 365 | resolveMIPSRelocation(LocalAddress, (uint32_t)(FinalAddress & 0xffffffffL), |
Akira Hatanaka | b862f09 | 2012-08-20 17:53:24 +0000 | [diff] [blame] | 366 | (uint32_t)(Value & 0xffffffffL), Type, |
| 367 | (uint32_t)(Addend & 0xffffffffL)); |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 368 | break; |
Craig Topper | 8581438 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 369 | default: llvm_unreachable("Unsupported CPU type!"); |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 373 | void RuntimeDyldELF::processRelocationRef(const ObjRelocationInfo &Rel, |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 374 | ObjectImage &Obj, |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 375 | ObjSectionToIDMap &ObjSectionToID, |
Eli Bendersky | d98c9e9 | 2012-05-01 06:58:59 +0000 | [diff] [blame] | 376 | const SymbolTableMap &Symbols, |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 377 | StubMap &Stubs) { |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 378 | |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 379 | uint32_t RelType = (uint32_t)(Rel.Type & 0xffffffffL); |
| 380 | intptr_t Addend = (intptr_t)Rel.AdditionalInfo; |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 381 | const SymbolRef &Symbol = Rel.Symbol; |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 382 | |
| 383 | // Obtain the symbol name which is referenced in the relocation |
| 384 | StringRef TargetName; |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 385 | Symbol.getName(TargetName); |
| 386 | DEBUG(dbgs() << "\t\tRelType: " << RelType |
| 387 | << " Addend: " << Addend |
| 388 | << " TargetName: " << TargetName |
| 389 | << "\n"); |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 390 | RelocationValueRef Value; |
| 391 | // First search for the symbol in the local symbol table |
Eli Bendersky | d98c9e9 | 2012-05-01 06:58:59 +0000 | [diff] [blame] | 392 | SymbolTableMap::const_iterator lsi = Symbols.find(TargetName.data()); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 393 | if (lsi != Symbols.end()) { |
| 394 | Value.SectionID = lsi->second.first; |
| 395 | Value.Addend = lsi->second.second; |
| 396 | } else { |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 397 | // Search for the symbol in the global symbol table |
Eli Bendersky | d98c9e9 | 2012-05-01 06:58:59 +0000 | [diff] [blame] | 398 | SymbolTableMap::const_iterator gsi = |
| 399 | GlobalSymbolTable.find(TargetName.data()); |
| 400 | if (gsi != GlobalSymbolTable.end()) { |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 401 | Value.SectionID = gsi->second.first; |
| 402 | Value.Addend = gsi->second.second; |
| 403 | } else { |
| 404 | SymbolRef::Type SymType; |
| 405 | Symbol.getType(SymType); |
| 406 | switch (SymType) { |
| 407 | case SymbolRef::ST_Debug: { |
| 408 | // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously |
| 409 | // and can be changed by another developers. Maybe best way is add |
| 410 | // a new symbol type ST_Section to SymbolRef and use it. |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 411 | section_iterator si(Obj.end_sections()); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 412 | Symbol.getSection(si); |
| 413 | if (si == Obj.end_sections()) |
| 414 | llvm_unreachable("Symbol section not found, bad object file format!"); |
| 415 | DEBUG(dbgs() << "\t\tThis is section symbol\n"); |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 416 | Value.SectionID = findOrEmitSection(Obj, (*si), true, ObjSectionToID); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 417 | Value.Addend = Addend; |
| 418 | break; |
| 419 | } |
| 420 | case SymbolRef::ST_Unknown: { |
| 421 | Value.SymbolName = TargetName.data(); |
| 422 | Value.Addend = Addend; |
| 423 | break; |
| 424 | } |
| 425 | default: |
| 426 | llvm_unreachable("Unresolved symbol type!"); |
| 427 | break; |
| 428 | } |
| 429 | } |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 430 | } |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 431 | DEBUG(dbgs() << "\t\tRel.SectionID: " << Rel.SectionID |
| 432 | << " Rel.Offset: " << Rel.Offset |
| 433 | << "\n"); |
| 434 | if (Arch == Triple::arm && |
| 435 | (RelType == ELF::R_ARM_PC24 || |
| 436 | RelType == ELF::R_ARM_CALL || |
| 437 | RelType == ELF::R_ARM_JUMP24)) { |
| 438 | // This is an ARM branch relocation, need to use a stub function. |
| 439 | DEBUG(dbgs() << "\t\tThis is an ARM branch relocation."); |
| 440 | SectionEntry &Section = Sections[Rel.SectionID]; |
| 441 | uint8_t *Target = Section.Address + Rel.Offset; |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 442 | |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 443 | // Look up for existing stub. |
| 444 | StubMap::const_iterator i = Stubs.find(Value); |
| 445 | if (i != Stubs.end()) { |
Danil Malyshev | ab42733 | 2012-04-17 20:10:16 +0000 | [diff] [blame] | 446 | resolveRelocation(Target, (uint64_t)Target, (uint64_t)Section.Address + |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 447 | i->second, RelType, 0); |
| 448 | DEBUG(dbgs() << " Stub function found\n"); |
| 449 | } else { |
| 450 | // Create a new stub function. |
| 451 | DEBUG(dbgs() << " Create a new stub function\n"); |
| 452 | Stubs[Value] = Section.StubOffset; |
| 453 | uint8_t *StubTargetAddr = createStubFunction(Section.Address + |
| 454 | Section.StubOffset); |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 455 | RelocationEntry RE(Rel.SectionID, StubTargetAddr - Section.Address, |
| 456 | ELF::R_ARM_ABS32, Value.Addend); |
| 457 | if (Value.SymbolName) |
| 458 | addRelocationForSymbol(RE, Value.SymbolName); |
| 459 | else |
| 460 | addRelocationForSection(RE, Value.SectionID); |
| 461 | |
Danil Malyshev | ab42733 | 2012-04-17 20:10:16 +0000 | [diff] [blame] | 462 | resolveRelocation(Target, (uint64_t)Target, (uint64_t)Section.Address + |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 463 | Section.StubOffset, RelType, 0); |
| 464 | Section.StubOffset += getMaxStubSize(); |
| 465 | } |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 466 | } else if (Arch == Triple::mipsel && RelType == ELF::R_MIPS_26) { |
| 467 | // This is an Mips branch relocation, need to use a stub function. |
| 468 | DEBUG(dbgs() << "\t\tThis is a Mips branch relocation."); |
| 469 | SectionEntry &Section = Sections[Rel.SectionID]; |
| 470 | uint8_t *Target = Section.Address + Rel.Offset; |
| 471 | uint32_t *TargetAddress = (uint32_t *)Target; |
| 472 | |
| 473 | // Extract the addend from the instruction. |
| 474 | uint32_t Addend = ((*TargetAddress) & 0x03ffffff) << 2; |
| 475 | |
| 476 | Value.Addend += Addend; |
| 477 | |
| 478 | // Look up for existing stub. |
| 479 | StubMap::const_iterator i = Stubs.find(Value); |
| 480 | if (i != Stubs.end()) { |
| 481 | resolveRelocation(Target, (uint64_t)Target, |
| 482 | (uint64_t)Section.Address + |
| 483 | i->second, RelType, 0); |
| 484 | DEBUG(dbgs() << " Stub function found\n"); |
| 485 | } else { |
| 486 | // Create a new stub function. |
| 487 | DEBUG(dbgs() << " Create a new stub function\n"); |
| 488 | Stubs[Value] = Section.StubOffset; |
| 489 | uint8_t *StubTargetAddr = createStubFunction(Section.Address + |
| 490 | Section.StubOffset); |
| 491 | |
| 492 | // Creating Hi and Lo relocations for the filled stub instructions. |
| 493 | RelocationEntry REHi(Rel.SectionID, |
| 494 | StubTargetAddr - Section.Address, |
| 495 | ELF::R_MIPS_HI16, Value.Addend); |
| 496 | RelocationEntry RELo(Rel.SectionID, |
| 497 | StubTargetAddr - Section.Address + 4, |
| 498 | ELF::R_MIPS_LO16, Value.Addend); |
| 499 | |
| 500 | if (Value.SymbolName) { |
| 501 | addRelocationForSymbol(REHi, Value.SymbolName); |
| 502 | addRelocationForSymbol(RELo, Value.SymbolName); |
| 503 | } else { |
| 504 | addRelocationForSection(REHi, Value.SectionID); |
| 505 | addRelocationForSection(RELo, Value.SectionID); |
| 506 | } |
| 507 | |
| 508 | resolveRelocation(Target, (uint64_t)Target, |
| 509 | (uint64_t)Section.Address + |
| 510 | Section.StubOffset, RelType, 0); |
| 511 | Section.StubOffset += getMaxStubSize(); |
| 512 | } |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 513 | } else { |
| 514 | RelocationEntry RE(Rel.SectionID, Rel.Offset, RelType, Value.Addend); |
| 515 | if (Value.SymbolName) |
| 516 | addRelocationForSymbol(RE, Value.SymbolName); |
| 517 | else |
| 518 | addRelocationForSection(RE, Value.SectionID); |
| 519 | } |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 522 | bool RuntimeDyldELF::isCompatibleFormat(const ObjectBuffer *Buffer) const { |
| 523 | if (Buffer->getBufferSize() < strlen(ELF::ElfMagic)) |
| 524 | return false; |
| 525 | return (memcmp(Buffer->getBufferStart(), ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0; |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 526 | } |
| 527 | } // namespace llvm |