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/MCELFObjectWriter.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/SmallPtrSet.h"
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/MC/MCAsmBackend.h"
20 #include "llvm/MC/MCAsmInfo.h"
21 #include "llvm/MC/MCAsmLayout.h"
22 #include "llvm/MC/MCAssembler.h"
23 #include "llvm/MC/MCContext.h"
24 #include "llvm/MC/MCExpr.h"
25 #include "llvm/MC/MCFixupKindInfo.h"
26 #include "llvm/MC/MCObjectWriter.h"
27 #include "llvm/MC/MCSectionELF.h"
28 #include "llvm/MC/MCSymbolELF.h"
29 #include "llvm/MC/MCValue.h"
30 #include "llvm/MC/StringTableBuilder.h"
31 #include "llvm/Support/Compression.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/ELF.h"
34 #include "llvm/Support/Endian.h"
35 #include "llvm/Support/ErrorHandling.h"
40 #define DEBUG_TYPE "reloc-info"
44 typedef DenseMap<const MCSectionELF *, uint32_t> SectionIndexMapTy;
46 class ELFObjectWriter;
48 class SymbolTableWriter {
49 ELFObjectWriter &EWriter;
52 // indexes we are going to write to .symtab_shndx.
53 std::vector<uint32_t> ShndxIndexes;
55 // The numbel of symbols written so far.
58 void createSymtabShndx();
60 template <typename T> void write(T Value);
63 SymbolTableWriter(ELFObjectWriter &EWriter, bool Is64Bit);
65 void writeSymbol(uint32_t name, uint8_t info, uint64_t value, uint64_t size,
66 uint8_t other, uint32_t shndx, bool Reserved);
68 ArrayRef<uint32_t> getShndxIndexes() const { return ShndxIndexes; }
71 class ELFObjectWriter : public MCObjectWriter {
72 static bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind);
73 static uint64_t SymbolValue(const MCSymbol &Sym, const MCAsmLayout &Layout);
74 static bool isInSymtab(const MCAsmLayout &Layout, const MCSymbolELF &Symbol,
75 bool Used, bool Renamed);
77 /// Helper struct for containing some precomputed information on symbols.
78 struct ELFSymbolData {
79 const MCSymbolELF *Symbol;
80 uint32_t SectionIndex;
83 // Support lexicographic sorting.
84 bool operator<(const ELFSymbolData &RHS) const {
85 unsigned LHSType = Symbol->getType();
86 unsigned RHSType = RHS.Symbol->getType();
87 if (LHSType == ELF::STT_SECTION && RHSType != ELF::STT_SECTION)
89 if (LHSType != ELF::STT_SECTION && RHSType == ELF::STT_SECTION)
91 if (LHSType == ELF::STT_SECTION && RHSType == ELF::STT_SECTION)
92 return SectionIndex < RHS.SectionIndex;
93 return Name < RHS.Name;
97 /// The target specific ELF writer instance.
98 std::unique_ptr<MCELFObjectTargetWriter> TargetObjectWriter;
100 DenseMap<const MCSymbolELF *, const MCSymbolELF *> Renames;
102 llvm::DenseMap<const MCSectionELF *, std::vector<ELFRelocationEntry>>
106 /// @name Symbol Table Data
109 StringTableBuilder StrTabBuilder;
113 // This holds the symbol table index of the last local symbol.
114 unsigned LastLocalSymbolIndex;
115 // This holds the .strtab section index.
116 unsigned StringTableIndex;
117 // This holds the .symtab section index.
118 unsigned SymbolTableIndex;
119 // This holds the .symtab_shndx section index.
120 unsigned SymtabShndxSectionIndex = 0;
122 // Sections in the order they are to be output in the section table.
123 std::vector<const MCSectionELF *> SectionTable;
124 unsigned addToSectionTable(const MCSectionELF *Sec);
126 // TargetObjectWriter wrappers.
127 bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
128 bool hasRelocationAddend() const {
129 return TargetObjectWriter->hasRelocationAddend();
131 unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
132 bool IsPCRel) const {
133 return TargetObjectWriter->GetRelocType(Target, Fixup, IsPCRel);
137 ELFObjectWriter(MCELFObjectTargetWriter *MOTW, raw_pwrite_stream &OS,
139 : MCObjectWriter(OS, IsLittleEndian), TargetObjectWriter(MOTW) {}
141 void reset() override {
144 StrTabBuilder.clear();
145 SectionTable.clear();
146 MCObjectWriter::reset();
149 ~ELFObjectWriter() override;
151 void WriteWord(uint64_t W) {
158 template <typename T> void write(T Val) {
160 support::endian::Writer<support::little>(OS).write(Val);
162 support::endian::Writer<support::big>(OS).write(Val);
165 void writeHeader(const MCAssembler &Asm);
167 void writeSymbol(SymbolTableWriter &Writer, uint32_t StringIndex,
168 ELFSymbolData &MSD, const MCAsmLayout &Layout);
170 // Start and end offset of each section
171 typedef std::map<const MCSectionELF *, std::pair<uint64_t, uint64_t>>
174 bool shouldRelocateWithSymbol(const MCAssembler &Asm,
175 const MCSymbolRefExpr *RefA,
176 const MCSymbol *Sym, uint64_t C,
177 unsigned Type) const;
179 void RecordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
180 const MCFragment *Fragment, const MCFixup &Fixup,
181 MCValue Target, bool &IsPCRel,
182 uint64_t &FixedValue) override;
184 // Map from a signature symbol to the group section index
185 typedef DenseMap<const MCSymbol *, unsigned> RevGroupMapTy;
187 /// Compute the symbol table data
189 /// \param Asm - The assembler.
190 /// \param SectionIndexMap - Maps a section to its index.
191 /// \param RevGroupMap - Maps a signature symbol to the group section.
192 void computeSymbolTable(MCAssembler &Asm, const MCAsmLayout &Layout,
193 const SectionIndexMapTy &SectionIndexMap,
194 const RevGroupMapTy &RevGroupMap,
195 SectionOffsetsTy &SectionOffsets);
197 MCSectionELF *createRelocationSection(MCContext &Ctx,
198 const MCSectionELF &Sec);
200 const MCSectionELF *createStringTable(MCContext &Ctx);
202 void ExecutePostLayoutBinding(MCAssembler &Asm,
203 const MCAsmLayout &Layout) override;
205 void writeSectionHeader(const MCAssembler &Asm, const MCAsmLayout &Layout,
206 const SectionIndexMapTy &SectionIndexMap,
207 const SectionOffsetsTy &SectionOffsets);
209 void writeSectionData(const MCAssembler &Asm, MCSection &Sec,
210 const MCAsmLayout &Layout);
212 void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
213 uint64_t Address, uint64_t Offset, uint64_t Size,
214 uint32_t Link, uint32_t Info, uint64_t Alignment,
217 void writeRelocations(const MCAssembler &Asm, const MCSectionELF &Sec);
219 bool IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
220 const MCSymbol &SymA,
221 const MCFragment &FB,
223 bool IsPCRel) const override;
225 bool isWeak(const MCSymbol &Sym) const override;
227 void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
228 void writeSection(const SectionIndexMapTy &SectionIndexMap,
229 uint32_t GroupSymbolIndex, uint64_t Offset, uint64_t Size,
230 const MCSectionELF &Section);
234 unsigned ELFObjectWriter::addToSectionTable(const MCSectionELF *Sec) {
235 SectionTable.push_back(Sec);
236 StrTabBuilder.add(Sec->getSectionName());
237 return SectionTable.size();
240 void SymbolTableWriter::createSymtabShndx() {
241 if (!ShndxIndexes.empty())
244 ShndxIndexes.resize(NumWritten);
247 template <typename T> void SymbolTableWriter::write(T Value) {
248 EWriter.write(Value);
251 SymbolTableWriter::SymbolTableWriter(ELFObjectWriter &EWriter, bool Is64Bit)
252 : EWriter(EWriter), Is64Bit(Is64Bit), NumWritten(0) {}
254 void SymbolTableWriter::writeSymbol(uint32_t name, uint8_t info, uint64_t value,
255 uint64_t size, uint8_t other,
256 uint32_t shndx, bool Reserved) {
257 bool LargeIndex = shndx >= ELF::SHN_LORESERVE && !Reserved;
262 if (!ShndxIndexes.empty()) {
264 ShndxIndexes.push_back(shndx);
266 ShndxIndexes.push_back(0);
269 uint16_t Index = LargeIndex ? uint16_t(ELF::SHN_XINDEX) : shndx;
272 write(name); // st_name
273 write(info); // st_info
274 write(other); // st_other
275 write(Index); // st_shndx
276 write(value); // st_value
277 write(size); // st_size
279 write(name); // st_name
280 write(uint32_t(value)); // st_value
281 write(uint32_t(size)); // st_size
282 write(info); // st_info
283 write(other); // st_other
284 write(Index); // st_shndx
290 bool ELFObjectWriter::isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) {
291 const MCFixupKindInfo &FKI =
292 Asm.getBackend().getFixupKindInfo((MCFixupKind) Kind);
294 return FKI.Flags & MCFixupKindInfo::FKF_IsPCRel;
297 ELFObjectWriter::~ELFObjectWriter()
300 // Emit the ELF header.
301 void ELFObjectWriter::writeHeader(const MCAssembler &Asm) {
307 // emitWord method behaves differently for ELF32 and ELF64, writing
308 // 4 bytes in the former and 8 in the latter.
310 WriteBytes(ELF::ElfMagic); // e_ident[EI_MAG0] to e_ident[EI_MAG3]
312 Write8(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
315 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
317 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
319 Write8(TargetObjectWriter->getOSABI());
320 Write8(0); // e_ident[EI_ABIVERSION]
322 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
324 Write16(ELF::ET_REL); // e_type
326 Write16(TargetObjectWriter->getEMachine()); // e_machine = target
328 Write32(ELF::EV_CURRENT); // e_version
329 WriteWord(0); // e_entry, no entry point in .o file
330 WriteWord(0); // e_phoff, no program header for .o
331 WriteWord(0); // e_shoff = sec hdr table off in bytes
333 // e_flags = whatever the target wants
334 Write32(Asm.getELFHeaderEFlags());
336 // e_ehsize = ELF header size
337 Write16(is64Bit() ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
339 Write16(0); // e_phentsize = prog header entry size
340 Write16(0); // e_phnum = # prog header entries = 0
342 // e_shentsize = Section header entry size
343 Write16(is64Bit() ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
345 // e_shnum = # of section header ents
348 // e_shstrndx = Section # of '.shstrtab'
349 assert(StringTableIndex < ELF::SHN_LORESERVE);
350 Write16(StringTableIndex);
353 uint64_t ELFObjectWriter::SymbolValue(const MCSymbol &Sym,
354 const MCAsmLayout &Layout) {
355 if (Sym.isCommon() && Sym.isExternal())
356 return Sym.getCommonAlignment();
359 if (!Layout.getSymbolOffset(Sym, Res))
362 if (Layout.getAssembler().isThumbFunc(&Sym))
368 void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
369 const MCAsmLayout &Layout) {
370 // The presence of symbol versions causes undefined symbols and
371 // versions declared with @@@ to be renamed.
373 for (const MCSymbol &A : Asm.symbols()) {
374 const auto &Alias = cast<MCSymbolELF>(A);
376 if (!Alias.isVariable())
378 auto *Ref = dyn_cast<MCSymbolRefExpr>(Alias.getVariableValue());
381 const auto &Symbol = cast<MCSymbolELF>(Ref->getSymbol());
383 StringRef AliasName = Alias.getName();
384 size_t Pos = AliasName.find('@');
385 if (Pos == StringRef::npos)
388 // Aliases defined with .symvar copy the binding from the symbol they alias.
389 // This is the first place we are able to copy this information.
390 Alias.setExternal(Symbol.isExternal());
391 Alias.setBinding(Symbol.getBinding());
393 StringRef Rest = AliasName.substr(Pos);
394 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
397 // FIXME: produce a better error message.
398 if (Symbol.isUndefined() && Rest.startswith("@@") &&
399 !Rest.startswith("@@@"))
400 report_fatal_error("A @@ version cannot be undefined");
402 Renames.insert(std::make_pair(&Symbol, &Alias));
406 static uint8_t mergeTypeForSet(uint8_t origType, uint8_t newType) {
407 uint8_t Type = newType;
409 // Propagation rules:
410 // IFUNC > FUNC > OBJECT > NOTYPE
411 // TLS_OBJECT > OBJECT > NOTYPE
413 // dont let the new type degrade the old type
417 case ELF::STT_GNU_IFUNC:
418 if (Type == ELF::STT_FUNC || Type == ELF::STT_OBJECT ||
419 Type == ELF::STT_NOTYPE || Type == ELF::STT_TLS)
420 Type = ELF::STT_GNU_IFUNC;
423 if (Type == ELF::STT_OBJECT || Type == ELF::STT_NOTYPE ||
424 Type == ELF::STT_TLS)
425 Type = ELF::STT_FUNC;
427 case ELF::STT_OBJECT:
428 if (Type == ELF::STT_NOTYPE)
429 Type = ELF::STT_OBJECT;
432 if (Type == ELF::STT_OBJECT || Type == ELF::STT_NOTYPE ||
433 Type == ELF::STT_GNU_IFUNC || Type == ELF::STT_FUNC)
441 void ELFObjectWriter::writeSymbol(SymbolTableWriter &Writer,
442 uint32_t StringIndex, ELFSymbolData &MSD,
443 const MCAsmLayout &Layout) {
444 const auto &Symbol = cast<MCSymbolELF>(*MSD.Symbol);
445 assert((!Symbol.getFragment() ||
446 (Symbol.getFragment()->getParent() == &Symbol.getSection())) &&
447 "The symbol's section doesn't match the fragment's symbol");
448 const MCSymbolELF *Base =
449 cast_or_null<MCSymbolELF>(Layout.getBaseSymbol(Symbol));
451 // This has to be in sync with when computeSymbolTable uses SHN_ABS or
453 bool IsReserved = !Base || Symbol.isCommon();
455 // Binding and Type share the same byte as upper and lower nibbles
456 uint8_t Binding = Symbol.getBinding();
457 uint8_t Type = Symbol.getType();
459 Type = mergeTypeForSet(Type, Base->getType());
461 uint8_t Info = (Binding << 4) | Type;
463 // Other and Visibility share the same byte with Visibility using the lower
465 uint8_t Visibility = Symbol.getVisibility();
466 uint8_t Other = Symbol.getOther() | Visibility;
468 uint64_t Value = SymbolValue(*MSD.Symbol, Layout);
471 const MCExpr *ESize = MSD.Symbol->getSize();
473 ESize = Base->getSize();
477 if (!ESize->evaluateKnownAbsolute(Res, Layout))
478 report_fatal_error("Size expression must be absolute.");
482 // Write out the symbol table entry
483 Writer.writeSymbol(StringIndex, Info, Value, Size, Other, MSD.SectionIndex,
487 // It is always valid to create a relocation with a symbol. It is preferable
488 // to use a relocation with a section if that is possible. Using the section
489 // allows us to omit some local symbols from the symbol table.
490 bool ELFObjectWriter::shouldRelocateWithSymbol(const MCAssembler &Asm,
491 const MCSymbolRefExpr *RefA,
492 const MCSymbol *S, uint64_t C,
493 unsigned Type) const {
494 const auto *Sym = cast_or_null<MCSymbolELF>(S);
495 // A PCRel relocation to an absolute value has no symbol (or section). We
496 // represent that with a relocation to a null section.
500 MCSymbolRefExpr::VariantKind Kind = RefA->getKind();
504 // The .odp creation emits a relocation against the symbol ".TOC." which
505 // create a R_PPC64_TOC relocation. However the relocation symbol name
506 // in final object creation should be NULL, since the symbol does not
507 // really exist, it is just the reference to TOC base for the current
508 // object file. Since the symbol is undefined, returning false results
509 // in a relocation with a null section which is the desired result.
510 case MCSymbolRefExpr::VK_PPC_TOCBASE:
513 // These VariantKind cause the relocation to refer to something other than
514 // the symbol itself, like a linker generated table. Since the address of
515 // symbol is not relevant, we cannot replace the symbol with the
516 // section and patch the difference in the addend.
517 case MCSymbolRefExpr::VK_GOT:
518 case MCSymbolRefExpr::VK_PLT:
519 case MCSymbolRefExpr::VK_GOTPCREL:
520 case MCSymbolRefExpr::VK_Mips_GOT:
521 case MCSymbolRefExpr::VK_PPC_GOT_LO:
522 case MCSymbolRefExpr::VK_PPC_GOT_HI:
523 case MCSymbolRefExpr::VK_PPC_GOT_HA:
527 // An undefined symbol is not in any section, so the relocation has to point
528 // to the symbol itself.
529 assert(Sym && "Expected a symbol");
530 if (Sym->isUndefined())
533 unsigned Binding = Sym->getBinding();
536 llvm_unreachable("Invalid Binding");
540 // If the symbol is weak, it might be overridden by a symbol in another
541 // file. The relocation has to point to the symbol so that the linker
544 case ELF::STB_GLOBAL:
545 // Global ELF symbols can be preempted by the dynamic linker. The relocation
546 // has to point to the symbol for a reason analogous to the STB_WEAK case.
550 // If a relocation points to a mergeable section, we have to be careful.
551 // If the offset is zero, a relocation with the section will encode the
552 // same information. With a non-zero offset, the situation is different.
553 // For example, a relocation can point 42 bytes past the end of a string.
554 // If we change such a relocation to use the section, the linker would think
555 // that it pointed to another string and subtracting 42 at runtime will
556 // produce the wrong value.
557 auto &Sec = cast<MCSectionELF>(Sym->getSection());
558 unsigned Flags = Sec.getFlags();
559 if (Flags & ELF::SHF_MERGE) {
563 // It looks like gold has a bug (http://sourceware.org/PR16794) and can
564 // only handle section relocations to mergeable sections if using RELA.
565 if (!hasRelocationAddend())
569 // Most TLS relocations use a got, so they need the symbol. Even those that
570 // are just an offset (@tpoff), require a symbol in gold versions before
571 // 5efeedf61e4fe720fd3e9a08e6c91c10abb66d42 (2014-09-26) which fixed
572 // http://sourceware.org/PR16773.
573 if (Flags & ELF::SHF_TLS)
576 // If the symbol is a thumb function the final relocation must set the lowest
577 // bit. With a symbol that is done by just having the symbol have that bit
578 // set, so we would lose the bit if we relocated with the section.
579 // FIXME: We could use the section but add the bit to the relocation value.
580 if (Asm.isThumbFunc(Sym))
583 if (TargetObjectWriter->needsRelocateWithSymbol(*Sym, Type))
588 // True if the assembler knows nothing about the final value of the symbol.
589 // This doesn't cover the comdat issues, since in those cases the assembler
590 // can at least know that all symbols in the section will move together.
591 static bool isWeak(const MCSymbolELF &Sym) {
592 if (Sym.getType() == ELF::STT_GNU_IFUNC)
595 switch (Sym.getBinding()) {
597 llvm_unreachable("Unknown binding");
600 case ELF::STB_GLOBAL:
603 case ELF::STB_GNU_UNIQUE:
608 void ELFObjectWriter::RecordRelocation(MCAssembler &Asm,
609 const MCAsmLayout &Layout,
610 const MCFragment *Fragment,
611 const MCFixup &Fixup, MCValue Target,
612 bool &IsPCRel, uint64_t &FixedValue) {
613 const MCSectionELF &FixupSection = cast<MCSectionELF>(*Fragment->getParent());
614 uint64_t C = Target.getConstant();
615 uint64_t FixupOffset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
617 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
618 assert(RefB->getKind() == MCSymbolRefExpr::VK_None &&
619 "Should not have constructed this");
621 // Let A, B and C being the components of Target and R be the location of
622 // the fixup. If the fixup is not pcrel, we want to compute (A - B + C).
623 // If it is pcrel, we want to compute (A - B + C - R).
625 // In general, ELF has no relocations for -B. It can only represent (A + C)
626 // or (A + C - R). If B = R + K and the relocation is not pcrel, we can
627 // replace B to implement it: (A - R - K + C)
629 Asm.getContext().reportFatalError(
631 "No relocation available to represent this relative expression");
633 const auto &SymB = cast<MCSymbolELF>(RefB->getSymbol());
635 if (SymB.isUndefined())
636 Asm.getContext().reportFatalError(
638 Twine("symbol '") + SymB.getName() +
639 "' can not be undefined in a subtraction expression");
641 assert(!SymB.isAbsolute() && "Should have been folded");
642 const MCSection &SecB = SymB.getSection();
643 if (&SecB != &FixupSection)
644 Asm.getContext().reportFatalError(
645 Fixup.getLoc(), "Cannot represent a difference across sections");
648 Asm.getContext().reportFatalError(
649 Fixup.getLoc(), "Cannot represent a subtraction with a weak symbol");
651 uint64_t SymBOffset = Layout.getSymbolOffset(SymB);
652 uint64_t K = SymBOffset - FixupOffset;
657 // We either rejected the fixup or folded B into C at this point.
658 const MCSymbolRefExpr *RefA = Target.getSymA();
659 const auto *SymA = RefA ? cast<MCSymbolELF>(&RefA->getSymbol()) : nullptr;
661 bool ViaWeakRef = false;
662 if (SymA && SymA->isVariable()) {
663 const MCExpr *Expr = SymA->getVariableValue();
664 if (const auto *Inner = dyn_cast<MCSymbolRefExpr>(Expr)) {
665 if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF) {
666 SymA = cast<MCSymbolELF>(&Inner->getSymbol());
672 unsigned Type = GetRelocType(Target, Fixup, IsPCRel);
673 bool RelocateWithSymbol = shouldRelocateWithSymbol(Asm, RefA, SymA, C, Type);
674 if (!RelocateWithSymbol && SymA && !SymA->isUndefined())
675 C += Layout.getSymbolOffset(*SymA);
678 if (hasRelocationAddend()) {
685 if (!RelocateWithSymbol) {
686 const MCSection *SecA =
687 (SymA && !SymA->isUndefined()) ? &SymA->getSection() : nullptr;
688 auto *ELFSec = cast_or_null<MCSectionELF>(SecA);
689 const auto *SectionSymbol =
690 ELFSec ? cast<MCSymbolELF>(ELFSec->getBeginSymbol()) : nullptr;
691 ELFRelocationEntry Rec(FixupOffset, SectionSymbol, Type, Addend);
692 Relocations[&FixupSection].push_back(Rec);
697 if (const MCSymbolELF *R = Renames.lookup(SymA))
701 SymA->setIsWeakrefUsedInReloc();
703 SymA->setUsedInReloc();
705 ELFRelocationEntry Rec(FixupOffset, SymA, Type, Addend);
706 Relocations[&FixupSection].push_back(Rec);
710 bool ELFObjectWriter::isInSymtab(const MCAsmLayout &Layout,
711 const MCSymbolELF &Symbol, bool Used,
713 if (Symbol.isVariable()) {
714 const MCExpr *Expr = Symbol.getVariableValue();
715 if (const MCSymbolRefExpr *Ref = dyn_cast<MCSymbolRefExpr>(Expr)) {
716 if (Ref->getKind() == MCSymbolRefExpr::VK_WEAKREF)
727 if (Symbol.isVariable() && Symbol.isUndefined()) {
728 // FIXME: this is here just to diagnose the case of a var = commmon_sym.
729 Layout.getBaseSymbol(Symbol);
733 if (Symbol.isUndefined() && !Symbol.isBindingSet())
736 if (Symbol.getType() == ELF::STT_SECTION)
739 if (Symbol.isTemporary())
745 void ELFObjectWriter::computeSymbolTable(
746 MCAssembler &Asm, const MCAsmLayout &Layout,
747 const SectionIndexMapTy &SectionIndexMap, const RevGroupMapTy &RevGroupMap,
748 SectionOffsetsTy &SectionOffsets) {
749 MCContext &Ctx = Asm.getContext();
750 SymbolTableWriter Writer(*this, is64Bit());
753 unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
754 MCSectionELF *SymtabSection =
755 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0, EntrySize, "");
756 SymtabSection->setAlignment(is64Bit() ? 8 : 4);
757 SymbolTableIndex = addToSectionTable(SymtabSection);
760 OffsetToAlignment(OS.tell(), SymtabSection->getAlignment());
763 uint64_t SecStart = OS.tell();
765 // The first entry is the undefined symbol entry.
766 Writer.writeSymbol(0, 0, 0, 0, 0, 0, false);
768 std::vector<ELFSymbolData> LocalSymbolData;
769 std::vector<ELFSymbolData> ExternalSymbolData;
771 // Add the data for the symbols.
772 bool HasLargeSectionIndex = false;
773 for (const MCSymbol &S : Asm.symbols()) {
774 const auto &Symbol = cast<MCSymbolELF>(S);
775 bool Used = Symbol.isUsedInReloc();
776 bool WeakrefUsed = Symbol.isWeakrefUsedInReloc();
777 bool isSignature = Symbol.isSignature();
779 if (!isInSymtab(Layout, Symbol, Used || WeakrefUsed || isSignature,
780 Renames.count(&Symbol)))
784 MSD.Symbol = cast<MCSymbolELF>(&Symbol);
786 bool Local = Symbol.getBinding() == ELF::STB_LOCAL;
787 if (Symbol.isAbsolute()) {
788 MSD.SectionIndex = ELF::SHN_ABS;
789 } else if (Symbol.isCommon()) {
791 MSD.SectionIndex = ELF::SHN_COMMON;
792 } else if (Symbol.isUndefined()) {
793 if (isSignature && !Used) {
794 MSD.SectionIndex = RevGroupMap.lookup(&Symbol);
795 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
796 HasLargeSectionIndex = true;
798 MSD.SectionIndex = ELF::SHN_UNDEF;
801 const MCSectionELF &Section =
802 static_cast<const MCSectionELF &>(Symbol.getSection());
803 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
804 assert(MSD.SectionIndex && "Invalid section index!");
805 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
806 HasLargeSectionIndex = true;
809 // The @@@ in symbol version is replaced with @ in undefined symbols and @@
812 // FIXME: All name handling should be done before we get to the writer,
813 // including dealing with GNU-style version suffixes. Fixing this isn't
816 // We thus have to be careful to not perform the symbol version replacement
819 // The ELF format is used on Windows by the MCJIT engine. Thus, on
820 // Windows, the ELFObjectWriter can encounter symbols mangled using the MS
821 // Visual Studio C++ name mangling scheme. Symbols mangled using the MSVC
822 // C++ name mangling can legally have "@@@" as a sub-string. In that case,
823 // the EFLObjectWriter should not interpret the "@@@" sub-string as
824 // specifying GNU-style symbol versioning. The ELFObjectWriter therefore
825 // checks for the MSVC C++ name mangling prefix which is either "?", "@?",
826 // "__imp_?" or "__imp_@?".
828 // It would have been interesting to perform the MS mangling prefix check
829 // only when the target triple is of the form *-pc-windows-elf. But, it
830 // seems that this information is not easily accessible from the
832 StringRef Name = Symbol.getName();
833 if (!Name.startswith("?") && !Name.startswith("@?") &&
834 !Name.startswith("__imp_?") && !Name.startswith("__imp_@?")) {
835 // This symbol isn't following the MSVC C++ name mangling convention. We
836 // can thus safely interpret the @@@ in symbol names as specifying symbol
839 size_t Pos = Name.find("@@@");
840 if (Pos != StringRef::npos) {
841 Buf += Name.substr(0, Pos);
842 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
843 Buf += Name.substr(Pos + Skip);
848 // Sections have their own string table
849 if (Symbol.getType() != ELF::STT_SECTION)
850 MSD.Name = StrTabBuilder.add(Name);
853 LocalSymbolData.push_back(MSD);
855 ExternalSymbolData.push_back(MSD);
858 if (HasLargeSectionIndex) {
859 MCSectionELF *SymtabShndxSection =
860 Ctx.getELFSection(".symtab_shndxr", ELF::SHT_SYMTAB_SHNDX, 0, 4, "");
861 SymtabShndxSectionIndex = addToSectionTable(SymtabShndxSection);
862 SymtabShndxSection->setAlignment(4);
865 ArrayRef<std::string> FileNames = Asm.getFileNames();
866 for (const std::string &Name : FileNames)
867 StrTabBuilder.add(Name);
869 StrTabBuilder.finalize(StringTableBuilder::ELF);
871 for (const std::string &Name : FileNames)
872 Writer.writeSymbol(StrTabBuilder.getOffset(Name),
873 ELF::STT_FILE | ELF::STB_LOCAL, 0, 0, ELF::STV_DEFAULT,
876 // Symbols are required to be in lexicographic order.
877 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
878 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
880 // Set the symbol indices. Local symbols must come before all other
881 // symbols with non-local bindings.
882 unsigned Index = FileNames.size() + 1;
884 for (ELFSymbolData &MSD : LocalSymbolData) {
885 unsigned StringIndex = MSD.Symbol->getType() == ELF::STT_SECTION
887 : StrTabBuilder.getOffset(MSD.Name);
888 MSD.Symbol->setIndex(Index++);
889 writeSymbol(Writer, StringIndex, MSD, Layout);
892 // Write the symbol table entries.
893 LastLocalSymbolIndex = Index;
895 for (ELFSymbolData &MSD : ExternalSymbolData) {
896 unsigned StringIndex = StrTabBuilder.getOffset(MSD.Name);
897 MSD.Symbol->setIndex(Index++);
898 writeSymbol(Writer, StringIndex, MSD, Layout);
899 assert(MSD.Symbol->getBinding() != ELF::STB_LOCAL);
902 uint64_t SecEnd = OS.tell();
903 SectionOffsets[SymtabSection] = std::make_pair(SecStart, SecEnd);
905 ArrayRef<uint32_t> ShndxIndexes = Writer.getShndxIndexes();
906 if (ShndxIndexes.empty()) {
907 assert(SymtabShndxSectionIndex == 0);
910 assert(SymtabShndxSectionIndex != 0);
912 SecStart = OS.tell();
913 const MCSectionELF *SymtabShndxSection =
914 SectionTable[SymtabShndxSectionIndex - 1];
915 for (uint32_t Index : ShndxIndexes)
918 SectionOffsets[SymtabShndxSection] = std::make_pair(SecStart, SecEnd);
922 ELFObjectWriter::createRelocationSection(MCContext &Ctx,
923 const MCSectionELF &Sec) {
924 if (Relocations[&Sec].empty())
927 const StringRef SectionName = Sec.getSectionName();
928 std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
929 RelaSectionName += SectionName;
932 if (hasRelocationAddend())
933 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
935 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
938 if (Sec.getFlags() & ELF::SHF_GROUP)
939 Flags = ELF::SHF_GROUP;
941 MCSectionELF *RelaSection = Ctx.createELFRelSection(
942 RelaSectionName, hasRelocationAddend() ? ELF::SHT_RELA : ELF::SHT_REL,
943 Flags, EntrySize, Sec.getGroup(), &Sec);
944 RelaSection->setAlignment(is64Bit() ? 8 : 4);
948 static SmallVector<char, 128>
949 getUncompressedData(const MCAsmLayout &Layout,
950 const MCSection::FragmentListType &Fragments) {
951 SmallVector<char, 128> UncompressedData;
952 for (const MCFragment &F : Fragments) {
953 const SmallVectorImpl<char> *Contents;
954 switch (F.getKind()) {
955 case MCFragment::FT_Data:
956 Contents = &cast<MCDataFragment>(F).getContents();
958 case MCFragment::FT_Dwarf:
959 Contents = &cast<MCDwarfLineAddrFragment>(F).getContents();
961 case MCFragment::FT_DwarfFrame:
962 Contents = &cast<MCDwarfCallFrameFragment>(F).getContents();
966 "Not expecting any other fragment types in a debug_* section");
968 UncompressedData.append(Contents->begin(), Contents->end());
970 return UncompressedData;
973 // Include the debug info compression header:
974 // "ZLIB" followed by 8 bytes representing the uncompressed size of the section,
975 // useful for consumers to preallocate a buffer to decompress into.
977 prependCompressionHeader(uint64_t Size,
978 SmallVectorImpl<char> &CompressedContents) {
979 const StringRef Magic = "ZLIB";
980 if (Size <= Magic.size() + sizeof(Size) + CompressedContents.size())
982 if (sys::IsLittleEndianHost)
983 sys::swapByteOrder(Size);
984 CompressedContents.insert(CompressedContents.begin(),
985 Magic.size() + sizeof(Size), 0);
986 std::copy(Magic.begin(), Magic.end(), CompressedContents.begin());
987 std::copy(reinterpret_cast<char *>(&Size),
988 reinterpret_cast<char *>(&Size + 1),
989 CompressedContents.begin() + Magic.size());
993 void ELFObjectWriter::writeSectionData(const MCAssembler &Asm, MCSection &Sec,
994 const MCAsmLayout &Layout) {
995 MCSectionELF &Section = static_cast<MCSectionELF &>(Sec);
996 StringRef SectionName = Section.getSectionName();
998 // Compressing debug_frame requires handling alignment fragments which is
999 // more work (possibly generalizing MCAssembler.cpp:writeFragment to allow
1000 // for writing to arbitrary buffers) for little benefit.
1001 if (!Asm.getContext().getAsmInfo()->compressDebugSections() ||
1002 !SectionName.startswith(".debug_") || SectionName == ".debug_frame") {
1003 Asm.writeSectionData(&Section, Layout);
1007 // Gather the uncompressed data from all the fragments.
1008 const MCSection::FragmentListType &Fragments = Section.getFragmentList();
1009 SmallVector<char, 128> UncompressedData =
1010 getUncompressedData(Layout, Fragments);
1012 SmallVector<char, 128> CompressedContents;
1013 zlib::Status Success = zlib::compress(
1014 StringRef(UncompressedData.data(), UncompressedData.size()),
1015 CompressedContents);
1016 if (Success != zlib::StatusOK) {
1017 Asm.writeSectionData(&Section, Layout);
1021 if (!prependCompressionHeader(UncompressedData.size(), CompressedContents)) {
1022 Asm.writeSectionData(&Section, Layout);
1025 Asm.getContext().renameELFSection(&Section,
1026 (".z" + SectionName.drop_front(1)).str());
1027 OS << CompressedContents;
1030 void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
1031 uint64_t Flags, uint64_t Address,
1032 uint64_t Offset, uint64_t Size,
1033 uint32_t Link, uint32_t Info,
1035 uint64_t EntrySize) {
1036 Write32(Name); // sh_name: index into string table
1037 Write32(Type); // sh_type
1038 WriteWord(Flags); // sh_flags
1039 WriteWord(Address); // sh_addr
1040 WriteWord(Offset); // sh_offset
1041 WriteWord(Size); // sh_size
1042 Write32(Link); // sh_link
1043 Write32(Info); // sh_info
1044 WriteWord(Alignment); // sh_addralign
1045 WriteWord(EntrySize); // sh_entsize
1048 void ELFObjectWriter::writeRelocations(const MCAssembler &Asm,
1049 const MCSectionELF &Sec) {
1050 std::vector<ELFRelocationEntry> &Relocs = Relocations[&Sec];
1052 // Sort the relocation entries. Most targets just sort by Offset, but some
1053 // (e.g., MIPS) have additional constraints.
1054 TargetObjectWriter->sortRelocs(Asm, Relocs);
1056 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1057 const ELFRelocationEntry &Entry = Relocs[e - i - 1];
1058 unsigned Index = Entry.Symbol ? Entry.Symbol->getIndex() : 0;
1061 write(Entry.Offset);
1062 if (TargetObjectWriter->isN64()) {
1063 write(uint32_t(Index));
1065 write(TargetObjectWriter->getRSsym(Entry.Type));
1066 write(TargetObjectWriter->getRType3(Entry.Type));
1067 write(TargetObjectWriter->getRType2(Entry.Type));
1068 write(TargetObjectWriter->getRType(Entry.Type));
1070 struct ELF::Elf64_Rela ERE64;
1071 ERE64.setSymbolAndType(Index, Entry.Type);
1072 write(ERE64.r_info);
1074 if (hasRelocationAddend())
1075 write(Entry.Addend);
1077 write(uint32_t(Entry.Offset));
1079 struct ELF::Elf32_Rela ERE32;
1080 ERE32.setSymbolAndType(Index, Entry.Type);
1081 write(ERE32.r_info);
1083 if (hasRelocationAddend())
1084 write(uint32_t(Entry.Addend));
1089 const MCSectionELF *ELFObjectWriter::createStringTable(MCContext &Ctx) {
1090 const MCSectionELF *StrtabSection = SectionTable[StringTableIndex - 1];
1091 OS << StrTabBuilder.data();
1092 return StrtabSection;
1095 void ELFObjectWriter::writeSection(const SectionIndexMapTy &SectionIndexMap,
1096 uint32_t GroupSymbolIndex, uint64_t Offset,
1097 uint64_t Size, const MCSectionELF &Section) {
1098 uint64_t sh_link = 0;
1099 uint64_t sh_info = 0;
1101 switch(Section.getType()) {
1106 case ELF::SHT_DYNAMIC:
1107 llvm_unreachable("SHT_DYNAMIC in a relocatable object");
1110 case ELF::SHT_RELA: {
1111 sh_link = SymbolTableIndex;
1112 assert(sh_link && ".symtab not found");
1113 const MCSectionELF *InfoSection = Section.getAssociatedSection();
1114 sh_info = SectionIndexMap.lookup(InfoSection);
1118 case ELF::SHT_SYMTAB:
1119 case ELF::SHT_DYNSYM:
1120 sh_link = StringTableIndex;
1121 sh_info = LastLocalSymbolIndex;
1124 case ELF::SHT_SYMTAB_SHNDX:
1125 sh_link = SymbolTableIndex;
1128 case ELF::SHT_GROUP:
1129 sh_link = SymbolTableIndex;
1130 sh_info = GroupSymbolIndex;
1134 if (TargetObjectWriter->getEMachine() == ELF::EM_ARM &&
1135 Section.getType() == ELF::SHT_ARM_EXIDX)
1136 sh_link = SectionIndexMap.lookup(Section.getAssociatedSection());
1138 WriteSecHdrEntry(StrTabBuilder.getOffset(Section.getSectionName()),
1139 Section.getType(), Section.getFlags(), 0, Offset, Size,
1140 sh_link, sh_info, Section.getAlignment(),
1141 Section.getEntrySize());
1144 void ELFObjectWriter::writeSectionHeader(
1145 const MCAssembler &Asm, const MCAsmLayout &Layout,
1146 const SectionIndexMapTy &SectionIndexMap,
1147 const SectionOffsetsTy &SectionOffsets) {
1148 const unsigned NumSections = SectionTable.size();
1150 // Null section first.
1151 uint64_t FirstSectionSize =
1152 (NumSections + 1) >= ELF::SHN_LORESERVE ? NumSections + 1 : 0;
1153 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, 0, 0, 0, 0);
1155 for (const MCSectionELF *Section : SectionTable) {
1156 uint32_t GroupSymbolIndex;
1157 unsigned Type = Section->getType();
1158 if (Type != ELF::SHT_GROUP)
1159 GroupSymbolIndex = 0;
1161 GroupSymbolIndex = Section->getGroup()->getIndex();
1163 const std::pair<uint64_t, uint64_t> &Offsets =
1164 SectionOffsets.find(Section)->second;
1166 if (Type == ELF::SHT_NOBITS)
1167 Size = Layout.getSectionAddressSize(Section);
1169 Size = Offsets.second - Offsets.first;
1171 writeSection(SectionIndexMap, GroupSymbolIndex, Offsets.first, Size,
1176 void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1177 const MCAsmLayout &Layout) {
1178 MCContext &Ctx = Asm.getContext();
1179 MCSectionELF *StrtabSection =
1180 Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0);
1181 StringTableIndex = addToSectionTable(StrtabSection);
1183 RevGroupMapTy RevGroupMap;
1184 SectionIndexMapTy SectionIndexMap;
1186 std::map<const MCSymbol *, std::vector<const MCSectionELF *>> GroupMembers;
1188 // Write out the ELF header ...
1191 // ... then the sections ...
1192 SectionOffsetsTy SectionOffsets;
1193 std::vector<MCSectionELF *> Groups;
1194 std::vector<MCSectionELF *> Relocations;
1195 for (MCSection &Sec : Asm) {
1196 MCSectionELF &Section = static_cast<MCSectionELF &>(Sec);
1198 uint64_t Padding = OffsetToAlignment(OS.tell(), Section.getAlignment());
1199 WriteZeros(Padding);
1201 // Remember the offset into the file for this section.
1202 uint64_t SecStart = OS.tell();
1204 const MCSymbolELF *SignatureSymbol = Section.getGroup();
1205 writeSectionData(Asm, Section, Layout);
1207 uint64_t SecEnd = OS.tell();
1208 SectionOffsets[&Section] = std::make_pair(SecStart, SecEnd);
1210 MCSectionELF *RelSection = createRelocationSection(Ctx, Section);
1212 if (SignatureSymbol) {
1213 Asm.registerSymbol(*SignatureSymbol);
1214 unsigned &GroupIdx = RevGroupMap[SignatureSymbol];
1216 MCSectionELF *Group = Ctx.createELFGroupSection(SignatureSymbol);
1217 GroupIdx = addToSectionTable(Group);
1218 Group->setAlignment(4);
1219 Groups.push_back(Group);
1221 GroupMembers[SignatureSymbol].push_back(&Section);
1223 GroupMembers[SignatureSymbol].push_back(RelSection);
1226 SectionIndexMap[&Section] = addToSectionTable(&Section);
1228 SectionIndexMap[RelSection] = addToSectionTable(RelSection);
1229 Relocations.push_back(RelSection);
1233 for (MCSectionELF *Group : Groups) {
1234 uint64_t Padding = OffsetToAlignment(OS.tell(), Group->getAlignment());
1235 WriteZeros(Padding);
1237 // Remember the offset into the file for this section.
1238 uint64_t SecStart = OS.tell();
1240 const MCSymbol *SignatureSymbol = Group->getGroup();
1241 assert(SignatureSymbol);
1242 write(uint32_t(ELF::GRP_COMDAT));
1243 for (const MCSectionELF *Member : GroupMembers[SignatureSymbol]) {
1244 uint32_t SecIndex = SectionIndexMap.lookup(Member);
1248 uint64_t SecEnd = OS.tell();
1249 SectionOffsets[Group] = std::make_pair(SecStart, SecEnd);
1252 // Compute symbol table information.
1253 computeSymbolTable(Asm, Layout, SectionIndexMap, RevGroupMap, SectionOffsets);
1255 for (MCSectionELF *RelSection : Relocations) {
1256 uint64_t Padding = OffsetToAlignment(OS.tell(), RelSection->getAlignment());
1257 WriteZeros(Padding);
1259 // Remember the offset into the file for this section.
1260 uint64_t SecStart = OS.tell();
1262 writeRelocations(Asm, *RelSection->getAssociatedSection());
1264 uint64_t SecEnd = OS.tell();
1265 SectionOffsets[RelSection] = std::make_pair(SecStart, SecEnd);
1269 uint64_t SecStart = OS.tell();
1270 const MCSectionELF *Sec = createStringTable(Ctx);
1271 uint64_t SecEnd = OS.tell();
1272 SectionOffsets[Sec] = std::make_pair(SecStart, SecEnd);
1275 uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
1276 uint64_t Padding = OffsetToAlignment(OS.tell(), NaturalAlignment);
1277 WriteZeros(Padding);
1279 const unsigned SectionHeaderOffset = OS.tell();
1281 // ... then the section header table ...
1282 writeSectionHeader(Asm, Layout, SectionIndexMap, SectionOffsets);
1284 uint16_t NumSections = (SectionTable.size() + 1 >= ELF::SHN_LORESERVE)
1285 ? (uint16_t)ELF::SHN_UNDEF
1286 : SectionTable.size() + 1;
1287 if (sys::IsLittleEndianHost != IsLittleEndian)
1288 sys::swapByteOrder(NumSections);
1289 unsigned NumSectionsOffset;
1292 uint64_t Val = SectionHeaderOffset;
1293 if (sys::IsLittleEndianHost != IsLittleEndian)
1294 sys::swapByteOrder(Val);
1295 OS.pwrite(reinterpret_cast<char *>(&Val), sizeof(Val),
1296 offsetof(ELF::Elf64_Ehdr, e_shoff));
1297 NumSectionsOffset = offsetof(ELF::Elf64_Ehdr, e_shnum);
1299 uint32_t Val = SectionHeaderOffset;
1300 if (sys::IsLittleEndianHost != IsLittleEndian)
1301 sys::swapByteOrder(Val);
1302 OS.pwrite(reinterpret_cast<char *>(&Val), sizeof(Val),
1303 offsetof(ELF::Elf32_Ehdr, e_shoff));
1304 NumSectionsOffset = offsetof(ELF::Elf32_Ehdr, e_shnum);
1306 OS.pwrite(reinterpret_cast<char *>(&NumSections), sizeof(NumSections),
1310 bool ELFObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(
1311 const MCAssembler &Asm, const MCSymbol &SA, const MCFragment &FB,
1312 bool InSet, bool IsPCRel) const {
1313 const auto &SymA = cast<MCSymbolELF>(SA);
1319 return MCObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB,
1323 bool ELFObjectWriter::isWeak(const MCSymbol &S) const {
1324 const auto &Sym = cast<MCSymbolELF>(S);
1328 // It is invalid to replace a reference to a global in a comdat
1329 // with a reference to a local since out of comdat references
1330 // to a local are forbidden.
1331 // We could try to return false for more cases, like the reference
1332 // being in the same comdat or Sym being an alias to another global,
1333 // but it is not clear if it is worth the effort.
1334 if (Sym.getBinding() != ELF::STB_GLOBAL)
1337 if (!Sym.isInSection())
1340 const auto &Sec = cast<MCSectionELF>(Sym.getSection());
1341 return Sec.getGroup();
1344 MCObjectWriter *llvm::createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1345 raw_pwrite_stream &OS,
1346 bool IsLittleEndian) {
1347 return new ELFObjectWriter(MOTW, OS, IsLittleEndian);