f26002371d226af71e1d2331c9227839a24d9b54
[oota-llvm.git] / lib / MC / ELFObjectWriter.cpp
1 //===- lib/MC/ELFObjectWriter.cpp - ELF File Writer -------------------===//
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 // This file implements ELF object file writer information.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/MC/ELFObjectWriter.h"
15 #include "llvm/ADT/SmallPtrSet.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/StringMap.h"
18 #include "llvm/ADT/Twine.h"
19 #include "llvm/MC/MCAssembler.h"
20 #include "llvm/MC/MCAsmLayout.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCELFSymbolFlags.h"
23 #include "llvm/MC/MCExpr.h"
24 #include "llvm/MC/MCObjectWriter.h"
25 #include "llvm/MC/MCSectionELF.h"
26 #include "llvm/MC/MCSymbol.h"
27 #include "llvm/MC/MCValue.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/ErrorHandling.h"
30 #include "llvm/Support/ELF.h"
31 #include "llvm/Target/TargetAsmBackend.h"
32
33 #include "../Target/X86/X86FixupKinds.h"
34
35 #include <vector>
36 using namespace llvm;
37
38 static unsigned GetType(const MCSymbolData &SD) {
39   uint32_t Type = (SD.getFlags() & (0xf << ELF_STT_Shift)) >> ELF_STT_Shift;
40   assert(Type == ELF::STT_NOTYPE || Type == ELF::STT_OBJECT ||
41          Type == ELF::STT_FUNC || Type == ELF::STT_SECTION ||
42          Type == ELF::STT_FILE || Type == ELF::STT_COMMON ||
43          Type == ELF::STT_TLS);
44   return Type;
45 }
46
47 static unsigned GetBinding(const MCSymbolData &SD) {
48   uint32_t Binding = (SD.getFlags() & (0xf << ELF_STB_Shift)) >> ELF_STB_Shift;
49   assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
50          Binding == ELF::STB_WEAK);
51   return Binding;
52 }
53
54 static void SetBinding(MCSymbolData &SD, unsigned Binding) {
55   assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
56          Binding == ELF::STB_WEAK);
57   uint32_t OtherFlags = SD.getFlags() & ~(0xf << ELF_STB_Shift);
58   SD.setFlags(OtherFlags | (Binding << ELF_STB_Shift));
59 }
60
61 static unsigned GetVisibility(MCSymbolData &SD) {
62   unsigned Visibility =
63     (SD.getFlags() & (0xf << ELF_STV_Shift)) >> ELF_STV_Shift;
64   assert(Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_INTERNAL ||
65          Visibility == ELF::STV_HIDDEN || Visibility == ELF::STV_PROTECTED);
66   return Visibility;
67 }
68
69 static bool isFixupKindX86PCRel(unsigned Kind) {
70   switch (Kind) {
71   default:
72     return false;
73   case X86::reloc_pcrel_1byte:
74   case X86::reloc_pcrel_4byte:
75   case X86::reloc_riprel_4byte:
76   case X86::reloc_riprel_4byte_movq_load:
77     return true;
78   }
79 }
80
81 static bool RelocNeedsGOT(unsigned Type) {
82   switch (Type) {
83   default:
84     return false;
85   case ELF::R_X86_64_GOT32:
86   case ELF::R_X86_64_PLT32:
87   case ELF::R_X86_64_GOTPCREL:
88     return true;
89   }
90 }
91
92 namespace {
93
94   class ELFObjectWriterImpl {
95     /*static bool isFixupKindX86RIPRel(unsigned Kind) {
96       return Kind == X86::reloc_riprel_4byte ||
97         Kind == X86::reloc_riprel_4byte_movq_load;
98     }*/
99
100
101     /// ELFSymbolData - Helper struct for containing some precomputed information
102     /// on symbols.
103     struct ELFSymbolData {
104       MCSymbolData *SymbolData;
105       uint64_t StringIndex;
106       uint32_t SectionIndex;
107
108       // Support lexicographic sorting.
109       bool operator<(const ELFSymbolData &RHS) const {
110         if (GetType(*SymbolData) == ELF::STT_FILE)
111           return true;
112         if (GetType(*RHS.SymbolData) == ELF::STT_FILE)
113           return false;
114         return SymbolData->getSymbol().getName() <
115                RHS.SymbolData->getSymbol().getName();
116       }
117     };
118
119     /// @name Relocation Data
120     /// @{
121
122     struct ELFRelocationEntry {
123       // Make these big enough for both 32-bit and 64-bit
124       uint64_t r_offset;
125       int Index;
126       unsigned Type;
127       const MCSymbol *Symbol;
128       uint64_t r_addend;
129
130       // Support lexicographic sorting.
131       bool operator<(const ELFRelocationEntry &RE) const {
132         return RE.r_offset < r_offset;
133       }
134     };
135
136     SmallPtrSet<const MCSymbol *, 16> UsedInReloc;
137
138     llvm::DenseMap<const MCSectionData*,
139                    std::vector<ELFRelocationEntry> > Relocations;
140     DenseMap<const MCSection*, uint64_t> SectionStringTableIndex;
141
142     /// @}
143     /// @name Symbol Table Data
144     /// @{
145
146     SmallString<256> StringTable;
147     std::vector<ELFSymbolData> LocalSymbolData;
148     std::vector<ELFSymbolData> ExternalSymbolData;
149     std::vector<ELFSymbolData> UndefinedSymbolData;
150
151     /// @}
152
153     int NumRegularSections;
154
155     bool NeedsGOT;
156
157     ELFObjectWriter *Writer;
158
159     raw_ostream &OS;
160
161     unsigned Is64Bit : 1;
162
163     bool HasRelocationAddend;
164
165     Triple::OSType OSType;
166
167     uint16_t EMachine;
168
169     // This holds the symbol table index of the last local symbol.
170     unsigned LastLocalSymbolIndex;
171     // This holds the .strtab section index.
172     unsigned StringTableIndex;
173
174     unsigned ShstrtabIndex;
175
176   public:
177     ELFObjectWriterImpl(ELFObjectWriter *_Writer, bool _Is64Bit,
178                         uint16_t _EMachine, bool _HasRelAddend,
179                         Triple::OSType _OSType)
180       : NeedsGOT(false), Writer(_Writer), OS(Writer->getStream()),
181         Is64Bit(_Is64Bit), HasRelocationAddend(_HasRelAddend),
182         OSType(_OSType), EMachine(_EMachine) {
183     }
184
185     void Write8(uint8_t Value) { Writer->Write8(Value); }
186     void Write16(uint16_t Value) { Writer->Write16(Value); }
187     void Write32(uint32_t Value) { Writer->Write32(Value); }
188     //void Write64(uint64_t Value) { Writer->Write64(Value); }
189     void WriteZeros(unsigned N) { Writer->WriteZeros(N); }
190     //void WriteBytes(StringRef Str, unsigned ZeroFillSize = 0) {
191     //  Writer->WriteBytes(Str, ZeroFillSize);
192     //}
193
194     void WriteWord(uint64_t W) {
195       if (Is64Bit)
196         Writer->Write64(W);
197       else
198         Writer->Write32(W);
199     }
200
201     void String8(char *buf, uint8_t Value) {
202       buf[0] = Value;
203     }
204
205     void StringLE16(char *buf, uint16_t Value) {
206       buf[0] = char(Value >> 0);
207       buf[1] = char(Value >> 8);
208     }
209
210     void StringLE32(char *buf, uint32_t Value) {
211       StringLE16(buf, uint16_t(Value >> 0));
212       StringLE16(buf + 2, uint16_t(Value >> 16));
213     }
214
215     void StringLE64(char *buf, uint64_t Value) {
216       StringLE32(buf, uint32_t(Value >> 0));
217       StringLE32(buf + 4, uint32_t(Value >> 32));
218     }
219
220     void StringBE16(char *buf ,uint16_t Value) {
221       buf[0] = char(Value >> 8);
222       buf[1] = char(Value >> 0);
223     }
224
225     void StringBE32(char *buf, uint32_t Value) {
226       StringBE16(buf, uint16_t(Value >> 16));
227       StringBE16(buf + 2, uint16_t(Value >> 0));
228     }
229
230     void StringBE64(char *buf, uint64_t Value) {
231       StringBE32(buf, uint32_t(Value >> 32));
232       StringBE32(buf + 4, uint32_t(Value >> 0));
233     }
234
235     void String16(char *buf, uint16_t Value) {
236       if (Writer->isLittleEndian())
237         StringLE16(buf, Value);
238       else
239         StringBE16(buf, Value);
240     }
241
242     void String32(char *buf, uint32_t Value) {
243       if (Writer->isLittleEndian())
244         StringLE32(buf, Value);
245       else
246         StringBE32(buf, Value);
247     }
248
249     void String64(char *buf, uint64_t Value) {
250       if (Writer->isLittleEndian())
251         StringLE64(buf, Value);
252       else
253         StringBE64(buf, Value);
254     }
255
256     void WriteHeader(uint64_t SectionDataSize, unsigned NumberOfSections);
257
258     void WriteSymbolEntry(MCDataFragment *F, uint64_t name, uint8_t info,
259                           uint64_t value, uint64_t size,
260                           uint8_t other, uint16_t shndx);
261
262     void WriteSymbol(MCDataFragment *F, ELFSymbolData &MSD,
263                      const MCAsmLayout &Layout);
264
265     void WriteSymbolTable(MCDataFragment *F, const MCAssembler &Asm,
266                           const MCAsmLayout &Layout,
267                           unsigned NumRegularSections);
268
269     void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
270                           const MCFragment *Fragment, const MCFixup &Fixup,
271                           MCValue Target, uint64_t &FixedValue);
272
273     uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm,
274                                          const MCSymbol *S);
275
276     /// ComputeSymbolTable - Compute the symbol table data
277     ///
278     /// \param StringTable [out] - The string table data.
279     /// \param StringIndexMap [out] - Map from symbol names to offsets in the
280     /// string table.
281     void ComputeSymbolTable(MCAssembler &Asm);
282
283     void WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
284                          const MCSectionData &SD);
285
286     void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout) {
287       for (MCAssembler::const_iterator it = Asm.begin(),
288              ie = Asm.end(); it != ie; ++it) {
289         WriteRelocation(Asm, Layout, *it);
290       }
291     }
292
293     void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout);
294
295     void ExecutePostLayoutBinding(MCAssembler &Asm) {
296     }
297
298     void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
299                           uint64_t Address, uint64_t Offset,
300                           uint64_t Size, uint32_t Link, uint32_t Info,
301                           uint64_t Alignment, uint64_t EntrySize);
302
303     void WriteRelocationsFragment(const MCAssembler &Asm, MCDataFragment *F,
304                                   const MCSectionData *SD);
305
306     bool IsFixupFullyResolved(const MCAssembler &Asm,
307                               const MCValue Target,
308                               bool IsPCRel,
309                               const MCFragment *DF) const;
310
311     void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
312   };
313
314 }
315
316 // Emit the ELF header.
317 void ELFObjectWriterImpl::WriteHeader(uint64_t SectionDataSize,
318                                       unsigned NumberOfSections) {
319   // ELF Header
320   // ----------
321   //
322   // Note
323   // ----
324   // emitWord method behaves differently for ELF32 and ELF64, writing
325   // 4 bytes in the former and 8 in the latter.
326
327   Write8(0x7f); // e_ident[EI_MAG0]
328   Write8('E');  // e_ident[EI_MAG1]
329   Write8('L');  // e_ident[EI_MAG2]
330   Write8('F');  // e_ident[EI_MAG3]
331
332   Write8(Is64Bit ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
333
334   // e_ident[EI_DATA]
335   Write8(Writer->isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
336
337   Write8(ELF::EV_CURRENT);        // e_ident[EI_VERSION]
338   // e_ident[EI_OSABI]
339   switch (OSType) {
340     case Triple::FreeBSD:  Write8(ELF::ELFOSABI_FREEBSD); break;
341     case Triple::Linux:    Write8(ELF::ELFOSABI_LINUX); break;
342     default:               Write8(ELF::ELFOSABI_NONE); break;
343   }
344   Write8(0);                  // e_ident[EI_ABIVERSION]
345
346   WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
347
348   Write16(ELF::ET_REL);             // e_type
349
350   Write16(EMachine); // e_machine = target
351
352   Write32(ELF::EV_CURRENT);         // e_version
353   WriteWord(0);                    // e_entry, no entry point in .o file
354   WriteWord(0);                    // e_phoff, no program header for .o
355   WriteWord(SectionDataSize + (Is64Bit ? sizeof(ELF::Elf64_Ehdr) :
356             sizeof(ELF::Elf32_Ehdr)));  // e_shoff = sec hdr table off in bytes
357
358   // FIXME: Make this configurable.
359   Write32(0);   // e_flags = whatever the target wants
360
361   // e_ehsize = ELF header size
362   Write16(Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
363
364   Write16(0);                  // e_phentsize = prog header entry size
365   Write16(0);                  // e_phnum = # prog header entries = 0
366
367   // e_shentsize = Section header entry size
368   Write16(Is64Bit ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
369
370   // e_shnum     = # of section header ents
371   Write16(NumberOfSections);
372
373   // e_shstrndx  = Section # of '.shstrtab'
374   Write16(ShstrtabIndex);
375 }
376
377 void ELFObjectWriterImpl::WriteSymbolEntry(MCDataFragment *F, uint64_t name,
378                                            uint8_t info, uint64_t value,
379                                            uint64_t size, uint8_t other,
380                                            uint16_t shndx) {
381   if (Is64Bit) {
382     char buf[8];
383
384     String32(buf, name);
385     F->getContents() += StringRef(buf, 4); // st_name
386
387     String8(buf, info);
388     F->getContents() += StringRef(buf, 1);  // st_info
389
390     String8(buf, other);
391     F->getContents() += StringRef(buf, 1); // st_other
392
393     String16(buf, shndx);
394     F->getContents() += StringRef(buf, 2); // st_shndx
395
396     String64(buf, value);
397     F->getContents() += StringRef(buf, 8); // st_value
398
399     String64(buf, size);
400     F->getContents() += StringRef(buf, 8);  // st_size
401   } else {
402     char buf[4];
403
404     String32(buf, name);
405     F->getContents() += StringRef(buf, 4);  // st_name
406
407     String32(buf, value);
408     F->getContents() += StringRef(buf, 4); // st_value
409
410     String32(buf, size);
411     F->getContents() += StringRef(buf, 4);  // st_size
412
413     String8(buf, info);
414     F->getContents() += StringRef(buf, 1);  // st_info
415
416     String8(buf, other);
417     F->getContents() += StringRef(buf, 1); // st_other
418
419     String16(buf, shndx);
420     F->getContents() += StringRef(buf, 2); // st_shndx
421   }
422 }
423
424 static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout) {
425   if (Data.isCommon() && Data.isExternal())
426     return Data.getCommonAlignment();
427
428   const MCSymbol &Symbol = Data.getSymbol();
429   if (!Symbol.isInSection())
430     return 0;
431
432   if (!Data.isCommon() && !(Data.getFlags() & ELF_STB_Weak))
433     if (MCFragment *FF = Data.getFragment())
434       return Layout.getSymbolAddress(&Data) -
435              Layout.getSectionAddress(FF->getParent());
436
437   return 0;
438 }
439
440 static const MCSymbol &AliasedSymbol(const MCSymbol &Symbol) {
441   const MCSymbol *S = &Symbol;
442   while (S->isVariable()) {
443     const MCExpr *Value = S->getVariableValue();
444     if (Value->getKind() != MCExpr::SymbolRef)
445       return *S;
446     const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr*>(Value);
447     S = &Ref->getSymbol();
448   }
449   return *S;
450 }
451
452 void ELFObjectWriterImpl::WriteSymbol(MCDataFragment *F, ELFSymbolData &MSD,
453                                       const MCAsmLayout &Layout) {
454   MCSymbolData &OrigData = *MSD.SymbolData;
455   MCSymbolData &Data =
456     Layout.getAssembler().getSymbolData(AliasedSymbol(OrigData.getSymbol()));
457
458   uint8_t Binding = GetBinding(OrigData);
459   uint8_t Visibility = GetVisibility(OrigData);
460   uint8_t Type = GetType(Data);
461
462   uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
463   uint8_t Other = Visibility;
464
465   uint64_t Value = SymbolValue(Data, Layout);
466   uint64_t Size = 0;
467   const MCExpr *ESize;
468
469   assert(!(Data.isCommon() && !Data.isExternal()));
470
471   ESize = Data.getSize();
472   if (Data.getSize()) {
473     MCValue Res;
474     if (ESize->getKind() == MCExpr::Binary) {
475       const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(ESize);
476
477       if (BE->EvaluateAsRelocatable(Res, &Layout)) {
478         assert(!Res.getSymA() || !Res.getSymA()->getSymbol().isDefined());
479         assert(!Res.getSymB() || !Res.getSymB()->getSymbol().isDefined());
480         Size = Res.getConstant();
481       }
482     } else if (ESize->getKind() == MCExpr::Constant) {
483       Size = static_cast<const MCConstantExpr *>(ESize)->getValue();
484     } else {
485       assert(0 && "Unsupported size expression");
486     }
487   }
488
489   // Write out the symbol table entry
490   WriteSymbolEntry(F, MSD.StringIndex, Info, Value,
491                    Size, Other, MSD.SectionIndex);
492 }
493
494 void ELFObjectWriterImpl::WriteSymbolTable(MCDataFragment *F,
495                                            const MCAssembler &Asm,
496                                            const MCAsmLayout &Layout,
497                                            unsigned NumRegularSections) {
498   // The string table must be emitted first because we need the index
499   // into the string table for all the symbol names.
500   assert(StringTable.size() && "Missing string table");
501
502   // FIXME: Make sure the start of the symbol table is aligned.
503
504   // The first entry is the undefined symbol entry.
505   unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
506   F->getContents().append(EntrySize, '\x00');
507
508   // Write the symbol table entries.
509   LastLocalSymbolIndex = LocalSymbolData.size() + 1;
510   for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
511     ELFSymbolData &MSD = LocalSymbolData[i];
512     WriteSymbol(F, MSD, Layout);
513   }
514
515   // Write out a symbol table entry for each regular section.
516   unsigned Index = 1;
517   for (MCAssembler::const_iterator it = Asm.begin();
518        Index <= NumRegularSections; ++it, ++Index) {
519     const MCSectionELF &Section =
520       static_cast<const MCSectionELF&>(it->getSection());
521     // Leave out relocations so we don't have indexes within
522     // the relocations messed up
523     if (Section.getType() == ELF::SHT_RELA || Section.getType() == ELF::SHT_REL)
524       continue;
525     WriteSymbolEntry(F, 0, ELF::STT_SECTION, 0, 0, ELF::STV_DEFAULT, Index);
526     LastLocalSymbolIndex++;
527   }
528
529   for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
530     ELFSymbolData &MSD = ExternalSymbolData[i];
531     MCSymbolData &Data = *MSD.SymbolData;
532     assert(((Data.getFlags() & ELF_STB_Global) ||
533             (Data.getFlags() & ELF_STB_Weak)) &&
534            "External symbol requires STB_GLOBAL or STB_WEAK flag");
535     WriteSymbol(F, MSD, Layout);
536     if (GetBinding(Data) == ELF::STB_LOCAL)
537       LastLocalSymbolIndex++;
538   }
539
540   for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
541     ELFSymbolData &MSD = UndefinedSymbolData[i];
542     MCSymbolData &Data = *MSD.SymbolData;
543     WriteSymbol(F, MSD, Layout);
544     if (GetBinding(Data) == ELF::STB_LOCAL)
545       LastLocalSymbolIndex++;
546   }
547 }
548
549 static bool ShouldRelocOnSymbol(const MCSymbolData &SD,
550                                 const MCValue &Target,
551                                 const MCFragment &F) {
552   const MCSymbol &Symbol = SD.getSymbol();
553   if (Symbol.isUndefined())
554     return true;
555
556   const MCSectionELF &Section =
557     static_cast<const MCSectionELF&>(Symbol.getSection());
558
559   if (SD.isExternal())
560     return true;
561
562   MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
563   const MCSectionELF &Sec2 =
564     static_cast<const MCSectionELF&>(F.getParent()->getSection());
565
566   if (Section.getKind().isBSS())
567     return false;
568
569   if (&Sec2 != &Section &&
570       (Kind == MCSymbolRefExpr::VK_PLT ||
571        Kind == MCSymbolRefExpr::VK_GOTPCREL ||
572        Kind == MCSymbolRefExpr::VK_GOTOFF))
573     return true;
574
575   if (Section.getFlags() & MCSectionELF::SHF_MERGE)
576     return Target.getConstant() != 0;
577
578   return false;
579 }
580
581 // FIXME: this is currently X86/X86_64 only
582 void ELFObjectWriterImpl::RecordRelocation(const MCAssembler &Asm,
583                                            const MCAsmLayout &Layout,
584                                            const MCFragment *Fragment,
585                                            const MCFixup &Fixup,
586                                            MCValue Target,
587                                            uint64_t &FixedValue) {
588   int64_t Addend = 0;
589   int Index = 0;
590   int64_t Value = Target.getConstant();
591   const MCSymbol *Symbol = 0;
592
593   bool IsPCRel = isFixupKindX86PCRel(Fixup.getKind());
594   if (!Target.isAbsolute()) {
595     Symbol = &AliasedSymbol(Target.getSymA()->getSymbol());
596     MCSymbolData &SD = Asm.getSymbolData(*Symbol);
597     MCFragment *F = SD.getFragment();
598
599     if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
600       const MCSymbol &SymbolB = RefB->getSymbol();
601       MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
602       IsPCRel = true;
603       MCSectionData *Sec = Fragment->getParent();
604
605       // Offset of the symbol in the section
606       int64_t a = Layout.getSymbolAddress(&SDB) - Layout.getSectionAddress(Sec);
607
608       // Ofeset of the relocation in the section
609       int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
610       Value += b - a;
611     }
612
613     // Check that this case has already been fully resolved before we get
614     // here.
615     if (Symbol->isDefined() && !SD.isExternal() &&
616         IsPCRel &&
617         &Fragment->getParent()->getSection() == &Symbol->getSection()) {
618       llvm_unreachable("We don't need a relocation in this case.");
619       return;
620     }
621
622     bool RelocOnSymbol = ShouldRelocOnSymbol(SD, Target, *Fragment);
623     if (!RelocOnSymbol) {
624       Index = F->getParent()->getOrdinal();
625
626       MCSectionData *FSD = F->getParent();
627       // Offset of the symbol in the section
628       Value += Layout.getSymbolAddress(&SD) - Layout.getSectionAddress(FSD);
629     } else {
630       UsedInReloc.insert(Symbol);
631       Index = -1;
632     }
633     Addend = Value;
634     // Compensate for the addend on i386.
635     if (Is64Bit)
636       Value = 0;
637   }
638
639   FixedValue = Value;
640
641   // determine the type of the relocation
642
643   MCSymbolRefExpr::VariantKind Modifier = Target.getSymA()->getKind();
644   unsigned Type;
645   if (Is64Bit) {
646     if (IsPCRel) {
647       switch (Modifier) {
648       default:
649         llvm_unreachable("Unimplemented");
650       case MCSymbolRefExpr::VK_None:
651         Type = ELF::R_X86_64_PC32;
652         break;
653       case MCSymbolRefExpr::VK_PLT:
654         Type = ELF::R_X86_64_PLT32;
655         break;
656       case llvm::MCSymbolRefExpr::VK_GOTPCREL:
657         Type = ELF::R_X86_64_GOTPCREL;
658         break;
659       }
660     } else {
661       switch ((unsigned)Fixup.getKind()) {
662       default: llvm_unreachable("invalid fixup kind!");
663       case FK_Data_8: Type = ELF::R_X86_64_64; break;
664       case X86::reloc_signed_4byte:
665       case X86::reloc_pcrel_4byte:
666         assert(isInt<32>(Target.getConstant()));
667         switch (Modifier) {
668         default:
669           llvm_unreachable("Unimplemented");
670         case MCSymbolRefExpr::VK_None:
671           Type = ELF::R_X86_64_32S;
672           break;
673         case MCSymbolRefExpr::VK_GOT:
674           Type = ELF::R_X86_64_GOT32;
675           break;
676         case MCSymbolRefExpr::VK_GOTPCREL:
677           Type = ELF::R_X86_64_GOTPCREL;
678           break;
679         }
680         break;
681       case FK_Data_4:
682         Type = ELF::R_X86_64_32;
683         break;
684       case FK_Data_2: Type = ELF::R_X86_64_16; break;
685       case X86::reloc_pcrel_1byte:
686       case FK_Data_1: Type = ELF::R_X86_64_8; break;
687       }
688     }
689   } else {
690     if (IsPCRel) {
691       switch (Modifier) {
692       default:
693         llvm_unreachable("Unimplemented");
694       case MCSymbolRefExpr::VK_None:
695         Type = ELF::R_386_PC32;
696         break;
697       case MCSymbolRefExpr::VK_PLT:
698         Type = ELF::R_386_PLT32;
699         break;
700       }
701     } else {
702       switch ((unsigned)Fixup.getKind()) {
703       default: llvm_unreachable("invalid fixup kind!");
704
705       case X86::reloc_global_offset_table:
706         Type = ELF::R_386_GOTPC;
707         break;
708
709       // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
710       // instead?
711       case X86::reloc_signed_4byte:
712       case X86::reloc_pcrel_4byte:
713       case FK_Data_4:
714         switch (Modifier) {
715         default:
716           llvm_unreachable("Unimplemented");
717         case MCSymbolRefExpr::VK_None:
718           Type = ELF::R_386_32;
719           break;
720         case MCSymbolRefExpr::VK_GOT:
721           Type = ELF::R_386_GOT32;
722           break;
723         case MCSymbolRefExpr::VK_GOTOFF:
724           Type = ELF::R_386_GOTOFF;
725           break;
726         }
727         break;
728       case FK_Data_2: Type = ELF::R_386_16; break;
729       case X86::reloc_pcrel_1byte:
730       case FK_Data_1: Type = ELF::R_386_8; break;
731       }
732     }
733   }
734
735   if (RelocNeedsGOT(Type))
736     NeedsGOT = true;
737
738   ELFRelocationEntry ERE;
739
740   ERE.Index = Index;
741   ERE.Type = Type;
742   ERE.Symbol = Symbol;
743
744   ERE.r_offset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
745
746   if (HasRelocationAddend)
747     ERE.r_addend = Addend;
748   else
749     ERE.r_addend = 0; // Silence compiler warning.
750
751   Relocations[Fragment->getParent()].push_back(ERE);
752 }
753
754 uint64_t
755 ELFObjectWriterImpl::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
756                                                  const MCSymbol *S) {
757   MCSymbolData &SD = Asm.getSymbolData(*S);
758
759   // Local symbol.
760   if (!SD.isExternal() && !S->isUndefined())
761     return SD.getIndex() + /* empty symbol */ 1;
762
763   // External or undefined symbol.
764   return SD.getIndex() + NumRegularSections + /* empty symbol */ 1;
765 }
766
767 static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
768                        bool Used) {
769   if (Used)
770     return true;
771
772   const MCSymbol &Symbol = Data.getSymbol();
773
774   const MCSymbol &A = AliasedSymbol(Symbol);
775   if (&A != &Symbol && A.isUndefined())
776     return false;
777
778   if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
779     return false;
780
781   if (Symbol.isTemporary())
782     return false;
783
784   return true;
785 }
786
787 static bool isLocal(const MCSymbolData &Data) {
788   if (Data.isExternal())
789     return false;
790
791   const MCSymbol &Symbol = Data.getSymbol();
792   if (Symbol.isUndefined() && !Symbol.isVariable())
793     return false;
794
795   return true;
796 }
797
798 void ELFObjectWriterImpl::ComputeSymbolTable(MCAssembler &Asm) {
799   // FIXME: Is this the correct place to do this?
800   if (NeedsGOT) {
801     llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
802     MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
803     MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
804     Data.setExternal(true);
805   }
806
807   // Build section lookup table.
808   NumRegularSections = Asm.size();
809   DenseMap<const MCSection*, uint32_t> SectionIndexMap;
810   unsigned Index = 1;
811   for (MCAssembler::iterator it = Asm.begin(),
812          ie = Asm.end(); it != ie; ++it, ++Index)
813     SectionIndexMap[&it->getSection()] = Index;
814
815   // Index 0 is always the empty string.
816   StringMap<uint64_t> StringIndexMap;
817   StringTable += '\x00';
818
819   // Add the data for the symbols.
820   for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
821          ie = Asm.symbol_end(); it != ie; ++it) {
822     const MCSymbol &Symbol = it->getSymbol();
823
824     if (!isInSymtab(Asm, *it, UsedInReloc.count(&Symbol)))
825       continue;
826
827     ELFSymbolData MSD;
828     MSD.SymbolData = it;
829     bool Local = isLocal(*it);
830
831     if (it->isCommon()) {
832       assert(!Local);
833       MSD.SectionIndex = ELF::SHN_COMMON;
834     } else if (Symbol.isAbsolute()) {
835       MSD.SectionIndex = ELF::SHN_ABS;
836     } else if (Symbol.isVariable()) {
837       const MCSymbol &RefSymbol = AliasedSymbol(Symbol);
838       if (RefSymbol.isDefined()) {
839         MSD.SectionIndex = SectionIndexMap.lookup(&RefSymbol.getSection());
840         assert(MSD.SectionIndex && "Invalid section index!");
841       }
842     } else if (Symbol.isUndefined()) {
843       assert(!Local);
844       MSD.SectionIndex = ELF::SHN_UNDEF;
845       // FIXME: Undefined symbols are global, but this is the first place we
846       // are able to set it.
847       if (GetBinding(*it) == ELF::STB_LOCAL)
848         SetBinding(*it, ELF::STB_GLOBAL);
849     } else {
850       MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection());
851       assert(MSD.SectionIndex && "Invalid section index!");
852     }
853
854     uint64_t &Entry = StringIndexMap[Symbol.getName()];
855     if (!Entry) {
856       Entry = StringTable.size();
857       StringTable += Symbol.getName();
858       StringTable += '\x00';
859     }
860     MSD.StringIndex = Entry;
861     if (MSD.SectionIndex == ELF::SHN_UNDEF)
862       UndefinedSymbolData.push_back(MSD);
863     else if (Local)
864       LocalSymbolData.push_back(MSD);
865     else
866       ExternalSymbolData.push_back(MSD);
867   }
868
869   // Symbols are required to be in lexicographic order.
870   array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
871   array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
872   array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
873
874   // Set the symbol indices. Local symbols must come before all other
875   // symbols with non-local bindings.
876   Index = 0;
877   for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
878     LocalSymbolData[i].SymbolData->setIndex(Index++);
879   for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
880     ExternalSymbolData[i].SymbolData->setIndex(Index++);
881   for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
882     UndefinedSymbolData[i].SymbolData->setIndex(Index++);
883 }
884
885 void ELFObjectWriterImpl::WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
886                                           const MCSectionData &SD) {
887   if (!Relocations[&SD].empty()) {
888     MCContext &Ctx = Asm.getContext();
889     const MCSection *RelaSection;
890     const MCSectionELF &Section =
891       static_cast<const MCSectionELF&>(SD.getSection());
892
893     const StringRef SectionName = Section.getSectionName();
894     std::string RelaSectionName = HasRelocationAddend ? ".rela" : ".rel";
895     RelaSectionName += SectionName;
896
897     unsigned EntrySize;
898     if (HasRelocationAddend)
899       EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
900     else
901       EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
902
903     RelaSection = Ctx.getELFSection(RelaSectionName, HasRelocationAddend ?
904                                     ELF::SHT_RELA : ELF::SHT_REL, 0,
905                                     SectionKind::getReadOnly(),
906                                     false, EntrySize);
907
908     MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
909     RelaSD.setAlignment(Is64Bit ? 8 : 4);
910
911     MCDataFragment *F = new MCDataFragment(&RelaSD);
912
913     WriteRelocationsFragment(Asm, F, &SD);
914
915     Asm.AddSectionToTheEnd(*Writer, RelaSD, Layout);
916   }
917 }
918
919 void ELFObjectWriterImpl::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
920                                            uint64_t Flags, uint64_t Address,
921                                            uint64_t Offset, uint64_t Size,
922                                            uint32_t Link, uint32_t Info,
923                                            uint64_t Alignment,
924                                            uint64_t EntrySize) {
925   Write32(Name);        // sh_name: index into string table
926   Write32(Type);        // sh_type
927   WriteWord(Flags);     // sh_flags
928   WriteWord(Address);   // sh_addr
929   WriteWord(Offset);    // sh_offset
930   WriteWord(Size);      // sh_size
931   Write32(Link);        // sh_link
932   Write32(Info);        // sh_info
933   WriteWord(Alignment); // sh_addralign
934   WriteWord(EntrySize); // sh_entsize
935 }
936
937 void ELFObjectWriterImpl::WriteRelocationsFragment(const MCAssembler &Asm,
938                                                    MCDataFragment *F,
939                                                    const MCSectionData *SD) {
940   std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
941   // sort by the r_offset just like gnu as does
942   array_pod_sort(Relocs.begin(), Relocs.end());
943
944   for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
945     ELFRelocationEntry entry = Relocs[e - i - 1];
946
947     if (entry.Index < 0)
948       entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
949     else
950       entry.Index += LocalSymbolData.size() + 1;
951     if (Is64Bit) {
952       char buf[8];
953
954       String64(buf, entry.r_offset);
955       F->getContents() += StringRef(buf, 8);
956
957       struct ELF::Elf64_Rela ERE64;
958       ERE64.setSymbolAndType(entry.Index, entry.Type);
959       String64(buf, ERE64.r_info);
960       F->getContents() += StringRef(buf, 8);
961
962       if (HasRelocationAddend) {
963         String64(buf, entry.r_addend);
964         F->getContents() += StringRef(buf, 8);
965       }
966     } else {
967       char buf[4];
968
969       String32(buf, entry.r_offset);
970       F->getContents() += StringRef(buf, 4);
971
972       struct ELF::Elf32_Rela ERE32;
973       ERE32.setSymbolAndType(entry.Index, entry.Type);
974       String32(buf, ERE32.r_info);
975       F->getContents() += StringRef(buf, 4);
976
977       if (HasRelocationAddend) {
978         String32(buf, entry.r_addend);
979         F->getContents() += StringRef(buf, 4);
980       }
981     }
982   }
983 }
984
985 void ELFObjectWriterImpl::CreateMetadataSections(MCAssembler &Asm,
986                                                  MCAsmLayout &Layout) {
987   MCContext &Ctx = Asm.getContext();
988   MCDataFragment *F;
989
990   const MCSection *SymtabSection;
991   unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
992
993   unsigned NumRegularSections = Asm.size();
994
995   // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
996   const MCSection *ShstrtabSection;
997   ShstrtabSection = Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
998                                       SectionKind::getReadOnly(), false);
999   MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1000   ShstrtabSD.setAlignment(1);
1001   ShstrtabIndex = Asm.size();
1002
1003   SymtabSection = Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1004                                     SectionKind::getReadOnly(),
1005                                     false, EntrySize);
1006   MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
1007   SymtabSD.setAlignment(Is64Bit ? 8 : 4);
1008
1009   const MCSection *StrtabSection;
1010   StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
1011                                     SectionKind::getReadOnly(), false);
1012   MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1013   StrtabSD.setAlignment(1);
1014   StringTableIndex = Asm.size();
1015
1016   WriteRelocations(Asm, Layout);
1017
1018   // Symbol table
1019   F = new MCDataFragment(&SymtabSD);
1020   WriteSymbolTable(F, Asm, Layout, NumRegularSections);
1021   Asm.AddSectionToTheEnd(*Writer, SymtabSD, Layout);
1022
1023   F = new MCDataFragment(&StrtabSD);
1024   F->getContents().append(StringTable.begin(), StringTable.end());
1025   Asm.AddSectionToTheEnd(*Writer, StrtabSD, Layout);
1026
1027   F = new MCDataFragment(&ShstrtabSD);
1028
1029   // Section header string table.
1030   //
1031   // The first entry of a string table holds a null character so skip
1032   // section 0.
1033   uint64_t Index = 1;
1034   F->getContents() += '\x00';
1035
1036   for (MCAssembler::const_iterator it = Asm.begin(),
1037          ie = Asm.end(); it != ie; ++it) {
1038     const MCSectionELF &Section =
1039       static_cast<const MCSectionELF&>(it->getSection());
1040     // FIXME: We could merge suffixes like in .text and .rela.text.
1041
1042     // Remember the index into the string table so we can write it
1043     // into the sh_name field of the section header table.
1044     SectionStringTableIndex[&it->getSection()] = Index;
1045
1046     Index += Section.getSectionName().size() + 1;
1047     F->getContents() += Section.getSectionName();
1048     F->getContents() += '\x00';
1049   }
1050
1051   Asm.AddSectionToTheEnd(*Writer, ShstrtabSD, Layout);
1052 }
1053
1054 bool ELFObjectWriterImpl::IsFixupFullyResolved(const MCAssembler &Asm,
1055                                                const MCValue Target,
1056                                                bool IsPCRel,
1057                                                const MCFragment *DF) const {
1058   // If this is a PCrel relocation, find the section this fixup value is
1059   // relative to.
1060   const MCSection *BaseSection = 0;
1061   if (IsPCRel) {
1062     BaseSection = &DF->getParent()->getSection();
1063     assert(BaseSection);
1064   }
1065
1066   const MCSection *SectionA = 0;
1067   const MCSymbol *SymbolA = 0;
1068   if (const MCSymbolRefExpr *A = Target.getSymA()) {
1069     SymbolA = &A->getSymbol();
1070     SectionA = &SymbolA->getSection();
1071   }
1072
1073   const MCSection *SectionB = 0;
1074   if (const MCSymbolRefExpr *B = Target.getSymB()) {
1075     SectionB = &B->getSymbol().getSection();
1076   }
1077
1078   if (!BaseSection)
1079     return SectionA == SectionB;
1080
1081   const MCSymbolData &DataA = Asm.getSymbolData(*SymbolA);
1082   if (DataA.isExternal())
1083     return false;
1084
1085   return !SectionB && BaseSection == SectionA;
1086 }
1087
1088 void ELFObjectWriterImpl::WriteObject(MCAssembler &Asm,
1089                                       const MCAsmLayout &Layout) {
1090   // Compute symbol table information.
1091   ComputeSymbolTable(Asm);
1092
1093   CreateMetadataSections(const_cast<MCAssembler&>(Asm),
1094                          const_cast<MCAsmLayout&>(Layout));
1095
1096   // Add 1 for the null section.
1097   unsigned NumSections = Asm.size() + 1;
1098   uint64_t NaturalAlignment = Is64Bit ? 8 : 4;
1099   uint64_t HeaderSize = Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr);
1100   uint64_t FileOff = HeaderSize;
1101
1102   for (MCAssembler::const_iterator it = Asm.begin(),
1103          ie = Asm.end(); it != ie; ++it) {
1104     const MCSectionData &SD = *it;
1105
1106     FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1107
1108     // Get the size of the section in the output file (including padding).
1109     uint64_t Size = Layout.getSectionFileSize(&SD);
1110
1111     FileOff += Size;
1112   }
1113
1114   FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1115
1116   // Write out the ELF header ...
1117   WriteHeader(FileOff - HeaderSize, NumSections);
1118
1119   FileOff = HeaderSize;
1120
1121   // ... then all of the sections ...
1122   DenseMap<const MCSection*, uint64_t> SectionOffsetMap;
1123
1124   DenseMap<const MCSection*, uint32_t> SectionIndexMap;
1125
1126   unsigned Index = 1;
1127   for (MCAssembler::const_iterator it = Asm.begin(),
1128          ie = Asm.end(); it != ie; ++it) {
1129     const MCSectionData &SD = *it;
1130
1131     uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1132     WriteZeros(Padding);
1133     FileOff += Padding;
1134
1135     // Remember the offset into the file for this section.
1136     SectionOffsetMap[&it->getSection()] = FileOff;
1137     SectionIndexMap[&it->getSection()] = Index++;
1138
1139     FileOff += Layout.getSectionFileSize(&SD);
1140
1141     Asm.WriteSectionData(it, Layout, Writer);
1142   }
1143
1144   uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1145   WriteZeros(Padding);
1146   FileOff += Padding;
1147
1148   // ... and then the section header table.
1149   // Should we align the section header table?
1150   //
1151   // Null section first.
1152   WriteSecHdrEntry(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1153
1154   for (MCAssembler::const_iterator it = Asm.begin(),
1155          ie = Asm.end(); it != ie; ++it) {
1156     const MCSectionData &SD = *it;
1157     const MCSectionELF &Section =
1158       static_cast<const MCSectionELF&>(SD.getSection());
1159
1160     uint64_t sh_link = 0;
1161     uint64_t sh_info = 0;
1162
1163     switch(Section.getType()) {
1164     case ELF::SHT_DYNAMIC:
1165       sh_link = SectionStringTableIndex[&it->getSection()];
1166       sh_info = 0;
1167       break;
1168
1169     case ELF::SHT_REL:
1170     case ELF::SHT_RELA: {
1171       const MCSection *SymtabSection;
1172       const MCSection *InfoSection;
1173
1174       SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1175                                                      SectionKind::getReadOnly(),
1176                                                      false);
1177       sh_link = SectionIndexMap[SymtabSection];
1178
1179       // Remove ".rel" and ".rela" prefixes.
1180       unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1181       StringRef SectionName = Section.getSectionName().substr(SecNameLen);
1182
1183       InfoSection = Asm.getContext().getELFSection(SectionName,
1184                                                    ELF::SHT_PROGBITS, 0,
1185                                                    SectionKind::getReadOnly(),
1186                                                    false);
1187       sh_info = SectionIndexMap[InfoSection];
1188       break;
1189     }
1190
1191     case ELF::SHT_SYMTAB:
1192     case ELF::SHT_DYNSYM:
1193       sh_link = StringTableIndex;
1194       sh_info = LastLocalSymbolIndex;
1195       break;
1196
1197     case ELF::SHT_PROGBITS:
1198     case ELF::SHT_STRTAB:
1199     case ELF::SHT_NOBITS:
1200     case ELF::SHT_NULL:
1201     case ELF::SHT_ARM_ATTRIBUTES:
1202       // Nothing to do.
1203       break;
1204
1205     default:
1206       assert(0 && "FIXME: sh_type value not supported!");
1207       break;
1208     }
1209
1210     WriteSecHdrEntry(SectionStringTableIndex[&it->getSection()],
1211                      Section.getType(), Section.getFlags(),
1212                      0,
1213                      SectionOffsetMap.lookup(&SD.getSection()),
1214                      Layout.getSectionSize(&SD), sh_link,
1215                      sh_info, SD.getAlignment(),
1216                      Section.getEntrySize());
1217   }
1218 }
1219
1220 ELFObjectWriter::ELFObjectWriter(raw_ostream &OS,
1221                                  bool Is64Bit,
1222                                  Triple::OSType OSType,
1223                                  uint16_t EMachine,
1224                                  bool IsLittleEndian,
1225                                  bool HasRelocationAddend)
1226   : MCObjectWriter(OS, IsLittleEndian)
1227 {
1228   Impl = new ELFObjectWriterImpl(this, Is64Bit, EMachine,
1229                                  HasRelocationAddend, OSType);
1230 }
1231
1232 ELFObjectWriter::~ELFObjectWriter() {
1233   delete (ELFObjectWriterImpl*) Impl;
1234 }
1235
1236 void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm) {
1237   ((ELFObjectWriterImpl*) Impl)->ExecutePostLayoutBinding(Asm);
1238 }
1239
1240 void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
1241                                        const MCAsmLayout &Layout,
1242                                        const MCFragment *Fragment,
1243                                        const MCFixup &Fixup, MCValue Target,
1244                                        uint64_t &FixedValue) {
1245   ((ELFObjectWriterImpl*) Impl)->RecordRelocation(Asm, Layout, Fragment, Fixup,
1246                                                   Target, FixedValue);
1247 }
1248
1249 bool ELFObjectWriter::IsFixupFullyResolved(const MCAssembler &Asm,
1250                                            const MCValue Target,
1251                                            bool IsPCRel,
1252                                            const MCFragment *DF) const {
1253   return ((ELFObjectWriterImpl*) Impl)->IsFixupFullyResolved(Asm, Target,
1254                                                              IsPCRel, DF);
1255 }
1256
1257 void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1258                                   const MCAsmLayout &Layout) {
1259   ((ELFObjectWriterImpl*) Impl)->WriteObject(Asm, Layout);
1260 }