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/MCAsmBackend.h"
19 #include "llvm/MC/MCAsmLayout.h"
20 #include "llvm/MC/MCContext.h"
21 #include "llvm/MC/MCExpr.h"
22 #include "llvm/MC/MCSectionELF.h"
23 #include "llvm/MC/MCValue.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/ELF.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/ADT/Statistic.h"
29 #include "llvm/ADT/StringSwitch.h"
31 #include "../Target/X86/MCTargetDesc/X86FixupKinds.h"
32 #include "../Target/ARM/MCTargetDesc/ARMFixupKinds.h"
33 #include "../Target/PowerPC/MCTargetDesc/PPCFixupKinds.h"
39 #define DEBUG_TYPE "reloc-info"
41 bool ELFObjectWriter::isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) {
42 const MCFixupKindInfo &FKI =
43 Asm.getBackend().getFixupKindInfo((MCFixupKind) Kind);
45 return FKI.Flags & MCFixupKindInfo::FKF_IsPCRel;
48 bool ELFObjectWriter::RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant) {
52 case MCSymbolRefExpr::VK_GOT:
53 case MCSymbolRefExpr::VK_PLT:
54 case MCSymbolRefExpr::VK_GOTPCREL:
55 case MCSymbolRefExpr::VK_GOTOFF:
56 case MCSymbolRefExpr::VK_TPOFF:
57 case MCSymbolRefExpr::VK_TLSGD:
58 case MCSymbolRefExpr::VK_GOTTPOFF:
59 case MCSymbolRefExpr::VK_INDNTPOFF:
60 case MCSymbolRefExpr::VK_NTPOFF:
61 case MCSymbolRefExpr::VK_GOTNTPOFF:
62 case MCSymbolRefExpr::VK_TLSLDM:
63 case MCSymbolRefExpr::VK_DTPOFF:
64 case MCSymbolRefExpr::VK_TLSLD:
69 ELFObjectWriter::~ELFObjectWriter()
72 // Emit the ELF header.
73 void ELFObjectWriter::WriteHeader(uint64_t SectionDataSize,
74 unsigned NumberOfSections) {
80 // emitWord method behaves differently for ELF32 and ELF64, writing
81 // 4 bytes in the former and 8 in the latter.
83 Write8(0x7f); // e_ident[EI_MAG0]
84 Write8('E'); // e_ident[EI_MAG1]
85 Write8('L'); // e_ident[EI_MAG2]
86 Write8('F'); // e_ident[EI_MAG3]
88 Write8(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
91 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
93 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
95 switch (TargetObjectWriter->getOSType()) {
96 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
97 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
98 default: Write8(ELF::ELFOSABI_NONE); break;
100 Write8(0); // e_ident[EI_ABIVERSION]
102 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
104 Write16(ELF::ET_REL); // e_type
106 Write16(TargetObjectWriter->getEMachine()); // e_machine = target
108 Write32(ELF::EV_CURRENT); // e_version
109 WriteWord(0); // e_entry, no entry point in .o file
110 WriteWord(0); // e_phoff, no program header for .o
111 WriteWord(SectionDataSize + (is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
112 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
114 // e_flags = whatever the target wants
117 // e_ehsize = ELF header size
118 Write16(is64Bit() ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
120 Write16(0); // e_phentsize = prog header entry size
121 Write16(0); // e_phnum = # prog header entries = 0
123 // e_shentsize = Section header entry size
124 Write16(is64Bit() ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
126 // e_shnum = # of section header ents
127 if (NumberOfSections >= ELF::SHN_LORESERVE)
130 Write16(NumberOfSections);
132 // e_shstrndx = Section # of '.shstrtab'
133 if (NumberOfSections >= ELF::SHN_LORESERVE)
134 Write16(ELF::SHN_XINDEX);
136 Write16(ShstrtabIndex);
139 void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
140 MCDataFragment *ShndxF,
142 uint8_t info, uint64_t value,
143 uint64_t size, uint8_t other,
147 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
148 String32(*ShndxF, shndx);
150 String32(*ShndxF, 0);
153 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
154 uint16_t(ELF::SHN_XINDEX) : shndx;
157 String32(*SymtabF, name); // st_name
158 String8(*SymtabF, info); // st_info
159 String8(*SymtabF, other); // st_other
160 String16(*SymtabF, Index); // st_shndx
161 String64(*SymtabF, value); // st_value
162 String64(*SymtabF, size); // st_size
164 String32(*SymtabF, name); // st_name
165 String32(*SymtabF, value); // st_value
166 String32(*SymtabF, size); // st_size
167 String8(*SymtabF, info); // st_info
168 String8(*SymtabF, other); // st_other
169 String16(*SymtabF, Index); // st_shndx
173 uint64_t ELFObjectWriter::SymbolValue(MCSymbolData &Data,
174 const MCAsmLayout &Layout) {
175 if (Data.isCommon() && Data.isExternal())
176 return Data.getCommonAlignment();
178 const MCSymbol &Symbol = Data.getSymbol();
180 if (Symbol.isAbsolute() && Symbol.isVariable()) {
181 if (const MCExpr *Value = Symbol.getVariableValue()) {
183 if (Value->EvaluateAsAbsolute(IntValue, Layout))
184 return (uint64_t)IntValue;
188 if (!Symbol.isInSection())
192 if (Data.getFragment()) {
193 if (Data.getFlags() & ELF_Other_ThumbFunc)
194 return Layout.getSymbolOffset(&Data)+1;
196 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 adjustFixupOffset(Fixup, RelocOffset);
453 if (!hasRelocationAddend())
457 assert(isInt<64>(Addend));
459 assert(isInt<32>(Addend));
461 ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend);
462 Relocations[Fragment->getParent()].push_back(ERE);
467 ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
469 MCSymbolData &SD = Asm.getSymbolData(*S);
470 return SD.getIndex();
473 bool ELFObjectWriter::isInSymtab(const MCAssembler &Asm,
474 const MCSymbolData &Data,
475 bool Used, bool Renamed) {
476 if (Data.getFlags() & ELF_Other_Weakref)
485 const MCSymbol &Symbol = Data.getSymbol();
487 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
490 const MCSymbol &A = Symbol.AliasedSymbol();
491 if (Symbol.isVariable() && !A.isVariable() && A.isUndefined())
494 bool IsGlobal = MCELF::GetBinding(Data) == ELF::STB_GLOBAL;
495 if (!Symbol.isVariable() && Symbol.isUndefined() && !IsGlobal)
498 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
501 if (Symbol.isTemporary())
507 bool ELFObjectWriter::isLocal(const MCSymbolData &Data, bool isSignature,
508 bool isUsedInReloc) {
509 if (Data.isExternal())
512 const MCSymbol &Symbol = Data.getSymbol();
513 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
515 if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
516 if (isSignature && !isUsedInReloc)
525 void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
526 SectionIndexMapTy &SectionIndexMap,
527 const RelMapTy &RelMap) {
529 for (MCAssembler::iterator it = Asm.begin(),
530 ie = Asm.end(); it != ie; ++it) {
531 const MCSectionELF &Section =
532 static_cast<const MCSectionELF &>(it->getSection());
533 if (Section.getType() != ELF::SHT_GROUP)
535 SectionIndexMap[&Section] = Index++;
538 for (MCAssembler::iterator it = Asm.begin(),
539 ie = Asm.end(); it != ie; ++it) {
540 const MCSectionELF &Section =
541 static_cast<const MCSectionELF &>(it->getSection());
542 if (Section.getType() == ELF::SHT_GROUP ||
543 Section.getType() == ELF::SHT_REL ||
544 Section.getType() == ELF::SHT_RELA)
546 SectionIndexMap[&Section] = Index++;
547 const MCSectionELF *RelSection = RelMap.lookup(&Section);
549 SectionIndexMap[RelSection] = Index++;
553 void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
554 const SectionIndexMapTy &SectionIndexMap,
555 RevGroupMapTy RevGroupMap,
556 unsigned NumRegularSections) {
557 // FIXME: Is this the correct place to do this?
558 // FIXME: Why is an undefined reference to _GLOBAL_OFFSET_TABLE_ needed?
560 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
561 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
562 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
563 Data.setExternal(true);
564 MCELF::SetBinding(Data, ELF::STB_GLOBAL);
567 // Index 0 is always the empty string.
568 StringMap<uint64_t> StringIndexMap;
569 StringTable += '\x00';
571 // FIXME: We could optimize suffixes in strtab in the same way we
572 // optimize them in shstrtab.
574 // Add the data for the symbols.
575 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
576 ie = Asm.symbol_end(); it != ie; ++it) {
577 const MCSymbol &Symbol = it->getSymbol();
579 bool Used = UsedInReloc.count(&Symbol);
580 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
581 bool isSignature = RevGroupMap.count(&Symbol);
583 if (!isInSymtab(Asm, *it,
584 Used || WeakrefUsed || isSignature,
585 Renames.count(&Symbol)))
590 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
592 // Undefined symbols are global, but this is the first place we
593 // are able to set it.
594 bool Local = isLocal(*it, isSignature, Used);
595 if (!Local && MCELF::GetBinding(*it) == ELF::STB_LOCAL) {
596 MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
597 MCELF::SetBinding(*it, ELF::STB_GLOBAL);
598 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
601 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
602 MCELF::SetBinding(*it, ELF::STB_WEAK);
604 if (it->isCommon()) {
606 MSD.SectionIndex = ELF::SHN_COMMON;
607 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
608 MSD.SectionIndex = ELF::SHN_ABS;
609 } else if (RefSymbol.isUndefined()) {
610 if (isSignature && !Used)
611 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
613 MSD.SectionIndex = ELF::SHN_UNDEF;
615 const MCSectionELF &Section =
616 static_cast<const MCSectionELF&>(RefSymbol.getSection());
617 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
618 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
619 NeedsSymtabShndx = true;
620 assert(MSD.SectionIndex && "Invalid section index!");
623 // The @@@ in symbol version is replaced with @ in undefined symbols and
624 // @@ in defined ones.
625 StringRef Name = Symbol.getName();
628 size_t Pos = Name.find("@@@");
629 if (Pos != StringRef::npos) {
630 Buf += Name.substr(0, Pos);
631 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
632 Buf += Name.substr(Pos + Skip);
636 uint64_t &Entry = StringIndexMap[Name];
638 Entry = StringTable.size();
640 StringTable += '\x00';
642 MSD.StringIndex = Entry;
643 if (MSD.SectionIndex == ELF::SHN_UNDEF)
644 UndefinedSymbolData.push_back(MSD);
646 LocalSymbolData.push_back(MSD);
648 ExternalSymbolData.push_back(MSD);
651 // Symbols are required to be in lexicographic order.
652 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
653 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
654 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
656 // Set the symbol indices. Local symbols must come before all other
657 // symbols with non-local bindings.
659 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
660 LocalSymbolData[i].SymbolData->setIndex(Index++);
662 Index += NumRegularSections;
664 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
665 ExternalSymbolData[i].SymbolData->setIndex(Index++);
666 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
667 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
670 void ELFObjectWriter::CreateRelocationSections(MCAssembler &Asm,
673 for (MCAssembler::const_iterator it = Asm.begin(),
674 ie = Asm.end(); it != ie; ++it) {
675 const MCSectionData &SD = *it;
676 if (Relocations[&SD].empty())
679 MCContext &Ctx = Asm.getContext();
680 const MCSectionELF &Section =
681 static_cast<const MCSectionELF&>(SD.getSection());
683 const StringRef SectionName = Section.getSectionName();
684 std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
685 RelaSectionName += SectionName;
688 if (hasRelocationAddend())
689 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
691 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
693 const MCSectionELF *RelaSection =
694 Ctx.getELFSection(RelaSectionName, hasRelocationAddend() ?
695 ELF::SHT_RELA : ELF::SHT_REL, 0,
696 SectionKind::getReadOnly(),
698 RelMap[&Section] = RelaSection;
699 Asm.getOrCreateSectionData(*RelaSection);
703 void ELFObjectWriter::WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout,
704 const RelMapTy &RelMap) {
705 for (MCAssembler::const_iterator it = Asm.begin(),
706 ie = Asm.end(); it != ie; ++it) {
707 const MCSectionData &SD = *it;
708 const MCSectionELF &Section =
709 static_cast<const MCSectionELF&>(SD.getSection());
711 const MCSectionELF *RelaSection = RelMap.lookup(&Section);
714 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
715 RelaSD.setAlignment(is64Bit() ? 8 : 4);
717 MCDataFragment *F = new MCDataFragment(&RelaSD);
718 WriteRelocationsFragment(Asm, F, &*it);
722 void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
723 uint64_t Flags, uint64_t Address,
724 uint64_t Offset, uint64_t Size,
725 uint32_t Link, uint32_t Info,
727 uint64_t EntrySize) {
728 Write32(Name); // sh_name: index into string table
729 Write32(Type); // sh_type
730 WriteWord(Flags); // sh_flags
731 WriteWord(Address); // sh_addr
732 WriteWord(Offset); // sh_offset
733 WriteWord(Size); // sh_size
734 Write32(Link); // sh_link
735 Write32(Info); // sh_info
736 WriteWord(Alignment); // sh_addralign
737 WriteWord(EntrySize); // sh_entsize
740 void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
742 const MCSectionData *SD) {
743 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
744 // sort by the r_offset just like gnu as does
745 array_pod_sort(Relocs.begin(), Relocs.end());
747 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
748 ELFRelocationEntry entry = Relocs[e - i - 1];
752 else if (entry.Index < 0)
753 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
755 entry.Index += LocalSymbolData.size();
757 String64(*F, entry.r_offset);
759 struct ELF::Elf64_Rela ERE64;
760 ERE64.setSymbolAndType(entry.Index, entry.Type);
761 String64(*F, ERE64.r_info);
763 if (hasRelocationAddend())
764 String64(*F, entry.r_addend);
766 String32(*F, entry.r_offset);
768 struct ELF::Elf32_Rela ERE32;
769 ERE32.setSymbolAndType(entry.Index, entry.Type);
770 String32(*F, ERE32.r_info);
772 if (hasRelocationAddend())
773 String32(*F, entry.r_addend);
778 static int compareBySuffix(const void *a, const void *b) {
779 const MCSectionELF *secA = *static_cast<const MCSectionELF* const *>(a);
780 const MCSectionELF *secB = *static_cast<const MCSectionELF* const *>(b);
781 const StringRef &NameA = secA->getSectionName();
782 const StringRef &NameB = secB->getSectionName();
783 const unsigned sizeA = NameA.size();
784 const unsigned sizeB = NameB.size();
785 const unsigned len = std::min(sizeA, sizeB);
786 for (unsigned int i = 0; i < len; ++i) {
787 char ca = NameA[sizeA - i - 1];
788 char cb = NameB[sizeB - i - 1];
793 return sizeB - sizeA;
796 void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
798 SectionIndexMapTy &SectionIndexMap,
799 const RelMapTy &RelMap) {
800 MCContext &Ctx = Asm.getContext();
803 unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
805 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
806 const MCSectionELF *ShstrtabSection =
807 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
808 SectionKind::getReadOnly());
809 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
810 ShstrtabSD.setAlignment(1);
812 const MCSectionELF *SymtabSection =
813 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
814 SectionKind::getReadOnly(),
816 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
817 SymtabSD.setAlignment(is64Bit() ? 8 : 4);
819 MCSectionData *SymtabShndxSD = NULL;
821 if (NeedsSymtabShndx) {
822 const MCSectionELF *SymtabShndxSection =
823 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
824 SectionKind::getReadOnly(), 4, "");
825 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
826 SymtabShndxSD->setAlignment(4);
829 const MCSectionELF *StrtabSection;
830 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
831 SectionKind::getReadOnly());
832 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
833 StrtabSD.setAlignment(1);
835 ComputeIndexMap(Asm, SectionIndexMap, RelMap);
837 ShstrtabIndex = SectionIndexMap.lookup(ShstrtabSection);
838 SymbolTableIndex = SectionIndexMap.lookup(SymtabSection);
839 StringTableIndex = SectionIndexMap.lookup(StrtabSection);
842 F = new MCDataFragment(&SymtabSD);
843 MCDataFragment *ShndxF = NULL;
844 if (NeedsSymtabShndx) {
845 ShndxF = new MCDataFragment(SymtabShndxSD);
847 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
849 F = new MCDataFragment(&StrtabSD);
850 F->getContents().append(StringTable.begin(), StringTable.end());
852 F = new MCDataFragment(&ShstrtabSD);
854 std::vector<const MCSectionELF*> Sections;
855 for (MCAssembler::const_iterator it = Asm.begin(),
856 ie = Asm.end(); it != ie; ++it) {
857 const MCSectionELF &Section =
858 static_cast<const MCSectionELF&>(it->getSection());
859 Sections.push_back(&Section);
861 array_pod_sort(Sections.begin(), Sections.end(), compareBySuffix);
863 // Section header string table.
865 // The first entry of a string table holds a null character so skip
868 F->getContents() += '\x00';
870 for (unsigned int I = 0, E = Sections.size(); I != E; ++I) {
871 const MCSectionELF &Section = *Sections[I];
873 StringRef Name = Section.getSectionName();
875 StringRef PreviousName = Sections[I - 1]->getSectionName();
876 if (PreviousName.endswith(Name)) {
877 SectionStringTableIndex[&Section] = Index - Name.size() - 1;
881 // Remember the index into the string table so we can write it
882 // into the sh_name field of the section header table.
883 SectionStringTableIndex[&Section] = Index;
885 Index += Name.size() + 1;
886 F->getContents() += Name;
887 F->getContents() += '\x00';
891 void ELFObjectWriter::CreateIndexedSections(MCAssembler &Asm,
893 GroupMapTy &GroupMap,
894 RevGroupMapTy &RevGroupMap,
895 SectionIndexMapTy &SectionIndexMap,
896 const RelMapTy &RelMap) {
897 // Create the .note.GNU-stack section if needed.
898 MCContext &Ctx = Asm.getContext();
899 if (Asm.getNoExecStack()) {
900 const MCSectionELF *GnuStackSection =
901 Ctx.getELFSection(".note.GNU-stack", ELF::SHT_PROGBITS, 0,
902 SectionKind::getReadOnly());
903 Asm.getOrCreateSectionData(*GnuStackSection);
907 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
909 const MCSectionELF &Section =
910 static_cast<const MCSectionELF&>(it->getSection());
911 if (!(Section.getFlags() & ELF::SHF_GROUP))
914 const MCSymbol *SignatureSymbol = Section.getGroup();
915 Asm.getOrCreateSymbolData(*SignatureSymbol);
916 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
918 Group = Ctx.CreateELFGroupSection();
919 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
920 Data.setAlignment(4);
921 MCDataFragment *F = new MCDataFragment(&Data);
922 String32(*F, ELF::GRP_COMDAT);
924 GroupMap[Group] = SignatureSymbol;
927 ComputeIndexMap(Asm, SectionIndexMap, RelMap);
929 // Add sections to the groups
930 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
932 const MCSectionELF &Section =
933 static_cast<const MCSectionELF&>(it->getSection());
934 if (!(Section.getFlags() & ELF::SHF_GROUP))
936 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
937 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
938 // FIXME: we could use the previous fragment
939 MCDataFragment *F = new MCDataFragment(&Data);
940 unsigned Index = SectionIndexMap.lookup(&Section);
945 void ELFObjectWriter::WriteSection(MCAssembler &Asm,
946 const SectionIndexMapTy &SectionIndexMap,
947 uint32_t GroupSymbolIndex,
948 uint64_t Offset, uint64_t Size,
950 const MCSectionELF &Section) {
951 uint64_t sh_link = 0;
952 uint64_t sh_info = 0;
954 switch(Section.getType()) {
955 case ELF::SHT_DYNAMIC:
956 sh_link = SectionStringTableIndex[&Section];
961 case ELF::SHT_RELA: {
962 const MCSectionELF *SymtabSection;
963 const MCSectionELF *InfoSection;
964 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
966 SectionKind::getReadOnly());
967 sh_link = SectionIndexMap.lookup(SymtabSection);
968 assert(sh_link && ".symtab not found");
970 // Remove ".rel" and ".rela" prefixes.
971 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
972 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
974 InfoSection = Asm.getContext().getELFSection(SectionName,
975 ELF::SHT_PROGBITS, 0,
976 SectionKind::getReadOnly());
977 sh_info = SectionIndexMap.lookup(InfoSection);
981 case ELF::SHT_SYMTAB:
982 case ELF::SHT_DYNSYM:
983 sh_link = StringTableIndex;
984 sh_info = LastLocalSymbolIndex;
987 case ELF::SHT_SYMTAB_SHNDX:
988 sh_link = SymbolTableIndex;
991 case ELF::SHT_PROGBITS:
992 case ELF::SHT_STRTAB:
993 case ELF::SHT_NOBITS:
996 case ELF::SHT_ARM_ATTRIBUTES:
997 case ELF::SHT_INIT_ARRAY:
998 case ELF::SHT_FINI_ARRAY:
999 case ELF::SHT_PREINIT_ARRAY:
1000 case ELF::SHT_X86_64_UNWIND:
1004 case ELF::SHT_GROUP: {
1005 sh_link = SymbolTableIndex;
1006 sh_info = GroupSymbolIndex;
1011 assert(0 && "FIXME: sh_type value not supported!");
1015 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1016 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1017 Alignment, Section.getEntrySize());
1020 bool ELFObjectWriter::IsELFMetaDataSection(const MCSectionData &SD) {
1021 return SD.getOrdinal() == ~UINT32_C(0) &&
1022 !SD.getSection().isVirtualSection();
1025 uint64_t ELFObjectWriter::DataSectionSize(const MCSectionData &SD) {
1027 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1029 const MCFragment &F = *i;
1030 assert(F.getKind() == MCFragment::FT_Data);
1031 Ret += cast<MCDataFragment>(F).getContents().size();
1036 uint64_t ELFObjectWriter::GetSectionFileSize(const MCAsmLayout &Layout,
1037 const MCSectionData &SD) {
1038 if (IsELFMetaDataSection(SD))
1039 return DataSectionSize(SD);
1040 return Layout.getSectionFileSize(&SD);
1043 uint64_t ELFObjectWriter::GetSectionAddressSize(const MCAsmLayout &Layout,
1044 const MCSectionData &SD) {
1045 if (IsELFMetaDataSection(SD))
1046 return DataSectionSize(SD);
1047 return Layout.getSectionAddressSize(&SD);
1050 void ELFObjectWriter::WriteDataSectionData(MCAssembler &Asm,
1051 const MCAsmLayout &Layout,
1052 const MCSectionELF &Section) {
1053 uint64_t FileOff = OS.tell();
1054 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1056 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1057 WriteZeros(Padding);
1060 FileOff += GetSectionFileSize(Layout, SD);
1062 if (IsELFMetaDataSection(SD)) {
1063 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1065 const MCFragment &F = *i;
1066 assert(F.getKind() == MCFragment::FT_Data);
1067 WriteBytes(cast<MCDataFragment>(F).getContents().str());
1070 Asm.WriteSectionData(&SD, Layout);
1074 void ELFObjectWriter::WriteSectionHeader(MCAssembler &Asm,
1075 const GroupMapTy &GroupMap,
1076 const MCAsmLayout &Layout,
1077 const SectionIndexMapTy &SectionIndexMap,
1078 const SectionOffsetMapTy &SectionOffsetMap) {
1079 const unsigned NumSections = Asm.size() + 1;
1081 std::vector<const MCSectionELF*> Sections;
1082 Sections.resize(NumSections - 1);
1084 for (SectionIndexMapTy::const_iterator i=
1085 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1086 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
1087 Sections[p.second - 1] = p.first;
1090 // Null section first.
1091 uint64_t FirstSectionSize =
1092 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1093 uint32_t FirstSectionLink =
1094 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1095 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
1097 for (unsigned i = 0; i < NumSections - 1; ++i) {
1098 const MCSectionELF &Section = *Sections[i];
1099 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1100 uint32_t GroupSymbolIndex;
1101 if (Section.getType() != ELF::SHT_GROUP)
1102 GroupSymbolIndex = 0;
1104 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm,
1105 GroupMap.lookup(&Section));
1107 uint64_t Size = GetSectionAddressSize(Layout, SD);
1109 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
1110 SectionOffsetMap.lookup(&Section), Size,
1111 SD.getAlignment(), Section);
1115 void ELFObjectWriter::ComputeSectionOrder(MCAssembler &Asm,
1116 std::vector<const MCSectionELF*> &Sections) {
1117 for (MCAssembler::iterator it = Asm.begin(),
1118 ie = Asm.end(); it != ie; ++it) {
1119 const MCSectionELF &Section =
1120 static_cast<const MCSectionELF &>(it->getSection());
1121 if (Section.getType() == ELF::SHT_GROUP)
1122 Sections.push_back(&Section);
1125 for (MCAssembler::iterator it = Asm.begin(),
1126 ie = Asm.end(); it != ie; ++it) {
1127 const MCSectionELF &Section =
1128 static_cast<const MCSectionELF &>(it->getSection());
1129 if (Section.getType() != ELF::SHT_GROUP &&
1130 Section.getType() != ELF::SHT_REL &&
1131 Section.getType() != ELF::SHT_RELA)
1132 Sections.push_back(&Section);
1135 for (MCAssembler::iterator it = Asm.begin(),
1136 ie = Asm.end(); it != ie; ++it) {
1137 const MCSectionELF &Section =
1138 static_cast<const MCSectionELF &>(it->getSection());
1139 if (Section.getType() == ELF::SHT_REL ||
1140 Section.getType() == ELF::SHT_RELA)
1141 Sections.push_back(&Section);
1145 void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1146 const MCAsmLayout &Layout) {
1147 GroupMapTy GroupMap;
1148 RevGroupMapTy RevGroupMap;
1149 SectionIndexMapTy SectionIndexMap;
1151 unsigned NumUserSections = Asm.size();
1153 DenseMap<const MCSectionELF*, const MCSectionELF*> RelMap;
1154 CreateRelocationSections(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1156 const unsigned NumUserAndRelocSections = Asm.size();
1157 CreateIndexedSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1158 RevGroupMap, SectionIndexMap, RelMap);
1159 const unsigned AllSections = Asm.size();
1160 const unsigned NumIndexedSections = AllSections - NumUserAndRelocSections;
1162 unsigned NumRegularSections = NumUserSections + NumIndexedSections;
1164 // Compute symbol table information.
1165 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap, NumRegularSections);
1168 WriteRelocations(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1170 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
1171 const_cast<MCAsmLayout&>(Layout),
1175 uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
1176 uint64_t HeaderSize = is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
1177 sizeof(ELF::Elf32_Ehdr);
1178 uint64_t FileOff = HeaderSize;
1180 std::vector<const MCSectionELF*> Sections;
1181 ComputeSectionOrder(Asm, Sections);
1182 unsigned NumSections = Sections.size();
1183 SectionOffsetMapTy SectionOffsetMap;
1184 for (unsigned i = 0; i < NumRegularSections + 1; ++i) {
1185 const MCSectionELF &Section = *Sections[i];
1186 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1188 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1190 // Remember the offset into the file for this section.
1191 SectionOffsetMap[&Section] = FileOff;
1193 // Get the size of the section in the output file (including padding).
1194 FileOff += GetSectionFileSize(Layout, SD);
1197 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1199 const unsigned SectionHeaderOffset = FileOff - HeaderSize;
1201 uint64_t SectionHeaderEntrySize = is64Bit() ?
1202 sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr);
1203 FileOff += (NumSections + 1) * SectionHeaderEntrySize;
1205 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i) {
1206 const MCSectionELF &Section = *Sections[i];
1207 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1209 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1211 // Remember the offset into the file for this section.
1212 SectionOffsetMap[&Section] = FileOff;
1214 // Get the size of the section in the output file (including padding).
1215 FileOff += GetSectionFileSize(Layout, SD);
1218 // Write out the ELF header ...
1219 WriteHeader(SectionHeaderOffset, NumSections + 1);
1221 // ... then the regular sections ...
1222 // + because of .shstrtab
1223 for (unsigned i = 0; i < NumRegularSections + 1; ++i)
1224 WriteDataSectionData(Asm, Layout, *Sections[i]);
1226 FileOff = OS.tell();
1227 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1228 WriteZeros(Padding);
1230 // ... then the section header table ...
1231 WriteSectionHeader(Asm, GroupMap, Layout, SectionIndexMap,
1234 FileOff = OS.tell();
1236 // ... and then the remainting sections ...
1237 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i)
1238 WriteDataSectionData(Asm, Layout, *Sections[i]);
1242 ELFObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
1243 const MCSymbolData &DataA,
1244 const MCFragment &FB,
1246 bool IsPCRel) const {
1247 if (DataA.getFlags() & ELF_STB_Weak)
1249 return MCObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(
1250 Asm, DataA, FB,InSet, IsPCRel);
1253 MCObjectWriter *llvm::createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1255 bool IsLittleEndian) {
1256 switch (MOTW->getEMachine()) {
1258 case ELF::EM_X86_64:
1259 return new X86ELFObjectWriter(MOTW, OS, IsLittleEndian); break;
1261 return new ARMELFObjectWriter(MOTW, OS, IsLittleEndian); break;
1262 case ELF::EM_MBLAZE:
1263 return new MBlazeELFObjectWriter(MOTW, OS, IsLittleEndian); break;
1266 return new PPCELFObjectWriter(MOTW, OS, IsLittleEndian); break;
1267 default: llvm_unreachable("Unsupported architecture"); break;
1272 /// START OF SUBCLASSES for ELFObjectWriter
1273 //===- ARMELFObjectWriter -------------------------------------------===//
1275 ARMELFObjectWriter::ARMELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1277 bool IsLittleEndian)
1278 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
1281 ARMELFObjectWriter::~ARMELFObjectWriter()
1284 // FIXME: get the real EABI Version from the Triple.
1285 void ARMELFObjectWriter::WriteEFlags() {
1286 Write32(ELF::EF_ARM_EABIMASK & DefaultEABIVersion);
1289 // In ARM, _MergedGlobals and other most symbols get emitted directly.
1290 // I.e. not as an offset to a section symbol.
1291 // This code is an approximation of what ARM/gcc does.
1293 STATISTIC(PCRelCount, "Total number of PIC Relocations");
1294 STATISTIC(NonPCRelCount, "Total number of non-PIC relocations");
1296 const MCSymbol *ARMELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
1297 const MCValue &Target,
1298 const MCFragment &F,
1299 const MCFixup &Fixup,
1300 bool IsPCRel) const {
1301 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
1302 bool EmitThisSym = false;
1304 const MCSectionELF &Section =
1305 static_cast<const MCSectionELF&>(Symbol.getSection());
1306 bool InNormalSection = true;
1307 unsigned RelocType = 0;
1308 RelocType = GetRelocTypeInner(Target, Fixup, IsPCRel);
1311 const MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
1312 MCSymbolRefExpr::VariantKind Kind2;
1313 Kind2 = Target.getSymB() ? Target.getSymB()->getKind() :
1314 MCSymbolRefExpr::VK_None;
1315 dbgs() << "considering symbol "
1316 << Section.getSectionName() << "/"
1317 << Symbol.getName() << "/"
1318 << " Rel:" << (unsigned)RelocType
1319 << " Kind: " << (int)Kind << "/" << (int)Kind2
1321 << Symbol.isAbsolute() << "/" << Symbol.isDefined() << "/"
1322 << Symbol.isVariable() << "/" << Symbol.isTemporary()
1323 << " Counts:" << PCRelCount << "/" << NonPCRelCount << "\n");
1325 if (IsPCRel) { ++PCRelCount;
1326 switch (RelocType) {
1328 // Most relocation types are emitted as explicit symbols
1330 StringSwitch<bool>(Section.getSectionName())
1331 .Case(".data.rel.ro.local", false)
1332 .Case(".data.rel", false)
1333 .Case(".bss", false)
1337 case ELF::R_ARM_ABS32:
1338 // But things get strange with R_ARM_ABS32
1339 // In this case, most things that go in .rodata show up
1340 // as section relative relocations
1342 StringSwitch<bool>(Section.getSectionName())
1343 .Case(".data.rel.ro.local", false)
1344 .Case(".data.rel", false)
1345 .Case(".rodata", false)
1346 .Case(".bss", false)
1348 EmitThisSym = false;
1354 StringSwitch<bool>(Section.getSectionName())
1355 .Case(".data.rel.ro.local", false)
1356 .Case(".rodata", false)
1357 .Case(".data.rel", false)
1358 .Case(".bss", false)
1361 switch (RelocType) {
1362 default: EmitThisSym = true; break;
1363 case ELF::R_ARM_ABS32: EmitThisSym = false; break;
1369 if (! Symbol.isTemporary() && InNormalSection) {
1375 // Need to examine the Fixup when determining whether to
1376 // emit the relocation as an explicit symbol or as a section relative
1378 unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target,
1379 const MCFixup &Fixup,
1381 bool IsRelocWithSymbol,
1383 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1384 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
1386 unsigned Type = GetRelocTypeInner(Target, Fixup, IsPCRel);
1388 if (RelocNeedsGOT(Modifier))
1394 unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
1395 const MCFixup &Fixup,
1396 bool IsPCRel) const {
1397 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1398 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
1402 switch ((unsigned)Fixup.getKind()) {
1403 default: assert(0 && "Unimplemented");
1406 default: llvm_unreachable("Unsupported Modifier");
1407 case MCSymbolRefExpr::VK_None:
1408 Type = ELF::R_ARM_REL32;
1410 case MCSymbolRefExpr::VK_ARM_TLSGD:
1411 assert(0 && "unimplemented");
1413 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
1414 Type = ELF::R_ARM_TLS_IE32;
1418 case ARM::fixup_arm_uncondbranch:
1420 case MCSymbolRefExpr::VK_ARM_PLT:
1421 Type = ELF::R_ARM_PLT32;
1424 Type = ELF::R_ARM_CALL;
1428 case ARM::fixup_arm_condbranch:
1429 Type = ELF::R_ARM_JUMP24;
1431 case ARM::fixup_arm_movt_hi16:
1432 case ARM::fixup_arm_movt_hi16_pcrel:
1433 Type = ELF::R_ARM_MOVT_PREL;
1435 case ARM::fixup_arm_movw_lo16:
1436 case ARM::fixup_arm_movw_lo16_pcrel:
1437 Type = ELF::R_ARM_MOVW_PREL_NC;
1439 case ARM::fixup_t2_movt_hi16:
1440 case ARM::fixup_t2_movt_hi16_pcrel:
1441 Type = ELF::R_ARM_THM_MOVT_PREL;
1443 case ARM::fixup_t2_movw_lo16:
1444 case ARM::fixup_t2_movw_lo16_pcrel:
1445 Type = ELF::R_ARM_THM_MOVW_PREL_NC;
1447 case ARM::fixup_arm_thumb_bl:
1448 case ARM::fixup_arm_thumb_blx:
1450 case MCSymbolRefExpr::VK_ARM_PLT:
1451 Type = ELF::R_ARM_THM_CALL;
1454 Type = ELF::R_ARM_NONE;
1460 switch ((unsigned)Fixup.getKind()) {
1461 default: llvm_unreachable("invalid fixup kind!");
1464 default: llvm_unreachable("Unsupported Modifier"); break;
1465 case MCSymbolRefExpr::VK_ARM_GOT:
1466 Type = ELF::R_ARM_GOT_BREL;
1468 case MCSymbolRefExpr::VK_ARM_TLSGD:
1469 Type = ELF::R_ARM_TLS_GD32;
1471 case MCSymbolRefExpr::VK_ARM_TPOFF:
1472 Type = ELF::R_ARM_TLS_LE32;
1474 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
1475 Type = ELF::R_ARM_TLS_IE32;
1477 case MCSymbolRefExpr::VK_None:
1478 Type = ELF::R_ARM_ABS32;
1480 case MCSymbolRefExpr::VK_ARM_GOTOFF:
1481 Type = ELF::R_ARM_GOTOFF32;
1485 case ARM::fixup_arm_ldst_pcrel_12:
1486 case ARM::fixup_arm_pcrel_10:
1487 case ARM::fixup_arm_adr_pcrel_12:
1488 case ARM::fixup_arm_thumb_bl:
1489 case ARM::fixup_arm_thumb_cb:
1490 case ARM::fixup_arm_thumb_cp:
1491 case ARM::fixup_arm_thumb_br:
1492 assert(0 && "Unimplemented");
1494 case ARM::fixup_arm_uncondbranch:
1495 Type = ELF::R_ARM_CALL;
1497 case ARM::fixup_arm_condbranch:
1498 Type = ELF::R_ARM_JUMP24;
1500 case ARM::fixup_arm_movt_hi16:
1501 Type = ELF::R_ARM_MOVT_ABS;
1503 case ARM::fixup_arm_movw_lo16:
1504 Type = ELF::R_ARM_MOVW_ABS_NC;
1506 case ARM::fixup_t2_movt_hi16:
1507 Type = ELF::R_ARM_THM_MOVT_ABS;
1509 case ARM::fixup_t2_movw_lo16:
1510 Type = ELF::R_ARM_THM_MOVW_ABS_NC;
1518 //===- PPCELFObjectWriter -------------------------------------------===//
1520 PPCELFObjectWriter::PPCELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1522 bool IsLittleEndian)
1523 : ELFObjectWriter(MOTW, _OS, IsLittleEndian) {
1526 PPCELFObjectWriter::~PPCELFObjectWriter() {
1529 unsigned PPCELFObjectWriter::GetRelocType(const MCValue &Target,
1530 const MCFixup &Fixup,
1532 bool IsRelocWithSymbol,
1534 // determine the type of the relocation
1537 switch ((unsigned)Fixup.getKind()) {
1539 llvm_unreachable("Unimplemented");
1540 case PPC::fixup_ppc_br24:
1541 Type = ELF::R_PPC_REL24;
1544 Type = ELF::R_PPC_REL32;
1548 switch ((unsigned)Fixup.getKind()) {
1549 default: llvm_unreachable("invalid fixup kind!");
1550 case PPC::fixup_ppc_br24:
1551 Type = ELF::R_PPC_ADDR24;
1553 case PPC::fixup_ppc_brcond14:
1554 Type = ELF::R_PPC_ADDR14_BRTAKEN; // XXX: or BRNTAKEN?_
1556 case PPC::fixup_ppc_ha16:
1557 Type = ELF::R_PPC_ADDR16_HA;
1559 case PPC::fixup_ppc_lo16:
1560 Type = ELF::R_PPC_ADDR16_LO;
1562 case PPC::fixup_ppc_lo14:
1563 Type = ELF::R_PPC_ADDR14;
1566 Type = ELF::R_PPC_ADDR32;
1569 Type = ELF::R_PPC_ADDR16;
1577 PPCELFObjectWriter::adjustFixupOffset(const MCFixup &Fixup, uint64_t &RelocOffset) {
1578 switch ((unsigned)Fixup.getKind()) {
1579 case PPC::fixup_ppc_ha16:
1580 case PPC::fixup_ppc_lo16:
1588 //===- MBlazeELFObjectWriter -------------------------------------------===//
1590 MBlazeELFObjectWriter::MBlazeELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1592 bool IsLittleEndian)
1593 : ELFObjectWriter(MOTW, _OS, IsLittleEndian) {
1596 MBlazeELFObjectWriter::~MBlazeELFObjectWriter() {
1599 unsigned MBlazeELFObjectWriter::GetRelocType(const MCValue &Target,
1600 const MCFixup &Fixup,
1602 bool IsRelocWithSymbol,
1604 // determine the type of the relocation
1607 switch ((unsigned)Fixup.getKind()) {
1609 llvm_unreachable("Unimplemented");
1611 Type = ELF::R_MICROBLAZE_64_PCREL;
1614 Type = ELF::R_MICROBLAZE_32_PCREL;
1618 switch ((unsigned)Fixup.getKind()) {
1619 default: llvm_unreachable("invalid fixup kind!");
1621 Type = ((IsRelocWithSymbol || Addend !=0)
1622 ? ELF::R_MICROBLAZE_32
1623 : ELF::R_MICROBLAZE_64);
1626 Type = ELF::R_MICROBLAZE_32;
1633 //===- X86ELFObjectWriter -------------------------------------------===//
1636 X86ELFObjectWriter::X86ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1638 bool IsLittleEndian)
1639 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
1642 X86ELFObjectWriter::~X86ELFObjectWriter()
1645 unsigned X86ELFObjectWriter::GetRelocType(const MCValue &Target,
1646 const MCFixup &Fixup,
1648 bool IsRelocWithSymbol,
1650 // determine the type of the relocation
1652 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1653 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
1657 switch ((unsigned)Fixup.getKind()) {
1658 default: llvm_unreachable("invalid fixup kind!");
1660 case FK_Data_8: Type = ELF::R_X86_64_PC64; break;
1661 case FK_Data_4: Type = ELF::R_X86_64_PC32; break;
1662 case FK_Data_2: Type = ELF::R_X86_64_PC16; break;
1665 assert(Modifier == MCSymbolRefExpr::VK_None);
1666 Type = ELF::R_X86_64_PC64;
1668 case X86::reloc_signed_4byte:
1669 case X86::reloc_riprel_4byte_movq_load:
1670 case X86::reloc_riprel_4byte:
1674 llvm_unreachable("Unimplemented");
1675 case MCSymbolRefExpr::VK_None:
1676 Type = ELF::R_X86_64_PC32;
1678 case MCSymbolRefExpr::VK_PLT:
1679 Type = ELF::R_X86_64_PLT32;
1681 case MCSymbolRefExpr::VK_GOTPCREL:
1682 Type = ELF::R_X86_64_GOTPCREL;
1684 case MCSymbolRefExpr::VK_GOTTPOFF:
1685 Type = ELF::R_X86_64_GOTTPOFF;
1687 case MCSymbolRefExpr::VK_TLSGD:
1688 Type = ELF::R_X86_64_TLSGD;
1690 case MCSymbolRefExpr::VK_TLSLD:
1691 Type = ELF::R_X86_64_TLSLD;
1696 assert(Modifier == MCSymbolRefExpr::VK_None);
1697 Type = ELF::R_X86_64_PC16;
1700 assert(Modifier == MCSymbolRefExpr::VK_None);
1701 Type = ELF::R_X86_64_PC8;
1705 switch ((unsigned)Fixup.getKind()) {
1706 default: llvm_unreachable("invalid fixup kind!");
1707 case FK_Data_8: Type = ELF::R_X86_64_64; break;
1708 case X86::reloc_signed_4byte:
1711 llvm_unreachable("Unimplemented");
1712 case MCSymbolRefExpr::VK_None:
1713 Type = ELF::R_X86_64_32S;
1715 case MCSymbolRefExpr::VK_GOT:
1716 Type = ELF::R_X86_64_GOT32;
1718 case MCSymbolRefExpr::VK_GOTPCREL:
1719 Type = ELF::R_X86_64_GOTPCREL;
1721 case MCSymbolRefExpr::VK_TPOFF:
1722 Type = ELF::R_X86_64_TPOFF32;
1724 case MCSymbolRefExpr::VK_DTPOFF:
1725 Type = ELF::R_X86_64_DTPOFF32;
1730 Type = ELF::R_X86_64_32;
1732 case FK_Data_2: Type = ELF::R_X86_64_16; break;
1734 case FK_Data_1: Type = ELF::R_X86_64_8; break;
1741 llvm_unreachable("Unimplemented");
1742 case MCSymbolRefExpr::VK_None:
1743 Type = ELF::R_386_PC32;
1745 case MCSymbolRefExpr::VK_PLT:
1746 Type = ELF::R_386_PLT32;
1750 switch ((unsigned)Fixup.getKind()) {
1751 default: llvm_unreachable("invalid fixup kind!");
1753 case X86::reloc_global_offset_table:
1754 Type = ELF::R_386_GOTPC;
1757 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
1759 case X86::reloc_signed_4byte:
1764 llvm_unreachable("Unimplemented");
1765 case MCSymbolRefExpr::VK_None:
1766 Type = ELF::R_386_32;
1768 case MCSymbolRefExpr::VK_GOT:
1769 Type = ELF::R_386_GOT32;
1771 case MCSymbolRefExpr::VK_GOTOFF:
1772 Type = ELF::R_386_GOTOFF;
1774 case MCSymbolRefExpr::VK_TLSGD:
1775 Type = ELF::R_386_TLS_GD;
1777 case MCSymbolRefExpr::VK_TPOFF:
1778 Type = ELF::R_386_TLS_LE_32;
1780 case MCSymbolRefExpr::VK_INDNTPOFF:
1781 Type = ELF::R_386_TLS_IE;
1783 case MCSymbolRefExpr::VK_NTPOFF:
1784 Type = ELF::R_386_TLS_LE;
1786 case MCSymbolRefExpr::VK_GOTNTPOFF:
1787 Type = ELF::R_386_TLS_GOTIE;
1789 case MCSymbolRefExpr::VK_TLSLDM:
1790 Type = ELF::R_386_TLS_LDM;
1792 case MCSymbolRefExpr::VK_DTPOFF:
1793 Type = ELF::R_386_TLS_LDO_32;
1795 case MCSymbolRefExpr::VK_GOTTPOFF:
1796 Type = ELF::R_386_TLS_IE_32;
1800 case FK_Data_2: Type = ELF::R_386_16; break;
1802 case FK_Data_1: Type = ELF::R_386_8; break;
1807 if (RelocNeedsGOT(Modifier))