blob: b6e1223bc418a1e6de7df9195c497fe1eeb76670 [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
Jason W Kimd3443e92010-11-15 16:18:39 +0000347 virtual bool IsFixupFullyResolved(const MCAssembler &Asm,
Rafael Espindola70703872010-09-30 02:22:20 +0000348 const MCValue Target,
349 bool IsPCRel,
350 const MCFragment *DF) const;
351
Jason W Kimd3443e92010-11-15 16:18:39 +0000352 virtual void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
353 virtual void WriteSection(MCAssembler &Asm,
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000354 const SectionIndexMapTy &SectionIndexMap,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000355 uint32_t GroupSymbolIndex,
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000356 uint64_t Offset, uint64_t Size, uint64_t Alignment,
357 const MCSectionELF &Section);
Jason W Kim56a39902010-12-06 21:57:34 +0000358
359 protected:
360 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
361 bool IsPCRel, bool IsRelocWithSymbol,
362 int64_t Addend) = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000363 };
364
Jason W Kimd3443e92010-11-15 16:18:39 +0000365 //===- X86ELFObjectWriter -------------------------------------------===//
366
367 class X86ELFObjectWriter : public ELFObjectWriter {
368 public:
Rafael Espindola31f35782010-12-17 18:01:31 +0000369 X86ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +0000370 raw_ostream &_OS,
371 bool IsLittleEndian);
Wesley Peck4b047132010-11-21 22:06:28 +0000372
Jason W Kimd3443e92010-11-15 16:18:39 +0000373 virtual ~X86ELFObjectWriter();
Jason W Kim56a39902010-12-06 21:57:34 +0000374 protected:
375 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
376 bool IsPCRel, bool IsRelocWithSymbol,
377 int64_t Addend);
Jason W Kimd3443e92010-11-15 16:18:39 +0000378 };
379
380
381 //===- ARMELFObjectWriter -------------------------------------------===//
382
383 class ARMELFObjectWriter : public ELFObjectWriter {
384 public:
Rafael Espindola31f35782010-12-17 18:01:31 +0000385 ARMELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +0000386 raw_ostream &_OS,
387 bool IsLittleEndian);
Wesley Peck4b047132010-11-21 22:06:28 +0000388
Jason W Kimd3443e92010-11-15 16:18:39 +0000389 virtual ~ARMELFObjectWriter();
Jason W Kim85fed5e2010-12-01 02:40:06 +0000390 protected:
Jason W Kim56a39902010-12-06 21:57:34 +0000391 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
392 bool IsPCRel, bool IsRelocWithSymbol,
393 int64_t Addend);
Jason W Kimd3443e92010-11-15 16:18:39 +0000394 };
Wesley Peck4b047132010-11-21 22:06:28 +0000395
396 //===- MBlazeELFObjectWriter -------------------------------------------===//
397
398 class MBlazeELFObjectWriter : public ELFObjectWriter {
399 public:
Rafael Espindola31f35782010-12-17 18:01:31 +0000400 MBlazeELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +0000401 raw_ostream &_OS,
402 bool IsLittleEndian);
Wesley Peck4b047132010-11-21 22:06:28 +0000403
404 virtual ~MBlazeELFObjectWriter();
Jason W Kim56a39902010-12-06 21:57:34 +0000405 protected:
406 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
407 bool IsPCRel, bool IsRelocWithSymbol,
408 int64_t Addend);
Wesley Peck4b047132010-11-21 22:06:28 +0000409 };
Matt Fleming3565a062010-08-16 18:57:57 +0000410}
411
Jason W Kimd3443e92010-11-15 16:18:39 +0000412ELFObjectWriter::~ELFObjectWriter()
413{}
414
Matt Fleming3565a062010-08-16 18:57:57 +0000415// Emit the ELF header.
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000416void ELFObjectWriter::WriteHeader(uint64_t SectionDataSize,
417 unsigned NumberOfSections) {
Matt Fleming3565a062010-08-16 18:57:57 +0000418 // ELF Header
419 // ----------
420 //
421 // Note
422 // ----
423 // emitWord method behaves differently for ELF32 and ELF64, writing
424 // 4 bytes in the former and 8 in the latter.
425
426 Write8(0x7f); // e_ident[EI_MAG0]
427 Write8('E'); // e_ident[EI_MAG1]
428 Write8('L'); // e_ident[EI_MAG2]
429 Write8('F'); // e_ident[EI_MAG3]
430
Rafael Espindolabff66a82010-12-18 03:27:34 +0000431 Write8(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
Matt Fleming3565a062010-08-16 18:57:57 +0000432
433 // e_ident[EI_DATA]
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000434 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
Matt Fleming3565a062010-08-16 18:57:57 +0000435
436 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky5baf79e2010-09-09 17:57:50 +0000437 // e_ident[EI_OSABI]
Rafael Espindolabff66a82010-12-18 03:27:34 +0000438 switch (TargetObjectWriter->getOSType()) {
Roman Divacky5baf79e2010-09-09 17:57:50 +0000439 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
440 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
441 default: Write8(ELF::ELFOSABI_NONE); break;
442 }
Matt Fleming3565a062010-08-16 18:57:57 +0000443 Write8(0); // e_ident[EI_ABIVERSION]
444
445 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
446
447 Write16(ELF::ET_REL); // e_type
448
Rafael Espindolabff66a82010-12-18 03:27:34 +0000449 Write16(TargetObjectWriter->getEMachine()); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +0000450
451 Write32(ELF::EV_CURRENT); // e_version
452 WriteWord(0); // e_entry, no entry point in .o file
453 WriteWord(0); // e_phoff, no program header for .o
Rafael Espindolabff66a82010-12-18 03:27:34 +0000454 WriteWord(SectionDataSize + (is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
Benjamin Kramereb976772010-08-17 17:02:29 +0000455 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000456
457 // FIXME: Make this configurable.
458 Write32(0); // e_flags = whatever the target wants
459
460 // e_ehsize = ELF header size
Rafael Espindolabff66a82010-12-18 03:27:34 +0000461 Write16(is64Bit() ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
Matt Fleming3565a062010-08-16 18:57:57 +0000462
463 Write16(0); // e_phentsize = prog header entry size
464 Write16(0); // e_phnum = # prog header entries = 0
465
466 // e_shentsize = Section header entry size
Rafael Espindolabff66a82010-12-18 03:27:34 +0000467 Write16(is64Bit() ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
Matt Fleming3565a062010-08-16 18:57:57 +0000468
469 // e_shnum = # of section header ents
Rafael Espindola7be2c332010-10-31 00:16:26 +0000470 if (NumberOfSections >= ELF::SHN_LORESERVE)
471 Write16(0);
472 else
473 Write16(NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000474
475 // e_shstrndx = Section # of '.shstrtab'
Rafael Espindola7be2c332010-10-31 00:16:26 +0000476 if (NumberOfSections >= ELF::SHN_LORESERVE)
477 Write16(ELF::SHN_XINDEX);
478 else
479 Write16(ShstrtabIndex);
Matt Fleming3565a062010-08-16 18:57:57 +0000480}
481
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000482void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
483 MCDataFragment *ShndxF,
484 uint64_t name,
485 uint8_t info, uint64_t value,
486 uint64_t size, uint8_t other,
487 uint32_t shndx,
488 bool Reserved) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000489 if (ShndxF) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000490 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000491 String32(*ShndxF, shndx);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000492 else
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000493 String32(*ShndxF, 0);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000494 }
495
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000496 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
497 uint16_t(ELF::SHN_XINDEX) : shndx;
498
Rafael Espindolabff66a82010-12-18 03:27:34 +0000499 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000500 String32(*SymtabF, name); // st_name
501 String8(*SymtabF, info); // st_info
502 String8(*SymtabF, other); // st_other
503 String16(*SymtabF, Index); // st_shndx
504 String64(*SymtabF, value); // st_value
505 String64(*SymtabF, size); // st_size
Matt Fleming3565a062010-08-16 18:57:57 +0000506 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000507 String32(*SymtabF, name); // st_name
508 String32(*SymtabF, value); // st_value
509 String32(*SymtabF, size); // st_size
510 String8(*SymtabF, info); // st_info
511 String8(*SymtabF, other); // st_other
512 String16(*SymtabF, Index); // st_shndx
Matt Fleming3565a062010-08-16 18:57:57 +0000513 }
514}
515
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000516static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout) {
517 if (Data.isCommon() && Data.isExternal())
518 return Data.getCommonAlignment();
519
520 const MCSymbol &Symbol = Data.getSymbol();
Roman Divackyd1491862010-12-20 21:14:39 +0000521
522 if (Symbol.isAbsolute() && Symbol.isVariable()) {
523 if (const MCExpr *Value = Symbol.getVariableValue()) {
524 int64_t IntValue;
525 if (Value->EvaluateAsAbsolute(IntValue, Layout))
526 return (uint64_t)IntValue;
527 }
528 }
529
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000530 if (!Symbol.isInSection())
531 return 0;
532
Rafael Espindolaffd902b2010-12-06 02:57:26 +0000533 if (Data.getFragment())
534 return Layout.getSymbolOffset(&Data);
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000535
536 return 0;
537}
538
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000539void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
540 const MCAsmLayout &Layout) {
Rafael Espindola88182132010-10-27 15:18:17 +0000541 // The presence of symbol versions causes undefined symbols and
542 // versions declared with @@@ to be renamed.
543
544 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
545 ie = Asm.symbol_end(); it != ie; ++it) {
546 const MCSymbol &Alias = it->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000547 const MCSymbol &Symbol = Alias.AliasedSymbol();
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000548 MCSymbolData &SD = Asm.getSymbolData(Symbol);
549
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000550 // Not an alias.
551 if (&Symbol == &Alias)
552 continue;
553
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000554 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000555 size_t Pos = AliasName.find('@');
556 if (Pos == StringRef::npos)
557 continue;
558
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000559 // Aliases defined with .symvar copy the binding from the symbol they alias.
560 // This is the first place we are able to copy this information.
561 it->setExternal(SD.isExternal());
562 SetBinding(*it, GetBinding(SD));
563
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000564 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000565 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
566 continue;
567
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000568 // FIXME: produce a better error message.
569 if (Symbol.isUndefined() && Rest.startswith("@@") &&
570 !Rest.startswith("@@@"))
571 report_fatal_error("A @@ version cannot be undefined");
572
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000573 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000574 }
575}
576
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000577void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
578 MCDataFragment *ShndxF,
579 ELFSymbolData &MSD,
580 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000581 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000582 MCSymbolData &Data =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000583 Layout.getAssembler().getSymbolData(OrigData.getSymbol().AliasedSymbol());
Rafael Espindola152c1062010-10-06 21:02:29 +0000584
Rafael Espindola7be2c332010-10-31 00:16:26 +0000585 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
586 Data.getSymbol().isVariable();
587
Rafael Espindola152c1062010-10-06 21:02:29 +0000588 uint8_t Binding = GetBinding(OrigData);
589 uint8_t Visibility = GetVisibility(OrigData);
590 uint8_t Type = GetType(Data);
591
592 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
593 uint8_t Other = Visibility;
594
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000595 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000596 uint64_t Size = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000597
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000598 assert(!(Data.isCommon() && !Data.isExternal()));
599
Rafael Espindolaf0121242010-12-22 16:03:00 +0000600 const MCExpr *ESize = Data.getSize();
601 if (ESize) {
602 int64_t Res;
603 if (!ESize->EvaluateAsAbsolute(Res, Layout))
604 report_fatal_error("Size expression must be absolute.");
605 Size = Res;
Matt Fleming3565a062010-08-16 18:57:57 +0000606 }
607
608 // Write out the symbol table entry
Rafael Espindola7be2c332010-10-31 00:16:26 +0000609 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
610 Size, Other, MSD.SectionIndex, IsReserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000611}
612
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000613void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
614 MCDataFragment *ShndxF,
615 const MCAssembler &Asm,
616 const MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000617 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000618 // The string table must be emitted first because we need the index
619 // into the string table for all the symbol names.
620 assert(StringTable.size() && "Missing string table");
621
622 // FIXME: Make sure the start of the symbol table is aligned.
623
624 // The first entry is the undefined symbol entry.
Rafael Espindola7be2c332010-10-31 00:16:26 +0000625 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
Matt Fleming3565a062010-08-16 18:57:57 +0000626
627 // Write the symbol table entries.
628 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
629 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
630 ELFSymbolData &MSD = LocalSymbolData[i];
Rafael Espindola7be2c332010-10-31 00:16:26 +0000631 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000632 }
633
Rafael Espindola71859c62010-09-16 19:46:31 +0000634 // Write out a symbol table entry for each regular section.
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000635 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
636 ++i) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000637 const MCSectionELF &Section =
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000638 static_cast<const MCSectionELF&>(i->getSection());
639 if (Section.getType() == ELF::SHT_RELA ||
640 Section.getType() == ELF::SHT_REL ||
641 Section.getType() == ELF::SHT_STRTAB ||
642 Section.getType() == ELF::SHT_SYMTAB)
Eli Friedmana44fa242010-08-16 21:17:09 +0000643 continue;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000644 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000645 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section), false);
Eli Friedmana44fa242010-08-16 21:17:09 +0000646 LastLocalSymbolIndex++;
647 }
Matt Fleming3565a062010-08-16 18:57:57 +0000648
649 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
650 ELFSymbolData &MSD = ExternalSymbolData[i];
651 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000652 assert(((Data.getFlags() & ELF_STB_Global) ||
653 (Data.getFlags() & ELF_STB_Weak)) &&
654 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000655 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000656 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000657 LastLocalSymbolIndex++;
658 }
659
660 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
661 ELFSymbolData &MSD = UndefinedSymbolData[i];
662 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000663 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000664 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000665 LastLocalSymbolIndex++;
666 }
667}
668
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000669const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm,
670 const MCValue &Target,
671 const MCFragment &F) const {
672 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000673 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
674 const MCSymbol *Renamed = Renames.lookup(&Symbol);
675 const MCSymbolData &SD = Asm.getSymbolData(Symbol);
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000676
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000677 if (ASymbol.isUndefined()) {
678 if (Renamed)
679 return Renamed;
680 return &ASymbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000681 }
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000682
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000683 if (SD.isExternal()) {
684 if (Renamed)
685 return Renamed;
686 return &Symbol;
687 }
Rafael Espindola73ffea42010-09-25 05:42:19 +0000688
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000689 const MCSectionELF &Section =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000690 static_cast<const MCSectionELF&>(ASymbol.getSection());
Rafael Espindola25958732010-11-24 21:57:39 +0000691 const SectionKind secKind = Section.getKind();
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000692
Rafael Espindola25958732010-11-24 21:57:39 +0000693 if (secKind.isBSS())
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000694 return NULL;
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000695
Rafael Espindola25958732010-11-24 21:57:39 +0000696 if (secKind.isThreadLocal()) {
697 if (Renamed)
698 return Renamed;
699 return &Symbol;
700 }
701
Rafael Espindola8cecf252010-10-06 16:23:36 +0000702 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000703 const MCSectionELF &Sec2 =
704 static_cast<const MCSectionELF&>(F.getParent()->getSection());
705
Rafael Espindola8cecf252010-10-06 16:23:36 +0000706 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000707 (Kind == MCSymbolRefExpr::VK_PLT ||
708 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
Rafael Espindola25958732010-11-24 21:57:39 +0000709 Kind == MCSymbolRefExpr::VK_GOTOFF)) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000710 if (Renamed)
711 return Renamed;
712 return &Symbol;
713 }
Rafael Espindola3729d002010-10-05 23:57:26 +0000714
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000715 if (Section.getFlags() & MCSectionELF::SHF_MERGE) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000716 if (Target.getConstant() == 0)
717 return NULL;
718 if (Renamed)
719 return Renamed;
720 return &Symbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000721 }
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000722
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000723 return NULL;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000724}
725
Matt Fleming3565a062010-08-16 18:57:57 +0000726
Jason W Kim56a39902010-12-06 21:57:34 +0000727void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
728 const MCAsmLayout &Layout,
729 const MCFragment *Fragment,
730 const MCFixup &Fixup,
731 MCValue Target,
732 uint64_t &FixedValue) {
733 int64_t Addend = 0;
734 int Index = 0;
735 int64_t Value = Target.getConstant();
736 const MCSymbol *RelocSymbol = NULL;
737
Rafael Espindola127a6a42010-12-17 07:28:17 +0000738 bool IsPCRel = isFixupKindPCRel(Asm, Fixup.getKind());
Jason W Kim56a39902010-12-06 21:57:34 +0000739 if (!Target.isAbsolute()) {
740 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
741 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
742 RelocSymbol = SymbolToReloc(Asm, Target, *Fragment);
743
744 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
745 const MCSymbol &SymbolB = RefB->getSymbol();
746 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
747 IsPCRel = true;
748
749 // Offset of the symbol in the section
750 int64_t a = Layout.getSymbolOffset(&SDB);
751
752 // Ofeset of the relocation in the section
753 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
754 Value += b - a;
755 }
756
757 if (!RelocSymbol) {
758 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
759 MCFragment *F = SD.getFragment();
760
761 Index = F->getParent()->getOrdinal() + 1;
762
763 // Offset of the symbol in the section
764 Value += Layout.getSymbolOffset(&SD);
765 } else {
766 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
767 WeakrefUsedInReloc.insert(RelocSymbol);
768 else
769 UsedInReloc.insert(RelocSymbol);
770 Index = -1;
771 }
772 Addend = Value;
773 // Compensate for the addend on i386.
Rafael Espindolabff66a82010-12-18 03:27:34 +0000774 if (is64Bit())
Jason W Kim56a39902010-12-06 21:57:34 +0000775 Value = 0;
776 }
777
778 FixedValue = Value;
779 unsigned Type = GetRelocType(Target, Fixup, IsPCRel,
780 (RelocSymbol != 0), Addend);
781
782 uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
783 Fixup.getOffset();
784
Rafael Espindolabff66a82010-12-18 03:27:34 +0000785 if (!hasRelocationAddend())
786 Addend = 0;
Jason W Kim56a39902010-12-06 21:57:34 +0000787 ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend);
788 Relocations[Fragment->getParent()].push_back(ERE);
789}
790
791
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000792uint64_t
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000793ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
794 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000795 MCSymbolData &SD = Asm.getSymbolData(*S);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000796 return SD.getIndex();
Matt Fleming3565a062010-08-16 18:57:57 +0000797}
798
Rafael Espindola737cd212010-10-05 18:01:23 +0000799static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
Rafael Espindola88182132010-10-27 15:18:17 +0000800 bool Used, bool Renamed) {
Rafael Espindola484291c2010-11-01 14:28:48 +0000801 if (Data.getFlags() & ELF_Other_Weakref)
802 return false;
803
Rafael Espindolabd701182010-10-19 19:31:37 +0000804 if (Used)
805 return true;
806
Rafael Espindola88182132010-10-27 15:18:17 +0000807 if (Renamed)
808 return false;
809
Rafael Espindola737cd212010-10-05 18:01:23 +0000810 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000811
Rafael Espindolad1798862010-10-29 23:09:31 +0000812 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
813 return true;
814
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000815 const MCSymbol &A = Symbol.AliasedSymbol();
Rafael Espindolad1798862010-10-29 23:09:31 +0000816 if (!A.isVariable() && A.isUndefined() && !Data.isCommon())
Rafael Espindolaa6866962010-10-27 14:44:52 +0000817 return false;
818
Rafael Espindola737cd212010-10-05 18:01:23 +0000819 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
820 return false;
821
Rafael Espindolabd701182010-10-19 19:31:37 +0000822 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000823 return false;
824
825 return true;
826}
827
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000828static bool isLocal(const MCSymbolData &Data, bool isSignature,
829 bool isUsedInReloc) {
Rafael Espindola737cd212010-10-05 18:01:23 +0000830 if (Data.isExternal())
831 return false;
832
833 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000834 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000835
836 if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
837 if (isSignature && !isUsedInReloc)
838 return true;
839
Rafael Espindola737cd212010-10-05 18:01:23 +0000840 return false;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000841 }
Rafael Espindola737cd212010-10-05 18:01:23 +0000842
843 return true;
844}
845
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000846void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
847 SectionIndexMapTy &SectionIndexMap) {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000848 unsigned Index = 1;
849 for (MCAssembler::iterator it = Asm.begin(),
850 ie = Asm.end(); it != ie; ++it) {
851 const MCSectionELF &Section =
852 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000853 if (Section.getType() != ELF::SHT_GROUP)
854 continue;
855 SectionIndexMap[&Section] = Index++;
856 }
857
858 for (MCAssembler::iterator it = Asm.begin(),
859 ie = Asm.end(); it != ie; ++it) {
860 const MCSectionELF &Section =
861 static_cast<const MCSectionELF &>(it->getSection());
862 if (Section.getType() == ELF::SHT_GROUP)
863 continue;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000864 SectionIndexMap[&Section] = Index++;
865 }
866}
867
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000868void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000869 const SectionIndexMapTy &SectionIndexMap,
870 RevGroupMapTy RevGroupMap) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000871 // FIXME: Is this the correct place to do this?
872 if (NeedsGOT) {
873 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
874 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
875 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
876 Data.setExternal(true);
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000877 SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindola5c77c162010-10-05 15:48:37 +0000878 }
879
Matt Fleming3565a062010-08-16 18:57:57 +0000880 // Build section lookup table.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000881 int NumRegularSections = Asm.size();
Matt Fleming3565a062010-08-16 18:57:57 +0000882
883 // Index 0 is always the empty string.
884 StringMap<uint64_t> StringIndexMap;
885 StringTable += '\x00';
886
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000887 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000888 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
889 ie = Asm.symbol_end(); it != ie; ++it) {
890 const MCSymbol &Symbol = it->getSymbol();
891
Rafael Espindola484291c2010-11-01 14:28:48 +0000892 bool Used = UsedInReloc.count(&Symbol);
893 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000894 bool isSignature = RevGroupMap.count(&Symbol);
895
896 if (!isInSymtab(Asm, *it,
897 Used || WeakrefUsed || isSignature,
Rafael Espindola88182132010-10-27 15:18:17 +0000898 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000899 continue;
900
Matt Fleming3565a062010-08-16 18:57:57 +0000901 ELFSymbolData MSD;
902 MSD.SymbolData = it;
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000903 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Matt Fleming3565a062010-08-16 18:57:57 +0000904
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000905 // Undefined symbols are global, but this is the first place we
906 // are able to set it.
907 bool Local = isLocal(*it, isSignature, Used);
908 if (!Local && GetBinding(*it) == ELF::STB_LOCAL) {
909 MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
910 SetBinding(*it, ELF::STB_GLOBAL);
911 SetBinding(SD, ELF::STB_GLOBAL);
912 }
913
Rafael Espindola484291c2010-11-01 14:28:48 +0000914 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
915 SetBinding(*it, ELF::STB_WEAK);
916
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000917 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000918 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000919 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000920 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000921 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000922 } else if (RefSymbol.isUndefined()) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000923 if (isSignature && !Used)
924 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
925 else
926 MSD.SectionIndex = ELF::SHN_UNDEF;
Matt Fleming3565a062010-08-16 18:57:57 +0000927 } else {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000928 const MCSectionELF &Section =
929 static_cast<const MCSectionELF&>(RefSymbol.getSection());
930 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000931 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
932 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000933 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000934 }
935
Rafael Espindola88182132010-10-27 15:18:17 +0000936 // The @@@ in symbol version is replaced with @ in undefined symbols and
937 // @@ in defined ones.
938 StringRef Name = Symbol.getName();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000939 SmallString<32> Buf;
940
Rafael Espindola88182132010-10-27 15:18:17 +0000941 size_t Pos = Name.find("@@@");
Rafael Espindola88182132010-10-27 15:18:17 +0000942 if (Pos != StringRef::npos) {
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000943 Buf += Name.substr(0, Pos);
944 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
945 Buf += Name.substr(Pos + Skip);
946 Name = Buf;
Rafael Espindola88182132010-10-27 15:18:17 +0000947 }
948
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000949 uint64_t &Entry = StringIndexMap[Name];
Rafael Espindolaa6866962010-10-27 14:44:52 +0000950 if (!Entry) {
951 Entry = StringTable.size();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000952 StringTable += Name;
Rafael Espindolaa6866962010-10-27 14:44:52 +0000953 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +0000954 }
Rafael Espindolaa6866962010-10-27 14:44:52 +0000955 MSD.StringIndex = Entry;
956 if (MSD.SectionIndex == ELF::SHN_UNDEF)
957 UndefinedSymbolData.push_back(MSD);
958 else if (Local)
959 LocalSymbolData.push_back(MSD);
960 else
961 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +0000962 }
963
964 // Symbols are required to be in lexicographic order.
965 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
966 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
967 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
968
969 // Set the symbol indices. Local symbols must come before all other
970 // symbols with non-local bindings.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000971 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000972 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
973 LocalSymbolData[i].SymbolData->setIndex(Index++);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000974
975 Index += NumRegularSections;
976
Matt Fleming3565a062010-08-16 18:57:57 +0000977 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
978 ExternalSymbolData[i].SymbolData->setIndex(Index++);
979 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
980 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
981}
982
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000983void ELFObjectWriter::WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
984 const MCSectionData &SD) {
Matt Fleming3565a062010-08-16 18:57:57 +0000985 if (!Relocations[&SD].empty()) {
986 MCContext &Ctx = Asm.getContext();
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000987 const MCSectionELF *RelaSection;
Matt Fleming3565a062010-08-16 18:57:57 +0000988 const MCSectionELF &Section =
989 static_cast<const MCSectionELF&>(SD.getSection());
990
991 const StringRef SectionName = Section.getSectionName();
Rafael Espindolabff66a82010-12-18 03:27:34 +0000992 std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +0000993 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000994
995 unsigned EntrySize;
Rafael Espindolabff66a82010-12-18 03:27:34 +0000996 if (hasRelocationAddend())
997 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000998 else
Rafael Espindolabff66a82010-12-18 03:27:34 +0000999 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +00001000
Rafael Espindolabff66a82010-12-18 03:27:34 +00001001 RelaSection = Ctx.getELFSection(RelaSectionName, hasRelocationAddend() ?
Benjamin Kramer377a5722010-08-17 17:30:07 +00001002 ELF::SHT_RELA : ELF::SHT_REL, 0,
Matt Fleming3565a062010-08-16 18:57:57 +00001003 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001004 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001005
1006 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +00001007 RelaSD.setAlignment(is64Bit() ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +00001008
1009 MCDataFragment *F = new MCDataFragment(&RelaSD);
1010
1011 WriteRelocationsFragment(Asm, F, &SD);
Matt Fleming3565a062010-08-16 18:57:57 +00001012 }
1013}
1014
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001015void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
1016 uint64_t Flags, uint64_t Address,
1017 uint64_t Offset, uint64_t Size,
1018 uint32_t Link, uint32_t Info,
1019 uint64_t Alignment,
1020 uint64_t EntrySize) {
Matt Fleming3565a062010-08-16 18:57:57 +00001021 Write32(Name); // sh_name: index into string table
1022 Write32(Type); // sh_type
1023 WriteWord(Flags); // sh_flags
1024 WriteWord(Address); // sh_addr
1025 WriteWord(Offset); // sh_offset
1026 WriteWord(Size); // sh_size
1027 Write32(Link); // sh_link
1028 Write32(Info); // sh_info
1029 WriteWord(Alignment); // sh_addralign
1030 WriteWord(EntrySize); // sh_entsize
1031}
1032
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001033void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
1034 MCDataFragment *F,
1035 const MCSectionData *SD) {
Matt Fleming3565a062010-08-16 18:57:57 +00001036 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
1037 // sort by the r_offset just like gnu as does
1038 array_pod_sort(Relocs.begin(), Relocs.end());
1039
1040 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1041 ELFRelocationEntry entry = Relocs[e - i - 1];
1042
Rafael Espindola12203cc2010-11-21 00:48:25 +00001043 if (!entry.Index)
1044 ;
1045 else if (entry.Index < 0)
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001046 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
1047 else
Rafael Espindola12203cc2010-11-21 00:48:25 +00001048 entry.Index += LocalSymbolData.size();
Rafael Espindolabff66a82010-12-18 03:27:34 +00001049 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001050 String64(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001051
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001052 struct ELF::Elf64_Rela ERE64;
1053 ERE64.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001054 String64(*F, ERE64.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001055
Rafael Espindolabff66a82010-12-18 03:27:34 +00001056 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001057 String64(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001058 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001059 String32(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001060
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001061 struct ELF::Elf32_Rela ERE32;
1062 ERE32.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001063 String32(*F, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001064
Rafael Espindolabff66a82010-12-18 03:27:34 +00001065 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001066 String32(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001067 }
Matt Fleming3565a062010-08-16 18:57:57 +00001068 }
1069}
1070
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001071void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
1072 MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001073 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +00001074 MCContext &Ctx = Asm.getContext();
1075 MCDataFragment *F;
1076
Rafael Espindolabff66a82010-12-18 03:27:34 +00001077 unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
Matt Fleming3565a062010-08-16 18:57:57 +00001078
Rafael Espindola38738bf2010-09-22 19:04:41 +00001079 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001080 const MCSectionELF *ShstrtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001081 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001082 SectionKind::getReadOnly());
Rafael Espindola71859c62010-09-16 19:46:31 +00001083 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1084 ShstrtabSD.setAlignment(1);
1085 ShstrtabIndex = Asm.size();
1086
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001087 const MCSectionELF *SymtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001088 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1089 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001090 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001091 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +00001092 SymtabSD.setAlignment(is64Bit() ? 8 : 4);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001093 SymbolTableIndex = Asm.size();
1094
1095 MCSectionData *SymtabShndxSD = NULL;
1096
1097 if (NeedsSymtabShndx) {
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001098 const MCSectionELF *SymtabShndxSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001099 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001100 SectionKind::getReadOnly(), 4, "");
Rafael Espindola7be2c332010-10-31 00:16:26 +00001101 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
1102 SymtabShndxSD->setAlignment(4);
1103 }
Matt Fleming3565a062010-08-16 18:57:57 +00001104
Matt Fleming3565a062010-08-16 18:57:57 +00001105 const MCSection *StrtabSection;
1106 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001107 SectionKind::getReadOnly());
Matt Fleming3565a062010-08-16 18:57:57 +00001108 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1109 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +00001110 StringTableIndex = Asm.size();
1111
Rafael Espindolac3c413f2010-09-27 22:04:54 +00001112 WriteRelocations(Asm, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001113
1114 // Symbol table
1115 F = new MCDataFragment(&SymtabSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001116 MCDataFragment *ShndxF = NULL;
1117 if (NeedsSymtabShndx) {
1118 ShndxF = new MCDataFragment(SymtabShndxSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001119 }
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001120 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
Rafael Espindola71859c62010-09-16 19:46:31 +00001121
Matt Fleming3565a062010-08-16 18:57:57 +00001122 F = new MCDataFragment(&StrtabSD);
1123 F->getContents().append(StringTable.begin(), StringTable.end());
Matt Fleming3565a062010-08-16 18:57:57 +00001124
Matt Fleming3565a062010-08-16 18:57:57 +00001125 F = new MCDataFragment(&ShstrtabSD);
1126
Matt Fleming3565a062010-08-16 18:57:57 +00001127 // Section header string table.
1128 //
1129 // The first entry of a string table holds a null character so skip
1130 // section 0.
1131 uint64_t Index = 1;
1132 F->getContents() += '\x00';
1133
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001134 StringMap<uint64_t> SecStringMap;
Matt Fleming3565a062010-08-16 18:57:57 +00001135 for (MCAssembler::const_iterator it = Asm.begin(),
1136 ie = Asm.end(); it != ie; ++it) {
Matt Fleming3565a062010-08-16 18:57:57 +00001137 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +00001138 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola51efe7a2010-09-23 14:14:56 +00001139 // FIXME: We could merge suffixes like in .text and .rela.text.
Matt Fleming3565a062010-08-16 18:57:57 +00001140
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001141 StringRef Name = Section.getSectionName();
1142 if (SecStringMap.count(Name)) {
1143 SectionStringTableIndex[&Section] = SecStringMap[Name];
1144 continue;
1145 }
Matt Fleming3565a062010-08-16 18:57:57 +00001146 // Remember the index into the string table so we can write it
1147 // into the sh_name field of the section header table.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001148 SectionStringTableIndex[&Section] = Index;
1149 SecStringMap[Name] = Index;
Matt Fleming3565a062010-08-16 18:57:57 +00001150
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001151 Index += Name.size() + 1;
1152 F->getContents() += Name;
Matt Fleming3565a062010-08-16 18:57:57 +00001153 F->getContents() += '\x00';
1154 }
Rafael Espindola70703872010-09-30 02:22:20 +00001155}
1156
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001157bool ELFObjectWriter::IsFixupFullyResolved(const MCAssembler &Asm,
1158 const MCValue Target,
1159 bool IsPCRel,
1160 const MCFragment *DF) const {
Rafael Espindola70703872010-09-30 02:22:20 +00001161 // If this is a PCrel relocation, find the section this fixup value is
1162 // relative to.
1163 const MCSection *BaseSection = 0;
1164 if (IsPCRel) {
1165 BaseSection = &DF->getParent()->getSection();
1166 assert(BaseSection);
1167 }
1168
1169 const MCSection *SectionA = 0;
1170 const MCSymbol *SymbolA = 0;
1171 if (const MCSymbolRefExpr *A = Target.getSymA()) {
Rafael Espindola2c920852010-11-16 04:11:46 +00001172 SymbolA = &A->getSymbol();
1173 SectionA = &SymbolA->AliasedSymbol().getSection();
Rafael Espindola70703872010-09-30 02:22:20 +00001174 }
1175
1176 const MCSection *SectionB = 0;
Rafael Espindola12203cc2010-11-21 00:48:25 +00001177 const MCSymbol *SymbolB = 0;
Rafael Espindola70703872010-09-30 02:22:20 +00001178 if (const MCSymbolRefExpr *B = Target.getSymB()) {
Rafael Espindola12203cc2010-11-21 00:48:25 +00001179 SymbolB = &B->getSymbol();
1180 SectionB = &SymbolB->AliasedSymbol().getSection();
Rafael Espindola70703872010-09-30 02:22:20 +00001181 }
1182
1183 if (!BaseSection)
1184 return SectionA == SectionB;
1185
Rafael Espindola12203cc2010-11-21 00:48:25 +00001186 if (SymbolB)
1187 return false;
1188
1189 // Absolute address but PCrel instruction, so we need a relocation.
1190 if (!SymbolA)
1191 return false;
1192
Rafael Espindola2c920852010-11-16 04:11:46 +00001193 // FIXME: This is in here just to match gnu as output. If the two ends
1194 // are in the same section, there is nothing that the linker can do to
1195 // break it.
Rafael Espindola70703872010-09-30 02:22:20 +00001196 const MCSymbolData &DataA = Asm.getSymbolData(*SymbolA);
1197 if (DataA.isExternal())
1198 return false;
1199
Rafael Espindola12203cc2010-11-21 00:48:25 +00001200 return BaseSection == SectionA;
Matt Fleming3565a062010-08-16 18:57:57 +00001201}
1202
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001203void ELFObjectWriter::CreateGroupSections(MCAssembler &Asm,
1204 MCAsmLayout &Layout,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001205 GroupMapTy &GroupMap,
1206 RevGroupMapTy &RevGroupMap) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001207 // Build the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001208 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1209 it != ie; ++it) {
1210 const MCSectionELF &Section =
1211 static_cast<const MCSectionELF&>(it->getSection());
1212 if (!(Section.getFlags() & MCSectionELF::SHF_GROUP))
1213 continue;
1214
1215 const MCSymbol *SignatureSymbol = Section.getGroup();
1216 Asm.getOrCreateSymbolData(*SignatureSymbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001217 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001218 if (!Group) {
1219 Group = Asm.getContext().CreateELFGroupSection();
1220 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1221 Data.setAlignment(4);
1222 MCDataFragment *F = new MCDataFragment(&Data);
1223 String32(*F, ELF::GRP_COMDAT);
1224 }
1225 GroupMap[Group] = SignatureSymbol;
1226 }
1227
1228 // Add sections to the groups
1229 unsigned Index = 1;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001230 unsigned NumGroups = RevGroupMap.size();
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001231 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1232 it != ie; ++it, ++Index) {
1233 const MCSectionELF &Section =
1234 static_cast<const MCSectionELF&>(it->getSection());
1235 if (!(Section.getFlags() & MCSectionELF::SHF_GROUP))
1236 continue;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001237 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001238 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1239 // FIXME: we could use the previous fragment
1240 MCDataFragment *F = new MCDataFragment(&Data);
1241 String32(*F, NumGroups + Index);
1242 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001243}
1244
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001245void ELFObjectWriter::WriteSection(MCAssembler &Asm,
1246 const SectionIndexMapTy &SectionIndexMap,
1247 uint32_t GroupSymbolIndex,
1248 uint64_t Offset, uint64_t Size,
1249 uint64_t Alignment,
1250 const MCSectionELF &Section) {
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001251 uint64_t sh_link = 0;
1252 uint64_t sh_info = 0;
1253
1254 switch(Section.getType()) {
1255 case ELF::SHT_DYNAMIC:
1256 sh_link = SectionStringTableIndex[&Section];
1257 sh_info = 0;
1258 break;
1259
1260 case ELF::SHT_REL:
1261 case ELF::SHT_RELA: {
1262 const MCSectionELF *SymtabSection;
1263 const MCSectionELF *InfoSection;
1264 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
1265 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001266 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001267 sh_link = SectionIndexMap.lookup(SymtabSection);
1268 assert(sh_link && ".symtab not found");
1269
1270 // Remove ".rel" and ".rela" prefixes.
1271 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1272 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
1273
1274 InfoSection = Asm.getContext().getELFSection(SectionName,
1275 ELF::SHT_PROGBITS, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001276 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001277 sh_info = SectionIndexMap.lookup(InfoSection);
1278 break;
1279 }
1280
1281 case ELF::SHT_SYMTAB:
1282 case ELF::SHT_DYNSYM:
1283 sh_link = StringTableIndex;
1284 sh_info = LastLocalSymbolIndex;
1285 break;
1286
1287 case ELF::SHT_SYMTAB_SHNDX:
1288 sh_link = SymbolTableIndex;
1289 break;
1290
1291 case ELF::SHT_PROGBITS:
1292 case ELF::SHT_STRTAB:
1293 case ELF::SHT_NOBITS:
1294 case ELF::SHT_NULL:
1295 case ELF::SHT_ARM_ATTRIBUTES:
1296 // Nothing to do.
1297 break;
1298
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001299 case ELF::SHT_GROUP: {
1300 sh_link = SymbolTableIndex;
1301 sh_info = GroupSymbolIndex;
1302 break;
1303 }
1304
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001305 default:
1306 assert(0 && "FIXME: sh_type value not supported!");
1307 break;
1308 }
1309
1310 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1311 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1312 Alignment, Section.getEntrySize());
1313}
1314
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001315static bool IsELFMetaDataSection(const MCSectionData &SD) {
Rafael Espindolaf8803fe2010-12-06 03:48:09 +00001316 return SD.getOrdinal() == ~UINT32_C(0) &&
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001317 !SD.getSection().isVirtualSection();
1318}
1319
1320static uint64_t DataSectionSize(const MCSectionData &SD) {
1321 uint64_t Ret = 0;
1322 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1323 ++i) {
1324 const MCFragment &F = *i;
1325 assert(F.getKind() == MCFragment::FT_Data);
1326 Ret += cast<MCDataFragment>(F).getContents().size();
1327 }
1328 return Ret;
1329}
1330
1331static uint64_t GetSectionFileSize(const MCAsmLayout &Layout,
1332 const MCSectionData &SD) {
1333 if (IsELFMetaDataSection(SD))
1334 return DataSectionSize(SD);
1335 return Layout.getSectionFileSize(&SD);
1336}
1337
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001338static uint64_t GetSectionAddressSize(const MCAsmLayout &Layout,
1339 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001340 if (IsELFMetaDataSection(SD))
1341 return DataSectionSize(SD);
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001342 return Layout.getSectionAddressSize(&SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001343}
1344
1345static void WriteDataSectionData(ELFObjectWriter *W, const MCSectionData &SD) {
1346 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1347 ++i) {
1348 const MCFragment &F = *i;
1349 assert(F.getKind() == MCFragment::FT_Data);
1350 W->WriteBytes(cast<MCDataFragment>(F).getContents().str());
1351 }
1352}
1353
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001354void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1355 const MCAsmLayout &Layout) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001356 GroupMapTy GroupMap;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001357 RevGroupMapTy RevGroupMap;
1358 CreateGroupSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1359 RevGroupMap);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001360
Rafael Espindolabab2a802010-11-10 21:51:05 +00001361 SectionIndexMapTy SectionIndexMap;
1362
1363 ComputeIndexMap(Asm, SectionIndexMap);
1364
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001365 // Compute symbol table information.
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001366 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap);
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001367
Matt Fleming3565a062010-08-16 18:57:57 +00001368 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001369 const_cast<MCAsmLayout&>(Layout),
1370 SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +00001371
Rafael Espindola1d739a02010-11-10 22:34:07 +00001372 // Update to include the metadata sections.
1373 ComputeIndexMap(Asm, SectionIndexMap);
1374
Matt Fleming3565a062010-08-16 18:57:57 +00001375 // Add 1 for the null section.
1376 unsigned NumSections = Asm.size() + 1;
Rafael Espindolabff66a82010-12-18 03:27:34 +00001377 uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
1378 uint64_t HeaderSize = is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
1379 sizeof(ELF::Elf32_Ehdr);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001380 uint64_t FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001381
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001382 std::vector<const MCSectionELF*> Sections;
1383 Sections.resize(NumSections);
1384
1385 for (SectionIndexMapTy::const_iterator i=
1386 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1387 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
1388 Sections[p.second] = p.first;
1389 }
1390
1391 for (unsigned i = 1; i < NumSections; ++i) {
1392 const MCSectionELF &Section = *Sections[i];
1393 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001394
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001395 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1396
Matt Fleming3565a062010-08-16 18:57:57 +00001397 // Get the size of the section in the output file (including padding).
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001398 FileOff += GetSectionFileSize(Layout, SD);
Matt Fleming3565a062010-08-16 18:57:57 +00001399 }
1400
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001401 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1402
Matt Fleming3565a062010-08-16 18:57:57 +00001403 // Write out the ELF header ...
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001404 WriteHeader(FileOff - HeaderSize, NumSections);
1405
1406 FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001407
1408 // ... then all of the sections ...
1409 DenseMap<const MCSection*, uint64_t> SectionOffsetMap;
1410
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001411 for (unsigned i = 1; i < NumSections; ++i) {
1412 const MCSectionELF &Section = *Sections[i];
1413 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001414
1415 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1416 WriteZeros(Padding);
1417 FileOff += Padding;
1418
Matt Fleming3565a062010-08-16 18:57:57 +00001419 // Remember the offset into the file for this section.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001420 SectionOffsetMap[&Section] = FileOff;
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001421
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001422 FileOff += GetSectionFileSize(Layout, SD);
Matt Fleming3565a062010-08-16 18:57:57 +00001423
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001424 if (IsELFMetaDataSection(SD))
1425 WriteDataSectionData(this, SD);
1426 else
Daniel Dunbar5d2477c2010-12-17 02:45:59 +00001427 Asm.WriteSectionData(&SD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +00001428 }
1429
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001430 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1431 WriteZeros(Padding);
1432 FileOff += Padding;
1433
Matt Fleming3565a062010-08-16 18:57:57 +00001434 // ... and then the section header table.
1435 // Should we align the section header table?
1436 //
1437 // Null section first.
Rafael Espindola7be2c332010-10-31 00:16:26 +00001438 uint64_t FirstSectionSize =
1439 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1440 uint32_t FirstSectionLink =
1441 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1442 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming3565a062010-08-16 18:57:57 +00001443
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001444 for (unsigned i = 1; i < NumSections; ++i) {
1445 const MCSectionELF &Section = *Sections[i];
1446 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1447 uint32_t GroupSymbolIndex;
1448 if (Section.getType() != ELF::SHT_GROUP)
1449 GroupSymbolIndex = 0;
1450 else
1451 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm, GroupMap[&Section]);
Matt Fleming3565a062010-08-16 18:57:57 +00001452
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001453 uint64_t Size = GetSectionAddressSize(Layout, SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001454
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001455 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001456 SectionOffsetMap[&Section], Size,
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001457 SD.getAlignment(), Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001458 }
1459}
1460
Rafael Espindola6024c972010-12-17 17:45:22 +00001461MCObjectWriter *llvm::createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1462 raw_ostream &OS,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001463 bool IsLittleEndian) {
1464 switch (MOTW->getEMachine()) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001465 case ELF::EM_386:
1466 case ELF::EM_X86_64:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001467 return new X86ELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001468 case ELF::EM_ARM:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001469 return new ARMELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Wesley Peck4b047132010-11-21 22:06:28 +00001470 case ELF::EM_MBLAZE:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001471 return new MBlazeELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Benjamin Kramer32858772010-11-15 19:20:50 +00001472 default: llvm_unreachable("Unsupported architecture"); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001473 }
1474}
1475
1476
1477/// START OF SUBCLASSES for ELFObjectWriter
1478//===- ARMELFObjectWriter -------------------------------------------===//
1479
Rafael Espindola31f35782010-12-17 18:01:31 +00001480ARMELFObjectWriter::ARMELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001481 raw_ostream &_OS,
1482 bool IsLittleEndian)
1483 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
Jason W Kimd3443e92010-11-15 16:18:39 +00001484{}
1485
1486ARMELFObjectWriter::~ARMELFObjectWriter()
1487{}
1488
Jason W Kim85fed5e2010-12-01 02:40:06 +00001489unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target,
1490 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001491 bool IsPCRel,
1492 bool IsRelocWithSymbol,
1493 int64_t Addend) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001494 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1495 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
1496
Jason W Kima0871e72010-12-08 23:14:44 +00001497 unsigned Type = 0;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001498 if (IsPCRel) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001499 switch ((unsigned)Fixup.getKind()) {
1500 default: assert(0 && "Unimplemented");
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001501 case FK_Data_4:
1502 switch (Modifier) {
1503 default: llvm_unreachable("Unsupported Modifier");
1504 case MCSymbolRefExpr::VK_None:
1505 Type = ELF::R_ARM_BASE_PREL; break;
1506 case MCSymbolRefExpr::VK_ARM_TLSGD:
1507 assert(0 && "unimplemented"); break;
1508 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
1509 Type = ELF::R_ARM_TLS_IE32;
1510 } break;
1511 case ARM::fixup_arm_branch:
1512 switch (Modifier) {
1513 case MCSymbolRefExpr::VK_ARM_PLT:
1514 Type = ELF::R_ARM_PLT32; break;
1515 default:
1516 Type = ELF::R_ARM_CALL; break;
1517 } break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001518 }
1519 } else {
1520 switch ((unsigned)Fixup.getKind()) {
1521 default: llvm_unreachable("invalid fixup kind!");
Jason W Kima0871e72010-12-08 23:14:44 +00001522 case FK_Data_4:
1523 switch (Modifier) {
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001524 default: llvm_unreachable("Unsupported Modifier"); break;
1525 case MCSymbolRefExpr::VK_ARM_GOT:
1526 Type = ELF::R_ARM_GOT_BREL; break;
1527 case MCSymbolRefExpr::VK_ARM_TLSGD:
1528 Type = ELF::R_ARM_TLS_GD32; break;
Jason W Kimf13743b2010-12-16 03:12:17 +00001529 case MCSymbolRefExpr::VK_ARM_TPOFF:
1530 Type = ELF::R_ARM_TLS_LE32; break;
Jason W Kima0871e72010-12-08 23:14:44 +00001531 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001532 Type = ELF::R_ARM_TLS_IE32; break;
Jason W Kimf13743b2010-12-16 03:12:17 +00001533 case MCSymbolRefExpr::VK_None:
1534 Type = ELF::R_ARM_ABS32; break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001535 case MCSymbolRefExpr::VK_ARM_GOTOFF:
1536 Type = ELF::R_ARM_GOTOFF32; break;
Jason W Kima0871e72010-12-08 23:14:44 +00001537 } break;
Jim Grosbachdff84b02010-12-02 00:28:45 +00001538 case ARM::fixup_arm_ldst_pcrel_12:
Owen Anderson9d63d902010-12-01 19:18:46 +00001539 case ARM::fixup_arm_pcrel_10:
Jim Grosbachdff84b02010-12-02 00:28:45 +00001540 case ARM::fixup_arm_adr_pcrel_12:
Jim Grosbach662a8162010-12-06 23:57:07 +00001541 case ARM::fixup_arm_thumb_bl:
Jim Grosbachb492a7c2010-12-09 19:50:12 +00001542 case ARM::fixup_arm_thumb_cb:
Bill Wendlingb8958b02010-12-08 01:57:09 +00001543 case ARM::fixup_arm_thumb_cp:
Jim Grosbache2467172010-12-10 18:21:33 +00001544 case ARM::fixup_arm_thumb_br:
Jason W Kim85fed5e2010-12-01 02:40:06 +00001545 assert(0 && "Unimplemented"); break;
1546 case ARM::fixup_arm_branch:
Jason W Kimf13743b2010-12-16 03:12:17 +00001547 // FIXME: Differentiate between R_ARM_CALL and
1548 // R_ARM_JUMP24 (latter used for conditional jumps)
Jason W Kima0871e72010-12-08 23:14:44 +00001549 Type = ELF::R_ARM_CALL; break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001550 case ARM::fixup_arm_movt_hi16:
Jason W Kima0871e72010-12-08 23:14:44 +00001551 Type = ELF::R_ARM_MOVT_ABS; break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001552 case ARM::fixup_arm_movw_lo16:
Jason W Kima0871e72010-12-08 23:14:44 +00001553 Type = ELF::R_ARM_MOVW_ABS_NC; break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001554 }
1555 }
1556
1557 if (RelocNeedsGOT(Modifier))
1558 NeedsGOT = true;
Jason W Kima0871e72010-12-08 23:14:44 +00001559
1560 return Type;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001561}
1562
Wesley Peck4b047132010-11-21 22:06:28 +00001563//===- MBlazeELFObjectWriter -------------------------------------------===//
Jason W Kimd3443e92010-11-15 16:18:39 +00001564
Rafael Espindola31f35782010-12-17 18:01:31 +00001565MBlazeELFObjectWriter::MBlazeELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001566 raw_ostream &_OS,
1567 bool IsLittleEndian)
1568 : ELFObjectWriter(MOTW, _OS, IsLittleEndian) {
Wesley Peck4b047132010-11-21 22:06:28 +00001569}
1570
1571MBlazeELFObjectWriter::~MBlazeELFObjectWriter() {
1572}
1573
Jason W Kim56a39902010-12-06 21:57:34 +00001574unsigned MBlazeELFObjectWriter::GetRelocType(const MCValue &Target,
Wesley Peck4b047132010-11-21 22:06:28 +00001575 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001576 bool IsPCRel,
1577 bool IsRelocWithSymbol,
1578 int64_t Addend) {
Wesley Peck4b047132010-11-21 22:06:28 +00001579 // determine the type of the relocation
1580 unsigned Type;
1581 if (IsPCRel) {
1582 switch ((unsigned)Fixup.getKind()) {
1583 default:
1584 llvm_unreachable("Unimplemented");
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001585 case FK_PCRel_4:
Wesley Peck4b047132010-11-21 22:06:28 +00001586 Type = ELF::R_MICROBLAZE_64_PCREL;
1587 break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001588 case FK_PCRel_2:
Wesley Peck4b047132010-11-21 22:06:28 +00001589 Type = ELF::R_MICROBLAZE_32_PCREL;
1590 break;
1591 }
1592 } else {
1593 switch ((unsigned)Fixup.getKind()) {
1594 default: llvm_unreachable("invalid fixup kind!");
1595 case FK_Data_4:
Jason W Kim56a39902010-12-06 21:57:34 +00001596 Type = ((IsRelocWithSymbol || Addend !=0)
1597 ? ELF::R_MICROBLAZE_32
1598 : ELF::R_MICROBLAZE_64);
Wesley Peck4b047132010-11-21 22:06:28 +00001599 break;
1600 case FK_Data_2:
1601 Type = ELF::R_MICROBLAZE_32;
1602 break;
1603 }
1604 }
Jason W Kim56a39902010-12-06 21:57:34 +00001605 return Type;
Wesley Peck4b047132010-11-21 22:06:28 +00001606}
Jason W Kimd3443e92010-11-15 16:18:39 +00001607
1608//===- X86ELFObjectWriter -------------------------------------------===//
1609
1610
Rafael Espindola31f35782010-12-17 18:01:31 +00001611X86ELFObjectWriter::X86ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001612 raw_ostream &_OS,
1613 bool IsLittleEndian)
1614 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
Jason W Kimd3443e92010-11-15 16:18:39 +00001615{}
1616
1617X86ELFObjectWriter::~X86ELFObjectWriter()
1618{}
1619
Jason W Kim56a39902010-12-06 21:57:34 +00001620unsigned X86ELFObjectWriter::GetRelocType(const MCValue &Target,
1621 const MCFixup &Fixup,
1622 bool IsPCRel,
1623 bool IsRelocWithSymbol,
1624 int64_t Addend) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001625 // determine the type of the relocation
1626
Rafael Espindola12203cc2010-11-21 00:48:25 +00001627 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1628 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
Jason W Kimd3443e92010-11-15 16:18:39 +00001629 unsigned Type;
Rafael Espindolabff66a82010-12-18 03:27:34 +00001630 if (is64Bit()) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001631 if (IsPCRel) {
1632 switch (Modifier) {
1633 default:
1634 llvm_unreachable("Unimplemented");
1635 case MCSymbolRefExpr::VK_None:
1636 Type = ELF::R_X86_64_PC32;
1637 break;
1638 case MCSymbolRefExpr::VK_PLT:
1639 Type = ELF::R_X86_64_PLT32;
1640 break;
1641 case MCSymbolRefExpr::VK_GOTPCREL:
1642 Type = ELF::R_X86_64_GOTPCREL;
1643 break;
1644 case MCSymbolRefExpr::VK_GOTTPOFF:
1645 Type = ELF::R_X86_64_GOTTPOFF;
1646 break;
1647 case MCSymbolRefExpr::VK_TLSGD:
1648 Type = ELF::R_X86_64_TLSGD;
1649 break;
1650 case MCSymbolRefExpr::VK_TLSLD:
1651 Type = ELF::R_X86_64_TLSLD;
1652 break;
1653 }
1654 } else {
1655 switch ((unsigned)Fixup.getKind()) {
1656 default: llvm_unreachable("invalid fixup kind!");
1657 case FK_Data_8: Type = ELF::R_X86_64_64; break;
1658 case X86::reloc_signed_4byte:
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001659 case FK_PCRel_4:
Jason W Kimd3443e92010-11-15 16:18:39 +00001660 assert(isInt<32>(Target.getConstant()));
1661 switch (Modifier) {
1662 default:
1663 llvm_unreachable("Unimplemented");
1664 case MCSymbolRefExpr::VK_None:
1665 Type = ELF::R_X86_64_32S;
1666 break;
1667 case MCSymbolRefExpr::VK_GOT:
1668 Type = ELF::R_X86_64_GOT32;
1669 break;
1670 case MCSymbolRefExpr::VK_GOTPCREL:
1671 Type = ELF::R_X86_64_GOTPCREL;
1672 break;
1673 case MCSymbolRefExpr::VK_TPOFF:
1674 Type = ELF::R_X86_64_TPOFF32;
1675 break;
1676 case MCSymbolRefExpr::VK_DTPOFF:
1677 Type = ELF::R_X86_64_DTPOFF32;
1678 break;
1679 }
1680 break;
1681 case FK_Data_4:
1682 Type = ELF::R_X86_64_32;
1683 break;
1684 case FK_Data_2: Type = ELF::R_X86_64_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001685 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001686 case FK_Data_1: Type = ELF::R_X86_64_8; break;
1687 }
1688 }
1689 } else {
1690 if (IsPCRel) {
1691 switch (Modifier) {
1692 default:
1693 llvm_unreachable("Unimplemented");
1694 case MCSymbolRefExpr::VK_None:
1695 Type = ELF::R_386_PC32;
1696 break;
1697 case MCSymbolRefExpr::VK_PLT:
1698 Type = ELF::R_386_PLT32;
1699 break;
1700 }
1701 } else {
1702 switch ((unsigned)Fixup.getKind()) {
1703 default: llvm_unreachable("invalid fixup kind!");
1704
1705 case X86::reloc_global_offset_table:
1706 Type = ELF::R_386_GOTPC;
1707 break;
1708
1709 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
1710 // instead?
1711 case X86::reloc_signed_4byte:
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001712 case FK_PCRel_4:
Jason W Kimd3443e92010-11-15 16:18:39 +00001713 case FK_Data_4:
1714 switch (Modifier) {
1715 default:
1716 llvm_unreachable("Unimplemented");
1717 case MCSymbolRefExpr::VK_None:
1718 Type = ELF::R_386_32;
1719 break;
1720 case MCSymbolRefExpr::VK_GOT:
1721 Type = ELF::R_386_GOT32;
1722 break;
1723 case MCSymbolRefExpr::VK_GOTOFF:
1724 Type = ELF::R_386_GOTOFF;
1725 break;
1726 case MCSymbolRefExpr::VK_TLSGD:
1727 Type = ELF::R_386_TLS_GD;
1728 break;
1729 case MCSymbolRefExpr::VK_TPOFF:
1730 Type = ELF::R_386_TLS_LE_32;
1731 break;
1732 case MCSymbolRefExpr::VK_INDNTPOFF:
1733 Type = ELF::R_386_TLS_IE;
1734 break;
1735 case MCSymbolRefExpr::VK_NTPOFF:
1736 Type = ELF::R_386_TLS_LE;
1737 break;
1738 case MCSymbolRefExpr::VK_GOTNTPOFF:
1739 Type = ELF::R_386_TLS_GOTIE;
1740 break;
1741 case MCSymbolRefExpr::VK_TLSLDM:
1742 Type = ELF::R_386_TLS_LDM;
1743 break;
1744 case MCSymbolRefExpr::VK_DTPOFF:
1745 Type = ELF::R_386_TLS_LDO_32;
1746 break;
1747 }
1748 break;
1749 case FK_Data_2: Type = ELF::R_386_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001750 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001751 case FK_Data_1: Type = ELF::R_386_8; break;
1752 }
1753 }
1754 }
1755
1756 if (RelocNeedsGOT(Modifier))
1757 NeedsGOT = true;
1758
Jason W Kim56a39902010-12-06 21:57:34 +00001759 return Type;
Matt Fleming3565a062010-08-16 18:57:57 +00001760}