blob: 77017fb4d936133057c102ba356f1aea5ae1cc28 [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
Jan Sjödin2ddfd952011-02-28 21:45:04 +000014#include "MCELF.h"
Rafael Espindola31f35782010-12-17 18:01:31 +000015#include "llvm/ADT/OwningPtr.h"
Rafael Espindola8f413fa2010-10-05 15:11:03 +000016#include "llvm/ADT/SmallPtrSet.h"
Matt Fleming3565a062010-08-16 18:57:57 +000017#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/StringMap.h"
19#include "llvm/ADT/Twine.h"
20#include "llvm/MC/MCAssembler.h"
21#include "llvm/MC/MCAsmLayout.h"
22#include "llvm/MC/MCContext.h"
23#include "llvm/MC/MCELFSymbolFlags.h"
24#include "llvm/MC/MCExpr.h"
Rafael Espindola285b3e52010-12-17 16:59:53 +000025#include "llvm/MC/MCELFObjectWriter.h"
Matt Fleming3565a062010-08-16 18:57:57 +000026#include "llvm/MC/MCObjectWriter.h"
27#include "llvm/MC/MCSectionELF.h"
28#include "llvm/MC/MCSymbol.h"
29#include "llvm/MC/MCValue.h"
30#include "llvm/Support/Debug.h"
31#include "llvm/Support/ErrorHandling.h"
32#include "llvm/Support/ELF.h"
33#include "llvm/Target/TargetAsmBackend.h"
Jason W Kim953a2a32011-02-07 01:11:15 +000034#include "llvm/ADT/StringSwitch.h"
Matt Fleming3565a062010-08-16 18:57:57 +000035
36#include "../Target/X86/X86FixupKinds.h"
Jason W Kim4a511f02010-11-22 18:41:13 +000037#include "../Target/ARM/ARMFixupKinds.h"
Matt Fleming3565a062010-08-16 18:57:57 +000038
39#include <vector>
40using namespace llvm;
41
42namespace {
Daniel Dunbar115a3dd2010-11-13 07:33:40 +000043 class ELFObjectWriter : public MCObjectWriter {
Jason W Kimd3443e92010-11-15 16:18:39 +000044 protected:
Jan Sjödin2ddfd952011-02-28 21:45:04 +000045
46 static bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind);
47 static bool RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant);
48 static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout);
49 static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
50 bool Used, bool Renamed);
51 static bool isLocal(const MCSymbolData &Data, bool isSignature,
52 bool isUsedInReloc);
53 static bool IsELFMetaDataSection(const MCSectionData &SD);
54 static uint64_t DataSectionSize(const MCSectionData &SD);
55 static uint64_t GetSectionFileSize(const MCAsmLayout &Layout,
56 const MCSectionData &SD);
57 static uint64_t GetSectionAddressSize(const MCAsmLayout &Layout,
58 const MCSectionData &SD);
59 static void WriteDataSectionData(ELFObjectWriter *W,
60 const MCSectionData &SD);
61
Chris Lattnerb188a372010-08-28 03:21:03 +000062 /*static bool isFixupKindX86RIPRel(unsigned Kind) {
Matt Fleming3565a062010-08-16 18:57:57 +000063 return Kind == X86::reloc_riprel_4byte ||
64 Kind == X86::reloc_riprel_4byte_movq_load;
Chris Lattnerb188a372010-08-28 03:21:03 +000065 }*/
Matt Fleming3565a062010-08-16 18:57:57 +000066
Jan Sjödin2ddfd952011-02-28 21:45:04 +000067 /// ELFSymbolData - Helper struct for containing some precomputed
68 /// information on symbols.
Matt Fleming3565a062010-08-16 18:57:57 +000069 struct ELFSymbolData {
70 MCSymbolData *SymbolData;
71 uint64_t StringIndex;
72 uint32_t SectionIndex;
73
74 // Support lexicographic sorting.
75 bool operator<(const ELFSymbolData &RHS) const {
Jan Sjödin2ddfd952011-02-28 21:45:04 +000076 if (MCELF::GetType(*SymbolData) == ELF::STT_FILE)
Rafael Espindolaad49cf52010-09-18 15:03:21 +000077 return true;
Jan Sjödin2ddfd952011-02-28 21:45:04 +000078 if (MCELF::GetType(*RHS.SymbolData) == ELF::STT_FILE)
Rafael Espindolaad49cf52010-09-18 15:03:21 +000079 return false;
Benjamin Kramer36c6dc22010-08-23 21:23:52 +000080 return SymbolData->getSymbol().getName() <
81 RHS.SymbolData->getSymbol().getName();
Matt Fleming3565a062010-08-16 18:57:57 +000082 }
83 };
84
85 /// @name Relocation Data
86 /// @{
87
88 struct ELFRelocationEntry {
89 // Make these big enough for both 32-bit and 64-bit
90 uint64_t r_offset;
Rafael Espindola8f413fa2010-10-05 15:11:03 +000091 int Index;
92 unsigned Type;
93 const MCSymbol *Symbol;
Matt Fleming3565a062010-08-16 18:57:57 +000094 uint64_t r_addend;
Jason W Kim858e7502010-11-22 18:42:07 +000095
Jason W Kim4a511f02010-11-22 18:41:13 +000096 ELFRelocationEntry()
97 : r_offset(0), Index(0), Type(0), Symbol(0), r_addend(0) {}
98
Jason W Kim10907422010-11-22 22:05:16 +000099 ELFRelocationEntry(uint64_t RelocOffset, int Idx,
100 unsigned RelType, const MCSymbol *Sym,
Jason W Kim4a511f02010-11-22 18:41:13 +0000101 uint64_t Addend)
Jason W Kim10907422010-11-22 22:05:16 +0000102 : r_offset(RelocOffset), Index(Idx), Type(RelType),
103 Symbol(Sym), r_addend(Addend) {}
Matt Fleming3565a062010-08-16 18:57:57 +0000104
105 // Support lexicographic sorting.
106 bool operator<(const ELFRelocationEntry &RE) const {
107 return RE.r_offset < r_offset;
108 }
109 };
110
Rafael Espindola31f35782010-12-17 18:01:31 +0000111 /// The target specific ELF writer instance.
112 llvm::OwningPtr<MCELFObjectTargetWriter> TargetObjectWriter;
113
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000114 SmallPtrSet<const MCSymbol *, 16> UsedInReloc;
Rafael Espindola484291c2010-11-01 14:28:48 +0000115 SmallPtrSet<const MCSymbol *, 16> WeakrefUsedInReloc;
Rafael Espindola88182132010-10-27 15:18:17 +0000116 DenseMap<const MCSymbol *, const MCSymbol *> Renames;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000117
Matt Fleming3565a062010-08-16 18:57:57 +0000118 llvm::DenseMap<const MCSectionData*,
119 std::vector<ELFRelocationEntry> > Relocations;
120 DenseMap<const MCSection*, uint64_t> SectionStringTableIndex;
121
122 /// @}
123 /// @name Symbol Table Data
124 /// @{
125
126 SmallString<256> StringTable;
127 std::vector<ELFSymbolData> LocalSymbolData;
128 std::vector<ELFSymbolData> ExternalSymbolData;
129 std::vector<ELFSymbolData> UndefinedSymbolData;
130
131 /// @}
132
Rafael Espindola5c77c162010-10-05 15:48:37 +0000133 bool NeedsGOT;
134
Rafael Espindola7be2c332010-10-31 00:16:26 +0000135 bool NeedsSymtabShndx;
136
Matt Fleming3565a062010-08-16 18:57:57 +0000137 // This holds the symbol table index of the last local symbol.
138 unsigned LastLocalSymbolIndex;
139 // This holds the .strtab section index.
140 unsigned StringTableIndex;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000141 // This holds the .symtab section index.
142 unsigned SymbolTableIndex;
Matt Fleming3565a062010-08-16 18:57:57 +0000143
144 unsigned ShstrtabIndex;
145
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000146
147 const MCSymbol *SymbolToReloc(const MCAssembler &Asm,
148 const MCValue &Target,
149 const MCFragment &F) const;
150
Jason W Kim953a2a32011-02-07 01:11:15 +0000151 // For arch-specific emission of explicit reloc symbol
152 virtual const MCSymbol *ExplicitRelSym(const MCAssembler &Asm,
153 const MCValue &Target,
154 const MCFragment &F,
155 bool IsBSS) const {
156 return NULL;
157 }
158
Rafael Espindolabff66a82010-12-18 03:27:34 +0000159 bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
160 bool hasRelocationAddend() const {
161 return TargetObjectWriter->hasRelocationAddend();
162 }
163
Matt Fleming3565a062010-08-16 18:57:57 +0000164 public:
Rafael Espindola31f35782010-12-17 18:01:31 +0000165 ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +0000166 raw_ostream &_OS, bool IsLittleEndian)
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000167 : MCObjectWriter(_OS, IsLittleEndian),
Rafael Espindola31f35782010-12-17 18:01:31 +0000168 TargetObjectWriter(MOTW),
Rafael Espindolabff66a82010-12-18 03:27:34 +0000169 NeedsGOT(false), NeedsSymtabShndx(false){
Matt Fleming3565a062010-08-16 18:57:57 +0000170 }
Jason W Kimd3443e92010-11-15 16:18:39 +0000171
172 virtual ~ELFObjectWriter();
173
Matt Fleming3565a062010-08-16 18:57:57 +0000174 void WriteWord(uint64_t W) {
Rafael Espindolabff66a82010-12-18 03:27:34 +0000175 if (is64Bit())
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000176 Write64(W);
Chris Lattnerb188a372010-08-28 03:21:03 +0000177 else
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000178 Write32(W);
Matt Fleming3565a062010-08-16 18:57:57 +0000179 }
180
Matt Fleming3565a062010-08-16 18:57:57 +0000181 void StringLE16(char *buf, uint16_t Value) {
182 buf[0] = char(Value >> 0);
183 buf[1] = char(Value >> 8);
184 }
185
186 void StringLE32(char *buf, uint32_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000187 StringLE16(buf, uint16_t(Value >> 0));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000188 StringLE16(buf + 2, uint16_t(Value >> 16));
Matt Fleming3565a062010-08-16 18:57:57 +0000189 }
190
191 void StringLE64(char *buf, uint64_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000192 StringLE32(buf, uint32_t(Value >> 0));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000193 StringLE32(buf + 4, uint32_t(Value >> 32));
Matt Fleming3565a062010-08-16 18:57:57 +0000194 }
195
196 void StringBE16(char *buf ,uint16_t Value) {
197 buf[0] = char(Value >> 8);
198 buf[1] = char(Value >> 0);
199 }
200
201 void StringBE32(char *buf, uint32_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000202 StringBE16(buf, uint16_t(Value >> 16));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000203 StringBE16(buf + 2, uint16_t(Value >> 0));
Matt Fleming3565a062010-08-16 18:57:57 +0000204 }
205
206 void StringBE64(char *buf, uint64_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000207 StringBE32(buf, uint32_t(Value >> 32));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000208 StringBE32(buf + 4, uint32_t(Value >> 0));
Matt Fleming3565a062010-08-16 18:57:57 +0000209 }
210
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000211 void String8(MCDataFragment &F, uint8_t Value) {
212 char buf[1];
213 buf[0] = Value;
214 F.getContents() += StringRef(buf, 1);
215 }
216
217 void String16(MCDataFragment &F, uint16_t Value) {
218 char buf[2];
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000219 if (isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000220 StringLE16(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000221 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000222 StringBE16(buf, Value);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000223 F.getContents() += StringRef(buf, 2);
Matt Fleming3565a062010-08-16 18:57:57 +0000224 }
225
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000226 void String32(MCDataFragment &F, uint32_t Value) {
227 char buf[4];
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000228 if (isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000229 StringLE32(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000230 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000231 StringBE32(buf, Value);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000232 F.getContents() += StringRef(buf, 4);
Matt Fleming3565a062010-08-16 18:57:57 +0000233 }
234
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000235 void String64(MCDataFragment &F, uint64_t Value) {
236 char buf[8];
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000237 if (isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000238 StringLE64(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000239 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000240 StringBE64(buf, Value);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000241 F.getContents() += StringRef(buf, 8);
Matt Fleming3565a062010-08-16 18:57:57 +0000242 }
243
Jason W Kimd3443e92010-11-15 16:18:39 +0000244 virtual void WriteHeader(uint64_t SectionDataSize, unsigned NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000245
Jason W Kim2d7a53a2011-02-04 21:41:11 +0000246 /// Default e_flags = 0
247 virtual void WriteEFlags() { Write32(0); }
248
Jason W Kimd3443e92010-11-15 16:18:39 +0000249 virtual void WriteSymbolEntry(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
Rafael Espindola7be2c332010-10-31 00:16:26 +0000250 uint64_t name, uint8_t info,
Matt Fleming3565a062010-08-16 18:57:57 +0000251 uint64_t value, uint64_t size,
Rafael Espindola7be2c332010-10-31 00:16:26 +0000252 uint8_t other, uint32_t shndx,
253 bool Reserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000254
Jason W Kimd3443e92010-11-15 16:18:39 +0000255 virtual void WriteSymbol(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
Rafael Espindola7be2c332010-10-31 00:16:26 +0000256 ELFSymbolData &MSD,
Matt Fleming3565a062010-08-16 18:57:57 +0000257 const MCAsmLayout &Layout);
258
Rafael Espindolabab2a802010-11-10 21:51:05 +0000259 typedef DenseMap<const MCSectionELF*, uint32_t> SectionIndexMapTy;
Jason W Kimd3443e92010-11-15 16:18:39 +0000260 virtual void WriteSymbolTable(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
Rafael Espindola7be2c332010-10-31 00:16:26 +0000261 const MCAssembler &Asm,
Rafael Espindola71859c62010-09-16 19:46:31 +0000262 const MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000263 const SectionIndexMapTy &SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +0000264
Jason W Kimd3443e92010-11-15 16:18:39 +0000265 virtual void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
Jason W Kim56a39902010-12-06 21:57:34 +0000266 const MCFragment *Fragment, const MCFixup &Fixup,
267 MCValue Target, uint64_t &FixedValue);
Matt Fleming3565a062010-08-16 18:57:57 +0000268
Jason W Kimd3443e92010-11-15 16:18:39 +0000269 virtual uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm,
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000270 const MCSymbol *S);
Matt Fleming3565a062010-08-16 18:57:57 +0000271
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000272 // Map from a group section to the signature symbol
273 typedef DenseMap<const MCSectionELF*, const MCSymbol*> GroupMapTy;
274 // Map from a signature symbol to the group section
275 typedef DenseMap<const MCSymbol*, const MCSectionELF*> RevGroupMapTy;
276
Matt Fleming3565a062010-08-16 18:57:57 +0000277 /// ComputeSymbolTable - Compute the symbol table data
278 ///
279 /// \param StringTable [out] - The string table data.
280 /// \param StringIndexMap [out] - Map from symbol names to offsets in the
281 /// string table.
Jason W Kimd3443e92010-11-15 16:18:39 +0000282 virtual void ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000283 const SectionIndexMapTy &SectionIndexMap,
284 RevGroupMapTy RevGroupMap);
Rafael Espindolabab2a802010-11-10 21:51:05 +0000285
Jason W Kimd3443e92010-11-15 16:18:39 +0000286 virtual void ComputeIndexMap(MCAssembler &Asm,
Rafael Espindolabab2a802010-11-10 21:51:05 +0000287 SectionIndexMapTy &SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +0000288
Jason W Kimd3443e92010-11-15 16:18:39 +0000289 virtual void WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
Matt Fleming3565a062010-08-16 18:57:57 +0000290 const MCSectionData &SD);
291
Jason W Kimd3443e92010-11-15 16:18:39 +0000292 virtual void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout) {
Matt Fleming3565a062010-08-16 18:57:57 +0000293 for (MCAssembler::const_iterator it = Asm.begin(),
294 ie = Asm.end(); it != ie; ++it) {
295 WriteRelocation(Asm, Layout, *it);
296 }
297 }
298
Jason W Kimd3443e92010-11-15 16:18:39 +0000299 virtual void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000300 const SectionIndexMapTy &SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +0000301
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000302 // Create the sections that show up in the symbol table. Currently
303 // those are the .note.GNU-stack section and the group sections.
304 virtual void CreateIndexedSections(MCAssembler &Asm, MCAsmLayout &Layout,
305 GroupMapTy &GroupMap,
306 RevGroupMapTy &RevGroupMap);
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000307
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000308 virtual void ExecutePostLayoutBinding(MCAssembler &Asm,
309 const MCAsmLayout &Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000310
Jason W Kimd3443e92010-11-15 16:18:39 +0000311 virtual void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
Matt Fleming3565a062010-08-16 18:57:57 +0000312 uint64_t Address, uint64_t Offset,
313 uint64_t Size, uint32_t Link, uint32_t Info,
314 uint64_t Alignment, uint64_t EntrySize);
315
Daniel Dunbar1f3662a2010-12-17 04:54:54 +0000316 virtual void WriteRelocationsFragment(const MCAssembler &Asm,
317 MCDataFragment *F,
318 const MCSectionData *SD);
319
Jason W Kimd3443e92010-11-15 16:18:39 +0000320 virtual void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
321 virtual void WriteSection(MCAssembler &Asm,
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000322 const SectionIndexMapTy &SectionIndexMap,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000323 uint32_t GroupSymbolIndex,
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000324 uint64_t Offset, uint64_t Size, uint64_t Alignment,
325 const MCSectionELF &Section);
Jason W Kim56a39902010-12-06 21:57:34 +0000326
327 protected:
328 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
329 bool IsPCRel, bool IsRelocWithSymbol,
330 int64_t Addend) = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000331 };
332
Jason W Kimd3443e92010-11-15 16:18:39 +0000333 //===- X86ELFObjectWriter -------------------------------------------===//
334
335 class X86ELFObjectWriter : public ELFObjectWriter {
336 public:
Rafael Espindola31f35782010-12-17 18:01:31 +0000337 X86ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +0000338 raw_ostream &_OS,
339 bool IsLittleEndian);
Wesley Peck4b047132010-11-21 22:06:28 +0000340
Jason W Kimd3443e92010-11-15 16:18:39 +0000341 virtual ~X86ELFObjectWriter();
Jason W Kim56a39902010-12-06 21:57:34 +0000342 protected:
343 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
344 bool IsPCRel, bool IsRelocWithSymbol,
345 int64_t Addend);
Jason W Kimd3443e92010-11-15 16:18:39 +0000346 };
347
348
349 //===- ARMELFObjectWriter -------------------------------------------===//
350
351 class ARMELFObjectWriter : public ELFObjectWriter {
352 public:
Jason W Kim2d7a53a2011-02-04 21:41:11 +0000353 // FIXME: MCAssembler can't yet return the Subtarget,
354 enum { DefaultEABIVersion = 0x05000000U };
355
Rafael Espindola31f35782010-12-17 18:01:31 +0000356 ARMELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +0000357 raw_ostream &_OS,
358 bool IsLittleEndian);
Wesley Peck4b047132010-11-21 22:06:28 +0000359
Jason W Kimd3443e92010-11-15 16:18:39 +0000360 virtual ~ARMELFObjectWriter();
Jason W Kim2d7a53a2011-02-04 21:41:11 +0000361
362 virtual void WriteEFlags();
Jason W Kim85fed5e2010-12-01 02:40:06 +0000363 protected:
Jason W Kim953a2a32011-02-07 01:11:15 +0000364 virtual const MCSymbol *ExplicitRelSym(const MCAssembler &Asm,
365 const MCValue &Target,
366 const MCFragment &F,
367 bool IsBSS) const;
368
Jason W Kim56a39902010-12-06 21:57:34 +0000369 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
370 bool IsPCRel, bool IsRelocWithSymbol,
371 int64_t Addend);
Jason W Kimd3443e92010-11-15 16:18:39 +0000372 };
Wesley Peck4b047132010-11-21 22:06:28 +0000373
374 //===- MBlazeELFObjectWriter -------------------------------------------===//
375
376 class MBlazeELFObjectWriter : public ELFObjectWriter {
377 public:
Rafael Espindola31f35782010-12-17 18:01:31 +0000378 MBlazeELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +0000379 raw_ostream &_OS,
380 bool IsLittleEndian);
Wesley Peck4b047132010-11-21 22:06:28 +0000381
382 virtual ~MBlazeELFObjectWriter();
Jason W Kim56a39902010-12-06 21:57:34 +0000383 protected:
384 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
385 bool IsPCRel, bool IsRelocWithSymbol,
386 int64_t Addend);
Wesley Peck4b047132010-11-21 22:06:28 +0000387 };
Matt Fleming3565a062010-08-16 18:57:57 +0000388}
389
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000390bool ELFObjectWriter::isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) {
391 const MCFixupKindInfo &FKI =
392 Asm.getBackend().getFixupKindInfo((MCFixupKind) Kind);
393
394 return FKI.Flags & MCFixupKindInfo::FKF_IsPCRel;
395}
396
397bool ELFObjectWriter::RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant) {
398 switch (Variant) {
399 default:
400 return false;
401 case MCSymbolRefExpr::VK_GOT:
402 case MCSymbolRefExpr::VK_PLT:
403 case MCSymbolRefExpr::VK_GOTPCREL:
404 case MCSymbolRefExpr::VK_TPOFF:
405 case MCSymbolRefExpr::VK_TLSGD:
406 case MCSymbolRefExpr::VK_GOTTPOFF:
407 case MCSymbolRefExpr::VK_INDNTPOFF:
408 case MCSymbolRefExpr::VK_NTPOFF:
409 case MCSymbolRefExpr::VK_GOTNTPOFF:
410 case MCSymbolRefExpr::VK_TLSLDM:
411 case MCSymbolRefExpr::VK_DTPOFF:
412 case MCSymbolRefExpr::VK_TLSLD:
413 return true;
414 }
415}
416
Jason W Kimd3443e92010-11-15 16:18:39 +0000417ELFObjectWriter::~ELFObjectWriter()
418{}
419
Matt Fleming3565a062010-08-16 18:57:57 +0000420// Emit the ELF header.
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000421void ELFObjectWriter::WriteHeader(uint64_t SectionDataSize,
422 unsigned NumberOfSections) {
Matt Fleming3565a062010-08-16 18:57:57 +0000423 // ELF Header
424 // ----------
425 //
426 // Note
427 // ----
428 // emitWord method behaves differently for ELF32 and ELF64, writing
429 // 4 bytes in the former and 8 in the latter.
430
431 Write8(0x7f); // e_ident[EI_MAG0]
432 Write8('E'); // e_ident[EI_MAG1]
433 Write8('L'); // e_ident[EI_MAG2]
434 Write8('F'); // e_ident[EI_MAG3]
435
Rafael Espindolabff66a82010-12-18 03:27:34 +0000436 Write8(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
Matt Fleming3565a062010-08-16 18:57:57 +0000437
438 // e_ident[EI_DATA]
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000439 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
Matt Fleming3565a062010-08-16 18:57:57 +0000440
441 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky5baf79e2010-09-09 17:57:50 +0000442 // e_ident[EI_OSABI]
Rafael Espindolabff66a82010-12-18 03:27:34 +0000443 switch (TargetObjectWriter->getOSType()) {
Roman Divacky5baf79e2010-09-09 17:57:50 +0000444 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
445 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
446 default: Write8(ELF::ELFOSABI_NONE); break;
447 }
Matt Fleming3565a062010-08-16 18:57:57 +0000448 Write8(0); // e_ident[EI_ABIVERSION]
449
450 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
451
452 Write16(ELF::ET_REL); // e_type
453
Rafael Espindolabff66a82010-12-18 03:27:34 +0000454 Write16(TargetObjectWriter->getEMachine()); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +0000455
456 Write32(ELF::EV_CURRENT); // e_version
457 WriteWord(0); // e_entry, no entry point in .o file
458 WriteWord(0); // e_phoff, no program header for .o
Rafael Espindolabff66a82010-12-18 03:27:34 +0000459 WriteWord(SectionDataSize + (is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
Benjamin Kramereb976772010-08-17 17:02:29 +0000460 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000461
Jason W Kim2d7a53a2011-02-04 21:41:11 +0000462 // e_flags = whatever the target wants
463 WriteEFlags();
Matt Fleming3565a062010-08-16 18:57:57 +0000464
465 // e_ehsize = ELF header size
Rafael Espindolabff66a82010-12-18 03:27:34 +0000466 Write16(is64Bit() ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
Matt Fleming3565a062010-08-16 18:57:57 +0000467
468 Write16(0); // e_phentsize = prog header entry size
469 Write16(0); // e_phnum = # prog header entries = 0
470
471 // e_shentsize = Section header entry size
Rafael Espindolabff66a82010-12-18 03:27:34 +0000472 Write16(is64Bit() ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
Matt Fleming3565a062010-08-16 18:57:57 +0000473
474 // e_shnum = # of section header ents
Rafael Espindola7be2c332010-10-31 00:16:26 +0000475 if (NumberOfSections >= ELF::SHN_LORESERVE)
476 Write16(0);
477 else
478 Write16(NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000479
480 // e_shstrndx = Section # of '.shstrtab'
Rafael Espindola7be2c332010-10-31 00:16:26 +0000481 if (NumberOfSections >= ELF::SHN_LORESERVE)
482 Write16(ELF::SHN_XINDEX);
483 else
484 Write16(ShstrtabIndex);
Matt Fleming3565a062010-08-16 18:57:57 +0000485}
486
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000487void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
488 MCDataFragment *ShndxF,
489 uint64_t name,
490 uint8_t info, uint64_t value,
491 uint64_t size, uint8_t other,
492 uint32_t shndx,
493 bool Reserved) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000494 if (ShndxF) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000495 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000496 String32(*ShndxF, shndx);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000497 else
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000498 String32(*ShndxF, 0);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000499 }
500
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000501 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
502 uint16_t(ELF::SHN_XINDEX) : shndx;
503
Rafael Espindolabff66a82010-12-18 03:27:34 +0000504 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000505 String32(*SymtabF, name); // st_name
506 String8(*SymtabF, info); // st_info
507 String8(*SymtabF, other); // st_other
508 String16(*SymtabF, Index); // st_shndx
509 String64(*SymtabF, value); // st_value
510 String64(*SymtabF, size); // st_size
Matt Fleming3565a062010-08-16 18:57:57 +0000511 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000512 String32(*SymtabF, name); // st_name
513 String32(*SymtabF, value); // st_value
514 String32(*SymtabF, size); // st_size
515 String8(*SymtabF, info); // st_info
516 String8(*SymtabF, other); // st_other
517 String16(*SymtabF, Index); // st_shndx
Matt Fleming3565a062010-08-16 18:57:57 +0000518 }
519}
520
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000521uint64_t ELFObjectWriter::SymbolValue(MCSymbolData &Data,
522 const MCAsmLayout &Layout) {
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000523 if (Data.isCommon() && Data.isExternal())
524 return Data.getCommonAlignment();
525
526 const MCSymbol &Symbol = Data.getSymbol();
Roman Divackyd1491862010-12-20 21:14:39 +0000527
528 if (Symbol.isAbsolute() && Symbol.isVariable()) {
529 if (const MCExpr *Value = Symbol.getVariableValue()) {
530 int64_t IntValue;
531 if (Value->EvaluateAsAbsolute(IntValue, Layout))
532 return (uint64_t)IntValue;
533 }
534 }
535
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000536 if (!Symbol.isInSection())
537 return 0;
538
Rafael Espindolaffd902b2010-12-06 02:57:26 +0000539 if (Data.getFragment())
540 return Layout.getSymbolOffset(&Data);
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000541
542 return 0;
543}
544
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000545void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
546 const MCAsmLayout &Layout) {
Rafael Espindola88182132010-10-27 15:18:17 +0000547 // The presence of symbol versions causes undefined symbols and
548 // versions declared with @@@ to be renamed.
549
550 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
551 ie = Asm.symbol_end(); it != ie; ++it) {
552 const MCSymbol &Alias = it->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000553 const MCSymbol &Symbol = Alias.AliasedSymbol();
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000554 MCSymbolData &SD = Asm.getSymbolData(Symbol);
555
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000556 // Not an alias.
557 if (&Symbol == &Alias)
558 continue;
559
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000560 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000561 size_t Pos = AliasName.find('@');
562 if (Pos == StringRef::npos)
563 continue;
564
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000565 // Aliases defined with .symvar copy the binding from the symbol they alias.
566 // This is the first place we are able to copy this information.
567 it->setExternal(SD.isExternal());
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000568 MCELF::SetBinding(*it, MCELF::GetBinding(SD));
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000569
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000570 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000571 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
572 continue;
573
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000574 // FIXME: produce a better error message.
575 if (Symbol.isUndefined() && Rest.startswith("@@") &&
576 !Rest.startswith("@@@"))
577 report_fatal_error("A @@ version cannot be undefined");
578
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000579 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000580 }
581}
582
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000583void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
584 MCDataFragment *ShndxF,
585 ELFSymbolData &MSD,
586 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000587 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000588 MCSymbolData &Data =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000589 Layout.getAssembler().getSymbolData(OrigData.getSymbol().AliasedSymbol());
Rafael Espindola152c1062010-10-06 21:02:29 +0000590
Rafael Espindola7be2c332010-10-31 00:16:26 +0000591 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
592 Data.getSymbol().isVariable();
593
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000594 uint8_t Binding = MCELF::GetBinding(OrigData);
595 uint8_t Visibility = MCELF::GetVisibility(OrigData);
596 uint8_t Type = MCELF::GetType(Data);
Rafael Espindola152c1062010-10-06 21:02:29 +0000597
598 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
599 uint8_t Other = Visibility;
600
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000601 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000602 uint64_t Size = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000603
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000604 assert(!(Data.isCommon() && !Data.isExternal()));
605
Rafael Espindolaf0121242010-12-22 16:03:00 +0000606 const MCExpr *ESize = Data.getSize();
607 if (ESize) {
608 int64_t Res;
609 if (!ESize->EvaluateAsAbsolute(Res, Layout))
610 report_fatal_error("Size expression must be absolute.");
611 Size = Res;
Matt Fleming3565a062010-08-16 18:57:57 +0000612 }
613
614 // Write out the symbol table entry
Rafael Espindola7be2c332010-10-31 00:16:26 +0000615 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
616 Size, Other, MSD.SectionIndex, IsReserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000617}
618
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000619void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
620 MCDataFragment *ShndxF,
621 const MCAssembler &Asm,
622 const MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000623 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000624 // The string table must be emitted first because we need the index
625 // into the string table for all the symbol names.
626 assert(StringTable.size() && "Missing string table");
627
628 // FIXME: Make sure the start of the symbol table is aligned.
629
630 // The first entry is the undefined symbol entry.
Rafael Espindola7be2c332010-10-31 00:16:26 +0000631 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
Matt Fleming3565a062010-08-16 18:57:57 +0000632
633 // Write the symbol table entries.
634 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
635 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
636 ELFSymbolData &MSD = LocalSymbolData[i];
Rafael Espindola7be2c332010-10-31 00:16:26 +0000637 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000638 }
639
Rafael Espindola71859c62010-09-16 19:46:31 +0000640 // Write out a symbol table entry for each regular section.
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000641 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
642 ++i) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000643 const MCSectionELF &Section =
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000644 static_cast<const MCSectionELF&>(i->getSection());
645 if (Section.getType() == ELF::SHT_RELA ||
646 Section.getType() == ELF::SHT_REL ||
647 Section.getType() == ELF::SHT_STRTAB ||
648 Section.getType() == ELF::SHT_SYMTAB)
Eli Friedmana44fa242010-08-16 21:17:09 +0000649 continue;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000650 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000651 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section), false);
Eli Friedmana44fa242010-08-16 21:17:09 +0000652 LastLocalSymbolIndex++;
653 }
Matt Fleming3565a062010-08-16 18:57:57 +0000654
655 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
656 ELFSymbolData &MSD = ExternalSymbolData[i];
657 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000658 assert(((Data.getFlags() & ELF_STB_Global) ||
659 (Data.getFlags() & ELF_STB_Weak)) &&
660 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000661 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000662 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000663 LastLocalSymbolIndex++;
664 }
665
666 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
667 ELFSymbolData &MSD = UndefinedSymbolData[i];
668 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000669 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000670 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000671 LastLocalSymbolIndex++;
672 }
673}
674
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000675const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm,
676 const MCValue &Target,
677 const MCFragment &F) const {
678 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000679 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
680 const MCSymbol *Renamed = Renames.lookup(&Symbol);
681 const MCSymbolData &SD = Asm.getSymbolData(Symbol);
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000682
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000683 if (ASymbol.isUndefined()) {
684 if (Renamed)
685 return Renamed;
686 return &ASymbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000687 }
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000688
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000689 if (SD.isExternal()) {
690 if (Renamed)
691 return Renamed;
692 return &Symbol;
693 }
Rafael Espindola73ffea42010-09-25 05:42:19 +0000694
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000695 const MCSectionELF &Section =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000696 static_cast<const MCSectionELF&>(ASymbol.getSection());
Rafael Espindola25958732010-11-24 21:57:39 +0000697 const SectionKind secKind = Section.getKind();
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000698
Rafael Espindola25958732010-11-24 21:57:39 +0000699 if (secKind.isBSS())
Jason W Kim953a2a32011-02-07 01:11:15 +0000700 return ExplicitRelSym(Asm, Target, F, true);
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000701
Rafael Espindola25958732010-11-24 21:57:39 +0000702 if (secKind.isThreadLocal()) {
703 if (Renamed)
704 return Renamed;
705 return &Symbol;
706 }
707
Rafael Espindola8cecf252010-10-06 16:23:36 +0000708 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000709 const MCSectionELF &Sec2 =
710 static_cast<const MCSectionELF&>(F.getParent()->getSection());
711
Rafael Espindola8cecf252010-10-06 16:23:36 +0000712 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000713 (Kind == MCSymbolRefExpr::VK_PLT ||
714 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
Rafael Espindola25958732010-11-24 21:57:39 +0000715 Kind == MCSymbolRefExpr::VK_GOTOFF)) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000716 if (Renamed)
717 return Renamed;
718 return &Symbol;
719 }
Rafael Espindola3729d002010-10-05 23:57:26 +0000720
Rafael Espindola1c130262011-01-23 04:43:11 +0000721 if (Section.getFlags() & ELF::SHF_MERGE) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000722 if (Target.getConstant() == 0)
723 return NULL;
724 if (Renamed)
725 return Renamed;
726 return &Symbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000727 }
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000728
Jason W Kim953a2a32011-02-07 01:11:15 +0000729 return ExplicitRelSym(Asm, Target, F, false);
Rafael Espindola73ffea42010-09-25 05:42:19 +0000730}
731
Matt Fleming3565a062010-08-16 18:57:57 +0000732
Jason W Kim56a39902010-12-06 21:57:34 +0000733void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
734 const MCAsmLayout &Layout,
735 const MCFragment *Fragment,
736 const MCFixup &Fixup,
737 MCValue Target,
738 uint64_t &FixedValue) {
739 int64_t Addend = 0;
740 int Index = 0;
741 int64_t Value = Target.getConstant();
742 const MCSymbol *RelocSymbol = NULL;
743
Rafael Espindola127a6a42010-12-17 07:28:17 +0000744 bool IsPCRel = isFixupKindPCRel(Asm, Fixup.getKind());
Jason W Kim56a39902010-12-06 21:57:34 +0000745 if (!Target.isAbsolute()) {
746 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
747 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
748 RelocSymbol = SymbolToReloc(Asm, Target, *Fragment);
749
750 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
751 const MCSymbol &SymbolB = RefB->getSymbol();
752 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
753 IsPCRel = true;
754
755 // Offset of the symbol in the section
756 int64_t a = Layout.getSymbolOffset(&SDB);
757
758 // Ofeset of the relocation in the section
759 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
760 Value += b - a;
761 }
762
763 if (!RelocSymbol) {
764 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
765 MCFragment *F = SD.getFragment();
766
767 Index = F->getParent()->getOrdinal() + 1;
768
769 // Offset of the symbol in the section
770 Value += Layout.getSymbolOffset(&SD);
771 } else {
772 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
773 WeakrefUsedInReloc.insert(RelocSymbol);
774 else
775 UsedInReloc.insert(RelocSymbol);
776 Index = -1;
777 }
778 Addend = Value;
779 // Compensate for the addend on i386.
Rafael Espindolabff66a82010-12-18 03:27:34 +0000780 if (is64Bit())
Jason W Kim56a39902010-12-06 21:57:34 +0000781 Value = 0;
782 }
783
784 FixedValue = Value;
785 unsigned Type = GetRelocType(Target, Fixup, IsPCRel,
786 (RelocSymbol != 0), Addend);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000787
Jason W Kim56a39902010-12-06 21:57:34 +0000788 uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
789 Fixup.getOffset();
790
Rafael Espindolabff66a82010-12-18 03:27:34 +0000791 if (!hasRelocationAddend())
792 Addend = 0;
Jason W Kim56a39902010-12-06 21:57:34 +0000793 ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend);
794 Relocations[Fragment->getParent()].push_back(ERE);
795}
796
797
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000798uint64_t
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000799ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
800 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000801 MCSymbolData &SD = Asm.getSymbolData(*S);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000802 return SD.getIndex();
Matt Fleming3565a062010-08-16 18:57:57 +0000803}
804
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000805bool ELFObjectWriter::isInSymtab(const MCAssembler &Asm,
806 const MCSymbolData &Data,
807 bool Used, bool Renamed) {
Rafael Espindola484291c2010-11-01 14:28:48 +0000808 if (Data.getFlags() & ELF_Other_Weakref)
809 return false;
810
Rafael Espindolabd701182010-10-19 19:31:37 +0000811 if (Used)
812 return true;
813
Rafael Espindola88182132010-10-27 15:18:17 +0000814 if (Renamed)
815 return false;
816
Rafael Espindola737cd212010-10-05 18:01:23 +0000817 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000818
Rafael Espindolad1798862010-10-29 23:09:31 +0000819 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
820 return true;
821
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000822 const MCSymbol &A = Symbol.AliasedSymbol();
Rafael Espindola21451e52011-02-23 20:22:07 +0000823 if (Symbol.isVariable() && !A.isVariable() && A.isUndefined())
824 return false;
825
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000826 bool IsGlobal = MCELF::GetBinding(Data) == ELF::STB_GLOBAL;
Rafael Espindola21451e52011-02-23 20:22:07 +0000827 if (!Symbol.isVariable() && Symbol.isUndefined() && !IsGlobal)
Rafael Espindolaa6866962010-10-27 14:44:52 +0000828 return false;
829
Rafael Espindola737cd212010-10-05 18:01:23 +0000830 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
831 return false;
832
Rafael Espindolabd701182010-10-19 19:31:37 +0000833 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000834 return false;
835
836 return true;
837}
838
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000839bool ELFObjectWriter::isLocal(const MCSymbolData &Data, bool isSignature,
840 bool isUsedInReloc) {
Rafael Espindola737cd212010-10-05 18:01:23 +0000841 if (Data.isExternal())
842 return false;
843
844 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000845 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000846
847 if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
848 if (isSignature && !isUsedInReloc)
849 return true;
850
Rafael Espindola737cd212010-10-05 18:01:23 +0000851 return false;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000852 }
Rafael Espindola737cd212010-10-05 18:01:23 +0000853
854 return true;
855}
856
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000857void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
858 SectionIndexMapTy &SectionIndexMap) {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000859 unsigned Index = 1;
860 for (MCAssembler::iterator it = Asm.begin(),
861 ie = Asm.end(); it != ie; ++it) {
862 const MCSectionELF &Section =
863 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000864 if (Section.getType() != ELF::SHT_GROUP)
865 continue;
866 SectionIndexMap[&Section] = Index++;
867 }
868
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());
873 if (Section.getType() == ELF::SHT_GROUP)
874 continue;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000875 SectionIndexMap[&Section] = Index++;
876 }
877}
878
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000879void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000880 const SectionIndexMapTy &SectionIndexMap,
881 RevGroupMapTy RevGroupMap) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000882 // FIXME: Is this the correct place to do this?
883 if (NeedsGOT) {
884 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
885 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
886 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
887 Data.setExternal(true);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000888 MCELF::SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindola5c77c162010-10-05 15:48:37 +0000889 }
890
Matt Fleming3565a062010-08-16 18:57:57 +0000891 // Build section lookup table.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000892 int NumRegularSections = Asm.size();
Matt Fleming3565a062010-08-16 18:57:57 +0000893
894 // Index 0 is always the empty string.
895 StringMap<uint64_t> StringIndexMap;
896 StringTable += '\x00';
897
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000898 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000899 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
900 ie = Asm.symbol_end(); it != ie; ++it) {
901 const MCSymbol &Symbol = it->getSymbol();
902
Rafael Espindola484291c2010-11-01 14:28:48 +0000903 bool Used = UsedInReloc.count(&Symbol);
904 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000905 bool isSignature = RevGroupMap.count(&Symbol);
906
907 if (!isInSymtab(Asm, *it,
908 Used || WeakrefUsed || isSignature,
Rafael Espindola88182132010-10-27 15:18:17 +0000909 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000910 continue;
911
Matt Fleming3565a062010-08-16 18:57:57 +0000912 ELFSymbolData MSD;
913 MSD.SymbolData = it;
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000914 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Matt Fleming3565a062010-08-16 18:57:57 +0000915
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000916 // Undefined symbols are global, but this is the first place we
917 // are able to set it.
918 bool Local = isLocal(*it, isSignature, Used);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000919 if (!Local && MCELF::GetBinding(*it) == ELF::STB_LOCAL) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000920 MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000921 MCELF::SetBinding(*it, ELF::STB_GLOBAL);
922 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000923 }
924
Rafael Espindola484291c2010-11-01 14:28:48 +0000925 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000926 MCELF::SetBinding(*it, ELF::STB_WEAK);
Rafael Espindola484291c2010-11-01 14:28:48 +0000927
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000928 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000929 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000930 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000931 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000932 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000933 } else if (RefSymbol.isUndefined()) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000934 if (isSignature && !Used)
935 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
936 else
937 MSD.SectionIndex = ELF::SHN_UNDEF;
Matt Fleming3565a062010-08-16 18:57:57 +0000938 } else {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000939 const MCSectionELF &Section =
940 static_cast<const MCSectionELF&>(RefSymbol.getSection());
941 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000942 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
943 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000944 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000945 }
946
Rafael Espindola88182132010-10-27 15:18:17 +0000947 // The @@@ in symbol version is replaced with @ in undefined symbols and
948 // @@ in defined ones.
949 StringRef Name = Symbol.getName();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000950 SmallString<32> Buf;
951
Rafael Espindola88182132010-10-27 15:18:17 +0000952 size_t Pos = Name.find("@@@");
Rafael Espindola88182132010-10-27 15:18:17 +0000953 if (Pos != StringRef::npos) {
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000954 Buf += Name.substr(0, Pos);
955 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
956 Buf += Name.substr(Pos + Skip);
957 Name = Buf;
Rafael Espindola88182132010-10-27 15:18:17 +0000958 }
959
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000960 uint64_t &Entry = StringIndexMap[Name];
Rafael Espindolaa6866962010-10-27 14:44:52 +0000961 if (!Entry) {
962 Entry = StringTable.size();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000963 StringTable += Name;
Rafael Espindolaa6866962010-10-27 14:44:52 +0000964 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +0000965 }
Rafael Espindolaa6866962010-10-27 14:44:52 +0000966 MSD.StringIndex = Entry;
967 if (MSD.SectionIndex == ELF::SHN_UNDEF)
968 UndefinedSymbolData.push_back(MSD);
969 else if (Local)
970 LocalSymbolData.push_back(MSD);
971 else
972 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +0000973 }
974
975 // Symbols are required to be in lexicographic order.
976 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
977 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
978 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
979
980 // Set the symbol indices. Local symbols must come before all other
981 // symbols with non-local bindings.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000982 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000983 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
984 LocalSymbolData[i].SymbolData->setIndex(Index++);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000985
986 Index += NumRegularSections;
987
Matt Fleming3565a062010-08-16 18:57:57 +0000988 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
989 ExternalSymbolData[i].SymbolData->setIndex(Index++);
990 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
991 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
992}
993
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000994void ELFObjectWriter::WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
995 const MCSectionData &SD) {
Matt Fleming3565a062010-08-16 18:57:57 +0000996 if (!Relocations[&SD].empty()) {
997 MCContext &Ctx = Asm.getContext();
Rafael Espindola4283f4b2010-11-10 19:05:07 +0000998 const MCSectionELF *RelaSection;
Matt Fleming3565a062010-08-16 18:57:57 +0000999 const MCSectionELF &Section =
1000 static_cast<const MCSectionELF&>(SD.getSection());
1001
1002 const StringRef SectionName = Section.getSectionName();
Rafael Espindolabff66a82010-12-18 03:27:34 +00001003 std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +00001004 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +00001005
1006 unsigned EntrySize;
Rafael Espindolabff66a82010-12-18 03:27:34 +00001007 if (hasRelocationAddend())
1008 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
Benjamin Kramer299fbe32010-08-17 17:56:13 +00001009 else
Rafael Espindolabff66a82010-12-18 03:27:34 +00001010 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +00001011
Rafael Espindolabff66a82010-12-18 03:27:34 +00001012 RelaSection = Ctx.getELFSection(RelaSectionName, hasRelocationAddend() ?
Benjamin Kramer377a5722010-08-17 17:30:07 +00001013 ELF::SHT_RELA : ELF::SHT_REL, 0,
Matt Fleming3565a062010-08-16 18:57:57 +00001014 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001015 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001016
1017 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +00001018 RelaSD.setAlignment(is64Bit() ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +00001019
1020 MCDataFragment *F = new MCDataFragment(&RelaSD);
1021
1022 WriteRelocationsFragment(Asm, F, &SD);
Matt Fleming3565a062010-08-16 18:57:57 +00001023 }
1024}
1025
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001026void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
1027 uint64_t Flags, uint64_t Address,
1028 uint64_t Offset, uint64_t Size,
1029 uint32_t Link, uint32_t Info,
1030 uint64_t Alignment,
1031 uint64_t EntrySize) {
Matt Fleming3565a062010-08-16 18:57:57 +00001032 Write32(Name); // sh_name: index into string table
1033 Write32(Type); // sh_type
1034 WriteWord(Flags); // sh_flags
1035 WriteWord(Address); // sh_addr
1036 WriteWord(Offset); // sh_offset
1037 WriteWord(Size); // sh_size
1038 Write32(Link); // sh_link
1039 Write32(Info); // sh_info
1040 WriteWord(Alignment); // sh_addralign
1041 WriteWord(EntrySize); // sh_entsize
1042}
1043
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001044void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
1045 MCDataFragment *F,
1046 const MCSectionData *SD) {
Matt Fleming3565a062010-08-16 18:57:57 +00001047 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
1048 // sort by the r_offset just like gnu as does
1049 array_pod_sort(Relocs.begin(), Relocs.end());
1050
1051 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1052 ELFRelocationEntry entry = Relocs[e - i - 1];
1053
Rafael Espindola12203cc2010-11-21 00:48:25 +00001054 if (!entry.Index)
1055 ;
1056 else if (entry.Index < 0)
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001057 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
1058 else
Rafael Espindola12203cc2010-11-21 00:48:25 +00001059 entry.Index += LocalSymbolData.size();
Rafael Espindolabff66a82010-12-18 03:27:34 +00001060 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001061 String64(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001062
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001063 struct ELF::Elf64_Rela ERE64;
1064 ERE64.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001065 String64(*F, ERE64.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001066
Rafael Espindolabff66a82010-12-18 03:27:34 +00001067 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001068 String64(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001069 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001070 String32(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001071
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001072 struct ELF::Elf32_Rela ERE32;
1073 ERE32.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001074 String32(*F, ERE32.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 String32(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001078 }
Matt Fleming3565a062010-08-16 18:57:57 +00001079 }
1080}
1081
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001082void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
1083 MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001084 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +00001085 MCContext &Ctx = Asm.getContext();
1086 MCDataFragment *F;
1087
Rafael Espindolabff66a82010-12-18 03:27:34 +00001088 unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
Matt Fleming3565a062010-08-16 18:57:57 +00001089
Rafael Espindola38738bf2010-09-22 19:04:41 +00001090 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001091 const MCSectionELF *ShstrtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001092 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001093 SectionKind::getReadOnly());
Rafael Espindola71859c62010-09-16 19:46:31 +00001094 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1095 ShstrtabSD.setAlignment(1);
1096 ShstrtabIndex = Asm.size();
1097
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001098 const MCSectionELF *SymtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001099 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1100 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001101 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001102 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +00001103 SymtabSD.setAlignment(is64Bit() ? 8 : 4);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001104 SymbolTableIndex = Asm.size();
1105
1106 MCSectionData *SymtabShndxSD = NULL;
1107
1108 if (NeedsSymtabShndx) {
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001109 const MCSectionELF *SymtabShndxSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001110 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001111 SectionKind::getReadOnly(), 4, "");
Rafael Espindola7be2c332010-10-31 00:16:26 +00001112 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
1113 SymtabShndxSD->setAlignment(4);
1114 }
Matt Fleming3565a062010-08-16 18:57:57 +00001115
Matt Fleming3565a062010-08-16 18:57:57 +00001116 const MCSection *StrtabSection;
1117 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001118 SectionKind::getReadOnly());
Matt Fleming3565a062010-08-16 18:57:57 +00001119 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1120 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +00001121 StringTableIndex = Asm.size();
1122
Rafael Espindolac3c413f2010-09-27 22:04:54 +00001123 WriteRelocations(Asm, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001124
1125 // Symbol table
1126 F = new MCDataFragment(&SymtabSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001127 MCDataFragment *ShndxF = NULL;
1128 if (NeedsSymtabShndx) {
1129 ShndxF = new MCDataFragment(SymtabShndxSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001130 }
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001131 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
Rafael Espindola71859c62010-09-16 19:46:31 +00001132
Matt Fleming3565a062010-08-16 18:57:57 +00001133 F = new MCDataFragment(&StrtabSD);
1134 F->getContents().append(StringTable.begin(), StringTable.end());
Matt Fleming3565a062010-08-16 18:57:57 +00001135
Matt Fleming3565a062010-08-16 18:57:57 +00001136 F = new MCDataFragment(&ShstrtabSD);
1137
Matt Fleming3565a062010-08-16 18:57:57 +00001138 // Section header string table.
1139 //
1140 // The first entry of a string table holds a null character so skip
1141 // section 0.
1142 uint64_t Index = 1;
1143 F->getContents() += '\x00';
1144
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001145 StringMap<uint64_t> SecStringMap;
Matt Fleming3565a062010-08-16 18:57:57 +00001146 for (MCAssembler::const_iterator it = Asm.begin(),
1147 ie = Asm.end(); it != ie; ++it) {
Matt Fleming3565a062010-08-16 18:57:57 +00001148 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +00001149 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola51efe7a2010-09-23 14:14:56 +00001150 // FIXME: We could merge suffixes like in .text and .rela.text.
Matt Fleming3565a062010-08-16 18:57:57 +00001151
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001152 StringRef Name = Section.getSectionName();
1153 if (SecStringMap.count(Name)) {
1154 SectionStringTableIndex[&Section] = SecStringMap[Name];
1155 continue;
1156 }
Matt Fleming3565a062010-08-16 18:57:57 +00001157 // Remember the index into the string table so we can write it
1158 // into the sh_name field of the section header table.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001159 SectionStringTableIndex[&Section] = Index;
1160 SecStringMap[Name] = Index;
Matt Fleming3565a062010-08-16 18:57:57 +00001161
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001162 Index += Name.size() + 1;
1163 F->getContents() += Name;
Matt Fleming3565a062010-08-16 18:57:57 +00001164 F->getContents() += '\x00';
1165 }
Rafael Espindola70703872010-09-30 02:22:20 +00001166}
1167
Rafael Espindola96aa78c2011-01-23 17:55:27 +00001168void ELFObjectWriter::CreateIndexedSections(MCAssembler &Asm,
1169 MCAsmLayout &Layout,
1170 GroupMapTy &GroupMap,
1171 RevGroupMapTy &RevGroupMap) {
1172 // Create the .note.GNU-stack section if needed.
1173 MCContext &Ctx = Asm.getContext();
1174 if (Asm.getNoExecStack()) {
1175 const MCSectionELF *GnuStackSection =
1176 Ctx.getELFSection(".note.GNU-stack", ELF::SHT_PROGBITS, 0,
1177 SectionKind::getReadOnly());
1178 Asm.getOrCreateSectionData(*GnuStackSection);
1179 }
1180
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001181 // Build the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001182 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1183 it != ie; ++it) {
1184 const MCSectionELF &Section =
1185 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola1c130262011-01-23 04:43:11 +00001186 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001187 continue;
1188
1189 const MCSymbol *SignatureSymbol = Section.getGroup();
1190 Asm.getOrCreateSymbolData(*SignatureSymbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001191 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001192 if (!Group) {
Rafael Espindola96aa78c2011-01-23 17:55:27 +00001193 Group = Ctx.CreateELFGroupSection();
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001194 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1195 Data.setAlignment(4);
1196 MCDataFragment *F = new MCDataFragment(&Data);
1197 String32(*F, ELF::GRP_COMDAT);
1198 }
1199 GroupMap[Group] = SignatureSymbol;
1200 }
1201
1202 // Add sections to the groups
1203 unsigned Index = 1;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001204 unsigned NumGroups = RevGroupMap.size();
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001205 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1206 it != ie; ++it, ++Index) {
1207 const MCSectionELF &Section =
1208 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola1c130262011-01-23 04:43:11 +00001209 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001210 continue;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001211 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001212 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1213 // FIXME: we could use the previous fragment
1214 MCDataFragment *F = new MCDataFragment(&Data);
1215 String32(*F, NumGroups + Index);
1216 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001217}
1218
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001219void ELFObjectWriter::WriteSection(MCAssembler &Asm,
1220 const SectionIndexMapTy &SectionIndexMap,
1221 uint32_t GroupSymbolIndex,
1222 uint64_t Offset, uint64_t Size,
1223 uint64_t Alignment,
1224 const MCSectionELF &Section) {
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001225 uint64_t sh_link = 0;
1226 uint64_t sh_info = 0;
1227
1228 switch(Section.getType()) {
1229 case ELF::SHT_DYNAMIC:
1230 sh_link = SectionStringTableIndex[&Section];
1231 sh_info = 0;
1232 break;
1233
1234 case ELF::SHT_REL:
1235 case ELF::SHT_RELA: {
1236 const MCSectionELF *SymtabSection;
1237 const MCSectionELF *InfoSection;
1238 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
1239 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001240 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001241 sh_link = SectionIndexMap.lookup(SymtabSection);
1242 assert(sh_link && ".symtab not found");
1243
1244 // Remove ".rel" and ".rela" prefixes.
1245 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1246 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
1247
1248 InfoSection = Asm.getContext().getELFSection(SectionName,
1249 ELF::SHT_PROGBITS, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001250 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001251 sh_info = SectionIndexMap.lookup(InfoSection);
1252 break;
1253 }
1254
1255 case ELF::SHT_SYMTAB:
1256 case ELF::SHT_DYNSYM:
1257 sh_link = StringTableIndex;
1258 sh_info = LastLocalSymbolIndex;
1259 break;
1260
1261 case ELF::SHT_SYMTAB_SHNDX:
1262 sh_link = SymbolTableIndex;
1263 break;
1264
1265 case ELF::SHT_PROGBITS:
1266 case ELF::SHT_STRTAB:
1267 case ELF::SHT_NOBITS:
Rafael Espindola98976612010-12-26 21:30:59 +00001268 case ELF::SHT_NOTE:
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001269 case ELF::SHT_NULL:
1270 case ELF::SHT_ARM_ATTRIBUTES:
Jason W Kim86a97f22011-01-12 00:19:25 +00001271 case ELF::SHT_INIT_ARRAY:
1272 case ELF::SHT_FINI_ARRAY:
1273 case ELF::SHT_PREINIT_ARRAY:
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +00001274 case ELF::SHT_X86_64_UNWIND:
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001275 // Nothing to do.
1276 break;
1277
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001278 case ELF::SHT_GROUP: {
1279 sh_link = SymbolTableIndex;
1280 sh_info = GroupSymbolIndex;
1281 break;
1282 }
1283
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001284 default:
1285 assert(0 && "FIXME: sh_type value not supported!");
1286 break;
1287 }
1288
1289 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1290 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1291 Alignment, Section.getEntrySize());
1292}
1293
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001294bool ELFObjectWriter::IsELFMetaDataSection(const MCSectionData &SD) {
Rafael Espindolaf8803fe2010-12-06 03:48:09 +00001295 return SD.getOrdinal() == ~UINT32_C(0) &&
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001296 !SD.getSection().isVirtualSection();
1297}
1298
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001299uint64_t ELFObjectWriter::DataSectionSize(const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001300 uint64_t Ret = 0;
1301 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1302 ++i) {
1303 const MCFragment &F = *i;
1304 assert(F.getKind() == MCFragment::FT_Data);
1305 Ret += cast<MCDataFragment>(F).getContents().size();
1306 }
1307 return Ret;
1308}
1309
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001310uint64_t ELFObjectWriter::GetSectionFileSize(const MCAsmLayout &Layout,
1311 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001312 if (IsELFMetaDataSection(SD))
1313 return DataSectionSize(SD);
1314 return Layout.getSectionFileSize(&SD);
1315}
1316
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001317uint64_t ELFObjectWriter::GetSectionAddressSize(const MCAsmLayout &Layout,
1318 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001319 if (IsELFMetaDataSection(SD))
1320 return DataSectionSize(SD);
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001321 return Layout.getSectionAddressSize(&SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001322}
1323
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001324void ELFObjectWriter::WriteDataSectionData(ELFObjectWriter *W,
1325 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001326 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1327 ++i) {
1328 const MCFragment &F = *i;
1329 assert(F.getKind() == MCFragment::FT_Data);
1330 W->WriteBytes(cast<MCDataFragment>(F).getContents().str());
1331 }
1332}
1333
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001334void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1335 const MCAsmLayout &Layout) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001336 GroupMapTy GroupMap;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001337 RevGroupMapTy RevGroupMap;
Rafael Espindola96aa78c2011-01-23 17:55:27 +00001338 CreateIndexedSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1339 RevGroupMap);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001340
Rafael Espindolabab2a802010-11-10 21:51:05 +00001341 SectionIndexMapTy SectionIndexMap;
1342
1343 ComputeIndexMap(Asm, SectionIndexMap);
1344
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001345 // Compute symbol table information.
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001346 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap);
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001347
Matt Fleming3565a062010-08-16 18:57:57 +00001348 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001349 const_cast<MCAsmLayout&>(Layout),
1350 SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +00001351
Rafael Espindola1d739a02010-11-10 22:34:07 +00001352 // Update to include the metadata sections.
1353 ComputeIndexMap(Asm, SectionIndexMap);
1354
Matt Fleming3565a062010-08-16 18:57:57 +00001355 // Add 1 for the null section.
1356 unsigned NumSections = Asm.size() + 1;
Rafael Espindolabff66a82010-12-18 03:27:34 +00001357 uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
1358 uint64_t HeaderSize = is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
1359 sizeof(ELF::Elf32_Ehdr);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001360 uint64_t FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001361
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001362 std::vector<const MCSectionELF*> Sections;
1363 Sections.resize(NumSections);
1364
1365 for (SectionIndexMapTy::const_iterator i=
1366 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1367 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
1368 Sections[p.second] = p.first;
1369 }
1370
1371 for (unsigned i = 1; i < NumSections; ++i) {
1372 const MCSectionELF &Section = *Sections[i];
1373 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001374
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001375 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1376
Matt Fleming3565a062010-08-16 18:57:57 +00001377 // Get the size of the section in the output file (including padding).
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001378 FileOff += GetSectionFileSize(Layout, SD);
Matt Fleming3565a062010-08-16 18:57:57 +00001379 }
1380
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001381 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1382
Matt Fleming3565a062010-08-16 18:57:57 +00001383 // Write out the ELF header ...
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001384 WriteHeader(FileOff - HeaderSize, NumSections);
1385
1386 FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001387
1388 // ... then all of the sections ...
1389 DenseMap<const MCSection*, uint64_t> SectionOffsetMap;
1390
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001391 for (unsigned i = 1; i < NumSections; ++i) {
1392 const MCSectionELF &Section = *Sections[i];
1393 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001394
1395 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1396 WriteZeros(Padding);
1397 FileOff += Padding;
1398
Matt Fleming3565a062010-08-16 18:57:57 +00001399 // Remember the offset into the file for this section.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001400 SectionOffsetMap[&Section] = FileOff;
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001401
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001402 FileOff += GetSectionFileSize(Layout, SD);
Matt Fleming3565a062010-08-16 18:57:57 +00001403
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001404 if (IsELFMetaDataSection(SD))
1405 WriteDataSectionData(this, SD);
1406 else
Daniel Dunbar5d2477c2010-12-17 02:45:59 +00001407 Asm.WriteSectionData(&SD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +00001408 }
1409
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001410 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1411 WriteZeros(Padding);
1412 FileOff += Padding;
1413
Matt Fleming3565a062010-08-16 18:57:57 +00001414 // ... and then the section header table.
1415 // Should we align the section header table?
1416 //
1417 // Null section first.
Rafael Espindola7be2c332010-10-31 00:16:26 +00001418 uint64_t FirstSectionSize =
1419 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1420 uint32_t FirstSectionLink =
1421 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1422 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming3565a062010-08-16 18:57:57 +00001423
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);
1427 uint32_t GroupSymbolIndex;
1428 if (Section.getType() != ELF::SHT_GROUP)
1429 GroupSymbolIndex = 0;
1430 else
1431 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm, GroupMap[&Section]);
Matt Fleming3565a062010-08-16 18:57:57 +00001432
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001433 uint64_t Size = GetSectionAddressSize(Layout, SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001434
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001435 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001436 SectionOffsetMap[&Section], Size,
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001437 SD.getAlignment(), Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001438 }
1439}
1440
Rafael Espindola6024c972010-12-17 17:45:22 +00001441MCObjectWriter *llvm::createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1442 raw_ostream &OS,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001443 bool IsLittleEndian) {
1444 switch (MOTW->getEMachine()) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001445 case ELF::EM_386:
1446 case ELF::EM_X86_64:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001447 return new X86ELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001448 case ELF::EM_ARM:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001449 return new ARMELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Wesley Peck4b047132010-11-21 22:06:28 +00001450 case ELF::EM_MBLAZE:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001451 return new MBlazeELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Benjamin Kramer32858772010-11-15 19:20:50 +00001452 default: llvm_unreachable("Unsupported architecture"); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001453 }
1454}
1455
1456
1457/// START OF SUBCLASSES for ELFObjectWriter
1458//===- ARMELFObjectWriter -------------------------------------------===//
1459
Rafael Espindola31f35782010-12-17 18:01:31 +00001460ARMELFObjectWriter::ARMELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001461 raw_ostream &_OS,
1462 bool IsLittleEndian)
1463 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
Jason W Kimd3443e92010-11-15 16:18:39 +00001464{}
1465
1466ARMELFObjectWriter::~ARMELFObjectWriter()
1467{}
1468
Jason W Kim2d7a53a2011-02-04 21:41:11 +00001469// FIXME: get the real EABI Version from the Triple.
1470void ARMELFObjectWriter::WriteEFlags() {
1471 Write32(ELF::EF_ARM_EABIMASK & DefaultEABIVersion);
1472}
1473
Jason W Kim953a2a32011-02-07 01:11:15 +00001474// In ARM, _MergedGlobals and other most symbols get emitted directly.
1475// I.e. not as an offset to a section symbol.
1476// This code is a first-cut approximation of what ARM/gcc does.
1477
1478const MCSymbol *ARMELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
1479 const MCValue &Target,
1480 const MCFragment &F,
1481 bool IsBSS) const {
1482 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
1483 bool EmitThisSym = false;
1484
1485 if (IsBSS) {
1486 EmitThisSym = StringSwitch<bool>(Symbol.getName())
1487 .Case("_MergedGlobals", true)
1488 .Default(false);
1489 } else {
1490 EmitThisSym = StringSwitch<bool>(Symbol.getName())
1491 .Case("_MergedGlobals", true)
1492 .StartsWith(".L.str", true)
1493 .Default(false);
1494 }
1495 if (EmitThisSym)
1496 return &Symbol;
1497 if (! Symbol.isTemporary())
1498 return &Symbol;
1499 return NULL;
1500}
1501
Jason W Kim85fed5e2010-12-01 02:40:06 +00001502unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target,
1503 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001504 bool IsPCRel,
1505 bool IsRelocWithSymbol,
1506 int64_t Addend) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001507 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1508 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
1509
Jason W Kima0871e72010-12-08 23:14:44 +00001510 unsigned Type = 0;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001511 if (IsPCRel) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001512 switch ((unsigned)Fixup.getKind()) {
1513 default: assert(0 && "Unimplemented");
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001514 case FK_Data_4:
1515 switch (Modifier) {
1516 default: llvm_unreachable("Unsupported Modifier");
1517 case MCSymbolRefExpr::VK_None:
Jason W Kim1d866172011-01-13 00:07:51 +00001518 Type = ELF::R_ARM_BASE_PREL;
1519 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001520 case MCSymbolRefExpr::VK_ARM_TLSGD:
Jason W Kim1d866172011-01-13 00:07:51 +00001521 assert(0 && "unimplemented");
1522 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001523 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
1524 Type = ELF::R_ARM_TLS_IE32;
Jason W Kim1d866172011-01-13 00:07:51 +00001525 break;
1526 }
1527 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001528 case ARM::fixup_arm_uncondbranch:
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001529 switch (Modifier) {
1530 case MCSymbolRefExpr::VK_ARM_PLT:
Jason W Kim1d866172011-01-13 00:07:51 +00001531 Type = ELF::R_ARM_PLT32;
1532 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001533 default:
Jason W Kim1d866172011-01-13 00:07:51 +00001534 Type = ELF::R_ARM_CALL;
1535 break;
1536 }
1537 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001538 case ARM::fixup_arm_condbranch:
1539 Type = ELF::R_ARM_JUMP24;
1540 break;
Jason W Kim86a97f22011-01-12 00:19:25 +00001541 case ARM::fixup_arm_movt_hi16:
1542 case ARM::fixup_arm_movt_hi16_pcrel:
Jason W Kim1d866172011-01-13 00:07:51 +00001543 Type = ELF::R_ARM_MOVT_PREL;
1544 break;
Jason W Kim86a97f22011-01-12 00:19:25 +00001545 case ARM::fixup_arm_movw_lo16:
1546 case ARM::fixup_arm_movw_lo16_pcrel:
Jason W Kim1d866172011-01-13 00:07:51 +00001547 Type = ELF::R_ARM_MOVW_PREL_NC;
1548 break;
Evan Chengf3eb3bb2011-01-14 02:38:49 +00001549 case ARM::fixup_t2_movt_hi16:
1550 case ARM::fixup_t2_movt_hi16_pcrel:
1551 Type = ELF::R_ARM_THM_MOVT_PREL;
1552 break;
1553 case ARM::fixup_t2_movw_lo16:
1554 case ARM::fixup_t2_movw_lo16_pcrel:
1555 Type = ELF::R_ARM_THM_MOVW_PREL_NC;
1556 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001557 }
1558 } else {
1559 switch ((unsigned)Fixup.getKind()) {
1560 default: llvm_unreachable("invalid fixup kind!");
Jason W Kima0871e72010-12-08 23:14:44 +00001561 case FK_Data_4:
1562 switch (Modifier) {
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001563 default: llvm_unreachable("Unsupported Modifier"); break;
1564 case MCSymbolRefExpr::VK_ARM_GOT:
Jason W Kim1d866172011-01-13 00:07:51 +00001565 Type = ELF::R_ARM_GOT_BREL;
1566 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001567 case MCSymbolRefExpr::VK_ARM_TLSGD:
Jason W Kim1d866172011-01-13 00:07:51 +00001568 Type = ELF::R_ARM_TLS_GD32;
1569 break;
Jason W Kimf13743b2010-12-16 03:12:17 +00001570 case MCSymbolRefExpr::VK_ARM_TPOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001571 Type = ELF::R_ARM_TLS_LE32;
1572 break;
Jason W Kima0871e72010-12-08 23:14:44 +00001573 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001574 Type = ELF::R_ARM_TLS_IE32;
1575 break;
Jason W Kimf13743b2010-12-16 03:12:17 +00001576 case MCSymbolRefExpr::VK_None:
Jason W Kim1d866172011-01-13 00:07:51 +00001577 Type = ELF::R_ARM_ABS32;
1578 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001579 case MCSymbolRefExpr::VK_ARM_GOTOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001580 Type = ELF::R_ARM_GOTOFF32;
1581 break;
1582 }
1583 break;
Jim Grosbachdff84b02010-12-02 00:28:45 +00001584 case ARM::fixup_arm_ldst_pcrel_12:
Owen Anderson9d63d902010-12-01 19:18:46 +00001585 case ARM::fixup_arm_pcrel_10:
Jim Grosbachdff84b02010-12-02 00:28:45 +00001586 case ARM::fixup_arm_adr_pcrel_12:
Jim Grosbach662a8162010-12-06 23:57:07 +00001587 case ARM::fixup_arm_thumb_bl:
Jim Grosbachb492a7c2010-12-09 19:50:12 +00001588 case ARM::fixup_arm_thumb_cb:
Bill Wendlingb8958b02010-12-08 01:57:09 +00001589 case ARM::fixup_arm_thumb_cp:
Jim Grosbache2467172010-12-10 18:21:33 +00001590 case ARM::fixup_arm_thumb_br:
Jason W Kim1d866172011-01-13 00:07:51 +00001591 assert(0 && "Unimplemented");
1592 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001593 case ARM::fixup_arm_uncondbranch:
Jason W Kim1d866172011-01-13 00:07:51 +00001594 Type = ELF::R_ARM_CALL;
1595 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001596 case ARM::fixup_arm_condbranch:
1597 Type = ELF::R_ARM_JUMP24;
1598 break;
Jason W Kim1d866172011-01-13 00:07:51 +00001599 case ARM::fixup_arm_movt_hi16:
1600 Type = ELF::R_ARM_MOVT_ABS;
1601 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001602 case ARM::fixup_arm_movw_lo16:
Jason W Kim1d866172011-01-13 00:07:51 +00001603 Type = ELF::R_ARM_MOVW_ABS_NC;
1604 break;
Evan Chengf3eb3bb2011-01-14 02:38:49 +00001605 case ARM::fixup_t2_movt_hi16:
1606 Type = ELF::R_ARM_THM_MOVT_ABS;
1607 break;
1608 case ARM::fixup_t2_movw_lo16:
1609 Type = ELF::R_ARM_THM_MOVW_ABS_NC;
1610 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001611 }
1612 }
1613
1614 if (RelocNeedsGOT(Modifier))
1615 NeedsGOT = true;
Jason W Kim953a2a32011-02-07 01:11:15 +00001616
Jason W Kima0871e72010-12-08 23:14:44 +00001617 return Type;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001618}
1619
Wesley Peck4b047132010-11-21 22:06:28 +00001620//===- MBlazeELFObjectWriter -------------------------------------------===//
Jason W Kimd3443e92010-11-15 16:18:39 +00001621
Rafael Espindola31f35782010-12-17 18:01:31 +00001622MBlazeELFObjectWriter::MBlazeELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001623 raw_ostream &_OS,
1624 bool IsLittleEndian)
1625 : ELFObjectWriter(MOTW, _OS, IsLittleEndian) {
Wesley Peck4b047132010-11-21 22:06:28 +00001626}
1627
1628MBlazeELFObjectWriter::~MBlazeELFObjectWriter() {
1629}
1630
Jason W Kim56a39902010-12-06 21:57:34 +00001631unsigned MBlazeELFObjectWriter::GetRelocType(const MCValue &Target,
Wesley Peck4b047132010-11-21 22:06:28 +00001632 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001633 bool IsPCRel,
1634 bool IsRelocWithSymbol,
1635 int64_t Addend) {
Wesley Peck4b047132010-11-21 22:06:28 +00001636 // determine the type of the relocation
1637 unsigned Type;
1638 if (IsPCRel) {
1639 switch ((unsigned)Fixup.getKind()) {
1640 default:
1641 llvm_unreachable("Unimplemented");
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001642 case FK_PCRel_4:
Wesley Peck4b047132010-11-21 22:06:28 +00001643 Type = ELF::R_MICROBLAZE_64_PCREL;
1644 break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001645 case FK_PCRel_2:
Wesley Peck4b047132010-11-21 22:06:28 +00001646 Type = ELF::R_MICROBLAZE_32_PCREL;
1647 break;
1648 }
1649 } else {
1650 switch ((unsigned)Fixup.getKind()) {
1651 default: llvm_unreachable("invalid fixup kind!");
1652 case FK_Data_4:
Jason W Kim56a39902010-12-06 21:57:34 +00001653 Type = ((IsRelocWithSymbol || Addend !=0)
1654 ? ELF::R_MICROBLAZE_32
1655 : ELF::R_MICROBLAZE_64);
Wesley Peck4b047132010-11-21 22:06:28 +00001656 break;
1657 case FK_Data_2:
1658 Type = ELF::R_MICROBLAZE_32;
1659 break;
1660 }
1661 }
Jason W Kim56a39902010-12-06 21:57:34 +00001662 return Type;
Wesley Peck4b047132010-11-21 22:06:28 +00001663}
Jason W Kimd3443e92010-11-15 16:18:39 +00001664
1665//===- X86ELFObjectWriter -------------------------------------------===//
1666
1667
Rafael Espindola31f35782010-12-17 18:01:31 +00001668X86ELFObjectWriter::X86ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001669 raw_ostream &_OS,
1670 bool IsLittleEndian)
1671 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
Jason W Kimd3443e92010-11-15 16:18:39 +00001672{}
1673
1674X86ELFObjectWriter::~X86ELFObjectWriter()
1675{}
1676
Jason W Kim56a39902010-12-06 21:57:34 +00001677unsigned X86ELFObjectWriter::GetRelocType(const MCValue &Target,
1678 const MCFixup &Fixup,
1679 bool IsPCRel,
1680 bool IsRelocWithSymbol,
1681 int64_t Addend) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001682 // determine the type of the relocation
1683
Rafael Espindola12203cc2010-11-21 00:48:25 +00001684 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1685 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
Jason W Kimd3443e92010-11-15 16:18:39 +00001686 unsigned Type;
Rafael Espindolabff66a82010-12-18 03:27:34 +00001687 if (is64Bit()) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001688 if (IsPCRel) {
Rafael Espindola3a83c402010-12-27 00:36:05 +00001689 switch ((unsigned)Fixup.getKind()) {
1690 default: llvm_unreachable("invalid fixup kind!");
1691 case FK_PCRel_8:
1692 assert(Modifier == MCSymbolRefExpr::VK_None);
1693 Type = ELF::R_X86_64_PC64;
Jason W Kimd3443e92010-11-15 16:18:39 +00001694 break;
Rafael Espindola7a549972011-01-01 19:05:35 +00001695 case X86::reloc_signed_4byte:
Rafael Espindolac3a561c2010-12-27 02:03:24 +00001696 case X86::reloc_riprel_4byte_movq_load:
Rafael Espindola3a83c402010-12-27 00:36:05 +00001697 case FK_Data_4: // FIXME?
1698 case X86::reloc_riprel_4byte:
1699 case FK_PCRel_4:
1700 switch (Modifier) {
1701 default:
1702 llvm_unreachable("Unimplemented");
1703 case MCSymbolRefExpr::VK_None:
1704 Type = ELF::R_X86_64_PC32;
1705 break;
1706 case MCSymbolRefExpr::VK_PLT:
1707 Type = ELF::R_X86_64_PLT32;
1708 break;
1709 case MCSymbolRefExpr::VK_GOTPCREL:
1710 Type = ELF::R_X86_64_GOTPCREL;
1711 break;
1712 case MCSymbolRefExpr::VK_GOTTPOFF:
1713 Type = ELF::R_X86_64_GOTTPOFF;
Jason W Kimd3443e92010-11-15 16:18:39 +00001714 break;
Rafael Espindola3a83c402010-12-27 00:36:05 +00001715 case MCSymbolRefExpr::VK_TLSGD:
1716 Type = ELF::R_X86_64_TLSGD;
1717 break;
1718 case MCSymbolRefExpr::VK_TLSLD:
1719 Type = ELF::R_X86_64_TLSLD;
1720 break;
1721 }
Jason W Kimd3443e92010-11-15 16:18:39 +00001722 break;
Rafael Espindola3a83c402010-12-27 00:36:05 +00001723 case FK_PCRel_2:
1724 assert(Modifier == MCSymbolRefExpr::VK_None);
1725 Type = ELF::R_X86_64_PC16;
Jason W Kimd3443e92010-11-15 16:18:39 +00001726 break;
Joerg Sonnenbergerd45e8bf2011-02-21 23:25:41 +00001727 case FK_PCRel_1:
1728 assert(Modifier == MCSymbolRefExpr::VK_None);
1729 Type = ELF::R_X86_64_PC8;
1730 break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001731 }
1732 } else {
1733 switch ((unsigned)Fixup.getKind()) {
1734 default: llvm_unreachable("invalid fixup kind!");
1735 case FK_Data_8: Type = ELF::R_X86_64_64; break;
1736 case X86::reloc_signed_4byte:
Jason W Kimd3443e92010-11-15 16:18:39 +00001737 assert(isInt<32>(Target.getConstant()));
1738 switch (Modifier) {
1739 default:
1740 llvm_unreachable("Unimplemented");
1741 case MCSymbolRefExpr::VK_None:
1742 Type = ELF::R_X86_64_32S;
1743 break;
1744 case MCSymbolRefExpr::VK_GOT:
1745 Type = ELF::R_X86_64_GOT32;
1746 break;
1747 case MCSymbolRefExpr::VK_GOTPCREL:
1748 Type = ELF::R_X86_64_GOTPCREL;
1749 break;
1750 case MCSymbolRefExpr::VK_TPOFF:
1751 Type = ELF::R_X86_64_TPOFF32;
1752 break;
1753 case MCSymbolRefExpr::VK_DTPOFF:
1754 Type = ELF::R_X86_64_DTPOFF32;
1755 break;
1756 }
1757 break;
1758 case FK_Data_4:
1759 Type = ELF::R_X86_64_32;
1760 break;
1761 case FK_Data_2: Type = ELF::R_X86_64_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001762 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001763 case FK_Data_1: Type = ELF::R_X86_64_8; break;
1764 }
1765 }
1766 } else {
1767 if (IsPCRel) {
1768 switch (Modifier) {
1769 default:
1770 llvm_unreachable("Unimplemented");
1771 case MCSymbolRefExpr::VK_None:
1772 Type = ELF::R_386_PC32;
1773 break;
1774 case MCSymbolRefExpr::VK_PLT:
1775 Type = ELF::R_386_PLT32;
1776 break;
1777 }
1778 } else {
1779 switch ((unsigned)Fixup.getKind()) {
1780 default: llvm_unreachable("invalid fixup kind!");
1781
1782 case X86::reloc_global_offset_table:
1783 Type = ELF::R_386_GOTPC;
1784 break;
1785
1786 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
1787 // instead?
1788 case X86::reloc_signed_4byte:
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001789 case FK_PCRel_4:
Jason W Kimd3443e92010-11-15 16:18:39 +00001790 case FK_Data_4:
1791 switch (Modifier) {
1792 default:
1793 llvm_unreachable("Unimplemented");
1794 case MCSymbolRefExpr::VK_None:
1795 Type = ELF::R_386_32;
1796 break;
1797 case MCSymbolRefExpr::VK_GOT:
1798 Type = ELF::R_386_GOT32;
1799 break;
1800 case MCSymbolRefExpr::VK_GOTOFF:
1801 Type = ELF::R_386_GOTOFF;
1802 break;
1803 case MCSymbolRefExpr::VK_TLSGD:
1804 Type = ELF::R_386_TLS_GD;
1805 break;
1806 case MCSymbolRefExpr::VK_TPOFF:
1807 Type = ELF::R_386_TLS_LE_32;
1808 break;
1809 case MCSymbolRefExpr::VK_INDNTPOFF:
1810 Type = ELF::R_386_TLS_IE;
1811 break;
1812 case MCSymbolRefExpr::VK_NTPOFF:
1813 Type = ELF::R_386_TLS_LE;
1814 break;
1815 case MCSymbolRefExpr::VK_GOTNTPOFF:
1816 Type = ELF::R_386_TLS_GOTIE;
1817 break;
1818 case MCSymbolRefExpr::VK_TLSLDM:
1819 Type = ELF::R_386_TLS_LDM;
1820 break;
1821 case MCSymbolRefExpr::VK_DTPOFF:
1822 Type = ELF::R_386_TLS_LDO_32;
1823 break;
1824 }
1825 break;
1826 case FK_Data_2: Type = ELF::R_386_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001827 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001828 case FK_Data_1: Type = ELF::R_386_8; break;
1829 }
1830 }
1831 }
1832
1833 if (RelocNeedsGOT(Modifier))
1834 NeedsGOT = true;
1835
Jason W Kim56a39902010-12-06 21:57:34 +00001836 return Type;
Matt Fleming3565a062010-08-16 18:57:57 +00001837}