blob: 3b649dbbd2595c78c299d8fc44a549247659169d [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
Jason W Kim10907422010-11-22 22:05:16 +0000132 ELFRelocationEntry(uint64_t RelocOffset, int Idx,
133 unsigned RelType, const MCSymbol *Sym,
Jason W Kim4a511f02010-11-22 18:41:13 +0000134 uint64_t Addend)
Jason W Kim10907422010-11-22 22:05:16 +0000135 : r_offset(RelocOffset), Index(Idx), Type(RelType),
136 Symbol(Sym), 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
Jason W Kim28ef0a52010-11-22 18:57:00 +0000369 private:
Jason W Kim4a511f02010-11-22 18:41:13 +0000370 static bool isFixupKindPCRel(unsigned Kind) {
371 switch (Kind) {
372 default:
373 return false;
374 case X86::reloc_pcrel_1byte:
375 case X86::reloc_pcrel_4byte:
376 case X86::reloc_riprel_4byte:
377 case X86::reloc_riprel_4byte_movq_load:
378 return true;
379 }
380 }
Jason W Kimd3443e92010-11-15 16:18:39 +0000381 };
382
383
384 //===- ARMELFObjectWriter -------------------------------------------===//
385
386 class ARMELFObjectWriter : public ELFObjectWriter {
387 public:
388 ARMELFObjectWriter(raw_ostream &_OS, bool _Is64Bit, bool IsLittleEndian,
389 uint16_t _EMachine, bool _HasRelAddend,
390 Triple::OSType _OSType);
Wesley Peck4b047132010-11-21 22:06:28 +0000391
Jason W Kimd3443e92010-11-15 16:18:39 +0000392 virtual ~ARMELFObjectWriter();
393 virtual void RecordRelocation(const MCAssembler &Asm,
394 const MCAsmLayout &Layout,
395 const MCFragment *Fragment,
396 const MCFixup &Fixup, MCValue Target,
397 uint64_t &FixedValue);
Jason W Kim4a511f02010-11-22 18:41:13 +0000398
Jason W Kim28ef0a52010-11-22 18:57:00 +0000399 private:
Jason W Kim4a511f02010-11-22 18:41:13 +0000400 static bool isFixupKindPCRel(unsigned Kind) {
401 switch (Kind) {
402 default:
403 return false;
Jason W Kimccbe0002010-11-22 18:47:05 +0000404 case ARM::fixup_arm_pcrel_12:
405 case ARM::fixup_arm_vfp_pcrel_12:
406 case ARM::fixup_arm_branch:
Jason W Kim4a511f02010-11-22 18:41:13 +0000407 return true;
408 }
409 }
Jason W Kimd3443e92010-11-15 16:18:39 +0000410 };
Wesley Peck4b047132010-11-21 22:06:28 +0000411
412 //===- MBlazeELFObjectWriter -------------------------------------------===//
413
414 class MBlazeELFObjectWriter : public ELFObjectWriter {
415 public:
416 MBlazeELFObjectWriter(raw_ostream &_OS, bool _Is64Bit, bool IsLittleEndian,
417 uint16_t _EMachine, bool _HasRelAddend,
418 Triple::OSType _OSType);
419
420 virtual ~MBlazeELFObjectWriter();
421 virtual void RecordRelocation(const MCAssembler &Asm,
422 const MCAsmLayout &Layout,
423 const MCFragment *Fragment,
424 const MCFixup &Fixup, MCValue Target,
425 uint64_t &FixedValue);
Jason W Kim4a511f02010-11-22 18:41:13 +0000426
Jason W Kim28ef0a52010-11-22 18:57:00 +0000427 private:
Jason W Kim4a511f02010-11-22 18:41:13 +0000428 static bool isFixupKindPCRel(unsigned Kind) {
429 switch (Kind) {
430 default:
431 return false;
Jason W Kimccbe0002010-11-22 18:47:05 +0000432 case MBlaze::reloc_pcrel_2byte:
433 case MBlaze::reloc_pcrel_4byte:
Jason W Kim4a511f02010-11-22 18:41:13 +0000434 return true;
435 }
436 }
Wesley Peck4b047132010-11-21 22:06:28 +0000437 };
Matt Fleming3565a062010-08-16 18:57:57 +0000438}
439
Jason W Kimd3443e92010-11-15 16:18:39 +0000440ELFObjectWriter::~ELFObjectWriter()
441{}
442
Matt Fleming3565a062010-08-16 18:57:57 +0000443// Emit the ELF header.
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000444void ELFObjectWriter::WriteHeader(uint64_t SectionDataSize,
445 unsigned NumberOfSections) {
Matt Fleming3565a062010-08-16 18:57:57 +0000446 // ELF Header
447 // ----------
448 //
449 // Note
450 // ----
451 // emitWord method behaves differently for ELF32 and ELF64, writing
452 // 4 bytes in the former and 8 in the latter.
453
454 Write8(0x7f); // e_ident[EI_MAG0]
455 Write8('E'); // e_ident[EI_MAG1]
456 Write8('L'); // e_ident[EI_MAG2]
457 Write8('F'); // e_ident[EI_MAG3]
458
459 Write8(Is64Bit ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
460
461 // e_ident[EI_DATA]
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000462 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
Matt Fleming3565a062010-08-16 18:57:57 +0000463
464 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky5baf79e2010-09-09 17:57:50 +0000465 // e_ident[EI_OSABI]
466 switch (OSType) {
467 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
468 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
469 default: Write8(ELF::ELFOSABI_NONE); break;
470 }
Matt Fleming3565a062010-08-16 18:57:57 +0000471 Write8(0); // e_ident[EI_ABIVERSION]
472
473 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
474
475 Write16(ELF::ET_REL); // e_type
476
Wesley Peckeecb8582010-10-22 15:52:49 +0000477 Write16(EMachine); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +0000478
479 Write32(ELF::EV_CURRENT); // e_version
480 WriteWord(0); // e_entry, no entry point in .o file
481 WriteWord(0); // e_phoff, no program header for .o
Benjamin Kramereb976772010-08-17 17:02:29 +0000482 WriteWord(SectionDataSize + (Is64Bit ? sizeof(ELF::Elf64_Ehdr) :
483 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000484
485 // FIXME: Make this configurable.
486 Write32(0); // e_flags = whatever the target wants
487
488 // e_ehsize = ELF header size
489 Write16(Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
490
491 Write16(0); // e_phentsize = prog header entry size
492 Write16(0); // e_phnum = # prog header entries = 0
493
494 // e_shentsize = Section header entry size
495 Write16(Is64Bit ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
496
497 // e_shnum = # of section header ents
Rafael Espindola7be2c332010-10-31 00:16:26 +0000498 if (NumberOfSections >= ELF::SHN_LORESERVE)
499 Write16(0);
500 else
501 Write16(NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000502
503 // e_shstrndx = Section # of '.shstrtab'
Rafael Espindola7be2c332010-10-31 00:16:26 +0000504 if (NumberOfSections >= ELF::SHN_LORESERVE)
505 Write16(ELF::SHN_XINDEX);
506 else
507 Write16(ShstrtabIndex);
Matt Fleming3565a062010-08-16 18:57:57 +0000508}
509
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000510void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
511 MCDataFragment *ShndxF,
512 uint64_t name,
513 uint8_t info, uint64_t value,
514 uint64_t size, uint8_t other,
515 uint32_t shndx,
516 bool Reserved) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000517 if (ShndxF) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000518 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000519 String32(*ShndxF, shndx);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000520 else
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000521 String32(*ShndxF, 0);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000522 }
523
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000524 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
525 uint16_t(ELF::SHN_XINDEX) : shndx;
526
Matt Fleming3565a062010-08-16 18:57:57 +0000527 if (Is64Bit) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000528 String32(*SymtabF, name); // st_name
529 String8(*SymtabF, info); // st_info
530 String8(*SymtabF, other); // st_other
531 String16(*SymtabF, Index); // st_shndx
532 String64(*SymtabF, value); // st_value
533 String64(*SymtabF, size); // st_size
Matt Fleming3565a062010-08-16 18:57:57 +0000534 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000535 String32(*SymtabF, name); // st_name
536 String32(*SymtabF, value); // st_value
537 String32(*SymtabF, size); // st_size
538 String8(*SymtabF, info); // st_info
539 String8(*SymtabF, other); // st_other
540 String16(*SymtabF, Index); // st_shndx
Matt Fleming3565a062010-08-16 18:57:57 +0000541 }
542}
543
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000544static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout) {
545 if (Data.isCommon() && Data.isExternal())
546 return Data.getCommonAlignment();
547
548 const MCSymbol &Symbol = Data.getSymbol();
549 if (!Symbol.isInSection())
550 return 0;
551
Rafael Espindola1973d432010-10-28 19:39:57 +0000552 if (MCFragment *FF = Data.getFragment())
553 return Layout.getSymbolAddress(&Data) -
554 Layout.getSectionAddress(FF->getParent());
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000555
556 return 0;
557}
558
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000559void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm) {
Rafael Espindola88182132010-10-27 15:18:17 +0000560 // The presence of symbol versions causes undefined symbols and
561 // versions declared with @@@ to be renamed.
562
563 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
564 ie = Asm.symbol_end(); it != ie; ++it) {
565 const MCSymbol &Alias = it->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000566 const MCSymbol &Symbol = Alias.AliasedSymbol();
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000567 MCSymbolData &SD = Asm.getSymbolData(Symbol);
568
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000569 // Not an alias.
570 if (&Symbol == &Alias)
571 continue;
572
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000573 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000574 size_t Pos = AliasName.find('@');
575 if (Pos == StringRef::npos)
576 continue;
577
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000578 // Aliases defined with .symvar copy the binding from the symbol they alias.
579 // This is the first place we are able to copy this information.
580 it->setExternal(SD.isExternal());
581 SetBinding(*it, GetBinding(SD));
582
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000583 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000584 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
585 continue;
586
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000587 // FIXME: produce a better error message.
588 if (Symbol.isUndefined() && Rest.startswith("@@") &&
589 !Rest.startswith("@@@"))
590 report_fatal_error("A @@ version cannot be undefined");
591
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000592 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000593 }
594}
595
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000596void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
597 MCDataFragment *ShndxF,
598 ELFSymbolData &MSD,
599 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000600 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000601 MCSymbolData &Data =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000602 Layout.getAssembler().getSymbolData(OrigData.getSymbol().AliasedSymbol());
Rafael Espindola152c1062010-10-06 21:02:29 +0000603
Rafael Espindola7be2c332010-10-31 00:16:26 +0000604 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
605 Data.getSymbol().isVariable();
606
Rafael Espindola152c1062010-10-06 21:02:29 +0000607 uint8_t Binding = GetBinding(OrigData);
608 uint8_t Visibility = GetVisibility(OrigData);
609 uint8_t Type = GetType(Data);
610
611 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
612 uint8_t Other = Visibility;
613
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000614 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000615 uint64_t Size = 0;
616 const MCExpr *ESize;
617
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000618 assert(!(Data.isCommon() && !Data.isExternal()));
619
Matt Fleming3565a062010-08-16 18:57:57 +0000620 ESize = Data.getSize();
621 if (Data.getSize()) {
622 MCValue Res;
623 if (ESize->getKind() == MCExpr::Binary) {
624 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(ESize);
625
626 if (BE->EvaluateAsRelocatable(Res, &Layout)) {
Benjamin Kramer24f12062010-10-17 07:38:40 +0000627 assert(!Res.getSymA() || !Res.getSymA()->getSymbol().isDefined());
628 assert(!Res.getSymB() || !Res.getSymB()->getSymbol().isDefined());
Rafael Espindolaf230df92010-10-16 18:23:53 +0000629 Size = Res.getConstant();
Matt Fleming3565a062010-08-16 18:57:57 +0000630 }
631 } else if (ESize->getKind() == MCExpr::Constant) {
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000632 Size = static_cast<const MCConstantExpr *>(ESize)->getValue();
Matt Fleming3565a062010-08-16 18:57:57 +0000633 } else {
634 assert(0 && "Unsupported size expression");
635 }
636 }
637
638 // Write out the symbol table entry
Rafael Espindola7be2c332010-10-31 00:16:26 +0000639 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
640 Size, Other, MSD.SectionIndex, IsReserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000641}
642
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000643void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
644 MCDataFragment *ShndxF,
645 const MCAssembler &Asm,
646 const MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000647 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000648 // The string table must be emitted first because we need the index
649 // into the string table for all the symbol names.
650 assert(StringTable.size() && "Missing string table");
651
652 // FIXME: Make sure the start of the symbol table is aligned.
653
654 // The first entry is the undefined symbol entry.
Rafael Espindola7be2c332010-10-31 00:16:26 +0000655 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
Matt Fleming3565a062010-08-16 18:57:57 +0000656
657 // Write the symbol table entries.
658 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
659 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
660 ELFSymbolData &MSD = LocalSymbolData[i];
Rafael Espindola7be2c332010-10-31 00:16:26 +0000661 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000662 }
663
Rafael Espindola71859c62010-09-16 19:46:31 +0000664 // Write out a symbol table entry for each regular section.
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000665 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
666 ++i) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000667 const MCSectionELF &Section =
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000668 static_cast<const MCSectionELF&>(i->getSection());
669 if (Section.getType() == ELF::SHT_RELA ||
670 Section.getType() == ELF::SHT_REL ||
671 Section.getType() == ELF::SHT_STRTAB ||
672 Section.getType() == ELF::SHT_SYMTAB)
Eli Friedmana44fa242010-08-16 21:17:09 +0000673 continue;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000674 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000675 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section), false);
Eli Friedmana44fa242010-08-16 21:17:09 +0000676 LastLocalSymbolIndex++;
677 }
Matt Fleming3565a062010-08-16 18:57:57 +0000678
679 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
680 ELFSymbolData &MSD = ExternalSymbolData[i];
681 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000682 assert(((Data.getFlags() & ELF_STB_Global) ||
683 (Data.getFlags() & ELF_STB_Weak)) &&
684 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000685 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000686 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000687 LastLocalSymbolIndex++;
688 }
689
690 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
691 ELFSymbolData &MSD = UndefinedSymbolData[i];
692 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000693 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000694 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000695 LastLocalSymbolIndex++;
696 }
697}
698
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000699const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm,
700 const MCValue &Target,
701 const MCFragment &F) const {
702 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000703 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
704 const MCSymbol *Renamed = Renames.lookup(&Symbol);
705 const MCSymbolData &SD = Asm.getSymbolData(Symbol);
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000706
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000707 if (ASymbol.isUndefined()) {
708 if (Renamed)
709 return Renamed;
710 return &ASymbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000711 }
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000712
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000713 if (SD.isExternal()) {
714 if (Renamed)
715 return Renamed;
716 return &Symbol;
717 }
Rafael Espindola73ffea42010-09-25 05:42:19 +0000718
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000719 const MCSectionELF &Section =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000720 static_cast<const MCSectionELF&>(ASymbol.getSection());
Rafael Espindola25958732010-11-24 21:57:39 +0000721 const SectionKind secKind = Section.getKind();
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000722
Rafael Espindola25958732010-11-24 21:57:39 +0000723 if (secKind.isBSS())
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000724 return NULL;
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000725
Rafael Espindola25958732010-11-24 21:57:39 +0000726 if (secKind.isThreadLocal()) {
727 if (Renamed)
728 return Renamed;
729 return &Symbol;
730 }
731
Rafael Espindola8cecf252010-10-06 16:23:36 +0000732 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000733 const MCSectionELF &Sec2 =
734 static_cast<const MCSectionELF&>(F.getParent()->getSection());
735
Rafael Espindola8cecf252010-10-06 16:23:36 +0000736 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000737 (Kind == MCSymbolRefExpr::VK_PLT ||
738 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
Rafael Espindola25958732010-11-24 21:57:39 +0000739 Kind == MCSymbolRefExpr::VK_GOTOFF)) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000740 if (Renamed)
741 return Renamed;
742 return &Symbol;
743 }
Rafael Espindola3729d002010-10-05 23:57:26 +0000744
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000745 if (Section.getFlags() & MCSectionELF::SHF_MERGE) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000746 if (Target.getConstant() == 0)
747 return NULL;
748 if (Renamed)
749 return Renamed;
750 return &Symbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000751 }
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000752
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000753 return NULL;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000754}
755
Matt Fleming3565a062010-08-16 18:57:57 +0000756
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000757uint64_t
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000758ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
759 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000760 MCSymbolData &SD = Asm.getSymbolData(*S);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000761 return SD.getIndex();
Matt Fleming3565a062010-08-16 18:57:57 +0000762}
763
Rafael Espindola737cd212010-10-05 18:01:23 +0000764static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
Rafael Espindola88182132010-10-27 15:18:17 +0000765 bool Used, bool Renamed) {
Rafael Espindola484291c2010-11-01 14:28:48 +0000766 if (Data.getFlags() & ELF_Other_Weakref)
767 return false;
768
Rafael Espindolabd701182010-10-19 19:31:37 +0000769 if (Used)
770 return true;
771
Rafael Espindola88182132010-10-27 15:18:17 +0000772 if (Renamed)
773 return false;
774
Rafael Espindola737cd212010-10-05 18:01:23 +0000775 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000776
Rafael Espindolad1798862010-10-29 23:09:31 +0000777 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
778 return true;
779
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000780 const MCSymbol &A = Symbol.AliasedSymbol();
Rafael Espindolad1798862010-10-29 23:09:31 +0000781 if (!A.isVariable() && A.isUndefined() && !Data.isCommon())
Rafael Espindolaa6866962010-10-27 14:44:52 +0000782 return false;
783
Rafael Espindola737cd212010-10-05 18:01:23 +0000784 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
785 return false;
786
Rafael Espindolabd701182010-10-19 19:31:37 +0000787 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000788 return false;
789
790 return true;
791}
792
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000793static bool isLocal(const MCSymbolData &Data, bool isSignature,
794 bool isUsedInReloc) {
Rafael Espindola737cd212010-10-05 18:01:23 +0000795 if (Data.isExternal())
796 return false;
797
798 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000799 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000800
801 if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
802 if (isSignature && !isUsedInReloc)
803 return true;
804
Rafael Espindola737cd212010-10-05 18:01:23 +0000805 return false;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000806 }
Rafael Espindola737cd212010-10-05 18:01:23 +0000807
808 return true;
809}
810
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000811void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
812 SectionIndexMapTy &SectionIndexMap) {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000813 unsigned Index = 1;
814 for (MCAssembler::iterator it = Asm.begin(),
815 ie = Asm.end(); it != ie; ++it) {
816 const MCSectionELF &Section =
817 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000818 if (Section.getType() != ELF::SHT_GROUP)
819 continue;
820 SectionIndexMap[&Section] = Index++;
821 }
822
823 for (MCAssembler::iterator it = Asm.begin(),
824 ie = Asm.end(); it != ie; ++it) {
825 const MCSectionELF &Section =
826 static_cast<const MCSectionELF &>(it->getSection());
827 if (Section.getType() == ELF::SHT_GROUP)
828 continue;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000829 SectionIndexMap[&Section] = Index++;
830 }
831}
832
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000833void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000834 const SectionIndexMapTy &SectionIndexMap,
835 RevGroupMapTy RevGroupMap) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000836 // FIXME: Is this the correct place to do this?
837 if (NeedsGOT) {
838 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
839 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
840 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
841 Data.setExternal(true);
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000842 SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindola5c77c162010-10-05 15:48:37 +0000843 }
844
Matt Fleming3565a062010-08-16 18:57:57 +0000845 // Build section lookup table.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000846 int NumRegularSections = Asm.size();
Matt Fleming3565a062010-08-16 18:57:57 +0000847
848 // Index 0 is always the empty string.
849 StringMap<uint64_t> StringIndexMap;
850 StringTable += '\x00';
851
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000852 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000853 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
854 ie = Asm.symbol_end(); it != ie; ++it) {
855 const MCSymbol &Symbol = it->getSymbol();
856
Rafael Espindola484291c2010-11-01 14:28:48 +0000857 bool Used = UsedInReloc.count(&Symbol);
858 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000859 bool isSignature = RevGroupMap.count(&Symbol);
860
861 if (!isInSymtab(Asm, *it,
862 Used || WeakrefUsed || isSignature,
Rafael Espindola88182132010-10-27 15:18:17 +0000863 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000864 continue;
865
Matt Fleming3565a062010-08-16 18:57:57 +0000866 ELFSymbolData MSD;
867 MSD.SymbolData = it;
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000868 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Matt Fleming3565a062010-08-16 18:57:57 +0000869
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000870 // Undefined symbols are global, but this is the first place we
871 // are able to set it.
872 bool Local = isLocal(*it, isSignature, Used);
873 if (!Local && GetBinding(*it) == ELF::STB_LOCAL) {
874 MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
875 SetBinding(*it, ELF::STB_GLOBAL);
876 SetBinding(SD, ELF::STB_GLOBAL);
877 }
878
Rafael Espindola484291c2010-11-01 14:28:48 +0000879 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
880 SetBinding(*it, ELF::STB_WEAK);
881
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000882 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000883 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000884 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000885 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000886 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000887 } else if (RefSymbol.isUndefined()) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000888 if (isSignature && !Used)
889 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
890 else
891 MSD.SectionIndex = ELF::SHN_UNDEF;
Matt Fleming3565a062010-08-16 18:57:57 +0000892 } else {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000893 const MCSectionELF &Section =
894 static_cast<const MCSectionELF&>(RefSymbol.getSection());
895 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000896 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
897 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000898 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000899 }
900
Rafael Espindola88182132010-10-27 15:18:17 +0000901 // The @@@ in symbol version is replaced with @ in undefined symbols and
902 // @@ in defined ones.
903 StringRef Name = Symbol.getName();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000904 SmallString<32> Buf;
905
Rafael Espindola88182132010-10-27 15:18:17 +0000906 size_t Pos = Name.find("@@@");
Rafael Espindola88182132010-10-27 15:18:17 +0000907 if (Pos != StringRef::npos) {
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000908 Buf += Name.substr(0, Pos);
909 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
910 Buf += Name.substr(Pos + Skip);
911 Name = Buf;
Rafael Espindola88182132010-10-27 15:18:17 +0000912 }
913
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000914 uint64_t &Entry = StringIndexMap[Name];
Rafael Espindolaa6866962010-10-27 14:44:52 +0000915 if (!Entry) {
916 Entry = StringTable.size();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000917 StringTable += Name;
Rafael Espindolaa6866962010-10-27 14:44:52 +0000918 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +0000919 }
Rafael Espindolaa6866962010-10-27 14:44:52 +0000920 MSD.StringIndex = Entry;
921 if (MSD.SectionIndex == ELF::SHN_UNDEF)
922 UndefinedSymbolData.push_back(MSD);
923 else if (Local)
924 LocalSymbolData.push_back(MSD);
925 else
926 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +0000927 }
928
929 // Symbols are required to be in lexicographic order.
930 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
931 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
932 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
933
934 // Set the symbol indices. Local symbols must come before all other
935 // symbols with non-local bindings.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000936 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000937 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
938 LocalSymbolData[i].SymbolData->setIndex(Index++);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000939
940 Index += NumRegularSections;
941
Matt Fleming3565a062010-08-16 18:57:57 +0000942 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
943 ExternalSymbolData[i].SymbolData->setIndex(Index++);
944 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
945 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
946}
947
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000948void ELFObjectWriter::WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
949 const MCSectionData &SD) {
Matt Fleming3565a062010-08-16 18:57:57 +0000950 if (!Relocations[&SD].empty()) {
951 MCContext &Ctx = Asm.getContext();
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000952 const MCSectionELF *RelaSection;
Matt Fleming3565a062010-08-16 18:57:57 +0000953 const MCSectionELF &Section =
954 static_cast<const MCSectionELF&>(SD.getSection());
955
956 const StringRef SectionName = Section.getSectionName();
Benjamin Kramer377a5722010-08-17 17:30:07 +0000957 std::string RelaSectionName = HasRelocationAddend ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +0000958 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000959
960 unsigned EntrySize;
961 if (HasRelocationAddend)
962 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
963 else
964 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +0000965
Benjamin Kramer377a5722010-08-17 17:30:07 +0000966 RelaSection = Ctx.getELFSection(RelaSectionName, HasRelocationAddend ?
967 ELF::SHT_RELA : ELF::SHT_REL, 0,
Matt Fleming3565a062010-08-16 18:57:57 +0000968 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000969 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +0000970
971 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Benjamin Kramera9eadca2010-09-06 16:11:52 +0000972 RelaSD.setAlignment(Is64Bit ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +0000973
974 MCDataFragment *F = new MCDataFragment(&RelaSD);
975
976 WriteRelocationsFragment(Asm, F, &SD);
977
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000978 Asm.AddSectionToTheEnd(*this, RelaSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000979 }
980}
981
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000982void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
983 uint64_t Flags, uint64_t Address,
984 uint64_t Offset, uint64_t Size,
985 uint32_t Link, uint32_t Info,
986 uint64_t Alignment,
987 uint64_t EntrySize) {
Matt Fleming3565a062010-08-16 18:57:57 +0000988 Write32(Name); // sh_name: index into string table
989 Write32(Type); // sh_type
990 WriteWord(Flags); // sh_flags
991 WriteWord(Address); // sh_addr
992 WriteWord(Offset); // sh_offset
993 WriteWord(Size); // sh_size
994 Write32(Link); // sh_link
995 Write32(Info); // sh_info
996 WriteWord(Alignment); // sh_addralign
997 WriteWord(EntrySize); // sh_entsize
998}
999
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001000void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
1001 MCDataFragment *F,
1002 const MCSectionData *SD) {
Matt Fleming3565a062010-08-16 18:57:57 +00001003 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
1004 // sort by the r_offset just like gnu as does
1005 array_pod_sort(Relocs.begin(), Relocs.end());
1006
1007 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1008 ELFRelocationEntry entry = Relocs[e - i - 1];
1009
Rafael Espindola12203cc2010-11-21 00:48:25 +00001010 if (!entry.Index)
1011 ;
1012 else if (entry.Index < 0)
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001013 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
1014 else
Rafael Espindola12203cc2010-11-21 00:48:25 +00001015 entry.Index += LocalSymbolData.size();
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001016 if (Is64Bit) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001017 String64(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001018
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001019 struct ELF::Elf64_Rela ERE64;
1020 ERE64.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001021 String64(*F, ERE64.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001022
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001023 if (HasRelocationAddend)
1024 String64(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001025 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001026 String32(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001027
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001028 struct ELF::Elf32_Rela ERE32;
1029 ERE32.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001030 String32(*F, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001031
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001032 if (HasRelocationAddend)
1033 String32(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001034 }
Matt Fleming3565a062010-08-16 18:57:57 +00001035 }
1036}
1037
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001038void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
1039 MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001040 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +00001041 MCContext &Ctx = Asm.getContext();
1042 MCDataFragment *F;
1043
Matt Fleming3565a062010-08-16 18:57:57 +00001044 unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
1045
Rafael Espindola38738bf2010-09-22 19:04:41 +00001046 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001047 const MCSectionELF *ShstrtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001048 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001049 SectionKind::getReadOnly());
Rafael Espindola71859c62010-09-16 19:46:31 +00001050 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1051 ShstrtabSD.setAlignment(1);
1052 ShstrtabIndex = Asm.size();
1053
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001054 const MCSectionELF *SymtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001055 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1056 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001057 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001058 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Matt Fleming3565a062010-08-16 18:57:57 +00001059 SymtabSD.setAlignment(Is64Bit ? 8 : 4);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001060 SymbolTableIndex = Asm.size();
1061
1062 MCSectionData *SymtabShndxSD = NULL;
1063
1064 if (NeedsSymtabShndx) {
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001065 const MCSectionELF *SymtabShndxSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001066 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001067 SectionKind::getReadOnly(), 4, "");
Rafael Espindola7be2c332010-10-31 00:16:26 +00001068 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
1069 SymtabShndxSD->setAlignment(4);
1070 }
Matt Fleming3565a062010-08-16 18:57:57 +00001071
Matt Fleming3565a062010-08-16 18:57:57 +00001072 const MCSection *StrtabSection;
1073 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001074 SectionKind::getReadOnly());
Matt Fleming3565a062010-08-16 18:57:57 +00001075 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1076 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +00001077 StringTableIndex = Asm.size();
1078
Rafael Espindolac3c413f2010-09-27 22:04:54 +00001079 WriteRelocations(Asm, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001080
1081 // Symbol table
1082 F = new MCDataFragment(&SymtabSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001083 MCDataFragment *ShndxF = NULL;
1084 if (NeedsSymtabShndx) {
1085 ShndxF = new MCDataFragment(SymtabShndxSD);
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001086 Asm.AddSectionToTheEnd(*this, *SymtabShndxSD, Layout);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001087 }
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001088 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001089 Asm.AddSectionToTheEnd(*this, SymtabSD, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001090
Matt Fleming3565a062010-08-16 18:57:57 +00001091 F = new MCDataFragment(&StrtabSD);
1092 F->getContents().append(StringTable.begin(), StringTable.end());
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001093 Asm.AddSectionToTheEnd(*this, StrtabSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +00001094
Matt Fleming3565a062010-08-16 18:57:57 +00001095 F = new MCDataFragment(&ShstrtabSD);
1096
Matt Fleming3565a062010-08-16 18:57:57 +00001097 // Section header string table.
1098 //
1099 // The first entry of a string table holds a null character so skip
1100 // section 0.
1101 uint64_t Index = 1;
1102 F->getContents() += '\x00';
1103
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001104 StringMap<uint64_t> SecStringMap;
Matt Fleming3565a062010-08-16 18:57:57 +00001105 for (MCAssembler::const_iterator it = Asm.begin(),
1106 ie = Asm.end(); it != ie; ++it) {
Matt Fleming3565a062010-08-16 18:57:57 +00001107 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +00001108 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola51efe7a2010-09-23 14:14:56 +00001109 // FIXME: We could merge suffixes like in .text and .rela.text.
Matt Fleming3565a062010-08-16 18:57:57 +00001110
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001111 StringRef Name = Section.getSectionName();
1112 if (SecStringMap.count(Name)) {
1113 SectionStringTableIndex[&Section] = SecStringMap[Name];
1114 continue;
1115 }
Matt Fleming3565a062010-08-16 18:57:57 +00001116 // Remember the index into the string table so we can write it
1117 // into the sh_name field of the section header table.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001118 SectionStringTableIndex[&Section] = Index;
1119 SecStringMap[Name] = Index;
Matt Fleming3565a062010-08-16 18:57:57 +00001120
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001121 Index += Name.size() + 1;
1122 F->getContents() += Name;
Matt Fleming3565a062010-08-16 18:57:57 +00001123 F->getContents() += '\x00';
1124 }
1125
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001126 Asm.AddSectionToTheEnd(*this, ShstrtabSD, Layout);
Rafael Espindola70703872010-09-30 02:22:20 +00001127}
1128
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001129bool ELFObjectWriter::IsFixupFullyResolved(const MCAssembler &Asm,
1130 const MCValue Target,
1131 bool IsPCRel,
1132 const MCFragment *DF) const {
Rafael Espindola70703872010-09-30 02:22:20 +00001133 // If this is a PCrel relocation, find the section this fixup value is
1134 // relative to.
1135 const MCSection *BaseSection = 0;
1136 if (IsPCRel) {
1137 BaseSection = &DF->getParent()->getSection();
1138 assert(BaseSection);
1139 }
1140
1141 const MCSection *SectionA = 0;
1142 const MCSymbol *SymbolA = 0;
1143 if (const MCSymbolRefExpr *A = Target.getSymA()) {
Rafael Espindola2c920852010-11-16 04:11:46 +00001144 SymbolA = &A->getSymbol();
1145 SectionA = &SymbolA->AliasedSymbol().getSection();
Rafael Espindola70703872010-09-30 02:22:20 +00001146 }
1147
1148 const MCSection *SectionB = 0;
Rafael Espindola12203cc2010-11-21 00:48:25 +00001149 const MCSymbol *SymbolB = 0;
Rafael Espindola70703872010-09-30 02:22:20 +00001150 if (const MCSymbolRefExpr *B = Target.getSymB()) {
Rafael Espindola12203cc2010-11-21 00:48:25 +00001151 SymbolB = &B->getSymbol();
1152 SectionB = &SymbolB->AliasedSymbol().getSection();
Rafael Espindola70703872010-09-30 02:22:20 +00001153 }
1154
1155 if (!BaseSection)
1156 return SectionA == SectionB;
1157
Rafael Espindola12203cc2010-11-21 00:48:25 +00001158 if (SymbolB)
1159 return false;
1160
1161 // Absolute address but PCrel instruction, so we need a relocation.
1162 if (!SymbolA)
1163 return false;
1164
Rafael Espindola2c920852010-11-16 04:11:46 +00001165 // FIXME: This is in here just to match gnu as output. If the two ends
1166 // are in the same section, there is nothing that the linker can do to
1167 // break it.
Rafael Espindola70703872010-09-30 02:22:20 +00001168 const MCSymbolData &DataA = Asm.getSymbolData(*SymbolA);
1169 if (DataA.isExternal())
1170 return false;
1171
Rafael Espindola12203cc2010-11-21 00:48:25 +00001172 return BaseSection == SectionA;
Matt Fleming3565a062010-08-16 18:57:57 +00001173}
1174
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001175void ELFObjectWriter::CreateGroupSections(MCAssembler &Asm,
1176 MCAsmLayout &Layout,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001177 GroupMapTy &GroupMap,
1178 RevGroupMapTy &RevGroupMap) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001179 // Build the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001180 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1181 it != ie; ++it) {
1182 const MCSectionELF &Section =
1183 static_cast<const MCSectionELF&>(it->getSection());
1184 if (!(Section.getFlags() & MCSectionELF::SHF_GROUP))
1185 continue;
1186
1187 const MCSymbol *SignatureSymbol = Section.getGroup();
1188 Asm.getOrCreateSymbolData(*SignatureSymbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001189 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001190 if (!Group) {
1191 Group = Asm.getContext().CreateELFGroupSection();
1192 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1193 Data.setAlignment(4);
1194 MCDataFragment *F = new MCDataFragment(&Data);
1195 String32(*F, ELF::GRP_COMDAT);
1196 }
1197 GroupMap[Group] = SignatureSymbol;
1198 }
1199
1200 // Add sections to the groups
1201 unsigned Index = 1;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001202 unsigned NumGroups = RevGroupMap.size();
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001203 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1204 it != ie; ++it, ++Index) {
1205 const MCSectionELF &Section =
1206 static_cast<const MCSectionELF&>(it->getSection());
1207 if (!(Section.getFlags() & MCSectionELF::SHF_GROUP))
1208 continue;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001209 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001210 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1211 // FIXME: we could use the previous fragment
1212 MCDataFragment *F = new MCDataFragment(&Data);
1213 String32(*F, NumGroups + Index);
1214 }
1215
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001216 for (RevGroupMapTy::const_iterator i = RevGroupMap.begin(),
1217 e = RevGroupMap.end(); i != e; ++i) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001218 const MCSectionELF *Group = i->second;
1219 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001220 Asm.AddSectionToTheEnd(*this, Data, Layout);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001221 }
1222}
1223
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001224void ELFObjectWriter::WriteSection(MCAssembler &Asm,
1225 const SectionIndexMapTy &SectionIndexMap,
1226 uint32_t GroupSymbolIndex,
1227 uint64_t Offset, uint64_t Size,
1228 uint64_t Alignment,
1229 const MCSectionELF &Section) {
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001230 uint64_t sh_link = 0;
1231 uint64_t sh_info = 0;
1232
1233 switch(Section.getType()) {
1234 case ELF::SHT_DYNAMIC:
1235 sh_link = SectionStringTableIndex[&Section];
1236 sh_info = 0;
1237 break;
1238
1239 case ELF::SHT_REL:
1240 case ELF::SHT_RELA: {
1241 const MCSectionELF *SymtabSection;
1242 const MCSectionELF *InfoSection;
1243 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
1244 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001245 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001246 sh_link = SectionIndexMap.lookup(SymtabSection);
1247 assert(sh_link && ".symtab not found");
1248
1249 // Remove ".rel" and ".rela" prefixes.
1250 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1251 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
1252
1253 InfoSection = Asm.getContext().getELFSection(SectionName,
1254 ELF::SHT_PROGBITS, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001255 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001256 sh_info = SectionIndexMap.lookup(InfoSection);
1257 break;
1258 }
1259
1260 case ELF::SHT_SYMTAB:
1261 case ELF::SHT_DYNSYM:
1262 sh_link = StringTableIndex;
1263 sh_info = LastLocalSymbolIndex;
1264 break;
1265
1266 case ELF::SHT_SYMTAB_SHNDX:
1267 sh_link = SymbolTableIndex;
1268 break;
1269
1270 case ELF::SHT_PROGBITS:
1271 case ELF::SHT_STRTAB:
1272 case ELF::SHT_NOBITS:
1273 case ELF::SHT_NULL:
1274 case ELF::SHT_ARM_ATTRIBUTES:
1275 // Nothing to do.
1276 break;
1277
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001278 case ELF::SHT_GROUP: {
1279 sh_link = SymbolTableIndex;
1280 sh_info = GroupSymbolIndex;
1281 break;
1282 }
1283
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001284 default:
1285 assert(0 && "FIXME: sh_type value not supported!");
1286 break;
1287 }
1288
1289 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1290 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1291 Alignment, Section.getEntrySize());
1292}
1293
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001294void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1295 const MCAsmLayout &Layout) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001296 GroupMapTy GroupMap;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001297 RevGroupMapTy RevGroupMap;
1298 CreateGroupSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1299 RevGroupMap);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001300
Rafael Espindolabab2a802010-11-10 21:51:05 +00001301 SectionIndexMapTy SectionIndexMap;
1302
1303 ComputeIndexMap(Asm, SectionIndexMap);
1304
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001305 // Compute symbol table information.
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001306 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap);
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001307
Matt Fleming3565a062010-08-16 18:57:57 +00001308 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001309 const_cast<MCAsmLayout&>(Layout),
1310 SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +00001311
Rafael Espindola1d739a02010-11-10 22:34:07 +00001312 // Update to include the metadata sections.
1313 ComputeIndexMap(Asm, SectionIndexMap);
1314
Matt Fleming3565a062010-08-16 18:57:57 +00001315 // Add 1 for the null section.
1316 unsigned NumSections = Asm.size() + 1;
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001317 uint64_t NaturalAlignment = Is64Bit ? 8 : 4;
1318 uint64_t HeaderSize = Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr);
1319 uint64_t FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001320
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001321 std::vector<const MCSectionELF*> Sections;
1322 Sections.resize(NumSections);
1323
1324 for (SectionIndexMapTy::const_iterator i=
1325 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1326 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
1327 Sections[p.second] = p.first;
1328 }
1329
1330 for (unsigned i = 1; i < NumSections; ++i) {
1331 const MCSectionELF &Section = *Sections[i];
1332 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001333
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001334 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1335
Matt Fleming3565a062010-08-16 18:57:57 +00001336 // Get the size of the section in the output file (including padding).
1337 uint64_t Size = Layout.getSectionFileSize(&SD);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001338
1339 FileOff += Size;
Matt Fleming3565a062010-08-16 18:57:57 +00001340 }
1341
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001342 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1343
Matt Fleming3565a062010-08-16 18:57:57 +00001344 // Write out the ELF header ...
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001345 WriteHeader(FileOff - HeaderSize, NumSections);
1346
1347 FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001348
1349 // ... then all of the sections ...
1350 DenseMap<const MCSection*, uint64_t> SectionOffsetMap;
1351
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001352 for (unsigned i = 1; i < NumSections; ++i) {
1353 const MCSectionELF &Section = *Sections[i];
1354 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001355
1356 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1357 WriteZeros(Padding);
1358 FileOff += Padding;
1359
Matt Fleming3565a062010-08-16 18:57:57 +00001360 // Remember the offset into the file for this section.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001361 SectionOffsetMap[&Section] = FileOff;
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001362
Matt Fleming3565a062010-08-16 18:57:57 +00001363 FileOff += Layout.getSectionFileSize(&SD);
1364
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001365 Asm.WriteSectionData(&SD, Layout, this);
Matt Fleming3565a062010-08-16 18:57:57 +00001366 }
1367
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001368 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1369 WriteZeros(Padding);
1370 FileOff += Padding;
1371
Matt Fleming3565a062010-08-16 18:57:57 +00001372 // ... and then the section header table.
1373 // Should we align the section header table?
1374 //
1375 // Null section first.
Rafael Espindola7be2c332010-10-31 00:16:26 +00001376 uint64_t FirstSectionSize =
1377 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1378 uint32_t FirstSectionLink =
1379 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1380 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming3565a062010-08-16 18:57:57 +00001381
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001382 for (unsigned i = 1; i < NumSections; ++i) {
1383 const MCSectionELF &Section = *Sections[i];
1384 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1385 uint32_t GroupSymbolIndex;
1386 if (Section.getType() != ELF::SHT_GROUP)
1387 GroupSymbolIndex = 0;
1388 else
1389 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm, GroupMap[&Section]);
Matt Fleming3565a062010-08-16 18:57:57 +00001390
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001391 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
1392 SectionOffsetMap[&Section], Layout.getSectionSize(&SD),
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001393 SD.getAlignment(), Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001394 }
1395}
1396
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001397MCObjectWriter *llvm::createELFObjectWriter(raw_ostream &OS,
1398 bool Is64Bit,
1399 Triple::OSType OSType,
1400 uint16_t EMachine,
1401 bool IsLittleEndian,
1402 bool HasRelocationAddend) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001403 switch (EMachine) {
1404 case ELF::EM_386:
1405 case ELF::EM_X86_64:
1406 return new X86ELFObjectWriter(OS, Is64Bit, IsLittleEndian, EMachine,
1407 HasRelocationAddend, OSType); break;
1408 case ELF::EM_ARM:
1409 return new ARMELFObjectWriter(OS, Is64Bit, IsLittleEndian, EMachine,
1410 HasRelocationAddend, OSType); break;
Wesley Peck4b047132010-11-21 22:06:28 +00001411 case ELF::EM_MBLAZE:
1412 return new MBlazeELFObjectWriter(OS, Is64Bit, IsLittleEndian, EMachine,
1413 HasRelocationAddend, OSType); break;
Benjamin Kramer32858772010-11-15 19:20:50 +00001414 default: llvm_unreachable("Unsupported architecture"); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001415 }
1416}
1417
1418
1419/// START OF SUBCLASSES for ELFObjectWriter
1420//===- ARMELFObjectWriter -------------------------------------------===//
1421
1422ARMELFObjectWriter::ARMELFObjectWriter(raw_ostream &_OS, bool _Is64Bit,
1423 bool _IsLittleEndian,
1424 uint16_t _EMachine, bool _HasRelocationAddend,
1425 Triple::OSType _OSType)
1426 : ELFObjectWriter(_OS, _Is64Bit, _IsLittleEndian, _EMachine,
1427 _HasRelocationAddend, _OSType)
1428{}
1429
1430ARMELFObjectWriter::~ARMELFObjectWriter()
1431{}
1432
1433void ARMELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
1434 const MCAsmLayout &Layout,
1435 const MCFragment *Fragment,
1436 const MCFixup &Fixup,
1437 MCValue Target,
1438 uint64_t &FixedValue) {
1439 assert(0 && "ARMELFObjectWriter::RecordRelocation() unimplemented");
1440}
1441
Wesley Peck4b047132010-11-21 22:06:28 +00001442//===- MBlazeELFObjectWriter -------------------------------------------===//
Jason W Kimd3443e92010-11-15 16:18:39 +00001443
Wesley Peck4b047132010-11-21 22:06:28 +00001444MBlazeELFObjectWriter::MBlazeELFObjectWriter(raw_ostream &_OS, bool _Is64Bit,
1445 bool _IsLittleEndian,
1446 uint16_t _EMachine,
1447 bool _HasRelocationAddend,
1448 Triple::OSType _OSType)
1449 : ELFObjectWriter(_OS, _Is64Bit, _IsLittleEndian, _EMachine,
1450 _HasRelocationAddend, _OSType) {
1451}
1452
1453MBlazeELFObjectWriter::~MBlazeELFObjectWriter() {
1454}
1455
1456void MBlazeELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
1457 const MCAsmLayout &Layout,
1458 const MCFragment *Fragment,
1459 const MCFixup &Fixup,
1460 MCValue Target,
1461 uint64_t &FixedValue) {
1462 int64_t Addend = 0;
1463 int Index = 0;
1464 int64_t Value = Target.getConstant();
1465 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
1466 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
1467 const MCSymbol *RelocSymbol = SymbolToReloc(Asm, Target, *Fragment);
1468
Jason W Kim4a511f02010-11-22 18:41:13 +00001469 bool IsPCRel = isFixupKindPCRel(Fixup.getKind());
Wesley Peck4b047132010-11-21 22:06:28 +00001470 if (!Target.isAbsolute()) {
1471 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
1472 const MCSymbol &SymbolB = RefB->getSymbol();
1473 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
1474 IsPCRel = true;
1475 MCSectionData *Sec = Fragment->getParent();
1476
1477 // Offset of the symbol in the section
1478 int64_t a = Layout.getSymbolAddress(&SDB) - Layout.getSectionAddress(Sec);
1479
1480 // Ofeset of the relocation in the section
1481 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
1482 Value += b - a;
1483 }
1484
1485 if (!RelocSymbol) {
1486 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
1487 MCFragment *F = SD.getFragment();
1488
1489 Index = F->getParent()->getOrdinal();
1490
1491 MCSectionData *FSD = F->getParent();
1492 // Offset of the symbol in the section
1493 Value += Layout.getSymbolAddress(&SD) - Layout.getSectionAddress(FSD);
1494 } else {
1495 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
1496 WeakrefUsedInReloc.insert(RelocSymbol);
1497 else
1498 UsedInReloc.insert(RelocSymbol);
1499 Index = -1;
1500 }
1501 Addend = Value;
1502 }
1503
1504 FixedValue = Value;
1505
1506 // determine the type of the relocation
1507 unsigned Type;
1508 if (IsPCRel) {
1509 switch ((unsigned)Fixup.getKind()) {
1510 default:
1511 llvm_unreachable("Unimplemented");
1512 case MBlaze::reloc_pcrel_4byte:
1513 Type = ELF::R_MICROBLAZE_64_PCREL;
1514 break;
1515 case MBlaze::reloc_pcrel_2byte:
1516 Type = ELF::R_MICROBLAZE_32_PCREL;
1517 break;
1518 }
1519 } else {
1520 switch ((unsigned)Fixup.getKind()) {
1521 default: llvm_unreachable("invalid fixup kind!");
1522 case FK_Data_4:
1523 Type = (RelocSymbol || Addend !=0) ? ELF::R_MICROBLAZE_32
1524 : ELF::R_MICROBLAZE_64;
1525 break;
1526 case FK_Data_2:
1527 Type = ELF::R_MICROBLAZE_32;
1528 break;
1529 }
1530 }
1531
1532 MCSymbolRefExpr::VariantKind Modifier = Target.getSymA()->getKind();
1533 if (RelocNeedsGOT(Modifier))
1534 NeedsGOT = true;
1535
Jason W Kim858e7502010-11-22 18:42:07 +00001536 uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
Jason W Kim4a511f02010-11-22 18:41:13 +00001537 Fixup.getOffset();
Wesley Peck4b047132010-11-21 22:06:28 +00001538
Jason W Kim10907422010-11-22 22:05:16 +00001539 if (!HasRelocationAddend) Addend = 0;
Jason W Kim4a511f02010-11-22 18:41:13 +00001540 ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend);
Wesley Peck4b047132010-11-21 22:06:28 +00001541 Relocations[Fragment->getParent()].push_back(ERE);
1542}
Jason W Kimd3443e92010-11-15 16:18:39 +00001543
1544//===- X86ELFObjectWriter -------------------------------------------===//
1545
1546
1547X86ELFObjectWriter::X86ELFObjectWriter(raw_ostream &_OS, bool _Is64Bit,
1548 bool _IsLittleEndian,
1549 uint16_t _EMachine, bool _HasRelocationAddend,
1550 Triple::OSType _OSType)
1551 : ELFObjectWriter(_OS, _Is64Bit, _IsLittleEndian, _EMachine,
1552 _HasRelocationAddend, _OSType)
1553{}
1554
1555X86ELFObjectWriter::~X86ELFObjectWriter()
1556{}
1557
1558void X86ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
1559 const MCAsmLayout &Layout,
1560 const MCFragment *Fragment,
1561 const MCFixup &Fixup,
1562 MCValue Target,
1563 uint64_t &FixedValue) {
1564 int64_t Addend = 0;
1565 int Index = 0;
1566 int64_t Value = Target.getConstant();
Rafael Espindola12203cc2010-11-21 00:48:25 +00001567 const MCSymbol *RelocSymbol = NULL;
Jason W Kimd3443e92010-11-15 16:18:39 +00001568
Jason W Kim4a511f02010-11-22 18:41:13 +00001569 bool IsPCRel = isFixupKindPCRel(Fixup.getKind());
Jason W Kimd3443e92010-11-15 16:18:39 +00001570 if (!Target.isAbsolute()) {
Rafael Espindola12203cc2010-11-21 00:48:25 +00001571 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
1572 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
1573 RelocSymbol = SymbolToReloc(Asm, Target, *Fragment);
1574
Jason W Kimd3443e92010-11-15 16:18:39 +00001575 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
1576 const MCSymbol &SymbolB = RefB->getSymbol();
1577 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
1578 IsPCRel = true;
1579 MCSectionData *Sec = Fragment->getParent();
1580
1581 // Offset of the symbol in the section
1582 int64_t a = Layout.getSymbolAddress(&SDB) - Layout.getSectionAddress(Sec);
1583
1584 // Ofeset of the relocation in the section
1585 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
1586 Value += b - a;
1587 }
1588
1589 if (!RelocSymbol) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +00001590 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
Jason W Kimd3443e92010-11-15 16:18:39 +00001591 MCFragment *F = SD.getFragment();
1592
Rafael Espindola12203cc2010-11-21 00:48:25 +00001593 Index = F->getParent()->getOrdinal() + 1;
Jason W Kimd3443e92010-11-15 16:18:39 +00001594
1595 MCSectionData *FSD = F->getParent();
1596 // Offset of the symbol in the section
1597 Value += Layout.getSymbolAddress(&SD) - Layout.getSectionAddress(FSD);
1598 } else {
1599 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
1600 WeakrefUsedInReloc.insert(RelocSymbol);
1601 else
1602 UsedInReloc.insert(RelocSymbol);
1603 Index = -1;
1604 }
1605 Addend = Value;
1606 // Compensate for the addend on i386.
1607 if (Is64Bit)
1608 Value = 0;
1609 }
1610
1611 FixedValue = Value;
1612
1613 // determine the type of the relocation
1614
Rafael Espindola12203cc2010-11-21 00:48:25 +00001615 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1616 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
Jason W Kimd3443e92010-11-15 16:18:39 +00001617 unsigned Type;
1618 if (Is64Bit) {
1619 if (IsPCRel) {
1620 switch (Modifier) {
1621 default:
1622 llvm_unreachable("Unimplemented");
1623 case MCSymbolRefExpr::VK_None:
1624 Type = ELF::R_X86_64_PC32;
1625 break;
1626 case MCSymbolRefExpr::VK_PLT:
1627 Type = ELF::R_X86_64_PLT32;
1628 break;
1629 case MCSymbolRefExpr::VK_GOTPCREL:
1630 Type = ELF::R_X86_64_GOTPCREL;
1631 break;
1632 case MCSymbolRefExpr::VK_GOTTPOFF:
1633 Type = ELF::R_X86_64_GOTTPOFF;
1634 break;
1635 case MCSymbolRefExpr::VK_TLSGD:
1636 Type = ELF::R_X86_64_TLSGD;
1637 break;
1638 case MCSymbolRefExpr::VK_TLSLD:
1639 Type = ELF::R_X86_64_TLSLD;
1640 break;
1641 }
1642 } else {
1643 switch ((unsigned)Fixup.getKind()) {
1644 default: llvm_unreachable("invalid fixup kind!");
1645 case FK_Data_8: Type = ELF::R_X86_64_64; break;
1646 case X86::reloc_signed_4byte:
1647 case X86::reloc_pcrel_4byte:
1648 assert(isInt<32>(Target.getConstant()));
1649 switch (Modifier) {
1650 default:
1651 llvm_unreachable("Unimplemented");
1652 case MCSymbolRefExpr::VK_None:
1653 Type = ELF::R_X86_64_32S;
1654 break;
1655 case MCSymbolRefExpr::VK_GOT:
1656 Type = ELF::R_X86_64_GOT32;
1657 break;
1658 case MCSymbolRefExpr::VK_GOTPCREL:
1659 Type = ELF::R_X86_64_GOTPCREL;
1660 break;
1661 case MCSymbolRefExpr::VK_TPOFF:
1662 Type = ELF::R_X86_64_TPOFF32;
1663 break;
1664 case MCSymbolRefExpr::VK_DTPOFF:
1665 Type = ELF::R_X86_64_DTPOFF32;
1666 break;
1667 }
1668 break;
1669 case FK_Data_4:
1670 Type = ELF::R_X86_64_32;
1671 break;
1672 case FK_Data_2: Type = ELF::R_X86_64_16; break;
1673 case X86::reloc_pcrel_1byte:
1674 case FK_Data_1: Type = ELF::R_X86_64_8; break;
1675 }
1676 }
1677 } else {
1678 if (IsPCRel) {
1679 switch (Modifier) {
1680 default:
1681 llvm_unreachable("Unimplemented");
1682 case MCSymbolRefExpr::VK_None:
1683 Type = ELF::R_386_PC32;
1684 break;
1685 case MCSymbolRefExpr::VK_PLT:
1686 Type = ELF::R_386_PLT32;
1687 break;
1688 }
1689 } else {
1690 switch ((unsigned)Fixup.getKind()) {
1691 default: llvm_unreachable("invalid fixup kind!");
1692
1693 case X86::reloc_global_offset_table:
1694 Type = ELF::R_386_GOTPC;
1695 break;
1696
1697 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
1698 // instead?
1699 case X86::reloc_signed_4byte:
1700 case X86::reloc_pcrel_4byte:
1701 case FK_Data_4:
1702 switch (Modifier) {
1703 default:
1704 llvm_unreachable("Unimplemented");
1705 case MCSymbolRefExpr::VK_None:
1706 Type = ELF::R_386_32;
1707 break;
1708 case MCSymbolRefExpr::VK_GOT:
1709 Type = ELF::R_386_GOT32;
1710 break;
1711 case MCSymbolRefExpr::VK_GOTOFF:
1712 Type = ELF::R_386_GOTOFF;
1713 break;
1714 case MCSymbolRefExpr::VK_TLSGD:
1715 Type = ELF::R_386_TLS_GD;
1716 break;
1717 case MCSymbolRefExpr::VK_TPOFF:
1718 Type = ELF::R_386_TLS_LE_32;
1719 break;
1720 case MCSymbolRefExpr::VK_INDNTPOFF:
1721 Type = ELF::R_386_TLS_IE;
1722 break;
1723 case MCSymbolRefExpr::VK_NTPOFF:
1724 Type = ELF::R_386_TLS_LE;
1725 break;
1726 case MCSymbolRefExpr::VK_GOTNTPOFF:
1727 Type = ELF::R_386_TLS_GOTIE;
1728 break;
1729 case MCSymbolRefExpr::VK_TLSLDM:
1730 Type = ELF::R_386_TLS_LDM;
1731 break;
1732 case MCSymbolRefExpr::VK_DTPOFF:
1733 Type = ELF::R_386_TLS_LDO_32;
1734 break;
1735 }
1736 break;
1737 case FK_Data_2: Type = ELF::R_386_16; break;
1738 case X86::reloc_pcrel_1byte:
1739 case FK_Data_1: Type = ELF::R_386_8; break;
1740 }
1741 }
1742 }
1743
1744 if (RelocNeedsGOT(Modifier))
1745 NeedsGOT = true;
1746
Jason W Kimd3443e92010-11-15 16:18:39 +00001747
Jason W Kim858e7502010-11-22 18:42:07 +00001748 uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
Jason W Kim4a511f02010-11-22 18:41:13 +00001749 Fixup.getOffset();
Jason W Kimd3443e92010-11-15 16:18:39 +00001750
Jason W Kim10907422010-11-22 22:05:16 +00001751 if (!HasRelocationAddend) Addend = 0;
Jason W Kim4a511f02010-11-22 18:41:13 +00001752 ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend);
Jason W Kimd3443e92010-11-15 16:18:39 +00001753 Relocations[Fragment->getParent()].push_back(ERE);
Matt Fleming3565a062010-08-16 18:57:57 +00001754}