blob: bd5d048f26940c185d98b6905e65d4aefbc28841 [file] [log] [blame]
Matt Fleming3565a062010-08-16 18:57:57 +00001//===- lib/MC/ELFObjectWriter.cpp - 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
Rafael Espindola31f35782010-12-17 18:01:31 +000014#include "llvm/ADT/OwningPtr.h"
Rafael Espindola8f413fa2010-10-05 15:11:03 +000015#include "llvm/ADT/SmallPtrSet.h"
Matt Fleming3565a062010-08-16 18:57:57 +000016#include "llvm/ADT/STLExtras.h"
17#include "llvm/ADT/StringMap.h"
18#include "llvm/ADT/Twine.h"
19#include "llvm/MC/MCAssembler.h"
20#include "llvm/MC/MCAsmLayout.h"
21#include "llvm/MC/MCContext.h"
22#include "llvm/MC/MCELFSymbolFlags.h"
23#include "llvm/MC/MCExpr.h"
Rafael Espindola285b3e52010-12-17 16:59:53 +000024#include "llvm/MC/MCELFObjectWriter.h"
Matt Fleming3565a062010-08-16 18:57:57 +000025#include "llvm/MC/MCObjectWriter.h"
26#include "llvm/MC/MCSectionELF.h"
27#include "llvm/MC/MCSymbol.h"
28#include "llvm/MC/MCValue.h"
29#include "llvm/Support/Debug.h"
30#include "llvm/Support/ErrorHandling.h"
31#include "llvm/Support/ELF.h"
32#include "llvm/Target/TargetAsmBackend.h"
33
34#include "../Target/X86/X86FixupKinds.h"
Jason W Kim4a511f02010-11-22 18:41:13 +000035#include "../Target/ARM/ARMFixupKinds.h"
Matt Fleming3565a062010-08-16 18:57:57 +000036
37#include <vector>
38using namespace llvm;
39
Rafael Espindolaad49cf52010-09-18 15:03:21 +000040static unsigned GetType(const MCSymbolData &SD) {
41 uint32_t Type = (SD.getFlags() & (0xf << ELF_STT_Shift)) >> ELF_STT_Shift;
42 assert(Type == ELF::STT_NOTYPE || Type == ELF::STT_OBJECT ||
43 Type == ELF::STT_FUNC || Type == ELF::STT_SECTION ||
44 Type == ELF::STT_FILE || Type == ELF::STT_COMMON ||
45 Type == ELF::STT_TLS);
46 return Type;
47}
48
Rafael Espindolae15eb4e2010-09-23 19:55:14 +000049static unsigned GetBinding(const MCSymbolData &SD) {
50 uint32_t Binding = (SD.getFlags() & (0xf << ELF_STB_Shift)) >> ELF_STB_Shift;
51 assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
52 Binding == ELF::STB_WEAK);
53 return Binding;
54}
55
56static void SetBinding(MCSymbolData &SD, unsigned Binding) {
57 assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
58 Binding == ELF::STB_WEAK);
59 uint32_t OtherFlags = SD.getFlags() & ~(0xf << ELF_STB_Shift);
60 SD.setFlags(OtherFlags | (Binding << ELF_STB_Shift));
61}
62
Rafael Espindola152c1062010-10-06 21:02:29 +000063static unsigned GetVisibility(MCSymbolData &SD) {
64 unsigned Visibility =
65 (SD.getFlags() & (0xf << ELF_STV_Shift)) >> ELF_STV_Shift;
66 assert(Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_INTERNAL ||
67 Visibility == ELF::STV_HIDDEN || Visibility == ELF::STV_PROTECTED);
68 return Visibility;
69}
70
Wesley Peck4b047132010-11-21 22:06:28 +000071
Rafael Espindolaa0a2f872010-10-28 14:22:44 +000072static bool RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant) {
73 switch (Variant) {
Rafael Espindola5c77c162010-10-05 15:48:37 +000074 default:
75 return false;
Rafael Espindolaa0a2f872010-10-28 14:22:44 +000076 case MCSymbolRefExpr::VK_GOT:
77 case MCSymbolRefExpr::VK_PLT:
78 case MCSymbolRefExpr::VK_GOTPCREL:
79 case MCSymbolRefExpr::VK_TPOFF:
80 case MCSymbolRefExpr::VK_TLSGD:
81 case MCSymbolRefExpr::VK_GOTTPOFF:
82 case MCSymbolRefExpr::VK_INDNTPOFF:
83 case MCSymbolRefExpr::VK_NTPOFF:
84 case MCSymbolRefExpr::VK_GOTNTPOFF:
Rafael Espindolaa264f722010-10-28 14:37:09 +000085 case MCSymbolRefExpr::VK_TLSLDM:
Rafael Espindola0cf15d62010-10-28 14:48:59 +000086 case MCSymbolRefExpr::VK_DTPOFF:
Rafael Espindolab4d17212010-10-28 15:02:40 +000087 case MCSymbolRefExpr::VK_TLSLD:
Rafael Espindola5c77c162010-10-05 15:48:37 +000088 return true;
89 }
90}
91
Rafael Espindola127a6a42010-12-17 07:28:17 +000092static bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) {
93 const MCFixupKindInfo &FKI =
94 Asm.getBackend().getFixupKindInfo((MCFixupKind) Kind);
95
96 return FKI.Flags & MCFixupKindInfo::FKF_IsPCRel;
97}
98
Matt Fleming3565a062010-08-16 18:57:57 +000099namespace {
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000100 class ELFObjectWriter : public MCObjectWriter {
Jason W Kimd3443e92010-11-15 16:18:39 +0000101 protected:
Chris Lattnerb188a372010-08-28 03:21:03 +0000102 /*static bool isFixupKindX86RIPRel(unsigned Kind) {
Matt Fleming3565a062010-08-16 18:57:57 +0000103 return Kind == X86::reloc_riprel_4byte ||
104 Kind == X86::reloc_riprel_4byte_movq_load;
Chris Lattnerb188a372010-08-28 03:21:03 +0000105 }*/
Matt Fleming3565a062010-08-16 18:57:57 +0000106
107
108 /// ELFSymbolData - Helper struct for containing some precomputed information
109 /// on symbols.
110 struct ELFSymbolData {
111 MCSymbolData *SymbolData;
112 uint64_t StringIndex;
113 uint32_t SectionIndex;
114
115 // Support lexicographic sorting.
116 bool operator<(const ELFSymbolData &RHS) const {
Rafael Espindolaad49cf52010-09-18 15:03:21 +0000117 if (GetType(*SymbolData) == ELF::STT_FILE)
118 return true;
119 if (GetType(*RHS.SymbolData) == ELF::STT_FILE)
120 return false;
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000121 return SymbolData->getSymbol().getName() <
122 RHS.SymbolData->getSymbol().getName();
Matt Fleming3565a062010-08-16 18:57:57 +0000123 }
124 };
125
126 /// @name Relocation Data
127 /// @{
128
129 struct ELFRelocationEntry {
130 // Make these big enough for both 32-bit and 64-bit
131 uint64_t r_offset;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000132 int Index;
133 unsigned Type;
134 const MCSymbol *Symbol;
Matt Fleming3565a062010-08-16 18:57:57 +0000135 uint64_t r_addend;
Jason W Kim858e7502010-11-22 18:42:07 +0000136
Jason W Kim4a511f02010-11-22 18:41:13 +0000137 ELFRelocationEntry()
138 : r_offset(0), Index(0), Type(0), Symbol(0), r_addend(0) {}
139
Jason W Kim10907422010-11-22 22:05:16 +0000140 ELFRelocationEntry(uint64_t RelocOffset, int Idx,
141 unsigned RelType, const MCSymbol *Sym,
Jason W Kim4a511f02010-11-22 18:41:13 +0000142 uint64_t Addend)
Jason W Kim10907422010-11-22 22:05:16 +0000143 : r_offset(RelocOffset), Index(Idx), Type(RelType),
144 Symbol(Sym), r_addend(Addend) {}
Matt Fleming3565a062010-08-16 18:57:57 +0000145
146 // Support lexicographic sorting.
147 bool operator<(const ELFRelocationEntry &RE) const {
148 return RE.r_offset < r_offset;
149 }
150 };
151
Rafael Espindola31f35782010-12-17 18:01:31 +0000152 /// The target specific ELF writer instance.
153 llvm::OwningPtr<MCELFObjectTargetWriter> TargetObjectWriter;
154
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000155 SmallPtrSet<const MCSymbol *, 16> UsedInReloc;
Rafael Espindola484291c2010-11-01 14:28:48 +0000156 SmallPtrSet<const MCSymbol *, 16> WeakrefUsedInReloc;
Rafael Espindola88182132010-10-27 15:18:17 +0000157 DenseMap<const MCSymbol *, const MCSymbol *> Renames;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000158
Matt Fleming3565a062010-08-16 18:57:57 +0000159 llvm::DenseMap<const MCSectionData*,
160 std::vector<ELFRelocationEntry> > Relocations;
161 DenseMap<const MCSection*, uint64_t> SectionStringTableIndex;
162
163 /// @}
164 /// @name Symbol Table Data
165 /// @{
166
167 SmallString<256> StringTable;
168 std::vector<ELFSymbolData> LocalSymbolData;
169 std::vector<ELFSymbolData> ExternalSymbolData;
170 std::vector<ELFSymbolData> UndefinedSymbolData;
171
172 /// @}
173
Rafael Espindola5c77c162010-10-05 15:48:37 +0000174 bool NeedsGOT;
175
Rafael Espindola7be2c332010-10-31 00:16:26 +0000176 bool NeedsSymtabShndx;
177
Matt Fleming3565a062010-08-16 18:57:57 +0000178 // This holds the symbol table index of the last local symbol.
179 unsigned LastLocalSymbolIndex;
180 // This holds the .strtab section index.
181 unsigned StringTableIndex;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000182 // This holds the .symtab section index.
183 unsigned SymbolTableIndex;
Matt Fleming3565a062010-08-16 18:57:57 +0000184
185 unsigned ShstrtabIndex;
186
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000187
188 const MCSymbol *SymbolToReloc(const MCAssembler &Asm,
189 const MCValue &Target,
190 const MCFragment &F) const;
191
Rafael Espindolabff66a82010-12-18 03:27:34 +0000192 bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
193 bool hasRelocationAddend() const {
194 return TargetObjectWriter->hasRelocationAddend();
195 }
196
Matt Fleming3565a062010-08-16 18:57:57 +0000197 public:
Rafael Espindola31f35782010-12-17 18:01:31 +0000198 ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +0000199 raw_ostream &_OS, bool IsLittleEndian)
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000200 : MCObjectWriter(_OS, IsLittleEndian),
Rafael Espindola31f35782010-12-17 18:01:31 +0000201 TargetObjectWriter(MOTW),
Rafael Espindolabff66a82010-12-18 03:27:34 +0000202 NeedsGOT(false), NeedsSymtabShndx(false){
Matt Fleming3565a062010-08-16 18:57:57 +0000203 }
Jason W Kimd3443e92010-11-15 16:18:39 +0000204
205 virtual ~ELFObjectWriter();
206
Matt Fleming3565a062010-08-16 18:57:57 +0000207 void WriteWord(uint64_t W) {
Rafael Espindolabff66a82010-12-18 03:27:34 +0000208 if (is64Bit())
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000209 Write64(W);
Chris Lattnerb188a372010-08-28 03:21:03 +0000210 else
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000211 Write32(W);
Matt Fleming3565a062010-08-16 18:57:57 +0000212 }
213
Matt Fleming3565a062010-08-16 18:57:57 +0000214 void StringLE16(char *buf, uint16_t Value) {
215 buf[0] = char(Value >> 0);
216 buf[1] = char(Value >> 8);
217 }
218
219 void StringLE32(char *buf, uint32_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000220 StringLE16(buf, uint16_t(Value >> 0));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000221 StringLE16(buf + 2, uint16_t(Value >> 16));
Matt Fleming3565a062010-08-16 18:57:57 +0000222 }
223
224 void StringLE64(char *buf, uint64_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000225 StringLE32(buf, uint32_t(Value >> 0));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000226 StringLE32(buf + 4, uint32_t(Value >> 32));
Matt Fleming3565a062010-08-16 18:57:57 +0000227 }
228
229 void StringBE16(char *buf ,uint16_t Value) {
230 buf[0] = char(Value >> 8);
231 buf[1] = char(Value >> 0);
232 }
233
234 void StringBE32(char *buf, uint32_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000235 StringBE16(buf, uint16_t(Value >> 16));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000236 StringBE16(buf + 2, uint16_t(Value >> 0));
Matt Fleming3565a062010-08-16 18:57:57 +0000237 }
238
239 void StringBE64(char *buf, uint64_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000240 StringBE32(buf, uint32_t(Value >> 32));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000241 StringBE32(buf + 4, uint32_t(Value >> 0));
Matt Fleming3565a062010-08-16 18:57:57 +0000242 }
243
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000244 void String8(MCDataFragment &F, uint8_t Value) {
245 char buf[1];
246 buf[0] = Value;
247 F.getContents() += StringRef(buf, 1);
248 }
249
250 void String16(MCDataFragment &F, uint16_t Value) {
251 char buf[2];
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000252 if (isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000253 StringLE16(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000254 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000255 StringBE16(buf, Value);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000256 F.getContents() += StringRef(buf, 2);
Matt Fleming3565a062010-08-16 18:57:57 +0000257 }
258
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000259 void String32(MCDataFragment &F, uint32_t Value) {
260 char buf[4];
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000261 if (isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000262 StringLE32(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000263 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000264 StringBE32(buf, Value);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000265 F.getContents() += StringRef(buf, 4);
Matt Fleming3565a062010-08-16 18:57:57 +0000266 }
267
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000268 void String64(MCDataFragment &F, uint64_t Value) {
269 char buf[8];
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000270 if (isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000271 StringLE64(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000272 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000273 StringBE64(buf, Value);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000274 F.getContents() += StringRef(buf, 8);
Matt Fleming3565a062010-08-16 18:57:57 +0000275 }
276
Jason W Kimd3443e92010-11-15 16:18:39 +0000277 virtual void WriteHeader(uint64_t SectionDataSize, unsigned NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000278
Jason W Kim2d7a53a2011-02-04 21:41:11 +0000279 /// Default e_flags = 0
280 virtual void WriteEFlags() { Write32(0); }
281
Jason W Kimd3443e92010-11-15 16:18:39 +0000282 virtual void WriteSymbolEntry(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
Rafael Espindola7be2c332010-10-31 00:16:26 +0000283 uint64_t name, uint8_t info,
Matt Fleming3565a062010-08-16 18:57:57 +0000284 uint64_t value, uint64_t size,
Rafael Espindola7be2c332010-10-31 00:16:26 +0000285 uint8_t other, uint32_t shndx,
286 bool Reserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000287
Jason W Kimd3443e92010-11-15 16:18:39 +0000288 virtual void WriteSymbol(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
Rafael Espindola7be2c332010-10-31 00:16:26 +0000289 ELFSymbolData &MSD,
Matt Fleming3565a062010-08-16 18:57:57 +0000290 const MCAsmLayout &Layout);
291
Rafael Espindolabab2a802010-11-10 21:51:05 +0000292 typedef DenseMap<const MCSectionELF*, uint32_t> SectionIndexMapTy;
Jason W Kimd3443e92010-11-15 16:18:39 +0000293 virtual void WriteSymbolTable(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
Rafael Espindola7be2c332010-10-31 00:16:26 +0000294 const MCAssembler &Asm,
Rafael Espindola71859c62010-09-16 19:46:31 +0000295 const MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000296 const SectionIndexMapTy &SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +0000297
Jason W Kimd3443e92010-11-15 16:18:39 +0000298 virtual void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
Jason W Kim56a39902010-12-06 21:57:34 +0000299 const MCFragment *Fragment, const MCFixup &Fixup,
300 MCValue Target, uint64_t &FixedValue);
Matt Fleming3565a062010-08-16 18:57:57 +0000301
Jason W Kimd3443e92010-11-15 16:18:39 +0000302 virtual uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm,
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000303 const MCSymbol *S);
Matt Fleming3565a062010-08-16 18:57:57 +0000304
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000305 // Map from a group section to the signature symbol
306 typedef DenseMap<const MCSectionELF*, const MCSymbol*> GroupMapTy;
307 // Map from a signature symbol to the group section
308 typedef DenseMap<const MCSymbol*, const MCSectionELF*> RevGroupMapTy;
309
Matt Fleming3565a062010-08-16 18:57:57 +0000310 /// ComputeSymbolTable - Compute the symbol table data
311 ///
312 /// \param StringTable [out] - The string table data.
313 /// \param StringIndexMap [out] - Map from symbol names to offsets in the
314 /// string table.
Jason W Kimd3443e92010-11-15 16:18:39 +0000315 virtual void ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000316 const SectionIndexMapTy &SectionIndexMap,
317 RevGroupMapTy RevGroupMap);
Rafael Espindolabab2a802010-11-10 21:51:05 +0000318
Jason W Kimd3443e92010-11-15 16:18:39 +0000319 virtual void ComputeIndexMap(MCAssembler &Asm,
Rafael Espindolabab2a802010-11-10 21:51:05 +0000320 SectionIndexMapTy &SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +0000321
Jason W Kimd3443e92010-11-15 16:18:39 +0000322 virtual void WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
Matt Fleming3565a062010-08-16 18:57:57 +0000323 const MCSectionData &SD);
324
Jason W Kimd3443e92010-11-15 16:18:39 +0000325 virtual void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout) {
Matt Fleming3565a062010-08-16 18:57:57 +0000326 for (MCAssembler::const_iterator it = Asm.begin(),
327 ie = Asm.end(); it != ie; ++it) {
328 WriteRelocation(Asm, Layout, *it);
329 }
330 }
331
Jason W Kimd3443e92010-11-15 16:18:39 +0000332 virtual void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000333 const SectionIndexMapTy &SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +0000334
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000335 // Create the sections that show up in the symbol table. Currently
336 // those are the .note.GNU-stack section and the group sections.
337 virtual void CreateIndexedSections(MCAssembler &Asm, MCAsmLayout &Layout,
338 GroupMapTy &GroupMap,
339 RevGroupMapTy &RevGroupMap);
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000340
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000341 virtual void ExecutePostLayoutBinding(MCAssembler &Asm,
342 const MCAsmLayout &Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000343
Jason W Kimd3443e92010-11-15 16:18:39 +0000344 virtual void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
Matt Fleming3565a062010-08-16 18:57:57 +0000345 uint64_t Address, uint64_t Offset,
346 uint64_t Size, uint32_t Link, uint32_t Info,
347 uint64_t Alignment, uint64_t EntrySize);
348
Daniel Dunbar1f3662a2010-12-17 04:54:54 +0000349 virtual void WriteRelocationsFragment(const MCAssembler &Asm,
350 MCDataFragment *F,
351 const MCSectionData *SD);
352
Rafael Espindolafea753b2010-12-24 21:22:02 +0000353 virtual bool
354 IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
355 const MCSymbolData &DataA,
356 const MCFragment &FB,
357 bool InSet,
358 bool IsPCRel) const;
Rafael Espindola70703872010-09-30 02:22:20 +0000359
Jason W Kimd3443e92010-11-15 16:18:39 +0000360 virtual void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
361 virtual void WriteSection(MCAssembler &Asm,
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000362 const SectionIndexMapTy &SectionIndexMap,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000363 uint32_t GroupSymbolIndex,
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000364 uint64_t Offset, uint64_t Size, uint64_t Alignment,
365 const MCSectionELF &Section);
Jason W Kim56a39902010-12-06 21:57:34 +0000366
367 protected:
368 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
369 bool IsPCRel, bool IsRelocWithSymbol,
370 int64_t Addend) = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000371 };
372
Jason W Kimd3443e92010-11-15 16:18:39 +0000373 //===- X86ELFObjectWriter -------------------------------------------===//
374
375 class X86ELFObjectWriter : public ELFObjectWriter {
376 public:
Rafael Espindola31f35782010-12-17 18:01:31 +0000377 X86ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +0000378 raw_ostream &_OS,
379 bool IsLittleEndian);
Wesley Peck4b047132010-11-21 22:06:28 +0000380
Jason W Kimd3443e92010-11-15 16:18:39 +0000381 virtual ~X86ELFObjectWriter();
Jason W Kim56a39902010-12-06 21:57:34 +0000382 protected:
383 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
384 bool IsPCRel, bool IsRelocWithSymbol,
385 int64_t Addend);
Jason W Kimd3443e92010-11-15 16:18:39 +0000386 };
387
388
389 //===- ARMELFObjectWriter -------------------------------------------===//
390
391 class ARMELFObjectWriter : public ELFObjectWriter {
392 public:
Jason W Kim2d7a53a2011-02-04 21:41:11 +0000393 // FIXME: MCAssembler can't yet return the Subtarget,
394 enum { DefaultEABIVersion = 0x05000000U };
395
Rafael Espindola31f35782010-12-17 18:01:31 +0000396 ARMELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +0000397 raw_ostream &_OS,
398 bool IsLittleEndian);
Wesley Peck4b047132010-11-21 22:06:28 +0000399
Jason W Kimd3443e92010-11-15 16:18:39 +0000400 virtual ~ARMELFObjectWriter();
Jason W Kim2d7a53a2011-02-04 21:41:11 +0000401
402 virtual void WriteEFlags();
Jason W Kim85fed5e2010-12-01 02:40:06 +0000403 protected:
Jason W Kim56a39902010-12-06 21:57:34 +0000404 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
405 bool IsPCRel, bool IsRelocWithSymbol,
406 int64_t Addend);
Jason W Kimd3443e92010-11-15 16:18:39 +0000407 };
Wesley Peck4b047132010-11-21 22:06:28 +0000408
409 //===- MBlazeELFObjectWriter -------------------------------------------===//
410
411 class MBlazeELFObjectWriter : public ELFObjectWriter {
412 public:
Rafael Espindola31f35782010-12-17 18:01:31 +0000413 MBlazeELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +0000414 raw_ostream &_OS,
415 bool IsLittleEndian);
Wesley Peck4b047132010-11-21 22:06:28 +0000416
417 virtual ~MBlazeELFObjectWriter();
Jason W Kim56a39902010-12-06 21:57:34 +0000418 protected:
419 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
420 bool IsPCRel, bool IsRelocWithSymbol,
421 int64_t Addend);
Wesley Peck4b047132010-11-21 22:06:28 +0000422 };
Matt Fleming3565a062010-08-16 18:57:57 +0000423}
424
Jason W Kimd3443e92010-11-15 16:18:39 +0000425ELFObjectWriter::~ELFObjectWriter()
426{}
427
Matt Fleming3565a062010-08-16 18:57:57 +0000428// Emit the ELF header.
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000429void ELFObjectWriter::WriteHeader(uint64_t SectionDataSize,
430 unsigned NumberOfSections) {
Matt Fleming3565a062010-08-16 18:57:57 +0000431 // ELF Header
432 // ----------
433 //
434 // Note
435 // ----
436 // emitWord method behaves differently for ELF32 and ELF64, writing
437 // 4 bytes in the former and 8 in the latter.
438
439 Write8(0x7f); // e_ident[EI_MAG0]
440 Write8('E'); // e_ident[EI_MAG1]
441 Write8('L'); // e_ident[EI_MAG2]
442 Write8('F'); // e_ident[EI_MAG3]
443
Rafael Espindolabff66a82010-12-18 03:27:34 +0000444 Write8(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
Matt Fleming3565a062010-08-16 18:57:57 +0000445
446 // e_ident[EI_DATA]
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000447 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
Matt Fleming3565a062010-08-16 18:57:57 +0000448
449 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky5baf79e2010-09-09 17:57:50 +0000450 // e_ident[EI_OSABI]
Rafael Espindolabff66a82010-12-18 03:27:34 +0000451 switch (TargetObjectWriter->getOSType()) {
Roman Divacky5baf79e2010-09-09 17:57:50 +0000452 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
453 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
454 default: Write8(ELF::ELFOSABI_NONE); break;
455 }
Matt Fleming3565a062010-08-16 18:57:57 +0000456 Write8(0); // e_ident[EI_ABIVERSION]
457
458 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
459
460 Write16(ELF::ET_REL); // e_type
461
Rafael Espindolabff66a82010-12-18 03:27:34 +0000462 Write16(TargetObjectWriter->getEMachine()); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +0000463
464 Write32(ELF::EV_CURRENT); // e_version
465 WriteWord(0); // e_entry, no entry point in .o file
466 WriteWord(0); // e_phoff, no program header for .o
Rafael Espindolabff66a82010-12-18 03:27:34 +0000467 WriteWord(SectionDataSize + (is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
Benjamin Kramereb976772010-08-17 17:02:29 +0000468 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000469
Jason W Kim2d7a53a2011-02-04 21:41:11 +0000470 // e_flags = whatever the target wants
471 WriteEFlags();
Matt Fleming3565a062010-08-16 18:57:57 +0000472
473 // e_ehsize = ELF header size
Rafael Espindolabff66a82010-12-18 03:27:34 +0000474 Write16(is64Bit() ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
Matt Fleming3565a062010-08-16 18:57:57 +0000475
476 Write16(0); // e_phentsize = prog header entry size
477 Write16(0); // e_phnum = # prog header entries = 0
478
479 // e_shentsize = Section header entry size
Rafael Espindolabff66a82010-12-18 03:27:34 +0000480 Write16(is64Bit() ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
Matt Fleming3565a062010-08-16 18:57:57 +0000481
482 // e_shnum = # of section header ents
Rafael Espindola7be2c332010-10-31 00:16:26 +0000483 if (NumberOfSections >= ELF::SHN_LORESERVE)
484 Write16(0);
485 else
486 Write16(NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000487
488 // e_shstrndx = Section # of '.shstrtab'
Rafael Espindola7be2c332010-10-31 00:16:26 +0000489 if (NumberOfSections >= ELF::SHN_LORESERVE)
490 Write16(ELF::SHN_XINDEX);
491 else
492 Write16(ShstrtabIndex);
Matt Fleming3565a062010-08-16 18:57:57 +0000493}
494
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000495void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
496 MCDataFragment *ShndxF,
497 uint64_t name,
498 uint8_t info, uint64_t value,
499 uint64_t size, uint8_t other,
500 uint32_t shndx,
501 bool Reserved) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000502 if (ShndxF) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000503 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000504 String32(*ShndxF, shndx);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000505 else
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000506 String32(*ShndxF, 0);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000507 }
508
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000509 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
510 uint16_t(ELF::SHN_XINDEX) : shndx;
511
Rafael Espindolabff66a82010-12-18 03:27:34 +0000512 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000513 String32(*SymtabF, name); // st_name
514 String8(*SymtabF, info); // st_info
515 String8(*SymtabF, other); // st_other
516 String16(*SymtabF, Index); // st_shndx
517 String64(*SymtabF, value); // st_value
518 String64(*SymtabF, size); // st_size
Matt Fleming3565a062010-08-16 18:57:57 +0000519 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000520 String32(*SymtabF, name); // st_name
521 String32(*SymtabF, value); // st_value
522 String32(*SymtabF, size); // st_size
523 String8(*SymtabF, info); // st_info
524 String8(*SymtabF, other); // st_other
525 String16(*SymtabF, Index); // st_shndx
Matt Fleming3565a062010-08-16 18:57:57 +0000526 }
527}
528
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000529static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout) {
530 if (Data.isCommon() && Data.isExternal())
531 return Data.getCommonAlignment();
532
533 const MCSymbol &Symbol = Data.getSymbol();
Roman Divackyd1491862010-12-20 21:14:39 +0000534
535 if (Symbol.isAbsolute() && Symbol.isVariable()) {
536 if (const MCExpr *Value = Symbol.getVariableValue()) {
537 int64_t IntValue;
538 if (Value->EvaluateAsAbsolute(IntValue, Layout))
539 return (uint64_t)IntValue;
540 }
541 }
542
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000543 if (!Symbol.isInSection())
544 return 0;
545
Rafael Espindolaffd902b2010-12-06 02:57:26 +0000546 if (Data.getFragment())
547 return Layout.getSymbolOffset(&Data);
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000548
549 return 0;
550}
551
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000552void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
553 const MCAsmLayout &Layout) {
Rafael Espindola88182132010-10-27 15:18:17 +0000554 // The presence of symbol versions causes undefined symbols and
555 // versions declared with @@@ to be renamed.
556
557 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
558 ie = Asm.symbol_end(); it != ie; ++it) {
559 const MCSymbol &Alias = it->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000560 const MCSymbol &Symbol = Alias.AliasedSymbol();
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000561 MCSymbolData &SD = Asm.getSymbolData(Symbol);
562
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000563 // Not an alias.
564 if (&Symbol == &Alias)
565 continue;
566
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000567 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000568 size_t Pos = AliasName.find('@');
569 if (Pos == StringRef::npos)
570 continue;
571
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000572 // Aliases defined with .symvar copy the binding from the symbol they alias.
573 // This is the first place we are able to copy this information.
574 it->setExternal(SD.isExternal());
575 SetBinding(*it, GetBinding(SD));
576
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000577 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000578 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
579 continue;
580
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000581 // FIXME: produce a better error message.
582 if (Symbol.isUndefined() && Rest.startswith("@@") &&
583 !Rest.startswith("@@@"))
584 report_fatal_error("A @@ version cannot be undefined");
585
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000586 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000587 }
588}
589
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000590void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
591 MCDataFragment *ShndxF,
592 ELFSymbolData &MSD,
593 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000594 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000595 MCSymbolData &Data =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000596 Layout.getAssembler().getSymbolData(OrigData.getSymbol().AliasedSymbol());
Rafael Espindola152c1062010-10-06 21:02:29 +0000597
Rafael Espindola7be2c332010-10-31 00:16:26 +0000598 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
599 Data.getSymbol().isVariable();
600
Rafael Espindola152c1062010-10-06 21:02:29 +0000601 uint8_t Binding = GetBinding(OrigData);
602 uint8_t Visibility = GetVisibility(OrigData);
603 uint8_t Type = GetType(Data);
604
605 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
606 uint8_t Other = Visibility;
607
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000608 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000609 uint64_t Size = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000610
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000611 assert(!(Data.isCommon() && !Data.isExternal()));
612
Rafael Espindolaf0121242010-12-22 16:03:00 +0000613 const MCExpr *ESize = Data.getSize();
614 if (ESize) {
615 int64_t Res;
616 if (!ESize->EvaluateAsAbsolute(Res, Layout))
617 report_fatal_error("Size expression must be absolute.");
618 Size = Res;
Matt Fleming3565a062010-08-16 18:57:57 +0000619 }
620
621 // Write out the symbol table entry
Rafael Espindola7be2c332010-10-31 00:16:26 +0000622 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
623 Size, Other, MSD.SectionIndex, IsReserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000624}
625
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000626void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
627 MCDataFragment *ShndxF,
628 const MCAssembler &Asm,
629 const MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000630 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000631 // The string table must be emitted first because we need the index
632 // into the string table for all the symbol names.
633 assert(StringTable.size() && "Missing string table");
634
635 // FIXME: Make sure the start of the symbol table is aligned.
636
637 // The first entry is the undefined symbol entry.
Rafael Espindola7be2c332010-10-31 00:16:26 +0000638 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
Matt Fleming3565a062010-08-16 18:57:57 +0000639
640 // Write the symbol table entries.
641 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
642 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
643 ELFSymbolData &MSD = LocalSymbolData[i];
Rafael Espindola7be2c332010-10-31 00:16:26 +0000644 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000645 }
646
Rafael Espindola71859c62010-09-16 19:46:31 +0000647 // Write out a symbol table entry for each regular section.
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000648 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
649 ++i) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000650 const MCSectionELF &Section =
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000651 static_cast<const MCSectionELF&>(i->getSection());
652 if (Section.getType() == ELF::SHT_RELA ||
653 Section.getType() == ELF::SHT_REL ||
654 Section.getType() == ELF::SHT_STRTAB ||
655 Section.getType() == ELF::SHT_SYMTAB)
Eli Friedmana44fa242010-08-16 21:17:09 +0000656 continue;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000657 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000658 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section), false);
Eli Friedmana44fa242010-08-16 21:17:09 +0000659 LastLocalSymbolIndex++;
660 }
Matt Fleming3565a062010-08-16 18:57:57 +0000661
662 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
663 ELFSymbolData &MSD = ExternalSymbolData[i];
664 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000665 assert(((Data.getFlags() & ELF_STB_Global) ||
666 (Data.getFlags() & ELF_STB_Weak)) &&
667 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000668 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000669 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000670 LastLocalSymbolIndex++;
671 }
672
673 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
674 ELFSymbolData &MSD = UndefinedSymbolData[i];
675 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000676 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000677 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000678 LastLocalSymbolIndex++;
679 }
680}
681
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000682const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm,
683 const MCValue &Target,
684 const MCFragment &F) const {
685 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000686 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
687 const MCSymbol *Renamed = Renames.lookup(&Symbol);
688 const MCSymbolData &SD = Asm.getSymbolData(Symbol);
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000689
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000690 if (ASymbol.isUndefined()) {
691 if (Renamed)
692 return Renamed;
693 return &ASymbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000694 }
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000695
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000696 if (SD.isExternal()) {
697 if (Renamed)
698 return Renamed;
699 return &Symbol;
700 }
Rafael Espindola73ffea42010-09-25 05:42:19 +0000701
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000702 const MCSectionELF &Section =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000703 static_cast<const MCSectionELF&>(ASymbol.getSection());
Rafael Espindola25958732010-11-24 21:57:39 +0000704 const SectionKind secKind = Section.getKind();
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000705
Rafael Espindola25958732010-11-24 21:57:39 +0000706 if (secKind.isBSS())
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000707 return NULL;
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000708
Rafael Espindola25958732010-11-24 21:57:39 +0000709 if (secKind.isThreadLocal()) {
710 if (Renamed)
711 return Renamed;
712 return &Symbol;
713 }
714
Rafael Espindola8cecf252010-10-06 16:23:36 +0000715 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000716 const MCSectionELF &Sec2 =
717 static_cast<const MCSectionELF&>(F.getParent()->getSection());
718
Rafael Espindola8cecf252010-10-06 16:23:36 +0000719 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000720 (Kind == MCSymbolRefExpr::VK_PLT ||
721 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
Rafael Espindola25958732010-11-24 21:57:39 +0000722 Kind == MCSymbolRefExpr::VK_GOTOFF)) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000723 if (Renamed)
724 return Renamed;
725 return &Symbol;
726 }
Rafael Espindola3729d002010-10-05 23:57:26 +0000727
Rafael Espindola1c130262011-01-23 04:43:11 +0000728 if (Section.getFlags() & ELF::SHF_MERGE) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000729 if (Target.getConstant() == 0)
730 return NULL;
731 if (Renamed)
732 return Renamed;
733 return &Symbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000734 }
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000735
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000736 return NULL;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000737}
738
Matt Fleming3565a062010-08-16 18:57:57 +0000739
Jason W Kim56a39902010-12-06 21:57:34 +0000740void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
741 const MCAsmLayout &Layout,
742 const MCFragment *Fragment,
743 const MCFixup &Fixup,
744 MCValue Target,
745 uint64_t &FixedValue) {
746 int64_t Addend = 0;
747 int Index = 0;
748 int64_t Value = Target.getConstant();
749 const MCSymbol *RelocSymbol = NULL;
750
Rafael Espindola127a6a42010-12-17 07:28:17 +0000751 bool IsPCRel = isFixupKindPCRel(Asm, Fixup.getKind());
Jason W Kim56a39902010-12-06 21:57:34 +0000752 if (!Target.isAbsolute()) {
753 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
754 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
755 RelocSymbol = SymbolToReloc(Asm, Target, *Fragment);
756
757 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
758 const MCSymbol &SymbolB = RefB->getSymbol();
759 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
760 IsPCRel = true;
761
762 // Offset of the symbol in the section
763 int64_t a = Layout.getSymbolOffset(&SDB);
764
765 // Ofeset of the relocation in the section
766 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
767 Value += b - a;
768 }
769
770 if (!RelocSymbol) {
771 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
772 MCFragment *F = SD.getFragment();
773
774 Index = F->getParent()->getOrdinal() + 1;
775
776 // Offset of the symbol in the section
777 Value += Layout.getSymbolOffset(&SD);
778 } else {
779 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
780 WeakrefUsedInReloc.insert(RelocSymbol);
781 else
782 UsedInReloc.insert(RelocSymbol);
783 Index = -1;
784 }
785 Addend = Value;
786 // Compensate for the addend on i386.
Rafael Espindolabff66a82010-12-18 03:27:34 +0000787 if (is64Bit())
Jason W Kim56a39902010-12-06 21:57:34 +0000788 Value = 0;
789 }
790
791 FixedValue = Value;
792 unsigned Type = GetRelocType(Target, Fixup, IsPCRel,
793 (RelocSymbol != 0), Addend);
794
795 uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
796 Fixup.getOffset();
797
Rafael Espindolabff66a82010-12-18 03:27:34 +0000798 if (!hasRelocationAddend())
799 Addend = 0;
Jason W Kim56a39902010-12-06 21:57:34 +0000800 ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend);
801 Relocations[Fragment->getParent()].push_back(ERE);
802}
803
804
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000805uint64_t
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000806ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
807 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000808 MCSymbolData &SD = Asm.getSymbolData(*S);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000809 return SD.getIndex();
Matt Fleming3565a062010-08-16 18:57:57 +0000810}
811
Rafael Espindola737cd212010-10-05 18:01:23 +0000812static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
Rafael Espindola88182132010-10-27 15:18:17 +0000813 bool Used, bool Renamed) {
Rafael Espindola484291c2010-11-01 14:28:48 +0000814 if (Data.getFlags() & ELF_Other_Weakref)
815 return false;
816
Rafael Espindolabd701182010-10-19 19:31:37 +0000817 if (Used)
818 return true;
819
Rafael Espindola88182132010-10-27 15:18:17 +0000820 if (Renamed)
821 return false;
822
Rafael Espindola737cd212010-10-05 18:01:23 +0000823 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000824
Rafael Espindolad1798862010-10-29 23:09:31 +0000825 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
826 return true;
827
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000828 const MCSymbol &A = Symbol.AliasedSymbol();
Rafael Espindolad1798862010-10-29 23:09:31 +0000829 if (!A.isVariable() && A.isUndefined() && !Data.isCommon())
Rafael Espindolaa6866962010-10-27 14:44:52 +0000830 return false;
831
Rafael Espindola737cd212010-10-05 18:01:23 +0000832 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
833 return false;
834
Rafael Espindolabd701182010-10-19 19:31:37 +0000835 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000836 return false;
837
838 return true;
839}
840
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000841static bool isLocal(const MCSymbolData &Data, bool isSignature,
842 bool isUsedInReloc) {
Rafael Espindola737cd212010-10-05 18:01:23 +0000843 if (Data.isExternal())
844 return false;
845
846 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000847 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000848
849 if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
850 if (isSignature && !isUsedInReloc)
851 return true;
852
Rafael Espindola737cd212010-10-05 18:01:23 +0000853 return false;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000854 }
Rafael Espindola737cd212010-10-05 18:01:23 +0000855
856 return true;
857}
858
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000859void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
860 SectionIndexMapTy &SectionIndexMap) {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000861 unsigned Index = 1;
862 for (MCAssembler::iterator it = Asm.begin(),
863 ie = Asm.end(); it != ie; ++it) {
864 const MCSectionELF &Section =
865 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000866 if (Section.getType() != ELF::SHT_GROUP)
867 continue;
868 SectionIndexMap[&Section] = Index++;
869 }
870
871 for (MCAssembler::iterator it = Asm.begin(),
872 ie = Asm.end(); it != ie; ++it) {
873 const MCSectionELF &Section =
874 static_cast<const MCSectionELF &>(it->getSection());
875 if (Section.getType() == ELF::SHT_GROUP)
876 continue;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000877 SectionIndexMap[&Section] = Index++;
878 }
879}
880
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000881void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000882 const SectionIndexMapTy &SectionIndexMap,
883 RevGroupMapTy RevGroupMap) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000884 // FIXME: Is this the correct place to do this?
885 if (NeedsGOT) {
886 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
887 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
888 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
889 Data.setExternal(true);
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000890 SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindola5c77c162010-10-05 15:48:37 +0000891 }
892
Matt Fleming3565a062010-08-16 18:57:57 +0000893 // Build section lookup table.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000894 int NumRegularSections = Asm.size();
Matt Fleming3565a062010-08-16 18:57:57 +0000895
896 // Index 0 is always the empty string.
897 StringMap<uint64_t> StringIndexMap;
898 StringTable += '\x00';
899
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000900 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000901 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
902 ie = Asm.symbol_end(); it != ie; ++it) {
903 const MCSymbol &Symbol = it->getSymbol();
904
Rafael Espindola484291c2010-11-01 14:28:48 +0000905 bool Used = UsedInReloc.count(&Symbol);
906 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000907 bool isSignature = RevGroupMap.count(&Symbol);
908
909 if (!isInSymtab(Asm, *it,
910 Used || WeakrefUsed || isSignature,
Rafael Espindola88182132010-10-27 15:18:17 +0000911 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000912 continue;
913
Matt Fleming3565a062010-08-16 18:57:57 +0000914 ELFSymbolData MSD;
915 MSD.SymbolData = it;
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000916 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Matt Fleming3565a062010-08-16 18:57:57 +0000917
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000918 // Undefined symbols are global, but this is the first place we
919 // are able to set it.
920 bool Local = isLocal(*it, isSignature, Used);
921 if (!Local && GetBinding(*it) == ELF::STB_LOCAL) {
922 MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
923 SetBinding(*it, ELF::STB_GLOBAL);
924 SetBinding(SD, ELF::STB_GLOBAL);
925 }
926
Rafael Espindola484291c2010-11-01 14:28:48 +0000927 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
928 SetBinding(*it, ELF::STB_WEAK);
929
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000930 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000931 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000932 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000933 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000934 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000935 } else if (RefSymbol.isUndefined()) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000936 if (isSignature && !Used)
937 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
938 else
939 MSD.SectionIndex = ELF::SHN_UNDEF;
Matt Fleming3565a062010-08-16 18:57:57 +0000940 } else {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000941 const MCSectionELF &Section =
942 static_cast<const MCSectionELF&>(RefSymbol.getSection());
943 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000944 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
945 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000946 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000947 }
948
Rafael Espindola88182132010-10-27 15:18:17 +0000949 // The @@@ in symbol version is replaced with @ in undefined symbols and
950 // @@ in defined ones.
951 StringRef Name = Symbol.getName();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000952 SmallString<32> Buf;
953
Rafael Espindola88182132010-10-27 15:18:17 +0000954 size_t Pos = Name.find("@@@");
Rafael Espindola88182132010-10-27 15:18:17 +0000955 if (Pos != StringRef::npos) {
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000956 Buf += Name.substr(0, Pos);
957 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
958 Buf += Name.substr(Pos + Skip);
959 Name = Buf;
Rafael Espindola88182132010-10-27 15:18:17 +0000960 }
961
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000962 uint64_t &Entry = StringIndexMap[Name];
Rafael Espindolaa6866962010-10-27 14:44:52 +0000963 if (!Entry) {
964 Entry = StringTable.size();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000965 StringTable += Name;
Rafael Espindolaa6866962010-10-27 14:44:52 +0000966 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +0000967 }
Rafael Espindolaa6866962010-10-27 14:44:52 +0000968 MSD.StringIndex = Entry;
969 if (MSD.SectionIndex == ELF::SHN_UNDEF)
970 UndefinedSymbolData.push_back(MSD);
971 else if (Local)
972 LocalSymbolData.push_back(MSD);
973 else
974 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +0000975 }
976
977 // Symbols are required to be in lexicographic order.
978 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
979 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
980 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
981
982 // Set the symbol indices. Local symbols must come before all other
983 // symbols with non-local bindings.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000984 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000985 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
986 LocalSymbolData[i].SymbolData->setIndex(Index++);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000987
988 Index += NumRegularSections;
989
Matt Fleming3565a062010-08-16 18:57:57 +0000990 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
991 ExternalSymbolData[i].SymbolData->setIndex(Index++);
992 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
993 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
994}
995
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000996void ELFObjectWriter::WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
997 const MCSectionData &SD) {
Matt Fleming3565a062010-08-16 18:57:57 +0000998 if (!Relocations[&SD].empty()) {
999 MCContext &Ctx = Asm.getContext();
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001000 const MCSectionELF *RelaSection;
Matt Fleming3565a062010-08-16 18:57:57 +00001001 const MCSectionELF &Section =
1002 static_cast<const MCSectionELF&>(SD.getSection());
1003
1004 const StringRef SectionName = Section.getSectionName();
Rafael Espindolabff66a82010-12-18 03:27:34 +00001005 std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +00001006 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +00001007
1008 unsigned EntrySize;
Rafael Espindolabff66a82010-12-18 03:27:34 +00001009 if (hasRelocationAddend())
1010 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
Benjamin Kramer299fbe32010-08-17 17:56:13 +00001011 else
Rafael Espindolabff66a82010-12-18 03:27:34 +00001012 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +00001013
Rafael Espindolabff66a82010-12-18 03:27:34 +00001014 RelaSection = Ctx.getELFSection(RelaSectionName, hasRelocationAddend() ?
Benjamin Kramer377a5722010-08-17 17:30:07 +00001015 ELF::SHT_RELA : ELF::SHT_REL, 0,
Matt Fleming3565a062010-08-16 18:57:57 +00001016 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001017 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001018
1019 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +00001020 RelaSD.setAlignment(is64Bit() ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +00001021
1022 MCDataFragment *F = new MCDataFragment(&RelaSD);
1023
1024 WriteRelocationsFragment(Asm, F, &SD);
Matt Fleming3565a062010-08-16 18:57:57 +00001025 }
1026}
1027
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001028void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
1029 uint64_t Flags, uint64_t Address,
1030 uint64_t Offset, uint64_t Size,
1031 uint32_t Link, uint32_t Info,
1032 uint64_t Alignment,
1033 uint64_t EntrySize) {
Matt Fleming3565a062010-08-16 18:57:57 +00001034 Write32(Name); // sh_name: index into string table
1035 Write32(Type); // sh_type
1036 WriteWord(Flags); // sh_flags
1037 WriteWord(Address); // sh_addr
1038 WriteWord(Offset); // sh_offset
1039 WriteWord(Size); // sh_size
1040 Write32(Link); // sh_link
1041 Write32(Info); // sh_info
1042 WriteWord(Alignment); // sh_addralign
1043 WriteWord(EntrySize); // sh_entsize
1044}
1045
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001046void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
1047 MCDataFragment *F,
1048 const MCSectionData *SD) {
Matt Fleming3565a062010-08-16 18:57:57 +00001049 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
1050 // sort by the r_offset just like gnu as does
1051 array_pod_sort(Relocs.begin(), Relocs.end());
1052
1053 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1054 ELFRelocationEntry entry = Relocs[e - i - 1];
1055
Rafael Espindola12203cc2010-11-21 00:48:25 +00001056 if (!entry.Index)
1057 ;
1058 else if (entry.Index < 0)
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001059 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
1060 else
Rafael Espindola12203cc2010-11-21 00:48:25 +00001061 entry.Index += LocalSymbolData.size();
Rafael Espindolabff66a82010-12-18 03:27:34 +00001062 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001063 String64(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001064
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001065 struct ELF::Elf64_Rela ERE64;
1066 ERE64.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001067 String64(*F, ERE64.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001068
Rafael Espindolabff66a82010-12-18 03:27:34 +00001069 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001070 String64(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001071 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001072 String32(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001073
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001074 struct ELF::Elf32_Rela ERE32;
1075 ERE32.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001076 String32(*F, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001077
Rafael Espindolabff66a82010-12-18 03:27:34 +00001078 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001079 String32(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001080 }
Matt Fleming3565a062010-08-16 18:57:57 +00001081 }
1082}
1083
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001084void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
1085 MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001086 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +00001087 MCContext &Ctx = Asm.getContext();
1088 MCDataFragment *F;
1089
Rafael Espindolabff66a82010-12-18 03:27:34 +00001090 unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
Matt Fleming3565a062010-08-16 18:57:57 +00001091
Rafael Espindola38738bf2010-09-22 19:04:41 +00001092 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001093 const MCSectionELF *ShstrtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001094 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001095 SectionKind::getReadOnly());
Rafael Espindola71859c62010-09-16 19:46:31 +00001096 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1097 ShstrtabSD.setAlignment(1);
1098 ShstrtabIndex = Asm.size();
1099
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001100 const MCSectionELF *SymtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001101 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1102 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001103 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001104 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +00001105 SymtabSD.setAlignment(is64Bit() ? 8 : 4);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001106 SymbolTableIndex = Asm.size();
1107
1108 MCSectionData *SymtabShndxSD = NULL;
1109
1110 if (NeedsSymtabShndx) {
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001111 const MCSectionELF *SymtabShndxSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001112 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001113 SectionKind::getReadOnly(), 4, "");
Rafael Espindola7be2c332010-10-31 00:16:26 +00001114 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
1115 SymtabShndxSD->setAlignment(4);
1116 }
Matt Fleming3565a062010-08-16 18:57:57 +00001117
Matt Fleming3565a062010-08-16 18:57:57 +00001118 const MCSection *StrtabSection;
1119 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001120 SectionKind::getReadOnly());
Matt Fleming3565a062010-08-16 18:57:57 +00001121 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1122 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +00001123 StringTableIndex = Asm.size();
1124
Rafael Espindolac3c413f2010-09-27 22:04:54 +00001125 WriteRelocations(Asm, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001126
1127 // Symbol table
1128 F = new MCDataFragment(&SymtabSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001129 MCDataFragment *ShndxF = NULL;
1130 if (NeedsSymtabShndx) {
1131 ShndxF = new MCDataFragment(SymtabShndxSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001132 }
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001133 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
Rafael Espindola71859c62010-09-16 19:46:31 +00001134
Matt Fleming3565a062010-08-16 18:57:57 +00001135 F = new MCDataFragment(&StrtabSD);
1136 F->getContents().append(StringTable.begin(), StringTable.end());
Matt Fleming3565a062010-08-16 18:57:57 +00001137
Matt Fleming3565a062010-08-16 18:57:57 +00001138 F = new MCDataFragment(&ShstrtabSD);
1139
Matt Fleming3565a062010-08-16 18:57:57 +00001140 // Section header string table.
1141 //
1142 // The first entry of a string table holds a null character so skip
1143 // section 0.
1144 uint64_t Index = 1;
1145 F->getContents() += '\x00';
1146
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001147 StringMap<uint64_t> SecStringMap;
Matt Fleming3565a062010-08-16 18:57:57 +00001148 for (MCAssembler::const_iterator it = Asm.begin(),
1149 ie = Asm.end(); it != ie; ++it) {
Matt Fleming3565a062010-08-16 18:57:57 +00001150 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +00001151 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola51efe7a2010-09-23 14:14:56 +00001152 // FIXME: We could merge suffixes like in .text and .rela.text.
Matt Fleming3565a062010-08-16 18:57:57 +00001153
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001154 StringRef Name = Section.getSectionName();
1155 if (SecStringMap.count(Name)) {
1156 SectionStringTableIndex[&Section] = SecStringMap[Name];
1157 continue;
1158 }
Matt Fleming3565a062010-08-16 18:57:57 +00001159 // Remember the index into the string table so we can write it
1160 // into the sh_name field of the section header table.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001161 SectionStringTableIndex[&Section] = Index;
1162 SecStringMap[Name] = Index;
Matt Fleming3565a062010-08-16 18:57:57 +00001163
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001164 Index += Name.size() + 1;
1165 F->getContents() += Name;
Matt Fleming3565a062010-08-16 18:57:57 +00001166 F->getContents() += '\x00';
1167 }
Rafael Espindola70703872010-09-30 02:22:20 +00001168}
1169
Rafael Espindolafea753b2010-12-24 21:22:02 +00001170bool
1171ELFObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
1172 const MCSymbolData &DataA,
1173 const MCFragment &FB,
1174 bool InSet,
1175 bool IsPCRel) const {
Rafael Espindola2c920852010-11-16 04:11:46 +00001176 // FIXME: This is in here just to match gnu as output. If the two ends
1177 // are in the same section, there is nothing that the linker can do to
1178 // break it.
Rafael Espindola70703872010-09-30 02:22:20 +00001179 if (DataA.isExternal())
1180 return false;
1181
Rafael Espindolafea753b2010-12-24 21:22:02 +00001182 const MCSection &SecA = DataA.getSymbol().AliasedSymbol().getSection();
1183 const MCSection &SecB = FB.getParent()->getSection();
1184 // On ELF A - B is absolute if A and B are in the same section.
1185 return &SecA == &SecB;
Matt Fleming3565a062010-08-16 18:57:57 +00001186}
1187
Rafael Espindola96aa78c2011-01-23 17:55:27 +00001188void ELFObjectWriter::CreateIndexedSections(MCAssembler &Asm,
1189 MCAsmLayout &Layout,
1190 GroupMapTy &GroupMap,
1191 RevGroupMapTy &RevGroupMap) {
1192 // Create the .note.GNU-stack section if needed.
1193 MCContext &Ctx = Asm.getContext();
1194 if (Asm.getNoExecStack()) {
1195 const MCSectionELF *GnuStackSection =
1196 Ctx.getELFSection(".note.GNU-stack", ELF::SHT_PROGBITS, 0,
1197 SectionKind::getReadOnly());
1198 Asm.getOrCreateSectionData(*GnuStackSection);
1199 }
1200
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001201 // Build the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001202 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1203 it != ie; ++it) {
1204 const MCSectionELF &Section =
1205 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola1c130262011-01-23 04:43:11 +00001206 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001207 continue;
1208
1209 const MCSymbol *SignatureSymbol = Section.getGroup();
1210 Asm.getOrCreateSymbolData(*SignatureSymbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001211 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001212 if (!Group) {
Rafael Espindola96aa78c2011-01-23 17:55:27 +00001213 Group = Ctx.CreateELFGroupSection();
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001214 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1215 Data.setAlignment(4);
1216 MCDataFragment *F = new MCDataFragment(&Data);
1217 String32(*F, ELF::GRP_COMDAT);
1218 }
1219 GroupMap[Group] = SignatureSymbol;
1220 }
1221
1222 // Add sections to the groups
1223 unsigned Index = 1;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001224 unsigned NumGroups = RevGroupMap.size();
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001225 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1226 it != ie; ++it, ++Index) {
1227 const MCSectionELF &Section =
1228 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola1c130262011-01-23 04:43:11 +00001229 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001230 continue;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001231 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001232 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1233 // FIXME: we could use the previous fragment
1234 MCDataFragment *F = new MCDataFragment(&Data);
1235 String32(*F, NumGroups + Index);
1236 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001237}
1238
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001239void ELFObjectWriter::WriteSection(MCAssembler &Asm,
1240 const SectionIndexMapTy &SectionIndexMap,
1241 uint32_t GroupSymbolIndex,
1242 uint64_t Offset, uint64_t Size,
1243 uint64_t Alignment,
1244 const MCSectionELF &Section) {
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001245 uint64_t sh_link = 0;
1246 uint64_t sh_info = 0;
1247
1248 switch(Section.getType()) {
1249 case ELF::SHT_DYNAMIC:
1250 sh_link = SectionStringTableIndex[&Section];
1251 sh_info = 0;
1252 break;
1253
1254 case ELF::SHT_REL:
1255 case ELF::SHT_RELA: {
1256 const MCSectionELF *SymtabSection;
1257 const MCSectionELF *InfoSection;
1258 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
1259 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001260 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001261 sh_link = SectionIndexMap.lookup(SymtabSection);
1262 assert(sh_link && ".symtab not found");
1263
1264 // Remove ".rel" and ".rela" prefixes.
1265 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1266 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
1267
1268 InfoSection = Asm.getContext().getELFSection(SectionName,
1269 ELF::SHT_PROGBITS, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001270 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001271 sh_info = SectionIndexMap.lookup(InfoSection);
1272 break;
1273 }
1274
1275 case ELF::SHT_SYMTAB:
1276 case ELF::SHT_DYNSYM:
1277 sh_link = StringTableIndex;
1278 sh_info = LastLocalSymbolIndex;
1279 break;
1280
1281 case ELF::SHT_SYMTAB_SHNDX:
1282 sh_link = SymbolTableIndex;
1283 break;
1284
1285 case ELF::SHT_PROGBITS:
1286 case ELF::SHT_STRTAB:
1287 case ELF::SHT_NOBITS:
Rafael Espindola98976612010-12-26 21:30:59 +00001288 case ELF::SHT_NOTE:
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001289 case ELF::SHT_NULL:
1290 case ELF::SHT_ARM_ATTRIBUTES:
Jason W Kim86a97f22011-01-12 00:19:25 +00001291 case ELF::SHT_INIT_ARRAY:
1292 case ELF::SHT_FINI_ARRAY:
1293 case ELF::SHT_PREINIT_ARRAY:
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +00001294 case ELF::SHT_X86_64_UNWIND:
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001295 // Nothing to do.
1296 break;
1297
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001298 case ELF::SHT_GROUP: {
1299 sh_link = SymbolTableIndex;
1300 sh_info = GroupSymbolIndex;
1301 break;
1302 }
1303
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001304 default:
1305 assert(0 && "FIXME: sh_type value not supported!");
1306 break;
1307 }
1308
1309 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1310 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1311 Alignment, Section.getEntrySize());
1312}
1313
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001314static bool IsELFMetaDataSection(const MCSectionData &SD) {
Rafael Espindolaf8803fe2010-12-06 03:48:09 +00001315 return SD.getOrdinal() == ~UINT32_C(0) &&
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001316 !SD.getSection().isVirtualSection();
1317}
1318
1319static uint64_t DataSectionSize(const MCSectionData &SD) {
1320 uint64_t Ret = 0;
1321 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1322 ++i) {
1323 const MCFragment &F = *i;
1324 assert(F.getKind() == MCFragment::FT_Data);
1325 Ret += cast<MCDataFragment>(F).getContents().size();
1326 }
1327 return Ret;
1328}
1329
1330static uint64_t GetSectionFileSize(const MCAsmLayout &Layout,
1331 const MCSectionData &SD) {
1332 if (IsELFMetaDataSection(SD))
1333 return DataSectionSize(SD);
1334 return Layout.getSectionFileSize(&SD);
1335}
1336
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001337static uint64_t GetSectionAddressSize(const MCAsmLayout &Layout,
1338 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001339 if (IsELFMetaDataSection(SD))
1340 return DataSectionSize(SD);
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001341 return Layout.getSectionAddressSize(&SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001342}
1343
1344static void WriteDataSectionData(ELFObjectWriter *W, const MCSectionData &SD) {
1345 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1346 ++i) {
1347 const MCFragment &F = *i;
1348 assert(F.getKind() == MCFragment::FT_Data);
1349 W->WriteBytes(cast<MCDataFragment>(F).getContents().str());
1350 }
1351}
1352
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001353void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1354 const MCAsmLayout &Layout) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001355 GroupMapTy GroupMap;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001356 RevGroupMapTy RevGroupMap;
Rafael Espindola96aa78c2011-01-23 17:55:27 +00001357 CreateIndexedSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1358 RevGroupMap);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001359
Rafael Espindolabab2a802010-11-10 21:51:05 +00001360 SectionIndexMapTy SectionIndexMap;
1361
1362 ComputeIndexMap(Asm, SectionIndexMap);
1363
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001364 // Compute symbol table information.
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001365 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap);
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001366
Matt Fleming3565a062010-08-16 18:57:57 +00001367 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001368 const_cast<MCAsmLayout&>(Layout),
1369 SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +00001370
Rafael Espindola1d739a02010-11-10 22:34:07 +00001371 // Update to include the metadata sections.
1372 ComputeIndexMap(Asm, SectionIndexMap);
1373
Matt Fleming3565a062010-08-16 18:57:57 +00001374 // Add 1 for the null section.
1375 unsigned NumSections = Asm.size() + 1;
Rafael Espindolabff66a82010-12-18 03:27:34 +00001376 uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
1377 uint64_t HeaderSize = is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
1378 sizeof(ELF::Elf32_Ehdr);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001379 uint64_t FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001380
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001381 std::vector<const MCSectionELF*> Sections;
1382 Sections.resize(NumSections);
1383
1384 for (SectionIndexMapTy::const_iterator i=
1385 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1386 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
1387 Sections[p.second] = p.first;
1388 }
1389
1390 for (unsigned i = 1; i < NumSections; ++i) {
1391 const MCSectionELF &Section = *Sections[i];
1392 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001393
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001394 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1395
Matt Fleming3565a062010-08-16 18:57:57 +00001396 // Get the size of the section in the output file (including padding).
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001397 FileOff += GetSectionFileSize(Layout, SD);
Matt Fleming3565a062010-08-16 18:57:57 +00001398 }
1399
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001400 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1401
Matt Fleming3565a062010-08-16 18:57:57 +00001402 // Write out the ELF header ...
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001403 WriteHeader(FileOff - HeaderSize, NumSections);
1404
1405 FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001406
1407 // ... then all of the sections ...
1408 DenseMap<const MCSection*, uint64_t> SectionOffsetMap;
1409
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001410 for (unsigned i = 1; i < NumSections; ++i) {
1411 const MCSectionELF &Section = *Sections[i];
1412 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001413
1414 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1415 WriteZeros(Padding);
1416 FileOff += Padding;
1417
Matt Fleming3565a062010-08-16 18:57:57 +00001418 // Remember the offset into the file for this section.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001419 SectionOffsetMap[&Section] = FileOff;
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001420
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001421 FileOff += GetSectionFileSize(Layout, SD);
Matt Fleming3565a062010-08-16 18:57:57 +00001422
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001423 if (IsELFMetaDataSection(SD))
1424 WriteDataSectionData(this, SD);
1425 else
Daniel Dunbar5d2477c2010-12-17 02:45:59 +00001426 Asm.WriteSectionData(&SD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +00001427 }
1428
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001429 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1430 WriteZeros(Padding);
1431 FileOff += Padding;
1432
Matt Fleming3565a062010-08-16 18:57:57 +00001433 // ... and then the section header table.
1434 // Should we align the section header table?
1435 //
1436 // Null section first.
Rafael Espindola7be2c332010-10-31 00:16:26 +00001437 uint64_t FirstSectionSize =
1438 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1439 uint32_t FirstSectionLink =
1440 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1441 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming3565a062010-08-16 18:57:57 +00001442
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001443 for (unsigned i = 1; i < NumSections; ++i) {
1444 const MCSectionELF &Section = *Sections[i];
1445 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1446 uint32_t GroupSymbolIndex;
1447 if (Section.getType() != ELF::SHT_GROUP)
1448 GroupSymbolIndex = 0;
1449 else
1450 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm, GroupMap[&Section]);
Matt Fleming3565a062010-08-16 18:57:57 +00001451
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001452 uint64_t Size = GetSectionAddressSize(Layout, SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001453
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001454 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001455 SectionOffsetMap[&Section], Size,
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001456 SD.getAlignment(), Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001457 }
1458}
1459
Rafael Espindola6024c972010-12-17 17:45:22 +00001460MCObjectWriter *llvm::createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1461 raw_ostream &OS,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001462 bool IsLittleEndian) {
1463 switch (MOTW->getEMachine()) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001464 case ELF::EM_386:
1465 case ELF::EM_X86_64:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001466 return new X86ELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001467 case ELF::EM_ARM:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001468 return new ARMELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Wesley Peck4b047132010-11-21 22:06:28 +00001469 case ELF::EM_MBLAZE:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001470 return new MBlazeELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Benjamin Kramer32858772010-11-15 19:20:50 +00001471 default: llvm_unreachable("Unsupported architecture"); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001472 }
1473}
1474
1475
1476/// START OF SUBCLASSES for ELFObjectWriter
1477//===- ARMELFObjectWriter -------------------------------------------===//
1478
Rafael Espindola31f35782010-12-17 18:01:31 +00001479ARMELFObjectWriter::ARMELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001480 raw_ostream &_OS,
1481 bool IsLittleEndian)
1482 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
Jason W Kimd3443e92010-11-15 16:18:39 +00001483{}
1484
1485ARMELFObjectWriter::~ARMELFObjectWriter()
1486{}
1487
Jason W Kim2d7a53a2011-02-04 21:41:11 +00001488// FIXME: get the real EABI Version from the Triple.
1489void ARMELFObjectWriter::WriteEFlags() {
1490 Write32(ELF::EF_ARM_EABIMASK & DefaultEABIVersion);
1491}
1492
Jason W Kim85fed5e2010-12-01 02:40:06 +00001493unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target,
1494 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001495 bool IsPCRel,
1496 bool IsRelocWithSymbol,
1497 int64_t Addend) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001498 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1499 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
1500
Jason W Kima0871e72010-12-08 23:14:44 +00001501 unsigned Type = 0;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001502 if (IsPCRel) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001503 switch ((unsigned)Fixup.getKind()) {
1504 default: assert(0 && "Unimplemented");
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001505 case FK_Data_4:
1506 switch (Modifier) {
1507 default: llvm_unreachable("Unsupported Modifier");
1508 case MCSymbolRefExpr::VK_None:
Jason W Kim1d866172011-01-13 00:07:51 +00001509 Type = ELF::R_ARM_BASE_PREL;
1510 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001511 case MCSymbolRefExpr::VK_ARM_TLSGD:
Jason W Kim1d866172011-01-13 00:07:51 +00001512 assert(0 && "unimplemented");
1513 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001514 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
1515 Type = ELF::R_ARM_TLS_IE32;
Jason W Kim1d866172011-01-13 00:07:51 +00001516 break;
1517 }
1518 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001519 case ARM::fixup_arm_uncondbranch:
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001520 switch (Modifier) {
1521 case MCSymbolRefExpr::VK_ARM_PLT:
Jason W Kim1d866172011-01-13 00:07:51 +00001522 Type = ELF::R_ARM_PLT32;
1523 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001524 default:
Jason W Kim1d866172011-01-13 00:07:51 +00001525 Type = ELF::R_ARM_CALL;
1526 break;
1527 }
1528 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001529 case ARM::fixup_arm_condbranch:
1530 Type = ELF::R_ARM_JUMP24;
1531 break;
Jason W Kim86a97f22011-01-12 00:19:25 +00001532 case ARM::fixup_arm_movt_hi16:
1533 case ARM::fixup_arm_movt_hi16_pcrel:
Jason W Kim1d866172011-01-13 00:07:51 +00001534 Type = ELF::R_ARM_MOVT_PREL;
1535 break;
Jason W Kim86a97f22011-01-12 00:19:25 +00001536 case ARM::fixup_arm_movw_lo16:
1537 case ARM::fixup_arm_movw_lo16_pcrel:
Jason W Kim1d866172011-01-13 00:07:51 +00001538 Type = ELF::R_ARM_MOVW_PREL_NC;
1539 break;
Evan Chengf3eb3bb2011-01-14 02:38:49 +00001540 case ARM::fixup_t2_movt_hi16:
1541 case ARM::fixup_t2_movt_hi16_pcrel:
1542 Type = ELF::R_ARM_THM_MOVT_PREL;
1543 break;
1544 case ARM::fixup_t2_movw_lo16:
1545 case ARM::fixup_t2_movw_lo16_pcrel:
1546 Type = ELF::R_ARM_THM_MOVW_PREL_NC;
1547 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001548 }
1549 } else {
1550 switch ((unsigned)Fixup.getKind()) {
1551 default: llvm_unreachable("invalid fixup kind!");
Jason W Kima0871e72010-12-08 23:14:44 +00001552 case FK_Data_4:
1553 switch (Modifier) {
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001554 default: llvm_unreachable("Unsupported Modifier"); break;
1555 case MCSymbolRefExpr::VK_ARM_GOT:
Jason W Kim1d866172011-01-13 00:07:51 +00001556 Type = ELF::R_ARM_GOT_BREL;
1557 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001558 case MCSymbolRefExpr::VK_ARM_TLSGD:
Jason W Kim1d866172011-01-13 00:07:51 +00001559 Type = ELF::R_ARM_TLS_GD32;
1560 break;
Jason W Kimf13743b2010-12-16 03:12:17 +00001561 case MCSymbolRefExpr::VK_ARM_TPOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001562 Type = ELF::R_ARM_TLS_LE32;
1563 break;
Jason W Kima0871e72010-12-08 23:14:44 +00001564 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001565 Type = ELF::R_ARM_TLS_IE32;
1566 break;
Jason W Kimf13743b2010-12-16 03:12:17 +00001567 case MCSymbolRefExpr::VK_None:
Jason W Kim1d866172011-01-13 00:07:51 +00001568 Type = ELF::R_ARM_ABS32;
1569 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001570 case MCSymbolRefExpr::VK_ARM_GOTOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001571 Type = ELF::R_ARM_GOTOFF32;
1572 break;
1573 }
1574 break;
Jim Grosbachdff84b02010-12-02 00:28:45 +00001575 case ARM::fixup_arm_ldst_pcrel_12:
Owen Anderson9d63d902010-12-01 19:18:46 +00001576 case ARM::fixup_arm_pcrel_10:
Jim Grosbachdff84b02010-12-02 00:28:45 +00001577 case ARM::fixup_arm_adr_pcrel_12:
Jim Grosbach662a8162010-12-06 23:57:07 +00001578 case ARM::fixup_arm_thumb_bl:
Jim Grosbachb492a7c2010-12-09 19:50:12 +00001579 case ARM::fixup_arm_thumb_cb:
Bill Wendlingb8958b02010-12-08 01:57:09 +00001580 case ARM::fixup_arm_thumb_cp:
Jim Grosbache2467172010-12-10 18:21:33 +00001581 case ARM::fixup_arm_thumb_br:
Jason W Kim1d866172011-01-13 00:07:51 +00001582 assert(0 && "Unimplemented");
1583 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001584 case ARM::fixup_arm_uncondbranch:
Jason W Kim1d866172011-01-13 00:07:51 +00001585 Type = ELF::R_ARM_CALL;
1586 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001587 case ARM::fixup_arm_condbranch:
1588 Type = ELF::R_ARM_JUMP24;
1589 break;
Jason W Kim1d866172011-01-13 00:07:51 +00001590 case ARM::fixup_arm_movt_hi16:
1591 Type = ELF::R_ARM_MOVT_ABS;
1592 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001593 case ARM::fixup_arm_movw_lo16:
Jason W Kim1d866172011-01-13 00:07:51 +00001594 Type = ELF::R_ARM_MOVW_ABS_NC;
1595 break;
Evan Chengf3eb3bb2011-01-14 02:38:49 +00001596 case ARM::fixup_t2_movt_hi16:
1597 Type = ELF::R_ARM_THM_MOVT_ABS;
1598 break;
1599 case ARM::fixup_t2_movw_lo16:
1600 Type = ELF::R_ARM_THM_MOVW_ABS_NC;
1601 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001602 }
1603 }
1604
1605 if (RelocNeedsGOT(Modifier))
1606 NeedsGOT = true;
Jason W Kima0871e72010-12-08 23:14:44 +00001607
1608 return Type;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001609}
1610
Wesley Peck4b047132010-11-21 22:06:28 +00001611//===- MBlazeELFObjectWriter -------------------------------------------===//
Jason W Kimd3443e92010-11-15 16:18:39 +00001612
Rafael Espindola31f35782010-12-17 18:01:31 +00001613MBlazeELFObjectWriter::MBlazeELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001614 raw_ostream &_OS,
1615 bool IsLittleEndian)
1616 : ELFObjectWriter(MOTW, _OS, IsLittleEndian) {
Wesley Peck4b047132010-11-21 22:06:28 +00001617}
1618
1619MBlazeELFObjectWriter::~MBlazeELFObjectWriter() {
1620}
1621
Jason W Kim56a39902010-12-06 21:57:34 +00001622unsigned MBlazeELFObjectWriter::GetRelocType(const MCValue &Target,
Wesley Peck4b047132010-11-21 22:06:28 +00001623 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001624 bool IsPCRel,
1625 bool IsRelocWithSymbol,
1626 int64_t Addend) {
Wesley Peck4b047132010-11-21 22:06:28 +00001627 // determine the type of the relocation
1628 unsigned Type;
1629 if (IsPCRel) {
1630 switch ((unsigned)Fixup.getKind()) {
1631 default:
1632 llvm_unreachable("Unimplemented");
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001633 case FK_PCRel_4:
Wesley Peck4b047132010-11-21 22:06:28 +00001634 Type = ELF::R_MICROBLAZE_64_PCREL;
1635 break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001636 case FK_PCRel_2:
Wesley Peck4b047132010-11-21 22:06:28 +00001637 Type = ELF::R_MICROBLAZE_32_PCREL;
1638 break;
1639 }
1640 } else {
1641 switch ((unsigned)Fixup.getKind()) {
1642 default: llvm_unreachable("invalid fixup kind!");
1643 case FK_Data_4:
Jason W Kim56a39902010-12-06 21:57:34 +00001644 Type = ((IsRelocWithSymbol || Addend !=0)
1645 ? ELF::R_MICROBLAZE_32
1646 : ELF::R_MICROBLAZE_64);
Wesley Peck4b047132010-11-21 22:06:28 +00001647 break;
1648 case FK_Data_2:
1649 Type = ELF::R_MICROBLAZE_32;
1650 break;
1651 }
1652 }
Jason W Kim56a39902010-12-06 21:57:34 +00001653 return Type;
Wesley Peck4b047132010-11-21 22:06:28 +00001654}
Jason W Kimd3443e92010-11-15 16:18:39 +00001655
1656//===- X86ELFObjectWriter -------------------------------------------===//
1657
1658
Rafael Espindola31f35782010-12-17 18:01:31 +00001659X86ELFObjectWriter::X86ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001660 raw_ostream &_OS,
1661 bool IsLittleEndian)
1662 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
Jason W Kimd3443e92010-11-15 16:18:39 +00001663{}
1664
1665X86ELFObjectWriter::~X86ELFObjectWriter()
1666{}
1667
Jason W Kim56a39902010-12-06 21:57:34 +00001668unsigned X86ELFObjectWriter::GetRelocType(const MCValue &Target,
1669 const MCFixup &Fixup,
1670 bool IsPCRel,
1671 bool IsRelocWithSymbol,
1672 int64_t Addend) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001673 // determine the type of the relocation
1674
Rafael Espindola12203cc2010-11-21 00:48:25 +00001675 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1676 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
Jason W Kimd3443e92010-11-15 16:18:39 +00001677 unsigned Type;
Rafael Espindolabff66a82010-12-18 03:27:34 +00001678 if (is64Bit()) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001679 if (IsPCRel) {
Rafael Espindola3a83c402010-12-27 00:36:05 +00001680 switch ((unsigned)Fixup.getKind()) {
1681 default: llvm_unreachable("invalid fixup kind!");
1682 case FK_PCRel_8:
1683 assert(Modifier == MCSymbolRefExpr::VK_None);
1684 Type = ELF::R_X86_64_PC64;
Jason W Kimd3443e92010-11-15 16:18:39 +00001685 break;
Rafael Espindola7a549972011-01-01 19:05:35 +00001686 case X86::reloc_signed_4byte:
Rafael Espindolac3a561c2010-12-27 02:03:24 +00001687 case X86::reloc_riprel_4byte_movq_load:
Rafael Espindola3a83c402010-12-27 00:36:05 +00001688 case FK_Data_4: // FIXME?
1689 case X86::reloc_riprel_4byte:
1690 case FK_PCRel_4:
1691 switch (Modifier) {
1692 default:
1693 llvm_unreachable("Unimplemented");
1694 case MCSymbolRefExpr::VK_None:
1695 Type = ELF::R_X86_64_PC32;
1696 break;
1697 case MCSymbolRefExpr::VK_PLT:
1698 Type = ELF::R_X86_64_PLT32;
1699 break;
1700 case MCSymbolRefExpr::VK_GOTPCREL:
1701 Type = ELF::R_X86_64_GOTPCREL;
1702 break;
1703 case MCSymbolRefExpr::VK_GOTTPOFF:
1704 Type = ELF::R_X86_64_GOTTPOFF;
Jason W Kimd3443e92010-11-15 16:18:39 +00001705 break;
Rafael Espindola3a83c402010-12-27 00:36:05 +00001706 case MCSymbolRefExpr::VK_TLSGD:
1707 Type = ELF::R_X86_64_TLSGD;
1708 break;
1709 case MCSymbolRefExpr::VK_TLSLD:
1710 Type = ELF::R_X86_64_TLSLD;
1711 break;
1712 }
Jason W Kimd3443e92010-11-15 16:18:39 +00001713 break;
Rafael Espindola3a83c402010-12-27 00:36:05 +00001714 case FK_PCRel_2:
1715 assert(Modifier == MCSymbolRefExpr::VK_None);
1716 Type = ELF::R_X86_64_PC16;
Jason W Kimd3443e92010-11-15 16:18:39 +00001717 break;
1718 }
1719 } else {
1720 switch ((unsigned)Fixup.getKind()) {
1721 default: llvm_unreachable("invalid fixup kind!");
1722 case FK_Data_8: Type = ELF::R_X86_64_64; break;
1723 case X86::reloc_signed_4byte:
Jason W Kimd3443e92010-11-15 16:18:39 +00001724 assert(isInt<32>(Target.getConstant()));
1725 switch (Modifier) {
1726 default:
1727 llvm_unreachable("Unimplemented");
1728 case MCSymbolRefExpr::VK_None:
1729 Type = ELF::R_X86_64_32S;
1730 break;
1731 case MCSymbolRefExpr::VK_GOT:
1732 Type = ELF::R_X86_64_GOT32;
1733 break;
1734 case MCSymbolRefExpr::VK_GOTPCREL:
1735 Type = ELF::R_X86_64_GOTPCREL;
1736 break;
1737 case MCSymbolRefExpr::VK_TPOFF:
1738 Type = ELF::R_X86_64_TPOFF32;
1739 break;
1740 case MCSymbolRefExpr::VK_DTPOFF:
1741 Type = ELF::R_X86_64_DTPOFF32;
1742 break;
1743 }
1744 break;
1745 case FK_Data_4:
1746 Type = ELF::R_X86_64_32;
1747 break;
1748 case FK_Data_2: Type = ELF::R_X86_64_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001749 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001750 case FK_Data_1: Type = ELF::R_X86_64_8; break;
1751 }
1752 }
1753 } else {
1754 if (IsPCRel) {
1755 switch (Modifier) {
1756 default:
1757 llvm_unreachable("Unimplemented");
1758 case MCSymbolRefExpr::VK_None:
1759 Type = ELF::R_386_PC32;
1760 break;
1761 case MCSymbolRefExpr::VK_PLT:
1762 Type = ELF::R_386_PLT32;
1763 break;
1764 }
1765 } else {
1766 switch ((unsigned)Fixup.getKind()) {
1767 default: llvm_unreachable("invalid fixup kind!");
1768
1769 case X86::reloc_global_offset_table:
1770 Type = ELF::R_386_GOTPC;
1771 break;
1772
1773 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
1774 // instead?
1775 case X86::reloc_signed_4byte:
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001776 case FK_PCRel_4:
Jason W Kimd3443e92010-11-15 16:18:39 +00001777 case FK_Data_4:
1778 switch (Modifier) {
1779 default:
1780 llvm_unreachable("Unimplemented");
1781 case MCSymbolRefExpr::VK_None:
1782 Type = ELF::R_386_32;
1783 break;
1784 case MCSymbolRefExpr::VK_GOT:
1785 Type = ELF::R_386_GOT32;
1786 break;
1787 case MCSymbolRefExpr::VK_GOTOFF:
1788 Type = ELF::R_386_GOTOFF;
1789 break;
1790 case MCSymbolRefExpr::VK_TLSGD:
1791 Type = ELF::R_386_TLS_GD;
1792 break;
1793 case MCSymbolRefExpr::VK_TPOFF:
1794 Type = ELF::R_386_TLS_LE_32;
1795 break;
1796 case MCSymbolRefExpr::VK_INDNTPOFF:
1797 Type = ELF::R_386_TLS_IE;
1798 break;
1799 case MCSymbolRefExpr::VK_NTPOFF:
1800 Type = ELF::R_386_TLS_LE;
1801 break;
1802 case MCSymbolRefExpr::VK_GOTNTPOFF:
1803 Type = ELF::R_386_TLS_GOTIE;
1804 break;
1805 case MCSymbolRefExpr::VK_TLSLDM:
1806 Type = ELF::R_386_TLS_LDM;
1807 break;
1808 case MCSymbolRefExpr::VK_DTPOFF:
1809 Type = ELF::R_386_TLS_LDO_32;
1810 break;
1811 }
1812 break;
1813 case FK_Data_2: Type = ELF::R_386_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001814 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001815 case FK_Data_1: Type = ELF::R_386_8; break;
1816 }
1817 }
1818 }
1819
1820 if (RelocNeedsGOT(Modifier))
1821 NeedsGOT = true;
1822
Jason W Kim56a39902010-12-06 21:57:34 +00001823 return Type;
Matt Fleming3565a062010-08-16 18:57:57 +00001824}