blob: 67907c7d8bf689c43e8059b0a06e5fdd3212e8ab [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,
Matt Fleming3565a062010-08-16 18:57:57 +0000289 const MCFragment *Fragment, const MCFixup &Fixup,
Jason W Kimd3443e92010-11-15 16:18:39 +0000290 MCValue Target, uint64_t &FixedValue) {
291 assert(0 && "RecordRelocation is not specific enough");
Benjamin Kramer32858772010-11-15 19:20:50 +0000292 }
Matt Fleming3565a062010-08-16 18:57:57 +0000293
Jason W Kimd3443e92010-11-15 16:18:39 +0000294 virtual uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm,
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000295 const MCSymbol *S);
Matt Fleming3565a062010-08-16 18:57:57 +0000296
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000297 // Map from a group section to the signature symbol
298 typedef DenseMap<const MCSectionELF*, const MCSymbol*> GroupMapTy;
299 // Map from a signature symbol to the group section
300 typedef DenseMap<const MCSymbol*, const MCSectionELF*> RevGroupMapTy;
301
Matt Fleming3565a062010-08-16 18:57:57 +0000302 /// ComputeSymbolTable - Compute the symbol table data
303 ///
304 /// \param StringTable [out] - The string table data.
305 /// \param StringIndexMap [out] - Map from symbol names to offsets in the
306 /// string table.
Jason W Kimd3443e92010-11-15 16:18:39 +0000307 virtual void ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000308 const SectionIndexMapTy &SectionIndexMap,
309 RevGroupMapTy RevGroupMap);
Rafael Espindolabab2a802010-11-10 21:51:05 +0000310
Jason W Kimd3443e92010-11-15 16:18:39 +0000311 virtual void ComputeIndexMap(MCAssembler &Asm,
Rafael Espindolabab2a802010-11-10 21:51:05 +0000312 SectionIndexMapTy &SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +0000313
Jason W Kimd3443e92010-11-15 16:18:39 +0000314 virtual void WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
Matt Fleming3565a062010-08-16 18:57:57 +0000315 const MCSectionData &SD);
316
Jason W Kimd3443e92010-11-15 16:18:39 +0000317 virtual void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout) {
Matt Fleming3565a062010-08-16 18:57:57 +0000318 for (MCAssembler::const_iterator it = Asm.begin(),
319 ie = Asm.end(); it != ie; ++it) {
320 WriteRelocation(Asm, Layout, *it);
321 }
322 }
323
Jason W Kimd3443e92010-11-15 16:18:39 +0000324 virtual void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000325 const SectionIndexMapTy &SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +0000326
Jason W Kimd3443e92010-11-15 16:18:39 +0000327 virtual void CreateGroupSections(MCAssembler &Asm, MCAsmLayout &Layout,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000328 GroupMapTy &GroupMap, RevGroupMapTy &RevGroupMap);
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000329
Jason W Kimd3443e92010-11-15 16:18:39 +0000330 virtual void ExecutePostLayoutBinding(MCAssembler &Asm);
Matt Fleming3565a062010-08-16 18:57:57 +0000331
Jason W Kimd3443e92010-11-15 16:18:39 +0000332 virtual void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
Matt Fleming3565a062010-08-16 18:57:57 +0000333 uint64_t Address, uint64_t Offset,
334 uint64_t Size, uint32_t Link, uint32_t Info,
335 uint64_t Alignment, uint64_t EntrySize);
336
Jason W Kimd3443e92010-11-15 16:18:39 +0000337 virtual void WriteRelocationsFragment(const MCAssembler &Asm, MCDataFragment *F,
Matt Fleming3565a062010-08-16 18:57:57 +0000338 const MCSectionData *SD);
339
Jason W Kimd3443e92010-11-15 16:18:39 +0000340 virtual bool IsFixupFullyResolved(const MCAssembler &Asm,
Rafael Espindola70703872010-09-30 02:22:20 +0000341 const MCValue Target,
342 bool IsPCRel,
343 const MCFragment *DF) const;
344
Jason W Kimd3443e92010-11-15 16:18:39 +0000345 virtual void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
346 virtual void WriteSection(MCAssembler &Asm,
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000347 const SectionIndexMapTy &SectionIndexMap,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000348 uint32_t GroupSymbolIndex,
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000349 uint64_t Offset, uint64_t Size, uint64_t Alignment,
350 const MCSectionELF &Section);
Matt Fleming3565a062010-08-16 18:57:57 +0000351 };
352
Jason W Kimd3443e92010-11-15 16:18:39 +0000353 //===- X86ELFObjectWriter -------------------------------------------===//
354
355 class X86ELFObjectWriter : public ELFObjectWriter {
356 public:
357 X86ELFObjectWriter(raw_ostream &_OS, bool _Is64Bit, bool IsLittleEndian,
358 uint16_t _EMachine, bool _HasRelAddend,
359 Triple::OSType _OSType);
Wesley Peck4b047132010-11-21 22:06:28 +0000360
Jason W Kimd3443e92010-11-15 16:18:39 +0000361 virtual ~X86ELFObjectWriter();
362 virtual void RecordRelocation(const MCAssembler &Asm,
363 const MCAsmLayout &Layout,
364 const MCFragment *Fragment,
365 const MCFixup &Fixup, MCValue Target,
366 uint64_t &FixedValue);
Jason W Kim4a511f02010-11-22 18:41:13 +0000367
Jason W Kim28ef0a52010-11-22 18:57:00 +0000368 private:
Jason W Kim4a511f02010-11-22 18:41:13 +0000369 static bool isFixupKindPCRel(unsigned Kind) {
370 switch (Kind) {
371 default:
372 return false;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +0000373 case FK_PCRel_1:
374 case FK_PCRel_2:
375 case FK_PCRel_4:
Jason W Kim4a511f02010-11-22 18:41:13 +0000376 case X86::reloc_riprel_4byte:
377 case X86::reloc_riprel_4byte_movq_load:
378 return true;
379 }
380 }
Jason W Kimd3443e92010-11-15 16:18:39 +0000381 };
382
383
384 //===- ARMELFObjectWriter -------------------------------------------===//
385
386 class ARMELFObjectWriter : public ELFObjectWriter {
387 public:
388 ARMELFObjectWriter(raw_ostream &_OS, bool _Is64Bit, bool IsLittleEndian,
389 uint16_t _EMachine, bool _HasRelAddend,
390 Triple::OSType _OSType);
Wesley Peck4b047132010-11-21 22:06:28 +0000391
Jason W Kimd3443e92010-11-15 16:18:39 +0000392 virtual ~ARMELFObjectWriter();
393 virtual void RecordRelocation(const MCAssembler &Asm,
394 const MCAsmLayout &Layout,
395 const MCFragment *Fragment,
396 const MCFixup &Fixup, MCValue Target,
397 uint64_t &FixedValue);
Jason W Kim4a511f02010-11-22 18:41:13 +0000398
Jason W Kim85fed5e2010-12-01 02:40:06 +0000399 protected:
400 // Fixme: pull up to ELFObjectWriter
401 unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
402 bool IsPCRel);
Jason W Kim28ef0a52010-11-22 18:57:00 +0000403 private:
Jason W Kim4a511f02010-11-22 18:41:13 +0000404 static bool isFixupKindPCRel(unsigned Kind) {
405 switch (Kind) {
406 default:
407 return false;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +0000408 case FK_PCRel_1:
409 case FK_PCRel_2:
410 case FK_PCRel_4:
Jim Grosbachdff84b02010-12-02 00:28:45 +0000411 case ARM::fixup_arm_ldst_pcrel_12:
Owen Anderson9d63d902010-12-01 19:18:46 +0000412 case ARM::fixup_arm_pcrel_10:
Jason W Kimccbe0002010-11-22 18:47:05 +0000413 case ARM::fixup_arm_branch:
Jason W Kim4a511f02010-11-22 18:41:13 +0000414 return true;
415 }
416 }
Jason W Kimd3443e92010-11-15 16:18:39 +0000417 };
Wesley Peck4b047132010-11-21 22:06:28 +0000418
419 //===- MBlazeELFObjectWriter -------------------------------------------===//
420
421 class MBlazeELFObjectWriter : public ELFObjectWriter {
422 public:
423 MBlazeELFObjectWriter(raw_ostream &_OS, bool _Is64Bit, bool IsLittleEndian,
424 uint16_t _EMachine, bool _HasRelAddend,
425 Triple::OSType _OSType);
426
427 virtual ~MBlazeELFObjectWriter();
428 virtual void RecordRelocation(const MCAssembler &Asm,
429 const MCAsmLayout &Layout,
430 const MCFragment *Fragment,
431 const MCFixup &Fixup, MCValue Target,
432 uint64_t &FixedValue);
Jason W Kim4a511f02010-11-22 18:41:13 +0000433
Jason W Kim28ef0a52010-11-22 18:57:00 +0000434 private:
Jason W Kim4a511f02010-11-22 18:41:13 +0000435 static bool isFixupKindPCRel(unsigned Kind) {
436 switch (Kind) {
437 default:
438 return false;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +0000439 case FK_PCRel_1:
440 case FK_PCRel_2:
441 case FK_PCRel_4:
Jason W Kim4a511f02010-11-22 18:41:13 +0000442 return true;
443 }
444 }
Wesley Peck4b047132010-11-21 22:06:28 +0000445 };
Matt Fleming3565a062010-08-16 18:57:57 +0000446}
447
Jason W Kimd3443e92010-11-15 16:18:39 +0000448ELFObjectWriter::~ELFObjectWriter()
449{}
450
Matt Fleming3565a062010-08-16 18:57:57 +0000451// Emit the ELF header.
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000452void ELFObjectWriter::WriteHeader(uint64_t SectionDataSize,
453 unsigned NumberOfSections) {
Matt Fleming3565a062010-08-16 18:57:57 +0000454 // ELF Header
455 // ----------
456 //
457 // Note
458 // ----
459 // emitWord method behaves differently for ELF32 and ELF64, writing
460 // 4 bytes in the former and 8 in the latter.
461
462 Write8(0x7f); // e_ident[EI_MAG0]
463 Write8('E'); // e_ident[EI_MAG1]
464 Write8('L'); // e_ident[EI_MAG2]
465 Write8('F'); // e_ident[EI_MAG3]
466
467 Write8(Is64Bit ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
468
469 // e_ident[EI_DATA]
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000470 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
Matt Fleming3565a062010-08-16 18:57:57 +0000471
472 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky5baf79e2010-09-09 17:57:50 +0000473 // e_ident[EI_OSABI]
474 switch (OSType) {
475 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
476 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
477 default: Write8(ELF::ELFOSABI_NONE); break;
478 }
Matt Fleming3565a062010-08-16 18:57:57 +0000479 Write8(0); // e_ident[EI_ABIVERSION]
480
481 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
482
483 Write16(ELF::ET_REL); // e_type
484
Wesley Peckeecb8582010-10-22 15:52:49 +0000485 Write16(EMachine); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +0000486
487 Write32(ELF::EV_CURRENT); // e_version
488 WriteWord(0); // e_entry, no entry point in .o file
489 WriteWord(0); // e_phoff, no program header for .o
Benjamin Kramereb976772010-08-17 17:02:29 +0000490 WriteWord(SectionDataSize + (Is64Bit ? sizeof(ELF::Elf64_Ehdr) :
491 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000492
493 // FIXME: Make this configurable.
494 Write32(0); // e_flags = whatever the target wants
495
496 // e_ehsize = ELF header size
497 Write16(Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
498
499 Write16(0); // e_phentsize = prog header entry size
500 Write16(0); // e_phnum = # prog header entries = 0
501
502 // e_shentsize = Section header entry size
503 Write16(Is64Bit ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
504
505 // e_shnum = # of section header ents
Rafael Espindola7be2c332010-10-31 00:16:26 +0000506 if (NumberOfSections >= ELF::SHN_LORESERVE)
507 Write16(0);
508 else
509 Write16(NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000510
511 // e_shstrndx = Section # of '.shstrtab'
Rafael Espindola7be2c332010-10-31 00:16:26 +0000512 if (NumberOfSections >= ELF::SHN_LORESERVE)
513 Write16(ELF::SHN_XINDEX);
514 else
515 Write16(ShstrtabIndex);
Matt Fleming3565a062010-08-16 18:57:57 +0000516}
517
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000518void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
519 MCDataFragment *ShndxF,
520 uint64_t name,
521 uint8_t info, uint64_t value,
522 uint64_t size, uint8_t other,
523 uint32_t shndx,
524 bool Reserved) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000525 if (ShndxF) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000526 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000527 String32(*ShndxF, shndx);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000528 else
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000529 String32(*ShndxF, 0);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000530 }
531
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000532 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
533 uint16_t(ELF::SHN_XINDEX) : shndx;
534
Matt Fleming3565a062010-08-16 18:57:57 +0000535 if (Is64Bit) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000536 String32(*SymtabF, name); // st_name
537 String8(*SymtabF, info); // st_info
538 String8(*SymtabF, other); // st_other
539 String16(*SymtabF, Index); // st_shndx
540 String64(*SymtabF, value); // st_value
541 String64(*SymtabF, size); // st_size
Matt Fleming3565a062010-08-16 18:57:57 +0000542 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000543 String32(*SymtabF, name); // st_name
544 String32(*SymtabF, value); // st_value
545 String32(*SymtabF, size); // st_size
546 String8(*SymtabF, info); // st_info
547 String8(*SymtabF, other); // st_other
548 String16(*SymtabF, Index); // st_shndx
Matt Fleming3565a062010-08-16 18:57:57 +0000549 }
550}
551
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000552static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout) {
553 if (Data.isCommon() && Data.isExternal())
554 return Data.getCommonAlignment();
555
556 const MCSymbol &Symbol = Data.getSymbol();
557 if (!Symbol.isInSection())
558 return 0;
559
Rafael Espindola1973d432010-10-28 19:39:57 +0000560 if (MCFragment *FF = Data.getFragment())
561 return Layout.getSymbolAddress(&Data) -
562 Layout.getSectionAddress(FF->getParent());
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000563
564 return 0;
565}
566
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000567void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm) {
Rafael Espindola88182132010-10-27 15:18:17 +0000568 // The presence of symbol versions causes undefined symbols and
569 // versions declared with @@@ to be renamed.
570
571 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
572 ie = Asm.symbol_end(); it != ie; ++it) {
573 const MCSymbol &Alias = it->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000574 const MCSymbol &Symbol = Alias.AliasedSymbol();
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000575 MCSymbolData &SD = Asm.getSymbolData(Symbol);
576
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000577 // Not an alias.
578 if (&Symbol == &Alias)
579 continue;
580
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000581 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000582 size_t Pos = AliasName.find('@');
583 if (Pos == StringRef::npos)
584 continue;
585
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000586 // Aliases defined with .symvar copy the binding from the symbol they alias.
587 // This is the first place we are able to copy this information.
588 it->setExternal(SD.isExternal());
589 SetBinding(*it, GetBinding(SD));
590
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000591 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000592 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
593 continue;
594
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000595 // FIXME: produce a better error message.
596 if (Symbol.isUndefined() && Rest.startswith("@@") &&
597 !Rest.startswith("@@@"))
598 report_fatal_error("A @@ version cannot be undefined");
599
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000600 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000601 }
602}
603
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000604void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
605 MCDataFragment *ShndxF,
606 ELFSymbolData &MSD,
607 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000608 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000609 MCSymbolData &Data =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000610 Layout.getAssembler().getSymbolData(OrigData.getSymbol().AliasedSymbol());
Rafael Espindola152c1062010-10-06 21:02:29 +0000611
Rafael Espindola7be2c332010-10-31 00:16:26 +0000612 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
613 Data.getSymbol().isVariable();
614
Rafael Espindola152c1062010-10-06 21:02:29 +0000615 uint8_t Binding = GetBinding(OrigData);
616 uint8_t Visibility = GetVisibility(OrigData);
617 uint8_t Type = GetType(Data);
618
619 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
620 uint8_t Other = Visibility;
621
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000622 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000623 uint64_t Size = 0;
624 const MCExpr *ESize;
625
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000626 assert(!(Data.isCommon() && !Data.isExternal()));
627
Matt Fleming3565a062010-08-16 18:57:57 +0000628 ESize = Data.getSize();
629 if (Data.getSize()) {
630 MCValue Res;
631 if (ESize->getKind() == MCExpr::Binary) {
632 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(ESize);
633
634 if (BE->EvaluateAsRelocatable(Res, &Layout)) {
Benjamin Kramer24f12062010-10-17 07:38:40 +0000635 assert(!Res.getSymA() || !Res.getSymA()->getSymbol().isDefined());
636 assert(!Res.getSymB() || !Res.getSymB()->getSymbol().isDefined());
Rafael Espindolaf230df92010-10-16 18:23:53 +0000637 Size = Res.getConstant();
Matt Fleming3565a062010-08-16 18:57:57 +0000638 }
639 } else if (ESize->getKind() == MCExpr::Constant) {
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000640 Size = static_cast<const MCConstantExpr *>(ESize)->getValue();
Matt Fleming3565a062010-08-16 18:57:57 +0000641 } else {
642 assert(0 && "Unsupported size expression");
643 }
644 }
645
646 // Write out the symbol table entry
Rafael Espindola7be2c332010-10-31 00:16:26 +0000647 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
648 Size, Other, MSD.SectionIndex, IsReserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000649}
650
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000651void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
652 MCDataFragment *ShndxF,
653 const MCAssembler &Asm,
654 const MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000655 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000656 // The string table must be emitted first because we need the index
657 // into the string table for all the symbol names.
658 assert(StringTable.size() && "Missing string table");
659
660 // FIXME: Make sure the start of the symbol table is aligned.
661
662 // The first entry is the undefined symbol entry.
Rafael Espindola7be2c332010-10-31 00:16:26 +0000663 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
Matt Fleming3565a062010-08-16 18:57:57 +0000664
665 // Write the symbol table entries.
666 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
667 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
668 ELFSymbolData &MSD = LocalSymbolData[i];
Rafael Espindola7be2c332010-10-31 00:16:26 +0000669 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000670 }
671
Rafael Espindola71859c62010-09-16 19:46:31 +0000672 // Write out a symbol table entry for each regular section.
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000673 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
674 ++i) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000675 const MCSectionELF &Section =
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000676 static_cast<const MCSectionELF&>(i->getSection());
677 if (Section.getType() == ELF::SHT_RELA ||
678 Section.getType() == ELF::SHT_REL ||
679 Section.getType() == ELF::SHT_STRTAB ||
680 Section.getType() == ELF::SHT_SYMTAB)
Eli Friedmana44fa242010-08-16 21:17:09 +0000681 continue;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000682 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000683 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section), false);
Eli Friedmana44fa242010-08-16 21:17:09 +0000684 LastLocalSymbolIndex++;
685 }
Matt Fleming3565a062010-08-16 18:57:57 +0000686
687 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
688 ELFSymbolData &MSD = ExternalSymbolData[i];
689 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000690 assert(((Data.getFlags() & ELF_STB_Global) ||
691 (Data.getFlags() & ELF_STB_Weak)) &&
692 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000693 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000694 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000695 LastLocalSymbolIndex++;
696 }
697
698 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
699 ELFSymbolData &MSD = UndefinedSymbolData[i];
700 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000701 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000702 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000703 LastLocalSymbolIndex++;
704 }
705}
706
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000707const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm,
708 const MCValue &Target,
709 const MCFragment &F) const {
710 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000711 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
712 const MCSymbol *Renamed = Renames.lookup(&Symbol);
713 const MCSymbolData &SD = Asm.getSymbolData(Symbol);
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000714
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000715 if (ASymbol.isUndefined()) {
716 if (Renamed)
717 return Renamed;
718 return &ASymbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000719 }
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000720
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000721 if (SD.isExternal()) {
722 if (Renamed)
723 return Renamed;
724 return &Symbol;
725 }
Rafael Espindola73ffea42010-09-25 05:42:19 +0000726
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000727 const MCSectionELF &Section =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000728 static_cast<const MCSectionELF&>(ASymbol.getSection());
Rafael Espindola25958732010-11-24 21:57:39 +0000729 const SectionKind secKind = Section.getKind();
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000730
Rafael Espindola25958732010-11-24 21:57:39 +0000731 if (secKind.isBSS())
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000732 return NULL;
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000733
Rafael Espindola25958732010-11-24 21:57:39 +0000734 if (secKind.isThreadLocal()) {
735 if (Renamed)
736 return Renamed;
737 return &Symbol;
738 }
739
Rafael Espindola8cecf252010-10-06 16:23:36 +0000740 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000741 const MCSectionELF &Sec2 =
742 static_cast<const MCSectionELF&>(F.getParent()->getSection());
743
Rafael Espindola8cecf252010-10-06 16:23:36 +0000744 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000745 (Kind == MCSymbolRefExpr::VK_PLT ||
746 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
Rafael Espindola25958732010-11-24 21:57:39 +0000747 Kind == MCSymbolRefExpr::VK_GOTOFF)) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000748 if (Renamed)
749 return Renamed;
750 return &Symbol;
751 }
Rafael Espindola3729d002010-10-05 23:57:26 +0000752
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000753 if (Section.getFlags() & MCSectionELF::SHF_MERGE) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000754 if (Target.getConstant() == 0)
755 return NULL;
756 if (Renamed)
757 return Renamed;
758 return &Symbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000759 }
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000760
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000761 return NULL;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000762}
763
Matt Fleming3565a062010-08-16 18:57:57 +0000764
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000765uint64_t
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000766ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
767 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000768 MCSymbolData &SD = Asm.getSymbolData(*S);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000769 return SD.getIndex();
Matt Fleming3565a062010-08-16 18:57:57 +0000770}
771
Rafael Espindola737cd212010-10-05 18:01:23 +0000772static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
Rafael Espindola88182132010-10-27 15:18:17 +0000773 bool Used, bool Renamed) {
Rafael Espindola484291c2010-11-01 14:28:48 +0000774 if (Data.getFlags() & ELF_Other_Weakref)
775 return false;
776
Rafael Espindolabd701182010-10-19 19:31:37 +0000777 if (Used)
778 return true;
779
Rafael Espindola88182132010-10-27 15:18:17 +0000780 if (Renamed)
781 return false;
782
Rafael Espindola737cd212010-10-05 18:01:23 +0000783 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000784
Rafael Espindolad1798862010-10-29 23:09:31 +0000785 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
786 return true;
787
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000788 const MCSymbol &A = Symbol.AliasedSymbol();
Rafael Espindolad1798862010-10-29 23:09:31 +0000789 if (!A.isVariable() && A.isUndefined() && !Data.isCommon())
Rafael Espindolaa6866962010-10-27 14:44:52 +0000790 return false;
791
Rafael Espindola737cd212010-10-05 18:01:23 +0000792 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
793 return false;
794
Rafael Espindolabd701182010-10-19 19:31:37 +0000795 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000796 return false;
797
798 return true;
799}
800
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000801static bool isLocal(const MCSymbolData &Data, bool isSignature,
802 bool isUsedInReloc) {
Rafael Espindola737cd212010-10-05 18:01:23 +0000803 if (Data.isExternal())
804 return false;
805
806 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000807 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000808
809 if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
810 if (isSignature && !isUsedInReloc)
811 return true;
812
Rafael Espindola737cd212010-10-05 18:01:23 +0000813 return false;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000814 }
Rafael Espindola737cd212010-10-05 18:01:23 +0000815
816 return true;
817}
818
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000819void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
820 SectionIndexMapTy &SectionIndexMap) {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000821 unsigned Index = 1;
822 for (MCAssembler::iterator it = Asm.begin(),
823 ie = Asm.end(); it != ie; ++it) {
824 const MCSectionELF &Section =
825 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000826 if (Section.getType() != ELF::SHT_GROUP)
827 continue;
828 SectionIndexMap[&Section] = Index++;
829 }
830
831 for (MCAssembler::iterator it = Asm.begin(),
832 ie = Asm.end(); it != ie; ++it) {
833 const MCSectionELF &Section =
834 static_cast<const MCSectionELF &>(it->getSection());
835 if (Section.getType() == ELF::SHT_GROUP)
836 continue;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000837 SectionIndexMap[&Section] = Index++;
838 }
839}
840
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000841void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000842 const SectionIndexMapTy &SectionIndexMap,
843 RevGroupMapTy RevGroupMap) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000844 // FIXME: Is this the correct place to do this?
845 if (NeedsGOT) {
846 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
847 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
848 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
849 Data.setExternal(true);
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000850 SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindola5c77c162010-10-05 15:48:37 +0000851 }
852
Matt Fleming3565a062010-08-16 18:57:57 +0000853 // Build section lookup table.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000854 int NumRegularSections = Asm.size();
Matt Fleming3565a062010-08-16 18:57:57 +0000855
856 // Index 0 is always the empty string.
857 StringMap<uint64_t> StringIndexMap;
858 StringTable += '\x00';
859
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000860 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000861 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
862 ie = Asm.symbol_end(); it != ie; ++it) {
863 const MCSymbol &Symbol = it->getSymbol();
864
Rafael Espindola484291c2010-11-01 14:28:48 +0000865 bool Used = UsedInReloc.count(&Symbol);
866 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000867 bool isSignature = RevGroupMap.count(&Symbol);
868
869 if (!isInSymtab(Asm, *it,
870 Used || WeakrefUsed || isSignature,
Rafael Espindola88182132010-10-27 15:18:17 +0000871 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000872 continue;
873
Matt Fleming3565a062010-08-16 18:57:57 +0000874 ELFSymbolData MSD;
875 MSD.SymbolData = it;
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000876 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Matt Fleming3565a062010-08-16 18:57:57 +0000877
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000878 // Undefined symbols are global, but this is the first place we
879 // are able to set it.
880 bool Local = isLocal(*it, isSignature, Used);
881 if (!Local && GetBinding(*it) == ELF::STB_LOCAL) {
882 MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
883 SetBinding(*it, ELF::STB_GLOBAL);
884 SetBinding(SD, ELF::STB_GLOBAL);
885 }
886
Rafael Espindola484291c2010-11-01 14:28:48 +0000887 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
888 SetBinding(*it, ELF::STB_WEAK);
889
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000890 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000891 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000892 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000893 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000894 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000895 } else if (RefSymbol.isUndefined()) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000896 if (isSignature && !Used)
897 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
898 else
899 MSD.SectionIndex = ELF::SHN_UNDEF;
Matt Fleming3565a062010-08-16 18:57:57 +0000900 } else {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000901 const MCSectionELF &Section =
902 static_cast<const MCSectionELF&>(RefSymbol.getSection());
903 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000904 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
905 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000906 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000907 }
908
Rafael Espindola88182132010-10-27 15:18:17 +0000909 // The @@@ in symbol version is replaced with @ in undefined symbols and
910 // @@ in defined ones.
911 StringRef Name = Symbol.getName();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000912 SmallString<32> Buf;
913
Rafael Espindola88182132010-10-27 15:18:17 +0000914 size_t Pos = Name.find("@@@");
Rafael Espindola88182132010-10-27 15:18:17 +0000915 if (Pos != StringRef::npos) {
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000916 Buf += Name.substr(0, Pos);
917 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
918 Buf += Name.substr(Pos + Skip);
919 Name = Buf;
Rafael Espindola88182132010-10-27 15:18:17 +0000920 }
921
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000922 uint64_t &Entry = StringIndexMap[Name];
Rafael Espindolaa6866962010-10-27 14:44:52 +0000923 if (!Entry) {
924 Entry = StringTable.size();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000925 StringTable += Name;
Rafael Espindolaa6866962010-10-27 14:44:52 +0000926 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +0000927 }
Rafael Espindolaa6866962010-10-27 14:44:52 +0000928 MSD.StringIndex = Entry;
929 if (MSD.SectionIndex == ELF::SHN_UNDEF)
930 UndefinedSymbolData.push_back(MSD);
931 else if (Local)
932 LocalSymbolData.push_back(MSD);
933 else
934 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +0000935 }
936
937 // Symbols are required to be in lexicographic order.
938 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
939 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
940 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
941
942 // Set the symbol indices. Local symbols must come before all other
943 // symbols with non-local bindings.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000944 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000945 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
946 LocalSymbolData[i].SymbolData->setIndex(Index++);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000947
948 Index += NumRegularSections;
949
Matt Fleming3565a062010-08-16 18:57:57 +0000950 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
951 ExternalSymbolData[i].SymbolData->setIndex(Index++);
952 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
953 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
954}
955
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000956void ELFObjectWriter::WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
957 const MCSectionData &SD) {
Matt Fleming3565a062010-08-16 18:57:57 +0000958 if (!Relocations[&SD].empty()) {
959 MCContext &Ctx = Asm.getContext();
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000960 const MCSectionELF *RelaSection;
Matt Fleming3565a062010-08-16 18:57:57 +0000961 const MCSectionELF &Section =
962 static_cast<const MCSectionELF&>(SD.getSection());
963
964 const StringRef SectionName = Section.getSectionName();
Benjamin Kramer377a5722010-08-17 17:30:07 +0000965 std::string RelaSectionName = HasRelocationAddend ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +0000966 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000967
968 unsigned EntrySize;
969 if (HasRelocationAddend)
970 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
971 else
972 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +0000973
Benjamin Kramer377a5722010-08-17 17:30:07 +0000974 RelaSection = Ctx.getELFSection(RelaSectionName, HasRelocationAddend ?
975 ELF::SHT_RELA : ELF::SHT_REL, 0,
Matt Fleming3565a062010-08-16 18:57:57 +0000976 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000977 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +0000978
979 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Benjamin Kramera9eadca2010-09-06 16:11:52 +0000980 RelaSD.setAlignment(Is64Bit ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +0000981
982 MCDataFragment *F = new MCDataFragment(&RelaSD);
983
984 WriteRelocationsFragment(Asm, F, &SD);
985
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000986 Asm.AddSectionToTheEnd(*this, RelaSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000987 }
988}
989
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000990void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
991 uint64_t Flags, uint64_t Address,
992 uint64_t Offset, uint64_t Size,
993 uint32_t Link, uint32_t Info,
994 uint64_t Alignment,
995 uint64_t EntrySize) {
Matt Fleming3565a062010-08-16 18:57:57 +0000996 Write32(Name); // sh_name: index into string table
997 Write32(Type); // sh_type
998 WriteWord(Flags); // sh_flags
999 WriteWord(Address); // sh_addr
1000 WriteWord(Offset); // sh_offset
1001 WriteWord(Size); // sh_size
1002 Write32(Link); // sh_link
1003 Write32(Info); // sh_info
1004 WriteWord(Alignment); // sh_addralign
1005 WriteWord(EntrySize); // sh_entsize
1006}
1007
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001008void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
1009 MCDataFragment *F,
1010 const MCSectionData *SD) {
Matt Fleming3565a062010-08-16 18:57:57 +00001011 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
1012 // sort by the r_offset just like gnu as does
1013 array_pod_sort(Relocs.begin(), Relocs.end());
1014
1015 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1016 ELFRelocationEntry entry = Relocs[e - i - 1];
1017
Rafael Espindola12203cc2010-11-21 00:48:25 +00001018 if (!entry.Index)
1019 ;
1020 else if (entry.Index < 0)
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001021 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
1022 else
Rafael Espindola12203cc2010-11-21 00:48:25 +00001023 entry.Index += LocalSymbolData.size();
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001024 if (Is64Bit) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001025 String64(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001026
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001027 struct ELF::Elf64_Rela ERE64;
1028 ERE64.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001029 String64(*F, ERE64.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001030
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001031 if (HasRelocationAddend)
1032 String64(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001033 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001034 String32(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001035
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001036 struct ELF::Elf32_Rela ERE32;
1037 ERE32.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001038 String32(*F, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001039
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001040 if (HasRelocationAddend)
1041 String32(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001042 }
Matt Fleming3565a062010-08-16 18:57:57 +00001043 }
1044}
1045
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001046void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
1047 MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001048 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +00001049 MCContext &Ctx = Asm.getContext();
1050 MCDataFragment *F;
1051
Matt Fleming3565a062010-08-16 18:57:57 +00001052 unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
1053
Rafael Espindola38738bf2010-09-22 19:04:41 +00001054 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001055 const MCSectionELF *ShstrtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001056 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001057 SectionKind::getReadOnly());
Rafael Espindola71859c62010-09-16 19:46:31 +00001058 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1059 ShstrtabSD.setAlignment(1);
1060 ShstrtabIndex = Asm.size();
1061
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001062 const MCSectionELF *SymtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001063 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1064 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001065 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001066 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Matt Fleming3565a062010-08-16 18:57:57 +00001067 SymtabSD.setAlignment(Is64Bit ? 8 : 4);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001068 SymbolTableIndex = Asm.size();
1069
1070 MCSectionData *SymtabShndxSD = NULL;
1071
1072 if (NeedsSymtabShndx) {
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001073 const MCSectionELF *SymtabShndxSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001074 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001075 SectionKind::getReadOnly(), 4, "");
Rafael Espindola7be2c332010-10-31 00:16:26 +00001076 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
1077 SymtabShndxSD->setAlignment(4);
1078 }
Matt Fleming3565a062010-08-16 18:57:57 +00001079
Matt Fleming3565a062010-08-16 18:57:57 +00001080 const MCSection *StrtabSection;
1081 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001082 SectionKind::getReadOnly());
Matt Fleming3565a062010-08-16 18:57:57 +00001083 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1084 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +00001085 StringTableIndex = Asm.size();
1086
Rafael Espindolac3c413f2010-09-27 22:04:54 +00001087 WriteRelocations(Asm, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001088
1089 // Symbol table
1090 F = new MCDataFragment(&SymtabSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001091 MCDataFragment *ShndxF = NULL;
1092 if (NeedsSymtabShndx) {
1093 ShndxF = new MCDataFragment(SymtabShndxSD);
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001094 Asm.AddSectionToTheEnd(*this, *SymtabShndxSD, Layout);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001095 }
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001096 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001097 Asm.AddSectionToTheEnd(*this, SymtabSD, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001098
Matt Fleming3565a062010-08-16 18:57:57 +00001099 F = new MCDataFragment(&StrtabSD);
1100 F->getContents().append(StringTable.begin(), StringTable.end());
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001101 Asm.AddSectionToTheEnd(*this, StrtabSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +00001102
Matt Fleming3565a062010-08-16 18:57:57 +00001103 F = new MCDataFragment(&ShstrtabSD);
1104
Matt Fleming3565a062010-08-16 18:57:57 +00001105 // Section header string table.
1106 //
1107 // The first entry of a string table holds a null character so skip
1108 // section 0.
1109 uint64_t Index = 1;
1110 F->getContents() += '\x00';
1111
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001112 StringMap<uint64_t> SecStringMap;
Matt Fleming3565a062010-08-16 18:57:57 +00001113 for (MCAssembler::const_iterator it = Asm.begin(),
1114 ie = Asm.end(); it != ie; ++it) {
Matt Fleming3565a062010-08-16 18:57:57 +00001115 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +00001116 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola51efe7a2010-09-23 14:14:56 +00001117 // FIXME: We could merge suffixes like in .text and .rela.text.
Matt Fleming3565a062010-08-16 18:57:57 +00001118
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001119 StringRef Name = Section.getSectionName();
1120 if (SecStringMap.count(Name)) {
1121 SectionStringTableIndex[&Section] = SecStringMap[Name];
1122 continue;
1123 }
Matt Fleming3565a062010-08-16 18:57:57 +00001124 // Remember the index into the string table so we can write it
1125 // into the sh_name field of the section header table.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001126 SectionStringTableIndex[&Section] = Index;
1127 SecStringMap[Name] = Index;
Matt Fleming3565a062010-08-16 18:57:57 +00001128
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001129 Index += Name.size() + 1;
1130 F->getContents() += Name;
Matt Fleming3565a062010-08-16 18:57:57 +00001131 F->getContents() += '\x00';
1132 }
1133
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001134 Asm.AddSectionToTheEnd(*this, ShstrtabSD, Layout);
Rafael Espindola70703872010-09-30 02:22:20 +00001135}
1136
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001137bool ELFObjectWriter::IsFixupFullyResolved(const MCAssembler &Asm,
1138 const MCValue Target,
1139 bool IsPCRel,
1140 const MCFragment *DF) const {
Rafael Espindola70703872010-09-30 02:22:20 +00001141 // If this is a PCrel relocation, find the section this fixup value is
1142 // relative to.
1143 const MCSection *BaseSection = 0;
1144 if (IsPCRel) {
1145 BaseSection = &DF->getParent()->getSection();
1146 assert(BaseSection);
1147 }
1148
1149 const MCSection *SectionA = 0;
1150 const MCSymbol *SymbolA = 0;
1151 if (const MCSymbolRefExpr *A = Target.getSymA()) {
Rafael Espindola2c920852010-11-16 04:11:46 +00001152 SymbolA = &A->getSymbol();
1153 SectionA = &SymbolA->AliasedSymbol().getSection();
Rafael Espindola70703872010-09-30 02:22:20 +00001154 }
1155
1156 const MCSection *SectionB = 0;
Rafael Espindola12203cc2010-11-21 00:48:25 +00001157 const MCSymbol *SymbolB = 0;
Rafael Espindola70703872010-09-30 02:22:20 +00001158 if (const MCSymbolRefExpr *B = Target.getSymB()) {
Rafael Espindola12203cc2010-11-21 00:48:25 +00001159 SymbolB = &B->getSymbol();
1160 SectionB = &SymbolB->AliasedSymbol().getSection();
Rafael Espindola70703872010-09-30 02:22:20 +00001161 }
1162
1163 if (!BaseSection)
1164 return SectionA == SectionB;
1165
Rafael Espindola12203cc2010-11-21 00:48:25 +00001166 if (SymbolB)
1167 return false;
1168
1169 // Absolute address but PCrel instruction, so we need a relocation.
1170 if (!SymbolA)
1171 return false;
1172
Rafael Espindola2c920852010-11-16 04:11:46 +00001173 // FIXME: This is in here just to match gnu as output. If the two ends
1174 // are in the same section, there is nothing that the linker can do to
1175 // break it.
Rafael Espindola70703872010-09-30 02:22:20 +00001176 const MCSymbolData &DataA = Asm.getSymbolData(*SymbolA);
1177 if (DataA.isExternal())
1178 return false;
1179
Rafael Espindola12203cc2010-11-21 00:48:25 +00001180 return BaseSection == SectionA;
Matt Fleming3565a062010-08-16 18:57:57 +00001181}
1182
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001183void ELFObjectWriter::CreateGroupSections(MCAssembler &Asm,
1184 MCAsmLayout &Layout,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001185 GroupMapTy &GroupMap,
1186 RevGroupMapTy &RevGroupMap) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001187 // Build the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001188 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1189 it != ie; ++it) {
1190 const MCSectionELF &Section =
1191 static_cast<const MCSectionELF&>(it->getSection());
1192 if (!(Section.getFlags() & MCSectionELF::SHF_GROUP))
1193 continue;
1194
1195 const MCSymbol *SignatureSymbol = Section.getGroup();
1196 Asm.getOrCreateSymbolData(*SignatureSymbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001197 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001198 if (!Group) {
1199 Group = Asm.getContext().CreateELFGroupSection();
1200 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1201 Data.setAlignment(4);
1202 MCDataFragment *F = new MCDataFragment(&Data);
1203 String32(*F, ELF::GRP_COMDAT);
1204 }
1205 GroupMap[Group] = SignatureSymbol;
1206 }
1207
1208 // Add sections to the groups
1209 unsigned Index = 1;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001210 unsigned NumGroups = RevGroupMap.size();
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001211 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1212 it != ie; ++it, ++Index) {
1213 const MCSectionELF &Section =
1214 static_cast<const MCSectionELF&>(it->getSection());
1215 if (!(Section.getFlags() & MCSectionELF::SHF_GROUP))
1216 continue;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001217 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001218 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1219 // FIXME: we could use the previous fragment
1220 MCDataFragment *F = new MCDataFragment(&Data);
1221 String32(*F, NumGroups + Index);
1222 }
1223
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001224 for (RevGroupMapTy::const_iterator i = RevGroupMap.begin(),
1225 e = RevGroupMap.end(); i != e; ++i) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001226 const MCSectionELF *Group = i->second;
1227 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001228 Asm.AddSectionToTheEnd(*this, Data, Layout);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001229 }
1230}
1231
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001232void ELFObjectWriter::WriteSection(MCAssembler &Asm,
1233 const SectionIndexMapTy &SectionIndexMap,
1234 uint32_t GroupSymbolIndex,
1235 uint64_t Offset, uint64_t Size,
1236 uint64_t Alignment,
1237 const MCSectionELF &Section) {
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001238 uint64_t sh_link = 0;
1239 uint64_t sh_info = 0;
1240
1241 switch(Section.getType()) {
1242 case ELF::SHT_DYNAMIC:
1243 sh_link = SectionStringTableIndex[&Section];
1244 sh_info = 0;
1245 break;
1246
1247 case ELF::SHT_REL:
1248 case ELF::SHT_RELA: {
1249 const MCSectionELF *SymtabSection;
1250 const MCSectionELF *InfoSection;
1251 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
1252 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001253 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001254 sh_link = SectionIndexMap.lookup(SymtabSection);
1255 assert(sh_link && ".symtab not found");
1256
1257 // Remove ".rel" and ".rela" prefixes.
1258 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1259 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
1260
1261 InfoSection = Asm.getContext().getELFSection(SectionName,
1262 ELF::SHT_PROGBITS, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001263 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001264 sh_info = SectionIndexMap.lookup(InfoSection);
1265 break;
1266 }
1267
1268 case ELF::SHT_SYMTAB:
1269 case ELF::SHT_DYNSYM:
1270 sh_link = StringTableIndex;
1271 sh_info = LastLocalSymbolIndex;
1272 break;
1273
1274 case ELF::SHT_SYMTAB_SHNDX:
1275 sh_link = SymbolTableIndex;
1276 break;
1277
1278 case ELF::SHT_PROGBITS:
1279 case ELF::SHT_STRTAB:
1280 case ELF::SHT_NOBITS:
1281 case ELF::SHT_NULL:
1282 case ELF::SHT_ARM_ATTRIBUTES:
1283 // Nothing to do.
1284 break;
1285
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001286 case ELF::SHT_GROUP: {
1287 sh_link = SymbolTableIndex;
1288 sh_info = GroupSymbolIndex;
1289 break;
1290 }
1291
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001292 default:
1293 assert(0 && "FIXME: sh_type value not supported!");
1294 break;
1295 }
1296
1297 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1298 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1299 Alignment, Section.getEntrySize());
1300}
1301
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001302void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1303 const MCAsmLayout &Layout) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001304 GroupMapTy GroupMap;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001305 RevGroupMapTy RevGroupMap;
1306 CreateGroupSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1307 RevGroupMap);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001308
Rafael Espindolabab2a802010-11-10 21:51:05 +00001309 SectionIndexMapTy SectionIndexMap;
1310
1311 ComputeIndexMap(Asm, SectionIndexMap);
1312
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001313 // Compute symbol table information.
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001314 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap);
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001315
Matt Fleming3565a062010-08-16 18:57:57 +00001316 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001317 const_cast<MCAsmLayout&>(Layout),
1318 SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +00001319
Rafael Espindola1d739a02010-11-10 22:34:07 +00001320 // Update to include the metadata sections.
1321 ComputeIndexMap(Asm, SectionIndexMap);
1322
Matt Fleming3565a062010-08-16 18:57:57 +00001323 // Add 1 for the null section.
1324 unsigned NumSections = Asm.size() + 1;
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001325 uint64_t NaturalAlignment = Is64Bit ? 8 : 4;
1326 uint64_t HeaderSize = Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr);
1327 uint64_t FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001328
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001329 std::vector<const MCSectionELF*> Sections;
1330 Sections.resize(NumSections);
1331
1332 for (SectionIndexMapTy::const_iterator i=
1333 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1334 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
1335 Sections[p.second] = p.first;
1336 }
1337
1338 for (unsigned i = 1; i < NumSections; ++i) {
1339 const MCSectionELF &Section = *Sections[i];
1340 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001341
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001342 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1343
Matt Fleming3565a062010-08-16 18:57:57 +00001344 // Get the size of the section in the output file (including padding).
1345 uint64_t Size = Layout.getSectionFileSize(&SD);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001346
1347 FileOff += Size;
Matt Fleming3565a062010-08-16 18:57:57 +00001348 }
1349
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001350 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1351
Matt Fleming3565a062010-08-16 18:57:57 +00001352 // Write out the ELF header ...
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001353 WriteHeader(FileOff - HeaderSize, NumSections);
1354
1355 FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001356
1357 // ... then all of the sections ...
1358 DenseMap<const MCSection*, uint64_t> SectionOffsetMap;
1359
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001360 for (unsigned i = 1; i < NumSections; ++i) {
1361 const MCSectionELF &Section = *Sections[i];
1362 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001363
1364 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1365 WriteZeros(Padding);
1366 FileOff += Padding;
1367
Matt Fleming3565a062010-08-16 18:57:57 +00001368 // Remember the offset into the file for this section.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001369 SectionOffsetMap[&Section] = FileOff;
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001370
Matt Fleming3565a062010-08-16 18:57:57 +00001371 FileOff += Layout.getSectionFileSize(&SD);
1372
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001373 Asm.WriteSectionData(&SD, Layout, this);
Matt Fleming3565a062010-08-16 18:57:57 +00001374 }
1375
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001376 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1377 WriteZeros(Padding);
1378 FileOff += Padding;
1379
Matt Fleming3565a062010-08-16 18:57:57 +00001380 // ... and then the section header table.
1381 // Should we align the section header table?
1382 //
1383 // Null section first.
Rafael Espindola7be2c332010-10-31 00:16:26 +00001384 uint64_t FirstSectionSize =
1385 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1386 uint32_t FirstSectionLink =
1387 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1388 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming3565a062010-08-16 18:57:57 +00001389
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001390 for (unsigned i = 1; i < NumSections; ++i) {
1391 const MCSectionELF &Section = *Sections[i];
1392 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1393 uint32_t GroupSymbolIndex;
1394 if (Section.getType() != ELF::SHT_GROUP)
1395 GroupSymbolIndex = 0;
1396 else
1397 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm, GroupMap[&Section]);
Matt Fleming3565a062010-08-16 18:57:57 +00001398
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001399 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
1400 SectionOffsetMap[&Section], Layout.getSectionSize(&SD),
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001401 SD.getAlignment(), Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001402 }
1403}
1404
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001405MCObjectWriter *llvm::createELFObjectWriter(raw_ostream &OS,
1406 bool Is64Bit,
1407 Triple::OSType OSType,
1408 uint16_t EMachine,
1409 bool IsLittleEndian,
1410 bool HasRelocationAddend) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001411 switch (EMachine) {
1412 case ELF::EM_386:
1413 case ELF::EM_X86_64:
1414 return new X86ELFObjectWriter(OS, Is64Bit, IsLittleEndian, EMachine,
1415 HasRelocationAddend, OSType); break;
1416 case ELF::EM_ARM:
1417 return new ARMELFObjectWriter(OS, Is64Bit, IsLittleEndian, EMachine,
1418 HasRelocationAddend, OSType); break;
Wesley Peck4b047132010-11-21 22:06:28 +00001419 case ELF::EM_MBLAZE:
1420 return new MBlazeELFObjectWriter(OS, Is64Bit, IsLittleEndian, EMachine,
1421 HasRelocationAddend, OSType); break;
Benjamin Kramer32858772010-11-15 19:20:50 +00001422 default: llvm_unreachable("Unsupported architecture"); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001423 }
1424}
1425
1426
1427/// START OF SUBCLASSES for ELFObjectWriter
1428//===- ARMELFObjectWriter -------------------------------------------===//
1429
1430ARMELFObjectWriter::ARMELFObjectWriter(raw_ostream &_OS, bool _Is64Bit,
1431 bool _IsLittleEndian,
1432 uint16_t _EMachine, bool _HasRelocationAddend,
1433 Triple::OSType _OSType)
1434 : ELFObjectWriter(_OS, _Is64Bit, _IsLittleEndian, _EMachine,
1435 _HasRelocationAddend, _OSType)
1436{}
1437
1438ARMELFObjectWriter::~ARMELFObjectWriter()
1439{}
1440
Jason W Kim85fed5e2010-12-01 02:40:06 +00001441unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target,
1442 const MCFixup &Fixup,
1443 bool IsPCRel) {
1444 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1445 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
1446
1447 if (IsPCRel) {
1448 switch (Modifier) {
1449 default: assert(0 && "Unimplemented Modifier");
1450 case MCSymbolRefExpr::VK_None: break;
1451 }
1452 switch ((unsigned)Fixup.getKind()) {
1453 default: assert(0 && "Unimplemented");
1454 case ARM::fixup_arm_branch: return ELF::R_ARM_CALL; break;
1455 }
1456 } else {
1457 switch ((unsigned)Fixup.getKind()) {
1458 default: llvm_unreachable("invalid fixup kind!");
Jim Grosbachdff84b02010-12-02 00:28:45 +00001459 case ARM::fixup_arm_ldst_pcrel_12:
Owen Anderson9d63d902010-12-01 19:18:46 +00001460 case ARM::fixup_arm_pcrel_10:
Jim Grosbachdff84b02010-12-02 00:28:45 +00001461 case ARM::fixup_arm_adr_pcrel_12:
Jason W Kim85fed5e2010-12-01 02:40:06 +00001462 assert(0 && "Unimplemented"); break;
1463 case ARM::fixup_arm_branch:
1464 return ELF::R_ARM_CALL; break;
1465 case ARM::fixup_arm_movt_hi16:
1466 return ELF::R_ARM_MOVT_ABS; break;
1467 case ARM::fixup_arm_movw_lo16:
1468 return ELF::R_ARM_MOVW_ABS_NC; break;
1469 }
1470 }
1471
1472 if (RelocNeedsGOT(Modifier))
1473 NeedsGOT = true;
1474 return -1;
1475}
1476
Jason W Kimd3443e92010-11-15 16:18:39 +00001477void ARMELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
1478 const MCAsmLayout &Layout,
1479 const MCFragment *Fragment,
1480 const MCFixup &Fixup,
1481 MCValue Target,
1482 uint64_t &FixedValue) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001483 int64_t Addend = 0;
1484 int Index = 0;
1485 int64_t Value = Target.getConstant();
1486 const MCSymbol *RelocSymbol = NULL;
1487
1488 bool IsPCRel = isFixupKindPCRel(Fixup.getKind());
1489 if (!Target.isAbsolute()) {
1490 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
1491 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
1492 RelocSymbol = SymbolToReloc(Asm, Target, *Fragment);
1493
1494 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
1495 const MCSymbol &SymbolB = RefB->getSymbol();
1496 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
1497 IsPCRel = true;
1498 MCSectionData *Sec = Fragment->getParent();
1499
1500 // Offset of the symbol in the section
1501 int64_t a = Layout.getSymbolAddress(&SDB) - Layout.getSectionAddress(Sec);
1502
1503 // Ofeset of the relocation in the section
1504 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
1505 Value += b - a;
1506 }
1507
1508 if (!RelocSymbol) {
1509 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
1510 MCFragment *F = SD.getFragment();
1511
1512 Index = F->getParent()->getOrdinal() + 1;
1513
1514 MCSectionData *FSD = F->getParent();
1515 // Offset of the symbol in the section
1516 Value += Layout.getSymbolAddress(&SD) - Layout.getSectionAddress(FSD);
1517 } else {
1518 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
1519 WeakrefUsedInReloc.insert(RelocSymbol);
1520 else
1521 UsedInReloc.insert(RelocSymbol);
1522 Index = -1;
1523 }
1524 Addend = Value;
1525 // Compensate for the addend on i386.
1526 if (Is64Bit)
1527 Value = 0;
1528 }
1529
1530 FixedValue = Value;
1531
1532 // determine the type of the relocation
1533 unsigned Type = GetRelocType(Target, Fixup, IsPCRel);
1534
1535 uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
1536 Fixup.getOffset();
1537
1538 if (!HasRelocationAddend) Addend = 0;
1539 ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend);
1540 Relocations[Fragment->getParent()].push_back(ERE);
Jason W Kimd3443e92010-11-15 16:18:39 +00001541}
1542
Wesley Peck4b047132010-11-21 22:06:28 +00001543//===- MBlazeELFObjectWriter -------------------------------------------===//
Jason W Kimd3443e92010-11-15 16:18:39 +00001544
Wesley Peck4b047132010-11-21 22:06:28 +00001545MBlazeELFObjectWriter::MBlazeELFObjectWriter(raw_ostream &_OS, bool _Is64Bit,
1546 bool _IsLittleEndian,
1547 uint16_t _EMachine,
1548 bool _HasRelocationAddend,
1549 Triple::OSType _OSType)
1550 : ELFObjectWriter(_OS, _Is64Bit, _IsLittleEndian, _EMachine,
1551 _HasRelocationAddend, _OSType) {
1552}
1553
1554MBlazeELFObjectWriter::~MBlazeELFObjectWriter() {
1555}
1556
1557void MBlazeELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
1558 const MCAsmLayout &Layout,
1559 const MCFragment *Fragment,
1560 const MCFixup &Fixup,
1561 MCValue Target,
1562 uint64_t &FixedValue) {
1563 int64_t Addend = 0;
1564 int Index = 0;
1565 int64_t Value = Target.getConstant();
1566 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
1567 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
1568 const MCSymbol *RelocSymbol = SymbolToReloc(Asm, Target, *Fragment);
1569
Jason W Kim4a511f02010-11-22 18:41:13 +00001570 bool IsPCRel = isFixupKindPCRel(Fixup.getKind());
Wesley Peck4b047132010-11-21 22:06:28 +00001571 if (!Target.isAbsolute()) {
1572 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
1573 const MCSymbol &SymbolB = RefB->getSymbol();
1574 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
1575 IsPCRel = true;
1576 MCSectionData *Sec = Fragment->getParent();
1577
1578 // Offset of the symbol in the section
1579 int64_t a = Layout.getSymbolAddress(&SDB) - Layout.getSectionAddress(Sec);
1580
1581 // Ofeset of the relocation in the section
1582 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
1583 Value += b - a;
1584 }
1585
1586 if (!RelocSymbol) {
1587 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
1588 MCFragment *F = SD.getFragment();
1589
1590 Index = F->getParent()->getOrdinal();
1591
1592 MCSectionData *FSD = F->getParent();
1593 // Offset of the symbol in the section
1594 Value += Layout.getSymbolAddress(&SD) - Layout.getSectionAddress(FSD);
1595 } else {
1596 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
1597 WeakrefUsedInReloc.insert(RelocSymbol);
1598 else
1599 UsedInReloc.insert(RelocSymbol);
1600 Index = -1;
1601 }
1602 Addend = Value;
1603 }
1604
1605 FixedValue = Value;
1606
1607 // determine the type of the relocation
1608 unsigned Type;
1609 if (IsPCRel) {
1610 switch ((unsigned)Fixup.getKind()) {
1611 default:
1612 llvm_unreachable("Unimplemented");
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001613 case FK_PCRel_4:
Wesley Peck4b047132010-11-21 22:06:28 +00001614 Type = ELF::R_MICROBLAZE_64_PCREL;
1615 break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001616 case FK_PCRel_2:
Wesley Peck4b047132010-11-21 22:06:28 +00001617 Type = ELF::R_MICROBLAZE_32_PCREL;
1618 break;
1619 }
1620 } else {
1621 switch ((unsigned)Fixup.getKind()) {
1622 default: llvm_unreachable("invalid fixup kind!");
1623 case FK_Data_4:
1624 Type = (RelocSymbol || Addend !=0) ? ELF::R_MICROBLAZE_32
1625 : ELF::R_MICROBLAZE_64;
1626 break;
1627 case FK_Data_2:
1628 Type = ELF::R_MICROBLAZE_32;
1629 break;
1630 }
1631 }
1632
1633 MCSymbolRefExpr::VariantKind Modifier = Target.getSymA()->getKind();
1634 if (RelocNeedsGOT(Modifier))
1635 NeedsGOT = true;
1636
Jason W Kim858e7502010-11-22 18:42:07 +00001637 uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
Jason W Kim4a511f02010-11-22 18:41:13 +00001638 Fixup.getOffset();
Wesley Peck4b047132010-11-21 22:06:28 +00001639
Jason W Kim10907422010-11-22 22:05:16 +00001640 if (!HasRelocationAddend) Addend = 0;
Jason W Kim4a511f02010-11-22 18:41:13 +00001641 ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend);
Wesley Peck4b047132010-11-21 22:06:28 +00001642 Relocations[Fragment->getParent()].push_back(ERE);
1643}
Jason W Kimd3443e92010-11-15 16:18:39 +00001644
1645//===- X86ELFObjectWriter -------------------------------------------===//
1646
1647
1648X86ELFObjectWriter::X86ELFObjectWriter(raw_ostream &_OS, bool _Is64Bit,
1649 bool _IsLittleEndian,
1650 uint16_t _EMachine, bool _HasRelocationAddend,
1651 Triple::OSType _OSType)
1652 : ELFObjectWriter(_OS, _Is64Bit, _IsLittleEndian, _EMachine,
1653 _HasRelocationAddend, _OSType)
1654{}
1655
1656X86ELFObjectWriter::~X86ELFObjectWriter()
1657{}
1658
1659void X86ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
1660 const MCAsmLayout &Layout,
1661 const MCFragment *Fragment,
1662 const MCFixup &Fixup,
1663 MCValue Target,
1664 uint64_t &FixedValue) {
1665 int64_t Addend = 0;
1666 int Index = 0;
1667 int64_t Value = Target.getConstant();
Rafael Espindola12203cc2010-11-21 00:48:25 +00001668 const MCSymbol *RelocSymbol = NULL;
Jason W Kimd3443e92010-11-15 16:18:39 +00001669
Jason W Kim4a511f02010-11-22 18:41:13 +00001670 bool IsPCRel = isFixupKindPCRel(Fixup.getKind());
Jason W Kimd3443e92010-11-15 16:18:39 +00001671 if (!Target.isAbsolute()) {
Rafael Espindola12203cc2010-11-21 00:48:25 +00001672 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
1673 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
1674 RelocSymbol = SymbolToReloc(Asm, Target, *Fragment);
1675
Jason W Kimd3443e92010-11-15 16:18:39 +00001676 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
1677 const MCSymbol &SymbolB = RefB->getSymbol();
1678 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
1679 IsPCRel = true;
1680 MCSectionData *Sec = Fragment->getParent();
1681
1682 // Offset of the symbol in the section
1683 int64_t a = Layout.getSymbolAddress(&SDB) - Layout.getSectionAddress(Sec);
1684
1685 // Ofeset of the relocation in the section
1686 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
1687 Value += b - a;
1688 }
1689
1690 if (!RelocSymbol) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +00001691 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
Jason W Kimd3443e92010-11-15 16:18:39 +00001692 MCFragment *F = SD.getFragment();
1693
Rafael Espindola12203cc2010-11-21 00:48:25 +00001694 Index = F->getParent()->getOrdinal() + 1;
Jason W Kimd3443e92010-11-15 16:18:39 +00001695
1696 MCSectionData *FSD = F->getParent();
1697 // Offset of the symbol in the section
1698 Value += Layout.getSymbolAddress(&SD) - Layout.getSectionAddress(FSD);
1699 } else {
1700 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
1701 WeakrefUsedInReloc.insert(RelocSymbol);
1702 else
1703 UsedInReloc.insert(RelocSymbol);
1704 Index = -1;
1705 }
1706 Addend = Value;
1707 // Compensate for the addend on i386.
1708 if (Is64Bit)
1709 Value = 0;
1710 }
1711
1712 FixedValue = Value;
1713
1714 // determine the type of the relocation
1715
Rafael Espindola12203cc2010-11-21 00:48:25 +00001716 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1717 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
Jason W Kimd3443e92010-11-15 16:18:39 +00001718 unsigned Type;
1719 if (Is64Bit) {
1720 if (IsPCRel) {
1721 switch (Modifier) {
1722 default:
1723 llvm_unreachable("Unimplemented");
1724 case MCSymbolRefExpr::VK_None:
1725 Type = ELF::R_X86_64_PC32;
1726 break;
1727 case MCSymbolRefExpr::VK_PLT:
1728 Type = ELF::R_X86_64_PLT32;
1729 break;
1730 case MCSymbolRefExpr::VK_GOTPCREL:
1731 Type = ELF::R_X86_64_GOTPCREL;
1732 break;
1733 case MCSymbolRefExpr::VK_GOTTPOFF:
1734 Type = ELF::R_X86_64_GOTTPOFF;
1735 break;
1736 case MCSymbolRefExpr::VK_TLSGD:
1737 Type = ELF::R_X86_64_TLSGD;
1738 break;
1739 case MCSymbolRefExpr::VK_TLSLD:
1740 Type = ELF::R_X86_64_TLSLD;
1741 break;
1742 }
1743 } else {
1744 switch ((unsigned)Fixup.getKind()) {
1745 default: llvm_unreachable("invalid fixup kind!");
1746 case FK_Data_8: Type = ELF::R_X86_64_64; break;
1747 case X86::reloc_signed_4byte:
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001748 case FK_PCRel_4:
Jason W Kimd3443e92010-11-15 16:18:39 +00001749 assert(isInt<32>(Target.getConstant()));
1750 switch (Modifier) {
1751 default:
1752 llvm_unreachable("Unimplemented");
1753 case MCSymbolRefExpr::VK_None:
1754 Type = ELF::R_X86_64_32S;
1755 break;
1756 case MCSymbolRefExpr::VK_GOT:
1757 Type = ELF::R_X86_64_GOT32;
1758 break;
1759 case MCSymbolRefExpr::VK_GOTPCREL:
1760 Type = ELF::R_X86_64_GOTPCREL;
1761 break;
1762 case MCSymbolRefExpr::VK_TPOFF:
1763 Type = ELF::R_X86_64_TPOFF32;
1764 break;
1765 case MCSymbolRefExpr::VK_DTPOFF:
1766 Type = ELF::R_X86_64_DTPOFF32;
1767 break;
1768 }
1769 break;
1770 case FK_Data_4:
1771 Type = ELF::R_X86_64_32;
1772 break;
1773 case FK_Data_2: Type = ELF::R_X86_64_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001774 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001775 case FK_Data_1: Type = ELF::R_X86_64_8; break;
1776 }
1777 }
1778 } else {
1779 if (IsPCRel) {
1780 switch (Modifier) {
1781 default:
1782 llvm_unreachable("Unimplemented");
1783 case MCSymbolRefExpr::VK_None:
1784 Type = ELF::R_386_PC32;
1785 break;
1786 case MCSymbolRefExpr::VK_PLT:
1787 Type = ELF::R_386_PLT32;
1788 break;
1789 }
1790 } else {
1791 switch ((unsigned)Fixup.getKind()) {
1792 default: llvm_unreachable("invalid fixup kind!");
1793
1794 case X86::reloc_global_offset_table:
1795 Type = ELF::R_386_GOTPC;
1796 break;
1797
1798 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
1799 // instead?
1800 case X86::reloc_signed_4byte:
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001801 case FK_PCRel_4:
Jason W Kimd3443e92010-11-15 16:18:39 +00001802 case FK_Data_4:
1803 switch (Modifier) {
1804 default:
1805 llvm_unreachable("Unimplemented");
1806 case MCSymbolRefExpr::VK_None:
1807 Type = ELF::R_386_32;
1808 break;
1809 case MCSymbolRefExpr::VK_GOT:
1810 Type = ELF::R_386_GOT32;
1811 break;
1812 case MCSymbolRefExpr::VK_GOTOFF:
1813 Type = ELF::R_386_GOTOFF;
1814 break;
1815 case MCSymbolRefExpr::VK_TLSGD:
1816 Type = ELF::R_386_TLS_GD;
1817 break;
1818 case MCSymbolRefExpr::VK_TPOFF:
1819 Type = ELF::R_386_TLS_LE_32;
1820 break;
1821 case MCSymbolRefExpr::VK_INDNTPOFF:
1822 Type = ELF::R_386_TLS_IE;
1823 break;
1824 case MCSymbolRefExpr::VK_NTPOFF:
1825 Type = ELF::R_386_TLS_LE;
1826 break;
1827 case MCSymbolRefExpr::VK_GOTNTPOFF:
1828 Type = ELF::R_386_TLS_GOTIE;
1829 break;
1830 case MCSymbolRefExpr::VK_TLSLDM:
1831 Type = ELF::R_386_TLS_LDM;
1832 break;
1833 case MCSymbolRefExpr::VK_DTPOFF:
1834 Type = ELF::R_386_TLS_LDO_32;
1835 break;
1836 }
1837 break;
1838 case FK_Data_2: Type = ELF::R_386_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001839 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001840 case FK_Data_1: Type = ELF::R_386_8; break;
1841 }
1842 }
1843 }
1844
1845 if (RelocNeedsGOT(Modifier))
1846 NeedsGOT = true;
1847
Jason W Kimd3443e92010-11-15 16:18:39 +00001848
Jason W Kim858e7502010-11-22 18:42:07 +00001849 uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
Jason W Kim4a511f02010-11-22 18:41:13 +00001850 Fixup.getOffset();
Jason W Kimd3443e92010-11-15 16:18:39 +00001851
Jason W Kim10907422010-11-22 22:05:16 +00001852 if (!HasRelocationAddend) Addend = 0;
Jason W Kim4a511f02010-11-22 18:41:13 +00001853 ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend);
Jason W Kimd3443e92010-11-15 16:18:39 +00001854 Relocations[Fragment->getParent()].push_back(ERE);
Matt Fleming3565a062010-08-16 18:57:57 +00001855}