blob: 3346930eeb27bf4ca577209479b4668aeedc1da2 [file] [log] [blame]
Matt Fleming3565a062010-08-16 18:57:57 +00001//===- lib/MC/ELFObjectWriter.cpp - ELF File Writer -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements ELF object file writer information.
11//
12//===----------------------------------------------------------------------===//
13
Rafael Espindola8f413fa2010-10-05 15:11:03 +000014#include "llvm/ADT/SmallPtrSet.h"
Matt Fleming3565a062010-08-16 18:57:57 +000015#include "llvm/ADT/STLExtras.h"
16#include "llvm/ADT/StringMap.h"
17#include "llvm/ADT/Twine.h"
18#include "llvm/MC/MCAssembler.h"
19#include "llvm/MC/MCAsmLayout.h"
20#include "llvm/MC/MCContext.h"
21#include "llvm/MC/MCELFSymbolFlags.h"
22#include "llvm/MC/MCExpr.h"
23#include "llvm/MC/MCObjectWriter.h"
24#include "llvm/MC/MCSectionELF.h"
25#include "llvm/MC/MCSymbol.h"
26#include "llvm/MC/MCValue.h"
27#include "llvm/Support/Debug.h"
28#include "llvm/Support/ErrorHandling.h"
29#include "llvm/Support/ELF.h"
30#include "llvm/Target/TargetAsmBackend.h"
31
32#include "../Target/X86/X86FixupKinds.h"
33
34#include <vector>
35using namespace llvm;
36
Rafael Espindolaad49cf52010-09-18 15:03:21 +000037static unsigned GetType(const MCSymbolData &SD) {
38 uint32_t Type = (SD.getFlags() & (0xf << ELF_STT_Shift)) >> ELF_STT_Shift;
39 assert(Type == ELF::STT_NOTYPE || Type == ELF::STT_OBJECT ||
40 Type == ELF::STT_FUNC || Type == ELF::STT_SECTION ||
41 Type == ELF::STT_FILE || Type == ELF::STT_COMMON ||
42 Type == ELF::STT_TLS);
43 return Type;
44}
45
Rafael Espindolae15eb4e2010-09-23 19:55:14 +000046static unsigned GetBinding(const MCSymbolData &SD) {
47 uint32_t Binding = (SD.getFlags() & (0xf << ELF_STB_Shift)) >> ELF_STB_Shift;
48 assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
49 Binding == ELF::STB_WEAK);
50 return Binding;
51}
52
53static void SetBinding(MCSymbolData &SD, unsigned Binding) {
54 assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
55 Binding == ELF::STB_WEAK);
56 uint32_t OtherFlags = SD.getFlags() & ~(0xf << ELF_STB_Shift);
57 SD.setFlags(OtherFlags | (Binding << ELF_STB_Shift));
58}
59
Rafael Espindola152c1062010-10-06 21:02:29 +000060static unsigned GetVisibility(MCSymbolData &SD) {
61 unsigned Visibility =
62 (SD.getFlags() & (0xf << ELF_STV_Shift)) >> ELF_STV_Shift;
63 assert(Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_INTERNAL ||
64 Visibility == ELF::STV_HIDDEN || Visibility == ELF::STV_PROTECTED);
65 return Visibility;
66}
67
Rafael Espindolacebdc012010-10-04 19:46:28 +000068static bool isFixupKindX86PCRel(unsigned Kind) {
69 switch (Kind) {
70 default:
71 return false;
72 case X86::reloc_pcrel_1byte:
73 case X86::reloc_pcrel_4byte:
74 case X86::reloc_riprel_4byte:
75 case X86::reloc_riprel_4byte_movq_load:
76 return true;
77 }
78}
79
Rafael Espindolaa0a2f872010-10-28 14:22:44 +000080static bool RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant) {
81 switch (Variant) {
Rafael Espindola5c77c162010-10-05 15:48:37 +000082 default:
83 return false;
Rafael Espindolaa0a2f872010-10-28 14:22:44 +000084 case MCSymbolRefExpr::VK_GOT:
85 case MCSymbolRefExpr::VK_PLT:
86 case MCSymbolRefExpr::VK_GOTPCREL:
87 case MCSymbolRefExpr::VK_TPOFF:
88 case MCSymbolRefExpr::VK_TLSGD:
89 case MCSymbolRefExpr::VK_GOTTPOFF:
90 case MCSymbolRefExpr::VK_INDNTPOFF:
91 case MCSymbolRefExpr::VK_NTPOFF:
92 case MCSymbolRefExpr::VK_GOTNTPOFF:
Rafael Espindolaa264f722010-10-28 14:37:09 +000093 case MCSymbolRefExpr::VK_TLSLDM:
Rafael Espindola0cf15d62010-10-28 14:48:59 +000094 case MCSymbolRefExpr::VK_DTPOFF:
Rafael Espindolab4d17212010-10-28 15:02:40 +000095 case MCSymbolRefExpr::VK_TLSLD:
Rafael Espindola5c77c162010-10-05 15:48:37 +000096 return true;
97 }
98}
99
Matt Fleming3565a062010-08-16 18:57:57 +0000100namespace {
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000101 class ELFObjectWriter : public MCObjectWriter {
Chris Lattnerb188a372010-08-28 03:21:03 +0000102 /*static bool isFixupKindX86RIPRel(unsigned Kind) {
Matt Fleming3565a062010-08-16 18:57:57 +0000103 return Kind == X86::reloc_riprel_4byte ||
104 Kind == X86::reloc_riprel_4byte_movq_load;
Chris Lattnerb188a372010-08-28 03:21:03 +0000105 }*/
Matt Fleming3565a062010-08-16 18:57:57 +0000106
107
108 /// ELFSymbolData - Helper struct for containing some precomputed information
109 /// on symbols.
110 struct ELFSymbolData {
111 MCSymbolData *SymbolData;
112 uint64_t StringIndex;
113 uint32_t SectionIndex;
114
115 // Support lexicographic sorting.
116 bool operator<(const ELFSymbolData &RHS) const {
Rafael Espindolaad49cf52010-09-18 15:03:21 +0000117 if (GetType(*SymbolData) == ELF::STT_FILE)
118 return true;
119 if (GetType(*RHS.SymbolData) == ELF::STT_FILE)
120 return false;
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000121 return SymbolData->getSymbol().getName() <
122 RHS.SymbolData->getSymbol().getName();
Matt Fleming3565a062010-08-16 18:57:57 +0000123 }
124 };
125
126 /// @name Relocation Data
127 /// @{
128
129 struct ELFRelocationEntry {
130 // Make these big enough for both 32-bit and 64-bit
131 uint64_t r_offset;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000132 int Index;
133 unsigned Type;
134 const MCSymbol *Symbol;
Matt Fleming3565a062010-08-16 18:57:57 +0000135 uint64_t r_addend;
136
137 // Support lexicographic sorting.
138 bool operator<(const ELFRelocationEntry &RE) const {
139 return RE.r_offset < r_offset;
140 }
141 };
142
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000143 SmallPtrSet<const MCSymbol *, 16> UsedInReloc;
Rafael Espindola484291c2010-11-01 14:28:48 +0000144 SmallPtrSet<const MCSymbol *, 16> WeakrefUsedInReloc;
Rafael Espindola88182132010-10-27 15:18:17 +0000145 DenseMap<const MCSymbol *, const MCSymbol *> Renames;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000146
Matt Fleming3565a062010-08-16 18:57:57 +0000147 llvm::DenseMap<const MCSectionData*,
148 std::vector<ELFRelocationEntry> > Relocations;
149 DenseMap<const MCSection*, uint64_t> SectionStringTableIndex;
150
151 /// @}
152 /// @name Symbol Table Data
153 /// @{
154
155 SmallString<256> StringTable;
156 std::vector<ELFSymbolData> LocalSymbolData;
157 std::vector<ELFSymbolData> ExternalSymbolData;
158 std::vector<ELFSymbolData> UndefinedSymbolData;
159
160 /// @}
161
Rafael Espindola5c77c162010-10-05 15:48:37 +0000162 bool NeedsGOT;
163
Rafael Espindola7be2c332010-10-31 00:16:26 +0000164 bool NeedsSymtabShndx;
165
Matt Fleming3565a062010-08-16 18:57:57 +0000166 unsigned Is64Bit : 1;
167
168 bool HasRelocationAddend;
169
Roman Divacky5baf79e2010-09-09 17:57:50 +0000170 Triple::OSType OSType;
171
Wesley Peckeecb8582010-10-22 15:52:49 +0000172 uint16_t EMachine;
173
Matt Fleming3565a062010-08-16 18:57:57 +0000174 // This holds the symbol table index of the last local symbol.
175 unsigned LastLocalSymbolIndex;
176 // This holds the .strtab section index.
177 unsigned StringTableIndex;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000178 // This holds the .symtab section index.
179 unsigned SymbolTableIndex;
Matt Fleming3565a062010-08-16 18:57:57 +0000180
181 unsigned ShstrtabIndex;
182
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000183
184 const MCSymbol *SymbolToReloc(const MCAssembler &Asm,
185 const MCValue &Target,
186 const MCFragment &F) const;
187
Matt Fleming3565a062010-08-16 18:57:57 +0000188 public:
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000189 ELFObjectWriter(raw_ostream &_OS, bool _Is64Bit, bool IsLittleEndian,
190 uint16_t _EMachine, bool _HasRelAddend,
191 Triple::OSType _OSType)
192 : MCObjectWriter(_OS, IsLittleEndian),
193 NeedsGOT(false), NeedsSymtabShndx(false),
Roman Divacky5baf79e2010-09-09 17:57:50 +0000194 Is64Bit(_Is64Bit), HasRelocationAddend(_HasRelAddend),
Wesley Peckeecb8582010-10-22 15:52:49 +0000195 OSType(_OSType), EMachine(_EMachine) {
Matt Fleming3565a062010-08-16 18:57:57 +0000196 }
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000197
Matt Fleming3565a062010-08-16 18:57:57 +0000198 void WriteWord(uint64_t W) {
Chris Lattnerb188a372010-08-28 03:21:03 +0000199 if (Is64Bit)
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000200 Write64(W);
Chris Lattnerb188a372010-08-28 03:21:03 +0000201 else
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000202 Write32(W);
Matt Fleming3565a062010-08-16 18:57:57 +0000203 }
204
Matt Fleming3565a062010-08-16 18:57:57 +0000205 void StringLE16(char *buf, uint16_t Value) {
206 buf[0] = char(Value >> 0);
207 buf[1] = char(Value >> 8);
208 }
209
210 void StringLE32(char *buf, uint32_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000211 StringLE16(buf, uint16_t(Value >> 0));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000212 StringLE16(buf + 2, uint16_t(Value >> 16));
Matt Fleming3565a062010-08-16 18:57:57 +0000213 }
214
215 void StringLE64(char *buf, uint64_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000216 StringLE32(buf, uint32_t(Value >> 0));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000217 StringLE32(buf + 4, uint32_t(Value >> 32));
Matt Fleming3565a062010-08-16 18:57:57 +0000218 }
219
220 void StringBE16(char *buf ,uint16_t Value) {
221 buf[0] = char(Value >> 8);
222 buf[1] = char(Value >> 0);
223 }
224
225 void StringBE32(char *buf, uint32_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000226 StringBE16(buf, uint16_t(Value >> 16));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000227 StringBE16(buf + 2, uint16_t(Value >> 0));
Matt Fleming3565a062010-08-16 18:57:57 +0000228 }
229
230 void StringBE64(char *buf, uint64_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000231 StringBE32(buf, uint32_t(Value >> 32));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000232 StringBE32(buf + 4, uint32_t(Value >> 0));
Matt Fleming3565a062010-08-16 18:57:57 +0000233 }
234
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000235 void String8(MCDataFragment &F, uint8_t Value) {
236 char buf[1];
237 buf[0] = Value;
238 F.getContents() += StringRef(buf, 1);
239 }
240
241 void String16(MCDataFragment &F, uint16_t Value) {
242 char buf[2];
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000243 if (isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000244 StringLE16(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000245 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000246 StringBE16(buf, Value);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000247 F.getContents() += StringRef(buf, 2);
Matt Fleming3565a062010-08-16 18:57:57 +0000248 }
249
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000250 void String32(MCDataFragment &F, uint32_t Value) {
251 char buf[4];
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000252 if (isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000253 StringLE32(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000254 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000255 StringBE32(buf, Value);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000256 F.getContents() += StringRef(buf, 4);
Matt Fleming3565a062010-08-16 18:57:57 +0000257 }
258
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000259 void String64(MCDataFragment &F, uint64_t Value) {
260 char buf[8];
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000261 if (isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000262 StringLE64(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000263 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000264 StringBE64(buf, Value);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000265 F.getContents() += StringRef(buf, 8);
Matt Fleming3565a062010-08-16 18:57:57 +0000266 }
267
268 void WriteHeader(uint64_t SectionDataSize, unsigned NumberOfSections);
269
Rafael Espindola7be2c332010-10-31 00:16:26 +0000270 void WriteSymbolEntry(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
271 uint64_t name, uint8_t info,
Matt Fleming3565a062010-08-16 18:57:57 +0000272 uint64_t value, uint64_t size,
Rafael Espindola7be2c332010-10-31 00:16:26 +0000273 uint8_t other, uint32_t shndx,
274 bool Reserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000275
Rafael Espindola7be2c332010-10-31 00:16:26 +0000276 void WriteSymbol(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
277 ELFSymbolData &MSD,
Matt Fleming3565a062010-08-16 18:57:57 +0000278 const MCAsmLayout &Layout);
279
Rafael Espindolabab2a802010-11-10 21:51:05 +0000280 typedef DenseMap<const MCSectionELF*, uint32_t> SectionIndexMapTy;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000281 void WriteSymbolTable(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
282 const MCAssembler &Asm,
Rafael Espindola71859c62010-09-16 19:46:31 +0000283 const MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000284 const SectionIndexMapTy &SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +0000285
286 void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
287 const MCFragment *Fragment, const MCFixup &Fixup,
288 MCValue Target, uint64_t &FixedValue);
289
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000290 uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm,
291 const MCSymbol *S);
Matt Fleming3565a062010-08-16 18:57:57 +0000292
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000293 // Map from a group section to the signature symbol
294 typedef DenseMap<const MCSectionELF*, const MCSymbol*> GroupMapTy;
295 // Map from a signature symbol to the group section
296 typedef DenseMap<const MCSymbol*, const MCSectionELF*> RevGroupMapTy;
297
Matt Fleming3565a062010-08-16 18:57:57 +0000298 /// ComputeSymbolTable - Compute the symbol table data
299 ///
300 /// \param StringTable [out] - The string table data.
301 /// \param StringIndexMap [out] - Map from symbol names to offsets in the
302 /// string table.
Rafael Espindolabab2a802010-11-10 21:51:05 +0000303 void ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000304 const SectionIndexMapTy &SectionIndexMap,
305 RevGroupMapTy RevGroupMap);
Rafael Espindolabab2a802010-11-10 21:51:05 +0000306
307 void ComputeIndexMap(MCAssembler &Asm,
308 SectionIndexMapTy &SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +0000309
310 void WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
311 const MCSectionData &SD);
312
313 void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout) {
314 for (MCAssembler::const_iterator it = Asm.begin(),
315 ie = Asm.end(); it != ie; ++it) {
316 WriteRelocation(Asm, Layout, *it);
317 }
318 }
319
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000320 void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout,
321 const SectionIndexMapTy &SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +0000322
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000323 void CreateGroupSections(MCAssembler &Asm, MCAsmLayout &Layout,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000324 GroupMapTy &GroupMap, RevGroupMapTy &RevGroupMap);
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000325
Rafael Espindola88182132010-10-27 15:18:17 +0000326 void ExecutePostLayoutBinding(MCAssembler &Asm);
Matt Fleming3565a062010-08-16 18:57:57 +0000327
328 void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
329 uint64_t Address, uint64_t Offset,
330 uint64_t Size, uint32_t Link, uint32_t Info,
331 uint64_t Alignment, uint64_t EntrySize);
332
333 void WriteRelocationsFragment(const MCAssembler &Asm, MCDataFragment *F,
334 const MCSectionData *SD);
335
Rafael Espindola70703872010-09-30 02:22:20 +0000336 bool IsFixupFullyResolved(const MCAssembler &Asm,
337 const MCValue Target,
338 bool IsPCRel,
339 const MCFragment *DF) const;
340
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000341 void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000342 void WriteSection(MCAssembler &Asm,
343 const SectionIndexMapTy &SectionIndexMap,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000344 uint32_t GroupSymbolIndex,
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000345 uint64_t Offset, uint64_t Size, uint64_t Alignment,
346 const MCSectionELF &Section);
Matt Fleming3565a062010-08-16 18:57:57 +0000347 };
348
349}
350
351// Emit the ELF header.
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000352void ELFObjectWriter::WriteHeader(uint64_t SectionDataSize,
353 unsigned NumberOfSections) {
Matt Fleming3565a062010-08-16 18:57:57 +0000354 // ELF Header
355 // ----------
356 //
357 // Note
358 // ----
359 // emitWord method behaves differently for ELF32 and ELF64, writing
360 // 4 bytes in the former and 8 in the latter.
361
362 Write8(0x7f); // e_ident[EI_MAG0]
363 Write8('E'); // e_ident[EI_MAG1]
364 Write8('L'); // e_ident[EI_MAG2]
365 Write8('F'); // e_ident[EI_MAG3]
366
367 Write8(Is64Bit ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
368
369 // e_ident[EI_DATA]
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000370 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
Matt Fleming3565a062010-08-16 18:57:57 +0000371
372 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky5baf79e2010-09-09 17:57:50 +0000373 // e_ident[EI_OSABI]
374 switch (OSType) {
375 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
376 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
377 default: Write8(ELF::ELFOSABI_NONE); break;
378 }
Matt Fleming3565a062010-08-16 18:57:57 +0000379 Write8(0); // e_ident[EI_ABIVERSION]
380
381 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
382
383 Write16(ELF::ET_REL); // e_type
384
Wesley Peckeecb8582010-10-22 15:52:49 +0000385 Write16(EMachine); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +0000386
387 Write32(ELF::EV_CURRENT); // e_version
388 WriteWord(0); // e_entry, no entry point in .o file
389 WriteWord(0); // e_phoff, no program header for .o
Benjamin Kramereb976772010-08-17 17:02:29 +0000390 WriteWord(SectionDataSize + (Is64Bit ? sizeof(ELF::Elf64_Ehdr) :
391 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000392
393 // FIXME: Make this configurable.
394 Write32(0); // e_flags = whatever the target wants
395
396 // e_ehsize = ELF header size
397 Write16(Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
398
399 Write16(0); // e_phentsize = prog header entry size
400 Write16(0); // e_phnum = # prog header entries = 0
401
402 // e_shentsize = Section header entry size
403 Write16(Is64Bit ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
404
405 // e_shnum = # of section header ents
Rafael Espindola7be2c332010-10-31 00:16:26 +0000406 if (NumberOfSections >= ELF::SHN_LORESERVE)
407 Write16(0);
408 else
409 Write16(NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000410
411 // e_shstrndx = Section # of '.shstrtab'
Rafael Espindola7be2c332010-10-31 00:16:26 +0000412 if (NumberOfSections >= ELF::SHN_LORESERVE)
413 Write16(ELF::SHN_XINDEX);
414 else
415 Write16(ShstrtabIndex);
Matt Fleming3565a062010-08-16 18:57:57 +0000416}
417
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000418void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
419 MCDataFragment *ShndxF,
420 uint64_t name,
421 uint8_t info, uint64_t value,
422 uint64_t size, uint8_t other,
423 uint32_t shndx,
424 bool Reserved) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000425 if (ShndxF) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000426 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000427 String32(*ShndxF, shndx);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000428 else
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000429 String32(*ShndxF, 0);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000430 }
431
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000432 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
433 uint16_t(ELF::SHN_XINDEX) : shndx;
434
Matt Fleming3565a062010-08-16 18:57:57 +0000435 if (Is64Bit) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000436 String32(*SymtabF, name); // st_name
437 String8(*SymtabF, info); // st_info
438 String8(*SymtabF, other); // st_other
439 String16(*SymtabF, Index); // st_shndx
440 String64(*SymtabF, value); // st_value
441 String64(*SymtabF, size); // st_size
Matt Fleming3565a062010-08-16 18:57:57 +0000442 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000443 String32(*SymtabF, name); // st_name
444 String32(*SymtabF, value); // st_value
445 String32(*SymtabF, size); // st_size
446 String8(*SymtabF, info); // st_info
447 String8(*SymtabF, other); // st_other
448 String16(*SymtabF, Index); // st_shndx
Matt Fleming3565a062010-08-16 18:57:57 +0000449 }
450}
451
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000452static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout) {
453 if (Data.isCommon() && Data.isExternal())
454 return Data.getCommonAlignment();
455
456 const MCSymbol &Symbol = Data.getSymbol();
457 if (!Symbol.isInSection())
458 return 0;
459
Rafael Espindola1973d432010-10-28 19:39:57 +0000460 if (MCFragment *FF = Data.getFragment())
461 return Layout.getSymbolAddress(&Data) -
462 Layout.getSectionAddress(FF->getParent());
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000463
464 return 0;
465}
466
Rafael Espindolade89b012010-10-15 18:25:33 +0000467static const MCSymbol &AliasedSymbol(const MCSymbol &Symbol) {
468 const MCSymbol *S = &Symbol;
469 while (S->isVariable()) {
470 const MCExpr *Value = S->getVariableValue();
Rafael Espindola9302bd62010-11-11 17:24:43 +0000471 MCExpr::ExprKind Kind = Value->getKind();
472 switch (Kind) {
473 case MCExpr::SymbolRef: {
474 const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr*>(Value);
475 S = &Ref->getSymbol();
476 break;
477 }
478 case MCExpr::Target: {
479 const MCTargetExpr *TExp = static_cast<const MCTargetExpr*>(Value);
480 MCValue Res;
481 TExp->EvaluateAsRelocatableImpl(Res, NULL);
482 S = &Res.getSymA()->getSymbol();
483 break;
484 }
485 default:
Rafael Espindolaa6866962010-10-27 14:44:52 +0000486 return *S;
Rafael Espindola9302bd62010-11-11 17:24:43 +0000487 }
Rafael Espindolade89b012010-10-15 18:25:33 +0000488 }
489 return *S;
490}
491
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000492void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm) {
Rafael Espindola88182132010-10-27 15:18:17 +0000493 // The presence of symbol versions causes undefined symbols and
494 // versions declared with @@@ to be renamed.
495
496 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
497 ie = Asm.symbol_end(); it != ie; ++it) {
498 const MCSymbol &Alias = it->getSymbol();
Rafael Espindola88182132010-10-27 15:18:17 +0000499 const MCSymbol &Symbol = AliasedSymbol(Alias);
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000500 MCSymbolData &SD = Asm.getSymbolData(Symbol);
501
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000502 // Not an alias.
503 if (&Symbol == &Alias)
504 continue;
505
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000506 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000507 size_t Pos = AliasName.find('@');
508 if (Pos == StringRef::npos)
509 continue;
510
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000511 // Aliases defined with .symvar copy the binding from the symbol they alias.
512 // This is the first place we are able to copy this information.
513 it->setExternal(SD.isExternal());
514 SetBinding(*it, GetBinding(SD));
515
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000516 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000517 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
518 continue;
519
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000520 // FIXME: produce a better error message.
521 if (Symbol.isUndefined() && Rest.startswith("@@") &&
522 !Rest.startswith("@@@"))
523 report_fatal_error("A @@ version cannot be undefined");
524
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000525 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000526 }
527}
528
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000529void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
530 MCDataFragment *ShndxF,
531 ELFSymbolData &MSD,
532 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000533 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000534 MCSymbolData &Data =
535 Layout.getAssembler().getSymbolData(AliasedSymbol(OrigData.getSymbol()));
Rafael Espindola152c1062010-10-06 21:02:29 +0000536
Rafael Espindola7be2c332010-10-31 00:16:26 +0000537 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
538 Data.getSymbol().isVariable();
539
Rafael Espindola152c1062010-10-06 21:02:29 +0000540 uint8_t Binding = GetBinding(OrigData);
541 uint8_t Visibility = GetVisibility(OrigData);
542 uint8_t Type = GetType(Data);
543
544 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
545 uint8_t Other = Visibility;
546
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000547 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000548 uint64_t Size = 0;
549 const MCExpr *ESize;
550
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000551 assert(!(Data.isCommon() && !Data.isExternal()));
552
Matt Fleming3565a062010-08-16 18:57:57 +0000553 ESize = Data.getSize();
554 if (Data.getSize()) {
555 MCValue Res;
556 if (ESize->getKind() == MCExpr::Binary) {
557 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(ESize);
558
559 if (BE->EvaluateAsRelocatable(Res, &Layout)) {
Benjamin Kramer24f12062010-10-17 07:38:40 +0000560 assert(!Res.getSymA() || !Res.getSymA()->getSymbol().isDefined());
561 assert(!Res.getSymB() || !Res.getSymB()->getSymbol().isDefined());
Rafael Espindolaf230df92010-10-16 18:23:53 +0000562 Size = Res.getConstant();
Matt Fleming3565a062010-08-16 18:57:57 +0000563 }
564 } else if (ESize->getKind() == MCExpr::Constant) {
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000565 Size = static_cast<const MCConstantExpr *>(ESize)->getValue();
Matt Fleming3565a062010-08-16 18:57:57 +0000566 } else {
567 assert(0 && "Unsupported size expression");
568 }
569 }
570
571 // Write out the symbol table entry
Rafael Espindola7be2c332010-10-31 00:16:26 +0000572 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
573 Size, Other, MSD.SectionIndex, IsReserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000574}
575
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000576void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
577 MCDataFragment *ShndxF,
578 const MCAssembler &Asm,
579 const MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000580 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000581 // The string table must be emitted first because we need the index
582 // into the string table for all the symbol names.
583 assert(StringTable.size() && "Missing string table");
584
585 // FIXME: Make sure the start of the symbol table is aligned.
586
587 // The first entry is the undefined symbol entry.
Rafael Espindola7be2c332010-10-31 00:16:26 +0000588 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
Matt Fleming3565a062010-08-16 18:57:57 +0000589
590 // Write the symbol table entries.
591 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
592 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
593 ELFSymbolData &MSD = LocalSymbolData[i];
Rafael Espindola7be2c332010-10-31 00:16:26 +0000594 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000595 }
596
Rafael Espindola71859c62010-09-16 19:46:31 +0000597 // Write out a symbol table entry for each regular section.
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000598 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
599 ++i) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000600 const MCSectionELF &Section =
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000601 static_cast<const MCSectionELF&>(i->getSection());
602 if (Section.getType() == ELF::SHT_RELA ||
603 Section.getType() == ELF::SHT_REL ||
604 Section.getType() == ELF::SHT_STRTAB ||
605 Section.getType() == ELF::SHT_SYMTAB)
Eli Friedmana44fa242010-08-16 21:17:09 +0000606 continue;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000607 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000608 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section), false);
Eli Friedmana44fa242010-08-16 21:17:09 +0000609 LastLocalSymbolIndex++;
610 }
Matt Fleming3565a062010-08-16 18:57:57 +0000611
612 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
613 ELFSymbolData &MSD = ExternalSymbolData[i];
614 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000615 assert(((Data.getFlags() & ELF_STB_Global) ||
616 (Data.getFlags() & ELF_STB_Weak)) &&
617 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000618 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000619 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000620 LastLocalSymbolIndex++;
621 }
622
623 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
624 ELFSymbolData &MSD = UndefinedSymbolData[i];
625 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000626 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000627 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000628 LastLocalSymbolIndex++;
629 }
630}
631
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000632const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm,
633 const MCValue &Target,
634 const MCFragment &F) const {
635 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
636 const MCSymbol &ASymbol = AliasedSymbol(Symbol);
637 const MCSymbol *RenamedP = Renames.lookup(&Symbol);
638
639 if (!RenamedP) {
640 if (Target.getSymA()->getKind() == MCSymbolRefExpr::VK_None ||
641 Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
642 RenamedP = &ASymbol;
643 else
644 RenamedP = &Symbol;
645 }
646 const MCSymbol &Renamed = *RenamedP;
647
648 MCSymbolData &SD = Asm.getSymbolData(Symbol);
649
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000650 if (Symbol.isUndefined())
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000651 return &Renamed;
652
653 if (SD.isExternal())
654 return &Renamed;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000655
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000656 const MCSectionELF &Section =
657 static_cast<const MCSectionELF&>(Symbol.getSection());
658
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000659 if (Section.getKind().isBSS())
660 return NULL;
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000661
Rafael Espindola8cecf252010-10-06 16:23:36 +0000662 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000663 const MCSectionELF &Sec2 =
664 static_cast<const MCSectionELF&>(F.getParent()->getSection());
665
Rafael Espindola8cecf252010-10-06 16:23:36 +0000666 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000667 (Kind == MCSymbolRefExpr::VK_PLT ||
668 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
669 Kind == MCSymbolRefExpr::VK_GOTOFF))
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000670 return &Renamed;
Rafael Espindola3729d002010-10-05 23:57:26 +0000671
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000672 if (Section.getFlags() & MCSectionELF::SHF_MERGE) {
673 if (Target.getConstant() != 0)
674 return &Renamed;
675 return NULL;
676 }
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000677
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000678 return NULL;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000679}
680
Benjamin Kramere5b57342010-08-17 18:20:28 +0000681// FIXME: this is currently X86/X86_64 only
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000682void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
683 const MCAsmLayout &Layout,
684 const MCFragment *Fragment,
685 const MCFixup &Fixup,
686 MCValue Target,
687 uint64_t &FixedValue) {
Matt Fleming3565a062010-08-16 18:57:57 +0000688 int64_t Addend = 0;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000689 int Index = 0;
Benjamin Kramer95c602a2010-08-27 10:38:39 +0000690 int64_t Value = Target.getConstant();
Rafael Espindola03f1b742010-11-11 16:48:11 +0000691 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000692 const MCSymbol *RelocSymbol = SymbolToReloc(Asm, Target, *Fragment);
Matt Fleming3565a062010-08-16 18:57:57 +0000693
Rafael Espindola00074892010-09-17 22:34:41 +0000694 bool IsPCRel = isFixupKindX86PCRel(Fixup.getKind());
Benjamin Kramer81cfb852010-08-17 19:45:05 +0000695 if (!Target.isAbsolute()) {
Rafael Espindola9d8b7552010-10-03 00:46:57 +0000696 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
697 const MCSymbol &SymbolB = RefB->getSymbol();
698 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
699 IsPCRel = true;
Rafael Espindola55fb1022010-10-04 15:59:01 +0000700 MCSectionData *Sec = Fragment->getParent();
701
702 // Offset of the symbol in the section
703 int64_t a = Layout.getSymbolAddress(&SDB) - Layout.getSectionAddress(Sec);
704
705 // Ofeset of the relocation in the section
706 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
707 Value += b - a;
Rafael Espindola9d8b7552010-10-03 00:46:57 +0000708 }
709
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000710 if (!RelocSymbol) {
711 MCSymbolData &SD = Asm.getSymbolData(Symbol);
712 MCFragment *F = SD.getFragment();
713
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000714 Index = F->getParent()->getOrdinal();
Rafael Espindolaa6489182010-09-24 21:19:03 +0000715
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000716 MCSectionData *FSD = F->getParent();
717 // Offset of the symbol in the section
718 Value += Layout.getSymbolAddress(&SD) - Layout.getSectionAddress(FSD);
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000719 } else {
Rafael Espindola03f1b742010-11-11 16:48:11 +0000720 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000721 WeakrefUsedInReloc.insert(RelocSymbol);
Rafael Espindola03f1b742010-11-11 16:48:11 +0000722 else
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000723 UsedInReloc.insert(RelocSymbol);
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000724 Index = -1;
725 }
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000726 Addend = Value;
727 // Compensate for the addend on i386.
728 if (Is64Bit)
729 Value = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000730 }
731
Benjamin Kramer95c602a2010-08-27 10:38:39 +0000732 FixedValue = Value;
733
Matt Fleming3565a062010-08-16 18:57:57 +0000734 // determine the type of the relocation
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000735
736 MCSymbolRefExpr::VariantKind Modifier = Target.getSymA()->getKind();
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000737 unsigned Type;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000738 if (Is64Bit) {
739 if (IsPCRel) {
Rafael Espindola92bf6682010-10-04 19:04:13 +0000740 switch (Modifier) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000741 default:
742 llvm_unreachable("Unimplemented");
Rafael Espindola92bf6682010-10-04 19:04:13 +0000743 case MCSymbolRefExpr::VK_None:
744 Type = ELF::R_X86_64_PC32;
745 break;
746 case MCSymbolRefExpr::VK_PLT:
747 Type = ELF::R_X86_64_PLT32;
748 break;
Rafael Espindolaa0a2f872010-10-28 14:22:44 +0000749 case MCSymbolRefExpr::VK_GOTPCREL:
Rafael Espindola607d1f62010-10-04 19:51:39 +0000750 Type = ELF::R_X86_64_GOTPCREL;
751 break;
Rafael Espindolabc82d8b2010-10-27 20:28:07 +0000752 case MCSymbolRefExpr::VK_GOTTPOFF:
753 Type = ELF::R_X86_64_GOTTPOFF;
754 break;
755 case MCSymbolRefExpr::VK_TLSGD:
756 Type = ELF::R_X86_64_TLSGD;
757 break;
Rafael Espindolab4d17212010-10-28 15:02:40 +0000758 case MCSymbolRefExpr::VK_TLSLD:
759 Type = ELF::R_X86_64_TLSLD;
760 break;
Rafael Espindola92bf6682010-10-04 19:04:13 +0000761 }
Benjamin Kramere5b57342010-08-17 18:20:28 +0000762 } else {
763 switch ((unsigned)Fixup.getKind()) {
764 default: llvm_unreachable("invalid fixup kind!");
765 case FK_Data_8: Type = ELF::R_X86_64_64; break;
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000766 case X86::reloc_signed_4byte:
Benjamin Kramere5b57342010-08-17 18:20:28 +0000767 case X86::reloc_pcrel_4byte:
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000768 assert(isInt<32>(Target.getConstant()));
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000769 switch (Modifier) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000770 default:
771 llvm_unreachable("Unimplemented");
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000772 case MCSymbolRefExpr::VK_None:
773 Type = ELF::R_X86_64_32S;
774 break;
775 case MCSymbolRefExpr::VK_GOT:
776 Type = ELF::R_X86_64_GOT32;
777 break;
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000778 case MCSymbolRefExpr::VK_GOTPCREL:
Rafael Espindola8cecf252010-10-06 16:23:36 +0000779 Type = ELF::R_X86_64_GOTPCREL;
780 break;
Rafael Espindolabc82d8b2010-10-27 20:28:07 +0000781 case MCSymbolRefExpr::VK_TPOFF:
782 Type = ELF::R_X86_64_TPOFF32;
783 break;
Rafael Espindolaaa8f1f02010-10-28 15:11:03 +0000784 case MCSymbolRefExpr::VK_DTPOFF:
785 Type = ELF::R_X86_64_DTPOFF32;
786 break;
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000787 }
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000788 break;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000789 case FK_Data_4:
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000790 Type = ELF::R_X86_64_32;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000791 break;
792 case FK_Data_2: Type = ELF::R_X86_64_16; break;
793 case X86::reloc_pcrel_1byte:
794 case FK_Data_1: Type = ELF::R_X86_64_8; break;
795 }
796 }
Matt Fleming3565a062010-08-16 18:57:57 +0000797 } else {
Benjamin Kramere5b57342010-08-17 18:20:28 +0000798 if (IsPCRel) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000799 switch (Modifier) {
800 default:
Rafael Espindola24dc9ec2010-10-18 19:33:01 +0000801 llvm_unreachable("Unimplemented");
802 case MCSymbolRefExpr::VK_None:
Rafael Espindola9baee3b2010-10-18 18:03:28 +0000803 Type = ELF::R_386_PC32;
Rafael Espindola9baee3b2010-10-18 18:03:28 +0000804 break;
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000805 case MCSymbolRefExpr::VK_PLT:
806 Type = ELF::R_386_PLT32;
807 break;
808 }
Benjamin Kramere5b57342010-08-17 18:20:28 +0000809 } else {
810 switch ((unsigned)Fixup.getKind()) {
811 default: llvm_unreachable("invalid fixup kind!");
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000812
Rafael Espindola24ba4f72010-10-24 17:35:42 +0000813 case X86::reloc_global_offset_table:
814 Type = ELF::R_386_GOTPC;
815 break;
816
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000817 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
818 // instead?
819 case X86::reloc_signed_4byte:
Benjamin Kramere5b57342010-08-17 18:20:28 +0000820 case X86::reloc_pcrel_4byte:
Rafael Espindolabd701182010-10-19 19:31:37 +0000821 case FK_Data_4:
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000822 switch (Modifier) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000823 default:
824 llvm_unreachable("Unimplemented");
Rafael Espindolabd701182010-10-19 19:31:37 +0000825 case MCSymbolRefExpr::VK_None:
Rafael Espindola24ba4f72010-10-24 17:35:42 +0000826 Type = ELF::R_386_32;
Rafael Espindolabd701182010-10-19 19:31:37 +0000827 break;
Rafael Espindolaeada3042010-10-18 20:47:21 +0000828 case MCSymbolRefExpr::VK_GOT:
829 Type = ELF::R_386_GOT32;
830 break;
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000831 case MCSymbolRefExpr::VK_GOTOFF:
832 Type = ELF::R_386_GOTOFF;
833 break;
Rafael Espindola3cede2d2010-10-27 21:23:52 +0000834 case MCSymbolRefExpr::VK_TLSGD:
835 Type = ELF::R_386_TLS_GD;
836 break;
837 case MCSymbolRefExpr::VK_TPOFF:
838 Type = ELF::R_386_TLS_LE_32;
839 break;
840 case MCSymbolRefExpr::VK_INDNTPOFF:
841 Type = ELF::R_386_TLS_IE;
842 break;
843 case MCSymbolRefExpr::VK_NTPOFF:
844 Type = ELF::R_386_TLS_LE;
845 break;
Rafael Espindolaa0a2f872010-10-28 14:22:44 +0000846 case MCSymbolRefExpr::VK_GOTNTPOFF:
847 Type = ELF::R_386_TLS_GOTIE;
848 break;
Rafael Espindolaa264f722010-10-28 14:37:09 +0000849 case MCSymbolRefExpr::VK_TLSLDM:
850 Type = ELF::R_386_TLS_LDM;
851 break;
Rafael Espindola0cf15d62010-10-28 14:48:59 +0000852 case MCSymbolRefExpr::VK_DTPOFF:
853 Type = ELF::R_386_TLS_LDO_32;
854 break;
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000855 }
856 break;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000857 case FK_Data_2: Type = ELF::R_386_16; break;
858 case X86::reloc_pcrel_1byte:
859 case FK_Data_1: Type = ELF::R_386_8; break;
860 }
Matt Fleming3565a062010-08-16 18:57:57 +0000861 }
862 }
863
Rafael Espindolaa0a2f872010-10-28 14:22:44 +0000864 if (RelocNeedsGOT(Modifier))
Rafael Espindola5c77c162010-10-05 15:48:37 +0000865 NeedsGOT = true;
866
Benjamin Kramere5b57342010-08-17 18:20:28 +0000867 ELFRelocationEntry ERE;
868
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000869 ERE.Index = Index;
870 ERE.Type = Type;
Rafael Espindola1f52dfe2010-11-14 23:53:26 +0000871 ERE.Symbol = RelocSymbol;
Matt Fleming3565a062010-08-16 18:57:57 +0000872
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000873 ERE.r_offset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
Benjamin Kramere5b57342010-08-17 18:20:28 +0000874
Matt Fleming3565a062010-08-16 18:57:57 +0000875 if (HasRelocationAddend)
876 ERE.r_addend = Addend;
Benjamin Kramer172d7d62010-08-17 00:33:24 +0000877 else
878 ERE.r_addend = 0; // Silence compiler warning.
Matt Fleming3565a062010-08-16 18:57:57 +0000879
880 Relocations[Fragment->getParent()].push_back(ERE);
881}
882
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000883uint64_t
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000884ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
885 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000886 MCSymbolData &SD = Asm.getSymbolData(*S);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000887 return SD.getIndex();
Matt Fleming3565a062010-08-16 18:57:57 +0000888}
889
Rafael Espindola737cd212010-10-05 18:01:23 +0000890static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
Rafael Espindola88182132010-10-27 15:18:17 +0000891 bool Used, bool Renamed) {
Rafael Espindola484291c2010-11-01 14:28:48 +0000892 if (Data.getFlags() & ELF_Other_Weakref)
893 return false;
894
Rafael Espindolabd701182010-10-19 19:31:37 +0000895 if (Used)
896 return true;
897
Rafael Espindola88182132010-10-27 15:18:17 +0000898 if (Renamed)
899 return false;
900
Rafael Espindola737cd212010-10-05 18:01:23 +0000901 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000902
Rafael Espindolad1798862010-10-29 23:09:31 +0000903 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
904 return true;
905
Rafael Espindolaa6866962010-10-27 14:44:52 +0000906 const MCSymbol &A = AliasedSymbol(Symbol);
Rafael Espindolad1798862010-10-29 23:09:31 +0000907 if (!A.isVariable() && A.isUndefined() && !Data.isCommon())
Rafael Espindolaa6866962010-10-27 14:44:52 +0000908 return false;
909
Rafael Espindola737cd212010-10-05 18:01:23 +0000910 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
911 return false;
912
Rafael Espindolabd701182010-10-19 19:31:37 +0000913 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000914 return false;
915
916 return true;
917}
918
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000919static bool isLocal(const MCSymbolData &Data, bool isSignature,
920 bool isUsedInReloc) {
Rafael Espindola737cd212010-10-05 18:01:23 +0000921 if (Data.isExternal())
922 return false;
923
924 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000925 const MCSymbol &RefSymbol = AliasedSymbol(Symbol);
926
927 if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
928 if (isSignature && !isUsedInReloc)
929 return true;
930
Rafael Espindola737cd212010-10-05 18:01:23 +0000931 return false;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000932 }
Rafael Espindola737cd212010-10-05 18:01:23 +0000933
934 return true;
935}
936
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000937void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
938 SectionIndexMapTy &SectionIndexMap) {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000939 unsigned Index = 1;
940 for (MCAssembler::iterator it = Asm.begin(),
941 ie = Asm.end(); it != ie; ++it) {
942 const MCSectionELF &Section =
943 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000944 if (Section.getType() != ELF::SHT_GROUP)
945 continue;
946 SectionIndexMap[&Section] = Index++;
947 }
948
949 for (MCAssembler::iterator it = Asm.begin(),
950 ie = Asm.end(); it != ie; ++it) {
951 const MCSectionELF &Section =
952 static_cast<const MCSectionELF &>(it->getSection());
953 if (Section.getType() == ELF::SHT_GROUP)
954 continue;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000955 SectionIndexMap[&Section] = Index++;
956 }
957}
958
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000959void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000960 const SectionIndexMapTy &SectionIndexMap,
961 RevGroupMapTy RevGroupMap) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000962 // FIXME: Is this the correct place to do this?
963 if (NeedsGOT) {
964 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
965 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
966 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
967 Data.setExternal(true);
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000968 SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindola5c77c162010-10-05 15:48:37 +0000969 }
970
Matt Fleming3565a062010-08-16 18:57:57 +0000971 // Build section lookup table.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000972 int NumRegularSections = Asm.size();
Matt Fleming3565a062010-08-16 18:57:57 +0000973
974 // Index 0 is always the empty string.
975 StringMap<uint64_t> StringIndexMap;
976 StringTable += '\x00';
977
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000978 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000979 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
980 ie = Asm.symbol_end(); it != ie; ++it) {
981 const MCSymbol &Symbol = it->getSymbol();
982
Rafael Espindola484291c2010-11-01 14:28:48 +0000983 bool Used = UsedInReloc.count(&Symbol);
984 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000985 bool isSignature = RevGroupMap.count(&Symbol);
986
987 if (!isInSymtab(Asm, *it,
988 Used || WeakrefUsed || isSignature,
Rafael Espindola88182132010-10-27 15:18:17 +0000989 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000990 continue;
991
Matt Fleming3565a062010-08-16 18:57:57 +0000992 ELFSymbolData MSD;
993 MSD.SymbolData = it;
Rafael Espindola88182132010-10-27 15:18:17 +0000994 const MCSymbol &RefSymbol = AliasedSymbol(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000995
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000996 // Undefined symbols are global, but this is the first place we
997 // are able to set it.
998 bool Local = isLocal(*it, isSignature, Used);
999 if (!Local && GetBinding(*it) == ELF::STB_LOCAL) {
1000 MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
1001 SetBinding(*it, ELF::STB_GLOBAL);
1002 SetBinding(SD, ELF::STB_GLOBAL);
1003 }
1004
Rafael Espindola484291c2010-11-01 14:28:48 +00001005 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
1006 SetBinding(*it, ELF::STB_WEAK);
1007
Rafael Espindolaf7c10a32010-09-21 00:24:38 +00001008 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +00001009 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +00001010 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +00001011 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +00001012 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +00001013 } else if (RefSymbol.isUndefined()) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001014 if (isSignature && !Used)
1015 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
1016 else
1017 MSD.SectionIndex = ELF::SHN_UNDEF;
Matt Fleming3565a062010-08-16 18:57:57 +00001018 } else {
Rafael Espindolabab2a802010-11-10 21:51:05 +00001019 const MCSectionELF &Section =
1020 static_cast<const MCSectionELF&>(RefSymbol.getSection());
1021 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001022 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
1023 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +00001024 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +00001025 }
1026
Rafael Espindola88182132010-10-27 15:18:17 +00001027 // The @@@ in symbol version is replaced with @ in undefined symbols and
1028 // @@ in defined ones.
1029 StringRef Name = Symbol.getName();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +00001030 SmallString<32> Buf;
1031
Rafael Espindola88182132010-10-27 15:18:17 +00001032 size_t Pos = Name.find("@@@");
Rafael Espindola88182132010-10-27 15:18:17 +00001033 if (Pos != StringRef::npos) {
Benjamin Kramer1261a2f2010-11-12 19:26:04 +00001034 Buf += Name.substr(0, Pos);
1035 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
1036 Buf += Name.substr(Pos + Skip);
1037 Name = Buf;
Rafael Espindola88182132010-10-27 15:18:17 +00001038 }
1039
Benjamin Kramer1261a2f2010-11-12 19:26:04 +00001040 uint64_t &Entry = StringIndexMap[Name];
Rafael Espindolaa6866962010-10-27 14:44:52 +00001041 if (!Entry) {
1042 Entry = StringTable.size();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +00001043 StringTable += Name;
Rafael Espindolaa6866962010-10-27 14:44:52 +00001044 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +00001045 }
Rafael Espindolaa6866962010-10-27 14:44:52 +00001046 MSD.StringIndex = Entry;
1047 if (MSD.SectionIndex == ELF::SHN_UNDEF)
1048 UndefinedSymbolData.push_back(MSD);
1049 else if (Local)
1050 LocalSymbolData.push_back(MSD);
1051 else
1052 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +00001053 }
1054
1055 // Symbols are required to be in lexicographic order.
1056 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
1057 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
1058 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
1059
1060 // Set the symbol indices. Local symbols must come before all other
1061 // symbols with non-local bindings.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +00001062 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +00001063 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
1064 LocalSymbolData[i].SymbolData->setIndex(Index++);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +00001065
1066 Index += NumRegularSections;
1067
Matt Fleming3565a062010-08-16 18:57:57 +00001068 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
1069 ExternalSymbolData[i].SymbolData->setIndex(Index++);
1070 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
1071 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
1072}
1073
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001074void ELFObjectWriter::WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
1075 const MCSectionData &SD) {
Matt Fleming3565a062010-08-16 18:57:57 +00001076 if (!Relocations[&SD].empty()) {
1077 MCContext &Ctx = Asm.getContext();
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001078 const MCSectionELF *RelaSection;
Matt Fleming3565a062010-08-16 18:57:57 +00001079 const MCSectionELF &Section =
1080 static_cast<const MCSectionELF&>(SD.getSection());
1081
1082 const StringRef SectionName = Section.getSectionName();
Benjamin Kramer377a5722010-08-17 17:30:07 +00001083 std::string RelaSectionName = HasRelocationAddend ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +00001084 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +00001085
1086 unsigned EntrySize;
1087 if (HasRelocationAddend)
1088 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
1089 else
1090 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +00001091
Benjamin Kramer377a5722010-08-17 17:30:07 +00001092 RelaSection = Ctx.getELFSection(RelaSectionName, HasRelocationAddend ?
1093 ELF::SHT_RELA : ELF::SHT_REL, 0,
Matt Fleming3565a062010-08-16 18:57:57 +00001094 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001095 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001096
1097 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001098 RelaSD.setAlignment(Is64Bit ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +00001099
1100 MCDataFragment *F = new MCDataFragment(&RelaSD);
1101
1102 WriteRelocationsFragment(Asm, F, &SD);
1103
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001104 Asm.AddSectionToTheEnd(*this, RelaSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +00001105 }
1106}
1107
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001108void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
1109 uint64_t Flags, uint64_t Address,
1110 uint64_t Offset, uint64_t Size,
1111 uint32_t Link, uint32_t Info,
1112 uint64_t Alignment,
1113 uint64_t EntrySize) {
Matt Fleming3565a062010-08-16 18:57:57 +00001114 Write32(Name); // sh_name: index into string table
1115 Write32(Type); // sh_type
1116 WriteWord(Flags); // sh_flags
1117 WriteWord(Address); // sh_addr
1118 WriteWord(Offset); // sh_offset
1119 WriteWord(Size); // sh_size
1120 Write32(Link); // sh_link
1121 Write32(Info); // sh_info
1122 WriteWord(Alignment); // sh_addralign
1123 WriteWord(EntrySize); // sh_entsize
1124}
1125
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001126void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
1127 MCDataFragment *F,
1128 const MCSectionData *SD) {
Matt Fleming3565a062010-08-16 18:57:57 +00001129 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
1130 // sort by the r_offset just like gnu as does
1131 array_pod_sort(Relocs.begin(), Relocs.end());
1132
1133 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1134 ELFRelocationEntry entry = Relocs[e - i - 1];
1135
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001136 if (entry.Index < 0)
1137 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
1138 else
1139 entry.Index += LocalSymbolData.size() + 1;
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001140 if (Is64Bit) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001141 String64(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001142
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001143 struct ELF::Elf64_Rela ERE64;
1144 ERE64.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001145 String64(*F, ERE64.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001146
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001147 if (HasRelocationAddend)
1148 String64(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001149 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001150 String32(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001151
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001152 struct ELF::Elf32_Rela ERE32;
1153 ERE32.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001154 String32(*F, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001155
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001156 if (HasRelocationAddend)
1157 String32(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001158 }
Matt Fleming3565a062010-08-16 18:57:57 +00001159 }
1160}
1161
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001162void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
1163 MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001164 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +00001165 MCContext &Ctx = Asm.getContext();
1166 MCDataFragment *F;
1167
Matt Fleming3565a062010-08-16 18:57:57 +00001168 unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
1169
Rafael Espindola38738bf2010-09-22 19:04:41 +00001170 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001171 const MCSectionELF *ShstrtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001172 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001173 SectionKind::getReadOnly());
Rafael Espindola71859c62010-09-16 19:46:31 +00001174 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1175 ShstrtabSD.setAlignment(1);
1176 ShstrtabIndex = Asm.size();
1177
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001178 const MCSectionELF *SymtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001179 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1180 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001181 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001182 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Matt Fleming3565a062010-08-16 18:57:57 +00001183 SymtabSD.setAlignment(Is64Bit ? 8 : 4);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001184 SymbolTableIndex = Asm.size();
1185
1186 MCSectionData *SymtabShndxSD = NULL;
1187
1188 if (NeedsSymtabShndx) {
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001189 const MCSectionELF *SymtabShndxSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001190 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001191 SectionKind::getReadOnly(), 4, "");
Rafael Espindola7be2c332010-10-31 00:16:26 +00001192 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
1193 SymtabShndxSD->setAlignment(4);
1194 }
Matt Fleming3565a062010-08-16 18:57:57 +00001195
Matt Fleming3565a062010-08-16 18:57:57 +00001196 const MCSection *StrtabSection;
1197 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001198 SectionKind::getReadOnly());
Matt Fleming3565a062010-08-16 18:57:57 +00001199 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1200 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +00001201 StringTableIndex = Asm.size();
1202
Rafael Espindolac3c413f2010-09-27 22:04:54 +00001203 WriteRelocations(Asm, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001204
1205 // Symbol table
1206 F = new MCDataFragment(&SymtabSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001207 MCDataFragment *ShndxF = NULL;
1208 if (NeedsSymtabShndx) {
1209 ShndxF = new MCDataFragment(SymtabShndxSD);
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001210 Asm.AddSectionToTheEnd(*this, *SymtabShndxSD, Layout);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001211 }
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001212 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001213 Asm.AddSectionToTheEnd(*this, SymtabSD, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001214
Matt Fleming3565a062010-08-16 18:57:57 +00001215 F = new MCDataFragment(&StrtabSD);
1216 F->getContents().append(StringTable.begin(), StringTable.end());
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001217 Asm.AddSectionToTheEnd(*this, StrtabSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +00001218
Matt Fleming3565a062010-08-16 18:57:57 +00001219 F = new MCDataFragment(&ShstrtabSD);
1220
Matt Fleming3565a062010-08-16 18:57:57 +00001221 // Section header string table.
1222 //
1223 // The first entry of a string table holds a null character so skip
1224 // section 0.
1225 uint64_t Index = 1;
1226 F->getContents() += '\x00';
1227
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001228 StringMap<uint64_t> SecStringMap;
Matt Fleming3565a062010-08-16 18:57:57 +00001229 for (MCAssembler::const_iterator it = Asm.begin(),
1230 ie = Asm.end(); it != ie; ++it) {
Matt Fleming3565a062010-08-16 18:57:57 +00001231 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +00001232 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola51efe7a2010-09-23 14:14:56 +00001233 // FIXME: We could merge suffixes like in .text and .rela.text.
Matt Fleming3565a062010-08-16 18:57:57 +00001234
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001235 StringRef Name = Section.getSectionName();
1236 if (SecStringMap.count(Name)) {
1237 SectionStringTableIndex[&Section] = SecStringMap[Name];
1238 continue;
1239 }
Matt Fleming3565a062010-08-16 18:57:57 +00001240 // Remember the index into the string table so we can write it
1241 // into the sh_name field of the section header table.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001242 SectionStringTableIndex[&Section] = Index;
1243 SecStringMap[Name] = Index;
Matt Fleming3565a062010-08-16 18:57:57 +00001244
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001245 Index += Name.size() + 1;
1246 F->getContents() += Name;
Matt Fleming3565a062010-08-16 18:57:57 +00001247 F->getContents() += '\x00';
1248 }
1249
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001250 Asm.AddSectionToTheEnd(*this, ShstrtabSD, Layout);
Rafael Espindola70703872010-09-30 02:22:20 +00001251}
1252
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001253bool ELFObjectWriter::IsFixupFullyResolved(const MCAssembler &Asm,
1254 const MCValue Target,
1255 bool IsPCRel,
1256 const MCFragment *DF) const {
Rafael Espindola70703872010-09-30 02:22:20 +00001257 // If this is a PCrel relocation, find the section this fixup value is
1258 // relative to.
1259 const MCSection *BaseSection = 0;
1260 if (IsPCRel) {
1261 BaseSection = &DF->getParent()->getSection();
1262 assert(BaseSection);
1263 }
1264
1265 const MCSection *SectionA = 0;
1266 const MCSymbol *SymbolA = 0;
1267 if (const MCSymbolRefExpr *A = Target.getSymA()) {
1268 SymbolA = &A->getSymbol();
1269 SectionA = &SymbolA->getSection();
1270 }
1271
1272 const MCSection *SectionB = 0;
1273 if (const MCSymbolRefExpr *B = Target.getSymB()) {
1274 SectionB = &B->getSymbol().getSection();
1275 }
1276
1277 if (!BaseSection)
1278 return SectionA == SectionB;
1279
1280 const MCSymbolData &DataA = Asm.getSymbolData(*SymbolA);
1281 if (DataA.isExternal())
1282 return false;
1283
1284 return !SectionB && BaseSection == SectionA;
Matt Fleming3565a062010-08-16 18:57:57 +00001285}
1286
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001287void ELFObjectWriter::CreateGroupSections(MCAssembler &Asm,
1288 MCAsmLayout &Layout,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001289 GroupMapTy &GroupMap,
1290 RevGroupMapTy &RevGroupMap) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001291 // Build the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001292 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1293 it != ie; ++it) {
1294 const MCSectionELF &Section =
1295 static_cast<const MCSectionELF&>(it->getSection());
1296 if (!(Section.getFlags() & MCSectionELF::SHF_GROUP))
1297 continue;
1298
1299 const MCSymbol *SignatureSymbol = Section.getGroup();
1300 Asm.getOrCreateSymbolData(*SignatureSymbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001301 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001302 if (!Group) {
1303 Group = Asm.getContext().CreateELFGroupSection();
1304 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1305 Data.setAlignment(4);
1306 MCDataFragment *F = new MCDataFragment(&Data);
1307 String32(*F, ELF::GRP_COMDAT);
1308 }
1309 GroupMap[Group] = SignatureSymbol;
1310 }
1311
1312 // Add sections to the groups
1313 unsigned Index = 1;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001314 unsigned NumGroups = RevGroupMap.size();
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001315 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1316 it != ie; ++it, ++Index) {
1317 const MCSectionELF &Section =
1318 static_cast<const MCSectionELF&>(it->getSection());
1319 if (!(Section.getFlags() & MCSectionELF::SHF_GROUP))
1320 continue;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001321 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001322 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1323 // FIXME: we could use the previous fragment
1324 MCDataFragment *F = new MCDataFragment(&Data);
1325 String32(*F, NumGroups + Index);
1326 }
1327
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001328 for (RevGroupMapTy::const_iterator i = RevGroupMap.begin(),
1329 e = RevGroupMap.end(); i != e; ++i) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001330 const MCSectionELF *Group = i->second;
1331 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001332 Asm.AddSectionToTheEnd(*this, Data, Layout);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001333 }
1334}
1335
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001336void ELFObjectWriter::WriteSection(MCAssembler &Asm,
1337 const SectionIndexMapTy &SectionIndexMap,
1338 uint32_t GroupSymbolIndex,
1339 uint64_t Offset, uint64_t Size,
1340 uint64_t Alignment,
1341 const MCSectionELF &Section) {
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001342 uint64_t sh_link = 0;
1343 uint64_t sh_info = 0;
1344
1345 switch(Section.getType()) {
1346 case ELF::SHT_DYNAMIC:
1347 sh_link = SectionStringTableIndex[&Section];
1348 sh_info = 0;
1349 break;
1350
1351 case ELF::SHT_REL:
1352 case ELF::SHT_RELA: {
1353 const MCSectionELF *SymtabSection;
1354 const MCSectionELF *InfoSection;
1355 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
1356 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001357 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001358 sh_link = SectionIndexMap.lookup(SymtabSection);
1359 assert(sh_link && ".symtab not found");
1360
1361 // Remove ".rel" and ".rela" prefixes.
1362 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1363 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
1364
1365 InfoSection = Asm.getContext().getELFSection(SectionName,
1366 ELF::SHT_PROGBITS, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001367 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001368 sh_info = SectionIndexMap.lookup(InfoSection);
1369 break;
1370 }
1371
1372 case ELF::SHT_SYMTAB:
1373 case ELF::SHT_DYNSYM:
1374 sh_link = StringTableIndex;
1375 sh_info = LastLocalSymbolIndex;
1376 break;
1377
1378 case ELF::SHT_SYMTAB_SHNDX:
1379 sh_link = SymbolTableIndex;
1380 break;
1381
1382 case ELF::SHT_PROGBITS:
1383 case ELF::SHT_STRTAB:
1384 case ELF::SHT_NOBITS:
1385 case ELF::SHT_NULL:
1386 case ELF::SHT_ARM_ATTRIBUTES:
1387 // Nothing to do.
1388 break;
1389
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001390 case ELF::SHT_GROUP: {
1391 sh_link = SymbolTableIndex;
1392 sh_info = GroupSymbolIndex;
1393 break;
1394 }
1395
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001396 default:
1397 assert(0 && "FIXME: sh_type value not supported!");
1398 break;
1399 }
1400
1401 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1402 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1403 Alignment, Section.getEntrySize());
1404}
1405
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001406void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1407 const MCAsmLayout &Layout) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001408 GroupMapTy GroupMap;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001409 RevGroupMapTy RevGroupMap;
1410 CreateGroupSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1411 RevGroupMap);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001412
Rafael Espindolabab2a802010-11-10 21:51:05 +00001413 SectionIndexMapTy SectionIndexMap;
1414
1415 ComputeIndexMap(Asm, SectionIndexMap);
1416
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001417 // Compute symbol table information.
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001418 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap);
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001419
Matt Fleming3565a062010-08-16 18:57:57 +00001420 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001421 const_cast<MCAsmLayout&>(Layout),
1422 SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +00001423
Rafael Espindola1d739a02010-11-10 22:34:07 +00001424 // Update to include the metadata sections.
1425 ComputeIndexMap(Asm, SectionIndexMap);
1426
Matt Fleming3565a062010-08-16 18:57:57 +00001427 // Add 1 for the null section.
1428 unsigned NumSections = Asm.size() + 1;
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001429 uint64_t NaturalAlignment = Is64Bit ? 8 : 4;
1430 uint64_t HeaderSize = Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr);
1431 uint64_t FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001432
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001433 std::vector<const MCSectionELF*> Sections;
1434 Sections.resize(NumSections);
1435
1436 for (SectionIndexMapTy::const_iterator i=
1437 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1438 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
1439 Sections[p.second] = p.first;
1440 }
1441
1442 for (unsigned i = 1; i < NumSections; ++i) {
1443 const MCSectionELF &Section = *Sections[i];
1444 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001445
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001446 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1447
Matt Fleming3565a062010-08-16 18:57:57 +00001448 // Get the size of the section in the output file (including padding).
1449 uint64_t Size = Layout.getSectionFileSize(&SD);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001450
1451 FileOff += Size;
Matt Fleming3565a062010-08-16 18:57:57 +00001452 }
1453
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001454 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1455
Matt Fleming3565a062010-08-16 18:57:57 +00001456 // Write out the ELF header ...
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001457 WriteHeader(FileOff - HeaderSize, NumSections);
1458
1459 FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001460
1461 // ... then all of the sections ...
1462 DenseMap<const MCSection*, uint64_t> SectionOffsetMap;
1463
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001464 for (unsigned i = 1; i < NumSections; ++i) {
1465 const MCSectionELF &Section = *Sections[i];
1466 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001467
1468 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1469 WriteZeros(Padding);
1470 FileOff += Padding;
1471
Matt Fleming3565a062010-08-16 18:57:57 +00001472 // Remember the offset into the file for this section.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001473 SectionOffsetMap[&Section] = FileOff;
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001474
Matt Fleming3565a062010-08-16 18:57:57 +00001475 FileOff += Layout.getSectionFileSize(&SD);
1476
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001477 Asm.WriteSectionData(&SD, Layout, this);
Matt Fleming3565a062010-08-16 18:57:57 +00001478 }
1479
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001480 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1481 WriteZeros(Padding);
1482 FileOff += Padding;
1483
Matt Fleming3565a062010-08-16 18:57:57 +00001484 // ... and then the section header table.
1485 // Should we align the section header table?
1486 //
1487 // Null section first.
Rafael Espindola7be2c332010-10-31 00:16:26 +00001488 uint64_t FirstSectionSize =
1489 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1490 uint32_t FirstSectionLink =
1491 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1492 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming3565a062010-08-16 18:57:57 +00001493
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001494 for (unsigned i = 1; i < NumSections; ++i) {
1495 const MCSectionELF &Section = *Sections[i];
1496 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1497 uint32_t GroupSymbolIndex;
1498 if (Section.getType() != ELF::SHT_GROUP)
1499 GroupSymbolIndex = 0;
1500 else
1501 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm, GroupMap[&Section]);
Matt Fleming3565a062010-08-16 18:57:57 +00001502
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001503 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
1504 SectionOffsetMap[&Section], Layout.getSectionSize(&SD),
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001505 SD.getAlignment(), Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001506 }
1507}
1508
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001509MCObjectWriter *llvm::createELFObjectWriter(raw_ostream &OS,
1510 bool Is64Bit,
1511 Triple::OSType OSType,
1512 uint16_t EMachine,
1513 bool IsLittleEndian,
1514 bool HasRelocationAddend) {
1515 return new ELFObjectWriter(OS, Is64Bit, IsLittleEndian, EMachine,
1516 HasRelocationAddend, OSType);
Matt Fleming3565a062010-08-16 18:57:57 +00001517}