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