blob: 0a68f4e654270e3ac872c33399219cec1b8fab8e [file] [log] [blame]
Jim Grosbache0934be2012-01-16 23:50:58 +00001//===-- RuntimeDyldELF.cpp - Run-time dynamic linker for MC-JIT -*- C++ -*-===//
Eli Benderskya66a1852012-01-16 08:56:09 +00002//
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 Kaylor3f23cef2012-10-02 21:18:39 +000015#include "RuntimeDyldELF.h"
16#include "JITRegistrar.h"
17#include "ObjectImageCommon.h"
Eli Benderskya66a1852012-01-16 08:56:09 +000018#include "llvm/ADT/IntervalMap.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000019#include "llvm/ADT/OwningPtr.h"
20#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/StringRef.h"
Eli Benderskya66a1852012-01-16 08:56:09 +000022#include "llvm/ADT/Triple.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000023#include "llvm/ExecutionEngine/ObjectBuffer.h"
24#include "llvm/ExecutionEngine/ObjectImage.h"
Preston Gurd689ff9c2012-04-16 22:12:58 +000025#include "llvm/Object/ELF.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000026#include "llvm/Object/ObjectFile.h"
27#include "llvm/Support/ELF.h"
Eli Benderskya66a1852012-01-16 08:56:09 +000028using namespace llvm;
29using namespace llvm::object;
30
Michael J. Spencer4d9c5392013-01-04 20:36:28 +000031using support::endianness;
32
Preston Gurd689ff9c2012-04-16 22:12:58 +000033namespace {
34
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +000035static inline
36error_code check(error_code Err) {
37 if (Err) {
38 report_fatal_error(Err.message());
39 }
40 return Err;
41}
42
Michael J. Spencer4d9c5392013-01-04 20:36:28 +000043template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
44class DyldELFObject
45 : public ELFObjectFile<target_endianness, max_alignment, is64Bits> {
46 LLVM_ELF_IMPORT_TYPES(target_endianness, max_alignment, is64Bits)
Preston Gurd689ff9c2012-04-16 22:12:58 +000047
Michael J. Spencer4d9c5392013-01-04 20:36:28 +000048 typedef Elf_Shdr_Impl<target_endianness, max_alignment, is64Bits> Elf_Shdr;
49 typedef Elf_Sym_Impl<target_endianness, max_alignment, is64Bits> Elf_Sym;
50 typedef
51 Elf_Rel_Impl<target_endianness, max_alignment, is64Bits, false> Elf_Rel;
52 typedef
53 Elf_Rel_Impl<target_endianness, max_alignment, is64Bits, true> Elf_Rela;
Preston Gurd689ff9c2012-04-16 22:12:58 +000054
Michael J. Spencer4d9c5392013-01-04 20:36:28 +000055 typedef Elf_Ehdr_Impl<target_endianness, max_alignment, is64Bits> Elf_Ehdr;
Preston Gurd689ff9c2012-04-16 22:12:58 +000056
57 typedef typename ELFDataTypeTypedefHelper<
Michael J. Spencer4d9c5392013-01-04 20:36:28 +000058 target_endianness, max_alignment, is64Bits>::value_type addr_type;
Preston Gurd689ff9c2012-04-16 22:12:58 +000059
Preston Gurd689ff9c2012-04-16 22:12:58 +000060public:
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000061 DyldELFObject(MemoryBuffer *Wrapper, error_code &ec);
Preston Gurd689ff9c2012-04-16 22:12:58 +000062
63 void updateSectionAddress(const SectionRef &Sec, uint64_t Addr);
64 void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr);
65
Andrew Kaylor2e319872012-07-27 17:52:42 +000066 // Methods for type inquiry through isa, cast and dyn_cast
Preston Gurd689ff9c2012-04-16 22:12:58 +000067 static inline bool classof(const Binary *v) {
Michael J. Spencer4d9c5392013-01-04 20:36:28 +000068 return (isa<ELFObjectFile<target_endianness, max_alignment, is64Bits> >(v)
69 && classof(cast<ELFObjectFile
70 <target_endianness, max_alignment, is64Bits> >(v)));
Preston Gurd689ff9c2012-04-16 22:12:58 +000071 }
72 static inline bool classof(
Michael J. Spencer4d9c5392013-01-04 20:36:28 +000073 const ELFObjectFile<target_endianness, max_alignment, is64Bits> *v) {
Preston Gurd689ff9c2012-04-16 22:12:58 +000074 return v->isDyldType();
75 }
Preston Gurd689ff9c2012-04-16 22:12:58 +000076};
77
Michael J. Spencer4d9c5392013-01-04 20:36:28 +000078template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000079class ELFObjectImage : public ObjectImageCommon {
Preston Gurd689ff9c2012-04-16 22:12:58 +000080 protected:
Michael J. Spencer4d9c5392013-01-04 20:36:28 +000081 DyldELFObject<target_endianness, max_alignment, is64Bits> *DyldObj;
Preston Gurd689ff9c2012-04-16 22:12:58 +000082 bool Registered;
83
84 public:
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000085 ELFObjectImage(ObjectBuffer *Input,
Michael J. Spencer4d9c5392013-01-04 20:36:28 +000086 DyldELFObject<target_endianness, max_alignment, is64Bits> *Obj)
Andrew Kaylor3f23cef2012-10-02 21:18:39 +000087 : ObjectImageCommon(Input, Obj),
Preston Gurd689ff9c2012-04-16 22:12:58 +000088 DyldObj(Obj),
89 Registered(false) {}
90
91 virtual ~ELFObjectImage() {
92 if (Registered)
93 deregisterWithDebugger();
94 }
95
96 // Subclasses can override these methods to update the image with loaded
97 // addresses for sections and common symbols
98 virtual void updateSectionAddress(const SectionRef &Sec, uint64_t Addr)
99 {
100 DyldObj->updateSectionAddress(Sec, Addr);
101 }
102
103 virtual void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr)
104 {
105 DyldObj->updateSymbolAddress(Sym, Addr);
106 }
107
108 virtual void registerWithDebugger()
109 {
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000110 JITRegistrar::getGDBRegistrar().registerObject(*Buffer);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000111 Registered = true;
112 }
113 virtual void deregisterWithDebugger()
114 {
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000115 JITRegistrar::getGDBRegistrar().deregisterObject(*Buffer);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000116 }
117};
118
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000119// The MemoryBuffer passed into this constructor is just a wrapper around the
120// actual memory. Ultimately, the Binary parent class will take ownership of
121// this MemoryBuffer object but not the underlying memory.
Michael J. Spencer4d9c5392013-01-04 20:36:28 +0000122template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
123DyldELFObject<target_endianness, max_alignment, is64Bits>
124 ::DyldELFObject(MemoryBuffer *Wrapper, error_code &ec)
125 : ELFObjectFile<target_endianness, max_alignment, is64Bits>(Wrapper, ec) {
Preston Gurd689ff9c2012-04-16 22:12:58 +0000126 this->isDyldELFObject = true;
127}
128
Michael J. Spencer4d9c5392013-01-04 20:36:28 +0000129template<endianness target_endianness, std::size_t max_alignment, bool is64Bits>
130void DyldELFObject<target_endianness, max_alignment, is64Bits>
131 ::updateSectionAddress(const SectionRef &Sec, uint64_t Addr) {
Preston Gurd689ff9c2012-04-16 22:12:58 +0000132 DataRefImpl ShdrRef = Sec.getRawDataRefImpl();
133 Elf_Shdr *shdr = const_cast<Elf_Shdr*>(
134 reinterpret_cast<const Elf_Shdr *>(ShdrRef.p));
135
136 // This assumes the address passed in matches the target address bitness
137 // The template-based type cast handles everything else.
138 shdr->sh_addr = static_cast<addr_type>(Addr);
139}
140
Michael J. Spencer4d9c5392013-01-04 20:36:28 +0000141template<endianness target_endianness, std::size_t max_align, bool is64Bits>
142void DyldELFObject<target_endianness, max_align, is64Bits>
143 ::updateSymbolAddress(const SymbolRef &SymRef, uint64_t Addr){
Preston Gurd689ff9c2012-04-16 22:12:58 +0000144
145 Elf_Sym *sym = const_cast<Elf_Sym*>(
Michael J. Spencer4d9c5392013-01-04 20:36:28 +0000146 ELFObjectFile<target_endianness, max_align, is64Bits>
147 ::getSymbol(SymRef.getRawDataRefImpl()));
Preston Gurd689ff9c2012-04-16 22:12:58 +0000148
149 // This assumes the address passed in matches the target address bitness
150 // The template-based type cast handles everything else.
151 sym->st_value = static_cast<addr_type>(Addr);
152}
153
154} // namespace
155
Eli Benderskya66a1852012-01-16 08:56:09 +0000156namespace llvm {
157
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000158ObjectImage *RuntimeDyldELF::createObjectImage(ObjectBuffer *Buffer) {
159 if (Buffer->getBufferSize() < ELF::EI_NIDENT)
160 llvm_unreachable("Unexpected ELF object size");
161 std::pair<unsigned char, unsigned char> Ident = std::make_pair(
162 (uint8_t)Buffer->getBufferStart()[ELF::EI_CLASS],
163 (uint8_t)Buffer->getBufferStart()[ELF::EI_DATA]);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000164 error_code ec;
165
166 if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB) {
Michael J. Spencer4d9c5392013-01-04 20:36:28 +0000167 DyldELFObject<support::little, 4, false> *Obj =
168 new DyldELFObject<support::little, 4, false>(Buffer->getMemBuffer(), ec);
169 return new ELFObjectImage<support::little, 4, false>(Buffer, Obj);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000170 }
171 else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB) {
Michael J. Spencer4d9c5392013-01-04 20:36:28 +0000172 DyldELFObject<support::big, 4, false> *Obj =
173 new DyldELFObject<support::big, 4, false>(Buffer->getMemBuffer(), ec);
174 return new ELFObjectImage<support::big, 4, false>(Buffer, Obj);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000175 }
176 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB) {
Michael J. Spencer4d9c5392013-01-04 20:36:28 +0000177 DyldELFObject<support::big, 8, true> *Obj =
178 new DyldELFObject<support::big, 8, true>(Buffer->getMemBuffer(), ec);
179 return new ELFObjectImage<support::big, 8, true>(Buffer, Obj);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000180 }
181 else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) {
Michael J. Spencer4d9c5392013-01-04 20:36:28 +0000182 DyldELFObject<support::little, 8, true> *Obj =
183 new DyldELFObject<support::little, 8, true>(Buffer->getMemBuffer(), ec);
184 return new ELFObjectImage<support::little, 8, true>(Buffer, Obj);
Preston Gurd689ff9c2012-04-16 22:12:58 +0000185 }
186 else
187 llvm_unreachable("Unexpected ELF format");
188}
189
Preston Gurd689ff9c2012-04-16 22:12:58 +0000190RuntimeDyldELF::~RuntimeDyldELF() {
Preston Gurd689ff9c2012-04-16 22:12:58 +0000191}
Eli Benderskya66a1852012-01-16 08:56:09 +0000192
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000193void RuntimeDyldELF::resolveX86_64Relocation(const SectionEntry &Section,
194 uint64_t Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000195 uint64_t Value,
196 uint32_t Type,
197 int64_t Addend) {
198 switch (Type) {
199 default:
200 llvm_unreachable("Relocation type not implemented yet!");
201 break;
Eli Benderskya66a1852012-01-16 08:56:09 +0000202 case ELF::R_X86_64_64: {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000203 uint64_t *Target = reinterpret_cast<uint64_t*>(Section.Address + Offset);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000204 *Target = Value + Addend;
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000205 DEBUG(dbgs() << "Writing " << format("%p", (Value + Addend))
206 << " at " << format("%p\n",Target));
Eli Benderskya66a1852012-01-16 08:56:09 +0000207 break;
208 }
209 case ELF::R_X86_64_32:
210 case ELF::R_X86_64_32S: {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000211 Value += Addend;
Andrew Kaylord83a5472012-07-27 20:30:12 +0000212 assert((Type == ELF::R_X86_64_32 && (Value <= UINT32_MAX)) ||
Michael J. Spencer4d9c5392013-01-04 20:36:28 +0000213 (Type == ELF::R_X86_64_32S &&
Andrew Kaylord83a5472012-07-27 20:30:12 +0000214 ((int64_t)Value <= INT32_MAX && (int64_t)Value >= INT32_MIN)));
Eli Benderskya66a1852012-01-16 08:56:09 +0000215 uint32_t TruncatedAddr = (Value & 0xFFFFFFFF);
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000216 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
Eli Benderskya66a1852012-01-16 08:56:09 +0000217 *Target = TruncatedAddr;
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000218 DEBUG(dbgs() << "Writing " << format("%p", TruncatedAddr)
219 << " at " << format("%p\n",Target));
Eli Benderskya66a1852012-01-16 08:56:09 +0000220 break;
221 }
222 case ELF::R_X86_64_PC32: {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000223 // Get the placeholder value from the generated object since
224 // a previous relocation attempt may have overwritten the loaded version
225 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress
226 + Offset);
227 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
228 uint64_t FinalAddress = Section.LoadAddress + Offset;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000229 int64_t RealOffset = *Placeholder + Value + Addend - FinalAddress;
Andrew Kaylord83a5472012-07-27 20:30:12 +0000230 assert(RealOffset <= INT32_MAX && RealOffset >= INT32_MIN);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000231 int32_t TruncOffset = (RealOffset & 0xFFFFFFFF);
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000232 *Target = TruncOffset;
Eli Benderskya66a1852012-01-16 08:56:09 +0000233 break;
234 }
235 }
236}
237
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000238void RuntimeDyldELF::resolveX86Relocation(const SectionEntry &Section,
239 uint64_t Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000240 uint32_t Value,
241 uint32_t Type,
242 int32_t Addend) {
243 switch (Type) {
Eli Benderskya66a1852012-01-16 08:56:09 +0000244 case ELF::R_386_32: {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000245 // Get the placeholder value from the generated object since
246 // a previous relocation attempt may have overwritten the loaded version
247 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress
248 + Offset);
249 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
250 *Target = *Placeholder + Value + Addend;
Eli Benderskya66a1852012-01-16 08:56:09 +0000251 break;
252 }
253 case ELF::R_386_PC32: {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000254 // Get the placeholder value from the generated object since
255 // a previous relocation attempt may have overwritten the loaded version
256 uint32_t *Placeholder = reinterpret_cast<uint32_t*>(Section.ObjAddress
257 + Offset);
258 uint32_t *Target = reinterpret_cast<uint32_t*>(Section.Address + Offset);
259 uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000260 uint32_t RealOffset = *Placeholder + Value + Addend - FinalAddress;
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000261 *Target = RealOffset;
Eli Benderskya66a1852012-01-16 08:56:09 +0000262 break;
263 }
264 default:
265 // There are other relocation types, but it appears these are the
Andrew Kaylore2e73bd2012-07-27 18:39:47 +0000266 // only ones currently used by the LLVM ELF object writer
Craig Topper85814382012-02-07 05:05:23 +0000267 llvm_unreachable("Relocation type not implemented yet!");
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000268 break;
Eli Benderskya66a1852012-01-16 08:56:09 +0000269 }
270}
271
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000272void RuntimeDyldELF::resolveARMRelocation(const SectionEntry &Section,
273 uint64_t Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000274 uint32_t Value,
275 uint32_t Type,
276 int32_t Addend) {
277 // TODO: Add Thumb relocations.
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000278 uint32_t* TargetPtr = (uint32_t*)(Section.Address + Offset);
279 uint32_t FinalAddress = ((Section.LoadAddress + Offset) & 0xFFFFFFFF);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000280 Value += Addend;
281
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000282 DEBUG(dbgs() << "resolveARMRelocation, LocalAddress: "
283 << Section.Address + Offset
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000284 << " FinalAddress: " << format("%p",FinalAddress)
285 << " Value: " << format("%x",Value)
286 << " Type: " << format("%x",Type)
287 << " Addend: " << format("%x",Addend)
288 << "\n");
289
290 switch(Type) {
291 default:
292 llvm_unreachable("Not implemented relocation type!");
293
Michael J. Spencer4d9c5392013-01-04 20:36:28 +0000294 // Write a 32bit value to relocation address, taking into account the
Tim Northover565ebde2012-10-03 16:29:42 +0000295 // implicit addend encoded in the target.
Amara Emerson098d6d52012-11-16 11:11:59 +0000296 case ELF::R_ARM_TARGET1 :
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000297 case ELF::R_ARM_ABS32 :
Tim Northover565ebde2012-10-03 16:29:42 +0000298 *TargetPtr += Value;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000299 break;
300
301 // Write first 16 bit of 32 bit value to the mov instruction.
302 // Last 4 bit should be shifted.
303 case ELF::R_ARM_MOVW_ABS_NC :
Tim Northover565ebde2012-10-03 16:29:42 +0000304 // We are not expecting any other addend in the relocation address.
Michael J. Spencer4d9c5392013-01-04 20:36:28 +0000305 // Using 0x000F0FFF because MOVW has its 16 bit immediate split into 2
Tim Northover565ebde2012-10-03 16:29:42 +0000306 // non-contiguous fields.
307 assert((*TargetPtr & 0x000F0FFF) == 0);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000308 Value = Value & 0xFFFF;
309 *TargetPtr |= Value & 0xFFF;
310 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
311 break;
312
313 // Write last 16 bit of 32 bit value to the mov instruction.
314 // Last 4 bit should be shifted.
315 case ELF::R_ARM_MOVT_ABS :
Tim Northover565ebde2012-10-03 16:29:42 +0000316 // We are not expecting any other addend in the relocation address.
317 // Use 0x000F0FFF for the same reason as R_ARM_MOVW_ABS_NC.
318 assert((*TargetPtr & 0x000F0FFF) == 0);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000319 Value = (Value >> 16) & 0xFFFF;
320 *TargetPtr |= Value & 0xFFF;
321 *TargetPtr |= ((Value >> 12) & 0xF) << 16;
322 break;
323
324 // Write 24 bit relative value to the branch instruction.
325 case ELF::R_ARM_PC24 : // Fall through.
326 case ELF::R_ARM_CALL : // Fall through.
327 case ELF::R_ARM_JUMP24 :
328 int32_t RelValue = static_cast<int32_t>(Value - FinalAddress - 8);
329 RelValue = (RelValue & 0x03FFFFFC) >> 2;
330 *TargetPtr &= 0xFF000000;
331 *TargetPtr |= RelValue;
332 break;
333 }
Eli Benderskya66a1852012-01-16 08:56:09 +0000334}
335
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000336void RuntimeDyldELF::resolveMIPSRelocation(const SectionEntry &Section,
337 uint64_t Offset,
Akira Hatanakab862f092012-08-20 17:53:24 +0000338 uint32_t Value,
339 uint32_t Type,
340 int32_t Addend) {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000341 uint32_t* TargetPtr = (uint32_t*)(Section.Address + Offset);
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000342 Value += Addend;
343
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000344 DEBUG(dbgs() << "resolveMipselocation, LocalAddress: "
345 << Section.Address + Offset
346 << " FinalAddress: "
347 << format("%p",Section.LoadAddress + Offset)
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000348 << " Value: " << format("%x",Value)
349 << " Type: " << format("%x",Type)
350 << " Addend: " << format("%x",Addend)
351 << "\n");
352
353 switch(Type) {
354 default:
355 llvm_unreachable("Not implemented relocation type!");
356 break;
357 case ELF::R_MIPS_32:
358 *TargetPtr = Value + (*TargetPtr);
359 break;
360 case ELF::R_MIPS_26:
361 *TargetPtr = ((*TargetPtr) & 0xfc000000) | (( Value & 0x0fffffff) >> 2);
362 break;
363 case ELF::R_MIPS_HI16:
364 // Get the higher 16-bits. Also add 1 if bit 15 is 1.
365 Value += ((*TargetPtr) & 0x0000ffff) << 16;
366 *TargetPtr = ((*TargetPtr) & 0xffff0000) |
367 (((Value + 0x8000) >> 16) & 0xffff);
368 break;
369 case ELF::R_MIPS_LO16:
370 Value += ((*TargetPtr) & 0x0000ffff);
371 *TargetPtr = ((*TargetPtr) & 0xffff0000) | (Value & 0xffff);
372 break;
373 }
374}
375
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000376// Return the .TOC. section address to R_PPC64_TOC relocations.
377uint64_t RuntimeDyldELF::findPPC64TOC() const {
378 // The TOC consists of sections .got, .toc, .tocbss, .plt in that
379 // order. The TOC starts where the first of these sections starts.
380 SectionList::const_iterator it = Sections.begin();
381 SectionList::const_iterator ite = Sections.end();
382 for (; it != ite; ++it) {
383 if (it->Name == ".got" ||
384 it->Name == ".toc" ||
385 it->Name == ".tocbss" ||
386 it->Name == ".plt")
387 break;
388 }
389 if (it == ite) {
390 // This may happen for
391 // * references to TOC base base (sym@toc, .odp relocation) without
392 // a .toc directive.
393 // In this case just use the first section (which is usually
394 // the .odp) since the code won't reference the .toc base
395 // directly.
396 it = Sections.begin();
397 }
398 assert (it != ite);
399 // Per the ppc64-elf-linux ABI, The TOC base is TOC value plus 0x8000
400 // thus permitting a full 64 Kbytes segment.
401 return it->LoadAddress + 0x8000;
402}
403
404// Returns the sections and offset associated with the ODP entry referenced
405// by Symbol.
406void RuntimeDyldELF::findOPDEntrySection(ObjectImage &Obj,
407 ObjSectionToIDMap &LocalSections,
408 RelocationValueRef &Rel) {
409 // Get the ELF symbol value (st_value) to compare with Relocation offset in
410 // .opd entries
411
412 error_code err;
413 for (section_iterator si = Obj.begin_sections(),
414 se = Obj.end_sections(); si != se; si.increment(err)) {
415 StringRef SectionName;
416 check(si->getName(SectionName));
417 if (SectionName != ".opd")
418 continue;
419
420 for (relocation_iterator i = si->begin_relocations(),
421 e = si->end_relocations(); i != e;) {
422 check(err);
423
424 // The R_PPC64_ADDR64 relocation indicates the first field
425 // of a .opd entry
426 uint64_t TypeFunc;
427 check(i->getType(TypeFunc));
428 if (TypeFunc != ELF::R_PPC64_ADDR64) {
429 i.increment(err);
430 continue;
431 }
432
433 SymbolRef TargetSymbol;
434 uint64_t TargetSymbolOffset;
435 int64_t TargetAdditionalInfo;
436 check(i->getSymbol(TargetSymbol));
437 check(i->getOffset(TargetSymbolOffset));
438 check(i->getAdditionalInfo(TargetAdditionalInfo));
439
440 i = i.increment(err);
441 if (i == e)
442 break;
443 check(err);
444
445 // Just check if following relocation is a R_PPC64_TOC
446 uint64_t TypeTOC;
447 check(i->getType(TypeTOC));
448 if (TypeTOC != ELF::R_PPC64_TOC)
449 continue;
450
451 // Finally compares the Symbol value and the target symbol offset
452 // to check if this .opd entry refers to the symbol the relocation
453 // points to.
454 if (Rel.Addend != (intptr_t)TargetSymbolOffset)
455 continue;
456
457 section_iterator tsi(Obj.end_sections());
458 check(TargetSymbol.getSection(tsi));
459 Rel.SectionID = findOrEmitSection(Obj, (*tsi), true, LocalSections);
460 Rel.Addend = (intptr_t)TargetAdditionalInfo;
461 return;
462 }
463 }
464 llvm_unreachable("Attempting to get address of ODP entry!");
465}
466
467// Relocation masks following the #lo(value), #hi(value), #higher(value),
468// and #highest(value) macros defined in section 4.5.1. Relocation Types
469// in PPC-elf64abi document.
470//
471static inline
472uint16_t applyPPClo (uint64_t value)
473{
474 return value & 0xffff;
475}
476
477static inline
478uint16_t applyPPChi (uint64_t value)
479{
480 return (value >> 16) & 0xffff;
481}
482
483static inline
484uint16_t applyPPChigher (uint64_t value)
485{
486 return (value >> 32) & 0xffff;
487}
488
489static inline
490uint16_t applyPPChighest (uint64_t value)
491{
492 return (value >> 48) & 0xffff;
493}
494
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000495void RuntimeDyldELF::resolvePPC64Relocation(const SectionEntry &Section,
496 uint64_t Offset,
497 uint64_t Value,
498 uint32_t Type,
499 int64_t Addend) {
500 uint8_t* LocalAddress = Section.Address + Offset;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000501 switch (Type) {
502 default:
503 llvm_unreachable("Relocation type not implemented yet!");
504 break;
505 case ELF::R_PPC64_ADDR16_LO :
506 writeInt16BE(LocalAddress, applyPPClo (Value + Addend));
507 break;
508 case ELF::R_PPC64_ADDR16_HI :
509 writeInt16BE(LocalAddress, applyPPChi (Value + Addend));
510 break;
511 case ELF::R_PPC64_ADDR16_HIGHER :
512 writeInt16BE(LocalAddress, applyPPChigher (Value + Addend));
513 break;
514 case ELF::R_PPC64_ADDR16_HIGHEST :
515 writeInt16BE(LocalAddress, applyPPChighest (Value + Addend));
516 break;
517 case ELF::R_PPC64_ADDR14 : {
518 assert(((Value + Addend) & 3) == 0);
519 // Preserve the AA/LK bits in the branch instruction
520 uint8_t aalk = *(LocalAddress+3);
521 writeInt16BE(LocalAddress + 2, (aalk & 3) | ((Value + Addend) & 0xfffc));
522 } break;
Adhemerval Zanella7b449882013-01-04 19:08:13 +0000523 case ELF::R_PPC64_ADDR32 : {
524 int32_t Result = static_cast<int32_t>(Value + Addend);
525 if (SignExtend32<32>(Result) != Result)
526 llvm_unreachable("Relocation R_PPC64_REL32 overflow");
527 writeInt32BE(LocalAddress, Result);
528 } break;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000529 case ELF::R_PPC64_REL24 : {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000530 uint64_t FinalAddress = (Section.LoadAddress + Offset);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000531 int32_t delta = static_cast<int32_t>(Value - FinalAddress + Addend);
532 if (SignExtend32<24>(delta) != delta)
533 llvm_unreachable("Relocation R_PPC64_REL24 overflow");
534 // Generates a 'bl <address>' instruction
535 writeInt32BE(LocalAddress, 0x48000001 | (delta & 0x03FFFFFC));
536 } break;
537 case ELF::R_PPC64_ADDR64 :
538 writeInt64BE(LocalAddress, Value + Addend);
539 break;
540 case ELF::R_PPC64_TOC :
541 writeInt64BE(LocalAddress, findPPC64TOC());
542 break;
543 case ELF::R_PPC64_TOC16 : {
544 uint64_t TOCStart = findPPC64TOC();
545 Value = applyPPClo((Value + Addend) - TOCStart);
546 writeInt16BE(LocalAddress, applyPPClo(Value));
547 } break;
548 case ELF::R_PPC64_TOC16_DS : {
549 uint64_t TOCStart = findPPC64TOC();
550 Value = ((Value + Addend) - TOCStart);
551 writeInt16BE(LocalAddress, applyPPClo(Value));
552 } break;
553 }
554}
555
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000556void RuntimeDyldELF::resolveRelocation(const SectionEntry &Section,
557 uint64_t Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000558 uint64_t Value,
559 uint32_t Type,
560 int64_t Addend) {
Eli Benderskya66a1852012-01-16 08:56:09 +0000561 switch (Arch) {
562 case Triple::x86_64:
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000563 resolveX86_64Relocation(Section, Offset, Value, Type, Addend);
Eli Benderskya66a1852012-01-16 08:56:09 +0000564 break;
565 case Triple::x86:
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000566 resolveX86Relocation(Section, Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000567 (uint32_t)(Value & 0xffffffffL), Type,
568 (uint32_t)(Addend & 0xffffffffL));
Eli Benderskya66a1852012-01-16 08:56:09 +0000569 break;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000570 case Triple::arm: // Fall through.
571 case Triple::thumb:
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000572 resolveARMRelocation(Section, Offset,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000573 (uint32_t)(Value & 0xffffffffL), Type,
574 (uint32_t)(Addend & 0xffffffffL));
Eli Benderskya66a1852012-01-16 08:56:09 +0000575 break;
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000576 case Triple::mips: // Fall through.
577 case Triple::mipsel:
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000578 resolveMIPSRelocation(Section, Offset,
Akira Hatanakab862f092012-08-20 17:53:24 +0000579 (uint32_t)(Value & 0xffffffffL), Type,
580 (uint32_t)(Addend & 0xffffffffL));
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000581 break;
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000582 case Triple::ppc64:
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000583 resolvePPC64Relocation(Section, Offset, Value, Type, Addend);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000584 break;
Craig Topper85814382012-02-07 05:05:23 +0000585 default: llvm_unreachable("Unsupported CPU type!");
Eli Benderskya66a1852012-01-16 08:56:09 +0000586 }
587}
588
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000589void RuntimeDyldELF::processRelocationRef(const ObjRelocationInfo &Rel,
Preston Gurd689ff9c2012-04-16 22:12:58 +0000590 ObjectImage &Obj,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000591 ObjSectionToIDMap &ObjSectionToID,
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000592 const SymbolTableMap &Symbols,
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000593 StubMap &Stubs) {
Eli Benderskya66a1852012-01-16 08:56:09 +0000594
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000595 uint32_t RelType = (uint32_t)(Rel.Type & 0xffffffffL);
596 intptr_t Addend = (intptr_t)Rel.AdditionalInfo;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000597 const SymbolRef &Symbol = Rel.Symbol;
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000598
599 // Obtain the symbol name which is referenced in the relocation
600 StringRef TargetName;
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000601 Symbol.getName(TargetName);
602 DEBUG(dbgs() << "\t\tRelType: " << RelType
603 << " Addend: " << Addend
604 << " TargetName: " << TargetName
605 << "\n");
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000606 RelocationValueRef Value;
607 // First search for the symbol in the local symbol table
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000608 SymbolTableMap::const_iterator lsi = Symbols.find(TargetName.data());
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000609 SymbolRef::Type SymType;
610 Symbol.getType(SymType);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000611 if (lsi != Symbols.end()) {
612 Value.SectionID = lsi->second.first;
613 Value.Addend = lsi->second.second;
614 } else {
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000615 // Search for the symbol in the global symbol table
Eli Benderskyd98c9e92012-05-01 06:58:59 +0000616 SymbolTableMap::const_iterator gsi =
617 GlobalSymbolTable.find(TargetName.data());
618 if (gsi != GlobalSymbolTable.end()) {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000619 Value.SectionID = gsi->second.first;
620 Value.Addend = gsi->second.second;
621 } else {
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000622 switch (SymType) {
623 case SymbolRef::ST_Debug: {
624 // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously
625 // and can be changed by another developers. Maybe best way is add
626 // a new symbol type ST_Section to SymbolRef and use it.
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000627 section_iterator si(Obj.end_sections());
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000628 Symbol.getSection(si);
629 if (si == Obj.end_sections())
630 llvm_unreachable("Symbol section not found, bad object file format!");
631 DEBUG(dbgs() << "\t\tThis is section symbol\n");
Andrew Kaylorfa8cd9d2012-10-12 23:53:16 +0000632 // Default to 'true' in case isText fails (though it never does).
633 bool isCode = true;
634 si->isText(isCode);
Michael J. Spencer4d9c5392013-01-04 20:36:28 +0000635 Value.SectionID = findOrEmitSection(Obj,
636 (*si),
637 isCode,
Andrew Kaylorfa8cd9d2012-10-12 23:53:16 +0000638 ObjSectionToID);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000639 Value.Addend = Addend;
640 break;
641 }
642 case SymbolRef::ST_Unknown: {
643 Value.SymbolName = TargetName.data();
644 Value.Addend = Addend;
645 break;
646 }
647 default:
648 llvm_unreachable("Unresolved symbol type!");
649 break;
650 }
651 }
Eli Benderskya66a1852012-01-16 08:56:09 +0000652 }
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000653 DEBUG(dbgs() << "\t\tRel.SectionID: " << Rel.SectionID
654 << " Rel.Offset: " << Rel.Offset
655 << "\n");
656 if (Arch == Triple::arm &&
657 (RelType == ELF::R_ARM_PC24 ||
658 RelType == ELF::R_ARM_CALL ||
659 RelType == ELF::R_ARM_JUMP24)) {
660 // This is an ARM branch relocation, need to use a stub function.
661 DEBUG(dbgs() << "\t\tThis is an ARM branch relocation.");
662 SectionEntry &Section = Sections[Rel.SectionID];
Eli Benderskya66a1852012-01-16 08:56:09 +0000663
Eric Christopherbf261f12012-10-23 17:19:15 +0000664 // Look for an existing stub.
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000665 StubMap::const_iterator i = Stubs.find(Value);
666 if (i != Stubs.end()) {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000667 resolveRelocation(Section, Rel.Offset,
668 (uint64_t)Section.Address + i->second, RelType, 0);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000669 DEBUG(dbgs() << " Stub function found\n");
670 } else {
671 // Create a new stub function.
672 DEBUG(dbgs() << " Create a new stub function\n");
673 Stubs[Value] = Section.StubOffset;
674 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
675 Section.StubOffset);
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000676 RelocationEntry RE(Rel.SectionID, StubTargetAddr - Section.Address,
677 ELF::R_ARM_ABS32, Value.Addend);
678 if (Value.SymbolName)
679 addRelocationForSymbol(RE, Value.SymbolName);
680 else
681 addRelocationForSection(RE, Value.SectionID);
682
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000683 resolveRelocation(Section, Rel.Offset,
684 (uint64_t)Section.Address + Section.StubOffset,
685 RelType, 0);
Danil Malyshev0e4fa5f2012-03-30 16:45:19 +0000686 Section.StubOffset += getMaxStubSize();
687 }
Akira Hatanakaade04742012-12-03 23:12:19 +0000688 } else if ((Arch == Triple::mipsel || Arch == Triple::mips) &&
689 RelType == ELF::R_MIPS_26) {
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000690 // This is an Mips branch relocation, need to use a stub function.
691 DEBUG(dbgs() << "\t\tThis is a Mips branch relocation.");
692 SectionEntry &Section = Sections[Rel.SectionID];
693 uint8_t *Target = Section.Address + Rel.Offset;
694 uint32_t *TargetAddress = (uint32_t *)Target;
695
696 // Extract the addend from the instruction.
697 uint32_t Addend = ((*TargetAddress) & 0x03ffffff) << 2;
698
699 Value.Addend += Addend;
700
701 // Look up for existing stub.
702 StubMap::const_iterator i = Stubs.find(Value);
703 if (i != Stubs.end()) {
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000704 resolveRelocation(Section, Rel.Offset,
705 (uint64_t)Section.Address + i->second, RelType, 0);
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000706 DEBUG(dbgs() << " Stub function found\n");
707 } else {
708 // Create a new stub function.
709 DEBUG(dbgs() << " Create a new stub function\n");
710 Stubs[Value] = Section.StubOffset;
711 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
712 Section.StubOffset);
713
714 // Creating Hi and Lo relocations for the filled stub instructions.
715 RelocationEntry REHi(Rel.SectionID,
716 StubTargetAddr - Section.Address,
717 ELF::R_MIPS_HI16, Value.Addend);
718 RelocationEntry RELo(Rel.SectionID,
719 StubTargetAddr - Section.Address + 4,
720 ELF::R_MIPS_LO16, Value.Addend);
721
722 if (Value.SymbolName) {
723 addRelocationForSymbol(REHi, Value.SymbolName);
724 addRelocationForSymbol(RELo, Value.SymbolName);
725 } else {
726 addRelocationForSection(REHi, Value.SectionID);
727 addRelocationForSection(RELo, Value.SectionID);
728 }
729
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000730 resolveRelocation(Section, Rel.Offset,
731 (uint64_t)Section.Address + Section.StubOffset,
732 RelType, 0);
Akira Hatanakab889e0c2012-08-17 21:28:04 +0000733 Section.StubOffset += getMaxStubSize();
734 }
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000735 } else if (Arch == Triple::ppc64) {
736 if (RelType == ELF::R_PPC64_REL24) {
737 // A PPC branch relocation will need a stub function if the target is
738 // an external symbol (Symbol::ST_Unknown) or if the target address
739 // is not within the signed 24-bits branch address.
740 SectionEntry &Section = Sections[Rel.SectionID];
741 uint8_t *Target = Section.Address + Rel.Offset;
742 bool RangeOverflow = false;
743 if (SymType != SymbolRef::ST_Unknown) {
744 // A function call may points to the .opd entry, so the final symbol value
745 // in calculated based in the relocation values in .opd section.
746 findOPDEntrySection(Obj, ObjSectionToID, Value);
747 uint8_t *RelocTarget = Sections[Value.SectionID].Address + Value.Addend;
748 int32_t delta = static_cast<int32_t>(Target - RelocTarget);
749 // If it is within 24-bits branch range, just set the branch target
750 if (SignExtend32<24>(delta) == delta) {
751 RelocationEntry RE(Rel.SectionID, Rel.Offset, RelType, Value.Addend);
752 if (Value.SymbolName)
753 addRelocationForSymbol(RE, Value.SymbolName);
754 else
755 addRelocationForSection(RE, Value.SectionID);
756 } else {
757 RangeOverflow = true;
758 }
759 }
760 if (SymType == SymbolRef::ST_Unknown || RangeOverflow == true) {
761 // It is an external symbol (SymbolRef::ST_Unknown) or within a range
762 // larger than 24-bits.
763 StubMap::const_iterator i = Stubs.find(Value);
764 if (i != Stubs.end()) {
765 // Symbol function stub already created, just relocate to it
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000766 resolveRelocation(Section, Rel.Offset,
767 (uint64_t)Section.Address + i->second, RelType, 0);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000768 DEBUG(dbgs() << " Stub function found\n");
769 } else {
770 // Create a new stub function.
771 DEBUG(dbgs() << " Create a new stub function\n");
772 Stubs[Value] = Section.StubOffset;
773 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
774 Section.StubOffset);
775 RelocationEntry RE(Rel.SectionID, StubTargetAddr - Section.Address,
776 ELF::R_PPC64_ADDR64, Value.Addend);
777
778 // Generates the 64-bits address loads as exemplified in section
779 // 4.5.1 in PPC64 ELF ABI.
780 RelocationEntry REhst(Rel.SectionID,
781 StubTargetAddr - Section.Address + 2,
782 ELF::R_PPC64_ADDR16_HIGHEST, Value.Addend);
783 RelocationEntry REhr(Rel.SectionID,
784 StubTargetAddr - Section.Address + 6,
785 ELF::R_PPC64_ADDR16_HIGHER, Value.Addend);
786 RelocationEntry REh(Rel.SectionID,
787 StubTargetAddr - Section.Address + 14,
788 ELF::R_PPC64_ADDR16_HI, Value.Addend);
789 RelocationEntry REl(Rel.SectionID,
790 StubTargetAddr - Section.Address + 18,
791 ELF::R_PPC64_ADDR16_LO, Value.Addend);
792
793 if (Value.SymbolName) {
794 addRelocationForSymbol(REhst, Value.SymbolName);
795 addRelocationForSymbol(REhr, Value.SymbolName);
796 addRelocationForSymbol(REh, Value.SymbolName);
797 addRelocationForSymbol(REl, Value.SymbolName);
798 } else {
799 addRelocationForSection(REhst, Value.SectionID);
800 addRelocationForSection(REhr, Value.SectionID);
801 addRelocationForSection(REh, Value.SectionID);
802 addRelocationForSection(REl, Value.SectionID);
803 }
804
Andrew Kaylora307a1c2012-11-02 19:45:23 +0000805 resolveRelocation(Section, Rel.Offset,
806 (uint64_t)Section.Address + Section.StubOffset,
807 RelType, 0);
Adhemerval Zanella6e8946c2012-10-25 13:13:48 +0000808 if (SymType == SymbolRef::ST_Unknown)
809 // Restore the TOC for external calls
810 writeInt32BE(Target+4, 0xE8410028); // ld r2,40(r1)
811 Section.StubOffset += getMaxStubSize();
812 }
813 }
814 } else {
815 RelocationEntry RE(Rel.SectionID, Rel.Offset, RelType, Value.Addend);
816 // Extra check to avoid relocation againt empty symbols (usually
817 // the R_PPC64_TOC).
818 if (Value.SymbolName && !TargetName.empty())
819 addRelocationForSymbol(RE, Value.SymbolName);
820 else
821 addRelocationForSection(RE, Value.SectionID);
822 }
Eli Benderskyc201e6e2012-05-01 10:41:12 +0000823 } else {
824 RelocationEntry RE(Rel.SectionID, Rel.Offset, RelType, Value.Addend);
825 if (Value.SymbolName)
826 addRelocationForSymbol(RE, Value.SymbolName);
827 else
828 addRelocationForSection(RE, Value.SectionID);
829 }
Jim Grosbach61425c02012-01-16 22:26:39 +0000830}
831
Tim Northoverf00677d2012-10-29 10:47:04 +0000832unsigned RuntimeDyldELF::getCommonSymbolAlignment(const SymbolRef &Sym) {
833 // In ELF, the value of an SHN_COMMON symbol is its alignment requirement.
834 uint64_t Align;
835 Check(Sym.getValue(Align));
836 return Align;
837}
838
Andrew Kaylor3f23cef2012-10-02 21:18:39 +0000839bool RuntimeDyldELF::isCompatibleFormat(const ObjectBuffer *Buffer) const {
840 if (Buffer->getBufferSize() < strlen(ELF::ElfMagic))
841 return false;
842 return (memcmp(Buffer->getBufferStart(), ELF::ElfMagic, strlen(ELF::ElfMagic))) == 0;
Eli Benderskya66a1852012-01-16 08:56:09 +0000843}
844} // namespace llvm