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