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 const SectionIndexMapTy &SectionIndexMap);
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,
327 const SectionIndexMapTy &SectionIndexMap);
329 void ExecutePostLayoutBinding(MCAssembler &Asm);
331 void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
332 uint64_t Address, uint64_t Offset,
333 uint64_t Size, uint32_t Link, uint32_t Info,
334 uint64_t Alignment, uint64_t EntrySize);
336 void WriteRelocationsFragment(const MCAssembler &Asm, MCDataFragment *F,
337 const MCSectionData *SD);
339 bool IsFixupFullyResolved(const MCAssembler &Asm,
340 const MCValue Target,
342 const MCFragment *DF) const;
344 void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
345 void WriteSection(MCAssembler &Asm,
346 const SectionIndexMapTy &SectionIndexMap,
347 uint64_t Offset, uint64_t Size, uint64_t Alignment,
348 const MCSectionELF &Section);
353 // Emit the ELF header.
354 void ELFObjectWriterImpl::WriteHeader(uint64_t SectionDataSize,
355 unsigned NumberOfSections) {
361 // emitWord method behaves differently for ELF32 and ELF64, writing
362 // 4 bytes in the former and 8 in the latter.
364 Write8(0x7f); // e_ident[EI_MAG0]
365 Write8('E'); // e_ident[EI_MAG1]
366 Write8('L'); // e_ident[EI_MAG2]
367 Write8('F'); // e_ident[EI_MAG3]
369 Write8(Is64Bit ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
372 Write8(Writer->isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
374 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
377 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
378 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
379 default: Write8(ELF::ELFOSABI_NONE); break;
381 Write8(0); // e_ident[EI_ABIVERSION]
383 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
385 Write16(ELF::ET_REL); // e_type
387 Write16(EMachine); // e_machine = target
389 Write32(ELF::EV_CURRENT); // e_version
390 WriteWord(0); // e_entry, no entry point in .o file
391 WriteWord(0); // e_phoff, no program header for .o
392 WriteWord(SectionDataSize + (Is64Bit ? sizeof(ELF::Elf64_Ehdr) :
393 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
395 // FIXME: Make this configurable.
396 Write32(0); // e_flags = whatever the target wants
398 // e_ehsize = ELF header size
399 Write16(Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
401 Write16(0); // e_phentsize = prog header entry size
402 Write16(0); // e_phnum = # prog header entries = 0
404 // e_shentsize = Section header entry size
405 Write16(Is64Bit ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
407 // e_shnum = # of section header ents
408 if (NumberOfSections >= ELF::SHN_LORESERVE)
411 Write16(NumberOfSections);
413 // e_shstrndx = Section # of '.shstrtab'
414 if (NumberOfSections >= ELF::SHN_LORESERVE)
415 Write16(ELF::SHN_XINDEX);
417 Write16(ShstrtabIndex);
420 void ELFObjectWriterImpl::WriteSymbolEntry(MCDataFragment *SymtabF,
421 MCDataFragment *ShndxF,
423 uint8_t info, uint64_t value,
424 uint64_t size, uint8_t other,
428 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
429 String32(*ShndxF, shndx);
431 String32(*ShndxF, 0);
434 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
435 uint16_t(ELF::SHN_XINDEX) : shndx;
438 String32(*SymtabF, name); // st_name
439 String8(*SymtabF, info); // st_info
440 String8(*SymtabF, other); // st_other
441 String16(*SymtabF, Index); // st_shndx
442 String64(*SymtabF, value); // st_value
443 String64(*SymtabF, size); // st_size
445 String32(*SymtabF, name); // st_name
446 String32(*SymtabF, value); // st_value
447 String32(*SymtabF, size); // st_size
448 String8(*SymtabF, info); // st_info
449 String8(*SymtabF, other); // st_other
450 String16(*SymtabF, Index); // st_shndx
454 static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout) {
455 if (Data.isCommon() && Data.isExternal())
456 return Data.getCommonAlignment();
458 const MCSymbol &Symbol = Data.getSymbol();
459 if (!Symbol.isInSection())
462 if (MCFragment *FF = Data.getFragment())
463 return Layout.getSymbolAddress(&Data) -
464 Layout.getSectionAddress(FF->getParent());
469 static const MCSymbol &AliasedSymbol(const MCSymbol &Symbol) {
470 const MCSymbol *S = &Symbol;
471 while (S->isVariable()) {
472 const MCExpr *Value = S->getVariableValue();
473 if (Value->getKind() != MCExpr::SymbolRef)
475 const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr*>(Value);
476 S = &Ref->getSymbol();
481 void ELFObjectWriterImpl::ExecutePostLayoutBinding(MCAssembler &Asm) {
482 // The presence of symbol versions causes undefined symbols and
483 // versions declared with @@@ to be renamed.
485 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
486 ie = Asm.symbol_end(); it != ie; ++it) {
487 const MCSymbol &Alias = it->getSymbol();
488 const MCSymbol &Symbol = AliasedSymbol(Alias);
489 MCSymbolData &SD = Asm.getSymbolData(Symbol);
491 // Undefined symbols are global, but this is the first place we
492 // are able to set it.
493 if (Symbol.isUndefined() && !Symbol.isVariable()) {
494 if (GetBinding(SD) == ELF::STB_LOCAL) {
495 SetBinding(SD, ELF::STB_GLOBAL);
496 SetBinding(*it, ELF::STB_GLOBAL);
501 if (&Symbol == &Alias)
504 StringRef AliasName = Alias.getName();
505 size_t Pos = AliasName.find('@');
506 if (Pos == StringRef::npos)
509 // Aliases defined with .symvar copy the binding from the symbol they alias.
510 // This is the first place we are able to copy this information.
511 it->setExternal(SD.isExternal());
512 SetBinding(*it, GetBinding(SD));
514 StringRef Rest = AliasName.substr(Pos);
515 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
518 // FIXME: produce a better error message.
519 if (Symbol.isUndefined() && Rest.startswith("@@") &&
520 !Rest.startswith("@@@"))
521 report_fatal_error("A @@ version cannot be undefined");
523 Renames.insert(std::make_pair(&Symbol, &Alias));
527 void ELFObjectWriterImpl::WriteSymbol(MCDataFragment *SymtabF,
528 MCDataFragment *ShndxF,
530 const MCAsmLayout &Layout) {
531 MCSymbolData &OrigData = *MSD.SymbolData;
533 Layout.getAssembler().getSymbolData(AliasedSymbol(OrigData.getSymbol()));
535 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
536 Data.getSymbol().isVariable();
538 uint8_t Binding = GetBinding(OrigData);
539 uint8_t Visibility = GetVisibility(OrigData);
540 uint8_t Type = GetType(Data);
542 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
543 uint8_t Other = Visibility;
545 uint64_t Value = SymbolValue(Data, Layout);
549 assert(!(Data.isCommon() && !Data.isExternal()));
551 ESize = Data.getSize();
552 if (Data.getSize()) {
554 if (ESize->getKind() == MCExpr::Binary) {
555 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(ESize);
557 if (BE->EvaluateAsRelocatable(Res, &Layout)) {
558 assert(!Res.getSymA() || !Res.getSymA()->getSymbol().isDefined());
559 assert(!Res.getSymB() || !Res.getSymB()->getSymbol().isDefined());
560 Size = Res.getConstant();
562 } else if (ESize->getKind() == MCExpr::Constant) {
563 Size = static_cast<const MCConstantExpr *>(ESize)->getValue();
565 assert(0 && "Unsupported size expression");
569 // Write out the symbol table entry
570 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
571 Size, Other, MSD.SectionIndex, IsReserved);
574 void ELFObjectWriterImpl::WriteSymbolTable(MCDataFragment *SymtabF,
575 MCDataFragment *ShndxF,
576 const MCAssembler &Asm,
577 const MCAsmLayout &Layout,
578 const SectionIndexMapTy &SectionIndexMap) {
579 // The string table must be emitted first because we need the index
580 // into the string table for all the symbol names.
581 assert(StringTable.size() && "Missing string table");
583 // FIXME: Make sure the start of the symbol table is aligned.
585 // The first entry is the undefined symbol entry.
586 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
588 // Write the symbol table entries.
589 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
590 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
591 ELFSymbolData &MSD = LocalSymbolData[i];
592 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
595 // Write out a symbol table entry for each regular section.
596 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
598 const MCSectionELF &Section =
599 static_cast<const MCSectionELF&>(i->getSection());
600 if (Section.getType() == ELF::SHT_RELA ||
601 Section.getType() == ELF::SHT_REL ||
602 Section.getType() == ELF::SHT_STRTAB ||
603 Section.getType() == ELF::SHT_SYMTAB)
605 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
606 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section), false);
607 LastLocalSymbolIndex++;
610 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
611 ELFSymbolData &MSD = ExternalSymbolData[i];
612 MCSymbolData &Data = *MSD.SymbolData;
613 assert(((Data.getFlags() & ELF_STB_Global) ||
614 (Data.getFlags() & ELF_STB_Weak)) &&
615 "External symbol requires STB_GLOBAL or STB_WEAK flag");
616 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
617 if (GetBinding(Data) == ELF::STB_LOCAL)
618 LastLocalSymbolIndex++;
621 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
622 ELFSymbolData &MSD = UndefinedSymbolData[i];
623 MCSymbolData &Data = *MSD.SymbolData;
624 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
625 if (GetBinding(Data) == ELF::STB_LOCAL)
626 LastLocalSymbolIndex++;
630 static bool ShouldRelocOnSymbol(const MCSymbolData &SD,
631 const MCValue &Target,
632 const MCFragment &F) {
633 const MCSymbol &Symbol = SD.getSymbol();
634 if (Symbol.isUndefined())
637 const MCSectionELF &Section =
638 static_cast<const MCSectionELF&>(Symbol.getSection());
643 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
644 const MCSectionELF &Sec2 =
645 static_cast<const MCSectionELF&>(F.getParent()->getSection());
647 if (Section.getKind().isBSS())
650 if (&Sec2 != &Section &&
651 (Kind == MCSymbolRefExpr::VK_PLT ||
652 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
653 Kind == MCSymbolRefExpr::VK_GOTOFF))
656 if (Section.getFlags() & MCSectionELF::SHF_MERGE)
657 return Target.getConstant() != 0;
662 // FIXME: this is currently X86/X86_64 only
663 void ELFObjectWriterImpl::RecordRelocation(const MCAssembler &Asm,
664 const MCAsmLayout &Layout,
665 const MCFragment *Fragment,
666 const MCFixup &Fixup,
668 uint64_t &FixedValue) {
671 int64_t Value = Target.getConstant();
672 const MCSymbol *Symbol = 0;
673 const MCSymbol *Renamed = 0;
675 bool IsPCRel = isFixupKindX86PCRel(Fixup.getKind());
676 if (!Target.isAbsolute()) {
677 Symbol = &AliasedSymbol(Target.getSymA()->getSymbol());
678 Renamed = Renames.lookup(Symbol);
680 Renamed = &Target.getSymA()->getSymbol();
681 MCSymbolData &SD = Asm.getSymbolData(*Symbol);
682 MCFragment *F = SD.getFragment();
684 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
685 const MCSymbol &SymbolB = RefB->getSymbol();
686 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
688 MCSectionData *Sec = Fragment->getParent();
690 // Offset of the symbol in the section
691 int64_t a = Layout.getSymbolAddress(&SDB) - Layout.getSectionAddress(Sec);
693 // Ofeset of the relocation in the section
694 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
698 // Check that this case has already been fully resolved before we get
700 if (Symbol->isDefined() && !SD.isExternal() &&
702 &Fragment->getParent()->getSection() == &Symbol->getSection()) {
703 llvm_unreachable("We don't need a relocation in this case.");
707 bool RelocOnSymbol = ShouldRelocOnSymbol(SD, Target, *Fragment);
708 if (!RelocOnSymbol) {
709 Index = F->getParent()->getOrdinal();
711 MCSectionData *FSD = F->getParent();
712 // Offset of the symbol in the section
713 Value += Layout.getSymbolAddress(&SD) - Layout.getSectionAddress(FSD);
715 UsedInReloc.insert(Renamed);
716 MCSymbolData &RenamedSD = Asm.getSymbolData(*Renamed);
717 if (RenamedSD.getFlags() & ELF_Other_Weakref) {
718 WeakrefUsedInReloc.insert(Symbol);
723 // Compensate for the addend on i386.
730 // determine the type of the relocation
732 MCSymbolRefExpr::VariantKind Modifier = Target.getSymA()->getKind();
738 llvm_unreachable("Unimplemented");
739 case MCSymbolRefExpr::VK_None:
740 Type = ELF::R_X86_64_PC32;
742 case MCSymbolRefExpr::VK_PLT:
743 Type = ELF::R_X86_64_PLT32;
745 case MCSymbolRefExpr::VK_GOTPCREL:
746 Type = ELF::R_X86_64_GOTPCREL;
748 case MCSymbolRefExpr::VK_GOTTPOFF:
749 Type = ELF::R_X86_64_GOTTPOFF;
751 case MCSymbolRefExpr::VK_TLSGD:
752 Type = ELF::R_X86_64_TLSGD;
754 case MCSymbolRefExpr::VK_TLSLD:
755 Type = ELF::R_X86_64_TLSLD;
759 switch ((unsigned)Fixup.getKind()) {
760 default: llvm_unreachable("invalid fixup kind!");
761 case FK_Data_8: Type = ELF::R_X86_64_64; break;
762 case X86::reloc_signed_4byte:
763 case X86::reloc_pcrel_4byte:
764 assert(isInt<32>(Target.getConstant()));
767 llvm_unreachable("Unimplemented");
768 case MCSymbolRefExpr::VK_None:
769 Type = ELF::R_X86_64_32S;
771 case MCSymbolRefExpr::VK_GOT:
772 Type = ELF::R_X86_64_GOT32;
774 case MCSymbolRefExpr::VK_GOTPCREL:
775 Type = ELF::R_X86_64_GOTPCREL;
777 case MCSymbolRefExpr::VK_TPOFF:
778 Type = ELF::R_X86_64_TPOFF32;
780 case MCSymbolRefExpr::VK_DTPOFF:
781 Type = ELF::R_X86_64_DTPOFF32;
786 Type = ELF::R_X86_64_32;
788 case FK_Data_2: Type = ELF::R_X86_64_16; break;
789 case X86::reloc_pcrel_1byte:
790 case FK_Data_1: Type = ELF::R_X86_64_8; break;
797 llvm_unreachable("Unimplemented");
798 case MCSymbolRefExpr::VK_None:
799 Type = ELF::R_386_PC32;
801 case MCSymbolRefExpr::VK_PLT:
802 Type = ELF::R_386_PLT32;
806 switch ((unsigned)Fixup.getKind()) {
807 default: llvm_unreachable("invalid fixup kind!");
809 case X86::reloc_global_offset_table:
810 Type = ELF::R_386_GOTPC;
813 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
815 case X86::reloc_signed_4byte:
816 case X86::reloc_pcrel_4byte:
820 llvm_unreachable("Unimplemented");
821 case MCSymbolRefExpr::VK_None:
822 Type = ELF::R_386_32;
824 case MCSymbolRefExpr::VK_GOT:
825 Type = ELF::R_386_GOT32;
827 case MCSymbolRefExpr::VK_GOTOFF:
828 Type = ELF::R_386_GOTOFF;
830 case MCSymbolRefExpr::VK_TLSGD:
831 Type = ELF::R_386_TLS_GD;
833 case MCSymbolRefExpr::VK_TPOFF:
834 Type = ELF::R_386_TLS_LE_32;
836 case MCSymbolRefExpr::VK_INDNTPOFF:
837 Type = ELF::R_386_TLS_IE;
839 case MCSymbolRefExpr::VK_NTPOFF:
840 Type = ELF::R_386_TLS_LE;
842 case MCSymbolRefExpr::VK_GOTNTPOFF:
843 Type = ELF::R_386_TLS_GOTIE;
845 case MCSymbolRefExpr::VK_TLSLDM:
846 Type = ELF::R_386_TLS_LDM;
848 case MCSymbolRefExpr::VK_DTPOFF:
849 Type = ELF::R_386_TLS_LDO_32;
853 case FK_Data_2: Type = ELF::R_386_16; break;
854 case X86::reloc_pcrel_1byte:
855 case FK_Data_1: Type = ELF::R_386_8; break;
860 if (RelocNeedsGOT(Modifier))
863 ELFRelocationEntry ERE;
867 ERE.Symbol = Renamed;
869 ERE.r_offset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
871 if (HasRelocationAddend)
872 ERE.r_addend = Addend;
874 ERE.r_addend = 0; // Silence compiler warning.
876 Relocations[Fragment->getParent()].push_back(ERE);
880 ELFObjectWriterImpl::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
882 MCSymbolData &SD = Asm.getSymbolData(*S);
885 if (!SD.isExternal() && !S->isUndefined())
886 return SD.getIndex() + /* empty symbol */ 1;
888 // External or undefined symbol.
889 return SD.getIndex() + NumRegularSections + /* empty symbol */ 1;
892 static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
893 bool Used, bool Renamed) {
894 if (Data.getFlags() & ELF_Other_Weakref)
903 const MCSymbol &Symbol = Data.getSymbol();
905 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
908 const MCSymbol &A = AliasedSymbol(Symbol);
909 if (!A.isVariable() && A.isUndefined() && !Data.isCommon())
912 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
915 if (Symbol.isTemporary())
921 static bool isLocal(const MCSymbolData &Data) {
922 if (Data.isExternal())
925 const MCSymbol &Symbol = Data.getSymbol();
926 if (Symbol.isUndefined() && !Symbol.isVariable())
932 void ELFObjectWriterImpl::ComputeIndexMap(MCAssembler &Asm,
933 SectionIndexMapTy &SectionIndexMap) {
935 for (MCAssembler::iterator it = Asm.begin(),
936 ie = Asm.end(); it != ie; ++it) {
937 const MCSectionELF &Section =
938 static_cast<const MCSectionELF &>(it->getSection());
939 SectionIndexMap[&Section] = Index++;
943 void ELFObjectWriterImpl::ComputeSymbolTable(MCAssembler &Asm,
944 const SectionIndexMapTy &SectionIndexMap) {
945 // FIXME: Is this the correct place to do this?
947 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
948 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
949 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
950 Data.setExternal(true);
951 SetBinding(Data, ELF::STB_GLOBAL);
954 // Build section lookup table.
955 NumRegularSections = Asm.size();
957 // Index 0 is always the empty string.
958 StringMap<uint64_t> StringIndexMap;
959 StringTable += '\x00';
961 // Add the data for the symbols.
962 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
963 ie = Asm.symbol_end(); it != ie; ++it) {
964 const MCSymbol &Symbol = it->getSymbol();
966 bool Used = UsedInReloc.count(&Symbol);
967 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
968 if (!isInSymtab(Asm, *it, Used || WeakrefUsed,
969 Renames.count(&Symbol)))
974 bool Local = isLocal(*it);
975 const MCSymbol &RefSymbol = AliasedSymbol(Symbol);
977 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
978 SetBinding(*it, ELF::STB_WEAK);
980 if (it->isCommon()) {
982 MSD.SectionIndex = ELF::SHN_COMMON;
983 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
984 MSD.SectionIndex = ELF::SHN_ABS;
985 } else if (RefSymbol.isUndefined()) {
986 MSD.SectionIndex = ELF::SHN_UNDEF;
988 const MCSectionELF &Section =
989 static_cast<const MCSectionELF&>(RefSymbol.getSection());
990 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
991 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
992 NeedsSymtabShndx = true;
993 assert(MSD.SectionIndex && "Invalid section index!");
996 // The @@@ in symbol version is replaced with @ in undefined symbols and
997 // @@ in defined ones.
998 StringRef Name = Symbol.getName();
999 size_t Pos = Name.find("@@@");
1000 std::string FinalName;
1001 if (Pos != StringRef::npos) {
1002 StringRef Prefix = Name.substr(0, Pos);
1003 unsigned n = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
1004 StringRef Suffix = Name.substr(Pos + n);
1005 FinalName = Prefix.str() + Suffix.str();
1007 FinalName = Name.str();
1010 uint64_t &Entry = StringIndexMap[FinalName];
1012 Entry = StringTable.size();
1013 StringTable += FinalName;
1014 StringTable += '\x00';
1016 MSD.StringIndex = Entry;
1017 if (MSD.SectionIndex == ELF::SHN_UNDEF)
1018 UndefinedSymbolData.push_back(MSD);
1020 LocalSymbolData.push_back(MSD);
1022 ExternalSymbolData.push_back(MSD);
1025 // Symbols are required to be in lexicographic order.
1026 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
1027 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
1028 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
1030 // Set the symbol indices. Local symbols must come before all other
1031 // symbols with non-local bindings.
1033 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
1034 LocalSymbolData[i].SymbolData->setIndex(Index++);
1035 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
1036 ExternalSymbolData[i].SymbolData->setIndex(Index++);
1037 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
1038 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
1041 void ELFObjectWriterImpl::WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
1042 const MCSectionData &SD) {
1043 if (!Relocations[&SD].empty()) {
1044 MCContext &Ctx = Asm.getContext();
1045 const MCSectionELF *RelaSection;
1046 const MCSectionELF &Section =
1047 static_cast<const MCSectionELF&>(SD.getSection());
1049 const StringRef SectionName = Section.getSectionName();
1050 std::string RelaSectionName = HasRelocationAddend ? ".rela" : ".rel";
1051 RelaSectionName += SectionName;
1054 if (HasRelocationAddend)
1055 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
1057 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
1059 RelaSection = Ctx.getELFSection(RelaSectionName, HasRelocationAddend ?
1060 ELF::SHT_RELA : ELF::SHT_REL, 0,
1061 SectionKind::getReadOnly(),
1064 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
1065 RelaSD.setAlignment(Is64Bit ? 8 : 4);
1067 MCDataFragment *F = new MCDataFragment(&RelaSD);
1069 WriteRelocationsFragment(Asm, F, &SD);
1071 Asm.AddSectionToTheEnd(*Writer, RelaSD, Layout);
1075 void ELFObjectWriterImpl::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
1076 uint64_t Flags, uint64_t Address,
1077 uint64_t Offset, uint64_t Size,
1078 uint32_t Link, uint32_t Info,
1080 uint64_t EntrySize) {
1081 Write32(Name); // sh_name: index into string table
1082 Write32(Type); // sh_type
1083 WriteWord(Flags); // sh_flags
1084 WriteWord(Address); // sh_addr
1085 WriteWord(Offset); // sh_offset
1086 WriteWord(Size); // sh_size
1087 Write32(Link); // sh_link
1088 Write32(Info); // sh_info
1089 WriteWord(Alignment); // sh_addralign
1090 WriteWord(EntrySize); // sh_entsize
1093 void ELFObjectWriterImpl::WriteRelocationsFragment(const MCAssembler &Asm,
1095 const MCSectionData *SD) {
1096 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
1097 // sort by the r_offset just like gnu as does
1098 array_pod_sort(Relocs.begin(), Relocs.end());
1100 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1101 ELFRelocationEntry entry = Relocs[e - i - 1];
1103 if (entry.Index < 0)
1104 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
1106 entry.Index += LocalSymbolData.size() + 1;
1108 String64(*F, entry.r_offset);
1110 struct ELF::Elf64_Rela ERE64;
1111 ERE64.setSymbolAndType(entry.Index, entry.Type);
1112 String64(*F, ERE64.r_info);
1114 if (HasRelocationAddend)
1115 String64(*F, entry.r_addend);
1117 String32(*F, entry.r_offset);
1119 struct ELF::Elf32_Rela ERE32;
1120 ERE32.setSymbolAndType(entry.Index, entry.Type);
1121 String32(*F, ERE32.r_info);
1123 if (HasRelocationAddend)
1124 String32(*F, entry.r_addend);
1129 void ELFObjectWriterImpl::CreateMetadataSections(MCAssembler &Asm,
1130 MCAsmLayout &Layout,
1131 const SectionIndexMapTy &SectionIndexMap) {
1132 MCContext &Ctx = Asm.getContext();
1135 unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
1137 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
1138 const MCSectionELF *ShstrtabSection =
1139 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
1140 SectionKind::getReadOnly());
1141 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1142 ShstrtabSD.setAlignment(1);
1143 ShstrtabIndex = Asm.size();
1145 const MCSectionELF *SymtabSection =
1146 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1147 SectionKind::getReadOnly(),
1149 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
1150 SymtabSD.setAlignment(Is64Bit ? 8 : 4);
1151 SymbolTableIndex = Asm.size();
1153 MCSectionData *SymtabShndxSD = NULL;
1155 if (NeedsSymtabShndx) {
1156 const MCSectionELF *SymtabShndxSection =
1157 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
1158 SectionKind::getReadOnly(), 4);
1159 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
1160 SymtabShndxSD->setAlignment(4);
1163 const MCSection *StrtabSection;
1164 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
1165 SectionKind::getReadOnly());
1166 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1167 StrtabSD.setAlignment(1);
1168 StringTableIndex = Asm.size();
1170 WriteRelocations(Asm, Layout);
1173 F = new MCDataFragment(&SymtabSD);
1174 MCDataFragment *ShndxF = NULL;
1175 if (NeedsSymtabShndx) {
1176 ShndxF = new MCDataFragment(SymtabShndxSD);
1177 Asm.AddSectionToTheEnd(*Writer, *SymtabShndxSD, Layout);
1179 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
1180 Asm.AddSectionToTheEnd(*Writer, SymtabSD, Layout);
1182 F = new MCDataFragment(&StrtabSD);
1183 F->getContents().append(StringTable.begin(), StringTable.end());
1184 Asm.AddSectionToTheEnd(*Writer, StrtabSD, Layout);
1186 F = new MCDataFragment(&ShstrtabSD);
1188 // Section header string table.
1190 // The first entry of a string table holds a null character so skip
1193 F->getContents() += '\x00';
1195 for (MCAssembler::const_iterator it = Asm.begin(),
1196 ie = Asm.end(); it != ie; ++it) {
1197 const MCSectionELF &Section =
1198 static_cast<const MCSectionELF&>(it->getSection());
1199 // FIXME: We could merge suffixes like in .text and .rela.text.
1201 // Remember the index into the string table so we can write it
1202 // into the sh_name field of the section header table.
1203 SectionStringTableIndex[&it->getSection()] = Index;
1205 Index += Section.getSectionName().size() + 1;
1206 F->getContents() += Section.getSectionName();
1207 F->getContents() += '\x00';
1210 Asm.AddSectionToTheEnd(*Writer, ShstrtabSD, Layout);
1213 bool ELFObjectWriterImpl::IsFixupFullyResolved(const MCAssembler &Asm,
1214 const MCValue Target,
1216 const MCFragment *DF) const {
1217 // If this is a PCrel relocation, find the section this fixup value is
1219 const MCSection *BaseSection = 0;
1221 BaseSection = &DF->getParent()->getSection();
1222 assert(BaseSection);
1225 const MCSection *SectionA = 0;
1226 const MCSymbol *SymbolA = 0;
1227 if (const MCSymbolRefExpr *A = Target.getSymA()) {
1228 SymbolA = &A->getSymbol();
1229 SectionA = &SymbolA->getSection();
1232 const MCSection *SectionB = 0;
1233 if (const MCSymbolRefExpr *B = Target.getSymB()) {
1234 SectionB = &B->getSymbol().getSection();
1238 return SectionA == SectionB;
1240 const MCSymbolData &DataA = Asm.getSymbolData(*SymbolA);
1241 if (DataA.isExternal())
1244 return !SectionB && BaseSection == SectionA;
1247 void ELFObjectWriterImpl::WriteSection(MCAssembler &Asm,
1248 const SectionIndexMapTy &SectionIndexMap,
1249 uint64_t Offset, uint64_t Size,
1251 const MCSectionELF &Section) {
1252 uint64_t sh_link = 0;
1253 uint64_t sh_info = 0;
1255 switch(Section.getType()) {
1256 case ELF::SHT_DYNAMIC:
1257 sh_link = SectionStringTableIndex[&Section];
1262 case ELF::SHT_RELA: {
1263 const MCSectionELF *SymtabSection;
1264 const MCSectionELF *InfoSection;
1265 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
1267 SectionKind::getReadOnly());
1268 sh_link = SectionIndexMap.lookup(SymtabSection);
1269 assert(sh_link && ".symtab not found");
1271 // Remove ".rel" and ".rela" prefixes.
1272 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1273 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
1275 InfoSection = Asm.getContext().getELFSection(SectionName,
1276 ELF::SHT_PROGBITS, 0,
1277 SectionKind::getReadOnly());
1278 sh_info = SectionIndexMap.lookup(InfoSection);
1282 case ELF::SHT_SYMTAB:
1283 case ELF::SHT_DYNSYM:
1284 sh_link = StringTableIndex;
1285 sh_info = LastLocalSymbolIndex;
1288 case ELF::SHT_SYMTAB_SHNDX:
1289 sh_link = SymbolTableIndex;
1292 case ELF::SHT_PROGBITS:
1293 case ELF::SHT_STRTAB:
1294 case ELF::SHT_NOBITS:
1296 case ELF::SHT_ARM_ATTRIBUTES:
1301 assert(0 && "FIXME: sh_type value not supported!");
1305 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1306 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1307 Alignment, Section.getEntrySize());
1310 void ELFObjectWriterImpl::WriteObject(MCAssembler &Asm,
1311 const MCAsmLayout &Layout) {
1312 SectionIndexMapTy SectionIndexMap;
1314 ComputeIndexMap(Asm, SectionIndexMap);
1316 // Compute symbol table information.
1317 ComputeSymbolTable(Asm, SectionIndexMap);
1319 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
1320 const_cast<MCAsmLayout&>(Layout),
1323 // Update to include the metadata sections.
1324 ComputeIndexMap(Asm, SectionIndexMap);
1326 // Add 1 for the null section.
1327 unsigned NumSections = Asm.size() + 1;
1328 uint64_t NaturalAlignment = Is64Bit ? 8 : 4;
1329 uint64_t HeaderSize = Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr);
1330 uint64_t FileOff = HeaderSize;
1332 for (MCAssembler::const_iterator it = Asm.begin(),
1333 ie = Asm.end(); it != ie; ++it) {
1334 const MCSectionData &SD = *it;
1336 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1338 // Get the size of the section in the output file (including padding).
1339 uint64_t Size = Layout.getSectionFileSize(&SD);
1344 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1346 // Write out the ELF header ...
1347 WriteHeader(FileOff - HeaderSize, NumSections);
1349 FileOff = HeaderSize;
1351 // ... then all of the sections ...
1352 DenseMap<const MCSection*, uint64_t> SectionOffsetMap;
1354 for (MCAssembler::const_iterator it = Asm.begin(),
1355 ie = Asm.end(); it != ie; ++it) {
1356 const MCSectionData &SD = *it;
1358 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1359 WriteZeros(Padding);
1362 // Remember the offset into the file for this section.
1363 SectionOffsetMap[&it->getSection()] = FileOff;
1365 FileOff += Layout.getSectionFileSize(&SD);
1367 Asm.WriteSectionData(it, Layout, Writer);
1370 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1371 WriteZeros(Padding);
1374 // ... and then the section header table.
1375 // Should we align the section header table?
1377 // Null section first.
1378 uint64_t FirstSectionSize =
1379 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1380 uint32_t FirstSectionLink =
1381 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1382 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
1384 for (MCAssembler::const_iterator it = Asm.begin(),
1385 ie = Asm.end(); it != ie; ++it) {
1386 const MCSectionData &SD = *it;
1387 const MCSectionELF &Section =
1388 static_cast<const MCSectionELF&>(SD.getSection());
1390 WriteSection(Asm, SectionIndexMap, SectionOffsetMap[&Section],
1391 Layout.getSectionSize(&SD),
1392 SD.getAlignment(), Section);
1396 ELFObjectWriter::ELFObjectWriter(raw_ostream &OS,
1398 Triple::OSType OSType,
1400 bool IsLittleEndian,
1401 bool HasRelocationAddend)
1402 : MCObjectWriter(OS, IsLittleEndian)
1404 Impl = new ELFObjectWriterImpl(this, Is64Bit, EMachine,
1405 HasRelocationAddend, OSType);
1408 ELFObjectWriter::~ELFObjectWriter() {
1409 delete (ELFObjectWriterImpl*) Impl;
1412 void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm) {
1413 ((ELFObjectWriterImpl*) Impl)->ExecutePostLayoutBinding(Asm);
1416 void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
1417 const MCAsmLayout &Layout,
1418 const MCFragment *Fragment,
1419 const MCFixup &Fixup, MCValue Target,
1420 uint64_t &FixedValue) {
1421 ((ELFObjectWriterImpl*) Impl)->RecordRelocation(Asm, Layout, Fragment, Fixup,
1422 Target, FixedValue);
1425 bool ELFObjectWriter::IsFixupFullyResolved(const MCAssembler &Asm,
1426 const MCValue Target,
1428 const MCFragment *DF) const {
1429 return ((ELFObjectWriterImpl*) Impl)->IsFixupFullyResolved(Asm, Target,
1433 void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1434 const MCAsmLayout &Layout) {
1435 ((ELFObjectWriterImpl*) Impl)->WriteObject(Asm, Layout);