blob: 6150f9fcb9549f58e0ea799ed71d548a2d45eb23 [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;
Jason W Kimccbe0002010-11-22 18:47:05 +0000402 case ARM::fixup_arm_pcrel_12:
403 case ARM::fixup_arm_vfp_pcrel_12:
404 case ARM::fixup_arm_branch:
Jason W Kim4a511f02010-11-22 18:41:13 +0000405 return true;
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;
Jason W Kimccbe0002010-11-22 18:47:05 +0000429 case MBlaze::reloc_pcrel_2byte:
430 case MBlaze::reloc_pcrel_4byte:
Jason W Kim4a511f02010-11-22 18:41:13 +0000431 return true;
432 }
433 }
Wesley Peck4b047132010-11-21 22:06:28 +0000434 };
Matt Fleming3565a062010-08-16 18:57:57 +0000435}
436
Jason W Kimd3443e92010-11-15 16:18:39 +0000437ELFObjectWriter::~ELFObjectWriter()
438{}
439
Matt Fleming3565a062010-08-16 18:57:57 +0000440// Emit the ELF header.
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000441void ELFObjectWriter::WriteHeader(uint64_t SectionDataSize,
442 unsigned NumberOfSections) {
Matt Fleming3565a062010-08-16 18:57:57 +0000443 // ELF Header
444 // ----------
445 //
446 // Note
447 // ----
448 // emitWord method behaves differently for ELF32 and ELF64, writing
449 // 4 bytes in the former and 8 in the latter.
450
451 Write8(0x7f); // e_ident[EI_MAG0]
452 Write8('E'); // e_ident[EI_MAG1]
453 Write8('L'); // e_ident[EI_MAG2]
454 Write8('F'); // e_ident[EI_MAG3]
455
456 Write8(Is64Bit ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
457
458 // e_ident[EI_DATA]
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000459 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
Matt Fleming3565a062010-08-16 18:57:57 +0000460
461 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky5baf79e2010-09-09 17:57:50 +0000462 // e_ident[EI_OSABI]
463 switch (OSType) {
464 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
465 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
466 default: Write8(ELF::ELFOSABI_NONE); break;
467 }
Matt Fleming3565a062010-08-16 18:57:57 +0000468 Write8(0); // e_ident[EI_ABIVERSION]
469
470 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
471
472 Write16(ELF::ET_REL); // e_type
473
Wesley Peckeecb8582010-10-22 15:52:49 +0000474 Write16(EMachine); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +0000475
476 Write32(ELF::EV_CURRENT); // e_version
477 WriteWord(0); // e_entry, no entry point in .o file
478 WriteWord(0); // e_phoff, no program header for .o
Benjamin Kramereb976772010-08-17 17:02:29 +0000479 WriteWord(SectionDataSize + (Is64Bit ? sizeof(ELF::Elf64_Ehdr) :
480 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000481
482 // FIXME: Make this configurable.
483 Write32(0); // e_flags = whatever the target wants
484
485 // e_ehsize = ELF header size
486 Write16(Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
487
488 Write16(0); // e_phentsize = prog header entry size
489 Write16(0); // e_phnum = # prog header entries = 0
490
491 // e_shentsize = Section header entry size
492 Write16(Is64Bit ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
493
494 // e_shnum = # of section header ents
Rafael Espindola7be2c332010-10-31 00:16:26 +0000495 if (NumberOfSections >= ELF::SHN_LORESERVE)
496 Write16(0);
497 else
498 Write16(NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000499
500 // e_shstrndx = Section # of '.shstrtab'
Rafael Espindola7be2c332010-10-31 00:16:26 +0000501 if (NumberOfSections >= ELF::SHN_LORESERVE)
502 Write16(ELF::SHN_XINDEX);
503 else
504 Write16(ShstrtabIndex);
Matt Fleming3565a062010-08-16 18:57:57 +0000505}
506
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000507void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
508 MCDataFragment *ShndxF,
509 uint64_t name,
510 uint8_t info, uint64_t value,
511 uint64_t size, uint8_t other,
512 uint32_t shndx,
513 bool Reserved) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000514 if (ShndxF) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000515 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000516 String32(*ShndxF, shndx);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000517 else
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000518 String32(*ShndxF, 0);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000519 }
520
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000521 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
522 uint16_t(ELF::SHN_XINDEX) : shndx;
523
Matt Fleming3565a062010-08-16 18:57:57 +0000524 if (Is64Bit) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000525 String32(*SymtabF, name); // st_name
526 String8(*SymtabF, info); // st_info
527 String8(*SymtabF, other); // st_other
528 String16(*SymtabF, Index); // st_shndx
529 String64(*SymtabF, value); // st_value
530 String64(*SymtabF, size); // st_size
Matt Fleming3565a062010-08-16 18:57:57 +0000531 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000532 String32(*SymtabF, name); // st_name
533 String32(*SymtabF, value); // st_value
534 String32(*SymtabF, size); // st_size
535 String8(*SymtabF, info); // st_info
536 String8(*SymtabF, other); // st_other
537 String16(*SymtabF, Index); // st_shndx
Matt Fleming3565a062010-08-16 18:57:57 +0000538 }
539}
540
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000541static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout) {
542 if (Data.isCommon() && Data.isExternal())
543 return Data.getCommonAlignment();
544
545 const MCSymbol &Symbol = Data.getSymbol();
546 if (!Symbol.isInSection())
547 return 0;
548
Rafael Espindola1973d432010-10-28 19:39:57 +0000549 if (MCFragment *FF = Data.getFragment())
550 return Layout.getSymbolAddress(&Data) -
551 Layout.getSectionAddress(FF->getParent());
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000552
553 return 0;
554}
555
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000556void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm) {
Rafael Espindola88182132010-10-27 15:18:17 +0000557 // The presence of symbol versions causes undefined symbols and
558 // versions declared with @@@ to be renamed.
559
560 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
561 ie = Asm.symbol_end(); it != ie; ++it) {
562 const MCSymbol &Alias = it->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000563 const MCSymbol &Symbol = Alias.AliasedSymbol();
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000564 MCSymbolData &SD = Asm.getSymbolData(Symbol);
565
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000566 // Not an alias.
567 if (&Symbol == &Alias)
568 continue;
569
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000570 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000571 size_t Pos = AliasName.find('@');
572 if (Pos == StringRef::npos)
573 continue;
574
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000575 // Aliases defined with .symvar copy the binding from the symbol they alias.
576 // This is the first place we are able to copy this information.
577 it->setExternal(SD.isExternal());
578 SetBinding(*it, GetBinding(SD));
579
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000580 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000581 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
582 continue;
583
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000584 // FIXME: produce a better error message.
585 if (Symbol.isUndefined() && Rest.startswith("@@") &&
586 !Rest.startswith("@@@"))
587 report_fatal_error("A @@ version cannot be undefined");
588
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000589 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000590 }
591}
592
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000593void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
594 MCDataFragment *ShndxF,
595 ELFSymbolData &MSD,
596 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000597 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000598 MCSymbolData &Data =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000599 Layout.getAssembler().getSymbolData(OrigData.getSymbol().AliasedSymbol());
Rafael Espindola152c1062010-10-06 21:02:29 +0000600
Rafael Espindola7be2c332010-10-31 00:16:26 +0000601 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
602 Data.getSymbol().isVariable();
603
Rafael Espindola152c1062010-10-06 21:02:29 +0000604 uint8_t Binding = GetBinding(OrigData);
605 uint8_t Visibility = GetVisibility(OrigData);
606 uint8_t Type = GetType(Data);
607
608 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
609 uint8_t Other = Visibility;
610
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000611 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000612 uint64_t Size = 0;
613 const MCExpr *ESize;
614
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000615 assert(!(Data.isCommon() && !Data.isExternal()));
616
Matt Fleming3565a062010-08-16 18:57:57 +0000617 ESize = Data.getSize();
618 if (Data.getSize()) {
619 MCValue Res;
620 if (ESize->getKind() == MCExpr::Binary) {
621 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(ESize);
622
623 if (BE->EvaluateAsRelocatable(Res, &Layout)) {
Benjamin Kramer24f12062010-10-17 07:38:40 +0000624 assert(!Res.getSymA() || !Res.getSymA()->getSymbol().isDefined());
625 assert(!Res.getSymB() || !Res.getSymB()->getSymbol().isDefined());
Rafael Espindolaf230df92010-10-16 18:23:53 +0000626 Size = Res.getConstant();
Matt Fleming3565a062010-08-16 18:57:57 +0000627 }
628 } else if (ESize->getKind() == MCExpr::Constant) {
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000629 Size = static_cast<const MCConstantExpr *>(ESize)->getValue();
Matt Fleming3565a062010-08-16 18:57:57 +0000630 } else {
631 assert(0 && "Unsupported size expression");
632 }
633 }
634
635 // Write out the symbol table entry
Rafael Espindola7be2c332010-10-31 00:16:26 +0000636 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
637 Size, Other, MSD.SectionIndex, IsReserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000638}
639
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000640void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
641 MCDataFragment *ShndxF,
642 const MCAssembler &Asm,
643 const MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000644 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000645 // The string table must be emitted first because we need the index
646 // into the string table for all the symbol names.
647 assert(StringTable.size() && "Missing string table");
648
649 // FIXME: Make sure the start of the symbol table is aligned.
650
651 // The first entry is the undefined symbol entry.
Rafael Espindola7be2c332010-10-31 00:16:26 +0000652 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
Matt Fleming3565a062010-08-16 18:57:57 +0000653
654 // Write the symbol table entries.
655 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
656 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
657 ELFSymbolData &MSD = LocalSymbolData[i];
Rafael Espindola7be2c332010-10-31 00:16:26 +0000658 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000659 }
660
Rafael Espindola71859c62010-09-16 19:46:31 +0000661 // Write out a symbol table entry for each regular section.
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000662 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
663 ++i) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000664 const MCSectionELF &Section =
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000665 static_cast<const MCSectionELF&>(i->getSection());
666 if (Section.getType() == ELF::SHT_RELA ||
667 Section.getType() == ELF::SHT_REL ||
668 Section.getType() == ELF::SHT_STRTAB ||
669 Section.getType() == ELF::SHT_SYMTAB)
Eli Friedmana44fa242010-08-16 21:17:09 +0000670 continue;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000671 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000672 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section), false);
Eli Friedmana44fa242010-08-16 21:17:09 +0000673 LastLocalSymbolIndex++;
674 }
Matt Fleming3565a062010-08-16 18:57:57 +0000675
676 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
677 ELFSymbolData &MSD = ExternalSymbolData[i];
678 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000679 assert(((Data.getFlags() & ELF_STB_Global) ||
680 (Data.getFlags() & ELF_STB_Weak)) &&
681 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000682 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000683 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000684 LastLocalSymbolIndex++;
685 }
686
687 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
688 ELFSymbolData &MSD = UndefinedSymbolData[i];
689 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000690 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000691 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000692 LastLocalSymbolIndex++;
693 }
694}
695
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000696const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm,
697 const MCValue &Target,
698 const MCFragment &F) const {
699 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000700 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
701 const MCSymbol *Renamed = Renames.lookup(&Symbol);
702 const MCSymbolData &SD = Asm.getSymbolData(Symbol);
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000703
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000704 if (ASymbol.isUndefined()) {
705 if (Renamed)
706 return Renamed;
707 return &ASymbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000708 }
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000709
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000710 if (SD.isExternal()) {
711 if (Renamed)
712 return Renamed;
713 return &Symbol;
714 }
Rafael Espindola73ffea42010-09-25 05:42:19 +0000715
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000716 const MCSectionELF &Section =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000717 static_cast<const MCSectionELF&>(ASymbol.getSection());
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000718
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000719 if (Section.getKind().isBSS())
720 return NULL;
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000721
Rafael Espindola8cecf252010-10-06 16:23:36 +0000722 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000723 const MCSectionELF &Sec2 =
724 static_cast<const MCSectionELF&>(F.getParent()->getSection());
725
Rafael Espindola8cecf252010-10-06 16:23:36 +0000726 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000727 (Kind == MCSymbolRefExpr::VK_PLT ||
728 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000729 Kind == MCSymbolRefExpr::VK_GOTOFF)) {
730 if (Renamed)
731 return Renamed;
732 return &Symbol;
733 }
Rafael Espindola3729d002010-10-05 23:57:26 +0000734
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000735 if (Section.getFlags() & MCSectionELF::SHF_MERGE) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000736 if (Target.getConstant() == 0)
737 return NULL;
738 if (Renamed)
739 return Renamed;
740 return &Symbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000741 }
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000742
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000743 return NULL;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000744}
745
Matt Fleming3565a062010-08-16 18:57:57 +0000746
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000747uint64_t
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000748ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
749 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000750 MCSymbolData &SD = Asm.getSymbolData(*S);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000751 return SD.getIndex();
Matt Fleming3565a062010-08-16 18:57:57 +0000752}
753
Rafael Espindola737cd212010-10-05 18:01:23 +0000754static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
Rafael Espindola88182132010-10-27 15:18:17 +0000755 bool Used, bool Renamed) {
Rafael Espindola484291c2010-11-01 14:28:48 +0000756 if (Data.getFlags() & ELF_Other_Weakref)
757 return false;
758
Rafael Espindolabd701182010-10-19 19:31:37 +0000759 if (Used)
760 return true;
761
Rafael Espindola88182132010-10-27 15:18:17 +0000762 if (Renamed)
763 return false;
764
Rafael Espindola737cd212010-10-05 18:01:23 +0000765 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000766
Rafael Espindolad1798862010-10-29 23:09:31 +0000767 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
768 return true;
769
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000770 const MCSymbol &A = Symbol.AliasedSymbol();
Rafael Espindolad1798862010-10-29 23:09:31 +0000771 if (!A.isVariable() && A.isUndefined() && !Data.isCommon())
Rafael Espindolaa6866962010-10-27 14:44:52 +0000772 return false;
773
Rafael Espindola737cd212010-10-05 18:01:23 +0000774 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
775 return false;
776
Rafael Espindolabd701182010-10-19 19:31:37 +0000777 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000778 return false;
779
780 return true;
781}
782
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000783static bool isLocal(const MCSymbolData &Data, bool isSignature,
784 bool isUsedInReloc) {
Rafael Espindola737cd212010-10-05 18:01:23 +0000785 if (Data.isExternal())
786 return false;
787
788 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000789 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000790
791 if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
792 if (isSignature && !isUsedInReloc)
793 return true;
794
Rafael Espindola737cd212010-10-05 18:01:23 +0000795 return false;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000796 }
Rafael Espindola737cd212010-10-05 18:01:23 +0000797
798 return true;
799}
800
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000801void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
802 SectionIndexMapTy &SectionIndexMap) {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000803 unsigned Index = 1;
804 for (MCAssembler::iterator it = Asm.begin(),
805 ie = Asm.end(); it != ie; ++it) {
806 const MCSectionELF &Section =
807 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000808 if (Section.getType() != ELF::SHT_GROUP)
809 continue;
810 SectionIndexMap[&Section] = Index++;
811 }
812
813 for (MCAssembler::iterator it = Asm.begin(),
814 ie = Asm.end(); it != ie; ++it) {
815 const MCSectionELF &Section =
816 static_cast<const MCSectionELF &>(it->getSection());
817 if (Section.getType() == ELF::SHT_GROUP)
818 continue;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000819 SectionIndexMap[&Section] = Index++;
820 }
821}
822
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000823void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000824 const SectionIndexMapTy &SectionIndexMap,
825 RevGroupMapTy RevGroupMap) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000826 // FIXME: Is this the correct place to do this?
827 if (NeedsGOT) {
828 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
829 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
830 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
831 Data.setExternal(true);
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000832 SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindola5c77c162010-10-05 15:48:37 +0000833 }
834
Matt Fleming3565a062010-08-16 18:57:57 +0000835 // Build section lookup table.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000836 int NumRegularSections = Asm.size();
Matt Fleming3565a062010-08-16 18:57:57 +0000837
838 // Index 0 is always the empty string.
839 StringMap<uint64_t> StringIndexMap;
840 StringTable += '\x00';
841
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000842 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000843 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
844 ie = Asm.symbol_end(); it != ie; ++it) {
845 const MCSymbol &Symbol = it->getSymbol();
846
Rafael Espindola484291c2010-11-01 14:28:48 +0000847 bool Used = UsedInReloc.count(&Symbol);
848 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000849 bool isSignature = RevGroupMap.count(&Symbol);
850
851 if (!isInSymtab(Asm, *it,
852 Used || WeakrefUsed || isSignature,
Rafael Espindola88182132010-10-27 15:18:17 +0000853 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000854 continue;
855
Matt Fleming3565a062010-08-16 18:57:57 +0000856 ELFSymbolData MSD;
857 MSD.SymbolData = it;
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000858 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Matt Fleming3565a062010-08-16 18:57:57 +0000859
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000860 // Undefined symbols are global, but this is the first place we
861 // are able to set it.
862 bool Local = isLocal(*it, isSignature, Used);
863 if (!Local && GetBinding(*it) == ELF::STB_LOCAL) {
864 MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
865 SetBinding(*it, ELF::STB_GLOBAL);
866 SetBinding(SD, ELF::STB_GLOBAL);
867 }
868
Rafael Espindola484291c2010-11-01 14:28:48 +0000869 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
870 SetBinding(*it, ELF::STB_WEAK);
871
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000872 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000873 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000874 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000875 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000876 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000877 } else if (RefSymbol.isUndefined()) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000878 if (isSignature && !Used)
879 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
880 else
881 MSD.SectionIndex = ELF::SHN_UNDEF;
Matt Fleming3565a062010-08-16 18:57:57 +0000882 } else {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000883 const MCSectionELF &Section =
884 static_cast<const MCSectionELF&>(RefSymbol.getSection());
885 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000886 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
887 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000888 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000889 }
890
Rafael Espindola88182132010-10-27 15:18:17 +0000891 // The @@@ in symbol version is replaced with @ in undefined symbols and
892 // @@ in defined ones.
893 StringRef Name = Symbol.getName();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000894 SmallString<32> Buf;
895
Rafael Espindola88182132010-10-27 15:18:17 +0000896 size_t Pos = Name.find("@@@");
Rafael Espindola88182132010-10-27 15:18:17 +0000897 if (Pos != StringRef::npos) {
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000898 Buf += Name.substr(0, Pos);
899 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
900 Buf += Name.substr(Pos + Skip);
901 Name = Buf;
Rafael Espindola88182132010-10-27 15:18:17 +0000902 }
903
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000904 uint64_t &Entry = StringIndexMap[Name];
Rafael Espindolaa6866962010-10-27 14:44:52 +0000905 if (!Entry) {
906 Entry = StringTable.size();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000907 StringTable += Name;
Rafael Espindolaa6866962010-10-27 14:44:52 +0000908 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +0000909 }
Rafael Espindolaa6866962010-10-27 14:44:52 +0000910 MSD.StringIndex = Entry;
911 if (MSD.SectionIndex == ELF::SHN_UNDEF)
912 UndefinedSymbolData.push_back(MSD);
913 else if (Local)
914 LocalSymbolData.push_back(MSD);
915 else
916 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +0000917 }
918
919 // Symbols are required to be in lexicographic order.
920 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
921 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
922 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
923
924 // Set the symbol indices. Local symbols must come before all other
925 // symbols with non-local bindings.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000926 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000927 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
928 LocalSymbolData[i].SymbolData->setIndex(Index++);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000929
930 Index += NumRegularSections;
931
Matt Fleming3565a062010-08-16 18:57:57 +0000932 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
933 ExternalSymbolData[i].SymbolData->setIndex(Index++);
934 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
935 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
936}
937
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000938void ELFObjectWriter::WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
939 const MCSectionData &SD) {
Matt Fleming3565a062010-08-16 18:57:57 +0000940 if (!Relocations[&SD].empty()) {
941 MCContext &Ctx = Asm.getContext();
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000942 const MCSectionELF *RelaSection;
Matt Fleming3565a062010-08-16 18:57:57 +0000943 const MCSectionELF &Section =
944 static_cast<const MCSectionELF&>(SD.getSection());
945
946 const StringRef SectionName = Section.getSectionName();
Benjamin Kramer377a5722010-08-17 17:30:07 +0000947 std::string RelaSectionName = HasRelocationAddend ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +0000948 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000949
950 unsigned EntrySize;
951 if (HasRelocationAddend)
952 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
953 else
954 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +0000955
Benjamin Kramer377a5722010-08-17 17:30:07 +0000956 RelaSection = Ctx.getELFSection(RelaSectionName, HasRelocationAddend ?
957 ELF::SHT_RELA : ELF::SHT_REL, 0,
Matt Fleming3565a062010-08-16 18:57:57 +0000958 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000959 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +0000960
961 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Benjamin Kramera9eadca2010-09-06 16:11:52 +0000962 RelaSD.setAlignment(Is64Bit ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +0000963
964 MCDataFragment *F = new MCDataFragment(&RelaSD);
965
966 WriteRelocationsFragment(Asm, F, &SD);
967
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000968 Asm.AddSectionToTheEnd(*this, RelaSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000969 }
970}
971
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000972void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
973 uint64_t Flags, uint64_t Address,
974 uint64_t Offset, uint64_t Size,
975 uint32_t Link, uint32_t Info,
976 uint64_t Alignment,
977 uint64_t EntrySize) {
Matt Fleming3565a062010-08-16 18:57:57 +0000978 Write32(Name); // sh_name: index into string table
979 Write32(Type); // sh_type
980 WriteWord(Flags); // sh_flags
981 WriteWord(Address); // sh_addr
982 WriteWord(Offset); // sh_offset
983 WriteWord(Size); // sh_size
984 Write32(Link); // sh_link
985 Write32(Info); // sh_info
986 WriteWord(Alignment); // sh_addralign
987 WriteWord(EntrySize); // sh_entsize
988}
989
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000990void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
991 MCDataFragment *F,
992 const MCSectionData *SD) {
Matt Fleming3565a062010-08-16 18:57:57 +0000993 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
994 // sort by the r_offset just like gnu as does
995 array_pod_sort(Relocs.begin(), Relocs.end());
996
997 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
998 ELFRelocationEntry entry = Relocs[e - i - 1];
999
Rafael Espindola12203cc2010-11-21 00:48:25 +00001000 if (!entry.Index)
1001 ;
1002 else if (entry.Index < 0)
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001003 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
1004 else
Rafael Espindola12203cc2010-11-21 00:48:25 +00001005 entry.Index += LocalSymbolData.size();
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001006 if (Is64Bit) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001007 String64(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001008
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001009 struct ELF::Elf64_Rela ERE64;
1010 ERE64.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001011 String64(*F, ERE64.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001012
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001013 if (HasRelocationAddend)
1014 String64(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001015 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001016 String32(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001017
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001018 struct ELF::Elf32_Rela ERE32;
1019 ERE32.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001020 String32(*F, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001021
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001022 if (HasRelocationAddend)
1023 String32(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001024 }
Matt Fleming3565a062010-08-16 18:57:57 +00001025 }
1026}
1027
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001028void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
1029 MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001030 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +00001031 MCContext &Ctx = Asm.getContext();
1032 MCDataFragment *F;
1033
Matt Fleming3565a062010-08-16 18:57:57 +00001034 unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
1035
Rafael Espindola38738bf2010-09-22 19:04:41 +00001036 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001037 const MCSectionELF *ShstrtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001038 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001039 SectionKind::getReadOnly());
Rafael Espindola71859c62010-09-16 19:46:31 +00001040 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1041 ShstrtabSD.setAlignment(1);
1042 ShstrtabIndex = Asm.size();
1043
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001044 const MCSectionELF *SymtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001045 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1046 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001047 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001048 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Matt Fleming3565a062010-08-16 18:57:57 +00001049 SymtabSD.setAlignment(Is64Bit ? 8 : 4);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001050 SymbolTableIndex = Asm.size();
1051
1052 MCSectionData *SymtabShndxSD = NULL;
1053
1054 if (NeedsSymtabShndx) {
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001055 const MCSectionELF *SymtabShndxSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001056 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001057 SectionKind::getReadOnly(), 4, "");
Rafael Espindola7be2c332010-10-31 00:16:26 +00001058 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
1059 SymtabShndxSD->setAlignment(4);
1060 }
Matt Fleming3565a062010-08-16 18:57:57 +00001061
Matt Fleming3565a062010-08-16 18:57:57 +00001062 const MCSection *StrtabSection;
1063 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001064 SectionKind::getReadOnly());
Matt Fleming3565a062010-08-16 18:57:57 +00001065 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1066 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +00001067 StringTableIndex = Asm.size();
1068
Rafael Espindolac3c413f2010-09-27 22:04:54 +00001069 WriteRelocations(Asm, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001070
1071 // Symbol table
1072 F = new MCDataFragment(&SymtabSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001073 MCDataFragment *ShndxF = NULL;
1074 if (NeedsSymtabShndx) {
1075 ShndxF = new MCDataFragment(SymtabShndxSD);
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001076 Asm.AddSectionToTheEnd(*this, *SymtabShndxSD, Layout);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001077 }
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001078 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001079 Asm.AddSectionToTheEnd(*this, SymtabSD, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001080
Matt Fleming3565a062010-08-16 18:57:57 +00001081 F = new MCDataFragment(&StrtabSD);
1082 F->getContents().append(StringTable.begin(), StringTable.end());
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001083 Asm.AddSectionToTheEnd(*this, StrtabSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +00001084
Matt Fleming3565a062010-08-16 18:57:57 +00001085 F = new MCDataFragment(&ShstrtabSD);
1086
Matt Fleming3565a062010-08-16 18:57:57 +00001087 // Section header string table.
1088 //
1089 // The first entry of a string table holds a null character so skip
1090 // section 0.
1091 uint64_t Index = 1;
1092 F->getContents() += '\x00';
1093
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001094 StringMap<uint64_t> SecStringMap;
Matt Fleming3565a062010-08-16 18:57:57 +00001095 for (MCAssembler::const_iterator it = Asm.begin(),
1096 ie = Asm.end(); it != ie; ++it) {
Matt Fleming3565a062010-08-16 18:57:57 +00001097 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +00001098 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola51efe7a2010-09-23 14:14:56 +00001099 // FIXME: We could merge suffixes like in .text and .rela.text.
Matt Fleming3565a062010-08-16 18:57:57 +00001100
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001101 StringRef Name = Section.getSectionName();
1102 if (SecStringMap.count(Name)) {
1103 SectionStringTableIndex[&Section] = SecStringMap[Name];
1104 continue;
1105 }
Matt Fleming3565a062010-08-16 18:57:57 +00001106 // Remember the index into the string table so we can write it
1107 // into the sh_name field of the section header table.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001108 SectionStringTableIndex[&Section] = Index;
1109 SecStringMap[Name] = Index;
Matt Fleming3565a062010-08-16 18:57:57 +00001110
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001111 Index += Name.size() + 1;
1112 F->getContents() += Name;
Matt Fleming3565a062010-08-16 18:57:57 +00001113 F->getContents() += '\x00';
1114 }
1115
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001116 Asm.AddSectionToTheEnd(*this, ShstrtabSD, Layout);
Rafael Espindola70703872010-09-30 02:22:20 +00001117}
1118
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001119bool ELFObjectWriter::IsFixupFullyResolved(const MCAssembler &Asm,
1120 const MCValue Target,
1121 bool IsPCRel,
1122 const MCFragment *DF) const {
Rafael Espindola70703872010-09-30 02:22:20 +00001123 // If this is a PCrel relocation, find the section this fixup value is
1124 // relative to.
1125 const MCSection *BaseSection = 0;
1126 if (IsPCRel) {
1127 BaseSection = &DF->getParent()->getSection();
1128 assert(BaseSection);
1129 }
1130
1131 const MCSection *SectionA = 0;
1132 const MCSymbol *SymbolA = 0;
1133 if (const MCSymbolRefExpr *A = Target.getSymA()) {
Rafael Espindola2c920852010-11-16 04:11:46 +00001134 SymbolA = &A->getSymbol();
1135 SectionA = &SymbolA->AliasedSymbol().getSection();
Rafael Espindola70703872010-09-30 02:22:20 +00001136 }
1137
1138 const MCSection *SectionB = 0;
Rafael Espindola12203cc2010-11-21 00:48:25 +00001139 const MCSymbol *SymbolB = 0;
Rafael Espindola70703872010-09-30 02:22:20 +00001140 if (const MCSymbolRefExpr *B = Target.getSymB()) {
Rafael Espindola12203cc2010-11-21 00:48:25 +00001141 SymbolB = &B->getSymbol();
1142 SectionB = &SymbolB->AliasedSymbol().getSection();
Rafael Espindola70703872010-09-30 02:22:20 +00001143 }
1144
1145 if (!BaseSection)
1146 return SectionA == SectionB;
1147
Rafael Espindola12203cc2010-11-21 00:48:25 +00001148 if (SymbolB)
1149 return false;
1150
1151 // Absolute address but PCrel instruction, so we need a relocation.
1152 if (!SymbolA)
1153 return false;
1154
Rafael Espindola2c920852010-11-16 04:11:46 +00001155 // FIXME: This is in here just to match gnu as output. If the two ends
1156 // are in the same section, there is nothing that the linker can do to
1157 // break it.
Rafael Espindola70703872010-09-30 02:22:20 +00001158 const MCSymbolData &DataA = Asm.getSymbolData(*SymbolA);
1159 if (DataA.isExternal())
1160 return false;
1161
Rafael Espindola12203cc2010-11-21 00:48:25 +00001162 return BaseSection == SectionA;
Matt Fleming3565a062010-08-16 18:57:57 +00001163}
1164
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001165void ELFObjectWriter::CreateGroupSections(MCAssembler &Asm,
1166 MCAsmLayout &Layout,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001167 GroupMapTy &GroupMap,
1168 RevGroupMapTy &RevGroupMap) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001169 // Build the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001170 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1171 it != ie; ++it) {
1172 const MCSectionELF &Section =
1173 static_cast<const MCSectionELF&>(it->getSection());
1174 if (!(Section.getFlags() & MCSectionELF::SHF_GROUP))
1175 continue;
1176
1177 const MCSymbol *SignatureSymbol = Section.getGroup();
1178 Asm.getOrCreateSymbolData(*SignatureSymbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001179 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001180 if (!Group) {
1181 Group = Asm.getContext().CreateELFGroupSection();
1182 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1183 Data.setAlignment(4);
1184 MCDataFragment *F = new MCDataFragment(&Data);
1185 String32(*F, ELF::GRP_COMDAT);
1186 }
1187 GroupMap[Group] = SignatureSymbol;
1188 }
1189
1190 // Add sections to the groups
1191 unsigned Index = 1;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001192 unsigned NumGroups = RevGroupMap.size();
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001193 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1194 it != ie; ++it, ++Index) {
1195 const MCSectionELF &Section =
1196 static_cast<const MCSectionELF&>(it->getSection());
1197 if (!(Section.getFlags() & MCSectionELF::SHF_GROUP))
1198 continue;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001199 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001200 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1201 // FIXME: we could use the previous fragment
1202 MCDataFragment *F = new MCDataFragment(&Data);
1203 String32(*F, NumGroups + Index);
1204 }
1205
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001206 for (RevGroupMapTy::const_iterator i = RevGroupMap.begin(),
1207 e = RevGroupMap.end(); i != e; ++i) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001208 const MCSectionELF *Group = i->second;
1209 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001210 Asm.AddSectionToTheEnd(*this, Data, Layout);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001211 }
1212}
1213
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001214void ELFObjectWriter::WriteSection(MCAssembler &Asm,
1215 const SectionIndexMapTy &SectionIndexMap,
1216 uint32_t GroupSymbolIndex,
1217 uint64_t Offset, uint64_t Size,
1218 uint64_t Alignment,
1219 const MCSectionELF &Section) {
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001220 uint64_t sh_link = 0;
1221 uint64_t sh_info = 0;
1222
1223 switch(Section.getType()) {
1224 case ELF::SHT_DYNAMIC:
1225 sh_link = SectionStringTableIndex[&Section];
1226 sh_info = 0;
1227 break;
1228
1229 case ELF::SHT_REL:
1230 case ELF::SHT_RELA: {
1231 const MCSectionELF *SymtabSection;
1232 const MCSectionELF *InfoSection;
1233 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
1234 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001235 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001236 sh_link = SectionIndexMap.lookup(SymtabSection);
1237 assert(sh_link && ".symtab not found");
1238
1239 // Remove ".rel" and ".rela" prefixes.
1240 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1241 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
1242
1243 InfoSection = Asm.getContext().getELFSection(SectionName,
1244 ELF::SHT_PROGBITS, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001245 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001246 sh_info = SectionIndexMap.lookup(InfoSection);
1247 break;
1248 }
1249
1250 case ELF::SHT_SYMTAB:
1251 case ELF::SHT_DYNSYM:
1252 sh_link = StringTableIndex;
1253 sh_info = LastLocalSymbolIndex;
1254 break;
1255
1256 case ELF::SHT_SYMTAB_SHNDX:
1257 sh_link = SymbolTableIndex;
1258 break;
1259
1260 case ELF::SHT_PROGBITS:
1261 case ELF::SHT_STRTAB:
1262 case ELF::SHT_NOBITS:
1263 case ELF::SHT_NULL:
1264 case ELF::SHT_ARM_ATTRIBUTES:
1265 // Nothing to do.
1266 break;
1267
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001268 case ELF::SHT_GROUP: {
1269 sh_link = SymbolTableIndex;
1270 sh_info = GroupSymbolIndex;
1271 break;
1272 }
1273
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001274 default:
1275 assert(0 && "FIXME: sh_type value not supported!");
1276 break;
1277 }
1278
1279 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1280 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1281 Alignment, Section.getEntrySize());
1282}
1283
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001284void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1285 const MCAsmLayout &Layout) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001286 GroupMapTy GroupMap;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001287 RevGroupMapTy RevGroupMap;
1288 CreateGroupSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1289 RevGroupMap);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001290
Rafael Espindolabab2a802010-11-10 21:51:05 +00001291 SectionIndexMapTy SectionIndexMap;
1292
1293 ComputeIndexMap(Asm, SectionIndexMap);
1294
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001295 // Compute symbol table information.
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001296 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap);
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001297
Matt Fleming3565a062010-08-16 18:57:57 +00001298 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001299 const_cast<MCAsmLayout&>(Layout),
1300 SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +00001301
Rafael Espindola1d739a02010-11-10 22:34:07 +00001302 // Update to include the metadata sections.
1303 ComputeIndexMap(Asm, SectionIndexMap);
1304
Matt Fleming3565a062010-08-16 18:57:57 +00001305 // Add 1 for the null section.
1306 unsigned NumSections = Asm.size() + 1;
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001307 uint64_t NaturalAlignment = Is64Bit ? 8 : 4;
1308 uint64_t HeaderSize = Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr);
1309 uint64_t FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001310
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001311 std::vector<const MCSectionELF*> Sections;
1312 Sections.resize(NumSections);
1313
1314 for (SectionIndexMapTy::const_iterator i=
1315 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1316 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
1317 Sections[p.second] = p.first;
1318 }
1319
1320 for (unsigned i = 1; i < NumSections; ++i) {
1321 const MCSectionELF &Section = *Sections[i];
1322 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001323
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001324 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1325
Matt Fleming3565a062010-08-16 18:57:57 +00001326 // Get the size of the section in the output file (including padding).
1327 uint64_t Size = Layout.getSectionFileSize(&SD);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001328
1329 FileOff += Size;
Matt Fleming3565a062010-08-16 18:57:57 +00001330 }
1331
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001332 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1333
Matt Fleming3565a062010-08-16 18:57:57 +00001334 // Write out the ELF header ...
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001335 WriteHeader(FileOff - HeaderSize, NumSections);
1336
1337 FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001338
1339 // ... then all of the sections ...
1340 DenseMap<const MCSection*, uint64_t> SectionOffsetMap;
1341
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001342 for (unsigned i = 1; i < NumSections; ++i) {
1343 const MCSectionELF &Section = *Sections[i];
1344 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001345
1346 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1347 WriteZeros(Padding);
1348 FileOff += Padding;
1349
Matt Fleming3565a062010-08-16 18:57:57 +00001350 // Remember the offset into the file for this section.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001351 SectionOffsetMap[&Section] = FileOff;
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001352
Matt Fleming3565a062010-08-16 18:57:57 +00001353 FileOff += Layout.getSectionFileSize(&SD);
1354
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001355 Asm.WriteSectionData(&SD, Layout, this);
Matt Fleming3565a062010-08-16 18:57:57 +00001356 }
1357
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001358 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1359 WriteZeros(Padding);
1360 FileOff += Padding;
1361
Matt Fleming3565a062010-08-16 18:57:57 +00001362 // ... and then the section header table.
1363 // Should we align the section header table?
1364 //
1365 // Null section first.
Rafael Espindola7be2c332010-10-31 00:16:26 +00001366 uint64_t FirstSectionSize =
1367 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1368 uint32_t FirstSectionLink =
1369 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1370 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming3565a062010-08-16 18:57:57 +00001371
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001372 for (unsigned i = 1; i < NumSections; ++i) {
1373 const MCSectionELF &Section = *Sections[i];
1374 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1375 uint32_t GroupSymbolIndex;
1376 if (Section.getType() != ELF::SHT_GROUP)
1377 GroupSymbolIndex = 0;
1378 else
1379 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm, GroupMap[&Section]);
Matt Fleming3565a062010-08-16 18:57:57 +00001380
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001381 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
1382 SectionOffsetMap[&Section], Layout.getSectionSize(&SD),
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001383 SD.getAlignment(), Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001384 }
1385}
1386
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001387MCObjectWriter *llvm::createELFObjectWriter(raw_ostream &OS,
1388 bool Is64Bit,
1389 Triple::OSType OSType,
1390 uint16_t EMachine,
1391 bool IsLittleEndian,
1392 bool HasRelocationAddend) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001393 switch (EMachine) {
1394 case ELF::EM_386:
1395 case ELF::EM_X86_64:
1396 return new X86ELFObjectWriter(OS, Is64Bit, IsLittleEndian, EMachine,
1397 HasRelocationAddend, OSType); break;
1398 case ELF::EM_ARM:
1399 return new ARMELFObjectWriter(OS, Is64Bit, IsLittleEndian, EMachine,
1400 HasRelocationAddend, OSType); break;
Wesley Peck4b047132010-11-21 22:06:28 +00001401 case ELF::EM_MBLAZE:
1402 return new MBlazeELFObjectWriter(OS, Is64Bit, IsLittleEndian, EMachine,
1403 HasRelocationAddend, OSType); break;
Benjamin Kramer32858772010-11-15 19:20:50 +00001404 default: llvm_unreachable("Unsupported architecture"); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001405 }
1406}
1407
1408
1409/// START OF SUBCLASSES for ELFObjectWriter
1410//===- ARMELFObjectWriter -------------------------------------------===//
1411
1412ARMELFObjectWriter::ARMELFObjectWriter(raw_ostream &_OS, bool _Is64Bit,
1413 bool _IsLittleEndian,
1414 uint16_t _EMachine, bool _HasRelocationAddend,
1415 Triple::OSType _OSType)
1416 : ELFObjectWriter(_OS, _Is64Bit, _IsLittleEndian, _EMachine,
1417 _HasRelocationAddend, _OSType)
1418{}
1419
1420ARMELFObjectWriter::~ARMELFObjectWriter()
1421{}
1422
1423void ARMELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
1424 const MCAsmLayout &Layout,
1425 const MCFragment *Fragment,
1426 const MCFixup &Fixup,
1427 MCValue Target,
1428 uint64_t &FixedValue) {
1429 assert(0 && "ARMELFObjectWriter::RecordRelocation() unimplemented");
1430}
1431
Wesley Peck4b047132010-11-21 22:06:28 +00001432//===- MBlazeELFObjectWriter -------------------------------------------===//
Jason W Kimd3443e92010-11-15 16:18:39 +00001433
Wesley Peck4b047132010-11-21 22:06:28 +00001434MBlazeELFObjectWriter::MBlazeELFObjectWriter(raw_ostream &_OS, bool _Is64Bit,
1435 bool _IsLittleEndian,
1436 uint16_t _EMachine,
1437 bool _HasRelocationAddend,
1438 Triple::OSType _OSType)
1439 : ELFObjectWriter(_OS, _Is64Bit, _IsLittleEndian, _EMachine,
1440 _HasRelocationAddend, _OSType) {
1441}
1442
1443MBlazeELFObjectWriter::~MBlazeELFObjectWriter() {
1444}
1445
1446void MBlazeELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
1447 const MCAsmLayout &Layout,
1448 const MCFragment *Fragment,
1449 const MCFixup &Fixup,
1450 MCValue Target,
1451 uint64_t &FixedValue) {
1452 int64_t Addend = 0;
1453 int Index = 0;
1454 int64_t Value = Target.getConstant();
1455 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
1456 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
1457 const MCSymbol *RelocSymbol = SymbolToReloc(Asm, Target, *Fragment);
1458
Jason W Kim4a511f02010-11-22 18:41:13 +00001459 bool IsPCRel = isFixupKindPCRel(Fixup.getKind());
Wesley Peck4b047132010-11-21 22:06:28 +00001460 if (!Target.isAbsolute()) {
1461 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
1462 const MCSymbol &SymbolB = RefB->getSymbol();
1463 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
1464 IsPCRel = true;
1465 MCSectionData *Sec = Fragment->getParent();
1466
1467 // Offset of the symbol in the section
1468 int64_t a = Layout.getSymbolAddress(&SDB) - Layout.getSectionAddress(Sec);
1469
1470 // Ofeset of the relocation in the section
1471 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
1472 Value += b - a;
1473 }
1474
1475 if (!RelocSymbol) {
1476 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
1477 MCFragment *F = SD.getFragment();
1478
1479 Index = F->getParent()->getOrdinal();
1480
1481 MCSectionData *FSD = F->getParent();
1482 // Offset of the symbol in the section
1483 Value += Layout.getSymbolAddress(&SD) - Layout.getSectionAddress(FSD);
1484 } else {
1485 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
1486 WeakrefUsedInReloc.insert(RelocSymbol);
1487 else
1488 UsedInReloc.insert(RelocSymbol);
1489 Index = -1;
1490 }
1491 Addend = Value;
1492 }
1493
1494 FixedValue = Value;
1495
1496 // determine the type of the relocation
1497 unsigned Type;
1498 if (IsPCRel) {
1499 switch ((unsigned)Fixup.getKind()) {
1500 default:
1501 llvm_unreachable("Unimplemented");
1502 case MBlaze::reloc_pcrel_4byte:
1503 Type = ELF::R_MICROBLAZE_64_PCREL;
1504 break;
1505 case MBlaze::reloc_pcrel_2byte:
1506 Type = ELF::R_MICROBLAZE_32_PCREL;
1507 break;
1508 }
1509 } else {
1510 switch ((unsigned)Fixup.getKind()) {
1511 default: llvm_unreachable("invalid fixup kind!");
1512 case FK_Data_4:
1513 Type = (RelocSymbol || Addend !=0) ? ELF::R_MICROBLAZE_32
1514 : ELF::R_MICROBLAZE_64;
1515 break;
1516 case FK_Data_2:
1517 Type = ELF::R_MICROBLAZE_32;
1518 break;
1519 }
1520 }
1521
1522 MCSymbolRefExpr::VariantKind Modifier = Target.getSymA()->getKind();
1523 if (RelocNeedsGOT(Modifier))
1524 NeedsGOT = true;
1525
Jason W Kim858e7502010-11-22 18:42:07 +00001526 uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
Jason W Kim4a511f02010-11-22 18:41:13 +00001527 Fixup.getOffset();
Wesley Peck4b047132010-11-21 22:06:28 +00001528
Jason W Kim4a511f02010-11-22 18:41:13 +00001529 if (! HasRelocationAddend) Addend = 0;
1530 ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend);
Wesley Peck4b047132010-11-21 22:06:28 +00001531 Relocations[Fragment->getParent()].push_back(ERE);
1532}
Jason W Kimd3443e92010-11-15 16:18:39 +00001533
1534//===- X86ELFObjectWriter -------------------------------------------===//
1535
1536
1537X86ELFObjectWriter::X86ELFObjectWriter(raw_ostream &_OS, bool _Is64Bit,
1538 bool _IsLittleEndian,
1539 uint16_t _EMachine, bool _HasRelocationAddend,
1540 Triple::OSType _OSType)
1541 : ELFObjectWriter(_OS, _Is64Bit, _IsLittleEndian, _EMachine,
1542 _HasRelocationAddend, _OSType)
1543{}
1544
1545X86ELFObjectWriter::~X86ELFObjectWriter()
1546{}
1547
1548void X86ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
1549 const MCAsmLayout &Layout,
1550 const MCFragment *Fragment,
1551 const MCFixup &Fixup,
1552 MCValue Target,
1553 uint64_t &FixedValue) {
1554 int64_t Addend = 0;
1555 int Index = 0;
1556 int64_t Value = Target.getConstant();
Rafael Espindola12203cc2010-11-21 00:48:25 +00001557 const MCSymbol *RelocSymbol = NULL;
Jason W Kimd3443e92010-11-15 16:18:39 +00001558
Jason W Kim4a511f02010-11-22 18:41:13 +00001559 bool IsPCRel = isFixupKindPCRel(Fixup.getKind());
Jason W Kimd3443e92010-11-15 16:18:39 +00001560 if (!Target.isAbsolute()) {
Rafael Espindola12203cc2010-11-21 00:48:25 +00001561 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
1562 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
1563 RelocSymbol = SymbolToReloc(Asm, Target, *Fragment);
1564
Jason W Kimd3443e92010-11-15 16:18:39 +00001565 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
1566 const MCSymbol &SymbolB = RefB->getSymbol();
1567 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
1568 IsPCRel = true;
1569 MCSectionData *Sec = Fragment->getParent();
1570
1571 // Offset of the symbol in the section
1572 int64_t a = Layout.getSymbolAddress(&SDB) - Layout.getSectionAddress(Sec);
1573
1574 // Ofeset of the relocation in the section
1575 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
1576 Value += b - a;
1577 }
1578
1579 if (!RelocSymbol) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +00001580 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
Jason W Kimd3443e92010-11-15 16:18:39 +00001581 MCFragment *F = SD.getFragment();
1582
Rafael Espindola12203cc2010-11-21 00:48:25 +00001583 Index = F->getParent()->getOrdinal() + 1;
Jason W Kimd3443e92010-11-15 16:18:39 +00001584
1585 MCSectionData *FSD = F->getParent();
1586 // Offset of the symbol in the section
1587 Value += Layout.getSymbolAddress(&SD) - Layout.getSectionAddress(FSD);
1588 } else {
1589 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
1590 WeakrefUsedInReloc.insert(RelocSymbol);
1591 else
1592 UsedInReloc.insert(RelocSymbol);
1593 Index = -1;
1594 }
1595 Addend = Value;
1596 // Compensate for the addend on i386.
1597 if (Is64Bit)
1598 Value = 0;
1599 }
1600
1601 FixedValue = Value;
1602
1603 // determine the type of the relocation
1604
Rafael Espindola12203cc2010-11-21 00:48:25 +00001605 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1606 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
Jason W Kimd3443e92010-11-15 16:18:39 +00001607 unsigned Type;
1608 if (Is64Bit) {
1609 if (IsPCRel) {
1610 switch (Modifier) {
1611 default:
1612 llvm_unreachable("Unimplemented");
1613 case MCSymbolRefExpr::VK_None:
1614 Type = ELF::R_X86_64_PC32;
1615 break;
1616 case MCSymbolRefExpr::VK_PLT:
1617 Type = ELF::R_X86_64_PLT32;
1618 break;
1619 case MCSymbolRefExpr::VK_GOTPCREL:
1620 Type = ELF::R_X86_64_GOTPCREL;
1621 break;
1622 case MCSymbolRefExpr::VK_GOTTPOFF:
1623 Type = ELF::R_X86_64_GOTTPOFF;
1624 break;
1625 case MCSymbolRefExpr::VK_TLSGD:
1626 Type = ELF::R_X86_64_TLSGD;
1627 break;
1628 case MCSymbolRefExpr::VK_TLSLD:
1629 Type = ELF::R_X86_64_TLSLD;
1630 break;
1631 }
1632 } else {
1633 switch ((unsigned)Fixup.getKind()) {
1634 default: llvm_unreachable("invalid fixup kind!");
1635 case FK_Data_8: Type = ELF::R_X86_64_64; break;
1636 case X86::reloc_signed_4byte:
1637 case X86::reloc_pcrel_4byte:
1638 assert(isInt<32>(Target.getConstant()));
1639 switch (Modifier) {
1640 default:
1641 llvm_unreachable("Unimplemented");
1642 case MCSymbolRefExpr::VK_None:
1643 Type = ELF::R_X86_64_32S;
1644 break;
1645 case MCSymbolRefExpr::VK_GOT:
1646 Type = ELF::R_X86_64_GOT32;
1647 break;
1648 case MCSymbolRefExpr::VK_GOTPCREL:
1649 Type = ELF::R_X86_64_GOTPCREL;
1650 break;
1651 case MCSymbolRefExpr::VK_TPOFF:
1652 Type = ELF::R_X86_64_TPOFF32;
1653 break;
1654 case MCSymbolRefExpr::VK_DTPOFF:
1655 Type = ELF::R_X86_64_DTPOFF32;
1656 break;
1657 }
1658 break;
1659 case FK_Data_4:
1660 Type = ELF::R_X86_64_32;
1661 break;
1662 case FK_Data_2: Type = ELF::R_X86_64_16; break;
1663 case X86::reloc_pcrel_1byte:
1664 case FK_Data_1: Type = ELF::R_X86_64_8; break;
1665 }
1666 }
1667 } else {
1668 if (IsPCRel) {
1669 switch (Modifier) {
1670 default:
1671 llvm_unreachable("Unimplemented");
1672 case MCSymbolRefExpr::VK_None:
1673 Type = ELF::R_386_PC32;
1674 break;
1675 case MCSymbolRefExpr::VK_PLT:
1676 Type = ELF::R_386_PLT32;
1677 break;
1678 }
1679 } else {
1680 switch ((unsigned)Fixup.getKind()) {
1681 default: llvm_unreachable("invalid fixup kind!");
1682
1683 case X86::reloc_global_offset_table:
1684 Type = ELF::R_386_GOTPC;
1685 break;
1686
1687 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
1688 // instead?
1689 case X86::reloc_signed_4byte:
1690 case X86::reloc_pcrel_4byte:
1691 case FK_Data_4:
1692 switch (Modifier) {
1693 default:
1694 llvm_unreachable("Unimplemented");
1695 case MCSymbolRefExpr::VK_None:
1696 Type = ELF::R_386_32;
1697 break;
1698 case MCSymbolRefExpr::VK_GOT:
1699 Type = ELF::R_386_GOT32;
1700 break;
1701 case MCSymbolRefExpr::VK_GOTOFF:
1702 Type = ELF::R_386_GOTOFF;
1703 break;
1704 case MCSymbolRefExpr::VK_TLSGD:
1705 Type = ELF::R_386_TLS_GD;
1706 break;
1707 case MCSymbolRefExpr::VK_TPOFF:
1708 Type = ELF::R_386_TLS_LE_32;
1709 break;
1710 case MCSymbolRefExpr::VK_INDNTPOFF:
1711 Type = ELF::R_386_TLS_IE;
1712 break;
1713 case MCSymbolRefExpr::VK_NTPOFF:
1714 Type = ELF::R_386_TLS_LE;
1715 break;
1716 case MCSymbolRefExpr::VK_GOTNTPOFF:
1717 Type = ELF::R_386_TLS_GOTIE;
1718 break;
1719 case MCSymbolRefExpr::VK_TLSLDM:
1720 Type = ELF::R_386_TLS_LDM;
1721 break;
1722 case MCSymbolRefExpr::VK_DTPOFF:
1723 Type = ELF::R_386_TLS_LDO_32;
1724 break;
1725 }
1726 break;
1727 case FK_Data_2: Type = ELF::R_386_16; break;
1728 case X86::reloc_pcrel_1byte:
1729 case FK_Data_1: Type = ELF::R_386_8; break;
1730 }
1731 }
1732 }
1733
1734 if (RelocNeedsGOT(Modifier))
1735 NeedsGOT = true;
1736
Jason W Kimd3443e92010-11-15 16:18:39 +00001737
Jason W Kim858e7502010-11-22 18:42:07 +00001738 uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
Jason W Kim4a511f02010-11-22 18:41:13 +00001739 Fixup.getOffset();
Jason W Kimd3443e92010-11-15 16:18:39 +00001740
Jason W Kim4a511f02010-11-22 18:41:13 +00001741 if (! HasRelocationAddend) Addend = 0;
1742 ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend);
Jason W Kimd3443e92010-11-15 16:18:39 +00001743 Relocations[Fragment->getParent()].push_back(ERE);
Matt Fleming3565a062010-08-16 18:57:57 +00001744}