blob: 4dbb5387f3dae98da7dec37d2dcb187669c4a8a4 [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
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000332 // Create the sections that show up in the symbol table. Currently
333 // those are the .note.GNU-stack section and the group sections.
334 virtual void CreateIndexedSections(MCAssembler &Asm, MCAsmLayout &Layout,
335 GroupMapTy &GroupMap,
336 RevGroupMapTy &RevGroupMap);
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000337
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000338 virtual void ExecutePostLayoutBinding(MCAssembler &Asm,
339 const MCAsmLayout &Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000340
Jason W Kimd3443e92010-11-15 16:18:39 +0000341 virtual void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
Matt Fleming3565a062010-08-16 18:57:57 +0000342 uint64_t Address, uint64_t Offset,
343 uint64_t Size, uint32_t Link, uint32_t Info,
344 uint64_t Alignment, uint64_t EntrySize);
345
Daniel Dunbar1f3662a2010-12-17 04:54:54 +0000346 virtual void WriteRelocationsFragment(const MCAssembler &Asm,
347 MCDataFragment *F,
348 const MCSectionData *SD);
349
Rafael Espindolafea753b2010-12-24 21:22:02 +0000350 virtual bool
351 IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
352 const MCSymbolData &DataA,
353 const MCFragment &FB,
354 bool InSet,
355 bool IsPCRel) const;
Rafael Espindola70703872010-09-30 02:22:20 +0000356
Jason W Kimd3443e92010-11-15 16:18:39 +0000357 virtual void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
358 virtual void WriteSection(MCAssembler &Asm,
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000359 const SectionIndexMapTy &SectionIndexMap,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000360 uint32_t GroupSymbolIndex,
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000361 uint64_t Offset, uint64_t Size, uint64_t Alignment,
362 const MCSectionELF &Section);
Jason W Kim56a39902010-12-06 21:57:34 +0000363
364 protected:
365 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
366 bool IsPCRel, bool IsRelocWithSymbol,
367 int64_t Addend) = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000368 };
369
Jason W Kimd3443e92010-11-15 16:18:39 +0000370 //===- X86ELFObjectWriter -------------------------------------------===//
371
372 class X86ELFObjectWriter : public ELFObjectWriter {
373 public:
Rafael Espindola31f35782010-12-17 18:01:31 +0000374 X86ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +0000375 raw_ostream &_OS,
376 bool IsLittleEndian);
Wesley Peck4b047132010-11-21 22:06:28 +0000377
Jason W Kimd3443e92010-11-15 16:18:39 +0000378 virtual ~X86ELFObjectWriter();
Jason W Kim56a39902010-12-06 21:57:34 +0000379 protected:
380 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
381 bool IsPCRel, bool IsRelocWithSymbol,
382 int64_t Addend);
Jason W Kimd3443e92010-11-15 16:18:39 +0000383 };
384
385
386 //===- ARMELFObjectWriter -------------------------------------------===//
387
388 class ARMELFObjectWriter : public ELFObjectWriter {
389 public:
Rafael Espindola31f35782010-12-17 18:01:31 +0000390 ARMELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +0000391 raw_ostream &_OS,
392 bool IsLittleEndian);
Wesley Peck4b047132010-11-21 22:06:28 +0000393
Jason W Kimd3443e92010-11-15 16:18:39 +0000394 virtual ~ARMELFObjectWriter();
Jason W Kim85fed5e2010-12-01 02:40:06 +0000395 protected:
Jason W Kim56a39902010-12-06 21:57:34 +0000396 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
397 bool IsPCRel, bool IsRelocWithSymbol,
398 int64_t Addend);
Jason W Kimd3443e92010-11-15 16:18:39 +0000399 };
Wesley Peck4b047132010-11-21 22:06:28 +0000400
401 //===- MBlazeELFObjectWriter -------------------------------------------===//
402
403 class MBlazeELFObjectWriter : public ELFObjectWriter {
404 public:
Rafael Espindola31f35782010-12-17 18:01:31 +0000405 MBlazeELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +0000406 raw_ostream &_OS,
407 bool IsLittleEndian);
Wesley Peck4b047132010-11-21 22:06:28 +0000408
409 virtual ~MBlazeELFObjectWriter();
Jason W Kim56a39902010-12-06 21:57:34 +0000410 protected:
411 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
412 bool IsPCRel, bool IsRelocWithSymbol,
413 int64_t Addend);
Wesley Peck4b047132010-11-21 22:06:28 +0000414 };
Matt Fleming3565a062010-08-16 18:57:57 +0000415}
416
Jason W Kimd3443e92010-11-15 16:18:39 +0000417ELFObjectWriter::~ELFObjectWriter()
418{}
419
Matt Fleming3565a062010-08-16 18:57:57 +0000420// Emit the ELF header.
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000421void ELFObjectWriter::WriteHeader(uint64_t SectionDataSize,
422 unsigned NumberOfSections) {
Matt Fleming3565a062010-08-16 18:57:57 +0000423 // ELF Header
424 // ----------
425 //
426 // Note
427 // ----
428 // emitWord method behaves differently for ELF32 and ELF64, writing
429 // 4 bytes in the former and 8 in the latter.
430
431 Write8(0x7f); // e_ident[EI_MAG0]
432 Write8('E'); // e_ident[EI_MAG1]
433 Write8('L'); // e_ident[EI_MAG2]
434 Write8('F'); // e_ident[EI_MAG3]
435
Rafael Espindolabff66a82010-12-18 03:27:34 +0000436 Write8(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
Matt Fleming3565a062010-08-16 18:57:57 +0000437
438 // e_ident[EI_DATA]
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000439 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
Matt Fleming3565a062010-08-16 18:57:57 +0000440
441 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky5baf79e2010-09-09 17:57:50 +0000442 // e_ident[EI_OSABI]
Rafael Espindolabff66a82010-12-18 03:27:34 +0000443 switch (TargetObjectWriter->getOSType()) {
Roman Divacky5baf79e2010-09-09 17:57:50 +0000444 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
445 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
446 default: Write8(ELF::ELFOSABI_NONE); break;
447 }
Matt Fleming3565a062010-08-16 18:57:57 +0000448 Write8(0); // e_ident[EI_ABIVERSION]
449
450 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
451
452 Write16(ELF::ET_REL); // e_type
453
Rafael Espindolabff66a82010-12-18 03:27:34 +0000454 Write16(TargetObjectWriter->getEMachine()); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +0000455
456 Write32(ELF::EV_CURRENT); // e_version
457 WriteWord(0); // e_entry, no entry point in .o file
458 WriteWord(0); // e_phoff, no program header for .o
Rafael Espindolabff66a82010-12-18 03:27:34 +0000459 WriteWord(SectionDataSize + (is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
Benjamin Kramereb976772010-08-17 17:02:29 +0000460 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000461
462 // FIXME: Make this configurable.
463 Write32(0); // e_flags = whatever the target wants
464
465 // e_ehsize = ELF header size
Rafael Espindolabff66a82010-12-18 03:27:34 +0000466 Write16(is64Bit() ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
Matt Fleming3565a062010-08-16 18:57:57 +0000467
468 Write16(0); // e_phentsize = prog header entry size
469 Write16(0); // e_phnum = # prog header entries = 0
470
471 // e_shentsize = Section header entry size
Rafael Espindolabff66a82010-12-18 03:27:34 +0000472 Write16(is64Bit() ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
Matt Fleming3565a062010-08-16 18:57:57 +0000473
474 // e_shnum = # of section header ents
Rafael Espindola7be2c332010-10-31 00:16:26 +0000475 if (NumberOfSections >= ELF::SHN_LORESERVE)
476 Write16(0);
477 else
478 Write16(NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000479
480 // e_shstrndx = Section # of '.shstrtab'
Rafael Espindola7be2c332010-10-31 00:16:26 +0000481 if (NumberOfSections >= ELF::SHN_LORESERVE)
482 Write16(ELF::SHN_XINDEX);
483 else
484 Write16(ShstrtabIndex);
Matt Fleming3565a062010-08-16 18:57:57 +0000485}
486
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000487void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
488 MCDataFragment *ShndxF,
489 uint64_t name,
490 uint8_t info, uint64_t value,
491 uint64_t size, uint8_t other,
492 uint32_t shndx,
493 bool Reserved) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000494 if (ShndxF) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000495 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000496 String32(*ShndxF, shndx);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000497 else
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000498 String32(*ShndxF, 0);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000499 }
500
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000501 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
502 uint16_t(ELF::SHN_XINDEX) : shndx;
503
Rafael Espindolabff66a82010-12-18 03:27:34 +0000504 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000505 String32(*SymtabF, name); // st_name
506 String8(*SymtabF, info); // st_info
507 String8(*SymtabF, other); // st_other
508 String16(*SymtabF, Index); // st_shndx
509 String64(*SymtabF, value); // st_value
510 String64(*SymtabF, size); // st_size
Matt Fleming3565a062010-08-16 18:57:57 +0000511 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000512 String32(*SymtabF, name); // st_name
513 String32(*SymtabF, value); // st_value
514 String32(*SymtabF, size); // st_size
515 String8(*SymtabF, info); // st_info
516 String8(*SymtabF, other); // st_other
517 String16(*SymtabF, Index); // st_shndx
Matt Fleming3565a062010-08-16 18:57:57 +0000518 }
519}
520
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000521static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout) {
522 if (Data.isCommon() && Data.isExternal())
523 return Data.getCommonAlignment();
524
525 const MCSymbol &Symbol = Data.getSymbol();
Roman Divackyd1491862010-12-20 21:14:39 +0000526
527 if (Symbol.isAbsolute() && Symbol.isVariable()) {
528 if (const MCExpr *Value = Symbol.getVariableValue()) {
529 int64_t IntValue;
530 if (Value->EvaluateAsAbsolute(IntValue, Layout))
531 return (uint64_t)IntValue;
532 }
533 }
534
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000535 if (!Symbol.isInSection())
536 return 0;
537
Rafael Espindolaffd902b2010-12-06 02:57:26 +0000538 if (Data.getFragment())
539 return Layout.getSymbolOffset(&Data);
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000540
541 return 0;
542}
543
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000544void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
545 const MCAsmLayout &Layout) {
Rafael Espindola88182132010-10-27 15:18:17 +0000546 // The presence of symbol versions causes undefined symbols and
547 // versions declared with @@@ to be renamed.
548
549 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
550 ie = Asm.symbol_end(); it != ie; ++it) {
551 const MCSymbol &Alias = it->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000552 const MCSymbol &Symbol = Alias.AliasedSymbol();
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000553 MCSymbolData &SD = Asm.getSymbolData(Symbol);
554
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000555 // Not an alias.
556 if (&Symbol == &Alias)
557 continue;
558
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000559 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000560 size_t Pos = AliasName.find('@');
561 if (Pos == StringRef::npos)
562 continue;
563
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000564 // Aliases defined with .symvar copy the binding from the symbol they alias.
565 // This is the first place we are able to copy this information.
566 it->setExternal(SD.isExternal());
567 SetBinding(*it, GetBinding(SD));
568
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000569 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000570 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
571 continue;
572
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000573 // FIXME: produce a better error message.
574 if (Symbol.isUndefined() && Rest.startswith("@@") &&
575 !Rest.startswith("@@@"))
576 report_fatal_error("A @@ version cannot be undefined");
577
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000578 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000579 }
580}
581
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000582void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
583 MCDataFragment *ShndxF,
584 ELFSymbolData &MSD,
585 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000586 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000587 MCSymbolData &Data =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000588 Layout.getAssembler().getSymbolData(OrigData.getSymbol().AliasedSymbol());
Rafael Espindola152c1062010-10-06 21:02:29 +0000589
Rafael Espindola7be2c332010-10-31 00:16:26 +0000590 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
591 Data.getSymbol().isVariable();
592
Rafael Espindola152c1062010-10-06 21:02:29 +0000593 uint8_t Binding = GetBinding(OrigData);
594 uint8_t Visibility = GetVisibility(OrigData);
595 uint8_t Type = GetType(Data);
596
597 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
598 uint8_t Other = Visibility;
599
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000600 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000601 uint64_t Size = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000602
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000603 assert(!(Data.isCommon() && !Data.isExternal()));
604
Rafael Espindolaf0121242010-12-22 16:03:00 +0000605 const MCExpr *ESize = Data.getSize();
606 if (ESize) {
607 int64_t Res;
608 if (!ESize->EvaluateAsAbsolute(Res, Layout))
609 report_fatal_error("Size expression must be absolute.");
610 Size = Res;
Matt Fleming3565a062010-08-16 18:57:57 +0000611 }
612
613 // Write out the symbol table entry
Rafael Espindola7be2c332010-10-31 00:16:26 +0000614 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
615 Size, Other, MSD.SectionIndex, IsReserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000616}
617
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000618void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
619 MCDataFragment *ShndxF,
620 const MCAssembler &Asm,
621 const MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000622 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000623 // The string table must be emitted first because we need the index
624 // into the string table for all the symbol names.
625 assert(StringTable.size() && "Missing string table");
626
627 // FIXME: Make sure the start of the symbol table is aligned.
628
629 // The first entry is the undefined symbol entry.
Rafael Espindola7be2c332010-10-31 00:16:26 +0000630 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
Matt Fleming3565a062010-08-16 18:57:57 +0000631
632 // Write the symbol table entries.
633 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
634 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
635 ELFSymbolData &MSD = LocalSymbolData[i];
Rafael Espindola7be2c332010-10-31 00:16:26 +0000636 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000637 }
638
Rafael Espindola71859c62010-09-16 19:46:31 +0000639 // Write out a symbol table entry for each regular section.
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000640 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
641 ++i) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000642 const MCSectionELF &Section =
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000643 static_cast<const MCSectionELF&>(i->getSection());
644 if (Section.getType() == ELF::SHT_RELA ||
645 Section.getType() == ELF::SHT_REL ||
646 Section.getType() == ELF::SHT_STRTAB ||
647 Section.getType() == ELF::SHT_SYMTAB)
Eli Friedmana44fa242010-08-16 21:17:09 +0000648 continue;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000649 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000650 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section), false);
Eli Friedmana44fa242010-08-16 21:17:09 +0000651 LastLocalSymbolIndex++;
652 }
Matt Fleming3565a062010-08-16 18:57:57 +0000653
654 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
655 ELFSymbolData &MSD = ExternalSymbolData[i];
656 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000657 assert(((Data.getFlags() & ELF_STB_Global) ||
658 (Data.getFlags() & ELF_STB_Weak)) &&
659 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000660 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000661 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000662 LastLocalSymbolIndex++;
663 }
664
665 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
666 ELFSymbolData &MSD = UndefinedSymbolData[i];
667 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000668 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000669 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000670 LastLocalSymbolIndex++;
671 }
672}
673
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000674const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm,
675 const MCValue &Target,
676 const MCFragment &F) const {
677 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000678 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
679 const MCSymbol *Renamed = Renames.lookup(&Symbol);
680 const MCSymbolData &SD = Asm.getSymbolData(Symbol);
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000681
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000682 if (ASymbol.isUndefined()) {
683 if (Renamed)
684 return Renamed;
685 return &ASymbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000686 }
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000687
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000688 if (SD.isExternal()) {
689 if (Renamed)
690 return Renamed;
691 return &Symbol;
692 }
Rafael Espindola73ffea42010-09-25 05:42:19 +0000693
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000694 const MCSectionELF &Section =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000695 static_cast<const MCSectionELF&>(ASymbol.getSection());
Rafael Espindola25958732010-11-24 21:57:39 +0000696 const SectionKind secKind = Section.getKind();
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000697
Rafael Espindola25958732010-11-24 21:57:39 +0000698 if (secKind.isBSS())
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000699 return NULL;
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000700
Rafael Espindola25958732010-11-24 21:57:39 +0000701 if (secKind.isThreadLocal()) {
702 if (Renamed)
703 return Renamed;
704 return &Symbol;
705 }
706
Rafael Espindola8cecf252010-10-06 16:23:36 +0000707 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000708 const MCSectionELF &Sec2 =
709 static_cast<const MCSectionELF&>(F.getParent()->getSection());
710
Rafael Espindola8cecf252010-10-06 16:23:36 +0000711 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000712 (Kind == MCSymbolRefExpr::VK_PLT ||
713 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
Rafael Espindola25958732010-11-24 21:57:39 +0000714 Kind == MCSymbolRefExpr::VK_GOTOFF)) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000715 if (Renamed)
716 return Renamed;
717 return &Symbol;
718 }
Rafael Espindola3729d002010-10-05 23:57:26 +0000719
Rafael Espindola1c130262011-01-23 04:43:11 +0000720 if (Section.getFlags() & ELF::SHF_MERGE) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000721 if (Target.getConstant() == 0)
722 return NULL;
723 if (Renamed)
724 return Renamed;
725 return &Symbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000726 }
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000727
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000728 return NULL;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000729}
730
Matt Fleming3565a062010-08-16 18:57:57 +0000731
Jason W Kim56a39902010-12-06 21:57:34 +0000732void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
733 const MCAsmLayout &Layout,
734 const MCFragment *Fragment,
735 const MCFixup &Fixup,
736 MCValue Target,
737 uint64_t &FixedValue) {
738 int64_t Addend = 0;
739 int Index = 0;
740 int64_t Value = Target.getConstant();
741 const MCSymbol *RelocSymbol = NULL;
742
Rafael Espindola127a6a42010-12-17 07:28:17 +0000743 bool IsPCRel = isFixupKindPCRel(Asm, Fixup.getKind());
Jason W Kim56a39902010-12-06 21:57:34 +0000744 if (!Target.isAbsolute()) {
745 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
746 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
747 RelocSymbol = SymbolToReloc(Asm, Target, *Fragment);
748
749 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
750 const MCSymbol &SymbolB = RefB->getSymbol();
751 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
752 IsPCRel = true;
753
754 // Offset of the symbol in the section
755 int64_t a = Layout.getSymbolOffset(&SDB);
756
757 // Ofeset of the relocation in the section
758 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
759 Value += b - a;
760 }
761
762 if (!RelocSymbol) {
763 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
764 MCFragment *F = SD.getFragment();
765
766 Index = F->getParent()->getOrdinal() + 1;
767
768 // Offset of the symbol in the section
769 Value += Layout.getSymbolOffset(&SD);
770 } else {
771 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
772 WeakrefUsedInReloc.insert(RelocSymbol);
773 else
774 UsedInReloc.insert(RelocSymbol);
775 Index = -1;
776 }
777 Addend = Value;
778 // Compensate for the addend on i386.
Rafael Espindolabff66a82010-12-18 03:27:34 +0000779 if (is64Bit())
Jason W Kim56a39902010-12-06 21:57:34 +0000780 Value = 0;
781 }
782
783 FixedValue = Value;
784 unsigned Type = GetRelocType(Target, Fixup, IsPCRel,
785 (RelocSymbol != 0), Addend);
786
787 uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
788 Fixup.getOffset();
789
Rafael Espindolabff66a82010-12-18 03:27:34 +0000790 if (!hasRelocationAddend())
791 Addend = 0;
Jason W Kim56a39902010-12-06 21:57:34 +0000792 ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend);
793 Relocations[Fragment->getParent()].push_back(ERE);
794}
795
796
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000797uint64_t
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000798ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
799 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000800 MCSymbolData &SD = Asm.getSymbolData(*S);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000801 return SD.getIndex();
Matt Fleming3565a062010-08-16 18:57:57 +0000802}
803
Rafael Espindola737cd212010-10-05 18:01:23 +0000804static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
Rafael Espindola88182132010-10-27 15:18:17 +0000805 bool Used, bool Renamed) {
Rafael Espindola484291c2010-11-01 14:28:48 +0000806 if (Data.getFlags() & ELF_Other_Weakref)
807 return false;
808
Rafael Espindolabd701182010-10-19 19:31:37 +0000809 if (Used)
810 return true;
811
Rafael Espindola88182132010-10-27 15:18:17 +0000812 if (Renamed)
813 return false;
814
Rafael Espindola737cd212010-10-05 18:01:23 +0000815 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000816
Rafael Espindolad1798862010-10-29 23:09:31 +0000817 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
818 return true;
819
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000820 const MCSymbol &A = Symbol.AliasedSymbol();
Rafael Espindolad1798862010-10-29 23:09:31 +0000821 if (!A.isVariable() && A.isUndefined() && !Data.isCommon())
Rafael Espindolaa6866962010-10-27 14:44:52 +0000822 return false;
823
Rafael Espindola737cd212010-10-05 18:01:23 +0000824 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
825 return false;
826
Rafael Espindolabd701182010-10-19 19:31:37 +0000827 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000828 return false;
829
830 return true;
831}
832
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000833static bool isLocal(const MCSymbolData &Data, bool isSignature,
834 bool isUsedInReloc) {
Rafael Espindola737cd212010-10-05 18:01:23 +0000835 if (Data.isExternal())
836 return false;
837
838 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000839 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000840
841 if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
842 if (isSignature && !isUsedInReloc)
843 return true;
844
Rafael Espindola737cd212010-10-05 18:01:23 +0000845 return false;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000846 }
Rafael Espindola737cd212010-10-05 18:01:23 +0000847
848 return true;
849}
850
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000851void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
852 SectionIndexMapTy &SectionIndexMap) {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000853 unsigned Index = 1;
854 for (MCAssembler::iterator it = Asm.begin(),
855 ie = Asm.end(); it != ie; ++it) {
856 const MCSectionELF &Section =
857 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000858 if (Section.getType() != ELF::SHT_GROUP)
859 continue;
860 SectionIndexMap[&Section] = Index++;
861 }
862
863 for (MCAssembler::iterator it = Asm.begin(),
864 ie = Asm.end(); it != ie; ++it) {
865 const MCSectionELF &Section =
866 static_cast<const MCSectionELF &>(it->getSection());
867 if (Section.getType() == ELF::SHT_GROUP)
868 continue;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000869 SectionIndexMap[&Section] = Index++;
870 }
871}
872
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000873void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000874 const SectionIndexMapTy &SectionIndexMap,
875 RevGroupMapTy RevGroupMap) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000876 // FIXME: Is this the correct place to do this?
877 if (NeedsGOT) {
878 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
879 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
880 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
881 Data.setExternal(true);
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000882 SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindola5c77c162010-10-05 15:48:37 +0000883 }
884
Matt Fleming3565a062010-08-16 18:57:57 +0000885 // Build section lookup table.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000886 int NumRegularSections = Asm.size();
Matt Fleming3565a062010-08-16 18:57:57 +0000887
888 // Index 0 is always the empty string.
889 StringMap<uint64_t> StringIndexMap;
890 StringTable += '\x00';
891
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000892 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000893 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
894 ie = Asm.symbol_end(); it != ie; ++it) {
895 const MCSymbol &Symbol = it->getSymbol();
896
Rafael Espindola484291c2010-11-01 14:28:48 +0000897 bool Used = UsedInReloc.count(&Symbol);
898 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000899 bool isSignature = RevGroupMap.count(&Symbol);
900
901 if (!isInSymtab(Asm, *it,
902 Used || WeakrefUsed || isSignature,
Rafael Espindola88182132010-10-27 15:18:17 +0000903 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000904 continue;
905
Matt Fleming3565a062010-08-16 18:57:57 +0000906 ELFSymbolData MSD;
907 MSD.SymbolData = it;
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000908 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Matt Fleming3565a062010-08-16 18:57:57 +0000909
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000910 // Undefined symbols are global, but this is the first place we
911 // are able to set it.
912 bool Local = isLocal(*it, isSignature, Used);
913 if (!Local && GetBinding(*it) == ELF::STB_LOCAL) {
914 MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
915 SetBinding(*it, ELF::STB_GLOBAL);
916 SetBinding(SD, ELF::STB_GLOBAL);
917 }
918
Rafael Espindola484291c2010-11-01 14:28:48 +0000919 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
920 SetBinding(*it, ELF::STB_WEAK);
921
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000922 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000923 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000924 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000925 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000926 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000927 } else if (RefSymbol.isUndefined()) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000928 if (isSignature && !Used)
929 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
930 else
931 MSD.SectionIndex = ELF::SHN_UNDEF;
Matt Fleming3565a062010-08-16 18:57:57 +0000932 } else {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000933 const MCSectionELF &Section =
934 static_cast<const MCSectionELF&>(RefSymbol.getSection());
935 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000936 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
937 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000938 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000939 }
940
Rafael Espindola88182132010-10-27 15:18:17 +0000941 // The @@@ in symbol version is replaced with @ in undefined symbols and
942 // @@ in defined ones.
943 StringRef Name = Symbol.getName();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000944 SmallString<32> Buf;
945
Rafael Espindola88182132010-10-27 15:18:17 +0000946 size_t Pos = Name.find("@@@");
Rafael Espindola88182132010-10-27 15:18:17 +0000947 if (Pos != StringRef::npos) {
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000948 Buf += Name.substr(0, Pos);
949 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
950 Buf += Name.substr(Pos + Skip);
951 Name = Buf;
Rafael Espindola88182132010-10-27 15:18:17 +0000952 }
953
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000954 uint64_t &Entry = StringIndexMap[Name];
Rafael Espindolaa6866962010-10-27 14:44:52 +0000955 if (!Entry) {
956 Entry = StringTable.size();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000957 StringTable += Name;
Rafael Espindolaa6866962010-10-27 14:44:52 +0000958 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +0000959 }
Rafael Espindolaa6866962010-10-27 14:44:52 +0000960 MSD.StringIndex = Entry;
961 if (MSD.SectionIndex == ELF::SHN_UNDEF)
962 UndefinedSymbolData.push_back(MSD);
963 else if (Local)
964 LocalSymbolData.push_back(MSD);
965 else
966 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +0000967 }
968
969 // Symbols are required to be in lexicographic order.
970 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
971 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
972 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
973
974 // Set the symbol indices. Local symbols must come before all other
975 // symbols with non-local bindings.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000976 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000977 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
978 LocalSymbolData[i].SymbolData->setIndex(Index++);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000979
980 Index += NumRegularSections;
981
Matt Fleming3565a062010-08-16 18:57:57 +0000982 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
983 ExternalSymbolData[i].SymbolData->setIndex(Index++);
984 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
985 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
986}
987
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000988void ELFObjectWriter::WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
989 const MCSectionData &SD) {
Matt Fleming3565a062010-08-16 18:57:57 +0000990 if (!Relocations[&SD].empty()) {
991 MCContext &Ctx = Asm.getContext();
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000992 const MCSectionELF *RelaSection;
Matt Fleming3565a062010-08-16 18:57:57 +0000993 const MCSectionELF &Section =
994 static_cast<const MCSectionELF&>(SD.getSection());
995
996 const StringRef SectionName = Section.getSectionName();
Rafael Espindolabff66a82010-12-18 03:27:34 +0000997 std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +0000998 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000999
1000 unsigned EntrySize;
Rafael Espindolabff66a82010-12-18 03:27:34 +00001001 if (hasRelocationAddend())
1002 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
Benjamin Kramer299fbe32010-08-17 17:56:13 +00001003 else
Rafael Espindolabff66a82010-12-18 03:27:34 +00001004 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +00001005
Rafael Espindolabff66a82010-12-18 03:27:34 +00001006 RelaSection = Ctx.getELFSection(RelaSectionName, hasRelocationAddend() ?
Benjamin Kramer377a5722010-08-17 17:30:07 +00001007 ELF::SHT_RELA : ELF::SHT_REL, 0,
Matt Fleming3565a062010-08-16 18:57:57 +00001008 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001009 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001010
1011 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +00001012 RelaSD.setAlignment(is64Bit() ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +00001013
1014 MCDataFragment *F = new MCDataFragment(&RelaSD);
1015
1016 WriteRelocationsFragment(Asm, F, &SD);
Matt Fleming3565a062010-08-16 18:57:57 +00001017 }
1018}
1019
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001020void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
1021 uint64_t Flags, uint64_t Address,
1022 uint64_t Offset, uint64_t Size,
1023 uint32_t Link, uint32_t Info,
1024 uint64_t Alignment,
1025 uint64_t EntrySize) {
Matt Fleming3565a062010-08-16 18:57:57 +00001026 Write32(Name); // sh_name: index into string table
1027 Write32(Type); // sh_type
1028 WriteWord(Flags); // sh_flags
1029 WriteWord(Address); // sh_addr
1030 WriteWord(Offset); // sh_offset
1031 WriteWord(Size); // sh_size
1032 Write32(Link); // sh_link
1033 Write32(Info); // sh_info
1034 WriteWord(Alignment); // sh_addralign
1035 WriteWord(EntrySize); // sh_entsize
1036}
1037
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001038void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
1039 MCDataFragment *F,
1040 const MCSectionData *SD) {
Matt Fleming3565a062010-08-16 18:57:57 +00001041 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
1042 // sort by the r_offset just like gnu as does
1043 array_pod_sort(Relocs.begin(), Relocs.end());
1044
1045 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1046 ELFRelocationEntry entry = Relocs[e - i - 1];
1047
Rafael Espindola12203cc2010-11-21 00:48:25 +00001048 if (!entry.Index)
1049 ;
1050 else if (entry.Index < 0)
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001051 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
1052 else
Rafael Espindola12203cc2010-11-21 00:48:25 +00001053 entry.Index += LocalSymbolData.size();
Rafael Espindolabff66a82010-12-18 03:27:34 +00001054 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001055 String64(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001056
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001057 struct ELF::Elf64_Rela ERE64;
1058 ERE64.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001059 String64(*F, ERE64.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001060
Rafael Espindolabff66a82010-12-18 03:27:34 +00001061 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001062 String64(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001063 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001064 String32(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001065
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001066 struct ELF::Elf32_Rela ERE32;
1067 ERE32.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001068 String32(*F, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001069
Rafael Espindolabff66a82010-12-18 03:27:34 +00001070 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001071 String32(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001072 }
Matt Fleming3565a062010-08-16 18:57:57 +00001073 }
1074}
1075
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001076void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
1077 MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001078 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +00001079 MCContext &Ctx = Asm.getContext();
1080 MCDataFragment *F;
1081
Rafael Espindolabff66a82010-12-18 03:27:34 +00001082 unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
Matt Fleming3565a062010-08-16 18:57:57 +00001083
Rafael Espindola38738bf2010-09-22 19:04:41 +00001084 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001085 const MCSectionELF *ShstrtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001086 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001087 SectionKind::getReadOnly());
Rafael Espindola71859c62010-09-16 19:46:31 +00001088 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1089 ShstrtabSD.setAlignment(1);
1090 ShstrtabIndex = Asm.size();
1091
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001092 const MCSectionELF *SymtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001093 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1094 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001095 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001096 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +00001097 SymtabSD.setAlignment(is64Bit() ? 8 : 4);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001098 SymbolTableIndex = Asm.size();
1099
1100 MCSectionData *SymtabShndxSD = NULL;
1101
1102 if (NeedsSymtabShndx) {
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001103 const MCSectionELF *SymtabShndxSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001104 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001105 SectionKind::getReadOnly(), 4, "");
Rafael Espindola7be2c332010-10-31 00:16:26 +00001106 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
1107 SymtabShndxSD->setAlignment(4);
1108 }
Matt Fleming3565a062010-08-16 18:57:57 +00001109
Matt Fleming3565a062010-08-16 18:57:57 +00001110 const MCSection *StrtabSection;
1111 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001112 SectionKind::getReadOnly());
Matt Fleming3565a062010-08-16 18:57:57 +00001113 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1114 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +00001115 StringTableIndex = Asm.size();
1116
Rafael Espindolac3c413f2010-09-27 22:04:54 +00001117 WriteRelocations(Asm, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001118
1119 // Symbol table
1120 F = new MCDataFragment(&SymtabSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001121 MCDataFragment *ShndxF = NULL;
1122 if (NeedsSymtabShndx) {
1123 ShndxF = new MCDataFragment(SymtabShndxSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001124 }
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001125 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
Rafael Espindola71859c62010-09-16 19:46:31 +00001126
Matt Fleming3565a062010-08-16 18:57:57 +00001127 F = new MCDataFragment(&StrtabSD);
1128 F->getContents().append(StringTable.begin(), StringTable.end());
Matt Fleming3565a062010-08-16 18:57:57 +00001129
Matt Fleming3565a062010-08-16 18:57:57 +00001130 F = new MCDataFragment(&ShstrtabSD);
1131
Matt Fleming3565a062010-08-16 18:57:57 +00001132 // Section header string table.
1133 //
1134 // The first entry of a string table holds a null character so skip
1135 // section 0.
1136 uint64_t Index = 1;
1137 F->getContents() += '\x00';
1138
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001139 StringMap<uint64_t> SecStringMap;
Matt Fleming3565a062010-08-16 18:57:57 +00001140 for (MCAssembler::const_iterator it = Asm.begin(),
1141 ie = Asm.end(); it != ie; ++it) {
Matt Fleming3565a062010-08-16 18:57:57 +00001142 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +00001143 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola51efe7a2010-09-23 14:14:56 +00001144 // FIXME: We could merge suffixes like in .text and .rela.text.
Matt Fleming3565a062010-08-16 18:57:57 +00001145
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001146 StringRef Name = Section.getSectionName();
1147 if (SecStringMap.count(Name)) {
1148 SectionStringTableIndex[&Section] = SecStringMap[Name];
1149 continue;
1150 }
Matt Fleming3565a062010-08-16 18:57:57 +00001151 // Remember the index into the string table so we can write it
1152 // into the sh_name field of the section header table.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001153 SectionStringTableIndex[&Section] = Index;
1154 SecStringMap[Name] = Index;
Matt Fleming3565a062010-08-16 18:57:57 +00001155
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001156 Index += Name.size() + 1;
1157 F->getContents() += Name;
Matt Fleming3565a062010-08-16 18:57:57 +00001158 F->getContents() += '\x00';
1159 }
Rafael Espindola70703872010-09-30 02:22:20 +00001160}
1161
Rafael Espindolafea753b2010-12-24 21:22:02 +00001162bool
1163ELFObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
1164 const MCSymbolData &DataA,
1165 const MCFragment &FB,
1166 bool InSet,
1167 bool IsPCRel) const {
Rafael Espindola2c920852010-11-16 04:11:46 +00001168 // FIXME: This is in here just to match gnu as output. If the two ends
1169 // are in the same section, there is nothing that the linker can do to
1170 // break it.
Rafael Espindola70703872010-09-30 02:22:20 +00001171 if (DataA.isExternal())
1172 return false;
1173
Rafael Espindolafea753b2010-12-24 21:22:02 +00001174 const MCSection &SecA = DataA.getSymbol().AliasedSymbol().getSection();
1175 const MCSection &SecB = FB.getParent()->getSection();
1176 // On ELF A - B is absolute if A and B are in the same section.
1177 return &SecA == &SecB;
Matt Fleming3565a062010-08-16 18:57:57 +00001178}
1179
Rafael Espindola96aa78c2011-01-23 17:55:27 +00001180void ELFObjectWriter::CreateIndexedSections(MCAssembler &Asm,
1181 MCAsmLayout &Layout,
1182 GroupMapTy &GroupMap,
1183 RevGroupMapTy &RevGroupMap) {
1184 // Create the .note.GNU-stack section if needed.
1185 MCContext &Ctx = Asm.getContext();
1186 if (Asm.getNoExecStack()) {
1187 const MCSectionELF *GnuStackSection =
1188 Ctx.getELFSection(".note.GNU-stack", ELF::SHT_PROGBITS, 0,
1189 SectionKind::getReadOnly());
1190 Asm.getOrCreateSectionData(*GnuStackSection);
1191 }
1192
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001193 // Build the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001194 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1195 it != ie; ++it) {
1196 const MCSectionELF &Section =
1197 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola1c130262011-01-23 04:43:11 +00001198 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001199 continue;
1200
1201 const MCSymbol *SignatureSymbol = Section.getGroup();
1202 Asm.getOrCreateSymbolData(*SignatureSymbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001203 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001204 if (!Group) {
Rafael Espindola96aa78c2011-01-23 17:55:27 +00001205 Group = Ctx.CreateELFGroupSection();
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001206 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1207 Data.setAlignment(4);
1208 MCDataFragment *F = new MCDataFragment(&Data);
1209 String32(*F, ELF::GRP_COMDAT);
1210 }
1211 GroupMap[Group] = SignatureSymbol;
1212 }
1213
1214 // Add sections to the groups
1215 unsigned Index = 1;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001216 unsigned NumGroups = RevGroupMap.size();
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001217 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1218 it != ie; ++it, ++Index) {
1219 const MCSectionELF &Section =
1220 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola1c130262011-01-23 04:43:11 +00001221 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001222 continue;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001223 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001224 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1225 // FIXME: we could use the previous fragment
1226 MCDataFragment *F = new MCDataFragment(&Data);
1227 String32(*F, NumGroups + Index);
1228 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001229}
1230
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001231void ELFObjectWriter::WriteSection(MCAssembler &Asm,
1232 const SectionIndexMapTy &SectionIndexMap,
1233 uint32_t GroupSymbolIndex,
1234 uint64_t Offset, uint64_t Size,
1235 uint64_t Alignment,
1236 const MCSectionELF &Section) {
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001237 uint64_t sh_link = 0;
1238 uint64_t sh_info = 0;
1239
1240 switch(Section.getType()) {
1241 case ELF::SHT_DYNAMIC:
1242 sh_link = SectionStringTableIndex[&Section];
1243 sh_info = 0;
1244 break;
1245
1246 case ELF::SHT_REL:
1247 case ELF::SHT_RELA: {
1248 const MCSectionELF *SymtabSection;
1249 const MCSectionELF *InfoSection;
1250 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
1251 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001252 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001253 sh_link = SectionIndexMap.lookup(SymtabSection);
1254 assert(sh_link && ".symtab not found");
1255
1256 // Remove ".rel" and ".rela" prefixes.
1257 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1258 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
1259
1260 InfoSection = Asm.getContext().getELFSection(SectionName,
1261 ELF::SHT_PROGBITS, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001262 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001263 sh_info = SectionIndexMap.lookup(InfoSection);
1264 break;
1265 }
1266
1267 case ELF::SHT_SYMTAB:
1268 case ELF::SHT_DYNSYM:
1269 sh_link = StringTableIndex;
1270 sh_info = LastLocalSymbolIndex;
1271 break;
1272
1273 case ELF::SHT_SYMTAB_SHNDX:
1274 sh_link = SymbolTableIndex;
1275 break;
1276
1277 case ELF::SHT_PROGBITS:
1278 case ELF::SHT_STRTAB:
1279 case ELF::SHT_NOBITS:
Rafael Espindola98976612010-12-26 21:30:59 +00001280 case ELF::SHT_NOTE:
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001281 case ELF::SHT_NULL:
1282 case ELF::SHT_ARM_ATTRIBUTES:
Jason W Kim86a97f22011-01-12 00:19:25 +00001283 case ELF::SHT_INIT_ARRAY:
1284 case ELF::SHT_FINI_ARRAY:
1285 case ELF::SHT_PREINIT_ARRAY:
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +00001286 case ELF::SHT_X86_64_UNWIND:
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001287 // Nothing to do.
1288 break;
1289
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001290 case ELF::SHT_GROUP: {
1291 sh_link = SymbolTableIndex;
1292 sh_info = GroupSymbolIndex;
1293 break;
1294 }
1295
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001296 default:
1297 assert(0 && "FIXME: sh_type value not supported!");
1298 break;
1299 }
1300
1301 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1302 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1303 Alignment, Section.getEntrySize());
1304}
1305
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001306static bool IsELFMetaDataSection(const MCSectionData &SD) {
Rafael Espindolaf8803fe2010-12-06 03:48:09 +00001307 return SD.getOrdinal() == ~UINT32_C(0) &&
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001308 !SD.getSection().isVirtualSection();
1309}
1310
1311static uint64_t DataSectionSize(const MCSectionData &SD) {
1312 uint64_t Ret = 0;
1313 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1314 ++i) {
1315 const MCFragment &F = *i;
1316 assert(F.getKind() == MCFragment::FT_Data);
1317 Ret += cast<MCDataFragment>(F).getContents().size();
1318 }
1319 return Ret;
1320}
1321
1322static uint64_t GetSectionFileSize(const MCAsmLayout &Layout,
1323 const MCSectionData &SD) {
1324 if (IsELFMetaDataSection(SD))
1325 return DataSectionSize(SD);
1326 return Layout.getSectionFileSize(&SD);
1327}
1328
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001329static uint64_t GetSectionAddressSize(const MCAsmLayout &Layout,
1330 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001331 if (IsELFMetaDataSection(SD))
1332 return DataSectionSize(SD);
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001333 return Layout.getSectionAddressSize(&SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001334}
1335
1336static void WriteDataSectionData(ELFObjectWriter *W, const MCSectionData &SD) {
1337 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1338 ++i) {
1339 const MCFragment &F = *i;
1340 assert(F.getKind() == MCFragment::FT_Data);
1341 W->WriteBytes(cast<MCDataFragment>(F).getContents().str());
1342 }
1343}
1344
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001345void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1346 const MCAsmLayout &Layout) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001347 GroupMapTy GroupMap;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001348 RevGroupMapTy RevGroupMap;
Rafael Espindola96aa78c2011-01-23 17:55:27 +00001349 CreateIndexedSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1350 RevGroupMap);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001351
Rafael Espindolabab2a802010-11-10 21:51:05 +00001352 SectionIndexMapTy SectionIndexMap;
1353
1354 ComputeIndexMap(Asm, SectionIndexMap);
1355
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001356 // Compute symbol table information.
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001357 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap);
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001358
Matt Fleming3565a062010-08-16 18:57:57 +00001359 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001360 const_cast<MCAsmLayout&>(Layout),
1361 SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +00001362
Rafael Espindola1d739a02010-11-10 22:34:07 +00001363 // Update to include the metadata sections.
1364 ComputeIndexMap(Asm, SectionIndexMap);
1365
Matt Fleming3565a062010-08-16 18:57:57 +00001366 // Add 1 for the null section.
1367 unsigned NumSections = Asm.size() + 1;
Rafael Espindolabff66a82010-12-18 03:27:34 +00001368 uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
1369 uint64_t HeaderSize = is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
1370 sizeof(ELF::Elf32_Ehdr);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001371 uint64_t FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001372
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001373 std::vector<const MCSectionELF*> Sections;
1374 Sections.resize(NumSections);
1375
1376 for (SectionIndexMapTy::const_iterator i=
1377 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1378 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
1379 Sections[p.second] = p.first;
1380 }
1381
1382 for (unsigned i = 1; i < NumSections; ++i) {
1383 const MCSectionELF &Section = *Sections[i];
1384 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001385
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001386 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1387
Matt Fleming3565a062010-08-16 18:57:57 +00001388 // Get the size of the section in the output file (including padding).
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001389 FileOff += GetSectionFileSize(Layout, SD);
Matt Fleming3565a062010-08-16 18:57:57 +00001390 }
1391
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001392 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1393
Matt Fleming3565a062010-08-16 18:57:57 +00001394 // Write out the ELF header ...
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001395 WriteHeader(FileOff - HeaderSize, NumSections);
1396
1397 FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001398
1399 // ... then all of the sections ...
1400 DenseMap<const MCSection*, uint64_t> SectionOffsetMap;
1401
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001402 for (unsigned i = 1; i < NumSections; ++i) {
1403 const MCSectionELF &Section = *Sections[i];
1404 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001405
1406 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1407 WriteZeros(Padding);
1408 FileOff += Padding;
1409
Matt Fleming3565a062010-08-16 18:57:57 +00001410 // Remember the offset into the file for this section.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001411 SectionOffsetMap[&Section] = FileOff;
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001412
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001413 FileOff += GetSectionFileSize(Layout, SD);
Matt Fleming3565a062010-08-16 18:57:57 +00001414
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001415 if (IsELFMetaDataSection(SD))
1416 WriteDataSectionData(this, SD);
1417 else
Daniel Dunbar5d2477c2010-12-17 02:45:59 +00001418 Asm.WriteSectionData(&SD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +00001419 }
1420
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001421 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1422 WriteZeros(Padding);
1423 FileOff += Padding;
1424
Matt Fleming3565a062010-08-16 18:57:57 +00001425 // ... and then the section header table.
1426 // Should we align the section header table?
1427 //
1428 // Null section first.
Rafael Espindola7be2c332010-10-31 00:16:26 +00001429 uint64_t FirstSectionSize =
1430 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1431 uint32_t FirstSectionLink =
1432 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1433 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming3565a062010-08-16 18:57:57 +00001434
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001435 for (unsigned i = 1; i < NumSections; ++i) {
1436 const MCSectionELF &Section = *Sections[i];
1437 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1438 uint32_t GroupSymbolIndex;
1439 if (Section.getType() != ELF::SHT_GROUP)
1440 GroupSymbolIndex = 0;
1441 else
1442 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm, GroupMap[&Section]);
Matt Fleming3565a062010-08-16 18:57:57 +00001443
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001444 uint64_t Size = GetSectionAddressSize(Layout, SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001445
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001446 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001447 SectionOffsetMap[&Section], Size,
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001448 SD.getAlignment(), Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001449 }
1450}
1451
Rafael Espindola6024c972010-12-17 17:45:22 +00001452MCObjectWriter *llvm::createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1453 raw_ostream &OS,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001454 bool IsLittleEndian) {
1455 switch (MOTW->getEMachine()) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001456 case ELF::EM_386:
1457 case ELF::EM_X86_64:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001458 return new X86ELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001459 case ELF::EM_ARM:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001460 return new ARMELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Wesley Peck4b047132010-11-21 22:06:28 +00001461 case ELF::EM_MBLAZE:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001462 return new MBlazeELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Benjamin Kramer32858772010-11-15 19:20:50 +00001463 default: llvm_unreachable("Unsupported architecture"); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001464 }
1465}
1466
1467
1468/// START OF SUBCLASSES for ELFObjectWriter
1469//===- ARMELFObjectWriter -------------------------------------------===//
1470
Rafael Espindola31f35782010-12-17 18:01:31 +00001471ARMELFObjectWriter::ARMELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001472 raw_ostream &_OS,
1473 bool IsLittleEndian)
1474 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
Jason W Kimd3443e92010-11-15 16:18:39 +00001475{}
1476
1477ARMELFObjectWriter::~ARMELFObjectWriter()
1478{}
1479
Jason W Kim85fed5e2010-12-01 02:40:06 +00001480unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target,
1481 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001482 bool IsPCRel,
1483 bool IsRelocWithSymbol,
1484 int64_t Addend) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001485 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1486 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
1487
Jason W Kima0871e72010-12-08 23:14:44 +00001488 unsigned Type = 0;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001489 if (IsPCRel) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001490 switch ((unsigned)Fixup.getKind()) {
1491 default: assert(0 && "Unimplemented");
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001492 case FK_Data_4:
1493 switch (Modifier) {
1494 default: llvm_unreachable("Unsupported Modifier");
1495 case MCSymbolRefExpr::VK_None:
Jason W Kim1d866172011-01-13 00:07:51 +00001496 Type = ELF::R_ARM_BASE_PREL;
1497 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001498 case MCSymbolRefExpr::VK_ARM_TLSGD:
Jason W Kim1d866172011-01-13 00:07:51 +00001499 assert(0 && "unimplemented");
1500 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001501 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
1502 Type = ELF::R_ARM_TLS_IE32;
Jason W Kim1d866172011-01-13 00:07:51 +00001503 break;
1504 }
1505 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001506 case ARM::fixup_arm_branch:
1507 switch (Modifier) {
1508 case MCSymbolRefExpr::VK_ARM_PLT:
Jason W Kim1d866172011-01-13 00:07:51 +00001509 Type = ELF::R_ARM_PLT32;
1510 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001511 default:
Jason W Kim1d866172011-01-13 00:07:51 +00001512 Type = ELF::R_ARM_CALL;
1513 break;
1514 }
1515 break;
Jason W Kim86a97f22011-01-12 00:19:25 +00001516 case ARM::fixup_arm_movt_hi16:
1517 case ARM::fixup_arm_movt_hi16_pcrel:
Jason W Kim1d866172011-01-13 00:07:51 +00001518 Type = ELF::R_ARM_MOVT_PREL;
1519 break;
Jason W Kim86a97f22011-01-12 00:19:25 +00001520 case ARM::fixup_arm_movw_lo16:
1521 case ARM::fixup_arm_movw_lo16_pcrel:
Jason W Kim1d866172011-01-13 00:07:51 +00001522 Type = ELF::R_ARM_MOVW_PREL_NC;
1523 break;
Evan Chengf3eb3bb2011-01-14 02:38:49 +00001524 case ARM::fixup_t2_movt_hi16:
1525 case ARM::fixup_t2_movt_hi16_pcrel:
1526 Type = ELF::R_ARM_THM_MOVT_PREL;
1527 break;
1528 case ARM::fixup_t2_movw_lo16:
1529 case ARM::fixup_t2_movw_lo16_pcrel:
1530 Type = ELF::R_ARM_THM_MOVW_PREL_NC;
1531 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001532 }
1533 } else {
1534 switch ((unsigned)Fixup.getKind()) {
1535 default: llvm_unreachable("invalid fixup kind!");
Jason W Kima0871e72010-12-08 23:14:44 +00001536 case FK_Data_4:
1537 switch (Modifier) {
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001538 default: llvm_unreachable("Unsupported Modifier"); break;
1539 case MCSymbolRefExpr::VK_ARM_GOT:
Jason W Kim1d866172011-01-13 00:07:51 +00001540 Type = ELF::R_ARM_GOT_BREL;
1541 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001542 case MCSymbolRefExpr::VK_ARM_TLSGD:
Jason W Kim1d866172011-01-13 00:07:51 +00001543 Type = ELF::R_ARM_TLS_GD32;
1544 break;
Jason W Kimf13743b2010-12-16 03:12:17 +00001545 case MCSymbolRefExpr::VK_ARM_TPOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001546 Type = ELF::R_ARM_TLS_LE32;
1547 break;
Jason W Kima0871e72010-12-08 23:14:44 +00001548 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001549 Type = ELF::R_ARM_TLS_IE32;
1550 break;
Jason W Kimf13743b2010-12-16 03:12:17 +00001551 case MCSymbolRefExpr::VK_None:
Jason W Kim1d866172011-01-13 00:07:51 +00001552 Type = ELF::R_ARM_ABS32;
1553 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001554 case MCSymbolRefExpr::VK_ARM_GOTOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001555 Type = ELF::R_ARM_GOTOFF32;
1556 break;
1557 }
1558 break;
Jim Grosbachdff84b02010-12-02 00:28:45 +00001559 case ARM::fixup_arm_ldst_pcrel_12:
Owen Anderson9d63d902010-12-01 19:18:46 +00001560 case ARM::fixup_arm_pcrel_10:
Jim Grosbachdff84b02010-12-02 00:28:45 +00001561 case ARM::fixup_arm_adr_pcrel_12:
Jim Grosbach662a8162010-12-06 23:57:07 +00001562 case ARM::fixup_arm_thumb_bl:
Jim Grosbachb492a7c2010-12-09 19:50:12 +00001563 case ARM::fixup_arm_thumb_cb:
Bill Wendlingb8958b02010-12-08 01:57:09 +00001564 case ARM::fixup_arm_thumb_cp:
Jim Grosbache2467172010-12-10 18:21:33 +00001565 case ARM::fixup_arm_thumb_br:
Jason W Kim1d866172011-01-13 00:07:51 +00001566 assert(0 && "Unimplemented");
1567 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001568 case ARM::fixup_arm_branch:
Jason W Kimf13743b2010-12-16 03:12:17 +00001569 // FIXME: Differentiate between R_ARM_CALL and
1570 // R_ARM_JUMP24 (latter used for conditional jumps)
Jason W Kim1d866172011-01-13 00:07:51 +00001571 Type = ELF::R_ARM_CALL;
1572 break;
1573 case ARM::fixup_arm_movt_hi16:
1574 Type = ELF::R_ARM_MOVT_ABS;
1575 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001576 case ARM::fixup_arm_movw_lo16:
Jason W Kim1d866172011-01-13 00:07:51 +00001577 Type = ELF::R_ARM_MOVW_ABS_NC;
1578 break;
Evan Chengf3eb3bb2011-01-14 02:38:49 +00001579 case ARM::fixup_t2_movt_hi16:
1580 Type = ELF::R_ARM_THM_MOVT_ABS;
1581 break;
1582 case ARM::fixup_t2_movw_lo16:
1583 Type = ELF::R_ARM_THM_MOVW_ABS_NC;
1584 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001585 }
1586 }
1587
1588 if (RelocNeedsGOT(Modifier))
1589 NeedsGOT = true;
Jason W Kima0871e72010-12-08 23:14:44 +00001590
1591 return Type;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001592}
1593
Wesley Peck4b047132010-11-21 22:06:28 +00001594//===- MBlazeELFObjectWriter -------------------------------------------===//
Jason W Kimd3443e92010-11-15 16:18:39 +00001595
Rafael Espindola31f35782010-12-17 18:01:31 +00001596MBlazeELFObjectWriter::MBlazeELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001597 raw_ostream &_OS,
1598 bool IsLittleEndian)
1599 : ELFObjectWriter(MOTW, _OS, IsLittleEndian) {
Wesley Peck4b047132010-11-21 22:06:28 +00001600}
1601
1602MBlazeELFObjectWriter::~MBlazeELFObjectWriter() {
1603}
1604
Jason W Kim56a39902010-12-06 21:57:34 +00001605unsigned MBlazeELFObjectWriter::GetRelocType(const MCValue &Target,
Wesley Peck4b047132010-11-21 22:06:28 +00001606 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001607 bool IsPCRel,
1608 bool IsRelocWithSymbol,
1609 int64_t Addend) {
Wesley Peck4b047132010-11-21 22:06:28 +00001610 // determine the type of the relocation
1611 unsigned Type;
1612 if (IsPCRel) {
1613 switch ((unsigned)Fixup.getKind()) {
1614 default:
1615 llvm_unreachable("Unimplemented");
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001616 case FK_PCRel_4:
Wesley Peck4b047132010-11-21 22:06:28 +00001617 Type = ELF::R_MICROBLAZE_64_PCREL;
1618 break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001619 case FK_PCRel_2:
Wesley Peck4b047132010-11-21 22:06:28 +00001620 Type = ELF::R_MICROBLAZE_32_PCREL;
1621 break;
1622 }
1623 } else {
1624 switch ((unsigned)Fixup.getKind()) {
1625 default: llvm_unreachable("invalid fixup kind!");
1626 case FK_Data_4:
Jason W Kim56a39902010-12-06 21:57:34 +00001627 Type = ((IsRelocWithSymbol || Addend !=0)
1628 ? ELF::R_MICROBLAZE_32
1629 : ELF::R_MICROBLAZE_64);
Wesley Peck4b047132010-11-21 22:06:28 +00001630 break;
1631 case FK_Data_2:
1632 Type = ELF::R_MICROBLAZE_32;
1633 break;
1634 }
1635 }
Jason W Kim56a39902010-12-06 21:57:34 +00001636 return Type;
Wesley Peck4b047132010-11-21 22:06:28 +00001637}
Jason W Kimd3443e92010-11-15 16:18:39 +00001638
1639//===- X86ELFObjectWriter -------------------------------------------===//
1640
1641
Rafael Espindola31f35782010-12-17 18:01:31 +00001642X86ELFObjectWriter::X86ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001643 raw_ostream &_OS,
1644 bool IsLittleEndian)
1645 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
Jason W Kimd3443e92010-11-15 16:18:39 +00001646{}
1647
1648X86ELFObjectWriter::~X86ELFObjectWriter()
1649{}
1650
Jason W Kim56a39902010-12-06 21:57:34 +00001651unsigned X86ELFObjectWriter::GetRelocType(const MCValue &Target,
1652 const MCFixup &Fixup,
1653 bool IsPCRel,
1654 bool IsRelocWithSymbol,
1655 int64_t Addend) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001656 // determine the type of the relocation
1657
Rafael Espindola12203cc2010-11-21 00:48:25 +00001658 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1659 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
Jason W Kimd3443e92010-11-15 16:18:39 +00001660 unsigned Type;
Rafael Espindolabff66a82010-12-18 03:27:34 +00001661 if (is64Bit()) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001662 if (IsPCRel) {
Rafael Espindola3a83c402010-12-27 00:36:05 +00001663 switch ((unsigned)Fixup.getKind()) {
1664 default: llvm_unreachable("invalid fixup kind!");
1665 case FK_PCRel_8:
1666 assert(Modifier == MCSymbolRefExpr::VK_None);
1667 Type = ELF::R_X86_64_PC64;
Jason W Kimd3443e92010-11-15 16:18:39 +00001668 break;
Rafael Espindola7a549972011-01-01 19:05:35 +00001669 case X86::reloc_signed_4byte:
Rafael Espindolac3a561c2010-12-27 02:03:24 +00001670 case X86::reloc_riprel_4byte_movq_load:
Rafael Espindola3a83c402010-12-27 00:36:05 +00001671 case FK_Data_4: // FIXME?
1672 case X86::reloc_riprel_4byte:
1673 case FK_PCRel_4:
1674 switch (Modifier) {
1675 default:
1676 llvm_unreachable("Unimplemented");
1677 case MCSymbolRefExpr::VK_None:
1678 Type = ELF::R_X86_64_PC32;
1679 break;
1680 case MCSymbolRefExpr::VK_PLT:
1681 Type = ELF::R_X86_64_PLT32;
1682 break;
1683 case MCSymbolRefExpr::VK_GOTPCREL:
1684 Type = ELF::R_X86_64_GOTPCREL;
1685 break;
1686 case MCSymbolRefExpr::VK_GOTTPOFF:
1687 Type = ELF::R_X86_64_GOTTPOFF;
Jason W Kimd3443e92010-11-15 16:18:39 +00001688 break;
Rafael Espindola3a83c402010-12-27 00:36:05 +00001689 case MCSymbolRefExpr::VK_TLSGD:
1690 Type = ELF::R_X86_64_TLSGD;
1691 break;
1692 case MCSymbolRefExpr::VK_TLSLD:
1693 Type = ELF::R_X86_64_TLSLD;
1694 break;
1695 }
Jason W Kimd3443e92010-11-15 16:18:39 +00001696 break;
Rafael Espindola3a83c402010-12-27 00:36:05 +00001697 case FK_PCRel_2:
1698 assert(Modifier == MCSymbolRefExpr::VK_None);
1699 Type = ELF::R_X86_64_PC16;
Jason W Kimd3443e92010-11-15 16:18:39 +00001700 break;
1701 }
1702 } else {
1703 switch ((unsigned)Fixup.getKind()) {
1704 default: llvm_unreachable("invalid fixup kind!");
1705 case FK_Data_8: Type = ELF::R_X86_64_64; break;
1706 case X86::reloc_signed_4byte:
Jason W Kimd3443e92010-11-15 16:18:39 +00001707 assert(isInt<32>(Target.getConstant()));
1708 switch (Modifier) {
1709 default:
1710 llvm_unreachable("Unimplemented");
1711 case MCSymbolRefExpr::VK_None:
1712 Type = ELF::R_X86_64_32S;
1713 break;
1714 case MCSymbolRefExpr::VK_GOT:
1715 Type = ELF::R_X86_64_GOT32;
1716 break;
1717 case MCSymbolRefExpr::VK_GOTPCREL:
1718 Type = ELF::R_X86_64_GOTPCREL;
1719 break;
1720 case MCSymbolRefExpr::VK_TPOFF:
1721 Type = ELF::R_X86_64_TPOFF32;
1722 break;
1723 case MCSymbolRefExpr::VK_DTPOFF:
1724 Type = ELF::R_X86_64_DTPOFF32;
1725 break;
1726 }
1727 break;
1728 case FK_Data_4:
1729 Type = ELF::R_X86_64_32;
1730 break;
1731 case FK_Data_2: Type = ELF::R_X86_64_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001732 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001733 case FK_Data_1: Type = ELF::R_X86_64_8; break;
1734 }
1735 }
1736 } else {
1737 if (IsPCRel) {
1738 switch (Modifier) {
1739 default:
1740 llvm_unreachable("Unimplemented");
1741 case MCSymbolRefExpr::VK_None:
1742 Type = ELF::R_386_PC32;
1743 break;
1744 case MCSymbolRefExpr::VK_PLT:
1745 Type = ELF::R_386_PLT32;
1746 break;
1747 }
1748 } else {
1749 switch ((unsigned)Fixup.getKind()) {
1750 default: llvm_unreachable("invalid fixup kind!");
1751
1752 case X86::reloc_global_offset_table:
1753 Type = ELF::R_386_GOTPC;
1754 break;
1755
1756 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
1757 // instead?
1758 case X86::reloc_signed_4byte:
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001759 case FK_PCRel_4:
Jason W Kimd3443e92010-11-15 16:18:39 +00001760 case FK_Data_4:
1761 switch (Modifier) {
1762 default:
1763 llvm_unreachable("Unimplemented");
1764 case MCSymbolRefExpr::VK_None:
1765 Type = ELF::R_386_32;
1766 break;
1767 case MCSymbolRefExpr::VK_GOT:
1768 Type = ELF::R_386_GOT32;
1769 break;
1770 case MCSymbolRefExpr::VK_GOTOFF:
1771 Type = ELF::R_386_GOTOFF;
1772 break;
1773 case MCSymbolRefExpr::VK_TLSGD:
1774 Type = ELF::R_386_TLS_GD;
1775 break;
1776 case MCSymbolRefExpr::VK_TPOFF:
1777 Type = ELF::R_386_TLS_LE_32;
1778 break;
1779 case MCSymbolRefExpr::VK_INDNTPOFF:
1780 Type = ELF::R_386_TLS_IE;
1781 break;
1782 case MCSymbolRefExpr::VK_NTPOFF:
1783 Type = ELF::R_386_TLS_LE;
1784 break;
1785 case MCSymbolRefExpr::VK_GOTNTPOFF:
1786 Type = ELF::R_386_TLS_GOTIE;
1787 break;
1788 case MCSymbolRefExpr::VK_TLSLDM:
1789 Type = ELF::R_386_TLS_LDM;
1790 break;
1791 case MCSymbolRefExpr::VK_DTPOFF:
1792 Type = ELF::R_386_TLS_LDO_32;
1793 break;
1794 }
1795 break;
1796 case FK_Data_2: Type = ELF::R_386_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001797 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001798 case FK_Data_1: Type = ELF::R_386_8; break;
1799 }
1800 }
1801 }
1802
1803 if (RelocNeedsGOT(Modifier))
1804 NeedsGOT = true;
1805
Jason W Kim56a39902010-12-06 21:57:34 +00001806 return Type;
Matt Fleming3565a062010-08-16 18:57:57 +00001807}