blob: 9457623556dea0cd1eee7258ea2bcbd748ab5e61 [file] [log] [blame]
Jan Sjödin24b17c62011-03-03 14:52:12 +00001//===- lib/MC/ELFObjectWriter.h - ELF File Writer -------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements ELF object file writer information.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_MC_ELFOBJECTWRITER_H
15#define LLVM_MC_ELFOBJECTWRITER_H
16
17#include "MCELF.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"
28
29#include <vector>
30
31namespace llvm {
32
33class MCSection;
34class MCDataFragment;
35class MCSectionELF;
36
37class ELFObjectWriter : public MCObjectWriter {
38 protected:
39
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,
46 bool isUsedInReloc);
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);
55
56 /*static bool isFixupKindX86RIPRel(unsigned Kind) {
57 return Kind == X86::reloc_riprel_4byte ||
58 Kind == X86::reloc_riprel_4byte_movq_load;
59 }*/
60
61 /// ELFSymbolData - Helper struct for containing some precomputed
62 /// information on symbols.
63 struct ELFSymbolData {
64 MCSymbolData *SymbolData;
65 uint64_t StringIndex;
66 uint32_t SectionIndex;
67
68 // Support lexicographic sorting.
69 bool operator<(const ELFSymbolData &RHS) const {
70 if (MCELF::GetType(*SymbolData) == ELF::STT_FILE)
71 return true;
72 if (MCELF::GetType(*RHS.SymbolData) == ELF::STT_FILE)
73 return false;
74 return SymbolData->getSymbol().getName() <
75 RHS.SymbolData->getSymbol().getName();
76 }
77 };
78
79 /// @name Relocation Data
80 /// @{
81
82 struct ELFRelocationEntry {
83 // Make these big enough for both 32-bit and 64-bit
84 uint64_t r_offset;
85 int Index;
86 unsigned Type;
87 const MCSymbol *Symbol;
88 uint64_t r_addend;
89
90 ELFRelocationEntry()
91 : r_offset(0), Index(0), Type(0), Symbol(0), r_addend(0) {}
92
93 ELFRelocationEntry(uint64_t RelocOffset, int Idx,
94 unsigned RelType, const MCSymbol *Sym,
95 uint64_t Addend)
96 : r_offset(RelocOffset), Index(Idx), Type(RelType),
97 Symbol(Sym), r_addend(Addend) {}
98
99 // Support lexicographic sorting.
100 bool operator<(const ELFRelocationEntry &RE) const {
101 return RE.r_offset < r_offset;
102 }
103 };
104
105 /// The target specific ELF writer instance.
106 llvm::OwningPtr<MCELFObjectTargetWriter> TargetObjectWriter;
107
108 SmallPtrSet<const MCSymbol *, 16> UsedInReloc;
109 SmallPtrSet<const MCSymbol *, 16> WeakrefUsedInReloc;
110 DenseMap<const MCSymbol *, const MCSymbol *> Renames;
111
112 llvm::DenseMap<const MCSectionData*,
113 std::vector<ELFRelocationEntry> > Relocations;
114 DenseMap<const MCSection*, uint64_t> SectionStringTableIndex;
115
116 /// @}
117 /// @name Symbol Table Data
118 /// @{
119
120 SmallString<256> StringTable;
121 std::vector<ELFSymbolData> LocalSymbolData;
122 std::vector<ELFSymbolData> ExternalSymbolData;
123 std::vector<ELFSymbolData> UndefinedSymbolData;
124
125 /// @}
126
127 bool NeedsGOT;
128
129 bool NeedsSymtabShndx;
130
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;
137
138 unsigned ShstrtabIndex;
139
140
141 const MCSymbol *SymbolToReloc(const MCAssembler &Asm,
142 const MCValue &Target,
143 const MCFragment &F) const;
144
145 // For arch-specific emission of explicit reloc symbol
146 virtual const MCSymbol *ExplicitRelSym(const MCAssembler &Asm,
147 const MCValue &Target,
148 const MCFragment &F,
149 bool IsBSS) const {
150 return NULL;
151 }
152
153 bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
154 bool hasRelocationAddend() const {
155 return TargetObjectWriter->hasRelocationAddend();
156 }
157
158 public:
159 ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
160 raw_ostream &_OS, bool IsLittleEndian)
161 : MCObjectWriter(_OS, IsLittleEndian),
162 TargetObjectWriter(MOTW),
163 NeedsGOT(false), NeedsSymtabShndx(false){
164 }
165
166 virtual ~ELFObjectWriter();
167
168 void WriteWord(uint64_t W) {
169 if (is64Bit())
170 Write64(W);
171 else
172 Write32(W);
173 }
174
175 void StringLE16(char *buf, uint16_t Value) {
176 buf[0] = char(Value >> 0);
177 buf[1] = char(Value >> 8);
178 }
179
180 void StringLE32(char *buf, uint32_t Value) {
181 StringLE16(buf, uint16_t(Value >> 0));
182 StringLE16(buf + 2, uint16_t(Value >> 16));
183 }
184
185 void StringLE64(char *buf, uint64_t Value) {
186 StringLE32(buf, uint32_t(Value >> 0));
187 StringLE32(buf + 4, uint32_t(Value >> 32));
188 }
189
190 void StringBE16(char *buf ,uint16_t Value) {
191 buf[0] = char(Value >> 8);
192 buf[1] = char(Value >> 0);
193 }
194
195 void StringBE32(char *buf, uint32_t Value) {
196 StringBE16(buf, uint16_t(Value >> 16));
197 StringBE16(buf + 2, uint16_t(Value >> 0));
198 }
199
200 void StringBE64(char *buf, uint64_t Value) {
201 StringBE32(buf, uint32_t(Value >> 32));
202 StringBE32(buf + 4, uint32_t(Value >> 0));
203 }
204
205 void String8(MCDataFragment &F, uint8_t Value) {
206 char buf[1];
207 buf[0] = Value;
208 F.getContents() += StringRef(buf, 1);
209 }
210
211 void String16(MCDataFragment &F, uint16_t Value) {
212 char buf[2];
213 if (isLittleEndian())
214 StringLE16(buf, Value);
215 else
216 StringBE16(buf, Value);
217 F.getContents() += StringRef(buf, 2);
218 }
219
220 void String32(MCDataFragment &F, uint32_t Value) {
221 char buf[4];
222 if (isLittleEndian())
223 StringLE32(buf, Value);
224 else
225 StringBE32(buf, Value);
226 F.getContents() += StringRef(buf, 4);
227 }
228
229 void String64(MCDataFragment &F, uint64_t Value) {
230 char buf[8];
231 if (isLittleEndian())
232 StringLE64(buf, Value);
233 else
234 StringBE64(buf, Value);
235 F.getContents() += StringRef(buf, 8);
236 }
237
238 virtual void WriteHeader(uint64_t SectionDataSize, unsigned NumberOfSections);
239
240 /// Default e_flags = 0
241 virtual void WriteEFlags() { Write32(0); }
242
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,
247 bool Reserved);
248
249 virtual void WriteSymbol(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
250 ELFSymbolData &MSD,
251 const MCAsmLayout &Layout);
252
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);
258
259 virtual void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
260 const MCFragment *Fragment, const MCFixup &Fixup,
261 MCValue Target, uint64_t &FixedValue);
262
263 virtual uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm,
264 const MCSymbol *S);
265
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;
270
271 /// ComputeSymbolTable - Compute the symbol table data
272 ///
273 /// \param StringTable [out] - The string table data.
274 /// \param StringIndexMap [out] - Map from symbol names to offsets in the
275 /// string table.
276 virtual void ComputeSymbolTable(MCAssembler &Asm,
277 const SectionIndexMapTy &SectionIndexMap,
278 RevGroupMapTy RevGroupMap);
279
280 virtual void ComputeIndexMap(MCAssembler &Asm,
281 SectionIndexMapTy &SectionIndexMap);
282
283 virtual void WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
284 const MCSectionData &SD);
285
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);
290 }
291 }
292
293 virtual void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout,
294 const SectionIndexMapTy &SectionIndexMap);
295
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);
301
302 virtual void ExecutePostLayoutBinding(MCAssembler &Asm,
303 const MCAsmLayout &Layout);
304
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);
309
310 virtual void WriteRelocationsFragment(const MCAssembler &Asm,
311 MCDataFragment *F,
312 const MCSectionData *SD);
313
314 virtual bool
315 IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
316 const MCSymbolData &DataA,
317 const MCFragment &FB,
318 bool InSet,
319 bool IsPCRel) const;
320
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);
327
328 protected:
329 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
330 bool IsPCRel, bool IsRelocWithSymbol,
331 int64_t Addend) = 0;
332 };
333
334 //===- X86ELFObjectWriter -------------------------------------------===//
335
336 class X86ELFObjectWriter : public ELFObjectWriter {
337 public:
338 X86ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
339 raw_ostream &_OS,
340 bool IsLittleEndian);
341
342 virtual ~X86ELFObjectWriter();
343 protected:
344 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
345 bool IsPCRel, bool IsRelocWithSymbol,
346 int64_t Addend);
347 };
348
349
350 //===- ARMELFObjectWriter -------------------------------------------===//
351
352 class ARMELFObjectWriter : public ELFObjectWriter {
353 public:
354 // FIXME: MCAssembler can't yet return the Subtarget,
355 enum { DefaultEABIVersion = 0x05000000U };
356
357 ARMELFObjectWriter(MCELFObjectTargetWriter *MOTW,
358 raw_ostream &_OS,
359 bool IsLittleEndian);
360
361 virtual ~ARMELFObjectWriter();
362
363 virtual void WriteEFlags();
364 protected:
365 virtual const MCSymbol *ExplicitRelSym(const MCAssembler &Asm,
366 const MCValue &Target,
367 const MCFragment &F,
368 bool IsBSS) const;
369
370 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
371 bool IsPCRel, bool IsRelocWithSymbol,
372 int64_t Addend);
373 };
374
375 //===- MBlazeELFObjectWriter -------------------------------------------===//
376
377 class MBlazeELFObjectWriter : public ELFObjectWriter {
378 public:
379 MBlazeELFObjectWriter(MCELFObjectTargetWriter *MOTW,
380 raw_ostream &_OS,
381 bool IsLittleEndian);
382
383 virtual ~MBlazeELFObjectWriter();
384 protected:
385 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
386 bool IsPCRel, bool IsRelocWithSymbol,
387 int64_t Addend);
388 };
389}
390
391#endif