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" |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 25 | #include "llvm/Object/ELF.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 | |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 41 | template<support::endianness target_endianness, bool is64Bits> |
| 42 | class DyldELFObject : public ELFObjectFile<target_endianness, is64Bits> { |
| 43 | LLVM_ELF_IMPORT_TYPES(target_endianness, is64Bits) |
| 44 | |
| 45 | typedef Elf_Shdr_Impl<target_endianness, is64Bits> Elf_Shdr; |
| 46 | typedef Elf_Sym_Impl<target_endianness, is64Bits> Elf_Sym; |
| 47 | typedef Elf_Rel_Impl<target_endianness, is64Bits, false> Elf_Rel; |
| 48 | typedef Elf_Rel_Impl<target_endianness, is64Bits, true> Elf_Rela; |
| 49 | |
Michael J. Spencer | 2c38a66 | 2012-09-10 19:04:02 +0000 | [diff] [blame] | 50 | typedef Elf_Ehdr_Impl<target_endianness, is64Bits> Elf_Ehdr; |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 51 | |
| 52 | typedef typename ELFDataTypeTypedefHelper< |
| 53 | target_endianness, is64Bits>::value_type addr_type; |
| 54 | |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 55 | public: |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 56 | DyldELFObject(MemoryBuffer *Wrapper, error_code &ec); |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 57 | |
| 58 | void updateSectionAddress(const SectionRef &Sec, uint64_t Addr); |
| 59 | void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr); |
| 60 | |
Andrew Kaylor | 2e31987 | 2012-07-27 17:52:42 +0000 | [diff] [blame] | 61 | // Methods for type inquiry through isa, cast and dyn_cast |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 62 | static inline bool classof(const Binary *v) { |
| 63 | return (isa<ELFObjectFile<target_endianness, is64Bits> >(v) |
| 64 | && classof(cast<ELFObjectFile<target_endianness, is64Bits> >(v))); |
| 65 | } |
| 66 | static inline bool classof( |
| 67 | const ELFObjectFile<target_endianness, is64Bits> *v) { |
| 68 | return v->isDyldType(); |
| 69 | } |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | template<support::endianness target_endianness, bool is64Bits> |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 73 | class ELFObjectImage : public ObjectImageCommon { |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 74 | protected: |
| 75 | DyldELFObject<target_endianness, is64Bits> *DyldObj; |
| 76 | bool Registered; |
| 77 | |
| 78 | public: |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 79 | ELFObjectImage(ObjectBuffer *Input, |
| 80 | DyldELFObject<target_endianness, is64Bits> *Obj) |
| 81 | : ObjectImageCommon(Input, Obj), |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 82 | DyldObj(Obj), |
| 83 | Registered(false) {} |
| 84 | |
| 85 | virtual ~ELFObjectImage() { |
| 86 | if (Registered) |
| 87 | deregisterWithDebugger(); |
| 88 | } |
| 89 | |
| 90 | // Subclasses can override these methods to update the image with loaded |
| 91 | // addresses for sections and common symbols |
| 92 | virtual void updateSectionAddress(const SectionRef &Sec, uint64_t Addr) |
| 93 | { |
| 94 | DyldObj->updateSectionAddress(Sec, Addr); |
| 95 | } |
| 96 | |
| 97 | virtual void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr) |
| 98 | { |
| 99 | DyldObj->updateSymbolAddress(Sym, Addr); |
| 100 | } |
| 101 | |
| 102 | virtual void registerWithDebugger() |
| 103 | { |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 104 | JITRegistrar::getGDBRegistrar().registerObject(*Buffer); |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 105 | Registered = true; |
| 106 | } |
| 107 | virtual void deregisterWithDebugger() |
| 108 | { |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 109 | JITRegistrar::getGDBRegistrar().deregisterObject(*Buffer); |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 110 | } |
| 111 | }; |
| 112 | |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 113 | // The MemoryBuffer passed into this constructor is just a wrapper around the |
| 114 | // actual memory. Ultimately, the Binary parent class will take ownership of |
| 115 | // this MemoryBuffer object but not the underlying memory. |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 116 | template<support::endianness target_endianness, bool is64Bits> |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 117 | DyldELFObject<target_endianness, is64Bits>::DyldELFObject(MemoryBuffer *Wrapper, |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 118 | error_code &ec) |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 119 | : ELFObjectFile<target_endianness, is64Bits>(Wrapper, ec) { |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 120 | this->isDyldELFObject = true; |
| 121 | } |
| 122 | |
| 123 | template<support::endianness target_endianness, bool is64Bits> |
| 124 | void DyldELFObject<target_endianness, is64Bits>::updateSectionAddress( |
| 125 | const SectionRef &Sec, |
| 126 | uint64_t Addr) { |
| 127 | DataRefImpl ShdrRef = Sec.getRawDataRefImpl(); |
| 128 | Elf_Shdr *shdr = const_cast<Elf_Shdr*>( |
| 129 | reinterpret_cast<const Elf_Shdr *>(ShdrRef.p)); |
| 130 | |
| 131 | // This assumes the address passed in matches the target address bitness |
| 132 | // The template-based type cast handles everything else. |
| 133 | shdr->sh_addr = static_cast<addr_type>(Addr); |
| 134 | } |
| 135 | |
| 136 | template<support::endianness target_endianness, bool is64Bits> |
| 137 | void DyldELFObject<target_endianness, is64Bits>::updateSymbolAddress( |
| 138 | const SymbolRef &SymRef, |
| 139 | uint64_t Addr) { |
| 140 | |
| 141 | Elf_Sym *sym = const_cast<Elf_Sym*>( |
| 142 | ELFObjectFile<target_endianness, is64Bits>:: |
| 143 | getSymbol(SymRef.getRawDataRefImpl())); |
| 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 | |
| 152 | |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 153 | namespace llvm { |
| 154 | |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 155 | ObjectImage *RuntimeDyldELF::createObjectImage(ObjectBuffer *Buffer) { |
| 156 | if (Buffer->getBufferSize() < ELF::EI_NIDENT) |
| 157 | llvm_unreachable("Unexpected ELF object size"); |
| 158 | std::pair<unsigned char, unsigned char> Ident = std::make_pair( |
| 159 | (uint8_t)Buffer->getBufferStart()[ELF::EI_CLASS], |
| 160 | (uint8_t)Buffer->getBufferStart()[ELF::EI_DATA]); |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 161 | error_code ec; |
| 162 | |
| 163 | if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB) { |
| 164 | DyldELFObject<support::little, false> *Obj = |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 165 | new DyldELFObject<support::little, false>(Buffer->getMemBuffer(), ec); |
| 166 | return new ELFObjectImage<support::little, false>(Buffer, Obj); |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 167 | } |
| 168 | else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB) { |
| 169 | DyldELFObject<support::big, false> *Obj = |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 170 | new DyldELFObject<support::big, false>(Buffer->getMemBuffer(), ec); |
| 171 | return new ELFObjectImage<support::big, false>(Buffer, Obj); |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 172 | } |
| 173 | else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB) { |
| 174 | DyldELFObject<support::big, true> *Obj = |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 175 | new DyldELFObject<support::big, true>(Buffer->getMemBuffer(), ec); |
| 176 | return new ELFObjectImage<support::big, true>(Buffer, Obj); |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 177 | } |
| 178 | else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) { |
| 179 | DyldELFObject<support::little, true> *Obj = |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 180 | new DyldELFObject<support::little, true>(Buffer->getMemBuffer(), ec); |
| 181 | return new ELFObjectImage<support::little, true>(Buffer, Obj); |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 182 | } |
| 183 | else |
| 184 | llvm_unreachable("Unexpected ELF format"); |
| 185 | } |
| 186 | |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 187 | RuntimeDyldELF::~RuntimeDyldELF() { |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 188 | } |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 189 | |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 190 | void RuntimeDyldELF::resolveX86_64Relocation(const SectionEntry &Section, |
| 191 | uint64_t Offset, |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 192 | uint64_t Value, |
| 193 | uint32_t Type, |
| 194 | int64_t Addend) { |
| 195 | switch (Type) { |
| 196 | default: |
| 197 | llvm_unreachable("Relocation type not implemented yet!"); |
| 198 | break; |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 199 | case ELF::R_X86_64_64: { |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 200 | uint64_t *Target = reinterpret_cast<uint64_t*>(Section.Address + Offset); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 201 | *Target = Value + Addend; |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 202 | DEBUG(dbgs() << "Writing " << format("%p", (Value + Addend)) |
| 203 | << " at " << format("%p\n",Target)); |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 204 | break; |
| 205 | } |
| 206 | case ELF::R_X86_64_32: |
| 207 | case ELF::R_X86_64_32S: { |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 208 | Value += Addend; |
Andrew Kaylor | d83a547 | 2012-07-27 20:30:12 +0000 | [diff] [blame] | 209 | assert((Type == ELF::R_X86_64_32 && (Value <= UINT32_MAX)) || |
| 210 | (Type == ELF::R_X86_64_32S && |
| 211 | ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN))); |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 212 | uint32_t TruncatedAddr = (Value & 0xFFFFFFFF); |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 213 | uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset); |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 214 | *Target = TruncatedAddr; |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 215 | DEBUG(dbgs() << "Writing " << format("%p", TruncatedAddr) |
| 216 | << " at " << format("%p\n",Target)); |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 217 | break; |
| 218 | } |
| 219 | case ELF::R_X86_64_PC32: { |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 220 | // Get the placeholder value from the generated object since |
| 221 | // a previous relocation attempt may have overwritten the loaded version |
| 222 | uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress |
| 223 | + Offset); |
| 224 | uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset); |
| 225 | uint64_t FinalAddress = Section.LoadAddress + Offset; |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 226 | int64_t RealOffset = *Placeholder + Value + Addend - FinalAddress; |
Andrew Kaylor | d83a547 | 2012-07-27 20:30:12 +0000 | [diff] [blame] | 227 | assert(RealOffset <= INT32_MAX && RealOffset >= INT32_MIN); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 228 | int32_t TruncOffset = (RealOffset & 0xFFFFFFFF); |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 229 | *Target = TruncOffset; |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 230 | break; |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 235 | void RuntimeDyldELF::resolveX86Relocation(const SectionEntry &Section, |
| 236 | uint64_t Offset, |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 237 | uint32_t Value, |
| 238 | uint32_t Type, |
| 239 | int32_t Addend) { |
| 240 | switch (Type) { |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 241 | case ELF::R_386_32: { |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 242 | // Get the placeholder value from the generated object since |
| 243 | // a previous relocation attempt may have overwritten the loaded version |
| 244 | uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress |
| 245 | + Offset); |
| 246 | uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset); |
| 247 | *Target = *Placeholder + Value + Addend; |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 248 | break; |
| 249 | } |
| 250 | case ELF::R_386_PC32: { |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 251 | // Get the placeholder value from the generated object since |
| 252 | // a previous relocation attempt may have overwritten the loaded version |
| 253 | uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress |
| 254 | + Offset); |
| 255 | uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset); |
| 256 | uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 257 | uint32_t RealOffset = *Placeholder + Value + Addend - FinalAddress; |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 258 | *Target = RealOffset; |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 259 | break; |
| 260 | } |
| 261 | default: |
| 262 | // There are other relocation types, but it appears these are the |
Andrew Kaylor | e2e73bd | 2012-07-27 18:39:47 +0000 | [diff] [blame] | 263 | // only ones currently used by the LLVM ELF object writer |
Craig Topper | 8581438 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 264 | llvm_unreachable("Relocation type not implemented yet!"); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 265 | break; |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 269 | void RuntimeDyldELF::resolveARMRelocation(const SectionEntry &Section, |
| 270 | uint64_t Offset, |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 271 | uint32_t Value, |
| 272 | uint32_t Type, |
| 273 | int32_t Addend) { |
| 274 | // TODO: Add Thumb relocations. |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 275 | uint32_t* TargetPtr = (uint32_t*)(Section.Address + Offset); |
| 276 | uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 277 | Value += Addend; |
| 278 | |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 279 | DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: " |
| 280 | << Section.Address + Offset |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 281 | << " FinalAddress: " << format("%p",FinalAddress) |
| 282 | << " Value: " << format("%x",Value) |
| 283 | << " Type: " << format("%x",Type) |
| 284 | << " Addend: " << format("%x",Addend) |
| 285 | << "\n"); |
| 286 | |
| 287 | switch(Type) { |
| 288 | default: |
| 289 | llvm_unreachable("Not implemented relocation type!"); |
| 290 | |
Tim Northover | 565ebde | 2012-10-03 16:29:42 +0000 | [diff] [blame] | 291 | // Write a 32bit value to relocation address, taking into account the |
| 292 | // implicit addend encoded in the target. |
Amara Emerson | 098d6d5 | 2012-11-16 11:11:59 +0000 | [diff] [blame] | 293 | case ELF::R_ARM_TARGET1 : |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 294 | case ELF::R_ARM_ABS32 : |
Tim Northover | 565ebde | 2012-10-03 16:29:42 +0000 | [diff] [blame] | 295 | *TargetPtr += Value; |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 296 | break; |
| 297 | |
| 298 | // Write first 16 bit of 32 bit value to the mov instruction. |
| 299 | // Last 4 bit should be shifted. |
| 300 | case ELF::R_ARM_MOVW_ABS_NC : |
Tim Northover | 565ebde | 2012-10-03 16:29:42 +0000 | [diff] [blame] | 301 | // We are not expecting any other addend in the relocation address. |
| 302 | // Using 0x000F0FFF because MOVW has its 16 bit immediate split into 2 |
| 303 | // non-contiguous fields. |
| 304 | assert((*TargetPtr & 0x000F0FFF) == 0); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 305 | Value = Value & 0xFFFF; |
| 306 | *TargetPtr |= Value & 0xFFF; |
| 307 | *TargetPtr |= ((Value >> 12) & 0xF) << 16; |
| 308 | break; |
| 309 | |
| 310 | // Write last 16 bit of 32 bit value to the mov instruction. |
| 311 | // Last 4 bit should be shifted. |
| 312 | case ELF::R_ARM_MOVT_ABS : |
Tim Northover | 565ebde | 2012-10-03 16:29:42 +0000 | [diff] [blame] | 313 | // We are not expecting any other addend in the relocation address. |
| 314 | // Use 0x000F0FFF for the same reason as R_ARM_MOVW_ABS_NC. |
| 315 | assert((*TargetPtr & 0x000F0FFF) == 0); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 316 | Value = (Value >> 16) & 0xFFFF; |
| 317 | *TargetPtr |= Value & 0xFFF; |
| 318 | *TargetPtr |= ((Value >> 12) & 0xF) << 16; |
| 319 | break; |
| 320 | |
| 321 | // Write 24 bit relative value to the branch instruction. |
| 322 | case ELF::R_ARM_PC24 : // Fall through. |
| 323 | case ELF::R_ARM_CALL : // Fall through. |
| 324 | case ELF::R_ARM_JUMP24 : |
| 325 | int32_t RelValue = static_cast<int32_t>(Value - FinalAddress - 8); |
| 326 | RelValue = (RelValue & 0x03FFFFFC) >> 2; |
| 327 | *TargetPtr &= 0xFF000000; |
| 328 | *TargetPtr |= RelValue; |
| 329 | break; |
| 330 | } |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 333 | void RuntimeDyldELF::resolveMIPSRelocation(const SectionEntry &Section, |
| 334 | uint64_t Offset, |
Akira Hatanaka | b862f09 | 2012-08-20 17:53:24 +0000 | [diff] [blame] | 335 | uint32_t Value, |
| 336 | uint32_t Type, |
| 337 | int32_t Addend) { |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 338 | uint32_t* TargetPtr = (uint32_t*)(Section.Address + Offset); |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 339 | Value += Addend; |
| 340 | |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 341 | DEBUG(dbgs() << "resolveMipselocation, LocalAddress: " |
| 342 | << Section.Address + Offset |
| 343 | << " FinalAddress: " |
| 344 | << format("%p",Section.LoadAddress + Offset) |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 345 | << " Value: " << format("%x",Value) |
| 346 | << " Type: " << format("%x",Type) |
| 347 | << " Addend: " << format("%x",Addend) |
| 348 | << "\n"); |
| 349 | |
| 350 | switch(Type) { |
| 351 | default: |
| 352 | llvm_unreachable("Not implemented relocation type!"); |
| 353 | break; |
| 354 | case ELF::R_MIPS_32: |
| 355 | *TargetPtr = Value + (*TargetPtr); |
| 356 | break; |
| 357 | case ELF::R_MIPS_26: |
| 358 | *TargetPtr = ((*TargetPtr) & 0xfc000000) | (( Value & 0x0fffffff) >> 2); |
| 359 | break; |
| 360 | case ELF::R_MIPS_HI16: |
| 361 | // Get the higher 16-bits. Also add 1 if bit 15 is 1. |
| 362 | Value += ((*TargetPtr) & 0x0000ffff) << 16; |
| 363 | *TargetPtr = ((*TargetPtr) & 0xffff0000) | |
| 364 | (((Value + 0x8000) >> 16) & 0xffff); |
| 365 | break; |
| 366 | case ELF::R_MIPS_LO16: |
| 367 | Value += ((*TargetPtr) & 0x0000ffff); |
| 368 | *TargetPtr = ((*TargetPtr) & 0xffff0000) | (Value & 0xffff); |
| 369 | break; |
| 370 | } |
| 371 | } |
| 372 | |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 373 | // Return the .TOC. section address to R_PPC64_TOC relocations. |
| 374 | uint64_t RuntimeDyldELF::findPPC64TOC() const { |
| 375 | // The TOC consists of sections .got, .toc, .tocbss, .plt in that |
| 376 | // order. The TOC starts where the first of these sections starts. |
| 377 | SectionList::const_iterator it = Sections.begin(); |
| 378 | SectionList::const_iterator ite = Sections.end(); |
| 379 | for (; it != ite; ++it) { |
| 380 | if (it->Name == ".got" || |
| 381 | it->Name == ".toc" || |
| 382 | it->Name == ".tocbss" || |
| 383 | it->Name == ".plt") |
| 384 | break; |
| 385 | } |
| 386 | if (it == ite) { |
| 387 | // This may happen for |
| 388 | // * references to TOC base base (sym@toc, .odp relocation) without |
| 389 | // a .toc directive. |
| 390 | // In this case just use the first section (which is usually |
| 391 | // the .odp) since the code won't reference the .toc base |
| 392 | // directly. |
| 393 | it = Sections.begin(); |
| 394 | } |
| 395 | assert (it != ite); |
| 396 | // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000 |
| 397 | // thus permitting a full 64 Kbytes segment. |
| 398 | return it->LoadAddress + 0x8000; |
| 399 | } |
| 400 | |
| 401 | // Returns the sections and offset associated with the ODP entry referenced |
| 402 | // by Symbol. |
| 403 | void RuntimeDyldELF::findOPDEntrySection(ObjectImage &Obj, |
| 404 | ObjSectionToIDMap &LocalSections, |
| 405 | RelocationValueRef &Rel) { |
| 406 | // Get the ELF symbol value (st_value) to compare with Relocation offset in |
| 407 | // .opd entries |
| 408 | |
| 409 | error_code err; |
| 410 | for (section_iterator si = Obj.begin_sections(), |
| 411 | se = Obj.end_sections(); si != se; si.increment(err)) { |
| 412 | StringRef SectionName; |
| 413 | check(si->getName(SectionName)); |
| 414 | if (SectionName != ".opd") |
| 415 | continue; |
| 416 | |
| 417 | for (relocation_iterator i = si->begin_relocations(), |
| 418 | e = si->end_relocations(); i != e;) { |
| 419 | check(err); |
| 420 | |
| 421 | // The R_PPC64_ADDR64 relocation indicates the first field |
| 422 | // of a .opd entry |
| 423 | uint64_t TypeFunc; |
| 424 | check(i->getType(TypeFunc)); |
| 425 | if (TypeFunc != ELF::R_PPC64_ADDR64) { |
| 426 | i.increment(err); |
| 427 | continue; |
| 428 | } |
| 429 | |
| 430 | SymbolRef TargetSymbol; |
| 431 | uint64_t TargetSymbolOffset; |
| 432 | int64_t TargetAdditionalInfo; |
| 433 | check(i->getSymbol(TargetSymbol)); |
| 434 | check(i->getOffset(TargetSymbolOffset)); |
| 435 | check(i->getAdditionalInfo(TargetAdditionalInfo)); |
| 436 | |
| 437 | i = i.increment(err); |
| 438 | if (i == e) |
| 439 | break; |
| 440 | check(err); |
| 441 | |
| 442 | // Just check if following relocation is a R_PPC64_TOC |
| 443 | uint64_t TypeTOC; |
| 444 | check(i->getType(TypeTOC)); |
| 445 | if (TypeTOC != ELF::R_PPC64_TOC) |
| 446 | continue; |
| 447 | |
| 448 | // Finally compares the Symbol value and the target symbol offset |
| 449 | // to check if this .opd entry refers to the symbol the relocation |
| 450 | // points to. |
| 451 | if (Rel.Addend != (intptr_t)TargetSymbolOffset) |
| 452 | continue; |
| 453 | |
| 454 | section_iterator tsi(Obj.end_sections()); |
| 455 | check(TargetSymbol.getSection(tsi)); |
| 456 | Rel.SectionID = findOrEmitSection(Obj, (*tsi), true, LocalSections); |
| 457 | Rel.Addend = (intptr_t)TargetAdditionalInfo; |
| 458 | return; |
| 459 | } |
| 460 | } |
| 461 | llvm_unreachable("Attempting to get address of ODP entry!"); |
| 462 | } |
| 463 | |
| 464 | // Relocation masks following the #lo(value), #hi(value), #higher(value), |
| 465 | // and #highest(value) macros defined in section 4.5.1. Relocation Types |
| 466 | // in PPC-elf64abi document. |
| 467 | // |
| 468 | static inline |
| 469 | uint16_t applyPPClo (uint64_t value) |
| 470 | { |
| 471 | return value & 0xffff; |
| 472 | } |
| 473 | |
| 474 | static inline |
| 475 | uint16_t applyPPChi (uint64_t value) |
| 476 | { |
| 477 | return (value >> 16) & 0xffff; |
| 478 | } |
| 479 | |
| 480 | static inline |
| 481 | uint16_t applyPPChigher (uint64_t value) |
| 482 | { |
| 483 | return (value >> 32) & 0xffff; |
| 484 | } |
| 485 | |
| 486 | static inline |
| 487 | uint16_t applyPPChighest (uint64_t value) |
| 488 | { |
| 489 | return (value >> 48) & 0xffff; |
| 490 | } |
| 491 | |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 492 | void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntry &Section, |
| 493 | uint64_t Offset, |
| 494 | uint64_t Value, |
| 495 | uint32_t Type, |
| 496 | int64_t Addend) { |
| 497 | uint8_t* LocalAddress = Section.Address + Offset; |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 498 | switch (Type) { |
| 499 | default: |
| 500 | llvm_unreachable("Relocation type not implemented yet!"); |
| 501 | break; |
| 502 | case ELF::R_PPC64_ADDR16_LO : |
| 503 | writeInt16BE(LocalAddress, applyPPClo (Value + Addend)); |
| 504 | break; |
| 505 | case ELF::R_PPC64_ADDR16_HI : |
| 506 | writeInt16BE(LocalAddress, applyPPChi (Value + Addend)); |
| 507 | break; |
| 508 | case ELF::R_PPC64_ADDR16_HIGHER : |
| 509 | writeInt16BE(LocalAddress, applyPPChigher (Value + Addend)); |
| 510 | break; |
| 511 | case ELF::R_PPC64_ADDR16_HIGHEST : |
| 512 | writeInt16BE(LocalAddress, applyPPChighest (Value + Addend)); |
| 513 | break; |
| 514 | case ELF::R_PPC64_ADDR14 : { |
| 515 | assert(((Value + Addend) & 3) == 0); |
| 516 | // Preserve the AA/LK bits in the branch instruction |
| 517 | uint8_t aalk = *(LocalAddress+3); |
| 518 | writeInt16BE(LocalAddress + 2, (aalk & 3) | ((Value + Addend) & 0xfffc)); |
| 519 | } break; |
Adhemerval Zanella | 7b44988 | 2013-01-04 19:08:13 +0000 | [diff] [blame^] | 520 | case ELF::R_PPC64_ADDR32 : { |
| 521 | int32_t Result = static_cast<int32_t>(Value + Addend); |
| 522 | if (SignExtend32<32>(Result) != Result) |
| 523 | llvm_unreachable("Relocation R_PPC64_REL32 overflow"); |
| 524 | writeInt32BE(LocalAddress, Result); |
| 525 | } break; |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 526 | case ELF::R_PPC64_REL24 : { |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 527 | uint64_t FinalAddress = (Section.LoadAddress + Offset); |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 528 | int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend); |
| 529 | if (SignExtend32<24>(delta) != delta) |
| 530 | llvm_unreachable("Relocation R_PPC64_REL24 overflow"); |
| 531 | // Generates a 'bl <address>' instruction |
| 532 | writeInt32BE(LocalAddress, 0x48000001 | (delta & 0x03FFFFFC)); |
| 533 | } break; |
| 534 | case ELF::R_PPC64_ADDR64 : |
| 535 | writeInt64BE(LocalAddress, Value + Addend); |
| 536 | break; |
| 537 | case ELF::R_PPC64_TOC : |
| 538 | writeInt64BE(LocalAddress, findPPC64TOC()); |
| 539 | break; |
| 540 | case ELF::R_PPC64_TOC16 : { |
| 541 | uint64_t TOCStart = findPPC64TOC(); |
| 542 | Value = applyPPClo((Value + Addend) - TOCStart); |
| 543 | writeInt16BE(LocalAddress, applyPPClo(Value)); |
| 544 | } break; |
| 545 | case ELF::R_PPC64_TOC16_DS : { |
| 546 | uint64_t TOCStart = findPPC64TOC(); |
| 547 | Value = ((Value + Addend) - TOCStart); |
| 548 | writeInt16BE(LocalAddress, applyPPClo(Value)); |
| 549 | } break; |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 554 | void RuntimeDyldELF::resolveRelocation(const SectionEntry &Section, |
| 555 | uint64_t Offset, |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 556 | uint64_t Value, |
| 557 | uint32_t Type, |
| 558 | int64_t Addend) { |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 559 | switch (Arch) { |
| 560 | case Triple::x86_64: |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 561 | resolveX86_64Relocation(Section, Offset, Value, Type, Addend); |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 562 | break; |
| 563 | case Triple::x86: |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 564 | resolveX86Relocation(Section, Offset, |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 565 | (uint32_t)(Value & 0xffffffffL), Type, |
| 566 | (uint32_t)(Addend & 0xffffffffL)); |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 567 | break; |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 568 | case Triple::arm: // Fall through. |
| 569 | case Triple::thumb: |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 570 | resolveARMRelocation(Section, Offset, |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 571 | (uint32_t)(Value & 0xffffffffL), Type, |
| 572 | (uint32_t)(Addend & 0xffffffffL)); |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 573 | break; |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 574 | case Triple::mips: // Fall through. |
| 575 | case Triple::mipsel: |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 576 | resolveMIPSRelocation(Section, Offset, |
Akira Hatanaka | b862f09 | 2012-08-20 17:53:24 +0000 | [diff] [blame] | 577 | (uint32_t)(Value & 0xffffffffL), Type, |
| 578 | (uint32_t)(Addend & 0xffffffffL)); |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 579 | break; |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 580 | case Triple::ppc64: |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 581 | resolvePPC64Relocation(Section, Offset, Value, Type, Addend); |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 582 | break; |
Craig Topper | 8581438 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 583 | default: llvm_unreachable("Unsupported CPU type!"); |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 584 | } |
| 585 | } |
| 586 | |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 587 | void RuntimeDyldELF::processRelocationRef(const ObjRelocationInfo &Rel, |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 588 | ObjectImage &Obj, |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 589 | ObjSectionToIDMap &ObjSectionToID, |
Eli Bendersky | d98c9e9 | 2012-05-01 06:58:59 +0000 | [diff] [blame] | 590 | const SymbolTableMap &Symbols, |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 591 | StubMap &Stubs) { |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 592 | |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 593 | uint32_t RelType = (uint32_t)(Rel.Type & 0xffffffffL); |
| 594 | intptr_t Addend = (intptr_t)Rel.AdditionalInfo; |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 595 | const SymbolRef &Symbol = Rel.Symbol; |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 596 | |
| 597 | // Obtain the symbol name which is referenced in the relocation |
| 598 | StringRef TargetName; |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 599 | Symbol.getName(TargetName); |
| 600 | DEBUG(dbgs() << "\t\tRelType: " << RelType |
| 601 | << " Addend: " << Addend |
| 602 | << " TargetName: " << TargetName |
| 603 | << "\n"); |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 604 | RelocationValueRef Value; |
| 605 | // First search for the symbol in the local symbol table |
Eli Bendersky | d98c9e9 | 2012-05-01 06:58:59 +0000 | [diff] [blame] | 606 | SymbolTableMap::const_iterator lsi = Symbols.find(TargetName.data()); |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 607 | SymbolRef::Type SymType; |
| 608 | Symbol.getType(SymType); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 609 | if (lsi != Symbols.end()) { |
| 610 | Value.SectionID = lsi->second.first; |
| 611 | Value.Addend = lsi->second.second; |
| 612 | } else { |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 613 | // Search for the symbol in the global symbol table |
Eli Bendersky | d98c9e9 | 2012-05-01 06:58:59 +0000 | [diff] [blame] | 614 | SymbolTableMap::const_iterator gsi = |
| 615 | GlobalSymbolTable.find(TargetName.data()); |
| 616 | if (gsi != GlobalSymbolTable.end()) { |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 617 | Value.SectionID = gsi->second.first; |
| 618 | Value.Addend = gsi->second.second; |
| 619 | } else { |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 620 | switch (SymType) { |
| 621 | case SymbolRef::ST_Debug: { |
| 622 | // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously |
| 623 | // and can be changed by another developers. Maybe best way is add |
| 624 | // a new symbol type ST_Section to SymbolRef and use it. |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 625 | section_iterator si(Obj.end_sections()); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 626 | Symbol.getSection(si); |
| 627 | if (si == Obj.end_sections()) |
| 628 | llvm_unreachable("Symbol section not found, bad object file format!"); |
| 629 | DEBUG(dbgs() << "\t\tThis is section symbol\n"); |
Andrew Kaylor | fa8cd9d | 2012-10-12 23:53:16 +0000 | [diff] [blame] | 630 | // Default to 'true' in case isText fails (though it never does). |
| 631 | bool isCode = true; |
| 632 | si->isText(isCode); |
| 633 | Value.SectionID = findOrEmitSection(Obj, |
| 634 | (*si), |
| 635 | isCode, |
| 636 | ObjSectionToID); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 637 | Value.Addend = Addend; |
| 638 | break; |
| 639 | } |
| 640 | case SymbolRef::ST_Unknown: { |
| 641 | Value.SymbolName = TargetName.data(); |
| 642 | Value.Addend = Addend; |
| 643 | break; |
| 644 | } |
| 645 | default: |
| 646 | llvm_unreachable("Unresolved symbol type!"); |
| 647 | break; |
| 648 | } |
| 649 | } |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 650 | } |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 651 | DEBUG(dbgs() << "\t\tRel.SectionID: " << Rel.SectionID |
| 652 | << " Rel.Offset: " << Rel.Offset |
| 653 | << "\n"); |
| 654 | if (Arch == Triple::arm && |
| 655 | (RelType == ELF::R_ARM_PC24 || |
| 656 | RelType == ELF::R_ARM_CALL || |
| 657 | RelType == ELF::R_ARM_JUMP24)) { |
| 658 | // This is an ARM branch relocation, need to use a stub function. |
| 659 | DEBUG(dbgs() << "\t\tThis is an ARM branch relocation."); |
| 660 | SectionEntry &Section = Sections[Rel.SectionID]; |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 661 | |
Eric Christopher | bf261f1 | 2012-10-23 17:19:15 +0000 | [diff] [blame] | 662 | // Look for an existing stub. |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 663 | StubMap::const_iterator i = Stubs.find(Value); |
| 664 | if (i != Stubs.end()) { |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 665 | resolveRelocation(Section, Rel.Offset, |
| 666 | (uint64_t)Section.Address + i->second, RelType, 0); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 667 | DEBUG(dbgs() << " Stub function found\n"); |
| 668 | } else { |
| 669 | // Create a new stub function. |
| 670 | DEBUG(dbgs() << " Create a new stub function\n"); |
| 671 | Stubs[Value] = Section.StubOffset; |
| 672 | uint8_t *StubTargetAddr = createStubFunction(Section.Address + |
| 673 | Section.StubOffset); |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 674 | RelocationEntry RE(Rel.SectionID, StubTargetAddr - Section.Address, |
| 675 | ELF::R_ARM_ABS32, Value.Addend); |
| 676 | if (Value.SymbolName) |
| 677 | addRelocationForSymbol(RE, Value.SymbolName); |
| 678 | else |
| 679 | addRelocationForSection(RE, Value.SectionID); |
| 680 | |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 681 | resolveRelocation(Section, Rel.Offset, |
| 682 | (uint64_t)Section.Address + Section.StubOffset, |
| 683 | RelType, 0); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 684 | Section.StubOffset += getMaxStubSize(); |
| 685 | } |
Akira Hatanaka | ade0474 | 2012-12-03 23:12:19 +0000 | [diff] [blame] | 686 | } else if ((Arch == Triple::mipsel || Arch == Triple::mips) && |
| 687 | RelType == ELF::R_MIPS_26) { |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 688 | // This is an Mips branch relocation, need to use a stub function. |
| 689 | DEBUG(dbgs() << "\t\tThis is a Mips branch relocation."); |
| 690 | SectionEntry &Section = Sections[Rel.SectionID]; |
| 691 | uint8_t *Target = Section.Address + Rel.Offset; |
| 692 | uint32_t *TargetAddress = (uint32_t *)Target; |
| 693 | |
| 694 | // Extract the addend from the instruction. |
| 695 | uint32_t Addend = ((*TargetAddress) & 0x03ffffff) << 2; |
| 696 | |
| 697 | Value.Addend += Addend; |
| 698 | |
| 699 | // Look up for existing stub. |
| 700 | StubMap::const_iterator i = Stubs.find(Value); |
| 701 | if (i != Stubs.end()) { |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 702 | resolveRelocation(Section, Rel.Offset, |
| 703 | (uint64_t)Section.Address + i->second, RelType, 0); |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 704 | DEBUG(dbgs() << " Stub function found\n"); |
| 705 | } else { |
| 706 | // Create a new stub function. |
| 707 | DEBUG(dbgs() << " Create a new stub function\n"); |
| 708 | Stubs[Value] = Section.StubOffset; |
| 709 | uint8_t *StubTargetAddr = createStubFunction(Section.Address + |
| 710 | Section.StubOffset); |
| 711 | |
| 712 | // Creating Hi and Lo relocations for the filled stub instructions. |
| 713 | RelocationEntry REHi(Rel.SectionID, |
| 714 | StubTargetAddr - Section.Address, |
| 715 | ELF::R_MIPS_HI16, Value.Addend); |
| 716 | RelocationEntry RELo(Rel.SectionID, |
| 717 | StubTargetAddr - Section.Address + 4, |
| 718 | ELF::R_MIPS_LO16, Value.Addend); |
| 719 | |
| 720 | if (Value.SymbolName) { |
| 721 | addRelocationForSymbol(REHi, Value.SymbolName); |
| 722 | addRelocationForSymbol(RELo, Value.SymbolName); |
| 723 | } else { |
| 724 | addRelocationForSection(REHi, Value.SectionID); |
| 725 | addRelocationForSection(RELo, Value.SectionID); |
| 726 | } |
| 727 | |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 728 | resolveRelocation(Section, Rel.Offset, |
| 729 | (uint64_t)Section.Address + Section.StubOffset, |
| 730 | RelType, 0); |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 731 | Section.StubOffset += getMaxStubSize(); |
| 732 | } |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 733 | } else if (Arch == Triple::ppc64) { |
| 734 | if (RelType == ELF::R_PPC64_REL24) { |
| 735 | // A PPC branch relocation will need a stub function if the target is |
| 736 | // an external symbol (Symbol::ST_Unknown) or if the target address |
| 737 | // is not within the signed 24-bits branch address. |
| 738 | SectionEntry &Section = Sections[Rel.SectionID]; |
| 739 | uint8_t *Target = Section.Address + Rel.Offset; |
| 740 | bool RangeOverflow = false; |
| 741 | if (SymType != SymbolRef::ST_Unknown) { |
| 742 | // A function call may points to the .opd entry, so the final symbol value |
| 743 | // in calculated based in the relocation values in .opd section. |
| 744 | findOPDEntrySection(Obj, ObjSectionToID, Value); |
| 745 | uint8_t *RelocTarget = Sections[Value.SectionID].Address + Value.Addend; |
| 746 | int32_t delta = static_cast<int32_t>(Target - RelocTarget); |
| 747 | // If it is within 24-bits branch range, just set the branch target |
| 748 | if (SignExtend32<24>(delta) == delta) { |
| 749 | RelocationEntry RE(Rel.SectionID, Rel.Offset, RelType, Value.Addend); |
| 750 | if (Value.SymbolName) |
| 751 | addRelocationForSymbol(RE, Value.SymbolName); |
| 752 | else |
| 753 | addRelocationForSection(RE, Value.SectionID); |
| 754 | } else { |
| 755 | RangeOverflow = true; |
| 756 | } |
| 757 | } |
| 758 | if (SymType == SymbolRef::ST_Unknown || RangeOverflow == true) { |
| 759 | // It is an external symbol (SymbolRef::ST_Unknown) or within a range |
| 760 | // larger than 24-bits. |
| 761 | StubMap::const_iterator i = Stubs.find(Value); |
| 762 | if (i != Stubs.end()) { |
| 763 | // Symbol function stub already created, just relocate to it |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 764 | resolveRelocation(Section, Rel.Offset, |
| 765 | (uint64_t)Section.Address + i->second, RelType, 0); |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 766 | DEBUG(dbgs() << " Stub function found\n"); |
| 767 | } else { |
| 768 | // Create a new stub function. |
| 769 | DEBUG(dbgs() << " Create a new stub function\n"); |
| 770 | Stubs[Value] = Section.StubOffset; |
| 771 | uint8_t *StubTargetAddr = createStubFunction(Section.Address + |
| 772 | Section.StubOffset); |
| 773 | RelocationEntry RE(Rel.SectionID, StubTargetAddr - Section.Address, |
| 774 | ELF::R_PPC64_ADDR64, Value.Addend); |
| 775 | |
| 776 | // Generates the 64-bits address loads as exemplified in section |
| 777 | // 4.5.1 in PPC64 ELF ABI. |
| 778 | RelocationEntry REhst(Rel.SectionID, |
| 779 | StubTargetAddr - Section.Address + 2, |
| 780 | ELF::R_PPC64_ADDR16_HIGHEST, Value.Addend); |
| 781 | RelocationEntry REhr(Rel.SectionID, |
| 782 | StubTargetAddr - Section.Address + 6, |
| 783 | ELF::R_PPC64_ADDR16_HIGHER, Value.Addend); |
| 784 | RelocationEntry REh(Rel.SectionID, |
| 785 | StubTargetAddr - Section.Address + 14, |
| 786 | ELF::R_PPC64_ADDR16_HI, Value.Addend); |
| 787 | RelocationEntry REl(Rel.SectionID, |
| 788 | StubTargetAddr - Section.Address + 18, |
| 789 | ELF::R_PPC64_ADDR16_LO, Value.Addend); |
| 790 | |
| 791 | if (Value.SymbolName) { |
| 792 | addRelocationForSymbol(REhst, Value.SymbolName); |
| 793 | addRelocationForSymbol(REhr, Value.SymbolName); |
| 794 | addRelocationForSymbol(REh, Value.SymbolName); |
| 795 | addRelocationForSymbol(REl, Value.SymbolName); |
| 796 | } else { |
| 797 | addRelocationForSection(REhst, Value.SectionID); |
| 798 | addRelocationForSection(REhr, Value.SectionID); |
| 799 | addRelocationForSection(REh, Value.SectionID); |
| 800 | addRelocationForSection(REl, Value.SectionID); |
| 801 | } |
| 802 | |
Andrew Kaylor | a307a1c | 2012-11-02 19:45:23 +0000 | [diff] [blame] | 803 | resolveRelocation(Section, Rel.Offset, |
| 804 | (uint64_t)Section.Address + Section.StubOffset, |
| 805 | RelType, 0); |
Adhemerval Zanella | 6e8946c | 2012-10-25 13:13:48 +0000 | [diff] [blame] | 806 | if (SymType == SymbolRef::ST_Unknown) |
| 807 | // Restore the TOC for external calls |
| 808 | writeInt32BE(Target+4, 0xE8410028); // ld r2,40(r1) |
| 809 | Section.StubOffset += getMaxStubSize(); |
| 810 | } |
| 811 | } |
| 812 | } else { |
| 813 | RelocationEntry RE(Rel.SectionID, Rel.Offset, RelType, Value.Addend); |
| 814 | // Extra check to avoid relocation againt empty symbols (usually |
| 815 | // the R_PPC64_TOC). |
| 816 | if (Value.SymbolName && !TargetName.empty()) |
| 817 | addRelocationForSymbol(RE, Value.SymbolName); |
| 818 | else |
| 819 | addRelocationForSection(RE, Value.SectionID); |
| 820 | } |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 821 | } else { |
| 822 | RelocationEntry RE(Rel.SectionID, Rel.Offset, RelType, Value.Addend); |
| 823 | if (Value.SymbolName) |
| 824 | addRelocationForSymbol(RE, Value.SymbolName); |
| 825 | else |
| 826 | addRelocationForSection(RE, Value.SectionID); |
| 827 | } |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 828 | } |
| 829 | |
Tim Northover | f00677d | 2012-10-29 10:47:04 +0000 | [diff] [blame] | 830 | unsigned RuntimeDyldELF::getCommonSymbolAlignment(const SymbolRef &Sym) { |
| 831 | // In ELF, the value of an SHN_COMMON symbol is its alignment requirement. |
| 832 | uint64_t Align; |
| 833 | Check(Sym.getValue(Align)); |
| 834 | return Align; |
| 835 | } |
| 836 | |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 837 | bool RuntimeDyldELF::isCompatibleFormat(const ObjectBuffer *Buffer) const { |
| 838 | if (Buffer->getBufferSize() < strlen(ELF::ElfMagic)) |
| 839 | return false; |
| 840 | return (memcmp(Buffer->getBufferStart(), ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0; |
Eli Bendersky | a66a185 | 2012-01-16 08:56:09 +0000 | [diff] [blame] | 841 | } |
| 842 | } // namespace llvm |