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 "llvm/ADT/SmallPtrSet.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/StringMap.h"
17 #include "llvm/ADT/Twine.h"
18 #include "llvm/MC/MCAssembler.h"
19 #include "llvm/MC/MCAsmLayout.h"
20 #include "llvm/MC/MCContext.h"
21 #include "llvm/MC/MCELFSymbolFlags.h"
22 #include "llvm/MC/MCExpr.h"
23 #include "llvm/MC/MCObjectWriter.h"
24 #include "llvm/MC/MCSectionELF.h"
25 #include "llvm/MC/MCSymbol.h"
26 #include "llvm/MC/MCValue.h"
27 #include "llvm/Support/Debug.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/Support/ELF.h"
30 #include "llvm/Target/TargetAsmBackend.h"
32 #include "../Target/X86/X86FixupKinds.h"
37 static unsigned GetType(const MCSymbolData &SD) {
38 uint32_t Type = (SD.getFlags() & (0xf << ELF_STT_Shift)) >> ELF_STT_Shift;
39 assert(Type == ELF::STT_NOTYPE || Type == ELF::STT_OBJECT ||
40 Type == ELF::STT_FUNC || Type == ELF::STT_SECTION ||
41 Type == ELF::STT_FILE || Type == ELF::STT_COMMON ||
42 Type == ELF::STT_TLS);
46 static unsigned GetBinding(const MCSymbolData &SD) {
47 uint32_t Binding = (SD.getFlags() & (0xf << ELF_STB_Shift)) >> ELF_STB_Shift;
48 assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
49 Binding == ELF::STB_WEAK);
53 static void SetBinding(MCSymbolData &SD, unsigned Binding) {
54 assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
55 Binding == ELF::STB_WEAK);
56 uint32_t OtherFlags = SD.getFlags() & ~(0xf << ELF_STB_Shift);
57 SD.setFlags(OtherFlags | (Binding << ELF_STB_Shift));
60 static unsigned GetVisibility(MCSymbolData &SD) {
62 (SD.getFlags() & (0xf << ELF_STV_Shift)) >> ELF_STV_Shift;
63 assert(Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_INTERNAL ||
64 Visibility == ELF::STV_HIDDEN || Visibility == ELF::STV_PROTECTED);
68 static bool isFixupKindX86PCRel(unsigned Kind) {
72 case X86::reloc_pcrel_1byte:
73 case X86::reloc_pcrel_4byte:
74 case X86::reloc_riprel_4byte:
75 case X86::reloc_riprel_4byte_movq_load:
80 static bool RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant) {
84 case MCSymbolRefExpr::VK_GOT:
85 case MCSymbolRefExpr::VK_PLT:
86 case MCSymbolRefExpr::VK_GOTPCREL:
87 case MCSymbolRefExpr::VK_TPOFF:
88 case MCSymbolRefExpr::VK_TLSGD:
89 case MCSymbolRefExpr::VK_GOTTPOFF:
90 case MCSymbolRefExpr::VK_INDNTPOFF:
91 case MCSymbolRefExpr::VK_NTPOFF:
92 case MCSymbolRefExpr::VK_GOTNTPOFF:
93 case MCSymbolRefExpr::VK_TLSLDM:
94 case MCSymbolRefExpr::VK_DTPOFF:
95 case MCSymbolRefExpr::VK_TLSLD:
101 class ELFObjectWriter : public MCObjectWriter {
103 /*static bool isFixupKindX86RIPRel(unsigned Kind) {
104 return Kind == X86::reloc_riprel_4byte ||
105 Kind == X86::reloc_riprel_4byte_movq_load;
109 /// ELFSymbolData - Helper struct for containing some precomputed information
111 struct ELFSymbolData {
112 MCSymbolData *SymbolData;
113 uint64_t StringIndex;
114 uint32_t SectionIndex;
116 // Support lexicographic sorting.
117 bool operator<(const ELFSymbolData &RHS) const {
118 if (GetType(*SymbolData) == ELF::STT_FILE)
120 if (GetType(*RHS.SymbolData) == ELF::STT_FILE)
122 return SymbolData->getSymbol().getName() <
123 RHS.SymbolData->getSymbol().getName();
127 /// @name Relocation Data
130 struct ELFRelocationEntry {
131 // Make these big enough for both 32-bit and 64-bit
135 const MCSymbol *Symbol;
138 // Support lexicographic sorting.
139 bool operator<(const ELFRelocationEntry &RE) const {
140 return RE.r_offset < r_offset;
144 SmallPtrSet<const MCSymbol *, 16> UsedInReloc;
145 SmallPtrSet<const MCSymbol *, 16> WeakrefUsedInReloc;
146 DenseMap<const MCSymbol *, const MCSymbol *> Renames;
148 llvm::DenseMap<const MCSectionData*,
149 std::vector<ELFRelocationEntry> > Relocations;
150 DenseMap<const MCSection*, uint64_t> SectionStringTableIndex;
153 /// @name Symbol Table Data
156 SmallString<256> StringTable;
157 std::vector<ELFSymbolData> LocalSymbolData;
158 std::vector<ELFSymbolData> ExternalSymbolData;
159 std::vector<ELFSymbolData> UndefinedSymbolData;
165 bool NeedsSymtabShndx;
167 unsigned Is64Bit : 1;
169 bool HasRelocationAddend;
171 Triple::OSType OSType;
175 // This holds the symbol table index of the last local symbol.
176 unsigned LastLocalSymbolIndex;
177 // This holds the .strtab section index.
178 unsigned StringTableIndex;
179 // This holds the .symtab section index.
180 unsigned SymbolTableIndex;
182 unsigned ShstrtabIndex;
185 const MCSymbol *SymbolToReloc(const MCAssembler &Asm,
186 const MCValue &Target,
187 const MCFragment &F) const;
190 ELFObjectWriter(raw_ostream &_OS, bool _Is64Bit, bool IsLittleEndian,
191 uint16_t _EMachine, bool _HasRelAddend,
192 Triple::OSType _OSType)
193 : MCObjectWriter(_OS, IsLittleEndian),
194 NeedsGOT(false), NeedsSymtabShndx(false),
195 Is64Bit(_Is64Bit), HasRelocationAddend(_HasRelAddend),
196 OSType(_OSType), EMachine(_EMachine) {
199 virtual ~ELFObjectWriter();
201 void WriteWord(uint64_t W) {
208 void StringLE16(char *buf, uint16_t Value) {
209 buf[0] = char(Value >> 0);
210 buf[1] = char(Value >> 8);
213 void StringLE32(char *buf, uint32_t Value) {
214 StringLE16(buf, uint16_t(Value >> 0));
215 StringLE16(buf + 2, uint16_t(Value >> 16));
218 void StringLE64(char *buf, uint64_t Value) {
219 StringLE32(buf, uint32_t(Value >> 0));
220 StringLE32(buf + 4, uint32_t(Value >> 32));
223 void StringBE16(char *buf ,uint16_t Value) {
224 buf[0] = char(Value >> 8);
225 buf[1] = char(Value >> 0);
228 void StringBE32(char *buf, uint32_t Value) {
229 StringBE16(buf, uint16_t(Value >> 16));
230 StringBE16(buf + 2, uint16_t(Value >> 0));
233 void StringBE64(char *buf, uint64_t Value) {
234 StringBE32(buf, uint32_t(Value >> 32));
235 StringBE32(buf + 4, uint32_t(Value >> 0));
238 void String8(MCDataFragment &F, uint8_t Value) {
241 F.getContents() += StringRef(buf, 1);
244 void String16(MCDataFragment &F, uint16_t Value) {
246 if (isLittleEndian())
247 StringLE16(buf, Value);
249 StringBE16(buf, Value);
250 F.getContents() += StringRef(buf, 2);
253 void String32(MCDataFragment &F, uint32_t Value) {
255 if (isLittleEndian())
256 StringLE32(buf, Value);
258 StringBE32(buf, Value);
259 F.getContents() += StringRef(buf, 4);
262 void String64(MCDataFragment &F, uint64_t Value) {
264 if (isLittleEndian())
265 StringLE64(buf, Value);
267 StringBE64(buf, Value);
268 F.getContents() += StringRef(buf, 8);
271 virtual void WriteHeader(uint64_t SectionDataSize, unsigned NumberOfSections);
273 virtual void WriteSymbolEntry(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
274 uint64_t name, uint8_t info,
275 uint64_t value, uint64_t size,
276 uint8_t other, uint32_t shndx,
279 virtual void WriteSymbol(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
281 const MCAsmLayout &Layout);
283 typedef DenseMap<const MCSectionELF*, uint32_t> SectionIndexMapTy;
284 virtual void WriteSymbolTable(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
285 const MCAssembler &Asm,
286 const MCAsmLayout &Layout,
287 const SectionIndexMapTy &SectionIndexMap);
289 virtual void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
290 const MCFragment *Fragment, const MCFixup &Fixup,
291 MCValue Target, uint64_t &FixedValue) {
292 assert(0 && "RecordRelocation is not specific enough");
295 virtual uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm,
298 // Map from a group section to the signature symbol
299 typedef DenseMap<const MCSectionELF*, const MCSymbol*> GroupMapTy;
300 // Map from a signature symbol to the group section
301 typedef DenseMap<const MCSymbol*, const MCSectionELF*> RevGroupMapTy;
303 /// ComputeSymbolTable - Compute the symbol table data
305 /// \param StringTable [out] - The string table data.
306 /// \param StringIndexMap [out] - Map from symbol names to offsets in the
308 virtual void ComputeSymbolTable(MCAssembler &Asm,
309 const SectionIndexMapTy &SectionIndexMap,
310 RevGroupMapTy RevGroupMap);
312 virtual void ComputeIndexMap(MCAssembler &Asm,
313 SectionIndexMapTy &SectionIndexMap);
315 virtual void WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
316 const MCSectionData &SD);
318 virtual void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout) {
319 for (MCAssembler::const_iterator it = Asm.begin(),
320 ie = Asm.end(); it != ie; ++it) {
321 WriteRelocation(Asm, Layout, *it);
325 virtual void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout,
326 const SectionIndexMapTy &SectionIndexMap);
328 virtual void CreateGroupSections(MCAssembler &Asm, MCAsmLayout &Layout,
329 GroupMapTy &GroupMap, RevGroupMapTy &RevGroupMap);
331 virtual void ExecutePostLayoutBinding(MCAssembler &Asm);
333 virtual void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
334 uint64_t Address, uint64_t Offset,
335 uint64_t Size, uint32_t Link, uint32_t Info,
336 uint64_t Alignment, uint64_t EntrySize);
338 virtual void WriteRelocationsFragment(const MCAssembler &Asm, MCDataFragment *F,
339 const MCSectionData *SD);
341 virtual bool IsFixupFullyResolved(const MCAssembler &Asm,
342 const MCValue Target,
344 const MCFragment *DF) const;
346 virtual void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
347 virtual void WriteSection(MCAssembler &Asm,
348 const SectionIndexMapTy &SectionIndexMap,
349 uint32_t GroupSymbolIndex,
350 uint64_t Offset, uint64_t Size, uint64_t Alignment,
351 const MCSectionELF &Section);
354 //===- X86ELFObjectWriter -------------------------------------------===//
356 class X86ELFObjectWriter : public ELFObjectWriter {
358 X86ELFObjectWriter(raw_ostream &_OS, bool _Is64Bit, bool IsLittleEndian,
359 uint16_t _EMachine, bool _HasRelAddend,
360 Triple::OSType _OSType);
362 virtual ~X86ELFObjectWriter();
363 virtual void RecordRelocation(const MCAssembler &Asm,
364 const MCAsmLayout &Layout,
365 const MCFragment *Fragment,
366 const MCFixup &Fixup, MCValue Target,
367 uint64_t &FixedValue);
371 //===- ARMELFObjectWriter -------------------------------------------===//
373 class ARMELFObjectWriter : public ELFObjectWriter {
375 ARMELFObjectWriter(raw_ostream &_OS, bool _Is64Bit, bool IsLittleEndian,
376 uint16_t _EMachine, bool _HasRelAddend,
377 Triple::OSType _OSType);
379 virtual ~ARMELFObjectWriter();
380 virtual void RecordRelocation(const MCAssembler &Asm,
381 const MCAsmLayout &Layout,
382 const MCFragment *Fragment,
383 const MCFixup &Fixup, MCValue Target,
384 uint64_t &FixedValue);
388 ELFObjectWriter::~ELFObjectWriter()
391 // Emit the ELF header.
392 void ELFObjectWriter::WriteHeader(uint64_t SectionDataSize,
393 unsigned NumberOfSections) {
399 // emitWord method behaves differently for ELF32 and ELF64, writing
400 // 4 bytes in the former and 8 in the latter.
402 Write8(0x7f); // e_ident[EI_MAG0]
403 Write8('E'); // e_ident[EI_MAG1]
404 Write8('L'); // e_ident[EI_MAG2]
405 Write8('F'); // e_ident[EI_MAG3]
407 Write8(Is64Bit ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
410 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
412 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
415 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
416 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
417 default: Write8(ELF::ELFOSABI_NONE); break;
419 Write8(0); // e_ident[EI_ABIVERSION]
421 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
423 Write16(ELF::ET_REL); // e_type
425 Write16(EMachine); // e_machine = target
427 Write32(ELF::EV_CURRENT); // e_version
428 WriteWord(0); // e_entry, no entry point in .o file
429 WriteWord(0); // e_phoff, no program header for .o
430 WriteWord(SectionDataSize + (Is64Bit ? sizeof(ELF::Elf64_Ehdr) :
431 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
433 // FIXME: Make this configurable.
434 Write32(0); // e_flags = whatever the target wants
436 // e_ehsize = ELF header size
437 Write16(Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
439 Write16(0); // e_phentsize = prog header entry size
440 Write16(0); // e_phnum = # prog header entries = 0
442 // e_shentsize = Section header entry size
443 Write16(Is64Bit ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
445 // e_shnum = # of section header ents
446 if (NumberOfSections >= ELF::SHN_LORESERVE)
449 Write16(NumberOfSections);
451 // e_shstrndx = Section # of '.shstrtab'
452 if (NumberOfSections >= ELF::SHN_LORESERVE)
453 Write16(ELF::SHN_XINDEX);
455 Write16(ShstrtabIndex);
458 void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
459 MCDataFragment *ShndxF,
461 uint8_t info, uint64_t value,
462 uint64_t size, uint8_t other,
466 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
467 String32(*ShndxF, shndx);
469 String32(*ShndxF, 0);
472 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
473 uint16_t(ELF::SHN_XINDEX) : shndx;
476 String32(*SymtabF, name); // st_name
477 String8(*SymtabF, info); // st_info
478 String8(*SymtabF, other); // st_other
479 String16(*SymtabF, Index); // st_shndx
480 String64(*SymtabF, value); // st_value
481 String64(*SymtabF, size); // st_size
483 String32(*SymtabF, name); // st_name
484 String32(*SymtabF, value); // st_value
485 String32(*SymtabF, size); // st_size
486 String8(*SymtabF, info); // st_info
487 String8(*SymtabF, other); // st_other
488 String16(*SymtabF, Index); // st_shndx
492 static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout) {
493 if (Data.isCommon() && Data.isExternal())
494 return Data.getCommonAlignment();
496 const MCSymbol &Symbol = Data.getSymbol();
497 if (!Symbol.isInSection())
500 if (MCFragment *FF = Data.getFragment())
501 return Layout.getSymbolAddress(&Data) -
502 Layout.getSectionAddress(FF->getParent());
507 void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm) {
508 // The presence of symbol versions causes undefined symbols and
509 // versions declared with @@@ to be renamed.
511 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
512 ie = Asm.symbol_end(); it != ie; ++it) {
513 const MCSymbol &Alias = it->getSymbol();
514 const MCSymbol &Symbol = Alias.AliasedSymbol();
515 MCSymbolData &SD = Asm.getSymbolData(Symbol);
518 if (&Symbol == &Alias)
521 StringRef AliasName = Alias.getName();
522 size_t Pos = AliasName.find('@');
523 if (Pos == StringRef::npos)
526 // Aliases defined with .symvar copy the binding from the symbol they alias.
527 // This is the first place we are able to copy this information.
528 it->setExternal(SD.isExternal());
529 SetBinding(*it, GetBinding(SD));
531 StringRef Rest = AliasName.substr(Pos);
532 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
535 // FIXME: produce a better error message.
536 if (Symbol.isUndefined() && Rest.startswith("@@") &&
537 !Rest.startswith("@@@"))
538 report_fatal_error("A @@ version cannot be undefined");
540 Renames.insert(std::make_pair(&Symbol, &Alias));
544 void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
545 MCDataFragment *ShndxF,
547 const MCAsmLayout &Layout) {
548 MCSymbolData &OrigData = *MSD.SymbolData;
550 Layout.getAssembler().getSymbolData(OrigData.getSymbol().AliasedSymbol());
552 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
553 Data.getSymbol().isVariable();
555 uint8_t Binding = GetBinding(OrigData);
556 uint8_t Visibility = GetVisibility(OrigData);
557 uint8_t Type = GetType(Data);
559 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
560 uint8_t Other = Visibility;
562 uint64_t Value = SymbolValue(Data, Layout);
566 assert(!(Data.isCommon() && !Data.isExternal()));
568 ESize = Data.getSize();
569 if (Data.getSize()) {
571 if (ESize->getKind() == MCExpr::Binary) {
572 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(ESize);
574 if (BE->EvaluateAsRelocatable(Res, &Layout)) {
575 assert(!Res.getSymA() || !Res.getSymA()->getSymbol().isDefined());
576 assert(!Res.getSymB() || !Res.getSymB()->getSymbol().isDefined());
577 Size = Res.getConstant();
579 } else if (ESize->getKind() == MCExpr::Constant) {
580 Size = static_cast<const MCConstantExpr *>(ESize)->getValue();
582 assert(0 && "Unsupported size expression");
586 // Write out the symbol table entry
587 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
588 Size, Other, MSD.SectionIndex, IsReserved);
591 void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
592 MCDataFragment *ShndxF,
593 const MCAssembler &Asm,
594 const MCAsmLayout &Layout,
595 const SectionIndexMapTy &SectionIndexMap) {
596 // The string table must be emitted first because we need the index
597 // into the string table for all the symbol names.
598 assert(StringTable.size() && "Missing string table");
600 // FIXME: Make sure the start of the symbol table is aligned.
602 // The first entry is the undefined symbol entry.
603 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
605 // Write the symbol table entries.
606 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
607 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
608 ELFSymbolData &MSD = LocalSymbolData[i];
609 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
612 // Write out a symbol table entry for each regular section.
613 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
615 const MCSectionELF &Section =
616 static_cast<const MCSectionELF&>(i->getSection());
617 if (Section.getType() == ELF::SHT_RELA ||
618 Section.getType() == ELF::SHT_REL ||
619 Section.getType() == ELF::SHT_STRTAB ||
620 Section.getType() == ELF::SHT_SYMTAB)
622 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
623 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section), false);
624 LastLocalSymbolIndex++;
627 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
628 ELFSymbolData &MSD = ExternalSymbolData[i];
629 MCSymbolData &Data = *MSD.SymbolData;
630 assert(((Data.getFlags() & ELF_STB_Global) ||
631 (Data.getFlags() & ELF_STB_Weak)) &&
632 "External symbol requires STB_GLOBAL or STB_WEAK flag");
633 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
634 if (GetBinding(Data) == ELF::STB_LOCAL)
635 LastLocalSymbolIndex++;
638 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
639 ELFSymbolData &MSD = UndefinedSymbolData[i];
640 MCSymbolData &Data = *MSD.SymbolData;
641 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
642 if (GetBinding(Data) == ELF::STB_LOCAL)
643 LastLocalSymbolIndex++;
647 const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm,
648 const MCValue &Target,
649 const MCFragment &F) const {
650 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
651 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
652 const MCSymbol *Renamed = Renames.lookup(&Symbol);
653 const MCSymbolData &SD = Asm.getSymbolData(Symbol);
655 if (ASymbol.isUndefined()) {
661 if (SD.isExternal()) {
667 const MCSectionELF &Section =
668 static_cast<const MCSectionELF&>(ASymbol.getSection());
670 if (Section.getKind().isBSS())
673 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
674 const MCSectionELF &Sec2 =
675 static_cast<const MCSectionELF&>(F.getParent()->getSection());
677 if (&Sec2 != &Section &&
678 (Kind == MCSymbolRefExpr::VK_PLT ||
679 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
680 Kind == MCSymbolRefExpr::VK_GOTOFF)) {
686 if (Section.getFlags() & MCSectionELF::SHF_MERGE) {
687 if (Target.getConstant() == 0)
699 ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
701 MCSymbolData &SD = Asm.getSymbolData(*S);
702 return SD.getIndex();
705 static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
706 bool Used, bool Renamed) {
707 if (Data.getFlags() & ELF_Other_Weakref)
716 const MCSymbol &Symbol = Data.getSymbol();
718 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
721 const MCSymbol &A = Symbol.AliasedSymbol();
722 if (!A.isVariable() && A.isUndefined() && !Data.isCommon())
725 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
728 if (Symbol.isTemporary())
734 static bool isLocal(const MCSymbolData &Data, bool isSignature,
735 bool isUsedInReloc) {
736 if (Data.isExternal())
739 const MCSymbol &Symbol = Data.getSymbol();
740 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
742 if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
743 if (isSignature && !isUsedInReloc)
752 void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
753 SectionIndexMapTy &SectionIndexMap) {
755 for (MCAssembler::iterator it = Asm.begin(),
756 ie = Asm.end(); it != ie; ++it) {
757 const MCSectionELF &Section =
758 static_cast<const MCSectionELF &>(it->getSection());
759 if (Section.getType() != ELF::SHT_GROUP)
761 SectionIndexMap[&Section] = Index++;
764 for (MCAssembler::iterator it = Asm.begin(),
765 ie = Asm.end(); it != ie; ++it) {
766 const MCSectionELF &Section =
767 static_cast<const MCSectionELF &>(it->getSection());
768 if (Section.getType() == ELF::SHT_GROUP)
770 SectionIndexMap[&Section] = Index++;
774 void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
775 const SectionIndexMapTy &SectionIndexMap,
776 RevGroupMapTy RevGroupMap) {
777 // FIXME: Is this the correct place to do this?
779 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
780 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
781 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
782 Data.setExternal(true);
783 SetBinding(Data, ELF::STB_GLOBAL);
786 // Build section lookup table.
787 int NumRegularSections = Asm.size();
789 // Index 0 is always the empty string.
790 StringMap<uint64_t> StringIndexMap;
791 StringTable += '\x00';
793 // Add the data for the symbols.
794 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
795 ie = Asm.symbol_end(); it != ie; ++it) {
796 const MCSymbol &Symbol = it->getSymbol();
798 bool Used = UsedInReloc.count(&Symbol);
799 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
800 bool isSignature = RevGroupMap.count(&Symbol);
802 if (!isInSymtab(Asm, *it,
803 Used || WeakrefUsed || isSignature,
804 Renames.count(&Symbol)))
809 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
811 // Undefined symbols are global, but this is the first place we
812 // are able to set it.
813 bool Local = isLocal(*it, isSignature, Used);
814 if (!Local && GetBinding(*it) == ELF::STB_LOCAL) {
815 MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
816 SetBinding(*it, ELF::STB_GLOBAL);
817 SetBinding(SD, ELF::STB_GLOBAL);
820 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
821 SetBinding(*it, ELF::STB_WEAK);
823 if (it->isCommon()) {
825 MSD.SectionIndex = ELF::SHN_COMMON;
826 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
827 MSD.SectionIndex = ELF::SHN_ABS;
828 } else if (RefSymbol.isUndefined()) {
829 if (isSignature && !Used)
830 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
832 MSD.SectionIndex = ELF::SHN_UNDEF;
834 const MCSectionELF &Section =
835 static_cast<const MCSectionELF&>(RefSymbol.getSection());
836 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
837 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
838 NeedsSymtabShndx = true;
839 assert(MSD.SectionIndex && "Invalid section index!");
842 // The @@@ in symbol version is replaced with @ in undefined symbols and
843 // @@ in defined ones.
844 StringRef Name = Symbol.getName();
847 size_t Pos = Name.find("@@@");
848 if (Pos != StringRef::npos) {
849 Buf += Name.substr(0, Pos);
850 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
851 Buf += Name.substr(Pos + Skip);
855 uint64_t &Entry = StringIndexMap[Name];
857 Entry = StringTable.size();
859 StringTable += '\x00';
861 MSD.StringIndex = Entry;
862 if (MSD.SectionIndex == ELF::SHN_UNDEF)
863 UndefinedSymbolData.push_back(MSD);
865 LocalSymbolData.push_back(MSD);
867 ExternalSymbolData.push_back(MSD);
870 // Symbols are required to be in lexicographic order.
871 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
872 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
873 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
875 // Set the symbol indices. Local symbols must come before all other
876 // symbols with non-local bindings.
878 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
879 LocalSymbolData[i].SymbolData->setIndex(Index++);
881 Index += NumRegularSections;
883 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
884 ExternalSymbolData[i].SymbolData->setIndex(Index++);
885 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
886 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
889 void ELFObjectWriter::WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
890 const MCSectionData &SD) {
891 if (!Relocations[&SD].empty()) {
892 MCContext &Ctx = Asm.getContext();
893 const MCSectionELF *RelaSection;
894 const MCSectionELF &Section =
895 static_cast<const MCSectionELF&>(SD.getSection());
897 const StringRef SectionName = Section.getSectionName();
898 std::string RelaSectionName = HasRelocationAddend ? ".rela" : ".rel";
899 RelaSectionName += SectionName;
902 if (HasRelocationAddend)
903 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
905 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
907 RelaSection = Ctx.getELFSection(RelaSectionName, HasRelocationAddend ?
908 ELF::SHT_RELA : ELF::SHT_REL, 0,
909 SectionKind::getReadOnly(),
912 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
913 RelaSD.setAlignment(Is64Bit ? 8 : 4);
915 MCDataFragment *F = new MCDataFragment(&RelaSD);
917 WriteRelocationsFragment(Asm, F, &SD);
919 Asm.AddSectionToTheEnd(*this, RelaSD, Layout);
923 void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
924 uint64_t Flags, uint64_t Address,
925 uint64_t Offset, uint64_t Size,
926 uint32_t Link, uint32_t Info,
928 uint64_t EntrySize) {
929 Write32(Name); // sh_name: index into string table
930 Write32(Type); // sh_type
931 WriteWord(Flags); // sh_flags
932 WriteWord(Address); // sh_addr
933 WriteWord(Offset); // sh_offset
934 WriteWord(Size); // sh_size
935 Write32(Link); // sh_link
936 Write32(Info); // sh_info
937 WriteWord(Alignment); // sh_addralign
938 WriteWord(EntrySize); // sh_entsize
941 void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
943 const MCSectionData *SD) {
944 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
945 // sort by the r_offset just like gnu as does
946 array_pod_sort(Relocs.begin(), Relocs.end());
948 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
949 ELFRelocationEntry entry = Relocs[e - i - 1];
952 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
954 entry.Index += LocalSymbolData.size() + 1;
956 String64(*F, entry.r_offset);
958 struct ELF::Elf64_Rela ERE64;
959 ERE64.setSymbolAndType(entry.Index, entry.Type);
960 String64(*F, ERE64.r_info);
962 if (HasRelocationAddend)
963 String64(*F, entry.r_addend);
965 String32(*F, entry.r_offset);
967 struct ELF::Elf32_Rela ERE32;
968 ERE32.setSymbolAndType(entry.Index, entry.Type);
969 String32(*F, ERE32.r_info);
971 if (HasRelocationAddend)
972 String32(*F, entry.r_addend);
977 void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
979 const SectionIndexMapTy &SectionIndexMap) {
980 MCContext &Ctx = Asm.getContext();
983 unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
985 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
986 const MCSectionELF *ShstrtabSection =
987 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
988 SectionKind::getReadOnly());
989 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
990 ShstrtabSD.setAlignment(1);
991 ShstrtabIndex = Asm.size();
993 const MCSectionELF *SymtabSection =
994 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
995 SectionKind::getReadOnly(),
997 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
998 SymtabSD.setAlignment(Is64Bit ? 8 : 4);
999 SymbolTableIndex = Asm.size();
1001 MCSectionData *SymtabShndxSD = NULL;
1003 if (NeedsSymtabShndx) {
1004 const MCSectionELF *SymtabShndxSection =
1005 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
1006 SectionKind::getReadOnly(), 4, "");
1007 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
1008 SymtabShndxSD->setAlignment(4);
1011 const MCSection *StrtabSection;
1012 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
1013 SectionKind::getReadOnly());
1014 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1015 StrtabSD.setAlignment(1);
1016 StringTableIndex = Asm.size();
1018 WriteRelocations(Asm, Layout);
1021 F = new MCDataFragment(&SymtabSD);
1022 MCDataFragment *ShndxF = NULL;
1023 if (NeedsSymtabShndx) {
1024 ShndxF = new MCDataFragment(SymtabShndxSD);
1025 Asm.AddSectionToTheEnd(*this, *SymtabShndxSD, Layout);
1027 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
1028 Asm.AddSectionToTheEnd(*this, SymtabSD, Layout);
1030 F = new MCDataFragment(&StrtabSD);
1031 F->getContents().append(StringTable.begin(), StringTable.end());
1032 Asm.AddSectionToTheEnd(*this, StrtabSD, Layout);
1034 F = new MCDataFragment(&ShstrtabSD);
1036 // Section header string table.
1038 // The first entry of a string table holds a null character so skip
1041 F->getContents() += '\x00';
1043 StringMap<uint64_t> SecStringMap;
1044 for (MCAssembler::const_iterator it = Asm.begin(),
1045 ie = Asm.end(); it != ie; ++it) {
1046 const MCSectionELF &Section =
1047 static_cast<const MCSectionELF&>(it->getSection());
1048 // FIXME: We could merge suffixes like in .text and .rela.text.
1050 StringRef Name = Section.getSectionName();
1051 if (SecStringMap.count(Name)) {
1052 SectionStringTableIndex[&Section] = SecStringMap[Name];
1055 // Remember the index into the string table so we can write it
1056 // into the sh_name field of the section header table.
1057 SectionStringTableIndex[&Section] = Index;
1058 SecStringMap[Name] = Index;
1060 Index += Name.size() + 1;
1061 F->getContents() += Name;
1062 F->getContents() += '\x00';
1065 Asm.AddSectionToTheEnd(*this, ShstrtabSD, Layout);
1068 bool ELFObjectWriter::IsFixupFullyResolved(const MCAssembler &Asm,
1069 const MCValue Target,
1071 const MCFragment *DF) const {
1072 // If this is a PCrel relocation, find the section this fixup value is
1074 const MCSection *BaseSection = 0;
1076 BaseSection = &DF->getParent()->getSection();
1077 assert(BaseSection);
1080 const MCSection *SectionA = 0;
1081 const MCSymbol *SymbolA = 0;
1082 if (const MCSymbolRefExpr *A = Target.getSymA()) {
1083 SymbolA = &A->getSymbol().AliasedSymbol();
1084 SectionA = &SymbolA->getSection();
1087 const MCSection *SectionB = 0;
1088 if (const MCSymbolRefExpr *B = Target.getSymB()) {
1089 SectionB = &B->getSymbol().AliasedSymbol().getSection();
1093 return SectionA == SectionB;
1095 const MCSymbolData &DataA = Asm.getSymbolData(*SymbolA);
1096 if (DataA.isExternal())
1099 return !SectionB && BaseSection == SectionA;
1102 void ELFObjectWriter::CreateGroupSections(MCAssembler &Asm,
1103 MCAsmLayout &Layout,
1104 GroupMapTy &GroupMap,
1105 RevGroupMapTy &RevGroupMap) {
1107 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1109 const MCSectionELF &Section =
1110 static_cast<const MCSectionELF&>(it->getSection());
1111 if (!(Section.getFlags() & MCSectionELF::SHF_GROUP))
1114 const MCSymbol *SignatureSymbol = Section.getGroup();
1115 Asm.getOrCreateSymbolData(*SignatureSymbol);
1116 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
1118 Group = Asm.getContext().CreateELFGroupSection();
1119 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1120 Data.setAlignment(4);
1121 MCDataFragment *F = new MCDataFragment(&Data);
1122 String32(*F, ELF::GRP_COMDAT);
1124 GroupMap[Group] = SignatureSymbol;
1127 // Add sections to the groups
1129 unsigned NumGroups = RevGroupMap.size();
1130 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1131 it != ie; ++it, ++Index) {
1132 const MCSectionELF &Section =
1133 static_cast<const MCSectionELF&>(it->getSection());
1134 if (!(Section.getFlags() & MCSectionELF::SHF_GROUP))
1136 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
1137 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1138 // FIXME: we could use the previous fragment
1139 MCDataFragment *F = new MCDataFragment(&Data);
1140 String32(*F, NumGroups + Index);
1143 for (RevGroupMapTy::const_iterator i = RevGroupMap.begin(),
1144 e = RevGroupMap.end(); i != e; ++i) {
1145 const MCSectionELF *Group = i->second;
1146 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1147 Asm.AddSectionToTheEnd(*this, Data, Layout);
1151 void ELFObjectWriter::WriteSection(MCAssembler &Asm,
1152 const SectionIndexMapTy &SectionIndexMap,
1153 uint32_t GroupSymbolIndex,
1154 uint64_t Offset, uint64_t Size,
1156 const MCSectionELF &Section) {
1157 uint64_t sh_link = 0;
1158 uint64_t sh_info = 0;
1160 switch(Section.getType()) {
1161 case ELF::SHT_DYNAMIC:
1162 sh_link = SectionStringTableIndex[&Section];
1167 case ELF::SHT_RELA: {
1168 const MCSectionELF *SymtabSection;
1169 const MCSectionELF *InfoSection;
1170 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
1172 SectionKind::getReadOnly());
1173 sh_link = SectionIndexMap.lookup(SymtabSection);
1174 assert(sh_link && ".symtab not found");
1176 // Remove ".rel" and ".rela" prefixes.
1177 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1178 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
1180 InfoSection = Asm.getContext().getELFSection(SectionName,
1181 ELF::SHT_PROGBITS, 0,
1182 SectionKind::getReadOnly());
1183 sh_info = SectionIndexMap.lookup(InfoSection);
1187 case ELF::SHT_SYMTAB:
1188 case ELF::SHT_DYNSYM:
1189 sh_link = StringTableIndex;
1190 sh_info = LastLocalSymbolIndex;
1193 case ELF::SHT_SYMTAB_SHNDX:
1194 sh_link = SymbolTableIndex;
1197 case ELF::SHT_PROGBITS:
1198 case ELF::SHT_STRTAB:
1199 case ELF::SHT_NOBITS:
1201 case ELF::SHT_ARM_ATTRIBUTES:
1205 case ELF::SHT_GROUP: {
1206 sh_link = SymbolTableIndex;
1207 sh_info = GroupSymbolIndex;
1212 assert(0 && "FIXME: sh_type value not supported!");
1216 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1217 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1218 Alignment, Section.getEntrySize());
1221 void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1222 const MCAsmLayout &Layout) {
1223 GroupMapTy GroupMap;
1224 RevGroupMapTy RevGroupMap;
1225 CreateGroupSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1228 SectionIndexMapTy SectionIndexMap;
1230 ComputeIndexMap(Asm, SectionIndexMap);
1232 // Compute symbol table information.
1233 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap);
1235 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
1236 const_cast<MCAsmLayout&>(Layout),
1239 // Update to include the metadata sections.
1240 ComputeIndexMap(Asm, SectionIndexMap);
1242 // Add 1 for the null section.
1243 unsigned NumSections = Asm.size() + 1;
1244 uint64_t NaturalAlignment = Is64Bit ? 8 : 4;
1245 uint64_t HeaderSize = Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr);
1246 uint64_t FileOff = HeaderSize;
1248 std::vector<const MCSectionELF*> Sections;
1249 Sections.resize(NumSections);
1251 for (SectionIndexMapTy::const_iterator i=
1252 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1253 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
1254 Sections[p.second] = p.first;
1257 for (unsigned i = 1; i < NumSections; ++i) {
1258 const MCSectionELF &Section = *Sections[i];
1259 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1261 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1263 // Get the size of the section in the output file (including padding).
1264 uint64_t Size = Layout.getSectionFileSize(&SD);
1269 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1271 // Write out the ELF header ...
1272 WriteHeader(FileOff - HeaderSize, NumSections);
1274 FileOff = HeaderSize;
1276 // ... then all of the sections ...
1277 DenseMap<const MCSection*, uint64_t> SectionOffsetMap;
1279 for (unsigned i = 1; i < NumSections; ++i) {
1280 const MCSectionELF &Section = *Sections[i];
1281 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1283 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1284 WriteZeros(Padding);
1287 // Remember the offset into the file for this section.
1288 SectionOffsetMap[&Section] = FileOff;
1290 FileOff += Layout.getSectionFileSize(&SD);
1292 Asm.WriteSectionData(&SD, Layout, this);
1295 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1296 WriteZeros(Padding);
1299 // ... and then the section header table.
1300 // Should we align the section header table?
1302 // Null section first.
1303 uint64_t FirstSectionSize =
1304 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1305 uint32_t FirstSectionLink =
1306 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1307 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
1309 for (unsigned i = 1; i < NumSections; ++i) {
1310 const MCSectionELF &Section = *Sections[i];
1311 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1312 uint32_t GroupSymbolIndex;
1313 if (Section.getType() != ELF::SHT_GROUP)
1314 GroupSymbolIndex = 0;
1316 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm, GroupMap[&Section]);
1318 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
1319 SectionOffsetMap[&Section], Layout.getSectionSize(&SD),
1320 SD.getAlignment(), Section);
1324 MCObjectWriter *llvm::createELFObjectWriter(raw_ostream &OS,
1326 Triple::OSType OSType,
1328 bool IsLittleEndian,
1329 bool HasRelocationAddend) {
1332 case ELF::EM_X86_64:
1333 return new X86ELFObjectWriter(OS, Is64Bit, IsLittleEndian, EMachine,
1334 HasRelocationAddend, OSType); break;
1336 return new ARMELFObjectWriter(OS, Is64Bit, IsLittleEndian, EMachine,
1337 HasRelocationAddend, OSType); break;
1338 default: llvm_unreachable("Unsupported architecture"); break;
1343 /// START OF SUBCLASSES for ELFObjectWriter
1344 //===- ARMELFObjectWriter -------------------------------------------===//
1346 ARMELFObjectWriter::ARMELFObjectWriter(raw_ostream &_OS, bool _Is64Bit,
1347 bool _IsLittleEndian,
1348 uint16_t _EMachine, bool _HasRelocationAddend,
1349 Triple::OSType _OSType)
1350 : ELFObjectWriter(_OS, _Is64Bit, _IsLittleEndian, _EMachine,
1351 _HasRelocationAddend, _OSType)
1354 ARMELFObjectWriter::~ARMELFObjectWriter()
1357 void ARMELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
1358 const MCAsmLayout &Layout,
1359 const MCFragment *Fragment,
1360 const MCFixup &Fixup,
1362 uint64_t &FixedValue) {
1363 assert(0 && "ARMELFObjectWriter::RecordRelocation() unimplemented");
1368 //===- X86ELFObjectWriter -------------------------------------------===//
1371 X86ELFObjectWriter::X86ELFObjectWriter(raw_ostream &_OS, bool _Is64Bit,
1372 bool _IsLittleEndian,
1373 uint16_t _EMachine, bool _HasRelocationAddend,
1374 Triple::OSType _OSType)
1375 : ELFObjectWriter(_OS, _Is64Bit, _IsLittleEndian, _EMachine,
1376 _HasRelocationAddend, _OSType)
1379 X86ELFObjectWriter::~X86ELFObjectWriter()
1382 void X86ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
1383 const MCAsmLayout &Layout,
1384 const MCFragment *Fragment,
1385 const MCFixup &Fixup,
1387 uint64_t &FixedValue) {
1390 int64_t Value = Target.getConstant();
1391 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
1392 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
1393 const MCSymbol *RelocSymbol = SymbolToReloc(Asm, Target, *Fragment);
1395 bool IsPCRel = isFixupKindX86PCRel(Fixup.getKind());
1396 if (!Target.isAbsolute()) {
1397 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
1398 const MCSymbol &SymbolB = RefB->getSymbol();
1399 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
1401 MCSectionData *Sec = Fragment->getParent();
1403 // Offset of the symbol in the section
1404 int64_t a = Layout.getSymbolAddress(&SDB) - Layout.getSectionAddress(Sec);
1406 // Ofeset of the relocation in the section
1407 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
1412 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
1413 MCFragment *F = SD.getFragment();
1415 Index = F->getParent()->getOrdinal();
1417 MCSectionData *FSD = F->getParent();
1418 // Offset of the symbol in the section
1419 Value += Layout.getSymbolAddress(&SD) - Layout.getSectionAddress(FSD);
1421 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
1422 WeakrefUsedInReloc.insert(RelocSymbol);
1424 UsedInReloc.insert(RelocSymbol);
1428 // Compensate for the addend on i386.
1435 // determine the type of the relocation
1437 MCSymbolRefExpr::VariantKind Modifier = Target.getSymA()->getKind();
1443 llvm_unreachable("Unimplemented");
1444 case MCSymbolRefExpr::VK_None:
1445 Type = ELF::R_X86_64_PC32;
1447 case MCSymbolRefExpr::VK_PLT:
1448 Type = ELF::R_X86_64_PLT32;
1450 case MCSymbolRefExpr::VK_GOTPCREL:
1451 Type = ELF::R_X86_64_GOTPCREL;
1453 case MCSymbolRefExpr::VK_GOTTPOFF:
1454 Type = ELF::R_X86_64_GOTTPOFF;
1456 case MCSymbolRefExpr::VK_TLSGD:
1457 Type = ELF::R_X86_64_TLSGD;
1459 case MCSymbolRefExpr::VK_TLSLD:
1460 Type = ELF::R_X86_64_TLSLD;
1464 switch ((unsigned)Fixup.getKind()) {
1465 default: llvm_unreachable("invalid fixup kind!");
1466 case FK_Data_8: Type = ELF::R_X86_64_64; break;
1467 case X86::reloc_signed_4byte:
1468 case X86::reloc_pcrel_4byte:
1469 assert(isInt<32>(Target.getConstant()));
1472 llvm_unreachable("Unimplemented");
1473 case MCSymbolRefExpr::VK_None:
1474 Type = ELF::R_X86_64_32S;
1476 case MCSymbolRefExpr::VK_GOT:
1477 Type = ELF::R_X86_64_GOT32;
1479 case MCSymbolRefExpr::VK_GOTPCREL:
1480 Type = ELF::R_X86_64_GOTPCREL;
1482 case MCSymbolRefExpr::VK_TPOFF:
1483 Type = ELF::R_X86_64_TPOFF32;
1485 case MCSymbolRefExpr::VK_DTPOFF:
1486 Type = ELF::R_X86_64_DTPOFF32;
1491 Type = ELF::R_X86_64_32;
1493 case FK_Data_2: Type = ELF::R_X86_64_16; break;
1494 case X86::reloc_pcrel_1byte:
1495 case FK_Data_1: Type = ELF::R_X86_64_8; break;
1502 llvm_unreachable("Unimplemented");
1503 case MCSymbolRefExpr::VK_None:
1504 Type = ELF::R_386_PC32;
1506 case MCSymbolRefExpr::VK_PLT:
1507 Type = ELF::R_386_PLT32;
1511 switch ((unsigned)Fixup.getKind()) {
1512 default: llvm_unreachable("invalid fixup kind!");
1514 case X86::reloc_global_offset_table:
1515 Type = ELF::R_386_GOTPC;
1518 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
1520 case X86::reloc_signed_4byte:
1521 case X86::reloc_pcrel_4byte:
1525 llvm_unreachable("Unimplemented");
1526 case MCSymbolRefExpr::VK_None:
1527 Type = ELF::R_386_32;
1529 case MCSymbolRefExpr::VK_GOT:
1530 Type = ELF::R_386_GOT32;
1532 case MCSymbolRefExpr::VK_GOTOFF:
1533 Type = ELF::R_386_GOTOFF;
1535 case MCSymbolRefExpr::VK_TLSGD:
1536 Type = ELF::R_386_TLS_GD;
1538 case MCSymbolRefExpr::VK_TPOFF:
1539 Type = ELF::R_386_TLS_LE_32;
1541 case MCSymbolRefExpr::VK_INDNTPOFF:
1542 Type = ELF::R_386_TLS_IE;
1544 case MCSymbolRefExpr::VK_NTPOFF:
1545 Type = ELF::R_386_TLS_LE;
1547 case MCSymbolRefExpr::VK_GOTNTPOFF:
1548 Type = ELF::R_386_TLS_GOTIE;
1550 case MCSymbolRefExpr::VK_TLSLDM:
1551 Type = ELF::R_386_TLS_LDM;
1553 case MCSymbolRefExpr::VK_DTPOFF:
1554 Type = ELF::R_386_TLS_LDO_32;
1558 case FK_Data_2: Type = ELF::R_386_16; break;
1559 case X86::reloc_pcrel_1byte:
1560 case FK_Data_1: Type = ELF::R_386_8; break;
1565 if (RelocNeedsGOT(Modifier))
1568 ELFRelocationEntry ERE;
1572 ERE.Symbol = RelocSymbol;
1574 ERE.r_offset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
1576 if (HasRelocationAddend)
1577 ERE.r_addend = Addend;
1579 ERE.r_addend = 0; // Silence compiler warning.
1581 Relocations[Fragment->getParent()].push_back(ERE);