blob: 57ec5492cdad36d3152fca97bc0c20dc62c8382a [file] [log] [blame]
Matt Fleming3565a062010-08-16 18:57:57 +00001//===- lib/MC/ELFObjectWriter.cpp - ELF File Writer -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements ELF object file writer information.
11//
12//===----------------------------------------------------------------------===//
13
Rafael Espindola8f413fa2010-10-05 15:11:03 +000014#include "llvm/ADT/SmallPtrSet.h"
Matt Fleming3565a062010-08-16 18:57:57 +000015#include "llvm/ADT/STLExtras.h"
16#include "llvm/ADT/StringMap.h"
17#include "llvm/ADT/Twine.h"
18#include "llvm/MC/MCAssembler.h"
19#include "llvm/MC/MCAsmLayout.h"
20#include "llvm/MC/MCContext.h"
21#include "llvm/MC/MCELFSymbolFlags.h"
22#include "llvm/MC/MCExpr.h"
Rafael Espindola285b3e52010-12-17 16:59:53 +000023#include "llvm/MC/MCELFObjectWriter.h"
Matt Fleming3565a062010-08-16 18:57:57 +000024#include "llvm/MC/MCObjectWriter.h"
25#include "llvm/MC/MCSectionELF.h"
26#include "llvm/MC/MCSymbol.h"
27#include "llvm/MC/MCValue.h"
28#include "llvm/Support/Debug.h"
29#include "llvm/Support/ErrorHandling.h"
30#include "llvm/Support/ELF.h"
31#include "llvm/Target/TargetAsmBackend.h"
32
33#include "../Target/X86/X86FixupKinds.h"
Jason W Kim4a511f02010-11-22 18:41:13 +000034#include "../Target/ARM/ARMFixupKinds.h"
Matt Fleming3565a062010-08-16 18:57:57 +000035
36#include <vector>
37using namespace llvm;
38
Rafael Espindolaad49cf52010-09-18 15:03:21 +000039static unsigned GetType(const MCSymbolData &SD) {
40 uint32_t Type = (SD.getFlags() & (0xf << ELF_STT_Shift)) >> ELF_STT_Shift;
41 assert(Type == ELF::STT_NOTYPE || Type == ELF::STT_OBJECT ||
42 Type == ELF::STT_FUNC || Type == ELF::STT_SECTION ||
43 Type == ELF::STT_FILE || Type == ELF::STT_COMMON ||
44 Type == ELF::STT_TLS);
45 return Type;
46}
47
Rafael Espindolae15eb4e2010-09-23 19:55:14 +000048static unsigned GetBinding(const MCSymbolData &SD) {
49 uint32_t Binding = (SD.getFlags() & (0xf << ELF_STB_Shift)) >> ELF_STB_Shift;
50 assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
51 Binding == ELF::STB_WEAK);
52 return Binding;
53}
54
55static void SetBinding(MCSymbolData &SD, unsigned Binding) {
56 assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
57 Binding == ELF::STB_WEAK);
58 uint32_t OtherFlags = SD.getFlags() & ~(0xf << ELF_STB_Shift);
59 SD.setFlags(OtherFlags | (Binding << ELF_STB_Shift));
60}
61
Rafael Espindola152c1062010-10-06 21:02:29 +000062static unsigned GetVisibility(MCSymbolData &SD) {
63 unsigned Visibility =
64 (SD.getFlags() & (0xf << ELF_STV_Shift)) >> ELF_STV_Shift;
65 assert(Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_INTERNAL ||
66 Visibility == ELF::STV_HIDDEN || Visibility == ELF::STV_PROTECTED);
67 return Visibility;
68}
69
Wesley Peck4b047132010-11-21 22:06:28 +000070
Rafael Espindolaa0a2f872010-10-28 14:22:44 +000071static bool RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant) {
72 switch (Variant) {
Rafael Espindola5c77c162010-10-05 15:48:37 +000073 default:
74 return false;
Rafael Espindolaa0a2f872010-10-28 14:22:44 +000075 case MCSymbolRefExpr::VK_GOT:
76 case MCSymbolRefExpr::VK_PLT:
77 case MCSymbolRefExpr::VK_GOTPCREL:
78 case MCSymbolRefExpr::VK_TPOFF:
79 case MCSymbolRefExpr::VK_TLSGD:
80 case MCSymbolRefExpr::VK_GOTTPOFF:
81 case MCSymbolRefExpr::VK_INDNTPOFF:
82 case MCSymbolRefExpr::VK_NTPOFF:
83 case MCSymbolRefExpr::VK_GOTNTPOFF:
Rafael Espindolaa264f722010-10-28 14:37:09 +000084 case MCSymbolRefExpr::VK_TLSLDM:
Rafael Espindola0cf15d62010-10-28 14:48:59 +000085 case MCSymbolRefExpr::VK_DTPOFF:
Rafael Espindolab4d17212010-10-28 15:02:40 +000086 case MCSymbolRefExpr::VK_TLSLD:
Rafael Espindola5c77c162010-10-05 15:48:37 +000087 return true;
88 }
89}
90
Rafael Espindola127a6a42010-12-17 07:28:17 +000091static bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) {
92 const MCFixupKindInfo &FKI =
93 Asm.getBackend().getFixupKindInfo((MCFixupKind) Kind);
94
95 return FKI.Flags & MCFixupKindInfo::FKF_IsPCRel;
96}
97
Matt Fleming3565a062010-08-16 18:57:57 +000098namespace {
Daniel Dunbar115a3dd2010-11-13 07:33:40 +000099 class ELFObjectWriter : public MCObjectWriter {
Jason W Kimd3443e92010-11-15 16:18:39 +0000100 protected:
Chris Lattnerb188a372010-08-28 03:21:03 +0000101 /*static bool isFixupKindX86RIPRel(unsigned Kind) {
Matt Fleming3565a062010-08-16 18:57:57 +0000102 return Kind == X86::reloc_riprel_4byte ||
103 Kind == X86::reloc_riprel_4byte_movq_load;
Chris Lattnerb188a372010-08-28 03:21:03 +0000104 }*/
Matt Fleming3565a062010-08-16 18:57:57 +0000105
106
107 /// ELFSymbolData - Helper struct for containing some precomputed information
108 /// on symbols.
109 struct ELFSymbolData {
110 MCSymbolData *SymbolData;
111 uint64_t StringIndex;
112 uint32_t SectionIndex;
113
114 // Support lexicographic sorting.
115 bool operator<(const ELFSymbolData &RHS) const {
Rafael Espindolaad49cf52010-09-18 15:03:21 +0000116 if (GetType(*SymbolData) == ELF::STT_FILE)
117 return true;
118 if (GetType(*RHS.SymbolData) == ELF::STT_FILE)
119 return false;
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000120 return SymbolData->getSymbol().getName() <
121 RHS.SymbolData->getSymbol().getName();
Matt Fleming3565a062010-08-16 18:57:57 +0000122 }
123 };
124
125 /// @name Relocation Data
126 /// @{
127
128 struct ELFRelocationEntry {
129 // Make these big enough for both 32-bit and 64-bit
130 uint64_t r_offset;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000131 int Index;
132 unsigned Type;
133 const MCSymbol *Symbol;
Matt Fleming3565a062010-08-16 18:57:57 +0000134 uint64_t r_addend;
Jason W Kim858e7502010-11-22 18:42:07 +0000135
Jason W Kim4a511f02010-11-22 18:41:13 +0000136 ELFRelocationEntry()
137 : r_offset(0), Index(0), Type(0), Symbol(0), r_addend(0) {}
138
Jason W Kim10907422010-11-22 22:05:16 +0000139 ELFRelocationEntry(uint64_t RelocOffset, int Idx,
140 unsigned RelType, const MCSymbol *Sym,
Jason W Kim4a511f02010-11-22 18:41:13 +0000141 uint64_t Addend)
Jason W Kim10907422010-11-22 22:05:16 +0000142 : r_offset(RelocOffset), Index(Idx), Type(RelType),
143 Symbol(Sym), r_addend(Addend) {}
Matt Fleming3565a062010-08-16 18:57:57 +0000144
145 // Support lexicographic sorting.
146 bool operator<(const ELFRelocationEntry &RE) const {
147 return RE.r_offset < r_offset;
148 }
149 };
150
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000151 SmallPtrSet<const MCSymbol *, 16> UsedInReloc;
Rafael Espindola484291c2010-11-01 14:28:48 +0000152 SmallPtrSet<const MCSymbol *, 16> WeakrefUsedInReloc;
Rafael Espindola88182132010-10-27 15:18:17 +0000153 DenseMap<const MCSymbol *, const MCSymbol *> Renames;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000154
Matt Fleming3565a062010-08-16 18:57:57 +0000155 llvm::DenseMap<const MCSectionData*,
156 std::vector<ELFRelocationEntry> > Relocations;
157 DenseMap<const MCSection*, uint64_t> SectionStringTableIndex;
158
159 /// @}
160 /// @name Symbol Table Data
161 /// @{
162
163 SmallString<256> StringTable;
164 std::vector<ELFSymbolData> LocalSymbolData;
165 std::vector<ELFSymbolData> ExternalSymbolData;
166 std::vector<ELFSymbolData> UndefinedSymbolData;
167
168 /// @}
169
Rafael Espindola5c77c162010-10-05 15:48:37 +0000170 bool NeedsGOT;
171
Rafael Espindola7be2c332010-10-31 00:16:26 +0000172 bool NeedsSymtabShndx;
173
Matt Fleming3565a062010-08-16 18:57:57 +0000174 unsigned Is64Bit : 1;
175
176 bool HasRelocationAddend;
177
Roman Divacky5baf79e2010-09-09 17:57:50 +0000178 Triple::OSType OSType;
179
Wesley Peckeecb8582010-10-22 15:52:49 +0000180 uint16_t EMachine;
181
Matt Fleming3565a062010-08-16 18:57:57 +0000182 // This holds the symbol table index of the last local symbol.
183 unsigned LastLocalSymbolIndex;
184 // This holds the .strtab section index.
185 unsigned StringTableIndex;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000186 // This holds the .symtab section index.
187 unsigned SymbolTableIndex;
Matt Fleming3565a062010-08-16 18:57:57 +0000188
189 unsigned ShstrtabIndex;
190
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000191
192 const MCSymbol *SymbolToReloc(const MCAssembler &Asm,
193 const MCValue &Target,
194 const MCFragment &F) const;
195
Matt Fleming3565a062010-08-16 18:57:57 +0000196 public:
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000197 ELFObjectWriter(raw_ostream &_OS, bool _Is64Bit, bool IsLittleEndian,
198 uint16_t _EMachine, bool _HasRelAddend,
199 Triple::OSType _OSType)
200 : MCObjectWriter(_OS, IsLittleEndian),
201 NeedsGOT(false), NeedsSymtabShndx(false),
Roman Divacky5baf79e2010-09-09 17:57:50 +0000202 Is64Bit(_Is64Bit), HasRelocationAddend(_HasRelAddend),
Wesley Peckeecb8582010-10-22 15:52:49 +0000203 OSType(_OSType), EMachine(_EMachine) {
Matt Fleming3565a062010-08-16 18:57:57 +0000204 }
Jason W Kimd3443e92010-11-15 16:18:39 +0000205
206 virtual ~ELFObjectWriter();
207
Matt Fleming3565a062010-08-16 18:57:57 +0000208 void WriteWord(uint64_t W) {
Chris Lattnerb188a372010-08-28 03:21:03 +0000209 if (Is64Bit)
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000210 Write64(W);
Chris Lattnerb188a372010-08-28 03:21:03 +0000211 else
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000212 Write32(W);
Matt Fleming3565a062010-08-16 18:57:57 +0000213 }
214
Matt Fleming3565a062010-08-16 18:57:57 +0000215 void StringLE16(char *buf, uint16_t Value) {
216 buf[0] = char(Value >> 0);
217 buf[1] = char(Value >> 8);
218 }
219
220 void StringLE32(char *buf, uint32_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000221 StringLE16(buf, uint16_t(Value >> 0));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000222 StringLE16(buf + 2, uint16_t(Value >> 16));
Matt Fleming3565a062010-08-16 18:57:57 +0000223 }
224
225 void StringLE64(char *buf, uint64_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000226 StringLE32(buf, uint32_t(Value >> 0));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000227 StringLE32(buf + 4, uint32_t(Value >> 32));
Matt Fleming3565a062010-08-16 18:57:57 +0000228 }
229
230 void StringBE16(char *buf ,uint16_t Value) {
231 buf[0] = char(Value >> 8);
232 buf[1] = char(Value >> 0);
233 }
234
235 void StringBE32(char *buf, uint32_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000236 StringBE16(buf, uint16_t(Value >> 16));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000237 StringBE16(buf + 2, uint16_t(Value >> 0));
Matt Fleming3565a062010-08-16 18:57:57 +0000238 }
239
240 void StringBE64(char *buf, uint64_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000241 StringBE32(buf, uint32_t(Value >> 32));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000242 StringBE32(buf + 4, uint32_t(Value >> 0));
Matt Fleming3565a062010-08-16 18:57:57 +0000243 }
244
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000245 void String8(MCDataFragment &F, uint8_t Value) {
246 char buf[1];
247 buf[0] = Value;
248 F.getContents() += StringRef(buf, 1);
249 }
250
251 void String16(MCDataFragment &F, uint16_t Value) {
252 char buf[2];
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000253 if (isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000254 StringLE16(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000255 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000256 StringBE16(buf, Value);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000257 F.getContents() += StringRef(buf, 2);
Matt Fleming3565a062010-08-16 18:57:57 +0000258 }
259
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000260 void String32(MCDataFragment &F, uint32_t Value) {
261 char buf[4];
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000262 if (isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000263 StringLE32(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000264 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000265 StringBE32(buf, Value);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000266 F.getContents() += StringRef(buf, 4);
Matt Fleming3565a062010-08-16 18:57:57 +0000267 }
268
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000269 void String64(MCDataFragment &F, uint64_t Value) {
270 char buf[8];
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000271 if (isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000272 StringLE64(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000273 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000274 StringBE64(buf, Value);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000275 F.getContents() += StringRef(buf, 8);
Matt Fleming3565a062010-08-16 18:57:57 +0000276 }
277
Jason W Kimd3443e92010-11-15 16:18:39 +0000278 virtual void WriteHeader(uint64_t SectionDataSize, unsigned NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000279
Jason W Kimd3443e92010-11-15 16:18:39 +0000280 virtual void WriteSymbolEntry(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
Rafael Espindola7be2c332010-10-31 00:16:26 +0000281 uint64_t name, uint8_t info,
Matt Fleming3565a062010-08-16 18:57:57 +0000282 uint64_t value, uint64_t size,
Rafael Espindola7be2c332010-10-31 00:16:26 +0000283 uint8_t other, uint32_t shndx,
284 bool Reserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000285
Jason W Kimd3443e92010-11-15 16:18:39 +0000286 virtual void WriteSymbol(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
Rafael Espindola7be2c332010-10-31 00:16:26 +0000287 ELFSymbolData &MSD,
Matt Fleming3565a062010-08-16 18:57:57 +0000288 const MCAsmLayout &Layout);
289
Rafael Espindolabab2a802010-11-10 21:51:05 +0000290 typedef DenseMap<const MCSectionELF*, uint32_t> SectionIndexMapTy;
Jason W Kimd3443e92010-11-15 16:18:39 +0000291 virtual void WriteSymbolTable(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
Rafael Espindola7be2c332010-10-31 00:16:26 +0000292 const MCAssembler &Asm,
Rafael Espindola71859c62010-09-16 19:46:31 +0000293 const MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000294 const SectionIndexMapTy &SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +0000295
Jason W Kimd3443e92010-11-15 16:18:39 +0000296 virtual void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
Jason W Kim56a39902010-12-06 21:57:34 +0000297 const MCFragment *Fragment, const MCFixup &Fixup,
298 MCValue Target, uint64_t &FixedValue);
Matt Fleming3565a062010-08-16 18:57:57 +0000299
Jason W Kimd3443e92010-11-15 16:18:39 +0000300 virtual uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm,
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000301 const MCSymbol *S);
Matt Fleming3565a062010-08-16 18:57:57 +0000302
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000303 // Map from a group section to the signature symbol
304 typedef DenseMap<const MCSectionELF*, const MCSymbol*> GroupMapTy;
305 // Map from a signature symbol to the group section
306 typedef DenseMap<const MCSymbol*, const MCSectionELF*> RevGroupMapTy;
307
Matt Fleming3565a062010-08-16 18:57:57 +0000308 /// ComputeSymbolTable - Compute the symbol table data
309 ///
310 /// \param StringTable [out] - The string table data.
311 /// \param StringIndexMap [out] - Map from symbol names to offsets in the
312 /// string table.
Jason W Kimd3443e92010-11-15 16:18:39 +0000313 virtual void ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000314 const SectionIndexMapTy &SectionIndexMap,
315 RevGroupMapTy RevGroupMap);
Rafael Espindolabab2a802010-11-10 21:51:05 +0000316
Jason W Kimd3443e92010-11-15 16:18:39 +0000317 virtual void ComputeIndexMap(MCAssembler &Asm,
Rafael Espindolabab2a802010-11-10 21:51:05 +0000318 SectionIndexMapTy &SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +0000319
Jason W Kimd3443e92010-11-15 16:18:39 +0000320 virtual void WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
Matt Fleming3565a062010-08-16 18:57:57 +0000321 const MCSectionData &SD);
322
Jason W Kimd3443e92010-11-15 16:18:39 +0000323 virtual void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout) {
Matt Fleming3565a062010-08-16 18:57:57 +0000324 for (MCAssembler::const_iterator it = Asm.begin(),
325 ie = Asm.end(); it != ie; ++it) {
326 WriteRelocation(Asm, Layout, *it);
327 }
328 }
329
Jason W Kimd3443e92010-11-15 16:18:39 +0000330 virtual void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000331 const SectionIndexMapTy &SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +0000332
Jason W Kimd3443e92010-11-15 16:18:39 +0000333 virtual void CreateGroupSections(MCAssembler &Asm, MCAsmLayout &Layout,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000334 GroupMapTy &GroupMap, RevGroupMapTy &RevGroupMap);
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000335
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000336 virtual void ExecutePostLayoutBinding(MCAssembler &Asm,
337 const MCAsmLayout &Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000338
Jason W Kimd3443e92010-11-15 16:18:39 +0000339 virtual void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
Matt Fleming3565a062010-08-16 18:57:57 +0000340 uint64_t Address, uint64_t Offset,
341 uint64_t Size, uint32_t Link, uint32_t Info,
342 uint64_t Alignment, uint64_t EntrySize);
343
Daniel Dunbar1f3662a2010-12-17 04:54:54 +0000344 virtual void WriteRelocationsFragment(const MCAssembler &Asm,
345 MCDataFragment *F,
346 const MCSectionData *SD);
347
348 virtual bool
349 IsSymbolRefDifferenceFullyResolved(const MCAssembler &Asm,
350 const MCSymbolRefExpr *A,
351 const MCSymbolRefExpr *B) const {
352 // FIXME: Implement this!
353 return false;
354 }
Matt Fleming3565a062010-08-16 18:57:57 +0000355
Jason W Kimd3443e92010-11-15 16:18:39 +0000356 virtual bool IsFixupFullyResolved(const MCAssembler &Asm,
Rafael Espindola70703872010-09-30 02:22:20 +0000357 const MCValue Target,
358 bool IsPCRel,
359 const MCFragment *DF) const;
360
Jason W Kimd3443e92010-11-15 16:18:39 +0000361 virtual void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
362 virtual void WriteSection(MCAssembler &Asm,
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000363 const SectionIndexMapTy &SectionIndexMap,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000364 uint32_t GroupSymbolIndex,
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000365 uint64_t Offset, uint64_t Size, uint64_t Alignment,
366 const MCSectionELF &Section);
Jason W Kim56a39902010-12-06 21:57:34 +0000367
368 protected:
369 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
370 bool IsPCRel, bool IsRelocWithSymbol,
371 int64_t Addend) = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000372 };
373
Jason W Kimd3443e92010-11-15 16:18:39 +0000374 //===- X86ELFObjectWriter -------------------------------------------===//
375
376 class X86ELFObjectWriter : public ELFObjectWriter {
377 public:
378 X86ELFObjectWriter(raw_ostream &_OS, bool _Is64Bit, bool IsLittleEndian,
379 uint16_t _EMachine, bool _HasRelAddend,
380 Triple::OSType _OSType);
Wesley Peck4b047132010-11-21 22:06:28 +0000381
Jason W Kimd3443e92010-11-15 16:18:39 +0000382 virtual ~X86ELFObjectWriter();
Jason W Kim56a39902010-12-06 21:57:34 +0000383 protected:
384 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
385 bool IsPCRel, bool IsRelocWithSymbol,
386 int64_t Addend);
Jason W Kimd3443e92010-11-15 16:18:39 +0000387 };
388
389
390 //===- ARMELFObjectWriter -------------------------------------------===//
391
392 class ARMELFObjectWriter : public ELFObjectWriter {
393 public:
394 ARMELFObjectWriter(raw_ostream &_OS, bool _Is64Bit, bool IsLittleEndian,
395 uint16_t _EMachine, bool _HasRelAddend,
396 Triple::OSType _OSType);
Wesley Peck4b047132010-11-21 22:06:28 +0000397
Jason W Kimd3443e92010-11-15 16:18:39 +0000398 virtual ~ARMELFObjectWriter();
Jason W Kim85fed5e2010-12-01 02:40:06 +0000399 protected:
Jason W Kim56a39902010-12-06 21:57:34 +0000400 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
401 bool IsPCRel, bool IsRelocWithSymbol,
402 int64_t Addend);
Jason W Kimd3443e92010-11-15 16:18:39 +0000403 };
Wesley Peck4b047132010-11-21 22:06:28 +0000404
405 //===- MBlazeELFObjectWriter -------------------------------------------===//
406
407 class MBlazeELFObjectWriter : public ELFObjectWriter {
408 public:
409 MBlazeELFObjectWriter(raw_ostream &_OS, bool _Is64Bit, bool IsLittleEndian,
410 uint16_t _EMachine, bool _HasRelAddend,
411 Triple::OSType _OSType);
412
413 virtual ~MBlazeELFObjectWriter();
Jason W Kim56a39902010-12-06 21:57:34 +0000414 protected:
415 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
416 bool IsPCRel, bool IsRelocWithSymbol,
417 int64_t Addend);
Wesley Peck4b047132010-11-21 22:06:28 +0000418 };
Matt Fleming3565a062010-08-16 18:57:57 +0000419}
420
Jason W Kimd3443e92010-11-15 16:18:39 +0000421ELFObjectWriter::~ELFObjectWriter()
422{}
423
Matt Fleming3565a062010-08-16 18:57:57 +0000424// Emit the ELF header.
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000425void ELFObjectWriter::WriteHeader(uint64_t SectionDataSize,
426 unsigned NumberOfSections) {
Matt Fleming3565a062010-08-16 18:57:57 +0000427 // ELF Header
428 // ----------
429 //
430 // Note
431 // ----
432 // emitWord method behaves differently for ELF32 and ELF64, writing
433 // 4 bytes in the former and 8 in the latter.
434
435 Write8(0x7f); // e_ident[EI_MAG0]
436 Write8('E'); // e_ident[EI_MAG1]
437 Write8('L'); // e_ident[EI_MAG2]
438 Write8('F'); // e_ident[EI_MAG3]
439
440 Write8(Is64Bit ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
441
442 // e_ident[EI_DATA]
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000443 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
Matt Fleming3565a062010-08-16 18:57:57 +0000444
445 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky5baf79e2010-09-09 17:57:50 +0000446 // e_ident[EI_OSABI]
447 switch (OSType) {
448 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
449 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
450 default: Write8(ELF::ELFOSABI_NONE); break;
451 }
Matt Fleming3565a062010-08-16 18:57:57 +0000452 Write8(0); // e_ident[EI_ABIVERSION]
453
454 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
455
456 Write16(ELF::ET_REL); // e_type
457
Wesley Peckeecb8582010-10-22 15:52:49 +0000458 Write16(EMachine); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +0000459
460 Write32(ELF::EV_CURRENT); // e_version
461 WriteWord(0); // e_entry, no entry point in .o file
462 WriteWord(0); // e_phoff, no program header for .o
Benjamin Kramereb976772010-08-17 17:02:29 +0000463 WriteWord(SectionDataSize + (Is64Bit ? sizeof(ELF::Elf64_Ehdr) :
464 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000465
466 // FIXME: Make this configurable.
467 Write32(0); // e_flags = whatever the target wants
468
469 // e_ehsize = ELF header size
470 Write16(Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
471
472 Write16(0); // e_phentsize = prog header entry size
473 Write16(0); // e_phnum = # prog header entries = 0
474
475 // e_shentsize = Section header entry size
476 Write16(Is64Bit ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
477
478 // e_shnum = # of section header ents
Rafael Espindola7be2c332010-10-31 00:16:26 +0000479 if (NumberOfSections >= ELF::SHN_LORESERVE)
480 Write16(0);
481 else
482 Write16(NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000483
484 // e_shstrndx = Section # of '.shstrtab'
Rafael Espindola7be2c332010-10-31 00:16:26 +0000485 if (NumberOfSections >= ELF::SHN_LORESERVE)
486 Write16(ELF::SHN_XINDEX);
487 else
488 Write16(ShstrtabIndex);
Matt Fleming3565a062010-08-16 18:57:57 +0000489}
490
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000491void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
492 MCDataFragment *ShndxF,
493 uint64_t name,
494 uint8_t info, uint64_t value,
495 uint64_t size, uint8_t other,
496 uint32_t shndx,
497 bool Reserved) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000498 if (ShndxF) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000499 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000500 String32(*ShndxF, shndx);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000501 else
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000502 String32(*ShndxF, 0);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000503 }
504
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000505 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
506 uint16_t(ELF::SHN_XINDEX) : shndx;
507
Matt Fleming3565a062010-08-16 18:57:57 +0000508 if (Is64Bit) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000509 String32(*SymtabF, name); // st_name
510 String8(*SymtabF, info); // st_info
511 String8(*SymtabF, other); // st_other
512 String16(*SymtabF, Index); // st_shndx
513 String64(*SymtabF, value); // st_value
514 String64(*SymtabF, size); // st_size
Matt Fleming3565a062010-08-16 18:57:57 +0000515 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000516 String32(*SymtabF, name); // st_name
517 String32(*SymtabF, value); // st_value
518 String32(*SymtabF, size); // st_size
519 String8(*SymtabF, info); // st_info
520 String8(*SymtabF, other); // st_other
521 String16(*SymtabF, Index); // st_shndx
Matt Fleming3565a062010-08-16 18:57:57 +0000522 }
523}
524
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000525static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout) {
526 if (Data.isCommon() && Data.isExternal())
527 return Data.getCommonAlignment();
528
529 const MCSymbol &Symbol = Data.getSymbol();
530 if (!Symbol.isInSection())
531 return 0;
532
Rafael Espindolaffd902b2010-12-06 02:57:26 +0000533 if (Data.getFragment())
534 return Layout.getSymbolOffset(&Data);
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000535
536 return 0;
537}
538
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000539void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
540 const MCAsmLayout &Layout) {
Rafael Espindola88182132010-10-27 15:18:17 +0000541 // The presence of symbol versions causes undefined symbols and
542 // versions declared with @@@ to be renamed.
543
544 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
545 ie = Asm.symbol_end(); it != ie; ++it) {
546 const MCSymbol &Alias = it->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000547 const MCSymbol &Symbol = Alias.AliasedSymbol();
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000548 MCSymbolData &SD = Asm.getSymbolData(Symbol);
549
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000550 // Not an alias.
551 if (&Symbol == &Alias)
552 continue;
553
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000554 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000555 size_t Pos = AliasName.find('@');
556 if (Pos == StringRef::npos)
557 continue;
558
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000559 // Aliases defined with .symvar copy the binding from the symbol they alias.
560 // This is the first place we are able to copy this information.
561 it->setExternal(SD.isExternal());
562 SetBinding(*it, GetBinding(SD));
563
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000564 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000565 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
566 continue;
567
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000568 // FIXME: produce a better error message.
569 if (Symbol.isUndefined() && Rest.startswith("@@") &&
570 !Rest.startswith("@@@"))
571 report_fatal_error("A @@ version cannot be undefined");
572
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000573 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000574 }
575}
576
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000577void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
578 MCDataFragment *ShndxF,
579 ELFSymbolData &MSD,
580 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000581 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000582 MCSymbolData &Data =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000583 Layout.getAssembler().getSymbolData(OrigData.getSymbol().AliasedSymbol());
Rafael Espindola152c1062010-10-06 21:02:29 +0000584
Rafael Espindola7be2c332010-10-31 00:16:26 +0000585 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
586 Data.getSymbol().isVariable();
587
Rafael Espindola152c1062010-10-06 21:02:29 +0000588 uint8_t Binding = GetBinding(OrigData);
589 uint8_t Visibility = GetVisibility(OrigData);
590 uint8_t Type = GetType(Data);
591
592 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
593 uint8_t Other = Visibility;
594
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000595 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000596 uint64_t Size = 0;
597 const MCExpr *ESize;
598
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000599 assert(!(Data.isCommon() && !Data.isExternal()));
600
Matt Fleming3565a062010-08-16 18:57:57 +0000601 ESize = Data.getSize();
602 if (Data.getSize()) {
603 MCValue Res;
604 if (ESize->getKind() == MCExpr::Binary) {
605 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(ESize);
606
607 if (BE->EvaluateAsRelocatable(Res, &Layout)) {
Benjamin Kramer24f12062010-10-17 07:38:40 +0000608 assert(!Res.getSymA() || !Res.getSymA()->getSymbol().isDefined());
609 assert(!Res.getSymB() || !Res.getSymB()->getSymbol().isDefined());
Rafael Espindolaf230df92010-10-16 18:23:53 +0000610 Size = Res.getConstant();
Matt Fleming3565a062010-08-16 18:57:57 +0000611 }
612 } else if (ESize->getKind() == MCExpr::Constant) {
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000613 Size = static_cast<const MCConstantExpr *>(ESize)->getValue();
Matt Fleming3565a062010-08-16 18:57:57 +0000614 } else {
615 assert(0 && "Unsupported size expression");
616 }
617 }
618
619 // Write out the symbol table entry
Rafael Espindola7be2c332010-10-31 00:16:26 +0000620 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
621 Size, Other, MSD.SectionIndex, IsReserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000622}
623
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000624void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
625 MCDataFragment *ShndxF,
626 const MCAssembler &Asm,
627 const MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000628 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000629 // The string table must be emitted first because we need the index
630 // into the string table for all the symbol names.
631 assert(StringTable.size() && "Missing string table");
632
633 // FIXME: Make sure the start of the symbol table is aligned.
634
635 // The first entry is the undefined symbol entry.
Rafael Espindola7be2c332010-10-31 00:16:26 +0000636 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
Matt Fleming3565a062010-08-16 18:57:57 +0000637
638 // Write the symbol table entries.
639 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
640 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
641 ELFSymbolData &MSD = LocalSymbolData[i];
Rafael Espindola7be2c332010-10-31 00:16:26 +0000642 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000643 }
644
Rafael Espindola71859c62010-09-16 19:46:31 +0000645 // Write out a symbol table entry for each regular section.
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000646 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
647 ++i) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000648 const MCSectionELF &Section =
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000649 static_cast<const MCSectionELF&>(i->getSection());
650 if (Section.getType() == ELF::SHT_RELA ||
651 Section.getType() == ELF::SHT_REL ||
652 Section.getType() == ELF::SHT_STRTAB ||
653 Section.getType() == ELF::SHT_SYMTAB)
Eli Friedmana44fa242010-08-16 21:17:09 +0000654 continue;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000655 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000656 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section), false);
Eli Friedmana44fa242010-08-16 21:17:09 +0000657 LastLocalSymbolIndex++;
658 }
Matt Fleming3565a062010-08-16 18:57:57 +0000659
660 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
661 ELFSymbolData &MSD = ExternalSymbolData[i];
662 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000663 assert(((Data.getFlags() & ELF_STB_Global) ||
664 (Data.getFlags() & ELF_STB_Weak)) &&
665 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000666 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000667 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000668 LastLocalSymbolIndex++;
669 }
670
671 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
672 ELFSymbolData &MSD = UndefinedSymbolData[i];
673 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000674 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000675 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000676 LastLocalSymbolIndex++;
677 }
678}
679
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000680const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm,
681 const MCValue &Target,
682 const MCFragment &F) const {
683 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000684 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
685 const MCSymbol *Renamed = Renames.lookup(&Symbol);
686 const MCSymbolData &SD = Asm.getSymbolData(Symbol);
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000687
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000688 if (ASymbol.isUndefined()) {
689 if (Renamed)
690 return Renamed;
691 return &ASymbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000692 }
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000693
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000694 if (SD.isExternal()) {
695 if (Renamed)
696 return Renamed;
697 return &Symbol;
698 }
Rafael Espindola73ffea42010-09-25 05:42:19 +0000699
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000700 const MCSectionELF &Section =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000701 static_cast<const MCSectionELF&>(ASymbol.getSection());
Rafael Espindola25958732010-11-24 21:57:39 +0000702 const SectionKind secKind = Section.getKind();
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000703
Rafael Espindola25958732010-11-24 21:57:39 +0000704 if (secKind.isBSS())
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000705 return NULL;
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000706
Rafael Espindola25958732010-11-24 21:57:39 +0000707 if (secKind.isThreadLocal()) {
708 if (Renamed)
709 return Renamed;
710 return &Symbol;
711 }
712
Rafael Espindola8cecf252010-10-06 16:23:36 +0000713 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000714 const MCSectionELF &Sec2 =
715 static_cast<const MCSectionELF&>(F.getParent()->getSection());
716
Rafael Espindola8cecf252010-10-06 16:23:36 +0000717 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000718 (Kind == MCSymbolRefExpr::VK_PLT ||
719 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
Rafael Espindola25958732010-11-24 21:57:39 +0000720 Kind == MCSymbolRefExpr::VK_GOTOFF)) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000721 if (Renamed)
722 return Renamed;
723 return &Symbol;
724 }
Rafael Espindola3729d002010-10-05 23:57:26 +0000725
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000726 if (Section.getFlags() & MCSectionELF::SHF_MERGE) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000727 if (Target.getConstant() == 0)
728 return NULL;
729 if (Renamed)
730 return Renamed;
731 return &Symbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000732 }
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000733
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000734 return NULL;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000735}
736
Matt Fleming3565a062010-08-16 18:57:57 +0000737
Jason W Kim56a39902010-12-06 21:57:34 +0000738void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
739 const MCAsmLayout &Layout,
740 const MCFragment *Fragment,
741 const MCFixup &Fixup,
742 MCValue Target,
743 uint64_t &FixedValue) {
744 int64_t Addend = 0;
745 int Index = 0;
746 int64_t Value = Target.getConstant();
747 const MCSymbol *RelocSymbol = NULL;
748
Rafael Espindola127a6a42010-12-17 07:28:17 +0000749 bool IsPCRel = isFixupKindPCRel(Asm, Fixup.getKind());
Jason W Kim56a39902010-12-06 21:57:34 +0000750 if (!Target.isAbsolute()) {
751 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
752 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
753 RelocSymbol = SymbolToReloc(Asm, Target, *Fragment);
754
755 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
756 const MCSymbol &SymbolB = RefB->getSymbol();
757 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
758 IsPCRel = true;
759
760 // Offset of the symbol in the section
761 int64_t a = Layout.getSymbolOffset(&SDB);
762
763 // Ofeset of the relocation in the section
764 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
765 Value += b - a;
766 }
767
768 if (!RelocSymbol) {
769 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
770 MCFragment *F = SD.getFragment();
771
772 Index = F->getParent()->getOrdinal() + 1;
773
774 // Offset of the symbol in the section
775 Value += Layout.getSymbolOffset(&SD);
776 } else {
777 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
778 WeakrefUsedInReloc.insert(RelocSymbol);
779 else
780 UsedInReloc.insert(RelocSymbol);
781 Index = -1;
782 }
783 Addend = Value;
784 // Compensate for the addend on i386.
785 if (Is64Bit)
786 Value = 0;
787 }
788
789 FixedValue = Value;
790 unsigned Type = GetRelocType(Target, Fixup, IsPCRel,
791 (RelocSymbol != 0), Addend);
792
793 uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
794 Fixup.getOffset();
795
796 if (!HasRelocationAddend) Addend = 0;
797 ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend);
798 Relocations[Fragment->getParent()].push_back(ERE);
799}
800
801
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000802uint64_t
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000803ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
804 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000805 MCSymbolData &SD = Asm.getSymbolData(*S);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000806 return SD.getIndex();
Matt Fleming3565a062010-08-16 18:57:57 +0000807}
808
Rafael Espindola737cd212010-10-05 18:01:23 +0000809static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
Rafael Espindola88182132010-10-27 15:18:17 +0000810 bool Used, bool Renamed) {
Rafael Espindola484291c2010-11-01 14:28:48 +0000811 if (Data.getFlags() & ELF_Other_Weakref)
812 return false;
813
Rafael Espindolabd701182010-10-19 19:31:37 +0000814 if (Used)
815 return true;
816
Rafael Espindola88182132010-10-27 15:18:17 +0000817 if (Renamed)
818 return false;
819
Rafael Espindola737cd212010-10-05 18:01:23 +0000820 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000821
Rafael Espindolad1798862010-10-29 23:09:31 +0000822 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
823 return true;
824
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000825 const MCSymbol &A = Symbol.AliasedSymbol();
Rafael Espindolad1798862010-10-29 23:09:31 +0000826 if (!A.isVariable() && A.isUndefined() && !Data.isCommon())
Rafael Espindolaa6866962010-10-27 14:44:52 +0000827 return false;
828
Rafael Espindola737cd212010-10-05 18:01:23 +0000829 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
830 return false;
831
Rafael Espindolabd701182010-10-19 19:31:37 +0000832 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000833 return false;
834
835 return true;
836}
837
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000838static bool isLocal(const MCSymbolData &Data, bool isSignature,
839 bool isUsedInReloc) {
Rafael Espindola737cd212010-10-05 18:01:23 +0000840 if (Data.isExternal())
841 return false;
842
843 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000844 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000845
846 if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
847 if (isSignature && !isUsedInReloc)
848 return true;
849
Rafael Espindola737cd212010-10-05 18:01:23 +0000850 return false;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000851 }
Rafael Espindola737cd212010-10-05 18:01:23 +0000852
853 return true;
854}
855
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000856void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
857 SectionIndexMapTy &SectionIndexMap) {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000858 unsigned Index = 1;
859 for (MCAssembler::iterator it = Asm.begin(),
860 ie = Asm.end(); it != ie; ++it) {
861 const MCSectionELF &Section =
862 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000863 if (Section.getType() != ELF::SHT_GROUP)
864 continue;
865 SectionIndexMap[&Section] = Index++;
866 }
867
868 for (MCAssembler::iterator it = Asm.begin(),
869 ie = Asm.end(); it != ie; ++it) {
870 const MCSectionELF &Section =
871 static_cast<const MCSectionELF &>(it->getSection());
872 if (Section.getType() == ELF::SHT_GROUP)
873 continue;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000874 SectionIndexMap[&Section] = Index++;
875 }
876}
877
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000878void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000879 const SectionIndexMapTy &SectionIndexMap,
880 RevGroupMapTy RevGroupMap) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000881 // FIXME: Is this the correct place to do this?
882 if (NeedsGOT) {
883 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
884 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
885 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
886 Data.setExternal(true);
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000887 SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindola5c77c162010-10-05 15:48:37 +0000888 }
889
Matt Fleming3565a062010-08-16 18:57:57 +0000890 // Build section lookup table.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000891 int NumRegularSections = Asm.size();
Matt Fleming3565a062010-08-16 18:57:57 +0000892
893 // Index 0 is always the empty string.
894 StringMap<uint64_t> StringIndexMap;
895 StringTable += '\x00';
896
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000897 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000898 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
899 ie = Asm.symbol_end(); it != ie; ++it) {
900 const MCSymbol &Symbol = it->getSymbol();
901
Rafael Espindola484291c2010-11-01 14:28:48 +0000902 bool Used = UsedInReloc.count(&Symbol);
903 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000904 bool isSignature = RevGroupMap.count(&Symbol);
905
906 if (!isInSymtab(Asm, *it,
907 Used || WeakrefUsed || isSignature,
Rafael Espindola88182132010-10-27 15:18:17 +0000908 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000909 continue;
910
Matt Fleming3565a062010-08-16 18:57:57 +0000911 ELFSymbolData MSD;
912 MSD.SymbolData = it;
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000913 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Matt Fleming3565a062010-08-16 18:57:57 +0000914
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000915 // Undefined symbols are global, but this is the first place we
916 // are able to set it.
917 bool Local = isLocal(*it, isSignature, Used);
918 if (!Local && GetBinding(*it) == ELF::STB_LOCAL) {
919 MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
920 SetBinding(*it, ELF::STB_GLOBAL);
921 SetBinding(SD, ELF::STB_GLOBAL);
922 }
923
Rafael Espindola484291c2010-11-01 14:28:48 +0000924 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
925 SetBinding(*it, ELF::STB_WEAK);
926
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000927 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000928 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000929 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000930 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000931 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000932 } else if (RefSymbol.isUndefined()) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000933 if (isSignature && !Used)
934 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
935 else
936 MSD.SectionIndex = ELF::SHN_UNDEF;
Matt Fleming3565a062010-08-16 18:57:57 +0000937 } else {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000938 const MCSectionELF &Section =
939 static_cast<const MCSectionELF&>(RefSymbol.getSection());
940 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000941 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
942 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000943 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000944 }
945
Rafael Espindola88182132010-10-27 15:18:17 +0000946 // The @@@ in symbol version is replaced with @ in undefined symbols and
947 // @@ in defined ones.
948 StringRef Name = Symbol.getName();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000949 SmallString<32> Buf;
950
Rafael Espindola88182132010-10-27 15:18:17 +0000951 size_t Pos = Name.find("@@@");
Rafael Espindola88182132010-10-27 15:18:17 +0000952 if (Pos != StringRef::npos) {
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000953 Buf += Name.substr(0, Pos);
954 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
955 Buf += Name.substr(Pos + Skip);
956 Name = Buf;
Rafael Espindola88182132010-10-27 15:18:17 +0000957 }
958
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000959 uint64_t &Entry = StringIndexMap[Name];
Rafael Espindolaa6866962010-10-27 14:44:52 +0000960 if (!Entry) {
961 Entry = StringTable.size();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000962 StringTable += Name;
Rafael Espindolaa6866962010-10-27 14:44:52 +0000963 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +0000964 }
Rafael Espindolaa6866962010-10-27 14:44:52 +0000965 MSD.StringIndex = Entry;
966 if (MSD.SectionIndex == ELF::SHN_UNDEF)
967 UndefinedSymbolData.push_back(MSD);
968 else if (Local)
969 LocalSymbolData.push_back(MSD);
970 else
971 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +0000972 }
973
974 // Symbols are required to be in lexicographic order.
975 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
976 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
977 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
978
979 // Set the symbol indices. Local symbols must come before all other
980 // symbols with non-local bindings.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000981 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000982 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
983 LocalSymbolData[i].SymbolData->setIndex(Index++);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000984
985 Index += NumRegularSections;
986
Matt Fleming3565a062010-08-16 18:57:57 +0000987 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
988 ExternalSymbolData[i].SymbolData->setIndex(Index++);
989 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
990 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
991}
992
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000993void ELFObjectWriter::WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
994 const MCSectionData &SD) {
Matt Fleming3565a062010-08-16 18:57:57 +0000995 if (!Relocations[&SD].empty()) {
996 MCContext &Ctx = Asm.getContext();
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000997 const MCSectionELF *RelaSection;
Matt Fleming3565a062010-08-16 18:57:57 +0000998 const MCSectionELF &Section =
999 static_cast<const MCSectionELF&>(SD.getSection());
1000
1001 const StringRef SectionName = Section.getSectionName();
Benjamin Kramer377a5722010-08-17 17:30:07 +00001002 std::string RelaSectionName = HasRelocationAddend ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +00001003 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +00001004
1005 unsigned EntrySize;
1006 if (HasRelocationAddend)
1007 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
1008 else
1009 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +00001010
Benjamin Kramer377a5722010-08-17 17:30:07 +00001011 RelaSection = Ctx.getELFSection(RelaSectionName, HasRelocationAddend ?
1012 ELF::SHT_RELA : ELF::SHT_REL, 0,
Matt Fleming3565a062010-08-16 18:57:57 +00001013 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001014 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001015
1016 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001017 RelaSD.setAlignment(Is64Bit ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +00001018
1019 MCDataFragment *F = new MCDataFragment(&RelaSD);
1020
1021 WriteRelocationsFragment(Asm, F, &SD);
Matt Fleming3565a062010-08-16 18:57:57 +00001022 }
1023}
1024
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001025void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
1026 uint64_t Flags, uint64_t Address,
1027 uint64_t Offset, uint64_t Size,
1028 uint32_t Link, uint32_t Info,
1029 uint64_t Alignment,
1030 uint64_t EntrySize) {
Matt Fleming3565a062010-08-16 18:57:57 +00001031 Write32(Name); // sh_name: index into string table
1032 Write32(Type); // sh_type
1033 WriteWord(Flags); // sh_flags
1034 WriteWord(Address); // sh_addr
1035 WriteWord(Offset); // sh_offset
1036 WriteWord(Size); // sh_size
1037 Write32(Link); // sh_link
1038 Write32(Info); // sh_info
1039 WriteWord(Alignment); // sh_addralign
1040 WriteWord(EntrySize); // sh_entsize
1041}
1042
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001043void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
1044 MCDataFragment *F,
1045 const MCSectionData *SD) {
Matt Fleming3565a062010-08-16 18:57:57 +00001046 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
1047 // sort by the r_offset just like gnu as does
1048 array_pod_sort(Relocs.begin(), Relocs.end());
1049
1050 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1051 ELFRelocationEntry entry = Relocs[e - i - 1];
1052
Rafael Espindola12203cc2010-11-21 00:48:25 +00001053 if (!entry.Index)
1054 ;
1055 else if (entry.Index < 0)
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001056 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
1057 else
Rafael Espindola12203cc2010-11-21 00:48:25 +00001058 entry.Index += LocalSymbolData.size();
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001059 if (Is64Bit) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001060 String64(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001061
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001062 struct ELF::Elf64_Rela ERE64;
1063 ERE64.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001064 String64(*F, ERE64.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001065
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001066 if (HasRelocationAddend)
1067 String64(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001068 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001069 String32(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001070
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001071 struct ELF::Elf32_Rela ERE32;
1072 ERE32.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001073 String32(*F, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001074
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001075 if (HasRelocationAddend)
1076 String32(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001077 }
Matt Fleming3565a062010-08-16 18:57:57 +00001078 }
1079}
1080
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001081void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
1082 MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001083 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +00001084 MCContext &Ctx = Asm.getContext();
1085 MCDataFragment *F;
1086
Matt Fleming3565a062010-08-16 18:57:57 +00001087 unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
1088
Rafael Espindola38738bf2010-09-22 19:04:41 +00001089 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001090 const MCSectionELF *ShstrtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001091 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001092 SectionKind::getReadOnly());
Rafael Espindola71859c62010-09-16 19:46:31 +00001093 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1094 ShstrtabSD.setAlignment(1);
1095 ShstrtabIndex = Asm.size();
1096
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001097 const MCSectionELF *SymtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001098 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1099 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001100 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001101 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Matt Fleming3565a062010-08-16 18:57:57 +00001102 SymtabSD.setAlignment(Is64Bit ? 8 : 4);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001103 SymbolTableIndex = Asm.size();
1104
1105 MCSectionData *SymtabShndxSD = NULL;
1106
1107 if (NeedsSymtabShndx) {
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001108 const MCSectionELF *SymtabShndxSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001109 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001110 SectionKind::getReadOnly(), 4, "");
Rafael Espindola7be2c332010-10-31 00:16:26 +00001111 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
1112 SymtabShndxSD->setAlignment(4);
1113 }
Matt Fleming3565a062010-08-16 18:57:57 +00001114
Matt Fleming3565a062010-08-16 18:57:57 +00001115 const MCSection *StrtabSection;
1116 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001117 SectionKind::getReadOnly());
Matt Fleming3565a062010-08-16 18:57:57 +00001118 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1119 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +00001120 StringTableIndex = Asm.size();
1121
Rafael Espindolac3c413f2010-09-27 22:04:54 +00001122 WriteRelocations(Asm, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001123
1124 // Symbol table
1125 F = new MCDataFragment(&SymtabSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001126 MCDataFragment *ShndxF = NULL;
1127 if (NeedsSymtabShndx) {
1128 ShndxF = new MCDataFragment(SymtabShndxSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001129 }
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001130 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
Rafael Espindola71859c62010-09-16 19:46:31 +00001131
Matt Fleming3565a062010-08-16 18:57:57 +00001132 F = new MCDataFragment(&StrtabSD);
1133 F->getContents().append(StringTable.begin(), StringTable.end());
Matt Fleming3565a062010-08-16 18:57:57 +00001134
Matt Fleming3565a062010-08-16 18:57:57 +00001135 F = new MCDataFragment(&ShstrtabSD);
1136
Matt Fleming3565a062010-08-16 18:57:57 +00001137 // Section header string table.
1138 //
1139 // The first entry of a string table holds a null character so skip
1140 // section 0.
1141 uint64_t Index = 1;
1142 F->getContents() += '\x00';
1143
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001144 StringMap<uint64_t> SecStringMap;
Matt Fleming3565a062010-08-16 18:57:57 +00001145 for (MCAssembler::const_iterator it = Asm.begin(),
1146 ie = Asm.end(); it != ie; ++it) {
Matt Fleming3565a062010-08-16 18:57:57 +00001147 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +00001148 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola51efe7a2010-09-23 14:14:56 +00001149 // FIXME: We could merge suffixes like in .text and .rela.text.
Matt Fleming3565a062010-08-16 18:57:57 +00001150
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001151 StringRef Name = Section.getSectionName();
1152 if (SecStringMap.count(Name)) {
1153 SectionStringTableIndex[&Section] = SecStringMap[Name];
1154 continue;
1155 }
Matt Fleming3565a062010-08-16 18:57:57 +00001156 // Remember the index into the string table so we can write it
1157 // into the sh_name field of the section header table.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001158 SectionStringTableIndex[&Section] = Index;
1159 SecStringMap[Name] = Index;
Matt Fleming3565a062010-08-16 18:57:57 +00001160
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001161 Index += Name.size() + 1;
1162 F->getContents() += Name;
Matt Fleming3565a062010-08-16 18:57:57 +00001163 F->getContents() += '\x00';
1164 }
Rafael Espindola70703872010-09-30 02:22:20 +00001165}
1166
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001167bool ELFObjectWriter::IsFixupFullyResolved(const MCAssembler &Asm,
1168 const MCValue Target,
1169 bool IsPCRel,
1170 const MCFragment *DF) const {
Rafael Espindola70703872010-09-30 02:22:20 +00001171 // If this is a PCrel relocation, find the section this fixup value is
1172 // relative to.
1173 const MCSection *BaseSection = 0;
1174 if (IsPCRel) {
1175 BaseSection = &DF->getParent()->getSection();
1176 assert(BaseSection);
1177 }
1178
1179 const MCSection *SectionA = 0;
1180 const MCSymbol *SymbolA = 0;
1181 if (const MCSymbolRefExpr *A = Target.getSymA()) {
Rafael Espindola2c920852010-11-16 04:11:46 +00001182 SymbolA = &A->getSymbol();
1183 SectionA = &SymbolA->AliasedSymbol().getSection();
Rafael Espindola70703872010-09-30 02:22:20 +00001184 }
1185
1186 const MCSection *SectionB = 0;
Rafael Espindola12203cc2010-11-21 00:48:25 +00001187 const MCSymbol *SymbolB = 0;
Rafael Espindola70703872010-09-30 02:22:20 +00001188 if (const MCSymbolRefExpr *B = Target.getSymB()) {
Rafael Espindola12203cc2010-11-21 00:48:25 +00001189 SymbolB = &B->getSymbol();
1190 SectionB = &SymbolB->AliasedSymbol().getSection();
Rafael Espindola70703872010-09-30 02:22:20 +00001191 }
1192
1193 if (!BaseSection)
1194 return SectionA == SectionB;
1195
Rafael Espindola12203cc2010-11-21 00:48:25 +00001196 if (SymbolB)
1197 return false;
1198
1199 // Absolute address but PCrel instruction, so we need a relocation.
1200 if (!SymbolA)
1201 return false;
1202
Rafael Espindola2c920852010-11-16 04:11:46 +00001203 // FIXME: This is in here just to match gnu as output. If the two ends
1204 // are in the same section, there is nothing that the linker can do to
1205 // break it.
Rafael Espindola70703872010-09-30 02:22:20 +00001206 const MCSymbolData &DataA = Asm.getSymbolData(*SymbolA);
1207 if (DataA.isExternal())
1208 return false;
1209
Rafael Espindola12203cc2010-11-21 00:48:25 +00001210 return BaseSection == SectionA;
Matt Fleming3565a062010-08-16 18:57:57 +00001211}
1212
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001213void ELFObjectWriter::CreateGroupSections(MCAssembler &Asm,
1214 MCAsmLayout &Layout,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001215 GroupMapTy &GroupMap,
1216 RevGroupMapTy &RevGroupMap) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001217 // Build the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001218 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1219 it != ie; ++it) {
1220 const MCSectionELF &Section =
1221 static_cast<const MCSectionELF&>(it->getSection());
1222 if (!(Section.getFlags() & MCSectionELF::SHF_GROUP))
1223 continue;
1224
1225 const MCSymbol *SignatureSymbol = Section.getGroup();
1226 Asm.getOrCreateSymbolData(*SignatureSymbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001227 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001228 if (!Group) {
1229 Group = Asm.getContext().CreateELFGroupSection();
1230 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1231 Data.setAlignment(4);
1232 MCDataFragment *F = new MCDataFragment(&Data);
1233 String32(*F, ELF::GRP_COMDAT);
1234 }
1235 GroupMap[Group] = SignatureSymbol;
1236 }
1237
1238 // Add sections to the groups
1239 unsigned Index = 1;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001240 unsigned NumGroups = RevGroupMap.size();
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001241 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1242 it != ie; ++it, ++Index) {
1243 const MCSectionELF &Section =
1244 static_cast<const MCSectionELF&>(it->getSection());
1245 if (!(Section.getFlags() & MCSectionELF::SHF_GROUP))
1246 continue;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001247 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001248 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1249 // FIXME: we could use the previous fragment
1250 MCDataFragment *F = new MCDataFragment(&Data);
1251 String32(*F, NumGroups + Index);
1252 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001253}
1254
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001255void ELFObjectWriter::WriteSection(MCAssembler &Asm,
1256 const SectionIndexMapTy &SectionIndexMap,
1257 uint32_t GroupSymbolIndex,
1258 uint64_t Offset, uint64_t Size,
1259 uint64_t Alignment,
1260 const MCSectionELF &Section) {
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001261 uint64_t sh_link = 0;
1262 uint64_t sh_info = 0;
1263
1264 switch(Section.getType()) {
1265 case ELF::SHT_DYNAMIC:
1266 sh_link = SectionStringTableIndex[&Section];
1267 sh_info = 0;
1268 break;
1269
1270 case ELF::SHT_REL:
1271 case ELF::SHT_RELA: {
1272 const MCSectionELF *SymtabSection;
1273 const MCSectionELF *InfoSection;
1274 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
1275 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001276 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001277 sh_link = SectionIndexMap.lookup(SymtabSection);
1278 assert(sh_link && ".symtab not found");
1279
1280 // Remove ".rel" and ".rela" prefixes.
1281 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1282 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
1283
1284 InfoSection = Asm.getContext().getELFSection(SectionName,
1285 ELF::SHT_PROGBITS, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001286 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001287 sh_info = SectionIndexMap.lookup(InfoSection);
1288 break;
1289 }
1290
1291 case ELF::SHT_SYMTAB:
1292 case ELF::SHT_DYNSYM:
1293 sh_link = StringTableIndex;
1294 sh_info = LastLocalSymbolIndex;
1295 break;
1296
1297 case ELF::SHT_SYMTAB_SHNDX:
1298 sh_link = SymbolTableIndex;
1299 break;
1300
1301 case ELF::SHT_PROGBITS:
1302 case ELF::SHT_STRTAB:
1303 case ELF::SHT_NOBITS:
1304 case ELF::SHT_NULL:
1305 case ELF::SHT_ARM_ATTRIBUTES:
1306 // Nothing to do.
1307 break;
1308
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001309 case ELF::SHT_GROUP: {
1310 sh_link = SymbolTableIndex;
1311 sh_info = GroupSymbolIndex;
1312 break;
1313 }
1314
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001315 default:
1316 assert(0 && "FIXME: sh_type value not supported!");
1317 break;
1318 }
1319
1320 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1321 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1322 Alignment, Section.getEntrySize());
1323}
1324
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001325static bool IsELFMetaDataSection(const MCSectionData &SD) {
Rafael Espindolaf8803fe2010-12-06 03:48:09 +00001326 return SD.getOrdinal() == ~UINT32_C(0) &&
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001327 !SD.getSection().isVirtualSection();
1328}
1329
1330static uint64_t DataSectionSize(const MCSectionData &SD) {
1331 uint64_t Ret = 0;
1332 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1333 ++i) {
1334 const MCFragment &F = *i;
1335 assert(F.getKind() == MCFragment::FT_Data);
1336 Ret += cast<MCDataFragment>(F).getContents().size();
1337 }
1338 return Ret;
1339}
1340
1341static uint64_t GetSectionFileSize(const MCAsmLayout &Layout,
1342 const MCSectionData &SD) {
1343 if (IsELFMetaDataSection(SD))
1344 return DataSectionSize(SD);
1345 return Layout.getSectionFileSize(&SD);
1346}
1347
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001348static uint64_t GetSectionAddressSize(const MCAsmLayout &Layout,
1349 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001350 if (IsELFMetaDataSection(SD))
1351 return DataSectionSize(SD);
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001352 return Layout.getSectionAddressSize(&SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001353}
1354
1355static void WriteDataSectionData(ELFObjectWriter *W, const MCSectionData &SD) {
1356 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1357 ++i) {
1358 const MCFragment &F = *i;
1359 assert(F.getKind() == MCFragment::FT_Data);
1360 W->WriteBytes(cast<MCDataFragment>(F).getContents().str());
1361 }
1362}
1363
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001364void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1365 const MCAsmLayout &Layout) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001366 GroupMapTy GroupMap;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001367 RevGroupMapTy RevGroupMap;
1368 CreateGroupSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1369 RevGroupMap);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001370
Rafael Espindolabab2a802010-11-10 21:51:05 +00001371 SectionIndexMapTy SectionIndexMap;
1372
1373 ComputeIndexMap(Asm, SectionIndexMap);
1374
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001375 // Compute symbol table information.
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001376 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap);
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001377
Matt Fleming3565a062010-08-16 18:57:57 +00001378 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001379 const_cast<MCAsmLayout&>(Layout),
1380 SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +00001381
Rafael Espindola1d739a02010-11-10 22:34:07 +00001382 // Update to include the metadata sections.
1383 ComputeIndexMap(Asm, SectionIndexMap);
1384
Matt Fleming3565a062010-08-16 18:57:57 +00001385 // Add 1 for the null section.
1386 unsigned NumSections = Asm.size() + 1;
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001387 uint64_t NaturalAlignment = Is64Bit ? 8 : 4;
1388 uint64_t HeaderSize = Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr);
1389 uint64_t FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001390
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001391 std::vector<const MCSectionELF*> Sections;
1392 Sections.resize(NumSections);
1393
1394 for (SectionIndexMapTy::const_iterator i=
1395 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1396 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
1397 Sections[p.second] = p.first;
1398 }
1399
1400 for (unsigned i = 1; i < NumSections; ++i) {
1401 const MCSectionELF &Section = *Sections[i];
1402 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001403
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001404 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1405
Matt Fleming3565a062010-08-16 18:57:57 +00001406 // Get the size of the section in the output file (including padding).
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001407 FileOff += GetSectionFileSize(Layout, SD);
Matt Fleming3565a062010-08-16 18:57:57 +00001408 }
1409
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001410 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1411
Matt Fleming3565a062010-08-16 18:57:57 +00001412 // Write out the ELF header ...
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001413 WriteHeader(FileOff - HeaderSize, NumSections);
1414
1415 FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001416
1417 // ... then all of the sections ...
1418 DenseMap<const MCSection*, uint64_t> SectionOffsetMap;
1419
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001420 for (unsigned i = 1; i < NumSections; ++i) {
1421 const MCSectionELF &Section = *Sections[i];
1422 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001423
1424 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1425 WriteZeros(Padding);
1426 FileOff += Padding;
1427
Matt Fleming3565a062010-08-16 18:57:57 +00001428 // Remember the offset into the file for this section.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001429 SectionOffsetMap[&Section] = FileOff;
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001430
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001431 FileOff += GetSectionFileSize(Layout, SD);
Matt Fleming3565a062010-08-16 18:57:57 +00001432
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001433 if (IsELFMetaDataSection(SD))
1434 WriteDataSectionData(this, SD);
1435 else
Daniel Dunbar5d2477c2010-12-17 02:45:59 +00001436 Asm.WriteSectionData(&SD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +00001437 }
1438
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001439 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1440 WriteZeros(Padding);
1441 FileOff += Padding;
1442
Matt Fleming3565a062010-08-16 18:57:57 +00001443 // ... and then the section header table.
1444 // Should we align the section header table?
1445 //
1446 // Null section first.
Rafael Espindola7be2c332010-10-31 00:16:26 +00001447 uint64_t FirstSectionSize =
1448 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1449 uint32_t FirstSectionLink =
1450 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1451 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming3565a062010-08-16 18:57:57 +00001452
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001453 for (unsigned i = 1; i < NumSections; ++i) {
1454 const MCSectionELF &Section = *Sections[i];
1455 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1456 uint32_t GroupSymbolIndex;
1457 if (Section.getType() != ELF::SHT_GROUP)
1458 GroupSymbolIndex = 0;
1459 else
1460 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm, GroupMap[&Section]);
Matt Fleming3565a062010-08-16 18:57:57 +00001461
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001462 uint64_t Size = GetSectionAddressSize(Layout, SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001463
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001464 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001465 SectionOffsetMap[&Section], Size,
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001466 SD.getAlignment(), Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001467 }
1468}
1469
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001470MCObjectWriter *llvm::createELFObjectWriter(raw_ostream &OS,
1471 bool Is64Bit,
1472 Triple::OSType OSType,
1473 uint16_t EMachine,
1474 bool IsLittleEndian,
1475 bool HasRelocationAddend) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001476 switch (EMachine) {
1477 case ELF::EM_386:
1478 case ELF::EM_X86_64:
1479 return new X86ELFObjectWriter(OS, Is64Bit, IsLittleEndian, EMachine,
1480 HasRelocationAddend, OSType); break;
1481 case ELF::EM_ARM:
1482 return new ARMELFObjectWriter(OS, Is64Bit, IsLittleEndian, EMachine,
1483 HasRelocationAddend, OSType); break;
Wesley Peck4b047132010-11-21 22:06:28 +00001484 case ELF::EM_MBLAZE:
1485 return new MBlazeELFObjectWriter(OS, Is64Bit, IsLittleEndian, EMachine,
1486 HasRelocationAddend, OSType); break;
Benjamin Kramer32858772010-11-15 19:20:50 +00001487 default: llvm_unreachable("Unsupported architecture"); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001488 }
1489}
1490
1491
1492/// START OF SUBCLASSES for ELFObjectWriter
1493//===- ARMELFObjectWriter -------------------------------------------===//
1494
1495ARMELFObjectWriter::ARMELFObjectWriter(raw_ostream &_OS, bool _Is64Bit,
1496 bool _IsLittleEndian,
1497 uint16_t _EMachine, bool _HasRelocationAddend,
1498 Triple::OSType _OSType)
1499 : ELFObjectWriter(_OS, _Is64Bit, _IsLittleEndian, _EMachine,
1500 _HasRelocationAddend, _OSType)
1501{}
1502
1503ARMELFObjectWriter::~ARMELFObjectWriter()
1504{}
1505
Jason W Kim85fed5e2010-12-01 02:40:06 +00001506unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target,
1507 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001508 bool IsPCRel,
1509 bool IsRelocWithSymbol,
1510 int64_t Addend) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001511 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1512 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
1513
Jason W Kima0871e72010-12-08 23:14:44 +00001514 unsigned Type = 0;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001515 if (IsPCRel) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001516 switch ((unsigned)Fixup.getKind()) {
1517 default: assert(0 && "Unimplemented");
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001518 case FK_Data_4:
1519 switch (Modifier) {
1520 default: llvm_unreachable("Unsupported Modifier");
1521 case MCSymbolRefExpr::VK_None:
1522 Type = ELF::R_ARM_BASE_PREL; break;
1523 case MCSymbolRefExpr::VK_ARM_TLSGD:
1524 assert(0 && "unimplemented"); break;
1525 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
1526 Type = ELF::R_ARM_TLS_IE32;
1527 } break;
1528 case ARM::fixup_arm_branch:
1529 switch (Modifier) {
1530 case MCSymbolRefExpr::VK_ARM_PLT:
1531 Type = ELF::R_ARM_PLT32; break;
1532 default:
1533 Type = ELF::R_ARM_CALL; break;
1534 } break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001535 }
1536 } else {
1537 switch ((unsigned)Fixup.getKind()) {
1538 default: llvm_unreachable("invalid fixup kind!");
Jason W Kima0871e72010-12-08 23:14:44 +00001539 case FK_Data_4:
1540 switch (Modifier) {
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001541 default: llvm_unreachable("Unsupported Modifier"); break;
1542 case MCSymbolRefExpr::VK_ARM_GOT:
1543 Type = ELF::R_ARM_GOT_BREL; break;
1544 case MCSymbolRefExpr::VK_ARM_TLSGD:
1545 Type = ELF::R_ARM_TLS_GD32; break;
Jason W Kimf13743b2010-12-16 03:12:17 +00001546 case MCSymbolRefExpr::VK_ARM_TPOFF:
1547 Type = ELF::R_ARM_TLS_LE32; break;
Jason W Kima0871e72010-12-08 23:14:44 +00001548 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001549 Type = ELF::R_ARM_TLS_IE32; break;
Jason W Kimf13743b2010-12-16 03:12:17 +00001550 case MCSymbolRefExpr::VK_None:
1551 Type = ELF::R_ARM_ABS32; break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001552 case MCSymbolRefExpr::VK_ARM_GOTOFF:
1553 Type = ELF::R_ARM_GOTOFF32; break;
Jason W Kima0871e72010-12-08 23:14:44 +00001554 } break;
Jim Grosbachdff84b02010-12-02 00:28:45 +00001555 case ARM::fixup_arm_ldst_pcrel_12:
Owen Anderson9d63d902010-12-01 19:18:46 +00001556 case ARM::fixup_arm_pcrel_10:
Jim Grosbachdff84b02010-12-02 00:28:45 +00001557 case ARM::fixup_arm_adr_pcrel_12:
Jim Grosbach662a8162010-12-06 23:57:07 +00001558 case ARM::fixup_arm_thumb_bl:
Jim Grosbachb492a7c2010-12-09 19:50:12 +00001559 case ARM::fixup_arm_thumb_cb:
Bill Wendlingb8958b02010-12-08 01:57:09 +00001560 case ARM::fixup_arm_thumb_cp:
Jim Grosbache2467172010-12-10 18:21:33 +00001561 case ARM::fixup_arm_thumb_br:
Jason W Kim85fed5e2010-12-01 02:40:06 +00001562 assert(0 && "Unimplemented"); break;
1563 case ARM::fixup_arm_branch:
Jason W Kimf13743b2010-12-16 03:12:17 +00001564 // FIXME: Differentiate between R_ARM_CALL and
1565 // R_ARM_JUMP24 (latter used for conditional jumps)
Jason W Kima0871e72010-12-08 23:14:44 +00001566 Type = ELF::R_ARM_CALL; break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001567 case ARM::fixup_arm_movt_hi16:
Jason W Kima0871e72010-12-08 23:14:44 +00001568 Type = ELF::R_ARM_MOVT_ABS; break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001569 case ARM::fixup_arm_movw_lo16:
Jason W Kima0871e72010-12-08 23:14:44 +00001570 Type = ELF::R_ARM_MOVW_ABS_NC; break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001571 }
1572 }
1573
1574 if (RelocNeedsGOT(Modifier))
1575 NeedsGOT = true;
Jason W Kima0871e72010-12-08 23:14:44 +00001576
1577 return Type;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001578}
1579
Wesley Peck4b047132010-11-21 22:06:28 +00001580//===- MBlazeELFObjectWriter -------------------------------------------===//
Jason W Kimd3443e92010-11-15 16:18:39 +00001581
Wesley Peck4b047132010-11-21 22:06:28 +00001582MBlazeELFObjectWriter::MBlazeELFObjectWriter(raw_ostream &_OS, bool _Is64Bit,
1583 bool _IsLittleEndian,
1584 uint16_t _EMachine,
1585 bool _HasRelocationAddend,
1586 Triple::OSType _OSType)
1587 : ELFObjectWriter(_OS, _Is64Bit, _IsLittleEndian, _EMachine,
1588 _HasRelocationAddend, _OSType) {
1589}
1590
1591MBlazeELFObjectWriter::~MBlazeELFObjectWriter() {
1592}
1593
Jason W Kim56a39902010-12-06 21:57:34 +00001594unsigned MBlazeELFObjectWriter::GetRelocType(const MCValue &Target,
Wesley Peck4b047132010-11-21 22:06:28 +00001595 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001596 bool IsPCRel,
1597 bool IsRelocWithSymbol,
1598 int64_t Addend) {
Wesley Peck4b047132010-11-21 22:06:28 +00001599 // determine the type of the relocation
1600 unsigned Type;
1601 if (IsPCRel) {
1602 switch ((unsigned)Fixup.getKind()) {
1603 default:
1604 llvm_unreachable("Unimplemented");
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001605 case FK_PCRel_4:
Wesley Peck4b047132010-11-21 22:06:28 +00001606 Type = ELF::R_MICROBLAZE_64_PCREL;
1607 break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001608 case FK_PCRel_2:
Wesley Peck4b047132010-11-21 22:06:28 +00001609 Type = ELF::R_MICROBLAZE_32_PCREL;
1610 break;
1611 }
1612 } else {
1613 switch ((unsigned)Fixup.getKind()) {
1614 default: llvm_unreachable("invalid fixup kind!");
1615 case FK_Data_4:
Jason W Kim56a39902010-12-06 21:57:34 +00001616 Type = ((IsRelocWithSymbol || Addend !=0)
1617 ? ELF::R_MICROBLAZE_32
1618 : ELF::R_MICROBLAZE_64);
Wesley Peck4b047132010-11-21 22:06:28 +00001619 break;
1620 case FK_Data_2:
1621 Type = ELF::R_MICROBLAZE_32;
1622 break;
1623 }
1624 }
Jason W Kim56a39902010-12-06 21:57:34 +00001625 return Type;
Wesley Peck4b047132010-11-21 22:06:28 +00001626}
Jason W Kimd3443e92010-11-15 16:18:39 +00001627
1628//===- X86ELFObjectWriter -------------------------------------------===//
1629
1630
1631X86ELFObjectWriter::X86ELFObjectWriter(raw_ostream &_OS, bool _Is64Bit,
1632 bool _IsLittleEndian,
1633 uint16_t _EMachine, bool _HasRelocationAddend,
1634 Triple::OSType _OSType)
1635 : ELFObjectWriter(_OS, _Is64Bit, _IsLittleEndian, _EMachine,
1636 _HasRelocationAddend, _OSType)
1637{}
1638
1639X86ELFObjectWriter::~X86ELFObjectWriter()
1640{}
1641
Jason W Kim56a39902010-12-06 21:57:34 +00001642unsigned X86ELFObjectWriter::GetRelocType(const MCValue &Target,
1643 const MCFixup &Fixup,
1644 bool IsPCRel,
1645 bool IsRelocWithSymbol,
1646 int64_t Addend) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001647 // determine the type of the relocation
1648
Rafael Espindola12203cc2010-11-21 00:48:25 +00001649 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1650 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
Jason W Kimd3443e92010-11-15 16:18:39 +00001651 unsigned Type;
1652 if (Is64Bit) {
1653 if (IsPCRel) {
1654 switch (Modifier) {
1655 default:
1656 llvm_unreachable("Unimplemented");
1657 case MCSymbolRefExpr::VK_None:
1658 Type = ELF::R_X86_64_PC32;
1659 break;
1660 case MCSymbolRefExpr::VK_PLT:
1661 Type = ELF::R_X86_64_PLT32;
1662 break;
1663 case MCSymbolRefExpr::VK_GOTPCREL:
1664 Type = ELF::R_X86_64_GOTPCREL;
1665 break;
1666 case MCSymbolRefExpr::VK_GOTTPOFF:
1667 Type = ELF::R_X86_64_GOTTPOFF;
1668 break;
1669 case MCSymbolRefExpr::VK_TLSGD:
1670 Type = ELF::R_X86_64_TLSGD;
1671 break;
1672 case MCSymbolRefExpr::VK_TLSLD:
1673 Type = ELF::R_X86_64_TLSLD;
1674 break;
1675 }
1676 } else {
1677 switch ((unsigned)Fixup.getKind()) {
1678 default: llvm_unreachable("invalid fixup kind!");
1679 case FK_Data_8: Type = ELF::R_X86_64_64; break;
1680 case X86::reloc_signed_4byte:
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001681 case FK_PCRel_4:
Jason W Kimd3443e92010-11-15 16:18:39 +00001682 assert(isInt<32>(Target.getConstant()));
1683 switch (Modifier) {
1684 default:
1685 llvm_unreachable("Unimplemented");
1686 case MCSymbolRefExpr::VK_None:
1687 Type = ELF::R_X86_64_32S;
1688 break;
1689 case MCSymbolRefExpr::VK_GOT:
1690 Type = ELF::R_X86_64_GOT32;
1691 break;
1692 case MCSymbolRefExpr::VK_GOTPCREL:
1693 Type = ELF::R_X86_64_GOTPCREL;
1694 break;
1695 case MCSymbolRefExpr::VK_TPOFF:
1696 Type = ELF::R_X86_64_TPOFF32;
1697 break;
1698 case MCSymbolRefExpr::VK_DTPOFF:
1699 Type = ELF::R_X86_64_DTPOFF32;
1700 break;
1701 }
1702 break;
1703 case FK_Data_4:
1704 Type = ELF::R_X86_64_32;
1705 break;
1706 case FK_Data_2: Type = ELF::R_X86_64_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001707 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001708 case FK_Data_1: Type = ELF::R_X86_64_8; break;
1709 }
1710 }
1711 } else {
1712 if (IsPCRel) {
1713 switch (Modifier) {
1714 default:
1715 llvm_unreachable("Unimplemented");
1716 case MCSymbolRefExpr::VK_None:
1717 Type = ELF::R_386_PC32;
1718 break;
1719 case MCSymbolRefExpr::VK_PLT:
1720 Type = ELF::R_386_PLT32;
1721 break;
1722 }
1723 } else {
1724 switch ((unsigned)Fixup.getKind()) {
1725 default: llvm_unreachable("invalid fixup kind!");
1726
1727 case X86::reloc_global_offset_table:
1728 Type = ELF::R_386_GOTPC;
1729 break;
1730
1731 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
1732 // instead?
1733 case X86::reloc_signed_4byte:
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001734 case FK_PCRel_4:
Jason W Kimd3443e92010-11-15 16:18:39 +00001735 case FK_Data_4:
1736 switch (Modifier) {
1737 default:
1738 llvm_unreachable("Unimplemented");
1739 case MCSymbolRefExpr::VK_None:
1740 Type = ELF::R_386_32;
1741 break;
1742 case MCSymbolRefExpr::VK_GOT:
1743 Type = ELF::R_386_GOT32;
1744 break;
1745 case MCSymbolRefExpr::VK_GOTOFF:
1746 Type = ELF::R_386_GOTOFF;
1747 break;
1748 case MCSymbolRefExpr::VK_TLSGD:
1749 Type = ELF::R_386_TLS_GD;
1750 break;
1751 case MCSymbolRefExpr::VK_TPOFF:
1752 Type = ELF::R_386_TLS_LE_32;
1753 break;
1754 case MCSymbolRefExpr::VK_INDNTPOFF:
1755 Type = ELF::R_386_TLS_IE;
1756 break;
1757 case MCSymbolRefExpr::VK_NTPOFF:
1758 Type = ELF::R_386_TLS_LE;
1759 break;
1760 case MCSymbolRefExpr::VK_GOTNTPOFF:
1761 Type = ELF::R_386_TLS_GOTIE;
1762 break;
1763 case MCSymbolRefExpr::VK_TLSLDM:
1764 Type = ELF::R_386_TLS_LDM;
1765 break;
1766 case MCSymbolRefExpr::VK_DTPOFF:
1767 Type = ELF::R_386_TLS_LDO_32;
1768 break;
1769 }
1770 break;
1771 case FK_Data_2: Type = ELF::R_386_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001772 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001773 case FK_Data_1: Type = ELF::R_386_8; break;
1774 }
1775 }
1776 }
1777
1778 if (RelocNeedsGOT(Modifier))
1779 NeedsGOT = true;
1780
Jason W Kim56a39902010-12-06 21:57:34 +00001781 return Type;
Matt Fleming3565a062010-08-16 18:57:57 +00001782}