blob: 4ea6308f45c66a8e3c73b5c22934f0fabb6fc3a6 [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"
33
34#include <vector>
35using namespace llvm;
36
Rafael Espindolaad49cf52010-09-18 15:03:21 +000037static unsigned GetType(const MCSymbolData &SD) {
38 uint32_t Type = (SD.getFlags() & (0xf << ELF_STT_Shift)) >> ELF_STT_Shift;
39 assert(Type == ELF::STT_NOTYPE || Type == ELF::STT_OBJECT ||
40 Type == ELF::STT_FUNC || Type == ELF::STT_SECTION ||
41 Type == ELF::STT_FILE || Type == ELF::STT_COMMON ||
42 Type == ELF::STT_TLS);
43 return Type;
44}
45
Rafael Espindolae15eb4e2010-09-23 19:55:14 +000046static unsigned GetBinding(const MCSymbolData &SD) {
47 uint32_t Binding = (SD.getFlags() & (0xf << ELF_STB_Shift)) >> ELF_STB_Shift;
48 assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
49 Binding == ELF::STB_WEAK);
50 return Binding;
51}
52
53static void SetBinding(MCSymbolData &SD, unsigned Binding) {
54 assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
55 Binding == ELF::STB_WEAK);
56 uint32_t OtherFlags = SD.getFlags() & ~(0xf << ELF_STB_Shift);
57 SD.setFlags(OtherFlags | (Binding << ELF_STB_Shift));
58}
59
Rafael Espindola152c1062010-10-06 21:02:29 +000060static unsigned GetVisibility(MCSymbolData &SD) {
61 unsigned Visibility =
62 (SD.getFlags() & (0xf << ELF_STV_Shift)) >> ELF_STV_Shift;
63 assert(Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_INTERNAL ||
64 Visibility == ELF::STV_HIDDEN || Visibility == ELF::STV_PROTECTED);
65 return Visibility;
66}
67
Rafael Espindolacebdc012010-10-04 19:46:28 +000068static bool isFixupKindX86PCRel(unsigned Kind) {
69 switch (Kind) {
70 default:
71 return false;
72 case X86::reloc_pcrel_1byte:
73 case X86::reloc_pcrel_4byte:
74 case X86::reloc_riprel_4byte:
75 case X86::reloc_riprel_4byte_movq_load:
76 return true;
77 }
78}
79
Rafael Espindolaa0a2f872010-10-28 14:22:44 +000080static bool RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant) {
81 switch (Variant) {
Rafael Espindola5c77c162010-10-05 15:48:37 +000082 default:
83 return false;
Rafael Espindolaa0a2f872010-10-28 14:22:44 +000084 case MCSymbolRefExpr::VK_GOT:
85 case MCSymbolRefExpr::VK_PLT:
86 case MCSymbolRefExpr::VK_GOTPCREL:
87 case MCSymbolRefExpr::VK_TPOFF:
88 case MCSymbolRefExpr::VK_TLSGD:
89 case MCSymbolRefExpr::VK_GOTTPOFF:
90 case MCSymbolRefExpr::VK_INDNTPOFF:
91 case MCSymbolRefExpr::VK_NTPOFF:
92 case MCSymbolRefExpr::VK_GOTNTPOFF:
Rafael Espindolaa264f722010-10-28 14:37:09 +000093 case MCSymbolRefExpr::VK_TLSLDM:
Rafael Espindola0cf15d62010-10-28 14:48:59 +000094 case MCSymbolRefExpr::VK_DTPOFF:
Rafael Espindolab4d17212010-10-28 15:02:40 +000095 case MCSymbolRefExpr::VK_TLSLD:
Rafael Espindola5c77c162010-10-05 15:48:37 +000096 return true;
97 }
98}
99
Matt Fleming3565a062010-08-16 18:57:57 +0000100namespace {
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000101 class ELFObjectWriter : public MCObjectWriter {
Jason W Kimd3443e92010-11-15 16:18:39 +0000102 protected:
Chris Lattnerb188a372010-08-28 03:21:03 +0000103 /*static bool isFixupKindX86RIPRel(unsigned Kind) {
Matt Fleming3565a062010-08-16 18:57:57 +0000104 return Kind == X86::reloc_riprel_4byte ||
105 Kind == X86::reloc_riprel_4byte_movq_load;
Chris Lattnerb188a372010-08-28 03:21:03 +0000106 }*/
Matt Fleming3565a062010-08-16 18:57:57 +0000107
108
109 /// ELFSymbolData - Helper struct for containing some precomputed information
110 /// on symbols.
111 struct ELFSymbolData {
112 MCSymbolData *SymbolData;
113 uint64_t StringIndex;
114 uint32_t SectionIndex;
115
116 // Support lexicographic sorting.
117 bool operator<(const ELFSymbolData &RHS) const {
Rafael Espindolaad49cf52010-09-18 15:03:21 +0000118 if (GetType(*SymbolData) == ELF::STT_FILE)
119 return true;
120 if (GetType(*RHS.SymbolData) == ELF::STT_FILE)
121 return false;
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000122 return SymbolData->getSymbol().getName() <
123 RHS.SymbolData->getSymbol().getName();
Matt Fleming3565a062010-08-16 18:57:57 +0000124 }
125 };
126
127 /// @name Relocation Data
128 /// @{
129
130 struct ELFRelocationEntry {
131 // Make these big enough for both 32-bit and 64-bit
132 uint64_t r_offset;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000133 int Index;
134 unsigned Type;
135 const MCSymbol *Symbol;
Matt Fleming3565a062010-08-16 18:57:57 +0000136 uint64_t r_addend;
137
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);
361
362 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);
368 };
369
370
371 //===- ARMELFObjectWriter -------------------------------------------===//
372
373 class ARMELFObjectWriter : public ELFObjectWriter {
374 public:
375 ARMELFObjectWriter(raw_ostream &_OS, bool _Is64Bit, bool IsLittleEndian,
376 uint16_t _EMachine, bool _HasRelAddend,
377 Triple::OSType _OSType);
378
379 virtual ~ARMELFObjectWriter();
380 virtual void RecordRelocation(const MCAssembler &Asm,
381 const MCAsmLayout &Layout,
382 const MCFragment *Fragment,
383 const MCFixup &Fixup, MCValue Target,
384 uint64_t &FixedValue);
385 };
Matt Fleming3565a062010-08-16 18:57:57 +0000386}
387
Jason W Kimd3443e92010-11-15 16:18:39 +0000388ELFObjectWriter::~ELFObjectWriter()
389{}
390
Matt Fleming3565a062010-08-16 18:57:57 +0000391// Emit the ELF header.
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000392void ELFObjectWriter::WriteHeader(uint64_t SectionDataSize,
393 unsigned NumberOfSections) {
Matt Fleming3565a062010-08-16 18:57:57 +0000394 // ELF Header
395 // ----------
396 //
397 // Note
398 // ----
399 // emitWord method behaves differently for ELF32 and ELF64, writing
400 // 4 bytes in the former and 8 in the latter.
401
402 Write8(0x7f); // e_ident[EI_MAG0]
403 Write8('E'); // e_ident[EI_MAG1]
404 Write8('L'); // e_ident[EI_MAG2]
405 Write8('F'); // e_ident[EI_MAG3]
406
407 Write8(Is64Bit ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
408
409 // e_ident[EI_DATA]
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000410 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
Matt Fleming3565a062010-08-16 18:57:57 +0000411
412 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky5baf79e2010-09-09 17:57:50 +0000413 // e_ident[EI_OSABI]
414 switch (OSType) {
415 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
416 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
417 default: Write8(ELF::ELFOSABI_NONE); break;
418 }
Matt Fleming3565a062010-08-16 18:57:57 +0000419 Write8(0); // e_ident[EI_ABIVERSION]
420
421 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
422
423 Write16(ELF::ET_REL); // e_type
424
Wesley Peckeecb8582010-10-22 15:52:49 +0000425 Write16(EMachine); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +0000426
427 Write32(ELF::EV_CURRENT); // e_version
428 WriteWord(0); // e_entry, no entry point in .o file
429 WriteWord(0); // e_phoff, no program header for .o
Benjamin Kramereb976772010-08-17 17:02:29 +0000430 WriteWord(SectionDataSize + (Is64Bit ? sizeof(ELF::Elf64_Ehdr) :
431 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000432
433 // FIXME: Make this configurable.
434 Write32(0); // e_flags = whatever the target wants
435
436 // e_ehsize = ELF header size
437 Write16(Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
438
439 Write16(0); // e_phentsize = prog header entry size
440 Write16(0); // e_phnum = # prog header entries = 0
441
442 // e_shentsize = Section header entry size
443 Write16(Is64Bit ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
444
445 // e_shnum = # of section header ents
Rafael Espindola7be2c332010-10-31 00:16:26 +0000446 if (NumberOfSections >= ELF::SHN_LORESERVE)
447 Write16(0);
448 else
449 Write16(NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000450
451 // e_shstrndx = Section # of '.shstrtab'
Rafael Espindola7be2c332010-10-31 00:16:26 +0000452 if (NumberOfSections >= ELF::SHN_LORESERVE)
453 Write16(ELF::SHN_XINDEX);
454 else
455 Write16(ShstrtabIndex);
Matt Fleming3565a062010-08-16 18:57:57 +0000456}
457
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000458void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
459 MCDataFragment *ShndxF,
460 uint64_t name,
461 uint8_t info, uint64_t value,
462 uint64_t size, uint8_t other,
463 uint32_t shndx,
464 bool Reserved) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000465 if (ShndxF) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000466 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000467 String32(*ShndxF, shndx);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000468 else
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000469 String32(*ShndxF, 0);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000470 }
471
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000472 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
473 uint16_t(ELF::SHN_XINDEX) : shndx;
474
Matt Fleming3565a062010-08-16 18:57:57 +0000475 if (Is64Bit) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000476 String32(*SymtabF, name); // st_name
477 String8(*SymtabF, info); // st_info
478 String8(*SymtabF, other); // st_other
479 String16(*SymtabF, Index); // st_shndx
480 String64(*SymtabF, value); // st_value
481 String64(*SymtabF, size); // st_size
Matt Fleming3565a062010-08-16 18:57:57 +0000482 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000483 String32(*SymtabF, name); // st_name
484 String32(*SymtabF, value); // st_value
485 String32(*SymtabF, size); // st_size
486 String8(*SymtabF, info); // st_info
487 String8(*SymtabF, other); // st_other
488 String16(*SymtabF, Index); // st_shndx
Matt Fleming3565a062010-08-16 18:57:57 +0000489 }
490}
491
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000492static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout) {
493 if (Data.isCommon() && Data.isExternal())
494 return Data.getCommonAlignment();
495
496 const MCSymbol &Symbol = Data.getSymbol();
497 if (!Symbol.isInSection())
498 return 0;
499
Rafael Espindola1973d432010-10-28 19:39:57 +0000500 if (MCFragment *FF = Data.getFragment())
501 return Layout.getSymbolAddress(&Data) -
502 Layout.getSectionAddress(FF->getParent());
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000503
504 return 0;
505}
506
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000507void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm) {
Rafael Espindola88182132010-10-27 15:18:17 +0000508 // The presence of symbol versions causes undefined symbols and
509 // versions declared with @@@ to be renamed.
510
511 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
512 ie = Asm.symbol_end(); it != ie; ++it) {
513 const MCSymbol &Alias = it->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000514 const MCSymbol &Symbol = Alias.AliasedSymbol();
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000515 MCSymbolData &SD = Asm.getSymbolData(Symbol);
516
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000517 // Not an alias.
518 if (&Symbol == &Alias)
519 continue;
520
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000521 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000522 size_t Pos = AliasName.find('@');
523 if (Pos == StringRef::npos)
524 continue;
525
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000526 // Aliases defined with .symvar copy the binding from the symbol they alias.
527 // This is the first place we are able to copy this information.
528 it->setExternal(SD.isExternal());
529 SetBinding(*it, GetBinding(SD));
530
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000531 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000532 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
533 continue;
534
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000535 // FIXME: produce a better error message.
536 if (Symbol.isUndefined() && Rest.startswith("@@") &&
537 !Rest.startswith("@@@"))
538 report_fatal_error("A @@ version cannot be undefined");
539
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000540 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000541 }
542}
543
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000544void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
545 MCDataFragment *ShndxF,
546 ELFSymbolData &MSD,
547 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000548 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000549 MCSymbolData &Data =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000550 Layout.getAssembler().getSymbolData(OrigData.getSymbol().AliasedSymbol());
Rafael Espindola152c1062010-10-06 21:02:29 +0000551
Rafael Espindola7be2c332010-10-31 00:16:26 +0000552 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
553 Data.getSymbol().isVariable();
554
Rafael Espindola152c1062010-10-06 21:02:29 +0000555 uint8_t Binding = GetBinding(OrigData);
556 uint8_t Visibility = GetVisibility(OrigData);
557 uint8_t Type = GetType(Data);
558
559 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
560 uint8_t Other = Visibility;
561
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000562 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000563 uint64_t Size = 0;
564 const MCExpr *ESize;
565
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000566 assert(!(Data.isCommon() && !Data.isExternal()));
567
Matt Fleming3565a062010-08-16 18:57:57 +0000568 ESize = Data.getSize();
569 if (Data.getSize()) {
570 MCValue Res;
571 if (ESize->getKind() == MCExpr::Binary) {
572 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(ESize);
573
574 if (BE->EvaluateAsRelocatable(Res, &Layout)) {
Benjamin Kramer24f12062010-10-17 07:38:40 +0000575 assert(!Res.getSymA() || !Res.getSymA()->getSymbol().isDefined());
576 assert(!Res.getSymB() || !Res.getSymB()->getSymbol().isDefined());
Rafael Espindolaf230df92010-10-16 18:23:53 +0000577 Size = Res.getConstant();
Matt Fleming3565a062010-08-16 18:57:57 +0000578 }
579 } else if (ESize->getKind() == MCExpr::Constant) {
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000580 Size = static_cast<const MCConstantExpr *>(ESize)->getValue();
Matt Fleming3565a062010-08-16 18:57:57 +0000581 } else {
582 assert(0 && "Unsupported size expression");
583 }
584 }
585
586 // Write out the symbol table entry
Rafael Espindola7be2c332010-10-31 00:16:26 +0000587 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
588 Size, Other, MSD.SectionIndex, IsReserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000589}
590
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000591void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
592 MCDataFragment *ShndxF,
593 const MCAssembler &Asm,
594 const MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000595 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000596 // The string table must be emitted first because we need the index
597 // into the string table for all the symbol names.
598 assert(StringTable.size() && "Missing string table");
599
600 // FIXME: Make sure the start of the symbol table is aligned.
601
602 // The first entry is the undefined symbol entry.
Rafael Espindola7be2c332010-10-31 00:16:26 +0000603 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
Matt Fleming3565a062010-08-16 18:57:57 +0000604
605 // Write the symbol table entries.
606 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
607 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
608 ELFSymbolData &MSD = LocalSymbolData[i];
Rafael Espindola7be2c332010-10-31 00:16:26 +0000609 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000610 }
611
Rafael Espindola71859c62010-09-16 19:46:31 +0000612 // Write out a symbol table entry for each regular section.
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000613 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
614 ++i) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000615 const MCSectionELF &Section =
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000616 static_cast<const MCSectionELF&>(i->getSection());
617 if (Section.getType() == ELF::SHT_RELA ||
618 Section.getType() == ELF::SHT_REL ||
619 Section.getType() == ELF::SHT_STRTAB ||
620 Section.getType() == ELF::SHT_SYMTAB)
Eli Friedmana44fa242010-08-16 21:17:09 +0000621 continue;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000622 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000623 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section), false);
Eli Friedmana44fa242010-08-16 21:17:09 +0000624 LastLocalSymbolIndex++;
625 }
Matt Fleming3565a062010-08-16 18:57:57 +0000626
627 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
628 ELFSymbolData &MSD = ExternalSymbolData[i];
629 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000630 assert(((Data.getFlags() & ELF_STB_Global) ||
631 (Data.getFlags() & ELF_STB_Weak)) &&
632 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000633 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000634 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000635 LastLocalSymbolIndex++;
636 }
637
638 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
639 ELFSymbolData &MSD = UndefinedSymbolData[i];
640 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000641 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000642 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000643 LastLocalSymbolIndex++;
644 }
645}
646
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000647const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm,
648 const MCValue &Target,
649 const MCFragment &F) const {
650 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000651 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
652 const MCSymbol *Renamed = Renames.lookup(&Symbol);
653 const MCSymbolData &SD = Asm.getSymbolData(Symbol);
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000654
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000655 if (ASymbol.isUndefined()) {
656 if (Renamed)
657 return Renamed;
658 return &ASymbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000659 }
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000660
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000661 if (SD.isExternal()) {
662 if (Renamed)
663 return Renamed;
664 return &Symbol;
665 }
Rafael Espindola73ffea42010-09-25 05:42:19 +0000666
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000667 const MCSectionELF &Section =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000668 static_cast<const MCSectionELF&>(ASymbol.getSection());
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000669
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000670 if (Section.getKind().isBSS())
671 return NULL;
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000672
Rafael Espindola8cecf252010-10-06 16:23:36 +0000673 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000674 const MCSectionELF &Sec2 =
675 static_cast<const MCSectionELF&>(F.getParent()->getSection());
676
Rafael Espindola8cecf252010-10-06 16:23:36 +0000677 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000678 (Kind == MCSymbolRefExpr::VK_PLT ||
679 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000680 Kind == MCSymbolRefExpr::VK_GOTOFF)) {
681 if (Renamed)
682 return Renamed;
683 return &Symbol;
684 }
Rafael Espindola3729d002010-10-05 23:57:26 +0000685
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000686 if (Section.getFlags() & MCSectionELF::SHF_MERGE) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000687 if (Target.getConstant() == 0)
688 return NULL;
689 if (Renamed)
690 return Renamed;
691 return &Symbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000692 }
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000693
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000694 return NULL;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000695}
696
Matt Fleming3565a062010-08-16 18:57:57 +0000697
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000698uint64_t
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000699ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
700 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000701 MCSymbolData &SD = Asm.getSymbolData(*S);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000702 return SD.getIndex();
Matt Fleming3565a062010-08-16 18:57:57 +0000703}
704
Rafael Espindola737cd212010-10-05 18:01:23 +0000705static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
Rafael Espindola88182132010-10-27 15:18:17 +0000706 bool Used, bool Renamed) {
Rafael Espindola484291c2010-11-01 14:28:48 +0000707 if (Data.getFlags() & ELF_Other_Weakref)
708 return false;
709
Rafael Espindolabd701182010-10-19 19:31:37 +0000710 if (Used)
711 return true;
712
Rafael Espindola88182132010-10-27 15:18:17 +0000713 if (Renamed)
714 return false;
715
Rafael Espindola737cd212010-10-05 18:01:23 +0000716 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000717
Rafael Espindolad1798862010-10-29 23:09:31 +0000718 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
719 return true;
720
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000721 const MCSymbol &A = Symbol.AliasedSymbol();
Rafael Espindolad1798862010-10-29 23:09:31 +0000722 if (!A.isVariable() && A.isUndefined() && !Data.isCommon())
Rafael Espindolaa6866962010-10-27 14:44:52 +0000723 return false;
724
Rafael Espindola737cd212010-10-05 18:01:23 +0000725 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
726 return false;
727
Rafael Espindolabd701182010-10-19 19:31:37 +0000728 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000729 return false;
730
731 return true;
732}
733
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000734static bool isLocal(const MCSymbolData &Data, bool isSignature,
735 bool isUsedInReloc) {
Rafael Espindola737cd212010-10-05 18:01:23 +0000736 if (Data.isExternal())
737 return false;
738
739 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000740 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000741
742 if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
743 if (isSignature && !isUsedInReloc)
744 return true;
745
Rafael Espindola737cd212010-10-05 18:01:23 +0000746 return false;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000747 }
Rafael Espindola737cd212010-10-05 18:01:23 +0000748
749 return true;
750}
751
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000752void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
753 SectionIndexMapTy &SectionIndexMap) {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000754 unsigned Index = 1;
755 for (MCAssembler::iterator it = Asm.begin(),
756 ie = Asm.end(); it != ie; ++it) {
757 const MCSectionELF &Section =
758 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000759 if (Section.getType() != ELF::SHT_GROUP)
760 continue;
761 SectionIndexMap[&Section] = Index++;
762 }
763
764 for (MCAssembler::iterator it = Asm.begin(),
765 ie = Asm.end(); it != ie; ++it) {
766 const MCSectionELF &Section =
767 static_cast<const MCSectionELF &>(it->getSection());
768 if (Section.getType() == ELF::SHT_GROUP)
769 continue;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000770 SectionIndexMap[&Section] = Index++;
771 }
772}
773
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000774void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000775 const SectionIndexMapTy &SectionIndexMap,
776 RevGroupMapTy RevGroupMap) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000777 // FIXME: Is this the correct place to do this?
778 if (NeedsGOT) {
779 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
780 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
781 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
782 Data.setExternal(true);
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000783 SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindola5c77c162010-10-05 15:48:37 +0000784 }
785
Matt Fleming3565a062010-08-16 18:57:57 +0000786 // Build section lookup table.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000787 int NumRegularSections = Asm.size();
Matt Fleming3565a062010-08-16 18:57:57 +0000788
789 // Index 0 is always the empty string.
790 StringMap<uint64_t> StringIndexMap;
791 StringTable += '\x00';
792
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000793 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000794 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
795 ie = Asm.symbol_end(); it != ie; ++it) {
796 const MCSymbol &Symbol = it->getSymbol();
797
Rafael Espindola484291c2010-11-01 14:28:48 +0000798 bool Used = UsedInReloc.count(&Symbol);
799 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000800 bool isSignature = RevGroupMap.count(&Symbol);
801
802 if (!isInSymtab(Asm, *it,
803 Used || WeakrefUsed || isSignature,
Rafael Espindola88182132010-10-27 15:18:17 +0000804 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000805 continue;
806
Matt Fleming3565a062010-08-16 18:57:57 +0000807 ELFSymbolData MSD;
808 MSD.SymbolData = it;
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000809 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Matt Fleming3565a062010-08-16 18:57:57 +0000810
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000811 // Undefined symbols are global, but this is the first place we
812 // are able to set it.
813 bool Local = isLocal(*it, isSignature, Used);
814 if (!Local && GetBinding(*it) == ELF::STB_LOCAL) {
815 MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
816 SetBinding(*it, ELF::STB_GLOBAL);
817 SetBinding(SD, ELF::STB_GLOBAL);
818 }
819
Rafael Espindola484291c2010-11-01 14:28:48 +0000820 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
821 SetBinding(*it, ELF::STB_WEAK);
822
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000823 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000824 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000825 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000826 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000827 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000828 } else if (RefSymbol.isUndefined()) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000829 if (isSignature && !Used)
830 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
831 else
832 MSD.SectionIndex = ELF::SHN_UNDEF;
Matt Fleming3565a062010-08-16 18:57:57 +0000833 } else {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000834 const MCSectionELF &Section =
835 static_cast<const MCSectionELF&>(RefSymbol.getSection());
836 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000837 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
838 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000839 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000840 }
841
Rafael Espindola88182132010-10-27 15:18:17 +0000842 // The @@@ in symbol version is replaced with @ in undefined symbols and
843 // @@ in defined ones.
844 StringRef Name = Symbol.getName();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000845 SmallString<32> Buf;
846
Rafael Espindola88182132010-10-27 15:18:17 +0000847 size_t Pos = Name.find("@@@");
Rafael Espindola88182132010-10-27 15:18:17 +0000848 if (Pos != StringRef::npos) {
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000849 Buf += Name.substr(0, Pos);
850 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
851 Buf += Name.substr(Pos + Skip);
852 Name = Buf;
Rafael Espindola88182132010-10-27 15:18:17 +0000853 }
854
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000855 uint64_t &Entry = StringIndexMap[Name];
Rafael Espindolaa6866962010-10-27 14:44:52 +0000856 if (!Entry) {
857 Entry = StringTable.size();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000858 StringTable += Name;
Rafael Espindolaa6866962010-10-27 14:44:52 +0000859 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +0000860 }
Rafael Espindolaa6866962010-10-27 14:44:52 +0000861 MSD.StringIndex = Entry;
862 if (MSD.SectionIndex == ELF::SHN_UNDEF)
863 UndefinedSymbolData.push_back(MSD);
864 else if (Local)
865 LocalSymbolData.push_back(MSD);
866 else
867 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +0000868 }
869
870 // Symbols are required to be in lexicographic order.
871 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
872 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
873 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
874
875 // Set the symbol indices. Local symbols must come before all other
876 // symbols with non-local bindings.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000877 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000878 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
879 LocalSymbolData[i].SymbolData->setIndex(Index++);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000880
881 Index += NumRegularSections;
882
Matt Fleming3565a062010-08-16 18:57:57 +0000883 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
884 ExternalSymbolData[i].SymbolData->setIndex(Index++);
885 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
886 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
887}
888
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000889void ELFObjectWriter::WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
890 const MCSectionData &SD) {
Matt Fleming3565a062010-08-16 18:57:57 +0000891 if (!Relocations[&SD].empty()) {
892 MCContext &Ctx = Asm.getContext();
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000893 const MCSectionELF *RelaSection;
Matt Fleming3565a062010-08-16 18:57:57 +0000894 const MCSectionELF &Section =
895 static_cast<const MCSectionELF&>(SD.getSection());
896
897 const StringRef SectionName = Section.getSectionName();
Benjamin Kramer377a5722010-08-17 17:30:07 +0000898 std::string RelaSectionName = HasRelocationAddend ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +0000899 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000900
901 unsigned EntrySize;
902 if (HasRelocationAddend)
903 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
904 else
905 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +0000906
Benjamin Kramer377a5722010-08-17 17:30:07 +0000907 RelaSection = Ctx.getELFSection(RelaSectionName, HasRelocationAddend ?
908 ELF::SHT_RELA : ELF::SHT_REL, 0,
Matt Fleming3565a062010-08-16 18:57:57 +0000909 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000910 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +0000911
912 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Benjamin Kramera9eadca2010-09-06 16:11:52 +0000913 RelaSD.setAlignment(Is64Bit ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +0000914
915 MCDataFragment *F = new MCDataFragment(&RelaSD);
916
917 WriteRelocationsFragment(Asm, F, &SD);
918
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000919 Asm.AddSectionToTheEnd(*this, RelaSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000920 }
921}
922
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000923void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
924 uint64_t Flags, uint64_t Address,
925 uint64_t Offset, uint64_t Size,
926 uint32_t Link, uint32_t Info,
927 uint64_t Alignment,
928 uint64_t EntrySize) {
Matt Fleming3565a062010-08-16 18:57:57 +0000929 Write32(Name); // sh_name: index into string table
930 Write32(Type); // sh_type
931 WriteWord(Flags); // sh_flags
932 WriteWord(Address); // sh_addr
933 WriteWord(Offset); // sh_offset
934 WriteWord(Size); // sh_size
935 Write32(Link); // sh_link
936 Write32(Info); // sh_info
937 WriteWord(Alignment); // sh_addralign
938 WriteWord(EntrySize); // sh_entsize
939}
940
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000941void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
942 MCDataFragment *F,
943 const MCSectionData *SD) {
Matt Fleming3565a062010-08-16 18:57:57 +0000944 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
945 // sort by the r_offset just like gnu as does
946 array_pod_sort(Relocs.begin(), Relocs.end());
947
948 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
949 ELFRelocationEntry entry = Relocs[e - i - 1];
950
Rafael Espindola12203cc2010-11-21 00:48:25 +0000951 if (!entry.Index)
952 ;
953 else if (entry.Index < 0)
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000954 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
955 else
Rafael Espindola12203cc2010-11-21 00:48:25 +0000956 entry.Index += LocalSymbolData.size();
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000957 if (Is64Bit) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000958 String64(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000959
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000960 struct ELF::Elf64_Rela ERE64;
961 ERE64.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000962 String64(*F, ERE64.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000963
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000964 if (HasRelocationAddend)
965 String64(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000966 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000967 String32(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000968
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000969 struct ELF::Elf32_Rela ERE32;
970 ERE32.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000971 String32(*F, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000972
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000973 if (HasRelocationAddend)
974 String32(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000975 }
Matt Fleming3565a062010-08-16 18:57:57 +0000976 }
977}
978
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000979void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
980 MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000981 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000982 MCContext &Ctx = Asm.getContext();
983 MCDataFragment *F;
984
Matt Fleming3565a062010-08-16 18:57:57 +0000985 unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
986
Rafael Espindola38738bf2010-09-22 19:04:41 +0000987 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000988 const MCSectionELF *ShstrtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +0000989 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +0000990 SectionKind::getReadOnly());
Rafael Espindola71859c62010-09-16 19:46:31 +0000991 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
992 ShstrtabSD.setAlignment(1);
993 ShstrtabIndex = Asm.size();
994
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000995 const MCSectionELF *SymtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +0000996 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
997 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000998 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +0000999 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Matt Fleming3565a062010-08-16 18:57:57 +00001000 SymtabSD.setAlignment(Is64Bit ? 8 : 4);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001001 SymbolTableIndex = Asm.size();
1002
1003 MCSectionData *SymtabShndxSD = NULL;
1004
1005 if (NeedsSymtabShndx) {
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001006 const MCSectionELF *SymtabShndxSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001007 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001008 SectionKind::getReadOnly(), 4, "");
Rafael Espindola7be2c332010-10-31 00:16:26 +00001009 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
1010 SymtabShndxSD->setAlignment(4);
1011 }
Matt Fleming3565a062010-08-16 18:57:57 +00001012
Matt Fleming3565a062010-08-16 18:57:57 +00001013 const MCSection *StrtabSection;
1014 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001015 SectionKind::getReadOnly());
Matt Fleming3565a062010-08-16 18:57:57 +00001016 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1017 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +00001018 StringTableIndex = Asm.size();
1019
Rafael Espindolac3c413f2010-09-27 22:04:54 +00001020 WriteRelocations(Asm, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001021
1022 // Symbol table
1023 F = new MCDataFragment(&SymtabSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001024 MCDataFragment *ShndxF = NULL;
1025 if (NeedsSymtabShndx) {
1026 ShndxF = new MCDataFragment(SymtabShndxSD);
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001027 Asm.AddSectionToTheEnd(*this, *SymtabShndxSD, Layout);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001028 }
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001029 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001030 Asm.AddSectionToTheEnd(*this, SymtabSD, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001031
Matt Fleming3565a062010-08-16 18:57:57 +00001032 F = new MCDataFragment(&StrtabSD);
1033 F->getContents().append(StringTable.begin(), StringTable.end());
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001034 Asm.AddSectionToTheEnd(*this, StrtabSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +00001035
Matt Fleming3565a062010-08-16 18:57:57 +00001036 F = new MCDataFragment(&ShstrtabSD);
1037
Matt Fleming3565a062010-08-16 18:57:57 +00001038 // Section header string table.
1039 //
1040 // The first entry of a string table holds a null character so skip
1041 // section 0.
1042 uint64_t Index = 1;
1043 F->getContents() += '\x00';
1044
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001045 StringMap<uint64_t> SecStringMap;
Matt Fleming3565a062010-08-16 18:57:57 +00001046 for (MCAssembler::const_iterator it = Asm.begin(),
1047 ie = Asm.end(); it != ie; ++it) {
Matt Fleming3565a062010-08-16 18:57:57 +00001048 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +00001049 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola51efe7a2010-09-23 14:14:56 +00001050 // FIXME: We could merge suffixes like in .text and .rela.text.
Matt Fleming3565a062010-08-16 18:57:57 +00001051
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001052 StringRef Name = Section.getSectionName();
1053 if (SecStringMap.count(Name)) {
1054 SectionStringTableIndex[&Section] = SecStringMap[Name];
1055 continue;
1056 }
Matt Fleming3565a062010-08-16 18:57:57 +00001057 // Remember the index into the string table so we can write it
1058 // into the sh_name field of the section header table.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001059 SectionStringTableIndex[&Section] = Index;
1060 SecStringMap[Name] = Index;
Matt Fleming3565a062010-08-16 18:57:57 +00001061
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001062 Index += Name.size() + 1;
1063 F->getContents() += Name;
Matt Fleming3565a062010-08-16 18:57:57 +00001064 F->getContents() += '\x00';
1065 }
1066
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001067 Asm.AddSectionToTheEnd(*this, ShstrtabSD, Layout);
Rafael Espindola70703872010-09-30 02:22:20 +00001068}
1069
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001070bool ELFObjectWriter::IsFixupFullyResolved(const MCAssembler &Asm,
1071 const MCValue Target,
1072 bool IsPCRel,
1073 const MCFragment *DF) const {
Rafael Espindola70703872010-09-30 02:22:20 +00001074 // If this is a PCrel relocation, find the section this fixup value is
1075 // relative to.
1076 const MCSection *BaseSection = 0;
1077 if (IsPCRel) {
1078 BaseSection = &DF->getParent()->getSection();
1079 assert(BaseSection);
1080 }
1081
1082 const MCSection *SectionA = 0;
1083 const MCSymbol *SymbolA = 0;
1084 if (const MCSymbolRefExpr *A = Target.getSymA()) {
Rafael Espindola2c920852010-11-16 04:11:46 +00001085 SymbolA = &A->getSymbol();
1086 SectionA = &SymbolA->AliasedSymbol().getSection();
Rafael Espindola70703872010-09-30 02:22:20 +00001087 }
1088
1089 const MCSection *SectionB = 0;
Rafael Espindola12203cc2010-11-21 00:48:25 +00001090 const MCSymbol *SymbolB = 0;
Rafael Espindola70703872010-09-30 02:22:20 +00001091 if (const MCSymbolRefExpr *B = Target.getSymB()) {
Rafael Espindola12203cc2010-11-21 00:48:25 +00001092 SymbolB = &B->getSymbol();
1093 SectionB = &SymbolB->AliasedSymbol().getSection();
Rafael Espindola70703872010-09-30 02:22:20 +00001094 }
1095
1096 if (!BaseSection)
1097 return SectionA == SectionB;
1098
Rafael Espindola12203cc2010-11-21 00:48:25 +00001099 if (SymbolB)
1100 return false;
1101
1102 // Absolute address but PCrel instruction, so we need a relocation.
1103 if (!SymbolA)
1104 return false;
1105
Rafael Espindola2c920852010-11-16 04:11:46 +00001106 // FIXME: This is in here just to match gnu as output. If the two ends
1107 // are in the same section, there is nothing that the linker can do to
1108 // break it.
Rafael Espindola70703872010-09-30 02:22:20 +00001109 const MCSymbolData &DataA = Asm.getSymbolData(*SymbolA);
1110 if (DataA.isExternal())
1111 return false;
1112
Rafael Espindola12203cc2010-11-21 00:48:25 +00001113 return BaseSection == SectionA;
Matt Fleming3565a062010-08-16 18:57:57 +00001114}
1115
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001116void ELFObjectWriter::CreateGroupSections(MCAssembler &Asm,
1117 MCAsmLayout &Layout,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001118 GroupMapTy &GroupMap,
1119 RevGroupMapTy &RevGroupMap) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001120 // Build the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001121 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1122 it != ie; ++it) {
1123 const MCSectionELF &Section =
1124 static_cast<const MCSectionELF&>(it->getSection());
1125 if (!(Section.getFlags() & MCSectionELF::SHF_GROUP))
1126 continue;
1127
1128 const MCSymbol *SignatureSymbol = Section.getGroup();
1129 Asm.getOrCreateSymbolData(*SignatureSymbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001130 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001131 if (!Group) {
1132 Group = Asm.getContext().CreateELFGroupSection();
1133 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1134 Data.setAlignment(4);
1135 MCDataFragment *F = new MCDataFragment(&Data);
1136 String32(*F, ELF::GRP_COMDAT);
1137 }
1138 GroupMap[Group] = SignatureSymbol;
1139 }
1140
1141 // Add sections to the groups
1142 unsigned Index = 1;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001143 unsigned NumGroups = RevGroupMap.size();
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001144 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1145 it != ie; ++it, ++Index) {
1146 const MCSectionELF &Section =
1147 static_cast<const MCSectionELF&>(it->getSection());
1148 if (!(Section.getFlags() & MCSectionELF::SHF_GROUP))
1149 continue;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001150 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001151 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1152 // FIXME: we could use the previous fragment
1153 MCDataFragment *F = new MCDataFragment(&Data);
1154 String32(*F, NumGroups + Index);
1155 }
1156
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001157 for (RevGroupMapTy::const_iterator i = RevGroupMap.begin(),
1158 e = RevGroupMap.end(); i != e; ++i) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001159 const MCSectionELF *Group = i->second;
1160 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001161 Asm.AddSectionToTheEnd(*this, Data, Layout);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001162 }
1163}
1164
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001165void ELFObjectWriter::WriteSection(MCAssembler &Asm,
1166 const SectionIndexMapTy &SectionIndexMap,
1167 uint32_t GroupSymbolIndex,
1168 uint64_t Offset, uint64_t Size,
1169 uint64_t Alignment,
1170 const MCSectionELF &Section) {
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001171 uint64_t sh_link = 0;
1172 uint64_t sh_info = 0;
1173
1174 switch(Section.getType()) {
1175 case ELF::SHT_DYNAMIC:
1176 sh_link = SectionStringTableIndex[&Section];
1177 sh_info = 0;
1178 break;
1179
1180 case ELF::SHT_REL:
1181 case ELF::SHT_RELA: {
1182 const MCSectionELF *SymtabSection;
1183 const MCSectionELF *InfoSection;
1184 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
1185 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001186 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001187 sh_link = SectionIndexMap.lookup(SymtabSection);
1188 assert(sh_link && ".symtab not found");
1189
1190 // Remove ".rel" and ".rela" prefixes.
1191 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1192 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
1193
1194 InfoSection = Asm.getContext().getELFSection(SectionName,
1195 ELF::SHT_PROGBITS, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001196 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001197 sh_info = SectionIndexMap.lookup(InfoSection);
1198 break;
1199 }
1200
1201 case ELF::SHT_SYMTAB:
1202 case ELF::SHT_DYNSYM:
1203 sh_link = StringTableIndex;
1204 sh_info = LastLocalSymbolIndex;
1205 break;
1206
1207 case ELF::SHT_SYMTAB_SHNDX:
1208 sh_link = SymbolTableIndex;
1209 break;
1210
1211 case ELF::SHT_PROGBITS:
1212 case ELF::SHT_STRTAB:
1213 case ELF::SHT_NOBITS:
1214 case ELF::SHT_NULL:
1215 case ELF::SHT_ARM_ATTRIBUTES:
1216 // Nothing to do.
1217 break;
1218
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001219 case ELF::SHT_GROUP: {
1220 sh_link = SymbolTableIndex;
1221 sh_info = GroupSymbolIndex;
1222 break;
1223 }
1224
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001225 default:
1226 assert(0 && "FIXME: sh_type value not supported!");
1227 break;
1228 }
1229
1230 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1231 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1232 Alignment, Section.getEntrySize());
1233}
1234
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001235void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1236 const MCAsmLayout &Layout) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001237 GroupMapTy GroupMap;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001238 RevGroupMapTy RevGroupMap;
1239 CreateGroupSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1240 RevGroupMap);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001241
Rafael Espindolabab2a802010-11-10 21:51:05 +00001242 SectionIndexMapTy SectionIndexMap;
1243
1244 ComputeIndexMap(Asm, SectionIndexMap);
1245
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001246 // Compute symbol table information.
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001247 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap);
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001248
Matt Fleming3565a062010-08-16 18:57:57 +00001249 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001250 const_cast<MCAsmLayout&>(Layout),
1251 SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +00001252
Rafael Espindola1d739a02010-11-10 22:34:07 +00001253 // Update to include the metadata sections.
1254 ComputeIndexMap(Asm, SectionIndexMap);
1255
Matt Fleming3565a062010-08-16 18:57:57 +00001256 // Add 1 for the null section.
1257 unsigned NumSections = Asm.size() + 1;
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001258 uint64_t NaturalAlignment = Is64Bit ? 8 : 4;
1259 uint64_t HeaderSize = Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr);
1260 uint64_t FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001261
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001262 std::vector<const MCSectionELF*> Sections;
1263 Sections.resize(NumSections);
1264
1265 for (SectionIndexMapTy::const_iterator i=
1266 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1267 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
1268 Sections[p.second] = p.first;
1269 }
1270
1271 for (unsigned i = 1; i < NumSections; ++i) {
1272 const MCSectionELF &Section = *Sections[i];
1273 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001274
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001275 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1276
Matt Fleming3565a062010-08-16 18:57:57 +00001277 // Get the size of the section in the output file (including padding).
1278 uint64_t Size = Layout.getSectionFileSize(&SD);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001279
1280 FileOff += Size;
Matt Fleming3565a062010-08-16 18:57:57 +00001281 }
1282
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001283 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1284
Matt Fleming3565a062010-08-16 18:57:57 +00001285 // Write out the ELF header ...
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001286 WriteHeader(FileOff - HeaderSize, NumSections);
1287
1288 FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001289
1290 // ... then all of the sections ...
1291 DenseMap<const MCSection*, uint64_t> SectionOffsetMap;
1292
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001293 for (unsigned i = 1; i < NumSections; ++i) {
1294 const MCSectionELF &Section = *Sections[i];
1295 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001296
1297 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1298 WriteZeros(Padding);
1299 FileOff += Padding;
1300
Matt Fleming3565a062010-08-16 18:57:57 +00001301 // Remember the offset into the file for this section.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001302 SectionOffsetMap[&Section] = FileOff;
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001303
Matt Fleming3565a062010-08-16 18:57:57 +00001304 FileOff += Layout.getSectionFileSize(&SD);
1305
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001306 Asm.WriteSectionData(&SD, Layout, this);
Matt Fleming3565a062010-08-16 18:57:57 +00001307 }
1308
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001309 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1310 WriteZeros(Padding);
1311 FileOff += Padding;
1312
Matt Fleming3565a062010-08-16 18:57:57 +00001313 // ... and then the section header table.
1314 // Should we align the section header table?
1315 //
1316 // Null section first.
Rafael Espindola7be2c332010-10-31 00:16:26 +00001317 uint64_t FirstSectionSize =
1318 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1319 uint32_t FirstSectionLink =
1320 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1321 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming3565a062010-08-16 18:57:57 +00001322
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001323 for (unsigned i = 1; i < NumSections; ++i) {
1324 const MCSectionELF &Section = *Sections[i];
1325 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1326 uint32_t GroupSymbolIndex;
1327 if (Section.getType() != ELF::SHT_GROUP)
1328 GroupSymbolIndex = 0;
1329 else
1330 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm, GroupMap[&Section]);
Matt Fleming3565a062010-08-16 18:57:57 +00001331
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001332 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
1333 SectionOffsetMap[&Section], Layout.getSectionSize(&SD),
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001334 SD.getAlignment(), Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001335 }
1336}
1337
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001338MCObjectWriter *llvm::createELFObjectWriter(raw_ostream &OS,
1339 bool Is64Bit,
1340 Triple::OSType OSType,
1341 uint16_t EMachine,
1342 bool IsLittleEndian,
1343 bool HasRelocationAddend) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001344 switch (EMachine) {
1345 case ELF::EM_386:
1346 case ELF::EM_X86_64:
1347 return new X86ELFObjectWriter(OS, Is64Bit, IsLittleEndian, EMachine,
1348 HasRelocationAddend, OSType); break;
1349 case ELF::EM_ARM:
1350 return new ARMELFObjectWriter(OS, Is64Bit, IsLittleEndian, EMachine,
1351 HasRelocationAddend, OSType); break;
Benjamin Kramer32858772010-11-15 19:20:50 +00001352 default: llvm_unreachable("Unsupported architecture"); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001353 }
1354}
1355
1356
1357/// START OF SUBCLASSES for ELFObjectWriter
1358//===- ARMELFObjectWriter -------------------------------------------===//
1359
1360ARMELFObjectWriter::ARMELFObjectWriter(raw_ostream &_OS, bool _Is64Bit,
1361 bool _IsLittleEndian,
1362 uint16_t _EMachine, bool _HasRelocationAddend,
1363 Triple::OSType _OSType)
1364 : ELFObjectWriter(_OS, _Is64Bit, _IsLittleEndian, _EMachine,
1365 _HasRelocationAddend, _OSType)
1366{}
1367
1368ARMELFObjectWriter::~ARMELFObjectWriter()
1369{}
1370
1371void ARMELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
1372 const MCAsmLayout &Layout,
1373 const MCFragment *Fragment,
1374 const MCFixup &Fixup,
1375 MCValue Target,
1376 uint64_t &FixedValue) {
1377 assert(0 && "ARMELFObjectWriter::RecordRelocation() unimplemented");
1378}
1379
1380
1381
1382//===- X86ELFObjectWriter -------------------------------------------===//
1383
1384
1385X86ELFObjectWriter::X86ELFObjectWriter(raw_ostream &_OS, bool _Is64Bit,
1386 bool _IsLittleEndian,
1387 uint16_t _EMachine, bool _HasRelocationAddend,
1388 Triple::OSType _OSType)
1389 : ELFObjectWriter(_OS, _Is64Bit, _IsLittleEndian, _EMachine,
1390 _HasRelocationAddend, _OSType)
1391{}
1392
1393X86ELFObjectWriter::~X86ELFObjectWriter()
1394{}
1395
1396void X86ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
1397 const MCAsmLayout &Layout,
1398 const MCFragment *Fragment,
1399 const MCFixup &Fixup,
1400 MCValue Target,
1401 uint64_t &FixedValue) {
1402 int64_t Addend = 0;
1403 int Index = 0;
1404 int64_t Value = Target.getConstant();
Rafael Espindola12203cc2010-11-21 00:48:25 +00001405 const MCSymbol *RelocSymbol = NULL;
Jason W Kimd3443e92010-11-15 16:18:39 +00001406
1407 bool IsPCRel = isFixupKindX86PCRel(Fixup.getKind());
1408 if (!Target.isAbsolute()) {
Rafael Espindola12203cc2010-11-21 00:48:25 +00001409 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
1410 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
1411 RelocSymbol = SymbolToReloc(Asm, Target, *Fragment);
1412
Jason W Kimd3443e92010-11-15 16:18:39 +00001413 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
1414 const MCSymbol &SymbolB = RefB->getSymbol();
1415 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
1416 IsPCRel = true;
1417 MCSectionData *Sec = Fragment->getParent();
1418
1419 // Offset of the symbol in the section
1420 int64_t a = Layout.getSymbolAddress(&SDB) - Layout.getSectionAddress(Sec);
1421
1422 // Ofeset of the relocation in the section
1423 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
1424 Value += b - a;
1425 }
1426
1427 if (!RelocSymbol) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +00001428 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
Jason W Kimd3443e92010-11-15 16:18:39 +00001429 MCFragment *F = SD.getFragment();
1430
Rafael Espindola12203cc2010-11-21 00:48:25 +00001431 Index = F->getParent()->getOrdinal() + 1;
Jason W Kimd3443e92010-11-15 16:18:39 +00001432
1433 MCSectionData *FSD = F->getParent();
1434 // Offset of the symbol in the section
1435 Value += Layout.getSymbolAddress(&SD) - Layout.getSectionAddress(FSD);
1436 } else {
1437 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
1438 WeakrefUsedInReloc.insert(RelocSymbol);
1439 else
1440 UsedInReloc.insert(RelocSymbol);
1441 Index = -1;
1442 }
1443 Addend = Value;
1444 // Compensate for the addend on i386.
1445 if (Is64Bit)
1446 Value = 0;
1447 }
1448
1449 FixedValue = Value;
1450
1451 // determine the type of the relocation
1452
Rafael Espindola12203cc2010-11-21 00:48:25 +00001453 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1454 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
Jason W Kimd3443e92010-11-15 16:18:39 +00001455 unsigned Type;
1456 if (Is64Bit) {
1457 if (IsPCRel) {
1458 switch (Modifier) {
1459 default:
1460 llvm_unreachable("Unimplemented");
1461 case MCSymbolRefExpr::VK_None:
1462 Type = ELF::R_X86_64_PC32;
1463 break;
1464 case MCSymbolRefExpr::VK_PLT:
1465 Type = ELF::R_X86_64_PLT32;
1466 break;
1467 case MCSymbolRefExpr::VK_GOTPCREL:
1468 Type = ELF::R_X86_64_GOTPCREL;
1469 break;
1470 case MCSymbolRefExpr::VK_GOTTPOFF:
1471 Type = ELF::R_X86_64_GOTTPOFF;
1472 break;
1473 case MCSymbolRefExpr::VK_TLSGD:
1474 Type = ELF::R_X86_64_TLSGD;
1475 break;
1476 case MCSymbolRefExpr::VK_TLSLD:
1477 Type = ELF::R_X86_64_TLSLD;
1478 break;
1479 }
1480 } else {
1481 switch ((unsigned)Fixup.getKind()) {
1482 default: llvm_unreachable("invalid fixup kind!");
1483 case FK_Data_8: Type = ELF::R_X86_64_64; break;
1484 case X86::reloc_signed_4byte:
1485 case X86::reloc_pcrel_4byte:
1486 assert(isInt<32>(Target.getConstant()));
1487 switch (Modifier) {
1488 default:
1489 llvm_unreachable("Unimplemented");
1490 case MCSymbolRefExpr::VK_None:
1491 Type = ELF::R_X86_64_32S;
1492 break;
1493 case MCSymbolRefExpr::VK_GOT:
1494 Type = ELF::R_X86_64_GOT32;
1495 break;
1496 case MCSymbolRefExpr::VK_GOTPCREL:
1497 Type = ELF::R_X86_64_GOTPCREL;
1498 break;
1499 case MCSymbolRefExpr::VK_TPOFF:
1500 Type = ELF::R_X86_64_TPOFF32;
1501 break;
1502 case MCSymbolRefExpr::VK_DTPOFF:
1503 Type = ELF::R_X86_64_DTPOFF32;
1504 break;
1505 }
1506 break;
1507 case FK_Data_4:
1508 Type = ELF::R_X86_64_32;
1509 break;
1510 case FK_Data_2: Type = ELF::R_X86_64_16; break;
1511 case X86::reloc_pcrel_1byte:
1512 case FK_Data_1: Type = ELF::R_X86_64_8; break;
1513 }
1514 }
1515 } else {
1516 if (IsPCRel) {
1517 switch (Modifier) {
1518 default:
1519 llvm_unreachable("Unimplemented");
1520 case MCSymbolRefExpr::VK_None:
1521 Type = ELF::R_386_PC32;
1522 break;
1523 case MCSymbolRefExpr::VK_PLT:
1524 Type = ELF::R_386_PLT32;
1525 break;
1526 }
1527 } else {
1528 switch ((unsigned)Fixup.getKind()) {
1529 default: llvm_unreachable("invalid fixup kind!");
1530
1531 case X86::reloc_global_offset_table:
1532 Type = ELF::R_386_GOTPC;
1533 break;
1534
1535 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
1536 // instead?
1537 case X86::reloc_signed_4byte:
1538 case X86::reloc_pcrel_4byte:
1539 case FK_Data_4:
1540 switch (Modifier) {
1541 default:
1542 llvm_unreachable("Unimplemented");
1543 case MCSymbolRefExpr::VK_None:
1544 Type = ELF::R_386_32;
1545 break;
1546 case MCSymbolRefExpr::VK_GOT:
1547 Type = ELF::R_386_GOT32;
1548 break;
1549 case MCSymbolRefExpr::VK_GOTOFF:
1550 Type = ELF::R_386_GOTOFF;
1551 break;
1552 case MCSymbolRefExpr::VK_TLSGD:
1553 Type = ELF::R_386_TLS_GD;
1554 break;
1555 case MCSymbolRefExpr::VK_TPOFF:
1556 Type = ELF::R_386_TLS_LE_32;
1557 break;
1558 case MCSymbolRefExpr::VK_INDNTPOFF:
1559 Type = ELF::R_386_TLS_IE;
1560 break;
1561 case MCSymbolRefExpr::VK_NTPOFF:
1562 Type = ELF::R_386_TLS_LE;
1563 break;
1564 case MCSymbolRefExpr::VK_GOTNTPOFF:
1565 Type = ELF::R_386_TLS_GOTIE;
1566 break;
1567 case MCSymbolRefExpr::VK_TLSLDM:
1568 Type = ELF::R_386_TLS_LDM;
1569 break;
1570 case MCSymbolRefExpr::VK_DTPOFF:
1571 Type = ELF::R_386_TLS_LDO_32;
1572 break;
1573 }
1574 break;
1575 case FK_Data_2: Type = ELF::R_386_16; break;
1576 case X86::reloc_pcrel_1byte:
1577 case FK_Data_1: Type = ELF::R_386_8; break;
1578 }
1579 }
1580 }
1581
1582 if (RelocNeedsGOT(Modifier))
1583 NeedsGOT = true;
1584
1585 ELFRelocationEntry ERE;
1586
1587 ERE.Index = Index;
1588 ERE.Type = Type;
1589 ERE.Symbol = RelocSymbol;
1590
1591 ERE.r_offset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
1592
1593 if (HasRelocationAddend)
1594 ERE.r_addend = Addend;
1595 else
1596 ERE.r_addend = 0; // Silence compiler warning.
1597
1598 Relocations[Fragment->getParent()].push_back(ERE);
Matt Fleming3565a062010-08-16 18:57:57 +00001599}