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"
29 #include "../Target/X86/X86FixupKinds.h"
30 #include "../Target/ARM/ARMFixupKinds.h"
35 bool ELFObjectWriter::isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) {
36 const MCFixupKindInfo &FKI =
37 Asm.getBackend().getFixupKindInfo((MCFixupKind) Kind);
39 return FKI.Flags & MCFixupKindInfo::FKF_IsPCRel;
42 bool ELFObjectWriter::RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant) {
46 case MCSymbolRefExpr::VK_GOT:
47 case MCSymbolRefExpr::VK_PLT:
48 case MCSymbolRefExpr::VK_GOTPCREL:
49 case MCSymbolRefExpr::VK_TPOFF:
50 case MCSymbolRefExpr::VK_TLSGD:
51 case MCSymbolRefExpr::VK_GOTTPOFF:
52 case MCSymbolRefExpr::VK_INDNTPOFF:
53 case MCSymbolRefExpr::VK_NTPOFF:
54 case MCSymbolRefExpr::VK_GOTNTPOFF:
55 case MCSymbolRefExpr::VK_TLSLDM:
56 case MCSymbolRefExpr::VK_DTPOFF:
57 case MCSymbolRefExpr::VK_TLSLD:
62 ELFObjectWriter::~ELFObjectWriter()
65 // Emit the ELF header.
66 void ELFObjectWriter::WriteHeader(uint64_t SectionDataSize,
67 unsigned NumberOfSections) {
73 // emitWord method behaves differently for ELF32 and ELF64, writing
74 // 4 bytes in the former and 8 in the latter.
76 Write8(0x7f); // e_ident[EI_MAG0]
77 Write8('E'); // e_ident[EI_MAG1]
78 Write8('L'); // e_ident[EI_MAG2]
79 Write8('F'); // e_ident[EI_MAG3]
81 Write8(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
84 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
86 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
88 switch (TargetObjectWriter->getOSType()) {
89 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
90 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
91 default: Write8(ELF::ELFOSABI_NONE); break;
93 Write8(0); // e_ident[EI_ABIVERSION]
95 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
97 Write16(ELF::ET_REL); // e_type
99 Write16(TargetObjectWriter->getEMachine()); // e_machine = target
101 Write32(ELF::EV_CURRENT); // e_version
102 WriteWord(0); // e_entry, no entry point in .o file
103 WriteWord(0); // e_phoff, no program header for .o
104 WriteWord(SectionDataSize + (is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
105 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
107 // e_flags = whatever the target wants
110 // e_ehsize = ELF header size
111 Write16(is64Bit() ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
113 Write16(0); // e_phentsize = prog header entry size
114 Write16(0); // e_phnum = # prog header entries = 0
116 // e_shentsize = Section header entry size
117 Write16(is64Bit() ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
119 // e_shnum = # of section header ents
120 if (NumberOfSections >= ELF::SHN_LORESERVE)
123 Write16(NumberOfSections);
125 // e_shstrndx = Section # of '.shstrtab'
126 if (NumberOfSections >= ELF::SHN_LORESERVE)
127 Write16(ELF::SHN_XINDEX);
129 Write16(ShstrtabIndex);
132 void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
133 MCDataFragment *ShndxF,
135 uint8_t info, uint64_t value,
136 uint64_t size, uint8_t other,
140 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
141 String32(*ShndxF, shndx);
143 String32(*ShndxF, 0);
146 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
147 uint16_t(ELF::SHN_XINDEX) : shndx;
150 String32(*SymtabF, name); // st_name
151 String8(*SymtabF, info); // st_info
152 String8(*SymtabF, other); // st_other
153 String16(*SymtabF, Index); // st_shndx
154 String64(*SymtabF, value); // st_value
155 String64(*SymtabF, size); // st_size
157 String32(*SymtabF, name); // st_name
158 String32(*SymtabF, value); // st_value
159 String32(*SymtabF, size); // st_size
160 String8(*SymtabF, info); // st_info
161 String8(*SymtabF, other); // st_other
162 String16(*SymtabF, Index); // st_shndx
166 uint64_t ELFObjectWriter::SymbolValue(MCSymbolData &Data,
167 const MCAsmLayout &Layout) {
168 if (Data.isCommon() && Data.isExternal())
169 return Data.getCommonAlignment();
171 const MCSymbol &Symbol = Data.getSymbol();
173 if (Symbol.isAbsolute() && Symbol.isVariable()) {
174 if (const MCExpr *Value = Symbol.getVariableValue()) {
176 if (Value->EvaluateAsAbsolute(IntValue, Layout))
177 return (uint64_t)IntValue;
181 if (!Symbol.isInSection())
184 if (Data.getFragment())
185 return Layout.getSymbolOffset(&Data);
190 void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
191 const MCAsmLayout &Layout) {
192 // The presence of symbol versions causes undefined symbols and
193 // versions declared with @@@ to be renamed.
195 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
196 ie = Asm.symbol_end(); it != ie; ++it) {
197 const MCSymbol &Alias = it->getSymbol();
198 const MCSymbol &Symbol = Alias.AliasedSymbol();
199 MCSymbolData &SD = Asm.getSymbolData(Symbol);
202 if (&Symbol == &Alias)
205 StringRef AliasName = Alias.getName();
206 size_t Pos = AliasName.find('@');
207 if (Pos == StringRef::npos)
210 // Aliases defined with .symvar copy the binding from the symbol they alias.
211 // This is the first place we are able to copy this information.
212 it->setExternal(SD.isExternal());
213 MCELF::SetBinding(*it, MCELF::GetBinding(SD));
215 StringRef Rest = AliasName.substr(Pos);
216 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
219 // FIXME: produce a better error message.
220 if (Symbol.isUndefined() && Rest.startswith("@@") &&
221 !Rest.startswith("@@@"))
222 report_fatal_error("A @@ version cannot be undefined");
224 Renames.insert(std::make_pair(&Symbol, &Alias));
228 void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
229 MCDataFragment *ShndxF,
231 const MCAsmLayout &Layout) {
232 MCSymbolData &OrigData = *MSD.SymbolData;
234 Layout.getAssembler().getSymbolData(OrigData.getSymbol().AliasedSymbol());
236 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
237 Data.getSymbol().isVariable();
239 uint8_t Binding = MCELF::GetBinding(OrigData);
240 uint8_t Visibility = MCELF::GetVisibility(OrigData);
241 uint8_t Type = MCELF::GetType(Data);
243 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
244 uint8_t Other = Visibility;
246 uint64_t Value = SymbolValue(Data, Layout);
249 assert(!(Data.isCommon() && !Data.isExternal()));
251 const MCExpr *ESize = Data.getSize();
254 if (!ESize->EvaluateAsAbsolute(Res, Layout))
255 report_fatal_error("Size expression must be absolute.");
259 // Write out the symbol table entry
260 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
261 Size, Other, MSD.SectionIndex, IsReserved);
264 void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
265 MCDataFragment *ShndxF,
266 const MCAssembler &Asm,
267 const MCAsmLayout &Layout,
268 const SectionIndexMapTy &SectionIndexMap) {
269 // The string table must be emitted first because we need the index
270 // into the string table for all the symbol names.
271 assert(StringTable.size() && "Missing string table");
273 // FIXME: Make sure the start of the symbol table is aligned.
275 // The first entry is the undefined symbol entry.
276 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
278 // Write the symbol table entries.
279 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
280 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
281 ELFSymbolData &MSD = LocalSymbolData[i];
282 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
285 // Write out a symbol table entry for each regular section.
286 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
288 const MCSectionELF &Section =
289 static_cast<const MCSectionELF&>(i->getSection());
290 if (Section.getType() == ELF::SHT_RELA ||
291 Section.getType() == ELF::SHT_REL ||
292 Section.getType() == ELF::SHT_STRTAB ||
293 Section.getType() == ELF::SHT_SYMTAB)
295 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
296 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section), false);
297 LastLocalSymbolIndex++;
300 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
301 ELFSymbolData &MSD = ExternalSymbolData[i];
302 MCSymbolData &Data = *MSD.SymbolData;
303 assert(((Data.getFlags() & ELF_STB_Global) ||
304 (Data.getFlags() & ELF_STB_Weak)) &&
305 "External symbol requires STB_GLOBAL or STB_WEAK flag");
306 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
307 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
308 LastLocalSymbolIndex++;
311 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
312 ELFSymbolData &MSD = UndefinedSymbolData[i];
313 MCSymbolData &Data = *MSD.SymbolData;
314 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
315 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
316 LastLocalSymbolIndex++;
320 const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm,
321 const MCValue &Target,
322 const MCFragment &F) const {
323 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
324 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
325 const MCSymbol *Renamed = Renames.lookup(&Symbol);
326 const MCSymbolData &SD = Asm.getSymbolData(Symbol);
328 if (ASymbol.isUndefined()) {
334 if (SD.isExternal()) {
340 const MCSectionELF &Section =
341 static_cast<const MCSectionELF&>(ASymbol.getSection());
342 const SectionKind secKind = Section.getKind();
345 return ExplicitRelSym(Asm, Target, F, true);
347 if (secKind.isThreadLocal()) {
353 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
354 const MCSectionELF &Sec2 =
355 static_cast<const MCSectionELF&>(F.getParent()->getSection());
357 if (&Sec2 != &Section &&
358 (Kind == MCSymbolRefExpr::VK_PLT ||
359 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
360 Kind == MCSymbolRefExpr::VK_GOTOFF)) {
366 if (Section.getFlags() & ELF::SHF_MERGE) {
367 if (Target.getConstant() == 0)
374 return ExplicitRelSym(Asm, Target, F, false);
378 void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
379 const MCAsmLayout &Layout,
380 const MCFragment *Fragment,
381 const MCFixup &Fixup,
383 uint64_t &FixedValue) {
386 int64_t Value = Target.getConstant();
387 const MCSymbol *RelocSymbol = NULL;
389 bool IsPCRel = isFixupKindPCRel(Asm, Fixup.getKind());
390 if (!Target.isAbsolute()) {
391 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
392 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
393 RelocSymbol = SymbolToReloc(Asm, Target, *Fragment);
395 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
396 const MCSymbol &SymbolB = RefB->getSymbol();
397 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
400 // Offset of the symbol in the section
401 int64_t a = Layout.getSymbolOffset(&SDB);
403 // Ofeset of the relocation in the section
404 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
409 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
410 MCFragment *F = SD.getFragment();
412 Index = F->getParent()->getOrdinal() + 1;
414 // Offset of the symbol in the section
415 Value += Layout.getSymbolOffset(&SD);
417 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
418 WeakrefUsedInReloc.insert(RelocSymbol);
420 UsedInReloc.insert(RelocSymbol);
424 // Compensate for the addend on i386.
430 unsigned Type = GetRelocType(Target, Fixup, IsPCRel,
431 (RelocSymbol != 0), Addend);
433 uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
436 if (!hasRelocationAddend())
438 ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend);
439 Relocations[Fragment->getParent()].push_back(ERE);
444 ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
446 MCSymbolData &SD = Asm.getSymbolData(*S);
447 return SD.getIndex();
450 bool ELFObjectWriter::isInSymtab(const MCAssembler &Asm,
451 const MCSymbolData &Data,
452 bool Used, bool Renamed) {
453 if (Data.getFlags() & ELF_Other_Weakref)
462 const MCSymbol &Symbol = Data.getSymbol();
464 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
467 const MCSymbol &A = Symbol.AliasedSymbol();
468 if (Symbol.isVariable() && !A.isVariable() && A.isUndefined())
471 bool IsGlobal = MCELF::GetBinding(Data) == ELF::STB_GLOBAL;
472 if (!Symbol.isVariable() && Symbol.isUndefined() && !IsGlobal)
475 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
478 if (Symbol.isTemporary())
484 bool ELFObjectWriter::isLocal(const MCSymbolData &Data, bool isSignature,
485 bool isUsedInReloc) {
486 if (Data.isExternal())
489 const MCSymbol &Symbol = Data.getSymbol();
490 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
492 if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
493 if (isSignature && !isUsedInReloc)
502 void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
503 SectionIndexMapTy &SectionIndexMap) {
505 for (MCAssembler::iterator it = Asm.begin(),
506 ie = Asm.end(); it != ie; ++it) {
507 const MCSectionELF &Section =
508 static_cast<const MCSectionELF &>(it->getSection());
509 if (Section.getType() != ELF::SHT_GROUP)
511 SectionIndexMap[&Section] = Index++;
514 for (MCAssembler::iterator it = Asm.begin(),
515 ie = Asm.end(); it != ie; ++it) {
516 const MCSectionELF &Section =
517 static_cast<const MCSectionELF &>(it->getSection());
518 if (Section.getType() == ELF::SHT_GROUP)
520 SectionIndexMap[&Section] = Index++;
524 void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
525 const SectionIndexMapTy &SectionIndexMap,
526 RevGroupMapTy RevGroupMap) {
527 // FIXME: Is this the correct place to do this?
529 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
530 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
531 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
532 Data.setExternal(true);
533 MCELF::SetBinding(Data, ELF::STB_GLOBAL);
536 // Build section lookup table.
537 int NumRegularSections = Asm.size();
539 // Index 0 is always the empty string.
540 StringMap<uint64_t> StringIndexMap;
541 StringTable += '\x00';
543 // Add the data for the symbols.
544 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
545 ie = Asm.symbol_end(); it != ie; ++it) {
546 const MCSymbol &Symbol = it->getSymbol();
548 bool Used = UsedInReloc.count(&Symbol);
549 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
550 bool isSignature = RevGroupMap.count(&Symbol);
552 if (!isInSymtab(Asm, *it,
553 Used || WeakrefUsed || isSignature,
554 Renames.count(&Symbol)))
559 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
561 // Undefined symbols are global, but this is the first place we
562 // are able to set it.
563 bool Local = isLocal(*it, isSignature, Used);
564 if (!Local && MCELF::GetBinding(*it) == ELF::STB_LOCAL) {
565 MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
566 MCELF::SetBinding(*it, ELF::STB_GLOBAL);
567 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
570 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
571 MCELF::SetBinding(*it, ELF::STB_WEAK);
573 if (it->isCommon()) {
575 MSD.SectionIndex = ELF::SHN_COMMON;
576 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
577 MSD.SectionIndex = ELF::SHN_ABS;
578 } else if (RefSymbol.isUndefined()) {
579 if (isSignature && !Used)
580 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
582 MSD.SectionIndex = ELF::SHN_UNDEF;
584 const MCSectionELF &Section =
585 static_cast<const MCSectionELF&>(RefSymbol.getSection());
586 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
587 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
588 NeedsSymtabShndx = true;
589 assert(MSD.SectionIndex && "Invalid section index!");
592 // The @@@ in symbol version is replaced with @ in undefined symbols and
593 // @@ in defined ones.
594 StringRef Name = Symbol.getName();
597 size_t Pos = Name.find("@@@");
598 if (Pos != StringRef::npos) {
599 Buf += Name.substr(0, Pos);
600 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
601 Buf += Name.substr(Pos + Skip);
605 uint64_t &Entry = StringIndexMap[Name];
607 Entry = StringTable.size();
609 StringTable += '\x00';
611 MSD.StringIndex = Entry;
612 if (MSD.SectionIndex == ELF::SHN_UNDEF)
613 UndefinedSymbolData.push_back(MSD);
615 LocalSymbolData.push_back(MSD);
617 ExternalSymbolData.push_back(MSD);
620 // Symbols are required to be in lexicographic order.
621 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
622 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
623 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
625 // Set the symbol indices. Local symbols must come before all other
626 // symbols with non-local bindings.
628 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
629 LocalSymbolData[i].SymbolData->setIndex(Index++);
631 Index += NumRegularSections;
633 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
634 ExternalSymbolData[i].SymbolData->setIndex(Index++);
635 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
636 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
639 void ELFObjectWriter::WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
640 const MCSectionData &SD) {
641 if (!Relocations[&SD].empty()) {
642 MCContext &Ctx = Asm.getContext();
643 const MCSectionELF *RelaSection;
644 const MCSectionELF &Section =
645 static_cast<const MCSectionELF&>(SD.getSection());
647 const StringRef SectionName = Section.getSectionName();
648 std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
649 RelaSectionName += SectionName;
652 if (hasRelocationAddend())
653 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
655 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
657 RelaSection = Ctx.getELFSection(RelaSectionName, hasRelocationAddend() ?
658 ELF::SHT_RELA : ELF::SHT_REL, 0,
659 SectionKind::getReadOnly(),
662 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
663 RelaSD.setAlignment(is64Bit() ? 8 : 4);
665 MCDataFragment *F = new MCDataFragment(&RelaSD);
667 WriteRelocationsFragment(Asm, F, &SD);
671 void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
672 uint64_t Flags, uint64_t Address,
673 uint64_t Offset, uint64_t Size,
674 uint32_t Link, uint32_t Info,
676 uint64_t EntrySize) {
677 Write32(Name); // sh_name: index into string table
678 Write32(Type); // sh_type
679 WriteWord(Flags); // sh_flags
680 WriteWord(Address); // sh_addr
681 WriteWord(Offset); // sh_offset
682 WriteWord(Size); // sh_size
683 Write32(Link); // sh_link
684 Write32(Info); // sh_info
685 WriteWord(Alignment); // sh_addralign
686 WriteWord(EntrySize); // sh_entsize
689 void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
691 const MCSectionData *SD) {
692 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
693 // sort by the r_offset just like gnu as does
694 array_pod_sort(Relocs.begin(), Relocs.end());
696 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
697 ELFRelocationEntry entry = Relocs[e - i - 1];
701 else if (entry.Index < 0)
702 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
704 entry.Index += LocalSymbolData.size();
706 String64(*F, entry.r_offset);
708 struct ELF::Elf64_Rela ERE64;
709 ERE64.setSymbolAndType(entry.Index, entry.Type);
710 String64(*F, ERE64.r_info);
712 if (hasRelocationAddend())
713 String64(*F, entry.r_addend);
715 String32(*F, entry.r_offset);
717 struct ELF::Elf32_Rela ERE32;
718 ERE32.setSymbolAndType(entry.Index, entry.Type);
719 String32(*F, ERE32.r_info);
721 if (hasRelocationAddend())
722 String32(*F, entry.r_addend);
727 void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
729 const SectionIndexMapTy &SectionIndexMap) {
730 MCContext &Ctx = Asm.getContext();
733 unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
735 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
736 const MCSectionELF *ShstrtabSection =
737 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
738 SectionKind::getReadOnly());
739 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
740 ShstrtabSD.setAlignment(1);
741 ShstrtabIndex = Asm.size();
743 const MCSectionELF *SymtabSection =
744 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
745 SectionKind::getReadOnly(),
747 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
748 SymtabSD.setAlignment(is64Bit() ? 8 : 4);
749 SymbolTableIndex = Asm.size();
751 MCSectionData *SymtabShndxSD = NULL;
753 if (NeedsSymtabShndx) {
754 const MCSectionELF *SymtabShndxSection =
755 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
756 SectionKind::getReadOnly(), 4, "");
757 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
758 SymtabShndxSD->setAlignment(4);
761 const MCSection *StrtabSection;
762 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
763 SectionKind::getReadOnly());
764 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
765 StrtabSD.setAlignment(1);
766 StringTableIndex = Asm.size();
768 WriteRelocations(Asm, Layout);
771 F = new MCDataFragment(&SymtabSD);
772 MCDataFragment *ShndxF = NULL;
773 if (NeedsSymtabShndx) {
774 ShndxF = new MCDataFragment(SymtabShndxSD);
776 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
778 F = new MCDataFragment(&StrtabSD);
779 F->getContents().append(StringTable.begin(), StringTable.end());
781 F = new MCDataFragment(&ShstrtabSD);
783 // Section header string table.
785 // The first entry of a string table holds a null character so skip
788 F->getContents() += '\x00';
790 StringMap<uint64_t> SecStringMap;
791 for (MCAssembler::const_iterator it = Asm.begin(),
792 ie = Asm.end(); it != ie; ++it) {
793 const MCSectionELF &Section =
794 static_cast<const MCSectionELF&>(it->getSection());
795 // FIXME: We could merge suffixes like in .text and .rela.text.
797 StringRef Name = Section.getSectionName();
798 if (SecStringMap.count(Name)) {
799 SectionStringTableIndex[&Section] = SecStringMap[Name];
802 // Remember the index into the string table so we can write it
803 // into the sh_name field of the section header table.
804 SectionStringTableIndex[&Section] = Index;
805 SecStringMap[Name] = Index;
807 Index += Name.size() + 1;
808 F->getContents() += Name;
809 F->getContents() += '\x00';
813 void ELFObjectWriter::CreateIndexedSections(MCAssembler &Asm,
815 GroupMapTy &GroupMap,
816 RevGroupMapTy &RevGroupMap) {
817 // Create the .note.GNU-stack section if needed.
818 MCContext &Ctx = Asm.getContext();
819 if (Asm.getNoExecStack()) {
820 const MCSectionELF *GnuStackSection =
821 Ctx.getELFSection(".note.GNU-stack", ELF::SHT_PROGBITS, 0,
822 SectionKind::getReadOnly());
823 Asm.getOrCreateSectionData(*GnuStackSection);
827 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
829 const MCSectionELF &Section =
830 static_cast<const MCSectionELF&>(it->getSection());
831 if (!(Section.getFlags() & ELF::SHF_GROUP))
834 const MCSymbol *SignatureSymbol = Section.getGroup();
835 Asm.getOrCreateSymbolData(*SignatureSymbol);
836 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
838 Group = Ctx.CreateELFGroupSection();
839 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
840 Data.setAlignment(4);
841 MCDataFragment *F = new MCDataFragment(&Data);
842 String32(*F, ELF::GRP_COMDAT);
844 GroupMap[Group] = SignatureSymbol;
847 // Add sections to the groups
849 unsigned NumGroups = RevGroupMap.size();
850 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
851 it != ie; ++it, ++Index) {
852 const MCSectionELF &Section =
853 static_cast<const MCSectionELF&>(it->getSection());
854 if (!(Section.getFlags() & ELF::SHF_GROUP))
856 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
857 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
858 // FIXME: we could use the previous fragment
859 MCDataFragment *F = new MCDataFragment(&Data);
860 String32(*F, NumGroups + Index);
864 void ELFObjectWriter::WriteSection(MCAssembler &Asm,
865 const SectionIndexMapTy &SectionIndexMap,
866 uint32_t GroupSymbolIndex,
867 uint64_t Offset, uint64_t Size,
869 const MCSectionELF &Section) {
870 uint64_t sh_link = 0;
871 uint64_t sh_info = 0;
873 switch(Section.getType()) {
874 case ELF::SHT_DYNAMIC:
875 sh_link = SectionStringTableIndex[&Section];
880 case ELF::SHT_RELA: {
881 const MCSectionELF *SymtabSection;
882 const MCSectionELF *InfoSection;
883 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
885 SectionKind::getReadOnly());
886 sh_link = SectionIndexMap.lookup(SymtabSection);
887 assert(sh_link && ".symtab not found");
889 // Remove ".rel" and ".rela" prefixes.
890 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
891 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
893 InfoSection = Asm.getContext().getELFSection(SectionName,
894 ELF::SHT_PROGBITS, 0,
895 SectionKind::getReadOnly());
896 sh_info = SectionIndexMap.lookup(InfoSection);
900 case ELF::SHT_SYMTAB:
901 case ELF::SHT_DYNSYM:
902 sh_link = StringTableIndex;
903 sh_info = LastLocalSymbolIndex;
906 case ELF::SHT_SYMTAB_SHNDX:
907 sh_link = SymbolTableIndex;
910 case ELF::SHT_PROGBITS:
911 case ELF::SHT_STRTAB:
912 case ELF::SHT_NOBITS:
915 case ELF::SHT_ARM_ATTRIBUTES:
916 case ELF::SHT_INIT_ARRAY:
917 case ELF::SHT_FINI_ARRAY:
918 case ELF::SHT_PREINIT_ARRAY:
919 case ELF::SHT_X86_64_UNWIND:
923 case ELF::SHT_GROUP: {
924 sh_link = SymbolTableIndex;
925 sh_info = GroupSymbolIndex;
930 assert(0 && "FIXME: sh_type value not supported!");
934 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
935 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
936 Alignment, Section.getEntrySize());
939 bool ELFObjectWriter::IsELFMetaDataSection(const MCSectionData &SD) {
940 return SD.getOrdinal() == ~UINT32_C(0) &&
941 !SD.getSection().isVirtualSection();
944 uint64_t ELFObjectWriter::DataSectionSize(const MCSectionData &SD) {
946 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
948 const MCFragment &F = *i;
949 assert(F.getKind() == MCFragment::FT_Data);
950 Ret += cast<MCDataFragment>(F).getContents().size();
955 uint64_t ELFObjectWriter::GetSectionFileSize(const MCAsmLayout &Layout,
956 const MCSectionData &SD) {
957 if (IsELFMetaDataSection(SD))
958 return DataSectionSize(SD);
959 return Layout.getSectionFileSize(&SD);
962 uint64_t ELFObjectWriter::GetSectionAddressSize(const MCAsmLayout &Layout,
963 const MCSectionData &SD) {
964 if (IsELFMetaDataSection(SD))
965 return DataSectionSize(SD);
966 return Layout.getSectionAddressSize(&SD);
969 void ELFObjectWriter::WriteDataSectionData(ELFObjectWriter *W,
970 const MCSectionData &SD) {
971 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
973 const MCFragment &F = *i;
974 assert(F.getKind() == MCFragment::FT_Data);
975 W->WriteBytes(cast<MCDataFragment>(F).getContents().str());
979 void ELFObjectWriter::WriteObject(MCAssembler &Asm,
980 const MCAsmLayout &Layout) {
982 RevGroupMapTy RevGroupMap;
983 CreateIndexedSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
986 SectionIndexMapTy SectionIndexMap;
988 ComputeIndexMap(Asm, SectionIndexMap);
990 // Compute symbol table information.
991 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap);
993 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
994 const_cast<MCAsmLayout&>(Layout),
997 // Update to include the metadata sections.
998 ComputeIndexMap(Asm, SectionIndexMap);
1000 // Add 1 for the null section.
1001 unsigned NumSections = Asm.size() + 1;
1002 uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
1003 uint64_t HeaderSize = is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
1004 sizeof(ELF::Elf32_Ehdr);
1005 uint64_t FileOff = HeaderSize;
1007 std::vector<const MCSectionELF*> Sections;
1008 Sections.resize(NumSections);
1010 for (SectionIndexMapTy::const_iterator i=
1011 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1012 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
1013 Sections[p.second] = p.first;
1016 for (unsigned i = 1; i < NumSections; ++i) {
1017 const MCSectionELF &Section = *Sections[i];
1018 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1020 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1022 // Get the size of the section in the output file (including padding).
1023 FileOff += GetSectionFileSize(Layout, SD);
1026 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1028 // Write out the ELF header ...
1029 WriteHeader(FileOff - HeaderSize, NumSections);
1031 FileOff = HeaderSize;
1033 // ... then all of the sections ...
1034 DenseMap<const MCSection*, uint64_t> SectionOffsetMap;
1036 for (unsigned i = 1; i < NumSections; ++i) {
1037 const MCSectionELF &Section = *Sections[i];
1038 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1040 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1041 WriteZeros(Padding);
1044 // Remember the offset into the file for this section.
1045 SectionOffsetMap[&Section] = FileOff;
1047 FileOff += GetSectionFileSize(Layout, SD);
1049 if (IsELFMetaDataSection(SD))
1050 WriteDataSectionData(this, SD);
1052 Asm.WriteSectionData(&SD, Layout);
1055 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1056 WriteZeros(Padding);
1059 // ... and then the section header table.
1060 // Should we align the section header table?
1062 // Null section first.
1063 uint64_t FirstSectionSize =
1064 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1065 uint32_t FirstSectionLink =
1066 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1067 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
1069 for (unsigned i = 1; i < NumSections; ++i) {
1070 const MCSectionELF &Section = *Sections[i];
1071 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1072 uint32_t GroupSymbolIndex;
1073 if (Section.getType() != ELF::SHT_GROUP)
1074 GroupSymbolIndex = 0;
1076 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm, GroupMap[&Section]);
1078 uint64_t Size = GetSectionAddressSize(Layout, SD);
1080 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
1081 SectionOffsetMap[&Section], Size,
1082 SD.getAlignment(), Section);
1087 ELFObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
1088 const MCSymbolData &DataA,
1089 const MCFragment &FB,
1091 bool IsPCRel) const {
1092 if (DataA.getFlags() & ELF_STB_Weak)
1094 return MCObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(
1095 Asm, DataA, FB,InSet, IsPCRel);
1098 MCObjectWriter *llvm::createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1100 bool IsLittleEndian) {
1101 switch (MOTW->getEMachine()) {
1103 case ELF::EM_X86_64:
1104 return new X86ELFObjectWriter(MOTW, OS, IsLittleEndian); break;
1106 return new ARMELFObjectWriter(MOTW, OS, IsLittleEndian); break;
1107 case ELF::EM_MBLAZE:
1108 return new MBlazeELFObjectWriter(MOTW, OS, IsLittleEndian); break;
1109 default: llvm_unreachable("Unsupported architecture"); break;
1114 /// START OF SUBCLASSES for ELFObjectWriter
1115 //===- ARMELFObjectWriter -------------------------------------------===//
1117 ARMELFObjectWriter::ARMELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1119 bool IsLittleEndian)
1120 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
1123 ARMELFObjectWriter::~ARMELFObjectWriter()
1126 // FIXME: get the real EABI Version from the Triple.
1127 void ARMELFObjectWriter::WriteEFlags() {
1128 Write32(ELF::EF_ARM_EABIMASK & DefaultEABIVersion);
1131 // In ARM, _MergedGlobals and other most symbols get emitted directly.
1132 // I.e. not as an offset to a section symbol.
1133 // This code is a first-cut approximation of what ARM/gcc does.
1135 const MCSymbol *ARMELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
1136 const MCValue &Target,
1137 const MCFragment &F,
1139 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
1140 bool EmitThisSym = false;
1143 EmitThisSym = StringSwitch<bool>(Symbol.getName())
1144 .Case("_MergedGlobals", true)
1147 EmitThisSym = StringSwitch<bool>(Symbol.getName())
1148 .Case("_MergedGlobals", true)
1149 .StartsWith(".L.str", true)
1154 if (! Symbol.isTemporary())
1159 unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target,
1160 const MCFixup &Fixup,
1162 bool IsRelocWithSymbol,
1164 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1165 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
1169 switch ((unsigned)Fixup.getKind()) {
1170 default: assert(0 && "Unimplemented");
1173 default: llvm_unreachable("Unsupported Modifier");
1174 case MCSymbolRefExpr::VK_None:
1175 Type = ELF::R_ARM_BASE_PREL;
1177 case MCSymbolRefExpr::VK_ARM_TLSGD:
1178 assert(0 && "unimplemented");
1180 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
1181 Type = ELF::R_ARM_TLS_IE32;
1185 case ARM::fixup_arm_uncondbranch:
1187 case MCSymbolRefExpr::VK_ARM_PLT:
1188 Type = ELF::R_ARM_PLT32;
1191 Type = ELF::R_ARM_CALL;
1195 case ARM::fixup_arm_condbranch:
1196 Type = ELF::R_ARM_JUMP24;
1198 case ARM::fixup_arm_movt_hi16:
1199 case ARM::fixup_arm_movt_hi16_pcrel:
1200 Type = ELF::R_ARM_MOVT_PREL;
1202 case ARM::fixup_arm_movw_lo16:
1203 case ARM::fixup_arm_movw_lo16_pcrel:
1204 Type = ELF::R_ARM_MOVW_PREL_NC;
1206 case ARM::fixup_t2_movt_hi16:
1207 case ARM::fixup_t2_movt_hi16_pcrel:
1208 Type = ELF::R_ARM_THM_MOVT_PREL;
1210 case ARM::fixup_t2_movw_lo16:
1211 case ARM::fixup_t2_movw_lo16_pcrel:
1212 Type = ELF::R_ARM_THM_MOVW_PREL_NC;
1216 switch ((unsigned)Fixup.getKind()) {
1217 default: llvm_unreachable("invalid fixup kind!");
1220 default: llvm_unreachable("Unsupported Modifier"); break;
1221 case MCSymbolRefExpr::VK_ARM_GOT:
1222 Type = ELF::R_ARM_GOT_BREL;
1224 case MCSymbolRefExpr::VK_ARM_TLSGD:
1225 Type = ELF::R_ARM_TLS_GD32;
1227 case MCSymbolRefExpr::VK_ARM_TPOFF:
1228 Type = ELF::R_ARM_TLS_LE32;
1230 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
1231 Type = ELF::R_ARM_TLS_IE32;
1233 case MCSymbolRefExpr::VK_None:
1234 Type = ELF::R_ARM_ABS32;
1236 case MCSymbolRefExpr::VK_ARM_GOTOFF:
1237 Type = ELF::R_ARM_GOTOFF32;
1241 case ARM::fixup_arm_ldst_pcrel_12:
1242 case ARM::fixup_arm_pcrel_10:
1243 case ARM::fixup_arm_adr_pcrel_12:
1244 case ARM::fixup_arm_thumb_bl:
1245 case ARM::fixup_arm_thumb_cb:
1246 case ARM::fixup_arm_thumb_cp:
1247 case ARM::fixup_arm_thumb_br:
1248 assert(0 && "Unimplemented");
1250 case ARM::fixup_arm_uncondbranch:
1251 Type = ELF::R_ARM_CALL;
1253 case ARM::fixup_arm_condbranch:
1254 Type = ELF::R_ARM_JUMP24;
1256 case ARM::fixup_arm_movt_hi16:
1257 Type = ELF::R_ARM_MOVT_ABS;
1259 case ARM::fixup_arm_movw_lo16:
1260 Type = ELF::R_ARM_MOVW_ABS_NC;
1262 case ARM::fixup_t2_movt_hi16:
1263 Type = ELF::R_ARM_THM_MOVT_ABS;
1265 case ARM::fixup_t2_movw_lo16:
1266 Type = ELF::R_ARM_THM_MOVW_ABS_NC;
1271 if (RelocNeedsGOT(Modifier))
1277 //===- MBlazeELFObjectWriter -------------------------------------------===//
1279 MBlazeELFObjectWriter::MBlazeELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1281 bool IsLittleEndian)
1282 : ELFObjectWriter(MOTW, _OS, IsLittleEndian) {
1285 MBlazeELFObjectWriter::~MBlazeELFObjectWriter() {
1288 unsigned MBlazeELFObjectWriter::GetRelocType(const MCValue &Target,
1289 const MCFixup &Fixup,
1291 bool IsRelocWithSymbol,
1293 // determine the type of the relocation
1296 switch ((unsigned)Fixup.getKind()) {
1298 llvm_unreachable("Unimplemented");
1300 Type = ELF::R_MICROBLAZE_64_PCREL;
1303 Type = ELF::R_MICROBLAZE_32_PCREL;
1307 switch ((unsigned)Fixup.getKind()) {
1308 default: llvm_unreachable("invalid fixup kind!");
1310 Type = ((IsRelocWithSymbol || Addend !=0)
1311 ? ELF::R_MICROBLAZE_32
1312 : ELF::R_MICROBLAZE_64);
1315 Type = ELF::R_MICROBLAZE_32;
1322 //===- X86ELFObjectWriter -------------------------------------------===//
1325 X86ELFObjectWriter::X86ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1327 bool IsLittleEndian)
1328 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
1331 X86ELFObjectWriter::~X86ELFObjectWriter()
1334 unsigned X86ELFObjectWriter::GetRelocType(const MCValue &Target,
1335 const MCFixup &Fixup,
1337 bool IsRelocWithSymbol,
1339 // determine the type of the relocation
1341 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1342 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
1346 switch ((unsigned)Fixup.getKind()) {
1347 default: llvm_unreachable("invalid fixup kind!");
1349 assert(Modifier == MCSymbolRefExpr::VK_None);
1350 Type = ELF::R_X86_64_PC64;
1352 case X86::reloc_signed_4byte:
1353 case X86::reloc_riprel_4byte_movq_load:
1354 case FK_Data_4: // FIXME?
1355 case X86::reloc_riprel_4byte:
1359 llvm_unreachable("Unimplemented");
1360 case MCSymbolRefExpr::VK_None:
1361 Type = ELF::R_X86_64_PC32;
1363 case MCSymbolRefExpr::VK_PLT:
1364 Type = ELF::R_X86_64_PLT32;
1366 case MCSymbolRefExpr::VK_GOTPCREL:
1367 Type = ELF::R_X86_64_GOTPCREL;
1369 case MCSymbolRefExpr::VK_GOTTPOFF:
1370 Type = ELF::R_X86_64_GOTTPOFF;
1372 case MCSymbolRefExpr::VK_TLSGD:
1373 Type = ELF::R_X86_64_TLSGD;
1375 case MCSymbolRefExpr::VK_TLSLD:
1376 Type = ELF::R_X86_64_TLSLD;
1381 assert(Modifier == MCSymbolRefExpr::VK_None);
1382 Type = ELF::R_X86_64_PC16;
1385 assert(Modifier == MCSymbolRefExpr::VK_None);
1386 Type = ELF::R_X86_64_PC8;
1390 switch ((unsigned)Fixup.getKind()) {
1391 default: llvm_unreachable("invalid fixup kind!");
1392 case FK_Data_8: Type = ELF::R_X86_64_64; break;
1393 case X86::reloc_signed_4byte:
1394 assert(isInt<32>(Target.getConstant()));
1397 llvm_unreachable("Unimplemented");
1398 case MCSymbolRefExpr::VK_None:
1399 Type = ELF::R_X86_64_32S;
1401 case MCSymbolRefExpr::VK_GOT:
1402 Type = ELF::R_X86_64_GOT32;
1404 case MCSymbolRefExpr::VK_GOTPCREL:
1405 Type = ELF::R_X86_64_GOTPCREL;
1407 case MCSymbolRefExpr::VK_TPOFF:
1408 Type = ELF::R_X86_64_TPOFF32;
1410 case MCSymbolRefExpr::VK_DTPOFF:
1411 Type = ELF::R_X86_64_DTPOFF32;
1416 Type = ELF::R_X86_64_32;
1418 case FK_Data_2: Type = ELF::R_X86_64_16; break;
1420 case FK_Data_1: Type = ELF::R_X86_64_8; break;
1427 llvm_unreachable("Unimplemented");
1428 case MCSymbolRefExpr::VK_None:
1429 Type = ELF::R_386_PC32;
1431 case MCSymbolRefExpr::VK_PLT:
1432 Type = ELF::R_386_PLT32;
1436 switch ((unsigned)Fixup.getKind()) {
1437 default: llvm_unreachable("invalid fixup kind!");
1439 case X86::reloc_global_offset_table:
1440 Type = ELF::R_386_GOTPC;
1443 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
1445 case X86::reloc_signed_4byte:
1450 llvm_unreachable("Unimplemented");
1451 case MCSymbolRefExpr::VK_None:
1452 Type = ELF::R_386_32;
1454 case MCSymbolRefExpr::VK_GOT:
1455 Type = ELF::R_386_GOT32;
1457 case MCSymbolRefExpr::VK_GOTOFF:
1458 Type = ELF::R_386_GOTOFF;
1460 case MCSymbolRefExpr::VK_TLSGD:
1461 Type = ELF::R_386_TLS_GD;
1463 case MCSymbolRefExpr::VK_TPOFF:
1464 Type = ELF::R_386_TLS_LE_32;
1466 case MCSymbolRefExpr::VK_INDNTPOFF:
1467 Type = ELF::R_386_TLS_IE;
1469 case MCSymbolRefExpr::VK_NTPOFF:
1470 Type = ELF::R_386_TLS_LE;
1472 case MCSymbolRefExpr::VK_GOTNTPOFF:
1473 Type = ELF::R_386_TLS_GOTIE;
1475 case MCSymbolRefExpr::VK_TLSLDM:
1476 Type = ELF::R_386_TLS_LDM;
1478 case MCSymbolRefExpr::VK_DTPOFF:
1479 Type = ELF::R_386_TLS_LDO_32;
1483 case FK_Data_2: Type = ELF::R_386_16; break;
1485 case FK_Data_1: Type = ELF::R_386_8; break;
1490 if (RelocNeedsGOT(Modifier))