blob: c07c23149b6b3003f7845e207023f2d05d3c1870 [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 unsigned Is64Bit : 1;
179
180 bool HasRelocationAddend;
181
Roman Divacky5baf79e2010-09-09 17:57:50 +0000182 Triple::OSType OSType;
183
Wesley Peckeecb8582010-10-22 15:52:49 +0000184 uint16_t EMachine;
185
Matt Fleming3565a062010-08-16 18:57:57 +0000186 // This holds the symbol table index of the last local symbol.
187 unsigned LastLocalSymbolIndex;
188 // This holds the .strtab section index.
189 unsigned StringTableIndex;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000190 // This holds the .symtab section index.
191 unsigned SymbolTableIndex;
Matt Fleming3565a062010-08-16 18:57:57 +0000192
193 unsigned ShstrtabIndex;
194
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000195
196 const MCSymbol *SymbolToReloc(const MCAssembler &Asm,
197 const MCValue &Target,
198 const MCFragment &F) const;
199
Matt Fleming3565a062010-08-16 18:57:57 +0000200 public:
Rafael Espindola31f35782010-12-17 18:01:31 +0000201 ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
202 raw_ostream &_OS, bool _Is64Bit, bool IsLittleEndian,
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000203 uint16_t _EMachine, bool _HasRelAddend,
204 Triple::OSType _OSType)
205 : MCObjectWriter(_OS, IsLittleEndian),
Rafael Espindola31f35782010-12-17 18:01:31 +0000206 TargetObjectWriter(MOTW),
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000207 NeedsGOT(false), NeedsSymtabShndx(false),
Roman Divacky5baf79e2010-09-09 17:57:50 +0000208 Is64Bit(_Is64Bit), HasRelocationAddend(_HasRelAddend),
Wesley Peckeecb8582010-10-22 15:52:49 +0000209 OSType(_OSType), EMachine(_EMachine) {
Matt Fleming3565a062010-08-16 18:57:57 +0000210 }
Jason W Kimd3443e92010-11-15 16:18:39 +0000211
212 virtual ~ELFObjectWriter();
213
Matt Fleming3565a062010-08-16 18:57:57 +0000214 void WriteWord(uint64_t W) {
Chris Lattnerb188a372010-08-28 03:21:03 +0000215 if (Is64Bit)
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000216 Write64(W);
Chris Lattnerb188a372010-08-28 03:21:03 +0000217 else
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000218 Write32(W);
Matt Fleming3565a062010-08-16 18:57:57 +0000219 }
220
Matt Fleming3565a062010-08-16 18:57:57 +0000221 void StringLE16(char *buf, uint16_t Value) {
222 buf[0] = char(Value >> 0);
223 buf[1] = char(Value >> 8);
224 }
225
226 void StringLE32(char *buf, uint32_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000227 StringLE16(buf, uint16_t(Value >> 0));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000228 StringLE16(buf + 2, uint16_t(Value >> 16));
Matt Fleming3565a062010-08-16 18:57:57 +0000229 }
230
231 void StringLE64(char *buf, uint64_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000232 StringLE32(buf, uint32_t(Value >> 0));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000233 StringLE32(buf + 4, uint32_t(Value >> 32));
Matt Fleming3565a062010-08-16 18:57:57 +0000234 }
235
236 void StringBE16(char *buf ,uint16_t Value) {
237 buf[0] = char(Value >> 8);
238 buf[1] = char(Value >> 0);
239 }
240
241 void StringBE32(char *buf, uint32_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000242 StringBE16(buf, uint16_t(Value >> 16));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000243 StringBE16(buf + 2, uint16_t(Value >> 0));
Matt Fleming3565a062010-08-16 18:57:57 +0000244 }
245
246 void StringBE64(char *buf, uint64_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000247 StringBE32(buf, uint32_t(Value >> 32));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000248 StringBE32(buf + 4, uint32_t(Value >> 0));
Matt Fleming3565a062010-08-16 18:57:57 +0000249 }
250
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000251 void String8(MCDataFragment &F, uint8_t Value) {
252 char buf[1];
253 buf[0] = Value;
254 F.getContents() += StringRef(buf, 1);
255 }
256
257 void String16(MCDataFragment &F, uint16_t Value) {
258 char buf[2];
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000259 if (isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000260 StringLE16(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000261 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000262 StringBE16(buf, Value);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000263 F.getContents() += StringRef(buf, 2);
Matt Fleming3565a062010-08-16 18:57:57 +0000264 }
265
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000266 void String32(MCDataFragment &F, uint32_t Value) {
267 char buf[4];
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000268 if (isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000269 StringLE32(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000270 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000271 StringBE32(buf, Value);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000272 F.getContents() += StringRef(buf, 4);
Matt Fleming3565a062010-08-16 18:57:57 +0000273 }
274
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000275 void String64(MCDataFragment &F, uint64_t Value) {
276 char buf[8];
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000277 if (isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000278 StringLE64(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000279 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000280 StringBE64(buf, Value);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000281 F.getContents() += StringRef(buf, 8);
Matt Fleming3565a062010-08-16 18:57:57 +0000282 }
283
Jason W Kimd3443e92010-11-15 16:18:39 +0000284 virtual void WriteHeader(uint64_t SectionDataSize, unsigned NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000285
Jason W Kimd3443e92010-11-15 16:18:39 +0000286 virtual void WriteSymbolEntry(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
Rafael Espindola7be2c332010-10-31 00:16:26 +0000287 uint64_t name, uint8_t info,
Matt Fleming3565a062010-08-16 18:57:57 +0000288 uint64_t value, uint64_t size,
Rafael Espindola7be2c332010-10-31 00:16:26 +0000289 uint8_t other, uint32_t shndx,
290 bool Reserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000291
Jason W Kimd3443e92010-11-15 16:18:39 +0000292 virtual void WriteSymbol(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
Rafael Espindola7be2c332010-10-31 00:16:26 +0000293 ELFSymbolData &MSD,
Matt Fleming3565a062010-08-16 18:57:57 +0000294 const MCAsmLayout &Layout);
295
Rafael Espindolabab2a802010-11-10 21:51:05 +0000296 typedef DenseMap<const MCSectionELF*, uint32_t> SectionIndexMapTy;
Jason W Kimd3443e92010-11-15 16:18:39 +0000297 virtual void WriteSymbolTable(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
Rafael Espindola7be2c332010-10-31 00:16:26 +0000298 const MCAssembler &Asm,
Rafael Espindola71859c62010-09-16 19:46:31 +0000299 const MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000300 const SectionIndexMapTy &SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +0000301
Jason W Kimd3443e92010-11-15 16:18:39 +0000302 virtual void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
Jason W Kim56a39902010-12-06 21:57:34 +0000303 const MCFragment *Fragment, const MCFixup &Fixup,
304 MCValue Target, uint64_t &FixedValue);
Matt Fleming3565a062010-08-16 18:57:57 +0000305
Jason W Kimd3443e92010-11-15 16:18:39 +0000306 virtual uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm,
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000307 const MCSymbol *S);
Matt Fleming3565a062010-08-16 18:57:57 +0000308
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000309 // Map from a group section to the signature symbol
310 typedef DenseMap<const MCSectionELF*, const MCSymbol*> GroupMapTy;
311 // Map from a signature symbol to the group section
312 typedef DenseMap<const MCSymbol*, const MCSectionELF*> RevGroupMapTy;
313
Matt Fleming3565a062010-08-16 18:57:57 +0000314 /// ComputeSymbolTable - Compute the symbol table data
315 ///
316 /// \param StringTable [out] - The string table data.
317 /// \param StringIndexMap [out] - Map from symbol names to offsets in the
318 /// string table.
Jason W Kimd3443e92010-11-15 16:18:39 +0000319 virtual void ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000320 const SectionIndexMapTy &SectionIndexMap,
321 RevGroupMapTy RevGroupMap);
Rafael Espindolabab2a802010-11-10 21:51:05 +0000322
Jason W Kimd3443e92010-11-15 16:18:39 +0000323 virtual void ComputeIndexMap(MCAssembler &Asm,
Rafael Espindolabab2a802010-11-10 21:51:05 +0000324 SectionIndexMapTy &SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +0000325
Jason W Kimd3443e92010-11-15 16:18:39 +0000326 virtual void WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
Matt Fleming3565a062010-08-16 18:57:57 +0000327 const MCSectionData &SD);
328
Jason W Kimd3443e92010-11-15 16:18:39 +0000329 virtual void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout) {
Matt Fleming3565a062010-08-16 18:57:57 +0000330 for (MCAssembler::const_iterator it = Asm.begin(),
331 ie = Asm.end(); it != ie; ++it) {
332 WriteRelocation(Asm, Layout, *it);
333 }
334 }
335
Jason W Kimd3443e92010-11-15 16:18:39 +0000336 virtual void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000337 const SectionIndexMapTy &SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +0000338
Jason W Kimd3443e92010-11-15 16:18:39 +0000339 virtual void CreateGroupSections(MCAssembler &Asm, MCAsmLayout &Layout,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000340 GroupMapTy &GroupMap, RevGroupMapTy &RevGroupMap);
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000341
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000342 virtual void ExecutePostLayoutBinding(MCAssembler &Asm,
343 const MCAsmLayout &Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000344
Jason W Kimd3443e92010-11-15 16:18:39 +0000345 virtual void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
Matt Fleming3565a062010-08-16 18:57:57 +0000346 uint64_t Address, uint64_t Offset,
347 uint64_t Size, uint32_t Link, uint32_t Info,
348 uint64_t Alignment, uint64_t EntrySize);
349
Daniel Dunbar1f3662a2010-12-17 04:54:54 +0000350 virtual void WriteRelocationsFragment(const MCAssembler &Asm,
351 MCDataFragment *F,
352 const MCSectionData *SD);
353
354 virtual bool
355 IsSymbolRefDifferenceFullyResolved(const MCAssembler &Asm,
356 const MCSymbolRefExpr *A,
357 const MCSymbolRefExpr *B) const {
358 // FIXME: Implement this!
359 return false;
360 }
Matt Fleming3565a062010-08-16 18:57:57 +0000361
Jason W Kimd3443e92010-11-15 16:18:39 +0000362 virtual bool IsFixupFullyResolved(const MCAssembler &Asm,
Rafael Espindola70703872010-09-30 02:22:20 +0000363 const MCValue Target,
364 bool IsPCRel,
365 const MCFragment *DF) const;
366
Jason W Kimd3443e92010-11-15 16:18:39 +0000367 virtual void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
368 virtual void WriteSection(MCAssembler &Asm,
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000369 const SectionIndexMapTy &SectionIndexMap,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000370 uint32_t GroupSymbolIndex,
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000371 uint64_t Offset, uint64_t Size, uint64_t Alignment,
372 const MCSectionELF &Section);
Jason W Kim56a39902010-12-06 21:57:34 +0000373
374 protected:
375 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
376 bool IsPCRel, bool IsRelocWithSymbol,
377 int64_t Addend) = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000378 };
379
Jason W Kimd3443e92010-11-15 16:18:39 +0000380 //===- X86ELFObjectWriter -------------------------------------------===//
381
382 class X86ELFObjectWriter : public ELFObjectWriter {
383 public:
Rafael Espindola31f35782010-12-17 18:01:31 +0000384 X86ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
385 raw_ostream &_OS, bool _Is64Bit, bool IsLittleEndian,
Jason W Kimd3443e92010-11-15 16:18:39 +0000386 uint16_t _EMachine, bool _HasRelAddend,
387 Triple::OSType _OSType);
Wesley Peck4b047132010-11-21 22:06:28 +0000388
Jason W Kimd3443e92010-11-15 16:18:39 +0000389 virtual ~X86ELFObjectWriter();
Jason W Kim56a39902010-12-06 21:57:34 +0000390 protected:
391 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 };
395
396
397 //===- ARMELFObjectWriter -------------------------------------------===//
398
399 class ARMELFObjectWriter : public ELFObjectWriter {
400 public:
Rafael Espindola31f35782010-12-17 18:01:31 +0000401 ARMELFObjectWriter(MCELFObjectTargetWriter *MOTW,
402 raw_ostream &_OS, bool _Is64Bit, bool IsLittleEndian,
Jason W Kimd3443e92010-11-15 16:18:39 +0000403 uint16_t _EMachine, bool _HasRelAddend,
404 Triple::OSType _OSType);
Wesley Peck4b047132010-11-21 22:06:28 +0000405
Jason W Kimd3443e92010-11-15 16:18:39 +0000406 virtual ~ARMELFObjectWriter();
Jason W Kim85fed5e2010-12-01 02:40:06 +0000407 protected:
Jason W Kim56a39902010-12-06 21:57:34 +0000408 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
409 bool IsPCRel, bool IsRelocWithSymbol,
410 int64_t Addend);
Jason W Kimd3443e92010-11-15 16:18:39 +0000411 };
Wesley Peck4b047132010-11-21 22:06:28 +0000412
413 //===- MBlazeELFObjectWriter -------------------------------------------===//
414
415 class MBlazeELFObjectWriter : public ELFObjectWriter {
416 public:
Rafael Espindola31f35782010-12-17 18:01:31 +0000417 MBlazeELFObjectWriter(MCELFObjectTargetWriter *MOTW,
418 raw_ostream &_OS, bool _Is64Bit, bool IsLittleEndian,
Wesley Peck4b047132010-11-21 22:06:28 +0000419 uint16_t _EMachine, bool _HasRelAddend,
420 Triple::OSType _OSType);
421
422 virtual ~MBlazeELFObjectWriter();
Jason W Kim56a39902010-12-06 21:57:34 +0000423 protected:
424 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
425 bool IsPCRel, bool IsRelocWithSymbol,
426 int64_t Addend);
Wesley Peck4b047132010-11-21 22:06:28 +0000427 };
Matt Fleming3565a062010-08-16 18:57:57 +0000428}
429
Jason W Kimd3443e92010-11-15 16:18:39 +0000430ELFObjectWriter::~ELFObjectWriter()
431{}
432
Matt Fleming3565a062010-08-16 18:57:57 +0000433// Emit the ELF header.
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000434void ELFObjectWriter::WriteHeader(uint64_t SectionDataSize,
435 unsigned NumberOfSections) {
Matt Fleming3565a062010-08-16 18:57:57 +0000436 // ELF Header
437 // ----------
438 //
439 // Note
440 // ----
441 // emitWord method behaves differently for ELF32 and ELF64, writing
442 // 4 bytes in the former and 8 in the latter.
443
444 Write8(0x7f); // e_ident[EI_MAG0]
445 Write8('E'); // e_ident[EI_MAG1]
446 Write8('L'); // e_ident[EI_MAG2]
447 Write8('F'); // e_ident[EI_MAG3]
448
449 Write8(Is64Bit ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
450
451 // e_ident[EI_DATA]
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000452 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
Matt Fleming3565a062010-08-16 18:57:57 +0000453
454 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky5baf79e2010-09-09 17:57:50 +0000455 // e_ident[EI_OSABI]
456 switch (OSType) {
457 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
458 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
459 default: Write8(ELF::ELFOSABI_NONE); break;
460 }
Matt Fleming3565a062010-08-16 18:57:57 +0000461 Write8(0); // e_ident[EI_ABIVERSION]
462
463 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
464
465 Write16(ELF::ET_REL); // e_type
466
Wesley Peckeecb8582010-10-22 15:52:49 +0000467 Write16(EMachine); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +0000468
469 Write32(ELF::EV_CURRENT); // e_version
470 WriteWord(0); // e_entry, no entry point in .o file
471 WriteWord(0); // e_phoff, no program header for .o
Benjamin Kramereb976772010-08-17 17:02:29 +0000472 WriteWord(SectionDataSize + (Is64Bit ? sizeof(ELF::Elf64_Ehdr) :
473 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000474
475 // FIXME: Make this configurable.
476 Write32(0); // e_flags = whatever the target wants
477
478 // e_ehsize = ELF header size
479 Write16(Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
480
481 Write16(0); // e_phentsize = prog header entry size
482 Write16(0); // e_phnum = # prog header entries = 0
483
484 // e_shentsize = Section header entry size
485 Write16(Is64Bit ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
486
487 // e_shnum = # of section header ents
Rafael Espindola7be2c332010-10-31 00:16:26 +0000488 if (NumberOfSections >= ELF::SHN_LORESERVE)
489 Write16(0);
490 else
491 Write16(NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000492
493 // e_shstrndx = Section # of '.shstrtab'
Rafael Espindola7be2c332010-10-31 00:16:26 +0000494 if (NumberOfSections >= ELF::SHN_LORESERVE)
495 Write16(ELF::SHN_XINDEX);
496 else
497 Write16(ShstrtabIndex);
Matt Fleming3565a062010-08-16 18:57:57 +0000498}
499
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000500void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
501 MCDataFragment *ShndxF,
502 uint64_t name,
503 uint8_t info, uint64_t value,
504 uint64_t size, uint8_t other,
505 uint32_t shndx,
506 bool Reserved) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000507 if (ShndxF) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000508 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000509 String32(*ShndxF, shndx);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000510 else
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000511 String32(*ShndxF, 0);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000512 }
513
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000514 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
515 uint16_t(ELF::SHN_XINDEX) : shndx;
516
Matt Fleming3565a062010-08-16 18:57:57 +0000517 if (Is64Bit) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000518 String32(*SymtabF, name); // st_name
519 String8(*SymtabF, info); // st_info
520 String8(*SymtabF, other); // st_other
521 String16(*SymtabF, Index); // st_shndx
522 String64(*SymtabF, value); // st_value
523 String64(*SymtabF, size); // st_size
Matt Fleming3565a062010-08-16 18:57:57 +0000524 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000525 String32(*SymtabF, name); // st_name
526 String32(*SymtabF, value); // st_value
527 String32(*SymtabF, size); // st_size
528 String8(*SymtabF, info); // st_info
529 String8(*SymtabF, other); // st_other
530 String16(*SymtabF, Index); // st_shndx
Matt Fleming3565a062010-08-16 18:57:57 +0000531 }
532}
533
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000534static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout) {
535 if (Data.isCommon() && Data.isExternal())
536 return Data.getCommonAlignment();
537
538 const MCSymbol &Symbol = Data.getSymbol();
539 if (!Symbol.isInSection())
540 return 0;
541
Rafael Espindolaffd902b2010-12-06 02:57:26 +0000542 if (Data.getFragment())
543 return Layout.getSymbolOffset(&Data);
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000544
545 return 0;
546}
547
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000548void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
549 const MCAsmLayout &Layout) {
Rafael Espindola88182132010-10-27 15:18:17 +0000550 // The presence of symbol versions causes undefined symbols and
551 // versions declared with @@@ to be renamed.
552
553 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
554 ie = Asm.symbol_end(); it != ie; ++it) {
555 const MCSymbol &Alias = it->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000556 const MCSymbol &Symbol = Alias.AliasedSymbol();
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000557 MCSymbolData &SD = Asm.getSymbolData(Symbol);
558
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000559 // Not an alias.
560 if (&Symbol == &Alias)
561 continue;
562
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000563 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000564 size_t Pos = AliasName.find('@');
565 if (Pos == StringRef::npos)
566 continue;
567
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000568 // Aliases defined with .symvar copy the binding from the symbol they alias.
569 // This is the first place we are able to copy this information.
570 it->setExternal(SD.isExternal());
571 SetBinding(*it, GetBinding(SD));
572
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000573 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000574 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
575 continue;
576
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000577 // FIXME: produce a better error message.
578 if (Symbol.isUndefined() && Rest.startswith("@@") &&
579 !Rest.startswith("@@@"))
580 report_fatal_error("A @@ version cannot be undefined");
581
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000582 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000583 }
584}
585
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000586void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
587 MCDataFragment *ShndxF,
588 ELFSymbolData &MSD,
589 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000590 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000591 MCSymbolData &Data =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000592 Layout.getAssembler().getSymbolData(OrigData.getSymbol().AliasedSymbol());
Rafael Espindola152c1062010-10-06 21:02:29 +0000593
Rafael Espindola7be2c332010-10-31 00:16:26 +0000594 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
595 Data.getSymbol().isVariable();
596
Rafael Espindola152c1062010-10-06 21:02:29 +0000597 uint8_t Binding = GetBinding(OrigData);
598 uint8_t Visibility = GetVisibility(OrigData);
599 uint8_t Type = GetType(Data);
600
601 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
602 uint8_t Other = Visibility;
603
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000604 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000605 uint64_t Size = 0;
606 const MCExpr *ESize;
607
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000608 assert(!(Data.isCommon() && !Data.isExternal()));
609
Matt Fleming3565a062010-08-16 18:57:57 +0000610 ESize = Data.getSize();
611 if (Data.getSize()) {
612 MCValue Res;
613 if (ESize->getKind() == MCExpr::Binary) {
614 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(ESize);
615
616 if (BE->EvaluateAsRelocatable(Res, &Layout)) {
Benjamin Kramer24f12062010-10-17 07:38:40 +0000617 assert(!Res.getSymA() || !Res.getSymA()->getSymbol().isDefined());
618 assert(!Res.getSymB() || !Res.getSymB()->getSymbol().isDefined());
Rafael Espindolaf230df92010-10-16 18:23:53 +0000619 Size = Res.getConstant();
Matt Fleming3565a062010-08-16 18:57:57 +0000620 }
621 } else if (ESize->getKind() == MCExpr::Constant) {
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000622 Size = static_cast<const MCConstantExpr *>(ESize)->getValue();
Matt Fleming3565a062010-08-16 18:57:57 +0000623 } else {
624 assert(0 && "Unsupported size expression");
625 }
626 }
627
628 // Write out the symbol table entry
Rafael Espindola7be2c332010-10-31 00:16:26 +0000629 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
630 Size, Other, MSD.SectionIndex, IsReserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000631}
632
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000633void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
634 MCDataFragment *ShndxF,
635 const MCAssembler &Asm,
636 const MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000637 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000638 // The string table must be emitted first because we need the index
639 // into the string table for all the symbol names.
640 assert(StringTable.size() && "Missing string table");
641
642 // FIXME: Make sure the start of the symbol table is aligned.
643
644 // The first entry is the undefined symbol entry.
Rafael Espindola7be2c332010-10-31 00:16:26 +0000645 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
Matt Fleming3565a062010-08-16 18:57:57 +0000646
647 // Write the symbol table entries.
648 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
649 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
650 ELFSymbolData &MSD = LocalSymbolData[i];
Rafael Espindola7be2c332010-10-31 00:16:26 +0000651 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000652 }
653
Rafael Espindola71859c62010-09-16 19:46:31 +0000654 // Write out a symbol table entry for each regular section.
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000655 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
656 ++i) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000657 const MCSectionELF &Section =
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000658 static_cast<const MCSectionELF&>(i->getSection());
659 if (Section.getType() == ELF::SHT_RELA ||
660 Section.getType() == ELF::SHT_REL ||
661 Section.getType() == ELF::SHT_STRTAB ||
662 Section.getType() == ELF::SHT_SYMTAB)
Eli Friedmana44fa242010-08-16 21:17:09 +0000663 continue;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000664 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000665 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section), false);
Eli Friedmana44fa242010-08-16 21:17:09 +0000666 LastLocalSymbolIndex++;
667 }
Matt Fleming3565a062010-08-16 18:57:57 +0000668
669 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
670 ELFSymbolData &MSD = ExternalSymbolData[i];
671 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000672 assert(((Data.getFlags() & ELF_STB_Global) ||
673 (Data.getFlags() & ELF_STB_Weak)) &&
674 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000675 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000676 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000677 LastLocalSymbolIndex++;
678 }
679
680 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
681 ELFSymbolData &MSD = UndefinedSymbolData[i];
682 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000683 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000684 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000685 LastLocalSymbolIndex++;
686 }
687}
688
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000689const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm,
690 const MCValue &Target,
691 const MCFragment &F) const {
692 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000693 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
694 const MCSymbol *Renamed = Renames.lookup(&Symbol);
695 const MCSymbolData &SD = Asm.getSymbolData(Symbol);
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000696
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000697 if (ASymbol.isUndefined()) {
698 if (Renamed)
699 return Renamed;
700 return &ASymbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000701 }
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000702
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000703 if (SD.isExternal()) {
704 if (Renamed)
705 return Renamed;
706 return &Symbol;
707 }
Rafael Espindola73ffea42010-09-25 05:42:19 +0000708
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000709 const MCSectionELF &Section =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000710 static_cast<const MCSectionELF&>(ASymbol.getSection());
Rafael Espindola25958732010-11-24 21:57:39 +0000711 const SectionKind secKind = Section.getKind();
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000712
Rafael Espindola25958732010-11-24 21:57:39 +0000713 if (secKind.isBSS())
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000714 return NULL;
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000715
Rafael Espindola25958732010-11-24 21:57:39 +0000716 if (secKind.isThreadLocal()) {
717 if (Renamed)
718 return Renamed;
719 return &Symbol;
720 }
721
Rafael Espindola8cecf252010-10-06 16:23:36 +0000722 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000723 const MCSectionELF &Sec2 =
724 static_cast<const MCSectionELF&>(F.getParent()->getSection());
725
Rafael Espindola8cecf252010-10-06 16:23:36 +0000726 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000727 (Kind == MCSymbolRefExpr::VK_PLT ||
728 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
Rafael Espindola25958732010-11-24 21:57:39 +0000729 Kind == MCSymbolRefExpr::VK_GOTOFF)) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000730 if (Renamed)
731 return Renamed;
732 return &Symbol;
733 }
Rafael Espindola3729d002010-10-05 23:57:26 +0000734
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000735 if (Section.getFlags() & MCSectionELF::SHF_MERGE) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000736 if (Target.getConstant() == 0)
737 return NULL;
738 if (Renamed)
739 return Renamed;
740 return &Symbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000741 }
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000742
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000743 return NULL;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000744}
745
Matt Fleming3565a062010-08-16 18:57:57 +0000746
Jason W Kim56a39902010-12-06 21:57:34 +0000747void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
748 const MCAsmLayout &Layout,
749 const MCFragment *Fragment,
750 const MCFixup &Fixup,
751 MCValue Target,
752 uint64_t &FixedValue) {
753 int64_t Addend = 0;
754 int Index = 0;
755 int64_t Value = Target.getConstant();
756 const MCSymbol *RelocSymbol = NULL;
757
Rafael Espindola127a6a42010-12-17 07:28:17 +0000758 bool IsPCRel = isFixupKindPCRel(Asm, Fixup.getKind());
Jason W Kim56a39902010-12-06 21:57:34 +0000759 if (!Target.isAbsolute()) {
760 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
761 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
762 RelocSymbol = SymbolToReloc(Asm, Target, *Fragment);
763
764 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
765 const MCSymbol &SymbolB = RefB->getSymbol();
766 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
767 IsPCRel = true;
768
769 // Offset of the symbol in the section
770 int64_t a = Layout.getSymbolOffset(&SDB);
771
772 // Ofeset of the relocation in the section
773 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
774 Value += b - a;
775 }
776
777 if (!RelocSymbol) {
778 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
779 MCFragment *F = SD.getFragment();
780
781 Index = F->getParent()->getOrdinal() + 1;
782
783 // Offset of the symbol in the section
784 Value += Layout.getSymbolOffset(&SD);
785 } else {
786 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
787 WeakrefUsedInReloc.insert(RelocSymbol);
788 else
789 UsedInReloc.insert(RelocSymbol);
790 Index = -1;
791 }
792 Addend = Value;
793 // Compensate for the addend on i386.
794 if (Is64Bit)
795 Value = 0;
796 }
797
798 FixedValue = Value;
799 unsigned Type = GetRelocType(Target, Fixup, IsPCRel,
800 (RelocSymbol != 0), Addend);
801
802 uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
803 Fixup.getOffset();
804
805 if (!HasRelocationAddend) Addend = 0;
806 ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend);
807 Relocations[Fragment->getParent()].push_back(ERE);
808}
809
810
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000811uint64_t
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000812ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
813 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000814 MCSymbolData &SD = Asm.getSymbolData(*S);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000815 return SD.getIndex();
Matt Fleming3565a062010-08-16 18:57:57 +0000816}
817
Rafael Espindola737cd212010-10-05 18:01:23 +0000818static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
Rafael Espindola88182132010-10-27 15:18:17 +0000819 bool Used, bool Renamed) {
Rafael Espindola484291c2010-11-01 14:28:48 +0000820 if (Data.getFlags() & ELF_Other_Weakref)
821 return false;
822
Rafael Espindolabd701182010-10-19 19:31:37 +0000823 if (Used)
824 return true;
825
Rafael Espindola88182132010-10-27 15:18:17 +0000826 if (Renamed)
827 return false;
828
Rafael Espindola737cd212010-10-05 18:01:23 +0000829 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000830
Rafael Espindolad1798862010-10-29 23:09:31 +0000831 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
832 return true;
833
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000834 const MCSymbol &A = Symbol.AliasedSymbol();
Rafael Espindolad1798862010-10-29 23:09:31 +0000835 if (!A.isVariable() && A.isUndefined() && !Data.isCommon())
Rafael Espindolaa6866962010-10-27 14:44:52 +0000836 return false;
837
Rafael Espindola737cd212010-10-05 18:01:23 +0000838 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
839 return false;
840
Rafael Espindolabd701182010-10-19 19:31:37 +0000841 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000842 return false;
843
844 return true;
845}
846
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000847static bool isLocal(const MCSymbolData &Data, bool isSignature,
848 bool isUsedInReloc) {
Rafael Espindola737cd212010-10-05 18:01:23 +0000849 if (Data.isExternal())
850 return false;
851
852 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000853 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000854
855 if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
856 if (isSignature && !isUsedInReloc)
857 return true;
858
Rafael Espindola737cd212010-10-05 18:01:23 +0000859 return false;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000860 }
Rafael Espindola737cd212010-10-05 18:01:23 +0000861
862 return true;
863}
864
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000865void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
866 SectionIndexMapTy &SectionIndexMap) {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000867 unsigned Index = 1;
868 for (MCAssembler::iterator it = Asm.begin(),
869 ie = Asm.end(); it != ie; ++it) {
870 const MCSectionELF &Section =
871 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000872 if (Section.getType() != ELF::SHT_GROUP)
873 continue;
874 SectionIndexMap[&Section] = Index++;
875 }
876
877 for (MCAssembler::iterator it = Asm.begin(),
878 ie = Asm.end(); it != ie; ++it) {
879 const MCSectionELF &Section =
880 static_cast<const MCSectionELF &>(it->getSection());
881 if (Section.getType() == ELF::SHT_GROUP)
882 continue;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000883 SectionIndexMap[&Section] = Index++;
884 }
885}
886
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000887void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000888 const SectionIndexMapTy &SectionIndexMap,
889 RevGroupMapTy RevGroupMap) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000890 // FIXME: Is this the correct place to do this?
891 if (NeedsGOT) {
892 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
893 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
894 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
895 Data.setExternal(true);
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000896 SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindola5c77c162010-10-05 15:48:37 +0000897 }
898
Matt Fleming3565a062010-08-16 18:57:57 +0000899 // Build section lookup table.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000900 int NumRegularSections = Asm.size();
Matt Fleming3565a062010-08-16 18:57:57 +0000901
902 // Index 0 is always the empty string.
903 StringMap<uint64_t> StringIndexMap;
904 StringTable += '\x00';
905
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000906 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000907 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
908 ie = Asm.symbol_end(); it != ie; ++it) {
909 const MCSymbol &Symbol = it->getSymbol();
910
Rafael Espindola484291c2010-11-01 14:28:48 +0000911 bool Used = UsedInReloc.count(&Symbol);
912 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000913 bool isSignature = RevGroupMap.count(&Symbol);
914
915 if (!isInSymtab(Asm, *it,
916 Used || WeakrefUsed || isSignature,
Rafael Espindola88182132010-10-27 15:18:17 +0000917 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000918 continue;
919
Matt Fleming3565a062010-08-16 18:57:57 +0000920 ELFSymbolData MSD;
921 MSD.SymbolData = it;
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000922 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Matt Fleming3565a062010-08-16 18:57:57 +0000923
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000924 // Undefined symbols are global, but this is the first place we
925 // are able to set it.
926 bool Local = isLocal(*it, isSignature, Used);
927 if (!Local && GetBinding(*it) == ELF::STB_LOCAL) {
928 MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
929 SetBinding(*it, ELF::STB_GLOBAL);
930 SetBinding(SD, ELF::STB_GLOBAL);
931 }
932
Rafael Espindola484291c2010-11-01 14:28:48 +0000933 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
934 SetBinding(*it, ELF::STB_WEAK);
935
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000936 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000937 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000938 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000939 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000940 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000941 } else if (RefSymbol.isUndefined()) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000942 if (isSignature && !Used)
943 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
944 else
945 MSD.SectionIndex = ELF::SHN_UNDEF;
Matt Fleming3565a062010-08-16 18:57:57 +0000946 } else {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000947 const MCSectionELF &Section =
948 static_cast<const MCSectionELF&>(RefSymbol.getSection());
949 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000950 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
951 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000952 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000953 }
954
Rafael Espindola88182132010-10-27 15:18:17 +0000955 // The @@@ in symbol version is replaced with @ in undefined symbols and
956 // @@ in defined ones.
957 StringRef Name = Symbol.getName();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000958 SmallString<32> Buf;
959
Rafael Espindola88182132010-10-27 15:18:17 +0000960 size_t Pos = Name.find("@@@");
Rafael Espindola88182132010-10-27 15:18:17 +0000961 if (Pos != StringRef::npos) {
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000962 Buf += Name.substr(0, Pos);
963 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
964 Buf += Name.substr(Pos + Skip);
965 Name = Buf;
Rafael Espindola88182132010-10-27 15:18:17 +0000966 }
967
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000968 uint64_t &Entry = StringIndexMap[Name];
Rafael Espindolaa6866962010-10-27 14:44:52 +0000969 if (!Entry) {
970 Entry = StringTable.size();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000971 StringTable += Name;
Rafael Espindolaa6866962010-10-27 14:44:52 +0000972 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +0000973 }
Rafael Espindolaa6866962010-10-27 14:44:52 +0000974 MSD.StringIndex = Entry;
975 if (MSD.SectionIndex == ELF::SHN_UNDEF)
976 UndefinedSymbolData.push_back(MSD);
977 else if (Local)
978 LocalSymbolData.push_back(MSD);
979 else
980 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +0000981 }
982
983 // Symbols are required to be in lexicographic order.
984 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
985 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
986 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
987
988 // Set the symbol indices. Local symbols must come before all other
989 // symbols with non-local bindings.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000990 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000991 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
992 LocalSymbolData[i].SymbolData->setIndex(Index++);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000993
994 Index += NumRegularSections;
995
Matt Fleming3565a062010-08-16 18:57:57 +0000996 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
997 ExternalSymbolData[i].SymbolData->setIndex(Index++);
998 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
999 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
1000}
1001
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001002void ELFObjectWriter::WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
1003 const MCSectionData &SD) {
Matt Fleming3565a062010-08-16 18:57:57 +00001004 if (!Relocations[&SD].empty()) {
1005 MCContext &Ctx = Asm.getContext();
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001006 const MCSectionELF *RelaSection;
Matt Fleming3565a062010-08-16 18:57:57 +00001007 const MCSectionELF &Section =
1008 static_cast<const MCSectionELF&>(SD.getSection());
1009
1010 const StringRef SectionName = Section.getSectionName();
Benjamin Kramer377a5722010-08-17 17:30:07 +00001011 std::string RelaSectionName = HasRelocationAddend ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +00001012 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +00001013
1014 unsigned EntrySize;
1015 if (HasRelocationAddend)
1016 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
1017 else
1018 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +00001019
Benjamin Kramer377a5722010-08-17 17:30:07 +00001020 RelaSection = Ctx.getELFSection(RelaSectionName, HasRelocationAddend ?
1021 ELF::SHT_RELA : ELF::SHT_REL, 0,
Matt Fleming3565a062010-08-16 18:57:57 +00001022 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001023 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001024
1025 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001026 RelaSD.setAlignment(Is64Bit ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +00001027
1028 MCDataFragment *F = new MCDataFragment(&RelaSD);
1029
1030 WriteRelocationsFragment(Asm, F, &SD);
Matt Fleming3565a062010-08-16 18:57:57 +00001031 }
1032}
1033
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001034void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
1035 uint64_t Flags, uint64_t Address,
1036 uint64_t Offset, uint64_t Size,
1037 uint32_t Link, uint32_t Info,
1038 uint64_t Alignment,
1039 uint64_t EntrySize) {
Matt Fleming3565a062010-08-16 18:57:57 +00001040 Write32(Name); // sh_name: index into string table
1041 Write32(Type); // sh_type
1042 WriteWord(Flags); // sh_flags
1043 WriteWord(Address); // sh_addr
1044 WriteWord(Offset); // sh_offset
1045 WriteWord(Size); // sh_size
1046 Write32(Link); // sh_link
1047 Write32(Info); // sh_info
1048 WriteWord(Alignment); // sh_addralign
1049 WriteWord(EntrySize); // sh_entsize
1050}
1051
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001052void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
1053 MCDataFragment *F,
1054 const MCSectionData *SD) {
Matt Fleming3565a062010-08-16 18:57:57 +00001055 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
1056 // sort by the r_offset just like gnu as does
1057 array_pod_sort(Relocs.begin(), Relocs.end());
1058
1059 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1060 ELFRelocationEntry entry = Relocs[e - i - 1];
1061
Rafael Espindola12203cc2010-11-21 00:48:25 +00001062 if (!entry.Index)
1063 ;
1064 else if (entry.Index < 0)
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001065 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
1066 else
Rafael Espindola12203cc2010-11-21 00:48:25 +00001067 entry.Index += LocalSymbolData.size();
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001068 if (Is64Bit) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001069 String64(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001070
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001071 struct ELF::Elf64_Rela ERE64;
1072 ERE64.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001073 String64(*F, ERE64.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001074
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001075 if (HasRelocationAddend)
1076 String64(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001077 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001078 String32(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001079
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001080 struct ELF::Elf32_Rela ERE32;
1081 ERE32.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001082 String32(*F, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001083
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001084 if (HasRelocationAddend)
1085 String32(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001086 }
Matt Fleming3565a062010-08-16 18:57:57 +00001087 }
1088}
1089
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001090void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
1091 MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001092 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +00001093 MCContext &Ctx = Asm.getContext();
1094 MCDataFragment *F;
1095
Matt Fleming3565a062010-08-16 18:57:57 +00001096 unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
1097
Rafael Espindola38738bf2010-09-22 19:04:41 +00001098 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001099 const MCSectionELF *ShstrtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001100 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001101 SectionKind::getReadOnly());
Rafael Espindola71859c62010-09-16 19:46:31 +00001102 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1103 ShstrtabSD.setAlignment(1);
1104 ShstrtabIndex = Asm.size();
1105
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001106 const MCSectionELF *SymtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001107 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1108 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001109 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001110 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Matt Fleming3565a062010-08-16 18:57:57 +00001111 SymtabSD.setAlignment(Is64Bit ? 8 : 4);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001112 SymbolTableIndex = Asm.size();
1113
1114 MCSectionData *SymtabShndxSD = NULL;
1115
1116 if (NeedsSymtabShndx) {
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001117 const MCSectionELF *SymtabShndxSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001118 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001119 SectionKind::getReadOnly(), 4, "");
Rafael Espindola7be2c332010-10-31 00:16:26 +00001120 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
1121 SymtabShndxSD->setAlignment(4);
1122 }
Matt Fleming3565a062010-08-16 18:57:57 +00001123
Matt Fleming3565a062010-08-16 18:57:57 +00001124 const MCSection *StrtabSection;
1125 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001126 SectionKind::getReadOnly());
Matt Fleming3565a062010-08-16 18:57:57 +00001127 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1128 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +00001129 StringTableIndex = Asm.size();
1130
Rafael Espindolac3c413f2010-09-27 22:04:54 +00001131 WriteRelocations(Asm, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001132
1133 // Symbol table
1134 F = new MCDataFragment(&SymtabSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001135 MCDataFragment *ShndxF = NULL;
1136 if (NeedsSymtabShndx) {
1137 ShndxF = new MCDataFragment(SymtabShndxSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001138 }
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001139 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
Rafael Espindola71859c62010-09-16 19:46:31 +00001140
Matt Fleming3565a062010-08-16 18:57:57 +00001141 F = new MCDataFragment(&StrtabSD);
1142 F->getContents().append(StringTable.begin(), StringTable.end());
Matt Fleming3565a062010-08-16 18:57:57 +00001143
Matt Fleming3565a062010-08-16 18:57:57 +00001144 F = new MCDataFragment(&ShstrtabSD);
1145
Matt Fleming3565a062010-08-16 18:57:57 +00001146 // Section header string table.
1147 //
1148 // The first entry of a string table holds a null character so skip
1149 // section 0.
1150 uint64_t Index = 1;
1151 F->getContents() += '\x00';
1152
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001153 StringMap<uint64_t> SecStringMap;
Matt Fleming3565a062010-08-16 18:57:57 +00001154 for (MCAssembler::const_iterator it = Asm.begin(),
1155 ie = Asm.end(); it != ie; ++it) {
Matt Fleming3565a062010-08-16 18:57:57 +00001156 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +00001157 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola51efe7a2010-09-23 14:14:56 +00001158 // FIXME: We could merge suffixes like in .text and .rela.text.
Matt Fleming3565a062010-08-16 18:57:57 +00001159
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001160 StringRef Name = Section.getSectionName();
1161 if (SecStringMap.count(Name)) {
1162 SectionStringTableIndex[&Section] = SecStringMap[Name];
1163 continue;
1164 }
Matt Fleming3565a062010-08-16 18:57:57 +00001165 // Remember the index into the string table so we can write it
1166 // into the sh_name field of the section header table.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001167 SectionStringTableIndex[&Section] = Index;
1168 SecStringMap[Name] = Index;
Matt Fleming3565a062010-08-16 18:57:57 +00001169
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001170 Index += Name.size() + 1;
1171 F->getContents() += Name;
Matt Fleming3565a062010-08-16 18:57:57 +00001172 F->getContents() += '\x00';
1173 }
Rafael Espindola70703872010-09-30 02:22:20 +00001174}
1175
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001176bool ELFObjectWriter::IsFixupFullyResolved(const MCAssembler &Asm,
1177 const MCValue Target,
1178 bool IsPCRel,
1179 const MCFragment *DF) const {
Rafael Espindola70703872010-09-30 02:22:20 +00001180 // If this is a PCrel relocation, find the section this fixup value is
1181 // relative to.
1182 const MCSection *BaseSection = 0;
1183 if (IsPCRel) {
1184 BaseSection = &DF->getParent()->getSection();
1185 assert(BaseSection);
1186 }
1187
1188 const MCSection *SectionA = 0;
1189 const MCSymbol *SymbolA = 0;
1190 if (const MCSymbolRefExpr *A = Target.getSymA()) {
Rafael Espindola2c920852010-11-16 04:11:46 +00001191 SymbolA = &A->getSymbol();
1192 SectionA = &SymbolA->AliasedSymbol().getSection();
Rafael Espindola70703872010-09-30 02:22:20 +00001193 }
1194
1195 const MCSection *SectionB = 0;
Rafael Espindola12203cc2010-11-21 00:48:25 +00001196 const MCSymbol *SymbolB = 0;
Rafael Espindola70703872010-09-30 02:22:20 +00001197 if (const MCSymbolRefExpr *B = Target.getSymB()) {
Rafael Espindola12203cc2010-11-21 00:48:25 +00001198 SymbolB = &B->getSymbol();
1199 SectionB = &SymbolB->AliasedSymbol().getSection();
Rafael Espindola70703872010-09-30 02:22:20 +00001200 }
1201
1202 if (!BaseSection)
1203 return SectionA == SectionB;
1204
Rafael Espindola12203cc2010-11-21 00:48:25 +00001205 if (SymbolB)
1206 return false;
1207
1208 // Absolute address but PCrel instruction, so we need a relocation.
1209 if (!SymbolA)
1210 return false;
1211
Rafael Espindola2c920852010-11-16 04:11:46 +00001212 // FIXME: This is in here just to match gnu as output. If the two ends
1213 // are in the same section, there is nothing that the linker can do to
1214 // break it.
Rafael Espindola70703872010-09-30 02:22:20 +00001215 const MCSymbolData &DataA = Asm.getSymbolData(*SymbolA);
1216 if (DataA.isExternal())
1217 return false;
1218
Rafael Espindola12203cc2010-11-21 00:48:25 +00001219 return BaseSection == SectionA;
Matt Fleming3565a062010-08-16 18:57:57 +00001220}
1221
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001222void ELFObjectWriter::CreateGroupSections(MCAssembler &Asm,
1223 MCAsmLayout &Layout,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001224 GroupMapTy &GroupMap,
1225 RevGroupMapTy &RevGroupMap) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001226 // Build the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001227 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1228 it != ie; ++it) {
1229 const MCSectionELF &Section =
1230 static_cast<const MCSectionELF&>(it->getSection());
1231 if (!(Section.getFlags() & MCSectionELF::SHF_GROUP))
1232 continue;
1233
1234 const MCSymbol *SignatureSymbol = Section.getGroup();
1235 Asm.getOrCreateSymbolData(*SignatureSymbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001236 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001237 if (!Group) {
1238 Group = Asm.getContext().CreateELFGroupSection();
1239 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1240 Data.setAlignment(4);
1241 MCDataFragment *F = new MCDataFragment(&Data);
1242 String32(*F, ELF::GRP_COMDAT);
1243 }
1244 GroupMap[Group] = SignatureSymbol;
1245 }
1246
1247 // Add sections to the groups
1248 unsigned Index = 1;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001249 unsigned NumGroups = RevGroupMap.size();
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001250 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1251 it != ie; ++it, ++Index) {
1252 const MCSectionELF &Section =
1253 static_cast<const MCSectionELF&>(it->getSection());
1254 if (!(Section.getFlags() & MCSectionELF::SHF_GROUP))
1255 continue;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001256 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001257 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1258 // FIXME: we could use the previous fragment
1259 MCDataFragment *F = new MCDataFragment(&Data);
1260 String32(*F, NumGroups + Index);
1261 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001262}
1263
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001264void ELFObjectWriter::WriteSection(MCAssembler &Asm,
1265 const SectionIndexMapTy &SectionIndexMap,
1266 uint32_t GroupSymbolIndex,
1267 uint64_t Offset, uint64_t Size,
1268 uint64_t Alignment,
1269 const MCSectionELF &Section) {
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001270 uint64_t sh_link = 0;
1271 uint64_t sh_info = 0;
1272
1273 switch(Section.getType()) {
1274 case ELF::SHT_DYNAMIC:
1275 sh_link = SectionStringTableIndex[&Section];
1276 sh_info = 0;
1277 break;
1278
1279 case ELF::SHT_REL:
1280 case ELF::SHT_RELA: {
1281 const MCSectionELF *SymtabSection;
1282 const MCSectionELF *InfoSection;
1283 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
1284 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001285 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001286 sh_link = SectionIndexMap.lookup(SymtabSection);
1287 assert(sh_link && ".symtab not found");
1288
1289 // Remove ".rel" and ".rela" prefixes.
1290 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1291 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
1292
1293 InfoSection = Asm.getContext().getELFSection(SectionName,
1294 ELF::SHT_PROGBITS, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001295 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001296 sh_info = SectionIndexMap.lookup(InfoSection);
1297 break;
1298 }
1299
1300 case ELF::SHT_SYMTAB:
1301 case ELF::SHT_DYNSYM:
1302 sh_link = StringTableIndex;
1303 sh_info = LastLocalSymbolIndex;
1304 break;
1305
1306 case ELF::SHT_SYMTAB_SHNDX:
1307 sh_link = SymbolTableIndex;
1308 break;
1309
1310 case ELF::SHT_PROGBITS:
1311 case ELF::SHT_STRTAB:
1312 case ELF::SHT_NOBITS:
1313 case ELF::SHT_NULL:
1314 case ELF::SHT_ARM_ATTRIBUTES:
1315 // Nothing to do.
1316 break;
1317
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001318 case ELF::SHT_GROUP: {
1319 sh_link = SymbolTableIndex;
1320 sh_info = GroupSymbolIndex;
1321 break;
1322 }
1323
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001324 default:
1325 assert(0 && "FIXME: sh_type value not supported!");
1326 break;
1327 }
1328
1329 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1330 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1331 Alignment, Section.getEntrySize());
1332}
1333
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001334static bool IsELFMetaDataSection(const MCSectionData &SD) {
Rafael Espindolaf8803fe2010-12-06 03:48:09 +00001335 return SD.getOrdinal() == ~UINT32_C(0) &&
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001336 !SD.getSection().isVirtualSection();
1337}
1338
1339static uint64_t DataSectionSize(const MCSectionData &SD) {
1340 uint64_t Ret = 0;
1341 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1342 ++i) {
1343 const MCFragment &F = *i;
1344 assert(F.getKind() == MCFragment::FT_Data);
1345 Ret += cast<MCDataFragment>(F).getContents().size();
1346 }
1347 return Ret;
1348}
1349
1350static uint64_t GetSectionFileSize(const MCAsmLayout &Layout,
1351 const MCSectionData &SD) {
1352 if (IsELFMetaDataSection(SD))
1353 return DataSectionSize(SD);
1354 return Layout.getSectionFileSize(&SD);
1355}
1356
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001357static uint64_t GetSectionAddressSize(const MCAsmLayout &Layout,
1358 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001359 if (IsELFMetaDataSection(SD))
1360 return DataSectionSize(SD);
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001361 return Layout.getSectionAddressSize(&SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001362}
1363
1364static void WriteDataSectionData(ELFObjectWriter *W, const MCSectionData &SD) {
1365 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1366 ++i) {
1367 const MCFragment &F = *i;
1368 assert(F.getKind() == MCFragment::FT_Data);
1369 W->WriteBytes(cast<MCDataFragment>(F).getContents().str());
1370 }
1371}
1372
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001373void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1374 const MCAsmLayout &Layout) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001375 GroupMapTy GroupMap;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001376 RevGroupMapTy RevGroupMap;
1377 CreateGroupSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1378 RevGroupMap);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001379
Rafael Espindolabab2a802010-11-10 21:51:05 +00001380 SectionIndexMapTy SectionIndexMap;
1381
1382 ComputeIndexMap(Asm, SectionIndexMap);
1383
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001384 // Compute symbol table information.
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001385 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap);
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001386
Matt Fleming3565a062010-08-16 18:57:57 +00001387 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001388 const_cast<MCAsmLayout&>(Layout),
1389 SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +00001390
Rafael Espindola1d739a02010-11-10 22:34:07 +00001391 // Update to include the metadata sections.
1392 ComputeIndexMap(Asm, SectionIndexMap);
1393
Matt Fleming3565a062010-08-16 18:57:57 +00001394 // Add 1 for the null section.
1395 unsigned NumSections = Asm.size() + 1;
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001396 uint64_t NaturalAlignment = Is64Bit ? 8 : 4;
1397 uint64_t HeaderSize = Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr);
1398 uint64_t FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001399
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001400 std::vector<const MCSectionELF*> Sections;
1401 Sections.resize(NumSections);
1402
1403 for (SectionIndexMapTy::const_iterator i=
1404 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1405 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
1406 Sections[p.second] = p.first;
1407 }
1408
1409 for (unsigned i = 1; i < NumSections; ++i) {
1410 const MCSectionELF &Section = *Sections[i];
1411 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001412
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001413 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1414
Matt Fleming3565a062010-08-16 18:57:57 +00001415 // Get the size of the section in the output file (including padding).
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001416 FileOff += GetSectionFileSize(Layout, SD);
Matt Fleming3565a062010-08-16 18:57:57 +00001417 }
1418
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001419 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1420
Matt Fleming3565a062010-08-16 18:57:57 +00001421 // Write out the ELF header ...
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001422 WriteHeader(FileOff - HeaderSize, NumSections);
1423
1424 FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001425
1426 // ... then all of the sections ...
1427 DenseMap<const MCSection*, uint64_t> SectionOffsetMap;
1428
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001429 for (unsigned i = 1; i < NumSections; ++i) {
1430 const MCSectionELF &Section = *Sections[i];
1431 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001432
1433 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1434 WriteZeros(Padding);
1435 FileOff += Padding;
1436
Matt Fleming3565a062010-08-16 18:57:57 +00001437 // Remember the offset into the file for this section.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001438 SectionOffsetMap[&Section] = FileOff;
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001439
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001440 FileOff += GetSectionFileSize(Layout, SD);
Matt Fleming3565a062010-08-16 18:57:57 +00001441
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001442 if (IsELFMetaDataSection(SD))
1443 WriteDataSectionData(this, SD);
1444 else
Daniel Dunbar5d2477c2010-12-17 02:45:59 +00001445 Asm.WriteSectionData(&SD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +00001446 }
1447
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001448 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1449 WriteZeros(Padding);
1450 FileOff += Padding;
1451
Matt Fleming3565a062010-08-16 18:57:57 +00001452 // ... and then the section header table.
1453 // Should we align the section header table?
1454 //
1455 // Null section first.
Rafael Espindola7be2c332010-10-31 00:16:26 +00001456 uint64_t FirstSectionSize =
1457 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1458 uint32_t FirstSectionLink =
1459 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1460 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming3565a062010-08-16 18:57:57 +00001461
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001462 for (unsigned i = 1; i < NumSections; ++i) {
1463 const MCSectionELF &Section = *Sections[i];
1464 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1465 uint32_t GroupSymbolIndex;
1466 if (Section.getType() != ELF::SHT_GROUP)
1467 GroupSymbolIndex = 0;
1468 else
1469 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm, GroupMap[&Section]);
Matt Fleming3565a062010-08-16 18:57:57 +00001470
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001471 uint64_t Size = GetSectionAddressSize(Layout, SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001472
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001473 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001474 SectionOffsetMap[&Section], Size,
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001475 SD.getAlignment(), Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001476 }
1477}
1478
Rafael Espindola6024c972010-12-17 17:45:22 +00001479MCObjectWriter *llvm::createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1480 raw_ostream &OS,
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001481 bool Is64Bit,
1482 Triple::OSType OSType,
1483 uint16_t EMachine,
1484 bool IsLittleEndian,
1485 bool HasRelocationAddend) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001486 switch (EMachine) {
1487 case ELF::EM_386:
1488 case ELF::EM_X86_64:
Rafael Espindola31f35782010-12-17 18:01:31 +00001489 return new X86ELFObjectWriter(MOTW, OS, Is64Bit, IsLittleEndian, EMachine,
Jason W Kimd3443e92010-11-15 16:18:39 +00001490 HasRelocationAddend, OSType); break;
1491 case ELF::EM_ARM:
Rafael Espindola31f35782010-12-17 18:01:31 +00001492 return new ARMELFObjectWriter(MOTW, OS, Is64Bit, IsLittleEndian, EMachine,
Jason W Kimd3443e92010-11-15 16:18:39 +00001493 HasRelocationAddend, OSType); break;
Wesley Peck4b047132010-11-21 22:06:28 +00001494 case ELF::EM_MBLAZE:
Rafael Espindola31f35782010-12-17 18:01:31 +00001495 return new MBlazeELFObjectWriter(MOTW, OS, Is64Bit, IsLittleEndian,
1496 EMachine,
Wesley Peck4b047132010-11-21 22:06:28 +00001497 HasRelocationAddend, OSType); break;
Benjamin Kramer32858772010-11-15 19:20:50 +00001498 default: llvm_unreachable("Unsupported architecture"); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001499 }
1500}
1501
1502
1503/// START OF SUBCLASSES for ELFObjectWriter
1504//===- ARMELFObjectWriter -------------------------------------------===//
1505
Rafael Espindola31f35782010-12-17 18:01:31 +00001506ARMELFObjectWriter::ARMELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1507 raw_ostream &_OS, bool _Is64Bit,
Jason W Kimd3443e92010-11-15 16:18:39 +00001508 bool _IsLittleEndian,
1509 uint16_t _EMachine, bool _HasRelocationAddend,
1510 Triple::OSType _OSType)
Rafael Espindola31f35782010-12-17 18:01:31 +00001511 : ELFObjectWriter(MOTW, _OS, _Is64Bit, _IsLittleEndian, _EMachine,
Jason W Kimd3443e92010-11-15 16:18:39 +00001512 _HasRelocationAddend, _OSType)
1513{}
1514
1515ARMELFObjectWriter::~ARMELFObjectWriter()
1516{}
1517
Jason W Kim85fed5e2010-12-01 02:40:06 +00001518unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target,
1519 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001520 bool IsPCRel,
1521 bool IsRelocWithSymbol,
1522 int64_t Addend) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001523 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1524 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
1525
Jason W Kima0871e72010-12-08 23:14:44 +00001526 unsigned Type = 0;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001527 if (IsPCRel) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001528 switch ((unsigned)Fixup.getKind()) {
1529 default: assert(0 && "Unimplemented");
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001530 case FK_Data_4:
1531 switch (Modifier) {
1532 default: llvm_unreachable("Unsupported Modifier");
1533 case MCSymbolRefExpr::VK_None:
1534 Type = ELF::R_ARM_BASE_PREL; break;
1535 case MCSymbolRefExpr::VK_ARM_TLSGD:
1536 assert(0 && "unimplemented"); break;
1537 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
1538 Type = ELF::R_ARM_TLS_IE32;
1539 } break;
1540 case ARM::fixup_arm_branch:
1541 switch (Modifier) {
1542 case MCSymbolRefExpr::VK_ARM_PLT:
1543 Type = ELF::R_ARM_PLT32; break;
1544 default:
1545 Type = ELF::R_ARM_CALL; break;
1546 } break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001547 }
1548 } else {
1549 switch ((unsigned)Fixup.getKind()) {
1550 default: llvm_unreachable("invalid fixup kind!");
Jason W Kima0871e72010-12-08 23:14:44 +00001551 case FK_Data_4:
1552 switch (Modifier) {
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001553 default: llvm_unreachable("Unsupported Modifier"); break;
1554 case MCSymbolRefExpr::VK_ARM_GOT:
1555 Type = ELF::R_ARM_GOT_BREL; break;
1556 case MCSymbolRefExpr::VK_ARM_TLSGD:
1557 Type = ELF::R_ARM_TLS_GD32; break;
Jason W Kimf13743b2010-12-16 03:12:17 +00001558 case MCSymbolRefExpr::VK_ARM_TPOFF:
1559 Type = ELF::R_ARM_TLS_LE32; break;
Jason W Kima0871e72010-12-08 23:14:44 +00001560 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001561 Type = ELF::R_ARM_TLS_IE32; break;
Jason W Kimf13743b2010-12-16 03:12:17 +00001562 case MCSymbolRefExpr::VK_None:
1563 Type = ELF::R_ARM_ABS32; break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001564 case MCSymbolRefExpr::VK_ARM_GOTOFF:
1565 Type = ELF::R_ARM_GOTOFF32; break;
Jason W Kima0871e72010-12-08 23:14:44 +00001566 } break;
Jim Grosbachdff84b02010-12-02 00:28:45 +00001567 case ARM::fixup_arm_ldst_pcrel_12:
Owen Anderson9d63d902010-12-01 19:18:46 +00001568 case ARM::fixup_arm_pcrel_10:
Jim Grosbachdff84b02010-12-02 00:28:45 +00001569 case ARM::fixup_arm_adr_pcrel_12:
Jim Grosbach662a8162010-12-06 23:57:07 +00001570 case ARM::fixup_arm_thumb_bl:
Jim Grosbachb492a7c2010-12-09 19:50:12 +00001571 case ARM::fixup_arm_thumb_cb:
Bill Wendlingb8958b02010-12-08 01:57:09 +00001572 case ARM::fixup_arm_thumb_cp:
Jim Grosbache2467172010-12-10 18:21:33 +00001573 case ARM::fixup_arm_thumb_br:
Jason W Kim85fed5e2010-12-01 02:40:06 +00001574 assert(0 && "Unimplemented"); break;
1575 case ARM::fixup_arm_branch:
Jason W Kimf13743b2010-12-16 03:12:17 +00001576 // FIXME: Differentiate between R_ARM_CALL and
1577 // R_ARM_JUMP24 (latter used for conditional jumps)
Jason W Kima0871e72010-12-08 23:14:44 +00001578 Type = ELF::R_ARM_CALL; break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001579 case ARM::fixup_arm_movt_hi16:
Jason W Kima0871e72010-12-08 23:14:44 +00001580 Type = ELF::R_ARM_MOVT_ABS; break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001581 case ARM::fixup_arm_movw_lo16:
Jason W Kima0871e72010-12-08 23:14:44 +00001582 Type = ELF::R_ARM_MOVW_ABS_NC; break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001583 }
1584 }
1585
1586 if (RelocNeedsGOT(Modifier))
1587 NeedsGOT = true;
Jason W Kima0871e72010-12-08 23:14:44 +00001588
1589 return Type;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001590}
1591
Wesley Peck4b047132010-11-21 22:06:28 +00001592//===- MBlazeELFObjectWriter -------------------------------------------===//
Jason W Kimd3443e92010-11-15 16:18:39 +00001593
Rafael Espindola31f35782010-12-17 18:01:31 +00001594MBlazeELFObjectWriter::MBlazeELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1595 raw_ostream &_OS, bool _Is64Bit,
Wesley Peck4b047132010-11-21 22:06:28 +00001596 bool _IsLittleEndian,
1597 uint16_t _EMachine,
1598 bool _HasRelocationAddend,
1599 Triple::OSType _OSType)
Rafael Espindola31f35782010-12-17 18:01:31 +00001600 : ELFObjectWriter(MOTW, _OS, _Is64Bit, _IsLittleEndian, _EMachine,
Wesley Peck4b047132010-11-21 22:06:28 +00001601 _HasRelocationAddend, _OSType) {
1602}
1603
1604MBlazeELFObjectWriter::~MBlazeELFObjectWriter() {
1605}
1606
Jason W Kim56a39902010-12-06 21:57:34 +00001607unsigned MBlazeELFObjectWriter::GetRelocType(const MCValue &Target,
Wesley Peck4b047132010-11-21 22:06:28 +00001608 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001609 bool IsPCRel,
1610 bool IsRelocWithSymbol,
1611 int64_t Addend) {
Wesley Peck4b047132010-11-21 22:06:28 +00001612 // determine the type of the relocation
1613 unsigned Type;
1614 if (IsPCRel) {
1615 switch ((unsigned)Fixup.getKind()) {
1616 default:
1617 llvm_unreachable("Unimplemented");
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001618 case FK_PCRel_4:
Wesley Peck4b047132010-11-21 22:06:28 +00001619 Type = ELF::R_MICROBLAZE_64_PCREL;
1620 break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001621 case FK_PCRel_2:
Wesley Peck4b047132010-11-21 22:06:28 +00001622 Type = ELF::R_MICROBLAZE_32_PCREL;
1623 break;
1624 }
1625 } else {
1626 switch ((unsigned)Fixup.getKind()) {
1627 default: llvm_unreachable("invalid fixup kind!");
1628 case FK_Data_4:
Jason W Kim56a39902010-12-06 21:57:34 +00001629 Type = ((IsRelocWithSymbol || Addend !=0)
1630 ? ELF::R_MICROBLAZE_32
1631 : ELF::R_MICROBLAZE_64);
Wesley Peck4b047132010-11-21 22:06:28 +00001632 break;
1633 case FK_Data_2:
1634 Type = ELF::R_MICROBLAZE_32;
1635 break;
1636 }
1637 }
Jason W Kim56a39902010-12-06 21:57:34 +00001638 return Type;
Wesley Peck4b047132010-11-21 22:06:28 +00001639}
Jason W Kimd3443e92010-11-15 16:18:39 +00001640
1641//===- X86ELFObjectWriter -------------------------------------------===//
1642
1643
Rafael Espindola31f35782010-12-17 18:01:31 +00001644X86ELFObjectWriter::X86ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1645 raw_ostream &_OS, bool _Is64Bit,
Jason W Kimd3443e92010-11-15 16:18:39 +00001646 bool _IsLittleEndian,
1647 uint16_t _EMachine, bool _HasRelocationAddend,
1648 Triple::OSType _OSType)
Rafael Espindola31f35782010-12-17 18:01:31 +00001649 : ELFObjectWriter(MOTW, _OS, _Is64Bit, _IsLittleEndian, _EMachine,
Jason W Kimd3443e92010-11-15 16:18:39 +00001650 _HasRelocationAddend, _OSType)
1651{}
1652
1653X86ELFObjectWriter::~X86ELFObjectWriter()
1654{}
1655
Jason W Kim56a39902010-12-06 21:57:34 +00001656unsigned X86ELFObjectWriter::GetRelocType(const MCValue &Target,
1657 const MCFixup &Fixup,
1658 bool IsPCRel,
1659 bool IsRelocWithSymbol,
1660 int64_t Addend) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001661 // determine the type of the relocation
1662
Rafael Espindola12203cc2010-11-21 00:48:25 +00001663 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1664 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
Jason W Kimd3443e92010-11-15 16:18:39 +00001665 unsigned Type;
1666 if (Is64Bit) {
1667 if (IsPCRel) {
1668 switch (Modifier) {
1669 default:
1670 llvm_unreachable("Unimplemented");
1671 case MCSymbolRefExpr::VK_None:
1672 Type = ELF::R_X86_64_PC32;
1673 break;
1674 case MCSymbolRefExpr::VK_PLT:
1675 Type = ELF::R_X86_64_PLT32;
1676 break;
1677 case MCSymbolRefExpr::VK_GOTPCREL:
1678 Type = ELF::R_X86_64_GOTPCREL;
1679 break;
1680 case MCSymbolRefExpr::VK_GOTTPOFF:
1681 Type = ELF::R_X86_64_GOTTPOFF;
1682 break;
1683 case MCSymbolRefExpr::VK_TLSGD:
1684 Type = ELF::R_X86_64_TLSGD;
1685 break;
1686 case MCSymbolRefExpr::VK_TLSLD:
1687 Type = ELF::R_X86_64_TLSLD;
1688 break;
1689 }
1690 } else {
1691 switch ((unsigned)Fixup.getKind()) {
1692 default: llvm_unreachable("invalid fixup kind!");
1693 case FK_Data_8: Type = ELF::R_X86_64_64; break;
1694 case X86::reloc_signed_4byte:
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001695 case FK_PCRel_4:
Jason W Kimd3443e92010-11-15 16:18:39 +00001696 assert(isInt<32>(Target.getConstant()));
1697 switch (Modifier) {
1698 default:
1699 llvm_unreachable("Unimplemented");
1700 case MCSymbolRefExpr::VK_None:
1701 Type = ELF::R_X86_64_32S;
1702 break;
1703 case MCSymbolRefExpr::VK_GOT:
1704 Type = ELF::R_X86_64_GOT32;
1705 break;
1706 case MCSymbolRefExpr::VK_GOTPCREL:
1707 Type = ELF::R_X86_64_GOTPCREL;
1708 break;
1709 case MCSymbolRefExpr::VK_TPOFF:
1710 Type = ELF::R_X86_64_TPOFF32;
1711 break;
1712 case MCSymbolRefExpr::VK_DTPOFF:
1713 Type = ELF::R_X86_64_DTPOFF32;
1714 break;
1715 }
1716 break;
1717 case FK_Data_4:
1718 Type = ELF::R_X86_64_32;
1719 break;
1720 case FK_Data_2: Type = ELF::R_X86_64_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001721 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001722 case FK_Data_1: Type = ELF::R_X86_64_8; break;
1723 }
1724 }
1725 } else {
1726 if (IsPCRel) {
1727 switch (Modifier) {
1728 default:
1729 llvm_unreachable("Unimplemented");
1730 case MCSymbolRefExpr::VK_None:
1731 Type = ELF::R_386_PC32;
1732 break;
1733 case MCSymbolRefExpr::VK_PLT:
1734 Type = ELF::R_386_PLT32;
1735 break;
1736 }
1737 } else {
1738 switch ((unsigned)Fixup.getKind()) {
1739 default: llvm_unreachable("invalid fixup kind!");
1740
1741 case X86::reloc_global_offset_table:
1742 Type = ELF::R_386_GOTPC;
1743 break;
1744
1745 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
1746 // instead?
1747 case X86::reloc_signed_4byte:
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001748 case FK_PCRel_4:
Jason W Kimd3443e92010-11-15 16:18:39 +00001749 case FK_Data_4:
1750 switch (Modifier) {
1751 default:
1752 llvm_unreachable("Unimplemented");
1753 case MCSymbolRefExpr::VK_None:
1754 Type = ELF::R_386_32;
1755 break;
1756 case MCSymbolRefExpr::VK_GOT:
1757 Type = ELF::R_386_GOT32;
1758 break;
1759 case MCSymbolRefExpr::VK_GOTOFF:
1760 Type = ELF::R_386_GOTOFF;
1761 break;
1762 case MCSymbolRefExpr::VK_TLSGD:
1763 Type = ELF::R_386_TLS_GD;
1764 break;
1765 case MCSymbolRefExpr::VK_TPOFF:
1766 Type = ELF::R_386_TLS_LE_32;
1767 break;
1768 case MCSymbolRefExpr::VK_INDNTPOFF:
1769 Type = ELF::R_386_TLS_IE;
1770 break;
1771 case MCSymbolRefExpr::VK_NTPOFF:
1772 Type = ELF::R_386_TLS_LE;
1773 break;
1774 case MCSymbolRefExpr::VK_GOTNTPOFF:
1775 Type = ELF::R_386_TLS_GOTIE;
1776 break;
1777 case MCSymbolRefExpr::VK_TLSLDM:
1778 Type = ELF::R_386_TLS_LDM;
1779 break;
1780 case MCSymbolRefExpr::VK_DTPOFF:
1781 Type = ELF::R_386_TLS_LDO_32;
1782 break;
1783 }
1784 break;
1785 case FK_Data_2: Type = ELF::R_386_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001786 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001787 case FK_Data_1: Type = ELF::R_386_8; break;
1788 }
1789 }
1790 }
1791
1792 if (RelocNeedsGOT(Modifier))
1793 NeedsGOT = true;
1794
Jason W Kim56a39902010-12-06 21:57:34 +00001795 return Type;
Matt Fleming3565a062010-08-16 18:57:57 +00001796}