1 //===- lib/MC/ELFObjectWriter.cpp - ELF File Writer -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements ELF object file writer information.
12 //===----------------------------------------------------------------------===//
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"
33 #include "../Target/X86/X86FixupKinds.h"
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);
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);
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));
61 static unsigned GetVisibility(MCSymbolData &SD) {
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);
69 static bool isFixupKindX86PCRel(unsigned Kind) {
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:
81 static bool RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant) {
85 case MCSymbolRefExpr::VK_GOT:
86 case MCSymbolRefExpr::VK_PLT:
87 case MCSymbolRefExpr::VK_GOTPCREL:
88 case MCSymbolRefExpr::VK_TPOFF:
89 case MCSymbolRefExpr::VK_TLSGD:
90 case MCSymbolRefExpr::VK_GOTTPOFF:
91 case MCSymbolRefExpr::VK_INDNTPOFF:
92 case MCSymbolRefExpr::VK_NTPOFF:
93 case MCSymbolRefExpr::VK_GOTNTPOFF:
94 case MCSymbolRefExpr::VK_TLSLDM:
95 case MCSymbolRefExpr::VK_DTPOFF:
96 case MCSymbolRefExpr::VK_TLSLD:
103 class ELFObjectWriterImpl {
104 /*static bool isFixupKindX86RIPRel(unsigned Kind) {
105 return Kind == X86::reloc_riprel_4byte ||
106 Kind == X86::reloc_riprel_4byte_movq_load;
110 /// ELFSymbolData - Helper struct for containing some precomputed information
112 struct ELFSymbolData {
113 MCSymbolData *SymbolData;
114 uint64_t StringIndex;
115 uint32_t SectionIndex;
117 // Support lexicographic sorting.
118 bool operator<(const ELFSymbolData &RHS) const {
119 if (GetType(*SymbolData) == ELF::STT_FILE)
121 if (GetType(*RHS.SymbolData) == ELF::STT_FILE)
123 return SymbolData->getSymbol().getName() <
124 RHS.SymbolData->getSymbol().getName();
128 /// @name Relocation Data
131 struct ELFRelocationEntry {
132 // Make these big enough for both 32-bit and 64-bit
136 const MCSymbol *Symbol;
139 // Support lexicographic sorting.
140 bool operator<(const ELFRelocationEntry &RE) const {
141 return RE.r_offset < r_offset;
145 SmallPtrSet<const MCSymbol *, 16> UsedInReloc;
146 SmallPtrSet<const MCSymbol *, 16> WeakrefUsedInReloc;
147 DenseMap<const MCSymbol *, const MCSymbol *> Renames;
149 llvm::DenseMap<const MCSectionData*,
150 std::vector<ELFRelocationEntry> > Relocations;
151 DenseMap<const MCSection*, uint64_t> SectionStringTableIndex;
154 /// @name Symbol Table Data
157 SmallString<256> StringTable;
158 std::vector<ELFSymbolData> LocalSymbolData;
159 std::vector<ELFSymbolData> ExternalSymbolData;
160 std::vector<ELFSymbolData> UndefinedSymbolData;
164 int NumRegularSections;
168 bool NeedsSymtabShndx;
170 ELFObjectWriter *Writer;
174 unsigned Is64Bit : 1;
176 bool HasRelocationAddend;
178 Triple::OSType OSType;
182 // This holds the symbol table index of the last local symbol.
183 unsigned LastLocalSymbolIndex;
184 // This holds the .strtab section index.
185 unsigned StringTableIndex;
186 // This holds the .symtab section index.
187 unsigned SymbolTableIndex;
189 unsigned ShstrtabIndex;
192 ELFObjectWriterImpl(ELFObjectWriter *_Writer, bool _Is64Bit,
193 uint16_t _EMachine, bool _HasRelAddend,
194 Triple::OSType _OSType)
195 : NeedsGOT(false), NeedsSymtabShndx(false), Writer(_Writer),
196 OS(Writer->getStream()),
197 Is64Bit(_Is64Bit), HasRelocationAddend(_HasRelAddend),
198 OSType(_OSType), EMachine(_EMachine) {
201 void Write8(uint8_t Value) { Writer->Write8(Value); }
202 void Write16(uint16_t Value) { Writer->Write16(Value); }
203 void Write32(uint32_t Value) { Writer->Write32(Value); }
204 //void Write64(uint64_t Value) { Writer->Write64(Value); }
205 void WriteZeros(unsigned N) { Writer->WriteZeros(N); }
206 //void WriteBytes(StringRef Str, unsigned ZeroFillSize = 0) {
207 // Writer->WriteBytes(Str, ZeroFillSize);
210 void WriteWord(uint64_t W) {
217 void StringLE16(char *buf, uint16_t Value) {
218 buf[0] = char(Value >> 0);
219 buf[1] = char(Value >> 8);
222 void StringLE32(char *buf, uint32_t Value) {
223 StringLE16(buf, uint16_t(Value >> 0));
224 StringLE16(buf + 2, uint16_t(Value >> 16));
227 void StringLE64(char *buf, uint64_t Value) {
228 StringLE32(buf, uint32_t(Value >> 0));
229 StringLE32(buf + 4, uint32_t(Value >> 32));
232 void StringBE16(char *buf ,uint16_t Value) {
233 buf[0] = char(Value >> 8);
234 buf[1] = char(Value >> 0);
237 void StringBE32(char *buf, uint32_t Value) {
238 StringBE16(buf, uint16_t(Value >> 16));
239 StringBE16(buf + 2, uint16_t(Value >> 0));
242 void StringBE64(char *buf, uint64_t Value) {
243 StringBE32(buf, uint32_t(Value >> 32));
244 StringBE32(buf + 4, uint32_t(Value >> 0));
247 void String8(MCDataFragment &F, uint8_t Value) {
250 F.getContents() += StringRef(buf, 1);
253 void String16(MCDataFragment &F, uint16_t Value) {
255 if (Writer->isLittleEndian())
256 StringLE16(buf, Value);
258 StringBE16(buf, Value);
259 F.getContents() += StringRef(buf, 2);
262 void String32(MCDataFragment &F, uint32_t Value) {
264 if (Writer->isLittleEndian())
265 StringLE32(buf, Value);
267 StringBE32(buf, Value);
268 F.getContents() += StringRef(buf, 4);
271 void String64(MCDataFragment &F, uint64_t Value) {
273 if (Writer->isLittleEndian())
274 StringLE64(buf, Value);
276 StringBE64(buf, Value);
277 F.getContents() += StringRef(buf, 8);
280 void WriteHeader(uint64_t SectionDataSize, unsigned NumberOfSections);
282 void WriteSymbolEntry(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
283 uint64_t name, uint8_t info,
284 uint64_t value, uint64_t size,
285 uint8_t other, uint32_t shndx,
288 void WriteSymbol(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
290 const MCAsmLayout &Layout);
292 typedef DenseMap<const MCSectionELF*, uint32_t> SectionIndexMapTy;
293 void WriteSymbolTable(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
294 const MCAssembler &Asm,
295 const MCAsmLayout &Layout,
296 unsigned NumRegularSections);
298 void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
299 const MCFragment *Fragment, const MCFixup &Fixup,
300 MCValue Target, uint64_t &FixedValue);
302 uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm,
305 /// ComputeSymbolTable - Compute the symbol table data
307 /// \param StringTable [out] - The string table data.
308 /// \param StringIndexMap [out] - Map from symbol names to offsets in the
310 void ComputeSymbolTable(MCAssembler &Asm,
311 const SectionIndexMapTy &SectionIndexMap);
313 void ComputeIndexMap(MCAssembler &Asm,
314 SectionIndexMapTy &SectionIndexMap);
316 void WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
317 const MCSectionData &SD);
319 void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout) {
320 for (MCAssembler::const_iterator it = Asm.begin(),
321 ie = Asm.end(); it != ie; ++it) {
322 WriteRelocation(Asm, Layout, *it);
326 void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout);
328 void ExecutePostLayoutBinding(MCAssembler &Asm);
330 void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
331 uint64_t Address, uint64_t Offset,
332 uint64_t Size, uint32_t Link, uint32_t Info,
333 uint64_t Alignment, uint64_t EntrySize);
335 void WriteRelocationsFragment(const MCAssembler &Asm, MCDataFragment *F,
336 const MCSectionData *SD);
338 bool IsFixupFullyResolved(const MCAssembler &Asm,
339 const MCValue Target,
341 const MCFragment *DF) const;
343 void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
348 // Emit the ELF header.
349 void ELFObjectWriterImpl::WriteHeader(uint64_t SectionDataSize,
350 unsigned NumberOfSections) {
356 // emitWord method behaves differently for ELF32 and ELF64, writing
357 // 4 bytes in the former and 8 in the latter.
359 Write8(0x7f); // e_ident[EI_MAG0]
360 Write8('E'); // e_ident[EI_MAG1]
361 Write8('L'); // e_ident[EI_MAG2]
362 Write8('F'); // e_ident[EI_MAG3]
364 Write8(Is64Bit ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
367 Write8(Writer->isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
369 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
372 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
373 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
374 default: Write8(ELF::ELFOSABI_NONE); break;
376 Write8(0); // e_ident[EI_ABIVERSION]
378 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
380 Write16(ELF::ET_REL); // e_type
382 Write16(EMachine); // e_machine = target
384 Write32(ELF::EV_CURRENT); // e_version
385 WriteWord(0); // e_entry, no entry point in .o file
386 WriteWord(0); // e_phoff, no program header for .o
387 WriteWord(SectionDataSize + (Is64Bit ? sizeof(ELF::Elf64_Ehdr) :
388 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
390 // FIXME: Make this configurable.
391 Write32(0); // e_flags = whatever the target wants
393 // e_ehsize = ELF header size
394 Write16(Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
396 Write16(0); // e_phentsize = prog header entry size
397 Write16(0); // e_phnum = # prog header entries = 0
399 // e_shentsize = Section header entry size
400 Write16(Is64Bit ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
402 // e_shnum = # of section header ents
403 if (NumberOfSections >= ELF::SHN_LORESERVE)
406 Write16(NumberOfSections);
408 // e_shstrndx = Section # of '.shstrtab'
409 if (NumberOfSections >= ELF::SHN_LORESERVE)
410 Write16(ELF::SHN_XINDEX);
412 Write16(ShstrtabIndex);
415 void ELFObjectWriterImpl::WriteSymbolEntry(MCDataFragment *SymtabF,
416 MCDataFragment *ShndxF,
418 uint8_t info, uint64_t value,
419 uint64_t size, uint8_t other,
423 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
424 String32(*ShndxF, shndx);
426 String32(*ShndxF, 0);
429 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
430 uint16_t(ELF::SHN_XINDEX) : shndx;
433 String32(*SymtabF, name); // st_name
434 String8(*SymtabF, info); // st_info
435 String8(*SymtabF, other); // st_other
436 String16(*SymtabF, Index); // st_shndx
437 String64(*SymtabF, value); // st_value
438 String64(*SymtabF, size); // st_size
440 String32(*SymtabF, name); // st_name
441 String32(*SymtabF, value); // st_value
442 String32(*SymtabF, size); // st_size
443 String8(*SymtabF, info); // st_info
444 String8(*SymtabF, other); // st_other
445 String16(*SymtabF, Index); // st_shndx
449 static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout) {
450 if (Data.isCommon() && Data.isExternal())
451 return Data.getCommonAlignment();
453 const MCSymbol &Symbol = Data.getSymbol();
454 if (!Symbol.isInSection())
457 if (MCFragment *FF = Data.getFragment())
458 return Layout.getSymbolAddress(&Data) -
459 Layout.getSectionAddress(FF->getParent());
464 static const MCSymbol &AliasedSymbol(const MCSymbol &Symbol) {
465 const MCSymbol *S = &Symbol;
466 while (S->isVariable()) {
467 const MCExpr *Value = S->getVariableValue();
468 if (Value->getKind() != MCExpr::SymbolRef)
470 const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr*>(Value);
471 S = &Ref->getSymbol();
476 void ELFObjectWriterImpl::ExecutePostLayoutBinding(MCAssembler &Asm) {
477 // The presence of symbol versions causes undefined symbols and
478 // versions declared with @@@ to be renamed.
480 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
481 ie = Asm.symbol_end(); it != ie; ++it) {
482 const MCSymbol &Alias = it->getSymbol();
483 const MCSymbol &Symbol = AliasedSymbol(Alias);
484 MCSymbolData &SD = Asm.getSymbolData(Symbol);
486 // Undefined symbols are global, but this is the first place we
487 // are able to set it.
488 if (Symbol.isUndefined() && !Symbol.isVariable()) {
489 if (GetBinding(SD) == ELF::STB_LOCAL) {
490 SetBinding(SD, ELF::STB_GLOBAL);
491 SetBinding(*it, ELF::STB_GLOBAL);
496 if (&Symbol == &Alias)
499 StringRef AliasName = Alias.getName();
500 size_t Pos = AliasName.find('@');
501 if (Pos == StringRef::npos)
504 // Aliases defined with .symvar copy the binding from the symbol they alias.
505 // This is the first place we are able to copy this information.
506 it->setExternal(SD.isExternal());
507 SetBinding(*it, GetBinding(SD));
509 StringRef Rest = AliasName.substr(Pos);
510 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
513 // FIXME: produce a better error message.
514 if (Symbol.isUndefined() && Rest.startswith("@@") &&
515 !Rest.startswith("@@@"))
516 report_fatal_error("A @@ version cannot be undefined");
518 Renames.insert(std::make_pair(&Symbol, &Alias));
522 void ELFObjectWriterImpl::WriteSymbol(MCDataFragment *SymtabF,
523 MCDataFragment *ShndxF,
525 const MCAsmLayout &Layout) {
526 MCSymbolData &OrigData = *MSD.SymbolData;
528 Layout.getAssembler().getSymbolData(AliasedSymbol(OrigData.getSymbol()));
530 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
531 Data.getSymbol().isVariable();
533 uint8_t Binding = GetBinding(OrigData);
534 uint8_t Visibility = GetVisibility(OrigData);
535 uint8_t Type = GetType(Data);
537 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
538 uint8_t Other = Visibility;
540 uint64_t Value = SymbolValue(Data, Layout);
544 assert(!(Data.isCommon() && !Data.isExternal()));
546 ESize = Data.getSize();
547 if (Data.getSize()) {
549 if (ESize->getKind() == MCExpr::Binary) {
550 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(ESize);
552 if (BE->EvaluateAsRelocatable(Res, &Layout)) {
553 assert(!Res.getSymA() || !Res.getSymA()->getSymbol().isDefined());
554 assert(!Res.getSymB() || !Res.getSymB()->getSymbol().isDefined());
555 Size = Res.getConstant();
557 } else if (ESize->getKind() == MCExpr::Constant) {
558 Size = static_cast<const MCConstantExpr *>(ESize)->getValue();
560 assert(0 && "Unsupported size expression");
564 // Write out the symbol table entry
565 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
566 Size, Other, MSD.SectionIndex, IsReserved);
569 void ELFObjectWriterImpl::WriteSymbolTable(MCDataFragment *SymtabF,
570 MCDataFragment *ShndxF,
571 const MCAssembler &Asm,
572 const MCAsmLayout &Layout,
573 unsigned NumRegularSections) {
574 // The string table must be emitted first because we need the index
575 // into the string table for all the symbol names.
576 assert(StringTable.size() && "Missing string table");
578 // FIXME: Make sure the start of the symbol table is aligned.
580 // The first entry is the undefined symbol entry.
581 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
583 // Write the symbol table entries.
584 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
585 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
586 ELFSymbolData &MSD = LocalSymbolData[i];
587 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
590 // Write out a symbol table entry for each regular section.
592 for (MCAssembler::const_iterator it = Asm.begin();
593 Index <= NumRegularSections; ++it, ++Index) {
594 const MCSectionELF &Section =
595 static_cast<const MCSectionELF&>(it->getSection());
596 // Leave out relocations so we don't have indexes within
597 // the relocations messed up
598 if (Section.getType() == ELF::SHT_RELA || Section.getType() == ELF::SHT_REL)
600 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
601 ELF::STV_DEFAULT, Index, false);
602 LastLocalSymbolIndex++;
605 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
606 ELFSymbolData &MSD = ExternalSymbolData[i];
607 MCSymbolData &Data = *MSD.SymbolData;
608 assert(((Data.getFlags() & ELF_STB_Global) ||
609 (Data.getFlags() & ELF_STB_Weak)) &&
610 "External symbol requires STB_GLOBAL or STB_WEAK flag");
611 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
612 if (GetBinding(Data) == ELF::STB_LOCAL)
613 LastLocalSymbolIndex++;
616 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
617 ELFSymbolData &MSD = UndefinedSymbolData[i];
618 MCSymbolData &Data = *MSD.SymbolData;
619 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
620 if (GetBinding(Data) == ELF::STB_LOCAL)
621 LastLocalSymbolIndex++;
625 static bool ShouldRelocOnSymbol(const MCSymbolData &SD,
626 const MCValue &Target,
627 const MCFragment &F) {
628 const MCSymbol &Symbol = SD.getSymbol();
629 if (Symbol.isUndefined())
632 const MCSectionELF &Section =
633 static_cast<const MCSectionELF&>(Symbol.getSection());
638 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
639 const MCSectionELF &Sec2 =
640 static_cast<const MCSectionELF&>(F.getParent()->getSection());
642 if (Section.getKind().isBSS())
645 if (&Sec2 != &Section &&
646 (Kind == MCSymbolRefExpr::VK_PLT ||
647 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
648 Kind == MCSymbolRefExpr::VK_GOTOFF))
651 if (Section.getFlags() & MCSectionELF::SHF_MERGE)
652 return Target.getConstant() != 0;
657 // FIXME: this is currently X86/X86_64 only
658 void ELFObjectWriterImpl::RecordRelocation(const MCAssembler &Asm,
659 const MCAsmLayout &Layout,
660 const MCFragment *Fragment,
661 const MCFixup &Fixup,
663 uint64_t &FixedValue) {
666 int64_t Value = Target.getConstant();
667 const MCSymbol *Symbol = 0;
668 const MCSymbol *Renamed = 0;
670 bool IsPCRel = isFixupKindX86PCRel(Fixup.getKind());
671 if (!Target.isAbsolute()) {
672 Symbol = &AliasedSymbol(Target.getSymA()->getSymbol());
673 Renamed = Renames.lookup(Symbol);
675 Renamed = &Target.getSymA()->getSymbol();
676 MCSymbolData &SD = Asm.getSymbolData(*Symbol);
677 MCFragment *F = SD.getFragment();
679 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
680 const MCSymbol &SymbolB = RefB->getSymbol();
681 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
683 MCSectionData *Sec = Fragment->getParent();
685 // Offset of the symbol in the section
686 int64_t a = Layout.getSymbolAddress(&SDB) - Layout.getSectionAddress(Sec);
688 // Ofeset of the relocation in the section
689 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
693 // Check that this case has already been fully resolved before we get
695 if (Symbol->isDefined() && !SD.isExternal() &&
697 &Fragment->getParent()->getSection() == &Symbol->getSection()) {
698 llvm_unreachable("We don't need a relocation in this case.");
702 bool RelocOnSymbol = ShouldRelocOnSymbol(SD, Target, *Fragment);
703 if (!RelocOnSymbol) {
704 Index = F->getParent()->getOrdinal();
706 MCSectionData *FSD = F->getParent();
707 // Offset of the symbol in the section
708 Value += Layout.getSymbolAddress(&SD) - Layout.getSectionAddress(FSD);
710 UsedInReloc.insert(Renamed);
711 MCSymbolData &RenamedSD = Asm.getSymbolData(*Renamed);
712 if (RenamedSD.getFlags() & ELF_Other_Weakref) {
713 WeakrefUsedInReloc.insert(Symbol);
718 // Compensate for the addend on i386.
725 // determine the type of the relocation
727 MCSymbolRefExpr::VariantKind Modifier = Target.getSymA()->getKind();
733 llvm_unreachable("Unimplemented");
734 case MCSymbolRefExpr::VK_None:
735 Type = ELF::R_X86_64_PC32;
737 case MCSymbolRefExpr::VK_PLT:
738 Type = ELF::R_X86_64_PLT32;
740 case MCSymbolRefExpr::VK_GOTPCREL:
741 Type = ELF::R_X86_64_GOTPCREL;
743 case MCSymbolRefExpr::VK_GOTTPOFF:
744 Type = ELF::R_X86_64_GOTTPOFF;
746 case MCSymbolRefExpr::VK_TLSGD:
747 Type = ELF::R_X86_64_TLSGD;
749 case MCSymbolRefExpr::VK_TLSLD:
750 Type = ELF::R_X86_64_TLSLD;
754 switch ((unsigned)Fixup.getKind()) {
755 default: llvm_unreachable("invalid fixup kind!");
756 case FK_Data_8: Type = ELF::R_X86_64_64; break;
757 case X86::reloc_signed_4byte:
758 case X86::reloc_pcrel_4byte:
759 assert(isInt<32>(Target.getConstant()));
762 llvm_unreachable("Unimplemented");
763 case MCSymbolRefExpr::VK_None:
764 Type = ELF::R_X86_64_32S;
766 case MCSymbolRefExpr::VK_GOT:
767 Type = ELF::R_X86_64_GOT32;
769 case MCSymbolRefExpr::VK_GOTPCREL:
770 Type = ELF::R_X86_64_GOTPCREL;
772 case MCSymbolRefExpr::VK_TPOFF:
773 Type = ELF::R_X86_64_TPOFF32;
775 case MCSymbolRefExpr::VK_DTPOFF:
776 Type = ELF::R_X86_64_DTPOFF32;
781 Type = ELF::R_X86_64_32;
783 case FK_Data_2: Type = ELF::R_X86_64_16; break;
784 case X86::reloc_pcrel_1byte:
785 case FK_Data_1: Type = ELF::R_X86_64_8; break;
792 llvm_unreachable("Unimplemented");
793 case MCSymbolRefExpr::VK_None:
794 Type = ELF::R_386_PC32;
796 case MCSymbolRefExpr::VK_PLT:
797 Type = ELF::R_386_PLT32;
801 switch ((unsigned)Fixup.getKind()) {
802 default: llvm_unreachable("invalid fixup kind!");
804 case X86::reloc_global_offset_table:
805 Type = ELF::R_386_GOTPC;
808 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
810 case X86::reloc_signed_4byte:
811 case X86::reloc_pcrel_4byte:
815 llvm_unreachable("Unimplemented");
816 case MCSymbolRefExpr::VK_None:
817 Type = ELF::R_386_32;
819 case MCSymbolRefExpr::VK_GOT:
820 Type = ELF::R_386_GOT32;
822 case MCSymbolRefExpr::VK_GOTOFF:
823 Type = ELF::R_386_GOTOFF;
825 case MCSymbolRefExpr::VK_TLSGD:
826 Type = ELF::R_386_TLS_GD;
828 case MCSymbolRefExpr::VK_TPOFF:
829 Type = ELF::R_386_TLS_LE_32;
831 case MCSymbolRefExpr::VK_INDNTPOFF:
832 Type = ELF::R_386_TLS_IE;
834 case MCSymbolRefExpr::VK_NTPOFF:
835 Type = ELF::R_386_TLS_LE;
837 case MCSymbolRefExpr::VK_GOTNTPOFF:
838 Type = ELF::R_386_TLS_GOTIE;
840 case MCSymbolRefExpr::VK_TLSLDM:
841 Type = ELF::R_386_TLS_LDM;
843 case MCSymbolRefExpr::VK_DTPOFF:
844 Type = ELF::R_386_TLS_LDO_32;
848 case FK_Data_2: Type = ELF::R_386_16; break;
849 case X86::reloc_pcrel_1byte:
850 case FK_Data_1: Type = ELF::R_386_8; break;
855 if (RelocNeedsGOT(Modifier))
858 ELFRelocationEntry ERE;
862 ERE.Symbol = Renamed;
864 ERE.r_offset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
866 if (HasRelocationAddend)
867 ERE.r_addend = Addend;
869 ERE.r_addend = 0; // Silence compiler warning.
871 Relocations[Fragment->getParent()].push_back(ERE);
875 ELFObjectWriterImpl::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
877 MCSymbolData &SD = Asm.getSymbolData(*S);
880 if (!SD.isExternal() && !S->isUndefined())
881 return SD.getIndex() + /* empty symbol */ 1;
883 // External or undefined symbol.
884 return SD.getIndex() + NumRegularSections + /* empty symbol */ 1;
887 static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
888 bool Used, bool Renamed) {
889 if (Data.getFlags() & ELF_Other_Weakref)
898 const MCSymbol &Symbol = Data.getSymbol();
900 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
903 const MCSymbol &A = AliasedSymbol(Symbol);
904 if (!A.isVariable() && A.isUndefined() && !Data.isCommon())
907 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
910 if (Symbol.isTemporary())
916 static bool isLocal(const MCSymbolData &Data) {
917 if (Data.isExternal())
920 const MCSymbol &Symbol = Data.getSymbol();
921 if (Symbol.isUndefined() && !Symbol.isVariable())
927 void ELFObjectWriterImpl::ComputeIndexMap(MCAssembler &Asm,
928 SectionIndexMapTy &SectionIndexMap) {
930 for (MCAssembler::iterator it = Asm.begin(),
931 ie = Asm.end(); it != ie; ++it) {
932 const MCSectionELF &Section =
933 static_cast<const MCSectionELF &>(it->getSection());
934 SectionIndexMap[&Section] = Index++;
938 void ELFObjectWriterImpl::ComputeSymbolTable(MCAssembler &Asm,
939 const SectionIndexMapTy &SectionIndexMap) {
940 // FIXME: Is this the correct place to do this?
942 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
943 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
944 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
945 Data.setExternal(true);
946 SetBinding(Data, ELF::STB_GLOBAL);
949 // Build section lookup table.
950 NumRegularSections = Asm.size();
952 // Index 0 is always the empty string.
953 StringMap<uint64_t> StringIndexMap;
954 StringTable += '\x00';
956 // Add the data for the symbols.
957 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
958 ie = Asm.symbol_end(); it != ie; ++it) {
959 const MCSymbol &Symbol = it->getSymbol();
961 bool Used = UsedInReloc.count(&Symbol);
962 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
963 if (!isInSymtab(Asm, *it, Used || WeakrefUsed,
964 Renames.count(&Symbol)))
969 bool Local = isLocal(*it);
970 const MCSymbol &RefSymbol = AliasedSymbol(Symbol);
972 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
973 SetBinding(*it, ELF::STB_WEAK);
975 if (it->isCommon()) {
977 MSD.SectionIndex = ELF::SHN_COMMON;
978 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
979 MSD.SectionIndex = ELF::SHN_ABS;
980 } else if (RefSymbol.isUndefined()) {
981 MSD.SectionIndex = ELF::SHN_UNDEF;
983 const MCSectionELF &Section =
984 static_cast<const MCSectionELF&>(RefSymbol.getSection());
985 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
986 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
987 NeedsSymtabShndx = true;
988 assert(MSD.SectionIndex && "Invalid section index!");
991 // The @@@ in symbol version is replaced with @ in undefined symbols and
992 // @@ in defined ones.
993 StringRef Name = Symbol.getName();
994 size_t Pos = Name.find("@@@");
995 std::string FinalName;
996 if (Pos != StringRef::npos) {
997 StringRef Prefix = Name.substr(0, Pos);
998 unsigned n = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
999 StringRef Suffix = Name.substr(Pos + n);
1000 FinalName = Prefix.str() + Suffix.str();
1002 FinalName = Name.str();
1005 uint64_t &Entry = StringIndexMap[FinalName];
1007 Entry = StringTable.size();
1008 StringTable += FinalName;
1009 StringTable += '\x00';
1011 MSD.StringIndex = Entry;
1012 if (MSD.SectionIndex == ELF::SHN_UNDEF)
1013 UndefinedSymbolData.push_back(MSD);
1015 LocalSymbolData.push_back(MSD);
1017 ExternalSymbolData.push_back(MSD);
1020 // Symbols are required to be in lexicographic order.
1021 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
1022 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
1023 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
1025 // Set the symbol indices. Local symbols must come before all other
1026 // symbols with non-local bindings.
1028 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
1029 LocalSymbolData[i].SymbolData->setIndex(Index++);
1030 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
1031 ExternalSymbolData[i].SymbolData->setIndex(Index++);
1032 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
1033 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
1036 void ELFObjectWriterImpl::WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
1037 const MCSectionData &SD) {
1038 if (!Relocations[&SD].empty()) {
1039 MCContext &Ctx = Asm.getContext();
1040 const MCSectionELF *RelaSection;
1041 const MCSectionELF &Section =
1042 static_cast<const MCSectionELF&>(SD.getSection());
1044 const StringRef SectionName = Section.getSectionName();
1045 std::string RelaSectionName = HasRelocationAddend ? ".rela" : ".rel";
1046 RelaSectionName += SectionName;
1049 if (HasRelocationAddend)
1050 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
1052 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
1054 RelaSection = Ctx.getELFSection(RelaSectionName, HasRelocationAddend ?
1055 ELF::SHT_RELA : ELF::SHT_REL, 0,
1056 SectionKind::getReadOnly(),
1059 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
1060 RelaSD.setAlignment(Is64Bit ? 8 : 4);
1062 MCDataFragment *F = new MCDataFragment(&RelaSD);
1064 WriteRelocationsFragment(Asm, F, &SD);
1066 Asm.AddSectionToTheEnd(*Writer, RelaSD, Layout);
1070 void ELFObjectWriterImpl::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
1071 uint64_t Flags, uint64_t Address,
1072 uint64_t Offset, uint64_t Size,
1073 uint32_t Link, uint32_t Info,
1075 uint64_t EntrySize) {
1076 Write32(Name); // sh_name: index into string table
1077 Write32(Type); // sh_type
1078 WriteWord(Flags); // sh_flags
1079 WriteWord(Address); // sh_addr
1080 WriteWord(Offset); // sh_offset
1081 WriteWord(Size); // sh_size
1082 Write32(Link); // sh_link
1083 Write32(Info); // sh_info
1084 WriteWord(Alignment); // sh_addralign
1085 WriteWord(EntrySize); // sh_entsize
1088 void ELFObjectWriterImpl::WriteRelocationsFragment(const MCAssembler &Asm,
1090 const MCSectionData *SD) {
1091 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
1092 // sort by the r_offset just like gnu as does
1093 array_pod_sort(Relocs.begin(), Relocs.end());
1095 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1096 ELFRelocationEntry entry = Relocs[e - i - 1];
1098 if (entry.Index < 0)
1099 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
1101 entry.Index += LocalSymbolData.size() + 1;
1103 String64(*F, entry.r_offset);
1105 struct ELF::Elf64_Rela ERE64;
1106 ERE64.setSymbolAndType(entry.Index, entry.Type);
1107 String64(*F, ERE64.r_info);
1109 if (HasRelocationAddend)
1110 String64(*F, entry.r_addend);
1112 String32(*F, entry.r_offset);
1114 struct ELF::Elf32_Rela ERE32;
1115 ERE32.setSymbolAndType(entry.Index, entry.Type);
1116 String32(*F, ERE32.r_info);
1118 if (HasRelocationAddend)
1119 String32(*F, entry.r_addend);
1124 void ELFObjectWriterImpl::CreateMetadataSections(MCAssembler &Asm,
1125 MCAsmLayout &Layout) {
1126 MCContext &Ctx = Asm.getContext();
1129 unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
1131 unsigned NumRegularSections = Asm.size();
1133 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
1134 const MCSectionELF *ShstrtabSection =
1135 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
1136 SectionKind::getReadOnly(), false);
1137 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1138 ShstrtabSD.setAlignment(1);
1139 ShstrtabIndex = Asm.size();
1141 const MCSectionELF *SymtabSection =
1142 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1143 SectionKind::getReadOnly(),
1145 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
1146 SymtabSD.setAlignment(Is64Bit ? 8 : 4);
1147 SymbolTableIndex = Asm.size();
1149 MCSectionData *SymtabShndxSD = NULL;
1151 if (NeedsSymtabShndx) {
1152 const MCSectionELF *SymtabShndxSection =
1153 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
1154 SectionKind::getReadOnly(), 4);
1155 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
1156 SymtabShndxSD->setAlignment(4);
1159 const MCSection *StrtabSection;
1160 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
1161 SectionKind::getReadOnly(), false);
1162 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1163 StrtabSD.setAlignment(1);
1164 StringTableIndex = Asm.size();
1166 WriteRelocations(Asm, Layout);
1169 F = new MCDataFragment(&SymtabSD);
1170 MCDataFragment *ShndxF = NULL;
1171 if (NeedsSymtabShndx) {
1172 ShndxF = new MCDataFragment(SymtabShndxSD);
1173 Asm.AddSectionToTheEnd(*Writer, *SymtabShndxSD, Layout);
1175 WriteSymbolTable(F, ShndxF, Asm, Layout, NumRegularSections);
1176 Asm.AddSectionToTheEnd(*Writer, SymtabSD, Layout);
1178 F = new MCDataFragment(&StrtabSD);
1179 F->getContents().append(StringTable.begin(), StringTable.end());
1180 Asm.AddSectionToTheEnd(*Writer, StrtabSD, Layout);
1182 F = new MCDataFragment(&ShstrtabSD);
1184 // Section header string table.
1186 // The first entry of a string table holds a null character so skip
1189 F->getContents() += '\x00';
1191 for (MCAssembler::const_iterator it = Asm.begin(),
1192 ie = Asm.end(); it != ie; ++it) {
1193 const MCSectionELF &Section =
1194 static_cast<const MCSectionELF&>(it->getSection());
1195 // FIXME: We could merge suffixes like in .text and .rela.text.
1197 // Remember the index into the string table so we can write it
1198 // into the sh_name field of the section header table.
1199 SectionStringTableIndex[&it->getSection()] = Index;
1201 Index += Section.getSectionName().size() + 1;
1202 F->getContents() += Section.getSectionName();
1203 F->getContents() += '\x00';
1206 Asm.AddSectionToTheEnd(*Writer, ShstrtabSD, Layout);
1209 bool ELFObjectWriterImpl::IsFixupFullyResolved(const MCAssembler &Asm,
1210 const MCValue Target,
1212 const MCFragment *DF) const {
1213 // If this is a PCrel relocation, find the section this fixup value is
1215 const MCSection *BaseSection = 0;
1217 BaseSection = &DF->getParent()->getSection();
1218 assert(BaseSection);
1221 const MCSection *SectionA = 0;
1222 const MCSymbol *SymbolA = 0;
1223 if (const MCSymbolRefExpr *A = Target.getSymA()) {
1224 SymbolA = &A->getSymbol();
1225 SectionA = &SymbolA->getSection();
1228 const MCSection *SectionB = 0;
1229 if (const MCSymbolRefExpr *B = Target.getSymB()) {
1230 SectionB = &B->getSymbol().getSection();
1234 return SectionA == SectionB;
1236 const MCSymbolData &DataA = Asm.getSymbolData(*SymbolA);
1237 if (DataA.isExternal())
1240 return !SectionB && BaseSection == SectionA;
1243 void ELFObjectWriterImpl::WriteObject(MCAssembler &Asm,
1244 const MCAsmLayout &Layout) {
1245 SectionIndexMapTy SectionIndexMap;
1247 ComputeIndexMap(Asm, SectionIndexMap);
1249 // Compute symbol table information.
1250 ComputeSymbolTable(Asm, SectionIndexMap);
1252 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
1253 const_cast<MCAsmLayout&>(Layout));
1255 // Add 1 for the null section.
1256 unsigned NumSections = Asm.size() + 1;
1257 uint64_t NaturalAlignment = Is64Bit ? 8 : 4;
1258 uint64_t HeaderSize = Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr);
1259 uint64_t FileOff = HeaderSize;
1261 for (MCAssembler::const_iterator it = Asm.begin(),
1262 ie = Asm.end(); it != ie; ++it) {
1263 const MCSectionData &SD = *it;
1265 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1267 // Get the size of the section in the output file (including padding).
1268 uint64_t Size = Layout.getSectionFileSize(&SD);
1273 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1275 // Write out the ELF header ...
1276 WriteHeader(FileOff - HeaderSize, NumSections);
1278 FileOff = HeaderSize;
1280 // ... then all of the sections ...
1281 DenseMap<const MCSection*, uint64_t> SectionOffsetMap;
1283 for (MCAssembler::const_iterator it = Asm.begin(),
1284 ie = Asm.end(); it != ie; ++it) {
1285 const MCSectionData &SD = *it;
1287 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1288 WriteZeros(Padding);
1291 // Remember the offset into the file for this section.
1292 SectionOffsetMap[&it->getSection()] = FileOff;
1294 FileOff += Layout.getSectionFileSize(&SD);
1296 Asm.WriteSectionData(it, Layout, Writer);
1299 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1300 WriteZeros(Padding);
1303 // ... and then the section header table.
1304 // Should we align the section header table?
1306 // Null section first.
1307 uint64_t FirstSectionSize =
1308 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1309 uint32_t FirstSectionLink =
1310 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1311 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
1313 for (MCAssembler::const_iterator it = Asm.begin(),
1314 ie = Asm.end(); it != ie; ++it) {
1315 const MCSectionData &SD = *it;
1316 const MCSectionELF &Section =
1317 static_cast<const MCSectionELF&>(SD.getSection());
1319 uint64_t sh_link = 0;
1320 uint64_t sh_info = 0;
1322 switch(Section.getType()) {
1323 case ELF::SHT_DYNAMIC:
1324 sh_link = SectionStringTableIndex[&it->getSection()];
1329 case ELF::SHT_RELA: {
1330 const MCSectionELF *SymtabSection;
1331 const MCSectionELF *InfoSection;
1333 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1334 SectionKind::getReadOnly(),
1336 sh_link = SectionIndexMap[SymtabSection];
1338 // Remove ".rel" and ".rela" prefixes.
1339 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1340 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
1342 InfoSection = Asm.getContext().getELFSection(SectionName,
1343 ELF::SHT_PROGBITS, 0,
1344 SectionKind::getReadOnly(),
1346 sh_info = SectionIndexMap[InfoSection];
1350 case ELF::SHT_SYMTAB:
1351 case ELF::SHT_DYNSYM:
1352 sh_link = StringTableIndex;
1353 sh_info = LastLocalSymbolIndex;
1356 case ELF::SHT_SYMTAB_SHNDX:
1357 sh_link = SymbolTableIndex;
1360 case ELF::SHT_PROGBITS:
1361 case ELF::SHT_STRTAB:
1362 case ELF::SHT_NOBITS:
1364 case ELF::SHT_ARM_ATTRIBUTES:
1369 assert(0 && "FIXME: sh_type value not supported!");
1373 WriteSecHdrEntry(SectionStringTableIndex[&it->getSection()],
1374 Section.getType(), Section.getFlags(),
1376 SectionOffsetMap.lookup(&SD.getSection()),
1377 Layout.getSectionSize(&SD), sh_link,
1378 sh_info, SD.getAlignment(),
1379 Section.getEntrySize());
1383 ELFObjectWriter::ELFObjectWriter(raw_ostream &OS,
1385 Triple::OSType OSType,
1387 bool IsLittleEndian,
1388 bool HasRelocationAddend)
1389 : MCObjectWriter(OS, IsLittleEndian)
1391 Impl = new ELFObjectWriterImpl(this, Is64Bit, EMachine,
1392 HasRelocationAddend, OSType);
1395 ELFObjectWriter::~ELFObjectWriter() {
1396 delete (ELFObjectWriterImpl*) Impl;
1399 void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm) {
1400 ((ELFObjectWriterImpl*) Impl)->ExecutePostLayoutBinding(Asm);
1403 void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
1404 const MCAsmLayout &Layout,
1405 const MCFragment *Fragment,
1406 const MCFixup &Fixup, MCValue Target,
1407 uint64_t &FixedValue) {
1408 ((ELFObjectWriterImpl*) Impl)->RecordRelocation(Asm, Layout, Fragment, Fixup,
1409 Target, FixedValue);
1412 bool ELFObjectWriter::IsFixupFullyResolved(const MCAssembler &Asm,
1413 const MCValue Target,
1415 const MCFragment *DF) const {
1416 return ((ELFObjectWriterImpl*) Impl)->IsFixupFullyResolved(Asm, Target,
1420 void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1421 const MCAsmLayout &Layout) {
1422 ((ELFObjectWriterImpl*) Impl)->WriteObject(Asm, Layout);