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 "ELFObjectWriter.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/StringMap.h"
17 #include "llvm/ADT/Twine.h"
18 #include "llvm/MC/MCAsmLayout.h"
19 #include "llvm/MC/MCContext.h"
20 #include "llvm/MC/MCExpr.h"
21 #include "llvm/MC/MCSectionELF.h"
22 #include "llvm/MC/MCValue.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/ELF.h"
26 #include "llvm/Target/TargetAsmBackend.h"
27 #include "llvm/ADT/StringSwitch.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/ADT/Statistic.h"
31 #include "../Target/X86/X86FixupKinds.h"
32 #include "../Target/ARM/ARMFixupKinds.h"
38 #define DEBUG_TYPE "reloc-info"
40 // Emulate the wierd behavior of GNU-as for relocation types
43 ForceARMElfPIC("arm-elf-force-pic", cl::Hidden, cl::init(false),
44 cl::desc("Force ELF emitter to emit PIC style relocations"));
47 bool ELFObjectWriter::isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) {
48 const MCFixupKindInfo &FKI =
49 Asm.getBackend().getFixupKindInfo((MCFixupKind) Kind);
51 return FKI.Flags & MCFixupKindInfo::FKF_IsPCRel;
54 bool ELFObjectWriter::RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant) {
58 case MCSymbolRefExpr::VK_GOT:
59 case MCSymbolRefExpr::VK_PLT:
60 case MCSymbolRefExpr::VK_GOTPCREL:
61 case MCSymbolRefExpr::VK_TPOFF:
62 case MCSymbolRefExpr::VK_TLSGD:
63 case MCSymbolRefExpr::VK_GOTTPOFF:
64 case MCSymbolRefExpr::VK_INDNTPOFF:
65 case MCSymbolRefExpr::VK_NTPOFF:
66 case MCSymbolRefExpr::VK_GOTNTPOFF:
67 case MCSymbolRefExpr::VK_TLSLDM:
68 case MCSymbolRefExpr::VK_DTPOFF:
69 case MCSymbolRefExpr::VK_TLSLD:
74 ELFObjectWriter::~ELFObjectWriter()
77 // Emit the ELF header.
78 void ELFObjectWriter::WriteHeader(uint64_t SectionDataSize,
79 unsigned NumberOfSections) {
85 // emitWord method behaves differently for ELF32 and ELF64, writing
86 // 4 bytes in the former and 8 in the latter.
88 Write8(0x7f); // e_ident[EI_MAG0]
89 Write8('E'); // e_ident[EI_MAG1]
90 Write8('L'); // e_ident[EI_MAG2]
91 Write8('F'); // e_ident[EI_MAG3]
93 Write8(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
96 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
98 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
100 switch (TargetObjectWriter->getOSType()) {
101 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
102 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
103 default: Write8(ELF::ELFOSABI_NONE); break;
105 Write8(0); // e_ident[EI_ABIVERSION]
107 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
109 Write16(ELF::ET_REL); // e_type
111 Write16(TargetObjectWriter->getEMachine()); // e_machine = target
113 Write32(ELF::EV_CURRENT); // e_version
114 WriteWord(0); // e_entry, no entry point in .o file
115 WriteWord(0); // e_phoff, no program header for .o
116 WriteWord(SectionDataSize + (is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
117 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
119 // e_flags = whatever the target wants
122 // e_ehsize = ELF header size
123 Write16(is64Bit() ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
125 Write16(0); // e_phentsize = prog header entry size
126 Write16(0); // e_phnum = # prog header entries = 0
128 // e_shentsize = Section header entry size
129 Write16(is64Bit() ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
131 // e_shnum = # of section header ents
132 if (NumberOfSections >= ELF::SHN_LORESERVE)
135 Write16(NumberOfSections);
137 // e_shstrndx = Section # of '.shstrtab'
138 if (NumberOfSections >= ELF::SHN_LORESERVE)
139 Write16(ELF::SHN_XINDEX);
141 Write16(ShstrtabIndex);
144 void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
145 MCDataFragment *ShndxF,
147 uint8_t info, uint64_t value,
148 uint64_t size, uint8_t other,
152 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
153 String32(*ShndxF, shndx);
155 String32(*ShndxF, 0);
158 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
159 uint16_t(ELF::SHN_XINDEX) : shndx;
162 String32(*SymtabF, name); // st_name
163 String8(*SymtabF, info); // st_info
164 String8(*SymtabF, other); // st_other
165 String16(*SymtabF, Index); // st_shndx
166 String64(*SymtabF, value); // st_value
167 String64(*SymtabF, size); // st_size
169 String32(*SymtabF, name); // st_name
170 String32(*SymtabF, value); // st_value
171 String32(*SymtabF, size); // st_size
172 String8(*SymtabF, info); // st_info
173 String8(*SymtabF, other); // st_other
174 String16(*SymtabF, Index); // st_shndx
178 uint64_t ELFObjectWriter::SymbolValue(MCSymbolData &Data,
179 const MCAsmLayout &Layout) {
180 if (Data.isCommon() && Data.isExternal())
181 return Data.getCommonAlignment();
183 const MCSymbol &Symbol = Data.getSymbol();
185 if (Symbol.isAbsolute() && Symbol.isVariable()) {
186 if (const MCExpr *Value = Symbol.getVariableValue()) {
188 if (Value->EvaluateAsAbsolute(IntValue, Layout))
189 return (uint64_t)IntValue;
193 if (!Symbol.isInSection())
196 if (Data.getFragment())
197 return Layout.getSymbolOffset(&Data);
202 void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
203 const MCAsmLayout &Layout) {
204 // The presence of symbol versions causes undefined symbols and
205 // versions declared with @@@ to be renamed.
207 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
208 ie = Asm.symbol_end(); it != ie; ++it) {
209 const MCSymbol &Alias = it->getSymbol();
210 const MCSymbol &Symbol = Alias.AliasedSymbol();
211 MCSymbolData &SD = Asm.getSymbolData(Symbol);
214 if (&Symbol == &Alias)
217 StringRef AliasName = Alias.getName();
218 size_t Pos = AliasName.find('@');
219 if (Pos == StringRef::npos)
222 // Aliases defined with .symvar copy the binding from the symbol they alias.
223 // This is the first place we are able to copy this information.
224 it->setExternal(SD.isExternal());
225 MCELF::SetBinding(*it, MCELF::GetBinding(SD));
227 StringRef Rest = AliasName.substr(Pos);
228 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
231 // FIXME: produce a better error message.
232 if (Symbol.isUndefined() && Rest.startswith("@@") &&
233 !Rest.startswith("@@@"))
234 report_fatal_error("A @@ version cannot be undefined");
236 Renames.insert(std::make_pair(&Symbol, &Alias));
240 void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
241 MCDataFragment *ShndxF,
243 const MCAsmLayout &Layout) {
244 MCSymbolData &OrigData = *MSD.SymbolData;
246 Layout.getAssembler().getSymbolData(OrigData.getSymbol().AliasedSymbol());
248 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
249 Data.getSymbol().isVariable();
251 uint8_t Binding = MCELF::GetBinding(OrigData);
252 uint8_t Visibility = MCELF::GetVisibility(OrigData);
253 uint8_t Type = MCELF::GetType(Data);
255 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
256 uint8_t Other = Visibility;
258 uint64_t Value = SymbolValue(Data, Layout);
261 assert(!(Data.isCommon() && !Data.isExternal()));
263 const MCExpr *ESize = Data.getSize();
266 if (!ESize->EvaluateAsAbsolute(Res, Layout))
267 report_fatal_error("Size expression must be absolute.");
271 // Write out the symbol table entry
272 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
273 Size, Other, MSD.SectionIndex, IsReserved);
276 void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
277 MCDataFragment *ShndxF,
278 const MCAssembler &Asm,
279 const MCAsmLayout &Layout,
280 const SectionIndexMapTy &SectionIndexMap) {
281 // The string table must be emitted first because we need the index
282 // into the string table for all the symbol names.
283 assert(StringTable.size() && "Missing string table");
285 // FIXME: Make sure the start of the symbol table is aligned.
287 // The first entry is the undefined symbol entry.
288 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
290 // Write the symbol table entries.
291 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
292 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
293 ELFSymbolData &MSD = LocalSymbolData[i];
294 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
297 // Write out a symbol table entry for each regular section.
298 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
300 const MCSectionELF &Section =
301 static_cast<const MCSectionELF&>(i->getSection());
302 if (Section.getType() == ELF::SHT_RELA ||
303 Section.getType() == ELF::SHT_REL ||
304 Section.getType() == ELF::SHT_STRTAB ||
305 Section.getType() == ELF::SHT_SYMTAB)
307 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
308 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section), false);
309 LastLocalSymbolIndex++;
312 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
313 ELFSymbolData &MSD = ExternalSymbolData[i];
314 MCSymbolData &Data = *MSD.SymbolData;
315 assert(((Data.getFlags() & ELF_STB_Global) ||
316 (Data.getFlags() & ELF_STB_Weak)) &&
317 "External symbol requires STB_GLOBAL or STB_WEAK flag");
318 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
319 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
320 LastLocalSymbolIndex++;
323 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
324 ELFSymbolData &MSD = UndefinedSymbolData[i];
325 MCSymbolData &Data = *MSD.SymbolData;
326 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
327 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
328 LastLocalSymbolIndex++;
332 const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm,
333 const MCValue &Target,
335 const MCFixup &Fixup,
336 bool IsPCRel) const {
337 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
338 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
339 const MCSymbol *Renamed = Renames.lookup(&Symbol);
340 const MCSymbolData &SD = Asm.getSymbolData(Symbol);
342 if (ASymbol.isUndefined()) {
348 if (SD.isExternal()) {
354 const MCSectionELF &Section =
355 static_cast<const MCSectionELF&>(ASymbol.getSection());
356 const SectionKind secKind = Section.getKind();
359 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
361 if (secKind.isThreadLocal()) {
367 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
368 const MCSectionELF &Sec2 =
369 static_cast<const MCSectionELF&>(F.getParent()->getSection());
371 if (&Sec2 != &Section &&
372 (Kind == MCSymbolRefExpr::VK_PLT ||
373 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
374 Kind == MCSymbolRefExpr::VK_GOTOFF)) {
380 if (Section.getFlags() & ELF::SHF_MERGE) {
381 if (Target.getConstant() == 0)
382 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
388 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
393 void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
394 const MCAsmLayout &Layout,
395 const MCFragment *Fragment,
396 const MCFixup &Fixup,
398 uint64_t &FixedValue) {
401 int64_t Value = Target.getConstant();
402 const MCSymbol *RelocSymbol = NULL;
404 bool IsPCRel = isFixupKindPCRel(Asm, Fixup.getKind());
405 if (!Target.isAbsolute()) {
406 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
407 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
408 RelocSymbol = SymbolToReloc(Asm, Target, *Fragment, Fixup, IsPCRel);
410 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
411 const MCSymbol &SymbolB = RefB->getSymbol();
412 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
415 // Offset of the symbol in the section
416 int64_t a = Layout.getSymbolOffset(&SDB);
418 // Ofeset of the relocation in the section
419 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
424 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
425 MCFragment *F = SD.getFragment();
427 Index = F->getParent()->getOrdinal() + 1;
429 // Offset of the symbol in the section
430 Value += Layout.getSymbolOffset(&SD);
432 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
433 WeakrefUsedInReloc.insert(RelocSymbol);
435 UsedInReloc.insert(RelocSymbol);
439 // Compensate for the addend on i386.
445 unsigned Type = GetRelocType(Target, Fixup, IsPCRel,
446 (RelocSymbol != 0), Addend);
448 uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
451 if (!hasRelocationAddend())
453 ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend);
454 Relocations[Fragment->getParent()].push_back(ERE);
459 ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
461 MCSymbolData &SD = Asm.getSymbolData(*S);
462 return SD.getIndex();
465 bool ELFObjectWriter::isInSymtab(const MCAssembler &Asm,
466 const MCSymbolData &Data,
467 bool Used, bool Renamed) {
468 if (Data.getFlags() & ELF_Other_Weakref)
477 const MCSymbol &Symbol = Data.getSymbol();
479 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
482 const MCSymbol &A = Symbol.AliasedSymbol();
483 if (Symbol.isVariable() && !A.isVariable() && A.isUndefined())
486 bool IsGlobal = MCELF::GetBinding(Data) == ELF::STB_GLOBAL;
487 if (!Symbol.isVariable() && Symbol.isUndefined() && !IsGlobal)
490 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
493 if (Symbol.isTemporary())
499 bool ELFObjectWriter::isLocal(const MCSymbolData &Data, bool isSignature,
500 bool isUsedInReloc) {
501 if (Data.isExternal())
504 const MCSymbol &Symbol = Data.getSymbol();
505 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
507 if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
508 if (isSignature && !isUsedInReloc)
517 void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
518 SectionIndexMapTy &SectionIndexMap,
519 const RelMapTy &RelMap) {
521 for (MCAssembler::iterator it = Asm.begin(),
522 ie = Asm.end(); it != ie; ++it) {
523 const MCSectionELF &Section =
524 static_cast<const MCSectionELF &>(it->getSection());
525 if (Section.getType() != ELF::SHT_GROUP)
527 SectionIndexMap[&Section] = Index++;
530 for (MCAssembler::iterator it = Asm.begin(),
531 ie = Asm.end(); it != ie; ++it) {
532 const MCSectionELF &Section =
533 static_cast<const MCSectionELF &>(it->getSection());
534 if (Section.getType() == ELF::SHT_GROUP ||
535 Section.getType() == ELF::SHT_REL ||
536 Section.getType() == ELF::SHT_RELA)
538 SectionIndexMap[&Section] = Index++;
539 const MCSectionELF *RelSection = RelMap.lookup(&Section);
541 SectionIndexMap[RelSection] = Index++;
545 void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
546 const SectionIndexMapTy &SectionIndexMap,
547 RevGroupMapTy RevGroupMap,
548 unsigned NumRegularSections) {
549 // FIXME: Is this the correct place to do this?
551 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
552 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
553 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
554 Data.setExternal(true);
555 MCELF::SetBinding(Data, ELF::STB_GLOBAL);
558 // Index 0 is always the empty string.
559 StringMap<uint64_t> StringIndexMap;
560 StringTable += '\x00';
562 // FIXME: We could optimize suffixes in strtab in the same way we
563 // optimize them in shstrtab.
565 // Add the data for the symbols.
566 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
567 ie = Asm.symbol_end(); it != ie; ++it) {
568 const MCSymbol &Symbol = it->getSymbol();
570 bool Used = UsedInReloc.count(&Symbol);
571 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
572 bool isSignature = RevGroupMap.count(&Symbol);
574 if (!isInSymtab(Asm, *it,
575 Used || WeakrefUsed || isSignature,
576 Renames.count(&Symbol)))
581 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
583 // Undefined symbols are global, but this is the first place we
584 // are able to set it.
585 bool Local = isLocal(*it, isSignature, Used);
586 if (!Local && MCELF::GetBinding(*it) == ELF::STB_LOCAL) {
587 MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
588 MCELF::SetBinding(*it, ELF::STB_GLOBAL);
589 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
592 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
593 MCELF::SetBinding(*it, ELF::STB_WEAK);
595 if (it->isCommon()) {
597 MSD.SectionIndex = ELF::SHN_COMMON;
598 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
599 MSD.SectionIndex = ELF::SHN_ABS;
600 } else if (RefSymbol.isUndefined()) {
601 if (isSignature && !Used)
602 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
604 MSD.SectionIndex = ELF::SHN_UNDEF;
606 const MCSectionELF &Section =
607 static_cast<const MCSectionELF&>(RefSymbol.getSection());
608 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
609 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
610 NeedsSymtabShndx = true;
611 assert(MSD.SectionIndex && "Invalid section index!");
614 // The @@@ in symbol version is replaced with @ in undefined symbols and
615 // @@ in defined ones.
616 StringRef Name = Symbol.getName();
619 size_t Pos = Name.find("@@@");
620 if (Pos != StringRef::npos) {
621 Buf += Name.substr(0, Pos);
622 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
623 Buf += Name.substr(Pos + Skip);
627 uint64_t &Entry = StringIndexMap[Name];
629 Entry = StringTable.size();
631 StringTable += '\x00';
633 MSD.StringIndex = Entry;
634 if (MSD.SectionIndex == ELF::SHN_UNDEF)
635 UndefinedSymbolData.push_back(MSD);
637 LocalSymbolData.push_back(MSD);
639 ExternalSymbolData.push_back(MSD);
642 // Symbols are required to be in lexicographic order.
643 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
644 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
645 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
647 // Set the symbol indices. Local symbols must come before all other
648 // symbols with non-local bindings.
650 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
651 LocalSymbolData[i].SymbolData->setIndex(Index++);
653 Index += NumRegularSections;
655 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
656 ExternalSymbolData[i].SymbolData->setIndex(Index++);
657 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
658 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
661 void ELFObjectWriter::CreateRelocationSections(MCAssembler &Asm,
664 for (MCAssembler::const_iterator it = Asm.begin(),
665 ie = Asm.end(); it != ie; ++it) {
666 const MCSectionData &SD = *it;
667 if (Relocations[&SD].empty())
670 MCContext &Ctx = Asm.getContext();
671 const MCSectionELF &Section =
672 static_cast<const MCSectionELF&>(SD.getSection());
674 const StringRef SectionName = Section.getSectionName();
675 std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
676 RelaSectionName += SectionName;
679 if (hasRelocationAddend())
680 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
682 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
684 const MCSectionELF *RelaSection =
685 Ctx.getELFSection(RelaSectionName, hasRelocationAddend() ?
686 ELF::SHT_RELA : ELF::SHT_REL, 0,
687 SectionKind::getReadOnly(),
689 RelMap[&Section] = RelaSection;
690 Asm.getOrCreateSectionData(*RelaSection);
694 void ELFObjectWriter::WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout,
695 const RelMapTy &RelMap) {
696 for (MCAssembler::const_iterator it = Asm.begin(),
697 ie = Asm.end(); it != ie; ++it) {
698 const MCSectionData &SD = *it;
699 const MCSectionELF &Section =
700 static_cast<const MCSectionELF&>(SD.getSection());
702 const MCSectionELF *RelaSection = RelMap.lookup(&Section);
705 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
706 RelaSD.setAlignment(is64Bit() ? 8 : 4);
708 MCDataFragment *F = new MCDataFragment(&RelaSD);
709 WriteRelocationsFragment(Asm, F, &*it);
713 void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
714 uint64_t Flags, uint64_t Address,
715 uint64_t Offset, uint64_t Size,
716 uint32_t Link, uint32_t Info,
718 uint64_t EntrySize) {
719 Write32(Name); // sh_name: index into string table
720 Write32(Type); // sh_type
721 WriteWord(Flags); // sh_flags
722 WriteWord(Address); // sh_addr
723 WriteWord(Offset); // sh_offset
724 WriteWord(Size); // sh_size
725 Write32(Link); // sh_link
726 Write32(Info); // sh_info
727 WriteWord(Alignment); // sh_addralign
728 WriteWord(EntrySize); // sh_entsize
731 void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
733 const MCSectionData *SD) {
734 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
735 // sort by the r_offset just like gnu as does
736 array_pod_sort(Relocs.begin(), Relocs.end());
738 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
739 ELFRelocationEntry entry = Relocs[e - i - 1];
743 else if (entry.Index < 0)
744 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
746 entry.Index += LocalSymbolData.size();
748 String64(*F, entry.r_offset);
750 struct ELF::Elf64_Rela ERE64;
751 ERE64.setSymbolAndType(entry.Index, entry.Type);
752 String64(*F, ERE64.r_info);
754 if (hasRelocationAddend())
755 String64(*F, entry.r_addend);
757 String32(*F, entry.r_offset);
759 struct ELF::Elf32_Rela ERE32;
760 ERE32.setSymbolAndType(entry.Index, entry.Type);
761 String32(*F, ERE32.r_info);
763 if (hasRelocationAddend())
764 String32(*F, entry.r_addend);
769 static int compareBySuffix(const void *a, const void *b) {
770 const MCSectionELF *secA = *static_cast<const MCSectionELF* const *>(a);
771 const MCSectionELF *secB = *static_cast<const MCSectionELF* const *>(b);
772 const StringRef &NameA = secA->getSectionName();
773 const StringRef &NameB = secB->getSectionName();
774 const unsigned sizeA = NameA.size();
775 const unsigned sizeB = NameB.size();
776 const unsigned len = std::min(sizeA, sizeB);
777 for (unsigned int i = 0; i < len; ++i) {
778 char ca = NameA[sizeA - i - 1];
779 char cb = NameB[sizeB - i - 1];
784 return sizeB - sizeA;
787 void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
789 SectionIndexMapTy &SectionIndexMap,
790 const RelMapTy &RelMap) {
791 MCContext &Ctx = Asm.getContext();
794 unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
796 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
797 const MCSectionELF *ShstrtabSection =
798 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
799 SectionKind::getReadOnly());
800 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
801 ShstrtabSD.setAlignment(1);
803 const MCSectionELF *SymtabSection =
804 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
805 SectionKind::getReadOnly(),
807 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
808 SymtabSD.setAlignment(is64Bit() ? 8 : 4);
810 MCSectionData *SymtabShndxSD = NULL;
812 if (NeedsSymtabShndx) {
813 const MCSectionELF *SymtabShndxSection =
814 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
815 SectionKind::getReadOnly(), 4, "");
816 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
817 SymtabShndxSD->setAlignment(4);
820 const MCSectionELF *StrtabSection;
821 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
822 SectionKind::getReadOnly());
823 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
824 StrtabSD.setAlignment(1);
826 ComputeIndexMap(Asm, SectionIndexMap, RelMap);
828 ShstrtabIndex = SectionIndexMap.lookup(ShstrtabSection);
829 SymbolTableIndex = SectionIndexMap.lookup(SymtabSection);
830 StringTableIndex = SectionIndexMap.lookup(StrtabSection);
833 F = new MCDataFragment(&SymtabSD);
834 MCDataFragment *ShndxF = NULL;
835 if (NeedsSymtabShndx) {
836 ShndxF = new MCDataFragment(SymtabShndxSD);
838 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
840 F = new MCDataFragment(&StrtabSD);
841 F->getContents().append(StringTable.begin(), StringTable.end());
843 F = new MCDataFragment(&ShstrtabSD);
845 std::vector<const MCSectionELF*> Sections;
846 for (MCAssembler::const_iterator it = Asm.begin(),
847 ie = Asm.end(); it != ie; ++it) {
848 const MCSectionELF &Section =
849 static_cast<const MCSectionELF&>(it->getSection());
850 Sections.push_back(&Section);
852 array_pod_sort(Sections.begin(), Sections.end(), compareBySuffix);
854 // Section header string table.
856 // The first entry of a string table holds a null character so skip
859 F->getContents() += '\x00';
861 for (unsigned int I = 0, E = Sections.size(); I != E; ++I) {
862 const MCSectionELF &Section = *Sections[I];
864 StringRef Name = Section.getSectionName();
866 StringRef PreviousName = Sections[I - 1]->getSectionName();
867 if (PreviousName.endswith(Name)) {
868 SectionStringTableIndex[&Section] = Index - Name.size() - 1;
872 // Remember the index into the string table so we can write it
873 // into the sh_name field of the section header table.
874 SectionStringTableIndex[&Section] = Index;
876 Index += Name.size() + 1;
877 F->getContents() += Name;
878 F->getContents() += '\x00';
882 void ELFObjectWriter::CreateIndexedSections(MCAssembler &Asm,
884 GroupMapTy &GroupMap,
885 RevGroupMapTy &RevGroupMap,
886 SectionIndexMapTy &SectionIndexMap,
887 const RelMapTy &RelMap) {
888 // Create the .note.GNU-stack section if needed.
889 MCContext &Ctx = Asm.getContext();
890 if (Asm.getNoExecStack()) {
891 const MCSectionELF *GnuStackSection =
892 Ctx.getELFSection(".note.GNU-stack", ELF::SHT_PROGBITS, 0,
893 SectionKind::getReadOnly());
894 Asm.getOrCreateSectionData(*GnuStackSection);
898 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
900 const MCSectionELF &Section =
901 static_cast<const MCSectionELF&>(it->getSection());
902 if (!(Section.getFlags() & ELF::SHF_GROUP))
905 const MCSymbol *SignatureSymbol = Section.getGroup();
906 Asm.getOrCreateSymbolData(*SignatureSymbol);
907 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
909 Group = Ctx.CreateELFGroupSection();
910 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
911 Data.setAlignment(4);
912 MCDataFragment *F = new MCDataFragment(&Data);
913 String32(*F, ELF::GRP_COMDAT);
915 GroupMap[Group] = SignatureSymbol;
918 ComputeIndexMap(Asm, SectionIndexMap, RelMap);
920 // Add sections to the groups
921 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
923 const MCSectionELF &Section =
924 static_cast<const MCSectionELF&>(it->getSection());
925 if (!(Section.getFlags() & ELF::SHF_GROUP))
927 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
928 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
929 // FIXME: we could use the previous fragment
930 MCDataFragment *F = new MCDataFragment(&Data);
931 unsigned Index = SectionIndexMap.lookup(&Section);
936 void ELFObjectWriter::WriteSection(MCAssembler &Asm,
937 const SectionIndexMapTy &SectionIndexMap,
938 uint32_t GroupSymbolIndex,
939 uint64_t Offset, uint64_t Size,
941 const MCSectionELF &Section) {
942 uint64_t sh_link = 0;
943 uint64_t sh_info = 0;
945 switch(Section.getType()) {
946 case ELF::SHT_DYNAMIC:
947 sh_link = SectionStringTableIndex[&Section];
952 case ELF::SHT_RELA: {
953 const MCSectionELF *SymtabSection;
954 const MCSectionELF *InfoSection;
955 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
957 SectionKind::getReadOnly());
958 sh_link = SectionIndexMap.lookup(SymtabSection);
959 assert(sh_link && ".symtab not found");
961 // Remove ".rel" and ".rela" prefixes.
962 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
963 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
965 InfoSection = Asm.getContext().getELFSection(SectionName,
966 ELF::SHT_PROGBITS, 0,
967 SectionKind::getReadOnly());
968 sh_info = SectionIndexMap.lookup(InfoSection);
972 case ELF::SHT_SYMTAB:
973 case ELF::SHT_DYNSYM:
974 sh_link = StringTableIndex;
975 sh_info = LastLocalSymbolIndex;
978 case ELF::SHT_SYMTAB_SHNDX:
979 sh_link = SymbolTableIndex;
982 case ELF::SHT_PROGBITS:
983 case ELF::SHT_STRTAB:
984 case ELF::SHT_NOBITS:
987 case ELF::SHT_ARM_ATTRIBUTES:
988 case ELF::SHT_INIT_ARRAY:
989 case ELF::SHT_FINI_ARRAY:
990 case ELF::SHT_PREINIT_ARRAY:
991 case ELF::SHT_X86_64_UNWIND:
995 case ELF::SHT_GROUP: {
996 sh_link = SymbolTableIndex;
997 sh_info = GroupSymbolIndex;
1002 assert(0 && "FIXME: sh_type value not supported!");
1006 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1007 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1008 Alignment, Section.getEntrySize());
1011 bool ELFObjectWriter::IsELFMetaDataSection(const MCSectionData &SD) {
1012 return SD.getOrdinal() == ~UINT32_C(0) &&
1013 !SD.getSection().isVirtualSection();
1016 uint64_t ELFObjectWriter::DataSectionSize(const MCSectionData &SD) {
1018 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1020 const MCFragment &F = *i;
1021 assert(F.getKind() == MCFragment::FT_Data);
1022 Ret += cast<MCDataFragment>(F).getContents().size();
1027 uint64_t ELFObjectWriter::GetSectionFileSize(const MCAsmLayout &Layout,
1028 const MCSectionData &SD) {
1029 if (IsELFMetaDataSection(SD))
1030 return DataSectionSize(SD);
1031 return Layout.getSectionFileSize(&SD);
1034 uint64_t ELFObjectWriter::GetSectionAddressSize(const MCAsmLayout &Layout,
1035 const MCSectionData &SD) {
1036 if (IsELFMetaDataSection(SD))
1037 return DataSectionSize(SD);
1038 return Layout.getSectionAddressSize(&SD);
1041 void ELFObjectWriter::WriteDataSectionData(MCAssembler &Asm,
1042 const MCAsmLayout &Layout,
1043 const MCSectionELF &Section) {
1044 uint64_t FileOff = OS.tell();
1045 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1047 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1048 WriteZeros(Padding);
1051 FileOff += GetSectionFileSize(Layout, SD);
1053 if (IsELFMetaDataSection(SD)) {
1054 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1056 const MCFragment &F = *i;
1057 assert(F.getKind() == MCFragment::FT_Data);
1058 WriteBytes(cast<MCDataFragment>(F).getContents().str());
1061 Asm.WriteSectionData(&SD, Layout);
1065 void ELFObjectWriter::WriteSectionHeader(MCAssembler &Asm,
1066 const GroupMapTy &GroupMap,
1067 const MCAsmLayout &Layout,
1068 const SectionIndexMapTy &SectionIndexMap,
1069 const SectionOffsetMapTy &SectionOffsetMap) {
1070 const unsigned NumSections = Asm.size() + 1;
1072 std::vector<const MCSectionELF*> Sections;
1073 Sections.resize(NumSections - 1);
1075 for (SectionIndexMapTy::const_iterator i=
1076 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1077 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
1078 Sections[p.second - 1] = p.first;
1081 // Null section first.
1082 uint64_t FirstSectionSize =
1083 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1084 uint32_t FirstSectionLink =
1085 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1086 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
1088 for (unsigned i = 0; i < NumSections - 1; ++i) {
1089 const MCSectionELF &Section = *Sections[i];
1090 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1091 uint32_t GroupSymbolIndex;
1092 if (Section.getType() != ELF::SHT_GROUP)
1093 GroupSymbolIndex = 0;
1095 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm,
1096 GroupMap.lookup(&Section));
1098 uint64_t Size = GetSectionAddressSize(Layout, SD);
1100 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
1101 SectionOffsetMap.lookup(&Section), Size,
1102 SD.getAlignment(), Section);
1106 void ELFObjectWriter::ComputeSectionOrder(MCAssembler &Asm,
1107 std::vector<const MCSectionELF*> &Sections) {
1108 for (MCAssembler::iterator it = Asm.begin(),
1109 ie = Asm.end(); it != ie; ++it) {
1110 const MCSectionELF &Section =
1111 static_cast<const MCSectionELF &>(it->getSection());
1112 if (Section.getType() == ELF::SHT_GROUP)
1113 Sections.push_back(&Section);
1116 for (MCAssembler::iterator it = Asm.begin(),
1117 ie = Asm.end(); it != ie; ++it) {
1118 const MCSectionELF &Section =
1119 static_cast<const MCSectionELF &>(it->getSection());
1120 if (Section.getType() != ELF::SHT_GROUP &&
1121 Section.getType() != ELF::SHT_REL &&
1122 Section.getType() != ELF::SHT_RELA)
1123 Sections.push_back(&Section);
1126 for (MCAssembler::iterator it = Asm.begin(),
1127 ie = Asm.end(); it != ie; ++it) {
1128 const MCSectionELF &Section =
1129 static_cast<const MCSectionELF &>(it->getSection());
1130 if (Section.getType() == ELF::SHT_REL ||
1131 Section.getType() == ELF::SHT_RELA)
1132 Sections.push_back(&Section);
1136 void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1137 const MCAsmLayout &Layout) {
1138 GroupMapTy GroupMap;
1139 RevGroupMapTy RevGroupMap;
1140 SectionIndexMapTy SectionIndexMap;
1142 unsigned NumUserSections = Asm.size();
1144 DenseMap<const MCSectionELF*, const MCSectionELF*> RelMap;
1145 CreateRelocationSections(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1147 const unsigned NumUserAndRelocSections = Asm.size();
1148 CreateIndexedSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1149 RevGroupMap, SectionIndexMap, RelMap);
1150 const unsigned AllSections = Asm.size();
1151 const unsigned NumIndexedSections = AllSections - NumUserAndRelocSections;
1153 unsigned NumRegularSections = NumUserSections + NumIndexedSections;
1155 // Compute symbol table information.
1156 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap, NumRegularSections);
1159 WriteRelocations(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1161 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
1162 const_cast<MCAsmLayout&>(Layout),
1166 uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
1167 uint64_t HeaderSize = is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
1168 sizeof(ELF::Elf32_Ehdr);
1169 uint64_t FileOff = HeaderSize;
1171 std::vector<const MCSectionELF*> Sections;
1172 ComputeSectionOrder(Asm, Sections);
1173 unsigned NumSections = Sections.size();
1174 SectionOffsetMapTy SectionOffsetMap;
1175 for (unsigned i = 0; i < NumRegularSections + 1; ++i) {
1176 const MCSectionELF &Section = *Sections[i];
1177 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1179 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1181 // Remember the offset into the file for this section.
1182 SectionOffsetMap[&Section] = FileOff;
1184 // Get the size of the section in the output file (including padding).
1185 FileOff += GetSectionFileSize(Layout, SD);
1188 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1190 const unsigned SectionHeaderOffset = FileOff - HeaderSize;
1192 uint64_t SectionHeaderEntrySize = is64Bit() ?
1193 sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr);
1194 FileOff += (NumSections + 1) * SectionHeaderEntrySize;
1196 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i) {
1197 const MCSectionELF &Section = *Sections[i];
1198 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1200 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1202 // Remember the offset into the file for this section.
1203 SectionOffsetMap[&Section] = FileOff;
1205 // Get the size of the section in the output file (including padding).
1206 FileOff += GetSectionFileSize(Layout, SD);
1209 // Write out the ELF header ...
1210 WriteHeader(SectionHeaderOffset, NumSections + 1);
1212 // ... then the regular sections ...
1213 // + because of .shstrtab
1214 for (unsigned i = 0; i < NumRegularSections + 1; ++i)
1215 WriteDataSectionData(Asm, Layout, *Sections[i]);
1217 FileOff = OS.tell();
1218 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1219 WriteZeros(Padding);
1221 // ... then the section header table ...
1222 WriteSectionHeader(Asm, GroupMap, Layout, SectionIndexMap,
1225 FileOff = OS.tell();
1227 // ... and then the remainting sections ...
1228 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i)
1229 WriteDataSectionData(Asm, Layout, *Sections[i]);
1233 ELFObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
1234 const MCSymbolData &DataA,
1235 const MCFragment &FB,
1237 bool IsPCRel) const {
1238 if (DataA.getFlags() & ELF_STB_Weak)
1240 return MCObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(
1241 Asm, DataA, FB,InSet, IsPCRel);
1244 MCObjectWriter *llvm::createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1246 bool IsLittleEndian) {
1247 switch (MOTW->getEMachine()) {
1249 case ELF::EM_X86_64:
1250 return new X86ELFObjectWriter(MOTW, OS, IsLittleEndian); break;
1252 return new ARMELFObjectWriter(MOTW, OS, IsLittleEndian); break;
1253 case ELF::EM_MBLAZE:
1254 return new MBlazeELFObjectWriter(MOTW, OS, IsLittleEndian); break;
1255 default: llvm_unreachable("Unsupported architecture"); break;
1260 /// START OF SUBCLASSES for ELFObjectWriter
1261 //===- ARMELFObjectWriter -------------------------------------------===//
1263 ARMELFObjectWriter::ARMELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1265 bool IsLittleEndian)
1266 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
1269 ARMELFObjectWriter::~ARMELFObjectWriter()
1272 // FIXME: get the real EABI Version from the Triple.
1273 void ARMELFObjectWriter::WriteEFlags() {
1274 Write32(ELF::EF_ARM_EABIMASK & DefaultEABIVersion);
1277 // In ARM, _MergedGlobals and other most symbols get emitted directly.
1278 // I.e. not as an offset to a section symbol.
1279 // This code is an approximation of what ARM/gcc does.
1281 STATISTIC(PCRelCount, "Total number of PIC Relocations");
1282 STATISTIC(NonPCRelCount, "Total number of non-PIC relocations");
1284 const MCSymbol *ARMELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
1285 const MCValue &Target,
1286 const MCFragment &F,
1287 const MCFixup &Fixup,
1288 bool IsPCRel) const {
1289 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
1290 bool EmitThisSym = false;
1292 const MCSectionELF &Section =
1293 static_cast<const MCSectionELF&>(Symbol.getSection());
1294 const SectionKind secKind = Section.getKind();
1295 const MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
1296 MCSymbolRefExpr::VariantKind Kind2;
1297 Kind2 = Target.getSymB() ? Target.getSymB()->getKind() :
1298 MCSymbolRefExpr::VK_None;
1299 bool InNormalSection = true;
1300 unsigned RelocType = 0;
1301 RelocType = GetRelocTypeInner(Target, Fixup, IsPCRel);
1303 DEBUG(dbgs() << "considering symbol "
1304 << Section.getSectionName() << "/"
1305 << Symbol.getName() << "/"
1306 << " Rel:" << (unsigned)RelocType
1307 << " Kind: " << (int)Kind << "/" << (int)Kind2
1309 << Symbol.isAbsolute() << "/" << Symbol.isDefined() << "/"
1310 << Symbol.isVariable() << "/" << Symbol.isTemporary()
1311 << " Counts:" << PCRelCount << "/" << NonPCRelCount << "\n");
1313 if (IsPCRel || ForceARMElfPIC) { ++PCRelCount;
1314 switch (RelocType) {
1316 // Most relocation types are emitted as explicit symbols
1318 StringSwitch<bool>(Section.getSectionName())
1319 .Case(".data.rel.ro.local", false)
1320 .Case(".data.rel", false)
1321 .Case(".bss", false)
1325 case ELF::R_ARM_ABS32:
1326 // But things get strange with R_ARM_ABS32
1327 // In this case, most things that go in .rodata show up
1328 // as section relative relocations
1330 StringSwitch<bool>(Section.getSectionName())
1331 .Case(".data.rel.ro.local", false)
1332 .Case(".data.rel", false)
1333 .Case(".rodata", false)
1334 .Case(".bss", false)
1336 EmitThisSym = false;
1342 StringSwitch<bool>(Section.getSectionName())
1343 .Case(".data.rel.ro.local", false)
1344 .Case(".rodata", false)
1345 .Case(".data.rel", false)
1346 .Case(".bss", false)
1349 switch (RelocType) {
1350 default: EmitThisSym = true; break;
1351 case ELF::R_ARM_ABS32: EmitThisSym = false; break;
1357 if (! Symbol.isTemporary() && InNormalSection) {
1363 // Need to examine the Fixup when determining whether to
1364 // emit the relocation as an explicit symbol or as a section relative
1366 unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target,
1367 const MCFixup &Fixup,
1369 bool IsRelocWithSymbol,
1371 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1372 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
1374 unsigned Type = GetRelocTypeInner(Target, Fixup, IsPCRel);
1376 if (RelocNeedsGOT(Modifier))
1382 unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
1383 const MCFixup &Fixup,
1384 bool IsPCRel) const {
1385 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1386 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
1390 switch ((unsigned)Fixup.getKind()) {
1391 default: assert(0 && "Unimplemented");
1394 default: llvm_unreachable("Unsupported Modifier");
1395 case MCSymbolRefExpr::VK_None:
1396 Type = ELF::R_ARM_REL32;
1398 case MCSymbolRefExpr::VK_ARM_TLSGD:
1399 assert(0 && "unimplemented");
1401 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
1402 Type = ELF::R_ARM_TLS_IE32;
1406 case ARM::fixup_arm_uncondbranch:
1408 case MCSymbolRefExpr::VK_ARM_PLT:
1409 Type = ELF::R_ARM_PLT32;
1412 Type = ELF::R_ARM_CALL;
1416 case ARM::fixup_arm_condbranch:
1417 Type = ELF::R_ARM_JUMP24;
1419 case ARM::fixup_arm_movt_hi16:
1420 case ARM::fixup_arm_movt_hi16_pcrel:
1421 Type = ELF::R_ARM_MOVT_PREL;
1423 case ARM::fixup_arm_movw_lo16:
1424 case ARM::fixup_arm_movw_lo16_pcrel:
1425 Type = ELF::R_ARM_MOVW_PREL_NC;
1427 case ARM::fixup_t2_movt_hi16:
1428 case ARM::fixup_t2_movt_hi16_pcrel:
1429 Type = ELF::R_ARM_THM_MOVT_PREL;
1431 case ARM::fixup_t2_movw_lo16:
1432 case ARM::fixup_t2_movw_lo16_pcrel:
1433 Type = ELF::R_ARM_THM_MOVW_PREL_NC;
1437 switch ((unsigned)Fixup.getKind()) {
1438 default: llvm_unreachable("invalid fixup kind!");
1441 default: llvm_unreachable("Unsupported Modifier"); break;
1442 case MCSymbolRefExpr::VK_ARM_GOT:
1443 Type = ELF::R_ARM_GOT_BREL;
1445 case MCSymbolRefExpr::VK_ARM_TLSGD:
1446 Type = ELF::R_ARM_TLS_GD32;
1448 case MCSymbolRefExpr::VK_ARM_TPOFF:
1449 Type = ELF::R_ARM_TLS_LE32;
1451 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
1452 Type = ELF::R_ARM_TLS_IE32;
1454 case MCSymbolRefExpr::VK_None:
1455 Type = ELF::R_ARM_ABS32;
1457 case MCSymbolRefExpr::VK_ARM_GOTOFF:
1458 Type = ELF::R_ARM_GOTOFF32;
1462 case ARM::fixup_arm_ldst_pcrel_12:
1463 case ARM::fixup_arm_pcrel_10:
1464 case ARM::fixup_arm_adr_pcrel_12:
1465 case ARM::fixup_arm_thumb_bl:
1466 case ARM::fixup_arm_thumb_cb:
1467 case ARM::fixup_arm_thumb_cp:
1468 case ARM::fixup_arm_thumb_br:
1469 assert(0 && "Unimplemented");
1471 case ARM::fixup_arm_uncondbranch:
1472 Type = ELF::R_ARM_CALL;
1474 case ARM::fixup_arm_condbranch:
1475 Type = ELF::R_ARM_JUMP24;
1477 case ARM::fixup_arm_movt_hi16:
1478 Type = ELF::R_ARM_MOVT_ABS;
1480 case ARM::fixup_arm_movw_lo16:
1481 Type = ELF::R_ARM_MOVW_ABS_NC;
1483 case ARM::fixup_t2_movt_hi16:
1484 Type = ELF::R_ARM_THM_MOVT_ABS;
1486 case ARM::fixup_t2_movw_lo16:
1487 Type = ELF::R_ARM_THM_MOVW_ABS_NC;
1495 //===- MBlazeELFObjectWriter -------------------------------------------===//
1497 MBlazeELFObjectWriter::MBlazeELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1499 bool IsLittleEndian)
1500 : ELFObjectWriter(MOTW, _OS, IsLittleEndian) {
1503 MBlazeELFObjectWriter::~MBlazeELFObjectWriter() {
1506 unsigned MBlazeELFObjectWriter::GetRelocType(const MCValue &Target,
1507 const MCFixup &Fixup,
1509 bool IsRelocWithSymbol,
1511 // determine the type of the relocation
1514 switch ((unsigned)Fixup.getKind()) {
1516 llvm_unreachable("Unimplemented");
1518 Type = ELF::R_MICROBLAZE_64_PCREL;
1521 Type = ELF::R_MICROBLAZE_32_PCREL;
1525 switch ((unsigned)Fixup.getKind()) {
1526 default: llvm_unreachable("invalid fixup kind!");
1528 Type = ((IsRelocWithSymbol || Addend !=0)
1529 ? ELF::R_MICROBLAZE_32
1530 : ELF::R_MICROBLAZE_64);
1533 Type = ELF::R_MICROBLAZE_32;
1540 //===- X86ELFObjectWriter -------------------------------------------===//
1543 X86ELFObjectWriter::X86ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1545 bool IsLittleEndian)
1546 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
1549 X86ELFObjectWriter::~X86ELFObjectWriter()
1552 unsigned X86ELFObjectWriter::GetRelocType(const MCValue &Target,
1553 const MCFixup &Fixup,
1555 bool IsRelocWithSymbol,
1557 // determine the type of the relocation
1559 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1560 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
1564 switch ((unsigned)Fixup.getKind()) {
1565 default: llvm_unreachable("invalid fixup kind!");
1567 case FK_Data_8: Type = ELF::R_X86_64_PC64; break;
1568 case FK_Data_4: Type = ELF::R_X86_64_PC32; break;
1569 case FK_Data_2: Type = ELF::R_X86_64_PC16; break;
1572 assert(Modifier == MCSymbolRefExpr::VK_None);
1573 Type = ELF::R_X86_64_PC64;
1575 case X86::reloc_signed_4byte:
1576 case X86::reloc_riprel_4byte_movq_load:
1577 case X86::reloc_riprel_4byte:
1581 llvm_unreachable("Unimplemented");
1582 case MCSymbolRefExpr::VK_None:
1583 Type = ELF::R_X86_64_PC32;
1585 case MCSymbolRefExpr::VK_PLT:
1586 Type = ELF::R_X86_64_PLT32;
1588 case MCSymbolRefExpr::VK_GOTPCREL:
1589 Type = ELF::R_X86_64_GOTPCREL;
1591 case MCSymbolRefExpr::VK_GOTTPOFF:
1592 Type = ELF::R_X86_64_GOTTPOFF;
1594 case MCSymbolRefExpr::VK_TLSGD:
1595 Type = ELF::R_X86_64_TLSGD;
1597 case MCSymbolRefExpr::VK_TLSLD:
1598 Type = ELF::R_X86_64_TLSLD;
1603 assert(Modifier == MCSymbolRefExpr::VK_None);
1604 Type = ELF::R_X86_64_PC16;
1607 assert(Modifier == MCSymbolRefExpr::VK_None);
1608 Type = ELF::R_X86_64_PC8;
1612 switch ((unsigned)Fixup.getKind()) {
1613 default: llvm_unreachable("invalid fixup kind!");
1614 case FK_Data_8: Type = ELF::R_X86_64_64; break;
1615 case X86::reloc_signed_4byte:
1616 assert(isInt<32>(Target.getConstant()));
1619 llvm_unreachable("Unimplemented");
1620 case MCSymbolRefExpr::VK_None:
1621 Type = ELF::R_X86_64_32S;
1623 case MCSymbolRefExpr::VK_GOT:
1624 Type = ELF::R_X86_64_GOT32;
1626 case MCSymbolRefExpr::VK_GOTPCREL:
1627 Type = ELF::R_X86_64_GOTPCREL;
1629 case MCSymbolRefExpr::VK_TPOFF:
1630 Type = ELF::R_X86_64_TPOFF32;
1632 case MCSymbolRefExpr::VK_DTPOFF:
1633 Type = ELF::R_X86_64_DTPOFF32;
1638 Type = ELF::R_X86_64_32;
1640 case FK_Data_2: Type = ELF::R_X86_64_16; break;
1642 case FK_Data_1: Type = ELF::R_X86_64_8; break;
1649 llvm_unreachable("Unimplemented");
1650 case MCSymbolRefExpr::VK_None:
1651 Type = ELF::R_386_PC32;
1653 case MCSymbolRefExpr::VK_PLT:
1654 Type = ELF::R_386_PLT32;
1658 switch ((unsigned)Fixup.getKind()) {
1659 default: llvm_unreachable("invalid fixup kind!");
1661 case X86::reloc_global_offset_table:
1662 Type = ELF::R_386_GOTPC;
1665 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
1667 case X86::reloc_signed_4byte:
1672 llvm_unreachable("Unimplemented");
1673 case MCSymbolRefExpr::VK_None:
1674 Type = ELF::R_386_32;
1676 case MCSymbolRefExpr::VK_GOT:
1677 Type = ELF::R_386_GOT32;
1679 case MCSymbolRefExpr::VK_GOTOFF:
1680 Type = ELF::R_386_GOTOFF;
1682 case MCSymbolRefExpr::VK_TLSGD:
1683 Type = ELF::R_386_TLS_GD;
1685 case MCSymbolRefExpr::VK_TPOFF:
1686 Type = ELF::R_386_TLS_LE_32;
1688 case MCSymbolRefExpr::VK_INDNTPOFF:
1689 Type = ELF::R_386_TLS_IE;
1691 case MCSymbolRefExpr::VK_NTPOFF:
1692 Type = ELF::R_386_TLS_LE;
1694 case MCSymbolRefExpr::VK_GOTNTPOFF:
1695 Type = ELF::R_386_TLS_GOTIE;
1697 case MCSymbolRefExpr::VK_TLSLDM:
1698 Type = ELF::R_386_TLS_LDM;
1700 case MCSymbolRefExpr::VK_DTPOFF:
1701 Type = ELF::R_386_TLS_LDO_32;
1705 case FK_Data_2: Type = ELF::R_386_16; break;
1707 case FK_Data_1: Type = ELF::R_386_8; break;
1712 if (RelocNeedsGOT(Modifier))