blob: bc9a2b67e75f8515cd55392fc04e83a423186aab [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
14#include "llvm/MC/ELFObjectWriter.h"
Rafael Espindola8f413fa2010-10-05 15:11:03 +000015#include "llvm/ADT/SmallPtrSet.h"
Matt Fleming3565a062010-08-16 18:57:57 +000016#include "llvm/ADT/STLExtras.h"
17#include "llvm/ADT/StringMap.h"
18#include "llvm/ADT/Twine.h"
19#include "llvm/MC/MCAssembler.h"
20#include "llvm/MC/MCAsmLayout.h"
21#include "llvm/MC/MCContext.h"
22#include "llvm/MC/MCELFSymbolFlags.h"
23#include "llvm/MC/MCExpr.h"
24#include "llvm/MC/MCObjectWriter.h"
25#include "llvm/MC/MCSectionELF.h"
26#include "llvm/MC/MCSymbol.h"
27#include "llvm/MC/MCValue.h"
28#include "llvm/Support/Debug.h"
29#include "llvm/Support/ErrorHandling.h"
30#include "llvm/Support/ELF.h"
31#include "llvm/Target/TargetAsmBackend.h"
32
33#include "../Target/X86/X86FixupKinds.h"
34
35#include <vector>
36using namespace llvm;
37
Rafael Espindolaad49cf52010-09-18 15:03:21 +000038static unsigned GetType(const MCSymbolData &SD) {
39 uint32_t Type = (SD.getFlags() & (0xf << ELF_STT_Shift)) >> ELF_STT_Shift;
40 assert(Type == ELF::STT_NOTYPE || Type == ELF::STT_OBJECT ||
41 Type == ELF::STT_FUNC || Type == ELF::STT_SECTION ||
42 Type == ELF::STT_FILE || Type == ELF::STT_COMMON ||
43 Type == ELF::STT_TLS);
44 return Type;
45}
46
Rafael Espindolae15eb4e2010-09-23 19:55:14 +000047static unsigned GetBinding(const MCSymbolData &SD) {
48 uint32_t Binding = (SD.getFlags() & (0xf << ELF_STB_Shift)) >> ELF_STB_Shift;
49 assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
50 Binding == ELF::STB_WEAK);
51 return Binding;
52}
53
54static void SetBinding(MCSymbolData &SD, unsigned Binding) {
55 assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
56 Binding == ELF::STB_WEAK);
57 uint32_t OtherFlags = SD.getFlags() & ~(0xf << ELF_STB_Shift);
58 SD.setFlags(OtherFlags | (Binding << ELF_STB_Shift));
59}
60
Rafael Espindola152c1062010-10-06 21:02:29 +000061static unsigned GetVisibility(MCSymbolData &SD) {
62 unsigned Visibility =
63 (SD.getFlags() & (0xf << ELF_STV_Shift)) >> ELF_STV_Shift;
64 assert(Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_INTERNAL ||
65 Visibility == ELF::STV_HIDDEN || Visibility == ELF::STV_PROTECTED);
66 return Visibility;
67}
68
Rafael Espindolacebdc012010-10-04 19:46:28 +000069static bool isFixupKindX86PCRel(unsigned Kind) {
70 switch (Kind) {
71 default:
72 return false;
73 case X86::reloc_pcrel_1byte:
74 case X86::reloc_pcrel_4byte:
75 case X86::reloc_riprel_4byte:
76 case X86::reloc_riprel_4byte_movq_load:
77 return true;
78 }
79}
80
Rafael Espindolaa0a2f872010-10-28 14:22:44 +000081static bool RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant) {
82 switch (Variant) {
Rafael Espindola5c77c162010-10-05 15:48:37 +000083 default:
84 return false;
Rafael Espindolaa0a2f872010-10-28 14:22:44 +000085 case MCSymbolRefExpr::VK_GOT:
86 case MCSymbolRefExpr::VK_PLT:
87 case MCSymbolRefExpr::VK_GOTPCREL:
88 case MCSymbolRefExpr::VK_TPOFF:
89 case MCSymbolRefExpr::VK_TLSGD:
90 case MCSymbolRefExpr::VK_GOTTPOFF:
91 case MCSymbolRefExpr::VK_INDNTPOFF:
92 case MCSymbolRefExpr::VK_NTPOFF:
93 case MCSymbolRefExpr::VK_GOTNTPOFF:
Rafael Espindolaa264f722010-10-28 14:37:09 +000094 case MCSymbolRefExpr::VK_TLSLDM:
Rafael Espindola0cf15d62010-10-28 14:48:59 +000095 case MCSymbolRefExpr::VK_DTPOFF:
Rafael Espindolab4d17212010-10-28 15:02:40 +000096 case MCSymbolRefExpr::VK_TLSLD:
Rafael Espindola5c77c162010-10-05 15:48:37 +000097 return true;
98 }
99}
100
Matt Fleming3565a062010-08-16 18:57:57 +0000101namespace {
102
103 class ELFObjectWriterImpl {
Chris Lattnerb188a372010-08-28 03:21:03 +0000104 /*static bool isFixupKindX86RIPRel(unsigned Kind) {
Matt Fleming3565a062010-08-16 18:57:57 +0000105 return Kind == X86::reloc_riprel_4byte ||
106 Kind == X86::reloc_riprel_4byte_movq_load;
Chris Lattnerb188a372010-08-28 03:21:03 +0000107 }*/
Matt Fleming3565a062010-08-16 18:57:57 +0000108
109
110 /// ELFSymbolData - Helper struct for containing some precomputed information
111 /// on symbols.
112 struct ELFSymbolData {
113 MCSymbolData *SymbolData;
114 uint64_t StringIndex;
115 uint32_t SectionIndex;
116
117 // Support lexicographic sorting.
118 bool operator<(const ELFSymbolData &RHS) const {
Rafael Espindolaad49cf52010-09-18 15:03:21 +0000119 if (GetType(*SymbolData) == ELF::STT_FILE)
120 return true;
121 if (GetType(*RHS.SymbolData) == ELF::STT_FILE)
122 return false;
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000123 return SymbolData->getSymbol().getName() <
124 RHS.SymbolData->getSymbol().getName();
Matt Fleming3565a062010-08-16 18:57:57 +0000125 }
126 };
127
128 /// @name Relocation Data
129 /// @{
130
131 struct ELFRelocationEntry {
132 // Make these big enough for both 32-bit and 64-bit
133 uint64_t r_offset;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000134 int Index;
135 unsigned Type;
136 const MCSymbol *Symbol;
Matt Fleming3565a062010-08-16 18:57:57 +0000137 uint64_t r_addend;
138
139 // Support lexicographic sorting.
140 bool operator<(const ELFRelocationEntry &RE) const {
141 return RE.r_offset < r_offset;
142 }
143 };
144
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000145 SmallPtrSet<const MCSymbol *, 16> UsedInReloc;
Rafael Espindola484291c2010-11-01 14:28:48 +0000146 SmallPtrSet<const MCSymbol *, 16> WeakrefUsedInReloc;
Rafael Espindola88182132010-10-27 15:18:17 +0000147 DenseMap<const MCSymbol *, const MCSymbol *> Renames;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000148
Matt Fleming3565a062010-08-16 18:57:57 +0000149 llvm::DenseMap<const MCSectionData*,
150 std::vector<ELFRelocationEntry> > Relocations;
151 DenseMap<const MCSection*, uint64_t> SectionStringTableIndex;
152
153 /// @}
154 /// @name Symbol Table Data
155 /// @{
156
157 SmallString<256> StringTable;
158 std::vector<ELFSymbolData> LocalSymbolData;
159 std::vector<ELFSymbolData> ExternalSymbolData;
160 std::vector<ELFSymbolData> UndefinedSymbolData;
161
162 /// @}
163
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000164 int NumRegularSections;
165
Rafael Espindola5c77c162010-10-05 15:48:37 +0000166 bool NeedsGOT;
167
Rafael Espindola7be2c332010-10-31 00:16:26 +0000168 bool NeedsSymtabShndx;
169
Matt Fleming3565a062010-08-16 18:57:57 +0000170 ELFObjectWriter *Writer;
171
172 raw_ostream &OS;
173
Matt Fleming3565a062010-08-16 18:57:57 +0000174 unsigned Is64Bit : 1;
175
176 bool HasRelocationAddend;
177
Roman Divacky5baf79e2010-09-09 17:57:50 +0000178 Triple::OSType OSType;
179
Wesley Peckeecb8582010-10-22 15:52:49 +0000180 uint16_t EMachine;
181
Matt Fleming3565a062010-08-16 18:57:57 +0000182 // This holds the symbol table index of the last local symbol.
183 unsigned LastLocalSymbolIndex;
184 // This holds the .strtab section index.
185 unsigned StringTableIndex;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000186 // This holds the .symtab section index.
187 unsigned SymbolTableIndex;
Matt Fleming3565a062010-08-16 18:57:57 +0000188
189 unsigned ShstrtabIndex;
190
191 public:
192 ELFObjectWriterImpl(ELFObjectWriter *_Writer, bool _Is64Bit,
Wesley Peckeecb8582010-10-22 15:52:49 +0000193 uint16_t _EMachine, bool _HasRelAddend,
194 Triple::OSType _OSType)
Rafael Espindola7be2c332010-10-31 00:16:26 +0000195 : NeedsGOT(false), NeedsSymtabShndx(false), Writer(_Writer),
196 OS(Writer->getStream()),
Roman Divacky5baf79e2010-09-09 17:57:50 +0000197 Is64Bit(_Is64Bit), HasRelocationAddend(_HasRelAddend),
Wesley Peckeecb8582010-10-22 15:52:49 +0000198 OSType(_OSType), EMachine(_EMachine) {
Matt Fleming3565a062010-08-16 18:57:57 +0000199 }
200
201 void Write8(uint8_t Value) { Writer->Write8(Value); }
202 void Write16(uint16_t Value) { Writer->Write16(Value); }
203 void Write32(uint32_t Value) { Writer->Write32(Value); }
Chris Lattnerb188a372010-08-28 03:21:03 +0000204 //void Write64(uint64_t Value) { Writer->Write64(Value); }
Matt Fleming3565a062010-08-16 18:57:57 +0000205 void WriteZeros(unsigned N) { Writer->WriteZeros(N); }
Chris Lattnerb188a372010-08-28 03:21:03 +0000206 //void WriteBytes(StringRef Str, unsigned ZeroFillSize = 0) {
207 // Writer->WriteBytes(Str, ZeroFillSize);
208 //}
Matt Fleming3565a062010-08-16 18:57:57 +0000209
210 void WriteWord(uint64_t W) {
Chris Lattnerb188a372010-08-28 03:21:03 +0000211 if (Is64Bit)
Matt Fleming3565a062010-08-16 18:57:57 +0000212 Writer->Write64(W);
Chris Lattnerb188a372010-08-28 03:21:03 +0000213 else
Matt Fleming3565a062010-08-16 18:57:57 +0000214 Writer->Write32(W);
Matt Fleming3565a062010-08-16 18:57:57 +0000215 }
216
Matt Fleming3565a062010-08-16 18:57:57 +0000217 void StringLE16(char *buf, uint16_t Value) {
218 buf[0] = char(Value >> 0);
219 buf[1] = char(Value >> 8);
220 }
221
222 void StringLE32(char *buf, uint32_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000223 StringLE16(buf, uint16_t(Value >> 0));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000224 StringLE16(buf + 2, uint16_t(Value >> 16));
Matt Fleming3565a062010-08-16 18:57:57 +0000225 }
226
227 void StringLE64(char *buf, uint64_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000228 StringLE32(buf, uint32_t(Value >> 0));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000229 StringLE32(buf + 4, uint32_t(Value >> 32));
Matt Fleming3565a062010-08-16 18:57:57 +0000230 }
231
232 void StringBE16(char *buf ,uint16_t Value) {
233 buf[0] = char(Value >> 8);
234 buf[1] = char(Value >> 0);
235 }
236
237 void StringBE32(char *buf, uint32_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000238 StringBE16(buf, uint16_t(Value >> 16));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000239 StringBE16(buf + 2, uint16_t(Value >> 0));
Matt Fleming3565a062010-08-16 18:57:57 +0000240 }
241
242 void StringBE64(char *buf, uint64_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000243 StringBE32(buf, uint32_t(Value >> 32));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000244 StringBE32(buf + 4, uint32_t(Value >> 0));
Matt Fleming3565a062010-08-16 18:57:57 +0000245 }
246
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000247 void String8(MCDataFragment &F, uint8_t Value) {
248 char buf[1];
249 buf[0] = Value;
250 F.getContents() += StringRef(buf, 1);
251 }
252
253 void String16(MCDataFragment &F, uint16_t Value) {
254 char buf[2];
Matt Fleming3565a062010-08-16 18:57:57 +0000255 if (Writer->isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000256 StringLE16(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000257 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000258 StringBE16(buf, Value);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000259 F.getContents() += StringRef(buf, 2);
Matt Fleming3565a062010-08-16 18:57:57 +0000260 }
261
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000262 void String32(MCDataFragment &F, uint32_t Value) {
263 char buf[4];
Matt Fleming3565a062010-08-16 18:57:57 +0000264 if (Writer->isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000265 StringLE32(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000266 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000267 StringBE32(buf, Value);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000268 F.getContents() += StringRef(buf, 4);
Matt Fleming3565a062010-08-16 18:57:57 +0000269 }
270
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000271 void String64(MCDataFragment &F, uint64_t Value) {
272 char buf[8];
Matt Fleming3565a062010-08-16 18:57:57 +0000273 if (Writer->isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000274 StringLE64(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000275 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000276 StringBE64(buf, Value);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000277 F.getContents() += StringRef(buf, 8);
Matt Fleming3565a062010-08-16 18:57:57 +0000278 }
279
280 void WriteHeader(uint64_t SectionDataSize, unsigned NumberOfSections);
281
Rafael Espindola7be2c332010-10-31 00:16:26 +0000282 void WriteSymbolEntry(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
283 uint64_t name, uint8_t info,
Matt Fleming3565a062010-08-16 18:57:57 +0000284 uint64_t value, uint64_t size,
Rafael Espindola7be2c332010-10-31 00:16:26 +0000285 uint8_t other, uint32_t shndx,
286 bool Reserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000287
Rafael Espindola7be2c332010-10-31 00:16:26 +0000288 void WriteSymbol(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
289 ELFSymbolData &MSD,
Matt Fleming3565a062010-08-16 18:57:57 +0000290 const MCAsmLayout &Layout);
291
Rafael Espindolabab2a802010-11-10 21:51:05 +0000292 typedef DenseMap<const MCSectionELF*, uint32_t> SectionIndexMapTy;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000293 void WriteSymbolTable(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
294 const MCAssembler &Asm,
Rafael Espindola71859c62010-09-16 19:46:31 +0000295 const MCAsmLayout &Layout,
296 unsigned NumRegularSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000297
298 void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
299 const MCFragment *Fragment, const MCFixup &Fixup,
300 MCValue Target, uint64_t &FixedValue);
301
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000302 uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm,
303 const MCSymbol *S);
Matt Fleming3565a062010-08-16 18:57:57 +0000304
305 /// ComputeSymbolTable - Compute the symbol table data
306 ///
307 /// \param StringTable [out] - The string table data.
308 /// \param StringIndexMap [out] - Map from symbol names to offsets in the
309 /// string table.
Rafael Espindolabab2a802010-11-10 21:51:05 +0000310 void ComputeSymbolTable(MCAssembler &Asm,
311 const SectionIndexMapTy &SectionIndexMap);
312
313 void ComputeIndexMap(MCAssembler &Asm,
314 SectionIndexMapTy &SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +0000315
316 void WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
317 const MCSectionData &SD);
318
319 void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout) {
320 for (MCAssembler::const_iterator it = Asm.begin(),
321 ie = Asm.end(); it != ie; ++it) {
322 WriteRelocation(Asm, Layout, *it);
323 }
324 }
325
326 void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout);
327
Rafael Espindola88182132010-10-27 15:18:17 +0000328 void ExecutePostLayoutBinding(MCAssembler &Asm);
Matt Fleming3565a062010-08-16 18:57:57 +0000329
330 void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
331 uint64_t Address, uint64_t Offset,
332 uint64_t Size, uint32_t Link, uint32_t Info,
333 uint64_t Alignment, uint64_t EntrySize);
334
335 void WriteRelocationsFragment(const MCAssembler &Asm, MCDataFragment *F,
336 const MCSectionData *SD);
337
Rafael Espindola70703872010-09-30 02:22:20 +0000338 bool IsFixupFullyResolved(const MCAssembler &Asm,
339 const MCValue Target,
340 bool IsPCRel,
341 const MCFragment *DF) const;
342
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000343 void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000344 };
345
346}
347
348// Emit the ELF header.
349void ELFObjectWriterImpl::WriteHeader(uint64_t SectionDataSize,
350 unsigned NumberOfSections) {
351 // ELF Header
352 // ----------
353 //
354 // Note
355 // ----
356 // emitWord method behaves differently for ELF32 and ELF64, writing
357 // 4 bytes in the former and 8 in the latter.
358
359 Write8(0x7f); // e_ident[EI_MAG0]
360 Write8('E'); // e_ident[EI_MAG1]
361 Write8('L'); // e_ident[EI_MAG2]
362 Write8('F'); // e_ident[EI_MAG3]
363
364 Write8(Is64Bit ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
365
366 // e_ident[EI_DATA]
367 Write8(Writer->isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
368
369 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky5baf79e2010-09-09 17:57:50 +0000370 // e_ident[EI_OSABI]
371 switch (OSType) {
372 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
373 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
374 default: Write8(ELF::ELFOSABI_NONE); break;
375 }
Matt Fleming3565a062010-08-16 18:57:57 +0000376 Write8(0); // e_ident[EI_ABIVERSION]
377
378 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
379
380 Write16(ELF::ET_REL); // e_type
381
Wesley Peckeecb8582010-10-22 15:52:49 +0000382 Write16(EMachine); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +0000383
384 Write32(ELF::EV_CURRENT); // e_version
385 WriteWord(0); // e_entry, no entry point in .o file
386 WriteWord(0); // e_phoff, no program header for .o
Benjamin Kramereb976772010-08-17 17:02:29 +0000387 WriteWord(SectionDataSize + (Is64Bit ? sizeof(ELF::Elf64_Ehdr) :
388 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000389
390 // FIXME: Make this configurable.
391 Write32(0); // e_flags = whatever the target wants
392
393 // e_ehsize = ELF header size
394 Write16(Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
395
396 Write16(0); // e_phentsize = prog header entry size
397 Write16(0); // e_phnum = # prog header entries = 0
398
399 // e_shentsize = Section header entry size
400 Write16(Is64Bit ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
401
402 // e_shnum = # of section header ents
Rafael Espindola7be2c332010-10-31 00:16:26 +0000403 if (NumberOfSections >= ELF::SHN_LORESERVE)
404 Write16(0);
405 else
406 Write16(NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000407
408 // e_shstrndx = Section # of '.shstrtab'
Rafael Espindola7be2c332010-10-31 00:16:26 +0000409 if (NumberOfSections >= ELF::SHN_LORESERVE)
410 Write16(ELF::SHN_XINDEX);
411 else
412 Write16(ShstrtabIndex);
Matt Fleming3565a062010-08-16 18:57:57 +0000413}
414
Rafael Espindola7be2c332010-10-31 00:16:26 +0000415void ELFObjectWriterImpl::WriteSymbolEntry(MCDataFragment *SymtabF,
416 MCDataFragment *ShndxF,
417 uint64_t name,
Matt Fleming3565a062010-08-16 18:57:57 +0000418 uint8_t info, uint64_t value,
419 uint64_t size, uint8_t other,
Rafael Espindola7be2c332010-10-31 00:16:26 +0000420 uint32_t shndx,
421 bool Reserved) {
422 if (ShndxF) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000423 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000424 String32(*ShndxF, shndx);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000425 else
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000426 String32(*ShndxF, 0);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000427 }
428
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000429 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
430 uint16_t(ELF::SHN_XINDEX) : shndx;
431
Matt Fleming3565a062010-08-16 18:57:57 +0000432 if (Is64Bit) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000433 String32(*SymtabF, name); // st_name
434 String8(*SymtabF, info); // st_info
435 String8(*SymtabF, other); // st_other
436 String16(*SymtabF, Index); // st_shndx
437 String64(*SymtabF, value); // st_value
438 String64(*SymtabF, size); // st_size
Matt Fleming3565a062010-08-16 18:57:57 +0000439 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000440 String32(*SymtabF, name); // st_name
441 String32(*SymtabF, value); // st_value
442 String32(*SymtabF, size); // st_size
443 String8(*SymtabF, info); // st_info
444 String8(*SymtabF, other); // st_other
445 String16(*SymtabF, Index); // st_shndx
Matt Fleming3565a062010-08-16 18:57:57 +0000446 }
447}
448
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000449static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout) {
450 if (Data.isCommon() && Data.isExternal())
451 return Data.getCommonAlignment();
452
453 const MCSymbol &Symbol = Data.getSymbol();
454 if (!Symbol.isInSection())
455 return 0;
456
Rafael Espindola1973d432010-10-28 19:39:57 +0000457 if (MCFragment *FF = Data.getFragment())
458 return Layout.getSymbolAddress(&Data) -
459 Layout.getSectionAddress(FF->getParent());
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000460
461 return 0;
462}
463
Rafael Espindolade89b012010-10-15 18:25:33 +0000464static const MCSymbol &AliasedSymbol(const MCSymbol &Symbol) {
465 const MCSymbol *S = &Symbol;
466 while (S->isVariable()) {
467 const MCExpr *Value = S->getVariableValue();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000468 if (Value->getKind() != MCExpr::SymbolRef)
469 return *S;
Rafael Espindolade89b012010-10-15 18:25:33 +0000470 const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr*>(Value);
471 S = &Ref->getSymbol();
472 }
473 return *S;
474}
475
Rafael Espindola88182132010-10-27 15:18:17 +0000476void ELFObjectWriterImpl::ExecutePostLayoutBinding(MCAssembler &Asm) {
477 // The presence of symbol versions causes undefined symbols and
478 // versions declared with @@@ to be renamed.
479
480 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
481 ie = Asm.symbol_end(); it != ie; ++it) {
482 const MCSymbol &Alias = it->getSymbol();
Rafael Espindola88182132010-10-27 15:18:17 +0000483 const MCSymbol &Symbol = AliasedSymbol(Alias);
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000484 MCSymbolData &SD = Asm.getSymbolData(Symbol);
485
486 // Undefined symbols are global, but this is the first place we
487 // are able to set it.
488 if (Symbol.isUndefined() && !Symbol.isVariable()) {
489 if (GetBinding(SD) == ELF::STB_LOCAL) {
490 SetBinding(SD, ELF::STB_GLOBAL);
491 SetBinding(*it, ELF::STB_GLOBAL);
492 }
493 }
494
495 // Not an alias.
496 if (&Symbol == &Alias)
497 continue;
498
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000499 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000500 size_t Pos = AliasName.find('@');
501 if (Pos == StringRef::npos)
502 continue;
503
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000504 // Aliases defined with .symvar copy the binding from the symbol they alias.
505 // This is the first place we are able to copy this information.
506 it->setExternal(SD.isExternal());
507 SetBinding(*it, GetBinding(SD));
508
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000509 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000510 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
511 continue;
512
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000513 // FIXME: produce a better error message.
514 if (Symbol.isUndefined() && Rest.startswith("@@") &&
515 !Rest.startswith("@@@"))
516 report_fatal_error("A @@ version cannot be undefined");
517
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000518 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000519 }
520}
521
Rafael Espindola7be2c332010-10-31 00:16:26 +0000522void ELFObjectWriterImpl::WriteSymbol(MCDataFragment *SymtabF,
523 MCDataFragment *ShndxF,
524 ELFSymbolData &MSD,
Matt Fleming3565a062010-08-16 18:57:57 +0000525 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000526 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000527 MCSymbolData &Data =
528 Layout.getAssembler().getSymbolData(AliasedSymbol(OrigData.getSymbol()));
Rafael Espindola152c1062010-10-06 21:02:29 +0000529
Rafael Espindola7be2c332010-10-31 00:16:26 +0000530 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
531 Data.getSymbol().isVariable();
532
Rafael Espindola152c1062010-10-06 21:02:29 +0000533 uint8_t Binding = GetBinding(OrigData);
534 uint8_t Visibility = GetVisibility(OrigData);
535 uint8_t Type = GetType(Data);
536
537 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
538 uint8_t Other = Visibility;
539
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000540 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000541 uint64_t Size = 0;
542 const MCExpr *ESize;
543
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000544 assert(!(Data.isCommon() && !Data.isExternal()));
545
Matt Fleming3565a062010-08-16 18:57:57 +0000546 ESize = Data.getSize();
547 if (Data.getSize()) {
548 MCValue Res;
549 if (ESize->getKind() == MCExpr::Binary) {
550 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(ESize);
551
552 if (BE->EvaluateAsRelocatable(Res, &Layout)) {
Benjamin Kramer24f12062010-10-17 07:38:40 +0000553 assert(!Res.getSymA() || !Res.getSymA()->getSymbol().isDefined());
554 assert(!Res.getSymB() || !Res.getSymB()->getSymbol().isDefined());
Rafael Espindolaf230df92010-10-16 18:23:53 +0000555 Size = Res.getConstant();
Matt Fleming3565a062010-08-16 18:57:57 +0000556 }
557 } else if (ESize->getKind() == MCExpr::Constant) {
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000558 Size = static_cast<const MCConstantExpr *>(ESize)->getValue();
Matt Fleming3565a062010-08-16 18:57:57 +0000559 } else {
560 assert(0 && "Unsupported size expression");
561 }
562 }
563
564 // Write out the symbol table entry
Rafael Espindola7be2c332010-10-31 00:16:26 +0000565 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
566 Size, Other, MSD.SectionIndex, IsReserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000567}
568
Rafael Espindola7be2c332010-10-31 00:16:26 +0000569void ELFObjectWriterImpl::WriteSymbolTable(MCDataFragment *SymtabF,
570 MCDataFragment *ShndxF,
Matt Fleming3565a062010-08-16 18:57:57 +0000571 const MCAssembler &Asm,
Rafael Espindola71859c62010-09-16 19:46:31 +0000572 const MCAsmLayout &Layout,
573 unsigned NumRegularSections) {
Matt Fleming3565a062010-08-16 18:57:57 +0000574 // The string table must be emitted first because we need the index
575 // into the string table for all the symbol names.
576 assert(StringTable.size() && "Missing string table");
577
578 // FIXME: Make sure the start of the symbol table is aligned.
579
580 // The first entry is the undefined symbol entry.
Rafael Espindola7be2c332010-10-31 00:16:26 +0000581 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
Matt Fleming3565a062010-08-16 18:57:57 +0000582
583 // Write the symbol table entries.
584 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
585 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
586 ELFSymbolData &MSD = LocalSymbolData[i];
Rafael Espindola7be2c332010-10-31 00:16:26 +0000587 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000588 }
589
Rafael Espindola71859c62010-09-16 19:46:31 +0000590 // Write out a symbol table entry for each regular section.
Eli Friedmana44fa242010-08-16 21:17:09 +0000591 unsigned Index = 1;
Rafael Espindola71859c62010-09-16 19:46:31 +0000592 for (MCAssembler::const_iterator it = Asm.begin();
593 Index <= NumRegularSections; ++it, ++Index) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000594 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000595 static_cast<const MCSectionELF&>(it->getSection());
Eli Friedmana44fa242010-08-16 21:17:09 +0000596 // Leave out relocations so we don't have indexes within
597 // the relocations messed up
Benjamin Kramer377a5722010-08-17 17:30:07 +0000598 if (Section.getType() == ELF::SHT_RELA || Section.getType() == ELF::SHT_REL)
Eli Friedmana44fa242010-08-16 21:17:09 +0000599 continue;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000600 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
601 ELF::STV_DEFAULT, Index, false);
Eli Friedmana44fa242010-08-16 21:17:09 +0000602 LastLocalSymbolIndex++;
603 }
Matt Fleming3565a062010-08-16 18:57:57 +0000604
605 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
606 ELFSymbolData &MSD = ExternalSymbolData[i];
607 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000608 assert(((Data.getFlags() & ELF_STB_Global) ||
609 (Data.getFlags() & ELF_STB_Weak)) &&
610 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000611 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000612 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000613 LastLocalSymbolIndex++;
614 }
615
616 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
617 ELFSymbolData &MSD = UndefinedSymbolData[i];
618 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000619 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000620 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000621 LastLocalSymbolIndex++;
622 }
623}
624
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000625static bool ShouldRelocOnSymbol(const MCSymbolData &SD,
Rafael Espindola3729d002010-10-05 23:57:26 +0000626 const MCValue &Target,
627 const MCFragment &F) {
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000628 const MCSymbol &Symbol = SD.getSymbol();
629 if (Symbol.isUndefined())
630 return true;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000631
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000632 const MCSectionELF &Section =
633 static_cast<const MCSectionELF&>(Symbol.getSection());
634
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000635 if (SD.isExternal())
636 return true;
637
Rafael Espindola8cecf252010-10-06 16:23:36 +0000638 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000639 const MCSectionELF &Sec2 =
640 static_cast<const MCSectionELF&>(F.getParent()->getSection());
641
Rafael Espindolace2d3c52010-10-18 20:25:33 +0000642 if (Section.getKind().isBSS())
643 return false;
644
Rafael Espindola8cecf252010-10-06 16:23:36 +0000645 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000646 (Kind == MCSymbolRefExpr::VK_PLT ||
647 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
648 Kind == MCSymbolRefExpr::VK_GOTOFF))
Rafael Espindola3729d002010-10-05 23:57:26 +0000649 return true;
650
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000651 if (Section.getFlags() & MCSectionELF::SHF_MERGE)
652 return Target.getConstant() != 0;
653
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000654 return false;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000655}
656
Benjamin Kramere5b57342010-08-17 18:20:28 +0000657// FIXME: this is currently X86/X86_64 only
Matt Fleming3565a062010-08-16 18:57:57 +0000658void ELFObjectWriterImpl::RecordRelocation(const MCAssembler &Asm,
659 const MCAsmLayout &Layout,
660 const MCFragment *Fragment,
661 const MCFixup &Fixup,
662 MCValue Target,
663 uint64_t &FixedValue) {
Matt Fleming3565a062010-08-16 18:57:57 +0000664 int64_t Addend = 0;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000665 int Index = 0;
Benjamin Kramer95c602a2010-08-27 10:38:39 +0000666 int64_t Value = Target.getConstant();
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000667 const MCSymbol *Symbol = 0;
Rafael Espindola29129722010-10-28 19:08:03 +0000668 const MCSymbol *Renamed = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000669
Rafael Espindola00074892010-09-17 22:34:41 +0000670 bool IsPCRel = isFixupKindX86PCRel(Fixup.getKind());
Benjamin Kramer81cfb852010-08-17 19:45:05 +0000671 if (!Target.isAbsolute()) {
Rafael Espindolade89b012010-10-15 18:25:33 +0000672 Symbol = &AliasedSymbol(Target.getSymA()->getSymbol());
Rafael Espindola29129722010-10-28 19:08:03 +0000673 Renamed = Renames.lookup(Symbol);
674 if (!Renamed)
Rafael Espindola484291c2010-11-01 14:28:48 +0000675 Renamed = &Target.getSymA()->getSymbol();
Matt Fleming3565a062010-08-16 18:57:57 +0000676 MCSymbolData &SD = Asm.getSymbolData(*Symbol);
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000677 MCFragment *F = SD.getFragment();
Matt Fleming3565a062010-08-16 18:57:57 +0000678
Rafael Espindola9d8b7552010-10-03 00:46:57 +0000679 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
680 const MCSymbol &SymbolB = RefB->getSymbol();
681 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
682 IsPCRel = true;
Rafael Espindola55fb1022010-10-04 15:59:01 +0000683 MCSectionData *Sec = Fragment->getParent();
684
685 // Offset of the symbol in the section
686 int64_t a = Layout.getSymbolAddress(&SDB) - Layout.getSectionAddress(Sec);
687
688 // Ofeset of the relocation in the section
689 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
690 Value += b - a;
Rafael Espindola9d8b7552010-10-03 00:46:57 +0000691 }
692
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000693 // Check that this case has already been fully resolved before we get
694 // here.
Rafael Espindola00074892010-09-17 22:34:41 +0000695 if (Symbol->isDefined() && !SD.isExternal() &&
696 IsPCRel &&
697 &Fragment->getParent()->getSection() == &Symbol->getSection()) {
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000698 llvm_unreachable("We don't need a relocation in this case.");
Rafael Espindola00074892010-09-17 22:34:41 +0000699 return;
700 }
701
Rafael Espindola3729d002010-10-05 23:57:26 +0000702 bool RelocOnSymbol = ShouldRelocOnSymbol(SD, Target, *Fragment);
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000703 if (!RelocOnSymbol) {
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000704 Index = F->getParent()->getOrdinal();
Rafael Espindolaa6489182010-09-24 21:19:03 +0000705
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000706 MCSectionData *FSD = F->getParent();
707 // Offset of the symbol in the section
708 Value += Layout.getSymbolAddress(&SD) - Layout.getSectionAddress(FSD);
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000709 } else {
Rafael Espindola29129722010-10-28 19:08:03 +0000710 UsedInReloc.insert(Renamed);
Rafael Espindola484291c2010-11-01 14:28:48 +0000711 MCSymbolData &RenamedSD = Asm.getSymbolData(*Renamed);
712 if (RenamedSD.getFlags() & ELF_Other_Weakref) {
713 WeakrefUsedInReloc.insert(Symbol);
714 }
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000715 Index = -1;
716 }
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000717 Addend = Value;
718 // Compensate for the addend on i386.
719 if (Is64Bit)
720 Value = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000721 }
722
Benjamin Kramer95c602a2010-08-27 10:38:39 +0000723 FixedValue = Value;
724
Matt Fleming3565a062010-08-16 18:57:57 +0000725 // determine the type of the relocation
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000726
727 MCSymbolRefExpr::VariantKind Modifier = Target.getSymA()->getKind();
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000728 unsigned Type;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000729 if (Is64Bit) {
730 if (IsPCRel) {
Rafael Espindola92bf6682010-10-04 19:04:13 +0000731 switch (Modifier) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000732 default:
733 llvm_unreachable("Unimplemented");
Rafael Espindola92bf6682010-10-04 19:04:13 +0000734 case MCSymbolRefExpr::VK_None:
735 Type = ELF::R_X86_64_PC32;
736 break;
737 case MCSymbolRefExpr::VK_PLT:
738 Type = ELF::R_X86_64_PLT32;
739 break;
Rafael Espindolaa0a2f872010-10-28 14:22:44 +0000740 case MCSymbolRefExpr::VK_GOTPCREL:
Rafael Espindola607d1f62010-10-04 19:51:39 +0000741 Type = ELF::R_X86_64_GOTPCREL;
742 break;
Rafael Espindolabc82d8b2010-10-27 20:28:07 +0000743 case MCSymbolRefExpr::VK_GOTTPOFF:
744 Type = ELF::R_X86_64_GOTTPOFF;
745 break;
746 case MCSymbolRefExpr::VK_TLSGD:
747 Type = ELF::R_X86_64_TLSGD;
748 break;
Rafael Espindolab4d17212010-10-28 15:02:40 +0000749 case MCSymbolRefExpr::VK_TLSLD:
750 Type = ELF::R_X86_64_TLSLD;
751 break;
Rafael Espindola92bf6682010-10-04 19:04:13 +0000752 }
Benjamin Kramere5b57342010-08-17 18:20:28 +0000753 } else {
754 switch ((unsigned)Fixup.getKind()) {
755 default: llvm_unreachable("invalid fixup kind!");
756 case FK_Data_8: Type = ELF::R_X86_64_64; break;
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000757 case X86::reloc_signed_4byte:
Benjamin Kramere5b57342010-08-17 18:20:28 +0000758 case X86::reloc_pcrel_4byte:
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000759 assert(isInt<32>(Target.getConstant()));
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000760 switch (Modifier) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000761 default:
762 llvm_unreachable("Unimplemented");
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000763 case MCSymbolRefExpr::VK_None:
764 Type = ELF::R_X86_64_32S;
765 break;
766 case MCSymbolRefExpr::VK_GOT:
767 Type = ELF::R_X86_64_GOT32;
768 break;
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000769 case MCSymbolRefExpr::VK_GOTPCREL:
Rafael Espindola8cecf252010-10-06 16:23:36 +0000770 Type = ELF::R_X86_64_GOTPCREL;
771 break;
Rafael Espindolabc82d8b2010-10-27 20:28:07 +0000772 case MCSymbolRefExpr::VK_TPOFF:
773 Type = ELF::R_X86_64_TPOFF32;
774 break;
Rafael Espindolaaa8f1f02010-10-28 15:11:03 +0000775 case MCSymbolRefExpr::VK_DTPOFF:
776 Type = ELF::R_X86_64_DTPOFF32;
777 break;
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000778 }
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000779 break;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000780 case FK_Data_4:
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000781 Type = ELF::R_X86_64_32;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000782 break;
783 case FK_Data_2: Type = ELF::R_X86_64_16; break;
784 case X86::reloc_pcrel_1byte:
785 case FK_Data_1: Type = ELF::R_X86_64_8; break;
786 }
787 }
Matt Fleming3565a062010-08-16 18:57:57 +0000788 } else {
Benjamin Kramere5b57342010-08-17 18:20:28 +0000789 if (IsPCRel) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000790 switch (Modifier) {
791 default:
Rafael Espindola24dc9ec2010-10-18 19:33:01 +0000792 llvm_unreachable("Unimplemented");
793 case MCSymbolRefExpr::VK_None:
Rafael Espindola9baee3b2010-10-18 18:03:28 +0000794 Type = ELF::R_386_PC32;
Rafael Espindola9baee3b2010-10-18 18:03:28 +0000795 break;
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000796 case MCSymbolRefExpr::VK_PLT:
797 Type = ELF::R_386_PLT32;
798 break;
799 }
Benjamin Kramere5b57342010-08-17 18:20:28 +0000800 } else {
801 switch ((unsigned)Fixup.getKind()) {
802 default: llvm_unreachable("invalid fixup kind!");
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000803
Rafael Espindola24ba4f72010-10-24 17:35:42 +0000804 case X86::reloc_global_offset_table:
805 Type = ELF::R_386_GOTPC;
806 break;
807
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000808 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
809 // instead?
810 case X86::reloc_signed_4byte:
Benjamin Kramere5b57342010-08-17 18:20:28 +0000811 case X86::reloc_pcrel_4byte:
Rafael Espindolabd701182010-10-19 19:31:37 +0000812 case FK_Data_4:
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000813 switch (Modifier) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000814 default:
815 llvm_unreachable("Unimplemented");
Rafael Espindolabd701182010-10-19 19:31:37 +0000816 case MCSymbolRefExpr::VK_None:
Rafael Espindola24ba4f72010-10-24 17:35:42 +0000817 Type = ELF::R_386_32;
Rafael Espindolabd701182010-10-19 19:31:37 +0000818 break;
Rafael Espindolaeada3042010-10-18 20:47:21 +0000819 case MCSymbolRefExpr::VK_GOT:
820 Type = ELF::R_386_GOT32;
821 break;
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000822 case MCSymbolRefExpr::VK_GOTOFF:
823 Type = ELF::R_386_GOTOFF;
824 break;
Rafael Espindola3cede2d2010-10-27 21:23:52 +0000825 case MCSymbolRefExpr::VK_TLSGD:
826 Type = ELF::R_386_TLS_GD;
827 break;
828 case MCSymbolRefExpr::VK_TPOFF:
829 Type = ELF::R_386_TLS_LE_32;
830 break;
831 case MCSymbolRefExpr::VK_INDNTPOFF:
832 Type = ELF::R_386_TLS_IE;
833 break;
834 case MCSymbolRefExpr::VK_NTPOFF:
835 Type = ELF::R_386_TLS_LE;
836 break;
Rafael Espindolaa0a2f872010-10-28 14:22:44 +0000837 case MCSymbolRefExpr::VK_GOTNTPOFF:
838 Type = ELF::R_386_TLS_GOTIE;
839 break;
Rafael Espindolaa264f722010-10-28 14:37:09 +0000840 case MCSymbolRefExpr::VK_TLSLDM:
841 Type = ELF::R_386_TLS_LDM;
842 break;
Rafael Espindola0cf15d62010-10-28 14:48:59 +0000843 case MCSymbolRefExpr::VK_DTPOFF:
844 Type = ELF::R_386_TLS_LDO_32;
845 break;
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000846 }
847 break;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000848 case FK_Data_2: Type = ELF::R_386_16; break;
849 case X86::reloc_pcrel_1byte:
850 case FK_Data_1: Type = ELF::R_386_8; break;
851 }
Matt Fleming3565a062010-08-16 18:57:57 +0000852 }
853 }
854
Rafael Espindolaa0a2f872010-10-28 14:22:44 +0000855 if (RelocNeedsGOT(Modifier))
Rafael Espindola5c77c162010-10-05 15:48:37 +0000856 NeedsGOT = true;
857
Benjamin Kramere5b57342010-08-17 18:20:28 +0000858 ELFRelocationEntry ERE;
859
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000860 ERE.Index = Index;
861 ERE.Type = Type;
Rafael Espindola29129722010-10-28 19:08:03 +0000862 ERE.Symbol = Renamed;
Matt Fleming3565a062010-08-16 18:57:57 +0000863
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000864 ERE.r_offset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
Benjamin Kramere5b57342010-08-17 18:20:28 +0000865
Matt Fleming3565a062010-08-16 18:57:57 +0000866 if (HasRelocationAddend)
867 ERE.r_addend = Addend;
Benjamin Kramer172d7d62010-08-17 00:33:24 +0000868 else
869 ERE.r_addend = 0; // Silence compiler warning.
Matt Fleming3565a062010-08-16 18:57:57 +0000870
871 Relocations[Fragment->getParent()].push_back(ERE);
872}
873
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000874uint64_t
875ELFObjectWriterImpl::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
876 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000877 MCSymbolData &SD = Asm.getSymbolData(*S);
Eli Friedmanf8020a32010-08-16 19:15:06 +0000878
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000879 // Local symbol.
880 if (!SD.isExternal() && !S->isUndefined())
881 return SD.getIndex() + /* empty symbol */ 1;
882
883 // External or undefined symbol.
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000884 return SD.getIndex() + NumRegularSections + /* empty symbol */ 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000885}
886
Rafael Espindola737cd212010-10-05 18:01:23 +0000887static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
Rafael Espindola88182132010-10-27 15:18:17 +0000888 bool Used, bool Renamed) {
Rafael Espindola484291c2010-11-01 14:28:48 +0000889 if (Data.getFlags() & ELF_Other_Weakref)
890 return false;
891
Rafael Espindolabd701182010-10-19 19:31:37 +0000892 if (Used)
893 return true;
894
Rafael Espindola88182132010-10-27 15:18:17 +0000895 if (Renamed)
896 return false;
897
Rafael Espindola737cd212010-10-05 18:01:23 +0000898 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000899
Rafael Espindolad1798862010-10-29 23:09:31 +0000900 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
901 return true;
902
Rafael Espindolaa6866962010-10-27 14:44:52 +0000903 const MCSymbol &A = AliasedSymbol(Symbol);
Rafael Espindolad1798862010-10-29 23:09:31 +0000904 if (!A.isVariable() && A.isUndefined() && !Data.isCommon())
Rafael Espindolaa6866962010-10-27 14:44:52 +0000905 return false;
906
Rafael Espindola737cd212010-10-05 18:01:23 +0000907 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
908 return false;
909
Rafael Espindolabd701182010-10-19 19:31:37 +0000910 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000911 return false;
912
913 return true;
914}
915
916static bool isLocal(const MCSymbolData &Data) {
917 if (Data.isExternal())
918 return false;
919
920 const MCSymbol &Symbol = Data.getSymbol();
921 if (Symbol.isUndefined() && !Symbol.isVariable())
922 return false;
923
924 return true;
925}
926
Rafael Espindolabab2a802010-11-10 21:51:05 +0000927void ELFObjectWriterImpl::ComputeIndexMap(MCAssembler &Asm,
928 SectionIndexMapTy &SectionIndexMap) {
929 unsigned Index = 1;
930 for (MCAssembler::iterator it = Asm.begin(),
931 ie = Asm.end(); it != ie; ++it) {
932 const MCSectionELF &Section =
933 static_cast<const MCSectionELF &>(it->getSection());
934 SectionIndexMap[&Section] = Index++;
935 }
936}
937
938void ELFObjectWriterImpl::ComputeSymbolTable(MCAssembler &Asm,
939 const SectionIndexMapTy &SectionIndexMap) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000940 // FIXME: Is this the correct place to do this?
941 if (NeedsGOT) {
942 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
943 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
944 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
945 Data.setExternal(true);
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000946 SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindola5c77c162010-10-05 15:48:37 +0000947 }
948
Matt Fleming3565a062010-08-16 18:57:57 +0000949 // Build section lookup table.
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000950 NumRegularSections = Asm.size();
Matt Fleming3565a062010-08-16 18:57:57 +0000951
952 // Index 0 is always the empty string.
953 StringMap<uint64_t> StringIndexMap;
954 StringTable += '\x00';
955
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000956 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000957 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
958 ie = Asm.symbol_end(); it != ie; ++it) {
959 const MCSymbol &Symbol = it->getSymbol();
960
Rafael Espindola484291c2010-11-01 14:28:48 +0000961 bool Used = UsedInReloc.count(&Symbol);
962 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
963 if (!isInSymtab(Asm, *it, Used || WeakrefUsed,
Rafael Espindola88182132010-10-27 15:18:17 +0000964 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000965 continue;
966
Matt Fleming3565a062010-08-16 18:57:57 +0000967 ELFSymbolData MSD;
968 MSD.SymbolData = it;
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000969 bool Local = isLocal(*it);
Rafael Espindola88182132010-10-27 15:18:17 +0000970 const MCSymbol &RefSymbol = AliasedSymbol(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000971
Rafael Espindola484291c2010-11-01 14:28:48 +0000972 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
973 SetBinding(*it, ELF::STB_WEAK);
974
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000975 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000976 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000977 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000978 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000979 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000980 } else if (RefSymbol.isUndefined()) {
Matt Fleming3565a062010-08-16 18:57:57 +0000981 MSD.SectionIndex = ELF::SHN_UNDEF;
Matt Fleming3565a062010-08-16 18:57:57 +0000982 } else {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000983 const MCSectionELF &Section =
984 static_cast<const MCSectionELF&>(RefSymbol.getSection());
985 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000986 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
987 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000988 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000989 }
990
Rafael Espindola88182132010-10-27 15:18:17 +0000991 // The @@@ in symbol version is replaced with @ in undefined symbols and
992 // @@ in defined ones.
993 StringRef Name = Symbol.getName();
994 size_t Pos = Name.find("@@@");
995 std::string FinalName;
996 if (Pos != StringRef::npos) {
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000997 StringRef Prefix = Name.substr(0, Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000998 unsigned n = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000999 StringRef Suffix = Name.substr(Pos + n);
Rafael Espindola88182132010-10-27 15:18:17 +00001000 FinalName = Prefix.str() + Suffix.str();
1001 } else {
1002 FinalName = Name.str();
1003 }
1004
1005 uint64_t &Entry = StringIndexMap[FinalName];
Rafael Espindolaa6866962010-10-27 14:44:52 +00001006 if (!Entry) {
1007 Entry = StringTable.size();
Rafael Espindola88182132010-10-27 15:18:17 +00001008 StringTable += FinalName;
Rafael Espindolaa6866962010-10-27 14:44:52 +00001009 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +00001010 }
Rafael Espindolaa6866962010-10-27 14:44:52 +00001011 MSD.StringIndex = Entry;
1012 if (MSD.SectionIndex == ELF::SHN_UNDEF)
1013 UndefinedSymbolData.push_back(MSD);
1014 else if (Local)
1015 LocalSymbolData.push_back(MSD);
1016 else
1017 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +00001018 }
1019
1020 // Symbols are required to be in lexicographic order.
1021 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
1022 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
1023 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
1024
1025 // Set the symbol indices. Local symbols must come before all other
1026 // symbols with non-local bindings.
Rafael Espindolabab2a802010-11-10 21:51:05 +00001027 unsigned Index = 0;
Matt Fleming3565a062010-08-16 18:57:57 +00001028 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
1029 LocalSymbolData[i].SymbolData->setIndex(Index++);
1030 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
1031 ExternalSymbolData[i].SymbolData->setIndex(Index++);
1032 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
1033 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
1034}
1035
1036void ELFObjectWriterImpl::WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
1037 const MCSectionData &SD) {
1038 if (!Relocations[&SD].empty()) {
1039 MCContext &Ctx = Asm.getContext();
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001040 const MCSectionELF *RelaSection;
Matt Fleming3565a062010-08-16 18:57:57 +00001041 const MCSectionELF &Section =
1042 static_cast<const MCSectionELF&>(SD.getSection());
1043
1044 const StringRef SectionName = Section.getSectionName();
Benjamin Kramer377a5722010-08-17 17:30:07 +00001045 std::string RelaSectionName = HasRelocationAddend ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +00001046 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +00001047
1048 unsigned EntrySize;
1049 if (HasRelocationAddend)
1050 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
1051 else
1052 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +00001053
Benjamin Kramer377a5722010-08-17 17:30:07 +00001054 RelaSection = Ctx.getELFSection(RelaSectionName, HasRelocationAddend ?
1055 ELF::SHT_RELA : ELF::SHT_REL, 0,
Matt Fleming3565a062010-08-16 18:57:57 +00001056 SectionKind::getReadOnly(),
Rafael Espindola34be3962010-11-09 23:42:07 +00001057 EntrySize);
Matt Fleming3565a062010-08-16 18:57:57 +00001058
1059 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001060 RelaSD.setAlignment(Is64Bit ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +00001061
1062 MCDataFragment *F = new MCDataFragment(&RelaSD);
1063
1064 WriteRelocationsFragment(Asm, F, &SD);
1065
Rafael Espindola70703872010-09-30 02:22:20 +00001066 Asm.AddSectionToTheEnd(*Writer, RelaSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +00001067 }
1068}
1069
1070void ELFObjectWriterImpl::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
1071 uint64_t Flags, uint64_t Address,
1072 uint64_t Offset, uint64_t Size,
1073 uint32_t Link, uint32_t Info,
1074 uint64_t Alignment,
1075 uint64_t EntrySize) {
1076 Write32(Name); // sh_name: index into string table
1077 Write32(Type); // sh_type
1078 WriteWord(Flags); // sh_flags
1079 WriteWord(Address); // sh_addr
1080 WriteWord(Offset); // sh_offset
1081 WriteWord(Size); // sh_size
1082 Write32(Link); // sh_link
1083 Write32(Info); // sh_info
1084 WriteWord(Alignment); // sh_addralign
1085 WriteWord(EntrySize); // sh_entsize
1086}
1087
1088void ELFObjectWriterImpl::WriteRelocationsFragment(const MCAssembler &Asm,
1089 MCDataFragment *F,
1090 const MCSectionData *SD) {
1091 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
1092 // sort by the r_offset just like gnu as does
1093 array_pod_sort(Relocs.begin(), Relocs.end());
1094
1095 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1096 ELFRelocationEntry entry = Relocs[e - i - 1];
1097
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001098 if (entry.Index < 0)
1099 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
1100 else
1101 entry.Index += LocalSymbolData.size() + 1;
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001102 if (Is64Bit) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001103 String64(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001104
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001105 struct ELF::Elf64_Rela ERE64;
1106 ERE64.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001107 String64(*F, ERE64.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001108
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001109 if (HasRelocationAddend)
1110 String64(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001111 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001112 String32(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001113
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001114 struct ELF::Elf32_Rela ERE32;
1115 ERE32.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001116 String32(*F, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001117
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001118 if (HasRelocationAddend)
1119 String32(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001120 }
Matt Fleming3565a062010-08-16 18:57:57 +00001121 }
1122}
1123
1124void ELFObjectWriterImpl::CreateMetadataSections(MCAssembler &Asm,
1125 MCAsmLayout &Layout) {
1126 MCContext &Ctx = Asm.getContext();
1127 MCDataFragment *F;
1128
Matt Fleming3565a062010-08-16 18:57:57 +00001129 unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
1130
Rafael Espindola71859c62010-09-16 19:46:31 +00001131 unsigned NumRegularSections = Asm.size();
1132
Rafael Espindola38738bf2010-09-22 19:04:41 +00001133 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001134 const MCSectionELF *ShstrtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001135 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
1136 SectionKind::getReadOnly(), false);
Rafael Espindola71859c62010-09-16 19:46:31 +00001137 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1138 ShstrtabSD.setAlignment(1);
1139 ShstrtabIndex = Asm.size();
1140
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001141 const MCSectionELF *SymtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001142 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1143 SectionKind::getReadOnly(),
Rafael Espindola34be3962010-11-09 23:42:07 +00001144 EntrySize);
Matt Fleming3565a062010-08-16 18:57:57 +00001145 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Matt Fleming3565a062010-08-16 18:57:57 +00001146 SymtabSD.setAlignment(Is64Bit ? 8 : 4);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001147 SymbolTableIndex = Asm.size();
1148
1149 MCSectionData *SymtabShndxSD = NULL;
1150
1151 if (NeedsSymtabShndx) {
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001152 const MCSectionELF *SymtabShndxSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001153 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
Rafael Espindola34be3962010-11-09 23:42:07 +00001154 SectionKind::getReadOnly(), 4);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001155 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
1156 SymtabShndxSD->setAlignment(4);
1157 }
Matt Fleming3565a062010-08-16 18:57:57 +00001158
Matt Fleming3565a062010-08-16 18:57:57 +00001159 const MCSection *StrtabSection;
1160 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
1161 SectionKind::getReadOnly(), false);
Matt Fleming3565a062010-08-16 18:57:57 +00001162 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1163 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +00001164 StringTableIndex = Asm.size();
1165
Rafael Espindolac3c413f2010-09-27 22:04:54 +00001166 WriteRelocations(Asm, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001167
1168 // Symbol table
1169 F = new MCDataFragment(&SymtabSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001170 MCDataFragment *ShndxF = NULL;
1171 if (NeedsSymtabShndx) {
1172 ShndxF = new MCDataFragment(SymtabShndxSD);
1173 Asm.AddSectionToTheEnd(*Writer, *SymtabShndxSD, Layout);
1174 }
1175 WriteSymbolTable(F, ShndxF, Asm, Layout, NumRegularSections);
Rafael Espindola70703872010-09-30 02:22:20 +00001176 Asm.AddSectionToTheEnd(*Writer, SymtabSD, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001177
Matt Fleming3565a062010-08-16 18:57:57 +00001178 F = new MCDataFragment(&StrtabSD);
1179 F->getContents().append(StringTable.begin(), StringTable.end());
Rafael Espindola70703872010-09-30 02:22:20 +00001180 Asm.AddSectionToTheEnd(*Writer, StrtabSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +00001181
Matt Fleming3565a062010-08-16 18:57:57 +00001182 F = new MCDataFragment(&ShstrtabSD);
1183
Matt Fleming3565a062010-08-16 18:57:57 +00001184 // Section header string table.
1185 //
1186 // The first entry of a string table holds a null character so skip
1187 // section 0.
1188 uint64_t Index = 1;
1189 F->getContents() += '\x00';
1190
1191 for (MCAssembler::const_iterator it = Asm.begin(),
1192 ie = Asm.end(); it != ie; ++it) {
Matt Fleming3565a062010-08-16 18:57:57 +00001193 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +00001194 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola51efe7a2010-09-23 14:14:56 +00001195 // FIXME: We could merge suffixes like in .text and .rela.text.
Matt Fleming3565a062010-08-16 18:57:57 +00001196
1197 // Remember the index into the string table so we can write it
1198 // into the sh_name field of the section header table.
1199 SectionStringTableIndex[&it->getSection()] = Index;
1200
1201 Index += Section.getSectionName().size() + 1;
1202 F->getContents() += Section.getSectionName();
1203 F->getContents() += '\x00';
1204 }
1205
Rafael Espindola70703872010-09-30 02:22:20 +00001206 Asm.AddSectionToTheEnd(*Writer, ShstrtabSD, Layout);
1207}
1208
1209bool ELFObjectWriterImpl::IsFixupFullyResolved(const MCAssembler &Asm,
1210 const MCValue Target,
1211 bool IsPCRel,
1212 const MCFragment *DF) const {
1213 // If this is a PCrel relocation, find the section this fixup value is
1214 // relative to.
1215 const MCSection *BaseSection = 0;
1216 if (IsPCRel) {
1217 BaseSection = &DF->getParent()->getSection();
1218 assert(BaseSection);
1219 }
1220
1221 const MCSection *SectionA = 0;
1222 const MCSymbol *SymbolA = 0;
1223 if (const MCSymbolRefExpr *A = Target.getSymA()) {
1224 SymbolA = &A->getSymbol();
1225 SectionA = &SymbolA->getSection();
1226 }
1227
1228 const MCSection *SectionB = 0;
1229 if (const MCSymbolRefExpr *B = Target.getSymB()) {
1230 SectionB = &B->getSymbol().getSection();
1231 }
1232
1233 if (!BaseSection)
1234 return SectionA == SectionB;
1235
1236 const MCSymbolData &DataA = Asm.getSymbolData(*SymbolA);
1237 if (DataA.isExternal())
1238 return false;
1239
1240 return !SectionB && BaseSection == SectionA;
Matt Fleming3565a062010-08-16 18:57:57 +00001241}
1242
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001243void ELFObjectWriterImpl::WriteObject(MCAssembler &Asm,
Matt Fleming3565a062010-08-16 18:57:57 +00001244 const MCAsmLayout &Layout) {
Rafael Espindolabab2a802010-11-10 21:51:05 +00001245 SectionIndexMapTy SectionIndexMap;
1246
1247 ComputeIndexMap(Asm, SectionIndexMap);
1248
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001249 // Compute symbol table information.
Rafael Espindolabab2a802010-11-10 21:51:05 +00001250 ComputeSymbolTable(Asm, SectionIndexMap);
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001251
Matt Fleming3565a062010-08-16 18:57:57 +00001252 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
1253 const_cast<MCAsmLayout&>(Layout));
1254
1255 // Add 1 for the null section.
1256 unsigned NumSections = Asm.size() + 1;
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001257 uint64_t NaturalAlignment = Is64Bit ? 8 : 4;
1258 uint64_t HeaderSize = Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr);
1259 uint64_t FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001260
1261 for (MCAssembler::const_iterator it = Asm.begin(),
1262 ie = Asm.end(); it != ie; ++it) {
1263 const MCSectionData &SD = *it;
Matt Fleming3565a062010-08-16 18:57:57 +00001264
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001265 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1266
Matt Fleming3565a062010-08-16 18:57:57 +00001267 // Get the size of the section in the output file (including padding).
1268 uint64_t Size = Layout.getSectionFileSize(&SD);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001269
1270 FileOff += Size;
Matt Fleming3565a062010-08-16 18:57:57 +00001271 }
1272
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001273 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1274
Matt Fleming3565a062010-08-16 18:57:57 +00001275 // Write out the ELF header ...
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001276 WriteHeader(FileOff - HeaderSize, NumSections);
1277
1278 FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001279
1280 // ... then all of the sections ...
1281 DenseMap<const MCSection*, uint64_t> SectionOffsetMap;
1282
1283 for (MCAssembler::const_iterator it = Asm.begin(),
1284 ie = Asm.end(); it != ie; ++it) {
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001285 const MCSectionData &SD = *it;
1286
1287 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1288 WriteZeros(Padding);
1289 FileOff += Padding;
1290
Matt Fleming3565a062010-08-16 18:57:57 +00001291 // Remember the offset into the file for this section.
1292 SectionOffsetMap[&it->getSection()] = FileOff;
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001293
Matt Fleming3565a062010-08-16 18:57:57 +00001294 FileOff += Layout.getSectionFileSize(&SD);
1295
1296 Asm.WriteSectionData(it, Layout, Writer);
1297 }
1298
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001299 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1300 WriteZeros(Padding);
1301 FileOff += Padding;
1302
Matt Fleming3565a062010-08-16 18:57:57 +00001303 // ... and then the section header table.
1304 // Should we align the section header table?
1305 //
1306 // Null section first.
Rafael Espindola7be2c332010-10-31 00:16:26 +00001307 uint64_t FirstSectionSize =
1308 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1309 uint32_t FirstSectionLink =
1310 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1311 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming3565a062010-08-16 18:57:57 +00001312
1313 for (MCAssembler::const_iterator it = Asm.begin(),
1314 ie = Asm.end(); it != ie; ++it) {
1315 const MCSectionData &SD = *it;
1316 const MCSectionELF &Section =
1317 static_cast<const MCSectionELF&>(SD.getSection());
1318
1319 uint64_t sh_link = 0;
1320 uint64_t sh_info = 0;
1321
1322 switch(Section.getType()) {
1323 case ELF::SHT_DYNAMIC:
1324 sh_link = SectionStringTableIndex[&it->getSection()];
1325 sh_info = 0;
1326 break;
1327
1328 case ELF::SHT_REL:
Eli Friedmanf8020a32010-08-16 19:15:06 +00001329 case ELF::SHT_RELA: {
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001330 const MCSectionELF *SymtabSection;
1331 const MCSectionELF *InfoSection;
Matt Fleming3565a062010-08-16 18:57:57 +00001332
1333 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1334 SectionKind::getReadOnly(),
Eli Friedmanf8020a32010-08-16 19:15:06 +00001335 false);
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001336 sh_link = SectionIndexMap[SymtabSection];
Matt Fleming3565a062010-08-16 18:57:57 +00001337
Benjamin Kramer377a5722010-08-17 17:30:07 +00001338 // Remove ".rel" and ".rela" prefixes.
1339 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1340 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
1341
Eli Friedmanf8020a32010-08-16 19:15:06 +00001342 InfoSection = Asm.getContext().getELFSection(SectionName,
Matt Fleming3565a062010-08-16 18:57:57 +00001343 ELF::SHT_PROGBITS, 0,
Eli Friedmanf8020a32010-08-16 19:15:06 +00001344 SectionKind::getReadOnly(),
1345 false);
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001346 sh_info = SectionIndexMap[InfoSection];
Matt Fleming3565a062010-08-16 18:57:57 +00001347 break;
Eli Friedmanf8020a32010-08-16 19:15:06 +00001348 }
Matt Fleming3565a062010-08-16 18:57:57 +00001349
1350 case ELF::SHT_SYMTAB:
1351 case ELF::SHT_DYNSYM:
1352 sh_link = StringTableIndex;
1353 sh_info = LastLocalSymbolIndex;
1354 break;
1355
Rafael Espindola7be2c332010-10-31 00:16:26 +00001356 case ELF::SHT_SYMTAB_SHNDX:
1357 sh_link = SymbolTableIndex;
1358 break;
1359
Matt Fleming3565a062010-08-16 18:57:57 +00001360 case ELF::SHT_PROGBITS:
1361 case ELF::SHT_STRTAB:
1362 case ELF::SHT_NOBITS:
Benjamin Kramer19dc7fa2010-08-31 17:03:33 +00001363 case ELF::SHT_NULL:
Rafael Espindolacecbc3d2010-10-25 17:50:35 +00001364 case ELF::SHT_ARM_ATTRIBUTES:
Matt Fleming3565a062010-08-16 18:57:57 +00001365 // Nothing to do.
1366 break;
1367
Matt Fleming3565a062010-08-16 18:57:57 +00001368 default:
1369 assert(0 && "FIXME: sh_type value not supported!");
1370 break;
1371 }
1372
1373 WriteSecHdrEntry(SectionStringTableIndex[&it->getSection()],
1374 Section.getType(), Section.getFlags(),
Rafael Espindola71859c62010-09-16 19:46:31 +00001375 0,
Matt Fleming3565a062010-08-16 18:57:57 +00001376 SectionOffsetMap.lookup(&SD.getSection()),
1377 Layout.getSectionSize(&SD), sh_link,
1378 sh_info, SD.getAlignment(),
1379 Section.getEntrySize());
1380 }
1381}
1382
1383ELFObjectWriter::ELFObjectWriter(raw_ostream &OS,
1384 bool Is64Bit,
Roman Divacky5baf79e2010-09-09 17:57:50 +00001385 Triple::OSType OSType,
Wesley Peckeecb8582010-10-22 15:52:49 +00001386 uint16_t EMachine,
Matt Fleming3565a062010-08-16 18:57:57 +00001387 bool IsLittleEndian,
1388 bool HasRelocationAddend)
1389 : MCObjectWriter(OS, IsLittleEndian)
1390{
Wesley Peckeecb8582010-10-22 15:52:49 +00001391 Impl = new ELFObjectWriterImpl(this, Is64Bit, EMachine,
1392 HasRelocationAddend, OSType);
Matt Fleming3565a062010-08-16 18:57:57 +00001393}
1394
1395ELFObjectWriter::~ELFObjectWriter() {
1396 delete (ELFObjectWriterImpl*) Impl;
1397}
1398
1399void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm) {
1400 ((ELFObjectWriterImpl*) Impl)->ExecutePostLayoutBinding(Asm);
1401}
1402
1403void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
1404 const MCAsmLayout &Layout,
1405 const MCFragment *Fragment,
1406 const MCFixup &Fixup, MCValue Target,
1407 uint64_t &FixedValue) {
1408 ((ELFObjectWriterImpl*) Impl)->RecordRelocation(Asm, Layout, Fragment, Fixup,
1409 Target, FixedValue);
1410}
1411
Rafael Espindola70703872010-09-30 02:22:20 +00001412bool ELFObjectWriter::IsFixupFullyResolved(const MCAssembler &Asm,
1413 const MCValue Target,
1414 bool IsPCRel,
1415 const MCFragment *DF) const {
1416 return ((ELFObjectWriterImpl*) Impl)->IsFixupFullyResolved(Asm, Target,
1417 IsPCRel, DF);
1418}
1419
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001420void ELFObjectWriter::WriteObject(MCAssembler &Asm,
Matt Fleming3565a062010-08-16 18:57:57 +00001421 const MCAsmLayout &Layout) {
1422 ((ELFObjectWriterImpl*) Impl)->WriteObject(Asm, Layout);
1423}