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/StringSwitch.h"
30 #include "../Target/Mips/MCTargetDesc/MipsFixupKinds.h"
31 #include "../Target/PowerPC/MCTargetDesc/PPCFixupKinds.h"
37 #define DEBUG_TYPE "reloc-info"
39 bool ELFObjectWriter::isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) {
40 const MCFixupKindInfo &FKI =
41 Asm.getBackend().getFixupKindInfo((MCFixupKind) Kind);
43 return FKI.Flags & MCFixupKindInfo::FKF_IsPCRel;
46 bool ELFObjectWriter::RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant) {
50 case MCSymbolRefExpr::VK_GOT:
51 case MCSymbolRefExpr::VK_PLT:
52 case MCSymbolRefExpr::VK_GOTPCREL:
53 case MCSymbolRefExpr::VK_GOTOFF:
54 case MCSymbolRefExpr::VK_TPOFF:
55 case MCSymbolRefExpr::VK_TLSGD:
56 case MCSymbolRefExpr::VK_GOTTPOFF:
57 case MCSymbolRefExpr::VK_INDNTPOFF:
58 case MCSymbolRefExpr::VK_NTPOFF:
59 case MCSymbolRefExpr::VK_GOTNTPOFF:
60 case MCSymbolRefExpr::VK_TLSLDM:
61 case MCSymbolRefExpr::VK_DTPOFF:
62 case MCSymbolRefExpr::VK_TLSLD:
67 ELFObjectWriter::~ELFObjectWriter()
70 // Emit the ELF header.
71 void ELFObjectWriter::WriteHeader(uint64_t SectionDataSize,
72 unsigned NumberOfSections) {
78 // emitWord method behaves differently for ELF32 and ELF64, writing
79 // 4 bytes in the former and 8 in the latter.
81 Write8(0x7f); // e_ident[EI_MAG0]
82 Write8('E'); // e_ident[EI_MAG1]
83 Write8('L'); // e_ident[EI_MAG2]
84 Write8('F'); // e_ident[EI_MAG3]
86 Write8(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
89 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
91 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
93 Write8(TargetObjectWriter->getOSABI());
94 Write8(0); // e_ident[EI_ABIVERSION]
96 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
98 Write16(ELF::ET_REL); // e_type
100 Write16(TargetObjectWriter->getEMachine()); // e_machine = target
102 Write32(ELF::EV_CURRENT); // e_version
103 WriteWord(0); // e_entry, no entry point in .o file
104 WriteWord(0); // e_phoff, no program header for .o
105 WriteWord(SectionDataSize + (is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
106 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
108 // e_flags = whatever the target wants
109 Write32(getEFlags());
111 // e_ehsize = ELF header size
112 Write16(is64Bit() ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
114 Write16(0); // e_phentsize = prog header entry size
115 Write16(0); // e_phnum = # prog header entries = 0
117 // e_shentsize = Section header entry size
118 Write16(is64Bit() ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
120 // e_shnum = # of section header ents
121 if (NumberOfSections >= ELF::SHN_LORESERVE)
122 Write16(ELF::SHN_UNDEF);
124 Write16(NumberOfSections);
126 // e_shstrndx = Section # of '.shstrtab'
127 if (ShstrtabIndex >= ELF::SHN_LORESERVE)
128 Write16(ELF::SHN_XINDEX);
130 Write16(ShstrtabIndex);
133 void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
134 MCDataFragment *ShndxF,
136 uint8_t info, uint64_t value,
137 uint64_t size, uint8_t other,
141 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
142 String32(*ShndxF, shndx);
144 String32(*ShndxF, 0);
147 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
148 uint16_t(ELF::SHN_XINDEX) : shndx;
151 String32(*SymtabF, name); // st_name
152 String8(*SymtabF, info); // st_info
153 String8(*SymtabF, other); // st_other
154 String16(*SymtabF, Index); // st_shndx
155 String64(*SymtabF, value); // st_value
156 String64(*SymtabF, size); // st_size
158 String32(*SymtabF, name); // st_name
159 String32(*SymtabF, value); // st_value
160 String32(*SymtabF, size); // st_size
161 String8(*SymtabF, info); // st_info
162 String8(*SymtabF, other); // st_other
163 String16(*SymtabF, Index); // st_shndx
167 uint64_t ELFObjectWriter::SymbolValue(MCSymbolData &Data,
168 const MCAsmLayout &Layout) {
169 if (Data.isCommon() && Data.isExternal())
170 return Data.getCommonAlignment();
172 const MCSymbol &Symbol = Data.getSymbol();
174 if (Symbol.isAbsolute() && Symbol.isVariable()) {
175 if (const MCExpr *Value = Symbol.getVariableValue()) {
177 if (Value->EvaluateAsAbsolute(IntValue, Layout))
178 return (uint64_t)IntValue;
182 if (!Symbol.isInSection())
186 if (Data.getFragment()) {
187 if (Data.getFlags() & ELF_Other_ThumbFunc)
188 return Layout.getSymbolOffset(&Data)+1;
190 return Layout.getSymbolOffset(&Data);
196 void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
197 const MCAsmLayout &Layout) {
198 // The presence of symbol versions causes undefined symbols and
199 // versions declared with @@@ to be renamed.
201 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
202 ie = Asm.symbol_end(); it != ie; ++it) {
203 const MCSymbol &Alias = it->getSymbol();
204 const MCSymbol &Symbol = Alias.AliasedSymbol();
205 MCSymbolData &SD = Asm.getSymbolData(Symbol);
208 if (&Symbol == &Alias)
211 StringRef AliasName = Alias.getName();
212 size_t Pos = AliasName.find('@');
213 if (Pos == StringRef::npos)
216 // Aliases defined with .symvar copy the binding from the symbol they alias.
217 // This is the first place we are able to copy this information.
218 it->setExternal(SD.isExternal());
219 MCELF::SetBinding(*it, MCELF::GetBinding(SD));
221 StringRef Rest = AliasName.substr(Pos);
222 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
225 // FIXME: produce a better error message.
226 if (Symbol.isUndefined() && Rest.startswith("@@") &&
227 !Rest.startswith("@@@"))
228 report_fatal_error("A @@ version cannot be undefined");
230 Renames.insert(std::make_pair(&Symbol, &Alias));
234 void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
235 MCDataFragment *ShndxF,
237 const MCAsmLayout &Layout) {
238 MCSymbolData &OrigData = *MSD.SymbolData;
240 Layout.getAssembler().getSymbolData(OrigData.getSymbol().AliasedSymbol());
242 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
243 Data.getSymbol().isVariable();
245 uint8_t Binding = MCELF::GetBinding(OrigData);
246 uint8_t Visibility = MCELF::GetVisibility(OrigData);
247 uint8_t Type = MCELF::GetType(Data);
249 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
250 uint8_t Other = Visibility;
252 uint64_t Value = SymbolValue(Data, Layout);
255 assert(!(Data.isCommon() && !Data.isExternal()));
257 const MCExpr *ESize = Data.getSize();
260 if (!ESize->EvaluateAsAbsolute(Res, Layout))
261 report_fatal_error("Size expression must be absolute.");
265 // Write out the symbol table entry
266 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
267 Size, Other, MSD.SectionIndex, IsReserved);
270 void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
271 MCDataFragment *ShndxF,
272 const MCAssembler &Asm,
273 const MCAsmLayout &Layout,
274 const SectionIndexMapTy &SectionIndexMap) {
275 // The string table must be emitted first because we need the index
276 // into the string table for all the symbol names.
277 assert(StringTable.size() && "Missing string table");
279 // FIXME: Make sure the start of the symbol table is aligned.
281 // The first entry is the undefined symbol entry.
282 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
284 // Write the symbol table entries.
285 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
286 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
287 ELFSymbolData &MSD = LocalSymbolData[i];
288 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
291 // Write out a symbol table entry for each regular section.
292 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
294 const MCSectionELF &Section =
295 static_cast<const MCSectionELF&>(i->getSection());
296 if (Section.getType() == ELF::SHT_RELA ||
297 Section.getType() == ELF::SHT_REL ||
298 Section.getType() == ELF::SHT_STRTAB ||
299 Section.getType() == ELF::SHT_SYMTAB ||
300 Section.getType() == ELF::SHT_SYMTAB_SHNDX)
302 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
303 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section),
305 LastLocalSymbolIndex++;
308 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
309 ELFSymbolData &MSD = ExternalSymbolData[i];
310 MCSymbolData &Data = *MSD.SymbolData;
311 assert(((Data.getFlags() & ELF_STB_Global) ||
312 (Data.getFlags() & ELF_STB_Weak)) &&
313 "External symbol requires STB_GLOBAL or STB_WEAK flag");
314 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
315 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
316 LastLocalSymbolIndex++;
319 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
320 ELFSymbolData &MSD = UndefinedSymbolData[i];
321 MCSymbolData &Data = *MSD.SymbolData;
322 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
323 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
324 LastLocalSymbolIndex++;
328 const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm,
329 const MCValue &Target,
331 const MCFixup &Fixup,
332 bool IsPCRel) const {
333 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
334 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
335 const MCSymbol *Renamed = Renames.lookup(&Symbol);
336 const MCSymbolData &SD = Asm.getSymbolData(Symbol);
338 if (ASymbol.isUndefined()) {
344 if (SD.isExternal()) {
350 const MCSectionELF &Section =
351 static_cast<const MCSectionELF&>(ASymbol.getSection());
352 const SectionKind secKind = Section.getKind();
355 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
357 if (secKind.isThreadLocal()) {
363 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
364 const MCSectionELF &Sec2 =
365 static_cast<const MCSectionELF&>(F.getParent()->getSection());
367 if (&Sec2 != &Section &&
368 (Kind == MCSymbolRefExpr::VK_PLT ||
369 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
370 Kind == MCSymbolRefExpr::VK_GOTOFF)) {
376 if (Section.getFlags() & ELF::SHF_MERGE) {
377 if (Target.getConstant() == 0)
378 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
384 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
389 void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
390 const MCAsmLayout &Layout,
391 const MCFragment *Fragment,
392 const MCFixup &Fixup,
394 uint64_t &FixedValue) {
397 int64_t Value = Target.getConstant();
398 const MCSymbol *RelocSymbol = NULL;
400 bool IsPCRel = isFixupKindPCRel(Asm, Fixup.getKind());
401 if (!Target.isAbsolute()) {
402 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
403 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
404 RelocSymbol = SymbolToReloc(Asm, Target, *Fragment, Fixup, IsPCRel);
406 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
407 const MCSymbol &SymbolB = RefB->getSymbol();
408 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
411 // Offset of the symbol in the section
412 int64_t a = Layout.getSymbolOffset(&SDB);
414 // Offset of the relocation in the section
415 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
420 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
421 MCFragment *F = SD.getFragment();
423 Index = F->getParent()->getOrdinal() + 1;
425 // Offset of the symbol in the section
426 Value += Layout.getSymbolOffset(&SD);
428 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
429 WeakrefUsedInReloc.insert(RelocSymbol);
431 UsedInReloc.insert(RelocSymbol);
435 // Compensate for the addend on i386.
441 unsigned Type = GetRelocType(Target, Fixup, IsPCRel,
442 (RelocSymbol != 0), Addend);
443 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
444 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
445 if (RelocNeedsGOT(Modifier))
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++);
669 if (NumRegularSections > ELF::SHN_LORESERVE)
670 NeedsSymtabShndx = true;
673 void ELFObjectWriter::CreateRelocationSections(MCAssembler &Asm,
676 for (MCAssembler::const_iterator it = Asm.begin(),
677 ie = Asm.end(); it != ie; ++it) {
678 const MCSectionData &SD = *it;
679 if (Relocations[&SD].empty())
682 MCContext &Ctx = Asm.getContext();
683 const MCSectionELF &Section =
684 static_cast<const MCSectionELF&>(SD.getSection());
686 const StringRef SectionName = Section.getSectionName();
687 std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
688 RelaSectionName += SectionName;
691 if (hasRelocationAddend())
692 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
694 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
696 const MCSectionELF *RelaSection =
697 Ctx.getELFSection(RelaSectionName, hasRelocationAddend() ?
698 ELF::SHT_RELA : ELF::SHT_REL, 0,
699 SectionKind::getReadOnly(),
701 RelMap[&Section] = RelaSection;
702 Asm.getOrCreateSectionData(*RelaSection);
706 void ELFObjectWriter::WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout,
707 const RelMapTy &RelMap) {
708 for (MCAssembler::const_iterator it = Asm.begin(),
709 ie = Asm.end(); it != ie; ++it) {
710 const MCSectionData &SD = *it;
711 const MCSectionELF &Section =
712 static_cast<const MCSectionELF&>(SD.getSection());
714 const MCSectionELF *RelaSection = RelMap.lookup(&Section);
717 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
718 RelaSD.setAlignment(is64Bit() ? 8 : 4);
720 MCDataFragment *F = new MCDataFragment(&RelaSD);
721 WriteRelocationsFragment(Asm, F, &*it);
725 void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
726 uint64_t Flags, uint64_t Address,
727 uint64_t Offset, uint64_t Size,
728 uint32_t Link, uint32_t Info,
730 uint64_t EntrySize) {
731 Write32(Name); // sh_name: index into string table
732 Write32(Type); // sh_type
733 WriteWord(Flags); // sh_flags
734 WriteWord(Address); // sh_addr
735 WriteWord(Offset); // sh_offset
736 WriteWord(Size); // sh_size
737 Write32(Link); // sh_link
738 Write32(Info); // sh_info
739 WriteWord(Alignment); // sh_addralign
740 WriteWord(EntrySize); // sh_entsize
743 void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
745 const MCSectionData *SD) {
746 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
747 // sort by the r_offset just like gnu as does
748 array_pod_sort(Relocs.begin(), Relocs.end());
750 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
751 ELFRelocationEntry entry = Relocs[e - i - 1];
755 else if (entry.Index < 0)
756 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
758 entry.Index += LocalSymbolData.size();
760 String64(*F, entry.r_offset);
762 struct ELF::Elf64_Rela ERE64;
763 ERE64.setSymbolAndType(entry.Index, entry.Type);
764 String64(*F, ERE64.r_info);
766 if (hasRelocationAddend())
767 String64(*F, entry.r_addend);
769 String32(*F, entry.r_offset);
771 struct ELF::Elf32_Rela ERE32;
772 ERE32.setSymbolAndType(entry.Index, entry.Type);
773 String32(*F, ERE32.r_info);
775 if (hasRelocationAddend())
776 String32(*F, entry.r_addend);
781 static int compareBySuffix(const void *a, const void *b) {
782 const MCSectionELF *secA = *static_cast<const MCSectionELF* const *>(a);
783 const MCSectionELF *secB = *static_cast<const MCSectionELF* const *>(b);
784 const StringRef &NameA = secA->getSectionName();
785 const StringRef &NameB = secB->getSectionName();
786 const unsigned sizeA = NameA.size();
787 const unsigned sizeB = NameB.size();
788 const unsigned len = std::min(sizeA, sizeB);
789 for (unsigned int i = 0; i < len; ++i) {
790 char ca = NameA[sizeA - i - 1];
791 char cb = NameB[sizeB - i - 1];
796 return sizeB - sizeA;
799 void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
801 SectionIndexMapTy &SectionIndexMap,
802 const RelMapTy &RelMap) {
803 MCContext &Ctx = Asm.getContext();
806 unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
808 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
809 const MCSectionELF *ShstrtabSection =
810 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
811 SectionKind::getReadOnly());
812 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
813 ShstrtabSD.setAlignment(1);
815 const MCSectionELF *SymtabSection =
816 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
817 SectionKind::getReadOnly(),
819 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
820 SymtabSD.setAlignment(is64Bit() ? 8 : 4);
822 MCSectionData *SymtabShndxSD = NULL;
824 if (NeedsSymtabShndx) {
825 const MCSectionELF *SymtabShndxSection =
826 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
827 SectionKind::getReadOnly(), 4, "");
828 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
829 SymtabShndxSD->setAlignment(4);
832 const MCSectionELF *StrtabSection;
833 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
834 SectionKind::getReadOnly());
835 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
836 StrtabSD.setAlignment(1);
838 ComputeIndexMap(Asm, SectionIndexMap, RelMap);
840 ShstrtabIndex = SectionIndexMap.lookup(ShstrtabSection);
841 SymbolTableIndex = SectionIndexMap.lookup(SymtabSection);
842 StringTableIndex = SectionIndexMap.lookup(StrtabSection);
845 F = new MCDataFragment(&SymtabSD);
846 MCDataFragment *ShndxF = NULL;
847 if (NeedsSymtabShndx) {
848 ShndxF = new MCDataFragment(SymtabShndxSD);
850 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
852 F = new MCDataFragment(&StrtabSD);
853 F->getContents().append(StringTable.begin(), StringTable.end());
855 F = new MCDataFragment(&ShstrtabSD);
857 std::vector<const MCSectionELF*> Sections;
858 for (MCAssembler::const_iterator it = Asm.begin(),
859 ie = Asm.end(); it != ie; ++it) {
860 const MCSectionELF &Section =
861 static_cast<const MCSectionELF&>(it->getSection());
862 Sections.push_back(&Section);
864 array_pod_sort(Sections.begin(), Sections.end(), compareBySuffix);
866 // Section header string table.
868 // The first entry of a string table holds a null character so skip
871 F->getContents() += '\x00';
873 for (unsigned int I = 0, E = Sections.size(); I != E; ++I) {
874 const MCSectionELF &Section = *Sections[I];
876 StringRef Name = Section.getSectionName();
878 StringRef PreviousName = Sections[I - 1]->getSectionName();
879 if (PreviousName.endswith(Name)) {
880 SectionStringTableIndex[&Section] = Index - Name.size() - 1;
884 // Remember the index into the string table so we can write it
885 // into the sh_name field of the section header table.
886 SectionStringTableIndex[&Section] = Index;
888 Index += Name.size() + 1;
889 F->getContents() += Name;
890 F->getContents() += '\x00';
894 void ELFObjectWriter::CreateIndexedSections(MCAssembler &Asm,
896 GroupMapTy &GroupMap,
897 RevGroupMapTy &RevGroupMap,
898 SectionIndexMapTy &SectionIndexMap,
899 const RelMapTy &RelMap) {
900 // Create the .note.GNU-stack section if needed.
901 MCContext &Ctx = Asm.getContext();
902 if (Asm.getNoExecStack()) {
903 const MCSectionELF *GnuStackSection =
904 Ctx.getELFSection(".note.GNU-stack", ELF::SHT_PROGBITS, 0,
905 SectionKind::getReadOnly());
906 Asm.getOrCreateSectionData(*GnuStackSection);
910 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
912 const MCSectionELF &Section =
913 static_cast<const MCSectionELF&>(it->getSection());
914 if (!(Section.getFlags() & ELF::SHF_GROUP))
917 const MCSymbol *SignatureSymbol = Section.getGroup();
918 Asm.getOrCreateSymbolData(*SignatureSymbol);
919 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
921 Group = Ctx.CreateELFGroupSection();
922 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
923 Data.setAlignment(4);
924 MCDataFragment *F = new MCDataFragment(&Data);
925 String32(*F, ELF::GRP_COMDAT);
927 GroupMap[Group] = SignatureSymbol;
930 ComputeIndexMap(Asm, SectionIndexMap, RelMap);
932 // Add sections to the groups
933 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
935 const MCSectionELF &Section =
936 static_cast<const MCSectionELF&>(it->getSection());
937 if (!(Section.getFlags() & ELF::SHF_GROUP))
939 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
940 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
941 // FIXME: we could use the previous fragment
942 MCDataFragment *F = new MCDataFragment(&Data);
943 unsigned Index = SectionIndexMap.lookup(&Section);
948 void ELFObjectWriter::WriteSection(MCAssembler &Asm,
949 const SectionIndexMapTy &SectionIndexMap,
950 uint32_t GroupSymbolIndex,
951 uint64_t Offset, uint64_t Size,
953 const MCSectionELF &Section) {
954 uint64_t sh_link = 0;
955 uint64_t sh_info = 0;
957 switch(Section.getType()) {
958 case ELF::SHT_DYNAMIC:
959 sh_link = SectionStringTableIndex[&Section];
964 case ELF::SHT_RELA: {
965 const MCSectionELF *SymtabSection;
966 const MCSectionELF *InfoSection;
967 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
969 SectionKind::getReadOnly());
970 sh_link = SectionIndexMap.lookup(SymtabSection);
971 assert(sh_link && ".symtab not found");
973 // Remove ".rel" and ".rela" prefixes.
974 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
975 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
977 InfoSection = Asm.getContext().getELFSection(SectionName,
978 ELF::SHT_PROGBITS, 0,
979 SectionKind::getReadOnly());
980 sh_info = SectionIndexMap.lookup(InfoSection);
984 case ELF::SHT_SYMTAB:
985 case ELF::SHT_DYNSYM:
986 sh_link = StringTableIndex;
987 sh_info = LastLocalSymbolIndex;
990 case ELF::SHT_SYMTAB_SHNDX:
991 sh_link = SymbolTableIndex;
994 case ELF::SHT_PROGBITS:
995 case ELF::SHT_STRTAB:
996 case ELF::SHT_NOBITS:
999 case ELF::SHT_ARM_ATTRIBUTES:
1000 case ELF::SHT_INIT_ARRAY:
1001 case ELF::SHT_FINI_ARRAY:
1002 case ELF::SHT_PREINIT_ARRAY:
1003 case ELF::SHT_X86_64_UNWIND:
1007 case ELF::SHT_GROUP:
1008 sh_link = SymbolTableIndex;
1009 sh_info = GroupSymbolIndex;
1013 assert(0 && "FIXME: sh_type value not supported!");
1017 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1018 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1019 Alignment, Section.getEntrySize());
1022 bool ELFObjectWriter::IsELFMetaDataSection(const MCSectionData &SD) {
1023 return SD.getOrdinal() == ~UINT32_C(0) &&
1024 !SD.getSection().isVirtualSection();
1027 uint64_t ELFObjectWriter::DataSectionSize(const MCSectionData &SD) {
1029 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1031 const MCFragment &F = *i;
1032 assert(F.getKind() == MCFragment::FT_Data);
1033 Ret += cast<MCDataFragment>(F).getContents().size();
1038 uint64_t ELFObjectWriter::GetSectionFileSize(const MCAsmLayout &Layout,
1039 const MCSectionData &SD) {
1040 if (IsELFMetaDataSection(SD))
1041 return DataSectionSize(SD);
1042 return Layout.getSectionFileSize(&SD);
1045 uint64_t ELFObjectWriter::GetSectionAddressSize(const MCAsmLayout &Layout,
1046 const MCSectionData &SD) {
1047 if (IsELFMetaDataSection(SD))
1048 return DataSectionSize(SD);
1049 return Layout.getSectionAddressSize(&SD);
1052 void ELFObjectWriter::WriteDataSectionData(MCAssembler &Asm,
1053 const MCAsmLayout &Layout,
1054 const MCSectionELF &Section) {
1055 uint64_t FileOff = OS.tell();
1056 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1058 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1059 WriteZeros(Padding);
1062 FileOff += GetSectionFileSize(Layout, SD);
1064 if (IsELFMetaDataSection(SD)) {
1065 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1067 const MCFragment &F = *i;
1068 assert(F.getKind() == MCFragment::FT_Data);
1069 WriteBytes(cast<MCDataFragment>(F).getContents().str());
1072 Asm.writeSectionData(&SD, Layout);
1076 void ELFObjectWriter::WriteSectionHeader(MCAssembler &Asm,
1077 const GroupMapTy &GroupMap,
1078 const MCAsmLayout &Layout,
1079 const SectionIndexMapTy &SectionIndexMap,
1080 const SectionOffsetMapTy &SectionOffsetMap) {
1081 const unsigned NumSections = Asm.size() + 1;
1083 std::vector<const MCSectionELF*> Sections;
1084 Sections.resize(NumSections - 1);
1086 for (SectionIndexMapTy::const_iterator i=
1087 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1088 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
1089 Sections[p.second - 1] = p.first;
1092 // Null section first.
1093 uint64_t FirstSectionSize =
1094 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1095 uint32_t FirstSectionLink =
1096 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1097 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
1099 for (unsigned i = 0; i < NumSections - 1; ++i) {
1100 const MCSectionELF &Section = *Sections[i];
1101 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1102 uint32_t GroupSymbolIndex;
1103 if (Section.getType() != ELF::SHT_GROUP)
1104 GroupSymbolIndex = 0;
1106 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm,
1107 GroupMap.lookup(&Section));
1109 uint64_t Size = GetSectionAddressSize(Layout, SD);
1111 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
1112 SectionOffsetMap.lookup(&Section), Size,
1113 SD.getAlignment(), Section);
1117 void ELFObjectWriter::ComputeSectionOrder(MCAssembler &Asm,
1118 std::vector<const MCSectionELF*> &Sections) {
1119 for (MCAssembler::iterator it = Asm.begin(),
1120 ie = Asm.end(); it != ie; ++it) {
1121 const MCSectionELF &Section =
1122 static_cast<const MCSectionELF &>(it->getSection());
1123 if (Section.getType() == ELF::SHT_GROUP)
1124 Sections.push_back(&Section);
1127 for (MCAssembler::iterator it = Asm.begin(),
1128 ie = Asm.end(); it != ie; ++it) {
1129 const MCSectionELF &Section =
1130 static_cast<const MCSectionELF &>(it->getSection());
1131 if (Section.getType() != ELF::SHT_GROUP &&
1132 Section.getType() != ELF::SHT_REL &&
1133 Section.getType() != ELF::SHT_RELA)
1134 Sections.push_back(&Section);
1137 for (MCAssembler::iterator it = Asm.begin(),
1138 ie = Asm.end(); it != ie; ++it) {
1139 const MCSectionELF &Section =
1140 static_cast<const MCSectionELF &>(it->getSection());
1141 if (Section.getType() == ELF::SHT_REL ||
1142 Section.getType() == ELF::SHT_RELA)
1143 Sections.push_back(&Section);
1147 void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1148 const MCAsmLayout &Layout) {
1149 GroupMapTy GroupMap;
1150 RevGroupMapTy RevGroupMap;
1151 SectionIndexMapTy SectionIndexMap;
1153 unsigned NumUserSections = Asm.size();
1155 DenseMap<const MCSectionELF*, const MCSectionELF*> RelMap;
1156 CreateRelocationSections(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1158 const unsigned NumUserAndRelocSections = Asm.size();
1159 CreateIndexedSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1160 RevGroupMap, SectionIndexMap, RelMap);
1161 const unsigned AllSections = Asm.size();
1162 const unsigned NumIndexedSections = AllSections - NumUserAndRelocSections;
1164 unsigned NumRegularSections = NumUserSections + NumIndexedSections;
1166 // Compute symbol table information.
1167 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap, NumRegularSections);
1170 WriteRelocations(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1172 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
1173 const_cast<MCAsmLayout&>(Layout),
1177 uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
1178 uint64_t HeaderSize = is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
1179 sizeof(ELF::Elf32_Ehdr);
1180 uint64_t FileOff = HeaderSize;
1182 std::vector<const MCSectionELF*> Sections;
1183 ComputeSectionOrder(Asm, Sections);
1184 unsigned NumSections = Sections.size();
1185 SectionOffsetMapTy SectionOffsetMap;
1186 for (unsigned i = 0; i < NumRegularSections + 1; ++i) {
1187 const MCSectionELF &Section = *Sections[i];
1188 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1190 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1192 // Remember the offset into the file for this section.
1193 SectionOffsetMap[&Section] = FileOff;
1195 // Get the size of the section in the output file (including padding).
1196 FileOff += GetSectionFileSize(Layout, SD);
1199 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1201 const unsigned SectionHeaderOffset = FileOff - HeaderSize;
1203 uint64_t SectionHeaderEntrySize = is64Bit() ?
1204 sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr);
1205 FileOff += (NumSections + 1) * SectionHeaderEntrySize;
1207 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i) {
1208 const MCSectionELF &Section = *Sections[i];
1209 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1211 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1213 // Remember the offset into the file for this section.
1214 SectionOffsetMap[&Section] = FileOff;
1216 // Get the size of the section in the output file (including padding).
1217 FileOff += GetSectionFileSize(Layout, SD);
1220 // Write out the ELF header ...
1221 WriteHeader(SectionHeaderOffset, NumSections + 1);
1223 // ... then the regular sections ...
1224 // + because of .shstrtab
1225 for (unsigned i = 0; i < NumRegularSections + 1; ++i)
1226 WriteDataSectionData(Asm, Layout, *Sections[i]);
1228 FileOff = OS.tell();
1229 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1230 WriteZeros(Padding);
1232 // ... then the section header table ...
1233 WriteSectionHeader(Asm, GroupMap, Layout, SectionIndexMap,
1236 FileOff = OS.tell();
1238 // ... and then the remaining sections ...
1239 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i)
1240 WriteDataSectionData(Asm, Layout, *Sections[i]);
1244 ELFObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
1245 const MCSymbolData &DataA,
1246 const MCFragment &FB,
1248 bool IsPCRel) const {
1249 if (DataA.getFlags() & ELF_STB_Weak)
1251 return MCObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(
1252 Asm, DataA, FB,InSet, IsPCRel);
1255 MCObjectWriter *llvm::createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1257 bool IsLittleEndian) {
1258 switch (MOTW->getEMachine()) {
1260 case ELF::EM_X86_64:
1262 return new ELFObjectWriter(MOTW, OS, IsLittleEndian); break;
1263 case ELF::EM_MBLAZE:
1264 return new MBlazeELFObjectWriter(MOTW, OS, IsLittleEndian); break;
1267 return new PPCELFObjectWriter(MOTW, OS, IsLittleEndian); break;
1269 return new MipsELFObjectWriter(MOTW, OS, IsLittleEndian); break;
1270 default: llvm_unreachable("Unsupported architecture"); break;
1274 unsigned ELFObjectWriter::GetRelocType(const MCValue &Target,
1275 const MCFixup &Fixup,
1277 bool IsRelocWithSymbol,
1278 int64_t Addend) const {
1279 return TargetObjectWriter->GetRelocType(Target, Fixup, IsPCRel,
1280 IsRelocWithSymbol, Addend);
1283 /// START OF SUBCLASSES for ELFObjectWriter
1284 //===- PPCELFObjectWriter -------------------------------------------===//
1286 PPCELFObjectWriter::PPCELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1288 bool IsLittleEndian)
1289 : ELFObjectWriter(MOTW, _OS, IsLittleEndian) {
1292 PPCELFObjectWriter::~PPCELFObjectWriter() {
1295 unsigned PPCELFObjectWriter::GetRelocType(const MCValue &Target,
1296 const MCFixup &Fixup,
1298 bool IsRelocWithSymbol,
1299 int64_t Addend) const {
1300 // determine the type of the relocation
1303 switch ((unsigned)Fixup.getKind()) {
1305 llvm_unreachable("Unimplemented");
1306 case PPC::fixup_ppc_br24:
1307 Type = ELF::R_PPC_REL24;
1310 Type = ELF::R_PPC_REL32;
1314 switch ((unsigned)Fixup.getKind()) {
1315 default: llvm_unreachable("invalid fixup kind!");
1316 case PPC::fixup_ppc_br24:
1317 Type = ELF::R_PPC_ADDR24;
1319 case PPC::fixup_ppc_brcond14:
1320 Type = ELF::R_PPC_ADDR14_BRTAKEN; // XXX: or BRNTAKEN?_
1322 case PPC::fixup_ppc_ha16:
1323 Type = ELF::R_PPC_ADDR16_HA;
1325 case PPC::fixup_ppc_lo16:
1326 Type = ELF::R_PPC_ADDR16_LO;
1328 case PPC::fixup_ppc_lo14:
1329 Type = ELF::R_PPC_ADDR14;
1332 Type = ELF::R_PPC_ADDR32;
1335 Type = ELF::R_PPC_ADDR16;
1342 void PPCELFObjectWriter::
1343 adjustFixupOffset(const MCFixup &Fixup, uint64_t &RelocOffset) {
1344 switch ((unsigned)Fixup.getKind()) {
1345 case PPC::fixup_ppc_ha16:
1346 case PPC::fixup_ppc_lo16:
1354 //===- MBlazeELFObjectWriter -------------------------------------------===//
1356 MBlazeELFObjectWriter::MBlazeELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1358 bool IsLittleEndian)
1359 : ELFObjectWriter(MOTW, _OS, IsLittleEndian) {
1362 MBlazeELFObjectWriter::~MBlazeELFObjectWriter() {
1365 unsigned MBlazeELFObjectWriter::GetRelocType(const MCValue &Target,
1366 const MCFixup &Fixup,
1368 bool IsRelocWithSymbol,
1369 int64_t Addend) const {
1370 // determine the type of the relocation
1373 switch ((unsigned)Fixup.getKind()) {
1375 llvm_unreachable("Unimplemented");
1377 Type = ELF::R_MICROBLAZE_64_PCREL;
1380 Type = ELF::R_MICROBLAZE_32_PCREL;
1384 switch ((unsigned)Fixup.getKind()) {
1385 default: llvm_unreachable("invalid fixup kind!");
1387 Type = ((IsRelocWithSymbol || Addend !=0)
1388 ? ELF::R_MICROBLAZE_32
1389 : ELF::R_MICROBLAZE_64);
1392 Type = ELF::R_MICROBLAZE_32;
1399 //===- MipsELFObjectWriter -------------------------------------------===//
1401 MipsELFObjectWriter::MipsELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1403 bool IsLittleEndian)
1404 : ELFObjectWriter(MOTW, _OS, IsLittleEndian) {}
1406 MipsELFObjectWriter::~MipsELFObjectWriter() {}
1408 // FIXME: get the real EABI Version from the Triple.
1409 unsigned MipsELFObjectWriter::getEFlags() const {
1410 return ELF::EF_MIPS_NOREORDER | ELF::EF_MIPS_ARCH_32R2;
1413 const MCSymbol *MipsELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
1414 const MCValue &Target,
1415 const MCFragment &F,
1416 const MCFixup &Fixup,
1417 bool IsPCRel) const {
1418 assert(Target.getSymA() && "SymA cannot be 0.");
1419 const MCSymbol &Sym = Target.getSymA()->getSymbol();
1421 if (Sym.getSection().getKind().isMergeableCString() ||
1422 Sym.getSection().getKind().isMergeableConst())
1428 unsigned MipsELFObjectWriter::GetRelocType(const MCValue &Target,
1429 const MCFixup &Fixup,
1431 bool IsRelocWithSymbol,
1432 int64_t Addend) const {
1433 // determine the type of the relocation
1434 unsigned Type = (unsigned)ELF::R_MIPS_NONE;
1435 unsigned Kind = (unsigned)Fixup.getKind();
1439 llvm_unreachable("invalid fixup kind!");
1441 Type = ELF::R_MIPS_32;
1444 Type = ELF::R_MIPS_GPREL32;
1446 case Mips::fixup_Mips_GPREL16:
1447 Type = ELF::R_MIPS_GPREL16;
1449 case Mips::fixup_Mips_26:
1450 Type = ELF::R_MIPS_26;
1452 case Mips::fixup_Mips_CALL16:
1453 Type = ELF::R_MIPS_CALL16;
1455 case Mips::fixup_Mips_GOT_Global:
1456 case Mips::fixup_Mips_GOT_Local:
1457 Type = ELF::R_MIPS_GOT16;
1459 case Mips::fixup_Mips_HI16:
1460 Type = ELF::R_MIPS_HI16;
1462 case Mips::fixup_Mips_LO16:
1463 Type = ELF::R_MIPS_LO16;
1465 case Mips::fixup_Mips_TLSGD:
1466 Type = ELF::R_MIPS_TLS_GD;
1468 case Mips::fixup_Mips_GOTTPREL:
1469 Type = ELF::R_MIPS_TLS_GOTTPREL;
1471 case Mips::fixup_Mips_TPREL_HI:
1472 Type = ELF::R_MIPS_TLS_TPREL_HI16;
1474 case Mips::fixup_Mips_TPREL_LO:
1475 Type = ELF::R_MIPS_TLS_TPREL_LO16;
1477 case Mips::fixup_Mips_TLSLDM:
1478 Type = ELF::R_MIPS_TLS_LDM;
1480 case Mips::fixup_Mips_DTPREL_HI:
1481 Type = ELF::R_MIPS_TLS_DTPREL_HI16;
1483 case Mips::fixup_Mips_DTPREL_LO:
1484 Type = ELF::R_MIPS_TLS_DTPREL_LO16;
1486 case Mips::fixup_Mips_Branch_PCRel:
1487 case Mips::fixup_Mips_PC16:
1488 Type = ELF::R_MIPS_PC16;