blob: 4daab49089aca36b34a04764900a29c40f6f1b2e [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
Jason W Kimd3443e92010-11-15 16:18:39 +0000328 virtual void ExecutePostLayoutBinding(MCAssembler &Asm);
Matt Fleming3565a062010-08-16 18:57:57 +0000329
Jason W Kimd3443e92010-11-15 16:18:39 +0000330 virtual void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
Matt Fleming3565a062010-08-16 18:57:57 +0000331 uint64_t Address, uint64_t Offset,
332 uint64_t Size, uint32_t Link, uint32_t Info,
333 uint64_t Alignment, uint64_t EntrySize);
334
Jason W Kimd3443e92010-11-15 16:18:39 +0000335 virtual void WriteRelocationsFragment(const MCAssembler &Asm, MCDataFragment *F,
Matt Fleming3565a062010-08-16 18:57:57 +0000336 const MCSectionData *SD);
337
Jason W Kimd3443e92010-11-15 16:18:39 +0000338 virtual bool IsFixupFullyResolved(const MCAssembler &Asm,
Rafael Espindola70703872010-09-30 02:22:20 +0000339 const MCValue Target,
340 bool IsPCRel,
341 const MCFragment *DF) const;
342
Jason W Kimd3443e92010-11-15 16:18:39 +0000343 virtual void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
344 virtual void WriteSection(MCAssembler &Asm,
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000345 const SectionIndexMapTy &SectionIndexMap,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000346 uint32_t GroupSymbolIndex,
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000347 uint64_t Offset, uint64_t Size, uint64_t Alignment,
348 const MCSectionELF &Section);
Jason W Kim56a39902010-12-06 21:57:34 +0000349
350 protected:
351 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
352 bool IsPCRel, bool IsRelocWithSymbol,
353 int64_t Addend) = 0;
354
355 virtual bool isFixupKindPCRel(unsigned Kind) const = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000356 };
357
Jason W Kimd3443e92010-11-15 16:18:39 +0000358 //===- X86ELFObjectWriter -------------------------------------------===//
359
360 class X86ELFObjectWriter : public ELFObjectWriter {
361 public:
362 X86ELFObjectWriter(raw_ostream &_OS, bool _Is64Bit, bool IsLittleEndian,
363 uint16_t _EMachine, bool _HasRelAddend,
364 Triple::OSType _OSType);
Wesley Peck4b047132010-11-21 22:06:28 +0000365
Jason W Kimd3443e92010-11-15 16:18:39 +0000366 virtual ~X86ELFObjectWriter();
Jason W Kim56a39902010-12-06 21:57:34 +0000367 protected:
368 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
369 bool IsPCRel, bool IsRelocWithSymbol,
370 int64_t Addend);
Jason W Kim4a511f02010-11-22 18:41:13 +0000371
Jason W Kim56a39902010-12-06 21:57:34 +0000372 virtual bool isFixupKindPCRel(unsigned Kind) const {
Jason W Kim4a511f02010-11-22 18:41:13 +0000373 switch (Kind) {
374 default:
375 return false;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +0000376 case FK_PCRel_1:
377 case FK_PCRel_2:
378 case FK_PCRel_4:
Jason W Kim4a511f02010-11-22 18:41:13 +0000379 case X86::reloc_riprel_4byte:
380 case X86::reloc_riprel_4byte_movq_load:
381 return true;
382 }
383 }
Jason W Kimd3443e92010-11-15 16:18:39 +0000384 };
385
386
387 //===- ARMELFObjectWriter -------------------------------------------===//
388
389 class ARMELFObjectWriter : public ELFObjectWriter {
390 public:
391 ARMELFObjectWriter(raw_ostream &_OS, bool _Is64Bit, bool IsLittleEndian,
392 uint16_t _EMachine, bool _HasRelAddend,
393 Triple::OSType _OSType);
Wesley Peck4b047132010-11-21 22:06:28 +0000394
Jason W Kimd3443e92010-11-15 16:18:39 +0000395 virtual ~ARMELFObjectWriter();
Jason W Kim85fed5e2010-12-01 02:40:06 +0000396 protected:
Jason W Kim56a39902010-12-06 21:57:34 +0000397 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
398 bool IsPCRel, bool IsRelocWithSymbol,
399 int64_t Addend);
400 virtual bool isFixupKindPCRel(unsigned Kind) const {
Jason W Kim4a511f02010-11-22 18:41:13 +0000401 switch (Kind) {
402 default:
403 return false;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +0000404 case FK_PCRel_1:
405 case FK_PCRel_2:
406 case FK_PCRel_4:
Jim Grosbachdff84b02010-12-02 00:28:45 +0000407 case ARM::fixup_arm_ldst_pcrel_12:
Owen Anderson9d63d902010-12-01 19:18:46 +0000408 case ARM::fixup_arm_pcrel_10:
Jason W Kimccbe0002010-11-22 18:47:05 +0000409 case ARM::fixup_arm_branch:
Jason W Kim4a511f02010-11-22 18:41:13 +0000410 return true;
411 }
412 }
Jason W Kimd3443e92010-11-15 16:18:39 +0000413 };
Wesley Peck4b047132010-11-21 22:06:28 +0000414
415 //===- MBlazeELFObjectWriter -------------------------------------------===//
416
417 class MBlazeELFObjectWriter : public ELFObjectWriter {
418 public:
419 MBlazeELFObjectWriter(raw_ostream &_OS, bool _Is64Bit, bool IsLittleEndian,
420 uint16_t _EMachine, bool _HasRelAddend,
421 Triple::OSType _OSType);
422
423 virtual ~MBlazeELFObjectWriter();
Jason W Kim56a39902010-12-06 21:57:34 +0000424 protected:
425 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
426 bool IsPCRel, bool IsRelocWithSymbol,
427 int64_t Addend);
Jason W Kim4a511f02010-11-22 18:41:13 +0000428
Jason W Kim56a39902010-12-06 21:57:34 +0000429 virtual bool isFixupKindPCRel(unsigned Kind) const {
Jason W Kim4a511f02010-11-22 18:41:13 +0000430 switch (Kind) {
431 default:
432 return false;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +0000433 case FK_PCRel_1:
434 case FK_PCRel_2:
435 case FK_PCRel_4:
Jason W Kim4a511f02010-11-22 18:41:13 +0000436 return true;
437 }
438 }
Wesley Peck4b047132010-11-21 22:06:28 +0000439 };
Matt Fleming3565a062010-08-16 18:57:57 +0000440}
441
Jason W Kimd3443e92010-11-15 16:18:39 +0000442ELFObjectWriter::~ELFObjectWriter()
443{}
444
Matt Fleming3565a062010-08-16 18:57:57 +0000445// Emit the ELF header.
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000446void ELFObjectWriter::WriteHeader(uint64_t SectionDataSize,
447 unsigned NumberOfSections) {
Matt Fleming3565a062010-08-16 18:57:57 +0000448 // ELF Header
449 // ----------
450 //
451 // Note
452 // ----
453 // emitWord method behaves differently for ELF32 and ELF64, writing
454 // 4 bytes in the former and 8 in the latter.
455
456 Write8(0x7f); // e_ident[EI_MAG0]
457 Write8('E'); // e_ident[EI_MAG1]
458 Write8('L'); // e_ident[EI_MAG2]
459 Write8('F'); // e_ident[EI_MAG3]
460
461 Write8(Is64Bit ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
462
463 // e_ident[EI_DATA]
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000464 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
Matt Fleming3565a062010-08-16 18:57:57 +0000465
466 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky5baf79e2010-09-09 17:57:50 +0000467 // e_ident[EI_OSABI]
468 switch (OSType) {
469 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
470 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
471 default: Write8(ELF::ELFOSABI_NONE); break;
472 }
Matt Fleming3565a062010-08-16 18:57:57 +0000473 Write8(0); // e_ident[EI_ABIVERSION]
474
475 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
476
477 Write16(ELF::ET_REL); // e_type
478
Wesley Peckeecb8582010-10-22 15:52:49 +0000479 Write16(EMachine); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +0000480
481 Write32(ELF::EV_CURRENT); // e_version
482 WriteWord(0); // e_entry, no entry point in .o file
483 WriteWord(0); // e_phoff, no program header for .o
Benjamin Kramereb976772010-08-17 17:02:29 +0000484 WriteWord(SectionDataSize + (Is64Bit ? sizeof(ELF::Elf64_Ehdr) :
485 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000486
487 // FIXME: Make this configurable.
488 Write32(0); // e_flags = whatever the target wants
489
490 // e_ehsize = ELF header size
491 Write16(Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
492
493 Write16(0); // e_phentsize = prog header entry size
494 Write16(0); // e_phnum = # prog header entries = 0
495
496 // e_shentsize = Section header entry size
497 Write16(Is64Bit ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
498
499 // e_shnum = # of section header ents
Rafael Espindola7be2c332010-10-31 00:16:26 +0000500 if (NumberOfSections >= ELF::SHN_LORESERVE)
501 Write16(0);
502 else
503 Write16(NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000504
505 // e_shstrndx = Section # of '.shstrtab'
Rafael Espindola7be2c332010-10-31 00:16:26 +0000506 if (NumberOfSections >= ELF::SHN_LORESERVE)
507 Write16(ELF::SHN_XINDEX);
508 else
509 Write16(ShstrtabIndex);
Matt Fleming3565a062010-08-16 18:57:57 +0000510}
511
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000512void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
513 MCDataFragment *ShndxF,
514 uint64_t name,
515 uint8_t info, uint64_t value,
516 uint64_t size, uint8_t other,
517 uint32_t shndx,
518 bool Reserved) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000519 if (ShndxF) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000520 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000521 String32(*ShndxF, shndx);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000522 else
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000523 String32(*ShndxF, 0);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000524 }
525
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000526 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
527 uint16_t(ELF::SHN_XINDEX) : shndx;
528
Matt Fleming3565a062010-08-16 18:57:57 +0000529 if (Is64Bit) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000530 String32(*SymtabF, name); // st_name
531 String8(*SymtabF, info); // st_info
532 String8(*SymtabF, other); // st_other
533 String16(*SymtabF, Index); // st_shndx
534 String64(*SymtabF, value); // st_value
535 String64(*SymtabF, size); // st_size
Matt Fleming3565a062010-08-16 18:57:57 +0000536 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000537 String32(*SymtabF, name); // st_name
538 String32(*SymtabF, value); // st_value
539 String32(*SymtabF, size); // st_size
540 String8(*SymtabF, info); // st_info
541 String8(*SymtabF, other); // st_other
542 String16(*SymtabF, Index); // st_shndx
Matt Fleming3565a062010-08-16 18:57:57 +0000543 }
544}
545
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000546static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout) {
547 if (Data.isCommon() && Data.isExternal())
548 return Data.getCommonAlignment();
549
550 const MCSymbol &Symbol = Data.getSymbol();
551 if (!Symbol.isInSection())
552 return 0;
553
Rafael Espindolaffd902b2010-12-06 02:57:26 +0000554 if (Data.getFragment())
555 return Layout.getSymbolOffset(&Data);
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000556
557 return 0;
558}
559
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000560void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm) {
Rafael Espindola88182132010-10-27 15:18:17 +0000561 // The presence of symbol versions causes undefined symbols and
562 // versions declared with @@@ to be renamed.
563
564 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
565 ie = Asm.symbol_end(); it != ie; ++it) {
566 const MCSymbol &Alias = it->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000567 const MCSymbol &Symbol = Alias.AliasedSymbol();
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000568 MCSymbolData &SD = Asm.getSymbolData(Symbol);
569
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000570 // Not an alias.
571 if (&Symbol == &Alias)
572 continue;
573
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000574 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000575 size_t Pos = AliasName.find('@');
576 if (Pos == StringRef::npos)
577 continue;
578
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000579 // Aliases defined with .symvar copy the binding from the symbol they alias.
580 // This is the first place we are able to copy this information.
581 it->setExternal(SD.isExternal());
582 SetBinding(*it, GetBinding(SD));
583
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000584 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000585 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
586 continue;
587
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000588 // FIXME: produce a better error message.
589 if (Symbol.isUndefined() && Rest.startswith("@@") &&
590 !Rest.startswith("@@@"))
591 report_fatal_error("A @@ version cannot be undefined");
592
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000593 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000594 }
595}
596
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000597void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
598 MCDataFragment *ShndxF,
599 ELFSymbolData &MSD,
600 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000601 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000602 MCSymbolData &Data =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000603 Layout.getAssembler().getSymbolData(OrigData.getSymbol().AliasedSymbol());
Rafael Espindola152c1062010-10-06 21:02:29 +0000604
Rafael Espindola7be2c332010-10-31 00:16:26 +0000605 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
606 Data.getSymbol().isVariable();
607
Rafael Espindola152c1062010-10-06 21:02:29 +0000608 uint8_t Binding = GetBinding(OrigData);
609 uint8_t Visibility = GetVisibility(OrigData);
610 uint8_t Type = GetType(Data);
611
612 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
613 uint8_t Other = Visibility;
614
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000615 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000616 uint64_t Size = 0;
617 const MCExpr *ESize;
618
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000619 assert(!(Data.isCommon() && !Data.isExternal()));
620
Matt Fleming3565a062010-08-16 18:57:57 +0000621 ESize = Data.getSize();
622 if (Data.getSize()) {
623 MCValue Res;
624 if (ESize->getKind() == MCExpr::Binary) {
625 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(ESize);
626
627 if (BE->EvaluateAsRelocatable(Res, &Layout)) {
Benjamin Kramer24f12062010-10-17 07:38:40 +0000628 assert(!Res.getSymA() || !Res.getSymA()->getSymbol().isDefined());
629 assert(!Res.getSymB() || !Res.getSymB()->getSymbol().isDefined());
Rafael Espindolaf230df92010-10-16 18:23:53 +0000630 Size = Res.getConstant();
Matt Fleming3565a062010-08-16 18:57:57 +0000631 }
632 } else if (ESize->getKind() == MCExpr::Constant) {
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000633 Size = static_cast<const MCConstantExpr *>(ESize)->getValue();
Matt Fleming3565a062010-08-16 18:57:57 +0000634 } else {
635 assert(0 && "Unsupported size expression");
636 }
637 }
638
639 // Write out the symbol table entry
Rafael Espindola7be2c332010-10-31 00:16:26 +0000640 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
641 Size, Other, MSD.SectionIndex, IsReserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000642}
643
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000644void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
645 MCDataFragment *ShndxF,
646 const MCAssembler &Asm,
647 const MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000648 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000649 // The string table must be emitted first because we need the index
650 // into the string table for all the symbol names.
651 assert(StringTable.size() && "Missing string table");
652
653 // FIXME: Make sure the start of the symbol table is aligned.
654
655 // The first entry is the undefined symbol entry.
Rafael Espindola7be2c332010-10-31 00:16:26 +0000656 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
Matt Fleming3565a062010-08-16 18:57:57 +0000657
658 // Write the symbol table entries.
659 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
660 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
661 ELFSymbolData &MSD = LocalSymbolData[i];
Rafael Espindola7be2c332010-10-31 00:16:26 +0000662 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000663 }
664
Rafael Espindola71859c62010-09-16 19:46:31 +0000665 // Write out a symbol table entry for each regular section.
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000666 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
667 ++i) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000668 const MCSectionELF &Section =
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000669 static_cast<const MCSectionELF&>(i->getSection());
670 if (Section.getType() == ELF::SHT_RELA ||
671 Section.getType() == ELF::SHT_REL ||
672 Section.getType() == ELF::SHT_STRTAB ||
673 Section.getType() == ELF::SHT_SYMTAB)
Eli Friedmana44fa242010-08-16 21:17:09 +0000674 continue;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000675 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000676 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section), false);
Eli Friedmana44fa242010-08-16 21:17:09 +0000677 LastLocalSymbolIndex++;
678 }
Matt Fleming3565a062010-08-16 18:57:57 +0000679
680 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
681 ELFSymbolData &MSD = ExternalSymbolData[i];
682 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000683 assert(((Data.getFlags() & ELF_STB_Global) ||
684 (Data.getFlags() & ELF_STB_Weak)) &&
685 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000686 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000687 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000688 LastLocalSymbolIndex++;
689 }
690
691 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
692 ELFSymbolData &MSD = UndefinedSymbolData[i];
693 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000694 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000695 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000696 LastLocalSymbolIndex++;
697 }
698}
699
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000700const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm,
701 const MCValue &Target,
702 const MCFragment &F) const {
703 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000704 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
705 const MCSymbol *Renamed = Renames.lookup(&Symbol);
706 const MCSymbolData &SD = Asm.getSymbolData(Symbol);
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000707
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000708 if (ASymbol.isUndefined()) {
709 if (Renamed)
710 return Renamed;
711 return &ASymbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000712 }
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000713
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000714 if (SD.isExternal()) {
715 if (Renamed)
716 return Renamed;
717 return &Symbol;
718 }
Rafael Espindola73ffea42010-09-25 05:42:19 +0000719
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000720 const MCSectionELF &Section =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000721 static_cast<const MCSectionELF&>(ASymbol.getSection());
Rafael Espindola25958732010-11-24 21:57:39 +0000722 const SectionKind secKind = Section.getKind();
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000723
Rafael Espindola25958732010-11-24 21:57:39 +0000724 if (secKind.isBSS())
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000725 return NULL;
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000726
Rafael Espindola25958732010-11-24 21:57:39 +0000727 if (secKind.isThreadLocal()) {
728 if (Renamed)
729 return Renamed;
730 return &Symbol;
731 }
732
Rafael Espindola8cecf252010-10-06 16:23:36 +0000733 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000734 const MCSectionELF &Sec2 =
735 static_cast<const MCSectionELF&>(F.getParent()->getSection());
736
Rafael Espindola8cecf252010-10-06 16:23:36 +0000737 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000738 (Kind == MCSymbolRefExpr::VK_PLT ||
739 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
Rafael Espindola25958732010-11-24 21:57:39 +0000740 Kind == MCSymbolRefExpr::VK_GOTOFF)) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000741 if (Renamed)
742 return Renamed;
743 return &Symbol;
744 }
Rafael Espindola3729d002010-10-05 23:57:26 +0000745
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000746 if (Section.getFlags() & MCSectionELF::SHF_MERGE) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000747 if (Target.getConstant() == 0)
748 return NULL;
749 if (Renamed)
750 return Renamed;
751 return &Symbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000752 }
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000753
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000754 return NULL;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000755}
756
Matt Fleming3565a062010-08-16 18:57:57 +0000757
Jason W Kim56a39902010-12-06 21:57:34 +0000758void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
759 const MCAsmLayout &Layout,
760 const MCFragment *Fragment,
761 const MCFixup &Fixup,
762 MCValue Target,
763 uint64_t &FixedValue) {
764 int64_t Addend = 0;
765 int Index = 0;
766 int64_t Value = Target.getConstant();
767 const MCSymbol *RelocSymbol = NULL;
768
769 bool IsPCRel = isFixupKindPCRel(Fixup.getKind());
770 if (!Target.isAbsolute()) {
771 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
772 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
773 RelocSymbol = SymbolToReloc(Asm, Target, *Fragment);
774
775 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
776 const MCSymbol &SymbolB = RefB->getSymbol();
777 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
778 IsPCRel = true;
779
780 // Offset of the symbol in the section
781 int64_t a = Layout.getSymbolOffset(&SDB);
782
783 // Ofeset of the relocation in the section
784 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
785 Value += b - a;
786 }
787
788 if (!RelocSymbol) {
789 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
790 MCFragment *F = SD.getFragment();
791
792 Index = F->getParent()->getOrdinal() + 1;
793
794 // Offset of the symbol in the section
795 Value += Layout.getSymbolOffset(&SD);
796 } else {
797 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
798 WeakrefUsedInReloc.insert(RelocSymbol);
799 else
800 UsedInReloc.insert(RelocSymbol);
801 Index = -1;
802 }
803 Addend = Value;
804 // Compensate for the addend on i386.
805 if (Is64Bit)
806 Value = 0;
807 }
808
809 FixedValue = Value;
810 unsigned Type = GetRelocType(Target, Fixup, IsPCRel,
811 (RelocSymbol != 0), Addend);
812
813 uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
814 Fixup.getOffset();
815
816 if (!HasRelocationAddend) Addend = 0;
817 ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend);
818 Relocations[Fragment->getParent()].push_back(ERE);
819}
820
821
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000822uint64_t
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000823ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
824 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000825 MCSymbolData &SD = Asm.getSymbolData(*S);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000826 return SD.getIndex();
Matt Fleming3565a062010-08-16 18:57:57 +0000827}
828
Rafael Espindola737cd212010-10-05 18:01:23 +0000829static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
Rafael Espindola88182132010-10-27 15:18:17 +0000830 bool Used, bool Renamed) {
Rafael Espindola484291c2010-11-01 14:28:48 +0000831 if (Data.getFlags() & ELF_Other_Weakref)
832 return false;
833
Rafael Espindolabd701182010-10-19 19:31:37 +0000834 if (Used)
835 return true;
836
Rafael Espindola88182132010-10-27 15:18:17 +0000837 if (Renamed)
838 return false;
839
Rafael Espindola737cd212010-10-05 18:01:23 +0000840 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000841
Rafael Espindolad1798862010-10-29 23:09:31 +0000842 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
843 return true;
844
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000845 const MCSymbol &A = Symbol.AliasedSymbol();
Rafael Espindolad1798862010-10-29 23:09:31 +0000846 if (!A.isVariable() && A.isUndefined() && !Data.isCommon())
Rafael Espindolaa6866962010-10-27 14:44:52 +0000847 return false;
848
Rafael Espindola737cd212010-10-05 18:01:23 +0000849 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
850 return false;
851
Rafael Espindolabd701182010-10-19 19:31:37 +0000852 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000853 return false;
854
855 return true;
856}
857
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000858static bool isLocal(const MCSymbolData &Data, bool isSignature,
859 bool isUsedInReloc) {
Rafael Espindola737cd212010-10-05 18:01:23 +0000860 if (Data.isExternal())
861 return false;
862
863 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000864 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000865
866 if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
867 if (isSignature && !isUsedInReloc)
868 return true;
869
Rafael Espindola737cd212010-10-05 18:01:23 +0000870 return false;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000871 }
Rafael Espindola737cd212010-10-05 18:01:23 +0000872
873 return true;
874}
875
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000876void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
877 SectionIndexMapTy &SectionIndexMap) {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000878 unsigned Index = 1;
879 for (MCAssembler::iterator it = Asm.begin(),
880 ie = Asm.end(); it != ie; ++it) {
881 const MCSectionELF &Section =
882 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000883 if (Section.getType() != ELF::SHT_GROUP)
884 continue;
885 SectionIndexMap[&Section] = Index++;
886 }
887
888 for (MCAssembler::iterator it = Asm.begin(),
889 ie = Asm.end(); it != ie; ++it) {
890 const MCSectionELF &Section =
891 static_cast<const MCSectionELF &>(it->getSection());
892 if (Section.getType() == ELF::SHT_GROUP)
893 continue;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000894 SectionIndexMap[&Section] = Index++;
895 }
896}
897
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000898void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000899 const SectionIndexMapTy &SectionIndexMap,
900 RevGroupMapTy RevGroupMap) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000901 // FIXME: Is this the correct place to do this?
902 if (NeedsGOT) {
903 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
904 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
905 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
906 Data.setExternal(true);
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000907 SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindola5c77c162010-10-05 15:48:37 +0000908 }
909
Matt Fleming3565a062010-08-16 18:57:57 +0000910 // Build section lookup table.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000911 int NumRegularSections = Asm.size();
Matt Fleming3565a062010-08-16 18:57:57 +0000912
913 // Index 0 is always the empty string.
914 StringMap<uint64_t> StringIndexMap;
915 StringTable += '\x00';
916
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000917 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000918 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
919 ie = Asm.symbol_end(); it != ie; ++it) {
920 const MCSymbol &Symbol = it->getSymbol();
921
Rafael Espindola484291c2010-11-01 14:28:48 +0000922 bool Used = UsedInReloc.count(&Symbol);
923 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000924 bool isSignature = RevGroupMap.count(&Symbol);
925
926 if (!isInSymtab(Asm, *it,
927 Used || WeakrefUsed || isSignature,
Rafael Espindola88182132010-10-27 15:18:17 +0000928 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000929 continue;
930
Matt Fleming3565a062010-08-16 18:57:57 +0000931 ELFSymbolData MSD;
932 MSD.SymbolData = it;
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000933 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Matt Fleming3565a062010-08-16 18:57:57 +0000934
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000935 // Undefined symbols are global, but this is the first place we
936 // are able to set it.
937 bool Local = isLocal(*it, isSignature, Used);
938 if (!Local && GetBinding(*it) == ELF::STB_LOCAL) {
939 MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
940 SetBinding(*it, ELF::STB_GLOBAL);
941 SetBinding(SD, ELF::STB_GLOBAL);
942 }
943
Rafael Espindola484291c2010-11-01 14:28:48 +0000944 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
945 SetBinding(*it, ELF::STB_WEAK);
946
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000947 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000948 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000949 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000950 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000951 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000952 } else if (RefSymbol.isUndefined()) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000953 if (isSignature && !Used)
954 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
955 else
956 MSD.SectionIndex = ELF::SHN_UNDEF;
Matt Fleming3565a062010-08-16 18:57:57 +0000957 } else {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000958 const MCSectionELF &Section =
959 static_cast<const MCSectionELF&>(RefSymbol.getSection());
960 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000961 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
962 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000963 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000964 }
965
Rafael Espindola88182132010-10-27 15:18:17 +0000966 // The @@@ in symbol version is replaced with @ in undefined symbols and
967 // @@ in defined ones.
968 StringRef Name = Symbol.getName();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000969 SmallString<32> Buf;
970
Rafael Espindola88182132010-10-27 15:18:17 +0000971 size_t Pos = Name.find("@@@");
Rafael Espindola88182132010-10-27 15:18:17 +0000972 if (Pos != StringRef::npos) {
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000973 Buf += Name.substr(0, Pos);
974 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
975 Buf += Name.substr(Pos + Skip);
976 Name = Buf;
Rafael Espindola88182132010-10-27 15:18:17 +0000977 }
978
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000979 uint64_t &Entry = StringIndexMap[Name];
Rafael Espindolaa6866962010-10-27 14:44:52 +0000980 if (!Entry) {
981 Entry = StringTable.size();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000982 StringTable += Name;
Rafael Espindolaa6866962010-10-27 14:44:52 +0000983 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +0000984 }
Rafael Espindolaa6866962010-10-27 14:44:52 +0000985 MSD.StringIndex = Entry;
986 if (MSD.SectionIndex == ELF::SHN_UNDEF)
987 UndefinedSymbolData.push_back(MSD);
988 else if (Local)
989 LocalSymbolData.push_back(MSD);
990 else
991 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +0000992 }
993
994 // Symbols are required to be in lexicographic order.
995 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
996 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
997 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
998
999 // Set the symbol indices. Local symbols must come before all other
1000 // symbols with non-local bindings.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +00001001 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +00001002 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
1003 LocalSymbolData[i].SymbolData->setIndex(Index++);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +00001004
1005 Index += NumRegularSections;
1006
Matt Fleming3565a062010-08-16 18:57:57 +00001007 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
1008 ExternalSymbolData[i].SymbolData->setIndex(Index++);
1009 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
1010 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
1011}
1012
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001013void ELFObjectWriter::WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
1014 const MCSectionData &SD) {
Matt Fleming3565a062010-08-16 18:57:57 +00001015 if (!Relocations[&SD].empty()) {
1016 MCContext &Ctx = Asm.getContext();
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001017 const MCSectionELF *RelaSection;
Matt Fleming3565a062010-08-16 18:57:57 +00001018 const MCSectionELF &Section =
1019 static_cast<const MCSectionELF&>(SD.getSection());
1020
1021 const StringRef SectionName = Section.getSectionName();
Benjamin Kramer377a5722010-08-17 17:30:07 +00001022 std::string RelaSectionName = HasRelocationAddend ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +00001023 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +00001024
1025 unsigned EntrySize;
1026 if (HasRelocationAddend)
1027 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
1028 else
1029 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +00001030
Benjamin Kramer377a5722010-08-17 17:30:07 +00001031 RelaSection = Ctx.getELFSection(RelaSectionName, HasRelocationAddend ?
1032 ELF::SHT_RELA : ELF::SHT_REL, 0,
Matt Fleming3565a062010-08-16 18:57:57 +00001033 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001034 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001035
1036 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001037 RelaSD.setAlignment(Is64Bit ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +00001038
1039 MCDataFragment *F = new MCDataFragment(&RelaSD);
1040
1041 WriteRelocationsFragment(Asm, F, &SD);
Matt Fleming3565a062010-08-16 18:57:57 +00001042 }
1043}
1044
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001045void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
1046 uint64_t Flags, uint64_t Address,
1047 uint64_t Offset, uint64_t Size,
1048 uint32_t Link, uint32_t Info,
1049 uint64_t Alignment,
1050 uint64_t EntrySize) {
Matt Fleming3565a062010-08-16 18:57:57 +00001051 Write32(Name); // sh_name: index into string table
1052 Write32(Type); // sh_type
1053 WriteWord(Flags); // sh_flags
1054 WriteWord(Address); // sh_addr
1055 WriteWord(Offset); // sh_offset
1056 WriteWord(Size); // sh_size
1057 Write32(Link); // sh_link
1058 Write32(Info); // sh_info
1059 WriteWord(Alignment); // sh_addralign
1060 WriteWord(EntrySize); // sh_entsize
1061}
1062
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001063void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
1064 MCDataFragment *F,
1065 const MCSectionData *SD) {
Matt Fleming3565a062010-08-16 18:57:57 +00001066 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
1067 // sort by the r_offset just like gnu as does
1068 array_pod_sort(Relocs.begin(), Relocs.end());
1069
1070 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1071 ELFRelocationEntry entry = Relocs[e - i - 1];
1072
Rafael Espindola12203cc2010-11-21 00:48:25 +00001073 if (!entry.Index)
1074 ;
1075 else if (entry.Index < 0)
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001076 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
1077 else
Rafael Espindola12203cc2010-11-21 00:48:25 +00001078 entry.Index += LocalSymbolData.size();
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001079 if (Is64Bit) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001080 String64(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001081
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001082 struct ELF::Elf64_Rela ERE64;
1083 ERE64.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001084 String64(*F, ERE64.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001085
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001086 if (HasRelocationAddend)
1087 String64(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001088 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001089 String32(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001090
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001091 struct ELF::Elf32_Rela ERE32;
1092 ERE32.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001093 String32(*F, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001094
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001095 if (HasRelocationAddend)
1096 String32(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001097 }
Matt Fleming3565a062010-08-16 18:57:57 +00001098 }
1099}
1100
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001101void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
1102 MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001103 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +00001104 MCContext &Ctx = Asm.getContext();
1105 MCDataFragment *F;
1106
Matt Fleming3565a062010-08-16 18:57:57 +00001107 unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
1108
Rafael Espindola38738bf2010-09-22 19:04:41 +00001109 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001110 const MCSectionELF *ShstrtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001111 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001112 SectionKind::getReadOnly());
Rafael Espindola71859c62010-09-16 19:46:31 +00001113 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1114 ShstrtabSD.setAlignment(1);
1115 ShstrtabIndex = Asm.size();
1116
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001117 const MCSectionELF *SymtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001118 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1119 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001120 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001121 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Matt Fleming3565a062010-08-16 18:57:57 +00001122 SymtabSD.setAlignment(Is64Bit ? 8 : 4);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001123 SymbolTableIndex = Asm.size();
1124
1125 MCSectionData *SymtabShndxSD = NULL;
1126
1127 if (NeedsSymtabShndx) {
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001128 const MCSectionELF *SymtabShndxSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001129 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001130 SectionKind::getReadOnly(), 4, "");
Rafael Espindola7be2c332010-10-31 00:16:26 +00001131 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
1132 SymtabShndxSD->setAlignment(4);
1133 }
Matt Fleming3565a062010-08-16 18:57:57 +00001134
Matt Fleming3565a062010-08-16 18:57:57 +00001135 const MCSection *StrtabSection;
1136 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001137 SectionKind::getReadOnly());
Matt Fleming3565a062010-08-16 18:57:57 +00001138 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1139 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +00001140 StringTableIndex = Asm.size();
1141
Rafael Espindolac3c413f2010-09-27 22:04:54 +00001142 WriteRelocations(Asm, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001143
1144 // Symbol table
1145 F = new MCDataFragment(&SymtabSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001146 MCDataFragment *ShndxF = NULL;
1147 if (NeedsSymtabShndx) {
1148 ShndxF = new MCDataFragment(SymtabShndxSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001149 }
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001150 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
Rafael Espindola71859c62010-09-16 19:46:31 +00001151
Matt Fleming3565a062010-08-16 18:57:57 +00001152 F = new MCDataFragment(&StrtabSD);
1153 F->getContents().append(StringTable.begin(), StringTable.end());
Matt Fleming3565a062010-08-16 18:57:57 +00001154
Matt Fleming3565a062010-08-16 18:57:57 +00001155 F = new MCDataFragment(&ShstrtabSD);
1156
Matt Fleming3565a062010-08-16 18:57:57 +00001157 // Section header string table.
1158 //
1159 // The first entry of a string table holds a null character so skip
1160 // section 0.
1161 uint64_t Index = 1;
1162 F->getContents() += '\x00';
1163
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001164 StringMap<uint64_t> SecStringMap;
Matt Fleming3565a062010-08-16 18:57:57 +00001165 for (MCAssembler::const_iterator it = Asm.begin(),
1166 ie = Asm.end(); it != ie; ++it) {
Matt Fleming3565a062010-08-16 18:57:57 +00001167 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +00001168 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola51efe7a2010-09-23 14:14:56 +00001169 // FIXME: We could merge suffixes like in .text and .rela.text.
Matt Fleming3565a062010-08-16 18:57:57 +00001170
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001171 StringRef Name = Section.getSectionName();
1172 if (SecStringMap.count(Name)) {
1173 SectionStringTableIndex[&Section] = SecStringMap[Name];
1174 continue;
1175 }
Matt Fleming3565a062010-08-16 18:57:57 +00001176 // Remember the index into the string table so we can write it
1177 // into the sh_name field of the section header table.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001178 SectionStringTableIndex[&Section] = Index;
1179 SecStringMap[Name] = Index;
Matt Fleming3565a062010-08-16 18:57:57 +00001180
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001181 Index += Name.size() + 1;
1182 F->getContents() += Name;
Matt Fleming3565a062010-08-16 18:57:57 +00001183 F->getContents() += '\x00';
1184 }
Rafael Espindola70703872010-09-30 02:22:20 +00001185}
1186
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001187bool ELFObjectWriter::IsFixupFullyResolved(const MCAssembler &Asm,
1188 const MCValue Target,
1189 bool IsPCRel,
1190 const MCFragment *DF) const {
Rafael Espindola70703872010-09-30 02:22:20 +00001191 // If this is a PCrel relocation, find the section this fixup value is
1192 // relative to.
1193 const MCSection *BaseSection = 0;
1194 if (IsPCRel) {
1195 BaseSection = &DF->getParent()->getSection();
1196 assert(BaseSection);
1197 }
1198
1199 const MCSection *SectionA = 0;
1200 const MCSymbol *SymbolA = 0;
1201 if (const MCSymbolRefExpr *A = Target.getSymA()) {
Rafael Espindola2c920852010-11-16 04:11:46 +00001202 SymbolA = &A->getSymbol();
1203 SectionA = &SymbolA->AliasedSymbol().getSection();
Rafael Espindola70703872010-09-30 02:22:20 +00001204 }
1205
1206 const MCSection *SectionB = 0;
Rafael Espindola12203cc2010-11-21 00:48:25 +00001207 const MCSymbol *SymbolB = 0;
Rafael Espindola70703872010-09-30 02:22:20 +00001208 if (const MCSymbolRefExpr *B = Target.getSymB()) {
Rafael Espindola12203cc2010-11-21 00:48:25 +00001209 SymbolB = &B->getSymbol();
1210 SectionB = &SymbolB->AliasedSymbol().getSection();
Rafael Espindola70703872010-09-30 02:22:20 +00001211 }
1212
1213 if (!BaseSection)
1214 return SectionA == SectionB;
1215
Rafael Espindola12203cc2010-11-21 00:48:25 +00001216 if (SymbolB)
1217 return false;
1218
1219 // Absolute address but PCrel instruction, so we need a relocation.
1220 if (!SymbolA)
1221 return false;
1222
Rafael Espindola2c920852010-11-16 04:11:46 +00001223 // FIXME: This is in here just to match gnu as output. If the two ends
1224 // are in the same section, there is nothing that the linker can do to
1225 // break it.
Rafael Espindola70703872010-09-30 02:22:20 +00001226 const MCSymbolData &DataA = Asm.getSymbolData(*SymbolA);
1227 if (DataA.isExternal())
1228 return false;
1229
Rafael Espindola12203cc2010-11-21 00:48:25 +00001230 return BaseSection == SectionA;
Matt Fleming3565a062010-08-16 18:57:57 +00001231}
1232
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001233void ELFObjectWriter::CreateGroupSections(MCAssembler &Asm,
1234 MCAsmLayout &Layout,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001235 GroupMapTy &GroupMap,
1236 RevGroupMapTy &RevGroupMap) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001237 // Build the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001238 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1239 it != ie; ++it) {
1240 const MCSectionELF &Section =
1241 static_cast<const MCSectionELF&>(it->getSection());
1242 if (!(Section.getFlags() & MCSectionELF::SHF_GROUP))
1243 continue;
1244
1245 const MCSymbol *SignatureSymbol = Section.getGroup();
1246 Asm.getOrCreateSymbolData(*SignatureSymbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001247 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001248 if (!Group) {
1249 Group = Asm.getContext().CreateELFGroupSection();
1250 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1251 Data.setAlignment(4);
1252 MCDataFragment *F = new MCDataFragment(&Data);
1253 String32(*F, ELF::GRP_COMDAT);
1254 }
1255 GroupMap[Group] = SignatureSymbol;
1256 }
1257
1258 // Add sections to the groups
1259 unsigned Index = 1;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001260 unsigned NumGroups = RevGroupMap.size();
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001261 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1262 it != ie; ++it, ++Index) {
1263 const MCSectionELF &Section =
1264 static_cast<const MCSectionELF&>(it->getSection());
1265 if (!(Section.getFlags() & MCSectionELF::SHF_GROUP))
1266 continue;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001267 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001268 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1269 // FIXME: we could use the previous fragment
1270 MCDataFragment *F = new MCDataFragment(&Data);
1271 String32(*F, NumGroups + Index);
1272 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001273}
1274
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001275void ELFObjectWriter::WriteSection(MCAssembler &Asm,
1276 const SectionIndexMapTy &SectionIndexMap,
1277 uint32_t GroupSymbolIndex,
1278 uint64_t Offset, uint64_t Size,
1279 uint64_t Alignment,
1280 const MCSectionELF &Section) {
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001281 uint64_t sh_link = 0;
1282 uint64_t sh_info = 0;
1283
1284 switch(Section.getType()) {
1285 case ELF::SHT_DYNAMIC:
1286 sh_link = SectionStringTableIndex[&Section];
1287 sh_info = 0;
1288 break;
1289
1290 case ELF::SHT_REL:
1291 case ELF::SHT_RELA: {
1292 const MCSectionELF *SymtabSection;
1293 const MCSectionELF *InfoSection;
1294 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
1295 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001296 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001297 sh_link = SectionIndexMap.lookup(SymtabSection);
1298 assert(sh_link && ".symtab not found");
1299
1300 // Remove ".rel" and ".rela" prefixes.
1301 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1302 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
1303
1304 InfoSection = Asm.getContext().getELFSection(SectionName,
1305 ELF::SHT_PROGBITS, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001306 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001307 sh_info = SectionIndexMap.lookup(InfoSection);
1308 break;
1309 }
1310
1311 case ELF::SHT_SYMTAB:
1312 case ELF::SHT_DYNSYM:
1313 sh_link = StringTableIndex;
1314 sh_info = LastLocalSymbolIndex;
1315 break;
1316
1317 case ELF::SHT_SYMTAB_SHNDX:
1318 sh_link = SymbolTableIndex;
1319 break;
1320
1321 case ELF::SHT_PROGBITS:
1322 case ELF::SHT_STRTAB:
1323 case ELF::SHT_NOBITS:
1324 case ELF::SHT_NULL:
1325 case ELF::SHT_ARM_ATTRIBUTES:
1326 // Nothing to do.
1327 break;
1328
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001329 case ELF::SHT_GROUP: {
1330 sh_link = SymbolTableIndex;
1331 sh_info = GroupSymbolIndex;
1332 break;
1333 }
1334
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001335 default:
1336 assert(0 && "FIXME: sh_type value not supported!");
1337 break;
1338 }
1339
1340 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1341 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1342 Alignment, Section.getEntrySize());
1343}
1344
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001345static bool IsELFMetaDataSection(const MCSectionData &SD) {
Rafael Espindolaf8803fe2010-12-06 03:48:09 +00001346 return SD.getOrdinal() == ~UINT32_C(0) &&
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001347 !SD.getSection().isVirtualSection();
1348}
1349
1350static uint64_t DataSectionSize(const MCSectionData &SD) {
1351 uint64_t Ret = 0;
1352 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1353 ++i) {
1354 const MCFragment &F = *i;
1355 assert(F.getKind() == MCFragment::FT_Data);
1356 Ret += cast<MCDataFragment>(F).getContents().size();
1357 }
1358 return Ret;
1359}
1360
1361static uint64_t GetSectionFileSize(const MCAsmLayout &Layout,
1362 const MCSectionData &SD) {
1363 if (IsELFMetaDataSection(SD))
1364 return DataSectionSize(SD);
1365 return Layout.getSectionFileSize(&SD);
1366}
1367
1368static uint64_t GetSectionSize(const MCAsmLayout &Layout,
1369 const MCSectionData &SD) {
1370 if (IsELFMetaDataSection(SD))
1371 return DataSectionSize(SD);
1372 return Layout.getSectionSize(&SD);
1373}
1374
1375static void WriteDataSectionData(ELFObjectWriter *W, const MCSectionData &SD) {
1376 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1377 ++i) {
1378 const MCFragment &F = *i;
1379 assert(F.getKind() == MCFragment::FT_Data);
1380 W->WriteBytes(cast<MCDataFragment>(F).getContents().str());
1381 }
1382}
1383
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001384void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1385 const MCAsmLayout &Layout) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001386 GroupMapTy GroupMap;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001387 RevGroupMapTy RevGroupMap;
1388 CreateGroupSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1389 RevGroupMap);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001390
Rafael Espindolabab2a802010-11-10 21:51:05 +00001391 SectionIndexMapTy SectionIndexMap;
1392
1393 ComputeIndexMap(Asm, SectionIndexMap);
1394
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001395 // Compute symbol table information.
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001396 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap);
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001397
Matt Fleming3565a062010-08-16 18:57:57 +00001398 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001399 const_cast<MCAsmLayout&>(Layout),
1400 SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +00001401
Rafael Espindola1d739a02010-11-10 22:34:07 +00001402 // Update to include the metadata sections.
1403 ComputeIndexMap(Asm, SectionIndexMap);
1404
Matt Fleming3565a062010-08-16 18:57:57 +00001405 // Add 1 for the null section.
1406 unsigned NumSections = Asm.size() + 1;
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001407 uint64_t NaturalAlignment = Is64Bit ? 8 : 4;
1408 uint64_t HeaderSize = Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr);
1409 uint64_t FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001410
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001411 std::vector<const MCSectionELF*> Sections;
1412 Sections.resize(NumSections);
1413
1414 for (SectionIndexMapTy::const_iterator i=
1415 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1416 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
1417 Sections[p.second] = p.first;
1418 }
1419
1420 for (unsigned i = 1; i < NumSections; ++i) {
1421 const MCSectionELF &Section = *Sections[i];
1422 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001423
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001424 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1425
Matt Fleming3565a062010-08-16 18:57:57 +00001426 // Get the size of the section in the output file (including padding).
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001427 FileOff += GetSectionFileSize(Layout, SD);
Matt Fleming3565a062010-08-16 18:57:57 +00001428 }
1429
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001430 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1431
Matt Fleming3565a062010-08-16 18:57:57 +00001432 // Write out the ELF header ...
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001433 WriteHeader(FileOff - HeaderSize, NumSections);
1434
1435 FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001436
1437 // ... then all of the sections ...
1438 DenseMap<const MCSection*, uint64_t> SectionOffsetMap;
1439
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001440 for (unsigned i = 1; i < NumSections; ++i) {
1441 const MCSectionELF &Section = *Sections[i];
1442 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001443
1444 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1445 WriteZeros(Padding);
1446 FileOff += Padding;
1447
Matt Fleming3565a062010-08-16 18:57:57 +00001448 // Remember the offset into the file for this section.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001449 SectionOffsetMap[&Section] = FileOff;
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001450
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001451 FileOff += GetSectionFileSize(Layout, SD);
Matt Fleming3565a062010-08-16 18:57:57 +00001452
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001453 if (IsELFMetaDataSection(SD))
1454 WriteDataSectionData(this, SD);
1455 else
1456 Asm.WriteSectionData(&SD, Layout, this);
Matt Fleming3565a062010-08-16 18:57:57 +00001457 }
1458
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001459 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1460 WriteZeros(Padding);
1461 FileOff += Padding;
1462
Matt Fleming3565a062010-08-16 18:57:57 +00001463 // ... and then the section header table.
1464 // Should we align the section header table?
1465 //
1466 // Null section first.
Rafael Espindola7be2c332010-10-31 00:16:26 +00001467 uint64_t FirstSectionSize =
1468 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1469 uint32_t FirstSectionLink =
1470 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1471 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming3565a062010-08-16 18:57:57 +00001472
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001473 for (unsigned i = 1; i < NumSections; ++i) {
1474 const MCSectionELF &Section = *Sections[i];
1475 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1476 uint32_t GroupSymbolIndex;
1477 if (Section.getType() != ELF::SHT_GROUP)
1478 GroupSymbolIndex = 0;
1479 else
1480 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm, GroupMap[&Section]);
Matt Fleming3565a062010-08-16 18:57:57 +00001481
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001482 uint64_t Size = GetSectionSize(Layout, SD);
1483
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001484 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001485 SectionOffsetMap[&Section], Size,
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001486 SD.getAlignment(), Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001487 }
1488}
1489
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001490MCObjectWriter *llvm::createELFObjectWriter(raw_ostream &OS,
1491 bool Is64Bit,
1492 Triple::OSType OSType,
1493 uint16_t EMachine,
1494 bool IsLittleEndian,
1495 bool HasRelocationAddend) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001496 switch (EMachine) {
1497 case ELF::EM_386:
1498 case ELF::EM_X86_64:
1499 return new X86ELFObjectWriter(OS, Is64Bit, IsLittleEndian, EMachine,
1500 HasRelocationAddend, OSType); break;
1501 case ELF::EM_ARM:
1502 return new ARMELFObjectWriter(OS, Is64Bit, IsLittleEndian, EMachine,
1503 HasRelocationAddend, OSType); break;
Wesley Peck4b047132010-11-21 22:06:28 +00001504 case ELF::EM_MBLAZE:
1505 return new MBlazeELFObjectWriter(OS, Is64Bit, IsLittleEndian, EMachine,
1506 HasRelocationAddend, OSType); break;
Benjamin Kramer32858772010-11-15 19:20:50 +00001507 default: llvm_unreachable("Unsupported architecture"); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001508 }
1509}
1510
1511
1512/// START OF SUBCLASSES for ELFObjectWriter
1513//===- ARMELFObjectWriter -------------------------------------------===//
1514
1515ARMELFObjectWriter::ARMELFObjectWriter(raw_ostream &_OS, bool _Is64Bit,
1516 bool _IsLittleEndian,
1517 uint16_t _EMachine, bool _HasRelocationAddend,
1518 Triple::OSType _OSType)
1519 : ELFObjectWriter(_OS, _Is64Bit, _IsLittleEndian, _EMachine,
1520 _HasRelocationAddend, _OSType)
1521{}
1522
1523ARMELFObjectWriter::~ARMELFObjectWriter()
1524{}
1525
Jason W Kim85fed5e2010-12-01 02:40:06 +00001526unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target,
1527 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001528 bool IsPCRel,
1529 bool IsRelocWithSymbol,
1530 int64_t Addend) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001531 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1532 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
1533
1534 if (IsPCRel) {
1535 switch (Modifier) {
1536 default: assert(0 && "Unimplemented Modifier");
1537 case MCSymbolRefExpr::VK_None: break;
1538 }
1539 switch ((unsigned)Fixup.getKind()) {
1540 default: assert(0 && "Unimplemented");
1541 case ARM::fixup_arm_branch: return ELF::R_ARM_CALL; break;
1542 }
1543 } else {
1544 switch ((unsigned)Fixup.getKind()) {
1545 default: llvm_unreachable("invalid fixup kind!");
Jim Grosbachdff84b02010-12-02 00:28:45 +00001546 case ARM::fixup_arm_ldst_pcrel_12:
Owen Anderson9d63d902010-12-01 19:18:46 +00001547 case ARM::fixup_arm_pcrel_10:
Jim Grosbachdff84b02010-12-02 00:28:45 +00001548 case ARM::fixup_arm_adr_pcrel_12:
Jim Grosbach662a8162010-12-06 23:57:07 +00001549 case ARM::fixup_arm_thumb_bl:
Jason W Kim85fed5e2010-12-01 02:40:06 +00001550 assert(0 && "Unimplemented"); break;
1551 case ARM::fixup_arm_branch:
1552 return ELF::R_ARM_CALL; break;
1553 case ARM::fixup_arm_movt_hi16:
1554 return ELF::R_ARM_MOVT_ABS; break;
1555 case ARM::fixup_arm_movw_lo16:
1556 return ELF::R_ARM_MOVW_ABS_NC; break;
1557 }
1558 }
1559
1560 if (RelocNeedsGOT(Modifier))
1561 NeedsGOT = true;
1562 return -1;
1563}
1564
Wesley Peck4b047132010-11-21 22:06:28 +00001565//===- MBlazeELFObjectWriter -------------------------------------------===//
Jason W Kimd3443e92010-11-15 16:18:39 +00001566
Wesley Peck4b047132010-11-21 22:06:28 +00001567MBlazeELFObjectWriter::MBlazeELFObjectWriter(raw_ostream &_OS, bool _Is64Bit,
1568 bool _IsLittleEndian,
1569 uint16_t _EMachine,
1570 bool _HasRelocationAddend,
1571 Triple::OSType _OSType)
1572 : ELFObjectWriter(_OS, _Is64Bit, _IsLittleEndian, _EMachine,
1573 _HasRelocationAddend, _OSType) {
1574}
1575
1576MBlazeELFObjectWriter::~MBlazeELFObjectWriter() {
1577}
1578
Jason W Kim56a39902010-12-06 21:57:34 +00001579unsigned MBlazeELFObjectWriter::GetRelocType(const MCValue &Target,
Wesley Peck4b047132010-11-21 22:06:28 +00001580 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001581 bool IsPCRel,
1582 bool IsRelocWithSymbol,
1583 int64_t Addend) {
Wesley Peck4b047132010-11-21 22:06:28 +00001584 // determine the type of the relocation
1585 unsigned Type;
1586 if (IsPCRel) {
1587 switch ((unsigned)Fixup.getKind()) {
1588 default:
1589 llvm_unreachable("Unimplemented");
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001590 case FK_PCRel_4:
Wesley Peck4b047132010-11-21 22:06:28 +00001591 Type = ELF::R_MICROBLAZE_64_PCREL;
1592 break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001593 case FK_PCRel_2:
Wesley Peck4b047132010-11-21 22:06:28 +00001594 Type = ELF::R_MICROBLAZE_32_PCREL;
1595 break;
1596 }
1597 } else {
1598 switch ((unsigned)Fixup.getKind()) {
1599 default: llvm_unreachable("invalid fixup kind!");
1600 case FK_Data_4:
Jason W Kim56a39902010-12-06 21:57:34 +00001601 Type = ((IsRelocWithSymbol || Addend !=0)
1602 ? ELF::R_MICROBLAZE_32
1603 : ELF::R_MICROBLAZE_64);
Wesley Peck4b047132010-11-21 22:06:28 +00001604 break;
1605 case FK_Data_2:
1606 Type = ELF::R_MICROBLAZE_32;
1607 break;
1608 }
1609 }
Jason W Kim56a39902010-12-06 21:57:34 +00001610 return Type;
Wesley Peck4b047132010-11-21 22:06:28 +00001611}
Jason W Kimd3443e92010-11-15 16:18:39 +00001612
1613//===- X86ELFObjectWriter -------------------------------------------===//
1614
1615
1616X86ELFObjectWriter::X86ELFObjectWriter(raw_ostream &_OS, bool _Is64Bit,
1617 bool _IsLittleEndian,
1618 uint16_t _EMachine, bool _HasRelocationAddend,
1619 Triple::OSType _OSType)
1620 : ELFObjectWriter(_OS, _Is64Bit, _IsLittleEndian, _EMachine,
1621 _HasRelocationAddend, _OSType)
1622{}
1623
1624X86ELFObjectWriter::~X86ELFObjectWriter()
1625{}
1626
Jason W Kim56a39902010-12-06 21:57:34 +00001627unsigned X86ELFObjectWriter::GetRelocType(const MCValue &Target,
1628 const MCFixup &Fixup,
1629 bool IsPCRel,
1630 bool IsRelocWithSymbol,
1631 int64_t Addend) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001632 // determine the type of the relocation
1633
Rafael Espindola12203cc2010-11-21 00:48:25 +00001634 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1635 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
Jason W Kimd3443e92010-11-15 16:18:39 +00001636 unsigned Type;
1637 if (Is64Bit) {
1638 if (IsPCRel) {
1639 switch (Modifier) {
1640 default:
1641 llvm_unreachable("Unimplemented");
1642 case MCSymbolRefExpr::VK_None:
1643 Type = ELF::R_X86_64_PC32;
1644 break;
1645 case MCSymbolRefExpr::VK_PLT:
1646 Type = ELF::R_X86_64_PLT32;
1647 break;
1648 case MCSymbolRefExpr::VK_GOTPCREL:
1649 Type = ELF::R_X86_64_GOTPCREL;
1650 break;
1651 case MCSymbolRefExpr::VK_GOTTPOFF:
1652 Type = ELF::R_X86_64_GOTTPOFF;
1653 break;
1654 case MCSymbolRefExpr::VK_TLSGD:
1655 Type = ELF::R_X86_64_TLSGD;
1656 break;
1657 case MCSymbolRefExpr::VK_TLSLD:
1658 Type = ELF::R_X86_64_TLSLD;
1659 break;
1660 }
1661 } else {
1662 switch ((unsigned)Fixup.getKind()) {
1663 default: llvm_unreachable("invalid fixup kind!");
1664 case FK_Data_8: Type = ELF::R_X86_64_64; break;
1665 case X86::reloc_signed_4byte:
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001666 case FK_PCRel_4:
Jason W Kimd3443e92010-11-15 16:18:39 +00001667 assert(isInt<32>(Target.getConstant()));
1668 switch (Modifier) {
1669 default:
1670 llvm_unreachable("Unimplemented");
1671 case MCSymbolRefExpr::VK_None:
1672 Type = ELF::R_X86_64_32S;
1673 break;
1674 case MCSymbolRefExpr::VK_GOT:
1675 Type = ELF::R_X86_64_GOT32;
1676 break;
1677 case MCSymbolRefExpr::VK_GOTPCREL:
1678 Type = ELF::R_X86_64_GOTPCREL;
1679 break;
1680 case MCSymbolRefExpr::VK_TPOFF:
1681 Type = ELF::R_X86_64_TPOFF32;
1682 break;
1683 case MCSymbolRefExpr::VK_DTPOFF:
1684 Type = ELF::R_X86_64_DTPOFF32;
1685 break;
1686 }
1687 break;
1688 case FK_Data_4:
1689 Type = ELF::R_X86_64_32;
1690 break;
1691 case FK_Data_2: Type = ELF::R_X86_64_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001692 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001693 case FK_Data_1: Type = ELF::R_X86_64_8; break;
1694 }
1695 }
1696 } else {
1697 if (IsPCRel) {
1698 switch (Modifier) {
1699 default:
1700 llvm_unreachable("Unimplemented");
1701 case MCSymbolRefExpr::VK_None:
1702 Type = ELF::R_386_PC32;
1703 break;
1704 case MCSymbolRefExpr::VK_PLT:
1705 Type = ELF::R_386_PLT32;
1706 break;
1707 }
1708 } else {
1709 switch ((unsigned)Fixup.getKind()) {
1710 default: llvm_unreachable("invalid fixup kind!");
1711
1712 case X86::reloc_global_offset_table:
1713 Type = ELF::R_386_GOTPC;
1714 break;
1715
1716 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
1717 // instead?
1718 case X86::reloc_signed_4byte:
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001719 case FK_PCRel_4:
Jason W Kimd3443e92010-11-15 16:18:39 +00001720 case FK_Data_4:
1721 switch (Modifier) {
1722 default:
1723 llvm_unreachable("Unimplemented");
1724 case MCSymbolRefExpr::VK_None:
1725 Type = ELF::R_386_32;
1726 break;
1727 case MCSymbolRefExpr::VK_GOT:
1728 Type = ELF::R_386_GOT32;
1729 break;
1730 case MCSymbolRefExpr::VK_GOTOFF:
1731 Type = ELF::R_386_GOTOFF;
1732 break;
1733 case MCSymbolRefExpr::VK_TLSGD:
1734 Type = ELF::R_386_TLS_GD;
1735 break;
1736 case MCSymbolRefExpr::VK_TPOFF:
1737 Type = ELF::R_386_TLS_LE_32;
1738 break;
1739 case MCSymbolRefExpr::VK_INDNTPOFF:
1740 Type = ELF::R_386_TLS_IE;
1741 break;
1742 case MCSymbolRefExpr::VK_NTPOFF:
1743 Type = ELF::R_386_TLS_LE;
1744 break;
1745 case MCSymbolRefExpr::VK_GOTNTPOFF:
1746 Type = ELF::R_386_TLS_GOTIE;
1747 break;
1748 case MCSymbolRefExpr::VK_TLSLDM:
1749 Type = ELF::R_386_TLS_LDM;
1750 break;
1751 case MCSymbolRefExpr::VK_DTPOFF:
1752 Type = ELF::R_386_TLS_LDO_32;
1753 break;
1754 }
1755 break;
1756 case FK_Data_2: Type = ELF::R_386_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001757 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001758 case FK_Data_1: Type = ELF::R_386_8; break;
1759 }
1760 }
1761 }
1762
1763 if (RelocNeedsGOT(Modifier))
1764 NeedsGOT = true;
1765
Jason W Kim56a39902010-12-06 21:57:34 +00001766 return Type;
Matt Fleming3565a062010-08-16 18:57:57 +00001767}