blob: 71140f10e808ca7322c60b2fe90d5541494f2f07 [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);
Rafael Espindola7c18fa82011-03-20 18:44:20 +000053
54 void WriteDataSectionData(MCAssembler &Asm,
55 const MCAsmLayout &Layout,
56 const MCSectionELF &Section);
Jan Sjödin24b17c62011-03-03 14:52:12 +000057
58 /*static bool isFixupKindX86RIPRel(unsigned Kind) {
59 return Kind == X86::reloc_riprel_4byte ||
60 Kind == X86::reloc_riprel_4byte_movq_load;
61 }*/
62
63 /// ELFSymbolData - Helper struct for containing some precomputed
64 /// information on symbols.
65 struct ELFSymbolData {
66 MCSymbolData *SymbolData;
67 uint64_t StringIndex;
68 uint32_t SectionIndex;
69
70 // Support lexicographic sorting.
71 bool operator<(const ELFSymbolData &RHS) const {
72 if (MCELF::GetType(*SymbolData) == ELF::STT_FILE)
73 return true;
74 if (MCELF::GetType(*RHS.SymbolData) == ELF::STT_FILE)
75 return false;
76 return SymbolData->getSymbol().getName() <
77 RHS.SymbolData->getSymbol().getName();
78 }
79 };
80
81 /// @name Relocation Data
82 /// @{
83
84 struct ELFRelocationEntry {
85 // Make these big enough for both 32-bit and 64-bit
86 uint64_t r_offset;
87 int Index;
88 unsigned Type;
89 const MCSymbol *Symbol;
90 uint64_t r_addend;
91
92 ELFRelocationEntry()
93 : r_offset(0), Index(0), Type(0), Symbol(0), r_addend(0) {}
94
95 ELFRelocationEntry(uint64_t RelocOffset, int Idx,
96 unsigned RelType, const MCSymbol *Sym,
97 uint64_t Addend)
98 : r_offset(RelocOffset), Index(Idx), Type(RelType),
99 Symbol(Sym), r_addend(Addend) {}
100
101 // Support lexicographic sorting.
102 bool operator<(const ELFRelocationEntry &RE) const {
103 return RE.r_offset < r_offset;
104 }
105 };
106
107 /// The target specific ELF writer instance.
108 llvm::OwningPtr<MCELFObjectTargetWriter> TargetObjectWriter;
109
110 SmallPtrSet<const MCSymbol *, 16> UsedInReloc;
111 SmallPtrSet<const MCSymbol *, 16> WeakrefUsedInReloc;
112 DenseMap<const MCSymbol *, const MCSymbol *> Renames;
113
114 llvm::DenseMap<const MCSectionData*,
115 std::vector<ELFRelocationEntry> > Relocations;
116 DenseMap<const MCSection*, uint64_t> SectionStringTableIndex;
117
118 /// @}
119 /// @name Symbol Table Data
120 /// @{
121
122 SmallString<256> StringTable;
123 std::vector<ELFSymbolData> LocalSymbolData;
124 std::vector<ELFSymbolData> ExternalSymbolData;
125 std::vector<ELFSymbolData> UndefinedSymbolData;
126
127 /// @}
128
129 bool NeedsGOT;
130
131 bool NeedsSymtabShndx;
132
133 // This holds the symbol table index of the last local symbol.
134 unsigned LastLocalSymbolIndex;
135 // This holds the .strtab section index.
136 unsigned StringTableIndex;
137 // This holds the .symtab section index.
138 unsigned SymbolTableIndex;
139
140 unsigned ShstrtabIndex;
141
142
Jason W Kime964d112011-05-11 22:53:06 +0000143 virtual const MCSymbol *SymbolToReloc(const MCAssembler &Asm,
144 const MCValue &Target,
145 const MCFragment &F,
146 const MCFixup &Fixup,
147 bool IsPCRel) const;
Jan Sjödin24b17c62011-03-03 14:52:12 +0000148
149 // For arch-specific emission of explicit reloc symbol
150 virtual const MCSymbol *ExplicitRelSym(const MCAssembler &Asm,
151 const MCValue &Target,
152 const MCFragment &F,
Jason W Kime964d112011-05-11 22:53:06 +0000153 const MCFixup &Fixup,
154 bool IsPCRel) const {
Rafael Espindola69bbda02011-12-22 00:37:50 +0000155 return TargetObjectWriter->ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
Jan Sjödin24b17c62011-03-03 14:52:12 +0000156 }
157
158 bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
159 bool hasRelocationAddend() const {
160 return TargetObjectWriter->hasRelocationAddend();
161 }
162
163 public:
164 ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
165 raw_ostream &_OS, bool IsLittleEndian)
166 : MCObjectWriter(_OS, IsLittleEndian),
167 TargetObjectWriter(MOTW),
168 NeedsGOT(false), NeedsSymtabShndx(false){
169 }
170
171 virtual ~ELFObjectWriter();
172
173 void WriteWord(uint64_t W) {
174 if (is64Bit())
175 Write64(W);
176 else
177 Write32(W);
178 }
179
180 void StringLE16(char *buf, uint16_t Value) {
181 buf[0] = char(Value >> 0);
182 buf[1] = char(Value >> 8);
183 }
184
185 void StringLE32(char *buf, uint32_t Value) {
186 StringLE16(buf, uint16_t(Value >> 0));
187 StringLE16(buf + 2, uint16_t(Value >> 16));
188 }
189
190 void StringLE64(char *buf, uint64_t Value) {
191 StringLE32(buf, uint32_t(Value >> 0));
192 StringLE32(buf + 4, uint32_t(Value >> 32));
193 }
194
195 void StringBE16(char *buf ,uint16_t Value) {
196 buf[0] = char(Value >> 8);
197 buf[1] = char(Value >> 0);
198 }
199
200 void StringBE32(char *buf, uint32_t Value) {
201 StringBE16(buf, uint16_t(Value >> 16));
202 StringBE16(buf + 2, uint16_t(Value >> 0));
203 }
204
205 void StringBE64(char *buf, uint64_t Value) {
206 StringBE32(buf, uint32_t(Value >> 32));
207 StringBE32(buf + 4, uint32_t(Value >> 0));
208 }
209
210 void String8(MCDataFragment &F, uint8_t Value) {
211 char buf[1];
212 buf[0] = Value;
213 F.getContents() += StringRef(buf, 1);
214 }
215
216 void String16(MCDataFragment &F, uint16_t Value) {
217 char buf[2];
218 if (isLittleEndian())
219 StringLE16(buf, Value);
220 else
221 StringBE16(buf, Value);
222 F.getContents() += StringRef(buf, 2);
223 }
224
225 void String32(MCDataFragment &F, uint32_t Value) {
226 char buf[4];
227 if (isLittleEndian())
228 StringLE32(buf, Value);
229 else
230 StringBE32(buf, Value);
231 F.getContents() += StringRef(buf, 4);
232 }
233
234 void String64(MCDataFragment &F, uint64_t Value) {
235 char buf[8];
236 if (isLittleEndian())
237 StringLE64(buf, Value);
238 else
239 StringBE64(buf, Value);
240 F.getContents() += StringRef(buf, 8);
241 }
242
Jim Grosbach946227d2011-11-15 16:46:22 +0000243 virtual void WriteHeader(uint64_t SectionDataSize,
244 unsigned NumberOfSections);
Jan Sjödin24b17c62011-03-03 14:52:12 +0000245
Rafael Espindola69bbda02011-12-22 00:37:50 +0000246 virtual unsigned getEFlags() const {
247 return TargetObjectWriter->getEFlags();
248 }
Jan Sjödin24b17c62011-03-03 14:52:12 +0000249
Jim Grosbach946227d2011-11-15 16:46:22 +0000250 virtual void WriteSymbolEntry(MCDataFragment *SymtabF,
251 MCDataFragment *ShndxF,
252 uint64_t name, uint8_t info,
253 uint64_t value, uint64_t size,
254 uint8_t other, uint32_t shndx,
255 bool Reserved);
Jan Sjödin24b17c62011-03-03 14:52:12 +0000256
257 virtual void WriteSymbol(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
258 ELFSymbolData &MSD,
259 const MCAsmLayout &Layout);
260
261 typedef DenseMap<const MCSectionELF*, uint32_t> SectionIndexMapTy;
Jim Grosbach946227d2011-11-15 16:46:22 +0000262 virtual void WriteSymbolTable(MCDataFragment *SymtabF,
263 MCDataFragment *ShndxF,
264 const MCAssembler &Asm,
265 const MCAsmLayout &Layout,
266 const SectionIndexMapTy &SectionIndexMap);
Jan Sjödin24b17c62011-03-03 14:52:12 +0000267
Jim Grosbach946227d2011-11-15 16:46:22 +0000268 virtual void RecordRelocation(const MCAssembler &Asm,
269 const MCAsmLayout &Layout,
270 const MCFragment *Fragment,
271 const MCFixup &Fixup,
Jan Sjödin24b17c62011-03-03 14:52:12 +0000272 MCValue Target, uint64_t &FixedValue);
273
274 virtual uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm,
Jim Grosbach946227d2011-11-15 16:46:22 +0000275 const MCSymbol *S);
Jan Sjödin24b17c62011-03-03 14:52:12 +0000276
277 // Map from a group section to the signature symbol
278 typedef DenseMap<const MCSectionELF*, const MCSymbol*> GroupMapTy;
279 // Map from a signature symbol to the group section
280 typedef DenseMap<const MCSymbol*, const MCSectionELF*> RevGroupMapTy;
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000281 // Map from a section to the section with the relocations
282 typedef DenseMap<const MCSectionELF*, const MCSectionELF*> RelMapTy;
283 // Map from a section to its offset
284 typedef DenseMap<const MCSectionELF*, uint64_t> SectionOffsetMapTy;
Jan Sjödin24b17c62011-03-03 14:52:12 +0000285
286 /// ComputeSymbolTable - Compute the symbol table data
287 ///
288 /// \param StringTable [out] - The string table data.
289 /// \param StringIndexMap [out] - Map from symbol names to offsets in the
290 /// string table.
291 virtual void ComputeSymbolTable(MCAssembler &Asm,
292 const SectionIndexMapTy &SectionIndexMap,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000293 RevGroupMapTy RevGroupMap,
294 unsigned NumRegularSections);
Jan Sjödin24b17c62011-03-03 14:52:12 +0000295
296 virtual void ComputeIndexMap(MCAssembler &Asm,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000297 SectionIndexMapTy &SectionIndexMap,
298 const RelMapTy &RelMap);
Jan Sjödin24b17c62011-03-03 14:52:12 +0000299
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000300 void CreateRelocationSections(MCAssembler &Asm, MCAsmLayout &Layout,
301 RelMapTy &RelMap);
Jan Sjödin24b17c62011-03-03 14:52:12 +0000302
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000303 void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout,
304 const RelMapTy &RelMap);
Jan Sjödin24b17c62011-03-03 14:52:12 +0000305
306 virtual void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000307 SectionIndexMapTy &SectionIndexMap,
308 const RelMapTy &RelMap);
Jan Sjödin24b17c62011-03-03 14:52:12 +0000309
310 // Create the sections that show up in the symbol table. Currently
311 // those are the .note.GNU-stack section and the group sections.
312 virtual void CreateIndexedSections(MCAssembler &Asm, MCAsmLayout &Layout,
313 GroupMapTy &GroupMap,
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000314 RevGroupMapTy &RevGroupMap,
315 SectionIndexMapTy &SectionIndexMap,
316 const RelMapTy &RelMap);
Jan Sjödin24b17c62011-03-03 14:52:12 +0000317
318 virtual void ExecutePostLayoutBinding(MCAssembler &Asm,
319 const MCAsmLayout &Layout);
320
Rafael Espindola7c18fa82011-03-20 18:44:20 +0000321 void WriteSectionHeader(MCAssembler &Asm, const GroupMapTy &GroupMap,
322 const MCAsmLayout &Layout,
323 const SectionIndexMapTy &SectionIndexMap,
324 const SectionOffsetMapTy &SectionOffsetMap);
325
326 void ComputeSectionOrder(MCAssembler &Asm,
327 std::vector<const MCSectionELF*> &Sections);
328
Jan Sjödin24b17c62011-03-03 14:52:12 +0000329 virtual void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
330 uint64_t Address, uint64_t Offset,
331 uint64_t Size, uint32_t Link, uint32_t Info,
332 uint64_t Alignment, uint64_t EntrySize);
333
334 virtual void WriteRelocationsFragment(const MCAssembler &Asm,
335 MCDataFragment *F,
336 const MCSectionData *SD);
337
338 virtual bool
339 IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
340 const MCSymbolData &DataA,
341 const MCFragment &FB,
342 bool InSet,
343 bool IsPCRel) const;
344
345 virtual void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
346 virtual void WriteSection(MCAssembler &Asm,
347 const SectionIndexMapTy &SectionIndexMap,
348 uint32_t GroupSymbolIndex,
349 uint64_t Offset, uint64_t Size, uint64_t Alignment,
350 const MCSectionELF &Section);
351
352 protected:
353 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
354 bool IsPCRel, bool IsRelocWithSymbol,
Rafael Espindolaedae8e12011-12-21 17:30:17 +0000355 int64_t Addend) const;
Jim Grosbach946227d2011-11-15 16:46:22 +0000356 virtual void adjustFixupOffset(const MCFixup &Fixup,
357 uint64_t &RelocOffset) {}
Jan Sjödin24b17c62011-03-03 14:52:12 +0000358 };
359
Roman Divacky2c0d69f2011-08-02 15:51:38 +0000360 //===- PPCELFObjectWriter -------------------------------------------===//
361
362 class PPCELFObjectWriter : public ELFObjectWriter {
363 public:
364 PPCELFObjectWriter(MCELFObjectTargetWriter *MOTW,
365 raw_ostream &_OS,
366 bool IsLittleEndian);
367
368 virtual ~PPCELFObjectWriter();
369 protected:
370 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
371 bool IsPCRel, bool IsRelocWithSymbol,
Rafael Espindolac677e792011-12-21 14:26:29 +0000372 int64_t Addend) const;
Roman Divacky2a66cea2011-08-04 19:08:19 +0000373 virtual void adjustFixupOffset(const MCFixup &Fixup, uint64_t &RelocOffset);
Roman Divacky2c0d69f2011-08-02 15:51:38 +0000374 };
375
Jan Sjödin24b17c62011-03-03 14:52:12 +0000376 //===- MBlazeELFObjectWriter -------------------------------------------===//
377
378 class MBlazeELFObjectWriter : public ELFObjectWriter {
379 public:
380 MBlazeELFObjectWriter(MCELFObjectTargetWriter *MOTW,
381 raw_ostream &_OS,
382 bool IsLittleEndian);
383
384 virtual ~MBlazeELFObjectWriter();
385 protected:
386 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
387 bool IsPCRel, bool IsRelocWithSymbol,
Rafael Espindolac677e792011-12-21 14:26:29 +0000388 int64_t Addend) const;
Jan Sjödin24b17c62011-03-03 14:52:12 +0000389 };
Akira Hatanaka291512f2011-09-30 21:55:40 +0000390
391 //===- MipsELFObjectWriter -------------------------------------------===//
392
393 class MipsELFObjectWriter : public ELFObjectWriter {
394 public:
395 MipsELFObjectWriter(MCELFObjectTargetWriter *MOTW,
396 raw_ostream &_OS,
397 bool IsLittleEndian);
398
399 virtual ~MipsELFObjectWriter();
Rafael Espindolae99183d2011-12-22 00:21:50 +0000400 virtual unsigned getEFlags() const;
Akira Hatanaka84bfc2f2011-11-23 22:18:04 +0000401
Akira Hatanaka291512f2011-09-30 21:55:40 +0000402 protected:
Bruno Cardoso Lopesa00a62a2011-12-06 03:34:42 +0000403 virtual const MCSymbol *ExplicitRelSym(const MCAssembler &Asm,
404 const MCValue &Target,
405 const MCFragment &F,
406 const MCFixup &Fixup,
407 bool IsPCRel) const;
408
Akira Hatanaka291512f2011-09-30 21:55:40 +0000409 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
410 bool IsPCRel, bool IsRelocWithSymbol,
Rafael Espindolac677e792011-12-21 14:26:29 +0000411 int64_t Addend) const;
Akira Hatanaka291512f2011-09-30 21:55:40 +0000412 };
Jan Sjödin24b17c62011-03-03 14:52:12 +0000413}
414
415#endif