Make some symbols static, move classes into anonymous namespaces.
[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     assert (Value->getKind() == MCExpr::SymbolRef && "Unimplemented");
445     const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr*>(Value);
446     S = &Ref->getSymbol();
447   }
448   return *S;
449 }
450
451 void ELFObjectWriterImpl::WriteSymbol(MCDataFragment *F, ELFSymbolData &MSD,
452                                       const MCAsmLayout &Layout) {
453   MCSymbolData &OrigData = *MSD.SymbolData;
454   MCSymbolData &Data =
455     Layout.getAssembler().getSymbolData(AliasedSymbol(OrigData.getSymbol()));
456
457   uint8_t Binding = GetBinding(OrigData);
458   uint8_t Visibility = GetVisibility(OrigData);
459   uint8_t Type = GetType(Data);
460
461   uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
462   uint8_t Other = Visibility;
463
464   uint64_t Value = SymbolValue(Data, Layout);
465   uint64_t Size = 0;
466   const MCExpr *ESize;
467
468   assert(!(Data.isCommon() && !Data.isExternal()));
469
470   ESize = Data.getSize();
471   if (Data.getSize()) {
472     MCValue Res;
473     if (ESize->getKind() == MCExpr::Binary) {
474       const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(ESize);
475
476       if (BE->EvaluateAsRelocatable(Res, &Layout)) {
477         assert(!Res.getSymA() || !Res.getSymA()->getSymbol().isDefined());
478         assert(!Res.getSymB() || !Res.getSymB()->getSymbol().isDefined());
479         Size = Res.getConstant();
480       }
481     } else if (ESize->getKind() == MCExpr::Constant) {
482       Size = static_cast<const MCConstantExpr *>(ESize)->getValue();
483     } else {
484       assert(0 && "Unsupported size expression");
485     }
486   }
487
488   // Write out the symbol table entry
489   WriteSymbolEntry(F, MSD.StringIndex, Info, Value,
490                    Size, Other, MSD.SectionIndex);
491 }
492
493 void ELFObjectWriterImpl::WriteSymbolTable(MCDataFragment *F,
494                                            const MCAssembler &Asm,
495                                            const MCAsmLayout &Layout,
496                                            unsigned NumRegularSections) {
497   // The string table must be emitted first because we need the index
498   // into the string table for all the symbol names.
499   assert(StringTable.size() && "Missing string table");
500
501   // FIXME: Make sure the start of the symbol table is aligned.
502
503   // The first entry is the undefined symbol entry.
504   unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
505   F->getContents().append(EntrySize, '\x00');
506
507   // Write the symbol table entries.
508   LastLocalSymbolIndex = LocalSymbolData.size() + 1;
509   for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
510     ELFSymbolData &MSD = LocalSymbolData[i];
511     WriteSymbol(F, MSD, Layout);
512   }
513
514   // Write out a symbol table entry for each regular section.
515   unsigned Index = 1;
516   for (MCAssembler::const_iterator it = Asm.begin();
517        Index <= NumRegularSections; ++it, ++Index) {
518     const MCSectionELF &Section =
519       static_cast<const MCSectionELF&>(it->getSection());
520     // Leave out relocations so we don't have indexes within
521     // the relocations messed up
522     if (Section.getType() == ELF::SHT_RELA || Section.getType() == ELF::SHT_REL)
523       continue;
524     WriteSymbolEntry(F, 0, ELF::STT_SECTION, 0, 0, ELF::STV_DEFAULT, Index);
525     LastLocalSymbolIndex++;
526   }
527
528   for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
529     ELFSymbolData &MSD = ExternalSymbolData[i];
530     MCSymbolData &Data = *MSD.SymbolData;
531     assert(((Data.getFlags() & ELF_STB_Global) ||
532             (Data.getFlags() & ELF_STB_Weak)) &&
533            "External symbol requires STB_GLOBAL or STB_WEAK flag");
534     WriteSymbol(F, MSD, Layout);
535     if (GetBinding(Data) == ELF::STB_LOCAL)
536       LastLocalSymbolIndex++;
537   }
538
539   for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
540     ELFSymbolData &MSD = UndefinedSymbolData[i];
541     MCSymbolData &Data = *MSD.SymbolData;
542     WriteSymbol(F, MSD, Layout);
543     if (GetBinding(Data) == ELF::STB_LOCAL)
544       LastLocalSymbolIndex++;
545   }
546 }
547
548 static bool ShouldRelocOnSymbol(const MCSymbolData &SD,
549                                 const MCValue &Target,
550                                 const MCFragment &F) {
551   const MCSymbol &Symbol = SD.getSymbol();
552   if (Symbol.isUndefined())
553     return true;
554
555   const MCSectionELF &Section =
556     static_cast<const MCSectionELF&>(Symbol.getSection());
557
558   if (SD.isExternal())
559     return true;
560
561   MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
562   const MCSectionELF &Sec2 =
563     static_cast<const MCSectionELF&>(F.getParent()->getSection());
564
565   if (Section.getKind().isBSS())
566     return false;
567
568   if (&Sec2 != &Section &&
569       (Kind == MCSymbolRefExpr::VK_PLT ||
570        Kind == MCSymbolRefExpr::VK_GOTPCREL ||
571        Kind == MCSymbolRefExpr::VK_GOTOFF))
572     return true;
573
574   if (Section.getFlags() & MCSectionELF::SHF_MERGE)
575     return Target.getConstant() != 0;
576
577   return false;
578 }
579
580 // FIXME: this is currently X86/X86_64 only
581 void ELFObjectWriterImpl::RecordRelocation(const MCAssembler &Asm,
582                                            const MCAsmLayout &Layout,
583                                            const MCFragment *Fragment,
584                                            const MCFixup &Fixup,
585                                            MCValue Target,
586                                            uint64_t &FixedValue) {
587   int64_t Addend = 0;
588   int Index = 0;
589   int64_t Value = Target.getConstant();
590   const MCSymbol *Symbol = 0;
591
592   bool IsPCRel = isFixupKindX86PCRel(Fixup.getKind());
593   if (!Target.isAbsolute()) {
594     Symbol = &AliasedSymbol(Target.getSymA()->getSymbol());
595     MCSymbolData &SD = Asm.getSymbolData(*Symbol);
596     MCFragment *F = SD.getFragment();
597
598     if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
599       const MCSymbol &SymbolB = RefB->getSymbol();
600       MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
601       IsPCRel = true;
602       MCSectionData *Sec = Fragment->getParent();
603
604       // Offset of the symbol in the section
605       int64_t a = Layout.getSymbolAddress(&SDB) - Layout.getSectionAddress(Sec);
606
607       // Ofeset of the relocation in the section
608       int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
609       Value += b - a;
610     }
611
612     // Check that this case has already been fully resolved before we get
613     // here.
614     if (Symbol->isDefined() && !SD.isExternal() &&
615         IsPCRel &&
616         &Fragment->getParent()->getSection() == &Symbol->getSection()) {
617       llvm_unreachable("We don't need a relocation in this case.");
618       return;
619     }
620
621     bool RelocOnSymbol = ShouldRelocOnSymbol(SD, Target, *Fragment);
622     if (!RelocOnSymbol) {
623       Index = F->getParent()->getOrdinal();
624
625       MCSectionData *FSD = F->getParent();
626       // Offset of the symbol in the section
627       Value += Layout.getSymbolAddress(&SD) - Layout.getSectionAddress(FSD);
628     } else {
629       UsedInReloc.insert(Symbol);
630       Index = -1;
631     }
632     Addend = Value;
633     // Compensate for the addend on i386.
634     if (Is64Bit)
635       Value = 0;
636   }
637
638   FixedValue = Value;
639
640   // determine the type of the relocation
641
642   MCSymbolRefExpr::VariantKind Modifier = Target.getSymA()->getKind();
643   unsigned Type;
644   if (Is64Bit) {
645     if (IsPCRel) {
646       switch (Modifier) {
647       default:
648         llvm_unreachable("Unimplemented");
649       case MCSymbolRefExpr::VK_None:
650         Type = ELF::R_X86_64_PC32;
651         break;
652       case MCSymbolRefExpr::VK_PLT:
653         Type = ELF::R_X86_64_PLT32;
654         break;
655       case llvm::MCSymbolRefExpr::VK_GOTPCREL:
656         Type = ELF::R_X86_64_GOTPCREL;
657         break;
658       }
659     } else {
660       switch ((unsigned)Fixup.getKind()) {
661       default: llvm_unreachable("invalid fixup kind!");
662       case FK_Data_8: Type = ELF::R_X86_64_64; break;
663       case X86::reloc_signed_4byte:
664       case X86::reloc_pcrel_4byte:
665         assert(isInt<32>(Target.getConstant()));
666         switch (Modifier) {
667         default:
668           llvm_unreachable("Unimplemented");
669         case MCSymbolRefExpr::VK_None:
670           Type = ELF::R_X86_64_32S;
671           break;
672         case MCSymbolRefExpr::VK_GOT:
673           Type = ELF::R_X86_64_GOT32;
674           break;
675         case MCSymbolRefExpr::VK_GOTPCREL:
676           Type = ELF::R_X86_64_GOTPCREL;
677           break;
678         }
679         break;
680       case FK_Data_4:
681         Type = ELF::R_X86_64_32;
682         break;
683       case FK_Data_2: Type = ELF::R_X86_64_16; break;
684       case X86::reloc_pcrel_1byte:
685       case FK_Data_1: Type = ELF::R_X86_64_8; break;
686       }
687     }
688   } else {
689     if (IsPCRel) {
690       switch (Modifier) {
691       default:
692         llvm_unreachable("Unimplemented");
693       case MCSymbolRefExpr::VK_None:
694         Type = ELF::R_386_PC32;
695         break;
696       case MCSymbolRefExpr::VK_PLT:
697         Type = ELF::R_386_PLT32;
698         break;
699       }
700     } else {
701       switch ((unsigned)Fixup.getKind()) {
702       default: llvm_unreachable("invalid fixup kind!");
703
704       // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
705       // instead?
706       case X86::reloc_signed_4byte:
707       case X86::reloc_pcrel_4byte:
708       case FK_Data_4:
709         switch (Modifier) {
710         default:
711           llvm_unreachable("Unimplemented");
712         case MCSymbolRefExpr::VK_None:
713           if (Symbol->getName() == "_GLOBAL_OFFSET_TABLE_")
714             Type = ELF::R_386_GOTPC;
715           else
716             Type = ELF::R_386_32;
717           break;
718         case MCSymbolRefExpr::VK_GOT:
719           Type = ELF::R_386_GOT32;
720           break;
721         case MCSymbolRefExpr::VK_GOTOFF:
722           Type = ELF::R_386_GOTOFF;
723           break;
724         }
725         break;
726       case FK_Data_2: Type = ELF::R_386_16; break;
727       case X86::reloc_pcrel_1byte:
728       case FK_Data_1: Type = ELF::R_386_8; break;
729       }
730     }
731   }
732
733   if (RelocNeedsGOT(Type))
734     NeedsGOT = true;
735
736   ELFRelocationEntry ERE;
737
738   ERE.Index = Index;
739   ERE.Type = Type;
740   ERE.Symbol = Symbol;
741
742   ERE.r_offset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
743
744   if (HasRelocationAddend)
745     ERE.r_addend = Addend;
746   else
747     ERE.r_addend = 0; // Silence compiler warning.
748
749   Relocations[Fragment->getParent()].push_back(ERE);
750 }
751
752 uint64_t
753 ELFObjectWriterImpl::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
754                                                  const MCSymbol *S) {
755   MCSymbolData &SD = Asm.getSymbolData(*S);
756
757   // Local symbol.
758   if (!SD.isExternal() && !S->isUndefined())
759     return SD.getIndex() + /* empty symbol */ 1;
760
761   // External or undefined symbol.
762   return SD.getIndex() + NumRegularSections + /* empty symbol */ 1;
763 }
764
765 static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
766                        bool Used) {
767   if (Used)
768     return true;
769
770   const MCSymbol &Symbol = Data.getSymbol();
771   if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
772     return false;
773
774   if (Symbol.isTemporary())
775     return false;
776
777   return true;
778 }
779
780 static bool isLocal(const MCSymbolData &Data) {
781   if (Data.isExternal())
782     return false;
783
784   const MCSymbol &Symbol = Data.getSymbol();
785   if (Symbol.isUndefined() && !Symbol.isVariable())
786     return false;
787
788   return true;
789 }
790
791 void ELFObjectWriterImpl::ComputeSymbolTable(MCAssembler &Asm) {
792   // FIXME: Is this the correct place to do this?
793   if (NeedsGOT) {
794     llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
795     MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
796     MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
797     Data.setExternal(true);
798   }
799
800   // Build section lookup table.
801   NumRegularSections = Asm.size();
802   DenseMap<const MCSection*, uint32_t> SectionIndexMap;
803   unsigned Index = 1;
804   for (MCAssembler::iterator it = Asm.begin(),
805          ie = Asm.end(); it != ie; ++it, ++Index)
806     SectionIndexMap[&it->getSection()] = Index;
807
808   // Index 0 is always the empty string.
809   StringMap<uint64_t> StringIndexMap;
810   StringTable += '\x00';
811
812   // Add the data for the symbols.
813   for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
814          ie = Asm.symbol_end(); it != ie; ++it) {
815     const MCSymbol &Symbol = it->getSymbol();
816
817     if (!isInSymtab(Asm, *it, UsedInReloc.count(&Symbol)))
818       continue;
819
820     ELFSymbolData MSD;
821     MSD.SymbolData = it;
822     bool Local = isLocal(*it);
823
824     bool Add = false;
825     if (it->isCommon()) {
826       assert(!Local);
827       MSD.SectionIndex = ELF::SHN_COMMON;
828       Add = true;
829     } else if (Symbol.isAbsolute()) {
830       MSD.SectionIndex = ELF::SHN_ABS;
831       Add = true;
832     } else if (Symbol.isVariable()) {
833       const MCSymbol &RefSymbol = AliasedSymbol(Symbol);
834       if (RefSymbol.isDefined()) {
835         MSD.SectionIndex = SectionIndexMap.lookup(&RefSymbol.getSection());
836         assert(MSD.SectionIndex && "Invalid section index!");
837         Add = true;
838       }
839     } else if (Symbol.isUndefined()) {
840       assert(!Local);
841       MSD.SectionIndex = ELF::SHN_UNDEF;
842       // FIXME: Undefined symbols are global, but this is the first place we
843       // are able to set it.
844       if (GetBinding(*it) == ELF::STB_LOCAL)
845         SetBinding(*it, ELF::STB_GLOBAL);
846       Add = true;
847     } else {
848       MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection());
849       assert(MSD.SectionIndex && "Invalid section index!");
850       Add = true;
851     }
852
853     if (Add) {
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
870   // Symbols are required to be in lexicographic order.
871   array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
872   array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
873   array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
874
875   // Set the symbol indices. Local symbols must come before all other
876   // symbols with non-local bindings.
877   Index = 0;
878   for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
879     LocalSymbolData[i].SymbolData->setIndex(Index++);
880   for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
881     ExternalSymbolData[i].SymbolData->setIndex(Index++);
882   for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
883     UndefinedSymbolData[i].SymbolData->setIndex(Index++);
884 }
885
886 void ELFObjectWriterImpl::WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
887                                           const MCSectionData &SD) {
888   if (!Relocations[&SD].empty()) {
889     MCContext &Ctx = Asm.getContext();
890     const MCSection *RelaSection;
891     const MCSectionELF &Section =
892       static_cast<const MCSectionELF&>(SD.getSection());
893
894     const StringRef SectionName = Section.getSectionName();
895     std::string RelaSectionName = HasRelocationAddend ? ".rela" : ".rel";
896     RelaSectionName += SectionName;
897
898     unsigned EntrySize;
899     if (HasRelocationAddend)
900       EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
901     else
902       EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
903
904     RelaSection = Ctx.getELFSection(RelaSectionName, HasRelocationAddend ?
905                                     ELF::SHT_RELA : ELF::SHT_REL, 0,
906                                     SectionKind::getReadOnly(),
907                                     false, EntrySize);
908
909     MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
910     RelaSD.setAlignment(Is64Bit ? 8 : 4);
911
912     MCDataFragment *F = new MCDataFragment(&RelaSD);
913
914     WriteRelocationsFragment(Asm, F, &SD);
915
916     Asm.AddSectionToTheEnd(*Writer, RelaSD, Layout);
917   }
918 }
919
920 void ELFObjectWriterImpl::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
921                                            uint64_t Flags, uint64_t Address,
922                                            uint64_t Offset, uint64_t Size,
923                                            uint32_t Link, uint32_t Info,
924                                            uint64_t Alignment,
925                                            uint64_t EntrySize) {
926   Write32(Name);        // sh_name: index into string table
927   Write32(Type);        // sh_type
928   WriteWord(Flags);     // sh_flags
929   WriteWord(Address);   // sh_addr
930   WriteWord(Offset);    // sh_offset
931   WriteWord(Size);      // sh_size
932   Write32(Link);        // sh_link
933   Write32(Info);        // sh_info
934   WriteWord(Alignment); // sh_addralign
935   WriteWord(EntrySize); // sh_entsize
936 }
937
938 void ELFObjectWriterImpl::WriteRelocationsFragment(const MCAssembler &Asm,
939                                                    MCDataFragment *F,
940                                                    const MCSectionData *SD) {
941   std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
942   // sort by the r_offset just like gnu as does
943   array_pod_sort(Relocs.begin(), Relocs.end());
944
945   for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
946     ELFRelocationEntry entry = Relocs[e - i - 1];
947
948     if (entry.Index < 0)
949       entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
950     else
951       entry.Index += LocalSymbolData.size() + 1;
952     if (Is64Bit) {
953       char buf[8];
954
955       String64(buf, entry.r_offset);
956       F->getContents() += StringRef(buf, 8);
957
958       struct ELF::Elf64_Rela ERE64;
959       ERE64.setSymbolAndType(entry.Index, entry.Type);
960       String64(buf, ERE64.r_info);
961       F->getContents() += StringRef(buf, 8);
962
963       if (HasRelocationAddend) {
964         String64(buf, entry.r_addend);
965         F->getContents() += StringRef(buf, 8);
966       }
967     } else {
968       char buf[4];
969
970       String32(buf, entry.r_offset);
971       F->getContents() += StringRef(buf, 4);
972
973       struct ELF::Elf32_Rela ERE32;
974       ERE32.setSymbolAndType(entry.Index, entry.Type);
975       String32(buf, ERE32.r_info);
976       F->getContents() += StringRef(buf, 4);
977
978       if (HasRelocationAddend) {
979         String32(buf, entry.r_addend);
980         F->getContents() += StringRef(buf, 4);
981       }
982     }
983   }
984 }
985
986 void ELFObjectWriterImpl::CreateMetadataSections(MCAssembler &Asm,
987                                                  MCAsmLayout &Layout) {
988   MCContext &Ctx = Asm.getContext();
989   MCDataFragment *F;
990
991   const MCSection *SymtabSection;
992   unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
993
994   unsigned NumRegularSections = Asm.size();
995
996   // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
997   const MCSection *ShstrtabSection;
998   ShstrtabSection = Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
999                                       SectionKind::getReadOnly(), false);
1000   MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1001   ShstrtabSD.setAlignment(1);
1002   ShstrtabIndex = Asm.size();
1003
1004   SymtabSection = Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1005                                     SectionKind::getReadOnly(),
1006                                     false, EntrySize);
1007   MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
1008   SymtabSD.setAlignment(Is64Bit ? 8 : 4);
1009
1010   const MCSection *StrtabSection;
1011   StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
1012                                     SectionKind::getReadOnly(), false);
1013   MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1014   StrtabSD.setAlignment(1);
1015   StringTableIndex = Asm.size();
1016
1017   WriteRelocations(Asm, Layout);
1018
1019   // Symbol table
1020   F = new MCDataFragment(&SymtabSD);
1021   WriteSymbolTable(F, Asm, Layout, NumRegularSections);
1022   Asm.AddSectionToTheEnd(*Writer, SymtabSD, Layout);
1023
1024   F = new MCDataFragment(&StrtabSD);
1025   F->getContents().append(StringTable.begin(), StringTable.end());
1026   Asm.AddSectionToTheEnd(*Writer, StrtabSD, Layout);
1027
1028   F = new MCDataFragment(&ShstrtabSD);
1029
1030   // Section header string table.
1031   //
1032   // The first entry of a string table holds a null character so skip
1033   // section 0.
1034   uint64_t Index = 1;
1035   F->getContents() += '\x00';
1036
1037   for (MCAssembler::const_iterator it = Asm.begin(),
1038          ie = Asm.end(); it != ie; ++it) {
1039     const MCSectionELF &Section =
1040       static_cast<const MCSectionELF&>(it->getSection());
1041     // FIXME: We could merge suffixes like in .text and .rela.text.
1042
1043     // Remember the index into the string table so we can write it
1044     // into the sh_name field of the section header table.
1045     SectionStringTableIndex[&it->getSection()] = Index;
1046
1047     Index += Section.getSectionName().size() + 1;
1048     F->getContents() += Section.getSectionName();
1049     F->getContents() += '\x00';
1050   }
1051
1052   Asm.AddSectionToTheEnd(*Writer, ShstrtabSD, Layout);
1053 }
1054
1055 bool ELFObjectWriterImpl::IsFixupFullyResolved(const MCAssembler &Asm,
1056                                                const MCValue Target,
1057                                                bool IsPCRel,
1058                                                const MCFragment *DF) const {
1059   // If this is a PCrel relocation, find the section this fixup value is
1060   // relative to.
1061   const MCSection *BaseSection = 0;
1062   if (IsPCRel) {
1063     BaseSection = &DF->getParent()->getSection();
1064     assert(BaseSection);
1065   }
1066
1067   const MCSection *SectionA = 0;
1068   const MCSymbol *SymbolA = 0;
1069   if (const MCSymbolRefExpr *A = Target.getSymA()) {
1070     SymbolA = &A->getSymbol();
1071     SectionA = &SymbolA->getSection();
1072   }
1073
1074   const MCSection *SectionB = 0;
1075   if (const MCSymbolRefExpr *B = Target.getSymB()) {
1076     SectionB = &B->getSymbol().getSection();
1077   }
1078
1079   if (!BaseSection)
1080     return SectionA == SectionB;
1081
1082   const MCSymbolData &DataA = Asm.getSymbolData(*SymbolA);
1083   if (DataA.isExternal())
1084     return false;
1085
1086   return !SectionB && BaseSection == SectionA;
1087 }
1088
1089 void ELFObjectWriterImpl::WriteObject(MCAssembler &Asm,
1090                                       const MCAsmLayout &Layout) {
1091   // Compute symbol table information.
1092   ComputeSymbolTable(Asm);
1093
1094   CreateMetadataSections(const_cast<MCAssembler&>(Asm),
1095                          const_cast<MCAsmLayout&>(Layout));
1096
1097   // Add 1 for the null section.
1098   unsigned NumSections = Asm.size() + 1;
1099   uint64_t NaturalAlignment = Is64Bit ? 8 : 4;
1100   uint64_t HeaderSize = Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr);
1101   uint64_t FileOff = HeaderSize;
1102
1103   for (MCAssembler::const_iterator it = Asm.begin(),
1104          ie = Asm.end(); it != ie; ++it) {
1105     const MCSectionData &SD = *it;
1106
1107     FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1108
1109     // Get the size of the section in the output file (including padding).
1110     uint64_t Size = Layout.getSectionFileSize(&SD);
1111
1112     FileOff += Size;
1113   }
1114
1115   FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1116
1117   // Write out the ELF header ...
1118   WriteHeader(FileOff - HeaderSize, NumSections);
1119
1120   FileOff = HeaderSize;
1121
1122   // ... then all of the sections ...
1123   DenseMap<const MCSection*, uint64_t> SectionOffsetMap;
1124
1125   DenseMap<const MCSection*, uint32_t> SectionIndexMap;
1126
1127   unsigned Index = 1;
1128   for (MCAssembler::const_iterator it = Asm.begin(),
1129          ie = Asm.end(); it != ie; ++it) {
1130     const MCSectionData &SD = *it;
1131
1132     uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1133     WriteZeros(Padding);
1134     FileOff += Padding;
1135
1136     // Remember the offset into the file for this section.
1137     SectionOffsetMap[&it->getSection()] = FileOff;
1138     SectionIndexMap[&it->getSection()] = Index++;
1139
1140     FileOff += Layout.getSectionFileSize(&SD);
1141
1142     Asm.WriteSectionData(it, Layout, Writer);
1143   }
1144
1145   uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1146   WriteZeros(Padding);
1147   FileOff += Padding;
1148
1149   // ... and then the section header table.
1150   // Should we align the section header table?
1151   //
1152   // Null section first.
1153   WriteSecHdrEntry(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1154
1155   for (MCAssembler::const_iterator it = Asm.begin(),
1156          ie = Asm.end(); it != ie; ++it) {
1157     const MCSectionData &SD = *it;
1158     const MCSectionELF &Section =
1159       static_cast<const MCSectionELF&>(SD.getSection());
1160
1161     uint64_t sh_link = 0;
1162     uint64_t sh_info = 0;
1163
1164     switch(Section.getType()) {
1165     case ELF::SHT_DYNAMIC:
1166       sh_link = SectionStringTableIndex[&it->getSection()];
1167       sh_info = 0;
1168       break;
1169
1170     case ELF::SHT_REL:
1171     case ELF::SHT_RELA: {
1172       const MCSection *SymtabSection;
1173       const MCSection *InfoSection;
1174
1175       SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1176                                                      SectionKind::getReadOnly(),
1177                                                      false);
1178       sh_link = SectionIndexMap[SymtabSection];
1179
1180       // Remove ".rel" and ".rela" prefixes.
1181       unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1182       StringRef SectionName = Section.getSectionName().substr(SecNameLen);
1183
1184       InfoSection = Asm.getContext().getELFSection(SectionName,
1185                                                    ELF::SHT_PROGBITS, 0,
1186                                                    SectionKind::getReadOnly(),
1187                                                    false);
1188       sh_info = SectionIndexMap[InfoSection];
1189       break;
1190     }
1191
1192     case ELF::SHT_SYMTAB:
1193     case ELF::SHT_DYNSYM:
1194       sh_link = StringTableIndex;
1195       sh_info = LastLocalSymbolIndex;
1196       break;
1197
1198     case ELF::SHT_PROGBITS:
1199     case ELF::SHT_STRTAB:
1200     case ELF::SHT_NOBITS:
1201     case ELF::SHT_NULL:
1202       // Nothing to do.
1203       break;
1204
1205     case ELF::SHT_HASH:
1206     case ELF::SHT_GROUP:
1207     case ELF::SHT_SYMTAB_SHNDX:
1208     default:
1209       assert(0 && "FIXME: sh_type value not supported!");
1210       break;
1211     }
1212
1213     WriteSecHdrEntry(SectionStringTableIndex[&it->getSection()],
1214                      Section.getType(), Section.getFlags(),
1215                      0,
1216                      SectionOffsetMap.lookup(&SD.getSection()),
1217                      Layout.getSectionSize(&SD), sh_link,
1218                      sh_info, SD.getAlignment(),
1219                      Section.getEntrySize());
1220   }
1221 }
1222
1223 ELFObjectWriter::ELFObjectWriter(raw_ostream &OS,
1224                                  bool Is64Bit,
1225                                  Triple::OSType OSType,
1226                                  uint16_t EMachine,
1227                                  bool IsLittleEndian,
1228                                  bool HasRelocationAddend)
1229   : MCObjectWriter(OS, IsLittleEndian)
1230 {
1231   Impl = new ELFObjectWriterImpl(this, Is64Bit, EMachine,
1232                                  HasRelocationAddend, OSType);
1233 }
1234
1235 ELFObjectWriter::~ELFObjectWriter() {
1236   delete (ELFObjectWriterImpl*) Impl;
1237 }
1238
1239 void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm) {
1240   ((ELFObjectWriterImpl*) Impl)->ExecutePostLayoutBinding(Asm);
1241 }
1242
1243 void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
1244                                        const MCAsmLayout &Layout,
1245                                        const MCFragment *Fragment,
1246                                        const MCFixup &Fixup, MCValue Target,
1247                                        uint64_t &FixedValue) {
1248   ((ELFObjectWriterImpl*) Impl)->RecordRelocation(Asm, Layout, Fragment, Fixup,
1249                                                   Target, FixedValue);
1250 }
1251
1252 bool ELFObjectWriter::IsFixupFullyResolved(const MCAssembler &Asm,
1253                                            const MCValue Target,
1254                                            bool IsPCRel,
1255                                            const MCFragment *DF) const {
1256   return ((ELFObjectWriterImpl*) Impl)->IsFixupFullyResolved(Asm, Target,
1257                                                              IsPCRel, DF);
1258 }
1259
1260 void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1261                                   const MCAsmLayout &Layout) {
1262   ((ELFObjectWriterImpl*) Impl)->WriteObject(Asm, Layout);
1263 }