blob: b80d6ca1f84d8f6c20f256237139b67275135874 [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"
Wesley Peck4b047132010-11-21 22:06:28 +000034#include "../Target/MBlaze/MBlazeFixupKinds.h"
Matt Fleming3565a062010-08-16 18:57:57 +000035
36#include <vector>
37using namespace llvm;
38
Rafael Espindolaad49cf52010-09-18 15:03:21 +000039static unsigned GetType(const MCSymbolData &SD) {
40 uint32_t Type = (SD.getFlags() & (0xf << ELF_STT_Shift)) >> ELF_STT_Shift;
41 assert(Type == ELF::STT_NOTYPE || Type == ELF::STT_OBJECT ||
42 Type == ELF::STT_FUNC || Type == ELF::STT_SECTION ||
43 Type == ELF::STT_FILE || Type == ELF::STT_COMMON ||
44 Type == ELF::STT_TLS);
45 return Type;
46}
47
Rafael Espindolae15eb4e2010-09-23 19:55:14 +000048static unsigned GetBinding(const MCSymbolData &SD) {
49 uint32_t Binding = (SD.getFlags() & (0xf << ELF_STB_Shift)) >> ELF_STB_Shift;
50 assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
51 Binding == ELF::STB_WEAK);
52 return Binding;
53}
54
55static void SetBinding(MCSymbolData &SD, unsigned Binding) {
56 assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
57 Binding == ELF::STB_WEAK);
58 uint32_t OtherFlags = SD.getFlags() & ~(0xf << ELF_STB_Shift);
59 SD.setFlags(OtherFlags | (Binding << ELF_STB_Shift));
60}
61
Rafael Espindola152c1062010-10-06 21:02:29 +000062static unsigned GetVisibility(MCSymbolData &SD) {
63 unsigned Visibility =
64 (SD.getFlags() & (0xf << ELF_STV_Shift)) >> ELF_STV_Shift;
65 assert(Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_INTERNAL ||
66 Visibility == ELF::STV_HIDDEN || Visibility == ELF::STV_PROTECTED);
67 return Visibility;
68}
69
Wesley Peck4b047132010-11-21 22:06:28 +000070
Rafael Espindolaa0a2f872010-10-28 14:22:44 +000071static bool RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant) {
72 switch (Variant) {
Rafael Espindola5c77c162010-10-05 15:48:37 +000073 default:
74 return false;
Rafael Espindolaa0a2f872010-10-28 14:22:44 +000075 case MCSymbolRefExpr::VK_GOT:
76 case MCSymbolRefExpr::VK_PLT:
77 case MCSymbolRefExpr::VK_GOTPCREL:
78 case MCSymbolRefExpr::VK_TPOFF:
79 case MCSymbolRefExpr::VK_TLSGD:
80 case MCSymbolRefExpr::VK_GOTTPOFF:
81 case MCSymbolRefExpr::VK_INDNTPOFF:
82 case MCSymbolRefExpr::VK_NTPOFF:
83 case MCSymbolRefExpr::VK_GOTNTPOFF:
Rafael Espindolaa264f722010-10-28 14:37:09 +000084 case MCSymbolRefExpr::VK_TLSLDM:
Rafael Espindola0cf15d62010-10-28 14:48:59 +000085 case MCSymbolRefExpr::VK_DTPOFF:
Rafael Espindolab4d17212010-10-28 15:02:40 +000086 case MCSymbolRefExpr::VK_TLSLD:
Rafael Espindola5c77c162010-10-05 15:48:37 +000087 return true;
88 }
89}
90
Matt Fleming3565a062010-08-16 18:57:57 +000091namespace {
Daniel Dunbar115a3dd2010-11-13 07:33:40 +000092 class ELFObjectWriter : public MCObjectWriter {
Jason W Kimd3443e92010-11-15 16:18:39 +000093 protected:
Chris Lattnerb188a372010-08-28 03:21:03 +000094 /*static bool isFixupKindX86RIPRel(unsigned Kind) {
Matt Fleming3565a062010-08-16 18:57:57 +000095 return Kind == X86::reloc_riprel_4byte ||
96 Kind == X86::reloc_riprel_4byte_movq_load;
Chris Lattnerb188a372010-08-28 03:21:03 +000097 }*/
Matt Fleming3565a062010-08-16 18:57:57 +000098
99
100 /// ELFSymbolData - Helper struct for containing some precomputed information
101 /// on symbols.
102 struct ELFSymbolData {
103 MCSymbolData *SymbolData;
104 uint64_t StringIndex;
105 uint32_t SectionIndex;
106
107 // Support lexicographic sorting.
108 bool operator<(const ELFSymbolData &RHS) const {
Rafael Espindolaad49cf52010-09-18 15:03:21 +0000109 if (GetType(*SymbolData) == ELF::STT_FILE)
110 return true;
111 if (GetType(*RHS.SymbolData) == ELF::STT_FILE)
112 return false;
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000113 return SymbolData->getSymbol().getName() <
114 RHS.SymbolData->getSymbol().getName();
Matt Fleming3565a062010-08-16 18:57:57 +0000115 }
116 };
117
118 /// @name Relocation Data
119 /// @{
120
121 struct ELFRelocationEntry {
122 // Make these big enough for both 32-bit and 64-bit
123 uint64_t r_offset;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000124 int Index;
125 unsigned Type;
126 const MCSymbol *Symbol;
Matt Fleming3565a062010-08-16 18:57:57 +0000127 uint64_t r_addend;
Jason W Kim858e7502010-11-22 18:42:07 +0000128
Jason W Kim4a511f02010-11-22 18:41:13 +0000129 ELFRelocationEntry()
130 : r_offset(0), Index(0), Type(0), Symbol(0), r_addend(0) {}
131
132 ELFRelocationEntry(uint64_t RelocOffset, int _Index,
133 unsigned _Type, const MCSymbol *_Symbol,
134 uint64_t Addend)
135 : r_offset(RelocOffset), Index(_Index), Type(_Type),
136 Symbol(_Symbol), r_addend(Addend) {}
Matt Fleming3565a062010-08-16 18:57:57 +0000137
138 // Support lexicographic sorting.
139 bool operator<(const ELFRelocationEntry &RE) const {
140 return RE.r_offset < r_offset;
141 }
142 };
143
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000144 SmallPtrSet<const MCSymbol *, 16> UsedInReloc;
Rafael Espindola484291c2010-11-01 14:28:48 +0000145 SmallPtrSet<const MCSymbol *, 16> WeakrefUsedInReloc;
Rafael Espindola88182132010-10-27 15:18:17 +0000146 DenseMap<const MCSymbol *, const MCSymbol *> Renames;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000147
Matt Fleming3565a062010-08-16 18:57:57 +0000148 llvm::DenseMap<const MCSectionData*,
149 std::vector<ELFRelocationEntry> > Relocations;
150 DenseMap<const MCSection*, uint64_t> SectionStringTableIndex;
151
152 /// @}
153 /// @name Symbol Table Data
154 /// @{
155
156 SmallString<256> StringTable;
157 std::vector<ELFSymbolData> LocalSymbolData;
158 std::vector<ELFSymbolData> ExternalSymbolData;
159 std::vector<ELFSymbolData> UndefinedSymbolData;
160
161 /// @}
162
Rafael Espindola5c77c162010-10-05 15:48:37 +0000163 bool NeedsGOT;
164
Rafael Espindola7be2c332010-10-31 00:16:26 +0000165 bool NeedsSymtabShndx;
166
Matt Fleming3565a062010-08-16 18:57:57 +0000167 unsigned Is64Bit : 1;
168
169 bool HasRelocationAddend;
170
Roman Divacky5baf79e2010-09-09 17:57:50 +0000171 Triple::OSType OSType;
172
Wesley Peckeecb8582010-10-22 15:52:49 +0000173 uint16_t EMachine;
174
Matt Fleming3565a062010-08-16 18:57:57 +0000175 // This holds the symbol table index of the last local symbol.
176 unsigned LastLocalSymbolIndex;
177 // This holds the .strtab section index.
178 unsigned StringTableIndex;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000179 // This holds the .symtab section index.
180 unsigned SymbolTableIndex;
Matt Fleming3565a062010-08-16 18:57:57 +0000181
182 unsigned ShstrtabIndex;
183
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000184
185 const MCSymbol *SymbolToReloc(const MCAssembler &Asm,
186 const MCValue &Target,
187 const MCFragment &F) const;
188
Matt Fleming3565a062010-08-16 18:57:57 +0000189 public:
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000190 ELFObjectWriter(raw_ostream &_OS, bool _Is64Bit, bool IsLittleEndian,
191 uint16_t _EMachine, bool _HasRelAddend,
192 Triple::OSType _OSType)
193 : MCObjectWriter(_OS, IsLittleEndian),
194 NeedsGOT(false), NeedsSymtabShndx(false),
Roman Divacky5baf79e2010-09-09 17:57:50 +0000195 Is64Bit(_Is64Bit), HasRelocationAddend(_HasRelAddend),
Wesley Peckeecb8582010-10-22 15:52:49 +0000196 OSType(_OSType), EMachine(_EMachine) {
Matt Fleming3565a062010-08-16 18:57:57 +0000197 }
Jason W Kimd3443e92010-11-15 16:18:39 +0000198
199 virtual ~ELFObjectWriter();
200
Matt Fleming3565a062010-08-16 18:57:57 +0000201 void WriteWord(uint64_t W) {
Chris Lattnerb188a372010-08-28 03:21:03 +0000202 if (Is64Bit)
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000203 Write64(W);
Chris Lattnerb188a372010-08-28 03:21:03 +0000204 else
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000205 Write32(W);
Matt Fleming3565a062010-08-16 18:57:57 +0000206 }
207
Matt Fleming3565a062010-08-16 18:57:57 +0000208 void StringLE16(char *buf, uint16_t Value) {
209 buf[0] = char(Value >> 0);
210 buf[1] = char(Value >> 8);
211 }
212
213 void StringLE32(char *buf, uint32_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000214 StringLE16(buf, uint16_t(Value >> 0));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000215 StringLE16(buf + 2, uint16_t(Value >> 16));
Matt Fleming3565a062010-08-16 18:57:57 +0000216 }
217
218 void StringLE64(char *buf, uint64_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000219 StringLE32(buf, uint32_t(Value >> 0));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000220 StringLE32(buf + 4, uint32_t(Value >> 32));
Matt Fleming3565a062010-08-16 18:57:57 +0000221 }
222
223 void StringBE16(char *buf ,uint16_t Value) {
224 buf[0] = char(Value >> 8);
225 buf[1] = char(Value >> 0);
226 }
227
228 void StringBE32(char *buf, uint32_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000229 StringBE16(buf, uint16_t(Value >> 16));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000230 StringBE16(buf + 2, uint16_t(Value >> 0));
Matt Fleming3565a062010-08-16 18:57:57 +0000231 }
232
233 void StringBE64(char *buf, uint64_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000234 StringBE32(buf, uint32_t(Value >> 32));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000235 StringBE32(buf + 4, uint32_t(Value >> 0));
Matt Fleming3565a062010-08-16 18:57:57 +0000236 }
237
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000238 void String8(MCDataFragment &F, uint8_t Value) {
239 char buf[1];
240 buf[0] = Value;
241 F.getContents() += StringRef(buf, 1);
242 }
243
244 void String16(MCDataFragment &F, uint16_t Value) {
245 char buf[2];
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000246 if (isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000247 StringLE16(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000248 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000249 StringBE16(buf, Value);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000250 F.getContents() += StringRef(buf, 2);
Matt Fleming3565a062010-08-16 18:57:57 +0000251 }
252
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000253 void String32(MCDataFragment &F, uint32_t Value) {
254 char buf[4];
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000255 if (isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000256 StringLE32(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000257 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000258 StringBE32(buf, Value);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000259 F.getContents() += StringRef(buf, 4);
Matt Fleming3565a062010-08-16 18:57:57 +0000260 }
261
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000262 void String64(MCDataFragment &F, uint64_t Value) {
263 char buf[8];
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000264 if (isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000265 StringLE64(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000266 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000267 StringBE64(buf, Value);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000268 F.getContents() += StringRef(buf, 8);
Matt Fleming3565a062010-08-16 18:57:57 +0000269 }
270
Jason W Kimd3443e92010-11-15 16:18:39 +0000271 virtual void WriteHeader(uint64_t SectionDataSize, unsigned NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000272
Jason W Kimd3443e92010-11-15 16:18:39 +0000273 virtual void WriteSymbolEntry(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
Rafael Espindola7be2c332010-10-31 00:16:26 +0000274 uint64_t name, uint8_t info,
Matt Fleming3565a062010-08-16 18:57:57 +0000275 uint64_t value, uint64_t size,
Rafael Espindola7be2c332010-10-31 00:16:26 +0000276 uint8_t other, uint32_t shndx,
277 bool Reserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000278
Jason W Kimd3443e92010-11-15 16:18:39 +0000279 virtual void WriteSymbol(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
Rafael Espindola7be2c332010-10-31 00:16:26 +0000280 ELFSymbolData &MSD,
Matt Fleming3565a062010-08-16 18:57:57 +0000281 const MCAsmLayout &Layout);
282
Rafael Espindolabab2a802010-11-10 21:51:05 +0000283 typedef DenseMap<const MCSectionELF*, uint32_t> SectionIndexMapTy;
Jason W Kimd3443e92010-11-15 16:18:39 +0000284 virtual void WriteSymbolTable(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
Rafael Espindola7be2c332010-10-31 00:16:26 +0000285 const MCAssembler &Asm,
Rafael Espindola71859c62010-09-16 19:46:31 +0000286 const MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000287 const SectionIndexMapTy &SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +0000288
Jason W Kimd3443e92010-11-15 16:18:39 +0000289 virtual void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
Matt Fleming3565a062010-08-16 18:57:57 +0000290 const MCFragment *Fragment, const MCFixup &Fixup,
Jason W Kimd3443e92010-11-15 16:18:39 +0000291 MCValue Target, uint64_t &FixedValue) {
292 assert(0 && "RecordRelocation is not specific enough");
Benjamin Kramer32858772010-11-15 19:20:50 +0000293 }
Matt Fleming3565a062010-08-16 18:57:57 +0000294
Jason W Kimd3443e92010-11-15 16:18:39 +0000295 virtual uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm,
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000296 const MCSymbol *S);
Matt Fleming3565a062010-08-16 18:57:57 +0000297
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000298 // Map from a group section to the signature symbol
299 typedef DenseMap<const MCSectionELF*, const MCSymbol*> GroupMapTy;
300 // Map from a signature symbol to the group section
301 typedef DenseMap<const MCSymbol*, const MCSectionELF*> RevGroupMapTy;
302
Matt Fleming3565a062010-08-16 18:57:57 +0000303 /// ComputeSymbolTable - Compute the symbol table data
304 ///
305 /// \param StringTable [out] - The string table data.
306 /// \param StringIndexMap [out] - Map from symbol names to offsets in the
307 /// string table.
Jason W Kimd3443e92010-11-15 16:18:39 +0000308 virtual void ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000309 const SectionIndexMapTy &SectionIndexMap,
310 RevGroupMapTy RevGroupMap);
Rafael Espindolabab2a802010-11-10 21:51:05 +0000311
Jason W Kimd3443e92010-11-15 16:18:39 +0000312 virtual void ComputeIndexMap(MCAssembler &Asm,
Rafael Espindolabab2a802010-11-10 21:51:05 +0000313 SectionIndexMapTy &SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +0000314
Jason W Kimd3443e92010-11-15 16:18:39 +0000315 virtual void WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
Matt Fleming3565a062010-08-16 18:57:57 +0000316 const MCSectionData &SD);
317
Jason W Kimd3443e92010-11-15 16:18:39 +0000318 virtual void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout) {
Matt Fleming3565a062010-08-16 18:57:57 +0000319 for (MCAssembler::const_iterator it = Asm.begin(),
320 ie = Asm.end(); it != ie; ++it) {
321 WriteRelocation(Asm, Layout, *it);
322 }
323 }
324
Jason W Kimd3443e92010-11-15 16:18:39 +0000325 virtual void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000326 const SectionIndexMapTy &SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +0000327
Jason W Kimd3443e92010-11-15 16:18:39 +0000328 virtual void CreateGroupSections(MCAssembler &Asm, MCAsmLayout &Layout,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000329 GroupMapTy &GroupMap, RevGroupMapTy &RevGroupMap);
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000330
Jason W Kimd3443e92010-11-15 16:18:39 +0000331 virtual void ExecutePostLayoutBinding(MCAssembler &Asm);
Matt Fleming3565a062010-08-16 18:57:57 +0000332
Jason W Kimd3443e92010-11-15 16:18:39 +0000333 virtual void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
Matt Fleming3565a062010-08-16 18:57:57 +0000334 uint64_t Address, uint64_t Offset,
335 uint64_t Size, uint32_t Link, uint32_t Info,
336 uint64_t Alignment, uint64_t EntrySize);
337
Jason W Kimd3443e92010-11-15 16:18:39 +0000338 virtual void WriteRelocationsFragment(const MCAssembler &Asm, MCDataFragment *F,
Matt Fleming3565a062010-08-16 18:57:57 +0000339 const MCSectionData *SD);
340
Jason W Kimd3443e92010-11-15 16:18:39 +0000341 virtual bool IsFixupFullyResolved(const MCAssembler &Asm,
Rafael Espindola70703872010-09-30 02:22:20 +0000342 const MCValue Target,
343 bool IsPCRel,
344 const MCFragment *DF) const;
345
Jason W Kimd3443e92010-11-15 16:18:39 +0000346 virtual void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
347 virtual void WriteSection(MCAssembler &Asm,
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000348 const SectionIndexMapTy &SectionIndexMap,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000349 uint32_t GroupSymbolIndex,
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000350 uint64_t Offset, uint64_t Size, uint64_t Alignment,
351 const MCSectionELF &Section);
Matt Fleming3565a062010-08-16 18:57:57 +0000352 };
353
Jason W Kimd3443e92010-11-15 16:18:39 +0000354 //===- X86ELFObjectWriter -------------------------------------------===//
355
356 class X86ELFObjectWriter : public ELFObjectWriter {
357 public:
358 X86ELFObjectWriter(raw_ostream &_OS, bool _Is64Bit, bool IsLittleEndian,
359 uint16_t _EMachine, bool _HasRelAddend,
360 Triple::OSType _OSType);
Wesley Peck4b047132010-11-21 22:06:28 +0000361
Jason W Kimd3443e92010-11-15 16:18:39 +0000362 virtual ~X86ELFObjectWriter();
363 virtual void RecordRelocation(const MCAssembler &Asm,
364 const MCAsmLayout &Layout,
365 const MCFragment *Fragment,
366 const MCFixup &Fixup, MCValue Target,
367 uint64_t &FixedValue);
Jason W Kim4a511f02010-11-22 18:41:13 +0000368
369 static bool isFixupKindPCRel(unsigned Kind) {
370 switch (Kind) {
371 default:
372 return false;
373 case X86::reloc_pcrel_1byte:
374 case X86::reloc_pcrel_4byte:
375 case X86::reloc_riprel_4byte:
376 case X86::reloc_riprel_4byte_movq_load:
377 return true;
378 }
379 }
Jason W Kimd3443e92010-11-15 16:18:39 +0000380 };
381
382
383 //===- ARMELFObjectWriter -------------------------------------------===//
384
385 class ARMELFObjectWriter : public ELFObjectWriter {
386 public:
387 ARMELFObjectWriter(raw_ostream &_OS, bool _Is64Bit, bool IsLittleEndian,
388 uint16_t _EMachine, bool _HasRelAddend,
389 Triple::OSType _OSType);
Wesley Peck4b047132010-11-21 22:06:28 +0000390
Jason W Kimd3443e92010-11-15 16:18:39 +0000391 virtual ~ARMELFObjectWriter();
392 virtual void RecordRelocation(const MCAssembler &Asm,
393 const MCAsmLayout &Layout,
394 const MCFragment *Fragment,
395 const MCFixup &Fixup, MCValue Target,
396 uint64_t &FixedValue);
Jason W Kim4a511f02010-11-22 18:41:13 +0000397
398 static bool isFixupKindPCRel(unsigned Kind) {
399 switch (Kind) {
400 default:
401 return false;
402 case MBlaze::reloc_pcrel_2byte:
403 case MBlaze::reloc_pcrel_4byte:
404 return true;
405 }
406 }
407
Jason W Kimd3443e92010-11-15 16:18:39 +0000408 };
Wesley Peck4b047132010-11-21 22:06:28 +0000409
410 //===- MBlazeELFObjectWriter -------------------------------------------===//
411
412 class MBlazeELFObjectWriter : public ELFObjectWriter {
413 public:
414 MBlazeELFObjectWriter(raw_ostream &_OS, bool _Is64Bit, bool IsLittleEndian,
415 uint16_t _EMachine, bool _HasRelAddend,
416 Triple::OSType _OSType);
417
418 virtual ~MBlazeELFObjectWriter();
419 virtual void RecordRelocation(const MCAssembler &Asm,
420 const MCAsmLayout &Layout,
421 const MCFragment *Fragment,
422 const MCFixup &Fixup, MCValue Target,
423 uint64_t &FixedValue);
Jason W Kim4a511f02010-11-22 18:41:13 +0000424
425 static bool isFixupKindPCRel(unsigned Kind) {
426 switch (Kind) {
427 default:
428 return false;
429 case ARM::fixup_arm_pcrel_12:
430 case ARM::fixup_arm_vfp_pcrel_12:
431 case ARM::fixup_arm_branch:
432 return true;
433 }
434 }
Wesley Peck4b047132010-11-21 22:06:28 +0000435 };
Matt Fleming3565a062010-08-16 18:57:57 +0000436}
437
Jason W Kimd3443e92010-11-15 16:18:39 +0000438ELFObjectWriter::~ELFObjectWriter()
439{}
440
Matt Fleming3565a062010-08-16 18:57:57 +0000441// Emit the ELF header.
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000442void ELFObjectWriter::WriteHeader(uint64_t SectionDataSize,
443 unsigned NumberOfSections) {
Matt Fleming3565a062010-08-16 18:57:57 +0000444 // ELF Header
445 // ----------
446 //
447 // Note
448 // ----
449 // emitWord method behaves differently for ELF32 and ELF64, writing
450 // 4 bytes in the former and 8 in the latter.
451
452 Write8(0x7f); // e_ident[EI_MAG0]
453 Write8('E'); // e_ident[EI_MAG1]
454 Write8('L'); // e_ident[EI_MAG2]
455 Write8('F'); // e_ident[EI_MAG3]
456
457 Write8(Is64Bit ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
458
459 // e_ident[EI_DATA]
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000460 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
Matt Fleming3565a062010-08-16 18:57:57 +0000461
462 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky5baf79e2010-09-09 17:57:50 +0000463 // e_ident[EI_OSABI]
464 switch (OSType) {
465 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
466 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
467 default: Write8(ELF::ELFOSABI_NONE); break;
468 }
Matt Fleming3565a062010-08-16 18:57:57 +0000469 Write8(0); // e_ident[EI_ABIVERSION]
470
471 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
472
473 Write16(ELF::ET_REL); // e_type
474
Wesley Peckeecb8582010-10-22 15:52:49 +0000475 Write16(EMachine); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +0000476
477 Write32(ELF::EV_CURRENT); // e_version
478 WriteWord(0); // e_entry, no entry point in .o file
479 WriteWord(0); // e_phoff, no program header for .o
Benjamin Kramereb976772010-08-17 17:02:29 +0000480 WriteWord(SectionDataSize + (Is64Bit ? sizeof(ELF::Elf64_Ehdr) :
481 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000482
483 // FIXME: Make this configurable.
484 Write32(0); // e_flags = whatever the target wants
485
486 // e_ehsize = ELF header size
487 Write16(Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
488
489 Write16(0); // e_phentsize = prog header entry size
490 Write16(0); // e_phnum = # prog header entries = 0
491
492 // e_shentsize = Section header entry size
493 Write16(Is64Bit ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
494
495 // e_shnum = # of section header ents
Rafael Espindola7be2c332010-10-31 00:16:26 +0000496 if (NumberOfSections >= ELF::SHN_LORESERVE)
497 Write16(0);
498 else
499 Write16(NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000500
501 // e_shstrndx = Section # of '.shstrtab'
Rafael Espindola7be2c332010-10-31 00:16:26 +0000502 if (NumberOfSections >= ELF::SHN_LORESERVE)
503 Write16(ELF::SHN_XINDEX);
504 else
505 Write16(ShstrtabIndex);
Matt Fleming3565a062010-08-16 18:57:57 +0000506}
507
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000508void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
509 MCDataFragment *ShndxF,
510 uint64_t name,
511 uint8_t info, uint64_t value,
512 uint64_t size, uint8_t other,
513 uint32_t shndx,
514 bool Reserved) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000515 if (ShndxF) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000516 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000517 String32(*ShndxF, shndx);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000518 else
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000519 String32(*ShndxF, 0);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000520 }
521
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000522 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
523 uint16_t(ELF::SHN_XINDEX) : shndx;
524
Matt Fleming3565a062010-08-16 18:57:57 +0000525 if (Is64Bit) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000526 String32(*SymtabF, name); // st_name
527 String8(*SymtabF, info); // st_info
528 String8(*SymtabF, other); // st_other
529 String16(*SymtabF, Index); // st_shndx
530 String64(*SymtabF, value); // st_value
531 String64(*SymtabF, size); // st_size
Matt Fleming3565a062010-08-16 18:57:57 +0000532 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000533 String32(*SymtabF, name); // st_name
534 String32(*SymtabF, value); // st_value
535 String32(*SymtabF, size); // st_size
536 String8(*SymtabF, info); // st_info
537 String8(*SymtabF, other); // st_other
538 String16(*SymtabF, Index); // st_shndx
Matt Fleming3565a062010-08-16 18:57:57 +0000539 }
540}
541
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000542static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout) {
543 if (Data.isCommon() && Data.isExternal())
544 return Data.getCommonAlignment();
545
546 const MCSymbol &Symbol = Data.getSymbol();
547 if (!Symbol.isInSection())
548 return 0;
549
Rafael Espindola1973d432010-10-28 19:39:57 +0000550 if (MCFragment *FF = Data.getFragment())
551 return Layout.getSymbolAddress(&Data) -
552 Layout.getSectionAddress(FF->getParent());
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000553
554 return 0;
555}
556
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000557void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm) {
Rafael Espindola88182132010-10-27 15:18:17 +0000558 // The presence of symbol versions causes undefined symbols and
559 // versions declared with @@@ to be renamed.
560
561 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
562 ie = Asm.symbol_end(); it != ie; ++it) {
563 const MCSymbol &Alias = it->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000564 const MCSymbol &Symbol = Alias.AliasedSymbol();
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000565 MCSymbolData &SD = Asm.getSymbolData(Symbol);
566
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000567 // Not an alias.
568 if (&Symbol == &Alias)
569 continue;
570
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000571 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000572 size_t Pos = AliasName.find('@');
573 if (Pos == StringRef::npos)
574 continue;
575
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000576 // Aliases defined with .symvar copy the binding from the symbol they alias.
577 // This is the first place we are able to copy this information.
578 it->setExternal(SD.isExternal());
579 SetBinding(*it, GetBinding(SD));
580
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000581 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000582 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
583 continue;
584
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000585 // FIXME: produce a better error message.
586 if (Symbol.isUndefined() && Rest.startswith("@@") &&
587 !Rest.startswith("@@@"))
588 report_fatal_error("A @@ version cannot be undefined");
589
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000590 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000591 }
592}
593
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000594void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
595 MCDataFragment *ShndxF,
596 ELFSymbolData &MSD,
597 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000598 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000599 MCSymbolData &Data =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000600 Layout.getAssembler().getSymbolData(OrigData.getSymbol().AliasedSymbol());
Rafael Espindola152c1062010-10-06 21:02:29 +0000601
Rafael Espindola7be2c332010-10-31 00:16:26 +0000602 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
603 Data.getSymbol().isVariable();
604
Rafael Espindola152c1062010-10-06 21:02:29 +0000605 uint8_t Binding = GetBinding(OrigData);
606 uint8_t Visibility = GetVisibility(OrigData);
607 uint8_t Type = GetType(Data);
608
609 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
610 uint8_t Other = Visibility;
611
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000612 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000613 uint64_t Size = 0;
614 const MCExpr *ESize;
615
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000616 assert(!(Data.isCommon() && !Data.isExternal()));
617
Matt Fleming3565a062010-08-16 18:57:57 +0000618 ESize = Data.getSize();
619 if (Data.getSize()) {
620 MCValue Res;
621 if (ESize->getKind() == MCExpr::Binary) {
622 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(ESize);
623
624 if (BE->EvaluateAsRelocatable(Res, &Layout)) {
Benjamin Kramer24f12062010-10-17 07:38:40 +0000625 assert(!Res.getSymA() || !Res.getSymA()->getSymbol().isDefined());
626 assert(!Res.getSymB() || !Res.getSymB()->getSymbol().isDefined());
Rafael Espindolaf230df92010-10-16 18:23:53 +0000627 Size = Res.getConstant();
Matt Fleming3565a062010-08-16 18:57:57 +0000628 }
629 } else if (ESize->getKind() == MCExpr::Constant) {
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000630 Size = static_cast<const MCConstantExpr *>(ESize)->getValue();
Matt Fleming3565a062010-08-16 18:57:57 +0000631 } else {
632 assert(0 && "Unsupported size expression");
633 }
634 }
635
636 // Write out the symbol table entry
Rafael Espindola7be2c332010-10-31 00:16:26 +0000637 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
638 Size, Other, MSD.SectionIndex, IsReserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000639}
640
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000641void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
642 MCDataFragment *ShndxF,
643 const MCAssembler &Asm,
644 const MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000645 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000646 // The string table must be emitted first because we need the index
647 // into the string table for all the symbol names.
648 assert(StringTable.size() && "Missing string table");
649
650 // FIXME: Make sure the start of the symbol table is aligned.
651
652 // The first entry is the undefined symbol entry.
Rafael Espindola7be2c332010-10-31 00:16:26 +0000653 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
Matt Fleming3565a062010-08-16 18:57:57 +0000654
655 // Write the symbol table entries.
656 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
657 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
658 ELFSymbolData &MSD = LocalSymbolData[i];
Rafael Espindola7be2c332010-10-31 00:16:26 +0000659 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000660 }
661
Rafael Espindola71859c62010-09-16 19:46:31 +0000662 // Write out a symbol table entry for each regular section.
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000663 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
664 ++i) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000665 const MCSectionELF &Section =
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000666 static_cast<const MCSectionELF&>(i->getSection());
667 if (Section.getType() == ELF::SHT_RELA ||
668 Section.getType() == ELF::SHT_REL ||
669 Section.getType() == ELF::SHT_STRTAB ||
670 Section.getType() == ELF::SHT_SYMTAB)
Eli Friedmana44fa242010-08-16 21:17:09 +0000671 continue;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000672 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000673 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section), false);
Eli Friedmana44fa242010-08-16 21:17:09 +0000674 LastLocalSymbolIndex++;
675 }
Matt Fleming3565a062010-08-16 18:57:57 +0000676
677 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
678 ELFSymbolData &MSD = ExternalSymbolData[i];
679 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000680 assert(((Data.getFlags() & ELF_STB_Global) ||
681 (Data.getFlags() & ELF_STB_Weak)) &&
682 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000683 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000684 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000685 LastLocalSymbolIndex++;
686 }
687
688 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
689 ELFSymbolData &MSD = UndefinedSymbolData[i];
690 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000691 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000692 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000693 LastLocalSymbolIndex++;
694 }
695}
696
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000697const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm,
698 const MCValue &Target,
699 const MCFragment &F) const {
700 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000701 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
702 const MCSymbol *Renamed = Renames.lookup(&Symbol);
703 const MCSymbolData &SD = Asm.getSymbolData(Symbol);
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000704
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000705 if (ASymbol.isUndefined()) {
706 if (Renamed)
707 return Renamed;
708 return &ASymbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000709 }
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000710
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000711 if (SD.isExternal()) {
712 if (Renamed)
713 return Renamed;
714 return &Symbol;
715 }
Rafael Espindola73ffea42010-09-25 05:42:19 +0000716
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000717 const MCSectionELF &Section =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000718 static_cast<const MCSectionELF&>(ASymbol.getSection());
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000719
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000720 if (Section.getKind().isBSS())
721 return NULL;
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000722
Rafael Espindola8cecf252010-10-06 16:23:36 +0000723 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000724 const MCSectionELF &Sec2 =
725 static_cast<const MCSectionELF&>(F.getParent()->getSection());
726
Rafael Espindola8cecf252010-10-06 16:23:36 +0000727 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000728 (Kind == MCSymbolRefExpr::VK_PLT ||
729 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000730 Kind == MCSymbolRefExpr::VK_GOTOFF)) {
731 if (Renamed)
732 return Renamed;
733 return &Symbol;
734 }
Rafael Espindola3729d002010-10-05 23:57:26 +0000735
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000736 if (Section.getFlags() & MCSectionELF::SHF_MERGE) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000737 if (Target.getConstant() == 0)
738 return NULL;
739 if (Renamed)
740 return Renamed;
741 return &Symbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000742 }
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000743
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000744 return NULL;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000745}
746
Matt Fleming3565a062010-08-16 18:57:57 +0000747
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000748uint64_t
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000749ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
750 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000751 MCSymbolData &SD = Asm.getSymbolData(*S);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000752 return SD.getIndex();
Matt Fleming3565a062010-08-16 18:57:57 +0000753}
754
Rafael Espindola737cd212010-10-05 18:01:23 +0000755static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
Rafael Espindola88182132010-10-27 15:18:17 +0000756 bool Used, bool Renamed) {
Rafael Espindola484291c2010-11-01 14:28:48 +0000757 if (Data.getFlags() & ELF_Other_Weakref)
758 return false;
759
Rafael Espindolabd701182010-10-19 19:31:37 +0000760 if (Used)
761 return true;
762
Rafael Espindola88182132010-10-27 15:18:17 +0000763 if (Renamed)
764 return false;
765
Rafael Espindola737cd212010-10-05 18:01:23 +0000766 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000767
Rafael Espindolad1798862010-10-29 23:09:31 +0000768 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
769 return true;
770
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000771 const MCSymbol &A = Symbol.AliasedSymbol();
Rafael Espindolad1798862010-10-29 23:09:31 +0000772 if (!A.isVariable() && A.isUndefined() && !Data.isCommon())
Rafael Espindolaa6866962010-10-27 14:44:52 +0000773 return false;
774
Rafael Espindola737cd212010-10-05 18:01:23 +0000775 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
776 return false;
777
Rafael Espindolabd701182010-10-19 19:31:37 +0000778 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000779 return false;
780
781 return true;
782}
783
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000784static bool isLocal(const MCSymbolData &Data, bool isSignature,
785 bool isUsedInReloc) {
Rafael Espindola737cd212010-10-05 18:01:23 +0000786 if (Data.isExternal())
787 return false;
788
789 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000790 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000791
792 if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
793 if (isSignature && !isUsedInReloc)
794 return true;
795
Rafael Espindola737cd212010-10-05 18:01:23 +0000796 return false;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000797 }
Rafael Espindola737cd212010-10-05 18:01:23 +0000798
799 return true;
800}
801
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000802void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
803 SectionIndexMapTy &SectionIndexMap) {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000804 unsigned Index = 1;
805 for (MCAssembler::iterator it = Asm.begin(),
806 ie = Asm.end(); it != ie; ++it) {
807 const MCSectionELF &Section =
808 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000809 if (Section.getType() != ELF::SHT_GROUP)
810 continue;
811 SectionIndexMap[&Section] = Index++;
812 }
813
814 for (MCAssembler::iterator it = Asm.begin(),
815 ie = Asm.end(); it != ie; ++it) {
816 const MCSectionELF &Section =
817 static_cast<const MCSectionELF &>(it->getSection());
818 if (Section.getType() == ELF::SHT_GROUP)
819 continue;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000820 SectionIndexMap[&Section] = Index++;
821 }
822}
823
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000824void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000825 const SectionIndexMapTy &SectionIndexMap,
826 RevGroupMapTy RevGroupMap) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000827 // FIXME: Is this the correct place to do this?
828 if (NeedsGOT) {
829 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
830 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
831 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
832 Data.setExternal(true);
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000833 SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindola5c77c162010-10-05 15:48:37 +0000834 }
835
Matt Fleming3565a062010-08-16 18:57:57 +0000836 // Build section lookup table.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000837 int NumRegularSections = Asm.size();
Matt Fleming3565a062010-08-16 18:57:57 +0000838
839 // Index 0 is always the empty string.
840 StringMap<uint64_t> StringIndexMap;
841 StringTable += '\x00';
842
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000843 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000844 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
845 ie = Asm.symbol_end(); it != ie; ++it) {
846 const MCSymbol &Symbol = it->getSymbol();
847
Rafael Espindola484291c2010-11-01 14:28:48 +0000848 bool Used = UsedInReloc.count(&Symbol);
849 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000850 bool isSignature = RevGroupMap.count(&Symbol);
851
852 if (!isInSymtab(Asm, *it,
853 Used || WeakrefUsed || isSignature,
Rafael Espindola88182132010-10-27 15:18:17 +0000854 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000855 continue;
856
Matt Fleming3565a062010-08-16 18:57:57 +0000857 ELFSymbolData MSD;
858 MSD.SymbolData = it;
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000859 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Matt Fleming3565a062010-08-16 18:57:57 +0000860
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000861 // Undefined symbols are global, but this is the first place we
862 // are able to set it.
863 bool Local = isLocal(*it, isSignature, Used);
864 if (!Local && GetBinding(*it) == ELF::STB_LOCAL) {
865 MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
866 SetBinding(*it, ELF::STB_GLOBAL);
867 SetBinding(SD, ELF::STB_GLOBAL);
868 }
869
Rafael Espindola484291c2010-11-01 14:28:48 +0000870 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
871 SetBinding(*it, ELF::STB_WEAK);
872
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000873 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000874 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000875 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000876 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000877 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000878 } else if (RefSymbol.isUndefined()) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000879 if (isSignature && !Used)
880 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
881 else
882 MSD.SectionIndex = ELF::SHN_UNDEF;
Matt Fleming3565a062010-08-16 18:57:57 +0000883 } else {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000884 const MCSectionELF &Section =
885 static_cast<const MCSectionELF&>(RefSymbol.getSection());
886 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000887 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
888 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000889 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000890 }
891
Rafael Espindola88182132010-10-27 15:18:17 +0000892 // The @@@ in symbol version is replaced with @ in undefined symbols and
893 // @@ in defined ones.
894 StringRef Name = Symbol.getName();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000895 SmallString<32> Buf;
896
Rafael Espindola88182132010-10-27 15:18:17 +0000897 size_t Pos = Name.find("@@@");
Rafael Espindola88182132010-10-27 15:18:17 +0000898 if (Pos != StringRef::npos) {
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000899 Buf += Name.substr(0, Pos);
900 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
901 Buf += Name.substr(Pos + Skip);
902 Name = Buf;
Rafael Espindola88182132010-10-27 15:18:17 +0000903 }
904
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000905 uint64_t &Entry = StringIndexMap[Name];
Rafael Espindolaa6866962010-10-27 14:44:52 +0000906 if (!Entry) {
907 Entry = StringTable.size();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000908 StringTable += Name;
Rafael Espindolaa6866962010-10-27 14:44:52 +0000909 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +0000910 }
Rafael Espindolaa6866962010-10-27 14:44:52 +0000911 MSD.StringIndex = Entry;
912 if (MSD.SectionIndex == ELF::SHN_UNDEF)
913 UndefinedSymbolData.push_back(MSD);
914 else if (Local)
915 LocalSymbolData.push_back(MSD);
916 else
917 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +0000918 }
919
920 // Symbols are required to be in lexicographic order.
921 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
922 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
923 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
924
925 // Set the symbol indices. Local symbols must come before all other
926 // symbols with non-local bindings.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000927 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000928 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
929 LocalSymbolData[i].SymbolData->setIndex(Index++);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000930
931 Index += NumRegularSections;
932
Matt Fleming3565a062010-08-16 18:57:57 +0000933 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
934 ExternalSymbolData[i].SymbolData->setIndex(Index++);
935 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
936 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
937}
938
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000939void ELFObjectWriter::WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
940 const MCSectionData &SD) {
Matt Fleming3565a062010-08-16 18:57:57 +0000941 if (!Relocations[&SD].empty()) {
942 MCContext &Ctx = Asm.getContext();
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000943 const MCSectionELF *RelaSection;
Matt Fleming3565a062010-08-16 18:57:57 +0000944 const MCSectionELF &Section =
945 static_cast<const MCSectionELF&>(SD.getSection());
946
947 const StringRef SectionName = Section.getSectionName();
Benjamin Kramer377a5722010-08-17 17:30:07 +0000948 std::string RelaSectionName = HasRelocationAddend ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +0000949 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000950
951 unsigned EntrySize;
952 if (HasRelocationAddend)
953 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
954 else
955 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +0000956
Benjamin Kramer377a5722010-08-17 17:30:07 +0000957 RelaSection = Ctx.getELFSection(RelaSectionName, HasRelocationAddend ?
958 ELF::SHT_RELA : ELF::SHT_REL, 0,
Matt Fleming3565a062010-08-16 18:57:57 +0000959 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000960 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +0000961
962 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Benjamin Kramera9eadca2010-09-06 16:11:52 +0000963 RelaSD.setAlignment(Is64Bit ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +0000964
965 MCDataFragment *F = new MCDataFragment(&RelaSD);
966
967 WriteRelocationsFragment(Asm, F, &SD);
968
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000969 Asm.AddSectionToTheEnd(*this, RelaSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000970 }
971}
972
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000973void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
974 uint64_t Flags, uint64_t Address,
975 uint64_t Offset, uint64_t Size,
976 uint32_t Link, uint32_t Info,
977 uint64_t Alignment,
978 uint64_t EntrySize) {
Matt Fleming3565a062010-08-16 18:57:57 +0000979 Write32(Name); // sh_name: index into string table
980 Write32(Type); // sh_type
981 WriteWord(Flags); // sh_flags
982 WriteWord(Address); // sh_addr
983 WriteWord(Offset); // sh_offset
984 WriteWord(Size); // sh_size
985 Write32(Link); // sh_link
986 Write32(Info); // sh_info
987 WriteWord(Alignment); // sh_addralign
988 WriteWord(EntrySize); // sh_entsize
989}
990
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000991void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
992 MCDataFragment *F,
993 const MCSectionData *SD) {
Matt Fleming3565a062010-08-16 18:57:57 +0000994 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
995 // sort by the r_offset just like gnu as does
996 array_pod_sort(Relocs.begin(), Relocs.end());
997
998 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
999 ELFRelocationEntry entry = Relocs[e - i - 1];
1000
Rafael Espindola12203cc2010-11-21 00:48:25 +00001001 if (!entry.Index)
1002 ;
1003 else if (entry.Index < 0)
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001004 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
1005 else
Rafael Espindola12203cc2010-11-21 00:48:25 +00001006 entry.Index += LocalSymbolData.size();
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001007 if (Is64Bit) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001008 String64(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001009
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001010 struct ELF::Elf64_Rela ERE64;
1011 ERE64.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001012 String64(*F, ERE64.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001013
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001014 if (HasRelocationAddend)
1015 String64(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001016 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001017 String32(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001018
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001019 struct ELF::Elf32_Rela ERE32;
1020 ERE32.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001021 String32(*F, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001022
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001023 if (HasRelocationAddend)
1024 String32(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001025 }
Matt Fleming3565a062010-08-16 18:57:57 +00001026 }
1027}
1028
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001029void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
1030 MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001031 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +00001032 MCContext &Ctx = Asm.getContext();
1033 MCDataFragment *F;
1034
Matt Fleming3565a062010-08-16 18:57:57 +00001035 unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
1036
Rafael Espindola38738bf2010-09-22 19:04:41 +00001037 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001038 const MCSectionELF *ShstrtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001039 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001040 SectionKind::getReadOnly());
Rafael Espindola71859c62010-09-16 19:46:31 +00001041 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1042 ShstrtabSD.setAlignment(1);
1043 ShstrtabIndex = Asm.size();
1044
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001045 const MCSectionELF *SymtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001046 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1047 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001048 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001049 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Matt Fleming3565a062010-08-16 18:57:57 +00001050 SymtabSD.setAlignment(Is64Bit ? 8 : 4);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001051 SymbolTableIndex = Asm.size();
1052
1053 MCSectionData *SymtabShndxSD = NULL;
1054
1055 if (NeedsSymtabShndx) {
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001056 const MCSectionELF *SymtabShndxSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001057 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001058 SectionKind::getReadOnly(), 4, "");
Rafael Espindola7be2c332010-10-31 00:16:26 +00001059 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
1060 SymtabShndxSD->setAlignment(4);
1061 }
Matt Fleming3565a062010-08-16 18:57:57 +00001062
Matt Fleming3565a062010-08-16 18:57:57 +00001063 const MCSection *StrtabSection;
1064 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001065 SectionKind::getReadOnly());
Matt Fleming3565a062010-08-16 18:57:57 +00001066 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1067 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +00001068 StringTableIndex = Asm.size();
1069
Rafael Espindolac3c413f2010-09-27 22:04:54 +00001070 WriteRelocations(Asm, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001071
1072 // Symbol table
1073 F = new MCDataFragment(&SymtabSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001074 MCDataFragment *ShndxF = NULL;
1075 if (NeedsSymtabShndx) {
1076 ShndxF = new MCDataFragment(SymtabShndxSD);
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001077 Asm.AddSectionToTheEnd(*this, *SymtabShndxSD, Layout);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001078 }
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001079 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001080 Asm.AddSectionToTheEnd(*this, SymtabSD, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001081
Matt Fleming3565a062010-08-16 18:57:57 +00001082 F = new MCDataFragment(&StrtabSD);
1083 F->getContents().append(StringTable.begin(), StringTable.end());
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001084 Asm.AddSectionToTheEnd(*this, StrtabSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +00001085
Matt Fleming3565a062010-08-16 18:57:57 +00001086 F = new MCDataFragment(&ShstrtabSD);
1087
Matt Fleming3565a062010-08-16 18:57:57 +00001088 // Section header string table.
1089 //
1090 // The first entry of a string table holds a null character so skip
1091 // section 0.
1092 uint64_t Index = 1;
1093 F->getContents() += '\x00';
1094
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001095 StringMap<uint64_t> SecStringMap;
Matt Fleming3565a062010-08-16 18:57:57 +00001096 for (MCAssembler::const_iterator it = Asm.begin(),
1097 ie = Asm.end(); it != ie; ++it) {
Matt Fleming3565a062010-08-16 18:57:57 +00001098 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +00001099 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola51efe7a2010-09-23 14:14:56 +00001100 // FIXME: We could merge suffixes like in .text and .rela.text.
Matt Fleming3565a062010-08-16 18:57:57 +00001101
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001102 StringRef Name = Section.getSectionName();
1103 if (SecStringMap.count(Name)) {
1104 SectionStringTableIndex[&Section] = SecStringMap[Name];
1105 continue;
1106 }
Matt Fleming3565a062010-08-16 18:57:57 +00001107 // Remember the index into the string table so we can write it
1108 // into the sh_name field of the section header table.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001109 SectionStringTableIndex[&Section] = Index;
1110 SecStringMap[Name] = Index;
Matt Fleming3565a062010-08-16 18:57:57 +00001111
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001112 Index += Name.size() + 1;
1113 F->getContents() += Name;
Matt Fleming3565a062010-08-16 18:57:57 +00001114 F->getContents() += '\x00';
1115 }
1116
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001117 Asm.AddSectionToTheEnd(*this, ShstrtabSD, Layout);
Rafael Espindola70703872010-09-30 02:22:20 +00001118}
1119
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001120bool ELFObjectWriter::IsFixupFullyResolved(const MCAssembler &Asm,
1121 const MCValue Target,
1122 bool IsPCRel,
1123 const MCFragment *DF) const {
Rafael Espindola70703872010-09-30 02:22:20 +00001124 // If this is a PCrel relocation, find the section this fixup value is
1125 // relative to.
1126 const MCSection *BaseSection = 0;
1127 if (IsPCRel) {
1128 BaseSection = &DF->getParent()->getSection();
1129 assert(BaseSection);
1130 }
1131
1132 const MCSection *SectionA = 0;
1133 const MCSymbol *SymbolA = 0;
1134 if (const MCSymbolRefExpr *A = Target.getSymA()) {
Rafael Espindola2c920852010-11-16 04:11:46 +00001135 SymbolA = &A->getSymbol();
1136 SectionA = &SymbolA->AliasedSymbol().getSection();
Rafael Espindola70703872010-09-30 02:22:20 +00001137 }
1138
1139 const MCSection *SectionB = 0;
Rafael Espindola12203cc2010-11-21 00:48:25 +00001140 const MCSymbol *SymbolB = 0;
Rafael Espindola70703872010-09-30 02:22:20 +00001141 if (const MCSymbolRefExpr *B = Target.getSymB()) {
Rafael Espindola12203cc2010-11-21 00:48:25 +00001142 SymbolB = &B->getSymbol();
1143 SectionB = &SymbolB->AliasedSymbol().getSection();
Rafael Espindola70703872010-09-30 02:22:20 +00001144 }
1145
1146 if (!BaseSection)
1147 return SectionA == SectionB;
1148
Rafael Espindola12203cc2010-11-21 00:48:25 +00001149 if (SymbolB)
1150 return false;
1151
1152 // Absolute address but PCrel instruction, so we need a relocation.
1153 if (!SymbolA)
1154 return false;
1155
Rafael Espindola2c920852010-11-16 04:11:46 +00001156 // FIXME: This is in here just to match gnu as output. If the two ends
1157 // are in the same section, there is nothing that the linker can do to
1158 // break it.
Rafael Espindola70703872010-09-30 02:22:20 +00001159 const MCSymbolData &DataA = Asm.getSymbolData(*SymbolA);
1160 if (DataA.isExternal())
1161 return false;
1162
Rafael Espindola12203cc2010-11-21 00:48:25 +00001163 return BaseSection == SectionA;
Matt Fleming3565a062010-08-16 18:57:57 +00001164}
1165
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001166void ELFObjectWriter::CreateGroupSections(MCAssembler &Asm,
1167 MCAsmLayout &Layout,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001168 GroupMapTy &GroupMap,
1169 RevGroupMapTy &RevGroupMap) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001170 // Build the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001171 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1172 it != ie; ++it) {
1173 const MCSectionELF &Section =
1174 static_cast<const MCSectionELF&>(it->getSection());
1175 if (!(Section.getFlags() & MCSectionELF::SHF_GROUP))
1176 continue;
1177
1178 const MCSymbol *SignatureSymbol = Section.getGroup();
1179 Asm.getOrCreateSymbolData(*SignatureSymbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001180 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001181 if (!Group) {
1182 Group = Asm.getContext().CreateELFGroupSection();
1183 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1184 Data.setAlignment(4);
1185 MCDataFragment *F = new MCDataFragment(&Data);
1186 String32(*F, ELF::GRP_COMDAT);
1187 }
1188 GroupMap[Group] = SignatureSymbol;
1189 }
1190
1191 // Add sections to the groups
1192 unsigned Index = 1;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001193 unsigned NumGroups = RevGroupMap.size();
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001194 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1195 it != ie; ++it, ++Index) {
1196 const MCSectionELF &Section =
1197 static_cast<const MCSectionELF&>(it->getSection());
1198 if (!(Section.getFlags() & MCSectionELF::SHF_GROUP))
1199 continue;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001200 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001201 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1202 // FIXME: we could use the previous fragment
1203 MCDataFragment *F = new MCDataFragment(&Data);
1204 String32(*F, NumGroups + Index);
1205 }
1206
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001207 for (RevGroupMapTy::const_iterator i = RevGroupMap.begin(),
1208 e = RevGroupMap.end(); i != e; ++i) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001209 const MCSectionELF *Group = i->second;
1210 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001211 Asm.AddSectionToTheEnd(*this, Data, Layout);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001212 }
1213}
1214
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001215void ELFObjectWriter::WriteSection(MCAssembler &Asm,
1216 const SectionIndexMapTy &SectionIndexMap,
1217 uint32_t GroupSymbolIndex,
1218 uint64_t Offset, uint64_t Size,
1219 uint64_t Alignment,
1220 const MCSectionELF &Section) {
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001221 uint64_t sh_link = 0;
1222 uint64_t sh_info = 0;
1223
1224 switch(Section.getType()) {
1225 case ELF::SHT_DYNAMIC:
1226 sh_link = SectionStringTableIndex[&Section];
1227 sh_info = 0;
1228 break;
1229
1230 case ELF::SHT_REL:
1231 case ELF::SHT_RELA: {
1232 const MCSectionELF *SymtabSection;
1233 const MCSectionELF *InfoSection;
1234 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
1235 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001236 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001237 sh_link = SectionIndexMap.lookup(SymtabSection);
1238 assert(sh_link && ".symtab not found");
1239
1240 // Remove ".rel" and ".rela" prefixes.
1241 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1242 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
1243
1244 InfoSection = Asm.getContext().getELFSection(SectionName,
1245 ELF::SHT_PROGBITS, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001246 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001247 sh_info = SectionIndexMap.lookup(InfoSection);
1248 break;
1249 }
1250
1251 case ELF::SHT_SYMTAB:
1252 case ELF::SHT_DYNSYM:
1253 sh_link = StringTableIndex;
1254 sh_info = LastLocalSymbolIndex;
1255 break;
1256
1257 case ELF::SHT_SYMTAB_SHNDX:
1258 sh_link = SymbolTableIndex;
1259 break;
1260
1261 case ELF::SHT_PROGBITS:
1262 case ELF::SHT_STRTAB:
1263 case ELF::SHT_NOBITS:
1264 case ELF::SHT_NULL:
1265 case ELF::SHT_ARM_ATTRIBUTES:
1266 // Nothing to do.
1267 break;
1268
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001269 case ELF::SHT_GROUP: {
1270 sh_link = SymbolTableIndex;
1271 sh_info = GroupSymbolIndex;
1272 break;
1273 }
1274
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001275 default:
1276 assert(0 && "FIXME: sh_type value not supported!");
1277 break;
1278 }
1279
1280 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1281 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1282 Alignment, Section.getEntrySize());
1283}
1284
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001285void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1286 const MCAsmLayout &Layout) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001287 GroupMapTy GroupMap;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001288 RevGroupMapTy RevGroupMap;
1289 CreateGroupSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1290 RevGroupMap);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001291
Rafael Espindolabab2a802010-11-10 21:51:05 +00001292 SectionIndexMapTy SectionIndexMap;
1293
1294 ComputeIndexMap(Asm, SectionIndexMap);
1295
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001296 // Compute symbol table information.
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001297 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap);
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001298
Matt Fleming3565a062010-08-16 18:57:57 +00001299 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001300 const_cast<MCAsmLayout&>(Layout),
1301 SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +00001302
Rafael Espindola1d739a02010-11-10 22:34:07 +00001303 // Update to include the metadata sections.
1304 ComputeIndexMap(Asm, SectionIndexMap);
1305
Matt Fleming3565a062010-08-16 18:57:57 +00001306 // Add 1 for the null section.
1307 unsigned NumSections = Asm.size() + 1;
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001308 uint64_t NaturalAlignment = Is64Bit ? 8 : 4;
1309 uint64_t HeaderSize = Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr);
1310 uint64_t FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001311
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001312 std::vector<const MCSectionELF*> Sections;
1313 Sections.resize(NumSections);
1314
1315 for (SectionIndexMapTy::const_iterator i=
1316 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1317 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
1318 Sections[p.second] = p.first;
1319 }
1320
1321 for (unsigned i = 1; i < NumSections; ++i) {
1322 const MCSectionELF &Section = *Sections[i];
1323 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001324
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001325 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1326
Matt Fleming3565a062010-08-16 18:57:57 +00001327 // Get the size of the section in the output file (including padding).
1328 uint64_t Size = Layout.getSectionFileSize(&SD);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001329
1330 FileOff += Size;
Matt Fleming3565a062010-08-16 18:57:57 +00001331 }
1332
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001333 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1334
Matt Fleming3565a062010-08-16 18:57:57 +00001335 // Write out the ELF header ...
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001336 WriteHeader(FileOff - HeaderSize, NumSections);
1337
1338 FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001339
1340 // ... then all of the sections ...
1341 DenseMap<const MCSection*, uint64_t> SectionOffsetMap;
1342
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001343 for (unsigned i = 1; i < NumSections; ++i) {
1344 const MCSectionELF &Section = *Sections[i];
1345 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001346
1347 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1348 WriteZeros(Padding);
1349 FileOff += Padding;
1350
Matt Fleming3565a062010-08-16 18:57:57 +00001351 // Remember the offset into the file for this section.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001352 SectionOffsetMap[&Section] = FileOff;
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001353
Matt Fleming3565a062010-08-16 18:57:57 +00001354 FileOff += Layout.getSectionFileSize(&SD);
1355
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001356 Asm.WriteSectionData(&SD, Layout, this);
Matt Fleming3565a062010-08-16 18:57:57 +00001357 }
1358
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001359 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1360 WriteZeros(Padding);
1361 FileOff += Padding;
1362
Matt Fleming3565a062010-08-16 18:57:57 +00001363 // ... and then the section header table.
1364 // Should we align the section header table?
1365 //
1366 // Null section first.
Rafael Espindola7be2c332010-10-31 00:16:26 +00001367 uint64_t FirstSectionSize =
1368 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1369 uint32_t FirstSectionLink =
1370 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1371 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming3565a062010-08-16 18:57:57 +00001372
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001373 for (unsigned i = 1; i < NumSections; ++i) {
1374 const MCSectionELF &Section = *Sections[i];
1375 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1376 uint32_t GroupSymbolIndex;
1377 if (Section.getType() != ELF::SHT_GROUP)
1378 GroupSymbolIndex = 0;
1379 else
1380 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm, GroupMap[&Section]);
Matt Fleming3565a062010-08-16 18:57:57 +00001381
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001382 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
1383 SectionOffsetMap[&Section], Layout.getSectionSize(&SD),
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001384 SD.getAlignment(), Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001385 }
1386}
1387
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001388MCObjectWriter *llvm::createELFObjectWriter(raw_ostream &OS,
1389 bool Is64Bit,
1390 Triple::OSType OSType,
1391 uint16_t EMachine,
1392 bool IsLittleEndian,
1393 bool HasRelocationAddend) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001394 switch (EMachine) {
1395 case ELF::EM_386:
1396 case ELF::EM_X86_64:
1397 return new X86ELFObjectWriter(OS, Is64Bit, IsLittleEndian, EMachine,
1398 HasRelocationAddend, OSType); break;
1399 case ELF::EM_ARM:
1400 return new ARMELFObjectWriter(OS, Is64Bit, IsLittleEndian, EMachine,
1401 HasRelocationAddend, OSType); break;
Wesley Peck4b047132010-11-21 22:06:28 +00001402 case ELF::EM_MBLAZE:
1403 return new MBlazeELFObjectWriter(OS, Is64Bit, IsLittleEndian, EMachine,
1404 HasRelocationAddend, OSType); break;
Benjamin Kramer32858772010-11-15 19:20:50 +00001405 default: llvm_unreachable("Unsupported architecture"); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001406 }
1407}
1408
1409
1410/// START OF SUBCLASSES for ELFObjectWriter
1411//===- ARMELFObjectWriter -------------------------------------------===//
1412
1413ARMELFObjectWriter::ARMELFObjectWriter(raw_ostream &_OS, bool _Is64Bit,
1414 bool _IsLittleEndian,
1415 uint16_t _EMachine, bool _HasRelocationAddend,
1416 Triple::OSType _OSType)
1417 : ELFObjectWriter(_OS, _Is64Bit, _IsLittleEndian, _EMachine,
1418 _HasRelocationAddend, _OSType)
1419{}
1420
1421ARMELFObjectWriter::~ARMELFObjectWriter()
1422{}
1423
1424void ARMELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
1425 const MCAsmLayout &Layout,
1426 const MCFragment *Fragment,
1427 const MCFixup &Fixup,
1428 MCValue Target,
1429 uint64_t &FixedValue) {
1430 assert(0 && "ARMELFObjectWriter::RecordRelocation() unimplemented");
1431}
1432
Wesley Peck4b047132010-11-21 22:06:28 +00001433//===- MBlazeELFObjectWriter -------------------------------------------===//
Jason W Kimd3443e92010-11-15 16:18:39 +00001434
Wesley Peck4b047132010-11-21 22:06:28 +00001435MBlazeELFObjectWriter::MBlazeELFObjectWriter(raw_ostream &_OS, bool _Is64Bit,
1436 bool _IsLittleEndian,
1437 uint16_t _EMachine,
1438 bool _HasRelocationAddend,
1439 Triple::OSType _OSType)
1440 : ELFObjectWriter(_OS, _Is64Bit, _IsLittleEndian, _EMachine,
1441 _HasRelocationAddend, _OSType) {
1442}
1443
1444MBlazeELFObjectWriter::~MBlazeELFObjectWriter() {
1445}
1446
1447void MBlazeELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
1448 const MCAsmLayout &Layout,
1449 const MCFragment *Fragment,
1450 const MCFixup &Fixup,
1451 MCValue Target,
1452 uint64_t &FixedValue) {
1453 int64_t Addend = 0;
1454 int Index = 0;
1455 int64_t Value = Target.getConstant();
1456 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
1457 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
1458 const MCSymbol *RelocSymbol = SymbolToReloc(Asm, Target, *Fragment);
1459
Jason W Kim4a511f02010-11-22 18:41:13 +00001460 bool IsPCRel = isFixupKindPCRel(Fixup.getKind());
Wesley Peck4b047132010-11-21 22:06:28 +00001461 if (!Target.isAbsolute()) {
1462 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
1463 const MCSymbol &SymbolB = RefB->getSymbol();
1464 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
1465 IsPCRel = true;
1466 MCSectionData *Sec = Fragment->getParent();
1467
1468 // Offset of the symbol in the section
1469 int64_t a = Layout.getSymbolAddress(&SDB) - Layout.getSectionAddress(Sec);
1470
1471 // Ofeset of the relocation in the section
1472 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
1473 Value += b - a;
1474 }
1475
1476 if (!RelocSymbol) {
1477 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
1478 MCFragment *F = SD.getFragment();
1479
1480 Index = F->getParent()->getOrdinal();
1481
1482 MCSectionData *FSD = F->getParent();
1483 // Offset of the symbol in the section
1484 Value += Layout.getSymbolAddress(&SD) - Layout.getSectionAddress(FSD);
1485 } else {
1486 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
1487 WeakrefUsedInReloc.insert(RelocSymbol);
1488 else
1489 UsedInReloc.insert(RelocSymbol);
1490 Index = -1;
1491 }
1492 Addend = Value;
1493 }
1494
1495 FixedValue = Value;
1496
1497 // determine the type of the relocation
1498 unsigned Type;
1499 if (IsPCRel) {
1500 switch ((unsigned)Fixup.getKind()) {
1501 default:
1502 llvm_unreachable("Unimplemented");
1503 case MBlaze::reloc_pcrel_4byte:
1504 Type = ELF::R_MICROBLAZE_64_PCREL;
1505 break;
1506 case MBlaze::reloc_pcrel_2byte:
1507 Type = ELF::R_MICROBLAZE_32_PCREL;
1508 break;
1509 }
1510 } else {
1511 switch ((unsigned)Fixup.getKind()) {
1512 default: llvm_unreachable("invalid fixup kind!");
1513 case FK_Data_4:
1514 Type = (RelocSymbol || Addend !=0) ? ELF::R_MICROBLAZE_32
1515 : ELF::R_MICROBLAZE_64;
1516 break;
1517 case FK_Data_2:
1518 Type = ELF::R_MICROBLAZE_32;
1519 break;
1520 }
1521 }
1522
1523 MCSymbolRefExpr::VariantKind Modifier = Target.getSymA()->getKind();
1524 if (RelocNeedsGOT(Modifier))
1525 NeedsGOT = true;
1526
Jason W Kim858e7502010-11-22 18:42:07 +00001527 uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
Jason W Kim4a511f02010-11-22 18:41:13 +00001528 Fixup.getOffset();
Wesley Peck4b047132010-11-21 22:06:28 +00001529
Jason W Kim4a511f02010-11-22 18:41:13 +00001530 if (! HasRelocationAddend) Addend = 0;
1531 ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend);
Wesley Peck4b047132010-11-21 22:06:28 +00001532 Relocations[Fragment->getParent()].push_back(ERE);
1533}
Jason W Kimd3443e92010-11-15 16:18:39 +00001534
1535//===- X86ELFObjectWriter -------------------------------------------===//
1536
1537
1538X86ELFObjectWriter::X86ELFObjectWriter(raw_ostream &_OS, bool _Is64Bit,
1539 bool _IsLittleEndian,
1540 uint16_t _EMachine, bool _HasRelocationAddend,
1541 Triple::OSType _OSType)
1542 : ELFObjectWriter(_OS, _Is64Bit, _IsLittleEndian, _EMachine,
1543 _HasRelocationAddend, _OSType)
1544{}
1545
1546X86ELFObjectWriter::~X86ELFObjectWriter()
1547{}
1548
1549void X86ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
1550 const MCAsmLayout &Layout,
1551 const MCFragment *Fragment,
1552 const MCFixup &Fixup,
1553 MCValue Target,
1554 uint64_t &FixedValue) {
1555 int64_t Addend = 0;
1556 int Index = 0;
1557 int64_t Value = Target.getConstant();
Rafael Espindola12203cc2010-11-21 00:48:25 +00001558 const MCSymbol *RelocSymbol = NULL;
Jason W Kimd3443e92010-11-15 16:18:39 +00001559
Jason W Kim4a511f02010-11-22 18:41:13 +00001560 bool IsPCRel = isFixupKindPCRel(Fixup.getKind());
Jason W Kimd3443e92010-11-15 16:18:39 +00001561 if (!Target.isAbsolute()) {
Rafael Espindola12203cc2010-11-21 00:48:25 +00001562 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
1563 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
1564 RelocSymbol = SymbolToReloc(Asm, Target, *Fragment);
1565
Jason W Kimd3443e92010-11-15 16:18:39 +00001566 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
1567 const MCSymbol &SymbolB = RefB->getSymbol();
1568 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
1569 IsPCRel = true;
1570 MCSectionData *Sec = Fragment->getParent();
1571
1572 // Offset of the symbol in the section
1573 int64_t a = Layout.getSymbolAddress(&SDB) - Layout.getSectionAddress(Sec);
1574
1575 // Ofeset of the relocation in the section
1576 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
1577 Value += b - a;
1578 }
1579
1580 if (!RelocSymbol) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +00001581 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
Jason W Kimd3443e92010-11-15 16:18:39 +00001582 MCFragment *F = SD.getFragment();
1583
Rafael Espindola12203cc2010-11-21 00:48:25 +00001584 Index = F->getParent()->getOrdinal() + 1;
Jason W Kimd3443e92010-11-15 16:18:39 +00001585
1586 MCSectionData *FSD = F->getParent();
1587 // Offset of the symbol in the section
1588 Value += Layout.getSymbolAddress(&SD) - Layout.getSectionAddress(FSD);
1589 } else {
1590 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
1591 WeakrefUsedInReloc.insert(RelocSymbol);
1592 else
1593 UsedInReloc.insert(RelocSymbol);
1594 Index = -1;
1595 }
1596 Addend = Value;
1597 // Compensate for the addend on i386.
1598 if (Is64Bit)
1599 Value = 0;
1600 }
1601
1602 FixedValue = Value;
1603
1604 // determine the type of the relocation
1605
Rafael Espindola12203cc2010-11-21 00:48:25 +00001606 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1607 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
Jason W Kimd3443e92010-11-15 16:18:39 +00001608 unsigned Type;
1609 if (Is64Bit) {
1610 if (IsPCRel) {
1611 switch (Modifier) {
1612 default:
1613 llvm_unreachable("Unimplemented");
1614 case MCSymbolRefExpr::VK_None:
1615 Type = ELF::R_X86_64_PC32;
1616 break;
1617 case MCSymbolRefExpr::VK_PLT:
1618 Type = ELF::R_X86_64_PLT32;
1619 break;
1620 case MCSymbolRefExpr::VK_GOTPCREL:
1621 Type = ELF::R_X86_64_GOTPCREL;
1622 break;
1623 case MCSymbolRefExpr::VK_GOTTPOFF:
1624 Type = ELF::R_X86_64_GOTTPOFF;
1625 break;
1626 case MCSymbolRefExpr::VK_TLSGD:
1627 Type = ELF::R_X86_64_TLSGD;
1628 break;
1629 case MCSymbolRefExpr::VK_TLSLD:
1630 Type = ELF::R_X86_64_TLSLD;
1631 break;
1632 }
1633 } else {
1634 switch ((unsigned)Fixup.getKind()) {
1635 default: llvm_unreachable("invalid fixup kind!");
1636 case FK_Data_8: Type = ELF::R_X86_64_64; break;
1637 case X86::reloc_signed_4byte:
1638 case X86::reloc_pcrel_4byte:
1639 assert(isInt<32>(Target.getConstant()));
1640 switch (Modifier) {
1641 default:
1642 llvm_unreachable("Unimplemented");
1643 case MCSymbolRefExpr::VK_None:
1644 Type = ELF::R_X86_64_32S;
1645 break;
1646 case MCSymbolRefExpr::VK_GOT:
1647 Type = ELF::R_X86_64_GOT32;
1648 break;
1649 case MCSymbolRefExpr::VK_GOTPCREL:
1650 Type = ELF::R_X86_64_GOTPCREL;
1651 break;
1652 case MCSymbolRefExpr::VK_TPOFF:
1653 Type = ELF::R_X86_64_TPOFF32;
1654 break;
1655 case MCSymbolRefExpr::VK_DTPOFF:
1656 Type = ELF::R_X86_64_DTPOFF32;
1657 break;
1658 }
1659 break;
1660 case FK_Data_4:
1661 Type = ELF::R_X86_64_32;
1662 break;
1663 case FK_Data_2: Type = ELF::R_X86_64_16; break;
1664 case X86::reloc_pcrel_1byte:
1665 case FK_Data_1: Type = ELF::R_X86_64_8; break;
1666 }
1667 }
1668 } else {
1669 if (IsPCRel) {
1670 switch (Modifier) {
1671 default:
1672 llvm_unreachable("Unimplemented");
1673 case MCSymbolRefExpr::VK_None:
1674 Type = ELF::R_386_PC32;
1675 break;
1676 case MCSymbolRefExpr::VK_PLT:
1677 Type = ELF::R_386_PLT32;
1678 break;
1679 }
1680 } else {
1681 switch ((unsigned)Fixup.getKind()) {
1682 default: llvm_unreachable("invalid fixup kind!");
1683
1684 case X86::reloc_global_offset_table:
1685 Type = ELF::R_386_GOTPC;
1686 break;
1687
1688 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
1689 // instead?
1690 case X86::reloc_signed_4byte:
1691 case X86::reloc_pcrel_4byte:
1692 case FK_Data_4:
1693 switch (Modifier) {
1694 default:
1695 llvm_unreachable("Unimplemented");
1696 case MCSymbolRefExpr::VK_None:
1697 Type = ELF::R_386_32;
1698 break;
1699 case MCSymbolRefExpr::VK_GOT:
1700 Type = ELF::R_386_GOT32;
1701 break;
1702 case MCSymbolRefExpr::VK_GOTOFF:
1703 Type = ELF::R_386_GOTOFF;
1704 break;
1705 case MCSymbolRefExpr::VK_TLSGD:
1706 Type = ELF::R_386_TLS_GD;
1707 break;
1708 case MCSymbolRefExpr::VK_TPOFF:
1709 Type = ELF::R_386_TLS_LE_32;
1710 break;
1711 case MCSymbolRefExpr::VK_INDNTPOFF:
1712 Type = ELF::R_386_TLS_IE;
1713 break;
1714 case MCSymbolRefExpr::VK_NTPOFF:
1715 Type = ELF::R_386_TLS_LE;
1716 break;
1717 case MCSymbolRefExpr::VK_GOTNTPOFF:
1718 Type = ELF::R_386_TLS_GOTIE;
1719 break;
1720 case MCSymbolRefExpr::VK_TLSLDM:
1721 Type = ELF::R_386_TLS_LDM;
1722 break;
1723 case MCSymbolRefExpr::VK_DTPOFF:
1724 Type = ELF::R_386_TLS_LDO_32;
1725 break;
1726 }
1727 break;
1728 case FK_Data_2: Type = ELF::R_386_16; break;
1729 case X86::reloc_pcrel_1byte:
1730 case FK_Data_1: Type = ELF::R_386_8; break;
1731 }
1732 }
1733 }
1734
1735 if (RelocNeedsGOT(Modifier))
1736 NeedsGOT = true;
1737
Jason W Kimd3443e92010-11-15 16:18:39 +00001738
Jason W Kim858e7502010-11-22 18:42:07 +00001739 uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
Jason W Kim4a511f02010-11-22 18:41:13 +00001740 Fixup.getOffset();
Jason W Kimd3443e92010-11-15 16:18:39 +00001741
Jason W Kim4a511f02010-11-22 18:41:13 +00001742 if (! HasRelocationAddend) Addend = 0;
1743 ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend);
Jason W Kimd3443e92010-11-15 16:18:39 +00001744 Relocations[Fragment->getParent()].push_back(ERE);
Matt Fleming3565a062010-08-16 18:57:57 +00001745}