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,
504 const RelMapTy &RelMap) {
506 for (MCAssembler::iterator it = Asm.begin(),
507 ie = Asm.end(); it != ie; ++it) {
508 const MCSectionELF &Section =
509 static_cast<const MCSectionELF &>(it->getSection());
510 if (Section.getType() != ELF::SHT_GROUP)
512 SectionIndexMap[&Section] = Index++;
515 for (MCAssembler::iterator it = Asm.begin(),
516 ie = Asm.end(); it != ie; ++it) {
517 const MCSectionELF &Section =
518 static_cast<const MCSectionELF &>(it->getSection());
519 if (Section.getType() == ELF::SHT_GROUP ||
520 Section.getType() == ELF::SHT_REL ||
521 Section.getType() == ELF::SHT_RELA)
523 SectionIndexMap[&Section] = Index++;
524 const MCSectionELF *RelSection = RelMap.lookup(&Section);
526 SectionIndexMap[RelSection] = Index++;
530 void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
531 const SectionIndexMapTy &SectionIndexMap,
532 RevGroupMapTy RevGroupMap,
533 unsigned NumRegularSections) {
534 // FIXME: Is this the correct place to do this?
536 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
537 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
538 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
539 Data.setExternal(true);
540 MCELF::SetBinding(Data, ELF::STB_GLOBAL);
543 // Index 0 is always the empty string.
544 StringMap<uint64_t> StringIndexMap;
545 StringTable += '\x00';
547 // Add the data for the symbols.
548 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
549 ie = Asm.symbol_end(); it != ie; ++it) {
550 const MCSymbol &Symbol = it->getSymbol();
552 bool Used = UsedInReloc.count(&Symbol);
553 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
554 bool isSignature = RevGroupMap.count(&Symbol);
556 if (!isInSymtab(Asm, *it,
557 Used || WeakrefUsed || isSignature,
558 Renames.count(&Symbol)))
563 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
565 // Undefined symbols are global, but this is the first place we
566 // are able to set it.
567 bool Local = isLocal(*it, isSignature, Used);
568 if (!Local && MCELF::GetBinding(*it) == ELF::STB_LOCAL) {
569 MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
570 MCELF::SetBinding(*it, ELF::STB_GLOBAL);
571 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
574 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
575 MCELF::SetBinding(*it, ELF::STB_WEAK);
577 if (it->isCommon()) {
579 MSD.SectionIndex = ELF::SHN_COMMON;
580 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
581 MSD.SectionIndex = ELF::SHN_ABS;
582 } else if (RefSymbol.isUndefined()) {
583 if (isSignature && !Used)
584 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
586 MSD.SectionIndex = ELF::SHN_UNDEF;
588 const MCSectionELF &Section =
589 static_cast<const MCSectionELF&>(RefSymbol.getSection());
590 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
591 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
592 NeedsSymtabShndx = true;
593 assert(MSD.SectionIndex && "Invalid section index!");
596 // The @@@ in symbol version is replaced with @ in undefined symbols and
597 // @@ in defined ones.
598 StringRef Name = Symbol.getName();
601 size_t Pos = Name.find("@@@");
602 if (Pos != StringRef::npos) {
603 Buf += Name.substr(0, Pos);
604 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
605 Buf += Name.substr(Pos + Skip);
609 uint64_t &Entry = StringIndexMap[Name];
611 Entry = StringTable.size();
613 StringTable += '\x00';
615 MSD.StringIndex = Entry;
616 if (MSD.SectionIndex == ELF::SHN_UNDEF)
617 UndefinedSymbolData.push_back(MSD);
619 LocalSymbolData.push_back(MSD);
621 ExternalSymbolData.push_back(MSD);
624 // Symbols are required to be in lexicographic order.
625 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
626 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
627 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
629 // Set the symbol indices. Local symbols must come before all other
630 // symbols with non-local bindings.
632 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
633 LocalSymbolData[i].SymbolData->setIndex(Index++);
635 Index += NumRegularSections;
637 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
638 ExternalSymbolData[i].SymbolData->setIndex(Index++);
639 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
640 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
643 void ELFObjectWriter::CreateRelocationSections(MCAssembler &Asm,
646 for (MCAssembler::const_iterator it = Asm.begin(),
647 ie = Asm.end(); it != ie; ++it) {
648 const MCSectionData &SD = *it;
649 if (Relocations[&SD].empty())
652 MCContext &Ctx = Asm.getContext();
653 const MCSectionELF &Section =
654 static_cast<const MCSectionELF&>(SD.getSection());
656 const StringRef SectionName = Section.getSectionName();
657 std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
658 RelaSectionName += SectionName;
661 if (hasRelocationAddend())
662 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
664 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
666 const MCSectionELF *RelaSection =
667 Ctx.getELFSection(RelaSectionName, hasRelocationAddend() ?
668 ELF::SHT_RELA : ELF::SHT_REL, 0,
669 SectionKind::getReadOnly(),
671 RelMap[&Section] = RelaSection;
672 Asm.getOrCreateSectionData(*RelaSection);
676 void ELFObjectWriter::WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout,
677 const RelMapTy &RelMap) {
678 for (MCAssembler::const_iterator it = Asm.begin(),
679 ie = Asm.end(); it != ie; ++it) {
680 const MCSectionData &SD = *it;
681 const MCSectionELF &Section =
682 static_cast<const MCSectionELF&>(SD.getSection());
684 const MCSectionELF *RelaSection = RelMap.lookup(&Section);
687 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
688 RelaSD.setAlignment(is64Bit() ? 8 : 4);
690 MCDataFragment *F = new MCDataFragment(&RelaSD);
691 WriteRelocationsFragment(Asm, F, &*it);
695 void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
696 uint64_t Flags, uint64_t Address,
697 uint64_t Offset, uint64_t Size,
698 uint32_t Link, uint32_t Info,
700 uint64_t EntrySize) {
701 Write32(Name); // sh_name: index into string table
702 Write32(Type); // sh_type
703 WriteWord(Flags); // sh_flags
704 WriteWord(Address); // sh_addr
705 WriteWord(Offset); // sh_offset
706 WriteWord(Size); // sh_size
707 Write32(Link); // sh_link
708 Write32(Info); // sh_info
709 WriteWord(Alignment); // sh_addralign
710 WriteWord(EntrySize); // sh_entsize
713 void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
715 const MCSectionData *SD) {
716 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
717 // sort by the r_offset just like gnu as does
718 array_pod_sort(Relocs.begin(), Relocs.end());
720 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
721 ELFRelocationEntry entry = Relocs[e - i - 1];
725 else if (entry.Index < 0)
726 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
728 entry.Index += LocalSymbolData.size();
730 String64(*F, entry.r_offset);
732 struct ELF::Elf64_Rela ERE64;
733 ERE64.setSymbolAndType(entry.Index, entry.Type);
734 String64(*F, ERE64.r_info);
736 if (hasRelocationAddend())
737 String64(*F, entry.r_addend);
739 String32(*F, entry.r_offset);
741 struct ELF::Elf32_Rela ERE32;
742 ERE32.setSymbolAndType(entry.Index, entry.Type);
743 String32(*F, ERE32.r_info);
745 if (hasRelocationAddend())
746 String32(*F, entry.r_addend);
751 void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
753 SectionIndexMapTy &SectionIndexMap,
754 const RelMapTy &RelMap) {
755 MCContext &Ctx = Asm.getContext();
758 unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
760 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
761 const MCSectionELF *ShstrtabSection =
762 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
763 SectionKind::getReadOnly());
764 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
765 ShstrtabSD.setAlignment(1);
767 const MCSectionELF *SymtabSection =
768 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
769 SectionKind::getReadOnly(),
771 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
772 SymtabSD.setAlignment(is64Bit() ? 8 : 4);
774 MCSectionData *SymtabShndxSD = NULL;
776 if (NeedsSymtabShndx) {
777 const MCSectionELF *SymtabShndxSection =
778 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
779 SectionKind::getReadOnly(), 4, "");
780 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
781 SymtabShndxSD->setAlignment(4);
784 const MCSectionELF *StrtabSection;
785 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
786 SectionKind::getReadOnly());
787 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
788 StrtabSD.setAlignment(1);
790 ComputeIndexMap(Asm, SectionIndexMap, RelMap);
792 ShstrtabIndex = SectionIndexMap.lookup(ShstrtabSection);
793 SymbolTableIndex = SectionIndexMap.lookup(SymtabSection);
794 StringTableIndex = SectionIndexMap.lookup(StrtabSection);
797 F = new MCDataFragment(&SymtabSD);
798 MCDataFragment *ShndxF = NULL;
799 if (NeedsSymtabShndx) {
800 ShndxF = new MCDataFragment(SymtabShndxSD);
802 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
804 F = new MCDataFragment(&StrtabSD);
805 F->getContents().append(StringTable.begin(), StringTable.end());
807 F = new MCDataFragment(&ShstrtabSD);
809 // Section header string table.
811 // The first entry of a string table holds a null character so skip
814 F->getContents() += '\x00';
816 StringMap<uint64_t> SecStringMap;
817 for (MCAssembler::const_iterator it = Asm.begin(),
818 ie = Asm.end(); it != ie; ++it) {
819 const MCSectionELF &Section =
820 static_cast<const MCSectionELF&>(it->getSection());
821 // FIXME: We could merge suffixes like in .text and .rela.text.
823 StringRef Name = Section.getSectionName();
824 if (SecStringMap.count(Name)) {
825 SectionStringTableIndex[&Section] = SecStringMap[Name];
828 // Remember the index into the string table so we can write it
829 // into the sh_name field of the section header table.
830 SectionStringTableIndex[&Section] = Index;
831 SecStringMap[Name] = Index;
833 Index += Name.size() + 1;
834 F->getContents() += Name;
835 F->getContents() += '\x00';
839 void ELFObjectWriter::CreateIndexedSections(MCAssembler &Asm,
841 GroupMapTy &GroupMap,
842 RevGroupMapTy &RevGroupMap,
843 SectionIndexMapTy &SectionIndexMap,
844 const RelMapTy &RelMap) {
845 // Create the .note.GNU-stack section if needed.
846 MCContext &Ctx = Asm.getContext();
847 if (Asm.getNoExecStack()) {
848 const MCSectionELF *GnuStackSection =
849 Ctx.getELFSection(".note.GNU-stack", ELF::SHT_PROGBITS, 0,
850 SectionKind::getReadOnly());
851 Asm.getOrCreateSectionData(*GnuStackSection);
855 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
857 const MCSectionELF &Section =
858 static_cast<const MCSectionELF&>(it->getSection());
859 if (!(Section.getFlags() & ELF::SHF_GROUP))
862 const MCSymbol *SignatureSymbol = Section.getGroup();
863 Asm.getOrCreateSymbolData(*SignatureSymbol);
864 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
866 Group = Ctx.CreateELFGroupSection();
867 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
868 Data.setAlignment(4);
869 MCDataFragment *F = new MCDataFragment(&Data);
870 String32(*F, ELF::GRP_COMDAT);
872 GroupMap[Group] = SignatureSymbol;
875 ComputeIndexMap(Asm, SectionIndexMap, RelMap);
877 // Add sections to the groups
878 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
880 const MCSectionELF &Section =
881 static_cast<const MCSectionELF&>(it->getSection());
882 if (!(Section.getFlags() & ELF::SHF_GROUP))
884 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
885 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
886 // FIXME: we could use the previous fragment
887 MCDataFragment *F = new MCDataFragment(&Data);
888 unsigned Index = SectionIndexMap.lookup(&Section);
893 void ELFObjectWriter::WriteSection(MCAssembler &Asm,
894 const SectionIndexMapTy &SectionIndexMap,
895 uint32_t GroupSymbolIndex,
896 uint64_t Offset, uint64_t Size,
898 const MCSectionELF &Section) {
899 uint64_t sh_link = 0;
900 uint64_t sh_info = 0;
902 switch(Section.getType()) {
903 case ELF::SHT_DYNAMIC:
904 sh_link = SectionStringTableIndex[&Section];
909 case ELF::SHT_RELA: {
910 const MCSectionELF *SymtabSection;
911 const MCSectionELF *InfoSection;
912 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
914 SectionKind::getReadOnly());
915 sh_link = SectionIndexMap.lookup(SymtabSection);
916 assert(sh_link && ".symtab not found");
918 // Remove ".rel" and ".rela" prefixes.
919 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
920 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
922 InfoSection = Asm.getContext().getELFSection(SectionName,
923 ELF::SHT_PROGBITS, 0,
924 SectionKind::getReadOnly());
925 sh_info = SectionIndexMap.lookup(InfoSection);
929 case ELF::SHT_SYMTAB:
930 case ELF::SHT_DYNSYM:
931 sh_link = StringTableIndex;
932 sh_info = LastLocalSymbolIndex;
935 case ELF::SHT_SYMTAB_SHNDX:
936 sh_link = SymbolTableIndex;
939 case ELF::SHT_PROGBITS:
940 case ELF::SHT_STRTAB:
941 case ELF::SHT_NOBITS:
944 case ELF::SHT_ARM_ATTRIBUTES:
945 case ELF::SHT_INIT_ARRAY:
946 case ELF::SHT_FINI_ARRAY:
947 case ELF::SHT_PREINIT_ARRAY:
948 case ELF::SHT_X86_64_UNWIND:
952 case ELF::SHT_GROUP: {
953 sh_link = SymbolTableIndex;
954 sh_info = GroupSymbolIndex;
959 assert(0 && "FIXME: sh_type value not supported!");
963 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
964 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
965 Alignment, Section.getEntrySize());
968 bool ELFObjectWriter::IsELFMetaDataSection(const MCSectionData &SD) {
969 return SD.getOrdinal() == ~UINT32_C(0) &&
970 !SD.getSection().isVirtualSection();
973 uint64_t ELFObjectWriter::DataSectionSize(const MCSectionData &SD) {
975 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
977 const MCFragment &F = *i;
978 assert(F.getKind() == MCFragment::FT_Data);
979 Ret += cast<MCDataFragment>(F).getContents().size();
984 uint64_t ELFObjectWriter::GetSectionFileSize(const MCAsmLayout &Layout,
985 const MCSectionData &SD) {
986 if (IsELFMetaDataSection(SD))
987 return DataSectionSize(SD);
988 return Layout.getSectionFileSize(&SD);
991 uint64_t ELFObjectWriter::GetSectionAddressSize(const MCAsmLayout &Layout,
992 const MCSectionData &SD) {
993 if (IsELFMetaDataSection(SD))
994 return DataSectionSize(SD);
995 return Layout.getSectionAddressSize(&SD);
998 void ELFObjectWriter::WriteDataSectionData(MCAssembler &Asm,
999 const MCAsmLayout &Layout,
1000 const MCSectionELF &Section) {
1001 uint64_t FileOff = OS.tell();
1002 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1004 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1005 WriteZeros(Padding);
1008 FileOff += GetSectionFileSize(Layout, SD);
1010 if (IsELFMetaDataSection(SD)) {
1011 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1013 const MCFragment &F = *i;
1014 assert(F.getKind() == MCFragment::FT_Data);
1015 WriteBytes(cast<MCDataFragment>(F).getContents().str());
1018 Asm.WriteSectionData(&SD, Layout);
1022 void ELFObjectWriter::WriteSectionHeader(MCAssembler &Asm,
1023 const GroupMapTy &GroupMap,
1024 const MCAsmLayout &Layout,
1025 const SectionIndexMapTy &SectionIndexMap,
1026 const SectionOffsetMapTy &SectionOffsetMap) {
1027 const unsigned NumSections = Asm.size() + 1;
1029 std::vector<const MCSectionELF*> Sections;
1030 Sections.resize(NumSections - 1);
1032 for (SectionIndexMapTy::const_iterator i=
1033 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1034 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
1035 Sections[p.second - 1] = p.first;
1038 // Null section first.
1039 uint64_t FirstSectionSize =
1040 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1041 uint32_t FirstSectionLink =
1042 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1043 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
1045 for (unsigned i = 0; i < NumSections - 1; ++i) {
1046 const MCSectionELF &Section = *Sections[i];
1047 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1048 uint32_t GroupSymbolIndex;
1049 if (Section.getType() != ELF::SHT_GROUP)
1050 GroupSymbolIndex = 0;
1052 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm,
1053 GroupMap.lookup(&Section));
1055 uint64_t Size = GetSectionAddressSize(Layout, SD);
1057 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
1058 SectionOffsetMap.lookup(&Section), Size,
1059 SD.getAlignment(), Section);
1063 void ELFObjectWriter::ComputeSectionOrder(MCAssembler &Asm,
1064 std::vector<const MCSectionELF*> &Sections) {
1065 for (MCAssembler::iterator it = Asm.begin(),
1066 ie = Asm.end(); it != ie; ++it) {
1067 const MCSectionELF &Section =
1068 static_cast<const MCSectionELF &>(it->getSection());
1069 if (Section.getType() == ELF::SHT_GROUP)
1070 Sections.push_back(&Section);
1073 for (MCAssembler::iterator it = Asm.begin(),
1074 ie = Asm.end(); it != ie; ++it) {
1075 const MCSectionELF &Section =
1076 static_cast<const MCSectionELF &>(it->getSection());
1077 if (Section.getType() != ELF::SHT_GROUP &&
1078 Section.getType() != ELF::SHT_REL &&
1079 Section.getType() != ELF::SHT_RELA)
1080 Sections.push_back(&Section);
1083 for (MCAssembler::iterator it = Asm.begin(),
1084 ie = Asm.end(); it != ie; ++it) {
1085 const MCSectionELF &Section =
1086 static_cast<const MCSectionELF &>(it->getSection());
1087 if (Section.getType() == ELF::SHT_REL ||
1088 Section.getType() == ELF::SHT_RELA)
1089 Sections.push_back(&Section);
1093 void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1094 const MCAsmLayout &Layout) {
1095 GroupMapTy GroupMap;
1096 RevGroupMapTy RevGroupMap;
1097 SectionIndexMapTy SectionIndexMap;
1099 unsigned NumUserSections = Asm.size();
1101 DenseMap<const MCSectionELF*, const MCSectionELF*> RelMap;
1102 CreateRelocationSections(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1104 const unsigned NumUserAndRelocSections = Asm.size();
1105 CreateIndexedSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1106 RevGroupMap, SectionIndexMap, RelMap);
1107 const unsigned AllSections = Asm.size();
1108 const unsigned NumIndexedSections = AllSections - NumUserAndRelocSections;
1110 unsigned NumRegularSections = NumUserSections + NumIndexedSections;
1112 // Compute symbol table information.
1113 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap, NumRegularSections);
1116 WriteRelocations(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1118 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
1119 const_cast<MCAsmLayout&>(Layout),
1123 uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
1124 uint64_t HeaderSize = is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
1125 sizeof(ELF::Elf32_Ehdr);
1126 uint64_t FileOff = HeaderSize;
1128 std::vector<const MCSectionELF*> Sections;
1129 ComputeSectionOrder(Asm, Sections);
1130 unsigned NumSections = Sections.size();
1131 SectionOffsetMapTy SectionOffsetMap;
1132 for (unsigned i = 0; i < NumRegularSections + 1; ++i) {
1133 const MCSectionELF &Section = *Sections[i];
1134 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1136 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1138 // Remember the offset into the file for this section.
1139 SectionOffsetMap[&Section] = FileOff;
1141 // Get the size of the section in the output file (including padding).
1142 FileOff += GetSectionFileSize(Layout, SD);
1145 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1147 const unsigned SectionHeaderOffset = FileOff - HeaderSize;
1149 uint64_t SectionHeaderEntrySize = is64Bit() ?
1150 sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr);
1151 FileOff += (NumSections + 1) * SectionHeaderEntrySize;
1153 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i) {
1154 const MCSectionELF &Section = *Sections[i];
1155 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1157 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1159 // Remember the offset into the file for this section.
1160 SectionOffsetMap[&Section] = FileOff;
1162 // Get the size of the section in the output file (including padding).
1163 FileOff += GetSectionFileSize(Layout, SD);
1166 // Write out the ELF header ...
1167 WriteHeader(SectionHeaderOffset, NumSections + 1);
1169 // ... then the regular sections ...
1170 // + because of .shstrtab
1171 for (unsigned i = 0; i < NumRegularSections + 1; ++i)
1172 WriteDataSectionData(Asm, Layout, *Sections[i]);
1174 FileOff = OS.tell();
1175 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1176 WriteZeros(Padding);
1178 // ... then the section header table ...
1179 WriteSectionHeader(Asm, GroupMap, Layout, SectionIndexMap,
1182 FileOff = OS.tell();
1184 // ... and then the remainting sections ...
1185 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i)
1186 WriteDataSectionData(Asm, Layout, *Sections[i]);
1190 ELFObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
1191 const MCSymbolData &DataA,
1192 const MCFragment &FB,
1194 bool IsPCRel) const {
1195 if (DataA.getFlags() & ELF_STB_Weak)
1197 return MCObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(
1198 Asm, DataA, FB,InSet, IsPCRel);
1201 MCObjectWriter *llvm::createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1203 bool IsLittleEndian) {
1204 switch (MOTW->getEMachine()) {
1206 case ELF::EM_X86_64:
1207 return new X86ELFObjectWriter(MOTW, OS, IsLittleEndian); break;
1209 return new ARMELFObjectWriter(MOTW, OS, IsLittleEndian); break;
1210 case ELF::EM_MBLAZE:
1211 return new MBlazeELFObjectWriter(MOTW, OS, IsLittleEndian); break;
1212 default: llvm_unreachable("Unsupported architecture"); break;
1217 /// START OF SUBCLASSES for ELFObjectWriter
1218 //===- ARMELFObjectWriter -------------------------------------------===//
1220 ARMELFObjectWriter::ARMELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1222 bool IsLittleEndian)
1223 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
1226 ARMELFObjectWriter::~ARMELFObjectWriter()
1229 // FIXME: get the real EABI Version from the Triple.
1230 void ARMELFObjectWriter::WriteEFlags() {
1231 Write32(ELF::EF_ARM_EABIMASK & DefaultEABIVersion);
1234 // In ARM, _MergedGlobals and other most symbols get emitted directly.
1235 // I.e. not as an offset to a section symbol.
1236 // This code is a first-cut approximation of what ARM/gcc does.
1238 const MCSymbol *ARMELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
1239 const MCValue &Target,
1240 const MCFragment &F,
1242 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
1243 bool EmitThisSym = false;
1246 EmitThisSym = StringSwitch<bool>(Symbol.getName())
1247 .Case("_MergedGlobals", true)
1250 EmitThisSym = StringSwitch<bool>(Symbol.getName())
1251 .Case("_MergedGlobals", true)
1252 .StartsWith(".L.str", true)
1257 if (! Symbol.isTemporary())
1262 unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target,
1263 const MCFixup &Fixup,
1265 bool IsRelocWithSymbol,
1267 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1268 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
1272 switch ((unsigned)Fixup.getKind()) {
1273 default: assert(0 && "Unimplemented");
1276 default: llvm_unreachable("Unsupported Modifier");
1277 case MCSymbolRefExpr::VK_None:
1278 Type = ELF::R_ARM_BASE_PREL;
1280 case MCSymbolRefExpr::VK_ARM_TLSGD:
1281 assert(0 && "unimplemented");
1283 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
1284 Type = ELF::R_ARM_TLS_IE32;
1288 case ARM::fixup_arm_uncondbranch:
1290 case MCSymbolRefExpr::VK_ARM_PLT:
1291 Type = ELF::R_ARM_PLT32;
1294 Type = ELF::R_ARM_CALL;
1298 case ARM::fixup_arm_condbranch:
1299 Type = ELF::R_ARM_JUMP24;
1301 case ARM::fixup_arm_movt_hi16:
1302 case ARM::fixup_arm_movt_hi16_pcrel:
1303 Type = ELF::R_ARM_MOVT_PREL;
1305 case ARM::fixup_arm_movw_lo16:
1306 case ARM::fixup_arm_movw_lo16_pcrel:
1307 Type = ELF::R_ARM_MOVW_PREL_NC;
1309 case ARM::fixup_t2_movt_hi16:
1310 case ARM::fixup_t2_movt_hi16_pcrel:
1311 Type = ELF::R_ARM_THM_MOVT_PREL;
1313 case ARM::fixup_t2_movw_lo16:
1314 case ARM::fixup_t2_movw_lo16_pcrel:
1315 Type = ELF::R_ARM_THM_MOVW_PREL_NC;
1319 switch ((unsigned)Fixup.getKind()) {
1320 default: llvm_unreachable("invalid fixup kind!");
1323 default: llvm_unreachable("Unsupported Modifier"); break;
1324 case MCSymbolRefExpr::VK_ARM_GOT:
1325 Type = ELF::R_ARM_GOT_BREL;
1327 case MCSymbolRefExpr::VK_ARM_TLSGD:
1328 Type = ELF::R_ARM_TLS_GD32;
1330 case MCSymbolRefExpr::VK_ARM_TPOFF:
1331 Type = ELF::R_ARM_TLS_LE32;
1333 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
1334 Type = ELF::R_ARM_TLS_IE32;
1336 case MCSymbolRefExpr::VK_None:
1337 Type = ELF::R_ARM_ABS32;
1339 case MCSymbolRefExpr::VK_ARM_GOTOFF:
1340 Type = ELF::R_ARM_GOTOFF32;
1344 case ARM::fixup_arm_ldst_pcrel_12:
1345 case ARM::fixup_arm_pcrel_10:
1346 case ARM::fixup_arm_adr_pcrel_12:
1347 case ARM::fixup_arm_thumb_bl:
1348 case ARM::fixup_arm_thumb_cb:
1349 case ARM::fixup_arm_thumb_cp:
1350 case ARM::fixup_arm_thumb_br:
1351 assert(0 && "Unimplemented");
1353 case ARM::fixup_arm_uncondbranch:
1354 Type = ELF::R_ARM_CALL;
1356 case ARM::fixup_arm_condbranch:
1357 Type = ELF::R_ARM_JUMP24;
1359 case ARM::fixup_arm_movt_hi16:
1360 Type = ELF::R_ARM_MOVT_ABS;
1362 case ARM::fixup_arm_movw_lo16:
1363 Type = ELF::R_ARM_MOVW_ABS_NC;
1365 case ARM::fixup_t2_movt_hi16:
1366 Type = ELF::R_ARM_THM_MOVT_ABS;
1368 case ARM::fixup_t2_movw_lo16:
1369 Type = ELF::R_ARM_THM_MOVW_ABS_NC;
1374 if (RelocNeedsGOT(Modifier))
1380 //===- MBlazeELFObjectWriter -------------------------------------------===//
1382 MBlazeELFObjectWriter::MBlazeELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1384 bool IsLittleEndian)
1385 : ELFObjectWriter(MOTW, _OS, IsLittleEndian) {
1388 MBlazeELFObjectWriter::~MBlazeELFObjectWriter() {
1391 unsigned MBlazeELFObjectWriter::GetRelocType(const MCValue &Target,
1392 const MCFixup &Fixup,
1394 bool IsRelocWithSymbol,
1396 // determine the type of the relocation
1399 switch ((unsigned)Fixup.getKind()) {
1401 llvm_unreachable("Unimplemented");
1403 Type = ELF::R_MICROBLAZE_64_PCREL;
1406 Type = ELF::R_MICROBLAZE_32_PCREL;
1410 switch ((unsigned)Fixup.getKind()) {
1411 default: llvm_unreachable("invalid fixup kind!");
1413 Type = ((IsRelocWithSymbol || Addend !=0)
1414 ? ELF::R_MICROBLAZE_32
1415 : ELF::R_MICROBLAZE_64);
1418 Type = ELF::R_MICROBLAZE_32;
1425 //===- X86ELFObjectWriter -------------------------------------------===//
1428 X86ELFObjectWriter::X86ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1430 bool IsLittleEndian)
1431 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
1434 X86ELFObjectWriter::~X86ELFObjectWriter()
1437 unsigned X86ELFObjectWriter::GetRelocType(const MCValue &Target,
1438 const MCFixup &Fixup,
1440 bool IsRelocWithSymbol,
1442 // determine the type of the relocation
1444 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1445 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
1449 switch ((unsigned)Fixup.getKind()) {
1450 default: llvm_unreachable("invalid fixup kind!");
1452 assert(Modifier == MCSymbolRefExpr::VK_None);
1453 Type = ELF::R_X86_64_PC64;
1455 case X86::reloc_signed_4byte:
1456 case X86::reloc_riprel_4byte_movq_load:
1457 case FK_Data_4: // FIXME?
1458 case X86::reloc_riprel_4byte:
1462 llvm_unreachable("Unimplemented");
1463 case MCSymbolRefExpr::VK_None:
1464 Type = ELF::R_X86_64_PC32;
1466 case MCSymbolRefExpr::VK_PLT:
1467 Type = ELF::R_X86_64_PLT32;
1469 case MCSymbolRefExpr::VK_GOTPCREL:
1470 Type = ELF::R_X86_64_GOTPCREL;
1472 case MCSymbolRefExpr::VK_GOTTPOFF:
1473 Type = ELF::R_X86_64_GOTTPOFF;
1475 case MCSymbolRefExpr::VK_TLSGD:
1476 Type = ELF::R_X86_64_TLSGD;
1478 case MCSymbolRefExpr::VK_TLSLD:
1479 Type = ELF::R_X86_64_TLSLD;
1484 assert(Modifier == MCSymbolRefExpr::VK_None);
1485 Type = ELF::R_X86_64_PC16;
1488 assert(Modifier == MCSymbolRefExpr::VK_None);
1489 Type = ELF::R_X86_64_PC8;
1493 switch ((unsigned)Fixup.getKind()) {
1494 default: llvm_unreachable("invalid fixup kind!");
1495 case FK_Data_8: Type = ELF::R_X86_64_64; break;
1496 case X86::reloc_signed_4byte:
1497 assert(isInt<32>(Target.getConstant()));
1500 llvm_unreachable("Unimplemented");
1501 case MCSymbolRefExpr::VK_None:
1502 Type = ELF::R_X86_64_32S;
1504 case MCSymbolRefExpr::VK_GOT:
1505 Type = ELF::R_X86_64_GOT32;
1507 case MCSymbolRefExpr::VK_GOTPCREL:
1508 Type = ELF::R_X86_64_GOTPCREL;
1510 case MCSymbolRefExpr::VK_TPOFF:
1511 Type = ELF::R_X86_64_TPOFF32;
1513 case MCSymbolRefExpr::VK_DTPOFF:
1514 Type = ELF::R_X86_64_DTPOFF32;
1519 Type = ELF::R_X86_64_32;
1521 case FK_Data_2: Type = ELF::R_X86_64_16; break;
1523 case FK_Data_1: Type = ELF::R_X86_64_8; break;
1530 llvm_unreachable("Unimplemented");
1531 case MCSymbolRefExpr::VK_None:
1532 Type = ELF::R_386_PC32;
1534 case MCSymbolRefExpr::VK_PLT:
1535 Type = ELF::R_386_PLT32;
1539 switch ((unsigned)Fixup.getKind()) {
1540 default: llvm_unreachable("invalid fixup kind!");
1542 case X86::reloc_global_offset_table:
1543 Type = ELF::R_386_GOTPC;
1546 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
1548 case X86::reloc_signed_4byte:
1553 llvm_unreachable("Unimplemented");
1554 case MCSymbolRefExpr::VK_None:
1555 Type = ELF::R_386_32;
1557 case MCSymbolRefExpr::VK_GOT:
1558 Type = ELF::R_386_GOT32;
1560 case MCSymbolRefExpr::VK_GOTOFF:
1561 Type = ELF::R_386_GOTOFF;
1563 case MCSymbolRefExpr::VK_TLSGD:
1564 Type = ELF::R_386_TLS_GD;
1566 case MCSymbolRefExpr::VK_TPOFF:
1567 Type = ELF::R_386_TLS_LE_32;
1569 case MCSymbolRefExpr::VK_INDNTPOFF:
1570 Type = ELF::R_386_TLS_IE;
1572 case MCSymbolRefExpr::VK_NTPOFF:
1573 Type = ELF::R_386_TLS_LE;
1575 case MCSymbolRefExpr::VK_GOTNTPOFF:
1576 Type = ELF::R_386_TLS_GOTIE;
1578 case MCSymbolRefExpr::VK_TLSLDM:
1579 Type = ELF::R_386_TLS_LDM;
1581 case MCSymbolRefExpr::VK_DTPOFF:
1582 Type = ELF::R_386_TLS_LDO_32;
1586 case FK_Data_2: Type = ELF::R_386_16; break;
1588 case FK_Data_1: Type = ELF::R_386_8; break;
1593 if (RelocNeedsGOT(Modifier))