1 //===--- Relocation.cpp  - Interface for object file relocations ----------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //===----------------------------------------------------------------------===//
10 
11 #include "bolt/Core/Relocation.h"
12 #include "llvm/MC/MCContext.h"
13 #include "llvm/MC/MCStreamer.h"
14 #include "llvm/Object/ELF.h"
15 
16 using namespace llvm;
17 using namespace bolt;
18 
19 Triple::ArchType Relocation::Arch;
20 
21 namespace {
22 
23 bool isSupportedX86(uint64_t Type) {
24   switch (Type) {
25   default:
26     return false;
27   case ELF::R_X86_64_8:
28   case ELF::R_X86_64_16:
29   case ELF::R_X86_64_32:
30   case ELF::R_X86_64_32S:
31   case ELF::R_X86_64_64:
32   case ELF::R_X86_64_PC8:
33   case ELF::R_X86_64_PC32:
34   case ELF::R_X86_64_PC64:
35   case ELF::R_X86_64_PLT32:
36   case ELF::R_X86_64_GOTPCREL:
37   case ELF::R_X86_64_GOTTPOFF:
38   case ELF::R_X86_64_TPOFF32:
39   case ELF::R_X86_64_GOTPCRELX:
40   case ELF::R_X86_64_REX_GOTPCRELX:
41     return true;
42   }
43 }
44 
45 bool isSupportedAArch64(uint64_t Type) {
46   switch (Type) {
47   default:
48     return false;
49   case ELF::R_AARCH64_CALL26:
50   case ELF::R_AARCH64_JUMP26:
51   case ELF::R_AARCH64_TSTBR14:
52   case ELF::R_AARCH64_CONDBR19:
53   case ELF::R_AARCH64_ADR_PREL_LO21:
54   case ELF::R_AARCH64_ADR_PREL_PG_HI21:
55   case ELF::R_AARCH64_ADR_PREL_PG_HI21_NC:
56   case ELF::R_AARCH64_LDST64_ABS_LO12_NC:
57   case ELF::R_AARCH64_ADD_ABS_LO12_NC:
58   case ELF::R_AARCH64_LDST128_ABS_LO12_NC:
59   case ELF::R_AARCH64_LDST32_ABS_LO12_NC:
60   case ELF::R_AARCH64_LDST16_ABS_LO12_NC:
61   case ELF::R_AARCH64_LDST8_ABS_LO12_NC:
62   case ELF::R_AARCH64_ADR_GOT_PAGE:
63   case ELF::R_AARCH64_TLSDESC_ADR_PREL21:
64   case ELF::R_AARCH64_TLSDESC_ADR_PAGE21:
65   case ELF::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
66   case ELF::R_AARCH64_TLSLE_ADD_TPREL_HI12:
67   case ELF::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
68   case ELF::R_AARCH64_LD64_GOT_LO12_NC:
69   case ELF::R_AARCH64_TLSDESC_LD64_LO12:
70   case ELF::R_AARCH64_TLSDESC_ADD_LO12:
71   case ELF::R_AARCH64_TLSDESC_CALL:
72   case ELF::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
73   case ELF::R_AARCH64_PREL32:
74   case ELF::R_AARCH64_ABS16:
75   case ELF::R_AARCH64_ABS32:
76   case ELF::R_AARCH64_ABS64:
77   case ELF::R_AARCH64_MOVW_UABS_G0:
78   case ELF::R_AARCH64_MOVW_UABS_G0_NC:
79   case ELF::R_AARCH64_MOVW_UABS_G1:
80   case ELF::R_AARCH64_MOVW_UABS_G1_NC:
81   case ELF::R_AARCH64_MOVW_UABS_G2:
82   case ELF::R_AARCH64_MOVW_UABS_G2_NC:
83   case ELF::R_AARCH64_MOVW_UABS_G3:
84     return true;
85   }
86 }
87 
88 size_t getSizeForTypeX86(uint64_t Type) {
89   switch (Type) {
90   default:
91     errs() << object::getELFRelocationTypeName(ELF::EM_X86_64, Type) << '\n';
92     llvm_unreachable("unsupported relocation type");
93   case ELF::R_X86_64_8:
94   case ELF::R_X86_64_PC8:
95     return 1;
96   case ELF::R_X86_64_16:
97     return 2;
98   case ELF::R_X86_64_PLT32:
99   case ELF::R_X86_64_PC32:
100   case ELF::R_X86_64_32S:
101   case ELF::R_X86_64_32:
102   case ELF::R_X86_64_GOTPCREL:
103   case ELF::R_X86_64_GOTTPOFF:
104   case ELF::R_X86_64_TPOFF32:
105   case ELF::R_X86_64_GOTPCRELX:
106   case ELF::R_X86_64_REX_GOTPCRELX:
107     return 4;
108   case ELF::R_X86_64_PC64:
109   case ELF::R_X86_64_64:
110     return 8;
111   }
112 }
113 
114 size_t getSizeForTypeAArch64(uint64_t Type) {
115   switch (Type) {
116   default:
117     errs() << object::getELFRelocationTypeName(ELF::EM_AARCH64, Type) << '\n';
118     llvm_unreachable("unsupported relocation type");
119   case ELF::R_AARCH64_ABS16:
120     return 2;
121   case ELF::R_AARCH64_CALL26:
122   case ELF::R_AARCH64_JUMP26:
123   case ELF::R_AARCH64_TSTBR14:
124   case ELF::R_AARCH64_CONDBR19:
125   case ELF::R_AARCH64_ADR_PREL_LO21:
126   case ELF::R_AARCH64_ADR_PREL_PG_HI21:
127   case ELF::R_AARCH64_ADR_PREL_PG_HI21_NC:
128   case ELF::R_AARCH64_LDST64_ABS_LO12_NC:
129   case ELF::R_AARCH64_ADD_ABS_LO12_NC:
130   case ELF::R_AARCH64_LDST128_ABS_LO12_NC:
131   case ELF::R_AARCH64_LDST32_ABS_LO12_NC:
132   case ELF::R_AARCH64_LDST16_ABS_LO12_NC:
133   case ELF::R_AARCH64_LDST8_ABS_LO12_NC:
134   case ELF::R_AARCH64_ADR_GOT_PAGE:
135   case ELF::R_AARCH64_TLSDESC_ADR_PREL21:
136   case ELF::R_AARCH64_TLSDESC_ADR_PAGE21:
137   case ELF::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
138   case ELF::R_AARCH64_TLSLE_ADD_TPREL_HI12:
139   case ELF::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
140   case ELF::R_AARCH64_LD64_GOT_LO12_NC:
141   case ELF::R_AARCH64_TLSDESC_LD64_LO12:
142   case ELF::R_AARCH64_TLSDESC_ADD_LO12:
143   case ELF::R_AARCH64_TLSDESC_CALL:
144   case ELF::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
145   case ELF::R_AARCH64_PREL32:
146   case ELF::R_AARCH64_MOVW_UABS_G0:
147   case ELF::R_AARCH64_MOVW_UABS_G0_NC:
148   case ELF::R_AARCH64_MOVW_UABS_G1:
149   case ELF::R_AARCH64_MOVW_UABS_G1_NC:
150   case ELF::R_AARCH64_MOVW_UABS_G2:
151   case ELF::R_AARCH64_MOVW_UABS_G2_NC:
152   case ELF::R_AARCH64_MOVW_UABS_G3:
153   case ELF::R_AARCH64_ABS32:
154     return 4;
155   case ELF::R_AARCH64_ABS64:
156     return 8;
157   }
158 }
159 
160 bool skipRelocationProcessX86(uint64_t Type, uint64_t Contents) {
161   return false;
162 }
163 
164 bool skipRelocationProcessAArch64(uint64_t Type, uint64_t Contents) {
165   auto IsMov = [](uint64_t Contents) -> bool {
166     // The bits 28-23 are 0b100101
167     if ((Contents & 0x1f800000) == 0x12800000)
168       return true;
169     return false;
170   };
171 
172   auto IsB = [](uint64_t Contents) -> bool {
173     // The bits 31-26 are 0b000101
174     if ((Contents & 0xfc000000) == 0x14000000)
175       return true;
176     return false;
177   };
178 
179   auto IsNop = [](uint64_t Contents) -> bool { return Contents == 0xd503201f; };
180 
181   // The linker might eliminate the instruction and replace it with NOP, ignore
182   if (IsNop(Contents))
183     return true;
184 
185   // The linker might perform TLS relocations relaxations, such as
186   // changed TLS access model (e.g. changed global dynamic model
187   // to initial exec), thus changing the instructions. The static
188   // relocations might be invalid at this point and we might no
189   // need to proccess these relocations anymore.
190   // More information could be found by searching
191   // elfNN_aarch64_tls_relax in bfd
192   switch (Type) {
193   default:
194     break;
195   case ELF::R_AARCH64_TLSDESC_LD64_LO12:
196   case ELF::R_AARCH64_TLSDESC_ADR_PAGE21:
197   case ELF::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
198   case ELF::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21: {
199     if (IsMov(Contents))
200       return true;
201   }
202   }
203 
204   // The ld might replace load/store instruction with jump and
205   // veneer due to errata 843419
206   // https://documentation-service.arm.com/static/5fa29fddb209f547eebd361d
207   // Thus load/store relocations for these instructions must be ignored
208   // NOTE: We only process GOT and TLS relocations this way since the
209   // addend used in load/store instructions won't change after bolt
210   // (it is important since the instruction in veneer won't have relocation)
211   switch (Type) {
212   default:
213     break;
214   case ELF::R_AARCH64_LD64_GOT_LO12_NC:
215   case ELF::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
216   case ELF::R_AARCH64_TLSDESC_LD64_LO12: {
217     if (IsB(Contents))
218       return true;
219   }
220   }
221 
222   return false;
223 }
224 
225 uint64_t extractValueX86(uint64_t Type, uint64_t Contents, uint64_t PC) {
226   if (Type == ELF::R_X86_64_32S)
227     return SignExtend64<32>(Contents & 0xffffffff);
228   return Contents;
229 }
230 
231 uint64_t extractValueAArch64(uint64_t Type, uint64_t Contents, uint64_t PC) {
232   switch (Type) {
233   default:
234     errs() << object::getELFRelocationTypeName(ELF::EM_AARCH64, Type) << '\n';
235     llvm_unreachable("unsupported relocation type");
236   case ELF::R_AARCH64_ABS16:
237   case ELF::R_AARCH64_ABS32:
238   case ELF::R_AARCH64_ABS64:
239     return Contents;
240   case ELF::R_AARCH64_PREL32:
241     return static_cast<int64_t>(PC) + SignExtend64<32>(Contents & 0xffffffff);
242   case ELF::R_AARCH64_TLSDESC_CALL:
243   case ELF::R_AARCH64_JUMP26:
244   case ELF::R_AARCH64_CALL26:
245     // Immediate goes in bits 25:0 of B and BL.
246     Contents &= ~0xfffffffffc000000ULL;
247     return static_cast<int64_t>(PC) + SignExtend64<28>(Contents << 2);
248   case ELF::R_AARCH64_TSTBR14:
249     // Immediate:15:2 goes in bits 18:5 of TBZ, TBNZ
250     Contents &= ~0xfffffffffff8001fULL;
251     return static_cast<int64_t>(PC) + SignExtend64<16>(Contents >> 3);
252   case ELF::R_AARCH64_CONDBR19:
253     // Immediate:20:2 goes in bits 23:5 of Bcc, CBZ, CBNZ
254     Contents &= ~0xffffffffff00001fULL;
255     return static_cast<int64_t>(PC) + SignExtend64<21>(Contents >> 3);
256   case ELF::R_AARCH64_ADR_GOT_PAGE:
257   case ELF::R_AARCH64_TLSDESC_ADR_PREL21:
258   case ELF::R_AARCH64_TLSDESC_ADR_PAGE21:
259   case ELF::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
260   case ELF::R_AARCH64_ADR_PREL_LO21:
261   case ELF::R_AARCH64_ADR_PREL_PG_HI21:
262   case ELF::R_AARCH64_ADR_PREL_PG_HI21_NC: {
263     // Bits 32:12 of Symbol address goes in bits 30:29 + 23:5 of ADRP
264     // and ADR instructions
265     bool IsAdr = !!(((Contents >> 31) & 0x1) == 0);
266     Contents &= ~0xffffffff9f00001fUll;
267     uint64_t LowBits = (Contents >> 29) & 0x3;
268     uint64_t HighBits = (Contents >> 5) & 0x7ffff;
269     Contents = LowBits | (HighBits << 2);
270     if (IsAdr)
271       return static_cast<int64_t>(PC) + SignExtend64<21>(Contents);
272 
273     // ADRP instruction
274     Contents = static_cast<int64_t>(PC) + SignExtend64<33>(Contents << 12);
275     Contents &= ~0xfffUll;
276     return Contents;
277   }
278   case ELF::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
279   case ELF::R_AARCH64_TLSDESC_LD64_LO12:
280   case ELF::R_AARCH64_LD64_GOT_LO12_NC:
281   case ELF::R_AARCH64_LDST64_ABS_LO12_NC: {
282     // Immediate goes in bits 21:10 of LD/ST instruction, taken
283     // from bits 11:3 of Symbol address
284     Contents &= ~0xffffffffffc003ffU;
285     return Contents >> (10 - 3);
286   }
287   case ELF::R_AARCH64_TLSLE_ADD_TPREL_HI12:
288   case ELF::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
289   case ELF::R_AARCH64_TLSDESC_ADD_LO12:
290   case ELF::R_AARCH64_ADD_ABS_LO12_NC: {
291     // Immediate goes in bits 21:10 of ADD instruction
292     Contents &= ~0xffffffffffc003ffU;
293     return Contents >> (10 - 0);
294   }
295   case ELF::R_AARCH64_LDST128_ABS_LO12_NC: {
296     // Immediate goes in bits 21:10 of ADD instruction, taken
297     // from bits 11:4 of Symbol address
298     Contents &= ~0xffffffffffc003ffU;
299     return Contents >> (10 - 4);
300   }
301   case ELF::R_AARCH64_LDST32_ABS_LO12_NC: {
302     // Immediate goes in bits 21:10 of ADD instruction, taken
303     // from bits 11:2 of Symbol address
304     Contents &= ~0xffffffffffc003ffU;
305     return Contents >> (10 - 2);
306   }
307   case ELF::R_AARCH64_LDST16_ABS_LO12_NC: {
308     // Immediate goes in bits 21:10 of ADD instruction, taken
309     // from bits 11:1 of Symbol address
310     Contents &= ~0xffffffffffc003ffU;
311     return Contents >> (10 - 1);
312   }
313   case ELF::R_AARCH64_LDST8_ABS_LO12_NC: {
314     // Immediate goes in bits 21:10 of ADD instruction, taken
315     // from bits 11:0 of Symbol address
316     Contents &= ~0xffffffffffc003ffU;
317     return Contents >> (10 - 0);
318   }
319   case ELF::R_AARCH64_MOVW_UABS_G3:
320   case ELF::R_AARCH64_MOVW_UABS_G2_NC:
321   case ELF::R_AARCH64_MOVW_UABS_G2:
322   case ELF::R_AARCH64_MOVW_UABS_G1_NC:
323   case ELF::R_AARCH64_MOVW_UABS_G1:
324   case ELF::R_AARCH64_MOVW_UABS_G0_NC:
325   case ELF::R_AARCH64_MOVW_UABS_G0:
326     // The shift goest in bits 22:21 of MOV* instructions
327     uint8_t Shift = (Contents >> 21) & 0x3;
328     // Immediate goes in bits 20:5
329     Contents = (Contents >> 5) & 0xffff;
330     return Contents << (16 * Shift);
331   }
332 }
333 
334 bool isGOTX86(uint64_t Type) {
335   switch (Type) {
336   default:
337     return false;
338   case ELF::R_X86_64_GOT32:
339   case ELF::R_X86_64_GOTPCREL:
340   case ELF::R_X86_64_GOTTPOFF:
341   case ELF::R_X86_64_GOTOFF64:
342   case ELF::R_X86_64_GOTPC32:
343   case ELF::R_X86_64_GOT64:
344   case ELF::R_X86_64_GOTPCREL64:
345   case ELF::R_X86_64_GOTPC64:
346   case ELF::R_X86_64_GOTPLT64:
347   case ELF::R_X86_64_GOTPC32_TLSDESC:
348   case ELF::R_X86_64_GOTPCRELX:
349   case ELF::R_X86_64_REX_GOTPCRELX:
350     return true;
351   }
352 }
353 
354 bool isGOTAArch64(uint64_t Type) {
355   switch (Type) {
356   default:
357     return false;
358   case ELF::R_AARCH64_ADR_GOT_PAGE:
359   case ELF::R_AARCH64_LD64_GOT_LO12_NC:
360   case ELF::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
361   case ELF::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
362   case ELF::R_AARCH64_TLSDESC_ADR_PREL21:
363   case ELF::R_AARCH64_TLSDESC_ADR_PAGE21:
364   case ELF::R_AARCH64_TLSDESC_LD64_LO12:
365   case ELF::R_AARCH64_TLSDESC_ADD_LO12:
366   case ELF::R_AARCH64_TLSDESC_CALL:
367     return true;
368   }
369 }
370 
371 bool isTLSX86(uint64_t Type) {
372   switch (Type) {
373   default:
374     return false;
375   case ELF::R_X86_64_TPOFF32:
376   case ELF::R_X86_64_TPOFF64:
377   case ELF::R_X86_64_GOTTPOFF:
378     return true;
379   }
380 }
381 
382 bool isTLSAArch64(uint64_t Type) {
383   switch (Type) {
384   default:
385     return false;
386   case ELF::R_AARCH64_TLSDESC_ADR_PREL21:
387   case ELF::R_AARCH64_TLSDESC_ADR_PAGE21:
388   case ELF::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
389   case ELF::R_AARCH64_TLSLE_ADD_TPREL_HI12:
390   case ELF::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
391   case ELF::R_AARCH64_TLSDESC_LD64_LO12:
392   case ELF::R_AARCH64_TLSDESC_ADD_LO12:
393   case ELF::R_AARCH64_TLSDESC_CALL:
394   case ELF::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
395     return true;
396   }
397 }
398 
399 bool isPCRelativeX86(uint64_t Type) {
400   switch (Type) {
401   default:
402     llvm_unreachable("Unknown relocation type");
403   case ELF::R_X86_64_64:
404   case ELF::R_X86_64_32:
405   case ELF::R_X86_64_32S:
406   case ELF::R_X86_64_16:
407   case ELF::R_X86_64_8:
408   case ELF::R_X86_64_TPOFF32:
409     return false;
410   case ELF::R_X86_64_PC8:
411   case ELF::R_X86_64_PC32:
412   case ELF::R_X86_64_PC64:
413   case ELF::R_X86_64_GOTPCREL:
414   case ELF::R_X86_64_PLT32:
415   case ELF::R_X86_64_GOTTPOFF:
416   case ELF::R_X86_64_GOTPCRELX:
417   case ELF::R_X86_64_REX_GOTPCRELX:
418     return true;
419   }
420 }
421 
422 bool isPCRelativeAArch64(uint64_t Type) {
423   switch (Type) {
424   default:
425     llvm_unreachable("Unknown relocation type");
426   case ELF::R_AARCH64_ABS16:
427   case ELF::R_AARCH64_ABS32:
428   case ELF::R_AARCH64_ABS64:
429   case ELF::R_AARCH64_LDST64_ABS_LO12_NC:
430   case ELF::R_AARCH64_ADD_ABS_LO12_NC:
431   case ELF::R_AARCH64_LDST128_ABS_LO12_NC:
432   case ELF::R_AARCH64_LDST32_ABS_LO12_NC:
433   case ELF::R_AARCH64_LDST16_ABS_LO12_NC:
434   case ELF::R_AARCH64_LDST8_ABS_LO12_NC:
435   case ELF::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
436   case ELF::R_AARCH64_TLSLE_ADD_TPREL_HI12:
437   case ELF::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
438   case ELF::R_AARCH64_LD64_GOT_LO12_NC:
439   case ELF::R_AARCH64_TLSDESC_LD64_LO12:
440   case ELF::R_AARCH64_TLSDESC_ADD_LO12:
441   case ELF::R_AARCH64_MOVW_UABS_G0:
442   case ELF::R_AARCH64_MOVW_UABS_G0_NC:
443   case ELF::R_AARCH64_MOVW_UABS_G1:
444   case ELF::R_AARCH64_MOVW_UABS_G1_NC:
445   case ELF::R_AARCH64_MOVW_UABS_G2:
446   case ELF::R_AARCH64_MOVW_UABS_G2_NC:
447   case ELF::R_AARCH64_MOVW_UABS_G3:
448     return false;
449   case ELF::R_AARCH64_TLSDESC_CALL:
450   case ELF::R_AARCH64_CALL26:
451   case ELF::R_AARCH64_JUMP26:
452   case ELF::R_AARCH64_TSTBR14:
453   case ELF::R_AARCH64_CONDBR19:
454   case ELF::R_AARCH64_ADR_PREL_LO21:
455   case ELF::R_AARCH64_ADR_PREL_PG_HI21:
456   case ELF::R_AARCH64_ADR_PREL_PG_HI21_NC:
457   case ELF::R_AARCH64_ADR_GOT_PAGE:
458   case ELF::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
459   case ELF::R_AARCH64_TLSDESC_ADR_PREL21:
460   case ELF::R_AARCH64_TLSDESC_ADR_PAGE21:
461   case ELF::R_AARCH64_PREL32:
462     return true;
463   }
464 }
465 
466 } // end anonymous namespace
467 
468 bool Relocation::isSupported(uint64_t Type) {
469   if (Arch == Triple::aarch64)
470     return isSupportedAArch64(Type);
471   return isSupportedX86(Type);
472 }
473 
474 size_t Relocation::getSizeForType(uint64_t Type) {
475   if (Arch == Triple::aarch64)
476     return getSizeForTypeAArch64(Type);
477   return getSizeForTypeX86(Type);
478 }
479 
480 bool Relocation::skipRelocationProcess(uint64_t Type, uint64_t Contents) {
481   if (Arch == Triple::aarch64)
482     return skipRelocationProcessAArch64(Type, Contents);
483   return skipRelocationProcessX86(Type, Contents);
484 }
485 
486 uint64_t Relocation::extractValue(uint64_t Type, uint64_t Contents,
487                                   uint64_t PC) {
488   if (Arch == Triple::aarch64)
489     return extractValueAArch64(Type, Contents, PC);
490   return extractValueX86(Type, Contents, PC);
491 }
492 
493 bool Relocation::isGOT(uint64_t Type) {
494   if (Arch == Triple::aarch64)
495     return isGOTAArch64(Type);
496   return isGOTX86(Type);
497 }
498 
499 bool Relocation::isNone(uint64_t Type) {
500   if (Arch == Triple::aarch64)
501     return Type == ELF::R_AARCH64_NONE;
502   return Type == ELF::R_X86_64_NONE;
503 }
504 
505 bool Relocation::isRelative(uint64_t Type) {
506   if (Arch == Triple::aarch64)
507     return Type == ELF::R_AARCH64_RELATIVE;
508   return Type == ELF::R_X86_64_RELATIVE;
509 }
510 
511 bool Relocation::isIRelative(uint64_t Type) {
512   if (Arch == Triple::aarch64)
513     return Type == ELF::R_AARCH64_IRELATIVE;
514   return Type == ELF::R_X86_64_IRELATIVE;
515 }
516 
517 bool Relocation::isTLS(uint64_t Type) {
518   if (Arch == Triple::aarch64)
519     return isTLSAArch64(Type);
520   return isTLSX86(Type);
521 }
522 
523 bool Relocation::isPCRelative(uint64_t Type) {
524   if (Arch == Triple::aarch64)
525     return isPCRelativeAArch64(Type);
526   return isPCRelativeX86(Type);
527 }
528 
529 uint64_t Relocation::getPC32() {
530   if (Arch == Triple::aarch64)
531     return ELF::R_AARCH64_PREL32;
532   return ELF::R_X86_64_PC32;
533 }
534 
535 uint64_t Relocation::getPC64() {
536   if (Arch == Triple::aarch64)
537     return ELF::R_AARCH64_PREL64;
538   return ELF::R_X86_64_PC64;
539 }
540 
541 size_t Relocation::emit(MCStreamer *Streamer) const {
542   const size_t Size = getSizeForType(Type);
543   MCContext &Ctx = Streamer->getContext();
544   if (isPCRelative(Type)) {
545     MCSymbol *TempLabel = Ctx.createNamedTempSymbol();
546     Streamer->emitLabel(TempLabel);
547     const MCExpr *Value = nullptr;
548     if (Symbol) {
549       Value = MCSymbolRefExpr::create(Symbol, Ctx);
550       if (Addend) {
551         Value = MCBinaryExpr::createAdd(
552             Value, MCConstantExpr::create(Addend, Ctx), Ctx);
553       }
554     } else {
555       Value = MCConstantExpr::create(Addend, Ctx);
556     }
557     Value = MCBinaryExpr::createSub(
558         Value, MCSymbolRefExpr::create(TempLabel, Ctx), Ctx);
559     Streamer->emitValue(Value, Size);
560 
561     return Size;
562   }
563 
564   if (Symbol && Addend) {
565     auto Value =
566         MCBinaryExpr::createAdd(MCSymbolRefExpr::create(Symbol, Ctx),
567                                 MCConstantExpr::create(Addend, Ctx), Ctx);
568     Streamer->emitValue(Value, Size);
569   } else if (Symbol) {
570     Streamer->emitSymbolValue(Symbol, Size);
571   } else {
572     Streamer->emitIntValue(Addend, Size);
573   }
574 
575   return Size;
576 }
577 
578 #define ELF_RELOC(name, value) #name,
579 
580 void Relocation::print(raw_ostream &OS) const {
581   static const char *X86RelocNames[] = {
582 #include "llvm/BinaryFormat/ELFRelocs/x86_64.def"
583   };
584   static const char *AArch64RelocNames[] = {
585 #include "llvm/BinaryFormat/ELFRelocs/AArch64.def"
586   };
587   if (Arch == Triple::aarch64)
588     OS << AArch64RelocNames[Type];
589   else
590     OS << X86RelocNames[Type];
591   OS << ", 0x" << Twine::utohexstr(Offset);
592   if (Symbol) {
593     OS << ", " << Symbol->getName();
594   }
595   if (int64_t(Addend) < 0)
596     OS << ", -0x" << Twine::utohexstr(-int64_t(Addend));
597   else
598     OS << ", 0x" << Twine::utohexstr(Addend);
599   OS << ", 0x" << Twine::utohexstr(Value);
600 }
601