blob: 7488b1b6c2a634959b94c1a31d07f20cc3d41fd6 [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
Eli Friedman78c1e172011-03-03 07:24:36 +0000320 virtual bool
321 IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
322 const MCSymbolData &DataA,
323 const MCFragment &FB,
324 bool InSet,
325 bool IsPCRel) const;
326
Jason W Kimd3443e92010-11-15 16:18:39 +0000327 virtual void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
328 virtual void WriteSection(MCAssembler &Asm,
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000329 const SectionIndexMapTy &SectionIndexMap,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000330 uint32_t GroupSymbolIndex,
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000331 uint64_t Offset, uint64_t Size, uint64_t Alignment,
332 const MCSectionELF &Section);
Jason W Kim56a39902010-12-06 21:57:34 +0000333
334 protected:
335 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
336 bool IsPCRel, bool IsRelocWithSymbol,
337 int64_t Addend) = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000338 };
339
Jason W Kimd3443e92010-11-15 16:18:39 +0000340 //===- X86ELFObjectWriter -------------------------------------------===//
341
342 class X86ELFObjectWriter : public ELFObjectWriter {
343 public:
Rafael Espindola31f35782010-12-17 18:01:31 +0000344 X86ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +0000345 raw_ostream &_OS,
346 bool IsLittleEndian);
Wesley Peck4b047132010-11-21 22:06:28 +0000347
Jason W Kimd3443e92010-11-15 16:18:39 +0000348 virtual ~X86ELFObjectWriter();
Jason W Kim56a39902010-12-06 21:57:34 +0000349 protected:
350 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
351 bool IsPCRel, bool IsRelocWithSymbol,
352 int64_t Addend);
Jason W Kimd3443e92010-11-15 16:18:39 +0000353 };
354
355
356 //===- ARMELFObjectWriter -------------------------------------------===//
357
358 class ARMELFObjectWriter : public ELFObjectWriter {
359 public:
Jason W Kim2d7a53a2011-02-04 21:41:11 +0000360 // FIXME: MCAssembler can't yet return the Subtarget,
361 enum { DefaultEABIVersion = 0x05000000U };
362
Rafael Espindola31f35782010-12-17 18:01:31 +0000363 ARMELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +0000364 raw_ostream &_OS,
365 bool IsLittleEndian);
Wesley Peck4b047132010-11-21 22:06:28 +0000366
Jason W Kimd3443e92010-11-15 16:18:39 +0000367 virtual ~ARMELFObjectWriter();
Jason W Kim2d7a53a2011-02-04 21:41:11 +0000368
369 virtual void WriteEFlags();
Jason W Kim85fed5e2010-12-01 02:40:06 +0000370 protected:
Jason W Kim953a2a32011-02-07 01:11:15 +0000371 virtual const MCSymbol *ExplicitRelSym(const MCAssembler &Asm,
372 const MCValue &Target,
373 const MCFragment &F,
374 bool IsBSS) const;
375
Jason W Kim56a39902010-12-06 21:57:34 +0000376 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
377 bool IsPCRel, bool IsRelocWithSymbol,
378 int64_t Addend);
Jason W Kimd3443e92010-11-15 16:18:39 +0000379 };
Wesley Peck4b047132010-11-21 22:06:28 +0000380
381 //===- MBlazeELFObjectWriter -------------------------------------------===//
382
383 class MBlazeELFObjectWriter : public ELFObjectWriter {
384 public:
Rafael Espindola31f35782010-12-17 18:01:31 +0000385 MBlazeELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +0000386 raw_ostream &_OS,
387 bool IsLittleEndian);
Wesley Peck4b047132010-11-21 22:06:28 +0000388
389 virtual ~MBlazeELFObjectWriter();
Jason W Kim56a39902010-12-06 21:57:34 +0000390 protected:
391 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
392 bool IsPCRel, bool IsRelocWithSymbol,
393 int64_t Addend);
Wesley Peck4b047132010-11-21 22:06:28 +0000394 };
Matt Fleming3565a062010-08-16 18:57:57 +0000395}
396
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000397bool ELFObjectWriter::isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) {
398 const MCFixupKindInfo &FKI =
399 Asm.getBackend().getFixupKindInfo((MCFixupKind) Kind);
400
401 return FKI.Flags & MCFixupKindInfo::FKF_IsPCRel;
402}
403
404bool ELFObjectWriter::RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant) {
405 switch (Variant) {
406 default:
407 return false;
408 case MCSymbolRefExpr::VK_GOT:
409 case MCSymbolRefExpr::VK_PLT:
410 case MCSymbolRefExpr::VK_GOTPCREL:
411 case MCSymbolRefExpr::VK_TPOFF:
412 case MCSymbolRefExpr::VK_TLSGD:
413 case MCSymbolRefExpr::VK_GOTTPOFF:
414 case MCSymbolRefExpr::VK_INDNTPOFF:
415 case MCSymbolRefExpr::VK_NTPOFF:
416 case MCSymbolRefExpr::VK_GOTNTPOFF:
417 case MCSymbolRefExpr::VK_TLSLDM:
418 case MCSymbolRefExpr::VK_DTPOFF:
419 case MCSymbolRefExpr::VK_TLSLD:
420 return true;
421 }
422}
423
Jason W Kimd3443e92010-11-15 16:18:39 +0000424ELFObjectWriter::~ELFObjectWriter()
425{}
426
Matt Fleming3565a062010-08-16 18:57:57 +0000427// Emit the ELF header.
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000428void ELFObjectWriter::WriteHeader(uint64_t SectionDataSize,
429 unsigned NumberOfSections) {
Matt Fleming3565a062010-08-16 18:57:57 +0000430 // ELF Header
431 // ----------
432 //
433 // Note
434 // ----
435 // emitWord method behaves differently for ELF32 and ELF64, writing
436 // 4 bytes in the former and 8 in the latter.
437
438 Write8(0x7f); // e_ident[EI_MAG0]
439 Write8('E'); // e_ident[EI_MAG1]
440 Write8('L'); // e_ident[EI_MAG2]
441 Write8('F'); // e_ident[EI_MAG3]
442
Rafael Espindolabff66a82010-12-18 03:27:34 +0000443 Write8(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
Matt Fleming3565a062010-08-16 18:57:57 +0000444
445 // e_ident[EI_DATA]
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000446 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
Matt Fleming3565a062010-08-16 18:57:57 +0000447
448 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky5baf79e2010-09-09 17:57:50 +0000449 // e_ident[EI_OSABI]
Rafael Espindolabff66a82010-12-18 03:27:34 +0000450 switch (TargetObjectWriter->getOSType()) {
Roman Divacky5baf79e2010-09-09 17:57:50 +0000451 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
452 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
453 default: Write8(ELF::ELFOSABI_NONE); break;
454 }
Matt Fleming3565a062010-08-16 18:57:57 +0000455 Write8(0); // e_ident[EI_ABIVERSION]
456
457 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
458
459 Write16(ELF::ET_REL); // e_type
460
Rafael Espindolabff66a82010-12-18 03:27:34 +0000461 Write16(TargetObjectWriter->getEMachine()); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +0000462
463 Write32(ELF::EV_CURRENT); // e_version
464 WriteWord(0); // e_entry, no entry point in .o file
465 WriteWord(0); // e_phoff, no program header for .o
Rafael Espindolabff66a82010-12-18 03:27:34 +0000466 WriteWord(SectionDataSize + (is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
Benjamin Kramereb976772010-08-17 17:02:29 +0000467 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000468
Jason W Kim2d7a53a2011-02-04 21:41:11 +0000469 // e_flags = whatever the target wants
470 WriteEFlags();
Matt Fleming3565a062010-08-16 18:57:57 +0000471
472 // e_ehsize = ELF header size
Rafael Espindolabff66a82010-12-18 03:27:34 +0000473 Write16(is64Bit() ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
Matt Fleming3565a062010-08-16 18:57:57 +0000474
475 Write16(0); // e_phentsize = prog header entry size
476 Write16(0); // e_phnum = # prog header entries = 0
477
478 // e_shentsize = Section header entry size
Rafael Espindolabff66a82010-12-18 03:27:34 +0000479 Write16(is64Bit() ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
Matt Fleming3565a062010-08-16 18:57:57 +0000480
481 // e_shnum = # of section header ents
Rafael Espindola7be2c332010-10-31 00:16:26 +0000482 if (NumberOfSections >= ELF::SHN_LORESERVE)
483 Write16(0);
484 else
485 Write16(NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000486
487 // e_shstrndx = Section # of '.shstrtab'
Rafael Espindola7be2c332010-10-31 00:16:26 +0000488 if (NumberOfSections >= ELF::SHN_LORESERVE)
489 Write16(ELF::SHN_XINDEX);
490 else
491 Write16(ShstrtabIndex);
Matt Fleming3565a062010-08-16 18:57:57 +0000492}
493
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000494void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
495 MCDataFragment *ShndxF,
496 uint64_t name,
497 uint8_t info, uint64_t value,
498 uint64_t size, uint8_t other,
499 uint32_t shndx,
500 bool Reserved) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000501 if (ShndxF) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000502 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000503 String32(*ShndxF, shndx);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000504 else
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000505 String32(*ShndxF, 0);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000506 }
507
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000508 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
509 uint16_t(ELF::SHN_XINDEX) : shndx;
510
Rafael Espindolabff66a82010-12-18 03:27:34 +0000511 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000512 String32(*SymtabF, name); // st_name
513 String8(*SymtabF, info); // st_info
514 String8(*SymtabF, other); // st_other
515 String16(*SymtabF, Index); // st_shndx
516 String64(*SymtabF, value); // st_value
517 String64(*SymtabF, size); // st_size
Matt Fleming3565a062010-08-16 18:57:57 +0000518 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000519 String32(*SymtabF, name); // st_name
520 String32(*SymtabF, value); // st_value
521 String32(*SymtabF, size); // st_size
522 String8(*SymtabF, info); // st_info
523 String8(*SymtabF, other); // st_other
524 String16(*SymtabF, Index); // st_shndx
Matt Fleming3565a062010-08-16 18:57:57 +0000525 }
526}
527
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000528uint64_t ELFObjectWriter::SymbolValue(MCSymbolData &Data,
529 const MCAsmLayout &Layout) {
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000530 if (Data.isCommon() && Data.isExternal())
531 return Data.getCommonAlignment();
532
533 const MCSymbol &Symbol = Data.getSymbol();
Roman Divackyd1491862010-12-20 21:14:39 +0000534
535 if (Symbol.isAbsolute() && Symbol.isVariable()) {
536 if (const MCExpr *Value = Symbol.getVariableValue()) {
537 int64_t IntValue;
538 if (Value->EvaluateAsAbsolute(IntValue, Layout))
539 return (uint64_t)IntValue;
540 }
541 }
542
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000543 if (!Symbol.isInSection())
544 return 0;
545
Rafael Espindolaffd902b2010-12-06 02:57:26 +0000546 if (Data.getFragment())
547 return Layout.getSymbolOffset(&Data);
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000548
549 return 0;
550}
551
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000552void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
553 const MCAsmLayout &Layout) {
Rafael Espindola88182132010-10-27 15:18:17 +0000554 // The presence of symbol versions causes undefined symbols and
555 // versions declared with @@@ to be renamed.
556
557 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
558 ie = Asm.symbol_end(); it != ie; ++it) {
559 const MCSymbol &Alias = it->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000560 const MCSymbol &Symbol = Alias.AliasedSymbol();
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000561 MCSymbolData &SD = Asm.getSymbolData(Symbol);
562
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000563 // Not an alias.
564 if (&Symbol == &Alias)
565 continue;
566
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000567 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000568 size_t Pos = AliasName.find('@');
569 if (Pos == StringRef::npos)
570 continue;
571
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000572 // Aliases defined with .symvar copy the binding from the symbol they alias.
573 // This is the first place we are able to copy this information.
574 it->setExternal(SD.isExternal());
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000575 MCELF::SetBinding(*it, MCELF::GetBinding(SD));
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000576
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000577 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000578 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
579 continue;
580
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000581 // FIXME: produce a better error message.
582 if (Symbol.isUndefined() && Rest.startswith("@@") &&
583 !Rest.startswith("@@@"))
584 report_fatal_error("A @@ version cannot be undefined");
585
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000586 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000587 }
588}
589
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000590void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
591 MCDataFragment *ShndxF,
592 ELFSymbolData &MSD,
593 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000594 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000595 MCSymbolData &Data =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000596 Layout.getAssembler().getSymbolData(OrigData.getSymbol().AliasedSymbol());
Rafael Espindola152c1062010-10-06 21:02:29 +0000597
Rafael Espindola7be2c332010-10-31 00:16:26 +0000598 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
599 Data.getSymbol().isVariable();
600
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000601 uint8_t Binding = MCELF::GetBinding(OrigData);
602 uint8_t Visibility = MCELF::GetVisibility(OrigData);
603 uint8_t Type = MCELF::GetType(Data);
Rafael Espindola152c1062010-10-06 21:02:29 +0000604
605 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
606 uint8_t Other = Visibility;
607
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000608 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000609 uint64_t Size = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000610
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000611 assert(!(Data.isCommon() && !Data.isExternal()));
612
Rafael Espindolaf0121242010-12-22 16:03:00 +0000613 const MCExpr *ESize = Data.getSize();
614 if (ESize) {
615 int64_t Res;
616 if (!ESize->EvaluateAsAbsolute(Res, Layout))
617 report_fatal_error("Size expression must be absolute.");
618 Size = Res;
Matt Fleming3565a062010-08-16 18:57:57 +0000619 }
620
621 // Write out the symbol table entry
Rafael Espindola7be2c332010-10-31 00:16:26 +0000622 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
623 Size, Other, MSD.SectionIndex, IsReserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000624}
625
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000626void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
627 MCDataFragment *ShndxF,
628 const MCAssembler &Asm,
629 const MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000630 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000631 // The string table must be emitted first because we need the index
632 // into the string table for all the symbol names.
633 assert(StringTable.size() && "Missing string table");
634
635 // FIXME: Make sure the start of the symbol table is aligned.
636
637 // The first entry is the undefined symbol entry.
Rafael Espindola7be2c332010-10-31 00:16:26 +0000638 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
Matt Fleming3565a062010-08-16 18:57:57 +0000639
640 // Write the symbol table entries.
641 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
642 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
643 ELFSymbolData &MSD = LocalSymbolData[i];
Rafael Espindola7be2c332010-10-31 00:16:26 +0000644 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000645 }
646
Rafael Espindola71859c62010-09-16 19:46:31 +0000647 // Write out a symbol table entry for each regular section.
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000648 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
649 ++i) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000650 const MCSectionELF &Section =
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000651 static_cast<const MCSectionELF&>(i->getSection());
652 if (Section.getType() == ELF::SHT_RELA ||
653 Section.getType() == ELF::SHT_REL ||
654 Section.getType() == ELF::SHT_STRTAB ||
655 Section.getType() == ELF::SHT_SYMTAB)
Eli Friedmana44fa242010-08-16 21:17:09 +0000656 continue;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000657 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000658 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section), false);
Eli Friedmana44fa242010-08-16 21:17:09 +0000659 LastLocalSymbolIndex++;
660 }
Matt Fleming3565a062010-08-16 18:57:57 +0000661
662 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
663 ELFSymbolData &MSD = ExternalSymbolData[i];
664 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000665 assert(((Data.getFlags() & ELF_STB_Global) ||
666 (Data.getFlags() & ELF_STB_Weak)) &&
667 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000668 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000669 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000670 LastLocalSymbolIndex++;
671 }
672
673 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
674 ELFSymbolData &MSD = UndefinedSymbolData[i];
675 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000676 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000677 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000678 LastLocalSymbolIndex++;
679 }
680}
681
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000682const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm,
683 const MCValue &Target,
684 const MCFragment &F) const {
685 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000686 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
687 const MCSymbol *Renamed = Renames.lookup(&Symbol);
688 const MCSymbolData &SD = Asm.getSymbolData(Symbol);
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000689
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000690 if (ASymbol.isUndefined()) {
691 if (Renamed)
692 return Renamed;
693 return &ASymbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000694 }
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000695
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000696 if (SD.isExternal()) {
697 if (Renamed)
698 return Renamed;
699 return &Symbol;
700 }
Rafael Espindola73ffea42010-09-25 05:42:19 +0000701
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000702 const MCSectionELF &Section =
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000703 static_cast<const MCSectionELF&>(ASymbol.getSection());
Rafael Espindola25958732010-11-24 21:57:39 +0000704 const SectionKind secKind = Section.getKind();
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000705
Rafael Espindola25958732010-11-24 21:57:39 +0000706 if (secKind.isBSS())
Jason W Kim953a2a32011-02-07 01:11:15 +0000707 return ExplicitRelSym(Asm, Target, F, true);
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000708
Rafael Espindola25958732010-11-24 21:57:39 +0000709 if (secKind.isThreadLocal()) {
710 if (Renamed)
711 return Renamed;
712 return &Symbol;
713 }
714
Rafael Espindola8cecf252010-10-06 16:23:36 +0000715 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000716 const MCSectionELF &Sec2 =
717 static_cast<const MCSectionELF&>(F.getParent()->getSection());
718
Rafael Espindola8cecf252010-10-06 16:23:36 +0000719 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000720 (Kind == MCSymbolRefExpr::VK_PLT ||
721 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
Rafael Espindola25958732010-11-24 21:57:39 +0000722 Kind == MCSymbolRefExpr::VK_GOTOFF)) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000723 if (Renamed)
724 return Renamed;
725 return &Symbol;
726 }
Rafael Espindola3729d002010-10-05 23:57:26 +0000727
Rafael Espindola1c130262011-01-23 04:43:11 +0000728 if (Section.getFlags() & ELF::SHF_MERGE) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000729 if (Target.getConstant() == 0)
730 return NULL;
731 if (Renamed)
732 return Renamed;
733 return &Symbol;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000734 }
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000735
Jason W Kim953a2a32011-02-07 01:11:15 +0000736 return ExplicitRelSym(Asm, Target, F, false);
Rafael Espindola73ffea42010-09-25 05:42:19 +0000737}
738
Matt Fleming3565a062010-08-16 18:57:57 +0000739
Jason W Kim56a39902010-12-06 21:57:34 +0000740void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
741 const MCAsmLayout &Layout,
742 const MCFragment *Fragment,
743 const MCFixup &Fixup,
744 MCValue Target,
745 uint64_t &FixedValue) {
746 int64_t Addend = 0;
747 int Index = 0;
748 int64_t Value = Target.getConstant();
749 const MCSymbol *RelocSymbol = NULL;
750
Rafael Espindola127a6a42010-12-17 07:28:17 +0000751 bool IsPCRel = isFixupKindPCRel(Asm, Fixup.getKind());
Jason W Kim56a39902010-12-06 21:57:34 +0000752 if (!Target.isAbsolute()) {
753 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
754 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
755 RelocSymbol = SymbolToReloc(Asm, Target, *Fragment);
756
757 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
758 const MCSymbol &SymbolB = RefB->getSymbol();
759 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
760 IsPCRel = true;
761
762 // Offset of the symbol in the section
763 int64_t a = Layout.getSymbolOffset(&SDB);
764
765 // Ofeset of the relocation in the section
766 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
767 Value += b - a;
768 }
769
770 if (!RelocSymbol) {
771 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
772 MCFragment *F = SD.getFragment();
773
774 Index = F->getParent()->getOrdinal() + 1;
775
776 // Offset of the symbol in the section
777 Value += Layout.getSymbolOffset(&SD);
778 } else {
779 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
780 WeakrefUsedInReloc.insert(RelocSymbol);
781 else
782 UsedInReloc.insert(RelocSymbol);
783 Index = -1;
784 }
785 Addend = Value;
786 // Compensate for the addend on i386.
Rafael Espindolabff66a82010-12-18 03:27:34 +0000787 if (is64Bit())
Jason W Kim56a39902010-12-06 21:57:34 +0000788 Value = 0;
789 }
790
791 FixedValue = Value;
792 unsigned Type = GetRelocType(Target, Fixup, IsPCRel,
793 (RelocSymbol != 0), Addend);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000794
Jason W Kim56a39902010-12-06 21:57:34 +0000795 uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
796 Fixup.getOffset();
797
Rafael Espindolabff66a82010-12-18 03:27:34 +0000798 if (!hasRelocationAddend())
799 Addend = 0;
Jason W Kim56a39902010-12-06 21:57:34 +0000800 ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend);
801 Relocations[Fragment->getParent()].push_back(ERE);
802}
803
804
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000805uint64_t
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000806ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
807 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000808 MCSymbolData &SD = Asm.getSymbolData(*S);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000809 return SD.getIndex();
Matt Fleming3565a062010-08-16 18:57:57 +0000810}
811
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000812bool ELFObjectWriter::isInSymtab(const MCAssembler &Asm,
813 const MCSymbolData &Data,
814 bool Used, bool Renamed) {
Rafael Espindola484291c2010-11-01 14:28:48 +0000815 if (Data.getFlags() & ELF_Other_Weakref)
816 return false;
817
Rafael Espindolabd701182010-10-19 19:31:37 +0000818 if (Used)
819 return true;
820
Rafael Espindola88182132010-10-27 15:18:17 +0000821 if (Renamed)
822 return false;
823
Rafael Espindola737cd212010-10-05 18:01:23 +0000824 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000825
Rafael Espindolad1798862010-10-29 23:09:31 +0000826 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
827 return true;
828
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000829 const MCSymbol &A = Symbol.AliasedSymbol();
Rafael Espindola21451e52011-02-23 20:22:07 +0000830 if (Symbol.isVariable() && !A.isVariable() && A.isUndefined())
831 return false;
832
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000833 bool IsGlobal = MCELF::GetBinding(Data) == ELF::STB_GLOBAL;
Rafael Espindola21451e52011-02-23 20:22:07 +0000834 if (!Symbol.isVariable() && Symbol.isUndefined() && !IsGlobal)
Rafael Espindolaa6866962010-10-27 14:44:52 +0000835 return false;
836
Rafael Espindola737cd212010-10-05 18:01:23 +0000837 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
838 return false;
839
Rafael Espindolabd701182010-10-19 19:31:37 +0000840 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000841 return false;
842
843 return true;
844}
845
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000846bool ELFObjectWriter::isLocal(const MCSymbolData &Data, bool isSignature,
847 bool isUsedInReloc) {
Rafael Espindola737cd212010-10-05 18:01:23 +0000848 if (Data.isExternal())
849 return false;
850
851 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000852 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000853
854 if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
855 if (isSignature && !isUsedInReloc)
856 return true;
857
Rafael Espindola737cd212010-10-05 18:01:23 +0000858 return false;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000859 }
Rafael Espindola737cd212010-10-05 18:01:23 +0000860
861 return true;
862}
863
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000864void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
865 SectionIndexMapTy &SectionIndexMap) {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000866 unsigned Index = 1;
867 for (MCAssembler::iterator it = Asm.begin(),
868 ie = Asm.end(); it != ie; ++it) {
869 const MCSectionELF &Section =
870 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000871 if (Section.getType() != ELF::SHT_GROUP)
872 continue;
873 SectionIndexMap[&Section] = Index++;
874 }
875
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());
880 if (Section.getType() == ELF::SHT_GROUP)
881 continue;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000882 SectionIndexMap[&Section] = Index++;
883 }
884}
885
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000886void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000887 const SectionIndexMapTy &SectionIndexMap,
888 RevGroupMapTy RevGroupMap) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000889 // FIXME: Is this the correct place to do this?
890 if (NeedsGOT) {
891 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
892 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
893 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
894 Data.setExternal(true);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000895 MCELF::SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindola5c77c162010-10-05 15:48:37 +0000896 }
897
Matt Fleming3565a062010-08-16 18:57:57 +0000898 // Build section lookup table.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000899 int NumRegularSections = Asm.size();
Matt Fleming3565a062010-08-16 18:57:57 +0000900
901 // Index 0 is always the empty string.
902 StringMap<uint64_t> StringIndexMap;
903 StringTable += '\x00';
904
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000905 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000906 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
907 ie = Asm.symbol_end(); it != ie; ++it) {
908 const MCSymbol &Symbol = it->getSymbol();
909
Rafael Espindola484291c2010-11-01 14:28:48 +0000910 bool Used = UsedInReloc.count(&Symbol);
911 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000912 bool isSignature = RevGroupMap.count(&Symbol);
913
914 if (!isInSymtab(Asm, *it,
915 Used || WeakrefUsed || isSignature,
Rafael Espindola88182132010-10-27 15:18:17 +0000916 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000917 continue;
918
Matt Fleming3565a062010-08-16 18:57:57 +0000919 ELFSymbolData MSD;
920 MSD.SymbolData = it;
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000921 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Matt Fleming3565a062010-08-16 18:57:57 +0000922
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000923 // Undefined symbols are global, but this is the first place we
924 // are able to set it.
925 bool Local = isLocal(*it, isSignature, Used);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000926 if (!Local && MCELF::GetBinding(*it) == ELF::STB_LOCAL) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000927 MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000928 MCELF::SetBinding(*it, ELF::STB_GLOBAL);
929 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000930 }
931
Rafael Espindola484291c2010-11-01 14:28:48 +0000932 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
Jan Sjödin2ddfd952011-02-28 21:45:04 +0000933 MCELF::SetBinding(*it, ELF::STB_WEAK);
Rafael Espindola484291c2010-11-01 14:28:48 +0000934
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000935 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000936 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000937 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000938 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000939 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000940 } else if (RefSymbol.isUndefined()) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000941 if (isSignature && !Used)
942 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
943 else
944 MSD.SectionIndex = ELF::SHN_UNDEF;
Matt Fleming3565a062010-08-16 18:57:57 +0000945 } else {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000946 const MCSectionELF &Section =
947 static_cast<const MCSectionELF&>(RefSymbol.getSection());
948 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000949 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
950 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000951 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000952 }
953
Rafael Espindola88182132010-10-27 15:18:17 +0000954 // The @@@ in symbol version is replaced with @ in undefined symbols and
955 // @@ in defined ones.
956 StringRef Name = Symbol.getName();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000957 SmallString<32> Buf;
958
Rafael Espindola88182132010-10-27 15:18:17 +0000959 size_t Pos = Name.find("@@@");
Rafael Espindola88182132010-10-27 15:18:17 +0000960 if (Pos != StringRef::npos) {
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000961 Buf += Name.substr(0, Pos);
962 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
963 Buf += Name.substr(Pos + Skip);
964 Name = Buf;
Rafael Espindola88182132010-10-27 15:18:17 +0000965 }
966
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000967 uint64_t &Entry = StringIndexMap[Name];
Rafael Espindolaa6866962010-10-27 14:44:52 +0000968 if (!Entry) {
969 Entry = StringTable.size();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000970 StringTable += Name;
Rafael Espindolaa6866962010-10-27 14:44:52 +0000971 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +0000972 }
Rafael Espindolaa6866962010-10-27 14:44:52 +0000973 MSD.StringIndex = Entry;
974 if (MSD.SectionIndex == ELF::SHN_UNDEF)
975 UndefinedSymbolData.push_back(MSD);
976 else if (Local)
977 LocalSymbolData.push_back(MSD);
978 else
979 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +0000980 }
981
982 // Symbols are required to be in lexicographic order.
983 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
984 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
985 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
986
987 // Set the symbol indices. Local symbols must come before all other
988 // symbols with non-local bindings.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000989 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000990 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
991 LocalSymbolData[i].SymbolData->setIndex(Index++);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000992
993 Index += NumRegularSections;
994
Matt Fleming3565a062010-08-16 18:57:57 +0000995 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
996 ExternalSymbolData[i].SymbolData->setIndex(Index++);
997 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
998 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
999}
1000
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001001void ELFObjectWriter::WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
1002 const MCSectionData &SD) {
Matt Fleming3565a062010-08-16 18:57:57 +00001003 if (!Relocations[&SD].empty()) {
1004 MCContext &Ctx = Asm.getContext();
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001005 const MCSectionELF *RelaSection;
Matt Fleming3565a062010-08-16 18:57:57 +00001006 const MCSectionELF &Section =
1007 static_cast<const MCSectionELF&>(SD.getSection());
1008
1009 const StringRef SectionName = Section.getSectionName();
Rafael Espindolabff66a82010-12-18 03:27:34 +00001010 std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +00001011 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +00001012
1013 unsigned EntrySize;
Rafael Espindolabff66a82010-12-18 03:27:34 +00001014 if (hasRelocationAddend())
1015 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
Benjamin Kramer299fbe32010-08-17 17:56:13 +00001016 else
Rafael Espindolabff66a82010-12-18 03:27:34 +00001017 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +00001018
Rafael Espindolabff66a82010-12-18 03:27:34 +00001019 RelaSection = Ctx.getELFSection(RelaSectionName, hasRelocationAddend() ?
Benjamin Kramer377a5722010-08-17 17:30:07 +00001020 ELF::SHT_RELA : ELF::SHT_REL, 0,
Matt Fleming3565a062010-08-16 18:57:57 +00001021 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001022 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001023
1024 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +00001025 RelaSD.setAlignment(is64Bit() ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +00001026
1027 MCDataFragment *F = new MCDataFragment(&RelaSD);
1028
1029 WriteRelocationsFragment(Asm, F, &SD);
Matt Fleming3565a062010-08-16 18:57:57 +00001030 }
1031}
1032
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001033void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
1034 uint64_t Flags, uint64_t Address,
1035 uint64_t Offset, uint64_t Size,
1036 uint32_t Link, uint32_t Info,
1037 uint64_t Alignment,
1038 uint64_t EntrySize) {
Matt Fleming3565a062010-08-16 18:57:57 +00001039 Write32(Name); // sh_name: index into string table
1040 Write32(Type); // sh_type
1041 WriteWord(Flags); // sh_flags
1042 WriteWord(Address); // sh_addr
1043 WriteWord(Offset); // sh_offset
1044 WriteWord(Size); // sh_size
1045 Write32(Link); // sh_link
1046 Write32(Info); // sh_info
1047 WriteWord(Alignment); // sh_addralign
1048 WriteWord(EntrySize); // sh_entsize
1049}
1050
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001051void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
1052 MCDataFragment *F,
1053 const MCSectionData *SD) {
Matt Fleming3565a062010-08-16 18:57:57 +00001054 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
1055 // sort by the r_offset just like gnu as does
1056 array_pod_sort(Relocs.begin(), Relocs.end());
1057
1058 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1059 ELFRelocationEntry entry = Relocs[e - i - 1];
1060
Rafael Espindola12203cc2010-11-21 00:48:25 +00001061 if (!entry.Index)
1062 ;
1063 else if (entry.Index < 0)
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001064 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
1065 else
Rafael Espindola12203cc2010-11-21 00:48:25 +00001066 entry.Index += LocalSymbolData.size();
Rafael Espindolabff66a82010-12-18 03:27:34 +00001067 if (is64Bit()) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001068 String64(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001069
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001070 struct ELF::Elf64_Rela ERE64;
1071 ERE64.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001072 String64(*F, ERE64.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001073
Rafael Espindolabff66a82010-12-18 03:27:34 +00001074 if (hasRelocationAddend())
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001075 String64(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001076 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001077 String32(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001078
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001079 struct ELF::Elf32_Rela ERE32;
1080 ERE32.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001081 String32(*F, ERE32.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 String32(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001085 }
Matt Fleming3565a062010-08-16 18:57:57 +00001086 }
1087}
1088
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001089void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
1090 MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001091 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +00001092 MCContext &Ctx = Asm.getContext();
1093 MCDataFragment *F;
1094
Rafael Espindolabff66a82010-12-18 03:27:34 +00001095 unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
Matt Fleming3565a062010-08-16 18:57:57 +00001096
Rafael Espindola38738bf2010-09-22 19:04:41 +00001097 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001098 const MCSectionELF *ShstrtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001099 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001100 SectionKind::getReadOnly());
Rafael Espindola71859c62010-09-16 19:46:31 +00001101 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1102 ShstrtabSD.setAlignment(1);
1103 ShstrtabIndex = Asm.size();
1104
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001105 const MCSectionELF *SymtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001106 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1107 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001108 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001109 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Rafael Espindolabff66a82010-12-18 03:27:34 +00001110 SymtabSD.setAlignment(is64Bit() ? 8 : 4);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001111 SymbolTableIndex = Asm.size();
1112
1113 MCSectionData *SymtabShndxSD = NULL;
1114
1115 if (NeedsSymtabShndx) {
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001116 const MCSectionELF *SymtabShndxSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001117 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001118 SectionKind::getReadOnly(), 4, "");
Rafael Espindola7be2c332010-10-31 00:16:26 +00001119 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
1120 SymtabShndxSD->setAlignment(4);
1121 }
Matt Fleming3565a062010-08-16 18:57:57 +00001122
Matt Fleming3565a062010-08-16 18:57:57 +00001123 const MCSection *StrtabSection;
1124 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001125 SectionKind::getReadOnly());
Matt Fleming3565a062010-08-16 18:57:57 +00001126 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1127 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +00001128 StringTableIndex = Asm.size();
1129
Rafael Espindolac3c413f2010-09-27 22:04:54 +00001130 WriteRelocations(Asm, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001131
1132 // Symbol table
1133 F = new MCDataFragment(&SymtabSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001134 MCDataFragment *ShndxF = NULL;
1135 if (NeedsSymtabShndx) {
1136 ShndxF = new MCDataFragment(SymtabShndxSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001137 }
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001138 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
Rafael Espindola71859c62010-09-16 19:46:31 +00001139
Matt Fleming3565a062010-08-16 18:57:57 +00001140 F = new MCDataFragment(&StrtabSD);
1141 F->getContents().append(StringTable.begin(), StringTable.end());
Matt Fleming3565a062010-08-16 18:57:57 +00001142
Matt Fleming3565a062010-08-16 18:57:57 +00001143 F = new MCDataFragment(&ShstrtabSD);
1144
Matt Fleming3565a062010-08-16 18:57:57 +00001145 // Section header string table.
1146 //
1147 // The first entry of a string table holds a null character so skip
1148 // section 0.
1149 uint64_t Index = 1;
1150 F->getContents() += '\x00';
1151
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001152 StringMap<uint64_t> SecStringMap;
Matt Fleming3565a062010-08-16 18:57:57 +00001153 for (MCAssembler::const_iterator it = Asm.begin(),
1154 ie = Asm.end(); it != ie; ++it) {
Matt Fleming3565a062010-08-16 18:57:57 +00001155 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +00001156 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola51efe7a2010-09-23 14:14:56 +00001157 // FIXME: We could merge suffixes like in .text and .rela.text.
Matt Fleming3565a062010-08-16 18:57:57 +00001158
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001159 StringRef Name = Section.getSectionName();
1160 if (SecStringMap.count(Name)) {
1161 SectionStringTableIndex[&Section] = SecStringMap[Name];
1162 continue;
1163 }
Matt Fleming3565a062010-08-16 18:57:57 +00001164 // Remember the index into the string table so we can write it
1165 // into the sh_name field of the section header table.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001166 SectionStringTableIndex[&Section] = Index;
1167 SecStringMap[Name] = Index;
Matt Fleming3565a062010-08-16 18:57:57 +00001168
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001169 Index += Name.size() + 1;
1170 F->getContents() += Name;
Matt Fleming3565a062010-08-16 18:57:57 +00001171 F->getContents() += '\x00';
1172 }
Rafael Espindola70703872010-09-30 02:22:20 +00001173}
1174
Rafael Espindola96aa78c2011-01-23 17:55:27 +00001175void ELFObjectWriter::CreateIndexedSections(MCAssembler &Asm,
1176 MCAsmLayout &Layout,
1177 GroupMapTy &GroupMap,
1178 RevGroupMapTy &RevGroupMap) {
1179 // Create the .note.GNU-stack section if needed.
1180 MCContext &Ctx = Asm.getContext();
1181 if (Asm.getNoExecStack()) {
1182 const MCSectionELF *GnuStackSection =
1183 Ctx.getELFSection(".note.GNU-stack", ELF::SHT_PROGBITS, 0,
1184 SectionKind::getReadOnly());
1185 Asm.getOrCreateSectionData(*GnuStackSection);
1186 }
1187
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001188 // Build the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001189 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1190 it != ie; ++it) {
1191 const MCSectionELF &Section =
1192 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola1c130262011-01-23 04:43:11 +00001193 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001194 continue;
1195
1196 const MCSymbol *SignatureSymbol = Section.getGroup();
1197 Asm.getOrCreateSymbolData(*SignatureSymbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001198 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001199 if (!Group) {
Rafael Espindola96aa78c2011-01-23 17:55:27 +00001200 Group = Ctx.CreateELFGroupSection();
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001201 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1202 Data.setAlignment(4);
1203 MCDataFragment *F = new MCDataFragment(&Data);
1204 String32(*F, ELF::GRP_COMDAT);
1205 }
1206 GroupMap[Group] = SignatureSymbol;
1207 }
1208
1209 // Add sections to the groups
1210 unsigned Index = 1;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001211 unsigned NumGroups = RevGroupMap.size();
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001212 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1213 it != ie; ++it, ++Index) {
1214 const MCSectionELF &Section =
1215 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola1c130262011-01-23 04:43:11 +00001216 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001217 continue;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001218 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001219 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1220 // FIXME: we could use the previous fragment
1221 MCDataFragment *F = new MCDataFragment(&Data);
1222 String32(*F, NumGroups + Index);
1223 }
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001224}
1225
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001226void ELFObjectWriter::WriteSection(MCAssembler &Asm,
1227 const SectionIndexMapTy &SectionIndexMap,
1228 uint32_t GroupSymbolIndex,
1229 uint64_t Offset, uint64_t Size,
1230 uint64_t Alignment,
1231 const MCSectionELF &Section) {
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001232 uint64_t sh_link = 0;
1233 uint64_t sh_info = 0;
1234
1235 switch(Section.getType()) {
1236 case ELF::SHT_DYNAMIC:
1237 sh_link = SectionStringTableIndex[&Section];
1238 sh_info = 0;
1239 break;
1240
1241 case ELF::SHT_REL:
1242 case ELF::SHT_RELA: {
1243 const MCSectionELF *SymtabSection;
1244 const MCSectionELF *InfoSection;
1245 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
1246 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001247 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001248 sh_link = SectionIndexMap.lookup(SymtabSection);
1249 assert(sh_link && ".symtab not found");
1250
1251 // Remove ".rel" and ".rela" prefixes.
1252 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1253 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
1254
1255 InfoSection = Asm.getContext().getELFSection(SectionName,
1256 ELF::SHT_PROGBITS, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001257 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001258 sh_info = SectionIndexMap.lookup(InfoSection);
1259 break;
1260 }
1261
1262 case ELF::SHT_SYMTAB:
1263 case ELF::SHT_DYNSYM:
1264 sh_link = StringTableIndex;
1265 sh_info = LastLocalSymbolIndex;
1266 break;
1267
1268 case ELF::SHT_SYMTAB_SHNDX:
1269 sh_link = SymbolTableIndex;
1270 break;
1271
1272 case ELF::SHT_PROGBITS:
1273 case ELF::SHT_STRTAB:
1274 case ELF::SHT_NOBITS:
Rafael Espindola98976612010-12-26 21:30:59 +00001275 case ELF::SHT_NOTE:
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001276 case ELF::SHT_NULL:
1277 case ELF::SHT_ARM_ATTRIBUTES:
Jason W Kim86a97f22011-01-12 00:19:25 +00001278 case ELF::SHT_INIT_ARRAY:
1279 case ELF::SHT_FINI_ARRAY:
1280 case ELF::SHT_PREINIT_ARRAY:
Rafael Espindola0cf5e3d2011-01-23 05:43:40 +00001281 case ELF::SHT_X86_64_UNWIND:
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001282 // Nothing to do.
1283 break;
1284
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001285 case ELF::SHT_GROUP: {
1286 sh_link = SymbolTableIndex;
1287 sh_info = GroupSymbolIndex;
1288 break;
1289 }
1290
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001291 default:
1292 assert(0 && "FIXME: sh_type value not supported!");
1293 break;
1294 }
1295
1296 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1297 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1298 Alignment, Section.getEntrySize());
1299}
1300
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001301bool ELFObjectWriter::IsELFMetaDataSection(const MCSectionData &SD) {
Rafael Espindolaf8803fe2010-12-06 03:48:09 +00001302 return SD.getOrdinal() == ~UINT32_C(0) &&
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001303 !SD.getSection().isVirtualSection();
1304}
1305
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001306uint64_t ELFObjectWriter::DataSectionSize(const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001307 uint64_t Ret = 0;
1308 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1309 ++i) {
1310 const MCFragment &F = *i;
1311 assert(F.getKind() == MCFragment::FT_Data);
1312 Ret += cast<MCDataFragment>(F).getContents().size();
1313 }
1314 return Ret;
1315}
1316
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001317uint64_t ELFObjectWriter::GetSectionFileSize(const MCAsmLayout &Layout,
1318 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001319 if (IsELFMetaDataSection(SD))
1320 return DataSectionSize(SD);
1321 return Layout.getSectionFileSize(&SD);
1322}
1323
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001324uint64_t ELFObjectWriter::GetSectionAddressSize(const MCAsmLayout &Layout,
1325 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001326 if (IsELFMetaDataSection(SD))
1327 return DataSectionSize(SD);
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001328 return Layout.getSectionAddressSize(&SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001329}
1330
Jan Sjödin2ddfd952011-02-28 21:45:04 +00001331void ELFObjectWriter::WriteDataSectionData(ELFObjectWriter *W,
1332 const MCSectionData &SD) {
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001333 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1334 ++i) {
1335 const MCFragment &F = *i;
1336 assert(F.getKind() == MCFragment::FT_Data);
1337 W->WriteBytes(cast<MCDataFragment>(F).getContents().str());
1338 }
1339}
1340
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001341void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1342 const MCAsmLayout &Layout) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001343 GroupMapTy GroupMap;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001344 RevGroupMapTy RevGroupMap;
Rafael Espindola96aa78c2011-01-23 17:55:27 +00001345 CreateIndexedSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1346 RevGroupMap);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001347
Rafael Espindolabab2a802010-11-10 21:51:05 +00001348 SectionIndexMapTy SectionIndexMap;
1349
1350 ComputeIndexMap(Asm, SectionIndexMap);
1351
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001352 // Compute symbol table information.
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001353 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap);
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001354
Matt Fleming3565a062010-08-16 18:57:57 +00001355 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001356 const_cast<MCAsmLayout&>(Layout),
1357 SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +00001358
Rafael Espindola1d739a02010-11-10 22:34:07 +00001359 // Update to include the metadata sections.
1360 ComputeIndexMap(Asm, SectionIndexMap);
1361
Matt Fleming3565a062010-08-16 18:57:57 +00001362 // Add 1 for the null section.
1363 unsigned NumSections = Asm.size() + 1;
Rafael Espindolabff66a82010-12-18 03:27:34 +00001364 uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
1365 uint64_t HeaderSize = is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
1366 sizeof(ELF::Elf32_Ehdr);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001367 uint64_t FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001368
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001369 std::vector<const MCSectionELF*> Sections;
1370 Sections.resize(NumSections);
1371
1372 for (SectionIndexMapTy::const_iterator i=
1373 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1374 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
1375 Sections[p.second] = p.first;
1376 }
1377
1378 for (unsigned i = 1; i < NumSections; ++i) {
1379 const MCSectionELF &Section = *Sections[i];
1380 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001381
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001382 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1383
Matt Fleming3565a062010-08-16 18:57:57 +00001384 // Get the size of the section in the output file (including padding).
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001385 FileOff += GetSectionFileSize(Layout, SD);
Matt Fleming3565a062010-08-16 18:57:57 +00001386 }
1387
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001388 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1389
Matt Fleming3565a062010-08-16 18:57:57 +00001390 // Write out the ELF header ...
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001391 WriteHeader(FileOff - HeaderSize, NumSections);
1392
1393 FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001394
1395 // ... then all of the sections ...
1396 DenseMap<const MCSection*, uint64_t> SectionOffsetMap;
1397
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001398 for (unsigned i = 1; i < NumSections; ++i) {
1399 const MCSectionELF &Section = *Sections[i];
1400 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001401
1402 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1403 WriteZeros(Padding);
1404 FileOff += Padding;
1405
Matt Fleming3565a062010-08-16 18:57:57 +00001406 // Remember the offset into the file for this section.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001407 SectionOffsetMap[&Section] = FileOff;
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001408
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001409 FileOff += GetSectionFileSize(Layout, SD);
Matt Fleming3565a062010-08-16 18:57:57 +00001410
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001411 if (IsELFMetaDataSection(SD))
1412 WriteDataSectionData(this, SD);
1413 else
Daniel Dunbar5d2477c2010-12-17 02:45:59 +00001414 Asm.WriteSectionData(&SD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +00001415 }
1416
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001417 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1418 WriteZeros(Padding);
1419 FileOff += Padding;
1420
Matt Fleming3565a062010-08-16 18:57:57 +00001421 // ... and then the section header table.
1422 // Should we align the section header table?
1423 //
1424 // Null section first.
Rafael Espindola7be2c332010-10-31 00:16:26 +00001425 uint64_t FirstSectionSize =
1426 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1427 uint32_t FirstSectionLink =
1428 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1429 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming3565a062010-08-16 18:57:57 +00001430
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001431 for (unsigned i = 1; i < NumSections; ++i) {
1432 const MCSectionELF &Section = *Sections[i];
1433 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1434 uint32_t GroupSymbolIndex;
1435 if (Section.getType() != ELF::SHT_GROUP)
1436 GroupSymbolIndex = 0;
1437 else
1438 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm, GroupMap[&Section]);
Matt Fleming3565a062010-08-16 18:57:57 +00001439
Rafael Espindola85f2ecc2010-12-07 00:27:36 +00001440 uint64_t Size = GetSectionAddressSize(Layout, SD);
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001441
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001442 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
Rafael Espindola6db8a9f2010-12-02 03:09:06 +00001443 SectionOffsetMap[&Section], Size,
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001444 SD.getAlignment(), Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001445 }
1446}
1447
Eli Friedman78c1e172011-03-03 07:24:36 +00001448bool
1449ELFObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
1450 const MCSymbolData &DataA,
1451 const MCFragment &FB,
1452 bool InSet,
1453 bool IsPCRel) const {
1454 if (DataA.getFlags() & ELF_STB_Weak)
1455 return false;
1456 return MCObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(
1457 Asm, DataA, FB,InSet, IsPCRel);
1458}
1459
Rafael Espindola6024c972010-12-17 17:45:22 +00001460MCObjectWriter *llvm::createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1461 raw_ostream &OS,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001462 bool IsLittleEndian) {
1463 switch (MOTW->getEMachine()) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001464 case ELF::EM_386:
1465 case ELF::EM_X86_64:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001466 return new X86ELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001467 case ELF::EM_ARM:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001468 return new ARMELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Wesley Peck4b047132010-11-21 22:06:28 +00001469 case ELF::EM_MBLAZE:
Rafael Espindolabff66a82010-12-18 03:27:34 +00001470 return new MBlazeELFObjectWriter(MOTW, OS, IsLittleEndian); break;
Benjamin Kramer32858772010-11-15 19:20:50 +00001471 default: llvm_unreachable("Unsupported architecture"); break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001472 }
1473}
1474
1475
1476/// START OF SUBCLASSES for ELFObjectWriter
1477//===- ARMELFObjectWriter -------------------------------------------===//
1478
Rafael Espindola31f35782010-12-17 18:01:31 +00001479ARMELFObjectWriter::ARMELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001480 raw_ostream &_OS,
1481 bool IsLittleEndian)
1482 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
Jason W Kimd3443e92010-11-15 16:18:39 +00001483{}
1484
1485ARMELFObjectWriter::~ARMELFObjectWriter()
1486{}
1487
Jason W Kim2d7a53a2011-02-04 21:41:11 +00001488// FIXME: get the real EABI Version from the Triple.
1489void ARMELFObjectWriter::WriteEFlags() {
1490 Write32(ELF::EF_ARM_EABIMASK & DefaultEABIVersion);
1491}
1492
Jason W Kim953a2a32011-02-07 01:11:15 +00001493// In ARM, _MergedGlobals and other most symbols get emitted directly.
1494// I.e. not as an offset to a section symbol.
1495// This code is a first-cut approximation of what ARM/gcc does.
1496
1497const MCSymbol *ARMELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
1498 const MCValue &Target,
1499 const MCFragment &F,
1500 bool IsBSS) const {
1501 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
1502 bool EmitThisSym = false;
1503
1504 if (IsBSS) {
1505 EmitThisSym = StringSwitch<bool>(Symbol.getName())
1506 .Case("_MergedGlobals", true)
1507 .Default(false);
1508 } else {
1509 EmitThisSym = StringSwitch<bool>(Symbol.getName())
1510 .Case("_MergedGlobals", true)
1511 .StartsWith(".L.str", true)
1512 .Default(false);
1513 }
1514 if (EmitThisSym)
1515 return &Symbol;
1516 if (! Symbol.isTemporary())
1517 return &Symbol;
1518 return NULL;
1519}
1520
Jason W Kim85fed5e2010-12-01 02:40:06 +00001521unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target,
1522 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001523 bool IsPCRel,
1524 bool IsRelocWithSymbol,
1525 int64_t Addend) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001526 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1527 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
1528
Jason W Kima0871e72010-12-08 23:14:44 +00001529 unsigned Type = 0;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001530 if (IsPCRel) {
Jason W Kim85fed5e2010-12-01 02:40:06 +00001531 switch ((unsigned)Fixup.getKind()) {
1532 default: assert(0 && "Unimplemented");
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001533 case FK_Data_4:
1534 switch (Modifier) {
1535 default: llvm_unreachable("Unsupported Modifier");
1536 case MCSymbolRefExpr::VK_None:
Jason W Kim1d866172011-01-13 00:07:51 +00001537 Type = ELF::R_ARM_BASE_PREL;
1538 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001539 case MCSymbolRefExpr::VK_ARM_TLSGD:
Jason W Kim1d866172011-01-13 00:07:51 +00001540 assert(0 && "unimplemented");
1541 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001542 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
1543 Type = ELF::R_ARM_TLS_IE32;
Jason W Kim1d866172011-01-13 00:07:51 +00001544 break;
1545 }
1546 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001547 case ARM::fixup_arm_uncondbranch:
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001548 switch (Modifier) {
1549 case MCSymbolRefExpr::VK_ARM_PLT:
Jason W Kim1d866172011-01-13 00:07:51 +00001550 Type = ELF::R_ARM_PLT32;
1551 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001552 default:
Jason W Kim1d866172011-01-13 00:07:51 +00001553 Type = ELF::R_ARM_CALL;
1554 break;
1555 }
1556 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001557 case ARM::fixup_arm_condbranch:
1558 Type = ELF::R_ARM_JUMP24;
1559 break;
Jason W Kim86a97f22011-01-12 00:19:25 +00001560 case ARM::fixup_arm_movt_hi16:
1561 case ARM::fixup_arm_movt_hi16_pcrel:
Jason W Kim1d866172011-01-13 00:07:51 +00001562 Type = ELF::R_ARM_MOVT_PREL;
1563 break;
Jason W Kim86a97f22011-01-12 00:19:25 +00001564 case ARM::fixup_arm_movw_lo16:
1565 case ARM::fixup_arm_movw_lo16_pcrel:
Jason W Kim1d866172011-01-13 00:07:51 +00001566 Type = ELF::R_ARM_MOVW_PREL_NC;
1567 break;
Evan Chengf3eb3bb2011-01-14 02:38:49 +00001568 case ARM::fixup_t2_movt_hi16:
1569 case ARM::fixup_t2_movt_hi16_pcrel:
1570 Type = ELF::R_ARM_THM_MOVT_PREL;
1571 break;
1572 case ARM::fixup_t2_movw_lo16:
1573 case ARM::fixup_t2_movw_lo16_pcrel:
1574 Type = ELF::R_ARM_THM_MOVW_PREL_NC;
1575 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001576 }
1577 } else {
1578 switch ((unsigned)Fixup.getKind()) {
1579 default: llvm_unreachable("invalid fixup kind!");
Jason W Kima0871e72010-12-08 23:14:44 +00001580 case FK_Data_4:
1581 switch (Modifier) {
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001582 default: llvm_unreachable("Unsupported Modifier"); break;
1583 case MCSymbolRefExpr::VK_ARM_GOT:
Jason W Kim1d866172011-01-13 00:07:51 +00001584 Type = ELF::R_ARM_GOT_BREL;
1585 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001586 case MCSymbolRefExpr::VK_ARM_TLSGD:
Jason W Kim1d866172011-01-13 00:07:51 +00001587 Type = ELF::R_ARM_TLS_GD32;
1588 break;
Jason W Kimf13743b2010-12-16 03:12:17 +00001589 case MCSymbolRefExpr::VK_ARM_TPOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001590 Type = ELF::R_ARM_TLS_LE32;
1591 break;
Jason W Kima0871e72010-12-08 23:14:44 +00001592 case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001593 Type = ELF::R_ARM_TLS_IE32;
1594 break;
Jason W Kimf13743b2010-12-16 03:12:17 +00001595 case MCSymbolRefExpr::VK_None:
Jason W Kim1d866172011-01-13 00:07:51 +00001596 Type = ELF::R_ARM_ABS32;
1597 break;
Jason W Kim3fa4c1d2010-12-13 23:16:07 +00001598 case MCSymbolRefExpr::VK_ARM_GOTOFF:
Jason W Kim1d866172011-01-13 00:07:51 +00001599 Type = ELF::R_ARM_GOTOFF32;
1600 break;
1601 }
1602 break;
Jim Grosbachdff84b02010-12-02 00:28:45 +00001603 case ARM::fixup_arm_ldst_pcrel_12:
Owen Anderson9d63d902010-12-01 19:18:46 +00001604 case ARM::fixup_arm_pcrel_10:
Jim Grosbachdff84b02010-12-02 00:28:45 +00001605 case ARM::fixup_arm_adr_pcrel_12:
Jim Grosbach662a8162010-12-06 23:57:07 +00001606 case ARM::fixup_arm_thumb_bl:
Jim Grosbachb492a7c2010-12-09 19:50:12 +00001607 case ARM::fixup_arm_thumb_cb:
Bill Wendlingb8958b02010-12-08 01:57:09 +00001608 case ARM::fixup_arm_thumb_cp:
Jim Grosbache2467172010-12-10 18:21:33 +00001609 case ARM::fixup_arm_thumb_br:
Jason W Kim1d866172011-01-13 00:07:51 +00001610 assert(0 && "Unimplemented");
1611 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001612 case ARM::fixup_arm_uncondbranch:
Jason W Kim1d866172011-01-13 00:07:51 +00001613 Type = ELF::R_ARM_CALL;
1614 break;
Jason W Kim685c3502011-02-04 19:47:15 +00001615 case ARM::fixup_arm_condbranch:
1616 Type = ELF::R_ARM_JUMP24;
1617 break;
Jason W Kim1d866172011-01-13 00:07:51 +00001618 case ARM::fixup_arm_movt_hi16:
1619 Type = ELF::R_ARM_MOVT_ABS;
1620 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001621 case ARM::fixup_arm_movw_lo16:
Jason W Kim1d866172011-01-13 00:07:51 +00001622 Type = ELF::R_ARM_MOVW_ABS_NC;
1623 break;
Evan Chengf3eb3bb2011-01-14 02:38:49 +00001624 case ARM::fixup_t2_movt_hi16:
1625 Type = ELF::R_ARM_THM_MOVT_ABS;
1626 break;
1627 case ARM::fixup_t2_movw_lo16:
1628 Type = ELF::R_ARM_THM_MOVW_ABS_NC;
1629 break;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001630 }
1631 }
1632
1633 if (RelocNeedsGOT(Modifier))
1634 NeedsGOT = true;
Jason W Kim953a2a32011-02-07 01:11:15 +00001635
Jason W Kima0871e72010-12-08 23:14:44 +00001636 return Type;
Jason W Kim85fed5e2010-12-01 02:40:06 +00001637}
1638
Wesley Peck4b047132010-11-21 22:06:28 +00001639//===- MBlazeELFObjectWriter -------------------------------------------===//
Jason W Kimd3443e92010-11-15 16:18:39 +00001640
Rafael Espindola31f35782010-12-17 18:01:31 +00001641MBlazeELFObjectWriter::MBlazeELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001642 raw_ostream &_OS,
1643 bool IsLittleEndian)
1644 : ELFObjectWriter(MOTW, _OS, IsLittleEndian) {
Wesley Peck4b047132010-11-21 22:06:28 +00001645}
1646
1647MBlazeELFObjectWriter::~MBlazeELFObjectWriter() {
1648}
1649
Jason W Kim56a39902010-12-06 21:57:34 +00001650unsigned MBlazeELFObjectWriter::GetRelocType(const MCValue &Target,
Wesley Peck4b047132010-11-21 22:06:28 +00001651 const MCFixup &Fixup,
Jason W Kim56a39902010-12-06 21:57:34 +00001652 bool IsPCRel,
1653 bool IsRelocWithSymbol,
1654 int64_t Addend) {
Wesley Peck4b047132010-11-21 22:06:28 +00001655 // determine the type of the relocation
1656 unsigned Type;
1657 if (IsPCRel) {
1658 switch ((unsigned)Fixup.getKind()) {
1659 default:
1660 llvm_unreachable("Unimplemented");
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001661 case FK_PCRel_4:
Wesley Peck4b047132010-11-21 22:06:28 +00001662 Type = ELF::R_MICROBLAZE_64_PCREL;
1663 break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001664 case FK_PCRel_2:
Wesley Peck4b047132010-11-21 22:06:28 +00001665 Type = ELF::R_MICROBLAZE_32_PCREL;
1666 break;
1667 }
1668 } else {
1669 switch ((unsigned)Fixup.getKind()) {
1670 default: llvm_unreachable("invalid fixup kind!");
1671 case FK_Data_4:
Jason W Kim56a39902010-12-06 21:57:34 +00001672 Type = ((IsRelocWithSymbol || Addend !=0)
1673 ? ELF::R_MICROBLAZE_32
1674 : ELF::R_MICROBLAZE_64);
Wesley Peck4b047132010-11-21 22:06:28 +00001675 break;
1676 case FK_Data_2:
1677 Type = ELF::R_MICROBLAZE_32;
1678 break;
1679 }
1680 }
Jason W Kim56a39902010-12-06 21:57:34 +00001681 return Type;
Wesley Peck4b047132010-11-21 22:06:28 +00001682}
Jason W Kimd3443e92010-11-15 16:18:39 +00001683
1684//===- X86ELFObjectWriter -------------------------------------------===//
1685
1686
Rafael Espindola31f35782010-12-17 18:01:31 +00001687X86ELFObjectWriter::X86ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
Rafael Espindolabff66a82010-12-18 03:27:34 +00001688 raw_ostream &_OS,
1689 bool IsLittleEndian)
1690 : ELFObjectWriter(MOTW, _OS, IsLittleEndian)
Jason W Kimd3443e92010-11-15 16:18:39 +00001691{}
1692
1693X86ELFObjectWriter::~X86ELFObjectWriter()
1694{}
1695
Jason W Kim56a39902010-12-06 21:57:34 +00001696unsigned X86ELFObjectWriter::GetRelocType(const MCValue &Target,
1697 const MCFixup &Fixup,
1698 bool IsPCRel,
1699 bool IsRelocWithSymbol,
1700 int64_t Addend) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001701 // determine the type of the relocation
1702
Rafael Espindola12203cc2010-11-21 00:48:25 +00001703 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
1704 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
Jason W Kimd3443e92010-11-15 16:18:39 +00001705 unsigned Type;
Rafael Espindolabff66a82010-12-18 03:27:34 +00001706 if (is64Bit()) {
Jason W Kimd3443e92010-11-15 16:18:39 +00001707 if (IsPCRel) {
Rafael Espindola3a83c402010-12-27 00:36:05 +00001708 switch ((unsigned)Fixup.getKind()) {
1709 default: llvm_unreachable("invalid fixup kind!");
1710 case FK_PCRel_8:
1711 assert(Modifier == MCSymbolRefExpr::VK_None);
1712 Type = ELF::R_X86_64_PC64;
Jason W Kimd3443e92010-11-15 16:18:39 +00001713 break;
Rafael Espindola7a549972011-01-01 19:05:35 +00001714 case X86::reloc_signed_4byte:
Rafael Espindolac3a561c2010-12-27 02:03:24 +00001715 case X86::reloc_riprel_4byte_movq_load:
Rafael Espindola3a83c402010-12-27 00:36:05 +00001716 case FK_Data_4: // FIXME?
1717 case X86::reloc_riprel_4byte:
1718 case FK_PCRel_4:
1719 switch (Modifier) {
1720 default:
1721 llvm_unreachable("Unimplemented");
1722 case MCSymbolRefExpr::VK_None:
1723 Type = ELF::R_X86_64_PC32;
1724 break;
1725 case MCSymbolRefExpr::VK_PLT:
1726 Type = ELF::R_X86_64_PLT32;
1727 break;
1728 case MCSymbolRefExpr::VK_GOTPCREL:
1729 Type = ELF::R_X86_64_GOTPCREL;
1730 break;
1731 case MCSymbolRefExpr::VK_GOTTPOFF:
1732 Type = ELF::R_X86_64_GOTTPOFF;
Jason W Kimd3443e92010-11-15 16:18:39 +00001733 break;
Rafael Espindola3a83c402010-12-27 00:36:05 +00001734 case MCSymbolRefExpr::VK_TLSGD:
1735 Type = ELF::R_X86_64_TLSGD;
1736 break;
1737 case MCSymbolRefExpr::VK_TLSLD:
1738 Type = ELF::R_X86_64_TLSLD;
1739 break;
1740 }
Jason W Kimd3443e92010-11-15 16:18:39 +00001741 break;
Rafael Espindola3a83c402010-12-27 00:36:05 +00001742 case FK_PCRel_2:
1743 assert(Modifier == MCSymbolRefExpr::VK_None);
1744 Type = ELF::R_X86_64_PC16;
Jason W Kimd3443e92010-11-15 16:18:39 +00001745 break;
Joerg Sonnenbergerd45e8bf2011-02-21 23:25:41 +00001746 case FK_PCRel_1:
1747 assert(Modifier == MCSymbolRefExpr::VK_None);
1748 Type = ELF::R_X86_64_PC8;
1749 break;
Jason W Kimd3443e92010-11-15 16:18:39 +00001750 }
1751 } else {
1752 switch ((unsigned)Fixup.getKind()) {
1753 default: llvm_unreachable("invalid fixup kind!");
1754 case FK_Data_8: Type = ELF::R_X86_64_64; break;
1755 case X86::reloc_signed_4byte:
Jason W Kimd3443e92010-11-15 16:18:39 +00001756 assert(isInt<32>(Target.getConstant()));
1757 switch (Modifier) {
1758 default:
1759 llvm_unreachable("Unimplemented");
1760 case MCSymbolRefExpr::VK_None:
1761 Type = ELF::R_X86_64_32S;
1762 break;
1763 case MCSymbolRefExpr::VK_GOT:
1764 Type = ELF::R_X86_64_GOT32;
1765 break;
1766 case MCSymbolRefExpr::VK_GOTPCREL:
1767 Type = ELF::R_X86_64_GOTPCREL;
1768 break;
1769 case MCSymbolRefExpr::VK_TPOFF:
1770 Type = ELF::R_X86_64_TPOFF32;
1771 break;
1772 case MCSymbolRefExpr::VK_DTPOFF:
1773 Type = ELF::R_X86_64_DTPOFF32;
1774 break;
1775 }
1776 break;
1777 case FK_Data_4:
1778 Type = ELF::R_X86_64_32;
1779 break;
1780 case FK_Data_2: Type = ELF::R_X86_64_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001781 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001782 case FK_Data_1: Type = ELF::R_X86_64_8; break;
1783 }
1784 }
1785 } else {
1786 if (IsPCRel) {
1787 switch (Modifier) {
1788 default:
1789 llvm_unreachable("Unimplemented");
1790 case MCSymbolRefExpr::VK_None:
1791 Type = ELF::R_386_PC32;
1792 break;
1793 case MCSymbolRefExpr::VK_PLT:
1794 Type = ELF::R_386_PLT32;
1795 break;
1796 }
1797 } else {
1798 switch ((unsigned)Fixup.getKind()) {
1799 default: llvm_unreachable("invalid fixup kind!");
1800
1801 case X86::reloc_global_offset_table:
1802 Type = ELF::R_386_GOTPC;
1803 break;
1804
1805 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
1806 // instead?
1807 case X86::reloc_signed_4byte:
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001808 case FK_PCRel_4:
Jason W Kimd3443e92010-11-15 16:18:39 +00001809 case FK_Data_4:
1810 switch (Modifier) {
1811 default:
1812 llvm_unreachable("Unimplemented");
1813 case MCSymbolRefExpr::VK_None:
1814 Type = ELF::R_386_32;
1815 break;
1816 case MCSymbolRefExpr::VK_GOT:
1817 Type = ELF::R_386_GOT32;
1818 break;
1819 case MCSymbolRefExpr::VK_GOTOFF:
1820 Type = ELF::R_386_GOTOFF;
1821 break;
1822 case MCSymbolRefExpr::VK_TLSGD:
1823 Type = ELF::R_386_TLS_GD;
1824 break;
1825 case MCSymbolRefExpr::VK_TPOFF:
1826 Type = ELF::R_386_TLS_LE_32;
1827 break;
1828 case MCSymbolRefExpr::VK_INDNTPOFF:
1829 Type = ELF::R_386_TLS_IE;
1830 break;
1831 case MCSymbolRefExpr::VK_NTPOFF:
1832 Type = ELF::R_386_TLS_LE;
1833 break;
1834 case MCSymbolRefExpr::VK_GOTNTPOFF:
1835 Type = ELF::R_386_TLS_GOTIE;
1836 break;
1837 case MCSymbolRefExpr::VK_TLSLDM:
1838 Type = ELF::R_386_TLS_LDM;
1839 break;
1840 case MCSymbolRefExpr::VK_DTPOFF:
1841 Type = ELF::R_386_TLS_LDO_32;
1842 break;
1843 }
1844 break;
1845 case FK_Data_2: Type = ELF::R_386_16; break;
Rafael Espindolae04ed7e2010-11-28 14:17:56 +00001846 case FK_PCRel_1:
Jason W Kimd3443e92010-11-15 16:18:39 +00001847 case FK_Data_1: Type = ELF::R_386_8; break;
1848 }
1849 }
1850 }
1851
1852 if (RelocNeedsGOT(Modifier))
1853 NeedsGOT = true;
1854
Jason W Kim56a39902010-12-06 21:57:34 +00001855 return Type;
Matt Fleming3565a062010-08-16 18:57:57 +00001856}