blob: 10e67d20d5c45a9a0363963ccc999b742261875e [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 Kimd3443e92010-11-15 16:18:39 +0000279 virtual void WriteSymbolEntry(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
Rafael Espindola7be2c332010-10-31 00:16:26 +0000280 uint64_t name, uint8_t info,
Matt Fleming3565a062010-08-16 18:57:57 +0000281 uint64_t value, uint64_t size,
Rafael Espindola7be2c332010-10-31 00:16:26 +0000282 uint8_t other, uint32_t shndx,
283 bool Reserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000284
Jason W Kimd3443e92010-11-15 16:18:39 +0000285 virtual void WriteSymbol(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
Rafael Espindola7be2c332010-10-31 00:16:26 +0000286 ELFSymbolData &MSD,
Matt Fleming3565a062010-08-16 18:57:57 +0000287 const MCAsmLayout &Layout);
288
Rafael Espindolabab2a802010-11-10 21:51:05 +0000289 typedef DenseMap<const MCSectionELF*, uint32_t> SectionIndexMapTy;
Jason W Kimd3443e92010-11-15 16:18:39 +0000290 virtual void WriteSymbolTable(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
Rafael Espindola7be2c332010-10-31 00:16:26 +0000291 const MCAssembler &Asm,
Rafael Espindola71859c62010-09-16 19:46:31 +0000292 const MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000293 const SectionIndexMapTy &SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +0000294
Jason W Kimd3443e92010-11-15 16:18:39 +0000295 virtual void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
Jason W Kim56a39902010-12-06 21:57:34 +0000296 const MCFragment *Fragment, const MCFixup &Fixup,
297 MCValue Target, uint64_t &FixedValue);
Matt Fleming3565a062010-08-16 18:57:57 +0000298
Jason W Kimd3443e92010-11-15 16:18:39 +0000299 virtual uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm,
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000300 const MCSymbol *S);
Matt Fleming3565a062010-08-16 18:57:57 +0000301
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000302 // Map from a group section to the signature symbol
303 typedef DenseMap<const MCSectionELF*, const MCSymbol*> GroupMapTy;
304 // Map from a signature symbol to the group section
305 typedef DenseMap<const MCSymbol*, const MCSectionELF*> RevGroupMapTy;
306
Matt Fleming3565a062010-08-16 18:57:57 +0000307 /// ComputeSymbolTable - Compute the symbol table data
308 ///
309 /// \param StringTable [out] - The string table data.
310 /// \param StringIndexMap [out] - Map from symbol names to offsets in the
311 /// string table.
Jason W Kimd3443e92010-11-15 16:18:39 +0000312 virtual void ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000313 const SectionIndexMapTy &SectionIndexMap,
314 RevGroupMapTy RevGroupMap);
Rafael Espindolabab2a802010-11-10 21:51:05 +0000315
Jason W Kimd3443e92010-11-15 16:18:39 +0000316 virtual void ComputeIndexMap(MCAssembler &Asm,
Rafael Espindolabab2a802010-11-10 21:51:05 +0000317 SectionIndexMapTy &SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +0000318
Jason W Kimd3443e92010-11-15 16:18:39 +0000319 virtual void WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
Matt Fleming3565a062010-08-16 18:57:57 +0000320 const MCSectionData &SD);
321
Jason W Kimd3443e92010-11-15 16:18:39 +0000322 virtual void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout) {
Matt Fleming3565a062010-08-16 18:57:57 +0000323 for (MCAssembler::const_iterator it = Asm.begin(),
324 ie = Asm.end(); it != ie; ++it) {
325 WriteRelocation(Asm, Layout, *it);
326 }
327 }
328
Jason W Kimd3443e92010-11-15 16:18:39 +0000329 virtual void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000330 const SectionIndexMapTy &SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +0000331
Jason W Kimd3443e92010-11-15 16:18:39 +0000332 virtual void CreateGroupSections(MCAssembler &Asm, MCAsmLayout &Layout,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000333 GroupMapTy &GroupMap, RevGroupMapTy &RevGroupMap);
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000334
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000335 virtual void ExecutePostLayoutBinding(MCAssembler &Asm,
336 const MCAsmLayout &Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000337
Jason W Kimd3443e92010-11-15 16:18:39 +0000338 virtual void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
Matt Fleming3565a062010-08-16 18:57:57 +0000339 uint64_t Address, uint64_t Offset,
340 uint64_t Size, uint32_t Link, uint32_t Info,
341 uint64_t Alignment, uint64_t EntrySize);
342
Daniel Dunbar1f3662a2010-12-17 04:54:54 +0000343 virtual void WriteRelocationsFragment(const MCAssembler &Asm,
344 MCDataFragment *F,
345 const MCSectionData *SD);
346
347 virtual bool
348 IsSymbolRefDifferenceFullyResolved(const MCAssembler &Asm,
349 const MCSymbolRefExpr *A,
350 const MCSymbolRefExpr *B) const {
351 // FIXME: Implement this!
352 return false;
353 }
Matt Fleming3565a062010-08-16 18:57:57 +0000354
Jason W Kimd3443e92010-11-15 16:18:39 +0000355 virtual bool IsFixupFullyResolved(const MCAssembler &Asm,
Rafael Espindola70703872010-09-30 02:22:20 +0000356 const MCValue Target,
357 bool IsPCRel,
358 const MCFragment *DF) const;
359
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:
Rafael Espindola31f35782010-12-17 18:01:31 +0000393 ARMELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +0000394 raw_ostream &_OS,
395 bool IsLittleEndian);
Wesley Peck4b047132010-11-21 22:06:28 +0000396
Jason W Kimd3443e92010-11-15 16:18:39 +0000397 virtual ~ARMELFObjectWriter();
Jason W Kim85fed5e2010-12-01 02:40:06 +0000398 protected:
Jason W Kim56a39902010-12-06 21:57:34 +0000399 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
400 bool IsPCRel, bool IsRelocWithSymbol,
401 int64_t Addend);
Jason W Kimd3443e92010-11-15 16:18:39 +0000402 };
Wesley Peck4b047132010-11-21 22:06:28 +0000403
404 //===- MBlazeELFObjectWriter -------------------------------------------===//
405
406 class MBlazeELFObjectWriter : public ELFObjectWriter {
407 public:
Rafael Espindola31f35782010-12-17 18:01:31 +0000408 MBlazeELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +0000409 raw_ostream &_OS,
410 bool IsLittleEndian);
Wesley Peck4b047132010-11-21 22:06:28 +0000411
412 virtual ~MBlazeELFObjectWriter();
Jason W Kim56a39902010-12-06 21:57:34 +0000413 protected:
414 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
415 bool IsPCRel, bool IsRelocWithSymbol,
416 int64_t Addend);
Wesley Peck4b047132010-11-21 22:06:28 +0000417 };
Matt Fleming3565a062010-08-16 18:57:57 +0000418}
419
Jason W Kimd3443e92010-11-15 16:18:39 +0000420ELFObjectWriter::~ELFObjectWriter()
421{}
422
Matt Fleming3565a062010-08-16 18:57:57 +0000423// Emit the ELF header.
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000424void ELFObjectWriter::WriteHeader(uint64_t SectionDataSize,
425 unsigned NumberOfSections) {
Matt Fleming3565a062010-08-16 18:57:57 +0000426 // ELF Header
427 // ----------
428 //
429 // Note
430 // ----
431 // emitWord method behaves differently for ELF32 and ELF64, writing
432 // 4 bytes in the former and 8 in the latter.
433
434 Write8(0x7f); // e_ident[EI_MAG0]
435 Write8('E'); // e_ident[EI_MAG1]
436 Write8('L'); // e_ident[EI_MAG2]
437 Write8('F'); // e_ident[EI_MAG3]
438
Rafael Espindolabff66a82010-12-18 03:27:34 +0000439 Write8(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
Matt Fleming3565a062010-08-16 18:57:57 +0000440
441 // e_ident[EI_DATA]
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000442 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
Matt Fleming3565a062010-08-16 18:57:57 +0000443
444 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky5baf79e2010-09-09 17:57:50 +0000445 // e_ident[EI_OSABI]
Rafael Espindolabff66a82010-12-18 03:27:34 +0000446 switch (TargetObjectWriter->getOSType()) {
Roman Divacky5baf79e2010-09-09 17:57:50 +0000447 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
448 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
449 default: Write8(ELF::ELFOSABI_NONE); break;
450 }
Matt Fleming3565a062010-08-16 18:57:57 +0000451 Write8(0); // e_ident[EI_ABIVERSION]
452
453 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
454
455 Write16(ELF::ET_REL); // e_type
456
Rafael Espindolabff66a82010-12-18 03:27:34 +0000457 Write16(TargetObjectWriter->getEMachine()); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +0000458
459 Write32(ELF::EV_CURRENT); // e_version
460 WriteWord(0); // e_entry, no entry point in .o file
461 WriteWord(0); // e_phoff, no program header for .o
Rafael Espindolabff66a82010-12-18 03:27:34 +0000462 WriteWord(SectionDataSize + (is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
Benjamin Kramereb976772010-08-17 17:02:29 +0000463 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000464
465 // FIXME: Make this configurable.
466 Write32(0); // e_flags = whatever the target wants
467
468 // e_ehsize = ELF header size
Rafael Espindolabff66a82010-12-18 03:27:34 +0000469 Write16(is64Bit() ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
Matt Fleming3565a062010-08-16 18:57:57 +0000470
471 Write16(0); // e_phentsize = prog header entry size
472 Write16(0); // e_phnum = # prog header entries = 0
473
474 // e_shentsize = Section header entry size
Rafael Espindolabff66a82010-12-18 03:27:34 +0000475 Write16(is64Bit() ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
Matt Fleming3565a062010-08-16 18:57:57 +0000476
477 // e_shnum = # of section header ents
Rafael Espindola7be2c332010-10-31 00:16:26 +0000478 if (NumberOfSections >= ELF::SHN_LORESERVE)
479 Write16(0);
480 else
481 Write16(NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000482
483 // e_shstrndx = Section # of '.shstrtab'
Rafael Espindola7be2c332010-10-31 00:16:26 +0000484 if (NumberOfSections >= ELF::SHN_LORESERVE)
485 Write16(ELF::SHN_XINDEX);
486 else
487 Write16(ShstrtabIndex);
Matt Fleming3565a062010-08-16 18:57:57 +0000488}
489
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000490void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
491 MCDataFragment *ShndxF,
492 uint64_t name,
493 uint8_t info, uint64_t value,
494 uint64_t size, uint8_t other,
495 uint32_t shndx,
496 bool Reserved) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000497 if (ShndxF) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000498 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000499 String32(*ShndxF, shndx);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000500 else
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000501 String32(*ShndxF, 0);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000502 }
503
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000504 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
505 uint16_t(ELF::SHN_XINDEX) : shndx;
506
Rafael Espindolabff66a82010-12-18 03:27:34 +0000507 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000508 String32(*SymtabF, name); // st_name
509 String8(*SymtabF, info); // st_info
510 String8(*SymtabF, other); // st_other
511 String16(*SymtabF, Index); // st_shndx
512 String64(*SymtabF, value); // st_value
513 String64(*SymtabF, size); // st_size
Matt Fleming3565a062010-08-16 18:57:57 +0000514 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000515 String32(*SymtabF, name); // st_name
516 String32(*SymtabF, value); // st_value
517 String32(*SymtabF, size); // st_size
518 String8(*SymtabF, info); // st_info
519 String8(*SymtabF, other); // st_other
520 String16(*SymtabF, Index); // st_shndx
Matt Fleming3565a062010-08-16 18:57:57 +0000521 }
522}
523
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000524static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout) {
525 if (Data.isCommon() && Data.isExternal())
526 return Data.getCommonAlignment();
527
528 const MCSymbol &Symbol = Data.getSymbol();
529 if (!Symbol.isInSection())
530 return 0;
531
Rafael Espindolaffd902b2010-12-06 02:57:26 +0000532 if (Data.getFragment())
533 return Layout.getSymbolOffset(&Data);
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000534
535 return 0;
536}
537
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000538void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
539 const MCAsmLayout &Layout) {
Rafael Espindola88182132010-10-27 15:18:17 +0000540 // The presence of symbol versions causes undefined symbols and
541 // versions declared with @@@ to be renamed.
542
543 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
544 ie = Asm.symbol_end(); it != ie; ++it) {
545 const MCSymbol &Alias = it->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000546 const MCSymbol &Symbol = Alias.AliasedSymbol();
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000547 MCSymbolData &SD = Asm.getSymbolData(Symbol);
548
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000549 // Not an alias.
550 if (&Symbol == &Alias)
551 continue;
552
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000553 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000554 size_t Pos = AliasName.find('@');
555 if (Pos == StringRef::npos)
556 continue;
557
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000558 // Aliases defined with .symvar copy the binding from the symbol they alias.
559 // This is the first place we are able to copy this information.
560 it->setExternal(SD.isExternal());
561 SetBinding(*it, GetBinding(SD));
562
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000563 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000564 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
565 continue;
566
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000567 // FIXME: produce a better error message.
568 if (Symbol.isUndefined() && Rest.startswith("@@") &&
569 !Rest.startswith("@@@"))
570 report_fatal_error("A @@ version cannot be undefined");
571
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000572 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000573 }
574}
575
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000576void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
577 MCDataFragment *ShndxF,
578 ELFSymbolData &MSD,
579 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000580 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000581 MCSymbolData &Data =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000582 Layout.getAssembler().getSymbolData(OrigData.getSymbol().AliasedSymbol());
Rafael Espindola152c1062010-10-06 21:02:29 +0000583
Rafael Espindola7be2c332010-10-31 00:16:26 +0000584 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
585 Data.getSymbol().isVariable();
586
Rafael Espindola152c1062010-10-06 21:02:29 +0000587 uint8_t Binding = GetBinding(OrigData);
588 uint8_t Visibility = GetVisibility(OrigData);
589 uint8_t Type = GetType(Data);
590
591 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
592 uint8_t Other = Visibility;
593
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000594 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000595 uint64_t Size = 0;
596 const MCExpr *ESize;
597
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000598 assert(!(Data.isCommon() && !Data.isExternal()));
599
Matt Fleming3565a062010-08-16 18:57:57 +0000600 ESize = Data.getSize();
601 if (Data.getSize()) {
602 MCValue Res;
603 if (ESize->getKind() == MCExpr::Binary) {
604 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(ESize);
605
606 if (BE->EvaluateAsRelocatable(Res, &Layout)) {
Benjamin Kramer24f12062010-10-17 07:38:40 +0000607 assert(!Res.getSymA() || !Res.getSymA()->getSymbol().isDefined());
608 assert(!Res.getSymB() || !Res.getSymB()->getSymbol().isDefined());
Rafael Espindolaf230df92010-10-16 18:23:53 +0000609 Size = Res.getConstant();
Matt Fleming3565a062010-08-16 18:57:57 +0000610 }
611 } else if (ESize->getKind() == MCExpr::Constant) {
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000612 Size = static_cast<const MCConstantExpr *>(ESize)->getValue();
Matt Fleming3565a062010-08-16 18:57:57 +0000613 } else {
614 assert(0 && "Unsupported size expression");
615 }
616 }
617
618 // Write out the symbol table entry
Rafael Espindola7be2c332010-10-31 00:16:26 +0000619 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
620 Size, Other, MSD.SectionIndex, IsReserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000621}
622
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000623void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
624 MCDataFragment *ShndxF,
625 const MCAssembler &Asm,
626 const MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000627 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000628 // The string table must be emitted first because we need the index
629 // into the string table for all the symbol names.
630 assert(StringTable.size() && "Missing string table");
631
632 // FIXME: Make sure the start of the symbol table is aligned.
633
634 // The first entry is the undefined symbol entry.
Rafael Espindola7be2c332010-10-31 00:16:26 +0000635 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
Matt Fleming3565a062010-08-16 18:57:57 +0000636
637 // Write the symbol table entries.
638 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
639 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
640 ELFSymbolData &MSD = LocalSymbolData[i];
Rafael Espindola7be2c332010-10-31 00:16:26 +0000641 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000642 }
643
Rafael Espindola71859c62010-09-16 19:46:31 +0000644 // Write out a symbol table entry for each regular section.
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000645 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
646 ++i) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000647 const MCSectionELF &Section =
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000648 static_cast<const MCSectionELF&>(i->getSection());
649 if (Section.getType() == ELF::SHT_RELA ||
650 Section.getType() == ELF::SHT_REL ||
651 Section.getType() == ELF::SHT_STRTAB ||
652 Section.getType() == ELF::SHT_SYMTAB)
Eli Friedmana44fa242010-08-16 21:17:09 +0000653 continue;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000654 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000655 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section), false);
Eli Friedmana44fa242010-08-16 21:17:09 +0000656 LastLocalSymbolIndex++;
657 }
Matt Fleming3565a062010-08-16 18:57:57 +0000658
659 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
660 ELFSymbolData &MSD = ExternalSymbolData[i];
661 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000662 assert(((Data.getFlags() & ELF_STB_Global) ||
663 (Data.getFlags() & ELF_STB_Weak)) &&
664 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000665 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000666 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000667 LastLocalSymbolIndex++;
668 }
669
670 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
671 ELFSymbolData &MSD = UndefinedSymbolData[i];
672 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000673 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000674 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000675 LastLocalSymbolIndex++;
676 }
677}
678
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000679const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm,
680 const MCValue &Target,
681 const MCFragment &F) const {
682 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000683 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
684 const MCSymbol *Renamed = Renames.lookup(&Symbol);
685 const MCSymbolData &SD = Asm.getSymbolData(Symbol);
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000686
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000687 if (ASymbol.isUndefined()) {
688 if (Renamed)
689 return Renamed;
690 return &ASymbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000691 }
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000692
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000693 if (SD.isExternal()) {
694 if (Renamed)
695 return Renamed;
696 return &Symbol;
697 }
Rafael Espindola73ffea42010-09-25 05:42:19 +0000698
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000699 const MCSectionELF &Section =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000700 static_cast<const MCSectionELF&>(ASymbol.getSection());
Rafael Espindola25958732010-11-24 21:57:39 +0000701 const SectionKind secKind = Section.getKind();
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000702
Rafael Espindola25958732010-11-24 21:57:39 +0000703 if (secKind.isBSS())
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000704 return NULL;
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000705
Rafael Espindola25958732010-11-24 21:57:39 +0000706 if (secKind.isThreadLocal()) {
707 if (Renamed)
708 return Renamed;
709 return &Symbol;
710 }
711
Rafael Espindola8cecf252010-10-06 16:23:36 +0000712 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000713 const MCSectionELF &Sec2 =
714 static_cast<const MCSectionELF&>(F.getParent()->getSection());
715
Rafael Espindola8cecf252010-10-06 16:23:36 +0000716 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000717 (Kind == MCSymbolRefExpr::VK_PLT ||
718 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
Rafael Espindola25958732010-11-24 21:57:39 +0000719 Kind == MCSymbolRefExpr::VK_GOTOFF)) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000720 if (Renamed)
721 return Renamed;
722 return &Symbol;
723 }
Rafael Espindola3729d002010-10-05 23:57:26 +0000724
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000725 if (Section.getFlags() & MCSectionELF::SHF_MERGE) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000726 if (Target.getConstant() == 0)
727 return NULL;
728 if (Renamed)
729 return Renamed;
730 return &Symbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000731 }
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000732
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000733 return NULL;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000734}
735
Matt Fleming3565a062010-08-16 18:57:57 +0000736
Jason W Kim56a39902010-12-06 21:57:34 +0000737void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
738 const MCAsmLayout &Layout,
739 const MCFragment *Fragment,
740 const MCFixup &Fixup,
741 MCValue Target,
742 uint64_t &FixedValue) {
743 int64_t Addend = 0;
744 int Index = 0;
745 int64_t Value = Target.getConstant();
746 const MCSymbol *RelocSymbol = NULL;
747
Rafael Espindola127a6a42010-12-17 07:28:17 +0000748 bool IsPCRel = isFixupKindPCRel(Asm, Fixup.getKind());
Jason W Kim56a39902010-12-06 21:57:34 +0000749 if (!Target.isAbsolute()) {
750 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
751 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
752 RelocSymbol = SymbolToReloc(Asm, Target, *Fragment);
753
754 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
755 const MCSymbol &SymbolB = RefB->getSymbol();
756 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
757 IsPCRel = true;
758
759 // Offset of the symbol in the section
760 int64_t a = Layout.getSymbolOffset(&SDB);
761
762 // Ofeset of the relocation in the section
763 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
764 Value += b - a;
765 }
766
767 if (!RelocSymbol) {
768 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
769 MCFragment *F = SD.getFragment();
770
771 Index = F->getParent()->getOrdinal() + 1;
772
773 // Offset of the symbol in the section
774 Value += Layout.getSymbolOffset(&SD);
775 } else {
776 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
777 WeakrefUsedInReloc.insert(RelocSymbol);
778 else
779 UsedInReloc.insert(RelocSymbol);
780 Index = -1;
781 }
782 Addend = Value;
783 // Compensate for the addend on i386.
Rafael Espindolabff66a82010-12-18 03:27:34 +0000784 if (is64Bit())
Jason W Kim56a39902010-12-06 21:57:34 +0000785 Value = 0;
786 }
787
788 FixedValue = Value;
789 unsigned Type = GetRelocType(Target, Fixup, IsPCRel,
790 (RelocSymbol != 0), Addend);
791
792 uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
793 Fixup.getOffset();
794
Rafael Espindolabff66a82010-12-18 03:27:34 +0000795 if (!hasRelocationAddend())
796 Addend = 0;
Jason W Kim56a39902010-12-06 21:57:34 +0000797 ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend);
798 Relocations[Fragment->getParent()].push_back(ERE);
799}
800
801
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000802uint64_t
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000803ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
804 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000805 MCSymbolData &SD = Asm.getSymbolData(*S);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000806 return SD.getIndex();
Matt Fleming3565a062010-08-16 18:57:57 +0000807}
808
Rafael Espindola737cd212010-10-05 18:01:23 +0000809static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
Rafael Espindola88182132010-10-27 15:18:17 +0000810 bool Used, bool Renamed) {
Rafael Espindola484291c2010-11-01 14:28:48 +0000811 if (Data.getFlags() & ELF_Other_Weakref)
812 return false;
813
Rafael Espindolabd701182010-10-19 19:31:37 +0000814 if (Used)
815 return true;
816
Rafael Espindola88182132010-10-27 15:18:17 +0000817 if (Renamed)
818 return false;
819
Rafael Espindola737cd212010-10-05 18:01:23 +0000820 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000821
Rafael Espindolad1798862010-10-29 23:09:31 +0000822 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
823 return true;
824
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000825 const MCSymbol &A = Symbol.AliasedSymbol();
Rafael Espindolad1798862010-10-29 23:09:31 +0000826 if (!A.isVariable() && A.isUndefined() && !Data.isCommon())
Rafael Espindolaa6866962010-10-27 14:44:52 +0000827 return false;
828
Rafael Espindola737cd212010-10-05 18:01:23 +0000829 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
830 return false;
831
Rafael Espindolabd701182010-10-19 19:31:37 +0000832 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000833 return false;
834
835 return true;
836}
837
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000838static bool isLocal(const MCSymbolData &Data, bool isSignature,
839 bool isUsedInReloc) {
Rafael Espindola737cd212010-10-05 18:01:23 +0000840 if (Data.isExternal())
841 return false;
842
843 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000844 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000845
846 if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
847 if (isSignature && !isUsedInReloc)
848 return true;
849
Rafael Espindola737cd212010-10-05 18:01:23 +0000850 return false;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000851 }
Rafael Espindola737cd212010-10-05 18:01:23 +0000852
853 return true;
854}
855
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000856void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
857 SectionIndexMapTy &SectionIndexMap) {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000858 unsigned Index = 1;
859 for (MCAssembler::iterator it = Asm.begin(),
860 ie = Asm.end(); it != ie; ++it) {
861 const MCSectionELF &Section =
862 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000863 if (Section.getType() != ELF::SHT_GROUP)
864 continue;
865 SectionIndexMap[&Section] = Index++;
866 }
867
868 for (MCAssembler::iterator it = Asm.begin(),
869 ie = Asm.end(); it != ie; ++it) {
870 const MCSectionELF &Section =
871 static_cast<const MCSectionELF &>(it->getSection());
872 if (Section.getType() == ELF::SHT_GROUP)
873 continue;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000874 SectionIndexMap[&Section] = Index++;
875 }
876}
877
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000878void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000879 const SectionIndexMapTy &SectionIndexMap,
880 RevGroupMapTy RevGroupMap) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000881 // FIXME: Is this the correct place to do this?
882 if (NeedsGOT) {
883 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
884 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
885 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
886 Data.setExternal(true);
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000887 SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindola5c77c162010-10-05 15:48:37 +0000888 }
889
Matt Fleming3565a062010-08-16 18:57:57 +0000890 // Build section lookup table.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000891 int NumRegularSections = Asm.size();
Matt Fleming3565a062010-08-16 18:57:57 +0000892
893 // Index 0 is always the empty string.
894 StringMap<uint64_t> StringIndexMap;
895 StringTable += '\x00';
896
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000897 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000898 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
899 ie = Asm.symbol_end(); it != ie; ++it) {
900 const MCSymbol &Symbol = it->getSymbol();
901
Rafael Espindola484291c2010-11-01 14:28:48 +0000902 bool Used = UsedInReloc.count(&Symbol);
903 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000904 bool isSignature = RevGroupMap.count(&Symbol);
905
906 if (!isInSymtab(Asm, *it,
907 Used || WeakrefUsed || isSignature,
Rafael Espindola88182132010-10-27 15:18:17 +0000908 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000909 continue;
910
Matt Fleming3565a062010-08-16 18:57:57 +0000911 ELFSymbolData MSD;
912 MSD.SymbolData = it;
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000913 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Matt Fleming3565a062010-08-16 18:57:57 +0000914
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000915 // Undefined symbols are global, but this is the first place we
916 // are able to set it.
917 bool Local = isLocal(*it, isSignature, Used);
918 if (!Local && GetBinding(*it) == ELF::STB_LOCAL) {
919 MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
920 SetBinding(*it, ELF::STB_GLOBAL);
921 SetBinding(SD, ELF::STB_GLOBAL);
922 }
923
Rafael Espindola484291c2010-11-01 14:28:48 +0000924 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
925 SetBinding(*it, ELF::STB_WEAK);
926
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000927 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000928 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000929 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000930 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000931 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000932 } else if (RefSymbol.isUndefined()) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000933 if (isSignature && !Used)
934 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
935 else
936 MSD.SectionIndex = ELF::SHN_UNDEF;
Matt Fleming3565a062010-08-16 18:57:57 +0000937 } else {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000938 const MCSectionELF &Section =
939 static_cast<const MCSectionELF&>(RefSymbol.getSection());
940 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000941 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
942 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000943 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000944 }
945
Rafael Espindola88182132010-10-27 15:18:17 +0000946 // The @@@ in symbol version is replaced with @ in undefined symbols and
947 // @@ in defined ones.
948 StringRef Name = Symbol.getName();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000949 SmallString<32> Buf;
950
Rafael Espindola88182132010-10-27 15:18:17 +0000951 size_t Pos = Name.find("@@@");
Rafael Espindola88182132010-10-27 15:18:17 +0000952 if (Pos != StringRef::npos) {
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000953 Buf += Name.substr(0, Pos);
954 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
955 Buf += Name.substr(Pos + Skip);
956 Name = Buf;
Rafael Espindola88182132010-10-27 15:18:17 +0000957 }
958
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000959 uint64_t &Entry = StringIndexMap[Name];
Rafael Espindolaa6866962010-10-27 14:44:52 +0000960 if (!Entry) {
961 Entry = StringTable.size();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000962 StringTable += Name;
Rafael Espindolaa6866962010-10-27 14:44:52 +0000963 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +0000964 }
Rafael Espindolaa6866962010-10-27 14:44:52 +0000965 MSD.StringIndex = Entry;
966 if (MSD.SectionIndex == ELF::SHN_UNDEF)
967 UndefinedSymbolData.push_back(MSD);
968 else if (Local)
969 LocalSymbolData.push_back(MSD);
970 else
971 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +0000972 }
973
974 // Symbols are required to be in lexicographic order.
975 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
976 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
977 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
978
979 // Set the symbol indices. Local symbols must come before all other
980 // symbols with non-local bindings.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000981 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000982 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
983 LocalSymbolData[i].SymbolData->setIndex(Index++);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000984
985 Index += NumRegularSections;
986
Matt Fleming3565a062010-08-16 18:57:57 +0000987 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
988 ExternalSymbolData[i].SymbolData->setIndex(Index++);
989 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
990 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
991}
992
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000993void ELFObjectWriter::WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
994 const MCSectionData &SD) {
Matt Fleming3565a062010-08-16 18:57:57 +0000995 if (!Relocations[&SD].empty()) {
996 MCContext &Ctx = Asm.getContext();
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000997 const MCSectionELF *RelaSection;
Matt Fleming3565a062010-08-16 18:57:57 +0000998 const MCSectionELF &Section =
999 static_cast<const MCSectionELF&>(SD.getSection());
1000
1001 const StringRef SectionName = Section.getSectionName();
Rafael Espindolabff66a82010-12-18 03:27:34 +00001002 std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +00001003 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +00001004
1005 unsigned EntrySize;
Rafael Espindolabff66a82010-12-18 03:27:34 +00001006 if (hasRelocationAddend())
1007 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
Benjamin Kramer299fbe32010-08-17 17:56:13 +00001008 else
Rafael Espindolabff66a82010-12-18 03:27:34 +00001009 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +00001010
Rafael Espindolabff66a82010-12-18 03:27:34 +00001011 RelaSection = Ctx.getELFSection(RelaSectionName, hasRelocationAddend() ?
Benjamin Kramer377a5722010-08-17 17:30:07 +00001012 ELF::SHT_RELA : ELF::SHT_REL, 0,
Matt Fleming3565a062010-08-16 18:57:57 +00001013 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001014 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001015
1016 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +00001017 RelaSD.setAlignment(is64Bit() ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +00001018
1019 MCDataFragment *F = new MCDataFragment(&RelaSD);
1020
1021 WriteRelocationsFragment(Asm, F, &SD);
Matt Fleming3565a062010-08-16 18:57:57 +00001022 }
1023}
1024
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001025void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
1026 uint64_t Flags, uint64_t Address,
1027 uint64_t Offset, uint64_t Size,
1028 uint32_t Link, uint32_t Info,
1029 uint64_t Alignment,
1030 uint64_t EntrySize) {
Matt Fleming3565a062010-08-16 18:57:57 +00001031 Write32(Name); // sh_name: index into string table
1032 Write32(Type); // sh_type
1033 WriteWord(Flags); // sh_flags
1034 WriteWord(Address); // sh_addr
1035 WriteWord(Offset); // sh_offset
1036 WriteWord(Size); // sh_size
1037 Write32(Link); // sh_link
1038 Write32(Info); // sh_info
1039 WriteWord(Alignment); // sh_addralign
1040 WriteWord(EntrySize); // sh_entsize
1041}
1042
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001043void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
1044 MCDataFragment *F,
1045 const MCSectionData *SD) {
Matt Fleming3565a062010-08-16 18:57:57 +00001046 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
1047 // sort by the r_offset just like gnu as does
1048 array_pod_sort(Relocs.begin(), Relocs.end());
1049
1050 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1051 ELFRelocationEntry entry = Relocs[e - i - 1];
1052
Rafael Espindola12203cc2010-11-21 00:48:25 +00001053 if (!entry.Index)
1054 ;
1055 else if (entry.Index < 0)
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001056 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
1057 else
Rafael Espindola12203cc2010-11-21 00:48:25 +00001058 entry.Index += LocalSymbolData.size();
Rafael Espindolabff66a82010-12-18 03:27:34 +00001059 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001060 String64(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001061
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001062 struct ELF::Elf64_Rela ERE64;
1063 ERE64.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001064 String64(*F, ERE64.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001065
Rafael Espindolabff66a82010-12-18 03:27:34 +00001066 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001067 String64(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001068 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001069 String32(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001070
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001071 struct ELF::Elf32_Rela ERE32;
1072 ERE32.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001073 String32(*F, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001074
Rafael Espindolabff66a82010-12-18 03:27:34 +00001075 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001076 String32(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001077 }
Matt Fleming3565a062010-08-16 18:57:57 +00001078 }
1079}
1080
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001081void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
1082 MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001083 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +00001084 MCContext &Ctx = Asm.getContext();
1085 MCDataFragment *F;
1086
Rafael Espindolabff66a82010-12-18 03:27:34 +00001087 unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
Matt Fleming3565a062010-08-16 18:57:57 +00001088
Rafael Espindola38738bf2010-09-22 19:04:41 +00001089 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001090 const MCSectionELF *ShstrtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001091 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001092 SectionKind::getReadOnly());
Rafael Espindola71859c62010-09-16 19:46:31 +00001093 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1094 ShstrtabSD.setAlignment(1);
1095 ShstrtabIndex = Asm.size();
1096
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001097 const MCSectionELF *SymtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001098 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1099 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001100 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001101 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +00001102 SymtabSD.setAlignment(is64Bit() ? 8 : 4);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001103 SymbolTableIndex = Asm.size();
1104
1105 MCSectionData *SymtabShndxSD = NULL;
1106
1107 if (NeedsSymtabShndx) {
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001108 const MCSectionELF *SymtabShndxSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001109 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001110 SectionKind::getReadOnly(), 4, "");
Rafael Espindola7be2c332010-10-31 00:16:26 +00001111 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
1112 SymtabShndxSD->setAlignment(4);
1113 }
Matt Fleming3565a062010-08-16 18:57:57 +00001114
Matt Fleming3565a062010-08-16 18:57:57 +00001115 const MCSection *StrtabSection;
1116 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001117 SectionKind::getReadOnly());
Matt Fleming3565a062010-08-16 18:57:57 +00001118 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1119 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +00001120 StringTableIndex = Asm.size();
1121
Rafael Espindolac3c413f2010-09-27 22:04:54 +00001122 WriteRelocations(Asm, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001123
1124 // Symbol table
1125 F = new MCDataFragment(&SymtabSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001126 MCDataFragment *ShndxF = NULL;
1127 if (NeedsSymtabShndx) {
1128 ShndxF = new MCDataFragment(SymtabShndxSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001129 }
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001130 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
Rafael Espindola71859c62010-09-16 19:46:31 +00001131
Matt Fleming3565a062010-08-16 18:57:57 +00001132 F = new MCDataFragment(&StrtabSD);
1133 F->getContents().append(StringTable.begin(), StringTable.end());
Matt Fleming3565a062010-08-16 18:57:57 +00001134
Matt Fleming3565a062010-08-16 18:57:57 +00001135 F = new MCDataFragment(&ShstrtabSD);
1136
Matt Fleming3565a062010-08-16 18:57:57 +00001137 // Section header string table.
1138 //
1139 // The first entry of a string table holds a null character so skip
1140 // section 0.
1141 uint64_t Index = 1;
1142 F->getContents() += '\x00';
1143
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001144 StringMap<uint64_t> SecStringMap;
Matt Fleming3565a062010-08-16 18:57:57 +00001145 for (MCAssembler::const_iterator it = Asm.begin(),
1146 ie = Asm.end(); it != ie; ++it) {
Matt Fleming3565a062010-08-16 18:57:57 +00001147 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +00001148 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola51efe7a2010-09-23 14:14:56 +00001149 // FIXME: We could merge suffixes like in .text and .rela.text.
Matt Fleming3565a062010-08-16 18:57:57 +00001150
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001151 StringRef Name = Section.getSectionName();
1152 if (SecStringMap.count(Name)) {
1153 SectionStringTableIndex[&Section] = SecStringMap[Name];
1154 continue;
1155 }
Matt Fleming3565a062010-08-16 18:57:57 +00001156 // Remember the index into the string table so we can write it
1157 // into the sh_name field of the section header table.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001158 SectionStringTableIndex[&Section] = Index;
1159 SecStringMap[Name] = Index;
Matt Fleming3565a062010-08-16 18:57:57 +00001160
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001161 Index += Name.size() + 1;
1162 F->getContents() += Name;
Matt Fleming3565a062010-08-16 18:57:57 +00001163 F->getContents() += '\x00';
1164 }
Rafael Espindola70703872010-09-30 02:22:20 +00001165}
1166
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001167bool ELFObjectWriter::IsFixupFullyResolved(const MCAssembler &Asm,
1168 const MCValue Target,
1169 bool IsPCRel,
1170 const MCFragment *DF) const {
Rafael Espindola70703872010-09-30 02:22:20 +00001171 // If this is a PCrel relocation, find the section this fixup value is
1172 // relative to.
1173 const MCSection *BaseSection = 0;
1174 if (IsPCRel) {
1175 BaseSection = &DF->getParent()->getSection();
1176 assert(BaseSection);
1177 }
1178
1179 const MCSection *SectionA = 0;
1180 const MCSymbol *SymbolA = 0;
1181 if (const MCSymbolRefExpr *A = Target.getSymA()) {
Rafael Espindola2c920852010-11-16 04:11:46 +00001182 SymbolA = &A->getSymbol();
1183 SectionA = &SymbolA->AliasedSymbol().getSection();
Rafael Espindola70703872010-09-30 02:22:20 +00001184 }
1185
1186 const MCSection *SectionB = 0;
Rafael Espindola12203cc2010-11-21 00:48:25 +00001187 const MCSymbol *SymbolB = 0;
Rafael Espindola70703872010-09-30 02:22:20 +00001188 if (const MCSymbolRefExpr *B = Target.getSymB()) {
Rafael Espindola12203cc2010-11-21 00:48:25 +00001189 SymbolB = &B->getSymbol();
1190 SectionB = &SymbolB->AliasedSymbol().getSection();
Rafael Espindola70703872010-09-30 02:22:20 +00001191 }
1192
1193 if (!BaseSection)
1194 return SectionA == SectionB;
1195
Rafael Espindola12203cc2010-11-21 00:48:25 +00001196 if (SymbolB)
1197 return false;
1198
1199 // Absolute address but PCrel instruction, so we need a relocation.
1200 if (!SymbolA)
1201 return false;
1202
Rafael Espindola2c920852010-11-16 04:11:46 +00001203 // FIXME: This is in here just to match gnu as output. If the two ends
1204 // are in the same section, there is nothing that the linker can do to
1205 // break it.
Rafael Espindola70703872010-09-30 02:22:20 +00001206 const MCSymbolData &DataA = Asm.getSymbolData(*SymbolA);
1207 if (DataA.isExternal())
1208 return false;
1209
Rafael Espindola12203cc2010-11-21 00:48:25 +00001210 return BaseSection == SectionA;
Matt Fleming3565a062010-08-16 18:57:57 +00001211}
1212
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001213void ELFObjectWriter::CreateGroupSections(MCAssembler &Asm,
1214 MCAsmLayout &Layout,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001215 GroupMapTy &GroupMap,
1216 RevGroupMapTy &RevGroupMap) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001217 // Build the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001218 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1219 it != ie; ++it) {
1220 const MCSectionELF &Section =
1221 static_cast<const MCSectionELF&>(it->getSection());
1222 if (!(Section.getFlags() & MCSectionELF::SHF_GROUP))
1223 continue;
1224
1225 const MCSymbol *SignatureSymbol = Section.getGroup();
1226 Asm.getOrCreateSymbolData(*SignatureSymbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001227 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001228 if (!Group) {
1229 Group = Asm.getContext().CreateELFGroupSection();
1230 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1231 Data.setAlignment(4);
1232 MCDataFragment *F = new MCDataFragment(&Data);
1233 String32(*F, ELF::GRP_COMDAT);
1234 }
1235 GroupMap[Group] = SignatureSymbol;
1236 }
1237
1238 // Add sections to the groups
1239 unsigned Index = 1;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001240 unsigned NumGroups = RevGroupMap.size();
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001241 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1242 it != ie; ++it, ++Index) {
1243 const MCSectionELF &Section =
1244 static_cast<const MCSectionELF&>(it->getSection());
1245 if (!(Section.getFlags() & MCSectionELF::SHF_GROUP))
1246 continue;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001247 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001248 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1249 // FIXME: we could use the previous fragment
1250 MCDataFragment *F = new MCDataFragment(&Data);
1251 String32(*F, NumGroups + Index);
1252 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001253}
1254
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001255void ELFObjectWriter::WriteSection(MCAssembler &Asm,
1256 const SectionIndexMapTy &SectionIndexMap,
1257 uint32_t GroupSymbolIndex,
1258 uint64_t Offset, uint64_t Size,
1259 uint64_t Alignment,
1260 const MCSectionELF &Section) {
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001261 uint64_t sh_link = 0;
1262 uint64_t sh_info = 0;
1263
1264 switch(Section.getType()) {
1265 case ELF::SHT_DYNAMIC:
1266 sh_link = SectionStringTableIndex[&Section];
1267 sh_info = 0;
1268 break;
1269
1270 case ELF::SHT_REL:
1271 case ELF::SHT_RELA: {
1272 const MCSectionELF *SymtabSection;
1273 const MCSectionELF *InfoSection;
1274 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
1275 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001276 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001277 sh_link = SectionIndexMap.lookup(SymtabSection);
1278 assert(sh_link && ".symtab not found");
1279
1280 // Remove ".rel" and ".rela" prefixes.
1281 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1282 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
1283
1284 InfoSection = Asm.getContext().getELFSection(SectionName,
1285 ELF::SHT_PROGBITS, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001286 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001287 sh_info = SectionIndexMap.lookup(InfoSection);
1288 break;
1289 }
1290
1291 case ELF::SHT_SYMTAB:
1292 case ELF::SHT_DYNSYM:
1293 sh_link = StringTableIndex;
1294 sh_info = LastLocalSymbolIndex;
1295 break;
1296
1297 case ELF::SHT_SYMTAB_SHNDX:
1298 sh_link = SymbolTableIndex;
1299 break;
1300
1301 case ELF::SHT_PROGBITS:
1302 case ELF::SHT_STRTAB:
1303 case ELF::SHT_NOBITS:
1304 case ELF::SHT_NULL:
1305 case ELF::SHT_ARM_ATTRIBUTES:
1306 // Nothing to do.
1307 break;
1308
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001309 case ELF::SHT_GROUP: {
1310 sh_link = SymbolTableIndex;
1311 sh_info = GroupSymbolIndex;
1312 break;
1313 }
1314
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001315 default:
1316 assert(0 && "FIXME: sh_type value not supported!");
1317 break;
1318 }
1319
1320 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1321 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1322 Alignment, Section.getEntrySize());
1323}
1324
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001325static bool IsELFMetaDataSection(const MCSectionData &SD) {
Rafael Espindolaf8803fe2010-12-06 03:48:09 +00001326 return SD.getOrdinal() == ~UINT32_C(0) &&
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001327 !SD.getSection().isVirtualSection();
1328}
1329
1330static uint64_t DataSectionSize(const MCSectionData &SD) {
1331 uint64_t Ret = 0;
1332 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1333 ++i) {
1334 const MCFragment &F = *i;
1335 assert(F.getKind() == MCFragment::FT_Data);
1336 Ret += cast<MCDataFragment>(F).getContents().size();
1337 }
1338 return Ret;
1339}
1340
1341static uint64_t GetSectionFileSize(const MCAsmLayout &Layout,
1342 const MCSectionData &SD) {
1343 if (IsELFMetaDataSection(SD))
1344 return DataSectionSize(SD);
1345 return Layout.getSectionFileSize(&SD);
1346}
1347
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001348static uint64_t GetSectionAddressSize(const MCAsmLayout &Layout,
1349 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001350 if (IsELFMetaDataSection(SD))
1351 return DataSectionSize(SD);
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001352 return Layout.getSectionAddressSize(&SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001353}
1354
1355static void WriteDataSectionData(ELFObjectWriter *W, const MCSectionData &SD) {
1356 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1357 ++i) {
1358 const MCFragment &F = *i;
1359 assert(F.getKind() == MCFragment::FT_Data);
1360 W->WriteBytes(cast<MCDataFragment>(F).getContents().str());
1361 }
1362}
1363
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001364void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1365 const MCAsmLayout &Layout) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001366 GroupMapTy GroupMap;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001367 RevGroupMapTy RevGroupMap;
1368 CreateGroupSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1369 RevGroupMap);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001370
Rafael Espindolabab2a802010-11-10 21:51:05 +00001371 SectionIndexMapTy SectionIndexMap;
1372
1373 ComputeIndexMap(Asm, SectionIndexMap);
1374
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001375 // Compute symbol table information.
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001376 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap);
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001377
Matt Fleming3565a062010-08-16 18:57:57 +00001378 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001379 const_cast<MCAsmLayout&>(Layout),
1380 SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +00001381
Rafael Espindola1d739a02010-11-10 22:34:07 +00001382 // Update to include the metadata sections.
1383 ComputeIndexMap(Asm, SectionIndexMap);
1384
Matt Fleming3565a062010-08-16 18:57:57 +00001385 // Add 1 for the null section.
1386 unsigned NumSections = Asm.size() + 1;
Rafael Espindolabff66a82010-12-18 03:27:34 +00001387 uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
1388 uint64_t HeaderSize = is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
1389 sizeof(ELF::Elf32_Ehdr);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001390 uint64_t FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001391
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001392 std::vector<const MCSectionELF*> Sections;
1393 Sections.resize(NumSections);
1394
1395 for (SectionIndexMapTy::const_iterator i=
1396 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1397 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
1398 Sections[p.second] = p.first;
1399 }
1400
1401 for (unsigned i = 1; i < NumSections; ++i) {
1402 const MCSectionELF &Section = *Sections[i];
1403 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001404
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001405 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1406
Matt Fleming3565a062010-08-16 18:57:57 +00001407 // Get the size of the section in the output file (including padding).
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001408 FileOff += GetSectionFileSize(Layout, SD);
Matt Fleming3565a062010-08-16 18:57:57 +00001409 }
1410
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001411 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1412
Matt Fleming3565a062010-08-16 18:57:57 +00001413 // Write out the ELF header ...
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001414 WriteHeader(FileOff - HeaderSize, NumSections);
1415
1416 FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001417
1418 // ... then all of the sections ...
1419 DenseMap<const MCSection*, uint64_t> SectionOffsetMap;
1420
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001421 for (unsigned i = 1; i < NumSections; ++i) {
1422 const MCSectionELF &Section = *Sections[i];
1423 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001424
1425 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1426 WriteZeros(Padding);
1427 FileOff += Padding;
1428
Matt Fleming3565a062010-08-16 18:57:57 +00001429 // Remember the offset into the file for this section.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001430 SectionOffsetMap[&Section] = FileOff;
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001431
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001432 FileOff += GetSectionFileSize(Layout, SD);
Matt Fleming3565a062010-08-16 18:57:57 +00001433
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001434 if (IsELFMetaDataSection(SD))
1435 WriteDataSectionData(this, SD);
1436 else
Daniel Dunbar5d2477c2010-12-17 02:45:59 +00001437 Asm.WriteSectionData(&SD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +00001438 }
1439
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001440 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1441 WriteZeros(Padding);
1442 FileOff += Padding;
1443
Matt Fleming3565a062010-08-16 18:57:57 +00001444 // ... and then the section header table.
1445 // Should we align the section header table?
1446 //
1447 // Null section first.
Rafael Espindola7be2c332010-10-31 00:16:26 +00001448 uint64_t FirstSectionSize =
1449 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1450 uint32_t FirstSectionLink =
1451 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1452 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming3565a062010-08-16 18:57:57 +00001453
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001454 for (unsigned i = 1; i < NumSections; ++i) {
1455 const MCSectionELF &Section = *Sections[i];
1456 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1457 uint32_t GroupSymbolIndex;
1458 if (Section.getType() != ELF::SHT_GROUP)
1459 GroupSymbolIndex = 0;
1460 else
1461 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm, GroupMap[&Section]);
Matt Fleming3565a062010-08-16 18:57:57 +00001462
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001463 uint64_t Size = GetSectionAddressSize(Layout, SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001464
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001465 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001466 SectionOffsetMap[&Section], Size,
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001467 SD.getAlignment(), Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001468 }
1469}
1470
Rafael Espindola6024c972010-12-17 17:45:22 +00001471MCObjectWriter *llvm::createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1472 raw_ostream &OS,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001473 bool IsLittleEndian) {
1474 switch (MOTW->getEMachine()) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001475 case ELF::EM_386:
1476 case ELF::EM_X86_64:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001477 return new X86ELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001478 case ELF::EM_ARM:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001479 return new ARMELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Wesley Peck4b047132010-11-21 22:06:28 +00001480 case ELF::EM_MBLAZE:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001481 return new MBlazeELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Benjamin Kramer32858772010-11-15 19:20:50 +00001482 default: llvm_unreachable("Unsupported architecture"); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001483 }
1484}
1485
1486
1487/// START OF SUBCLASSES for ELFObjectWriter
1488//===- ARMELFObjectWriter -------------------------------------------===//
1489
Rafael Espindola31f35782010-12-17 18:01:31 +00001490ARMELFObjectWriter::ARMELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001491 raw_ostream &_OS,
1492 bool IsLittleEndian)
1493 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
Jason W Kimd3443e92010-11-15 16:18:39 +00001494{}
1495
1496ARMELFObjectWriter::~ARMELFObjectWriter()
1497{}
1498
Jason W Kim85fed5e2010-12-01 02:40:06 +00001499unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target,
1500 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001501 bool IsPCRel,
1502 bool IsRelocWithSymbol,
1503 int64_t Addend) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001504 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1505 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
1506
Jason W Kima0871e72010-12-08 23:14:44 +00001507 unsigned Type = 0;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001508 if (IsPCRel) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001509 switch ((unsigned)Fixup.getKind()) {
1510 default: assert(0 && "Unimplemented");
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001511 case FK_Data_4:
1512 switch (Modifier) {
1513 default: llvm_unreachable("Unsupported Modifier");
1514 case MCSymbolRefExpr::VK_None:
1515 Type = ELF::R_ARM_BASE_PREL; break;
1516 case MCSymbolRefExpr::VK_ARM_TLSGD:
1517 assert(0 && "unimplemented"); break;
1518 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
1519 Type = ELF::R_ARM_TLS_IE32;
1520 } break;
1521 case ARM::fixup_arm_branch:
1522 switch (Modifier) {
1523 case MCSymbolRefExpr::VK_ARM_PLT:
1524 Type = ELF::R_ARM_PLT32; break;
1525 default:
1526 Type = ELF::R_ARM_CALL; break;
1527 } break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001528 }
1529 } else {
1530 switch ((unsigned)Fixup.getKind()) {
1531 default: llvm_unreachable("invalid fixup kind!");
Jason W Kima0871e72010-12-08 23:14:44 +00001532 case FK_Data_4:
1533 switch (Modifier) {
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001534 default: llvm_unreachable("Unsupported Modifier"); break;
1535 case MCSymbolRefExpr::VK_ARM_GOT:
1536 Type = ELF::R_ARM_GOT_BREL; break;
1537 case MCSymbolRefExpr::VK_ARM_TLSGD:
1538 Type = ELF::R_ARM_TLS_GD32; break;
Jason W Kimf13743b2010-12-16 03:12:17 +00001539 case MCSymbolRefExpr::VK_ARM_TPOFF:
1540 Type = ELF::R_ARM_TLS_LE32; break;
Jason W Kima0871e72010-12-08 23:14:44 +00001541 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001542 Type = ELF::R_ARM_TLS_IE32; break;
Jason W Kimf13743b2010-12-16 03:12:17 +00001543 case MCSymbolRefExpr::VK_None:
1544 Type = ELF::R_ARM_ABS32; break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001545 case MCSymbolRefExpr::VK_ARM_GOTOFF:
1546 Type = ELF::R_ARM_GOTOFF32; break;
Jason W Kima0871e72010-12-08 23:14:44 +00001547 } break;
Jim Grosbachdff84b02010-12-02 00:28:45 +00001548 case ARM::fixup_arm_ldst_pcrel_12:
Owen Anderson9d63d902010-12-01 19:18:46 +00001549 case ARM::fixup_arm_pcrel_10:
Jim Grosbachdff84b02010-12-02 00:28:45 +00001550 case ARM::fixup_arm_adr_pcrel_12:
Jim Grosbach662a8162010-12-06 23:57:07 +00001551 case ARM::fixup_arm_thumb_bl:
Jim Grosbachb492a7c2010-12-09 19:50:12 +00001552 case ARM::fixup_arm_thumb_cb:
Bill Wendlingb8958b02010-12-08 01:57:09 +00001553 case ARM::fixup_arm_thumb_cp:
Jim Grosbache2467172010-12-10 18:21:33 +00001554 case ARM::fixup_arm_thumb_br:
Jason W Kim85fed5e2010-12-01 02:40:06 +00001555 assert(0 && "Unimplemented"); break;
1556 case ARM::fixup_arm_branch:
Jason W Kimf13743b2010-12-16 03:12:17 +00001557 // FIXME: Differentiate between R_ARM_CALL and
1558 // R_ARM_JUMP24 (latter used for conditional jumps)
Jason W Kima0871e72010-12-08 23:14:44 +00001559 Type = ELF::R_ARM_CALL; break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001560 case ARM::fixup_arm_movt_hi16:
Jason W Kima0871e72010-12-08 23:14:44 +00001561 Type = ELF::R_ARM_MOVT_ABS; break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001562 case ARM::fixup_arm_movw_lo16:
Jason W Kima0871e72010-12-08 23:14:44 +00001563 Type = ELF::R_ARM_MOVW_ABS_NC; break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001564 }
1565 }
1566
1567 if (RelocNeedsGOT(Modifier))
1568 NeedsGOT = true;
Jason W Kima0871e72010-12-08 23:14:44 +00001569
1570 return Type;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001571}
1572
Wesley Peck4b047132010-11-21 22:06:28 +00001573//===- MBlazeELFObjectWriter -------------------------------------------===//
Jason W Kimd3443e92010-11-15 16:18:39 +00001574
Rafael Espindola31f35782010-12-17 18:01:31 +00001575MBlazeELFObjectWriter::MBlazeELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001576 raw_ostream &_OS,
1577 bool IsLittleEndian)
1578 : ELFObjectWriter(MOTW, _OS, IsLittleEndian) {
Wesley Peck4b047132010-11-21 22:06:28 +00001579}
1580
1581MBlazeELFObjectWriter::~MBlazeELFObjectWriter() {
1582}
1583
Jason W Kim56a39902010-12-06 21:57:34 +00001584unsigned MBlazeELFObjectWriter::GetRelocType(const MCValue &Target,
Wesley Peck4b047132010-11-21 22:06:28 +00001585 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001586 bool IsPCRel,
1587 bool IsRelocWithSymbol,
1588 int64_t Addend) {
Wesley Peck4b047132010-11-21 22:06:28 +00001589 // determine the type of the relocation
1590 unsigned Type;
1591 if (IsPCRel) {
1592 switch ((unsigned)Fixup.getKind()) {
1593 default:
1594 llvm_unreachable("Unimplemented");
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001595 case FK_PCRel_4:
Wesley Peck4b047132010-11-21 22:06:28 +00001596 Type = ELF::R_MICROBLAZE_64_PCREL;
1597 break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001598 case FK_PCRel_2:
Wesley Peck4b047132010-11-21 22:06:28 +00001599 Type = ELF::R_MICROBLAZE_32_PCREL;
1600 break;
1601 }
1602 } else {
1603 switch ((unsigned)Fixup.getKind()) {
1604 default: llvm_unreachable("invalid fixup kind!");
1605 case FK_Data_4:
Jason W Kim56a39902010-12-06 21:57:34 +00001606 Type = ((IsRelocWithSymbol || Addend !=0)
1607 ? ELF::R_MICROBLAZE_32
1608 : ELF::R_MICROBLAZE_64);
Wesley Peck4b047132010-11-21 22:06:28 +00001609 break;
1610 case FK_Data_2:
1611 Type = ELF::R_MICROBLAZE_32;
1612 break;
1613 }
1614 }
Jason W Kim56a39902010-12-06 21:57:34 +00001615 return Type;
Wesley Peck4b047132010-11-21 22:06:28 +00001616}
Jason W Kimd3443e92010-11-15 16:18:39 +00001617
1618//===- X86ELFObjectWriter -------------------------------------------===//
1619
1620
Rafael Espindola31f35782010-12-17 18:01:31 +00001621X86ELFObjectWriter::X86ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001622 raw_ostream &_OS,
1623 bool IsLittleEndian)
1624 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
Jason W Kimd3443e92010-11-15 16:18:39 +00001625{}
1626
1627X86ELFObjectWriter::~X86ELFObjectWriter()
1628{}
1629
Jason W Kim56a39902010-12-06 21:57:34 +00001630unsigned X86ELFObjectWriter::GetRelocType(const MCValue &Target,
1631 const MCFixup &Fixup,
1632 bool IsPCRel,
1633 bool IsRelocWithSymbol,
1634 int64_t Addend) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001635 // determine the type of the relocation
1636
Rafael Espindola12203cc2010-11-21 00:48:25 +00001637 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1638 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
Jason W Kimd3443e92010-11-15 16:18:39 +00001639 unsigned Type;
Rafael Espindolabff66a82010-12-18 03:27:34 +00001640 if (is64Bit()) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001641 if (IsPCRel) {
1642 switch (Modifier) {
1643 default:
1644 llvm_unreachable("Unimplemented");
1645 case MCSymbolRefExpr::VK_None:
1646 Type = ELF::R_X86_64_PC32;
1647 break;
1648 case MCSymbolRefExpr::VK_PLT:
1649 Type = ELF::R_X86_64_PLT32;
1650 break;
1651 case MCSymbolRefExpr::VK_GOTPCREL:
1652 Type = ELF::R_X86_64_GOTPCREL;
1653 break;
1654 case MCSymbolRefExpr::VK_GOTTPOFF:
1655 Type = ELF::R_X86_64_GOTTPOFF;
1656 break;
1657 case MCSymbolRefExpr::VK_TLSGD:
1658 Type = ELF::R_X86_64_TLSGD;
1659 break;
1660 case MCSymbolRefExpr::VK_TLSLD:
1661 Type = ELF::R_X86_64_TLSLD;
1662 break;
1663 }
1664 } else {
1665 switch ((unsigned)Fixup.getKind()) {
1666 default: llvm_unreachable("invalid fixup kind!");
1667 case FK_Data_8: Type = ELF::R_X86_64_64; break;
1668 case X86::reloc_signed_4byte:
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001669 case FK_PCRel_4:
Jason W Kimd3443e92010-11-15 16:18:39 +00001670 assert(isInt<32>(Target.getConstant()));
1671 switch (Modifier) {
1672 default:
1673 llvm_unreachable("Unimplemented");
1674 case MCSymbolRefExpr::VK_None:
1675 Type = ELF::R_X86_64_32S;
1676 break;
1677 case MCSymbolRefExpr::VK_GOT:
1678 Type = ELF::R_X86_64_GOT32;
1679 break;
1680 case MCSymbolRefExpr::VK_GOTPCREL:
1681 Type = ELF::R_X86_64_GOTPCREL;
1682 break;
1683 case MCSymbolRefExpr::VK_TPOFF:
1684 Type = ELF::R_X86_64_TPOFF32;
1685 break;
1686 case MCSymbolRefExpr::VK_DTPOFF:
1687 Type = ELF::R_X86_64_DTPOFF32;
1688 break;
1689 }
1690 break;
1691 case FK_Data_4:
1692 Type = ELF::R_X86_64_32;
1693 break;
1694 case FK_Data_2: Type = ELF::R_X86_64_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001695 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001696 case FK_Data_1: Type = ELF::R_X86_64_8; break;
1697 }
1698 }
1699 } else {
1700 if (IsPCRel) {
1701 switch (Modifier) {
1702 default:
1703 llvm_unreachable("Unimplemented");
1704 case MCSymbolRefExpr::VK_None:
1705 Type = ELF::R_386_PC32;
1706 break;
1707 case MCSymbolRefExpr::VK_PLT:
1708 Type = ELF::R_386_PLT32;
1709 break;
1710 }
1711 } else {
1712 switch ((unsigned)Fixup.getKind()) {
1713 default: llvm_unreachable("invalid fixup kind!");
1714
1715 case X86::reloc_global_offset_table:
1716 Type = ELF::R_386_GOTPC;
1717 break;
1718
1719 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
1720 // instead?
1721 case X86::reloc_signed_4byte:
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001722 case FK_PCRel_4:
Jason W Kimd3443e92010-11-15 16:18:39 +00001723 case FK_Data_4:
1724 switch (Modifier) {
1725 default:
1726 llvm_unreachable("Unimplemented");
1727 case MCSymbolRefExpr::VK_None:
1728 Type = ELF::R_386_32;
1729 break;
1730 case MCSymbolRefExpr::VK_GOT:
1731 Type = ELF::R_386_GOT32;
1732 break;
1733 case MCSymbolRefExpr::VK_GOTOFF:
1734 Type = ELF::R_386_GOTOFF;
1735 break;
1736 case MCSymbolRefExpr::VK_TLSGD:
1737 Type = ELF::R_386_TLS_GD;
1738 break;
1739 case MCSymbolRefExpr::VK_TPOFF:
1740 Type = ELF::R_386_TLS_LE_32;
1741 break;
1742 case MCSymbolRefExpr::VK_INDNTPOFF:
1743 Type = ELF::R_386_TLS_IE;
1744 break;
1745 case MCSymbolRefExpr::VK_NTPOFF:
1746 Type = ELF::R_386_TLS_LE;
1747 break;
1748 case MCSymbolRefExpr::VK_GOTNTPOFF:
1749 Type = ELF::R_386_TLS_GOTIE;
1750 break;
1751 case MCSymbolRefExpr::VK_TLSLDM:
1752 Type = ELF::R_386_TLS_LDM;
1753 break;
1754 case MCSymbolRefExpr::VK_DTPOFF:
1755 Type = ELF::R_386_TLS_LDO_32;
1756 break;
1757 }
1758 break;
1759 case FK_Data_2: Type = ELF::R_386_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001760 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001761 case FK_Data_1: Type = ELF::R_386_8; break;
1762 }
1763 }
1764 }
1765
1766 if (RelocNeedsGOT(Modifier))
1767 NeedsGOT = true;
1768
Jason W Kim56a39902010-12-06 21:57:34 +00001769 return Type;
Matt Fleming3565a062010-08-16 18:57:57 +00001770}