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 //===----------------------------------------------------------------------===//
15 #include "llvm/ADT/OwningPtr.h"
16 #include "llvm/ADT/SmallPtrSet.h"
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/MC/MCAsmBackend.h"
21 #include "llvm/MC/MCAsmLayout.h"
22 #include "llvm/MC/MCAssembler.h"
23 #include "llvm/MC/MCContext.h"
24 #include "llvm/MC/MCELFObjectWriter.h"
25 #include "llvm/MC/MCELFSymbolFlags.h"
26 #include "llvm/MC/MCExpr.h"
27 #include "llvm/MC/MCFixupKindInfo.h"
28 #include "llvm/MC/MCObjectWriter.h"
29 #include "llvm/MC/MCSectionELF.h"
30 #include "llvm/MC/MCValue.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Support/ELF.h"
39 #define DEBUG_TYPE "reloc-info"
42 class ELFObjectWriter : public MCObjectWriter {
45 static bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind);
46 static bool RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant);
47 static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout);
48 static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
49 bool Used, bool Renamed);
50 static bool isLocal(const MCSymbolData &Data, bool isSignature,
52 static bool IsELFMetaDataSection(const MCSectionData &SD);
53 static uint64_t DataSectionSize(const MCSectionData &SD);
54 static uint64_t GetSectionFileSize(const MCAsmLayout &Layout,
55 const MCSectionData &SD);
56 static uint64_t GetSectionAddressSize(const MCAsmLayout &Layout,
57 const MCSectionData &SD);
59 void WriteDataSectionData(MCAssembler &Asm,
60 const MCAsmLayout &Layout,
61 const MCSectionELF &Section);
63 /*static bool isFixupKindX86RIPRel(unsigned Kind) {
64 return Kind == X86::reloc_riprel_4byte ||
65 Kind == X86::reloc_riprel_4byte_movq_load;
68 /// ELFSymbolData - Helper struct for containing some precomputed
69 /// information on symbols.
70 struct ELFSymbolData {
71 MCSymbolData *SymbolData;
73 uint32_t SectionIndex;
75 // Support lexicographic sorting.
76 bool operator<(const ELFSymbolData &RHS) const {
77 if (MCELF::GetType(*SymbolData) == ELF::STT_FILE)
79 if (MCELF::GetType(*RHS.SymbolData) == ELF::STT_FILE)
81 return SymbolData->getSymbol().getName() <
82 RHS.SymbolData->getSymbol().getName();
86 /// The target specific ELF writer instance.
87 llvm::OwningPtr<MCELFObjectTargetWriter> TargetObjectWriter;
89 SmallPtrSet<const MCSymbol *, 16> UsedInReloc;
90 SmallPtrSet<const MCSymbol *, 16> WeakrefUsedInReloc;
91 DenseMap<const MCSymbol *, const MCSymbol *> Renames;
93 llvm::DenseMap<const MCSectionData*,
94 std::vector<ELFRelocationEntry> > Relocations;
95 DenseMap<const MCSection*, uint64_t> SectionStringTableIndex;
98 /// @name Symbol Table Data
101 SmallString<256> StringTable;
102 std::vector<ELFSymbolData> LocalSymbolData;
103 std::vector<ELFSymbolData> ExternalSymbolData;
104 std::vector<ELFSymbolData> UndefinedSymbolData;
110 bool NeedsSymtabShndx;
112 // This holds the symbol table index of the last local symbol.
113 unsigned LastLocalSymbolIndex;
114 // This holds the .strtab section index.
115 unsigned StringTableIndex;
116 // This holds the .symtab section index.
117 unsigned SymbolTableIndex;
119 unsigned ShstrtabIndex;
122 const MCSymbol *SymbolToReloc(const MCAssembler &Asm,
123 const MCValue &Target,
125 const MCFixup &Fixup,
128 // TargetObjectWriter wrappers.
129 const MCSymbol *ExplicitRelSym(const MCAssembler &Asm,
130 const MCValue &Target,
132 const MCFixup &Fixup,
133 bool IsPCRel) const {
134 return TargetObjectWriter->ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
137 bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
138 bool hasRelocationAddend() const {
139 return TargetObjectWriter->hasRelocationAddend();
141 unsigned getEFlags() const {
142 return TargetObjectWriter->getEFlags();
144 unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
145 bool IsPCRel, bool IsRelocWithSymbol,
146 int64_t Addend) const {
147 return TargetObjectWriter->GetRelocType(Target, Fixup, IsPCRel,
148 IsRelocWithSymbol, Addend);
153 ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
154 raw_ostream &_OS, bool IsLittleEndian)
155 : MCObjectWriter(_OS, IsLittleEndian),
156 TargetObjectWriter(MOTW),
157 NeedsGOT(false), NeedsSymtabShndx(false){
160 virtual ~ELFObjectWriter();
162 void WriteWord(uint64_t W) {
169 void StringLE16(char *buf, uint16_t Value) {
170 buf[0] = char(Value >> 0);
171 buf[1] = char(Value >> 8);
174 void StringLE32(char *buf, uint32_t Value) {
175 StringLE16(buf, uint16_t(Value >> 0));
176 StringLE16(buf + 2, uint16_t(Value >> 16));
179 void StringLE64(char *buf, uint64_t Value) {
180 StringLE32(buf, uint32_t(Value >> 0));
181 StringLE32(buf + 4, uint32_t(Value >> 32));
184 void StringBE16(char *buf ,uint16_t Value) {
185 buf[0] = char(Value >> 8);
186 buf[1] = char(Value >> 0);
189 void StringBE32(char *buf, uint32_t Value) {
190 StringBE16(buf, uint16_t(Value >> 16));
191 StringBE16(buf + 2, uint16_t(Value >> 0));
194 void StringBE64(char *buf, uint64_t Value) {
195 StringBE32(buf, uint32_t(Value >> 32));
196 StringBE32(buf + 4, uint32_t(Value >> 0));
199 void String8(MCDataFragment &F, uint8_t Value) {
202 F.getContents() += StringRef(buf, 1);
205 void String16(MCDataFragment &F, uint16_t Value) {
207 if (isLittleEndian())
208 StringLE16(buf, Value);
210 StringBE16(buf, Value);
211 F.getContents() += StringRef(buf, 2);
214 void String32(MCDataFragment &F, uint32_t Value) {
216 if (isLittleEndian())
217 StringLE32(buf, Value);
219 StringBE32(buf, Value);
220 F.getContents() += StringRef(buf, 4);
223 void String64(MCDataFragment &F, uint64_t Value) {
225 if (isLittleEndian())
226 StringLE64(buf, Value);
228 StringBE64(buf, Value);
229 F.getContents() += StringRef(buf, 8);
232 void WriteHeader(uint64_t SectionDataSize,
233 unsigned NumberOfSections);
235 void WriteSymbolEntry(MCDataFragment *SymtabF,
236 MCDataFragment *ShndxF,
237 uint64_t name, uint8_t info,
238 uint64_t value, uint64_t size,
239 uint8_t other, uint32_t shndx,
242 void WriteSymbol(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
244 const MCAsmLayout &Layout);
246 typedef DenseMap<const MCSectionELF*, uint32_t> SectionIndexMapTy;
247 void WriteSymbolTable(MCDataFragment *SymtabF,
248 MCDataFragment *ShndxF,
249 const MCAssembler &Asm,
250 const MCAsmLayout &Layout,
251 const SectionIndexMapTy &SectionIndexMap);
253 virtual void RecordRelocation(const MCAssembler &Asm,
254 const MCAsmLayout &Layout,
255 const MCFragment *Fragment,
256 const MCFixup &Fixup,
257 MCValue Target, uint64_t &FixedValue);
259 uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm,
262 // Map from a group section to the signature symbol
263 typedef DenseMap<const MCSectionELF*, const MCSymbol*> GroupMapTy;
264 // Map from a signature symbol to the group section
265 typedef DenseMap<const MCSymbol*, const MCSectionELF*> RevGroupMapTy;
266 // Map from a section to the section with the relocations
267 typedef DenseMap<const MCSectionELF*, const MCSectionELF*> RelMapTy;
268 // Map from a section to its offset
269 typedef DenseMap<const MCSectionELF*, uint64_t> SectionOffsetMapTy;
271 /// ComputeSymbolTable - Compute the symbol table data
273 /// \param Asm - The assembler.
274 /// \param SectionIndexMap - Maps a section to its index.
275 /// \param RevGroupMap - Maps a signature symbol to the group section.
276 /// \param NumRegularSections - Number of non-relocation sections.
277 void ComputeSymbolTable(MCAssembler &Asm,
278 const SectionIndexMapTy &SectionIndexMap,
279 RevGroupMapTy RevGroupMap,
280 unsigned NumRegularSections);
282 void ComputeIndexMap(MCAssembler &Asm,
283 SectionIndexMapTy &SectionIndexMap,
284 const RelMapTy &RelMap);
286 void CreateRelocationSections(MCAssembler &Asm, MCAsmLayout &Layout,
289 void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout,
290 const RelMapTy &RelMap);
292 void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout,
293 SectionIndexMapTy &SectionIndexMap,
294 const RelMapTy &RelMap);
296 // Create the sections that show up in the symbol table. Currently
297 // those are the .note.GNU-stack section and the group sections.
298 void CreateIndexedSections(MCAssembler &Asm, MCAsmLayout &Layout,
299 GroupMapTy &GroupMap,
300 RevGroupMapTy &RevGroupMap,
301 SectionIndexMapTy &SectionIndexMap,
302 const RelMapTy &RelMap);
304 virtual void ExecutePostLayoutBinding(MCAssembler &Asm,
305 const MCAsmLayout &Layout);
307 void WriteSectionHeader(MCAssembler &Asm, const GroupMapTy &GroupMap,
308 const MCAsmLayout &Layout,
309 const SectionIndexMapTy &SectionIndexMap,
310 const SectionOffsetMapTy &SectionOffsetMap);
312 void ComputeSectionOrder(MCAssembler &Asm,
313 std::vector<const MCSectionELF*> &Sections);
315 void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
316 uint64_t Address, uint64_t Offset,
317 uint64_t Size, uint32_t Link, uint32_t Info,
318 uint64_t Alignment, uint64_t EntrySize);
320 void WriteRelocationsFragment(const MCAssembler &Asm,
322 const MCSectionData *SD);
325 IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
326 const MCSymbolData &DataA,
327 const MCFragment &FB,
331 virtual void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
332 void WriteSection(MCAssembler &Asm,
333 const SectionIndexMapTy &SectionIndexMap,
334 uint32_t GroupSymbolIndex,
335 uint64_t Offset, uint64_t Size, uint64_t Alignment,
336 const MCSectionELF &Section);
340 bool ELFObjectWriter::isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) {
341 const MCFixupKindInfo &FKI =
342 Asm.getBackend().getFixupKindInfo((MCFixupKind) Kind);
344 return FKI.Flags & MCFixupKindInfo::FKF_IsPCRel;
347 bool ELFObjectWriter::RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant) {
351 case MCSymbolRefExpr::VK_GOT:
352 case MCSymbolRefExpr::VK_PLT:
353 case MCSymbolRefExpr::VK_GOTPCREL:
354 case MCSymbolRefExpr::VK_GOTOFF:
355 case MCSymbolRefExpr::VK_TPOFF:
356 case MCSymbolRefExpr::VK_TLSGD:
357 case MCSymbolRefExpr::VK_GOTTPOFF:
358 case MCSymbolRefExpr::VK_INDNTPOFF:
359 case MCSymbolRefExpr::VK_NTPOFF:
360 case MCSymbolRefExpr::VK_GOTNTPOFF:
361 case MCSymbolRefExpr::VK_TLSLDM:
362 case MCSymbolRefExpr::VK_DTPOFF:
363 case MCSymbolRefExpr::VK_TLSLD:
368 ELFObjectWriter::~ELFObjectWriter()
371 // Emit the ELF header.
372 void ELFObjectWriter::WriteHeader(uint64_t SectionDataSize,
373 unsigned NumberOfSections) {
379 // emitWord method behaves differently for ELF32 and ELF64, writing
380 // 4 bytes in the former and 8 in the latter.
382 Write8(0x7f); // e_ident[EI_MAG0]
383 Write8('E'); // e_ident[EI_MAG1]
384 Write8('L'); // e_ident[EI_MAG2]
385 Write8('F'); // e_ident[EI_MAG3]
387 Write8(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
390 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
392 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
394 Write8(TargetObjectWriter->getOSABI());
395 Write8(0); // e_ident[EI_ABIVERSION]
397 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
399 Write16(ELF::ET_REL); // e_type
401 Write16(TargetObjectWriter->getEMachine()); // e_machine = target
403 Write32(ELF::EV_CURRENT); // e_version
404 WriteWord(0); // e_entry, no entry point in .o file
405 WriteWord(0); // e_phoff, no program header for .o
406 WriteWord(SectionDataSize + (is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
407 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
409 // e_flags = whatever the target wants
410 Write32(getEFlags());
412 // e_ehsize = ELF header size
413 Write16(is64Bit() ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
415 Write16(0); // e_phentsize = prog header entry size
416 Write16(0); // e_phnum = # prog header entries = 0
418 // e_shentsize = Section header entry size
419 Write16(is64Bit() ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
421 // e_shnum = # of section header ents
422 if (NumberOfSections >= ELF::SHN_LORESERVE)
423 Write16(ELF::SHN_UNDEF);
425 Write16(NumberOfSections);
427 // e_shstrndx = Section # of '.shstrtab'
428 if (ShstrtabIndex >= ELF::SHN_LORESERVE)
429 Write16(ELF::SHN_XINDEX);
431 Write16(ShstrtabIndex);
434 void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
435 MCDataFragment *ShndxF,
437 uint8_t info, uint64_t value,
438 uint64_t size, uint8_t other,
442 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
443 String32(*ShndxF, shndx);
445 String32(*ShndxF, 0);
448 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
449 uint16_t(ELF::SHN_XINDEX) : shndx;
452 String32(*SymtabF, name); // st_name
453 String8(*SymtabF, info); // st_info
454 String8(*SymtabF, other); // st_other
455 String16(*SymtabF, Index); // st_shndx
456 String64(*SymtabF, value); // st_value
457 String64(*SymtabF, size); // st_size
459 String32(*SymtabF, name); // st_name
460 String32(*SymtabF, value); // st_value
461 String32(*SymtabF, size); // st_size
462 String8(*SymtabF, info); // st_info
463 String8(*SymtabF, other); // st_other
464 String16(*SymtabF, Index); // st_shndx
468 uint64_t ELFObjectWriter::SymbolValue(MCSymbolData &Data,
469 const MCAsmLayout &Layout) {
470 if (Data.isCommon() && Data.isExternal())
471 return Data.getCommonAlignment();
473 const MCSymbol &Symbol = Data.getSymbol();
475 if (Symbol.isAbsolute() && Symbol.isVariable()) {
476 if (const MCExpr *Value = Symbol.getVariableValue()) {
478 if (Value->EvaluateAsAbsolute(IntValue, Layout))
479 return (uint64_t)IntValue;
483 if (!Symbol.isInSection())
487 if (Data.getFragment()) {
488 if (Data.getFlags() & ELF_Other_ThumbFunc)
489 return Layout.getSymbolOffset(&Data)+1;
491 return Layout.getSymbolOffset(&Data);
497 void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
498 const MCAsmLayout &Layout) {
499 // The presence of symbol versions causes undefined symbols and
500 // versions declared with @@@ to be renamed.
502 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
503 ie = Asm.symbol_end(); it != ie; ++it) {
504 const MCSymbol &Alias = it->getSymbol();
505 const MCSymbol &Symbol = Alias.AliasedSymbol();
506 MCSymbolData &SD = Asm.getSymbolData(Symbol);
509 if (&Symbol == &Alias)
512 StringRef AliasName = Alias.getName();
513 size_t Pos = AliasName.find('@');
514 if (Pos == StringRef::npos)
517 // Aliases defined with .symvar copy the binding from the symbol they alias.
518 // This is the first place we are able to copy this information.
519 it->setExternal(SD.isExternal());
520 MCELF::SetBinding(*it, MCELF::GetBinding(SD));
522 StringRef Rest = AliasName.substr(Pos);
523 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
526 // FIXME: produce a better error message.
527 if (Symbol.isUndefined() && Rest.startswith("@@") &&
528 !Rest.startswith("@@@"))
529 report_fatal_error("A @@ version cannot be undefined");
531 Renames.insert(std::make_pair(&Symbol, &Alias));
535 void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
536 MCDataFragment *ShndxF,
538 const MCAsmLayout &Layout) {
539 MCSymbolData &OrigData = *MSD.SymbolData;
541 Layout.getAssembler().getSymbolData(OrigData.getSymbol().AliasedSymbol());
543 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
544 Data.getSymbol().isVariable();
546 uint8_t Binding = MCELF::GetBinding(OrigData);
547 uint8_t Visibility = MCELF::GetVisibility(OrigData);
548 uint8_t Type = MCELF::GetType(Data);
550 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
551 uint8_t Other = Visibility;
553 uint64_t Value = SymbolValue(Data, Layout);
556 assert(!(Data.isCommon() && !Data.isExternal()));
558 const MCExpr *ESize = Data.getSize();
561 if (!ESize->EvaluateAsAbsolute(Res, Layout))
562 report_fatal_error("Size expression must be absolute.");
566 // Write out the symbol table entry
567 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
568 Size, Other, MSD.SectionIndex, IsReserved);
571 void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
572 MCDataFragment *ShndxF,
573 const MCAssembler &Asm,
574 const MCAsmLayout &Layout,
575 const SectionIndexMapTy &SectionIndexMap) {
576 // The string table must be emitted first because we need the index
577 // into the string table for all the symbol names.
578 assert(StringTable.size() && "Missing string table");
580 // FIXME: Make sure the start of the symbol table is aligned.
582 // The first entry is the undefined symbol entry.
583 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
585 // Write the symbol table entries.
586 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
587 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
588 ELFSymbolData &MSD = LocalSymbolData[i];
589 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
592 // Write out a symbol table entry for each regular section.
593 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
595 const MCSectionELF &Section =
596 static_cast<const MCSectionELF&>(i->getSection());
597 if (Section.getType() == ELF::SHT_RELA ||
598 Section.getType() == ELF::SHT_REL ||
599 Section.getType() == ELF::SHT_STRTAB ||
600 Section.getType() == ELF::SHT_SYMTAB ||
601 Section.getType() == ELF::SHT_SYMTAB_SHNDX)
603 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
604 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section),
606 LastLocalSymbolIndex++;
609 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
610 ELFSymbolData &MSD = ExternalSymbolData[i];
611 MCSymbolData &Data = *MSD.SymbolData;
612 assert(((Data.getFlags() & ELF_STB_Global) ||
613 (Data.getFlags() & ELF_STB_Weak)) &&
614 "External symbol requires STB_GLOBAL or STB_WEAK flag");
615 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
616 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
617 LastLocalSymbolIndex++;
620 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
621 ELFSymbolData &MSD = UndefinedSymbolData[i];
622 MCSymbolData &Data = *MSD.SymbolData;
623 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
624 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
625 LastLocalSymbolIndex++;
629 const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm,
630 const MCValue &Target,
632 const MCFixup &Fixup,
633 bool IsPCRel) const {
634 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
635 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
636 const MCSymbol *Renamed = Renames.lookup(&Symbol);
637 const MCSymbolData &SD = Asm.getSymbolData(Symbol);
639 if (ASymbol.isUndefined()) {
645 if (SD.isExternal()) {
651 const MCSectionELF &Section =
652 static_cast<const MCSectionELF&>(ASymbol.getSection());
653 const SectionKind secKind = Section.getKind();
656 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
658 if (secKind.isThreadLocal()) {
664 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
665 const MCSectionELF &Sec2 =
666 static_cast<const MCSectionELF&>(F.getParent()->getSection());
668 if (&Sec2 != &Section &&
669 (Kind == MCSymbolRefExpr::VK_PLT ||
670 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
671 Kind == MCSymbolRefExpr::VK_GOTOFF)) {
677 if (Section.getFlags() & ELF::SHF_MERGE) {
678 if (Target.getConstant() == 0)
679 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
685 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
690 void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
691 const MCAsmLayout &Layout,
692 const MCFragment *Fragment,
693 const MCFixup &Fixup,
695 uint64_t &FixedValue) {
698 int64_t Value = Target.getConstant();
699 const MCSymbol *RelocSymbol = NULL;
701 bool IsPCRel = isFixupKindPCRel(Asm, Fixup.getKind());
702 if (!Target.isAbsolute()) {
703 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
704 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
705 RelocSymbol = SymbolToReloc(Asm, Target, *Fragment, Fixup, IsPCRel);
707 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
708 const MCSymbol &SymbolB = RefB->getSymbol();
709 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
712 // Offset of the symbol in the section
713 int64_t a = Layout.getSymbolOffset(&SDB);
715 // Offset of the relocation in the section
716 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
721 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
722 MCFragment *F = SD.getFragment();
724 Index = F->getParent()->getOrdinal() + 1;
726 // Offset of the symbol in the section
727 Value += Layout.getSymbolOffset(&SD);
729 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
730 WeakrefUsedInReloc.insert(RelocSymbol);
732 UsedInReloc.insert(RelocSymbol);
736 // Compensate for the addend on i386.
742 unsigned Type = GetRelocType(Target, Fixup, IsPCRel,
743 (RelocSymbol != 0), Addend);
744 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
745 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
746 if (RelocNeedsGOT(Modifier))
749 uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
752 // FIXME: no tests cover this. Is adjustFixupOffset dead code?
753 TargetObjectWriter->adjustFixupOffset(Fixup, RelocOffset);
755 if (!hasRelocationAddend())
759 assert(isInt<64>(Addend));
761 assert(isInt<32>(Addend));
763 ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend, Fixup);
764 Relocations[Fragment->getParent()].push_back(ERE);
769 ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
771 MCSymbolData &SD = Asm.getSymbolData(*S);
772 return SD.getIndex();
775 bool ELFObjectWriter::isInSymtab(const MCAssembler &Asm,
776 const MCSymbolData &Data,
777 bool Used, bool Renamed) {
778 if (Data.getFlags() & ELF_Other_Weakref)
787 const MCSymbol &Symbol = Data.getSymbol();
789 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
792 const MCSymbol &A = Symbol.AliasedSymbol();
793 if (Symbol.isVariable() && !A.isVariable() && A.isUndefined())
796 bool IsGlobal = MCELF::GetBinding(Data) == ELF::STB_GLOBAL;
797 if (!Symbol.isVariable() && Symbol.isUndefined() && !IsGlobal)
800 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
803 if (Symbol.isTemporary())
809 bool ELFObjectWriter::isLocal(const MCSymbolData &Data, bool isSignature,
810 bool isUsedInReloc) {
811 if (Data.isExternal())
814 const MCSymbol &Symbol = Data.getSymbol();
815 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
817 if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
818 if (isSignature && !isUsedInReloc)
827 void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
828 SectionIndexMapTy &SectionIndexMap,
829 const RelMapTy &RelMap) {
831 for (MCAssembler::iterator it = Asm.begin(),
832 ie = Asm.end(); it != ie; ++it) {
833 const MCSectionELF &Section =
834 static_cast<const MCSectionELF &>(it->getSection());
835 if (Section.getType() != ELF::SHT_GROUP)
837 SectionIndexMap[&Section] = Index++;
840 for (MCAssembler::iterator it = Asm.begin(),
841 ie = Asm.end(); it != ie; ++it) {
842 const MCSectionELF &Section =
843 static_cast<const MCSectionELF &>(it->getSection());
844 if (Section.getType() == ELF::SHT_GROUP ||
845 Section.getType() == ELF::SHT_REL ||
846 Section.getType() == ELF::SHT_RELA)
848 SectionIndexMap[&Section] = Index++;
849 const MCSectionELF *RelSection = RelMap.lookup(&Section);
851 SectionIndexMap[RelSection] = Index++;
855 void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
856 const SectionIndexMapTy &SectionIndexMap,
857 RevGroupMapTy RevGroupMap,
858 unsigned NumRegularSections) {
859 // FIXME: Is this the correct place to do this?
860 // FIXME: Why is an undefined reference to _GLOBAL_OFFSET_TABLE_ needed?
862 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
863 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
864 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
865 Data.setExternal(true);
866 MCELF::SetBinding(Data, ELF::STB_GLOBAL);
869 // Index 0 is always the empty string.
870 StringMap<uint64_t> StringIndexMap;
871 StringTable += '\x00';
873 // FIXME: We could optimize suffixes in strtab in the same way we
874 // optimize them in shstrtab.
876 // Add the data for the symbols.
877 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
878 ie = Asm.symbol_end(); it != ie; ++it) {
879 const MCSymbol &Symbol = it->getSymbol();
881 bool Used = UsedInReloc.count(&Symbol);
882 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
883 bool isSignature = RevGroupMap.count(&Symbol);
885 if (!isInSymtab(Asm, *it,
886 Used || WeakrefUsed || isSignature,
887 Renames.count(&Symbol)))
892 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
894 // Undefined symbols are global, but this is the first place we
895 // are able to set it.
896 bool Local = isLocal(*it, isSignature, Used);
897 if (!Local && MCELF::GetBinding(*it) == ELF::STB_LOCAL) {
898 MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
899 MCELF::SetBinding(*it, ELF::STB_GLOBAL);
900 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
903 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
904 MCELF::SetBinding(*it, ELF::STB_WEAK);
906 if (it->isCommon()) {
908 MSD.SectionIndex = ELF::SHN_COMMON;
909 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
910 MSD.SectionIndex = ELF::SHN_ABS;
911 } else if (RefSymbol.isUndefined()) {
912 if (isSignature && !Used)
913 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
915 MSD.SectionIndex = ELF::SHN_UNDEF;
917 const MCSectionELF &Section =
918 static_cast<const MCSectionELF&>(RefSymbol.getSection());
919 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
920 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
921 NeedsSymtabShndx = true;
922 assert(MSD.SectionIndex && "Invalid section index!");
925 // The @@@ in symbol version is replaced with @ in undefined symbols and
926 // @@ in defined ones.
927 StringRef Name = Symbol.getName();
930 size_t Pos = Name.find("@@@");
931 if (Pos != StringRef::npos) {
932 Buf += Name.substr(0, Pos);
933 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
934 Buf += Name.substr(Pos + Skip);
938 uint64_t &Entry = StringIndexMap[Name];
940 Entry = StringTable.size();
942 StringTable += '\x00';
944 MSD.StringIndex = Entry;
945 if (MSD.SectionIndex == ELF::SHN_UNDEF)
946 UndefinedSymbolData.push_back(MSD);
948 LocalSymbolData.push_back(MSD);
950 ExternalSymbolData.push_back(MSD);
953 // Symbols are required to be in lexicographic order.
954 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
955 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
956 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
958 // Set the symbol indices. Local symbols must come before all other
959 // symbols with non-local bindings.
961 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
962 LocalSymbolData[i].SymbolData->setIndex(Index++);
964 Index += NumRegularSections;
966 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
967 ExternalSymbolData[i].SymbolData->setIndex(Index++);
968 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
969 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
971 if (NumRegularSections > ELF::SHN_LORESERVE)
972 NeedsSymtabShndx = true;
975 void ELFObjectWriter::CreateRelocationSections(MCAssembler &Asm,
978 for (MCAssembler::const_iterator it = Asm.begin(),
979 ie = Asm.end(); it != ie; ++it) {
980 const MCSectionData &SD = *it;
981 if (Relocations[&SD].empty())
984 MCContext &Ctx = Asm.getContext();
985 const MCSectionELF &Section =
986 static_cast<const MCSectionELF&>(SD.getSection());
988 const StringRef SectionName = Section.getSectionName();
989 std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
990 RelaSectionName += SectionName;
993 if (hasRelocationAddend())
994 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
996 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
998 const MCSectionELF *RelaSection =
999 Ctx.getELFSection(RelaSectionName, hasRelocationAddend() ?
1000 ELF::SHT_RELA : ELF::SHT_REL, 0,
1001 SectionKind::getReadOnly(),
1003 RelMap[&Section] = RelaSection;
1004 Asm.getOrCreateSectionData(*RelaSection);
1008 void ELFObjectWriter::WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout,
1009 const RelMapTy &RelMap) {
1010 for (MCAssembler::const_iterator it = Asm.begin(),
1011 ie = Asm.end(); it != ie; ++it) {
1012 const MCSectionData &SD = *it;
1013 const MCSectionELF &Section =
1014 static_cast<const MCSectionELF&>(SD.getSection());
1016 const MCSectionELF *RelaSection = RelMap.lookup(&Section);
1019 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
1020 RelaSD.setAlignment(is64Bit() ? 8 : 4);
1022 MCDataFragment *F = new MCDataFragment(&RelaSD);
1023 WriteRelocationsFragment(Asm, F, &*it);
1027 void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
1028 uint64_t Flags, uint64_t Address,
1029 uint64_t Offset, uint64_t Size,
1030 uint32_t Link, uint32_t Info,
1032 uint64_t EntrySize) {
1033 Write32(Name); // sh_name: index into string table
1034 Write32(Type); // sh_type
1035 WriteWord(Flags); // sh_flags
1036 WriteWord(Address); // sh_addr
1037 WriteWord(Offset); // sh_offset
1038 WriteWord(Size); // sh_size
1039 Write32(Link); // sh_link
1040 Write32(Info); // sh_info
1041 WriteWord(Alignment); // sh_addralign
1042 WriteWord(EntrySize); // sh_entsize
1045 void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
1047 const MCSectionData *SD) {
1048 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
1050 // Sort the relocation entries. Most targets just sort by r_offset, but some
1051 // (e.g., MIPS) have additional constraints.
1052 TargetObjectWriter->sortRelocs(Asm, Relocs);
1054 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1055 ELFRelocationEntry entry = Relocs[e - i - 1];
1059 else if (entry.Index < 0)
1060 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
1062 entry.Index += LocalSymbolData.size();
1064 String64(*F, entry.r_offset);
1065 if (TargetObjectWriter->isN64()) {
1066 String32(*F, entry.Index);
1068 String8(*F, TargetObjectWriter->getRSsym(entry.Type));
1069 String8(*F, TargetObjectWriter->getRType3(entry.Type));
1070 String8(*F, TargetObjectWriter->getRType2(entry.Type));
1071 String8(*F, TargetObjectWriter->getRType(entry.Type));
1074 struct ELF::Elf64_Rela ERE64;
1075 ERE64.setSymbolAndType(entry.Index, entry.Type);
1076 String64(*F, ERE64.r_info);
1078 if (hasRelocationAddend())
1079 String64(*F, entry.r_addend);
1081 String32(*F, entry.r_offset);
1083 struct ELF::Elf32_Rela ERE32;
1084 ERE32.setSymbolAndType(entry.Index, entry.Type);
1085 String32(*F, ERE32.r_info);
1087 if (hasRelocationAddend())
1088 String32(*F, entry.r_addend);
1093 static int compareBySuffix(const void *a, const void *b) {
1094 const MCSectionELF *secA = *static_cast<const MCSectionELF* const *>(a);
1095 const MCSectionELF *secB = *static_cast<const MCSectionELF* const *>(b);
1096 const StringRef &NameA = secA->getSectionName();
1097 const StringRef &NameB = secB->getSectionName();
1098 const unsigned sizeA = NameA.size();
1099 const unsigned sizeB = NameB.size();
1100 const unsigned len = std::min(sizeA, sizeB);
1101 for (unsigned int i = 0; i < len; ++i) {
1102 char ca = NameA[sizeA - i - 1];
1103 char cb = NameB[sizeB - i - 1];
1108 return sizeB - sizeA;
1111 void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
1112 MCAsmLayout &Layout,
1113 SectionIndexMapTy &SectionIndexMap,
1114 const RelMapTy &RelMap) {
1115 MCContext &Ctx = Asm.getContext();
1118 unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
1120 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
1121 const MCSectionELF *ShstrtabSection =
1122 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
1123 SectionKind::getReadOnly());
1124 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1125 ShstrtabSD.setAlignment(1);
1127 const MCSectionELF *SymtabSection =
1128 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1129 SectionKind::getReadOnly(),
1131 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
1132 SymtabSD.setAlignment(is64Bit() ? 8 : 4);
1134 MCSectionData *SymtabShndxSD = NULL;
1136 if (NeedsSymtabShndx) {
1137 const MCSectionELF *SymtabShndxSection =
1138 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
1139 SectionKind::getReadOnly(), 4, "");
1140 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
1141 SymtabShndxSD->setAlignment(4);
1144 const MCSectionELF *StrtabSection;
1145 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
1146 SectionKind::getReadOnly());
1147 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1148 StrtabSD.setAlignment(1);
1150 ComputeIndexMap(Asm, SectionIndexMap, RelMap);
1152 ShstrtabIndex = SectionIndexMap.lookup(ShstrtabSection);
1153 SymbolTableIndex = SectionIndexMap.lookup(SymtabSection);
1154 StringTableIndex = SectionIndexMap.lookup(StrtabSection);
1157 F = new MCDataFragment(&SymtabSD);
1158 MCDataFragment *ShndxF = NULL;
1159 if (NeedsSymtabShndx) {
1160 ShndxF = new MCDataFragment(SymtabShndxSD);
1162 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
1164 F = new MCDataFragment(&StrtabSD);
1165 F->getContents().append(StringTable.begin(), StringTable.end());
1167 F = new MCDataFragment(&ShstrtabSD);
1169 std::vector<const MCSectionELF*> Sections;
1170 for (MCAssembler::const_iterator it = Asm.begin(),
1171 ie = Asm.end(); it != ie; ++it) {
1172 const MCSectionELF &Section =
1173 static_cast<const MCSectionELF&>(it->getSection());
1174 Sections.push_back(&Section);
1176 array_pod_sort(Sections.begin(), Sections.end(), compareBySuffix);
1178 // Section header string table.
1180 // The first entry of a string table holds a null character so skip
1183 F->getContents() += '\x00';
1185 for (unsigned int I = 0, E = Sections.size(); I != E; ++I) {
1186 const MCSectionELF &Section = *Sections[I];
1188 StringRef Name = Section.getSectionName();
1190 StringRef PreviousName = Sections[I - 1]->getSectionName();
1191 if (PreviousName.endswith(Name)) {
1192 SectionStringTableIndex[&Section] = Index - Name.size() - 1;
1196 // Remember the index into the string table so we can write it
1197 // into the sh_name field of the section header table.
1198 SectionStringTableIndex[&Section] = Index;
1200 Index += Name.size() + 1;
1201 F->getContents() += Name;
1202 F->getContents() += '\x00';
1206 void ELFObjectWriter::CreateIndexedSections(MCAssembler &Asm,
1207 MCAsmLayout &Layout,
1208 GroupMapTy &GroupMap,
1209 RevGroupMapTy &RevGroupMap,
1210 SectionIndexMapTy &SectionIndexMap,
1211 const RelMapTy &RelMap) {
1212 // Create the .note.GNU-stack section if needed.
1213 MCContext &Ctx = Asm.getContext();
1214 if (Asm.getNoExecStack()) {
1215 const MCSectionELF *GnuStackSection =
1216 Ctx.getELFSection(".note.GNU-stack", ELF::SHT_PROGBITS, 0,
1217 SectionKind::getReadOnly());
1218 Asm.getOrCreateSectionData(*GnuStackSection);
1222 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1224 const MCSectionELF &Section =
1225 static_cast<const MCSectionELF&>(it->getSection());
1226 if (!(Section.getFlags() & ELF::SHF_GROUP))
1229 const MCSymbol *SignatureSymbol = Section.getGroup();
1230 Asm.getOrCreateSymbolData(*SignatureSymbol);
1231 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
1233 Group = Ctx.CreateELFGroupSection();
1234 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1235 Data.setAlignment(4);
1236 MCDataFragment *F = new MCDataFragment(&Data);
1237 String32(*F, ELF::GRP_COMDAT);
1239 GroupMap[Group] = SignatureSymbol;
1242 ComputeIndexMap(Asm, SectionIndexMap, RelMap);
1244 // Add sections to the groups
1245 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1247 const MCSectionELF &Section =
1248 static_cast<const MCSectionELF&>(it->getSection());
1249 if (!(Section.getFlags() & ELF::SHF_GROUP))
1251 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
1252 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1253 // FIXME: we could use the previous fragment
1254 MCDataFragment *F = new MCDataFragment(&Data);
1255 unsigned Index = SectionIndexMap.lookup(&Section);
1256 String32(*F, Index);
1260 void ELFObjectWriter::WriteSection(MCAssembler &Asm,
1261 const SectionIndexMapTy &SectionIndexMap,
1262 uint32_t GroupSymbolIndex,
1263 uint64_t Offset, uint64_t Size,
1265 const MCSectionELF &Section) {
1266 uint64_t sh_link = 0;
1267 uint64_t sh_info = 0;
1269 switch(Section.getType()) {
1270 case ELF::SHT_DYNAMIC:
1271 sh_link = SectionStringTableIndex[&Section];
1276 case ELF::SHT_RELA: {
1277 const MCSectionELF *SymtabSection;
1278 const MCSectionELF *InfoSection;
1279 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
1281 SectionKind::getReadOnly());
1282 sh_link = SectionIndexMap.lookup(SymtabSection);
1283 assert(sh_link && ".symtab not found");
1285 // Remove ".rel" and ".rela" prefixes.
1286 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1287 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
1289 InfoSection = Asm.getContext().getELFSection(SectionName,
1290 ELF::SHT_PROGBITS, 0,
1291 SectionKind::getReadOnly());
1292 sh_info = SectionIndexMap.lookup(InfoSection);
1296 case ELF::SHT_SYMTAB:
1297 case ELF::SHT_DYNSYM:
1298 sh_link = StringTableIndex;
1299 sh_info = LastLocalSymbolIndex;
1302 case ELF::SHT_SYMTAB_SHNDX:
1303 sh_link = SymbolTableIndex;
1306 case ELF::SHT_PROGBITS:
1307 case ELF::SHT_STRTAB:
1308 case ELF::SHT_NOBITS:
1311 case ELF::SHT_ARM_ATTRIBUTES:
1312 case ELF::SHT_INIT_ARRAY:
1313 case ELF::SHT_FINI_ARRAY:
1314 case ELF::SHT_PREINIT_ARRAY:
1315 case ELF::SHT_X86_64_UNWIND:
1319 case ELF::SHT_GROUP:
1320 sh_link = SymbolTableIndex;
1321 sh_info = GroupSymbolIndex;
1325 assert(0 && "FIXME: sh_type value not supported!");
1329 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1330 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1331 Alignment, Section.getEntrySize());
1334 bool ELFObjectWriter::IsELFMetaDataSection(const MCSectionData &SD) {
1335 return SD.getOrdinal() == ~UINT32_C(0) &&
1336 !SD.getSection().isVirtualSection();
1339 uint64_t ELFObjectWriter::DataSectionSize(const MCSectionData &SD) {
1341 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1343 const MCFragment &F = *i;
1344 assert(F.getKind() == MCFragment::FT_Data);
1345 Ret += cast<MCDataFragment>(F).getContents().size();
1350 uint64_t ELFObjectWriter::GetSectionFileSize(const MCAsmLayout &Layout,
1351 const MCSectionData &SD) {
1352 if (IsELFMetaDataSection(SD))
1353 return DataSectionSize(SD);
1354 return Layout.getSectionFileSize(&SD);
1357 uint64_t ELFObjectWriter::GetSectionAddressSize(const MCAsmLayout &Layout,
1358 const MCSectionData &SD) {
1359 if (IsELFMetaDataSection(SD))
1360 return DataSectionSize(SD);
1361 return Layout.getSectionAddressSize(&SD);
1364 void ELFObjectWriter::WriteDataSectionData(MCAssembler &Asm,
1365 const MCAsmLayout &Layout,
1366 const MCSectionELF &Section) {
1367 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1369 uint64_t Padding = OffsetToAlignment(OS.tell(), SD.getAlignment());
1370 WriteZeros(Padding);
1372 if (IsELFMetaDataSection(SD)) {
1373 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1375 const MCFragment &F = *i;
1376 assert(F.getKind() == MCFragment::FT_Data);
1377 WriteBytes(cast<MCDataFragment>(F).getContents().str());
1380 Asm.writeSectionData(&SD, Layout);
1384 void ELFObjectWriter::WriteSectionHeader(MCAssembler &Asm,
1385 const GroupMapTy &GroupMap,
1386 const MCAsmLayout &Layout,
1387 const SectionIndexMapTy &SectionIndexMap,
1388 const SectionOffsetMapTy &SectionOffsetMap) {
1389 const unsigned NumSections = Asm.size() + 1;
1391 std::vector<const MCSectionELF*> Sections;
1392 Sections.resize(NumSections - 1);
1394 for (SectionIndexMapTy::const_iterator i=
1395 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1396 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
1397 Sections[p.second - 1] = p.first;
1400 // Null section first.
1401 uint64_t FirstSectionSize =
1402 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1403 uint32_t FirstSectionLink =
1404 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1405 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
1407 for (unsigned i = 0; i < NumSections - 1; ++i) {
1408 const MCSectionELF &Section = *Sections[i];
1409 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1410 uint32_t GroupSymbolIndex;
1411 if (Section.getType() != ELF::SHT_GROUP)
1412 GroupSymbolIndex = 0;
1414 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm,
1415 GroupMap.lookup(&Section));
1417 uint64_t Size = GetSectionAddressSize(Layout, SD);
1419 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
1420 SectionOffsetMap.lookup(&Section), Size,
1421 SD.getAlignment(), Section);
1425 void ELFObjectWriter::ComputeSectionOrder(MCAssembler &Asm,
1426 std::vector<const MCSectionELF*> &Sections) {
1427 for (MCAssembler::iterator it = Asm.begin(),
1428 ie = Asm.end(); it != ie; ++it) {
1429 const MCSectionELF &Section =
1430 static_cast<const MCSectionELF &>(it->getSection());
1431 if (Section.getType() == ELF::SHT_GROUP)
1432 Sections.push_back(&Section);
1435 for (MCAssembler::iterator it = Asm.begin(),
1436 ie = Asm.end(); it != ie; ++it) {
1437 const MCSectionELF &Section =
1438 static_cast<const MCSectionELF &>(it->getSection());
1439 if (Section.getType() != ELF::SHT_GROUP &&
1440 Section.getType() != ELF::SHT_REL &&
1441 Section.getType() != ELF::SHT_RELA)
1442 Sections.push_back(&Section);
1445 for (MCAssembler::iterator it = Asm.begin(),
1446 ie = Asm.end(); it != ie; ++it) {
1447 const MCSectionELF &Section =
1448 static_cast<const MCSectionELF &>(it->getSection());
1449 if (Section.getType() == ELF::SHT_REL ||
1450 Section.getType() == ELF::SHT_RELA)
1451 Sections.push_back(&Section);
1455 void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1456 const MCAsmLayout &Layout) {
1457 GroupMapTy GroupMap;
1458 RevGroupMapTy RevGroupMap;
1459 SectionIndexMapTy SectionIndexMap;
1461 unsigned NumUserSections = Asm.size();
1463 DenseMap<const MCSectionELF*, const MCSectionELF*> RelMap;
1464 CreateRelocationSections(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1466 const unsigned NumUserAndRelocSections = Asm.size();
1467 CreateIndexedSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1468 RevGroupMap, SectionIndexMap, RelMap);
1469 const unsigned AllSections = Asm.size();
1470 const unsigned NumIndexedSections = AllSections - NumUserAndRelocSections;
1472 unsigned NumRegularSections = NumUserSections + NumIndexedSections;
1474 // Compute symbol table information.
1475 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap, NumRegularSections);
1478 WriteRelocations(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1480 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
1481 const_cast<MCAsmLayout&>(Layout),
1485 uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
1486 uint64_t HeaderSize = is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
1487 sizeof(ELF::Elf32_Ehdr);
1488 uint64_t FileOff = HeaderSize;
1490 std::vector<const MCSectionELF*> Sections;
1491 ComputeSectionOrder(Asm, Sections);
1492 unsigned NumSections = Sections.size();
1493 SectionOffsetMapTy SectionOffsetMap;
1494 for (unsigned i = 0; i < NumRegularSections + 1; ++i) {
1495 const MCSectionELF &Section = *Sections[i];
1496 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1498 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1500 // Remember the offset into the file for this section.
1501 SectionOffsetMap[&Section] = FileOff;
1503 // Get the size of the section in the output file (including padding).
1504 FileOff += GetSectionFileSize(Layout, SD);
1507 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1509 const unsigned SectionHeaderOffset = FileOff - HeaderSize;
1511 uint64_t SectionHeaderEntrySize = is64Bit() ?
1512 sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr);
1513 FileOff += (NumSections + 1) * SectionHeaderEntrySize;
1515 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i) {
1516 const MCSectionELF &Section = *Sections[i];
1517 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1519 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1521 // Remember the offset into the file for this section.
1522 SectionOffsetMap[&Section] = FileOff;
1524 // Get the size of the section in the output file (including padding).
1525 FileOff += GetSectionFileSize(Layout, SD);
1528 // Write out the ELF header ...
1529 WriteHeader(SectionHeaderOffset, NumSections + 1);
1531 // ... then the regular sections ...
1532 // + because of .shstrtab
1533 for (unsigned i = 0; i < NumRegularSections + 1; ++i)
1534 WriteDataSectionData(Asm, Layout, *Sections[i]);
1536 uint64_t Padding = OffsetToAlignment(OS.tell(), NaturalAlignment);
1537 WriteZeros(Padding);
1539 // ... then the section header table ...
1540 WriteSectionHeader(Asm, GroupMap, Layout, SectionIndexMap,
1543 // ... and then the remaining sections ...
1544 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i)
1545 WriteDataSectionData(Asm, Layout, *Sections[i]);
1549 ELFObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
1550 const MCSymbolData &DataA,
1551 const MCFragment &FB,
1553 bool IsPCRel) const {
1554 if (DataA.getFlags() & ELF_STB_Weak)
1556 return MCObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(
1557 Asm, DataA, FB,InSet, IsPCRel);
1560 MCObjectWriter *llvm::createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1562 bool IsLittleEndian) {
1563 return new ELFObjectWriter(MOTW, OS, IsLittleEndian);