blob: b9804d474b327aae8d0198288b7e933ab8a5c846 [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
Daniel Dunbar1f3662a2010-12-17 04:54:54 +0000336 virtual void WriteRelocationsFragment(const MCAssembler &Asm,
337 MCDataFragment *F,
338 const MCSectionData *SD);
339
340 virtual bool
341 IsSymbolRefDifferenceFullyResolved(const MCAssembler &Asm,
342 const MCSymbolRefExpr *A,
343 const MCSymbolRefExpr *B) const {
344 // FIXME: Implement this!
345 return false;
346 }
Matt Fleming3565a062010-08-16 18:57:57 +0000347
Jason W Kimd3443e92010-11-15 16:18:39 +0000348 virtual bool IsFixupFullyResolved(const MCAssembler &Asm,
Rafael Espindola70703872010-09-30 02:22:20 +0000349 const MCValue Target,
350 bool IsPCRel,
351 const MCFragment *DF) const;
352
Jason W Kimd3443e92010-11-15 16:18:39 +0000353 virtual void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
354 virtual void WriteSection(MCAssembler &Asm,
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000355 const SectionIndexMapTy &SectionIndexMap,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000356 uint32_t GroupSymbolIndex,
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000357 uint64_t Offset, uint64_t Size, uint64_t Alignment,
358 const MCSectionELF &Section);
Jason W Kim56a39902010-12-06 21:57:34 +0000359
360 protected:
361 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
362 bool IsPCRel, bool IsRelocWithSymbol,
363 int64_t Addend) = 0;
364
365 virtual bool isFixupKindPCRel(unsigned Kind) const = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000366 };
367
Jason W Kimd3443e92010-11-15 16:18:39 +0000368 //===- X86ELFObjectWriter -------------------------------------------===//
369
370 class X86ELFObjectWriter : public ELFObjectWriter {
371 public:
372 X86ELFObjectWriter(raw_ostream &_OS, bool _Is64Bit, bool IsLittleEndian,
373 uint16_t _EMachine, bool _HasRelAddend,
374 Triple::OSType _OSType);
Wesley Peck4b047132010-11-21 22:06:28 +0000375
Jason W Kimd3443e92010-11-15 16:18:39 +0000376 virtual ~X86ELFObjectWriter();
Jason W Kim56a39902010-12-06 21:57:34 +0000377 protected:
378 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
379 bool IsPCRel, bool IsRelocWithSymbol,
380 int64_t Addend);
Jason W Kim4a511f02010-11-22 18:41:13 +0000381
Jason W Kim56a39902010-12-06 21:57:34 +0000382 virtual bool isFixupKindPCRel(unsigned Kind) const {
Jason W Kim4a511f02010-11-22 18:41:13 +0000383 switch (Kind) {
384 default:
385 return false;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +0000386 case FK_PCRel_1:
387 case FK_PCRel_2:
388 case FK_PCRel_4:
Jason W Kim4a511f02010-11-22 18:41:13 +0000389 case X86::reloc_riprel_4byte:
390 case X86::reloc_riprel_4byte_movq_load:
391 return true;
392 }
393 }
Jason W Kimd3443e92010-11-15 16:18:39 +0000394 };
395
396
397 //===- ARMELFObjectWriter -------------------------------------------===//
398
399 class ARMELFObjectWriter : public ELFObjectWriter {
400 public:
401 ARMELFObjectWriter(raw_ostream &_OS, bool _Is64Bit, bool IsLittleEndian,
402 uint16_t _EMachine, bool _HasRelAddend,
403 Triple::OSType _OSType);
Wesley Peck4b047132010-11-21 22:06:28 +0000404
Jason W Kimd3443e92010-11-15 16:18:39 +0000405 virtual ~ARMELFObjectWriter();
Jason W Kim85fed5e2010-12-01 02:40:06 +0000406 protected:
Jason W Kim56a39902010-12-06 21:57:34 +0000407 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
408 bool IsPCRel, bool IsRelocWithSymbol,
409 int64_t Addend);
410 virtual bool isFixupKindPCRel(unsigned Kind) const {
Jason W Kim4a511f02010-11-22 18:41:13 +0000411 switch (Kind) {
412 default:
413 return false;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +0000414 case FK_PCRel_1:
415 case FK_PCRel_2:
416 case FK_PCRel_4:
Jim Grosbachdff84b02010-12-02 00:28:45 +0000417 case ARM::fixup_arm_ldst_pcrel_12:
Owen Anderson9d63d902010-12-01 19:18:46 +0000418 case ARM::fixup_arm_pcrel_10:
Jason W Kimccbe0002010-11-22 18:47:05 +0000419 case ARM::fixup_arm_branch:
Jason W Kim4a511f02010-11-22 18:41:13 +0000420 return true;
421 }
422 }
Jason W Kimd3443e92010-11-15 16:18:39 +0000423 };
Wesley Peck4b047132010-11-21 22:06:28 +0000424
425 //===- MBlazeELFObjectWriter -------------------------------------------===//
426
427 class MBlazeELFObjectWriter : public ELFObjectWriter {
428 public:
429 MBlazeELFObjectWriter(raw_ostream &_OS, bool _Is64Bit, bool IsLittleEndian,
430 uint16_t _EMachine, bool _HasRelAddend,
431 Triple::OSType _OSType);
432
433 virtual ~MBlazeELFObjectWriter();
Jason W Kim56a39902010-12-06 21:57:34 +0000434 protected:
435 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
436 bool IsPCRel, bool IsRelocWithSymbol,
437 int64_t Addend);
Jason W Kim4a511f02010-11-22 18:41:13 +0000438
Jason W Kim56a39902010-12-06 21:57:34 +0000439 virtual bool isFixupKindPCRel(unsigned Kind) const {
Jason W Kim4a511f02010-11-22 18:41:13 +0000440 switch (Kind) {
441 default:
442 return false;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +0000443 case FK_PCRel_1:
444 case FK_PCRel_2:
445 case FK_PCRel_4:
Jason W Kim4a511f02010-11-22 18:41:13 +0000446 return true;
447 }
448 }
Wesley Peck4b047132010-11-21 22:06:28 +0000449 };
Matt Fleming3565a062010-08-16 18:57:57 +0000450}
451
Jason W Kimd3443e92010-11-15 16:18:39 +0000452ELFObjectWriter::~ELFObjectWriter()
453{}
454
Matt Fleming3565a062010-08-16 18:57:57 +0000455// Emit the ELF header.
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000456void ELFObjectWriter::WriteHeader(uint64_t SectionDataSize,
457 unsigned NumberOfSections) {
Matt Fleming3565a062010-08-16 18:57:57 +0000458 // ELF Header
459 // ----------
460 //
461 // Note
462 // ----
463 // emitWord method behaves differently for ELF32 and ELF64, writing
464 // 4 bytes in the former and 8 in the latter.
465
466 Write8(0x7f); // e_ident[EI_MAG0]
467 Write8('E'); // e_ident[EI_MAG1]
468 Write8('L'); // e_ident[EI_MAG2]
469 Write8('F'); // e_ident[EI_MAG3]
470
471 Write8(Is64Bit ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
472
473 // e_ident[EI_DATA]
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000474 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
Matt Fleming3565a062010-08-16 18:57:57 +0000475
476 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky5baf79e2010-09-09 17:57:50 +0000477 // e_ident[EI_OSABI]
478 switch (OSType) {
479 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
480 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
481 default: Write8(ELF::ELFOSABI_NONE); break;
482 }
Matt Fleming3565a062010-08-16 18:57:57 +0000483 Write8(0); // e_ident[EI_ABIVERSION]
484
485 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
486
487 Write16(ELF::ET_REL); // e_type
488
Wesley Peckeecb8582010-10-22 15:52:49 +0000489 Write16(EMachine); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +0000490
491 Write32(ELF::EV_CURRENT); // e_version
492 WriteWord(0); // e_entry, no entry point in .o file
493 WriteWord(0); // e_phoff, no program header for .o
Benjamin Kramereb976772010-08-17 17:02:29 +0000494 WriteWord(SectionDataSize + (Is64Bit ? sizeof(ELF::Elf64_Ehdr) :
495 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000496
497 // FIXME: Make this configurable.
498 Write32(0); // e_flags = whatever the target wants
499
500 // e_ehsize = ELF header size
501 Write16(Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
502
503 Write16(0); // e_phentsize = prog header entry size
504 Write16(0); // e_phnum = # prog header entries = 0
505
506 // e_shentsize = Section header entry size
507 Write16(Is64Bit ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
508
509 // e_shnum = # of section header ents
Rafael Espindola7be2c332010-10-31 00:16:26 +0000510 if (NumberOfSections >= ELF::SHN_LORESERVE)
511 Write16(0);
512 else
513 Write16(NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000514
515 // e_shstrndx = Section # of '.shstrtab'
Rafael Espindola7be2c332010-10-31 00:16:26 +0000516 if (NumberOfSections >= ELF::SHN_LORESERVE)
517 Write16(ELF::SHN_XINDEX);
518 else
519 Write16(ShstrtabIndex);
Matt Fleming3565a062010-08-16 18:57:57 +0000520}
521
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000522void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
523 MCDataFragment *ShndxF,
524 uint64_t name,
525 uint8_t info, uint64_t value,
526 uint64_t size, uint8_t other,
527 uint32_t shndx,
528 bool Reserved) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000529 if (ShndxF) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000530 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000531 String32(*ShndxF, shndx);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000532 else
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000533 String32(*ShndxF, 0);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000534 }
535
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000536 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
537 uint16_t(ELF::SHN_XINDEX) : shndx;
538
Matt Fleming3565a062010-08-16 18:57:57 +0000539 if (Is64Bit) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000540 String32(*SymtabF, name); // st_name
541 String8(*SymtabF, info); // st_info
542 String8(*SymtabF, other); // st_other
543 String16(*SymtabF, Index); // st_shndx
544 String64(*SymtabF, value); // st_value
545 String64(*SymtabF, size); // st_size
Matt Fleming3565a062010-08-16 18:57:57 +0000546 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000547 String32(*SymtabF, name); // st_name
548 String32(*SymtabF, value); // st_value
549 String32(*SymtabF, size); // st_size
550 String8(*SymtabF, info); // st_info
551 String8(*SymtabF, other); // st_other
552 String16(*SymtabF, Index); // st_shndx
Matt Fleming3565a062010-08-16 18:57:57 +0000553 }
554}
555
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000556static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout) {
557 if (Data.isCommon() && Data.isExternal())
558 return Data.getCommonAlignment();
559
560 const MCSymbol &Symbol = Data.getSymbol();
561 if (!Symbol.isInSection())
562 return 0;
563
Rafael Espindolaffd902b2010-12-06 02:57:26 +0000564 if (Data.getFragment())
565 return Layout.getSymbolOffset(&Data);
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000566
567 return 0;
568}
569
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000570void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
571 const MCAsmLayout &Layout) {
Rafael Espindola88182132010-10-27 15:18:17 +0000572 // The presence of symbol versions causes undefined symbols and
573 // versions declared with @@@ to be renamed.
574
575 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
576 ie = Asm.symbol_end(); it != ie; ++it) {
577 const MCSymbol &Alias = it->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000578 const MCSymbol &Symbol = Alias.AliasedSymbol();
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000579 MCSymbolData &SD = Asm.getSymbolData(Symbol);
580
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000581 // Not an alias.
582 if (&Symbol == &Alias)
583 continue;
584
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000585 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000586 size_t Pos = AliasName.find('@');
587 if (Pos == StringRef::npos)
588 continue;
589
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000590 // Aliases defined with .symvar copy the binding from the symbol they alias.
591 // This is the first place we are able to copy this information.
592 it->setExternal(SD.isExternal());
593 SetBinding(*it, GetBinding(SD));
594
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000595 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000596 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
597 continue;
598
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000599 // FIXME: produce a better error message.
600 if (Symbol.isUndefined() && Rest.startswith("@@") &&
601 !Rest.startswith("@@@"))
602 report_fatal_error("A @@ version cannot be undefined");
603
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000604 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000605 }
606}
607
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000608void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
609 MCDataFragment *ShndxF,
610 ELFSymbolData &MSD,
611 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000612 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000613 MCSymbolData &Data =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000614 Layout.getAssembler().getSymbolData(OrigData.getSymbol().AliasedSymbol());
Rafael Espindola152c1062010-10-06 21:02:29 +0000615
Rafael Espindola7be2c332010-10-31 00:16:26 +0000616 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
617 Data.getSymbol().isVariable();
618
Rafael Espindola152c1062010-10-06 21:02:29 +0000619 uint8_t Binding = GetBinding(OrigData);
620 uint8_t Visibility = GetVisibility(OrigData);
621 uint8_t Type = GetType(Data);
622
623 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
624 uint8_t Other = Visibility;
625
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000626 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000627 uint64_t Size = 0;
628 const MCExpr *ESize;
629
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000630 assert(!(Data.isCommon() && !Data.isExternal()));
631
Matt Fleming3565a062010-08-16 18:57:57 +0000632 ESize = Data.getSize();
633 if (Data.getSize()) {
634 MCValue Res;
635 if (ESize->getKind() == MCExpr::Binary) {
636 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(ESize);
637
638 if (BE->EvaluateAsRelocatable(Res, &Layout)) {
Benjamin Kramer24f12062010-10-17 07:38:40 +0000639 assert(!Res.getSymA() || !Res.getSymA()->getSymbol().isDefined());
640 assert(!Res.getSymB() || !Res.getSymB()->getSymbol().isDefined());
Rafael Espindolaf230df92010-10-16 18:23:53 +0000641 Size = Res.getConstant();
Matt Fleming3565a062010-08-16 18:57:57 +0000642 }
643 } else if (ESize->getKind() == MCExpr::Constant) {
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000644 Size = static_cast<const MCConstantExpr *>(ESize)->getValue();
Matt Fleming3565a062010-08-16 18:57:57 +0000645 } else {
646 assert(0 && "Unsupported size expression");
647 }
648 }
649
650 // Write out the symbol table entry
Rafael Espindola7be2c332010-10-31 00:16:26 +0000651 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
652 Size, Other, MSD.SectionIndex, IsReserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000653}
654
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000655void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
656 MCDataFragment *ShndxF,
657 const MCAssembler &Asm,
658 const MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000659 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000660 // The string table must be emitted first because we need the index
661 // into the string table for all the symbol names.
662 assert(StringTable.size() && "Missing string table");
663
664 // FIXME: Make sure the start of the symbol table is aligned.
665
666 // The first entry is the undefined symbol entry.
Rafael Espindola7be2c332010-10-31 00:16:26 +0000667 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
Matt Fleming3565a062010-08-16 18:57:57 +0000668
669 // Write the symbol table entries.
670 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
671 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
672 ELFSymbolData &MSD = LocalSymbolData[i];
Rafael Espindola7be2c332010-10-31 00:16:26 +0000673 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000674 }
675
Rafael Espindola71859c62010-09-16 19:46:31 +0000676 // Write out a symbol table entry for each regular section.
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000677 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
678 ++i) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000679 const MCSectionELF &Section =
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000680 static_cast<const MCSectionELF&>(i->getSection());
681 if (Section.getType() == ELF::SHT_RELA ||
682 Section.getType() == ELF::SHT_REL ||
683 Section.getType() == ELF::SHT_STRTAB ||
684 Section.getType() == ELF::SHT_SYMTAB)
Eli Friedmana44fa242010-08-16 21:17:09 +0000685 continue;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000686 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000687 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section), false);
Eli Friedmana44fa242010-08-16 21:17:09 +0000688 LastLocalSymbolIndex++;
689 }
Matt Fleming3565a062010-08-16 18:57:57 +0000690
691 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
692 ELFSymbolData &MSD = ExternalSymbolData[i];
693 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000694 assert(((Data.getFlags() & ELF_STB_Global) ||
695 (Data.getFlags() & ELF_STB_Weak)) &&
696 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000697 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000698 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000699 LastLocalSymbolIndex++;
700 }
701
702 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
703 ELFSymbolData &MSD = UndefinedSymbolData[i];
704 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000705 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000706 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000707 LastLocalSymbolIndex++;
708 }
709}
710
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000711const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm,
712 const MCValue &Target,
713 const MCFragment &F) const {
714 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000715 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
716 const MCSymbol *Renamed = Renames.lookup(&Symbol);
717 const MCSymbolData &SD = Asm.getSymbolData(Symbol);
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000718
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000719 if (ASymbol.isUndefined()) {
720 if (Renamed)
721 return Renamed;
722 return &ASymbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000723 }
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000724
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000725 if (SD.isExternal()) {
726 if (Renamed)
727 return Renamed;
728 return &Symbol;
729 }
Rafael Espindola73ffea42010-09-25 05:42:19 +0000730
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000731 const MCSectionELF &Section =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000732 static_cast<const MCSectionELF&>(ASymbol.getSection());
Rafael Espindola25958732010-11-24 21:57:39 +0000733 const SectionKind secKind = Section.getKind();
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000734
Rafael Espindola25958732010-11-24 21:57:39 +0000735 if (secKind.isBSS())
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000736 return NULL;
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000737
Rafael Espindola25958732010-11-24 21:57:39 +0000738 if (secKind.isThreadLocal()) {
739 if (Renamed)
740 return Renamed;
741 return &Symbol;
742 }
743
Rafael Espindola8cecf252010-10-06 16:23:36 +0000744 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000745 const MCSectionELF &Sec2 =
746 static_cast<const MCSectionELF&>(F.getParent()->getSection());
747
Rafael Espindola8cecf252010-10-06 16:23:36 +0000748 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000749 (Kind == MCSymbolRefExpr::VK_PLT ||
750 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
Rafael Espindola25958732010-11-24 21:57:39 +0000751 Kind == MCSymbolRefExpr::VK_GOTOFF)) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000752 if (Renamed)
753 return Renamed;
754 return &Symbol;
755 }
Rafael Espindola3729d002010-10-05 23:57:26 +0000756
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000757 if (Section.getFlags() & MCSectionELF::SHF_MERGE) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000758 if (Target.getConstant() == 0)
759 return NULL;
760 if (Renamed)
761 return Renamed;
762 return &Symbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000763 }
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000764
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000765 return NULL;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000766}
767
Matt Fleming3565a062010-08-16 18:57:57 +0000768
Jason W Kim56a39902010-12-06 21:57:34 +0000769void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
770 const MCAsmLayout &Layout,
771 const MCFragment *Fragment,
772 const MCFixup &Fixup,
773 MCValue Target,
774 uint64_t &FixedValue) {
775 int64_t Addend = 0;
776 int Index = 0;
777 int64_t Value = Target.getConstant();
778 const MCSymbol *RelocSymbol = NULL;
779
780 bool IsPCRel = isFixupKindPCRel(Fixup.getKind());
781 if (!Target.isAbsolute()) {
782 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
783 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
784 RelocSymbol = SymbolToReloc(Asm, Target, *Fragment);
785
786 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
787 const MCSymbol &SymbolB = RefB->getSymbol();
788 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
789 IsPCRel = true;
790
791 // Offset of the symbol in the section
792 int64_t a = Layout.getSymbolOffset(&SDB);
793
794 // Ofeset of the relocation in the section
795 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
796 Value += b - a;
797 }
798
799 if (!RelocSymbol) {
800 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
801 MCFragment *F = SD.getFragment();
802
803 Index = F->getParent()->getOrdinal() + 1;
804
805 // Offset of the symbol in the section
806 Value += Layout.getSymbolOffset(&SD);
807 } else {
808 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
809 WeakrefUsedInReloc.insert(RelocSymbol);
810 else
811 UsedInReloc.insert(RelocSymbol);
812 Index = -1;
813 }
814 Addend = Value;
815 // Compensate for the addend on i386.
816 if (Is64Bit)
817 Value = 0;
818 }
819
820 FixedValue = Value;
821 unsigned Type = GetRelocType(Target, Fixup, IsPCRel,
822 (RelocSymbol != 0), Addend);
823
824 uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
825 Fixup.getOffset();
826
827 if (!HasRelocationAddend) Addend = 0;
828 ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend);
829 Relocations[Fragment->getParent()].push_back(ERE);
830}
831
832
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000833uint64_t
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000834ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
835 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000836 MCSymbolData &SD = Asm.getSymbolData(*S);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000837 return SD.getIndex();
Matt Fleming3565a062010-08-16 18:57:57 +0000838}
839
Rafael Espindola737cd212010-10-05 18:01:23 +0000840static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
Rafael Espindola88182132010-10-27 15:18:17 +0000841 bool Used, bool Renamed) {
Rafael Espindola484291c2010-11-01 14:28:48 +0000842 if (Data.getFlags() & ELF_Other_Weakref)
843 return false;
844
Rafael Espindolabd701182010-10-19 19:31:37 +0000845 if (Used)
846 return true;
847
Rafael Espindola88182132010-10-27 15:18:17 +0000848 if (Renamed)
849 return false;
850
Rafael Espindola737cd212010-10-05 18:01:23 +0000851 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000852
Rafael Espindolad1798862010-10-29 23:09:31 +0000853 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
854 return true;
855
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000856 const MCSymbol &A = Symbol.AliasedSymbol();
Rafael Espindolad1798862010-10-29 23:09:31 +0000857 if (!A.isVariable() && A.isUndefined() && !Data.isCommon())
Rafael Espindolaa6866962010-10-27 14:44:52 +0000858 return false;
859
Rafael Espindola737cd212010-10-05 18:01:23 +0000860 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
861 return false;
862
Rafael Espindolabd701182010-10-19 19:31:37 +0000863 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000864 return false;
865
866 return true;
867}
868
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000869static bool isLocal(const MCSymbolData &Data, bool isSignature,
870 bool isUsedInReloc) {
Rafael Espindola737cd212010-10-05 18:01:23 +0000871 if (Data.isExternal())
872 return false;
873
874 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000875 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000876
877 if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
878 if (isSignature && !isUsedInReloc)
879 return true;
880
Rafael Espindola737cd212010-10-05 18:01:23 +0000881 return false;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000882 }
Rafael Espindola737cd212010-10-05 18:01:23 +0000883
884 return true;
885}
886
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000887void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
888 SectionIndexMapTy &SectionIndexMap) {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000889 unsigned Index = 1;
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());
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000894 if (Section.getType() != ELF::SHT_GROUP)
895 continue;
896 SectionIndexMap[&Section] = Index++;
897 }
898
899 for (MCAssembler::iterator it = Asm.begin(),
900 ie = Asm.end(); it != ie; ++it) {
901 const MCSectionELF &Section =
902 static_cast<const MCSectionELF &>(it->getSection());
903 if (Section.getType() == ELF::SHT_GROUP)
904 continue;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000905 SectionIndexMap[&Section] = Index++;
906 }
907}
908
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000909void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000910 const SectionIndexMapTy &SectionIndexMap,
911 RevGroupMapTy RevGroupMap) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000912 // FIXME: Is this the correct place to do this?
913 if (NeedsGOT) {
914 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
915 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
916 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
917 Data.setExternal(true);
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000918 SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindola5c77c162010-10-05 15:48:37 +0000919 }
920
Matt Fleming3565a062010-08-16 18:57:57 +0000921 // Build section lookup table.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000922 int NumRegularSections = Asm.size();
Matt Fleming3565a062010-08-16 18:57:57 +0000923
924 // Index 0 is always the empty string.
925 StringMap<uint64_t> StringIndexMap;
926 StringTable += '\x00';
927
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000928 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000929 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
930 ie = Asm.symbol_end(); it != ie; ++it) {
931 const MCSymbol &Symbol = it->getSymbol();
932
Rafael Espindola484291c2010-11-01 14:28:48 +0000933 bool Used = UsedInReloc.count(&Symbol);
934 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000935 bool isSignature = RevGroupMap.count(&Symbol);
936
937 if (!isInSymtab(Asm, *it,
938 Used || WeakrefUsed || isSignature,
Rafael Espindola88182132010-10-27 15:18:17 +0000939 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000940 continue;
941
Matt Fleming3565a062010-08-16 18:57:57 +0000942 ELFSymbolData MSD;
943 MSD.SymbolData = it;
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000944 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Matt Fleming3565a062010-08-16 18:57:57 +0000945
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000946 // Undefined symbols are global, but this is the first place we
947 // are able to set it.
948 bool Local = isLocal(*it, isSignature, Used);
949 if (!Local && GetBinding(*it) == ELF::STB_LOCAL) {
950 MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
951 SetBinding(*it, ELF::STB_GLOBAL);
952 SetBinding(SD, ELF::STB_GLOBAL);
953 }
954
Rafael Espindola484291c2010-11-01 14:28:48 +0000955 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
956 SetBinding(*it, ELF::STB_WEAK);
957
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000958 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000959 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000960 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000961 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000962 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000963 } else if (RefSymbol.isUndefined()) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000964 if (isSignature && !Used)
965 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
966 else
967 MSD.SectionIndex = ELF::SHN_UNDEF;
Matt Fleming3565a062010-08-16 18:57:57 +0000968 } else {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000969 const MCSectionELF &Section =
970 static_cast<const MCSectionELF&>(RefSymbol.getSection());
971 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000972 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
973 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000974 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000975 }
976
Rafael Espindola88182132010-10-27 15:18:17 +0000977 // The @@@ in symbol version is replaced with @ in undefined symbols and
978 // @@ in defined ones.
979 StringRef Name = Symbol.getName();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000980 SmallString<32> Buf;
981
Rafael Espindola88182132010-10-27 15:18:17 +0000982 size_t Pos = Name.find("@@@");
Rafael Espindola88182132010-10-27 15:18:17 +0000983 if (Pos != StringRef::npos) {
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000984 Buf += Name.substr(0, Pos);
985 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
986 Buf += Name.substr(Pos + Skip);
987 Name = Buf;
Rafael Espindola88182132010-10-27 15:18:17 +0000988 }
989
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000990 uint64_t &Entry = StringIndexMap[Name];
Rafael Espindolaa6866962010-10-27 14:44:52 +0000991 if (!Entry) {
992 Entry = StringTable.size();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000993 StringTable += Name;
Rafael Espindolaa6866962010-10-27 14:44:52 +0000994 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +0000995 }
Rafael Espindolaa6866962010-10-27 14:44:52 +0000996 MSD.StringIndex = Entry;
997 if (MSD.SectionIndex == ELF::SHN_UNDEF)
998 UndefinedSymbolData.push_back(MSD);
999 else if (Local)
1000 LocalSymbolData.push_back(MSD);
1001 else
1002 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +00001003 }
1004
1005 // Symbols are required to be in lexicographic order.
1006 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
1007 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
1008 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
1009
1010 // Set the symbol indices. Local symbols must come before all other
1011 // symbols with non-local bindings.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +00001012 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +00001013 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
1014 LocalSymbolData[i].SymbolData->setIndex(Index++);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +00001015
1016 Index += NumRegularSections;
1017
Matt Fleming3565a062010-08-16 18:57:57 +00001018 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
1019 ExternalSymbolData[i].SymbolData->setIndex(Index++);
1020 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
1021 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
1022}
1023
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001024void ELFObjectWriter::WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
1025 const MCSectionData &SD) {
Matt Fleming3565a062010-08-16 18:57:57 +00001026 if (!Relocations[&SD].empty()) {
1027 MCContext &Ctx = Asm.getContext();
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001028 const MCSectionELF *RelaSection;
Matt Fleming3565a062010-08-16 18:57:57 +00001029 const MCSectionELF &Section =
1030 static_cast<const MCSectionELF&>(SD.getSection());
1031
1032 const StringRef SectionName = Section.getSectionName();
Benjamin Kramer377a5722010-08-17 17:30:07 +00001033 std::string RelaSectionName = HasRelocationAddend ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +00001034 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +00001035
1036 unsigned EntrySize;
1037 if (HasRelocationAddend)
1038 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
1039 else
1040 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +00001041
Benjamin Kramer377a5722010-08-17 17:30:07 +00001042 RelaSection = Ctx.getELFSection(RelaSectionName, HasRelocationAddend ?
1043 ELF::SHT_RELA : ELF::SHT_REL, 0,
Matt Fleming3565a062010-08-16 18:57:57 +00001044 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001045 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001046
1047 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001048 RelaSD.setAlignment(Is64Bit ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +00001049
1050 MCDataFragment *F = new MCDataFragment(&RelaSD);
1051
1052 WriteRelocationsFragment(Asm, F, &SD);
Matt Fleming3565a062010-08-16 18:57:57 +00001053 }
1054}
1055
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001056void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
1057 uint64_t Flags, uint64_t Address,
1058 uint64_t Offset, uint64_t Size,
1059 uint32_t Link, uint32_t Info,
1060 uint64_t Alignment,
1061 uint64_t EntrySize) {
Matt Fleming3565a062010-08-16 18:57:57 +00001062 Write32(Name); // sh_name: index into string table
1063 Write32(Type); // sh_type
1064 WriteWord(Flags); // sh_flags
1065 WriteWord(Address); // sh_addr
1066 WriteWord(Offset); // sh_offset
1067 WriteWord(Size); // sh_size
1068 Write32(Link); // sh_link
1069 Write32(Info); // sh_info
1070 WriteWord(Alignment); // sh_addralign
1071 WriteWord(EntrySize); // sh_entsize
1072}
1073
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001074void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
1075 MCDataFragment *F,
1076 const MCSectionData *SD) {
Matt Fleming3565a062010-08-16 18:57:57 +00001077 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
1078 // sort by the r_offset just like gnu as does
1079 array_pod_sort(Relocs.begin(), Relocs.end());
1080
1081 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1082 ELFRelocationEntry entry = Relocs[e - i - 1];
1083
Rafael Espindola12203cc2010-11-21 00:48:25 +00001084 if (!entry.Index)
1085 ;
1086 else if (entry.Index < 0)
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001087 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
1088 else
Rafael Espindola12203cc2010-11-21 00:48:25 +00001089 entry.Index += LocalSymbolData.size();
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001090 if (Is64Bit) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001091 String64(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001092
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001093 struct ELF::Elf64_Rela ERE64;
1094 ERE64.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001095 String64(*F, ERE64.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001096
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001097 if (HasRelocationAddend)
1098 String64(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001099 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001100 String32(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001101
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001102 struct ELF::Elf32_Rela ERE32;
1103 ERE32.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001104 String32(*F, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001105
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001106 if (HasRelocationAddend)
1107 String32(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001108 }
Matt Fleming3565a062010-08-16 18:57:57 +00001109 }
1110}
1111
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001112void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
1113 MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001114 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +00001115 MCContext &Ctx = Asm.getContext();
1116 MCDataFragment *F;
1117
Matt Fleming3565a062010-08-16 18:57:57 +00001118 unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
1119
Rafael Espindola38738bf2010-09-22 19:04:41 +00001120 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001121 const MCSectionELF *ShstrtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001122 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001123 SectionKind::getReadOnly());
Rafael Espindola71859c62010-09-16 19:46:31 +00001124 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1125 ShstrtabSD.setAlignment(1);
1126 ShstrtabIndex = Asm.size();
1127
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001128 const MCSectionELF *SymtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001129 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1130 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001131 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001132 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Matt Fleming3565a062010-08-16 18:57:57 +00001133 SymtabSD.setAlignment(Is64Bit ? 8 : 4);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001134 SymbolTableIndex = Asm.size();
1135
1136 MCSectionData *SymtabShndxSD = NULL;
1137
1138 if (NeedsSymtabShndx) {
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001139 const MCSectionELF *SymtabShndxSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001140 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001141 SectionKind::getReadOnly(), 4, "");
Rafael Espindola7be2c332010-10-31 00:16:26 +00001142 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
1143 SymtabShndxSD->setAlignment(4);
1144 }
Matt Fleming3565a062010-08-16 18:57:57 +00001145
Matt Fleming3565a062010-08-16 18:57:57 +00001146 const MCSection *StrtabSection;
1147 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001148 SectionKind::getReadOnly());
Matt Fleming3565a062010-08-16 18:57:57 +00001149 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1150 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +00001151 StringTableIndex = Asm.size();
1152
Rafael Espindolac3c413f2010-09-27 22:04:54 +00001153 WriteRelocations(Asm, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001154
1155 // Symbol table
1156 F = new MCDataFragment(&SymtabSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001157 MCDataFragment *ShndxF = NULL;
1158 if (NeedsSymtabShndx) {
1159 ShndxF = new MCDataFragment(SymtabShndxSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001160 }
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001161 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
Rafael Espindola71859c62010-09-16 19:46:31 +00001162
Matt Fleming3565a062010-08-16 18:57:57 +00001163 F = new MCDataFragment(&StrtabSD);
1164 F->getContents().append(StringTable.begin(), StringTable.end());
Matt Fleming3565a062010-08-16 18:57:57 +00001165
Matt Fleming3565a062010-08-16 18:57:57 +00001166 F = new MCDataFragment(&ShstrtabSD);
1167
Matt Fleming3565a062010-08-16 18:57:57 +00001168 // Section header string table.
1169 //
1170 // The first entry of a string table holds a null character so skip
1171 // section 0.
1172 uint64_t Index = 1;
1173 F->getContents() += '\x00';
1174
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001175 StringMap<uint64_t> SecStringMap;
Matt Fleming3565a062010-08-16 18:57:57 +00001176 for (MCAssembler::const_iterator it = Asm.begin(),
1177 ie = Asm.end(); it != ie; ++it) {
Matt Fleming3565a062010-08-16 18:57:57 +00001178 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +00001179 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola51efe7a2010-09-23 14:14:56 +00001180 // FIXME: We could merge suffixes like in .text and .rela.text.
Matt Fleming3565a062010-08-16 18:57:57 +00001181
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001182 StringRef Name = Section.getSectionName();
1183 if (SecStringMap.count(Name)) {
1184 SectionStringTableIndex[&Section] = SecStringMap[Name];
1185 continue;
1186 }
Matt Fleming3565a062010-08-16 18:57:57 +00001187 // Remember the index into the string table so we can write it
1188 // into the sh_name field of the section header table.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001189 SectionStringTableIndex[&Section] = Index;
1190 SecStringMap[Name] = Index;
Matt Fleming3565a062010-08-16 18:57:57 +00001191
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001192 Index += Name.size() + 1;
1193 F->getContents() += Name;
Matt Fleming3565a062010-08-16 18:57:57 +00001194 F->getContents() += '\x00';
1195 }
Rafael Espindola70703872010-09-30 02:22:20 +00001196}
1197
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001198bool ELFObjectWriter::IsFixupFullyResolved(const MCAssembler &Asm,
1199 const MCValue Target,
1200 bool IsPCRel,
1201 const MCFragment *DF) const {
Rafael Espindola70703872010-09-30 02:22:20 +00001202 // If this is a PCrel relocation, find the section this fixup value is
1203 // relative to.
1204 const MCSection *BaseSection = 0;
1205 if (IsPCRel) {
1206 BaseSection = &DF->getParent()->getSection();
1207 assert(BaseSection);
1208 }
1209
1210 const MCSection *SectionA = 0;
1211 const MCSymbol *SymbolA = 0;
1212 if (const MCSymbolRefExpr *A = Target.getSymA()) {
Rafael Espindola2c920852010-11-16 04:11:46 +00001213 SymbolA = &A->getSymbol();
1214 SectionA = &SymbolA->AliasedSymbol().getSection();
Rafael Espindola70703872010-09-30 02:22:20 +00001215 }
1216
1217 const MCSection *SectionB = 0;
Rafael Espindola12203cc2010-11-21 00:48:25 +00001218 const MCSymbol *SymbolB = 0;
Rafael Espindola70703872010-09-30 02:22:20 +00001219 if (const MCSymbolRefExpr *B = Target.getSymB()) {
Rafael Espindola12203cc2010-11-21 00:48:25 +00001220 SymbolB = &B->getSymbol();
1221 SectionB = &SymbolB->AliasedSymbol().getSection();
Rafael Espindola70703872010-09-30 02:22:20 +00001222 }
1223
1224 if (!BaseSection)
1225 return SectionA == SectionB;
1226
Rafael Espindola12203cc2010-11-21 00:48:25 +00001227 if (SymbolB)
1228 return false;
1229
1230 // Absolute address but PCrel instruction, so we need a relocation.
1231 if (!SymbolA)
1232 return false;
1233
Rafael Espindola2c920852010-11-16 04:11:46 +00001234 // FIXME: This is in here just to match gnu as output. If the two ends
1235 // are in the same section, there is nothing that the linker can do to
1236 // break it.
Rafael Espindola70703872010-09-30 02:22:20 +00001237 const MCSymbolData &DataA = Asm.getSymbolData(*SymbolA);
1238 if (DataA.isExternal())
1239 return false;
1240
Rafael Espindola12203cc2010-11-21 00:48:25 +00001241 return BaseSection == SectionA;
Matt Fleming3565a062010-08-16 18:57:57 +00001242}
1243
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001244void ELFObjectWriter::CreateGroupSections(MCAssembler &Asm,
1245 MCAsmLayout &Layout,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001246 GroupMapTy &GroupMap,
1247 RevGroupMapTy &RevGroupMap) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001248 // Build the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001249 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1250 it != ie; ++it) {
1251 const MCSectionELF &Section =
1252 static_cast<const MCSectionELF&>(it->getSection());
1253 if (!(Section.getFlags() & MCSectionELF::SHF_GROUP))
1254 continue;
1255
1256 const MCSymbol *SignatureSymbol = Section.getGroup();
1257 Asm.getOrCreateSymbolData(*SignatureSymbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001258 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001259 if (!Group) {
1260 Group = Asm.getContext().CreateELFGroupSection();
1261 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1262 Data.setAlignment(4);
1263 MCDataFragment *F = new MCDataFragment(&Data);
1264 String32(*F, ELF::GRP_COMDAT);
1265 }
1266 GroupMap[Group] = SignatureSymbol;
1267 }
1268
1269 // Add sections to the groups
1270 unsigned Index = 1;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001271 unsigned NumGroups = RevGroupMap.size();
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001272 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1273 it != ie; ++it, ++Index) {
1274 const MCSectionELF &Section =
1275 static_cast<const MCSectionELF&>(it->getSection());
1276 if (!(Section.getFlags() & MCSectionELF::SHF_GROUP))
1277 continue;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001278 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001279 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1280 // FIXME: we could use the previous fragment
1281 MCDataFragment *F = new MCDataFragment(&Data);
1282 String32(*F, NumGroups + Index);
1283 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001284}
1285
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001286void ELFObjectWriter::WriteSection(MCAssembler &Asm,
1287 const SectionIndexMapTy &SectionIndexMap,
1288 uint32_t GroupSymbolIndex,
1289 uint64_t Offset, uint64_t Size,
1290 uint64_t Alignment,
1291 const MCSectionELF &Section) {
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001292 uint64_t sh_link = 0;
1293 uint64_t sh_info = 0;
1294
1295 switch(Section.getType()) {
1296 case ELF::SHT_DYNAMIC:
1297 sh_link = SectionStringTableIndex[&Section];
1298 sh_info = 0;
1299 break;
1300
1301 case ELF::SHT_REL:
1302 case ELF::SHT_RELA: {
1303 const MCSectionELF *SymtabSection;
1304 const MCSectionELF *InfoSection;
1305 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
1306 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001307 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001308 sh_link = SectionIndexMap.lookup(SymtabSection);
1309 assert(sh_link && ".symtab not found");
1310
1311 // Remove ".rel" and ".rela" prefixes.
1312 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1313 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
1314
1315 InfoSection = Asm.getContext().getELFSection(SectionName,
1316 ELF::SHT_PROGBITS, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001317 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001318 sh_info = SectionIndexMap.lookup(InfoSection);
1319 break;
1320 }
1321
1322 case ELF::SHT_SYMTAB:
1323 case ELF::SHT_DYNSYM:
1324 sh_link = StringTableIndex;
1325 sh_info = LastLocalSymbolIndex;
1326 break;
1327
1328 case ELF::SHT_SYMTAB_SHNDX:
1329 sh_link = SymbolTableIndex;
1330 break;
1331
1332 case ELF::SHT_PROGBITS:
1333 case ELF::SHT_STRTAB:
1334 case ELF::SHT_NOBITS:
1335 case ELF::SHT_NULL:
1336 case ELF::SHT_ARM_ATTRIBUTES:
1337 // Nothing to do.
1338 break;
1339
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001340 case ELF::SHT_GROUP: {
1341 sh_link = SymbolTableIndex;
1342 sh_info = GroupSymbolIndex;
1343 break;
1344 }
1345
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001346 default:
1347 assert(0 && "FIXME: sh_type value not supported!");
1348 break;
1349 }
1350
1351 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1352 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1353 Alignment, Section.getEntrySize());
1354}
1355
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001356static bool IsELFMetaDataSection(const MCSectionData &SD) {
Rafael Espindolaf8803fe2010-12-06 03:48:09 +00001357 return SD.getOrdinal() == ~UINT32_C(0) &&
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001358 !SD.getSection().isVirtualSection();
1359}
1360
1361static uint64_t DataSectionSize(const MCSectionData &SD) {
1362 uint64_t Ret = 0;
1363 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1364 ++i) {
1365 const MCFragment &F = *i;
1366 assert(F.getKind() == MCFragment::FT_Data);
1367 Ret += cast<MCDataFragment>(F).getContents().size();
1368 }
1369 return Ret;
1370}
1371
1372static uint64_t GetSectionFileSize(const MCAsmLayout &Layout,
1373 const MCSectionData &SD) {
1374 if (IsELFMetaDataSection(SD))
1375 return DataSectionSize(SD);
1376 return Layout.getSectionFileSize(&SD);
1377}
1378
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001379static uint64_t GetSectionAddressSize(const MCAsmLayout &Layout,
1380 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001381 if (IsELFMetaDataSection(SD))
1382 return DataSectionSize(SD);
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001383 return Layout.getSectionAddressSize(&SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001384}
1385
1386static void WriteDataSectionData(ELFObjectWriter *W, const MCSectionData &SD) {
1387 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1388 ++i) {
1389 const MCFragment &F = *i;
1390 assert(F.getKind() == MCFragment::FT_Data);
1391 W->WriteBytes(cast<MCDataFragment>(F).getContents().str());
1392 }
1393}
1394
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001395void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1396 const MCAsmLayout &Layout) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001397 GroupMapTy GroupMap;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001398 RevGroupMapTy RevGroupMap;
1399 CreateGroupSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1400 RevGroupMap);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001401
Rafael Espindolabab2a802010-11-10 21:51:05 +00001402 SectionIndexMapTy SectionIndexMap;
1403
1404 ComputeIndexMap(Asm, SectionIndexMap);
1405
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001406 // Compute symbol table information.
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001407 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap);
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001408
Matt Fleming3565a062010-08-16 18:57:57 +00001409 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001410 const_cast<MCAsmLayout&>(Layout),
1411 SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +00001412
Rafael Espindola1d739a02010-11-10 22:34:07 +00001413 // Update to include the metadata sections.
1414 ComputeIndexMap(Asm, SectionIndexMap);
1415
Matt Fleming3565a062010-08-16 18:57:57 +00001416 // Add 1 for the null section.
1417 unsigned NumSections = Asm.size() + 1;
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001418 uint64_t NaturalAlignment = Is64Bit ? 8 : 4;
1419 uint64_t HeaderSize = Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr);
1420 uint64_t FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001421
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001422 std::vector<const MCSectionELF*> Sections;
1423 Sections.resize(NumSections);
1424
1425 for (SectionIndexMapTy::const_iterator i=
1426 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1427 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
1428 Sections[p.second] = p.first;
1429 }
1430
1431 for (unsigned i = 1; i < NumSections; ++i) {
1432 const MCSectionELF &Section = *Sections[i];
1433 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001434
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001435 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1436
Matt Fleming3565a062010-08-16 18:57:57 +00001437 // Get the size of the section in the output file (including padding).
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001438 FileOff += GetSectionFileSize(Layout, SD);
Matt Fleming3565a062010-08-16 18:57:57 +00001439 }
1440
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001441 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1442
Matt Fleming3565a062010-08-16 18:57:57 +00001443 // Write out the ELF header ...
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001444 WriteHeader(FileOff - HeaderSize, NumSections);
1445
1446 FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001447
1448 // ... then all of the sections ...
1449 DenseMap<const MCSection*, uint64_t> SectionOffsetMap;
1450
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001451 for (unsigned i = 1; i < NumSections; ++i) {
1452 const MCSectionELF &Section = *Sections[i];
1453 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001454
1455 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1456 WriteZeros(Padding);
1457 FileOff += Padding;
1458
Matt Fleming3565a062010-08-16 18:57:57 +00001459 // Remember the offset into the file for this section.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001460 SectionOffsetMap[&Section] = FileOff;
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001461
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001462 FileOff += GetSectionFileSize(Layout, SD);
Matt Fleming3565a062010-08-16 18:57:57 +00001463
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001464 if (IsELFMetaDataSection(SD))
1465 WriteDataSectionData(this, SD);
1466 else
Daniel Dunbar5d2477c2010-12-17 02:45:59 +00001467 Asm.WriteSectionData(&SD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +00001468 }
1469
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001470 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1471 WriteZeros(Padding);
1472 FileOff += Padding;
1473
Matt Fleming3565a062010-08-16 18:57:57 +00001474 // ... and then the section header table.
1475 // Should we align the section header table?
1476 //
1477 // Null section first.
Rafael Espindola7be2c332010-10-31 00:16:26 +00001478 uint64_t FirstSectionSize =
1479 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1480 uint32_t FirstSectionLink =
1481 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1482 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming3565a062010-08-16 18:57:57 +00001483
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001484 for (unsigned i = 1; i < NumSections; ++i) {
1485 const MCSectionELF &Section = *Sections[i];
1486 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1487 uint32_t GroupSymbolIndex;
1488 if (Section.getType() != ELF::SHT_GROUP)
1489 GroupSymbolIndex = 0;
1490 else
1491 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm, GroupMap[&Section]);
Matt Fleming3565a062010-08-16 18:57:57 +00001492
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001493 uint64_t Size = GetSectionAddressSize(Layout, SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001494
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001495 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001496 SectionOffsetMap[&Section], Size,
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001497 SD.getAlignment(), Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001498 }
1499}
1500
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001501MCObjectWriter *llvm::createELFObjectWriter(raw_ostream &OS,
1502 bool Is64Bit,
1503 Triple::OSType OSType,
1504 uint16_t EMachine,
1505 bool IsLittleEndian,
1506 bool HasRelocationAddend) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001507 switch (EMachine) {
1508 case ELF::EM_386:
1509 case ELF::EM_X86_64:
1510 return new X86ELFObjectWriter(OS, Is64Bit, IsLittleEndian, EMachine,
1511 HasRelocationAddend, OSType); break;
1512 case ELF::EM_ARM:
1513 return new ARMELFObjectWriter(OS, Is64Bit, IsLittleEndian, EMachine,
1514 HasRelocationAddend, OSType); break;
Wesley Peck4b047132010-11-21 22:06:28 +00001515 case ELF::EM_MBLAZE:
1516 return new MBlazeELFObjectWriter(OS, Is64Bit, IsLittleEndian, EMachine,
1517 HasRelocationAddend, OSType); break;
Benjamin Kramer32858772010-11-15 19:20:50 +00001518 default: llvm_unreachable("Unsupported architecture"); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001519 }
1520}
1521
1522
1523/// START OF SUBCLASSES for ELFObjectWriter
1524//===- ARMELFObjectWriter -------------------------------------------===//
1525
1526ARMELFObjectWriter::ARMELFObjectWriter(raw_ostream &_OS, bool _Is64Bit,
1527 bool _IsLittleEndian,
1528 uint16_t _EMachine, bool _HasRelocationAddend,
1529 Triple::OSType _OSType)
1530 : ELFObjectWriter(_OS, _Is64Bit, _IsLittleEndian, _EMachine,
1531 _HasRelocationAddend, _OSType)
1532{}
1533
1534ARMELFObjectWriter::~ARMELFObjectWriter()
1535{}
1536
Jason W Kim85fed5e2010-12-01 02:40:06 +00001537unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target,
1538 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001539 bool IsPCRel,
1540 bool IsRelocWithSymbol,
1541 int64_t Addend) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001542 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1543 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
1544
Jason W Kima0871e72010-12-08 23:14:44 +00001545 unsigned Type = 0;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001546 if (IsPCRel) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001547 switch ((unsigned)Fixup.getKind()) {
1548 default: assert(0 && "Unimplemented");
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001549 case FK_Data_4:
1550 switch (Modifier) {
1551 default: llvm_unreachable("Unsupported Modifier");
1552 case MCSymbolRefExpr::VK_None:
1553 Type = ELF::R_ARM_BASE_PREL; break;
1554 case MCSymbolRefExpr::VK_ARM_TLSGD:
1555 assert(0 && "unimplemented"); break;
1556 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
1557 Type = ELF::R_ARM_TLS_IE32;
1558 } break;
1559 case ARM::fixup_arm_branch:
1560 switch (Modifier) {
1561 case MCSymbolRefExpr::VK_ARM_PLT:
1562 Type = ELF::R_ARM_PLT32; break;
1563 default:
1564 Type = ELF::R_ARM_CALL; break;
1565 } break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001566 }
1567 } else {
1568 switch ((unsigned)Fixup.getKind()) {
1569 default: llvm_unreachable("invalid fixup kind!");
Jason W Kima0871e72010-12-08 23:14:44 +00001570 case FK_Data_4:
1571 switch (Modifier) {
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001572 default: llvm_unreachable("Unsupported Modifier"); break;
1573 case MCSymbolRefExpr::VK_ARM_GOT:
1574 Type = ELF::R_ARM_GOT_BREL; break;
1575 case MCSymbolRefExpr::VK_ARM_TLSGD:
1576 Type = ELF::R_ARM_TLS_GD32; break;
Jason W Kimf13743b2010-12-16 03:12:17 +00001577 case MCSymbolRefExpr::VK_ARM_TPOFF:
1578 Type = ELF::R_ARM_TLS_LE32; break;
Jason W Kima0871e72010-12-08 23:14:44 +00001579 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001580 Type = ELF::R_ARM_TLS_IE32; break;
Jason W Kimf13743b2010-12-16 03:12:17 +00001581 case MCSymbolRefExpr::VK_None:
1582 Type = ELF::R_ARM_ABS32; break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001583 case MCSymbolRefExpr::VK_ARM_GOTOFF:
1584 Type = ELF::R_ARM_GOTOFF32; break;
Jason W Kima0871e72010-12-08 23:14:44 +00001585 } break;
Jim Grosbachdff84b02010-12-02 00:28:45 +00001586 case ARM::fixup_arm_ldst_pcrel_12:
Owen Anderson9d63d902010-12-01 19:18:46 +00001587 case ARM::fixup_arm_pcrel_10:
Jim Grosbachdff84b02010-12-02 00:28:45 +00001588 case ARM::fixup_arm_adr_pcrel_12:
Jim Grosbach662a8162010-12-06 23:57:07 +00001589 case ARM::fixup_arm_thumb_bl:
Jim Grosbachb492a7c2010-12-09 19:50:12 +00001590 case ARM::fixup_arm_thumb_cb:
Bill Wendlingb8958b02010-12-08 01:57:09 +00001591 case ARM::fixup_arm_thumb_cp:
Jim Grosbache2467172010-12-10 18:21:33 +00001592 case ARM::fixup_arm_thumb_br:
Jason W Kim85fed5e2010-12-01 02:40:06 +00001593 assert(0 && "Unimplemented"); break;
1594 case ARM::fixup_arm_branch:
Jason W Kimf13743b2010-12-16 03:12:17 +00001595 // FIXME: Differentiate between R_ARM_CALL and
1596 // R_ARM_JUMP24 (latter used for conditional jumps)
Jason W Kima0871e72010-12-08 23:14:44 +00001597 Type = ELF::R_ARM_CALL; break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001598 case ARM::fixup_arm_movt_hi16:
Jason W Kima0871e72010-12-08 23:14:44 +00001599 Type = ELF::R_ARM_MOVT_ABS; break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001600 case ARM::fixup_arm_movw_lo16:
Jason W Kima0871e72010-12-08 23:14:44 +00001601 Type = ELF::R_ARM_MOVW_ABS_NC; break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001602 }
1603 }
1604
1605 if (RelocNeedsGOT(Modifier))
1606 NeedsGOT = true;
Jason W Kima0871e72010-12-08 23:14:44 +00001607
1608 return Type;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001609}
1610
Wesley Peck4b047132010-11-21 22:06:28 +00001611//===- MBlazeELFObjectWriter -------------------------------------------===//
Jason W Kimd3443e92010-11-15 16:18:39 +00001612
Wesley Peck4b047132010-11-21 22:06:28 +00001613MBlazeELFObjectWriter::MBlazeELFObjectWriter(raw_ostream &_OS, bool _Is64Bit,
1614 bool _IsLittleEndian,
1615 uint16_t _EMachine,
1616 bool _HasRelocationAddend,
1617 Triple::OSType _OSType)
1618 : ELFObjectWriter(_OS, _Is64Bit, _IsLittleEndian, _EMachine,
1619 _HasRelocationAddend, _OSType) {
1620}
1621
1622MBlazeELFObjectWriter::~MBlazeELFObjectWriter() {
1623}
1624
Jason W Kim56a39902010-12-06 21:57:34 +00001625unsigned MBlazeELFObjectWriter::GetRelocType(const MCValue &Target,
Wesley Peck4b047132010-11-21 22:06:28 +00001626 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001627 bool IsPCRel,
1628 bool IsRelocWithSymbol,
1629 int64_t Addend) {
Wesley Peck4b047132010-11-21 22:06:28 +00001630 // determine the type of the relocation
1631 unsigned Type;
1632 if (IsPCRel) {
1633 switch ((unsigned)Fixup.getKind()) {
1634 default:
1635 llvm_unreachable("Unimplemented");
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001636 case FK_PCRel_4:
Wesley Peck4b047132010-11-21 22:06:28 +00001637 Type = ELF::R_MICROBLAZE_64_PCREL;
1638 break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001639 case FK_PCRel_2:
Wesley Peck4b047132010-11-21 22:06:28 +00001640 Type = ELF::R_MICROBLAZE_32_PCREL;
1641 break;
1642 }
1643 } else {
1644 switch ((unsigned)Fixup.getKind()) {
1645 default: llvm_unreachable("invalid fixup kind!");
1646 case FK_Data_4:
Jason W Kim56a39902010-12-06 21:57:34 +00001647 Type = ((IsRelocWithSymbol || Addend !=0)
1648 ? ELF::R_MICROBLAZE_32
1649 : ELF::R_MICROBLAZE_64);
Wesley Peck4b047132010-11-21 22:06:28 +00001650 break;
1651 case FK_Data_2:
1652 Type = ELF::R_MICROBLAZE_32;
1653 break;
1654 }
1655 }
Jason W Kim56a39902010-12-06 21:57:34 +00001656 return Type;
Wesley Peck4b047132010-11-21 22:06:28 +00001657}
Jason W Kimd3443e92010-11-15 16:18:39 +00001658
1659//===- X86ELFObjectWriter -------------------------------------------===//
1660
1661
1662X86ELFObjectWriter::X86ELFObjectWriter(raw_ostream &_OS, bool _Is64Bit,
1663 bool _IsLittleEndian,
1664 uint16_t _EMachine, bool _HasRelocationAddend,
1665 Triple::OSType _OSType)
1666 : ELFObjectWriter(_OS, _Is64Bit, _IsLittleEndian, _EMachine,
1667 _HasRelocationAddend, _OSType)
1668{}
1669
1670X86ELFObjectWriter::~X86ELFObjectWriter()
1671{}
1672
Jason W Kim56a39902010-12-06 21:57:34 +00001673unsigned X86ELFObjectWriter::GetRelocType(const MCValue &Target,
1674 const MCFixup &Fixup,
1675 bool IsPCRel,
1676 bool IsRelocWithSymbol,
1677 int64_t Addend) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001678 // determine the type of the relocation
1679
Rafael Espindola12203cc2010-11-21 00:48:25 +00001680 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1681 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
Jason W Kimd3443e92010-11-15 16:18:39 +00001682 unsigned Type;
1683 if (Is64Bit) {
1684 if (IsPCRel) {
1685 switch (Modifier) {
1686 default:
1687 llvm_unreachable("Unimplemented");
1688 case MCSymbolRefExpr::VK_None:
1689 Type = ELF::R_X86_64_PC32;
1690 break;
1691 case MCSymbolRefExpr::VK_PLT:
1692 Type = ELF::R_X86_64_PLT32;
1693 break;
1694 case MCSymbolRefExpr::VK_GOTPCREL:
1695 Type = ELF::R_X86_64_GOTPCREL;
1696 break;
1697 case MCSymbolRefExpr::VK_GOTTPOFF:
1698 Type = ELF::R_X86_64_GOTTPOFF;
1699 break;
1700 case MCSymbolRefExpr::VK_TLSGD:
1701 Type = ELF::R_X86_64_TLSGD;
1702 break;
1703 case MCSymbolRefExpr::VK_TLSLD:
1704 Type = ELF::R_X86_64_TLSLD;
1705 break;
1706 }
1707 } else {
1708 switch ((unsigned)Fixup.getKind()) {
1709 default: llvm_unreachable("invalid fixup kind!");
1710 case FK_Data_8: Type = ELF::R_X86_64_64; break;
1711 case X86::reloc_signed_4byte:
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001712 case FK_PCRel_4:
Jason W Kimd3443e92010-11-15 16:18:39 +00001713 assert(isInt<32>(Target.getConstant()));
1714 switch (Modifier) {
1715 default:
1716 llvm_unreachable("Unimplemented");
1717 case MCSymbolRefExpr::VK_None:
1718 Type = ELF::R_X86_64_32S;
1719 break;
1720 case MCSymbolRefExpr::VK_GOT:
1721 Type = ELF::R_X86_64_GOT32;
1722 break;
1723 case MCSymbolRefExpr::VK_GOTPCREL:
1724 Type = ELF::R_X86_64_GOTPCREL;
1725 break;
1726 case MCSymbolRefExpr::VK_TPOFF:
1727 Type = ELF::R_X86_64_TPOFF32;
1728 break;
1729 case MCSymbolRefExpr::VK_DTPOFF:
1730 Type = ELF::R_X86_64_DTPOFF32;
1731 break;
1732 }
1733 break;
1734 case FK_Data_4:
1735 Type = ELF::R_X86_64_32;
1736 break;
1737 case FK_Data_2: Type = ELF::R_X86_64_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001738 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001739 case FK_Data_1: Type = ELF::R_X86_64_8; break;
1740 }
1741 }
1742 } else {
1743 if (IsPCRel) {
1744 switch (Modifier) {
1745 default:
1746 llvm_unreachable("Unimplemented");
1747 case MCSymbolRefExpr::VK_None:
1748 Type = ELF::R_386_PC32;
1749 break;
1750 case MCSymbolRefExpr::VK_PLT:
1751 Type = ELF::R_386_PLT32;
1752 break;
1753 }
1754 } else {
1755 switch ((unsigned)Fixup.getKind()) {
1756 default: llvm_unreachable("invalid fixup kind!");
1757
1758 case X86::reloc_global_offset_table:
1759 Type = ELF::R_386_GOTPC;
1760 break;
1761
1762 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
1763 // instead?
1764 case X86::reloc_signed_4byte:
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001765 case FK_PCRel_4:
Jason W Kimd3443e92010-11-15 16:18:39 +00001766 case FK_Data_4:
1767 switch (Modifier) {
1768 default:
1769 llvm_unreachable("Unimplemented");
1770 case MCSymbolRefExpr::VK_None:
1771 Type = ELF::R_386_32;
1772 break;
1773 case MCSymbolRefExpr::VK_GOT:
1774 Type = ELF::R_386_GOT32;
1775 break;
1776 case MCSymbolRefExpr::VK_GOTOFF:
1777 Type = ELF::R_386_GOTOFF;
1778 break;
1779 case MCSymbolRefExpr::VK_TLSGD:
1780 Type = ELF::R_386_TLS_GD;
1781 break;
1782 case MCSymbolRefExpr::VK_TPOFF:
1783 Type = ELF::R_386_TLS_LE_32;
1784 break;
1785 case MCSymbolRefExpr::VK_INDNTPOFF:
1786 Type = ELF::R_386_TLS_IE;
1787 break;
1788 case MCSymbolRefExpr::VK_NTPOFF:
1789 Type = ELF::R_386_TLS_LE;
1790 break;
1791 case MCSymbolRefExpr::VK_GOTNTPOFF:
1792 Type = ELF::R_386_TLS_GOTIE;
1793 break;
1794 case MCSymbolRefExpr::VK_TLSLDM:
1795 Type = ELF::R_386_TLS_LDM;
1796 break;
1797 case MCSymbolRefExpr::VK_DTPOFF:
1798 Type = ELF::R_386_TLS_LDO_32;
1799 break;
1800 }
1801 break;
1802 case FK_Data_2: Type = ELF::R_386_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001803 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001804 case FK_Data_1: Type = ELF::R_386_8; break;
1805 }
1806 }
1807 }
1808
1809 if (RelocNeedsGOT(Modifier))
1810 NeedsGOT = true;
1811
Jason W Kim56a39902010-12-06 21:57:34 +00001812 return Type;
Matt Fleming3565a062010-08-16 18:57:57 +00001813}