1 //===- lib/MC/ELFObjectWriter.h - 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 #ifndef LLVM_MC_ELFOBJECTWRITER_H
15 #define LLVM_MC_ELFOBJECTWRITER_H
18 #include "llvm/ADT/OwningPtr.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/ADT/SmallString.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/MC/MCAssembler.h"
23 #include "llvm/MC/MCELFObjectWriter.h"
24 #include "llvm/MC/MCELFSymbolFlags.h"
25 #include "llvm/MC/MCObjectWriter.h"
26 #include "llvm/MC/MCExpr.h"
27 #include "llvm/MC/MCSymbol.h"
37 class ELFObjectWriter : public MCObjectWriter {
40 static bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind);
41 static bool RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant);
42 static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout);
43 static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
44 bool Used, bool Renamed);
45 static bool isLocal(const MCSymbolData &Data, bool isSignature,
47 static bool IsELFMetaDataSection(const MCSectionData &SD);
48 static uint64_t DataSectionSize(const MCSectionData &SD);
49 static uint64_t GetSectionFileSize(const MCAsmLayout &Layout,
50 const MCSectionData &SD);
51 static uint64_t GetSectionAddressSize(const MCAsmLayout &Layout,
52 const MCSectionData &SD);
53 static void WriteDataSectionData(ELFObjectWriter *W,
54 const MCSectionData &SD);
56 /*static bool isFixupKindX86RIPRel(unsigned Kind) {
57 return Kind == X86::reloc_riprel_4byte ||
58 Kind == X86::reloc_riprel_4byte_movq_load;
61 /// ELFSymbolData - Helper struct for containing some precomputed
62 /// information on symbols.
63 struct ELFSymbolData {
64 MCSymbolData *SymbolData;
66 uint32_t SectionIndex;
68 // Support lexicographic sorting.
69 bool operator<(const ELFSymbolData &RHS) const {
70 if (MCELF::GetType(*SymbolData) == ELF::STT_FILE)
72 if (MCELF::GetType(*RHS.SymbolData) == ELF::STT_FILE)
74 return SymbolData->getSymbol().getName() <
75 RHS.SymbolData->getSymbol().getName();
79 /// @name Relocation Data
82 struct ELFRelocationEntry {
83 // Make these big enough for both 32-bit and 64-bit
87 const MCSymbol *Symbol;
91 : r_offset(0), Index(0), Type(0), Symbol(0), r_addend(0) {}
93 ELFRelocationEntry(uint64_t RelocOffset, int Idx,
94 unsigned RelType, const MCSymbol *Sym,
96 : r_offset(RelocOffset), Index(Idx), Type(RelType),
97 Symbol(Sym), r_addend(Addend) {}
99 // Support lexicographic sorting.
100 bool operator<(const ELFRelocationEntry &RE) const {
101 return RE.r_offset < r_offset;
105 /// The target specific ELF writer instance.
106 llvm::OwningPtr<MCELFObjectTargetWriter> TargetObjectWriter;
108 SmallPtrSet<const MCSymbol *, 16> UsedInReloc;
109 SmallPtrSet<const MCSymbol *, 16> WeakrefUsedInReloc;
110 DenseMap<const MCSymbol *, const MCSymbol *> Renames;
112 llvm::DenseMap<const MCSectionData*,
113 std::vector<ELFRelocationEntry> > Relocations;
114 DenseMap<const MCSection*, uint64_t> SectionStringTableIndex;
117 /// @name Symbol Table Data
120 SmallString<256> StringTable;
121 std::vector<ELFSymbolData> LocalSymbolData;
122 std::vector<ELFSymbolData> ExternalSymbolData;
123 std::vector<ELFSymbolData> UndefinedSymbolData;
129 bool NeedsSymtabShndx;
131 // This holds the symbol table index of the last local symbol.
132 unsigned LastLocalSymbolIndex;
133 // This holds the .strtab section index.
134 unsigned StringTableIndex;
135 // This holds the .symtab section index.
136 unsigned SymbolTableIndex;
138 unsigned ShstrtabIndex;
141 const MCSymbol *SymbolToReloc(const MCAssembler &Asm,
142 const MCValue &Target,
143 const MCFragment &F) const;
145 // For arch-specific emission of explicit reloc symbol
146 virtual const MCSymbol *ExplicitRelSym(const MCAssembler &Asm,
147 const MCValue &Target,
153 bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
154 bool hasRelocationAddend() const {
155 return TargetObjectWriter->hasRelocationAddend();
159 ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
160 raw_ostream &_OS, bool IsLittleEndian)
161 : MCObjectWriter(_OS, IsLittleEndian),
162 TargetObjectWriter(MOTW),
163 NeedsGOT(false), NeedsSymtabShndx(false){
166 virtual ~ELFObjectWriter();
168 void WriteWord(uint64_t W) {
175 void StringLE16(char *buf, uint16_t Value) {
176 buf[0] = char(Value >> 0);
177 buf[1] = char(Value >> 8);
180 void StringLE32(char *buf, uint32_t Value) {
181 StringLE16(buf, uint16_t(Value >> 0));
182 StringLE16(buf + 2, uint16_t(Value >> 16));
185 void StringLE64(char *buf, uint64_t Value) {
186 StringLE32(buf, uint32_t(Value >> 0));
187 StringLE32(buf + 4, uint32_t(Value >> 32));
190 void StringBE16(char *buf ,uint16_t Value) {
191 buf[0] = char(Value >> 8);
192 buf[1] = char(Value >> 0);
195 void StringBE32(char *buf, uint32_t Value) {
196 StringBE16(buf, uint16_t(Value >> 16));
197 StringBE16(buf + 2, uint16_t(Value >> 0));
200 void StringBE64(char *buf, uint64_t Value) {
201 StringBE32(buf, uint32_t(Value >> 32));
202 StringBE32(buf + 4, uint32_t(Value >> 0));
205 void String8(MCDataFragment &F, uint8_t Value) {
208 F.getContents() += StringRef(buf, 1);
211 void String16(MCDataFragment &F, uint16_t Value) {
213 if (isLittleEndian())
214 StringLE16(buf, Value);
216 StringBE16(buf, Value);
217 F.getContents() += StringRef(buf, 2);
220 void String32(MCDataFragment &F, uint32_t Value) {
222 if (isLittleEndian())
223 StringLE32(buf, Value);
225 StringBE32(buf, Value);
226 F.getContents() += StringRef(buf, 4);
229 void String64(MCDataFragment &F, uint64_t Value) {
231 if (isLittleEndian())
232 StringLE64(buf, Value);
234 StringBE64(buf, Value);
235 F.getContents() += StringRef(buf, 8);
238 virtual void WriteHeader(uint64_t SectionDataSize, unsigned NumberOfSections);
240 /// Default e_flags = 0
241 virtual void WriteEFlags() { Write32(0); }
243 virtual void WriteSymbolEntry(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
244 uint64_t name, uint8_t info,
245 uint64_t value, uint64_t size,
246 uint8_t other, uint32_t shndx,
249 virtual void WriteSymbol(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
251 const MCAsmLayout &Layout);
253 typedef DenseMap<const MCSectionELF*, uint32_t> SectionIndexMapTy;
254 virtual void WriteSymbolTable(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
255 const MCAssembler &Asm,
256 const MCAsmLayout &Layout,
257 const SectionIndexMapTy &SectionIndexMap);
259 virtual void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
260 const MCFragment *Fragment, const MCFixup &Fixup,
261 MCValue Target, uint64_t &FixedValue);
263 virtual uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm,
266 // Map from a group section to the signature symbol
267 typedef DenseMap<const MCSectionELF*, const MCSymbol*> GroupMapTy;
268 // Map from a signature symbol to the group section
269 typedef DenseMap<const MCSymbol*, const MCSectionELF*> RevGroupMapTy;
271 /// ComputeSymbolTable - Compute the symbol table data
273 /// \param StringTable [out] - The string table data.
274 /// \param StringIndexMap [out] - Map from symbol names to offsets in the
276 virtual void ComputeSymbolTable(MCAssembler &Asm,
277 const SectionIndexMapTy &SectionIndexMap,
278 RevGroupMapTy RevGroupMap);
280 virtual void ComputeIndexMap(MCAssembler &Asm,
281 SectionIndexMapTy &SectionIndexMap);
283 virtual void WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
284 const MCSectionData &SD);
286 virtual void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout) {
287 for (MCAssembler::const_iterator it = Asm.begin(),
288 ie = Asm.end(); it != ie; ++it) {
289 WriteRelocation(Asm, Layout, *it);
293 virtual void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout,
294 const SectionIndexMapTy &SectionIndexMap);
296 // Create the sections that show up in the symbol table. Currently
297 // those are the .note.GNU-stack section and the group sections.
298 virtual void CreateIndexedSections(MCAssembler &Asm, MCAsmLayout &Layout,
299 GroupMapTy &GroupMap,
300 RevGroupMapTy &RevGroupMap);
302 virtual void ExecutePostLayoutBinding(MCAssembler &Asm,
303 const MCAsmLayout &Layout);
305 virtual void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
306 uint64_t Address, uint64_t Offset,
307 uint64_t Size, uint32_t Link, uint32_t Info,
308 uint64_t Alignment, uint64_t EntrySize);
310 virtual void WriteRelocationsFragment(const MCAssembler &Asm,
312 const MCSectionData *SD);
315 IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
316 const MCSymbolData &DataA,
317 const MCFragment &FB,
321 virtual void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
322 virtual void WriteSection(MCAssembler &Asm,
323 const SectionIndexMapTy &SectionIndexMap,
324 uint32_t GroupSymbolIndex,
325 uint64_t Offset, uint64_t Size, uint64_t Alignment,
326 const MCSectionELF &Section);
329 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
330 bool IsPCRel, bool IsRelocWithSymbol,
334 //===- X86ELFObjectWriter -------------------------------------------===//
336 class X86ELFObjectWriter : public ELFObjectWriter {
338 X86ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
340 bool IsLittleEndian);
342 virtual ~X86ELFObjectWriter();
344 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
345 bool IsPCRel, bool IsRelocWithSymbol,
350 //===- ARMELFObjectWriter -------------------------------------------===//
352 class ARMELFObjectWriter : public ELFObjectWriter {
354 // FIXME: MCAssembler can't yet return the Subtarget,
355 enum { DefaultEABIVersion = 0x05000000U };
357 ARMELFObjectWriter(MCELFObjectTargetWriter *MOTW,
359 bool IsLittleEndian);
361 virtual ~ARMELFObjectWriter();
363 virtual void WriteEFlags();
365 virtual const MCSymbol *ExplicitRelSym(const MCAssembler &Asm,
366 const MCValue &Target,
370 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
371 bool IsPCRel, bool IsRelocWithSymbol,
375 //===- MBlazeELFObjectWriter -------------------------------------------===//
377 class MBlazeELFObjectWriter : public ELFObjectWriter {
379 MBlazeELFObjectWriter(MCELFObjectTargetWriter *MOTW,
381 bool IsLittleEndian);
383 virtual ~MBlazeELFObjectWriter();
385 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
386 bool IsPCRel, bool IsRelocWithSymbol,