blob: 0db54b5d48f8aa0894369c08e965172a046c08a8 [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
Rafael Espindolafea753b2010-12-24 21:22:02 +0000362 virtual bool
363 IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
364 const MCSymbolData &DataA,
365 const MCFragment &FB,
366 bool InSet,
367 bool IsPCRel) const;
Rafael Espindola70703872010-09-30 02:22:20 +0000368
Jason W Kimd3443e92010-11-15 16:18:39 +0000369 virtual void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
370 virtual void WriteSection(MCAssembler &Asm,
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000371 const SectionIndexMapTy &SectionIndexMap,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000372 uint32_t GroupSymbolIndex,
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000373 uint64_t Offset, uint64_t Size, uint64_t Alignment,
374 const MCSectionELF &Section);
Jason W Kim56a39902010-12-06 21:57:34 +0000375
376 protected:
377 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
378 bool IsPCRel, bool IsRelocWithSymbol,
379 int64_t Addend) = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000380 };
381
Jason W Kimd3443e92010-11-15 16:18:39 +0000382 //===- X86ELFObjectWriter -------------------------------------------===//
383
384 class X86ELFObjectWriter : public ELFObjectWriter {
385 public:
Rafael Espindola31f35782010-12-17 18:01:31 +0000386 X86ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +0000387 raw_ostream &_OS,
388 bool IsLittleEndian);
Wesley Peck4b047132010-11-21 22:06:28 +0000389
Jason W Kimd3443e92010-11-15 16:18:39 +0000390 virtual ~X86ELFObjectWriter();
Jason W Kim56a39902010-12-06 21:57:34 +0000391 protected:
392 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
393 bool IsPCRel, bool IsRelocWithSymbol,
394 int64_t Addend);
Jason W Kimd3443e92010-11-15 16:18:39 +0000395 };
396
397
398 //===- ARMELFObjectWriter -------------------------------------------===//
399
400 class ARMELFObjectWriter : public ELFObjectWriter {
401 public:
Jason W Kim2d7a53a2011-02-04 21:41:11 +0000402 // FIXME: MCAssembler can't yet return the Subtarget,
403 enum { DefaultEABIVersion = 0x05000000U };
404
Rafael Espindola31f35782010-12-17 18:01:31 +0000405 ARMELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +0000406 raw_ostream &_OS,
407 bool IsLittleEndian);
Wesley Peck4b047132010-11-21 22:06:28 +0000408
Jason W Kimd3443e92010-11-15 16:18:39 +0000409 virtual ~ARMELFObjectWriter();
Jason W Kim2d7a53a2011-02-04 21:41:11 +0000410
411 virtual void WriteEFlags();
Jason W Kim85fed5e2010-12-01 02:40:06 +0000412 protected:
Jason W Kim953a2a32011-02-07 01:11:15 +0000413 virtual const MCSymbol *ExplicitRelSym(const MCAssembler &Asm,
414 const MCValue &Target,
415 const MCFragment &F,
416 bool IsBSS) const;
417
Jason W Kim56a39902010-12-06 21:57:34 +0000418 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
419 bool IsPCRel, bool IsRelocWithSymbol,
420 int64_t Addend);
Jason W Kimd3443e92010-11-15 16:18:39 +0000421 };
Wesley Peck4b047132010-11-21 22:06:28 +0000422
423 //===- MBlazeELFObjectWriter -------------------------------------------===//
424
425 class MBlazeELFObjectWriter : public ELFObjectWriter {
426 public:
Rafael Espindola31f35782010-12-17 18:01:31 +0000427 MBlazeELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +0000428 raw_ostream &_OS,
429 bool IsLittleEndian);
Wesley Peck4b047132010-11-21 22:06:28 +0000430
431 virtual ~MBlazeELFObjectWriter();
Jason W Kim56a39902010-12-06 21:57:34 +0000432 protected:
433 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
434 bool IsPCRel, bool IsRelocWithSymbol,
435 int64_t Addend);
Wesley Peck4b047132010-11-21 22:06:28 +0000436 };
Matt Fleming3565a062010-08-16 18:57:57 +0000437}
438
Jason W Kimd3443e92010-11-15 16:18:39 +0000439ELFObjectWriter::~ELFObjectWriter()
440{}
441
Matt Fleming3565a062010-08-16 18:57:57 +0000442// Emit the ELF header.
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000443void ELFObjectWriter::WriteHeader(uint64_t SectionDataSize,
444 unsigned NumberOfSections) {
Matt Fleming3565a062010-08-16 18:57:57 +0000445 // ELF Header
446 // ----------
447 //
448 // Note
449 // ----
450 // emitWord method behaves differently for ELF32 and ELF64, writing
451 // 4 bytes in the former and 8 in the latter.
452
453 Write8(0x7f); // e_ident[EI_MAG0]
454 Write8('E'); // e_ident[EI_MAG1]
455 Write8('L'); // e_ident[EI_MAG2]
456 Write8('F'); // e_ident[EI_MAG3]
457
Rafael Espindolabff66a82010-12-18 03:27:34 +0000458 Write8(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
Matt Fleming3565a062010-08-16 18:57:57 +0000459
460 // e_ident[EI_DATA]
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000461 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
Matt Fleming3565a062010-08-16 18:57:57 +0000462
463 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky5baf79e2010-09-09 17:57:50 +0000464 // e_ident[EI_OSABI]
Rafael Espindolabff66a82010-12-18 03:27:34 +0000465 switch (TargetObjectWriter->getOSType()) {
Roman Divacky5baf79e2010-09-09 17:57:50 +0000466 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
467 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
468 default: Write8(ELF::ELFOSABI_NONE); break;
469 }
Matt Fleming3565a062010-08-16 18:57:57 +0000470 Write8(0); // e_ident[EI_ABIVERSION]
471
472 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
473
474 Write16(ELF::ET_REL); // e_type
475
Rafael Espindolabff66a82010-12-18 03:27:34 +0000476 Write16(TargetObjectWriter->getEMachine()); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +0000477
478 Write32(ELF::EV_CURRENT); // e_version
479 WriteWord(0); // e_entry, no entry point in .o file
480 WriteWord(0); // e_phoff, no program header for .o
Rafael Espindolabff66a82010-12-18 03:27:34 +0000481 WriteWord(SectionDataSize + (is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
Benjamin Kramereb976772010-08-17 17:02:29 +0000482 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000483
Jason W Kim2d7a53a2011-02-04 21:41:11 +0000484 // e_flags = whatever the target wants
485 WriteEFlags();
Matt Fleming3565a062010-08-16 18:57:57 +0000486
487 // e_ehsize = ELF header size
Rafael Espindolabff66a82010-12-18 03:27:34 +0000488 Write16(is64Bit() ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
Matt Fleming3565a062010-08-16 18:57:57 +0000489
490 Write16(0); // e_phentsize = prog header entry size
491 Write16(0); // e_phnum = # prog header entries = 0
492
493 // e_shentsize = Section header entry size
Rafael Espindolabff66a82010-12-18 03:27:34 +0000494 Write16(is64Bit() ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
Matt Fleming3565a062010-08-16 18:57:57 +0000495
496 // e_shnum = # of section header ents
Rafael Espindola7be2c332010-10-31 00:16:26 +0000497 if (NumberOfSections >= ELF::SHN_LORESERVE)
498 Write16(0);
499 else
500 Write16(NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000501
502 // e_shstrndx = Section # of '.shstrtab'
Rafael Espindola7be2c332010-10-31 00:16:26 +0000503 if (NumberOfSections >= ELF::SHN_LORESERVE)
504 Write16(ELF::SHN_XINDEX);
505 else
506 Write16(ShstrtabIndex);
Matt Fleming3565a062010-08-16 18:57:57 +0000507}
508
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000509void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
510 MCDataFragment *ShndxF,
511 uint64_t name,
512 uint8_t info, uint64_t value,
513 uint64_t size, uint8_t other,
514 uint32_t shndx,
515 bool Reserved) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000516 if (ShndxF) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000517 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000518 String32(*ShndxF, shndx);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000519 else
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000520 String32(*ShndxF, 0);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000521 }
522
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000523 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
524 uint16_t(ELF::SHN_XINDEX) : shndx;
525
Rafael Espindolabff66a82010-12-18 03:27:34 +0000526 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000527 String32(*SymtabF, name); // st_name
528 String8(*SymtabF, info); // st_info
529 String8(*SymtabF, other); // st_other
530 String16(*SymtabF, Index); // st_shndx
531 String64(*SymtabF, value); // st_value
532 String64(*SymtabF, size); // st_size
Matt Fleming3565a062010-08-16 18:57:57 +0000533 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000534 String32(*SymtabF, name); // st_name
535 String32(*SymtabF, value); // st_value
536 String32(*SymtabF, size); // st_size
537 String8(*SymtabF, info); // st_info
538 String8(*SymtabF, other); // st_other
539 String16(*SymtabF, Index); // st_shndx
Matt Fleming3565a062010-08-16 18:57:57 +0000540 }
541}
542
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000543static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout) {
544 if (Data.isCommon() && Data.isExternal())
545 return Data.getCommonAlignment();
546
547 const MCSymbol &Symbol = Data.getSymbol();
Roman Divackyd1491862010-12-20 21:14:39 +0000548
549 if (Symbol.isAbsolute() && Symbol.isVariable()) {
550 if (const MCExpr *Value = Symbol.getVariableValue()) {
551 int64_t IntValue;
552 if (Value->EvaluateAsAbsolute(IntValue, Layout))
553 return (uint64_t)IntValue;
554 }
555 }
556
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000557 if (!Symbol.isInSection())
558 return 0;
559
Rafael Espindolaffd902b2010-12-06 02:57:26 +0000560 if (Data.getFragment())
561 return Layout.getSymbolOffset(&Data);
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000562
563 return 0;
564}
565
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000566void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
567 const MCAsmLayout &Layout) {
Rafael Espindola88182132010-10-27 15:18:17 +0000568 // The presence of symbol versions causes undefined symbols and
569 // versions declared with @@@ to be renamed.
570
571 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
572 ie = Asm.symbol_end(); it != ie; ++it) {
573 const MCSymbol &Alias = it->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000574 const MCSymbol &Symbol = Alias.AliasedSymbol();
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000575 MCSymbolData &SD = Asm.getSymbolData(Symbol);
576
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000577 // Not an alias.
578 if (&Symbol == &Alias)
579 continue;
580
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000581 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000582 size_t Pos = AliasName.find('@');
583 if (Pos == StringRef::npos)
584 continue;
585
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000586 // Aliases defined with .symvar copy the binding from the symbol they alias.
587 // This is the first place we are able to copy this information.
588 it->setExternal(SD.isExternal());
589 SetBinding(*it, GetBinding(SD));
590
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000591 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000592 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
593 continue;
594
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000595 // FIXME: produce a better error message.
596 if (Symbol.isUndefined() && Rest.startswith("@@") &&
597 !Rest.startswith("@@@"))
598 report_fatal_error("A @@ version cannot be undefined");
599
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000600 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000601 }
602}
603
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000604void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
605 MCDataFragment *ShndxF,
606 ELFSymbolData &MSD,
607 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000608 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000609 MCSymbolData &Data =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000610 Layout.getAssembler().getSymbolData(OrigData.getSymbol().AliasedSymbol());
Rafael Espindola152c1062010-10-06 21:02:29 +0000611
Rafael Espindola7be2c332010-10-31 00:16:26 +0000612 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
613 Data.getSymbol().isVariable();
614
Rafael Espindola152c1062010-10-06 21:02:29 +0000615 uint8_t Binding = GetBinding(OrigData);
616 uint8_t Visibility = GetVisibility(OrigData);
617 uint8_t Type = GetType(Data);
618
619 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
620 uint8_t Other = Visibility;
621
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000622 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000623 uint64_t Size = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000624
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000625 assert(!(Data.isCommon() && !Data.isExternal()));
626
Rafael Espindolaf0121242010-12-22 16:03:00 +0000627 const MCExpr *ESize = Data.getSize();
628 if (ESize) {
629 int64_t Res;
630 if (!ESize->EvaluateAsAbsolute(Res, Layout))
631 report_fatal_error("Size expression must be absolute.");
632 Size = Res;
Matt Fleming3565a062010-08-16 18:57:57 +0000633 }
634
635 // Write out the symbol table entry
Rafael Espindola7be2c332010-10-31 00:16:26 +0000636 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
637 Size, Other, MSD.SectionIndex, IsReserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000638}
639
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000640void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
641 MCDataFragment *ShndxF,
642 const MCAssembler &Asm,
643 const MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000644 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000645 // The string table must be emitted first because we need the index
646 // into the string table for all the symbol names.
647 assert(StringTable.size() && "Missing string table");
648
649 // FIXME: Make sure the start of the symbol table is aligned.
650
651 // The first entry is the undefined symbol entry.
Rafael Espindola7be2c332010-10-31 00:16:26 +0000652 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
Matt Fleming3565a062010-08-16 18:57:57 +0000653
654 // Write the symbol table entries.
655 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
656 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
657 ELFSymbolData &MSD = LocalSymbolData[i];
Rafael Espindola7be2c332010-10-31 00:16:26 +0000658 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000659 }
660
Rafael Espindola71859c62010-09-16 19:46:31 +0000661 // Write out a symbol table entry for each regular section.
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000662 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
663 ++i) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000664 const MCSectionELF &Section =
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000665 static_cast<const MCSectionELF&>(i->getSection());
666 if (Section.getType() == ELF::SHT_RELA ||
667 Section.getType() == ELF::SHT_REL ||
668 Section.getType() == ELF::SHT_STRTAB ||
669 Section.getType() == ELF::SHT_SYMTAB)
Eli Friedmana44fa242010-08-16 21:17:09 +0000670 continue;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000671 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000672 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section), false);
Eli Friedmana44fa242010-08-16 21:17:09 +0000673 LastLocalSymbolIndex++;
674 }
Matt Fleming3565a062010-08-16 18:57:57 +0000675
676 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
677 ELFSymbolData &MSD = ExternalSymbolData[i];
678 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000679 assert(((Data.getFlags() & ELF_STB_Global) ||
680 (Data.getFlags() & ELF_STB_Weak)) &&
681 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000682 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000683 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000684 LastLocalSymbolIndex++;
685 }
686
687 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
688 ELFSymbolData &MSD = UndefinedSymbolData[i];
689 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000690 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000691 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000692 LastLocalSymbolIndex++;
693 }
694}
695
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000696const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm,
697 const MCValue &Target,
698 const MCFragment &F) const {
699 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000700 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
701 const MCSymbol *Renamed = Renames.lookup(&Symbol);
702 const MCSymbolData &SD = Asm.getSymbolData(Symbol);
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000703
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000704 if (ASymbol.isUndefined()) {
705 if (Renamed)
706 return Renamed;
707 return &ASymbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000708 }
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000709
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000710 if (SD.isExternal()) {
711 if (Renamed)
712 return Renamed;
713 return &Symbol;
714 }
Rafael Espindola73ffea42010-09-25 05:42:19 +0000715
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000716 const MCSectionELF &Section =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000717 static_cast<const MCSectionELF&>(ASymbol.getSection());
Rafael Espindola25958732010-11-24 21:57:39 +0000718 const SectionKind secKind = Section.getKind();
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000719
Rafael Espindola25958732010-11-24 21:57:39 +0000720 if (secKind.isBSS())
Jason W Kim953a2a32011-02-07 01:11:15 +0000721 return ExplicitRelSym(Asm, Target, F, true);
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000722
Rafael Espindola25958732010-11-24 21:57:39 +0000723 if (secKind.isThreadLocal()) {
724 if (Renamed)
725 return Renamed;
726 return &Symbol;
727 }
728
Rafael Espindola8cecf252010-10-06 16:23:36 +0000729 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000730 const MCSectionELF &Sec2 =
731 static_cast<const MCSectionELF&>(F.getParent()->getSection());
732
Rafael Espindola8cecf252010-10-06 16:23:36 +0000733 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000734 (Kind == MCSymbolRefExpr::VK_PLT ||
735 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
Rafael Espindola25958732010-11-24 21:57:39 +0000736 Kind == MCSymbolRefExpr::VK_GOTOFF)) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000737 if (Renamed)
738 return Renamed;
739 return &Symbol;
740 }
Rafael Espindola3729d002010-10-05 23:57:26 +0000741
Rafael Espindola1c130262011-01-23 04:43:11 +0000742 if (Section.getFlags() & ELF::SHF_MERGE) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000743 if (Target.getConstant() == 0)
744 return NULL;
745 if (Renamed)
746 return Renamed;
747 return &Symbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000748 }
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000749
Jason W Kim953a2a32011-02-07 01:11:15 +0000750 return ExplicitRelSym(Asm, Target, F, false);
Rafael Espindola73ffea42010-09-25 05:42:19 +0000751}
752
Matt Fleming3565a062010-08-16 18:57:57 +0000753
Jason W Kim56a39902010-12-06 21:57:34 +0000754void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
755 const MCAsmLayout &Layout,
756 const MCFragment *Fragment,
757 const MCFixup &Fixup,
758 MCValue Target,
759 uint64_t &FixedValue) {
760 int64_t Addend = 0;
761 int Index = 0;
762 int64_t Value = Target.getConstant();
763 const MCSymbol *RelocSymbol = NULL;
764
Rafael Espindola127a6a42010-12-17 07:28:17 +0000765 bool IsPCRel = isFixupKindPCRel(Asm, Fixup.getKind());
Jason W Kim56a39902010-12-06 21:57:34 +0000766 if (!Target.isAbsolute()) {
767 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
768 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
769 RelocSymbol = SymbolToReloc(Asm, Target, *Fragment);
770
771 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
772 const MCSymbol &SymbolB = RefB->getSymbol();
773 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
774 IsPCRel = true;
775
776 // Offset of the symbol in the section
777 int64_t a = Layout.getSymbolOffset(&SDB);
778
779 // Ofeset of the relocation in the section
780 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
781 Value += b - a;
782 }
783
784 if (!RelocSymbol) {
785 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
786 MCFragment *F = SD.getFragment();
787
788 Index = F->getParent()->getOrdinal() + 1;
789
790 // Offset of the symbol in the section
791 Value += Layout.getSymbolOffset(&SD);
792 } else {
793 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
794 WeakrefUsedInReloc.insert(RelocSymbol);
795 else
796 UsedInReloc.insert(RelocSymbol);
797 Index = -1;
798 }
799 Addend = Value;
800 // Compensate for the addend on i386.
Rafael Espindolabff66a82010-12-18 03:27:34 +0000801 if (is64Bit())
Jason W Kim56a39902010-12-06 21:57:34 +0000802 Value = 0;
803 }
804
805 FixedValue = Value;
806 unsigned Type = GetRelocType(Target, Fixup, IsPCRel,
807 (RelocSymbol != 0), Addend);
808
809 uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
810 Fixup.getOffset();
811
Rafael Espindolabff66a82010-12-18 03:27:34 +0000812 if (!hasRelocationAddend())
813 Addend = 0;
Jason W Kim56a39902010-12-06 21:57:34 +0000814 ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend);
815 Relocations[Fragment->getParent()].push_back(ERE);
816}
817
818
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000819uint64_t
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000820ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
821 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000822 MCSymbolData &SD = Asm.getSymbolData(*S);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000823 return SD.getIndex();
Matt Fleming3565a062010-08-16 18:57:57 +0000824}
825
Rafael Espindola737cd212010-10-05 18:01:23 +0000826static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
Rafael Espindola88182132010-10-27 15:18:17 +0000827 bool Used, bool Renamed) {
Rafael Espindola484291c2010-11-01 14:28:48 +0000828 if (Data.getFlags() & ELF_Other_Weakref)
829 return false;
830
Rafael Espindolabd701182010-10-19 19:31:37 +0000831 if (Used)
832 return true;
833
Rafael Espindola88182132010-10-27 15:18:17 +0000834 if (Renamed)
835 return false;
836
Rafael Espindola737cd212010-10-05 18:01:23 +0000837 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000838
Rafael Espindolad1798862010-10-29 23:09:31 +0000839 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
840 return true;
841
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000842 const MCSymbol &A = Symbol.AliasedSymbol();
Rafael Espindolad1798862010-10-29 23:09:31 +0000843 if (!A.isVariable() && A.isUndefined() && !Data.isCommon())
Rafael Espindolaa6866962010-10-27 14:44:52 +0000844 return false;
845
Rafael Espindola737cd212010-10-05 18:01:23 +0000846 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
847 return false;
848
Rafael Espindolabd701182010-10-19 19:31:37 +0000849 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000850 return false;
851
852 return true;
853}
854
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000855static bool isLocal(const MCSymbolData &Data, bool isSignature,
856 bool isUsedInReloc) {
Rafael Espindola737cd212010-10-05 18:01:23 +0000857 if (Data.isExternal())
858 return false;
859
860 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000861 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000862
863 if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
864 if (isSignature && !isUsedInReloc)
865 return true;
866
Rafael Espindola737cd212010-10-05 18:01:23 +0000867 return false;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000868 }
Rafael Espindola737cd212010-10-05 18:01:23 +0000869
870 return true;
871}
872
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000873void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
874 SectionIndexMapTy &SectionIndexMap) {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000875 unsigned Index = 1;
876 for (MCAssembler::iterator it = Asm.begin(),
877 ie = Asm.end(); it != ie; ++it) {
878 const MCSectionELF &Section =
879 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000880 if (Section.getType() != ELF::SHT_GROUP)
881 continue;
882 SectionIndexMap[&Section] = Index++;
883 }
884
885 for (MCAssembler::iterator it = Asm.begin(),
886 ie = Asm.end(); it != ie; ++it) {
887 const MCSectionELF &Section =
888 static_cast<const MCSectionELF &>(it->getSection());
889 if (Section.getType() == ELF::SHT_GROUP)
890 continue;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000891 SectionIndexMap[&Section] = Index++;
892 }
893}
894
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000895void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000896 const SectionIndexMapTy &SectionIndexMap,
897 RevGroupMapTy RevGroupMap) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000898 // FIXME: Is this the correct place to do this?
899 if (NeedsGOT) {
900 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
901 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
902 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
903 Data.setExternal(true);
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000904 SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindola5c77c162010-10-05 15:48:37 +0000905 }
906
Matt Fleming3565a062010-08-16 18:57:57 +0000907 // Build section lookup table.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000908 int NumRegularSections = Asm.size();
Matt Fleming3565a062010-08-16 18:57:57 +0000909
910 // Index 0 is always the empty string.
911 StringMap<uint64_t> StringIndexMap;
912 StringTable += '\x00';
913
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000914 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000915 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
916 ie = Asm.symbol_end(); it != ie; ++it) {
917 const MCSymbol &Symbol = it->getSymbol();
918
Rafael Espindola484291c2010-11-01 14:28:48 +0000919 bool Used = UsedInReloc.count(&Symbol);
920 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000921 bool isSignature = RevGroupMap.count(&Symbol);
922
923 if (!isInSymtab(Asm, *it,
924 Used || WeakrefUsed || isSignature,
Rafael Espindola88182132010-10-27 15:18:17 +0000925 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000926 continue;
927
Matt Fleming3565a062010-08-16 18:57:57 +0000928 ELFSymbolData MSD;
929 MSD.SymbolData = it;
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000930 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Matt Fleming3565a062010-08-16 18:57:57 +0000931
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000932 // Undefined symbols are global, but this is the first place we
933 // are able to set it.
934 bool Local = isLocal(*it, isSignature, Used);
935 if (!Local && GetBinding(*it) == ELF::STB_LOCAL) {
936 MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
937 SetBinding(*it, ELF::STB_GLOBAL);
938 SetBinding(SD, ELF::STB_GLOBAL);
939 }
940
Rafael Espindola484291c2010-11-01 14:28:48 +0000941 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
942 SetBinding(*it, ELF::STB_WEAK);
943
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000944 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000945 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000946 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000947 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000948 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000949 } else if (RefSymbol.isUndefined()) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000950 if (isSignature && !Used)
951 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
952 else
953 MSD.SectionIndex = ELF::SHN_UNDEF;
Matt Fleming3565a062010-08-16 18:57:57 +0000954 } else {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000955 const MCSectionELF &Section =
956 static_cast<const MCSectionELF&>(RefSymbol.getSection());
957 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000958 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
959 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000960 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000961 }
962
Rafael Espindola88182132010-10-27 15:18:17 +0000963 // The @@@ in symbol version is replaced with @ in undefined symbols and
964 // @@ in defined ones.
965 StringRef Name = Symbol.getName();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000966 SmallString<32> Buf;
967
Rafael Espindola88182132010-10-27 15:18:17 +0000968 size_t Pos = Name.find("@@@");
Rafael Espindola88182132010-10-27 15:18:17 +0000969 if (Pos != StringRef::npos) {
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000970 Buf += Name.substr(0, Pos);
971 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
972 Buf += Name.substr(Pos + Skip);
973 Name = Buf;
Rafael Espindola88182132010-10-27 15:18:17 +0000974 }
975
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000976 uint64_t &Entry = StringIndexMap[Name];
Rafael Espindolaa6866962010-10-27 14:44:52 +0000977 if (!Entry) {
978 Entry = StringTable.size();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000979 StringTable += Name;
Rafael Espindolaa6866962010-10-27 14:44:52 +0000980 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +0000981 }
Rafael Espindolaa6866962010-10-27 14:44:52 +0000982 MSD.StringIndex = Entry;
983 if (MSD.SectionIndex == ELF::SHN_UNDEF)
984 UndefinedSymbolData.push_back(MSD);
985 else if (Local)
986 LocalSymbolData.push_back(MSD);
987 else
988 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +0000989 }
990
991 // Symbols are required to be in lexicographic order.
992 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
993 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
994 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
995
996 // Set the symbol indices. Local symbols must come before all other
997 // symbols with non-local bindings.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000998 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000999 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
1000 LocalSymbolData[i].SymbolData->setIndex(Index++);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +00001001
1002 Index += NumRegularSections;
1003
Matt Fleming3565a062010-08-16 18:57:57 +00001004 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
1005 ExternalSymbolData[i].SymbolData->setIndex(Index++);
1006 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
1007 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
1008}
1009
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001010void ELFObjectWriter::WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
1011 const MCSectionData &SD) {
Matt Fleming3565a062010-08-16 18:57:57 +00001012 if (!Relocations[&SD].empty()) {
1013 MCContext &Ctx = Asm.getContext();
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001014 const MCSectionELF *RelaSection;
Matt Fleming3565a062010-08-16 18:57:57 +00001015 const MCSectionELF &Section =
1016 static_cast<const MCSectionELF&>(SD.getSection());
1017
1018 const StringRef SectionName = Section.getSectionName();
Rafael Espindolabff66a82010-12-18 03:27:34 +00001019 std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +00001020 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +00001021
1022 unsigned EntrySize;
Rafael Espindolabff66a82010-12-18 03:27:34 +00001023 if (hasRelocationAddend())
1024 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
Benjamin Kramer299fbe32010-08-17 17:56:13 +00001025 else
Rafael Espindolabff66a82010-12-18 03:27:34 +00001026 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +00001027
Rafael Espindolabff66a82010-12-18 03:27:34 +00001028 RelaSection = Ctx.getELFSection(RelaSectionName, hasRelocationAddend() ?
Benjamin Kramer377a5722010-08-17 17:30:07 +00001029 ELF::SHT_RELA : ELF::SHT_REL, 0,
Matt Fleming3565a062010-08-16 18:57:57 +00001030 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001031 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001032
1033 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +00001034 RelaSD.setAlignment(is64Bit() ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +00001035
1036 MCDataFragment *F = new MCDataFragment(&RelaSD);
1037
1038 WriteRelocationsFragment(Asm, F, &SD);
Matt Fleming3565a062010-08-16 18:57:57 +00001039 }
1040}
1041
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001042void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
1043 uint64_t Flags, uint64_t Address,
1044 uint64_t Offset, uint64_t Size,
1045 uint32_t Link, uint32_t Info,
1046 uint64_t Alignment,
1047 uint64_t EntrySize) {
Matt Fleming3565a062010-08-16 18:57:57 +00001048 Write32(Name); // sh_name: index into string table
1049 Write32(Type); // sh_type
1050 WriteWord(Flags); // sh_flags
1051 WriteWord(Address); // sh_addr
1052 WriteWord(Offset); // sh_offset
1053 WriteWord(Size); // sh_size
1054 Write32(Link); // sh_link
1055 Write32(Info); // sh_info
1056 WriteWord(Alignment); // sh_addralign
1057 WriteWord(EntrySize); // sh_entsize
1058}
1059
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001060void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
1061 MCDataFragment *F,
1062 const MCSectionData *SD) {
Matt Fleming3565a062010-08-16 18:57:57 +00001063 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
1064 // sort by the r_offset just like gnu as does
1065 array_pod_sort(Relocs.begin(), Relocs.end());
1066
1067 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1068 ELFRelocationEntry entry = Relocs[e - i - 1];
1069
Rafael Espindola12203cc2010-11-21 00:48:25 +00001070 if (!entry.Index)
1071 ;
1072 else if (entry.Index < 0)
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001073 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
1074 else
Rafael Espindola12203cc2010-11-21 00:48:25 +00001075 entry.Index += LocalSymbolData.size();
Rafael Espindolabff66a82010-12-18 03:27:34 +00001076 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001077 String64(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001078
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001079 struct ELF::Elf64_Rela ERE64;
1080 ERE64.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001081 String64(*F, ERE64.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001082
Rafael Espindolabff66a82010-12-18 03:27:34 +00001083 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001084 String64(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001085 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001086 String32(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001087
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001088 struct ELF::Elf32_Rela ERE32;
1089 ERE32.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001090 String32(*F, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001091
Rafael Espindolabff66a82010-12-18 03:27:34 +00001092 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001093 String32(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001094 }
Matt Fleming3565a062010-08-16 18:57:57 +00001095 }
1096}
1097
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001098void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
1099 MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001100 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +00001101 MCContext &Ctx = Asm.getContext();
1102 MCDataFragment *F;
1103
Rafael Espindolabff66a82010-12-18 03:27:34 +00001104 unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
Matt Fleming3565a062010-08-16 18:57:57 +00001105
Rafael Espindola38738bf2010-09-22 19:04:41 +00001106 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001107 const MCSectionELF *ShstrtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001108 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001109 SectionKind::getReadOnly());
Rafael Espindola71859c62010-09-16 19:46:31 +00001110 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1111 ShstrtabSD.setAlignment(1);
1112 ShstrtabIndex = Asm.size();
1113
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001114 const MCSectionELF *SymtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001115 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1116 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001117 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001118 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +00001119 SymtabSD.setAlignment(is64Bit() ? 8 : 4);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001120 SymbolTableIndex = Asm.size();
1121
1122 MCSectionData *SymtabShndxSD = NULL;
1123
1124 if (NeedsSymtabShndx) {
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001125 const MCSectionELF *SymtabShndxSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001126 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001127 SectionKind::getReadOnly(), 4, "");
Rafael Espindola7be2c332010-10-31 00:16:26 +00001128 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
1129 SymtabShndxSD->setAlignment(4);
1130 }
Matt Fleming3565a062010-08-16 18:57:57 +00001131
Matt Fleming3565a062010-08-16 18:57:57 +00001132 const MCSection *StrtabSection;
1133 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001134 SectionKind::getReadOnly());
Matt Fleming3565a062010-08-16 18:57:57 +00001135 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1136 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +00001137 StringTableIndex = Asm.size();
1138
Rafael Espindolac3c413f2010-09-27 22:04:54 +00001139 WriteRelocations(Asm, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001140
1141 // Symbol table
1142 F = new MCDataFragment(&SymtabSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001143 MCDataFragment *ShndxF = NULL;
1144 if (NeedsSymtabShndx) {
1145 ShndxF = new MCDataFragment(SymtabShndxSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001146 }
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001147 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
Rafael Espindola71859c62010-09-16 19:46:31 +00001148
Matt Fleming3565a062010-08-16 18:57:57 +00001149 F = new MCDataFragment(&StrtabSD);
1150 F->getContents().append(StringTable.begin(), StringTable.end());
Matt Fleming3565a062010-08-16 18:57:57 +00001151
Matt Fleming3565a062010-08-16 18:57:57 +00001152 F = new MCDataFragment(&ShstrtabSD);
1153
Matt Fleming3565a062010-08-16 18:57:57 +00001154 // Section header string table.
1155 //
1156 // The first entry of a string table holds a null character so skip
1157 // section 0.
1158 uint64_t Index = 1;
1159 F->getContents() += '\x00';
1160
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001161 StringMap<uint64_t> SecStringMap;
Matt Fleming3565a062010-08-16 18:57:57 +00001162 for (MCAssembler::const_iterator it = Asm.begin(),
1163 ie = Asm.end(); it != ie; ++it) {
Matt Fleming3565a062010-08-16 18:57:57 +00001164 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +00001165 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola51efe7a2010-09-23 14:14:56 +00001166 // FIXME: We could merge suffixes like in .text and .rela.text.
Matt Fleming3565a062010-08-16 18:57:57 +00001167
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001168 StringRef Name = Section.getSectionName();
1169 if (SecStringMap.count(Name)) {
1170 SectionStringTableIndex[&Section] = SecStringMap[Name];
1171 continue;
1172 }
Matt Fleming3565a062010-08-16 18:57:57 +00001173 // Remember the index into the string table so we can write it
1174 // into the sh_name field of the section header table.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001175 SectionStringTableIndex[&Section] = Index;
1176 SecStringMap[Name] = Index;
Matt Fleming3565a062010-08-16 18:57:57 +00001177
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001178 Index += Name.size() + 1;
1179 F->getContents() += Name;
Matt Fleming3565a062010-08-16 18:57:57 +00001180 F->getContents() += '\x00';
1181 }
Rafael Espindola70703872010-09-30 02:22:20 +00001182}
1183
Rafael Espindolafea753b2010-12-24 21:22:02 +00001184bool
1185ELFObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
1186 const MCSymbolData &DataA,
1187 const MCFragment &FB,
1188 bool InSet,
1189 bool IsPCRel) const {
Rafael Espindola2c920852010-11-16 04:11:46 +00001190 // FIXME: This is in here just to match gnu as output. If the two ends
1191 // are in the same section, there is nothing that the linker can do to
1192 // break it.
Rafael Espindola70703872010-09-30 02:22:20 +00001193 if (DataA.isExternal())
1194 return false;
1195
Rafael Espindolafea753b2010-12-24 21:22:02 +00001196 const MCSection &SecA = DataA.getSymbol().AliasedSymbol().getSection();
1197 const MCSection &SecB = FB.getParent()->getSection();
1198 // On ELF A - B is absolute if A and B are in the same section.
1199 return &SecA == &SecB;
Matt Fleming3565a062010-08-16 18:57:57 +00001200}
1201
Rafael Espindola96aa78c2011-01-23 17:55:27 +00001202void ELFObjectWriter::CreateIndexedSections(MCAssembler &Asm,
1203 MCAsmLayout &Layout,
1204 GroupMapTy &GroupMap,
1205 RevGroupMapTy &RevGroupMap) {
1206 // Create the .note.GNU-stack section if needed.
1207 MCContext &Ctx = Asm.getContext();
1208 if (Asm.getNoExecStack()) {
1209 const MCSectionELF *GnuStackSection =
1210 Ctx.getELFSection(".note.GNU-stack", ELF::SHT_PROGBITS, 0,
1211 SectionKind::getReadOnly());
1212 Asm.getOrCreateSectionData(*GnuStackSection);
1213 }
1214
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001215 // Build the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001216 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1217 it != ie; ++it) {
1218 const MCSectionELF &Section =
1219 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola1c130262011-01-23 04:43:11 +00001220 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001221 continue;
1222
1223 const MCSymbol *SignatureSymbol = Section.getGroup();
1224 Asm.getOrCreateSymbolData(*SignatureSymbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001225 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001226 if (!Group) {
Rafael Espindola96aa78c2011-01-23 17:55:27 +00001227 Group = Ctx.CreateELFGroupSection();
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001228 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1229 Data.setAlignment(4);
1230 MCDataFragment *F = new MCDataFragment(&Data);
1231 String32(*F, ELF::GRP_COMDAT);
1232 }
1233 GroupMap[Group] = SignatureSymbol;
1234 }
1235
1236 // Add sections to the groups
1237 unsigned Index = 1;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001238 unsigned NumGroups = RevGroupMap.size();
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001239 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1240 it != ie; ++it, ++Index) {
1241 const MCSectionELF &Section =
1242 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola1c130262011-01-23 04:43:11 +00001243 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001244 continue;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001245 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001246 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1247 // FIXME: we could use the previous fragment
1248 MCDataFragment *F = new MCDataFragment(&Data);
1249 String32(*F, NumGroups + Index);
1250 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001251}
1252
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001253void ELFObjectWriter::WriteSection(MCAssembler &Asm,
1254 const SectionIndexMapTy &SectionIndexMap,
1255 uint32_t GroupSymbolIndex,
1256 uint64_t Offset, uint64_t Size,
1257 uint64_t Alignment,
1258 const MCSectionELF &Section) {
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001259 uint64_t sh_link = 0;
1260 uint64_t sh_info = 0;
1261
1262 switch(Section.getType()) {
1263 case ELF::SHT_DYNAMIC:
1264 sh_link = SectionStringTableIndex[&Section];
1265 sh_info = 0;
1266 break;
1267
1268 case ELF::SHT_REL:
1269 case ELF::SHT_RELA: {
1270 const MCSectionELF *SymtabSection;
1271 const MCSectionELF *InfoSection;
1272 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
1273 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001274 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001275 sh_link = SectionIndexMap.lookup(SymtabSection);
1276 assert(sh_link && ".symtab not found");
1277
1278 // Remove ".rel" and ".rela" prefixes.
1279 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1280 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
1281
1282 InfoSection = Asm.getContext().getELFSection(SectionName,
1283 ELF::SHT_PROGBITS, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001284 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001285 sh_info = SectionIndexMap.lookup(InfoSection);
1286 break;
1287 }
1288
1289 case ELF::SHT_SYMTAB:
1290 case ELF::SHT_DYNSYM:
1291 sh_link = StringTableIndex;
1292 sh_info = LastLocalSymbolIndex;
1293 break;
1294
1295 case ELF::SHT_SYMTAB_SHNDX:
1296 sh_link = SymbolTableIndex;
1297 break;
1298
1299 case ELF::SHT_PROGBITS:
1300 case ELF::SHT_STRTAB:
1301 case ELF::SHT_NOBITS:
Rafael Espindola98976612010-12-26 21:30:59 +00001302 case ELF::SHT_NOTE:
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001303 case ELF::SHT_NULL:
1304 case ELF::SHT_ARM_ATTRIBUTES:
Jason W Kim86a97f22011-01-12 00:19:25 +00001305 case ELF::SHT_INIT_ARRAY:
1306 case ELF::SHT_FINI_ARRAY:
1307 case ELF::SHT_PREINIT_ARRAY:
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +00001308 case ELF::SHT_X86_64_UNWIND:
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001309 // Nothing to do.
1310 break;
1311
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001312 case ELF::SHT_GROUP: {
1313 sh_link = SymbolTableIndex;
1314 sh_info = GroupSymbolIndex;
1315 break;
1316 }
1317
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001318 default:
1319 assert(0 && "FIXME: sh_type value not supported!");
1320 break;
1321 }
1322
1323 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1324 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1325 Alignment, Section.getEntrySize());
1326}
1327
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001328static bool IsELFMetaDataSection(const MCSectionData &SD) {
Rafael Espindolaf8803fe2010-12-06 03:48:09 +00001329 return SD.getOrdinal() == ~UINT32_C(0) &&
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001330 !SD.getSection().isVirtualSection();
1331}
1332
1333static uint64_t DataSectionSize(const MCSectionData &SD) {
1334 uint64_t Ret = 0;
1335 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1336 ++i) {
1337 const MCFragment &F = *i;
1338 assert(F.getKind() == MCFragment::FT_Data);
1339 Ret += cast<MCDataFragment>(F).getContents().size();
1340 }
1341 return Ret;
1342}
1343
1344static uint64_t GetSectionFileSize(const MCAsmLayout &Layout,
1345 const MCSectionData &SD) {
1346 if (IsELFMetaDataSection(SD))
1347 return DataSectionSize(SD);
1348 return Layout.getSectionFileSize(&SD);
1349}
1350
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001351static uint64_t GetSectionAddressSize(const MCAsmLayout &Layout,
1352 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001353 if (IsELFMetaDataSection(SD))
1354 return DataSectionSize(SD);
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001355 return Layout.getSectionAddressSize(&SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001356}
1357
1358static void WriteDataSectionData(ELFObjectWriter *W, const MCSectionData &SD) {
1359 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1360 ++i) {
1361 const MCFragment &F = *i;
1362 assert(F.getKind() == MCFragment::FT_Data);
1363 W->WriteBytes(cast<MCDataFragment>(F).getContents().str());
1364 }
1365}
1366
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001367void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1368 const MCAsmLayout &Layout) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001369 GroupMapTy GroupMap;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001370 RevGroupMapTy RevGroupMap;
Rafael Espindola96aa78c2011-01-23 17:55:27 +00001371 CreateIndexedSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1372 RevGroupMap);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001373
Rafael Espindolabab2a802010-11-10 21:51:05 +00001374 SectionIndexMapTy SectionIndexMap;
1375
1376 ComputeIndexMap(Asm, SectionIndexMap);
1377
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001378 // Compute symbol table information.
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001379 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap);
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001380
Matt Fleming3565a062010-08-16 18:57:57 +00001381 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001382 const_cast<MCAsmLayout&>(Layout),
1383 SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +00001384
Rafael Espindola1d739a02010-11-10 22:34:07 +00001385 // Update to include the metadata sections.
1386 ComputeIndexMap(Asm, SectionIndexMap);
1387
Matt Fleming3565a062010-08-16 18:57:57 +00001388 // Add 1 for the null section.
1389 unsigned NumSections = Asm.size() + 1;
Rafael Espindolabff66a82010-12-18 03:27:34 +00001390 uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
1391 uint64_t HeaderSize = is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
1392 sizeof(ELF::Elf32_Ehdr);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001393 uint64_t FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001394
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001395 std::vector<const MCSectionELF*> Sections;
1396 Sections.resize(NumSections);
1397
1398 for (SectionIndexMapTy::const_iterator i=
1399 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1400 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
1401 Sections[p.second] = p.first;
1402 }
1403
1404 for (unsigned i = 1; i < NumSections; ++i) {
1405 const MCSectionELF &Section = *Sections[i];
1406 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001407
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001408 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1409
Matt Fleming3565a062010-08-16 18:57:57 +00001410 // Get the size of the section in the output file (including padding).
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001411 FileOff += GetSectionFileSize(Layout, SD);
Matt Fleming3565a062010-08-16 18:57:57 +00001412 }
1413
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001414 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1415
Matt Fleming3565a062010-08-16 18:57:57 +00001416 // Write out the ELF header ...
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001417 WriteHeader(FileOff - HeaderSize, NumSections);
1418
1419 FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001420
1421 // ... then all of the sections ...
1422 DenseMap<const MCSection*, uint64_t> SectionOffsetMap;
1423
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001424 for (unsigned i = 1; i < NumSections; ++i) {
1425 const MCSectionELF &Section = *Sections[i];
1426 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001427
1428 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1429 WriteZeros(Padding);
1430 FileOff += Padding;
1431
Matt Fleming3565a062010-08-16 18:57:57 +00001432 // Remember the offset into the file for this section.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001433 SectionOffsetMap[&Section] = FileOff;
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001434
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001435 FileOff += GetSectionFileSize(Layout, SD);
Matt Fleming3565a062010-08-16 18:57:57 +00001436
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001437 if (IsELFMetaDataSection(SD))
1438 WriteDataSectionData(this, SD);
1439 else
Daniel Dunbar5d2477c2010-12-17 02:45:59 +00001440 Asm.WriteSectionData(&SD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +00001441 }
1442
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001443 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1444 WriteZeros(Padding);
1445 FileOff += Padding;
1446
Matt Fleming3565a062010-08-16 18:57:57 +00001447 // ... and then the section header table.
1448 // Should we align the section header table?
1449 //
1450 // Null section first.
Rafael Espindola7be2c332010-10-31 00:16:26 +00001451 uint64_t FirstSectionSize =
1452 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1453 uint32_t FirstSectionLink =
1454 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1455 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming3565a062010-08-16 18:57:57 +00001456
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001457 for (unsigned i = 1; i < NumSections; ++i) {
1458 const MCSectionELF &Section = *Sections[i];
1459 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1460 uint32_t GroupSymbolIndex;
1461 if (Section.getType() != ELF::SHT_GROUP)
1462 GroupSymbolIndex = 0;
1463 else
1464 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm, GroupMap[&Section]);
Matt Fleming3565a062010-08-16 18:57:57 +00001465
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001466 uint64_t Size = GetSectionAddressSize(Layout, SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001467
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001468 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001469 SectionOffsetMap[&Section], Size,
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001470 SD.getAlignment(), Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001471 }
1472}
1473
Rafael Espindola6024c972010-12-17 17:45:22 +00001474MCObjectWriter *llvm::createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1475 raw_ostream &OS,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001476 bool IsLittleEndian) {
1477 switch (MOTW->getEMachine()) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001478 case ELF::EM_386:
1479 case ELF::EM_X86_64:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001480 return new X86ELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001481 case ELF::EM_ARM:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001482 return new ARMELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Wesley Peck4b047132010-11-21 22:06:28 +00001483 case ELF::EM_MBLAZE:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001484 return new MBlazeELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Benjamin Kramer32858772010-11-15 19:20:50 +00001485 default: llvm_unreachable("Unsupported architecture"); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001486 }
1487}
1488
1489
1490/// START OF SUBCLASSES for ELFObjectWriter
1491//===- ARMELFObjectWriter -------------------------------------------===//
1492
Rafael Espindola31f35782010-12-17 18:01:31 +00001493ARMELFObjectWriter::ARMELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001494 raw_ostream &_OS,
1495 bool IsLittleEndian)
1496 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
Jason W Kimd3443e92010-11-15 16:18:39 +00001497{}
1498
1499ARMELFObjectWriter::~ARMELFObjectWriter()
1500{}
1501
Jason W Kim2d7a53a2011-02-04 21:41:11 +00001502// FIXME: get the real EABI Version from the Triple.
1503void ARMELFObjectWriter::WriteEFlags() {
1504 Write32(ELF::EF_ARM_EABIMASK & DefaultEABIVersion);
1505}
1506
Jason W Kim953a2a32011-02-07 01:11:15 +00001507// In ARM, _MergedGlobals and other most symbols get emitted directly.
1508// I.e. not as an offset to a section symbol.
1509// This code is a first-cut approximation of what ARM/gcc does.
1510
1511const MCSymbol *ARMELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
1512 const MCValue &Target,
1513 const MCFragment &F,
1514 bool IsBSS) const {
1515 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
1516 bool EmitThisSym = false;
1517
1518 if (IsBSS) {
1519 EmitThisSym = StringSwitch<bool>(Symbol.getName())
1520 .Case("_MergedGlobals", true)
1521 .Default(false);
1522 } else {
1523 EmitThisSym = StringSwitch<bool>(Symbol.getName())
1524 .Case("_MergedGlobals", true)
1525 .StartsWith(".L.str", true)
1526 .Default(false);
1527 }
1528 if (EmitThisSym)
1529 return &Symbol;
1530 if (! Symbol.isTemporary())
1531 return &Symbol;
1532 return NULL;
1533}
1534
Jason W Kim85fed5e2010-12-01 02:40:06 +00001535unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target,
1536 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001537 bool IsPCRel,
1538 bool IsRelocWithSymbol,
1539 int64_t Addend) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001540 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1541 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
1542
Jason W Kima0871e72010-12-08 23:14:44 +00001543 unsigned Type = 0;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001544 if (IsPCRel) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001545 switch ((unsigned)Fixup.getKind()) {
1546 default: assert(0 && "Unimplemented");
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001547 case FK_Data_4:
1548 switch (Modifier) {
1549 default: llvm_unreachable("Unsupported Modifier");
1550 case MCSymbolRefExpr::VK_None:
Jason W Kim1d866172011-01-13 00:07:51 +00001551 Type = ELF::R_ARM_BASE_PREL;
1552 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001553 case MCSymbolRefExpr::VK_ARM_TLSGD:
Jason W Kim1d866172011-01-13 00:07:51 +00001554 assert(0 && "unimplemented");
1555 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001556 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
1557 Type = ELF::R_ARM_TLS_IE32;
Jason W Kim1d866172011-01-13 00:07:51 +00001558 break;
1559 }
1560 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001561 case ARM::fixup_arm_uncondbranch:
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001562 switch (Modifier) {
1563 case MCSymbolRefExpr::VK_ARM_PLT:
Jason W Kim1d866172011-01-13 00:07:51 +00001564 Type = ELF::R_ARM_PLT32;
1565 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001566 default:
Jason W Kim1d866172011-01-13 00:07:51 +00001567 Type = ELF::R_ARM_CALL;
1568 break;
1569 }
1570 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001571 case ARM::fixup_arm_condbranch:
1572 Type = ELF::R_ARM_JUMP24;
1573 break;
Jason W Kim86a97f22011-01-12 00:19:25 +00001574 case ARM::fixup_arm_movt_hi16:
1575 case ARM::fixup_arm_movt_hi16_pcrel:
Jason W Kim1d866172011-01-13 00:07:51 +00001576 Type = ELF::R_ARM_MOVT_PREL;
1577 break;
Jason W Kim86a97f22011-01-12 00:19:25 +00001578 case ARM::fixup_arm_movw_lo16:
1579 case ARM::fixup_arm_movw_lo16_pcrel:
Jason W Kim1d866172011-01-13 00:07:51 +00001580 Type = ELF::R_ARM_MOVW_PREL_NC;
1581 break;
Evan Chengf3eb3bb2011-01-14 02:38:49 +00001582 case ARM::fixup_t2_movt_hi16:
1583 case ARM::fixup_t2_movt_hi16_pcrel:
1584 Type = ELF::R_ARM_THM_MOVT_PREL;
1585 break;
1586 case ARM::fixup_t2_movw_lo16:
1587 case ARM::fixup_t2_movw_lo16_pcrel:
1588 Type = ELF::R_ARM_THM_MOVW_PREL_NC;
1589 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001590 }
1591 } else {
1592 switch ((unsigned)Fixup.getKind()) {
1593 default: llvm_unreachable("invalid fixup kind!");
Jason W Kima0871e72010-12-08 23:14:44 +00001594 case FK_Data_4:
1595 switch (Modifier) {
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001596 default: llvm_unreachable("Unsupported Modifier"); break;
1597 case MCSymbolRefExpr::VK_ARM_GOT:
Jason W Kim1d866172011-01-13 00:07:51 +00001598 Type = ELF::R_ARM_GOT_BREL;
1599 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001600 case MCSymbolRefExpr::VK_ARM_TLSGD:
Jason W Kim1d866172011-01-13 00:07:51 +00001601 Type = ELF::R_ARM_TLS_GD32;
1602 break;
Jason W Kimf13743b2010-12-16 03:12:17 +00001603 case MCSymbolRefExpr::VK_ARM_TPOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001604 Type = ELF::R_ARM_TLS_LE32;
1605 break;
Jason W Kima0871e72010-12-08 23:14:44 +00001606 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001607 Type = ELF::R_ARM_TLS_IE32;
1608 break;
Jason W Kimf13743b2010-12-16 03:12:17 +00001609 case MCSymbolRefExpr::VK_None:
Jason W Kim1d866172011-01-13 00:07:51 +00001610 Type = ELF::R_ARM_ABS32;
1611 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001612 case MCSymbolRefExpr::VK_ARM_GOTOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001613 Type = ELF::R_ARM_GOTOFF32;
1614 break;
1615 }
1616 break;
Jim Grosbachdff84b02010-12-02 00:28:45 +00001617 case ARM::fixup_arm_ldst_pcrel_12:
Owen Anderson9d63d902010-12-01 19:18:46 +00001618 case ARM::fixup_arm_pcrel_10:
Jim Grosbachdff84b02010-12-02 00:28:45 +00001619 case ARM::fixup_arm_adr_pcrel_12:
Jim Grosbach662a8162010-12-06 23:57:07 +00001620 case ARM::fixup_arm_thumb_bl:
Jim Grosbachb492a7c2010-12-09 19:50:12 +00001621 case ARM::fixup_arm_thumb_cb:
Bill Wendlingb8958b02010-12-08 01:57:09 +00001622 case ARM::fixup_arm_thumb_cp:
Jim Grosbache2467172010-12-10 18:21:33 +00001623 case ARM::fixup_arm_thumb_br:
Jason W Kim1d866172011-01-13 00:07:51 +00001624 assert(0 && "Unimplemented");
1625 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001626 case ARM::fixup_arm_uncondbranch:
Jason W Kim1d866172011-01-13 00:07:51 +00001627 Type = ELF::R_ARM_CALL;
1628 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001629 case ARM::fixup_arm_condbranch:
1630 Type = ELF::R_ARM_JUMP24;
1631 break;
Jason W Kim1d866172011-01-13 00:07:51 +00001632 case ARM::fixup_arm_movt_hi16:
1633 Type = ELF::R_ARM_MOVT_ABS;
1634 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001635 case ARM::fixup_arm_movw_lo16:
Jason W Kim1d866172011-01-13 00:07:51 +00001636 Type = ELF::R_ARM_MOVW_ABS_NC;
1637 break;
Evan Chengf3eb3bb2011-01-14 02:38:49 +00001638 case ARM::fixup_t2_movt_hi16:
1639 Type = ELF::R_ARM_THM_MOVT_ABS;
1640 break;
1641 case ARM::fixup_t2_movw_lo16:
1642 Type = ELF::R_ARM_THM_MOVW_ABS_NC;
1643 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001644 }
1645 }
1646
1647 if (RelocNeedsGOT(Modifier))
1648 NeedsGOT = true;
Jason W Kim953a2a32011-02-07 01:11:15 +00001649
Jason W Kima0871e72010-12-08 23:14:44 +00001650 return Type;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001651}
1652
Wesley Peck4b047132010-11-21 22:06:28 +00001653//===- MBlazeELFObjectWriter -------------------------------------------===//
Jason W Kimd3443e92010-11-15 16:18:39 +00001654
Rafael Espindola31f35782010-12-17 18:01:31 +00001655MBlazeELFObjectWriter::MBlazeELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001656 raw_ostream &_OS,
1657 bool IsLittleEndian)
1658 : ELFObjectWriter(MOTW, _OS, IsLittleEndian) {
Wesley Peck4b047132010-11-21 22:06:28 +00001659}
1660
1661MBlazeELFObjectWriter::~MBlazeELFObjectWriter() {
1662}
1663
Jason W Kim56a39902010-12-06 21:57:34 +00001664unsigned MBlazeELFObjectWriter::GetRelocType(const MCValue &Target,
Wesley Peck4b047132010-11-21 22:06:28 +00001665 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001666 bool IsPCRel,
1667 bool IsRelocWithSymbol,
1668 int64_t Addend) {
Wesley Peck4b047132010-11-21 22:06:28 +00001669 // determine the type of the relocation
1670 unsigned Type;
1671 if (IsPCRel) {
1672 switch ((unsigned)Fixup.getKind()) {
1673 default:
1674 llvm_unreachable("Unimplemented");
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001675 case FK_PCRel_4:
Wesley Peck4b047132010-11-21 22:06:28 +00001676 Type = ELF::R_MICROBLAZE_64_PCREL;
1677 break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001678 case FK_PCRel_2:
Wesley Peck4b047132010-11-21 22:06:28 +00001679 Type = ELF::R_MICROBLAZE_32_PCREL;
1680 break;
1681 }
1682 } else {
1683 switch ((unsigned)Fixup.getKind()) {
1684 default: llvm_unreachable("invalid fixup kind!");
1685 case FK_Data_4:
Jason W Kim56a39902010-12-06 21:57:34 +00001686 Type = ((IsRelocWithSymbol || Addend !=0)
1687 ? ELF::R_MICROBLAZE_32
1688 : ELF::R_MICROBLAZE_64);
Wesley Peck4b047132010-11-21 22:06:28 +00001689 break;
1690 case FK_Data_2:
1691 Type = ELF::R_MICROBLAZE_32;
1692 break;
1693 }
1694 }
Jason W Kim56a39902010-12-06 21:57:34 +00001695 return Type;
Wesley Peck4b047132010-11-21 22:06:28 +00001696}
Jason W Kimd3443e92010-11-15 16:18:39 +00001697
1698//===- X86ELFObjectWriter -------------------------------------------===//
1699
1700
Rafael Espindola31f35782010-12-17 18:01:31 +00001701X86ELFObjectWriter::X86ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001702 raw_ostream &_OS,
1703 bool IsLittleEndian)
1704 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
Jason W Kimd3443e92010-11-15 16:18:39 +00001705{}
1706
1707X86ELFObjectWriter::~X86ELFObjectWriter()
1708{}
1709
Jason W Kim56a39902010-12-06 21:57:34 +00001710unsigned X86ELFObjectWriter::GetRelocType(const MCValue &Target,
1711 const MCFixup &Fixup,
1712 bool IsPCRel,
1713 bool IsRelocWithSymbol,
1714 int64_t Addend) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001715 // determine the type of the relocation
1716
Rafael Espindola12203cc2010-11-21 00:48:25 +00001717 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1718 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
Jason W Kimd3443e92010-11-15 16:18:39 +00001719 unsigned Type;
Rafael Espindolabff66a82010-12-18 03:27:34 +00001720 if (is64Bit()) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001721 if (IsPCRel) {
Rafael Espindola3a83c402010-12-27 00:36:05 +00001722 switch ((unsigned)Fixup.getKind()) {
1723 default: llvm_unreachable("invalid fixup kind!");
1724 case FK_PCRel_8:
1725 assert(Modifier == MCSymbolRefExpr::VK_None);
1726 Type = ELF::R_X86_64_PC64;
Jason W Kimd3443e92010-11-15 16:18:39 +00001727 break;
Rafael Espindola7a549972011-01-01 19:05:35 +00001728 case X86::reloc_signed_4byte:
Rafael Espindolac3a561c2010-12-27 02:03:24 +00001729 case X86::reloc_riprel_4byte_movq_load:
Rafael Espindola3a83c402010-12-27 00:36:05 +00001730 case FK_Data_4: // FIXME?
1731 case X86::reloc_riprel_4byte:
1732 case FK_PCRel_4:
1733 switch (Modifier) {
1734 default:
1735 llvm_unreachable("Unimplemented");
1736 case MCSymbolRefExpr::VK_None:
1737 Type = ELF::R_X86_64_PC32;
1738 break;
1739 case MCSymbolRefExpr::VK_PLT:
1740 Type = ELF::R_X86_64_PLT32;
1741 break;
1742 case MCSymbolRefExpr::VK_GOTPCREL:
1743 Type = ELF::R_X86_64_GOTPCREL;
1744 break;
1745 case MCSymbolRefExpr::VK_GOTTPOFF:
1746 Type = ELF::R_X86_64_GOTTPOFF;
Jason W Kimd3443e92010-11-15 16:18:39 +00001747 break;
Rafael Espindola3a83c402010-12-27 00:36:05 +00001748 case MCSymbolRefExpr::VK_TLSGD:
1749 Type = ELF::R_X86_64_TLSGD;
1750 break;
1751 case MCSymbolRefExpr::VK_TLSLD:
1752 Type = ELF::R_X86_64_TLSLD;
1753 break;
1754 }
Jason W Kimd3443e92010-11-15 16:18:39 +00001755 break;
Rafael Espindola3a83c402010-12-27 00:36:05 +00001756 case FK_PCRel_2:
1757 assert(Modifier == MCSymbolRefExpr::VK_None);
1758 Type = ELF::R_X86_64_PC16;
Jason W Kimd3443e92010-11-15 16:18:39 +00001759 break;
1760 }
1761 } else {
1762 switch ((unsigned)Fixup.getKind()) {
1763 default: llvm_unreachable("invalid fixup kind!");
1764 case FK_Data_8: Type = ELF::R_X86_64_64; break;
1765 case X86::reloc_signed_4byte:
Jason W Kimd3443e92010-11-15 16:18:39 +00001766 assert(isInt<32>(Target.getConstant()));
1767 switch (Modifier) {
1768 default:
1769 llvm_unreachable("Unimplemented");
1770 case MCSymbolRefExpr::VK_None:
1771 Type = ELF::R_X86_64_32S;
1772 break;
1773 case MCSymbolRefExpr::VK_GOT:
1774 Type = ELF::R_X86_64_GOT32;
1775 break;
1776 case MCSymbolRefExpr::VK_GOTPCREL:
1777 Type = ELF::R_X86_64_GOTPCREL;
1778 break;
1779 case MCSymbolRefExpr::VK_TPOFF:
1780 Type = ELF::R_X86_64_TPOFF32;
1781 break;
1782 case MCSymbolRefExpr::VK_DTPOFF:
1783 Type = ELF::R_X86_64_DTPOFF32;
1784 break;
1785 }
1786 break;
1787 case FK_Data_4:
1788 Type = ELF::R_X86_64_32;
1789 break;
1790 case FK_Data_2: Type = ELF::R_X86_64_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001791 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001792 case FK_Data_1: Type = ELF::R_X86_64_8; break;
1793 }
1794 }
1795 } else {
1796 if (IsPCRel) {
1797 switch (Modifier) {
1798 default:
1799 llvm_unreachable("Unimplemented");
1800 case MCSymbolRefExpr::VK_None:
1801 Type = ELF::R_386_PC32;
1802 break;
1803 case MCSymbolRefExpr::VK_PLT:
1804 Type = ELF::R_386_PLT32;
1805 break;
1806 }
1807 } else {
1808 switch ((unsigned)Fixup.getKind()) {
1809 default: llvm_unreachable("invalid fixup kind!");
1810
1811 case X86::reloc_global_offset_table:
1812 Type = ELF::R_386_GOTPC;
1813 break;
1814
1815 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
1816 // instead?
1817 case X86::reloc_signed_4byte:
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001818 case FK_PCRel_4:
Jason W Kimd3443e92010-11-15 16:18:39 +00001819 case FK_Data_4:
1820 switch (Modifier) {
1821 default:
1822 llvm_unreachable("Unimplemented");
1823 case MCSymbolRefExpr::VK_None:
1824 Type = ELF::R_386_32;
1825 break;
1826 case MCSymbolRefExpr::VK_GOT:
1827 Type = ELF::R_386_GOT32;
1828 break;
1829 case MCSymbolRefExpr::VK_GOTOFF:
1830 Type = ELF::R_386_GOTOFF;
1831 break;
1832 case MCSymbolRefExpr::VK_TLSGD:
1833 Type = ELF::R_386_TLS_GD;
1834 break;
1835 case MCSymbolRefExpr::VK_TPOFF:
1836 Type = ELF::R_386_TLS_LE_32;
1837 break;
1838 case MCSymbolRefExpr::VK_INDNTPOFF:
1839 Type = ELF::R_386_TLS_IE;
1840 break;
1841 case MCSymbolRefExpr::VK_NTPOFF:
1842 Type = ELF::R_386_TLS_LE;
1843 break;
1844 case MCSymbolRefExpr::VK_GOTNTPOFF:
1845 Type = ELF::R_386_TLS_GOTIE;
1846 break;
1847 case MCSymbolRefExpr::VK_TLSLDM:
1848 Type = ELF::R_386_TLS_LDM;
1849 break;
1850 case MCSymbolRefExpr::VK_DTPOFF:
1851 Type = ELF::R_386_TLS_LDO_32;
1852 break;
1853 }
1854 break;
1855 case FK_Data_2: Type = ELF::R_386_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001856 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001857 case FK_Data_1: Type = ELF::R_386_8; break;
1858 }
1859 }
1860 }
1861
1862 if (RelocNeedsGOT(Modifier))
1863 NeedsGOT = true;
1864
Jason W Kim56a39902010-12-06 21:57:34 +00001865 return Type;
Matt Fleming3565a062010-08-16 18:57:57 +00001866}