blob: c9e8074c4131da385daa1cefba3da8aa27395bfc [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 Espindola8f413fa2010-10-05 15:11:03 +000014#include "llvm/ADT/SmallPtrSet.h"
Matt Fleming3565a062010-08-16 18:57:57 +000015#include "llvm/ADT/STLExtras.h"
16#include "llvm/ADT/StringMap.h"
17#include "llvm/ADT/Twine.h"
18#include "llvm/MC/MCAssembler.h"
19#include "llvm/MC/MCAsmLayout.h"
20#include "llvm/MC/MCContext.h"
21#include "llvm/MC/MCELFSymbolFlags.h"
22#include "llvm/MC/MCExpr.h"
23#include "llvm/MC/MCObjectWriter.h"
24#include "llvm/MC/MCSectionELF.h"
25#include "llvm/MC/MCSymbol.h"
26#include "llvm/MC/MCValue.h"
27#include "llvm/Support/Debug.h"
28#include "llvm/Support/ErrorHandling.h"
29#include "llvm/Support/ELF.h"
30#include "llvm/Target/TargetAsmBackend.h"
31
32#include "../Target/X86/X86FixupKinds.h"
Jason W Kim4a511f02010-11-22 18:41:13 +000033#include "../Target/ARM/ARMFixupKinds.h"
Matt Fleming3565a062010-08-16 18:57:57 +000034
35#include <vector>
36using namespace llvm;
37
Rafael Espindolaad49cf52010-09-18 15:03:21 +000038static unsigned GetType(const MCSymbolData &SD) {
39 uint32_t Type = (SD.getFlags() & (0xf << ELF_STT_Shift)) >> ELF_STT_Shift;
40 assert(Type == ELF::STT_NOTYPE || Type == ELF::STT_OBJECT ||
41 Type == ELF::STT_FUNC || Type == ELF::STT_SECTION ||
42 Type == ELF::STT_FILE || Type == ELF::STT_COMMON ||
43 Type == ELF::STT_TLS);
44 return Type;
45}
46
Rafael Espindolae15eb4e2010-09-23 19:55:14 +000047static unsigned GetBinding(const MCSymbolData &SD) {
48 uint32_t Binding = (SD.getFlags() & (0xf << ELF_STB_Shift)) >> ELF_STB_Shift;
49 assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
50 Binding == ELF::STB_WEAK);
51 return Binding;
52}
53
54static void SetBinding(MCSymbolData &SD, unsigned Binding) {
55 assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
56 Binding == ELF::STB_WEAK);
57 uint32_t OtherFlags = SD.getFlags() & ~(0xf << ELF_STB_Shift);
58 SD.setFlags(OtherFlags | (Binding << ELF_STB_Shift));
59}
60
Rafael Espindola152c1062010-10-06 21:02:29 +000061static unsigned GetVisibility(MCSymbolData &SD) {
62 unsigned Visibility =
63 (SD.getFlags() & (0xf << ELF_STV_Shift)) >> ELF_STV_Shift;
64 assert(Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_INTERNAL ||
65 Visibility == ELF::STV_HIDDEN || Visibility == ELF::STV_PROTECTED);
66 return Visibility;
67}
68
Wesley Peck4b047132010-11-21 22:06:28 +000069
Rafael Espindolaa0a2f872010-10-28 14:22:44 +000070static bool RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant) {
71 switch (Variant) {
Rafael Espindola5c77c162010-10-05 15:48:37 +000072 default:
73 return false;
Rafael Espindolaa0a2f872010-10-28 14:22:44 +000074 case MCSymbolRefExpr::VK_GOT:
75 case MCSymbolRefExpr::VK_PLT:
76 case MCSymbolRefExpr::VK_GOTPCREL:
77 case MCSymbolRefExpr::VK_TPOFF:
78 case MCSymbolRefExpr::VK_TLSGD:
79 case MCSymbolRefExpr::VK_GOTTPOFF:
80 case MCSymbolRefExpr::VK_INDNTPOFF:
81 case MCSymbolRefExpr::VK_NTPOFF:
82 case MCSymbolRefExpr::VK_GOTNTPOFF:
Rafael Espindolaa264f722010-10-28 14:37:09 +000083 case MCSymbolRefExpr::VK_TLSLDM:
Rafael Espindola0cf15d62010-10-28 14:48:59 +000084 case MCSymbolRefExpr::VK_DTPOFF:
Rafael Espindolab4d17212010-10-28 15:02:40 +000085 case MCSymbolRefExpr::VK_TLSLD:
Rafael Espindola5c77c162010-10-05 15:48:37 +000086 return true;
87 }
88}
89
Matt Fleming3565a062010-08-16 18:57:57 +000090namespace {
Daniel Dunbar115a3dd2010-11-13 07:33:40 +000091 class ELFObjectWriter : public MCObjectWriter {
Jason W Kimd3443e92010-11-15 16:18:39 +000092 protected:
Chris Lattnerb188a372010-08-28 03:21:03 +000093 /*static bool isFixupKindX86RIPRel(unsigned Kind) {
Matt Fleming3565a062010-08-16 18:57:57 +000094 return Kind == X86::reloc_riprel_4byte ||
95 Kind == X86::reloc_riprel_4byte_movq_load;
Chris Lattnerb188a372010-08-28 03:21:03 +000096 }*/
Matt Fleming3565a062010-08-16 18:57:57 +000097
98
99 /// ELFSymbolData - Helper struct for containing some precomputed information
100 /// on symbols.
101 struct ELFSymbolData {
102 MCSymbolData *SymbolData;
103 uint64_t StringIndex;
104 uint32_t SectionIndex;
105
106 // Support lexicographic sorting.
107 bool operator<(const ELFSymbolData &RHS) const {
Rafael Espindolaad49cf52010-09-18 15:03:21 +0000108 if (GetType(*SymbolData) == ELF::STT_FILE)
109 return true;
110 if (GetType(*RHS.SymbolData) == ELF::STT_FILE)
111 return false;
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000112 return SymbolData->getSymbol().getName() <
113 RHS.SymbolData->getSymbol().getName();
Matt Fleming3565a062010-08-16 18:57:57 +0000114 }
115 };
116
117 /// @name Relocation Data
118 /// @{
119
120 struct ELFRelocationEntry {
121 // Make these big enough for both 32-bit and 64-bit
122 uint64_t r_offset;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000123 int Index;
124 unsigned Type;
125 const MCSymbol *Symbol;
Matt Fleming3565a062010-08-16 18:57:57 +0000126 uint64_t r_addend;
Jason W Kim858e7502010-11-22 18:42:07 +0000127
Jason W Kim4a511f02010-11-22 18:41:13 +0000128 ELFRelocationEntry()
129 : r_offset(0), Index(0), Type(0), Symbol(0), r_addend(0) {}
130
Jason W Kim10907422010-11-22 22:05:16 +0000131 ELFRelocationEntry(uint64_t RelocOffset, int Idx,
132 unsigned RelType, const MCSymbol *Sym,
Jason W Kim4a511f02010-11-22 18:41:13 +0000133 uint64_t Addend)
Jason W Kim10907422010-11-22 22:05:16 +0000134 : r_offset(RelocOffset), Index(Idx), Type(RelType),
135 Symbol(Sym), r_addend(Addend) {}
Matt Fleming3565a062010-08-16 18:57:57 +0000136
137 // Support lexicographic sorting.
138 bool operator<(const ELFRelocationEntry &RE) const {
139 return RE.r_offset < r_offset;
140 }
141 };
142
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000143 SmallPtrSet<const MCSymbol *, 16> UsedInReloc;
Rafael Espindola484291c2010-11-01 14:28:48 +0000144 SmallPtrSet<const MCSymbol *, 16> WeakrefUsedInReloc;
Rafael Espindola88182132010-10-27 15:18:17 +0000145 DenseMap<const MCSymbol *, const MCSymbol *> Renames;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000146
Matt Fleming3565a062010-08-16 18:57:57 +0000147 llvm::DenseMap<const MCSectionData*,
148 std::vector<ELFRelocationEntry> > Relocations;
149 DenseMap<const MCSection*, uint64_t> SectionStringTableIndex;
150
151 /// @}
152 /// @name Symbol Table Data
153 /// @{
154
155 SmallString<256> StringTable;
156 std::vector<ELFSymbolData> LocalSymbolData;
157 std::vector<ELFSymbolData> ExternalSymbolData;
158 std::vector<ELFSymbolData> UndefinedSymbolData;
159
160 /// @}
161
Rafael Espindola5c77c162010-10-05 15:48:37 +0000162 bool NeedsGOT;
163
Rafael Espindola7be2c332010-10-31 00:16:26 +0000164 bool NeedsSymtabShndx;
165
Matt Fleming3565a062010-08-16 18:57:57 +0000166 unsigned Is64Bit : 1;
167
168 bool HasRelocationAddend;
169
Roman Divacky5baf79e2010-09-09 17:57:50 +0000170 Triple::OSType OSType;
171
Wesley Peckeecb8582010-10-22 15:52:49 +0000172 uint16_t EMachine;
173
Matt Fleming3565a062010-08-16 18:57:57 +0000174 // This holds the symbol table index of the last local symbol.
175 unsigned LastLocalSymbolIndex;
176 // This holds the .strtab section index.
177 unsigned StringTableIndex;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000178 // This holds the .symtab section index.
179 unsigned SymbolTableIndex;
Matt Fleming3565a062010-08-16 18:57:57 +0000180
181 unsigned ShstrtabIndex;
182
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000183
184 const MCSymbol *SymbolToReloc(const MCAssembler &Asm,
185 const MCValue &Target,
186 const MCFragment &F) const;
187
Matt Fleming3565a062010-08-16 18:57:57 +0000188 public:
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000189 ELFObjectWriter(raw_ostream &_OS, bool _Is64Bit, bool IsLittleEndian,
190 uint16_t _EMachine, bool _HasRelAddend,
191 Triple::OSType _OSType)
192 : MCObjectWriter(_OS, IsLittleEndian),
193 NeedsGOT(false), NeedsSymtabShndx(false),
Roman Divacky5baf79e2010-09-09 17:57:50 +0000194 Is64Bit(_Is64Bit), HasRelocationAddend(_HasRelAddend),
Wesley Peckeecb8582010-10-22 15:52:49 +0000195 OSType(_OSType), EMachine(_EMachine) {
Matt Fleming3565a062010-08-16 18:57:57 +0000196 }
Jason W Kimd3443e92010-11-15 16:18:39 +0000197
198 virtual ~ELFObjectWriter();
199
Matt Fleming3565a062010-08-16 18:57:57 +0000200 void WriteWord(uint64_t W) {
Chris Lattnerb188a372010-08-28 03:21:03 +0000201 if (Is64Bit)
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000202 Write64(W);
Chris Lattnerb188a372010-08-28 03:21:03 +0000203 else
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000204 Write32(W);
Matt Fleming3565a062010-08-16 18:57:57 +0000205 }
206
Matt Fleming3565a062010-08-16 18:57:57 +0000207 void StringLE16(char *buf, uint16_t Value) {
208 buf[0] = char(Value >> 0);
209 buf[1] = char(Value >> 8);
210 }
211
212 void StringLE32(char *buf, uint32_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000213 StringLE16(buf, uint16_t(Value >> 0));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000214 StringLE16(buf + 2, uint16_t(Value >> 16));
Matt Fleming3565a062010-08-16 18:57:57 +0000215 }
216
217 void StringLE64(char *buf, uint64_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000218 StringLE32(buf, uint32_t(Value >> 0));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000219 StringLE32(buf + 4, uint32_t(Value >> 32));
Matt Fleming3565a062010-08-16 18:57:57 +0000220 }
221
222 void StringBE16(char *buf ,uint16_t Value) {
223 buf[0] = char(Value >> 8);
224 buf[1] = char(Value >> 0);
225 }
226
227 void StringBE32(char *buf, uint32_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000228 StringBE16(buf, uint16_t(Value >> 16));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000229 StringBE16(buf + 2, uint16_t(Value >> 0));
Matt Fleming3565a062010-08-16 18:57:57 +0000230 }
231
232 void StringBE64(char *buf, uint64_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000233 StringBE32(buf, uint32_t(Value >> 32));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000234 StringBE32(buf + 4, uint32_t(Value >> 0));
Matt Fleming3565a062010-08-16 18:57:57 +0000235 }
236
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000237 void String8(MCDataFragment &F, uint8_t Value) {
238 char buf[1];
239 buf[0] = Value;
240 F.getContents() += StringRef(buf, 1);
241 }
242
243 void String16(MCDataFragment &F, uint16_t Value) {
244 char buf[2];
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000245 if (isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000246 StringLE16(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000247 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000248 StringBE16(buf, Value);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000249 F.getContents() += StringRef(buf, 2);
Matt Fleming3565a062010-08-16 18:57:57 +0000250 }
251
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000252 void String32(MCDataFragment &F, uint32_t Value) {
253 char buf[4];
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000254 if (isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000255 StringLE32(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000256 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000257 StringBE32(buf, Value);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000258 F.getContents() += StringRef(buf, 4);
Matt Fleming3565a062010-08-16 18:57:57 +0000259 }
260
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000261 void String64(MCDataFragment &F, uint64_t Value) {
262 char buf[8];
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000263 if (isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000264 StringLE64(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000265 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000266 StringBE64(buf, Value);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000267 F.getContents() += StringRef(buf, 8);
Matt Fleming3565a062010-08-16 18:57:57 +0000268 }
269
Jason W Kimd3443e92010-11-15 16:18:39 +0000270 virtual void WriteHeader(uint64_t SectionDataSize, unsigned NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000271
Jason W Kimd3443e92010-11-15 16:18:39 +0000272 virtual void WriteSymbolEntry(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
Rafael Espindola7be2c332010-10-31 00:16:26 +0000273 uint64_t name, uint8_t info,
Matt Fleming3565a062010-08-16 18:57:57 +0000274 uint64_t value, uint64_t size,
Rafael Espindola7be2c332010-10-31 00:16:26 +0000275 uint8_t other, uint32_t shndx,
276 bool Reserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000277
Jason W Kimd3443e92010-11-15 16:18:39 +0000278 virtual void WriteSymbol(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
Rafael Espindola7be2c332010-10-31 00:16:26 +0000279 ELFSymbolData &MSD,
Matt Fleming3565a062010-08-16 18:57:57 +0000280 const MCAsmLayout &Layout);
281
Rafael Espindolabab2a802010-11-10 21:51:05 +0000282 typedef DenseMap<const MCSectionELF*, uint32_t> SectionIndexMapTy;
Jason W Kimd3443e92010-11-15 16:18:39 +0000283 virtual void WriteSymbolTable(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
Rafael Espindola7be2c332010-10-31 00:16:26 +0000284 const MCAssembler &Asm,
Rafael Espindola71859c62010-09-16 19:46:31 +0000285 const MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000286 const SectionIndexMapTy &SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +0000287
Jason W Kimd3443e92010-11-15 16:18:39 +0000288 virtual void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
Jason W Kim56a39902010-12-06 21:57:34 +0000289 const MCFragment *Fragment, const MCFixup &Fixup,
290 MCValue Target, uint64_t &FixedValue);
Matt Fleming3565a062010-08-16 18:57:57 +0000291
Jason W Kimd3443e92010-11-15 16:18:39 +0000292 virtual uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm,
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000293 const MCSymbol *S);
Matt Fleming3565a062010-08-16 18:57:57 +0000294
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000295 // Map from a group section to the signature symbol
296 typedef DenseMap<const MCSectionELF*, const MCSymbol*> GroupMapTy;
297 // Map from a signature symbol to the group section
298 typedef DenseMap<const MCSymbol*, const MCSectionELF*> RevGroupMapTy;
299
Matt Fleming3565a062010-08-16 18:57:57 +0000300 /// ComputeSymbolTable - Compute the symbol table data
301 ///
302 /// \param StringTable [out] - The string table data.
303 /// \param StringIndexMap [out] - Map from symbol names to offsets in the
304 /// string table.
Jason W Kimd3443e92010-11-15 16:18:39 +0000305 virtual void ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000306 const SectionIndexMapTy &SectionIndexMap,
307 RevGroupMapTy RevGroupMap);
Rafael Espindolabab2a802010-11-10 21:51:05 +0000308
Jason W Kimd3443e92010-11-15 16:18:39 +0000309 virtual void ComputeIndexMap(MCAssembler &Asm,
Rafael Espindolabab2a802010-11-10 21:51:05 +0000310 SectionIndexMapTy &SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +0000311
Jason W Kimd3443e92010-11-15 16:18:39 +0000312 virtual void WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
Matt Fleming3565a062010-08-16 18:57:57 +0000313 const MCSectionData &SD);
314
Jason W Kimd3443e92010-11-15 16:18:39 +0000315 virtual void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout) {
Matt Fleming3565a062010-08-16 18:57:57 +0000316 for (MCAssembler::const_iterator it = Asm.begin(),
317 ie = Asm.end(); it != ie; ++it) {
318 WriteRelocation(Asm, Layout, *it);
319 }
320 }
321
Jason W Kimd3443e92010-11-15 16:18:39 +0000322 virtual void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000323 const SectionIndexMapTy &SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +0000324
Jason W Kimd3443e92010-11-15 16:18:39 +0000325 virtual void CreateGroupSections(MCAssembler &Asm, MCAsmLayout &Layout,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000326 GroupMapTy &GroupMap, RevGroupMapTy &RevGroupMap);
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000327
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000328 virtual void ExecutePostLayoutBinding(MCAssembler &Asm,
329 const MCAsmLayout &Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000330
Jason W Kimd3443e92010-11-15 16:18:39 +0000331 virtual void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
Matt Fleming3565a062010-08-16 18:57:57 +0000332 uint64_t Address, uint64_t Offset,
333 uint64_t Size, uint32_t Link, uint32_t Info,
334 uint64_t Alignment, uint64_t EntrySize);
335
Jason W Kimd3443e92010-11-15 16:18:39 +0000336 virtual void WriteRelocationsFragment(const MCAssembler &Asm, MCDataFragment *F,
Matt Fleming3565a062010-08-16 18:57:57 +0000337 const MCSectionData *SD);
338
Jason W Kimd3443e92010-11-15 16:18:39 +0000339 virtual bool IsFixupFullyResolved(const MCAssembler &Asm,
Rafael Espindola70703872010-09-30 02:22:20 +0000340 const MCValue Target,
341 bool IsPCRel,
342 const MCFragment *DF) const;
343
Jason W Kimd3443e92010-11-15 16:18:39 +0000344 virtual void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
345 virtual void WriteSection(MCAssembler &Asm,
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000346 const SectionIndexMapTy &SectionIndexMap,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000347 uint32_t GroupSymbolIndex,
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000348 uint64_t Offset, uint64_t Size, uint64_t Alignment,
349 const MCSectionELF &Section);
Jason W Kim56a39902010-12-06 21:57:34 +0000350
351 protected:
352 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
353 bool IsPCRel, bool IsRelocWithSymbol,
354 int64_t Addend) = 0;
355
356 virtual bool isFixupKindPCRel(unsigned Kind) const = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000357 };
358
Jason W Kimd3443e92010-11-15 16:18:39 +0000359 //===- X86ELFObjectWriter -------------------------------------------===//
360
361 class X86ELFObjectWriter : public ELFObjectWriter {
362 public:
363 X86ELFObjectWriter(raw_ostream &_OS, bool _Is64Bit, bool IsLittleEndian,
364 uint16_t _EMachine, bool _HasRelAddend,
365 Triple::OSType _OSType);
Wesley Peck4b047132010-11-21 22:06:28 +0000366
Jason W Kimd3443e92010-11-15 16:18:39 +0000367 virtual ~X86ELFObjectWriter();
Jason W Kim56a39902010-12-06 21:57:34 +0000368 protected:
369 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
370 bool IsPCRel, bool IsRelocWithSymbol,
371 int64_t Addend);
Jason W Kim4a511f02010-11-22 18:41:13 +0000372
Jason W Kim56a39902010-12-06 21:57:34 +0000373 virtual bool isFixupKindPCRel(unsigned Kind) const {
Jason W Kim4a511f02010-11-22 18:41:13 +0000374 switch (Kind) {
375 default:
376 return false;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +0000377 case FK_PCRel_1:
378 case FK_PCRel_2:
379 case FK_PCRel_4:
Jason W Kim4a511f02010-11-22 18:41:13 +0000380 case X86::reloc_riprel_4byte:
381 case X86::reloc_riprel_4byte_movq_load:
382 return true;
383 }
384 }
Jason W Kimd3443e92010-11-15 16:18:39 +0000385 };
386
387
388 //===- ARMELFObjectWriter -------------------------------------------===//
389
390 class ARMELFObjectWriter : public ELFObjectWriter {
391 public:
392 ARMELFObjectWriter(raw_ostream &_OS, bool _Is64Bit, bool IsLittleEndian,
393 uint16_t _EMachine, bool _HasRelAddend,
394 Triple::OSType _OSType);
Wesley Peck4b047132010-11-21 22:06:28 +0000395
Jason W Kimd3443e92010-11-15 16:18:39 +0000396 virtual ~ARMELFObjectWriter();
Jason W Kim85fed5e2010-12-01 02:40:06 +0000397 protected:
Jason W Kim56a39902010-12-06 21:57:34 +0000398 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
399 bool IsPCRel, bool IsRelocWithSymbol,
400 int64_t Addend);
401 virtual bool isFixupKindPCRel(unsigned Kind) const {
Jason W Kim4a511f02010-11-22 18:41:13 +0000402 switch (Kind) {
403 default:
404 return false;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +0000405 case FK_PCRel_1:
406 case FK_PCRel_2:
407 case FK_PCRel_4:
Jim Grosbachdff84b02010-12-02 00:28:45 +0000408 case ARM::fixup_arm_ldst_pcrel_12:
Owen Anderson9d63d902010-12-01 19:18:46 +0000409 case ARM::fixup_arm_pcrel_10:
Jason W Kimccbe0002010-11-22 18:47:05 +0000410 case ARM::fixup_arm_branch:
Jason W Kim4a511f02010-11-22 18:41:13 +0000411 return true;
412 }
413 }
Jason W Kimd3443e92010-11-15 16:18:39 +0000414 };
Wesley Peck4b047132010-11-21 22:06:28 +0000415
416 //===- MBlazeELFObjectWriter -------------------------------------------===//
417
418 class MBlazeELFObjectWriter : public ELFObjectWriter {
419 public:
420 MBlazeELFObjectWriter(raw_ostream &_OS, bool _Is64Bit, bool IsLittleEndian,
421 uint16_t _EMachine, bool _HasRelAddend,
422 Triple::OSType _OSType);
423
424 virtual ~MBlazeELFObjectWriter();
Jason W Kim56a39902010-12-06 21:57:34 +0000425 protected:
426 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
427 bool IsPCRel, bool IsRelocWithSymbol,
428 int64_t Addend);
Jason W Kim4a511f02010-11-22 18:41:13 +0000429
Jason W Kim56a39902010-12-06 21:57:34 +0000430 virtual bool isFixupKindPCRel(unsigned Kind) const {
Jason W Kim4a511f02010-11-22 18:41:13 +0000431 switch (Kind) {
432 default:
433 return false;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +0000434 case FK_PCRel_1:
435 case FK_PCRel_2:
436 case FK_PCRel_4:
Jason W Kim4a511f02010-11-22 18:41:13 +0000437 return true;
438 }
439 }
Wesley Peck4b047132010-11-21 22:06:28 +0000440 };
Matt Fleming3565a062010-08-16 18:57:57 +0000441}
442
Jason W Kimd3443e92010-11-15 16:18:39 +0000443ELFObjectWriter::~ELFObjectWriter()
444{}
445
Matt Fleming3565a062010-08-16 18:57:57 +0000446// Emit the ELF header.
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000447void ELFObjectWriter::WriteHeader(uint64_t SectionDataSize,
448 unsigned NumberOfSections) {
Matt Fleming3565a062010-08-16 18:57:57 +0000449 // ELF Header
450 // ----------
451 //
452 // Note
453 // ----
454 // emitWord method behaves differently for ELF32 and ELF64, writing
455 // 4 bytes in the former and 8 in the latter.
456
457 Write8(0x7f); // e_ident[EI_MAG0]
458 Write8('E'); // e_ident[EI_MAG1]
459 Write8('L'); // e_ident[EI_MAG2]
460 Write8('F'); // e_ident[EI_MAG3]
461
462 Write8(Is64Bit ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
463
464 // e_ident[EI_DATA]
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000465 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
Matt Fleming3565a062010-08-16 18:57:57 +0000466
467 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky5baf79e2010-09-09 17:57:50 +0000468 // e_ident[EI_OSABI]
469 switch (OSType) {
470 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
471 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
472 default: Write8(ELF::ELFOSABI_NONE); break;
473 }
Matt Fleming3565a062010-08-16 18:57:57 +0000474 Write8(0); // e_ident[EI_ABIVERSION]
475
476 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
477
478 Write16(ELF::ET_REL); // e_type
479
Wesley Peckeecb8582010-10-22 15:52:49 +0000480 Write16(EMachine); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +0000481
482 Write32(ELF::EV_CURRENT); // e_version
483 WriteWord(0); // e_entry, no entry point in .o file
484 WriteWord(0); // e_phoff, no program header for .o
Benjamin Kramereb976772010-08-17 17:02:29 +0000485 WriteWord(SectionDataSize + (Is64Bit ? sizeof(ELF::Elf64_Ehdr) :
486 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000487
488 // FIXME: Make this configurable.
489 Write32(0); // e_flags = whatever the target wants
490
491 // e_ehsize = ELF header size
492 Write16(Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
493
494 Write16(0); // e_phentsize = prog header entry size
495 Write16(0); // e_phnum = # prog header entries = 0
496
497 // e_shentsize = Section header entry size
498 Write16(Is64Bit ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
499
500 // e_shnum = # of section header ents
Rafael Espindola7be2c332010-10-31 00:16:26 +0000501 if (NumberOfSections >= ELF::SHN_LORESERVE)
502 Write16(0);
503 else
504 Write16(NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000505
506 // e_shstrndx = Section # of '.shstrtab'
Rafael Espindola7be2c332010-10-31 00:16:26 +0000507 if (NumberOfSections >= ELF::SHN_LORESERVE)
508 Write16(ELF::SHN_XINDEX);
509 else
510 Write16(ShstrtabIndex);
Matt Fleming3565a062010-08-16 18:57:57 +0000511}
512
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000513void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
514 MCDataFragment *ShndxF,
515 uint64_t name,
516 uint8_t info, uint64_t value,
517 uint64_t size, uint8_t other,
518 uint32_t shndx,
519 bool Reserved) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000520 if (ShndxF) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000521 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000522 String32(*ShndxF, shndx);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000523 else
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000524 String32(*ShndxF, 0);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000525 }
526
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000527 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
528 uint16_t(ELF::SHN_XINDEX) : shndx;
529
Matt Fleming3565a062010-08-16 18:57:57 +0000530 if (Is64Bit) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000531 String32(*SymtabF, name); // st_name
532 String8(*SymtabF, info); // st_info
533 String8(*SymtabF, other); // st_other
534 String16(*SymtabF, Index); // st_shndx
535 String64(*SymtabF, value); // st_value
536 String64(*SymtabF, size); // st_size
Matt Fleming3565a062010-08-16 18:57:57 +0000537 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000538 String32(*SymtabF, name); // st_name
539 String32(*SymtabF, value); // st_value
540 String32(*SymtabF, size); // st_size
541 String8(*SymtabF, info); // st_info
542 String8(*SymtabF, other); // st_other
543 String16(*SymtabF, Index); // st_shndx
Matt Fleming3565a062010-08-16 18:57:57 +0000544 }
545}
546
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000547static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout) {
548 if (Data.isCommon() && Data.isExternal())
549 return Data.getCommonAlignment();
550
551 const MCSymbol &Symbol = Data.getSymbol();
552 if (!Symbol.isInSection())
553 return 0;
554
Rafael Espindolaffd902b2010-12-06 02:57:26 +0000555 if (Data.getFragment())
556 return Layout.getSymbolOffset(&Data);
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000557
558 return 0;
559}
560
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000561void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
562 const MCAsmLayout &Layout) {
Rafael Espindola88182132010-10-27 15:18:17 +0000563 // The presence of symbol versions causes undefined symbols and
564 // versions declared with @@@ to be renamed.
565
566 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
567 ie = Asm.symbol_end(); it != ie; ++it) {
568 const MCSymbol &Alias = it->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000569 const MCSymbol &Symbol = Alias.AliasedSymbol();
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000570 MCSymbolData &SD = Asm.getSymbolData(Symbol);
571
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000572 // Not an alias.
573 if (&Symbol == &Alias)
574 continue;
575
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000576 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000577 size_t Pos = AliasName.find('@');
578 if (Pos == StringRef::npos)
579 continue;
580
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000581 // Aliases defined with .symvar copy the binding from the symbol they alias.
582 // This is the first place we are able to copy this information.
583 it->setExternal(SD.isExternal());
584 SetBinding(*it, GetBinding(SD));
585
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000586 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000587 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
588 continue;
589
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000590 // FIXME: produce a better error message.
591 if (Symbol.isUndefined() && Rest.startswith("@@") &&
592 !Rest.startswith("@@@"))
593 report_fatal_error("A @@ version cannot be undefined");
594
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000595 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000596 }
597}
598
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000599void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
600 MCDataFragment *ShndxF,
601 ELFSymbolData &MSD,
602 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000603 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000604 MCSymbolData &Data =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000605 Layout.getAssembler().getSymbolData(OrigData.getSymbol().AliasedSymbol());
Rafael Espindola152c1062010-10-06 21:02:29 +0000606
Rafael Espindola7be2c332010-10-31 00:16:26 +0000607 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
608 Data.getSymbol().isVariable();
609
Rafael Espindola152c1062010-10-06 21:02:29 +0000610 uint8_t Binding = GetBinding(OrigData);
611 uint8_t Visibility = GetVisibility(OrigData);
612 uint8_t Type = GetType(Data);
613
614 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
615 uint8_t Other = Visibility;
616
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000617 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000618 uint64_t Size = 0;
619 const MCExpr *ESize;
620
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000621 assert(!(Data.isCommon() && !Data.isExternal()));
622
Matt Fleming3565a062010-08-16 18:57:57 +0000623 ESize = Data.getSize();
624 if (Data.getSize()) {
625 MCValue Res;
626 if (ESize->getKind() == MCExpr::Binary) {
627 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(ESize);
628
629 if (BE->EvaluateAsRelocatable(Res, &Layout)) {
Benjamin Kramer24f12062010-10-17 07:38:40 +0000630 assert(!Res.getSymA() || !Res.getSymA()->getSymbol().isDefined());
631 assert(!Res.getSymB() || !Res.getSymB()->getSymbol().isDefined());
Rafael Espindolaf230df92010-10-16 18:23:53 +0000632 Size = Res.getConstant();
Matt Fleming3565a062010-08-16 18:57:57 +0000633 }
634 } else if (ESize->getKind() == MCExpr::Constant) {
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000635 Size = static_cast<const MCConstantExpr *>(ESize)->getValue();
Matt Fleming3565a062010-08-16 18:57:57 +0000636 } else {
637 assert(0 && "Unsupported size expression");
638 }
639 }
640
641 // Write out the symbol table entry
Rafael Espindola7be2c332010-10-31 00:16:26 +0000642 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
643 Size, Other, MSD.SectionIndex, IsReserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000644}
645
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000646void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
647 MCDataFragment *ShndxF,
648 const MCAssembler &Asm,
649 const MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000650 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000651 // The string table must be emitted first because we need the index
652 // into the string table for all the symbol names.
653 assert(StringTable.size() && "Missing string table");
654
655 // FIXME: Make sure the start of the symbol table is aligned.
656
657 // The first entry is the undefined symbol entry.
Rafael Espindola7be2c332010-10-31 00:16:26 +0000658 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
Matt Fleming3565a062010-08-16 18:57:57 +0000659
660 // Write the symbol table entries.
661 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
662 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
663 ELFSymbolData &MSD = LocalSymbolData[i];
Rafael Espindola7be2c332010-10-31 00:16:26 +0000664 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000665 }
666
Rafael Espindola71859c62010-09-16 19:46:31 +0000667 // Write out a symbol table entry for each regular section.
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000668 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
669 ++i) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000670 const MCSectionELF &Section =
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000671 static_cast<const MCSectionELF&>(i->getSection());
672 if (Section.getType() == ELF::SHT_RELA ||
673 Section.getType() == ELF::SHT_REL ||
674 Section.getType() == ELF::SHT_STRTAB ||
675 Section.getType() == ELF::SHT_SYMTAB)
Eli Friedmana44fa242010-08-16 21:17:09 +0000676 continue;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000677 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000678 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section), false);
Eli Friedmana44fa242010-08-16 21:17:09 +0000679 LastLocalSymbolIndex++;
680 }
Matt Fleming3565a062010-08-16 18:57:57 +0000681
682 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
683 ELFSymbolData &MSD = ExternalSymbolData[i];
684 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000685 assert(((Data.getFlags() & ELF_STB_Global) ||
686 (Data.getFlags() & ELF_STB_Weak)) &&
687 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000688 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000689 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000690 LastLocalSymbolIndex++;
691 }
692
693 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
694 ELFSymbolData &MSD = UndefinedSymbolData[i];
695 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000696 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000697 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000698 LastLocalSymbolIndex++;
699 }
700}
701
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000702const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm,
703 const MCValue &Target,
704 const MCFragment &F) const {
705 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000706 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
707 const MCSymbol *Renamed = Renames.lookup(&Symbol);
708 const MCSymbolData &SD = Asm.getSymbolData(Symbol);
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000709
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000710 if (ASymbol.isUndefined()) {
711 if (Renamed)
712 return Renamed;
713 return &ASymbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000714 }
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000715
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000716 if (SD.isExternal()) {
717 if (Renamed)
718 return Renamed;
719 return &Symbol;
720 }
Rafael Espindola73ffea42010-09-25 05:42:19 +0000721
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000722 const MCSectionELF &Section =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000723 static_cast<const MCSectionELF&>(ASymbol.getSection());
Rafael Espindola25958732010-11-24 21:57:39 +0000724 const SectionKind secKind = Section.getKind();
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000725
Rafael Espindola25958732010-11-24 21:57:39 +0000726 if (secKind.isBSS())
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000727 return NULL;
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000728
Rafael Espindola25958732010-11-24 21:57:39 +0000729 if (secKind.isThreadLocal()) {
730 if (Renamed)
731 return Renamed;
732 return &Symbol;
733 }
734
Rafael Espindola8cecf252010-10-06 16:23:36 +0000735 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000736 const MCSectionELF &Sec2 =
737 static_cast<const MCSectionELF&>(F.getParent()->getSection());
738
Rafael Espindola8cecf252010-10-06 16:23:36 +0000739 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000740 (Kind == MCSymbolRefExpr::VK_PLT ||
741 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
Rafael Espindola25958732010-11-24 21:57:39 +0000742 Kind == MCSymbolRefExpr::VK_GOTOFF)) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000743 if (Renamed)
744 return Renamed;
745 return &Symbol;
746 }
Rafael Espindola3729d002010-10-05 23:57:26 +0000747
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000748 if (Section.getFlags() & MCSectionELF::SHF_MERGE) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000749 if (Target.getConstant() == 0)
750 return NULL;
751 if (Renamed)
752 return Renamed;
753 return &Symbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000754 }
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000755
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000756 return NULL;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000757}
758
Matt Fleming3565a062010-08-16 18:57:57 +0000759
Jason W Kim56a39902010-12-06 21:57:34 +0000760void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
761 const MCAsmLayout &Layout,
762 const MCFragment *Fragment,
763 const MCFixup &Fixup,
764 MCValue Target,
765 uint64_t &FixedValue) {
766 int64_t Addend = 0;
767 int Index = 0;
768 int64_t Value = Target.getConstant();
769 const MCSymbol *RelocSymbol = NULL;
770
771 bool IsPCRel = isFixupKindPCRel(Fixup.getKind());
772 if (!Target.isAbsolute()) {
773 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
774 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
775 RelocSymbol = SymbolToReloc(Asm, Target, *Fragment);
776
777 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
778 const MCSymbol &SymbolB = RefB->getSymbol();
779 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
780 IsPCRel = true;
781
782 // Offset of the symbol in the section
783 int64_t a = Layout.getSymbolOffset(&SDB);
784
785 // Ofeset of the relocation in the section
786 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
787 Value += b - a;
788 }
789
790 if (!RelocSymbol) {
791 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
792 MCFragment *F = SD.getFragment();
793
794 Index = F->getParent()->getOrdinal() + 1;
795
796 // Offset of the symbol in the section
797 Value += Layout.getSymbolOffset(&SD);
798 } else {
799 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
800 WeakrefUsedInReloc.insert(RelocSymbol);
801 else
802 UsedInReloc.insert(RelocSymbol);
803 Index = -1;
804 }
805 Addend = Value;
806 // Compensate for the addend on i386.
807 if (Is64Bit)
808 Value = 0;
809 }
810
811 FixedValue = Value;
812 unsigned Type = GetRelocType(Target, Fixup, IsPCRel,
813 (RelocSymbol != 0), Addend);
814
815 uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
816 Fixup.getOffset();
817
818 if (!HasRelocationAddend) Addend = 0;
819 ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend);
820 Relocations[Fragment->getParent()].push_back(ERE);
821}
822
823
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000824uint64_t
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000825ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
826 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000827 MCSymbolData &SD = Asm.getSymbolData(*S);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000828 return SD.getIndex();
Matt Fleming3565a062010-08-16 18:57:57 +0000829}
830
Rafael Espindola737cd212010-10-05 18:01:23 +0000831static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
Rafael Espindola88182132010-10-27 15:18:17 +0000832 bool Used, bool Renamed) {
Rafael Espindola484291c2010-11-01 14:28:48 +0000833 if (Data.getFlags() & ELF_Other_Weakref)
834 return false;
835
Rafael Espindolabd701182010-10-19 19:31:37 +0000836 if (Used)
837 return true;
838
Rafael Espindola88182132010-10-27 15:18:17 +0000839 if (Renamed)
840 return false;
841
Rafael Espindola737cd212010-10-05 18:01:23 +0000842 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000843
Rafael Espindolad1798862010-10-29 23:09:31 +0000844 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
845 return true;
846
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000847 const MCSymbol &A = Symbol.AliasedSymbol();
Rafael Espindolad1798862010-10-29 23:09:31 +0000848 if (!A.isVariable() && A.isUndefined() && !Data.isCommon())
Rafael Espindolaa6866962010-10-27 14:44:52 +0000849 return false;
850
Rafael Espindola737cd212010-10-05 18:01:23 +0000851 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
852 return false;
853
Rafael Espindolabd701182010-10-19 19:31:37 +0000854 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000855 return false;
856
857 return true;
858}
859
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000860static bool isLocal(const MCSymbolData &Data, bool isSignature,
861 bool isUsedInReloc) {
Rafael Espindola737cd212010-10-05 18:01:23 +0000862 if (Data.isExternal())
863 return false;
864
865 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000866 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000867
868 if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
869 if (isSignature && !isUsedInReloc)
870 return true;
871
Rafael Espindola737cd212010-10-05 18:01:23 +0000872 return false;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000873 }
Rafael Espindola737cd212010-10-05 18:01:23 +0000874
875 return true;
876}
877
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000878void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
879 SectionIndexMapTy &SectionIndexMap) {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000880 unsigned Index = 1;
881 for (MCAssembler::iterator it = Asm.begin(),
882 ie = Asm.end(); it != ie; ++it) {
883 const MCSectionELF &Section =
884 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000885 if (Section.getType() != ELF::SHT_GROUP)
886 continue;
887 SectionIndexMap[&Section] = Index++;
888 }
889
890 for (MCAssembler::iterator it = Asm.begin(),
891 ie = Asm.end(); it != ie; ++it) {
892 const MCSectionELF &Section =
893 static_cast<const MCSectionELF &>(it->getSection());
894 if (Section.getType() == ELF::SHT_GROUP)
895 continue;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000896 SectionIndexMap[&Section] = Index++;
897 }
898}
899
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000900void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000901 const SectionIndexMapTy &SectionIndexMap,
902 RevGroupMapTy RevGroupMap) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000903 // FIXME: Is this the correct place to do this?
904 if (NeedsGOT) {
905 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
906 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
907 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
908 Data.setExternal(true);
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000909 SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindola5c77c162010-10-05 15:48:37 +0000910 }
911
Matt Fleming3565a062010-08-16 18:57:57 +0000912 // Build section lookup table.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000913 int NumRegularSections = Asm.size();
Matt Fleming3565a062010-08-16 18:57:57 +0000914
915 // Index 0 is always the empty string.
916 StringMap<uint64_t> StringIndexMap;
917 StringTable += '\x00';
918
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000919 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000920 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
921 ie = Asm.symbol_end(); it != ie; ++it) {
922 const MCSymbol &Symbol = it->getSymbol();
923
Rafael Espindola484291c2010-11-01 14:28:48 +0000924 bool Used = UsedInReloc.count(&Symbol);
925 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000926 bool isSignature = RevGroupMap.count(&Symbol);
927
928 if (!isInSymtab(Asm, *it,
929 Used || WeakrefUsed || isSignature,
Rafael Espindola88182132010-10-27 15:18:17 +0000930 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000931 continue;
932
Matt Fleming3565a062010-08-16 18:57:57 +0000933 ELFSymbolData MSD;
934 MSD.SymbolData = it;
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000935 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Matt Fleming3565a062010-08-16 18:57:57 +0000936
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000937 // Undefined symbols are global, but this is the first place we
938 // are able to set it.
939 bool Local = isLocal(*it, isSignature, Used);
940 if (!Local && GetBinding(*it) == ELF::STB_LOCAL) {
941 MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
942 SetBinding(*it, ELF::STB_GLOBAL);
943 SetBinding(SD, ELF::STB_GLOBAL);
944 }
945
Rafael Espindola484291c2010-11-01 14:28:48 +0000946 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
947 SetBinding(*it, ELF::STB_WEAK);
948
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000949 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000950 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000951 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000952 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000953 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000954 } else if (RefSymbol.isUndefined()) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000955 if (isSignature && !Used)
956 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
957 else
958 MSD.SectionIndex = ELF::SHN_UNDEF;
Matt Fleming3565a062010-08-16 18:57:57 +0000959 } else {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000960 const MCSectionELF &Section =
961 static_cast<const MCSectionELF&>(RefSymbol.getSection());
962 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000963 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
964 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000965 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000966 }
967
Rafael Espindola88182132010-10-27 15:18:17 +0000968 // The @@@ in symbol version is replaced with @ in undefined symbols and
969 // @@ in defined ones.
970 StringRef Name = Symbol.getName();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000971 SmallString<32> Buf;
972
Rafael Espindola88182132010-10-27 15:18:17 +0000973 size_t Pos = Name.find("@@@");
Rafael Espindola88182132010-10-27 15:18:17 +0000974 if (Pos != StringRef::npos) {
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000975 Buf += Name.substr(0, Pos);
976 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
977 Buf += Name.substr(Pos + Skip);
978 Name = Buf;
Rafael Espindola88182132010-10-27 15:18:17 +0000979 }
980
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000981 uint64_t &Entry = StringIndexMap[Name];
Rafael Espindolaa6866962010-10-27 14:44:52 +0000982 if (!Entry) {
983 Entry = StringTable.size();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000984 StringTable += Name;
Rafael Espindolaa6866962010-10-27 14:44:52 +0000985 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +0000986 }
Rafael Espindolaa6866962010-10-27 14:44:52 +0000987 MSD.StringIndex = Entry;
988 if (MSD.SectionIndex == ELF::SHN_UNDEF)
989 UndefinedSymbolData.push_back(MSD);
990 else if (Local)
991 LocalSymbolData.push_back(MSD);
992 else
993 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +0000994 }
995
996 // Symbols are required to be in lexicographic order.
997 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
998 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
999 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
1000
1001 // Set the symbol indices. Local symbols must come before all other
1002 // symbols with non-local bindings.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +00001003 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +00001004 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
1005 LocalSymbolData[i].SymbolData->setIndex(Index++);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +00001006
1007 Index += NumRegularSections;
1008
Matt Fleming3565a062010-08-16 18:57:57 +00001009 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
1010 ExternalSymbolData[i].SymbolData->setIndex(Index++);
1011 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
1012 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
1013}
1014
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001015void ELFObjectWriter::WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
1016 const MCSectionData &SD) {
Matt Fleming3565a062010-08-16 18:57:57 +00001017 if (!Relocations[&SD].empty()) {
1018 MCContext &Ctx = Asm.getContext();
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001019 const MCSectionELF *RelaSection;
Matt Fleming3565a062010-08-16 18:57:57 +00001020 const MCSectionELF &Section =
1021 static_cast<const MCSectionELF&>(SD.getSection());
1022
1023 const StringRef SectionName = Section.getSectionName();
Benjamin Kramer377a5722010-08-17 17:30:07 +00001024 std::string RelaSectionName = HasRelocationAddend ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +00001025 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +00001026
1027 unsigned EntrySize;
1028 if (HasRelocationAddend)
1029 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
1030 else
1031 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +00001032
Benjamin Kramer377a5722010-08-17 17:30:07 +00001033 RelaSection = Ctx.getELFSection(RelaSectionName, HasRelocationAddend ?
1034 ELF::SHT_RELA : ELF::SHT_REL, 0,
Matt Fleming3565a062010-08-16 18:57:57 +00001035 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001036 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001037
1038 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001039 RelaSD.setAlignment(Is64Bit ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +00001040
1041 MCDataFragment *F = new MCDataFragment(&RelaSD);
1042
1043 WriteRelocationsFragment(Asm, F, &SD);
Matt Fleming3565a062010-08-16 18:57:57 +00001044 }
1045}
1046
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001047void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
1048 uint64_t Flags, uint64_t Address,
1049 uint64_t Offset, uint64_t Size,
1050 uint32_t Link, uint32_t Info,
1051 uint64_t Alignment,
1052 uint64_t EntrySize) {
Matt Fleming3565a062010-08-16 18:57:57 +00001053 Write32(Name); // sh_name: index into string table
1054 Write32(Type); // sh_type
1055 WriteWord(Flags); // sh_flags
1056 WriteWord(Address); // sh_addr
1057 WriteWord(Offset); // sh_offset
1058 WriteWord(Size); // sh_size
1059 Write32(Link); // sh_link
1060 Write32(Info); // sh_info
1061 WriteWord(Alignment); // sh_addralign
1062 WriteWord(EntrySize); // sh_entsize
1063}
1064
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001065void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
1066 MCDataFragment *F,
1067 const MCSectionData *SD) {
Matt Fleming3565a062010-08-16 18:57:57 +00001068 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
1069 // sort by the r_offset just like gnu as does
1070 array_pod_sort(Relocs.begin(), Relocs.end());
1071
1072 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1073 ELFRelocationEntry entry = Relocs[e - i - 1];
1074
Rafael Espindola12203cc2010-11-21 00:48:25 +00001075 if (!entry.Index)
1076 ;
1077 else if (entry.Index < 0)
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001078 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
1079 else
Rafael Espindola12203cc2010-11-21 00:48:25 +00001080 entry.Index += LocalSymbolData.size();
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001081 if (Is64Bit) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001082 String64(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001083
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001084 struct ELF::Elf64_Rela ERE64;
1085 ERE64.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001086 String64(*F, ERE64.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001087
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001088 if (HasRelocationAddend)
1089 String64(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001090 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001091 String32(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001092
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001093 struct ELF::Elf32_Rela ERE32;
1094 ERE32.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001095 String32(*F, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001096
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001097 if (HasRelocationAddend)
1098 String32(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001099 }
Matt Fleming3565a062010-08-16 18:57:57 +00001100 }
1101}
1102
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001103void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
1104 MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001105 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +00001106 MCContext &Ctx = Asm.getContext();
1107 MCDataFragment *F;
1108
Matt Fleming3565a062010-08-16 18:57:57 +00001109 unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
1110
Rafael Espindola38738bf2010-09-22 19:04:41 +00001111 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001112 const MCSectionELF *ShstrtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001113 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001114 SectionKind::getReadOnly());
Rafael Espindola71859c62010-09-16 19:46:31 +00001115 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1116 ShstrtabSD.setAlignment(1);
1117 ShstrtabIndex = Asm.size();
1118
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001119 const MCSectionELF *SymtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001120 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1121 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001122 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001123 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Matt Fleming3565a062010-08-16 18:57:57 +00001124 SymtabSD.setAlignment(Is64Bit ? 8 : 4);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001125 SymbolTableIndex = Asm.size();
1126
1127 MCSectionData *SymtabShndxSD = NULL;
1128
1129 if (NeedsSymtabShndx) {
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001130 const MCSectionELF *SymtabShndxSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001131 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001132 SectionKind::getReadOnly(), 4, "");
Rafael Espindola7be2c332010-10-31 00:16:26 +00001133 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
1134 SymtabShndxSD->setAlignment(4);
1135 }
Matt Fleming3565a062010-08-16 18:57:57 +00001136
Matt Fleming3565a062010-08-16 18:57:57 +00001137 const MCSection *StrtabSection;
1138 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001139 SectionKind::getReadOnly());
Matt Fleming3565a062010-08-16 18:57:57 +00001140 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1141 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +00001142 StringTableIndex = Asm.size();
1143
Rafael Espindolac3c413f2010-09-27 22:04:54 +00001144 WriteRelocations(Asm, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001145
1146 // Symbol table
1147 F = new MCDataFragment(&SymtabSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001148 MCDataFragment *ShndxF = NULL;
1149 if (NeedsSymtabShndx) {
1150 ShndxF = new MCDataFragment(SymtabShndxSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001151 }
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001152 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
Rafael Espindola71859c62010-09-16 19:46:31 +00001153
Matt Fleming3565a062010-08-16 18:57:57 +00001154 F = new MCDataFragment(&StrtabSD);
1155 F->getContents().append(StringTable.begin(), StringTable.end());
Matt Fleming3565a062010-08-16 18:57:57 +00001156
Matt Fleming3565a062010-08-16 18:57:57 +00001157 F = new MCDataFragment(&ShstrtabSD);
1158
Matt Fleming3565a062010-08-16 18:57:57 +00001159 // Section header string table.
1160 //
1161 // The first entry of a string table holds a null character so skip
1162 // section 0.
1163 uint64_t Index = 1;
1164 F->getContents() += '\x00';
1165
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001166 StringMap<uint64_t> SecStringMap;
Matt Fleming3565a062010-08-16 18:57:57 +00001167 for (MCAssembler::const_iterator it = Asm.begin(),
1168 ie = Asm.end(); it != ie; ++it) {
Matt Fleming3565a062010-08-16 18:57:57 +00001169 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +00001170 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola51efe7a2010-09-23 14:14:56 +00001171 // FIXME: We could merge suffixes like in .text and .rela.text.
Matt Fleming3565a062010-08-16 18:57:57 +00001172
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001173 StringRef Name = Section.getSectionName();
1174 if (SecStringMap.count(Name)) {
1175 SectionStringTableIndex[&Section] = SecStringMap[Name];
1176 continue;
1177 }
Matt Fleming3565a062010-08-16 18:57:57 +00001178 // Remember the index into the string table so we can write it
1179 // into the sh_name field of the section header table.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001180 SectionStringTableIndex[&Section] = Index;
1181 SecStringMap[Name] = Index;
Matt Fleming3565a062010-08-16 18:57:57 +00001182
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001183 Index += Name.size() + 1;
1184 F->getContents() += Name;
Matt Fleming3565a062010-08-16 18:57:57 +00001185 F->getContents() += '\x00';
1186 }
Rafael Espindola70703872010-09-30 02:22:20 +00001187}
1188
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001189bool ELFObjectWriter::IsFixupFullyResolved(const MCAssembler &Asm,
1190 const MCValue Target,
1191 bool IsPCRel,
1192 const MCFragment *DF) const {
Rafael Espindola70703872010-09-30 02:22:20 +00001193 // If this is a PCrel relocation, find the section this fixup value is
1194 // relative to.
1195 const MCSection *BaseSection = 0;
1196 if (IsPCRel) {
1197 BaseSection = &DF->getParent()->getSection();
1198 assert(BaseSection);
1199 }
1200
1201 const MCSection *SectionA = 0;
1202 const MCSymbol *SymbolA = 0;
1203 if (const MCSymbolRefExpr *A = Target.getSymA()) {
Rafael Espindola2c920852010-11-16 04:11:46 +00001204 SymbolA = &A->getSymbol();
1205 SectionA = &SymbolA->AliasedSymbol().getSection();
Rafael Espindola70703872010-09-30 02:22:20 +00001206 }
1207
1208 const MCSection *SectionB = 0;
Rafael Espindola12203cc2010-11-21 00:48:25 +00001209 const MCSymbol *SymbolB = 0;
Rafael Espindola70703872010-09-30 02:22:20 +00001210 if (const MCSymbolRefExpr *B = Target.getSymB()) {
Rafael Espindola12203cc2010-11-21 00:48:25 +00001211 SymbolB = &B->getSymbol();
1212 SectionB = &SymbolB->AliasedSymbol().getSection();
Rafael Espindola70703872010-09-30 02:22:20 +00001213 }
1214
1215 if (!BaseSection)
1216 return SectionA == SectionB;
1217
Rafael Espindola12203cc2010-11-21 00:48:25 +00001218 if (SymbolB)
1219 return false;
1220
1221 // Absolute address but PCrel instruction, so we need a relocation.
1222 if (!SymbolA)
1223 return false;
1224
Rafael Espindola2c920852010-11-16 04:11:46 +00001225 // FIXME: This is in here just to match gnu as output. If the two ends
1226 // are in the same section, there is nothing that the linker can do to
1227 // break it.
Rafael Espindola70703872010-09-30 02:22:20 +00001228 const MCSymbolData &DataA = Asm.getSymbolData(*SymbolA);
1229 if (DataA.isExternal())
1230 return false;
1231
Rafael Espindola12203cc2010-11-21 00:48:25 +00001232 return BaseSection == SectionA;
Matt Fleming3565a062010-08-16 18:57:57 +00001233}
1234
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001235void ELFObjectWriter::CreateGroupSections(MCAssembler &Asm,
1236 MCAsmLayout &Layout,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001237 GroupMapTy &GroupMap,
1238 RevGroupMapTy &RevGroupMap) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001239 // Build the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001240 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1241 it != ie; ++it) {
1242 const MCSectionELF &Section =
1243 static_cast<const MCSectionELF&>(it->getSection());
1244 if (!(Section.getFlags() & MCSectionELF::SHF_GROUP))
1245 continue;
1246
1247 const MCSymbol *SignatureSymbol = Section.getGroup();
1248 Asm.getOrCreateSymbolData(*SignatureSymbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001249 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001250 if (!Group) {
1251 Group = Asm.getContext().CreateELFGroupSection();
1252 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1253 Data.setAlignment(4);
1254 MCDataFragment *F = new MCDataFragment(&Data);
1255 String32(*F, ELF::GRP_COMDAT);
1256 }
1257 GroupMap[Group] = SignatureSymbol;
1258 }
1259
1260 // Add sections to the groups
1261 unsigned Index = 1;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001262 unsigned NumGroups = RevGroupMap.size();
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001263 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1264 it != ie; ++it, ++Index) {
1265 const MCSectionELF &Section =
1266 static_cast<const MCSectionELF&>(it->getSection());
1267 if (!(Section.getFlags() & MCSectionELF::SHF_GROUP))
1268 continue;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001269 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001270 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1271 // FIXME: we could use the previous fragment
1272 MCDataFragment *F = new MCDataFragment(&Data);
1273 String32(*F, NumGroups + Index);
1274 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001275}
1276
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001277void ELFObjectWriter::WriteSection(MCAssembler &Asm,
1278 const SectionIndexMapTy &SectionIndexMap,
1279 uint32_t GroupSymbolIndex,
1280 uint64_t Offset, uint64_t Size,
1281 uint64_t Alignment,
1282 const MCSectionELF &Section) {
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001283 uint64_t sh_link = 0;
1284 uint64_t sh_info = 0;
1285
1286 switch(Section.getType()) {
1287 case ELF::SHT_DYNAMIC:
1288 sh_link = SectionStringTableIndex[&Section];
1289 sh_info = 0;
1290 break;
1291
1292 case ELF::SHT_REL:
1293 case ELF::SHT_RELA: {
1294 const MCSectionELF *SymtabSection;
1295 const MCSectionELF *InfoSection;
1296 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
1297 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001298 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001299 sh_link = SectionIndexMap.lookup(SymtabSection);
1300 assert(sh_link && ".symtab not found");
1301
1302 // Remove ".rel" and ".rela" prefixes.
1303 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1304 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
1305
1306 InfoSection = Asm.getContext().getELFSection(SectionName,
1307 ELF::SHT_PROGBITS, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001308 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001309 sh_info = SectionIndexMap.lookup(InfoSection);
1310 break;
1311 }
1312
1313 case ELF::SHT_SYMTAB:
1314 case ELF::SHT_DYNSYM:
1315 sh_link = StringTableIndex;
1316 sh_info = LastLocalSymbolIndex;
1317 break;
1318
1319 case ELF::SHT_SYMTAB_SHNDX:
1320 sh_link = SymbolTableIndex;
1321 break;
1322
1323 case ELF::SHT_PROGBITS:
1324 case ELF::SHT_STRTAB:
1325 case ELF::SHT_NOBITS:
1326 case ELF::SHT_NULL:
1327 case ELF::SHT_ARM_ATTRIBUTES:
1328 // Nothing to do.
1329 break;
1330
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001331 case ELF::SHT_GROUP: {
1332 sh_link = SymbolTableIndex;
1333 sh_info = GroupSymbolIndex;
1334 break;
1335 }
1336
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001337 default:
1338 assert(0 && "FIXME: sh_type value not supported!");
1339 break;
1340 }
1341
1342 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1343 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1344 Alignment, Section.getEntrySize());
1345}
1346
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001347static bool IsELFMetaDataSection(const MCSectionData &SD) {
Rafael Espindolaf8803fe2010-12-06 03:48:09 +00001348 return SD.getOrdinal() == ~UINT32_C(0) &&
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001349 !SD.getSection().isVirtualSection();
1350}
1351
1352static uint64_t DataSectionSize(const MCSectionData &SD) {
1353 uint64_t Ret = 0;
1354 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1355 ++i) {
1356 const MCFragment &F = *i;
1357 assert(F.getKind() == MCFragment::FT_Data);
1358 Ret += cast<MCDataFragment>(F).getContents().size();
1359 }
1360 return Ret;
1361}
1362
1363static uint64_t GetSectionFileSize(const MCAsmLayout &Layout,
1364 const MCSectionData &SD) {
1365 if (IsELFMetaDataSection(SD))
1366 return DataSectionSize(SD);
1367 return Layout.getSectionFileSize(&SD);
1368}
1369
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001370static uint64_t GetSectionAddressSize(const MCAsmLayout &Layout,
1371 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001372 if (IsELFMetaDataSection(SD))
1373 return DataSectionSize(SD);
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001374 return Layout.getSectionAddressSize(&SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001375}
1376
1377static void WriteDataSectionData(ELFObjectWriter *W, const MCSectionData &SD) {
1378 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1379 ++i) {
1380 const MCFragment &F = *i;
1381 assert(F.getKind() == MCFragment::FT_Data);
1382 W->WriteBytes(cast<MCDataFragment>(F).getContents().str());
1383 }
1384}
1385
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001386void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1387 const MCAsmLayout &Layout) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001388 GroupMapTy GroupMap;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001389 RevGroupMapTy RevGroupMap;
1390 CreateGroupSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1391 RevGroupMap);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001392
Rafael Espindolabab2a802010-11-10 21:51:05 +00001393 SectionIndexMapTy SectionIndexMap;
1394
1395 ComputeIndexMap(Asm, SectionIndexMap);
1396
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001397 // Compute symbol table information.
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001398 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap);
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001399
Matt Fleming3565a062010-08-16 18:57:57 +00001400 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001401 const_cast<MCAsmLayout&>(Layout),
1402 SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +00001403
Rafael Espindola1d739a02010-11-10 22:34:07 +00001404 // Update to include the metadata sections.
1405 ComputeIndexMap(Asm, SectionIndexMap);
1406
Matt Fleming3565a062010-08-16 18:57:57 +00001407 // Add 1 for the null section.
1408 unsigned NumSections = Asm.size() + 1;
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001409 uint64_t NaturalAlignment = Is64Bit ? 8 : 4;
1410 uint64_t HeaderSize = Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr);
1411 uint64_t FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001412
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001413 std::vector<const MCSectionELF*> Sections;
1414 Sections.resize(NumSections);
1415
1416 for (SectionIndexMapTy::const_iterator i=
1417 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1418 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
1419 Sections[p.second] = p.first;
1420 }
1421
1422 for (unsigned i = 1; i < NumSections; ++i) {
1423 const MCSectionELF &Section = *Sections[i];
1424 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001425
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001426 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1427
Matt Fleming3565a062010-08-16 18:57:57 +00001428 // Get the size of the section in the output file (including padding).
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001429 FileOff += GetSectionFileSize(Layout, SD);
Matt Fleming3565a062010-08-16 18:57:57 +00001430 }
1431
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001432 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1433
Matt Fleming3565a062010-08-16 18:57:57 +00001434 // Write out the ELF header ...
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001435 WriteHeader(FileOff - HeaderSize, NumSections);
1436
1437 FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001438
1439 // ... then all of the sections ...
1440 DenseMap<const MCSection*, uint64_t> SectionOffsetMap;
1441
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001442 for (unsigned i = 1; i < NumSections; ++i) {
1443 const MCSectionELF &Section = *Sections[i];
1444 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001445
1446 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1447 WriteZeros(Padding);
1448 FileOff += Padding;
1449
Matt Fleming3565a062010-08-16 18:57:57 +00001450 // Remember the offset into the file for this section.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001451 SectionOffsetMap[&Section] = FileOff;
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001452
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001453 FileOff += GetSectionFileSize(Layout, SD);
Matt Fleming3565a062010-08-16 18:57:57 +00001454
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001455 if (IsELFMetaDataSection(SD))
1456 WriteDataSectionData(this, SD);
1457 else
1458 Asm.WriteSectionData(&SD, Layout, this);
Matt Fleming3565a062010-08-16 18:57:57 +00001459 }
1460
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001461 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1462 WriteZeros(Padding);
1463 FileOff += Padding;
1464
Matt Fleming3565a062010-08-16 18:57:57 +00001465 // ... and then the section header table.
1466 // Should we align the section header table?
1467 //
1468 // Null section first.
Rafael Espindola7be2c332010-10-31 00:16:26 +00001469 uint64_t FirstSectionSize =
1470 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1471 uint32_t FirstSectionLink =
1472 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1473 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming3565a062010-08-16 18:57:57 +00001474
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001475 for (unsigned i = 1; i < NumSections; ++i) {
1476 const MCSectionELF &Section = *Sections[i];
1477 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1478 uint32_t GroupSymbolIndex;
1479 if (Section.getType() != ELF::SHT_GROUP)
1480 GroupSymbolIndex = 0;
1481 else
1482 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm, GroupMap[&Section]);
Matt Fleming3565a062010-08-16 18:57:57 +00001483
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001484 uint64_t Size = GetSectionAddressSize(Layout, SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001485
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001486 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001487 SectionOffsetMap[&Section], Size,
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001488 SD.getAlignment(), Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001489 }
1490}
1491
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001492MCObjectWriter *llvm::createELFObjectWriter(raw_ostream &OS,
1493 bool Is64Bit,
1494 Triple::OSType OSType,
1495 uint16_t EMachine,
1496 bool IsLittleEndian,
1497 bool HasRelocationAddend) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001498 switch (EMachine) {
1499 case ELF::EM_386:
1500 case ELF::EM_X86_64:
1501 return new X86ELFObjectWriter(OS, Is64Bit, IsLittleEndian, EMachine,
1502 HasRelocationAddend, OSType); break;
1503 case ELF::EM_ARM:
1504 return new ARMELFObjectWriter(OS, Is64Bit, IsLittleEndian, EMachine,
1505 HasRelocationAddend, OSType); break;
Wesley Peck4b047132010-11-21 22:06:28 +00001506 case ELF::EM_MBLAZE:
1507 return new MBlazeELFObjectWriter(OS, Is64Bit, IsLittleEndian, EMachine,
1508 HasRelocationAddend, OSType); break;
Benjamin Kramer32858772010-11-15 19:20:50 +00001509 default: llvm_unreachable("Unsupported architecture"); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001510 }
1511}
1512
1513
1514/// START OF SUBCLASSES for ELFObjectWriter
1515//===- ARMELFObjectWriter -------------------------------------------===//
1516
1517ARMELFObjectWriter::ARMELFObjectWriter(raw_ostream &_OS, bool _Is64Bit,
1518 bool _IsLittleEndian,
1519 uint16_t _EMachine, bool _HasRelocationAddend,
1520 Triple::OSType _OSType)
1521 : ELFObjectWriter(_OS, _Is64Bit, _IsLittleEndian, _EMachine,
1522 _HasRelocationAddend, _OSType)
1523{}
1524
1525ARMELFObjectWriter::~ARMELFObjectWriter()
1526{}
1527
Jason W Kim85fed5e2010-12-01 02:40:06 +00001528unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target,
1529 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001530 bool IsPCRel,
1531 bool IsRelocWithSymbol,
1532 int64_t Addend) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001533 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1534 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
1535
1536 if (IsPCRel) {
1537 switch (Modifier) {
1538 default: assert(0 && "Unimplemented Modifier");
1539 case MCSymbolRefExpr::VK_None: break;
1540 }
1541 switch ((unsigned)Fixup.getKind()) {
1542 default: assert(0 && "Unimplemented");
1543 case ARM::fixup_arm_branch: return ELF::R_ARM_CALL; break;
1544 }
1545 } else {
1546 switch ((unsigned)Fixup.getKind()) {
1547 default: llvm_unreachable("invalid fixup kind!");
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:
Bill Wendlingb8958b02010-12-08 01:57:09 +00001552 case ARM::fixup_arm_thumb_cp:
Jason W Kim85fed5e2010-12-01 02:40:06 +00001553 assert(0 && "Unimplemented"); break;
1554 case ARM::fixup_arm_branch:
1555 return ELF::R_ARM_CALL; break;
1556 case ARM::fixup_arm_movt_hi16:
1557 return ELF::R_ARM_MOVT_ABS; break;
1558 case ARM::fixup_arm_movw_lo16:
1559 return ELF::R_ARM_MOVW_ABS_NC; break;
1560 }
1561 }
1562
1563 if (RelocNeedsGOT(Modifier))
1564 NeedsGOT = true;
1565 return -1;
1566}
1567
Wesley Peck4b047132010-11-21 22:06:28 +00001568//===- MBlazeELFObjectWriter -------------------------------------------===//
Jason W Kimd3443e92010-11-15 16:18:39 +00001569
Wesley Peck4b047132010-11-21 22:06:28 +00001570MBlazeELFObjectWriter::MBlazeELFObjectWriter(raw_ostream &_OS, bool _Is64Bit,
1571 bool _IsLittleEndian,
1572 uint16_t _EMachine,
1573 bool _HasRelocationAddend,
1574 Triple::OSType _OSType)
1575 : ELFObjectWriter(_OS, _Is64Bit, _IsLittleEndian, _EMachine,
1576 _HasRelocationAddend, _OSType) {
1577}
1578
1579MBlazeELFObjectWriter::~MBlazeELFObjectWriter() {
1580}
1581
Jason W Kim56a39902010-12-06 21:57:34 +00001582unsigned MBlazeELFObjectWriter::GetRelocType(const MCValue &Target,
Wesley Peck4b047132010-11-21 22:06:28 +00001583 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001584 bool IsPCRel,
1585 bool IsRelocWithSymbol,
1586 int64_t Addend) {
Wesley Peck4b047132010-11-21 22:06:28 +00001587 // determine the type of the relocation
1588 unsigned Type;
1589 if (IsPCRel) {
1590 switch ((unsigned)Fixup.getKind()) {
1591 default:
1592 llvm_unreachable("Unimplemented");
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001593 case FK_PCRel_4:
Wesley Peck4b047132010-11-21 22:06:28 +00001594 Type = ELF::R_MICROBLAZE_64_PCREL;
1595 break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001596 case FK_PCRel_2:
Wesley Peck4b047132010-11-21 22:06:28 +00001597 Type = ELF::R_MICROBLAZE_32_PCREL;
1598 break;
1599 }
1600 } else {
1601 switch ((unsigned)Fixup.getKind()) {
1602 default: llvm_unreachable("invalid fixup kind!");
1603 case FK_Data_4:
Jason W Kim56a39902010-12-06 21:57:34 +00001604 Type = ((IsRelocWithSymbol || Addend !=0)
1605 ? ELF::R_MICROBLAZE_32
1606 : ELF::R_MICROBLAZE_64);
Wesley Peck4b047132010-11-21 22:06:28 +00001607 break;
1608 case FK_Data_2:
1609 Type = ELF::R_MICROBLAZE_32;
1610 break;
1611 }
1612 }
Jason W Kim56a39902010-12-06 21:57:34 +00001613 return Type;
Wesley Peck4b047132010-11-21 22:06:28 +00001614}
Jason W Kimd3443e92010-11-15 16:18:39 +00001615
1616//===- X86ELFObjectWriter -------------------------------------------===//
1617
1618
1619X86ELFObjectWriter::X86ELFObjectWriter(raw_ostream &_OS, bool _Is64Bit,
1620 bool _IsLittleEndian,
1621 uint16_t _EMachine, bool _HasRelocationAddend,
1622 Triple::OSType _OSType)
1623 : ELFObjectWriter(_OS, _Is64Bit, _IsLittleEndian, _EMachine,
1624 _HasRelocationAddend, _OSType)
1625{}
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;
1640 if (Is64Bit) {
1641 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}