blob: 8a00a16cfb4a9896ad9cf66b47156cf087ec3335 [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"
Jason W Kim953a2a32011-02-07 01:11:15 +000033#include "llvm/ADT/StringSwitch.h"
Matt Fleming3565a062010-08-16 18:57:57 +000034
35#include "../Target/X86/X86FixupKinds.h"
Jason W Kim4a511f02010-11-22 18:41:13 +000036#include "../Target/ARM/ARMFixupKinds.h"
Matt Fleming3565a062010-08-16 18:57:57 +000037
38#include <vector>
39using namespace llvm;
40
Rafael Espindolaad49cf52010-09-18 15:03:21 +000041static unsigned GetType(const MCSymbolData &SD) {
42 uint32_t Type = (SD.getFlags() & (0xf << ELF_STT_Shift)) >> ELF_STT_Shift;
43 assert(Type == ELF::STT_NOTYPE || Type == ELF::STT_OBJECT ||
44 Type == ELF::STT_FUNC || Type == ELF::STT_SECTION ||
45 Type == ELF::STT_FILE || Type == ELF::STT_COMMON ||
46 Type == ELF::STT_TLS);
47 return Type;
48}
49
Rafael Espindolae15eb4e2010-09-23 19:55:14 +000050static unsigned GetBinding(const MCSymbolData &SD) {
51 uint32_t Binding = (SD.getFlags() & (0xf << ELF_STB_Shift)) >> ELF_STB_Shift;
52 assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
53 Binding == ELF::STB_WEAK);
54 return Binding;
55}
56
57static void SetBinding(MCSymbolData &SD, unsigned Binding) {
58 assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
59 Binding == ELF::STB_WEAK);
60 uint32_t OtherFlags = SD.getFlags() & ~(0xf << ELF_STB_Shift);
61 SD.setFlags(OtherFlags | (Binding << ELF_STB_Shift));
62}
63
Rafael Espindola152c1062010-10-06 21:02:29 +000064static unsigned GetVisibility(MCSymbolData &SD) {
65 unsigned Visibility =
66 (SD.getFlags() & (0xf << ELF_STV_Shift)) >> ELF_STV_Shift;
67 assert(Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_INTERNAL ||
68 Visibility == ELF::STV_HIDDEN || Visibility == ELF::STV_PROTECTED);
69 return Visibility;
70}
71
Wesley Peck4b047132010-11-21 22:06:28 +000072
Rafael Espindolaa0a2f872010-10-28 14:22:44 +000073static bool RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant) {
74 switch (Variant) {
Rafael Espindola5c77c162010-10-05 15:48:37 +000075 default:
76 return false;
Rafael Espindolaa0a2f872010-10-28 14:22:44 +000077 case MCSymbolRefExpr::VK_GOT:
78 case MCSymbolRefExpr::VK_PLT:
79 case MCSymbolRefExpr::VK_GOTPCREL:
80 case MCSymbolRefExpr::VK_TPOFF:
81 case MCSymbolRefExpr::VK_TLSGD:
82 case MCSymbolRefExpr::VK_GOTTPOFF:
83 case MCSymbolRefExpr::VK_INDNTPOFF:
84 case MCSymbolRefExpr::VK_NTPOFF:
85 case MCSymbolRefExpr::VK_GOTNTPOFF:
Rafael Espindolaa264f722010-10-28 14:37:09 +000086 case MCSymbolRefExpr::VK_TLSLDM:
Rafael Espindola0cf15d62010-10-28 14:48:59 +000087 case MCSymbolRefExpr::VK_DTPOFF:
Rafael Espindolab4d17212010-10-28 15:02:40 +000088 case MCSymbolRefExpr::VK_TLSLD:
Rafael Espindola5c77c162010-10-05 15:48:37 +000089 return true;
90 }
91}
92
Rafael Espindola127a6a42010-12-17 07:28:17 +000093static bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) {
94 const MCFixupKindInfo &FKI =
95 Asm.getBackend().getFixupKindInfo((MCFixupKind) Kind);
96
97 return FKI.Flags & MCFixupKindInfo::FKF_IsPCRel;
98}
99
Matt Fleming3565a062010-08-16 18:57:57 +0000100namespace {
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000101 class ELFObjectWriter : public MCObjectWriter {
Jason W Kimd3443e92010-11-15 16:18:39 +0000102 protected:
Chris Lattnerb188a372010-08-28 03:21:03 +0000103 /*static bool isFixupKindX86RIPRel(unsigned Kind) {
Matt Fleming3565a062010-08-16 18:57:57 +0000104 return Kind == X86::reloc_riprel_4byte ||
105 Kind == X86::reloc_riprel_4byte_movq_load;
Chris Lattnerb188a372010-08-28 03:21:03 +0000106 }*/
Matt Fleming3565a062010-08-16 18:57:57 +0000107
108
109 /// ELFSymbolData - Helper struct for containing some precomputed information
110 /// on symbols.
111 struct ELFSymbolData {
112 MCSymbolData *SymbolData;
113 uint64_t StringIndex;
114 uint32_t SectionIndex;
115
116 // Support lexicographic sorting.
117 bool operator<(const ELFSymbolData &RHS) const {
Rafael Espindolaad49cf52010-09-18 15:03:21 +0000118 if (GetType(*SymbolData) == ELF::STT_FILE)
119 return true;
120 if (GetType(*RHS.SymbolData) == ELF::STT_FILE)
121 return false;
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000122 return SymbolData->getSymbol().getName() <
123 RHS.SymbolData->getSymbol().getName();
Matt Fleming3565a062010-08-16 18:57:57 +0000124 }
125 };
126
127 /// @name Relocation Data
128 /// @{
129
130 struct ELFRelocationEntry {
131 // Make these big enough for both 32-bit and 64-bit
132 uint64_t r_offset;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000133 int Index;
134 unsigned Type;
135 const MCSymbol *Symbol;
Matt Fleming3565a062010-08-16 18:57:57 +0000136 uint64_t r_addend;
Jason W Kim858e7502010-11-22 18:42:07 +0000137
Jason W Kim4a511f02010-11-22 18:41:13 +0000138 ELFRelocationEntry()
139 : r_offset(0), Index(0), Type(0), Symbol(0), r_addend(0) {}
140
Jason W Kim10907422010-11-22 22:05:16 +0000141 ELFRelocationEntry(uint64_t RelocOffset, int Idx,
142 unsigned RelType, const MCSymbol *Sym,
Jason W Kim4a511f02010-11-22 18:41:13 +0000143 uint64_t Addend)
Jason W Kim10907422010-11-22 22:05:16 +0000144 : r_offset(RelocOffset), Index(Idx), Type(RelType),
145 Symbol(Sym), r_addend(Addend) {}
Matt Fleming3565a062010-08-16 18:57:57 +0000146
147 // Support lexicographic sorting.
148 bool operator<(const ELFRelocationEntry &RE) const {
149 return RE.r_offset < r_offset;
150 }
151 };
152
Rafael Espindola31f35782010-12-17 18:01:31 +0000153 /// The target specific ELF writer instance.
154 llvm::OwningPtr<MCELFObjectTargetWriter> TargetObjectWriter;
155
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000156 SmallPtrSet<const MCSymbol *, 16> UsedInReloc;
Rafael Espindola484291c2010-11-01 14:28:48 +0000157 SmallPtrSet<const MCSymbol *, 16> WeakrefUsedInReloc;
Rafael Espindola88182132010-10-27 15:18:17 +0000158 DenseMap<const MCSymbol *, const MCSymbol *> Renames;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000159
Matt Fleming3565a062010-08-16 18:57:57 +0000160 llvm::DenseMap<const MCSectionData*,
161 std::vector<ELFRelocationEntry> > Relocations;
162 DenseMap<const MCSection*, uint64_t> SectionStringTableIndex;
163
164 /// @}
165 /// @name Symbol Table Data
166 /// @{
167
168 SmallString<256> StringTable;
169 std::vector<ELFSymbolData> LocalSymbolData;
170 std::vector<ELFSymbolData> ExternalSymbolData;
171 std::vector<ELFSymbolData> UndefinedSymbolData;
172
173 /// @}
174
Rafael Espindola5c77c162010-10-05 15:48:37 +0000175 bool NeedsGOT;
176
Rafael Espindola7be2c332010-10-31 00:16:26 +0000177 bool NeedsSymtabShndx;
178
Matt Fleming3565a062010-08-16 18:57:57 +0000179 // This holds the symbol table index of the last local symbol.
180 unsigned LastLocalSymbolIndex;
181 // This holds the .strtab section index.
182 unsigned StringTableIndex;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000183 // This holds the .symtab section index.
184 unsigned SymbolTableIndex;
Matt Fleming3565a062010-08-16 18:57:57 +0000185
186 unsigned ShstrtabIndex;
187
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000188
189 const MCSymbol *SymbolToReloc(const MCAssembler &Asm,
190 const MCValue &Target,
191 const MCFragment &F) const;
192
Jason W Kim953a2a32011-02-07 01:11:15 +0000193 // For arch-specific emission of explicit reloc symbol
194 virtual const MCSymbol *ExplicitRelSym(const MCAssembler &Asm,
195 const MCValue &Target,
196 const MCFragment &F,
197 bool IsBSS) const {
198 return NULL;
199 }
200
Rafael Espindolabff66a82010-12-18 03:27:34 +0000201 bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
202 bool hasRelocationAddend() const {
203 return TargetObjectWriter->hasRelocationAddend();
204 }
205
Matt Fleming3565a062010-08-16 18:57:57 +0000206 public:
Rafael Espindola31f35782010-12-17 18:01:31 +0000207 ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +0000208 raw_ostream &_OS, bool IsLittleEndian)
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000209 : MCObjectWriter(_OS, IsLittleEndian),
Rafael Espindola31f35782010-12-17 18:01:31 +0000210 TargetObjectWriter(MOTW),
Rafael Espindolabff66a82010-12-18 03:27:34 +0000211 NeedsGOT(false), NeedsSymtabShndx(false){
Matt Fleming3565a062010-08-16 18:57:57 +0000212 }
Jason W Kimd3443e92010-11-15 16:18:39 +0000213
214 virtual ~ELFObjectWriter();
215
Matt Fleming3565a062010-08-16 18:57:57 +0000216 void WriteWord(uint64_t W) {
Rafael Espindolabff66a82010-12-18 03:27:34 +0000217 if (is64Bit())
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000218 Write64(W);
Chris Lattnerb188a372010-08-28 03:21:03 +0000219 else
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000220 Write32(W);
Matt Fleming3565a062010-08-16 18:57:57 +0000221 }
222
Matt Fleming3565a062010-08-16 18:57:57 +0000223 void StringLE16(char *buf, uint16_t Value) {
224 buf[0] = char(Value >> 0);
225 buf[1] = char(Value >> 8);
226 }
227
228 void StringLE32(char *buf, uint32_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000229 StringLE16(buf, uint16_t(Value >> 0));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000230 StringLE16(buf + 2, uint16_t(Value >> 16));
Matt Fleming3565a062010-08-16 18:57:57 +0000231 }
232
233 void StringLE64(char *buf, uint64_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000234 StringLE32(buf, uint32_t(Value >> 0));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000235 StringLE32(buf + 4, uint32_t(Value >> 32));
Matt Fleming3565a062010-08-16 18:57:57 +0000236 }
237
238 void StringBE16(char *buf ,uint16_t Value) {
239 buf[0] = char(Value >> 8);
240 buf[1] = char(Value >> 0);
241 }
242
243 void StringBE32(char *buf, uint32_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000244 StringBE16(buf, uint16_t(Value >> 16));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000245 StringBE16(buf + 2, uint16_t(Value >> 0));
Matt Fleming3565a062010-08-16 18:57:57 +0000246 }
247
248 void StringBE64(char *buf, uint64_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000249 StringBE32(buf, uint32_t(Value >> 32));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000250 StringBE32(buf + 4, uint32_t(Value >> 0));
Matt Fleming3565a062010-08-16 18:57:57 +0000251 }
252
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000253 void String8(MCDataFragment &F, uint8_t Value) {
254 char buf[1];
255 buf[0] = Value;
256 F.getContents() += StringRef(buf, 1);
257 }
258
259 void String16(MCDataFragment &F, uint16_t Value) {
260 char buf[2];
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000261 if (isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000262 StringLE16(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000263 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000264 StringBE16(buf, Value);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000265 F.getContents() += StringRef(buf, 2);
Matt Fleming3565a062010-08-16 18:57:57 +0000266 }
267
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000268 void String32(MCDataFragment &F, uint32_t Value) {
269 char buf[4];
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000270 if (isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000271 StringLE32(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000272 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000273 StringBE32(buf, Value);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000274 F.getContents() += StringRef(buf, 4);
Matt Fleming3565a062010-08-16 18:57:57 +0000275 }
276
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000277 void String64(MCDataFragment &F, uint64_t Value) {
278 char buf[8];
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000279 if (isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000280 StringLE64(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000281 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000282 StringBE64(buf, Value);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000283 F.getContents() += StringRef(buf, 8);
Matt Fleming3565a062010-08-16 18:57:57 +0000284 }
285
Jason W Kimd3443e92010-11-15 16:18:39 +0000286 virtual void WriteHeader(uint64_t SectionDataSize, unsigned NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000287
Jason W Kim2d7a53a2011-02-04 21:41:11 +0000288 /// Default e_flags = 0
289 virtual void WriteEFlags() { Write32(0); }
290
Jason W Kimd3443e92010-11-15 16:18:39 +0000291 virtual void WriteSymbolEntry(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
Rafael Espindola7be2c332010-10-31 00:16:26 +0000292 uint64_t name, uint8_t info,
Matt Fleming3565a062010-08-16 18:57:57 +0000293 uint64_t value, uint64_t size,
Rafael Espindola7be2c332010-10-31 00:16:26 +0000294 uint8_t other, uint32_t shndx,
295 bool Reserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000296
Jason W Kimd3443e92010-11-15 16:18:39 +0000297 virtual void WriteSymbol(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
Rafael Espindola7be2c332010-10-31 00:16:26 +0000298 ELFSymbolData &MSD,
Matt Fleming3565a062010-08-16 18:57:57 +0000299 const MCAsmLayout &Layout);
300
Rafael Espindolabab2a802010-11-10 21:51:05 +0000301 typedef DenseMap<const MCSectionELF*, uint32_t> SectionIndexMapTy;
Jason W Kimd3443e92010-11-15 16:18:39 +0000302 virtual void WriteSymbolTable(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
Rafael Espindola7be2c332010-10-31 00:16:26 +0000303 const MCAssembler &Asm,
Rafael Espindola71859c62010-09-16 19:46:31 +0000304 const MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000305 const SectionIndexMapTy &SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +0000306
Jason W Kimd3443e92010-11-15 16:18:39 +0000307 virtual void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
Jason W Kim56a39902010-12-06 21:57:34 +0000308 const MCFragment *Fragment, const MCFixup &Fixup,
309 MCValue Target, uint64_t &FixedValue);
Matt Fleming3565a062010-08-16 18:57:57 +0000310
Jason W Kimd3443e92010-11-15 16:18:39 +0000311 virtual uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm,
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000312 const MCSymbol *S);
Matt Fleming3565a062010-08-16 18:57:57 +0000313
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000314 // Map from a group section to the signature symbol
315 typedef DenseMap<const MCSectionELF*, const MCSymbol*> GroupMapTy;
316 // Map from a signature symbol to the group section
317 typedef DenseMap<const MCSymbol*, const MCSectionELF*> RevGroupMapTy;
318
Matt Fleming3565a062010-08-16 18:57:57 +0000319 /// ComputeSymbolTable - Compute the symbol table data
320 ///
321 /// \param StringTable [out] - The string table data.
322 /// \param StringIndexMap [out] - Map from symbol names to offsets in the
323 /// string table.
Jason W Kimd3443e92010-11-15 16:18:39 +0000324 virtual void ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000325 const SectionIndexMapTy &SectionIndexMap,
326 RevGroupMapTy RevGroupMap);
Rafael Espindolabab2a802010-11-10 21:51:05 +0000327
Jason W Kimd3443e92010-11-15 16:18:39 +0000328 virtual void ComputeIndexMap(MCAssembler &Asm,
Rafael Espindolabab2a802010-11-10 21:51:05 +0000329 SectionIndexMapTy &SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +0000330
Jason W Kimd3443e92010-11-15 16:18:39 +0000331 virtual void WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
Matt Fleming3565a062010-08-16 18:57:57 +0000332 const MCSectionData &SD);
333
Jason W Kimd3443e92010-11-15 16:18:39 +0000334 virtual void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout) {
Matt Fleming3565a062010-08-16 18:57:57 +0000335 for (MCAssembler::const_iterator it = Asm.begin(),
336 ie = Asm.end(); it != ie; ++it) {
337 WriteRelocation(Asm, Layout, *it);
338 }
339 }
340
Jason W Kimd3443e92010-11-15 16:18:39 +0000341 virtual void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000342 const SectionIndexMapTy &SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +0000343
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000344 // Create the sections that show up in the symbol table. Currently
345 // those are the .note.GNU-stack section and the group sections.
346 virtual void CreateIndexedSections(MCAssembler &Asm, MCAsmLayout &Layout,
347 GroupMapTy &GroupMap,
348 RevGroupMapTy &RevGroupMap);
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000349
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000350 virtual void ExecutePostLayoutBinding(MCAssembler &Asm,
351 const MCAsmLayout &Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000352
Jason W Kimd3443e92010-11-15 16:18:39 +0000353 virtual void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
Matt Fleming3565a062010-08-16 18:57:57 +0000354 uint64_t Address, uint64_t Offset,
355 uint64_t Size, uint32_t Link, uint32_t Info,
356 uint64_t Alignment, uint64_t EntrySize);
357
Daniel Dunbar1f3662a2010-12-17 04:54:54 +0000358 virtual void WriteRelocationsFragment(const MCAssembler &Asm,
359 MCDataFragment *F,
360 const MCSectionData *SD);
361
Jason W Kimd3443e92010-11-15 16:18:39 +0000362 virtual void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
363 virtual void WriteSection(MCAssembler &Asm,
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000364 const SectionIndexMapTy &SectionIndexMap,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000365 uint32_t GroupSymbolIndex,
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000366 uint64_t Offset, uint64_t Size, uint64_t Alignment,
367 const MCSectionELF &Section);
Jason W Kim56a39902010-12-06 21:57:34 +0000368
369 protected:
370 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
371 bool IsPCRel, bool IsRelocWithSymbol,
372 int64_t Addend) = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000373 };
374
Jason W Kimd3443e92010-11-15 16:18:39 +0000375 //===- X86ELFObjectWriter -------------------------------------------===//
376
377 class X86ELFObjectWriter : public ELFObjectWriter {
378 public:
Rafael Espindola31f35782010-12-17 18:01:31 +0000379 X86ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +0000380 raw_ostream &_OS,
381 bool IsLittleEndian);
Wesley Peck4b047132010-11-21 22:06:28 +0000382
Jason W Kimd3443e92010-11-15 16:18:39 +0000383 virtual ~X86ELFObjectWriter();
Jason W Kim56a39902010-12-06 21:57:34 +0000384 protected:
385 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
386 bool IsPCRel, bool IsRelocWithSymbol,
387 int64_t Addend);
Jason W Kimd3443e92010-11-15 16:18:39 +0000388 };
389
390
391 //===- ARMELFObjectWriter -------------------------------------------===//
392
393 class ARMELFObjectWriter : public ELFObjectWriter {
394 public:
Jason W Kim2d7a53a2011-02-04 21:41:11 +0000395 // FIXME: MCAssembler can't yet return the Subtarget,
396 enum { DefaultEABIVersion = 0x05000000U };
397
Rafael Espindola31f35782010-12-17 18:01:31 +0000398 ARMELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +0000399 raw_ostream &_OS,
400 bool IsLittleEndian);
Wesley Peck4b047132010-11-21 22:06:28 +0000401
Jason W Kimd3443e92010-11-15 16:18:39 +0000402 virtual ~ARMELFObjectWriter();
Jason W Kim2d7a53a2011-02-04 21:41:11 +0000403
404 virtual void WriteEFlags();
Jason W Kim85fed5e2010-12-01 02:40:06 +0000405 protected:
Jason W Kim953a2a32011-02-07 01:11:15 +0000406 virtual const MCSymbol *ExplicitRelSym(const MCAssembler &Asm,
407 const MCValue &Target,
408 const MCFragment &F,
409 bool IsBSS) const;
410
Jason W Kim56a39902010-12-06 21:57:34 +0000411 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
412 bool IsPCRel, bool IsRelocWithSymbol,
413 int64_t Addend);
Jason W Kimd3443e92010-11-15 16:18:39 +0000414 };
Wesley Peck4b047132010-11-21 22:06:28 +0000415
416 //===- MBlazeELFObjectWriter -------------------------------------------===//
417
418 class MBlazeELFObjectWriter : public ELFObjectWriter {
419 public:
Rafael Espindola31f35782010-12-17 18:01:31 +0000420 MBlazeELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +0000421 raw_ostream &_OS,
422 bool IsLittleEndian);
Wesley Peck4b047132010-11-21 22:06:28 +0000423
424 virtual ~MBlazeELFObjectWriter();
Jason W Kim56a39902010-12-06 21:57:34 +0000425 protected:
426 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
427 bool IsPCRel, bool IsRelocWithSymbol,
428 int64_t Addend);
Wesley Peck4b047132010-11-21 22:06:28 +0000429 };
Matt Fleming3565a062010-08-16 18:57:57 +0000430}
431
Jason W Kimd3443e92010-11-15 16:18:39 +0000432ELFObjectWriter::~ELFObjectWriter()
433{}
434
Matt Fleming3565a062010-08-16 18:57:57 +0000435// Emit the ELF header.
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000436void ELFObjectWriter::WriteHeader(uint64_t SectionDataSize,
437 unsigned NumberOfSections) {
Matt Fleming3565a062010-08-16 18:57:57 +0000438 // ELF Header
439 // ----------
440 //
441 // Note
442 // ----
443 // emitWord method behaves differently for ELF32 and ELF64, writing
444 // 4 bytes in the former and 8 in the latter.
445
446 Write8(0x7f); // e_ident[EI_MAG0]
447 Write8('E'); // e_ident[EI_MAG1]
448 Write8('L'); // e_ident[EI_MAG2]
449 Write8('F'); // e_ident[EI_MAG3]
450
Rafael Espindolabff66a82010-12-18 03:27:34 +0000451 Write8(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
Matt Fleming3565a062010-08-16 18:57:57 +0000452
453 // e_ident[EI_DATA]
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000454 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
Matt Fleming3565a062010-08-16 18:57:57 +0000455
456 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky5baf79e2010-09-09 17:57:50 +0000457 // e_ident[EI_OSABI]
Rafael Espindolabff66a82010-12-18 03:27:34 +0000458 switch (TargetObjectWriter->getOSType()) {
Roman Divacky5baf79e2010-09-09 17:57:50 +0000459 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
460 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
461 default: Write8(ELF::ELFOSABI_NONE); break;
462 }
Matt Fleming3565a062010-08-16 18:57:57 +0000463 Write8(0); // e_ident[EI_ABIVERSION]
464
465 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
466
467 Write16(ELF::ET_REL); // e_type
468
Rafael Espindolabff66a82010-12-18 03:27:34 +0000469 Write16(TargetObjectWriter->getEMachine()); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +0000470
471 Write32(ELF::EV_CURRENT); // e_version
472 WriteWord(0); // e_entry, no entry point in .o file
473 WriteWord(0); // e_phoff, no program header for .o
Rafael Espindolabff66a82010-12-18 03:27:34 +0000474 WriteWord(SectionDataSize + (is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
Benjamin Kramereb976772010-08-17 17:02:29 +0000475 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000476
Jason W Kim2d7a53a2011-02-04 21:41:11 +0000477 // e_flags = whatever the target wants
478 WriteEFlags();
Matt Fleming3565a062010-08-16 18:57:57 +0000479
480 // e_ehsize = ELF header size
Rafael Espindolabff66a82010-12-18 03:27:34 +0000481 Write16(is64Bit() ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
Matt Fleming3565a062010-08-16 18:57:57 +0000482
483 Write16(0); // e_phentsize = prog header entry size
484 Write16(0); // e_phnum = # prog header entries = 0
485
486 // e_shentsize = Section header entry size
Rafael Espindolabff66a82010-12-18 03:27:34 +0000487 Write16(is64Bit() ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
Matt Fleming3565a062010-08-16 18:57:57 +0000488
489 // e_shnum = # of section header ents
Rafael Espindola7be2c332010-10-31 00:16:26 +0000490 if (NumberOfSections >= ELF::SHN_LORESERVE)
491 Write16(0);
492 else
493 Write16(NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000494
495 // e_shstrndx = Section # of '.shstrtab'
Rafael Espindola7be2c332010-10-31 00:16:26 +0000496 if (NumberOfSections >= ELF::SHN_LORESERVE)
497 Write16(ELF::SHN_XINDEX);
498 else
499 Write16(ShstrtabIndex);
Matt Fleming3565a062010-08-16 18:57:57 +0000500}
501
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000502void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
503 MCDataFragment *ShndxF,
504 uint64_t name,
505 uint8_t info, uint64_t value,
506 uint64_t size, uint8_t other,
507 uint32_t shndx,
508 bool Reserved) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000509 if (ShndxF) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000510 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000511 String32(*ShndxF, shndx);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000512 else
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000513 String32(*ShndxF, 0);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000514 }
515
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000516 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
517 uint16_t(ELF::SHN_XINDEX) : shndx;
518
Rafael Espindolabff66a82010-12-18 03:27:34 +0000519 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000520 String32(*SymtabF, name); // st_name
521 String8(*SymtabF, info); // st_info
522 String8(*SymtabF, other); // st_other
523 String16(*SymtabF, Index); // st_shndx
524 String64(*SymtabF, value); // st_value
525 String64(*SymtabF, size); // st_size
Matt Fleming3565a062010-08-16 18:57:57 +0000526 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000527 String32(*SymtabF, name); // st_name
528 String32(*SymtabF, value); // st_value
529 String32(*SymtabF, size); // st_size
530 String8(*SymtabF, info); // st_info
531 String8(*SymtabF, other); // st_other
532 String16(*SymtabF, Index); // st_shndx
Matt Fleming3565a062010-08-16 18:57:57 +0000533 }
534}
535
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000536static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout) {
537 if (Data.isCommon() && Data.isExternal())
538 return Data.getCommonAlignment();
539
540 const MCSymbol &Symbol = Data.getSymbol();
Roman Divackyd1491862010-12-20 21:14:39 +0000541
542 if (Symbol.isAbsolute() && Symbol.isVariable()) {
543 if (const MCExpr *Value = Symbol.getVariableValue()) {
544 int64_t IntValue;
545 if (Value->EvaluateAsAbsolute(IntValue, Layout))
546 return (uint64_t)IntValue;
547 }
548 }
549
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000550 if (!Symbol.isInSection())
551 return 0;
552
Rafael Espindolaffd902b2010-12-06 02:57:26 +0000553 if (Data.getFragment())
554 return Layout.getSymbolOffset(&Data);
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000555
556 return 0;
557}
558
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000559void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
560 const MCAsmLayout &Layout) {
Rafael Espindola88182132010-10-27 15:18:17 +0000561 // The presence of symbol versions causes undefined symbols and
562 // versions declared with @@@ to be renamed.
563
564 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
565 ie = Asm.symbol_end(); it != ie; ++it) {
566 const MCSymbol &Alias = it->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000567 const MCSymbol &Symbol = Alias.AliasedSymbol();
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000568 MCSymbolData &SD = Asm.getSymbolData(Symbol);
569
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000570 // Not an alias.
571 if (&Symbol == &Alias)
572 continue;
573
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000574 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000575 size_t Pos = AliasName.find('@');
576 if (Pos == StringRef::npos)
577 continue;
578
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000579 // Aliases defined with .symvar copy the binding from the symbol they alias.
580 // This is the first place we are able to copy this information.
581 it->setExternal(SD.isExternal());
582 SetBinding(*it, GetBinding(SD));
583
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000584 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000585 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
586 continue;
587
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000588 // FIXME: produce a better error message.
589 if (Symbol.isUndefined() && Rest.startswith("@@") &&
590 !Rest.startswith("@@@"))
591 report_fatal_error("A @@ version cannot be undefined");
592
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000593 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000594 }
595}
596
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000597void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
598 MCDataFragment *ShndxF,
599 ELFSymbolData &MSD,
600 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000601 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000602 MCSymbolData &Data =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000603 Layout.getAssembler().getSymbolData(OrigData.getSymbol().AliasedSymbol());
Rafael Espindola152c1062010-10-06 21:02:29 +0000604
Rafael Espindola7be2c332010-10-31 00:16:26 +0000605 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
606 Data.getSymbol().isVariable();
607
Rafael Espindola152c1062010-10-06 21:02:29 +0000608 uint8_t Binding = GetBinding(OrigData);
609 uint8_t Visibility = GetVisibility(OrigData);
610 uint8_t Type = GetType(Data);
611
612 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
613 uint8_t Other = Visibility;
614
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000615 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000616 uint64_t Size = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000617
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000618 assert(!(Data.isCommon() && !Data.isExternal()));
619
Rafael Espindolaf0121242010-12-22 16:03:00 +0000620 const MCExpr *ESize = Data.getSize();
621 if (ESize) {
622 int64_t Res;
623 if (!ESize->EvaluateAsAbsolute(Res, Layout))
624 report_fatal_error("Size expression must be absolute.");
625 Size = Res;
Matt Fleming3565a062010-08-16 18:57:57 +0000626 }
627
628 // Write out the symbol table entry
Rafael Espindola7be2c332010-10-31 00:16:26 +0000629 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
630 Size, Other, MSD.SectionIndex, IsReserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000631}
632
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000633void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
634 MCDataFragment *ShndxF,
635 const MCAssembler &Asm,
636 const MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000637 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000638 // The string table must be emitted first because we need the index
639 // into the string table for all the symbol names.
640 assert(StringTable.size() && "Missing string table");
641
642 // FIXME: Make sure the start of the symbol table is aligned.
643
644 // The first entry is the undefined symbol entry.
Rafael Espindola7be2c332010-10-31 00:16:26 +0000645 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
Matt Fleming3565a062010-08-16 18:57:57 +0000646
647 // Write the symbol table entries.
648 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
649 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
650 ELFSymbolData &MSD = LocalSymbolData[i];
Rafael Espindola7be2c332010-10-31 00:16:26 +0000651 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000652 }
653
Rafael Espindola71859c62010-09-16 19:46:31 +0000654 // Write out a symbol table entry for each regular section.
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000655 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
656 ++i) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000657 const MCSectionELF &Section =
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000658 static_cast<const MCSectionELF&>(i->getSection());
659 if (Section.getType() == ELF::SHT_RELA ||
660 Section.getType() == ELF::SHT_REL ||
661 Section.getType() == ELF::SHT_STRTAB ||
662 Section.getType() == ELF::SHT_SYMTAB)
Eli Friedmana44fa242010-08-16 21:17:09 +0000663 continue;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000664 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000665 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section), false);
Eli Friedmana44fa242010-08-16 21:17:09 +0000666 LastLocalSymbolIndex++;
667 }
Matt Fleming3565a062010-08-16 18:57:57 +0000668
669 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
670 ELFSymbolData &MSD = ExternalSymbolData[i];
671 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000672 assert(((Data.getFlags() & ELF_STB_Global) ||
673 (Data.getFlags() & ELF_STB_Weak)) &&
674 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000675 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000676 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000677 LastLocalSymbolIndex++;
678 }
679
680 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
681 ELFSymbolData &MSD = UndefinedSymbolData[i];
682 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000683 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000684 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000685 LastLocalSymbolIndex++;
686 }
687}
688
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000689const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm,
690 const MCValue &Target,
691 const MCFragment &F) const {
692 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000693 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
694 const MCSymbol *Renamed = Renames.lookup(&Symbol);
695 const MCSymbolData &SD = Asm.getSymbolData(Symbol);
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000696
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000697 if (ASymbol.isUndefined()) {
698 if (Renamed)
699 return Renamed;
700 return &ASymbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000701 }
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000702
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000703 if (SD.isExternal()) {
704 if (Renamed)
705 return Renamed;
706 return &Symbol;
707 }
Rafael Espindola73ffea42010-09-25 05:42:19 +0000708
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000709 const MCSectionELF &Section =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000710 static_cast<const MCSectionELF&>(ASymbol.getSection());
Rafael Espindola25958732010-11-24 21:57:39 +0000711 const SectionKind secKind = Section.getKind();
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000712
Rafael Espindola25958732010-11-24 21:57:39 +0000713 if (secKind.isBSS())
Jason W Kim953a2a32011-02-07 01:11:15 +0000714 return ExplicitRelSym(Asm, Target, F, true);
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000715
Rafael Espindola25958732010-11-24 21:57:39 +0000716 if (secKind.isThreadLocal()) {
717 if (Renamed)
718 return Renamed;
719 return &Symbol;
720 }
721
Rafael Espindola8cecf252010-10-06 16:23:36 +0000722 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000723 const MCSectionELF &Sec2 =
724 static_cast<const MCSectionELF&>(F.getParent()->getSection());
725
Rafael Espindola8cecf252010-10-06 16:23:36 +0000726 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000727 (Kind == MCSymbolRefExpr::VK_PLT ||
728 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
Rafael Espindola25958732010-11-24 21:57:39 +0000729 Kind == MCSymbolRefExpr::VK_GOTOFF)) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000730 if (Renamed)
731 return Renamed;
732 return &Symbol;
733 }
Rafael Espindola3729d002010-10-05 23:57:26 +0000734
Rafael Espindola1c130262011-01-23 04:43:11 +0000735 if (Section.getFlags() & ELF::SHF_MERGE) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000736 if (Target.getConstant() == 0)
737 return NULL;
738 if (Renamed)
739 return Renamed;
740 return &Symbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000741 }
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000742
Jason W Kim953a2a32011-02-07 01:11:15 +0000743 return ExplicitRelSym(Asm, Target, F, false);
Rafael Espindola73ffea42010-09-25 05:42:19 +0000744}
745
Matt Fleming3565a062010-08-16 18:57:57 +0000746
Jason W Kim56a39902010-12-06 21:57:34 +0000747void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
748 const MCAsmLayout &Layout,
749 const MCFragment *Fragment,
750 const MCFixup &Fixup,
751 MCValue Target,
752 uint64_t &FixedValue) {
753 int64_t Addend = 0;
754 int Index = 0;
755 int64_t Value = Target.getConstant();
756 const MCSymbol *RelocSymbol = NULL;
757
Rafael Espindola127a6a42010-12-17 07:28:17 +0000758 bool IsPCRel = isFixupKindPCRel(Asm, Fixup.getKind());
Jason W Kim56a39902010-12-06 21:57:34 +0000759 if (!Target.isAbsolute()) {
760 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
761 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
762 RelocSymbol = SymbolToReloc(Asm, Target, *Fragment);
763
764 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
765 const MCSymbol &SymbolB = RefB->getSymbol();
766 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
767 IsPCRel = true;
768
769 // Offset of the symbol in the section
770 int64_t a = Layout.getSymbolOffset(&SDB);
771
772 // Ofeset of the relocation in the section
773 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
774 Value += b - a;
775 }
776
777 if (!RelocSymbol) {
778 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
779 MCFragment *F = SD.getFragment();
780
781 Index = F->getParent()->getOrdinal() + 1;
782
783 // Offset of the symbol in the section
784 Value += Layout.getSymbolOffset(&SD);
785 } else {
786 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
787 WeakrefUsedInReloc.insert(RelocSymbol);
788 else
789 UsedInReloc.insert(RelocSymbol);
790 Index = -1;
791 }
792 Addend = Value;
793 // Compensate for the addend on i386.
Rafael Espindolabff66a82010-12-18 03:27:34 +0000794 if (is64Bit())
Jason W Kim56a39902010-12-06 21:57:34 +0000795 Value = 0;
796 }
797
798 FixedValue = Value;
799 unsigned Type = GetRelocType(Target, Fixup, IsPCRel,
800 (RelocSymbol != 0), Addend);
801
802 uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
803 Fixup.getOffset();
804
Rafael Espindolabff66a82010-12-18 03:27:34 +0000805 if (!hasRelocationAddend())
806 Addend = 0;
Jason W Kim56a39902010-12-06 21:57:34 +0000807 ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend);
808 Relocations[Fragment->getParent()].push_back(ERE);
809}
810
811
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000812uint64_t
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000813ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
814 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000815 MCSymbolData &SD = Asm.getSymbolData(*S);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000816 return SD.getIndex();
Matt Fleming3565a062010-08-16 18:57:57 +0000817}
818
Rafael Espindola737cd212010-10-05 18:01:23 +0000819static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
Rafael Espindola88182132010-10-27 15:18:17 +0000820 bool Used, bool Renamed) {
Rafael Espindola484291c2010-11-01 14:28:48 +0000821 if (Data.getFlags() & ELF_Other_Weakref)
822 return false;
823
Rafael Espindolabd701182010-10-19 19:31:37 +0000824 if (Used)
825 return true;
826
Rafael Espindola88182132010-10-27 15:18:17 +0000827 if (Renamed)
828 return false;
829
Rafael Espindola737cd212010-10-05 18:01:23 +0000830 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000831
Rafael Espindolad1798862010-10-29 23:09:31 +0000832 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
833 return true;
834
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000835 const MCSymbol &A = Symbol.AliasedSymbol();
Rafael Espindolad1798862010-10-29 23:09:31 +0000836 if (!A.isVariable() && A.isUndefined() && !Data.isCommon())
Rafael Espindolaa6866962010-10-27 14:44:52 +0000837 return false;
838
Rafael Espindola737cd212010-10-05 18:01:23 +0000839 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
840 return false;
841
Rafael Espindolabd701182010-10-19 19:31:37 +0000842 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000843 return false;
844
845 return true;
846}
847
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000848static bool isLocal(const MCSymbolData &Data, bool isSignature,
849 bool isUsedInReloc) {
Rafael Espindola737cd212010-10-05 18:01:23 +0000850 if (Data.isExternal())
851 return false;
852
853 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000854 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000855
856 if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
857 if (isSignature && !isUsedInReloc)
858 return true;
859
Rafael Espindola737cd212010-10-05 18:01:23 +0000860 return false;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000861 }
Rafael Espindola737cd212010-10-05 18:01:23 +0000862
863 return true;
864}
865
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000866void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
867 SectionIndexMapTy &SectionIndexMap) {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000868 unsigned Index = 1;
869 for (MCAssembler::iterator it = Asm.begin(),
870 ie = Asm.end(); it != ie; ++it) {
871 const MCSectionELF &Section =
872 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000873 if (Section.getType() != ELF::SHT_GROUP)
874 continue;
875 SectionIndexMap[&Section] = Index++;
876 }
877
878 for (MCAssembler::iterator it = Asm.begin(),
879 ie = Asm.end(); it != ie; ++it) {
880 const MCSectionELF &Section =
881 static_cast<const MCSectionELF &>(it->getSection());
882 if (Section.getType() == ELF::SHT_GROUP)
883 continue;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000884 SectionIndexMap[&Section] = Index++;
885 }
886}
887
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000888void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000889 const SectionIndexMapTy &SectionIndexMap,
890 RevGroupMapTy RevGroupMap) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000891 // FIXME: Is this the correct place to do this?
892 if (NeedsGOT) {
893 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
894 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
895 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
896 Data.setExternal(true);
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000897 SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindola5c77c162010-10-05 15:48:37 +0000898 }
899
Matt Fleming3565a062010-08-16 18:57:57 +0000900 // Build section lookup table.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000901 int NumRegularSections = Asm.size();
Matt Fleming3565a062010-08-16 18:57:57 +0000902
903 // Index 0 is always the empty string.
904 StringMap<uint64_t> StringIndexMap;
905 StringTable += '\x00';
906
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000907 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000908 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
909 ie = Asm.symbol_end(); it != ie; ++it) {
910 const MCSymbol &Symbol = it->getSymbol();
911
Rafael Espindola484291c2010-11-01 14:28:48 +0000912 bool Used = UsedInReloc.count(&Symbol);
913 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000914 bool isSignature = RevGroupMap.count(&Symbol);
915
916 if (!isInSymtab(Asm, *it,
917 Used || WeakrefUsed || isSignature,
Rafael Espindola88182132010-10-27 15:18:17 +0000918 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000919 continue;
920
Matt Fleming3565a062010-08-16 18:57:57 +0000921 ELFSymbolData MSD;
922 MSD.SymbolData = it;
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000923 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Matt Fleming3565a062010-08-16 18:57:57 +0000924
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000925 // Undefined symbols are global, but this is the first place we
926 // are able to set it.
927 bool Local = isLocal(*it, isSignature, Used);
928 if (!Local && GetBinding(*it) == ELF::STB_LOCAL) {
929 MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
930 SetBinding(*it, ELF::STB_GLOBAL);
931 SetBinding(SD, ELF::STB_GLOBAL);
932 }
933
Rafael Espindola484291c2010-11-01 14:28:48 +0000934 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
935 SetBinding(*it, ELF::STB_WEAK);
936
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000937 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000938 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000939 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000940 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000941 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000942 } else if (RefSymbol.isUndefined()) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000943 if (isSignature && !Used)
944 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
945 else
946 MSD.SectionIndex = ELF::SHN_UNDEF;
Matt Fleming3565a062010-08-16 18:57:57 +0000947 } else {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000948 const MCSectionELF &Section =
949 static_cast<const MCSectionELF&>(RefSymbol.getSection());
950 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000951 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
952 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000953 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000954 }
955
Rafael Espindola88182132010-10-27 15:18:17 +0000956 // The @@@ in symbol version is replaced with @ in undefined symbols and
957 // @@ in defined ones.
958 StringRef Name = Symbol.getName();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000959 SmallString<32> Buf;
960
Rafael Espindola88182132010-10-27 15:18:17 +0000961 size_t Pos = Name.find("@@@");
Rafael Espindola88182132010-10-27 15:18:17 +0000962 if (Pos != StringRef::npos) {
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000963 Buf += Name.substr(0, Pos);
964 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
965 Buf += Name.substr(Pos + Skip);
966 Name = Buf;
Rafael Espindola88182132010-10-27 15:18:17 +0000967 }
968
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000969 uint64_t &Entry = StringIndexMap[Name];
Rafael Espindolaa6866962010-10-27 14:44:52 +0000970 if (!Entry) {
971 Entry = StringTable.size();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000972 StringTable += Name;
Rafael Espindolaa6866962010-10-27 14:44:52 +0000973 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +0000974 }
Rafael Espindolaa6866962010-10-27 14:44:52 +0000975 MSD.StringIndex = Entry;
976 if (MSD.SectionIndex == ELF::SHN_UNDEF)
977 UndefinedSymbolData.push_back(MSD);
978 else if (Local)
979 LocalSymbolData.push_back(MSD);
980 else
981 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +0000982 }
983
984 // Symbols are required to be in lexicographic order.
985 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
986 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
987 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
988
989 // Set the symbol indices. Local symbols must come before all other
990 // symbols with non-local bindings.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000991 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000992 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
993 LocalSymbolData[i].SymbolData->setIndex(Index++);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000994
995 Index += NumRegularSections;
996
Matt Fleming3565a062010-08-16 18:57:57 +0000997 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
998 ExternalSymbolData[i].SymbolData->setIndex(Index++);
999 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
1000 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
1001}
1002
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001003void ELFObjectWriter::WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
1004 const MCSectionData &SD) {
Matt Fleming3565a062010-08-16 18:57:57 +00001005 if (!Relocations[&SD].empty()) {
1006 MCContext &Ctx = Asm.getContext();
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001007 const MCSectionELF *RelaSection;
Matt Fleming3565a062010-08-16 18:57:57 +00001008 const MCSectionELF &Section =
1009 static_cast<const MCSectionELF&>(SD.getSection());
1010
1011 const StringRef SectionName = Section.getSectionName();
Rafael Espindolabff66a82010-12-18 03:27:34 +00001012 std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +00001013 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +00001014
1015 unsigned EntrySize;
Rafael Espindolabff66a82010-12-18 03:27:34 +00001016 if (hasRelocationAddend())
1017 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
Benjamin Kramer299fbe32010-08-17 17:56:13 +00001018 else
Rafael Espindolabff66a82010-12-18 03:27:34 +00001019 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +00001020
Rafael Espindolabff66a82010-12-18 03:27:34 +00001021 RelaSection = Ctx.getELFSection(RelaSectionName, hasRelocationAddend() ?
Benjamin Kramer377a5722010-08-17 17:30:07 +00001022 ELF::SHT_RELA : ELF::SHT_REL, 0,
Matt Fleming3565a062010-08-16 18:57:57 +00001023 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001024 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001025
1026 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +00001027 RelaSD.setAlignment(is64Bit() ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +00001028
1029 MCDataFragment *F = new MCDataFragment(&RelaSD);
1030
1031 WriteRelocationsFragment(Asm, F, &SD);
Matt Fleming3565a062010-08-16 18:57:57 +00001032 }
1033}
1034
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001035void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
1036 uint64_t Flags, uint64_t Address,
1037 uint64_t Offset, uint64_t Size,
1038 uint32_t Link, uint32_t Info,
1039 uint64_t Alignment,
1040 uint64_t EntrySize) {
Matt Fleming3565a062010-08-16 18:57:57 +00001041 Write32(Name); // sh_name: index into string table
1042 Write32(Type); // sh_type
1043 WriteWord(Flags); // sh_flags
1044 WriteWord(Address); // sh_addr
1045 WriteWord(Offset); // sh_offset
1046 WriteWord(Size); // sh_size
1047 Write32(Link); // sh_link
1048 Write32(Info); // sh_info
1049 WriteWord(Alignment); // sh_addralign
1050 WriteWord(EntrySize); // sh_entsize
1051}
1052
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001053void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
1054 MCDataFragment *F,
1055 const MCSectionData *SD) {
Matt Fleming3565a062010-08-16 18:57:57 +00001056 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
1057 // sort by the r_offset just like gnu as does
1058 array_pod_sort(Relocs.begin(), Relocs.end());
1059
1060 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1061 ELFRelocationEntry entry = Relocs[e - i - 1];
1062
Rafael Espindola12203cc2010-11-21 00:48:25 +00001063 if (!entry.Index)
1064 ;
1065 else if (entry.Index < 0)
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001066 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
1067 else
Rafael Espindola12203cc2010-11-21 00:48:25 +00001068 entry.Index += LocalSymbolData.size();
Rafael Espindolabff66a82010-12-18 03:27:34 +00001069 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001070 String64(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001071
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001072 struct ELF::Elf64_Rela ERE64;
1073 ERE64.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001074 String64(*F, ERE64.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001075
Rafael Espindolabff66a82010-12-18 03:27:34 +00001076 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001077 String64(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001078 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001079 String32(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001080
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001081 struct ELF::Elf32_Rela ERE32;
1082 ERE32.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001083 String32(*F, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001084
Rafael Espindolabff66a82010-12-18 03:27:34 +00001085 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001086 String32(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001087 }
Matt Fleming3565a062010-08-16 18:57:57 +00001088 }
1089}
1090
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001091void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
1092 MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001093 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +00001094 MCContext &Ctx = Asm.getContext();
1095 MCDataFragment *F;
1096
Rafael Espindolabff66a82010-12-18 03:27:34 +00001097 unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
Matt Fleming3565a062010-08-16 18:57:57 +00001098
Rafael Espindola38738bf2010-09-22 19:04:41 +00001099 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001100 const MCSectionELF *ShstrtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001101 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001102 SectionKind::getReadOnly());
Rafael Espindola71859c62010-09-16 19:46:31 +00001103 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1104 ShstrtabSD.setAlignment(1);
1105 ShstrtabIndex = Asm.size();
1106
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001107 const MCSectionELF *SymtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001108 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1109 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001110 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001111 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +00001112 SymtabSD.setAlignment(is64Bit() ? 8 : 4);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001113 SymbolTableIndex = Asm.size();
1114
1115 MCSectionData *SymtabShndxSD = NULL;
1116
1117 if (NeedsSymtabShndx) {
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001118 const MCSectionELF *SymtabShndxSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001119 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001120 SectionKind::getReadOnly(), 4, "");
Rafael Espindola7be2c332010-10-31 00:16:26 +00001121 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
1122 SymtabShndxSD->setAlignment(4);
1123 }
Matt Fleming3565a062010-08-16 18:57:57 +00001124
Matt Fleming3565a062010-08-16 18:57:57 +00001125 const MCSection *StrtabSection;
1126 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001127 SectionKind::getReadOnly());
Matt Fleming3565a062010-08-16 18:57:57 +00001128 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1129 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +00001130 StringTableIndex = Asm.size();
1131
Rafael Espindolac3c413f2010-09-27 22:04:54 +00001132 WriteRelocations(Asm, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001133
1134 // Symbol table
1135 F = new MCDataFragment(&SymtabSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001136 MCDataFragment *ShndxF = NULL;
1137 if (NeedsSymtabShndx) {
1138 ShndxF = new MCDataFragment(SymtabShndxSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001139 }
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001140 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
Rafael Espindola71859c62010-09-16 19:46:31 +00001141
Matt Fleming3565a062010-08-16 18:57:57 +00001142 F = new MCDataFragment(&StrtabSD);
1143 F->getContents().append(StringTable.begin(), StringTable.end());
Matt Fleming3565a062010-08-16 18:57:57 +00001144
Matt Fleming3565a062010-08-16 18:57:57 +00001145 F = new MCDataFragment(&ShstrtabSD);
1146
Matt Fleming3565a062010-08-16 18:57:57 +00001147 // Section header string table.
1148 //
1149 // The first entry of a string table holds a null character so skip
1150 // section 0.
1151 uint64_t Index = 1;
1152 F->getContents() += '\x00';
1153
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001154 StringMap<uint64_t> SecStringMap;
Matt Fleming3565a062010-08-16 18:57:57 +00001155 for (MCAssembler::const_iterator it = Asm.begin(),
1156 ie = Asm.end(); it != ie; ++it) {
Matt Fleming3565a062010-08-16 18:57:57 +00001157 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +00001158 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola51efe7a2010-09-23 14:14:56 +00001159 // FIXME: We could merge suffixes like in .text and .rela.text.
Matt Fleming3565a062010-08-16 18:57:57 +00001160
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001161 StringRef Name = Section.getSectionName();
1162 if (SecStringMap.count(Name)) {
1163 SectionStringTableIndex[&Section] = SecStringMap[Name];
1164 continue;
1165 }
Matt Fleming3565a062010-08-16 18:57:57 +00001166 // Remember the index into the string table so we can write it
1167 // into the sh_name field of the section header table.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001168 SectionStringTableIndex[&Section] = Index;
1169 SecStringMap[Name] = Index;
Matt Fleming3565a062010-08-16 18:57:57 +00001170
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001171 Index += Name.size() + 1;
1172 F->getContents() += Name;
Matt Fleming3565a062010-08-16 18:57:57 +00001173 F->getContents() += '\x00';
1174 }
Rafael Espindola70703872010-09-30 02:22:20 +00001175}
1176
Rafael Espindola96aa78c2011-01-23 17:55:27 +00001177void ELFObjectWriter::CreateIndexedSections(MCAssembler &Asm,
1178 MCAsmLayout &Layout,
1179 GroupMapTy &GroupMap,
1180 RevGroupMapTy &RevGroupMap) {
1181 // Create the .note.GNU-stack section if needed.
1182 MCContext &Ctx = Asm.getContext();
1183 if (Asm.getNoExecStack()) {
1184 const MCSectionELF *GnuStackSection =
1185 Ctx.getELFSection(".note.GNU-stack", ELF::SHT_PROGBITS, 0,
1186 SectionKind::getReadOnly());
1187 Asm.getOrCreateSectionData(*GnuStackSection);
1188 }
1189
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001190 // Build the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001191 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1192 it != ie; ++it) {
1193 const MCSectionELF &Section =
1194 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola1c130262011-01-23 04:43:11 +00001195 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001196 continue;
1197
1198 const MCSymbol *SignatureSymbol = Section.getGroup();
1199 Asm.getOrCreateSymbolData(*SignatureSymbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001200 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001201 if (!Group) {
Rafael Espindola96aa78c2011-01-23 17:55:27 +00001202 Group = Ctx.CreateELFGroupSection();
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001203 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1204 Data.setAlignment(4);
1205 MCDataFragment *F = new MCDataFragment(&Data);
1206 String32(*F, ELF::GRP_COMDAT);
1207 }
1208 GroupMap[Group] = SignatureSymbol;
1209 }
1210
1211 // Add sections to the groups
1212 unsigned Index = 1;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001213 unsigned NumGroups = RevGroupMap.size();
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001214 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1215 it != ie; ++it, ++Index) {
1216 const MCSectionELF &Section =
1217 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola1c130262011-01-23 04:43:11 +00001218 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001219 continue;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001220 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001221 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1222 // FIXME: we could use the previous fragment
1223 MCDataFragment *F = new MCDataFragment(&Data);
1224 String32(*F, NumGroups + Index);
1225 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001226}
1227
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001228void ELFObjectWriter::WriteSection(MCAssembler &Asm,
1229 const SectionIndexMapTy &SectionIndexMap,
1230 uint32_t GroupSymbolIndex,
1231 uint64_t Offset, uint64_t Size,
1232 uint64_t Alignment,
1233 const MCSectionELF &Section) {
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001234 uint64_t sh_link = 0;
1235 uint64_t sh_info = 0;
1236
1237 switch(Section.getType()) {
1238 case ELF::SHT_DYNAMIC:
1239 sh_link = SectionStringTableIndex[&Section];
1240 sh_info = 0;
1241 break;
1242
1243 case ELF::SHT_REL:
1244 case ELF::SHT_RELA: {
1245 const MCSectionELF *SymtabSection;
1246 const MCSectionELF *InfoSection;
1247 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
1248 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001249 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001250 sh_link = SectionIndexMap.lookup(SymtabSection);
1251 assert(sh_link && ".symtab not found");
1252
1253 // Remove ".rel" and ".rela" prefixes.
1254 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1255 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
1256
1257 InfoSection = Asm.getContext().getELFSection(SectionName,
1258 ELF::SHT_PROGBITS, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001259 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001260 sh_info = SectionIndexMap.lookup(InfoSection);
1261 break;
1262 }
1263
1264 case ELF::SHT_SYMTAB:
1265 case ELF::SHT_DYNSYM:
1266 sh_link = StringTableIndex;
1267 sh_info = LastLocalSymbolIndex;
1268 break;
1269
1270 case ELF::SHT_SYMTAB_SHNDX:
1271 sh_link = SymbolTableIndex;
1272 break;
1273
1274 case ELF::SHT_PROGBITS:
1275 case ELF::SHT_STRTAB:
1276 case ELF::SHT_NOBITS:
Rafael Espindola98976612010-12-26 21:30:59 +00001277 case ELF::SHT_NOTE:
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001278 case ELF::SHT_NULL:
1279 case ELF::SHT_ARM_ATTRIBUTES:
Jason W Kim86a97f22011-01-12 00:19:25 +00001280 case ELF::SHT_INIT_ARRAY:
1281 case ELF::SHT_FINI_ARRAY:
1282 case ELF::SHT_PREINIT_ARRAY:
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +00001283 case ELF::SHT_X86_64_UNWIND:
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001284 // Nothing to do.
1285 break;
1286
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001287 case ELF::SHT_GROUP: {
1288 sh_link = SymbolTableIndex;
1289 sh_info = GroupSymbolIndex;
1290 break;
1291 }
1292
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001293 default:
1294 assert(0 && "FIXME: sh_type value not supported!");
1295 break;
1296 }
1297
1298 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1299 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1300 Alignment, Section.getEntrySize());
1301}
1302
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001303static bool IsELFMetaDataSection(const MCSectionData &SD) {
Rafael Espindolaf8803fe2010-12-06 03:48:09 +00001304 return SD.getOrdinal() == ~UINT32_C(0) &&
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001305 !SD.getSection().isVirtualSection();
1306}
1307
1308static uint64_t DataSectionSize(const MCSectionData &SD) {
1309 uint64_t Ret = 0;
1310 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1311 ++i) {
1312 const MCFragment &F = *i;
1313 assert(F.getKind() == MCFragment::FT_Data);
1314 Ret += cast<MCDataFragment>(F).getContents().size();
1315 }
1316 return Ret;
1317}
1318
1319static uint64_t GetSectionFileSize(const MCAsmLayout &Layout,
1320 const MCSectionData &SD) {
1321 if (IsELFMetaDataSection(SD))
1322 return DataSectionSize(SD);
1323 return Layout.getSectionFileSize(&SD);
1324}
1325
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001326static uint64_t GetSectionAddressSize(const MCAsmLayout &Layout,
1327 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001328 if (IsELFMetaDataSection(SD))
1329 return DataSectionSize(SD);
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001330 return Layout.getSectionAddressSize(&SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001331}
1332
1333static void WriteDataSectionData(ELFObjectWriter *W, const MCSectionData &SD) {
1334 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1335 ++i) {
1336 const MCFragment &F = *i;
1337 assert(F.getKind() == MCFragment::FT_Data);
1338 W->WriteBytes(cast<MCDataFragment>(F).getContents().str());
1339 }
1340}
1341
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001342void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1343 const MCAsmLayout &Layout) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001344 GroupMapTy GroupMap;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001345 RevGroupMapTy RevGroupMap;
Rafael Espindola96aa78c2011-01-23 17:55:27 +00001346 CreateIndexedSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1347 RevGroupMap);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001348
Rafael Espindolabab2a802010-11-10 21:51:05 +00001349 SectionIndexMapTy SectionIndexMap;
1350
1351 ComputeIndexMap(Asm, SectionIndexMap);
1352
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001353 // Compute symbol table information.
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001354 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap);
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001355
Matt Fleming3565a062010-08-16 18:57:57 +00001356 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001357 const_cast<MCAsmLayout&>(Layout),
1358 SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +00001359
Rafael Espindola1d739a02010-11-10 22:34:07 +00001360 // Update to include the metadata sections.
1361 ComputeIndexMap(Asm, SectionIndexMap);
1362
Matt Fleming3565a062010-08-16 18:57:57 +00001363 // Add 1 for the null section.
1364 unsigned NumSections = Asm.size() + 1;
Rafael Espindolabff66a82010-12-18 03:27:34 +00001365 uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
1366 uint64_t HeaderSize = is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
1367 sizeof(ELF::Elf32_Ehdr);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001368 uint64_t FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001369
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001370 std::vector<const MCSectionELF*> Sections;
1371 Sections.resize(NumSections);
1372
1373 for (SectionIndexMapTy::const_iterator i=
1374 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1375 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
1376 Sections[p.second] = p.first;
1377 }
1378
1379 for (unsigned i = 1; i < NumSections; ++i) {
1380 const MCSectionELF &Section = *Sections[i];
1381 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001382
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001383 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1384
Matt Fleming3565a062010-08-16 18:57:57 +00001385 // Get the size of the section in the output file (including padding).
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001386 FileOff += GetSectionFileSize(Layout, SD);
Matt Fleming3565a062010-08-16 18:57:57 +00001387 }
1388
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001389 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1390
Matt Fleming3565a062010-08-16 18:57:57 +00001391 // Write out the ELF header ...
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001392 WriteHeader(FileOff - HeaderSize, NumSections);
1393
1394 FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001395
1396 // ... then all of the sections ...
1397 DenseMap<const MCSection*, uint64_t> SectionOffsetMap;
1398
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001399 for (unsigned i = 1; i < NumSections; ++i) {
1400 const MCSectionELF &Section = *Sections[i];
1401 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001402
1403 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1404 WriteZeros(Padding);
1405 FileOff += Padding;
1406
Matt Fleming3565a062010-08-16 18:57:57 +00001407 // Remember the offset into the file for this section.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001408 SectionOffsetMap[&Section] = FileOff;
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001409
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001410 FileOff += GetSectionFileSize(Layout, SD);
Matt Fleming3565a062010-08-16 18:57:57 +00001411
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001412 if (IsELFMetaDataSection(SD))
1413 WriteDataSectionData(this, SD);
1414 else
Daniel Dunbar5d2477c2010-12-17 02:45:59 +00001415 Asm.WriteSectionData(&SD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +00001416 }
1417
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001418 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1419 WriteZeros(Padding);
1420 FileOff += Padding;
1421
Matt Fleming3565a062010-08-16 18:57:57 +00001422 // ... and then the section header table.
1423 // Should we align the section header table?
1424 //
1425 // Null section first.
Rafael Espindola7be2c332010-10-31 00:16:26 +00001426 uint64_t FirstSectionSize =
1427 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1428 uint32_t FirstSectionLink =
1429 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1430 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming3565a062010-08-16 18:57:57 +00001431
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001432 for (unsigned i = 1; i < NumSections; ++i) {
1433 const MCSectionELF &Section = *Sections[i];
1434 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1435 uint32_t GroupSymbolIndex;
1436 if (Section.getType() != ELF::SHT_GROUP)
1437 GroupSymbolIndex = 0;
1438 else
1439 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm, GroupMap[&Section]);
Matt Fleming3565a062010-08-16 18:57:57 +00001440
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001441 uint64_t Size = GetSectionAddressSize(Layout, SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001442
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001443 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001444 SectionOffsetMap[&Section], Size,
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001445 SD.getAlignment(), Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001446 }
1447}
1448
Rafael Espindola6024c972010-12-17 17:45:22 +00001449MCObjectWriter *llvm::createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1450 raw_ostream &OS,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001451 bool IsLittleEndian) {
1452 switch (MOTW->getEMachine()) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001453 case ELF::EM_386:
1454 case ELF::EM_X86_64:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001455 return new X86ELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001456 case ELF::EM_ARM:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001457 return new ARMELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Wesley Peck4b047132010-11-21 22:06:28 +00001458 case ELF::EM_MBLAZE:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001459 return new MBlazeELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Benjamin Kramer32858772010-11-15 19:20:50 +00001460 default: llvm_unreachable("Unsupported architecture"); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001461 }
1462}
1463
1464
1465/// START OF SUBCLASSES for ELFObjectWriter
1466//===- ARMELFObjectWriter -------------------------------------------===//
1467
Rafael Espindola31f35782010-12-17 18:01:31 +00001468ARMELFObjectWriter::ARMELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001469 raw_ostream &_OS,
1470 bool IsLittleEndian)
1471 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
Jason W Kimd3443e92010-11-15 16:18:39 +00001472{}
1473
1474ARMELFObjectWriter::~ARMELFObjectWriter()
1475{}
1476
Jason W Kim2d7a53a2011-02-04 21:41:11 +00001477// FIXME: get the real EABI Version from the Triple.
1478void ARMELFObjectWriter::WriteEFlags() {
1479 Write32(ELF::EF_ARM_EABIMASK & DefaultEABIVersion);
1480}
1481
Jason W Kim953a2a32011-02-07 01:11:15 +00001482// In ARM, _MergedGlobals and other most symbols get emitted directly.
1483// I.e. not as an offset to a section symbol.
1484// This code is a first-cut approximation of what ARM/gcc does.
1485
1486const MCSymbol *ARMELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
1487 const MCValue &Target,
1488 const MCFragment &F,
1489 bool IsBSS) const {
1490 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
1491 bool EmitThisSym = false;
1492
1493 if (IsBSS) {
1494 EmitThisSym = StringSwitch<bool>(Symbol.getName())
1495 .Case("_MergedGlobals", true)
1496 .Default(false);
1497 } else {
1498 EmitThisSym = StringSwitch<bool>(Symbol.getName())
1499 .Case("_MergedGlobals", true)
1500 .StartsWith(".L.str", true)
1501 .Default(false);
1502 }
1503 if (EmitThisSym)
1504 return &Symbol;
1505 if (! Symbol.isTemporary())
1506 return &Symbol;
1507 return NULL;
1508}
1509
Jason W Kim85fed5e2010-12-01 02:40:06 +00001510unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target,
1511 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001512 bool IsPCRel,
1513 bool IsRelocWithSymbol,
1514 int64_t Addend) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001515 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1516 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
1517
Jason W Kima0871e72010-12-08 23:14:44 +00001518 unsigned Type = 0;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001519 if (IsPCRel) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001520 switch ((unsigned)Fixup.getKind()) {
1521 default: assert(0 && "Unimplemented");
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001522 case FK_Data_4:
1523 switch (Modifier) {
1524 default: llvm_unreachable("Unsupported Modifier");
1525 case MCSymbolRefExpr::VK_None:
Jason W Kim1d866172011-01-13 00:07:51 +00001526 Type = ELF::R_ARM_BASE_PREL;
1527 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001528 case MCSymbolRefExpr::VK_ARM_TLSGD:
Jason W Kim1d866172011-01-13 00:07:51 +00001529 assert(0 && "unimplemented");
1530 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001531 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
1532 Type = ELF::R_ARM_TLS_IE32;
Jason W Kim1d866172011-01-13 00:07:51 +00001533 break;
1534 }
1535 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001536 case ARM::fixup_arm_uncondbranch:
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001537 switch (Modifier) {
1538 case MCSymbolRefExpr::VK_ARM_PLT:
Jason W Kim1d866172011-01-13 00:07:51 +00001539 Type = ELF::R_ARM_PLT32;
1540 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001541 default:
Jason W Kim1d866172011-01-13 00:07:51 +00001542 Type = ELF::R_ARM_CALL;
1543 break;
1544 }
1545 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001546 case ARM::fixup_arm_condbranch:
1547 Type = ELF::R_ARM_JUMP24;
1548 break;
Jason W Kim86a97f22011-01-12 00:19:25 +00001549 case ARM::fixup_arm_movt_hi16:
1550 case ARM::fixup_arm_movt_hi16_pcrel:
Jason W Kim1d866172011-01-13 00:07:51 +00001551 Type = ELF::R_ARM_MOVT_PREL;
1552 break;
Jason W Kim86a97f22011-01-12 00:19:25 +00001553 case ARM::fixup_arm_movw_lo16:
1554 case ARM::fixup_arm_movw_lo16_pcrel:
Jason W Kim1d866172011-01-13 00:07:51 +00001555 Type = ELF::R_ARM_MOVW_PREL_NC;
1556 break;
Evan Chengf3eb3bb2011-01-14 02:38:49 +00001557 case ARM::fixup_t2_movt_hi16:
1558 case ARM::fixup_t2_movt_hi16_pcrel:
1559 Type = ELF::R_ARM_THM_MOVT_PREL;
1560 break;
1561 case ARM::fixup_t2_movw_lo16:
1562 case ARM::fixup_t2_movw_lo16_pcrel:
1563 Type = ELF::R_ARM_THM_MOVW_PREL_NC;
1564 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001565 }
1566 } else {
1567 switch ((unsigned)Fixup.getKind()) {
1568 default: llvm_unreachable("invalid fixup kind!");
Jason W Kima0871e72010-12-08 23:14:44 +00001569 case FK_Data_4:
1570 switch (Modifier) {
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001571 default: llvm_unreachable("Unsupported Modifier"); break;
1572 case MCSymbolRefExpr::VK_ARM_GOT:
Jason W Kim1d866172011-01-13 00:07:51 +00001573 Type = ELF::R_ARM_GOT_BREL;
1574 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001575 case MCSymbolRefExpr::VK_ARM_TLSGD:
Jason W Kim1d866172011-01-13 00:07:51 +00001576 Type = ELF::R_ARM_TLS_GD32;
1577 break;
Jason W Kimf13743b2010-12-16 03:12:17 +00001578 case MCSymbolRefExpr::VK_ARM_TPOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001579 Type = ELF::R_ARM_TLS_LE32;
1580 break;
Jason W Kima0871e72010-12-08 23:14:44 +00001581 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001582 Type = ELF::R_ARM_TLS_IE32;
1583 break;
Jason W Kimf13743b2010-12-16 03:12:17 +00001584 case MCSymbolRefExpr::VK_None:
Jason W Kim1d866172011-01-13 00:07:51 +00001585 Type = ELF::R_ARM_ABS32;
1586 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001587 case MCSymbolRefExpr::VK_ARM_GOTOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001588 Type = ELF::R_ARM_GOTOFF32;
1589 break;
1590 }
1591 break;
Jim Grosbachdff84b02010-12-02 00:28:45 +00001592 case ARM::fixup_arm_ldst_pcrel_12:
Owen Anderson9d63d902010-12-01 19:18:46 +00001593 case ARM::fixup_arm_pcrel_10:
Jim Grosbachdff84b02010-12-02 00:28:45 +00001594 case ARM::fixup_arm_adr_pcrel_12:
Jim Grosbach662a8162010-12-06 23:57:07 +00001595 case ARM::fixup_arm_thumb_bl:
Jim Grosbachb492a7c2010-12-09 19:50:12 +00001596 case ARM::fixup_arm_thumb_cb:
Bill Wendlingb8958b02010-12-08 01:57:09 +00001597 case ARM::fixup_arm_thumb_cp:
Jim Grosbache2467172010-12-10 18:21:33 +00001598 case ARM::fixup_arm_thumb_br:
Jason W Kim1d866172011-01-13 00:07:51 +00001599 assert(0 && "Unimplemented");
1600 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001601 case ARM::fixup_arm_uncondbranch:
Jason W Kim1d866172011-01-13 00:07:51 +00001602 Type = ELF::R_ARM_CALL;
1603 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001604 case ARM::fixup_arm_condbranch:
1605 Type = ELF::R_ARM_JUMP24;
1606 break;
Jason W Kim1d866172011-01-13 00:07:51 +00001607 case ARM::fixup_arm_movt_hi16:
1608 Type = ELF::R_ARM_MOVT_ABS;
1609 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001610 case ARM::fixup_arm_movw_lo16:
Jason W Kim1d866172011-01-13 00:07:51 +00001611 Type = ELF::R_ARM_MOVW_ABS_NC;
1612 break;
Evan Chengf3eb3bb2011-01-14 02:38:49 +00001613 case ARM::fixup_t2_movt_hi16:
1614 Type = ELF::R_ARM_THM_MOVT_ABS;
1615 break;
1616 case ARM::fixup_t2_movw_lo16:
1617 Type = ELF::R_ARM_THM_MOVW_ABS_NC;
1618 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001619 }
1620 }
1621
1622 if (RelocNeedsGOT(Modifier))
1623 NeedsGOT = true;
Jason W Kim953a2a32011-02-07 01:11:15 +00001624
Jason W Kima0871e72010-12-08 23:14:44 +00001625 return Type;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001626}
1627
Wesley Peck4b047132010-11-21 22:06:28 +00001628//===- MBlazeELFObjectWriter -------------------------------------------===//
Jason W Kimd3443e92010-11-15 16:18:39 +00001629
Rafael Espindola31f35782010-12-17 18:01:31 +00001630MBlazeELFObjectWriter::MBlazeELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001631 raw_ostream &_OS,
1632 bool IsLittleEndian)
1633 : ELFObjectWriter(MOTW, _OS, IsLittleEndian) {
Wesley Peck4b047132010-11-21 22:06:28 +00001634}
1635
1636MBlazeELFObjectWriter::~MBlazeELFObjectWriter() {
1637}
1638
Jason W Kim56a39902010-12-06 21:57:34 +00001639unsigned MBlazeELFObjectWriter::GetRelocType(const MCValue &Target,
Wesley Peck4b047132010-11-21 22:06:28 +00001640 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001641 bool IsPCRel,
1642 bool IsRelocWithSymbol,
1643 int64_t Addend) {
Wesley Peck4b047132010-11-21 22:06:28 +00001644 // determine the type of the relocation
1645 unsigned Type;
1646 if (IsPCRel) {
1647 switch ((unsigned)Fixup.getKind()) {
1648 default:
1649 llvm_unreachable("Unimplemented");
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001650 case FK_PCRel_4:
Wesley Peck4b047132010-11-21 22:06:28 +00001651 Type = ELF::R_MICROBLAZE_64_PCREL;
1652 break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001653 case FK_PCRel_2:
Wesley Peck4b047132010-11-21 22:06:28 +00001654 Type = ELF::R_MICROBLAZE_32_PCREL;
1655 break;
1656 }
1657 } else {
1658 switch ((unsigned)Fixup.getKind()) {
1659 default: llvm_unreachable("invalid fixup kind!");
1660 case FK_Data_4:
Jason W Kim56a39902010-12-06 21:57:34 +00001661 Type = ((IsRelocWithSymbol || Addend !=0)
1662 ? ELF::R_MICROBLAZE_32
1663 : ELF::R_MICROBLAZE_64);
Wesley Peck4b047132010-11-21 22:06:28 +00001664 break;
1665 case FK_Data_2:
1666 Type = ELF::R_MICROBLAZE_32;
1667 break;
1668 }
1669 }
Jason W Kim56a39902010-12-06 21:57:34 +00001670 return Type;
Wesley Peck4b047132010-11-21 22:06:28 +00001671}
Jason W Kimd3443e92010-11-15 16:18:39 +00001672
1673//===- X86ELFObjectWriter -------------------------------------------===//
1674
1675
Rafael Espindola31f35782010-12-17 18:01:31 +00001676X86ELFObjectWriter::X86ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001677 raw_ostream &_OS,
1678 bool IsLittleEndian)
1679 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
Jason W Kimd3443e92010-11-15 16:18:39 +00001680{}
1681
1682X86ELFObjectWriter::~X86ELFObjectWriter()
1683{}
1684
Jason W Kim56a39902010-12-06 21:57:34 +00001685unsigned X86ELFObjectWriter::GetRelocType(const MCValue &Target,
1686 const MCFixup &Fixup,
1687 bool IsPCRel,
1688 bool IsRelocWithSymbol,
1689 int64_t Addend) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001690 // determine the type of the relocation
1691
Rafael Espindola12203cc2010-11-21 00:48:25 +00001692 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1693 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
Jason W Kimd3443e92010-11-15 16:18:39 +00001694 unsigned Type;
Rafael Espindolabff66a82010-12-18 03:27:34 +00001695 if (is64Bit()) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001696 if (IsPCRel) {
Rafael Espindola3a83c402010-12-27 00:36:05 +00001697 switch ((unsigned)Fixup.getKind()) {
1698 default: llvm_unreachable("invalid fixup kind!");
1699 case FK_PCRel_8:
1700 assert(Modifier == MCSymbolRefExpr::VK_None);
1701 Type = ELF::R_X86_64_PC64;
Jason W Kimd3443e92010-11-15 16:18:39 +00001702 break;
Rafael Espindola7a549972011-01-01 19:05:35 +00001703 case X86::reloc_signed_4byte:
Rafael Espindolac3a561c2010-12-27 02:03:24 +00001704 case X86::reloc_riprel_4byte_movq_load:
Rafael Espindola3a83c402010-12-27 00:36:05 +00001705 case FK_Data_4: // FIXME?
1706 case X86::reloc_riprel_4byte:
1707 case FK_PCRel_4:
1708 switch (Modifier) {
1709 default:
1710 llvm_unreachable("Unimplemented");
1711 case MCSymbolRefExpr::VK_None:
1712 Type = ELF::R_X86_64_PC32;
1713 break;
1714 case MCSymbolRefExpr::VK_PLT:
1715 Type = ELF::R_X86_64_PLT32;
1716 break;
1717 case MCSymbolRefExpr::VK_GOTPCREL:
1718 Type = ELF::R_X86_64_GOTPCREL;
1719 break;
1720 case MCSymbolRefExpr::VK_GOTTPOFF:
1721 Type = ELF::R_X86_64_GOTTPOFF;
Jason W Kimd3443e92010-11-15 16:18:39 +00001722 break;
Rafael Espindola3a83c402010-12-27 00:36:05 +00001723 case MCSymbolRefExpr::VK_TLSGD:
1724 Type = ELF::R_X86_64_TLSGD;
1725 break;
1726 case MCSymbolRefExpr::VK_TLSLD:
1727 Type = ELF::R_X86_64_TLSLD;
1728 break;
1729 }
Jason W Kimd3443e92010-11-15 16:18:39 +00001730 break;
Rafael Espindola3a83c402010-12-27 00:36:05 +00001731 case FK_PCRel_2:
1732 assert(Modifier == MCSymbolRefExpr::VK_None);
1733 Type = ELF::R_X86_64_PC16;
Jason W Kimd3443e92010-11-15 16:18:39 +00001734 break;
1735 }
1736 } else {
1737 switch ((unsigned)Fixup.getKind()) {
1738 default: llvm_unreachable("invalid fixup kind!");
1739 case FK_Data_8: Type = ELF::R_X86_64_64; break;
1740 case X86::reloc_signed_4byte:
Jason W Kimd3443e92010-11-15 16:18:39 +00001741 assert(isInt<32>(Target.getConstant()));
1742 switch (Modifier) {
1743 default:
1744 llvm_unreachable("Unimplemented");
1745 case MCSymbolRefExpr::VK_None:
1746 Type = ELF::R_X86_64_32S;
1747 break;
1748 case MCSymbolRefExpr::VK_GOT:
1749 Type = ELF::R_X86_64_GOT32;
1750 break;
1751 case MCSymbolRefExpr::VK_GOTPCREL:
1752 Type = ELF::R_X86_64_GOTPCREL;
1753 break;
1754 case MCSymbolRefExpr::VK_TPOFF:
1755 Type = ELF::R_X86_64_TPOFF32;
1756 break;
1757 case MCSymbolRefExpr::VK_DTPOFF:
1758 Type = ELF::R_X86_64_DTPOFF32;
1759 break;
1760 }
1761 break;
1762 case FK_Data_4:
1763 Type = ELF::R_X86_64_32;
1764 break;
1765 case FK_Data_2: Type = ELF::R_X86_64_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001766 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001767 case FK_Data_1: Type = ELF::R_X86_64_8; break;
1768 }
1769 }
1770 } else {
1771 if (IsPCRel) {
1772 switch (Modifier) {
1773 default:
1774 llvm_unreachable("Unimplemented");
1775 case MCSymbolRefExpr::VK_None:
1776 Type = ELF::R_386_PC32;
1777 break;
1778 case MCSymbolRefExpr::VK_PLT:
1779 Type = ELF::R_386_PLT32;
1780 break;
1781 }
1782 } else {
1783 switch ((unsigned)Fixup.getKind()) {
1784 default: llvm_unreachable("invalid fixup kind!");
1785
1786 case X86::reloc_global_offset_table:
1787 Type = ELF::R_386_GOTPC;
1788 break;
1789
1790 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
1791 // instead?
1792 case X86::reloc_signed_4byte:
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001793 case FK_PCRel_4:
Jason W Kimd3443e92010-11-15 16:18:39 +00001794 case FK_Data_4:
1795 switch (Modifier) {
1796 default:
1797 llvm_unreachable("Unimplemented");
1798 case MCSymbolRefExpr::VK_None:
1799 Type = ELF::R_386_32;
1800 break;
1801 case MCSymbolRefExpr::VK_GOT:
1802 Type = ELF::R_386_GOT32;
1803 break;
1804 case MCSymbolRefExpr::VK_GOTOFF:
1805 Type = ELF::R_386_GOTOFF;
1806 break;
1807 case MCSymbolRefExpr::VK_TLSGD:
1808 Type = ELF::R_386_TLS_GD;
1809 break;
1810 case MCSymbolRefExpr::VK_TPOFF:
1811 Type = ELF::R_386_TLS_LE_32;
1812 break;
1813 case MCSymbolRefExpr::VK_INDNTPOFF:
1814 Type = ELF::R_386_TLS_IE;
1815 break;
1816 case MCSymbolRefExpr::VK_NTPOFF:
1817 Type = ELF::R_386_TLS_LE;
1818 break;
1819 case MCSymbolRefExpr::VK_GOTNTPOFF:
1820 Type = ELF::R_386_TLS_GOTIE;
1821 break;
1822 case MCSymbolRefExpr::VK_TLSLDM:
1823 Type = ELF::R_386_TLS_LDM;
1824 break;
1825 case MCSymbolRefExpr::VK_DTPOFF:
1826 Type = ELF::R_386_TLS_LDO_32;
1827 break;
1828 }
1829 break;
1830 case FK_Data_2: Type = ELF::R_386_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001831 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001832 case FK_Data_1: Type = ELF::R_386_8; break;
1833 }
1834 }
1835 }
1836
1837 if (RelocNeedsGOT(Modifier))
1838 NeedsGOT = true;
1839
Jason W Kim56a39902010-12-06 21:57:34 +00001840 return Type;
Matt Fleming3565a062010-08-16 18:57:57 +00001841}