blob: 9b344f7728988ebd20697686f2eccbcd65313334 [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 Espindola31f35782010-12-17 18:01:31 +000014#include "llvm/ADT/OwningPtr.h"
Rafael Espindola8f413fa2010-10-05 15:11:03 +000015#include "llvm/ADT/SmallPtrSet.h"
Matt Fleming3565a062010-08-16 18:57:57 +000016#include "llvm/ADT/STLExtras.h"
17#include "llvm/ADT/StringMap.h"
18#include "llvm/ADT/Twine.h"
19#include "llvm/MC/MCAssembler.h"
20#include "llvm/MC/MCAsmLayout.h"
21#include "llvm/MC/MCContext.h"
22#include "llvm/MC/MCELFSymbolFlags.h"
23#include "llvm/MC/MCExpr.h"
Rafael Espindola285b3e52010-12-17 16:59:53 +000024#include "llvm/MC/MCELFObjectWriter.h"
Matt Fleming3565a062010-08-16 18:57:57 +000025#include "llvm/MC/MCObjectWriter.h"
26#include "llvm/MC/MCSectionELF.h"
27#include "llvm/MC/MCSymbol.h"
28#include "llvm/MC/MCValue.h"
29#include "llvm/Support/Debug.h"
30#include "llvm/Support/ErrorHandling.h"
31#include "llvm/Support/ELF.h"
32#include "llvm/Target/TargetAsmBackend.h"
33
34#include "../Target/X86/X86FixupKinds.h"
Jason W Kim4a511f02010-11-22 18:41:13 +000035#include "../Target/ARM/ARMFixupKinds.h"
Matt Fleming3565a062010-08-16 18:57:57 +000036
37#include <vector>
38using namespace llvm;
39
Rafael Espindolaad49cf52010-09-18 15:03:21 +000040static unsigned GetType(const MCSymbolData &SD) {
41 uint32_t Type = (SD.getFlags() & (0xf << ELF_STT_Shift)) >> ELF_STT_Shift;
42 assert(Type == ELF::STT_NOTYPE || Type == ELF::STT_OBJECT ||
43 Type == ELF::STT_FUNC || Type == ELF::STT_SECTION ||
44 Type == ELF::STT_FILE || Type == ELF::STT_COMMON ||
45 Type == ELF::STT_TLS);
46 return Type;
47}
48
Rafael Espindolae15eb4e2010-09-23 19:55:14 +000049static unsigned GetBinding(const MCSymbolData &SD) {
50 uint32_t Binding = (SD.getFlags() & (0xf << ELF_STB_Shift)) >> ELF_STB_Shift;
51 assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
52 Binding == ELF::STB_WEAK);
53 return Binding;
54}
55
56static void SetBinding(MCSymbolData &SD, unsigned Binding) {
57 assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
58 Binding == ELF::STB_WEAK);
59 uint32_t OtherFlags = SD.getFlags() & ~(0xf << ELF_STB_Shift);
60 SD.setFlags(OtherFlags | (Binding << ELF_STB_Shift));
61}
62
Rafael Espindola152c1062010-10-06 21:02:29 +000063static unsigned GetVisibility(MCSymbolData &SD) {
64 unsigned Visibility =
65 (SD.getFlags() & (0xf << ELF_STV_Shift)) >> ELF_STV_Shift;
66 assert(Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_INTERNAL ||
67 Visibility == ELF::STV_HIDDEN || Visibility == ELF::STV_PROTECTED);
68 return Visibility;
69}
70
Wesley Peck4b047132010-11-21 22:06:28 +000071
Rafael Espindolaa0a2f872010-10-28 14:22:44 +000072static bool RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant) {
73 switch (Variant) {
Rafael Espindola5c77c162010-10-05 15:48:37 +000074 default:
75 return false;
Rafael Espindolaa0a2f872010-10-28 14:22:44 +000076 case MCSymbolRefExpr::VK_GOT:
77 case MCSymbolRefExpr::VK_PLT:
78 case MCSymbolRefExpr::VK_GOTPCREL:
79 case MCSymbolRefExpr::VK_TPOFF:
80 case MCSymbolRefExpr::VK_TLSGD:
81 case MCSymbolRefExpr::VK_GOTTPOFF:
82 case MCSymbolRefExpr::VK_INDNTPOFF:
83 case MCSymbolRefExpr::VK_NTPOFF:
84 case MCSymbolRefExpr::VK_GOTNTPOFF:
Rafael Espindolaa264f722010-10-28 14:37:09 +000085 case MCSymbolRefExpr::VK_TLSLDM:
Rafael Espindola0cf15d62010-10-28 14:48:59 +000086 case MCSymbolRefExpr::VK_DTPOFF:
Rafael Espindolab4d17212010-10-28 15:02:40 +000087 case MCSymbolRefExpr::VK_TLSLD:
Rafael Espindola5c77c162010-10-05 15:48:37 +000088 return true;
89 }
90}
91
Rafael Espindola127a6a42010-12-17 07:28:17 +000092static bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) {
93 const MCFixupKindInfo &FKI =
94 Asm.getBackend().getFixupKindInfo((MCFixupKind) Kind);
95
96 return FKI.Flags & MCFixupKindInfo::FKF_IsPCRel;
97}
98
Matt Fleming3565a062010-08-16 18:57:57 +000099namespace {
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000100 class ELFObjectWriter : public MCObjectWriter {
Jason W Kimd3443e92010-11-15 16:18:39 +0000101 protected:
Chris Lattnerb188a372010-08-28 03:21:03 +0000102 /*static bool isFixupKindX86RIPRel(unsigned Kind) {
Matt Fleming3565a062010-08-16 18:57:57 +0000103 return Kind == X86::reloc_riprel_4byte ||
104 Kind == X86::reloc_riprel_4byte_movq_load;
Chris Lattnerb188a372010-08-28 03:21:03 +0000105 }*/
Matt Fleming3565a062010-08-16 18:57:57 +0000106
107
108 /// ELFSymbolData - Helper struct for containing some precomputed information
109 /// on symbols.
110 struct ELFSymbolData {
111 MCSymbolData *SymbolData;
112 uint64_t StringIndex;
113 uint32_t SectionIndex;
114
115 // Support lexicographic sorting.
116 bool operator<(const ELFSymbolData &RHS) const {
Rafael Espindolaad49cf52010-09-18 15:03:21 +0000117 if (GetType(*SymbolData) == ELF::STT_FILE)
118 return true;
119 if (GetType(*RHS.SymbolData) == ELF::STT_FILE)
120 return false;
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000121 return SymbolData->getSymbol().getName() <
122 RHS.SymbolData->getSymbol().getName();
Matt Fleming3565a062010-08-16 18:57:57 +0000123 }
124 };
125
126 /// @name Relocation Data
127 /// @{
128
129 struct ELFRelocationEntry {
130 // Make these big enough for both 32-bit and 64-bit
131 uint64_t r_offset;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000132 int Index;
133 unsigned Type;
134 const MCSymbol *Symbol;
Matt Fleming3565a062010-08-16 18:57:57 +0000135 uint64_t r_addend;
Jason W Kim858e7502010-11-22 18:42:07 +0000136
Jason W Kim4a511f02010-11-22 18:41:13 +0000137 ELFRelocationEntry()
138 : r_offset(0), Index(0), Type(0), Symbol(0), r_addend(0) {}
139
Jason W Kim10907422010-11-22 22:05:16 +0000140 ELFRelocationEntry(uint64_t RelocOffset, int Idx,
141 unsigned RelType, const MCSymbol *Sym,
Jason W Kim4a511f02010-11-22 18:41:13 +0000142 uint64_t Addend)
Jason W Kim10907422010-11-22 22:05:16 +0000143 : r_offset(RelocOffset), Index(Idx), Type(RelType),
144 Symbol(Sym), r_addend(Addend) {}
Matt Fleming3565a062010-08-16 18:57:57 +0000145
146 // Support lexicographic sorting.
147 bool operator<(const ELFRelocationEntry &RE) const {
148 return RE.r_offset < r_offset;
149 }
150 };
151
Rafael Espindola31f35782010-12-17 18:01:31 +0000152 /// The target specific ELF writer instance.
153 llvm::OwningPtr<MCELFObjectTargetWriter> TargetObjectWriter;
154
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000155 SmallPtrSet<const MCSymbol *, 16> UsedInReloc;
Rafael Espindola484291c2010-11-01 14:28:48 +0000156 SmallPtrSet<const MCSymbol *, 16> WeakrefUsedInReloc;
Rafael Espindola88182132010-10-27 15:18:17 +0000157 DenseMap<const MCSymbol *, const MCSymbol *> Renames;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000158
Matt Fleming3565a062010-08-16 18:57:57 +0000159 llvm::DenseMap<const MCSectionData*,
160 std::vector<ELFRelocationEntry> > Relocations;
161 DenseMap<const MCSection*, uint64_t> SectionStringTableIndex;
162
163 /// @}
164 /// @name Symbol Table Data
165 /// @{
166
167 SmallString<256> StringTable;
168 std::vector<ELFSymbolData> LocalSymbolData;
169 std::vector<ELFSymbolData> ExternalSymbolData;
170 std::vector<ELFSymbolData> UndefinedSymbolData;
171
172 /// @}
173
Rafael Espindola5c77c162010-10-05 15:48:37 +0000174 bool NeedsGOT;
175
Rafael Espindola7be2c332010-10-31 00:16:26 +0000176 bool NeedsSymtabShndx;
177
Matt Fleming3565a062010-08-16 18:57:57 +0000178 // This holds the symbol table index of the last local symbol.
179 unsigned LastLocalSymbolIndex;
180 // This holds the .strtab section index.
181 unsigned StringTableIndex;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000182 // This holds the .symtab section index.
183 unsigned SymbolTableIndex;
Matt Fleming3565a062010-08-16 18:57:57 +0000184
185 unsigned ShstrtabIndex;
186
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000187
188 const MCSymbol *SymbolToReloc(const MCAssembler &Asm,
189 const MCValue &Target,
190 const MCFragment &F) const;
191
Rafael Espindolabff66a82010-12-18 03:27:34 +0000192 bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
193 bool hasRelocationAddend() const {
194 return TargetObjectWriter->hasRelocationAddend();
195 }
196
Matt Fleming3565a062010-08-16 18:57:57 +0000197 public:
Rafael Espindola31f35782010-12-17 18:01:31 +0000198 ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +0000199 raw_ostream &_OS, bool IsLittleEndian)
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000200 : MCObjectWriter(_OS, IsLittleEndian),
Rafael Espindola31f35782010-12-17 18:01:31 +0000201 TargetObjectWriter(MOTW),
Rafael Espindolabff66a82010-12-18 03:27:34 +0000202 NeedsGOT(false), NeedsSymtabShndx(false){
Matt Fleming3565a062010-08-16 18:57:57 +0000203 }
Jason W Kimd3443e92010-11-15 16:18:39 +0000204
205 virtual ~ELFObjectWriter();
206
Matt Fleming3565a062010-08-16 18:57:57 +0000207 void WriteWord(uint64_t W) {
Rafael Espindolabff66a82010-12-18 03:27:34 +0000208 if (is64Bit())
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000209 Write64(W);
Chris Lattnerb188a372010-08-28 03:21:03 +0000210 else
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000211 Write32(W);
Matt Fleming3565a062010-08-16 18:57:57 +0000212 }
213
Matt Fleming3565a062010-08-16 18:57:57 +0000214 void StringLE16(char *buf, uint16_t Value) {
215 buf[0] = char(Value >> 0);
216 buf[1] = char(Value >> 8);
217 }
218
219 void StringLE32(char *buf, uint32_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000220 StringLE16(buf, uint16_t(Value >> 0));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000221 StringLE16(buf + 2, uint16_t(Value >> 16));
Matt Fleming3565a062010-08-16 18:57:57 +0000222 }
223
224 void StringLE64(char *buf, uint64_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000225 StringLE32(buf, uint32_t(Value >> 0));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000226 StringLE32(buf + 4, uint32_t(Value >> 32));
Matt Fleming3565a062010-08-16 18:57:57 +0000227 }
228
229 void StringBE16(char *buf ,uint16_t Value) {
230 buf[0] = char(Value >> 8);
231 buf[1] = char(Value >> 0);
232 }
233
234 void StringBE32(char *buf, uint32_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000235 StringBE16(buf, uint16_t(Value >> 16));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000236 StringBE16(buf + 2, uint16_t(Value >> 0));
Matt Fleming3565a062010-08-16 18:57:57 +0000237 }
238
239 void StringBE64(char *buf, uint64_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000240 StringBE32(buf, uint32_t(Value >> 32));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000241 StringBE32(buf + 4, uint32_t(Value >> 0));
Matt Fleming3565a062010-08-16 18:57:57 +0000242 }
243
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000244 void String8(MCDataFragment &F, uint8_t Value) {
245 char buf[1];
246 buf[0] = Value;
247 F.getContents() += StringRef(buf, 1);
248 }
249
250 void String16(MCDataFragment &F, uint16_t Value) {
251 char buf[2];
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000252 if (isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000253 StringLE16(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000254 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000255 StringBE16(buf, Value);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000256 F.getContents() += StringRef(buf, 2);
Matt Fleming3565a062010-08-16 18:57:57 +0000257 }
258
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000259 void String32(MCDataFragment &F, uint32_t Value) {
260 char buf[4];
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000261 if (isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000262 StringLE32(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000263 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000264 StringBE32(buf, Value);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000265 F.getContents() += StringRef(buf, 4);
Matt Fleming3565a062010-08-16 18:57:57 +0000266 }
267
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000268 void String64(MCDataFragment &F, uint64_t Value) {
269 char buf[8];
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000270 if (isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000271 StringLE64(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000272 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000273 StringBE64(buf, Value);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000274 F.getContents() += StringRef(buf, 8);
Matt Fleming3565a062010-08-16 18:57:57 +0000275 }
276
Jason W Kimd3443e92010-11-15 16:18:39 +0000277 virtual void WriteHeader(uint64_t SectionDataSize, unsigned NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000278
Jason W Kimd3443e92010-11-15 16:18:39 +0000279 virtual void WriteSymbolEntry(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
Rafael Espindola7be2c332010-10-31 00:16:26 +0000280 uint64_t name, uint8_t info,
Matt Fleming3565a062010-08-16 18:57:57 +0000281 uint64_t value, uint64_t size,
Rafael Espindola7be2c332010-10-31 00:16:26 +0000282 uint8_t other, uint32_t shndx,
283 bool Reserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000284
Jason W Kimd3443e92010-11-15 16:18:39 +0000285 virtual void WriteSymbol(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
Rafael Espindola7be2c332010-10-31 00:16:26 +0000286 ELFSymbolData &MSD,
Matt Fleming3565a062010-08-16 18:57:57 +0000287 const MCAsmLayout &Layout);
288
Rafael Espindolabab2a802010-11-10 21:51:05 +0000289 typedef DenseMap<const MCSectionELF*, uint32_t> SectionIndexMapTy;
Jason W Kimd3443e92010-11-15 16:18:39 +0000290 virtual void WriteSymbolTable(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
Rafael Espindola7be2c332010-10-31 00:16:26 +0000291 const MCAssembler &Asm,
Rafael Espindola71859c62010-09-16 19:46:31 +0000292 const MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000293 const SectionIndexMapTy &SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +0000294
Jason W Kimd3443e92010-11-15 16:18:39 +0000295 virtual void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
Jason W Kim56a39902010-12-06 21:57:34 +0000296 const MCFragment *Fragment, const MCFixup &Fixup,
297 MCValue Target, uint64_t &FixedValue);
Matt Fleming3565a062010-08-16 18:57:57 +0000298
Jason W Kimd3443e92010-11-15 16:18:39 +0000299 virtual uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm,
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000300 const MCSymbol *S);
Matt Fleming3565a062010-08-16 18:57:57 +0000301
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000302 // Map from a group section to the signature symbol
303 typedef DenseMap<const MCSectionELF*, const MCSymbol*> GroupMapTy;
304 // Map from a signature symbol to the group section
305 typedef DenseMap<const MCSymbol*, const MCSectionELF*> RevGroupMapTy;
306
Matt Fleming3565a062010-08-16 18:57:57 +0000307 /// ComputeSymbolTable - Compute the symbol table data
308 ///
309 /// \param StringTable [out] - The string table data.
310 /// \param StringIndexMap [out] - Map from symbol names to offsets in the
311 /// string table.
Jason W Kimd3443e92010-11-15 16:18:39 +0000312 virtual void ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000313 const SectionIndexMapTy &SectionIndexMap,
314 RevGroupMapTy RevGroupMap);
Rafael Espindolabab2a802010-11-10 21:51:05 +0000315
Jason W Kimd3443e92010-11-15 16:18:39 +0000316 virtual void ComputeIndexMap(MCAssembler &Asm,
Rafael Espindolabab2a802010-11-10 21:51:05 +0000317 SectionIndexMapTy &SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +0000318
Jason W Kimd3443e92010-11-15 16:18:39 +0000319 virtual void WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
Matt Fleming3565a062010-08-16 18:57:57 +0000320 const MCSectionData &SD);
321
Jason W Kimd3443e92010-11-15 16:18:39 +0000322 virtual void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout) {
Matt Fleming3565a062010-08-16 18:57:57 +0000323 for (MCAssembler::const_iterator it = Asm.begin(),
324 ie = Asm.end(); it != ie; ++it) {
325 WriteRelocation(Asm, Layout, *it);
326 }
327 }
328
Jason W Kimd3443e92010-11-15 16:18:39 +0000329 virtual void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000330 const SectionIndexMapTy &SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +0000331
Jason W Kimd3443e92010-11-15 16:18:39 +0000332 virtual void CreateGroupSections(MCAssembler &Asm, MCAsmLayout &Layout,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000333 GroupMapTy &GroupMap, RevGroupMapTy &RevGroupMap);
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000334
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000335 virtual void ExecutePostLayoutBinding(MCAssembler &Asm,
336 const MCAsmLayout &Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000337
Jason W Kimd3443e92010-11-15 16:18:39 +0000338 virtual void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
Matt Fleming3565a062010-08-16 18:57:57 +0000339 uint64_t Address, uint64_t Offset,
340 uint64_t Size, uint32_t Link, uint32_t Info,
341 uint64_t Alignment, uint64_t EntrySize);
342
Daniel Dunbar1f3662a2010-12-17 04:54:54 +0000343 virtual void WriteRelocationsFragment(const MCAssembler &Asm,
344 MCDataFragment *F,
345 const MCSectionData *SD);
346
Rafael Espindolafea753b2010-12-24 21:22:02 +0000347 virtual bool
348 IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
349 const MCSymbolData &DataA,
350 const MCFragment &FB,
351 bool InSet,
352 bool IsPCRel) const;
Rafael Espindola70703872010-09-30 02:22:20 +0000353
Jason W Kimd3443e92010-11-15 16:18:39 +0000354 virtual void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
355 virtual void WriteSection(MCAssembler &Asm,
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000356 const SectionIndexMapTy &SectionIndexMap,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000357 uint32_t GroupSymbolIndex,
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000358 uint64_t Offset, uint64_t Size, uint64_t Alignment,
359 const MCSectionELF &Section);
Jason W Kim56a39902010-12-06 21:57:34 +0000360
361 protected:
362 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
363 bool IsPCRel, bool IsRelocWithSymbol,
364 int64_t Addend) = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000365 };
366
Jason W Kimd3443e92010-11-15 16:18:39 +0000367 //===- X86ELFObjectWriter -------------------------------------------===//
368
369 class X86ELFObjectWriter : public ELFObjectWriter {
370 public:
Rafael Espindola31f35782010-12-17 18:01:31 +0000371 X86ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +0000372 raw_ostream &_OS,
373 bool IsLittleEndian);
Wesley Peck4b047132010-11-21 22:06:28 +0000374
Jason W Kimd3443e92010-11-15 16:18:39 +0000375 virtual ~X86ELFObjectWriter();
Jason W Kim56a39902010-12-06 21:57:34 +0000376 protected:
377 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
378 bool IsPCRel, bool IsRelocWithSymbol,
379 int64_t Addend);
Jason W Kimd3443e92010-11-15 16:18:39 +0000380 };
381
382
383 //===- ARMELFObjectWriter -------------------------------------------===//
384
385 class ARMELFObjectWriter : public ELFObjectWriter {
386 public:
Rafael Espindola31f35782010-12-17 18:01:31 +0000387 ARMELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +0000388 raw_ostream &_OS,
389 bool IsLittleEndian);
Wesley Peck4b047132010-11-21 22:06:28 +0000390
Jason W Kimd3443e92010-11-15 16:18:39 +0000391 virtual ~ARMELFObjectWriter();
Jason W Kim85fed5e2010-12-01 02:40:06 +0000392 protected:
Jason W Kim56a39902010-12-06 21:57:34 +0000393 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
394 bool IsPCRel, bool IsRelocWithSymbol,
395 int64_t Addend);
Jason W Kimd3443e92010-11-15 16:18:39 +0000396 };
Wesley Peck4b047132010-11-21 22:06:28 +0000397
398 //===- MBlazeELFObjectWriter -------------------------------------------===//
399
400 class MBlazeELFObjectWriter : public ELFObjectWriter {
401 public:
Rafael Espindola31f35782010-12-17 18:01:31 +0000402 MBlazeELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +0000403 raw_ostream &_OS,
404 bool IsLittleEndian);
Wesley Peck4b047132010-11-21 22:06:28 +0000405
406 virtual ~MBlazeELFObjectWriter();
Jason W Kim56a39902010-12-06 21:57:34 +0000407 protected:
408 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
409 bool IsPCRel, bool IsRelocWithSymbol,
410 int64_t Addend);
Wesley Peck4b047132010-11-21 22:06:28 +0000411 };
Matt Fleming3565a062010-08-16 18:57:57 +0000412}
413
Jason W Kimd3443e92010-11-15 16:18:39 +0000414ELFObjectWriter::~ELFObjectWriter()
415{}
416
Matt Fleming3565a062010-08-16 18:57:57 +0000417// Emit the ELF header.
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000418void ELFObjectWriter::WriteHeader(uint64_t SectionDataSize,
419 unsigned NumberOfSections) {
Matt Fleming3565a062010-08-16 18:57:57 +0000420 // ELF Header
421 // ----------
422 //
423 // Note
424 // ----
425 // emitWord method behaves differently for ELF32 and ELF64, writing
426 // 4 bytes in the former and 8 in the latter.
427
428 Write8(0x7f); // e_ident[EI_MAG0]
429 Write8('E'); // e_ident[EI_MAG1]
430 Write8('L'); // e_ident[EI_MAG2]
431 Write8('F'); // e_ident[EI_MAG3]
432
Rafael Espindolabff66a82010-12-18 03:27:34 +0000433 Write8(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
Matt Fleming3565a062010-08-16 18:57:57 +0000434
435 // e_ident[EI_DATA]
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000436 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
Matt Fleming3565a062010-08-16 18:57:57 +0000437
438 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky5baf79e2010-09-09 17:57:50 +0000439 // e_ident[EI_OSABI]
Rafael Espindolabff66a82010-12-18 03:27:34 +0000440 switch (TargetObjectWriter->getOSType()) {
Roman Divacky5baf79e2010-09-09 17:57:50 +0000441 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
442 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
443 default: Write8(ELF::ELFOSABI_NONE); break;
444 }
Matt Fleming3565a062010-08-16 18:57:57 +0000445 Write8(0); // e_ident[EI_ABIVERSION]
446
447 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
448
449 Write16(ELF::ET_REL); // e_type
450
Rafael Espindolabff66a82010-12-18 03:27:34 +0000451 Write16(TargetObjectWriter->getEMachine()); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +0000452
453 Write32(ELF::EV_CURRENT); // e_version
454 WriteWord(0); // e_entry, no entry point in .o file
455 WriteWord(0); // e_phoff, no program header for .o
Rafael Espindolabff66a82010-12-18 03:27:34 +0000456 WriteWord(SectionDataSize + (is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
Benjamin Kramereb976772010-08-17 17:02:29 +0000457 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000458
459 // FIXME: Make this configurable.
460 Write32(0); // e_flags = whatever the target wants
461
462 // e_ehsize = ELF header size
Rafael Espindolabff66a82010-12-18 03:27:34 +0000463 Write16(is64Bit() ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
Matt Fleming3565a062010-08-16 18:57:57 +0000464
465 Write16(0); // e_phentsize = prog header entry size
466 Write16(0); // e_phnum = # prog header entries = 0
467
468 // e_shentsize = Section header entry size
Rafael Espindolabff66a82010-12-18 03:27:34 +0000469 Write16(is64Bit() ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
Matt Fleming3565a062010-08-16 18:57:57 +0000470
471 // e_shnum = # of section header ents
Rafael Espindola7be2c332010-10-31 00:16:26 +0000472 if (NumberOfSections >= ELF::SHN_LORESERVE)
473 Write16(0);
474 else
475 Write16(NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000476
477 // e_shstrndx = Section # of '.shstrtab'
Rafael Espindola7be2c332010-10-31 00:16:26 +0000478 if (NumberOfSections >= ELF::SHN_LORESERVE)
479 Write16(ELF::SHN_XINDEX);
480 else
481 Write16(ShstrtabIndex);
Matt Fleming3565a062010-08-16 18:57:57 +0000482}
483
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000484void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
485 MCDataFragment *ShndxF,
486 uint64_t name,
487 uint8_t info, uint64_t value,
488 uint64_t size, uint8_t other,
489 uint32_t shndx,
490 bool Reserved) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000491 if (ShndxF) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000492 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000493 String32(*ShndxF, shndx);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000494 else
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000495 String32(*ShndxF, 0);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000496 }
497
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000498 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
499 uint16_t(ELF::SHN_XINDEX) : shndx;
500
Rafael Espindolabff66a82010-12-18 03:27:34 +0000501 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000502 String32(*SymtabF, name); // st_name
503 String8(*SymtabF, info); // st_info
504 String8(*SymtabF, other); // st_other
505 String16(*SymtabF, Index); // st_shndx
506 String64(*SymtabF, value); // st_value
507 String64(*SymtabF, size); // st_size
Matt Fleming3565a062010-08-16 18:57:57 +0000508 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000509 String32(*SymtabF, name); // st_name
510 String32(*SymtabF, value); // st_value
511 String32(*SymtabF, size); // st_size
512 String8(*SymtabF, info); // st_info
513 String8(*SymtabF, other); // st_other
514 String16(*SymtabF, Index); // st_shndx
Matt Fleming3565a062010-08-16 18:57:57 +0000515 }
516}
517
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000518static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout) {
519 if (Data.isCommon() && Data.isExternal())
520 return Data.getCommonAlignment();
521
522 const MCSymbol &Symbol = Data.getSymbol();
Roman Divackyd1491862010-12-20 21:14:39 +0000523
524 if (Symbol.isAbsolute() && Symbol.isVariable()) {
525 if (const MCExpr *Value = Symbol.getVariableValue()) {
526 int64_t IntValue;
527 if (Value->EvaluateAsAbsolute(IntValue, Layout))
528 return (uint64_t)IntValue;
529 }
530 }
531
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000532 if (!Symbol.isInSection())
533 return 0;
534
Rafael Espindolaffd902b2010-12-06 02:57:26 +0000535 if (Data.getFragment())
536 return Layout.getSymbolOffset(&Data);
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000537
538 return 0;
539}
540
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000541void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
542 const MCAsmLayout &Layout) {
Rafael Espindola88182132010-10-27 15:18:17 +0000543 // The presence of symbol versions causes undefined symbols and
544 // versions declared with @@@ to be renamed.
545
546 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
547 ie = Asm.symbol_end(); it != ie; ++it) {
548 const MCSymbol &Alias = it->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000549 const MCSymbol &Symbol = Alias.AliasedSymbol();
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000550 MCSymbolData &SD = Asm.getSymbolData(Symbol);
551
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000552 // Not an alias.
553 if (&Symbol == &Alias)
554 continue;
555
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000556 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000557 size_t Pos = AliasName.find('@');
558 if (Pos == StringRef::npos)
559 continue;
560
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000561 // Aliases defined with .symvar copy the binding from the symbol they alias.
562 // This is the first place we are able to copy this information.
563 it->setExternal(SD.isExternal());
564 SetBinding(*it, GetBinding(SD));
565
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000566 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000567 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
568 continue;
569
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000570 // FIXME: produce a better error message.
571 if (Symbol.isUndefined() && Rest.startswith("@@") &&
572 !Rest.startswith("@@@"))
573 report_fatal_error("A @@ version cannot be undefined");
574
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000575 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000576 }
577}
578
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000579void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
580 MCDataFragment *ShndxF,
581 ELFSymbolData &MSD,
582 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000583 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000584 MCSymbolData &Data =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000585 Layout.getAssembler().getSymbolData(OrigData.getSymbol().AliasedSymbol());
Rafael Espindola152c1062010-10-06 21:02:29 +0000586
Rafael Espindola7be2c332010-10-31 00:16:26 +0000587 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
588 Data.getSymbol().isVariable();
589
Rafael Espindola152c1062010-10-06 21:02:29 +0000590 uint8_t Binding = GetBinding(OrigData);
591 uint8_t Visibility = GetVisibility(OrigData);
592 uint8_t Type = GetType(Data);
593
594 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
595 uint8_t Other = Visibility;
596
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000597 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000598 uint64_t Size = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000599
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000600 assert(!(Data.isCommon() && !Data.isExternal()));
601
Rafael Espindolaf0121242010-12-22 16:03:00 +0000602 const MCExpr *ESize = Data.getSize();
603 if (ESize) {
604 int64_t Res;
605 if (!ESize->EvaluateAsAbsolute(Res, Layout))
606 report_fatal_error("Size expression must be absolute.");
607 Size = Res;
Matt Fleming3565a062010-08-16 18:57:57 +0000608 }
609
610 // Write out the symbol table entry
Rafael Espindola7be2c332010-10-31 00:16:26 +0000611 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
612 Size, Other, MSD.SectionIndex, IsReserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000613}
614
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000615void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
616 MCDataFragment *ShndxF,
617 const MCAssembler &Asm,
618 const MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000619 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000620 // The string table must be emitted first because we need the index
621 // into the string table for all the symbol names.
622 assert(StringTable.size() && "Missing string table");
623
624 // FIXME: Make sure the start of the symbol table is aligned.
625
626 // The first entry is the undefined symbol entry.
Rafael Espindola7be2c332010-10-31 00:16:26 +0000627 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
Matt Fleming3565a062010-08-16 18:57:57 +0000628
629 // Write the symbol table entries.
630 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
631 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
632 ELFSymbolData &MSD = LocalSymbolData[i];
Rafael Espindola7be2c332010-10-31 00:16:26 +0000633 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000634 }
635
Rafael Espindola71859c62010-09-16 19:46:31 +0000636 // Write out a symbol table entry for each regular section.
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000637 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
638 ++i) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000639 const MCSectionELF &Section =
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000640 static_cast<const MCSectionELF&>(i->getSection());
641 if (Section.getType() == ELF::SHT_RELA ||
642 Section.getType() == ELF::SHT_REL ||
643 Section.getType() == ELF::SHT_STRTAB ||
644 Section.getType() == ELF::SHT_SYMTAB)
Eli Friedmana44fa242010-08-16 21:17:09 +0000645 continue;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000646 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000647 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section), false);
Eli Friedmana44fa242010-08-16 21:17:09 +0000648 LastLocalSymbolIndex++;
649 }
Matt Fleming3565a062010-08-16 18:57:57 +0000650
651 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
652 ELFSymbolData &MSD = ExternalSymbolData[i];
653 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000654 assert(((Data.getFlags() & ELF_STB_Global) ||
655 (Data.getFlags() & ELF_STB_Weak)) &&
656 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000657 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000658 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000659 LastLocalSymbolIndex++;
660 }
661
662 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
663 ELFSymbolData &MSD = UndefinedSymbolData[i];
664 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000665 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000666 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000667 LastLocalSymbolIndex++;
668 }
669}
670
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000671const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm,
672 const MCValue &Target,
673 const MCFragment &F) const {
674 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000675 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
676 const MCSymbol *Renamed = Renames.lookup(&Symbol);
677 const MCSymbolData &SD = Asm.getSymbolData(Symbol);
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000678
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000679 if (ASymbol.isUndefined()) {
680 if (Renamed)
681 return Renamed;
682 return &ASymbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000683 }
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000684
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000685 if (SD.isExternal()) {
686 if (Renamed)
687 return Renamed;
688 return &Symbol;
689 }
Rafael Espindola73ffea42010-09-25 05:42:19 +0000690
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000691 const MCSectionELF &Section =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000692 static_cast<const MCSectionELF&>(ASymbol.getSection());
Rafael Espindola25958732010-11-24 21:57:39 +0000693 const SectionKind secKind = Section.getKind();
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000694
Rafael Espindola25958732010-11-24 21:57:39 +0000695 if (secKind.isBSS())
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000696 return NULL;
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000697
Rafael Espindola25958732010-11-24 21:57:39 +0000698 if (secKind.isThreadLocal()) {
699 if (Renamed)
700 return Renamed;
701 return &Symbol;
702 }
703
Rafael Espindola8cecf252010-10-06 16:23:36 +0000704 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000705 const MCSectionELF &Sec2 =
706 static_cast<const MCSectionELF&>(F.getParent()->getSection());
707
Rafael Espindola8cecf252010-10-06 16:23:36 +0000708 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000709 (Kind == MCSymbolRefExpr::VK_PLT ||
710 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
Rafael Espindola25958732010-11-24 21:57:39 +0000711 Kind == MCSymbolRefExpr::VK_GOTOFF)) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000712 if (Renamed)
713 return Renamed;
714 return &Symbol;
715 }
Rafael Espindola3729d002010-10-05 23:57:26 +0000716
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000717 if (Section.getFlags() & MCSectionELF::SHF_MERGE) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000718 if (Target.getConstant() == 0)
719 return NULL;
720 if (Renamed)
721 return Renamed;
722 return &Symbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000723 }
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000724
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000725 return NULL;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000726}
727
Matt Fleming3565a062010-08-16 18:57:57 +0000728
Jason W Kim56a39902010-12-06 21:57:34 +0000729void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
730 const MCAsmLayout &Layout,
731 const MCFragment *Fragment,
732 const MCFixup &Fixup,
733 MCValue Target,
734 uint64_t &FixedValue) {
735 int64_t Addend = 0;
736 int Index = 0;
737 int64_t Value = Target.getConstant();
738 const MCSymbol *RelocSymbol = NULL;
739
Rafael Espindola127a6a42010-12-17 07:28:17 +0000740 bool IsPCRel = isFixupKindPCRel(Asm, Fixup.getKind());
Jason W Kim56a39902010-12-06 21:57:34 +0000741 if (!Target.isAbsolute()) {
742 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
743 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
744 RelocSymbol = SymbolToReloc(Asm, Target, *Fragment);
745
746 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
747 const MCSymbol &SymbolB = RefB->getSymbol();
748 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
749 IsPCRel = true;
750
751 // Offset of the symbol in the section
752 int64_t a = Layout.getSymbolOffset(&SDB);
753
754 // Ofeset of the relocation in the section
755 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
756 Value += b - a;
757 }
758
759 if (!RelocSymbol) {
760 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
761 MCFragment *F = SD.getFragment();
762
763 Index = F->getParent()->getOrdinal() + 1;
764
765 // Offset of the symbol in the section
766 Value += Layout.getSymbolOffset(&SD);
767 } else {
768 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
769 WeakrefUsedInReloc.insert(RelocSymbol);
770 else
771 UsedInReloc.insert(RelocSymbol);
772 Index = -1;
773 }
774 Addend = Value;
775 // Compensate for the addend on i386.
Rafael Espindolabff66a82010-12-18 03:27:34 +0000776 if (is64Bit())
Jason W Kim56a39902010-12-06 21:57:34 +0000777 Value = 0;
778 }
779
780 FixedValue = Value;
781 unsigned Type = GetRelocType(Target, Fixup, IsPCRel,
782 (RelocSymbol != 0), Addend);
783
784 uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
785 Fixup.getOffset();
786
Rafael Espindolabff66a82010-12-18 03:27:34 +0000787 if (!hasRelocationAddend())
788 Addend = 0;
Jason W Kim56a39902010-12-06 21:57:34 +0000789 ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend);
790 Relocations[Fragment->getParent()].push_back(ERE);
791}
792
793
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000794uint64_t
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000795ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
796 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000797 MCSymbolData &SD = Asm.getSymbolData(*S);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000798 return SD.getIndex();
Matt Fleming3565a062010-08-16 18:57:57 +0000799}
800
Rafael Espindola737cd212010-10-05 18:01:23 +0000801static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
Rafael Espindola88182132010-10-27 15:18:17 +0000802 bool Used, bool Renamed) {
Rafael Espindola484291c2010-11-01 14:28:48 +0000803 if (Data.getFlags() & ELF_Other_Weakref)
804 return false;
805
Rafael Espindolabd701182010-10-19 19:31:37 +0000806 if (Used)
807 return true;
808
Rafael Espindola88182132010-10-27 15:18:17 +0000809 if (Renamed)
810 return false;
811
Rafael Espindola737cd212010-10-05 18:01:23 +0000812 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000813
Rafael Espindolad1798862010-10-29 23:09:31 +0000814 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
815 return true;
816
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000817 const MCSymbol &A = Symbol.AliasedSymbol();
Rafael Espindolad1798862010-10-29 23:09:31 +0000818 if (!A.isVariable() && A.isUndefined() && !Data.isCommon())
Rafael Espindolaa6866962010-10-27 14:44:52 +0000819 return false;
820
Rafael Espindola737cd212010-10-05 18:01:23 +0000821 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
822 return false;
823
Rafael Espindolabd701182010-10-19 19:31:37 +0000824 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000825 return false;
826
827 return true;
828}
829
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000830static bool isLocal(const MCSymbolData &Data, bool isSignature,
831 bool isUsedInReloc) {
Rafael Espindola737cd212010-10-05 18:01:23 +0000832 if (Data.isExternal())
833 return false;
834
835 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000836 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000837
838 if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
839 if (isSignature && !isUsedInReloc)
840 return true;
841
Rafael Espindola737cd212010-10-05 18:01:23 +0000842 return false;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000843 }
Rafael Espindola737cd212010-10-05 18:01:23 +0000844
845 return true;
846}
847
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000848void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
849 SectionIndexMapTy &SectionIndexMap) {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000850 unsigned Index = 1;
851 for (MCAssembler::iterator it = Asm.begin(),
852 ie = Asm.end(); it != ie; ++it) {
853 const MCSectionELF &Section =
854 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000855 if (Section.getType() != ELF::SHT_GROUP)
856 continue;
857 SectionIndexMap[&Section] = Index++;
858 }
859
860 for (MCAssembler::iterator it = Asm.begin(),
861 ie = Asm.end(); it != ie; ++it) {
862 const MCSectionELF &Section =
863 static_cast<const MCSectionELF &>(it->getSection());
864 if (Section.getType() == ELF::SHT_GROUP)
865 continue;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000866 SectionIndexMap[&Section] = Index++;
867 }
868}
869
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000870void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000871 const SectionIndexMapTy &SectionIndexMap,
872 RevGroupMapTy RevGroupMap) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000873 // FIXME: Is this the correct place to do this?
874 if (NeedsGOT) {
875 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
876 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
877 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
878 Data.setExternal(true);
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000879 SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindola5c77c162010-10-05 15:48:37 +0000880 }
881
Matt Fleming3565a062010-08-16 18:57:57 +0000882 // Build section lookup table.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000883 int NumRegularSections = Asm.size();
Matt Fleming3565a062010-08-16 18:57:57 +0000884
885 // Index 0 is always the empty string.
886 StringMap<uint64_t> StringIndexMap;
887 StringTable += '\x00';
888
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000889 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000890 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
891 ie = Asm.symbol_end(); it != ie; ++it) {
892 const MCSymbol &Symbol = it->getSymbol();
893
Rafael Espindola484291c2010-11-01 14:28:48 +0000894 bool Used = UsedInReloc.count(&Symbol);
895 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000896 bool isSignature = RevGroupMap.count(&Symbol);
897
898 if (!isInSymtab(Asm, *it,
899 Used || WeakrefUsed || isSignature,
Rafael Espindola88182132010-10-27 15:18:17 +0000900 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000901 continue;
902
Matt Fleming3565a062010-08-16 18:57:57 +0000903 ELFSymbolData MSD;
904 MSD.SymbolData = it;
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000905 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Matt Fleming3565a062010-08-16 18:57:57 +0000906
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000907 // Undefined symbols are global, but this is the first place we
908 // are able to set it.
909 bool Local = isLocal(*it, isSignature, Used);
910 if (!Local && GetBinding(*it) == ELF::STB_LOCAL) {
911 MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
912 SetBinding(*it, ELF::STB_GLOBAL);
913 SetBinding(SD, ELF::STB_GLOBAL);
914 }
915
Rafael Espindola484291c2010-11-01 14:28:48 +0000916 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
917 SetBinding(*it, ELF::STB_WEAK);
918
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000919 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000920 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000921 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000922 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000923 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000924 } else if (RefSymbol.isUndefined()) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000925 if (isSignature && !Used)
926 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
927 else
928 MSD.SectionIndex = ELF::SHN_UNDEF;
Matt Fleming3565a062010-08-16 18:57:57 +0000929 } else {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000930 const MCSectionELF &Section =
931 static_cast<const MCSectionELF&>(RefSymbol.getSection());
932 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000933 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
934 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000935 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000936 }
937
Rafael Espindola88182132010-10-27 15:18:17 +0000938 // The @@@ in symbol version is replaced with @ in undefined symbols and
939 // @@ in defined ones.
940 StringRef Name = Symbol.getName();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000941 SmallString<32> Buf;
942
Rafael Espindola88182132010-10-27 15:18:17 +0000943 size_t Pos = Name.find("@@@");
Rafael Espindola88182132010-10-27 15:18:17 +0000944 if (Pos != StringRef::npos) {
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000945 Buf += Name.substr(0, Pos);
946 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
947 Buf += Name.substr(Pos + Skip);
948 Name = Buf;
Rafael Espindola88182132010-10-27 15:18:17 +0000949 }
950
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000951 uint64_t &Entry = StringIndexMap[Name];
Rafael Espindolaa6866962010-10-27 14:44:52 +0000952 if (!Entry) {
953 Entry = StringTable.size();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000954 StringTable += Name;
Rafael Espindolaa6866962010-10-27 14:44:52 +0000955 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +0000956 }
Rafael Espindolaa6866962010-10-27 14:44:52 +0000957 MSD.StringIndex = Entry;
958 if (MSD.SectionIndex == ELF::SHN_UNDEF)
959 UndefinedSymbolData.push_back(MSD);
960 else if (Local)
961 LocalSymbolData.push_back(MSD);
962 else
963 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +0000964 }
965
966 // Symbols are required to be in lexicographic order.
967 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
968 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
969 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
970
971 // Set the symbol indices. Local symbols must come before all other
972 // symbols with non-local bindings.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000973 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000974 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
975 LocalSymbolData[i].SymbolData->setIndex(Index++);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000976
977 Index += NumRegularSections;
978
Matt Fleming3565a062010-08-16 18:57:57 +0000979 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
980 ExternalSymbolData[i].SymbolData->setIndex(Index++);
981 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
982 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
983}
984
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000985void ELFObjectWriter::WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
986 const MCSectionData &SD) {
Matt Fleming3565a062010-08-16 18:57:57 +0000987 if (!Relocations[&SD].empty()) {
988 MCContext &Ctx = Asm.getContext();
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000989 const MCSectionELF *RelaSection;
Matt Fleming3565a062010-08-16 18:57:57 +0000990 const MCSectionELF &Section =
991 static_cast<const MCSectionELF&>(SD.getSection());
992
993 const StringRef SectionName = Section.getSectionName();
Rafael Espindolabff66a82010-12-18 03:27:34 +0000994 std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +0000995 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000996
997 unsigned EntrySize;
Rafael Espindolabff66a82010-12-18 03:27:34 +0000998 if (hasRelocationAddend())
999 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
Benjamin Kramer299fbe32010-08-17 17:56:13 +00001000 else
Rafael Espindolabff66a82010-12-18 03:27:34 +00001001 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +00001002
Rafael Espindolabff66a82010-12-18 03:27:34 +00001003 RelaSection = Ctx.getELFSection(RelaSectionName, hasRelocationAddend() ?
Benjamin Kramer377a5722010-08-17 17:30:07 +00001004 ELF::SHT_RELA : ELF::SHT_REL, 0,
Matt Fleming3565a062010-08-16 18:57:57 +00001005 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001006 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001007
1008 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +00001009 RelaSD.setAlignment(is64Bit() ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +00001010
1011 MCDataFragment *F = new MCDataFragment(&RelaSD);
1012
1013 WriteRelocationsFragment(Asm, F, &SD);
Matt Fleming3565a062010-08-16 18:57:57 +00001014 }
1015}
1016
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001017void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
1018 uint64_t Flags, uint64_t Address,
1019 uint64_t Offset, uint64_t Size,
1020 uint32_t Link, uint32_t Info,
1021 uint64_t Alignment,
1022 uint64_t EntrySize) {
Matt Fleming3565a062010-08-16 18:57:57 +00001023 Write32(Name); // sh_name: index into string table
1024 Write32(Type); // sh_type
1025 WriteWord(Flags); // sh_flags
1026 WriteWord(Address); // sh_addr
1027 WriteWord(Offset); // sh_offset
1028 WriteWord(Size); // sh_size
1029 Write32(Link); // sh_link
1030 Write32(Info); // sh_info
1031 WriteWord(Alignment); // sh_addralign
1032 WriteWord(EntrySize); // sh_entsize
1033}
1034
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001035void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
1036 MCDataFragment *F,
1037 const MCSectionData *SD) {
Matt Fleming3565a062010-08-16 18:57:57 +00001038 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
1039 // sort by the r_offset just like gnu as does
1040 array_pod_sort(Relocs.begin(), Relocs.end());
1041
1042 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1043 ELFRelocationEntry entry = Relocs[e - i - 1];
1044
Rafael Espindola12203cc2010-11-21 00:48:25 +00001045 if (!entry.Index)
1046 ;
1047 else if (entry.Index < 0)
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001048 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
1049 else
Rafael Espindola12203cc2010-11-21 00:48:25 +00001050 entry.Index += LocalSymbolData.size();
Rafael Espindolabff66a82010-12-18 03:27:34 +00001051 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001052 String64(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001053
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001054 struct ELF::Elf64_Rela ERE64;
1055 ERE64.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001056 String64(*F, ERE64.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001057
Rafael Espindolabff66a82010-12-18 03:27:34 +00001058 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001059 String64(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001060 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001061 String32(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001062
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001063 struct ELF::Elf32_Rela ERE32;
1064 ERE32.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001065 String32(*F, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001066
Rafael Espindolabff66a82010-12-18 03:27:34 +00001067 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001068 String32(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001069 }
Matt Fleming3565a062010-08-16 18:57:57 +00001070 }
1071}
1072
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001073void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
1074 MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001075 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +00001076 MCContext &Ctx = Asm.getContext();
1077 MCDataFragment *F;
1078
Rafael Espindolabff66a82010-12-18 03:27:34 +00001079 unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
Matt Fleming3565a062010-08-16 18:57:57 +00001080
Rafael Espindola38738bf2010-09-22 19:04:41 +00001081 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001082 const MCSectionELF *ShstrtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001083 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001084 SectionKind::getReadOnly());
Rafael Espindola71859c62010-09-16 19:46:31 +00001085 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1086 ShstrtabSD.setAlignment(1);
1087 ShstrtabIndex = Asm.size();
1088
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001089 const MCSectionELF *SymtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001090 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1091 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001092 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001093 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +00001094 SymtabSD.setAlignment(is64Bit() ? 8 : 4);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001095 SymbolTableIndex = Asm.size();
1096
1097 MCSectionData *SymtabShndxSD = NULL;
1098
1099 if (NeedsSymtabShndx) {
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001100 const MCSectionELF *SymtabShndxSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001101 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001102 SectionKind::getReadOnly(), 4, "");
Rafael Espindola7be2c332010-10-31 00:16:26 +00001103 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
1104 SymtabShndxSD->setAlignment(4);
1105 }
Matt Fleming3565a062010-08-16 18:57:57 +00001106
Matt Fleming3565a062010-08-16 18:57:57 +00001107 const MCSection *StrtabSection;
1108 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001109 SectionKind::getReadOnly());
Matt Fleming3565a062010-08-16 18:57:57 +00001110 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1111 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +00001112 StringTableIndex = Asm.size();
1113
Rafael Espindolac3c413f2010-09-27 22:04:54 +00001114 WriteRelocations(Asm, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001115
1116 // Symbol table
1117 F = new MCDataFragment(&SymtabSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001118 MCDataFragment *ShndxF = NULL;
1119 if (NeedsSymtabShndx) {
1120 ShndxF = new MCDataFragment(SymtabShndxSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001121 }
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001122 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
Rafael Espindola71859c62010-09-16 19:46:31 +00001123
Matt Fleming3565a062010-08-16 18:57:57 +00001124 F = new MCDataFragment(&StrtabSD);
1125 F->getContents().append(StringTable.begin(), StringTable.end());
Matt Fleming3565a062010-08-16 18:57:57 +00001126
Matt Fleming3565a062010-08-16 18:57:57 +00001127 F = new MCDataFragment(&ShstrtabSD);
1128
Matt Fleming3565a062010-08-16 18:57:57 +00001129 // Section header string table.
1130 //
1131 // The first entry of a string table holds a null character so skip
1132 // section 0.
1133 uint64_t Index = 1;
1134 F->getContents() += '\x00';
1135
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001136 StringMap<uint64_t> SecStringMap;
Matt Fleming3565a062010-08-16 18:57:57 +00001137 for (MCAssembler::const_iterator it = Asm.begin(),
1138 ie = Asm.end(); it != ie; ++it) {
Matt Fleming3565a062010-08-16 18:57:57 +00001139 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +00001140 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola51efe7a2010-09-23 14:14:56 +00001141 // FIXME: We could merge suffixes like in .text and .rela.text.
Matt Fleming3565a062010-08-16 18:57:57 +00001142
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001143 StringRef Name = Section.getSectionName();
1144 if (SecStringMap.count(Name)) {
1145 SectionStringTableIndex[&Section] = SecStringMap[Name];
1146 continue;
1147 }
Matt Fleming3565a062010-08-16 18:57:57 +00001148 // Remember the index into the string table so we can write it
1149 // into the sh_name field of the section header table.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001150 SectionStringTableIndex[&Section] = Index;
1151 SecStringMap[Name] = Index;
Matt Fleming3565a062010-08-16 18:57:57 +00001152
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001153 Index += Name.size() + 1;
1154 F->getContents() += Name;
Matt Fleming3565a062010-08-16 18:57:57 +00001155 F->getContents() += '\x00';
1156 }
Rafael Espindola70703872010-09-30 02:22:20 +00001157}
1158
Rafael Espindolafea753b2010-12-24 21:22:02 +00001159bool
1160ELFObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
1161 const MCSymbolData &DataA,
1162 const MCFragment &FB,
1163 bool InSet,
1164 bool IsPCRel) const {
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 if (DataA.isExternal())
1169 return false;
1170
Rafael Espindolafea753b2010-12-24 21:22:02 +00001171 const MCSection &SecA = DataA.getSymbol().AliasedSymbol().getSection();
1172 const MCSection &SecB = FB.getParent()->getSection();
1173 // On ELF A - B is absolute if A and B are in the same section.
1174 return &SecA == &SecB;
Matt Fleming3565a062010-08-16 18:57:57 +00001175}
1176
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001177void ELFObjectWriter::CreateGroupSections(MCAssembler &Asm,
1178 MCAsmLayout &Layout,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001179 GroupMapTy &GroupMap,
1180 RevGroupMapTy &RevGroupMap) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001181 // Build the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001182 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1183 it != ie; ++it) {
1184 const MCSectionELF &Section =
1185 static_cast<const MCSectionELF&>(it->getSection());
1186 if (!(Section.getFlags() & MCSectionELF::SHF_GROUP))
1187 continue;
1188
1189 const MCSymbol *SignatureSymbol = Section.getGroup();
1190 Asm.getOrCreateSymbolData(*SignatureSymbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001191 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001192 if (!Group) {
1193 Group = Asm.getContext().CreateELFGroupSection();
1194 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1195 Data.setAlignment(4);
1196 MCDataFragment *F = new MCDataFragment(&Data);
1197 String32(*F, ELF::GRP_COMDAT);
1198 }
1199 GroupMap[Group] = SignatureSymbol;
1200 }
1201
1202 // Add sections to the groups
1203 unsigned Index = 1;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001204 unsigned NumGroups = RevGroupMap.size();
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001205 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1206 it != ie; ++it, ++Index) {
1207 const MCSectionELF &Section =
1208 static_cast<const MCSectionELF&>(it->getSection());
1209 if (!(Section.getFlags() & MCSectionELF::SHF_GROUP))
1210 continue;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001211 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001212 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1213 // FIXME: we could use the previous fragment
1214 MCDataFragment *F = new MCDataFragment(&Data);
1215 String32(*F, NumGroups + Index);
1216 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001217}
1218
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001219void ELFObjectWriter::WriteSection(MCAssembler &Asm,
1220 const SectionIndexMapTy &SectionIndexMap,
1221 uint32_t GroupSymbolIndex,
1222 uint64_t Offset, uint64_t Size,
1223 uint64_t Alignment,
1224 const MCSectionELF &Section) {
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001225 uint64_t sh_link = 0;
1226 uint64_t sh_info = 0;
1227
1228 switch(Section.getType()) {
1229 case ELF::SHT_DYNAMIC:
1230 sh_link = SectionStringTableIndex[&Section];
1231 sh_info = 0;
1232 break;
1233
1234 case ELF::SHT_REL:
1235 case ELF::SHT_RELA: {
1236 const MCSectionELF *SymtabSection;
1237 const MCSectionELF *InfoSection;
1238 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
1239 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001240 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001241 sh_link = SectionIndexMap.lookup(SymtabSection);
1242 assert(sh_link && ".symtab not found");
1243
1244 // Remove ".rel" and ".rela" prefixes.
1245 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1246 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
1247
1248 InfoSection = Asm.getContext().getELFSection(SectionName,
1249 ELF::SHT_PROGBITS, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001250 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001251 sh_info = SectionIndexMap.lookup(InfoSection);
1252 break;
1253 }
1254
1255 case ELF::SHT_SYMTAB:
1256 case ELF::SHT_DYNSYM:
1257 sh_link = StringTableIndex;
1258 sh_info = LastLocalSymbolIndex;
1259 break;
1260
1261 case ELF::SHT_SYMTAB_SHNDX:
1262 sh_link = SymbolTableIndex;
1263 break;
1264
1265 case ELF::SHT_PROGBITS:
1266 case ELF::SHT_STRTAB:
1267 case ELF::SHT_NOBITS:
Rafael Espindola98976612010-12-26 21:30:59 +00001268 case ELF::SHT_NOTE:
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001269 case ELF::SHT_NULL:
1270 case ELF::SHT_ARM_ATTRIBUTES:
Jason W Kim86a97f22011-01-12 00:19:25 +00001271 case ELF::SHT_INIT_ARRAY:
1272 case ELF::SHT_FINI_ARRAY:
1273 case ELF::SHT_PREINIT_ARRAY:
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001274 // Nothing to do.
1275 break;
1276
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001277 case ELF::SHT_GROUP: {
1278 sh_link = SymbolTableIndex;
1279 sh_info = GroupSymbolIndex;
1280 break;
1281 }
1282
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001283 default:
1284 assert(0 && "FIXME: sh_type value not supported!");
1285 break;
1286 }
1287
1288 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1289 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1290 Alignment, Section.getEntrySize());
1291}
1292
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001293static bool IsELFMetaDataSection(const MCSectionData &SD) {
Rafael Espindolaf8803fe2010-12-06 03:48:09 +00001294 return SD.getOrdinal() == ~UINT32_C(0) &&
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001295 !SD.getSection().isVirtualSection();
1296}
1297
1298static uint64_t DataSectionSize(const MCSectionData &SD) {
1299 uint64_t Ret = 0;
1300 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1301 ++i) {
1302 const MCFragment &F = *i;
1303 assert(F.getKind() == MCFragment::FT_Data);
1304 Ret += cast<MCDataFragment>(F).getContents().size();
1305 }
1306 return Ret;
1307}
1308
1309static uint64_t GetSectionFileSize(const MCAsmLayout &Layout,
1310 const MCSectionData &SD) {
1311 if (IsELFMetaDataSection(SD))
1312 return DataSectionSize(SD);
1313 return Layout.getSectionFileSize(&SD);
1314}
1315
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001316static uint64_t GetSectionAddressSize(const MCAsmLayout &Layout,
1317 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001318 if (IsELFMetaDataSection(SD))
1319 return DataSectionSize(SD);
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001320 return Layout.getSectionAddressSize(&SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001321}
1322
1323static void WriteDataSectionData(ELFObjectWriter *W, const MCSectionData &SD) {
1324 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1325 ++i) {
1326 const MCFragment &F = *i;
1327 assert(F.getKind() == MCFragment::FT_Data);
1328 W->WriteBytes(cast<MCDataFragment>(F).getContents().str());
1329 }
1330}
1331
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001332void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1333 const MCAsmLayout &Layout) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001334 GroupMapTy GroupMap;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001335 RevGroupMapTy RevGroupMap;
1336 CreateGroupSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1337 RevGroupMap);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001338
Rafael Espindolabab2a802010-11-10 21:51:05 +00001339 SectionIndexMapTy SectionIndexMap;
1340
1341 ComputeIndexMap(Asm, SectionIndexMap);
1342
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001343 // Compute symbol table information.
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001344 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap);
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001345
Matt Fleming3565a062010-08-16 18:57:57 +00001346 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001347 const_cast<MCAsmLayout&>(Layout),
1348 SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +00001349
Rafael Espindola1d739a02010-11-10 22:34:07 +00001350 // Update to include the metadata sections.
1351 ComputeIndexMap(Asm, SectionIndexMap);
1352
Matt Fleming3565a062010-08-16 18:57:57 +00001353 // Add 1 for the null section.
1354 unsigned NumSections = Asm.size() + 1;
Rafael Espindolabff66a82010-12-18 03:27:34 +00001355 uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
1356 uint64_t HeaderSize = is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
1357 sizeof(ELF::Elf32_Ehdr);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001358 uint64_t FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001359
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001360 std::vector<const MCSectionELF*> Sections;
1361 Sections.resize(NumSections);
1362
1363 for (SectionIndexMapTy::const_iterator i=
1364 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1365 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
1366 Sections[p.second] = p.first;
1367 }
1368
1369 for (unsigned i = 1; i < NumSections; ++i) {
1370 const MCSectionELF &Section = *Sections[i];
1371 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001372
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001373 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1374
Matt Fleming3565a062010-08-16 18:57:57 +00001375 // Get the size of the section in the output file (including padding).
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001376 FileOff += GetSectionFileSize(Layout, SD);
Matt Fleming3565a062010-08-16 18:57:57 +00001377 }
1378
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001379 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1380
Matt Fleming3565a062010-08-16 18:57:57 +00001381 // Write out the ELF header ...
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001382 WriteHeader(FileOff - HeaderSize, NumSections);
1383
1384 FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001385
1386 // ... then all of the sections ...
1387 DenseMap<const MCSection*, uint64_t> SectionOffsetMap;
1388
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001389 for (unsigned i = 1; i < NumSections; ++i) {
1390 const MCSectionELF &Section = *Sections[i];
1391 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001392
1393 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1394 WriteZeros(Padding);
1395 FileOff += Padding;
1396
Matt Fleming3565a062010-08-16 18:57:57 +00001397 // Remember the offset into the file for this section.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001398 SectionOffsetMap[&Section] = FileOff;
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001399
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001400 FileOff += GetSectionFileSize(Layout, SD);
Matt Fleming3565a062010-08-16 18:57:57 +00001401
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001402 if (IsELFMetaDataSection(SD))
1403 WriteDataSectionData(this, SD);
1404 else
Daniel Dunbar5d2477c2010-12-17 02:45:59 +00001405 Asm.WriteSectionData(&SD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +00001406 }
1407
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001408 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1409 WriteZeros(Padding);
1410 FileOff += Padding;
1411
Matt Fleming3565a062010-08-16 18:57:57 +00001412 // ... and then the section header table.
1413 // Should we align the section header table?
1414 //
1415 // Null section first.
Rafael Espindola7be2c332010-10-31 00:16:26 +00001416 uint64_t FirstSectionSize =
1417 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1418 uint32_t FirstSectionLink =
1419 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1420 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming3565a062010-08-16 18:57:57 +00001421
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001422 for (unsigned i = 1; i < NumSections; ++i) {
1423 const MCSectionELF &Section = *Sections[i];
1424 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1425 uint32_t GroupSymbolIndex;
1426 if (Section.getType() != ELF::SHT_GROUP)
1427 GroupSymbolIndex = 0;
1428 else
1429 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm, GroupMap[&Section]);
Matt Fleming3565a062010-08-16 18:57:57 +00001430
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001431 uint64_t Size = GetSectionAddressSize(Layout, SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001432
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001433 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001434 SectionOffsetMap[&Section], Size,
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001435 SD.getAlignment(), Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001436 }
1437}
1438
Rafael Espindola6024c972010-12-17 17:45:22 +00001439MCObjectWriter *llvm::createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1440 raw_ostream &OS,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001441 bool IsLittleEndian) {
1442 switch (MOTW->getEMachine()) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001443 case ELF::EM_386:
1444 case ELF::EM_X86_64:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001445 return new X86ELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001446 case ELF::EM_ARM:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001447 return new ARMELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Wesley Peck4b047132010-11-21 22:06:28 +00001448 case ELF::EM_MBLAZE:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001449 return new MBlazeELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Benjamin Kramer32858772010-11-15 19:20:50 +00001450 default: llvm_unreachable("Unsupported architecture"); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001451 }
1452}
1453
1454
1455/// START OF SUBCLASSES for ELFObjectWriter
1456//===- ARMELFObjectWriter -------------------------------------------===//
1457
Rafael Espindola31f35782010-12-17 18:01:31 +00001458ARMELFObjectWriter::ARMELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001459 raw_ostream &_OS,
1460 bool IsLittleEndian)
1461 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
Jason W Kimd3443e92010-11-15 16:18:39 +00001462{}
1463
1464ARMELFObjectWriter::~ARMELFObjectWriter()
1465{}
1466
Jason W Kim85fed5e2010-12-01 02:40:06 +00001467unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target,
1468 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001469 bool IsPCRel,
1470 bool IsRelocWithSymbol,
1471 int64_t Addend) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001472 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1473 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
1474
Jason W Kima0871e72010-12-08 23:14:44 +00001475 unsigned Type = 0;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001476 if (IsPCRel) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001477 switch ((unsigned)Fixup.getKind()) {
1478 default: assert(0 && "Unimplemented");
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001479 case FK_Data_4:
1480 switch (Modifier) {
1481 default: llvm_unreachable("Unsupported Modifier");
1482 case MCSymbolRefExpr::VK_None:
Jason W Kim1d866172011-01-13 00:07:51 +00001483 Type = ELF::R_ARM_BASE_PREL;
1484 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001485 case MCSymbolRefExpr::VK_ARM_TLSGD:
Jason W Kim1d866172011-01-13 00:07:51 +00001486 assert(0 && "unimplemented");
1487 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001488 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
1489 Type = ELF::R_ARM_TLS_IE32;
Jason W Kim1d866172011-01-13 00:07:51 +00001490 break;
1491 }
1492 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001493 case ARM::fixup_arm_branch:
1494 switch (Modifier) {
1495 case MCSymbolRefExpr::VK_ARM_PLT:
Jason W Kim1d866172011-01-13 00:07:51 +00001496 Type = ELF::R_ARM_PLT32;
1497 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001498 default:
Jason W Kim1d866172011-01-13 00:07:51 +00001499 Type = ELF::R_ARM_CALL;
1500 break;
1501 }
1502 break;
Jason W Kim86a97f22011-01-12 00:19:25 +00001503 case ARM::fixup_arm_movt_hi16:
1504 case ARM::fixup_arm_movt_hi16_pcrel:
Jason W Kim1d866172011-01-13 00:07:51 +00001505 Type = ELF::R_ARM_MOVT_PREL;
1506 break;
Jason W Kim86a97f22011-01-12 00:19:25 +00001507 case ARM::fixup_arm_movw_lo16:
1508 case ARM::fixup_arm_movw_lo16_pcrel:
Jason W Kim1d866172011-01-13 00:07:51 +00001509 Type = ELF::R_ARM_MOVW_PREL_NC;
1510 break;
Evan Chengf3eb3bb2011-01-14 02:38:49 +00001511 case ARM::fixup_t2_movt_hi16:
1512 case ARM::fixup_t2_movt_hi16_pcrel:
1513 Type = ELF::R_ARM_THM_MOVT_PREL;
1514 break;
1515 case ARM::fixup_t2_movw_lo16:
1516 case ARM::fixup_t2_movw_lo16_pcrel:
1517 Type = ELF::R_ARM_THM_MOVW_PREL_NC;
1518 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001519 }
1520 } else {
1521 switch ((unsigned)Fixup.getKind()) {
1522 default: llvm_unreachable("invalid fixup kind!");
Jason W Kima0871e72010-12-08 23:14:44 +00001523 case FK_Data_4:
1524 switch (Modifier) {
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001525 default: llvm_unreachable("Unsupported Modifier"); break;
1526 case MCSymbolRefExpr::VK_ARM_GOT:
Jason W Kim1d866172011-01-13 00:07:51 +00001527 Type = ELF::R_ARM_GOT_BREL;
1528 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001529 case MCSymbolRefExpr::VK_ARM_TLSGD:
Jason W Kim1d866172011-01-13 00:07:51 +00001530 Type = ELF::R_ARM_TLS_GD32;
1531 break;
Jason W Kimf13743b2010-12-16 03:12:17 +00001532 case MCSymbolRefExpr::VK_ARM_TPOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001533 Type = ELF::R_ARM_TLS_LE32;
1534 break;
Jason W Kima0871e72010-12-08 23:14:44 +00001535 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001536 Type = ELF::R_ARM_TLS_IE32;
1537 break;
Jason W Kimf13743b2010-12-16 03:12:17 +00001538 case MCSymbolRefExpr::VK_None:
Jason W Kim1d866172011-01-13 00:07:51 +00001539 Type = ELF::R_ARM_ABS32;
1540 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001541 case MCSymbolRefExpr::VK_ARM_GOTOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001542 Type = ELF::R_ARM_GOTOFF32;
1543 break;
1544 }
1545 break;
Jim Grosbachdff84b02010-12-02 00:28:45 +00001546 case ARM::fixup_arm_ldst_pcrel_12:
Owen Anderson9d63d902010-12-01 19:18:46 +00001547 case ARM::fixup_arm_pcrel_10:
Jim Grosbachdff84b02010-12-02 00:28:45 +00001548 case ARM::fixup_arm_adr_pcrel_12:
Jim Grosbach662a8162010-12-06 23:57:07 +00001549 case ARM::fixup_arm_thumb_bl:
Jim Grosbachb492a7c2010-12-09 19:50:12 +00001550 case ARM::fixup_arm_thumb_cb:
Bill Wendlingb8958b02010-12-08 01:57:09 +00001551 case ARM::fixup_arm_thumb_cp:
Jim Grosbache2467172010-12-10 18:21:33 +00001552 case ARM::fixup_arm_thumb_br:
Jason W Kim1d866172011-01-13 00:07:51 +00001553 assert(0 && "Unimplemented");
1554 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001555 case ARM::fixup_arm_branch:
Jason W Kimf13743b2010-12-16 03:12:17 +00001556 // FIXME: Differentiate between R_ARM_CALL and
1557 // R_ARM_JUMP24 (latter used for conditional jumps)
Jason W Kim1d866172011-01-13 00:07:51 +00001558 Type = ELF::R_ARM_CALL;
1559 break;
1560 case ARM::fixup_arm_movt_hi16:
1561 Type = ELF::R_ARM_MOVT_ABS;
1562 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001563 case ARM::fixup_arm_movw_lo16:
Jason W Kim1d866172011-01-13 00:07:51 +00001564 Type = ELF::R_ARM_MOVW_ABS_NC;
1565 break;
Evan Chengf3eb3bb2011-01-14 02:38:49 +00001566 case ARM::fixup_t2_movt_hi16:
1567 Type = ELF::R_ARM_THM_MOVT_ABS;
1568 break;
1569 case ARM::fixup_t2_movw_lo16:
1570 Type = ELF::R_ARM_THM_MOVW_ABS_NC;
1571 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001572 }
1573 }
1574
1575 if (RelocNeedsGOT(Modifier))
1576 NeedsGOT = true;
Jason W Kima0871e72010-12-08 23:14:44 +00001577
1578 return Type;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001579}
1580
Wesley Peck4b047132010-11-21 22:06:28 +00001581//===- MBlazeELFObjectWriter -------------------------------------------===//
Jason W Kimd3443e92010-11-15 16:18:39 +00001582
Rafael Espindola31f35782010-12-17 18:01:31 +00001583MBlazeELFObjectWriter::MBlazeELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001584 raw_ostream &_OS,
1585 bool IsLittleEndian)
1586 : ELFObjectWriter(MOTW, _OS, IsLittleEndian) {
Wesley Peck4b047132010-11-21 22:06:28 +00001587}
1588
1589MBlazeELFObjectWriter::~MBlazeELFObjectWriter() {
1590}
1591
Jason W Kim56a39902010-12-06 21:57:34 +00001592unsigned MBlazeELFObjectWriter::GetRelocType(const MCValue &Target,
Wesley Peck4b047132010-11-21 22:06:28 +00001593 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001594 bool IsPCRel,
1595 bool IsRelocWithSymbol,
1596 int64_t Addend) {
Wesley Peck4b047132010-11-21 22:06:28 +00001597 // determine the type of the relocation
1598 unsigned Type;
1599 if (IsPCRel) {
1600 switch ((unsigned)Fixup.getKind()) {
1601 default:
1602 llvm_unreachable("Unimplemented");
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001603 case FK_PCRel_4:
Wesley Peck4b047132010-11-21 22:06:28 +00001604 Type = ELF::R_MICROBLAZE_64_PCREL;
1605 break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001606 case FK_PCRel_2:
Wesley Peck4b047132010-11-21 22:06:28 +00001607 Type = ELF::R_MICROBLAZE_32_PCREL;
1608 break;
1609 }
1610 } else {
1611 switch ((unsigned)Fixup.getKind()) {
1612 default: llvm_unreachable("invalid fixup kind!");
1613 case FK_Data_4:
Jason W Kim56a39902010-12-06 21:57:34 +00001614 Type = ((IsRelocWithSymbol || Addend !=0)
1615 ? ELF::R_MICROBLAZE_32
1616 : ELF::R_MICROBLAZE_64);
Wesley Peck4b047132010-11-21 22:06:28 +00001617 break;
1618 case FK_Data_2:
1619 Type = ELF::R_MICROBLAZE_32;
1620 break;
1621 }
1622 }
Jason W Kim56a39902010-12-06 21:57:34 +00001623 return Type;
Wesley Peck4b047132010-11-21 22:06:28 +00001624}
Jason W Kimd3443e92010-11-15 16:18:39 +00001625
1626//===- X86ELFObjectWriter -------------------------------------------===//
1627
1628
Rafael Espindola31f35782010-12-17 18:01:31 +00001629X86ELFObjectWriter::X86ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001630 raw_ostream &_OS,
1631 bool IsLittleEndian)
1632 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
Jason W Kimd3443e92010-11-15 16:18:39 +00001633{}
1634
1635X86ELFObjectWriter::~X86ELFObjectWriter()
1636{}
1637
Jason W Kim56a39902010-12-06 21:57:34 +00001638unsigned X86ELFObjectWriter::GetRelocType(const MCValue &Target,
1639 const MCFixup &Fixup,
1640 bool IsPCRel,
1641 bool IsRelocWithSymbol,
1642 int64_t Addend) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001643 // determine the type of the relocation
1644
Rafael Espindola12203cc2010-11-21 00:48:25 +00001645 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1646 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
Jason W Kimd3443e92010-11-15 16:18:39 +00001647 unsigned Type;
Rafael Espindolabff66a82010-12-18 03:27:34 +00001648 if (is64Bit()) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001649 if (IsPCRel) {
Rafael Espindola3a83c402010-12-27 00:36:05 +00001650 switch ((unsigned)Fixup.getKind()) {
1651 default: llvm_unreachable("invalid fixup kind!");
1652 case FK_PCRel_8:
1653 assert(Modifier == MCSymbolRefExpr::VK_None);
1654 Type = ELF::R_X86_64_PC64;
Jason W Kimd3443e92010-11-15 16:18:39 +00001655 break;
Rafael Espindola7a549972011-01-01 19:05:35 +00001656 case X86::reloc_signed_4byte:
Rafael Espindolac3a561c2010-12-27 02:03:24 +00001657 case X86::reloc_riprel_4byte_movq_load:
Rafael Espindola3a83c402010-12-27 00:36:05 +00001658 case FK_Data_4: // FIXME?
1659 case X86::reloc_riprel_4byte:
1660 case FK_PCRel_4:
1661 switch (Modifier) {
1662 default:
1663 llvm_unreachable("Unimplemented");
1664 case MCSymbolRefExpr::VK_None:
1665 Type = ELF::R_X86_64_PC32;
1666 break;
1667 case MCSymbolRefExpr::VK_PLT:
1668 Type = ELF::R_X86_64_PLT32;
1669 break;
1670 case MCSymbolRefExpr::VK_GOTPCREL:
1671 Type = ELF::R_X86_64_GOTPCREL;
1672 break;
1673 case MCSymbolRefExpr::VK_GOTTPOFF:
1674 Type = ELF::R_X86_64_GOTTPOFF;
Jason W Kimd3443e92010-11-15 16:18:39 +00001675 break;
Rafael Espindola3a83c402010-12-27 00:36:05 +00001676 case MCSymbolRefExpr::VK_TLSGD:
1677 Type = ELF::R_X86_64_TLSGD;
1678 break;
1679 case MCSymbolRefExpr::VK_TLSLD:
1680 Type = ELF::R_X86_64_TLSLD;
1681 break;
1682 }
Jason W Kimd3443e92010-11-15 16:18:39 +00001683 break;
Rafael Espindola3a83c402010-12-27 00:36:05 +00001684 case FK_PCRel_2:
1685 assert(Modifier == MCSymbolRefExpr::VK_None);
1686 Type = ELF::R_X86_64_PC16;
Jason W Kimd3443e92010-11-15 16:18:39 +00001687 break;
1688 }
1689 } else {
1690 switch ((unsigned)Fixup.getKind()) {
1691 default: llvm_unreachable("invalid fixup kind!");
1692 case FK_Data_8: Type = ELF::R_X86_64_64; break;
1693 case X86::reloc_signed_4byte:
Jason W Kimd3443e92010-11-15 16:18:39 +00001694 assert(isInt<32>(Target.getConstant()));
1695 switch (Modifier) {
1696 default:
1697 llvm_unreachable("Unimplemented");
1698 case MCSymbolRefExpr::VK_None:
1699 Type = ELF::R_X86_64_32S;
1700 break;
1701 case MCSymbolRefExpr::VK_GOT:
1702 Type = ELF::R_X86_64_GOT32;
1703 break;
1704 case MCSymbolRefExpr::VK_GOTPCREL:
1705 Type = ELF::R_X86_64_GOTPCREL;
1706 break;
1707 case MCSymbolRefExpr::VK_TPOFF:
1708 Type = ELF::R_X86_64_TPOFF32;
1709 break;
1710 case MCSymbolRefExpr::VK_DTPOFF:
1711 Type = ELF::R_X86_64_DTPOFF32;
1712 break;
1713 }
1714 break;
1715 case FK_Data_4:
1716 Type = ELF::R_X86_64_32;
1717 break;
1718 case FK_Data_2: Type = ELF::R_X86_64_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001719 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001720 case FK_Data_1: Type = ELF::R_X86_64_8; break;
1721 }
1722 }
1723 } else {
1724 if (IsPCRel) {
1725 switch (Modifier) {
1726 default:
1727 llvm_unreachable("Unimplemented");
1728 case MCSymbolRefExpr::VK_None:
1729 Type = ELF::R_386_PC32;
1730 break;
1731 case MCSymbolRefExpr::VK_PLT:
1732 Type = ELF::R_386_PLT32;
1733 break;
1734 }
1735 } else {
1736 switch ((unsigned)Fixup.getKind()) {
1737 default: llvm_unreachable("invalid fixup kind!");
1738
1739 case X86::reloc_global_offset_table:
1740 Type = ELF::R_386_GOTPC;
1741 break;
1742
1743 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
1744 // instead?
1745 case X86::reloc_signed_4byte:
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001746 case FK_PCRel_4:
Jason W Kimd3443e92010-11-15 16:18:39 +00001747 case FK_Data_4:
1748 switch (Modifier) {
1749 default:
1750 llvm_unreachable("Unimplemented");
1751 case MCSymbolRefExpr::VK_None:
1752 Type = ELF::R_386_32;
1753 break;
1754 case MCSymbolRefExpr::VK_GOT:
1755 Type = ELF::R_386_GOT32;
1756 break;
1757 case MCSymbolRefExpr::VK_GOTOFF:
1758 Type = ELF::R_386_GOTOFF;
1759 break;
1760 case MCSymbolRefExpr::VK_TLSGD:
1761 Type = ELF::R_386_TLS_GD;
1762 break;
1763 case MCSymbolRefExpr::VK_TPOFF:
1764 Type = ELF::R_386_TLS_LE_32;
1765 break;
1766 case MCSymbolRefExpr::VK_INDNTPOFF:
1767 Type = ELF::R_386_TLS_IE;
1768 break;
1769 case MCSymbolRefExpr::VK_NTPOFF:
1770 Type = ELF::R_386_TLS_LE;
1771 break;
1772 case MCSymbolRefExpr::VK_GOTNTPOFF:
1773 Type = ELF::R_386_TLS_GOTIE;
1774 break;
1775 case MCSymbolRefExpr::VK_TLSLDM:
1776 Type = ELF::R_386_TLS_LDM;
1777 break;
1778 case MCSymbolRefExpr::VK_DTPOFF:
1779 Type = ELF::R_386_TLS_LDO_32;
1780 break;
1781 }
1782 break;
1783 case FK_Data_2: Type = ELF::R_386_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001784 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001785 case FK_Data_1: Type = ELF::R_386_8; break;
1786 }
1787 }
1788 }
1789
1790 if (RelocNeedsGOT(Modifier))
1791 NeedsGOT = true;
1792
Jason W Kim56a39902010-12-06 21:57:34 +00001793 return Type;
Matt Fleming3565a062010-08-16 18:57:57 +00001794}