blob: 313f2e20085b57984e7e35654d2dd8ed2e3074f0 [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
217 void String8(char *buf, uint8_t Value) {
218 buf[0] = Value;
219 }
220
221 void StringLE16(char *buf, uint16_t Value) {
222 buf[0] = char(Value >> 0);
223 buf[1] = char(Value >> 8);
224 }
225
226 void StringLE32(char *buf, uint32_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000227 StringLE16(buf, uint16_t(Value >> 0));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000228 StringLE16(buf + 2, uint16_t(Value >> 16));
Matt Fleming3565a062010-08-16 18:57:57 +0000229 }
230
231 void StringLE64(char *buf, uint64_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000232 StringLE32(buf, uint32_t(Value >> 0));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000233 StringLE32(buf + 4, uint32_t(Value >> 32));
Matt Fleming3565a062010-08-16 18:57:57 +0000234 }
235
236 void StringBE16(char *buf ,uint16_t Value) {
237 buf[0] = char(Value >> 8);
238 buf[1] = char(Value >> 0);
239 }
240
241 void StringBE32(char *buf, uint32_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000242 StringBE16(buf, uint16_t(Value >> 16));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000243 StringBE16(buf + 2, uint16_t(Value >> 0));
Matt Fleming3565a062010-08-16 18:57:57 +0000244 }
245
246 void StringBE64(char *buf, uint64_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000247 StringBE32(buf, uint32_t(Value >> 32));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000248 StringBE32(buf + 4, uint32_t(Value >> 0));
Matt Fleming3565a062010-08-16 18:57:57 +0000249 }
250
251 void String16(char *buf, uint16_t Value) {
252 if (Writer->isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000253 StringLE16(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000254 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000255 StringBE16(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000256 }
257
258 void String32(char *buf, uint32_t Value) {
259 if (Writer->isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000260 StringLE32(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000261 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000262 StringBE32(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000263 }
264
265 void String64(char *buf, uint64_t Value) {
266 if (Writer->isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000267 StringLE64(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000268 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000269 StringBE64(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000270 }
271
272 void WriteHeader(uint64_t SectionDataSize, unsigned NumberOfSections);
273
Rafael Espindola7be2c332010-10-31 00:16:26 +0000274 void WriteSymbolEntry(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
275 uint64_t name, uint8_t info,
Matt Fleming3565a062010-08-16 18:57:57 +0000276 uint64_t value, uint64_t size,
Rafael Espindola7be2c332010-10-31 00:16:26 +0000277 uint8_t other, uint32_t shndx,
278 bool Reserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000279
Rafael Espindola7be2c332010-10-31 00:16:26 +0000280 void WriteSymbol(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
281 ELFSymbolData &MSD,
Matt Fleming3565a062010-08-16 18:57:57 +0000282 const MCAsmLayout &Layout);
283
Rafael Espindola7be2c332010-10-31 00:16:26 +0000284 void WriteSymbolTable(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
285 const MCAssembler &Asm,
Rafael Espindola71859c62010-09-16 19:46:31 +0000286 const MCAsmLayout &Layout,
287 unsigned NumRegularSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000288
289 void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
290 const MCFragment *Fragment, const MCFixup &Fixup,
291 MCValue Target, uint64_t &FixedValue);
292
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000293 uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm,
294 const MCSymbol *S);
Matt Fleming3565a062010-08-16 18:57:57 +0000295
296 /// ComputeSymbolTable - Compute the symbol table data
297 ///
298 /// \param StringTable [out] - The string table data.
299 /// \param StringIndexMap [out] - Map from symbol names to offsets in the
300 /// string table.
301 void ComputeSymbolTable(MCAssembler &Asm);
302
303 void WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
304 const MCSectionData &SD);
305
306 void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout) {
307 for (MCAssembler::const_iterator it = Asm.begin(),
308 ie = Asm.end(); it != ie; ++it) {
309 WriteRelocation(Asm, Layout, *it);
310 }
311 }
312
313 void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout);
314
Rafael Espindola88182132010-10-27 15:18:17 +0000315 void ExecutePostLayoutBinding(MCAssembler &Asm);
Matt Fleming3565a062010-08-16 18:57:57 +0000316
317 void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
318 uint64_t Address, uint64_t Offset,
319 uint64_t Size, uint32_t Link, uint32_t Info,
320 uint64_t Alignment, uint64_t EntrySize);
321
322 void WriteRelocationsFragment(const MCAssembler &Asm, MCDataFragment *F,
323 const MCSectionData *SD);
324
Rafael Espindola70703872010-09-30 02:22:20 +0000325 bool IsFixupFullyResolved(const MCAssembler &Asm,
326 const MCValue Target,
327 bool IsPCRel,
328 const MCFragment *DF) const;
329
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000330 void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000331 };
332
333}
334
335// Emit the ELF header.
336void ELFObjectWriterImpl::WriteHeader(uint64_t SectionDataSize,
337 unsigned NumberOfSections) {
338 // ELF Header
339 // ----------
340 //
341 // Note
342 // ----
343 // emitWord method behaves differently for ELF32 and ELF64, writing
344 // 4 bytes in the former and 8 in the latter.
345
346 Write8(0x7f); // e_ident[EI_MAG0]
347 Write8('E'); // e_ident[EI_MAG1]
348 Write8('L'); // e_ident[EI_MAG2]
349 Write8('F'); // e_ident[EI_MAG3]
350
351 Write8(Is64Bit ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
352
353 // e_ident[EI_DATA]
354 Write8(Writer->isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
355
356 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky5baf79e2010-09-09 17:57:50 +0000357 // e_ident[EI_OSABI]
358 switch (OSType) {
359 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
360 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
361 default: Write8(ELF::ELFOSABI_NONE); break;
362 }
Matt Fleming3565a062010-08-16 18:57:57 +0000363 Write8(0); // e_ident[EI_ABIVERSION]
364
365 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
366
367 Write16(ELF::ET_REL); // e_type
368
Wesley Peckeecb8582010-10-22 15:52:49 +0000369 Write16(EMachine); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +0000370
371 Write32(ELF::EV_CURRENT); // e_version
372 WriteWord(0); // e_entry, no entry point in .o file
373 WriteWord(0); // e_phoff, no program header for .o
Benjamin Kramereb976772010-08-17 17:02:29 +0000374 WriteWord(SectionDataSize + (Is64Bit ? sizeof(ELF::Elf64_Ehdr) :
375 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000376
377 // FIXME: Make this configurable.
378 Write32(0); // e_flags = whatever the target wants
379
380 // e_ehsize = ELF header size
381 Write16(Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
382
383 Write16(0); // e_phentsize = prog header entry size
384 Write16(0); // e_phnum = # prog header entries = 0
385
386 // e_shentsize = Section header entry size
387 Write16(Is64Bit ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
388
389 // e_shnum = # of section header ents
Rafael Espindola7be2c332010-10-31 00:16:26 +0000390 if (NumberOfSections >= ELF::SHN_LORESERVE)
391 Write16(0);
392 else
393 Write16(NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000394
395 // e_shstrndx = Section # of '.shstrtab'
Rafael Espindola7be2c332010-10-31 00:16:26 +0000396 if (NumberOfSections >= ELF::SHN_LORESERVE)
397 Write16(ELF::SHN_XINDEX);
398 else
399 Write16(ShstrtabIndex);
Matt Fleming3565a062010-08-16 18:57:57 +0000400}
401
Rafael Espindola7be2c332010-10-31 00:16:26 +0000402void ELFObjectWriterImpl::WriteSymbolEntry(MCDataFragment *SymtabF,
403 MCDataFragment *ShndxF,
404 uint64_t name,
Matt Fleming3565a062010-08-16 18:57:57 +0000405 uint8_t info, uint64_t value,
406 uint64_t size, uint8_t other,
Rafael Espindola7be2c332010-10-31 00:16:26 +0000407 uint32_t shndx,
408 bool Reserved) {
409 if (ShndxF) {
410 char buf[4];
411 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
412 String32(buf, shndx);
413 else
414 String32(buf, 0);
415 ShndxF->getContents() += StringRef(buf, 4);
416 }
417
Matt Fleming3565a062010-08-16 18:57:57 +0000418 if (Is64Bit) {
419 char buf[8];
420
421 String32(buf, name);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000422 SymtabF->getContents() += StringRef(buf, 4); // st_name
Matt Fleming3565a062010-08-16 18:57:57 +0000423
424 String8(buf, info);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000425 SymtabF->getContents() += StringRef(buf, 1); // st_info
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000426
Matt Fleming3565a062010-08-16 18:57:57 +0000427 String8(buf, other);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000428 SymtabF->getContents() += StringRef(buf, 1); // st_other
Matt Fleming3565a062010-08-16 18:57:57 +0000429
Rafael Espindola7be2c332010-10-31 00:16:26 +0000430 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
431 String16(buf, ELF::SHN_XINDEX);
432 else
433 String16(buf, shndx);
434
435 SymtabF->getContents() += StringRef(buf, 2); // st_shndx
Matt Fleming3565a062010-08-16 18:57:57 +0000436
437 String64(buf, value);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000438 SymtabF->getContents() += StringRef(buf, 8); // st_value
Matt Fleming3565a062010-08-16 18:57:57 +0000439
440 String64(buf, size);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000441 SymtabF->getContents() += StringRef(buf, 8); // st_size
Matt Fleming3565a062010-08-16 18:57:57 +0000442 } else {
443 char buf[4];
444
445 String32(buf, name);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000446 SymtabF->getContents() += StringRef(buf, 4); // st_name
Matt Fleming3565a062010-08-16 18:57:57 +0000447
448 String32(buf, value);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000449 SymtabF->getContents() += StringRef(buf, 4); // st_value
Matt Fleming3565a062010-08-16 18:57:57 +0000450
451 String32(buf, size);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000452 SymtabF->getContents() += StringRef(buf, 4); // st_size
Matt Fleming3565a062010-08-16 18:57:57 +0000453
454 String8(buf, info);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000455 SymtabF->getContents() += StringRef(buf, 1); // st_info
Matt Fleming3565a062010-08-16 18:57:57 +0000456
457 String8(buf, other);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000458 SymtabF->getContents() += StringRef(buf, 1); // st_other
Matt Fleming3565a062010-08-16 18:57:57 +0000459
Rafael Espindola7be2c332010-10-31 00:16:26 +0000460 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
461 String16(buf, ELF::SHN_XINDEX);
462 else
463 String16(buf, shndx);
464
465 SymtabF->getContents() += StringRef(buf, 2); // st_shndx
Matt Fleming3565a062010-08-16 18:57:57 +0000466 }
467}
468
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000469static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout) {
470 if (Data.isCommon() && Data.isExternal())
471 return Data.getCommonAlignment();
472
473 const MCSymbol &Symbol = Data.getSymbol();
474 if (!Symbol.isInSection())
475 return 0;
476
Rafael Espindola1973d432010-10-28 19:39:57 +0000477 if (MCFragment *FF = Data.getFragment())
478 return Layout.getSymbolAddress(&Data) -
479 Layout.getSectionAddress(FF->getParent());
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000480
481 return 0;
482}
483
Rafael Espindolade89b012010-10-15 18:25:33 +0000484static const MCSymbol &AliasedSymbol(const MCSymbol &Symbol) {
485 const MCSymbol *S = &Symbol;
486 while (S->isVariable()) {
487 const MCExpr *Value = S->getVariableValue();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000488 if (Value->getKind() != MCExpr::SymbolRef)
489 return *S;
Rafael Espindolade89b012010-10-15 18:25:33 +0000490 const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr*>(Value);
491 S = &Ref->getSymbol();
492 }
493 return *S;
494}
495
Rafael Espindola88182132010-10-27 15:18:17 +0000496void ELFObjectWriterImpl::ExecutePostLayoutBinding(MCAssembler &Asm) {
497 // The presence of symbol versions causes undefined symbols and
498 // versions declared with @@@ to be renamed.
499
500 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
501 ie = Asm.symbol_end(); it != ie; ++it) {
502 const MCSymbol &Alias = it->getSymbol();
Rafael Espindola88182132010-10-27 15:18:17 +0000503 const MCSymbol &Symbol = AliasedSymbol(Alias);
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000504 MCSymbolData &SD = Asm.getSymbolData(Symbol);
505
506 // Undefined symbols are global, but this is the first place we
507 // are able to set it.
508 if (Symbol.isUndefined() && !Symbol.isVariable()) {
509 if (GetBinding(SD) == ELF::STB_LOCAL) {
510 SetBinding(SD, ELF::STB_GLOBAL);
511 SetBinding(*it, ELF::STB_GLOBAL);
512 }
513 }
514
515 // Not an alias.
516 if (&Symbol == &Alias)
517 continue;
518
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000519 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000520 size_t Pos = AliasName.find('@');
521 if (Pos == StringRef::npos)
522 continue;
523
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000524 // Aliases defined with .symvar copy the binding from the symbol they alias.
525 // This is the first place we are able to copy this information.
526 it->setExternal(SD.isExternal());
527 SetBinding(*it, GetBinding(SD));
528
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000529 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000530 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
531 continue;
532
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000533 // FIXME: produce a better error message.
534 if (Symbol.isUndefined() && Rest.startswith("@@") &&
535 !Rest.startswith("@@@"))
536 report_fatal_error("A @@ version cannot be undefined");
537
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000538 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000539 }
540}
541
Rafael Espindola7be2c332010-10-31 00:16:26 +0000542void ELFObjectWriterImpl::WriteSymbol(MCDataFragment *SymtabF,
543 MCDataFragment *ShndxF,
544 ELFSymbolData &MSD,
Matt Fleming3565a062010-08-16 18:57:57 +0000545 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000546 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000547 MCSymbolData &Data =
548 Layout.getAssembler().getSymbolData(AliasedSymbol(OrigData.getSymbol()));
Rafael Espindola152c1062010-10-06 21:02:29 +0000549
Rafael Espindola7be2c332010-10-31 00:16:26 +0000550 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
551 Data.getSymbol().isVariable();
552
Rafael Espindola152c1062010-10-06 21:02:29 +0000553 uint8_t Binding = GetBinding(OrigData);
554 uint8_t Visibility = GetVisibility(OrigData);
555 uint8_t Type = GetType(Data);
556
557 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
558 uint8_t Other = Visibility;
559
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000560 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000561 uint64_t Size = 0;
562 const MCExpr *ESize;
563
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000564 assert(!(Data.isCommon() && !Data.isExternal()));
565
Matt Fleming3565a062010-08-16 18:57:57 +0000566 ESize = Data.getSize();
567 if (Data.getSize()) {
568 MCValue Res;
569 if (ESize->getKind() == MCExpr::Binary) {
570 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(ESize);
571
572 if (BE->EvaluateAsRelocatable(Res, &Layout)) {
Benjamin Kramer24f12062010-10-17 07:38:40 +0000573 assert(!Res.getSymA() || !Res.getSymA()->getSymbol().isDefined());
574 assert(!Res.getSymB() || !Res.getSymB()->getSymbol().isDefined());
Rafael Espindolaf230df92010-10-16 18:23:53 +0000575 Size = Res.getConstant();
Matt Fleming3565a062010-08-16 18:57:57 +0000576 }
577 } else if (ESize->getKind() == MCExpr::Constant) {
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000578 Size = static_cast<const MCConstantExpr *>(ESize)->getValue();
Matt Fleming3565a062010-08-16 18:57:57 +0000579 } else {
580 assert(0 && "Unsupported size expression");
581 }
582 }
583
584 // Write out the symbol table entry
Rafael Espindola7be2c332010-10-31 00:16:26 +0000585 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
586 Size, Other, MSD.SectionIndex, IsReserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000587}
588
Rafael Espindola7be2c332010-10-31 00:16:26 +0000589void ELFObjectWriterImpl::WriteSymbolTable(MCDataFragment *SymtabF,
590 MCDataFragment *ShndxF,
Matt Fleming3565a062010-08-16 18:57:57 +0000591 const MCAssembler &Asm,
Rafael Espindola71859c62010-09-16 19:46:31 +0000592 const MCAsmLayout &Layout,
593 unsigned NumRegularSections) {
Matt Fleming3565a062010-08-16 18:57:57 +0000594 // The string table must be emitted first because we need the index
595 // into the string table for all the symbol names.
596 assert(StringTable.size() && "Missing string table");
597
598 // FIXME: Make sure the start of the symbol table is aligned.
599
600 // The first entry is the undefined symbol entry.
Rafael Espindola7be2c332010-10-31 00:16:26 +0000601 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
Matt Fleming3565a062010-08-16 18:57:57 +0000602
603 // Write the symbol table entries.
604 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
605 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
606 ELFSymbolData &MSD = LocalSymbolData[i];
Rafael Espindola7be2c332010-10-31 00:16:26 +0000607 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000608 }
609
Rafael Espindola71859c62010-09-16 19:46:31 +0000610 // Write out a symbol table entry for each regular section.
Eli Friedmana44fa242010-08-16 21:17:09 +0000611 unsigned Index = 1;
Rafael Espindola71859c62010-09-16 19:46:31 +0000612 for (MCAssembler::const_iterator it = Asm.begin();
613 Index <= NumRegularSections; ++it, ++Index) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000614 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000615 static_cast<const MCSectionELF&>(it->getSection());
Eli Friedmana44fa242010-08-16 21:17:09 +0000616 // Leave out relocations so we don't have indexes within
617 // the relocations messed up
Benjamin Kramer377a5722010-08-17 17:30:07 +0000618 if (Section.getType() == ELF::SHT_RELA || Section.getType() == ELF::SHT_REL)
Eli Friedmana44fa242010-08-16 21:17:09 +0000619 continue;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000620 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
621 ELF::STV_DEFAULT, Index, false);
Eli Friedmana44fa242010-08-16 21:17:09 +0000622 LastLocalSymbolIndex++;
623 }
Matt Fleming3565a062010-08-16 18:57:57 +0000624
625 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
626 ELFSymbolData &MSD = ExternalSymbolData[i];
627 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000628 assert(((Data.getFlags() & ELF_STB_Global) ||
629 (Data.getFlags() & ELF_STB_Weak)) &&
630 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000631 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000632 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000633 LastLocalSymbolIndex++;
634 }
635
636 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
637 ELFSymbolData &MSD = UndefinedSymbolData[i];
638 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000639 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000640 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000641 LastLocalSymbolIndex++;
642 }
643}
644
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000645static bool ShouldRelocOnSymbol(const MCSymbolData &SD,
Rafael Espindola3729d002010-10-05 23:57:26 +0000646 const MCValue &Target,
647 const MCFragment &F) {
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000648 const MCSymbol &Symbol = SD.getSymbol();
649 if (Symbol.isUndefined())
650 return true;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000651
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000652 const MCSectionELF &Section =
653 static_cast<const MCSectionELF&>(Symbol.getSection());
654
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000655 if (SD.isExternal())
656 return true;
657
Rafael Espindola8cecf252010-10-06 16:23:36 +0000658 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000659 const MCSectionELF &Sec2 =
660 static_cast<const MCSectionELF&>(F.getParent()->getSection());
661
Rafael Espindolace2d3c52010-10-18 20:25:33 +0000662 if (Section.getKind().isBSS())
663 return false;
664
Rafael Espindola8cecf252010-10-06 16:23:36 +0000665 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000666 (Kind == MCSymbolRefExpr::VK_PLT ||
667 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
668 Kind == MCSymbolRefExpr::VK_GOTOFF))
Rafael Espindola3729d002010-10-05 23:57:26 +0000669 return true;
670
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000671 if (Section.getFlags() & MCSectionELF::SHF_MERGE)
672 return Target.getConstant() != 0;
673
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000674 return false;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000675}
676
Benjamin Kramere5b57342010-08-17 18:20:28 +0000677// FIXME: this is currently X86/X86_64 only
Matt Fleming3565a062010-08-16 18:57:57 +0000678void ELFObjectWriterImpl::RecordRelocation(const MCAssembler &Asm,
679 const MCAsmLayout &Layout,
680 const MCFragment *Fragment,
681 const MCFixup &Fixup,
682 MCValue Target,
683 uint64_t &FixedValue) {
Matt Fleming3565a062010-08-16 18:57:57 +0000684 int64_t Addend = 0;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000685 int Index = 0;
Benjamin Kramer95c602a2010-08-27 10:38:39 +0000686 int64_t Value = Target.getConstant();
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000687 const MCSymbol *Symbol = 0;
Rafael Espindola29129722010-10-28 19:08:03 +0000688 const MCSymbol *Renamed = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000689
Rafael Espindola00074892010-09-17 22:34:41 +0000690 bool IsPCRel = isFixupKindX86PCRel(Fixup.getKind());
Benjamin Kramer81cfb852010-08-17 19:45:05 +0000691 if (!Target.isAbsolute()) {
Rafael Espindolade89b012010-10-15 18:25:33 +0000692 Symbol = &AliasedSymbol(Target.getSymA()->getSymbol());
Rafael Espindola29129722010-10-28 19:08:03 +0000693 Renamed = Renames.lookup(Symbol);
694 if (!Renamed)
Rafael Espindola484291c2010-11-01 14:28:48 +0000695 Renamed = &Target.getSymA()->getSymbol();
Matt Fleming3565a062010-08-16 18:57:57 +0000696 MCSymbolData &SD = Asm.getSymbolData(*Symbol);
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000697 MCFragment *F = SD.getFragment();
Matt Fleming3565a062010-08-16 18:57:57 +0000698
Rafael Espindola9d8b7552010-10-03 00:46:57 +0000699 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
700 const MCSymbol &SymbolB = RefB->getSymbol();
701 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
702 IsPCRel = true;
Rafael Espindola55fb1022010-10-04 15:59:01 +0000703 MCSectionData *Sec = Fragment->getParent();
704
705 // Offset of the symbol in the section
706 int64_t a = Layout.getSymbolAddress(&SDB) - Layout.getSectionAddress(Sec);
707
708 // Ofeset of the relocation in the section
709 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
710 Value += b - a;
Rafael Espindola9d8b7552010-10-03 00:46:57 +0000711 }
712
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000713 // Check that this case has already been fully resolved before we get
714 // here.
Rafael Espindola00074892010-09-17 22:34:41 +0000715 if (Symbol->isDefined() && !SD.isExternal() &&
716 IsPCRel &&
717 &Fragment->getParent()->getSection() == &Symbol->getSection()) {
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000718 llvm_unreachable("We don't need a relocation in this case.");
Rafael Espindola00074892010-09-17 22:34:41 +0000719 return;
720 }
721
Rafael Espindola3729d002010-10-05 23:57:26 +0000722 bool RelocOnSymbol = ShouldRelocOnSymbol(SD, Target, *Fragment);
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000723 if (!RelocOnSymbol) {
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000724 Index = F->getParent()->getOrdinal();
Rafael Espindolaa6489182010-09-24 21:19:03 +0000725
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000726 MCSectionData *FSD = F->getParent();
727 // Offset of the symbol in the section
728 Value += Layout.getSymbolAddress(&SD) - Layout.getSectionAddress(FSD);
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000729 } else {
Rafael Espindola29129722010-10-28 19:08:03 +0000730 UsedInReloc.insert(Renamed);
Rafael Espindola484291c2010-11-01 14:28:48 +0000731 MCSymbolData &RenamedSD = Asm.getSymbolData(*Renamed);
732 if (RenamedSD.getFlags() & ELF_Other_Weakref) {
733 WeakrefUsedInReloc.insert(Symbol);
734 }
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000735 Index = -1;
736 }
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000737 Addend = Value;
738 // Compensate for the addend on i386.
739 if (Is64Bit)
740 Value = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000741 }
742
Benjamin Kramer95c602a2010-08-27 10:38:39 +0000743 FixedValue = Value;
744
Matt Fleming3565a062010-08-16 18:57:57 +0000745 // determine the type of the relocation
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000746
747 MCSymbolRefExpr::VariantKind Modifier = Target.getSymA()->getKind();
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000748 unsigned Type;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000749 if (Is64Bit) {
750 if (IsPCRel) {
Rafael Espindola92bf6682010-10-04 19:04:13 +0000751 switch (Modifier) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000752 default:
753 llvm_unreachable("Unimplemented");
Rafael Espindola92bf6682010-10-04 19:04:13 +0000754 case MCSymbolRefExpr::VK_None:
755 Type = ELF::R_X86_64_PC32;
756 break;
757 case MCSymbolRefExpr::VK_PLT:
758 Type = ELF::R_X86_64_PLT32;
759 break;
Rafael Espindolaa0a2f872010-10-28 14:22:44 +0000760 case MCSymbolRefExpr::VK_GOTPCREL:
Rafael Espindola607d1f62010-10-04 19:51:39 +0000761 Type = ELF::R_X86_64_GOTPCREL;
762 break;
Rafael Espindolabc82d8b2010-10-27 20:28:07 +0000763 case MCSymbolRefExpr::VK_GOTTPOFF:
764 Type = ELF::R_X86_64_GOTTPOFF;
765 break;
766 case MCSymbolRefExpr::VK_TLSGD:
767 Type = ELF::R_X86_64_TLSGD;
768 break;
Rafael Espindolab4d17212010-10-28 15:02:40 +0000769 case MCSymbolRefExpr::VK_TLSLD:
770 Type = ELF::R_X86_64_TLSLD;
771 break;
Rafael Espindola92bf6682010-10-04 19:04:13 +0000772 }
Benjamin Kramere5b57342010-08-17 18:20:28 +0000773 } else {
774 switch ((unsigned)Fixup.getKind()) {
775 default: llvm_unreachable("invalid fixup kind!");
776 case FK_Data_8: Type = ELF::R_X86_64_64; break;
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000777 case X86::reloc_signed_4byte:
Benjamin Kramere5b57342010-08-17 18:20:28 +0000778 case X86::reloc_pcrel_4byte:
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000779 assert(isInt<32>(Target.getConstant()));
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000780 switch (Modifier) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000781 default:
782 llvm_unreachable("Unimplemented");
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000783 case MCSymbolRefExpr::VK_None:
784 Type = ELF::R_X86_64_32S;
785 break;
786 case MCSymbolRefExpr::VK_GOT:
787 Type = ELF::R_X86_64_GOT32;
788 break;
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000789 case MCSymbolRefExpr::VK_GOTPCREL:
Rafael Espindola8cecf252010-10-06 16:23:36 +0000790 Type = ELF::R_X86_64_GOTPCREL;
791 break;
Rafael Espindolabc82d8b2010-10-27 20:28:07 +0000792 case MCSymbolRefExpr::VK_TPOFF:
793 Type = ELF::R_X86_64_TPOFF32;
794 break;
Rafael Espindolaaa8f1f02010-10-28 15:11:03 +0000795 case MCSymbolRefExpr::VK_DTPOFF:
796 Type = ELF::R_X86_64_DTPOFF32;
797 break;
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000798 }
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000799 break;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000800 case FK_Data_4:
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000801 Type = ELF::R_X86_64_32;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000802 break;
803 case FK_Data_2: Type = ELF::R_X86_64_16; break;
804 case X86::reloc_pcrel_1byte:
805 case FK_Data_1: Type = ELF::R_X86_64_8; break;
806 }
807 }
Matt Fleming3565a062010-08-16 18:57:57 +0000808 } else {
Benjamin Kramere5b57342010-08-17 18:20:28 +0000809 if (IsPCRel) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000810 switch (Modifier) {
811 default:
Rafael Espindola24dc9ec2010-10-18 19:33:01 +0000812 llvm_unreachable("Unimplemented");
813 case MCSymbolRefExpr::VK_None:
Rafael Espindola9baee3b2010-10-18 18:03:28 +0000814 Type = ELF::R_386_PC32;
Rafael Espindola9baee3b2010-10-18 18:03:28 +0000815 break;
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000816 case MCSymbolRefExpr::VK_PLT:
817 Type = ELF::R_386_PLT32;
818 break;
819 }
Benjamin Kramere5b57342010-08-17 18:20:28 +0000820 } else {
821 switch ((unsigned)Fixup.getKind()) {
822 default: llvm_unreachable("invalid fixup kind!");
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000823
Rafael Espindola24ba4f72010-10-24 17:35:42 +0000824 case X86::reloc_global_offset_table:
825 Type = ELF::R_386_GOTPC;
826 break;
827
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000828 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
829 // instead?
830 case X86::reloc_signed_4byte:
Benjamin Kramere5b57342010-08-17 18:20:28 +0000831 case X86::reloc_pcrel_4byte:
Rafael Espindolabd701182010-10-19 19:31:37 +0000832 case FK_Data_4:
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000833 switch (Modifier) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000834 default:
835 llvm_unreachable("Unimplemented");
Rafael Espindolabd701182010-10-19 19:31:37 +0000836 case MCSymbolRefExpr::VK_None:
Rafael Espindola24ba4f72010-10-24 17:35:42 +0000837 Type = ELF::R_386_32;
Rafael Espindolabd701182010-10-19 19:31:37 +0000838 break;
Rafael Espindolaeada3042010-10-18 20:47:21 +0000839 case MCSymbolRefExpr::VK_GOT:
840 Type = ELF::R_386_GOT32;
841 break;
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000842 case MCSymbolRefExpr::VK_GOTOFF:
843 Type = ELF::R_386_GOTOFF;
844 break;
Rafael Espindola3cede2d2010-10-27 21:23:52 +0000845 case MCSymbolRefExpr::VK_TLSGD:
846 Type = ELF::R_386_TLS_GD;
847 break;
848 case MCSymbolRefExpr::VK_TPOFF:
849 Type = ELF::R_386_TLS_LE_32;
850 break;
851 case MCSymbolRefExpr::VK_INDNTPOFF:
852 Type = ELF::R_386_TLS_IE;
853 break;
854 case MCSymbolRefExpr::VK_NTPOFF:
855 Type = ELF::R_386_TLS_LE;
856 break;
Rafael Espindolaa0a2f872010-10-28 14:22:44 +0000857 case MCSymbolRefExpr::VK_GOTNTPOFF:
858 Type = ELF::R_386_TLS_GOTIE;
859 break;
Rafael Espindolaa264f722010-10-28 14:37:09 +0000860 case MCSymbolRefExpr::VK_TLSLDM:
861 Type = ELF::R_386_TLS_LDM;
862 break;
Rafael Espindola0cf15d62010-10-28 14:48:59 +0000863 case MCSymbolRefExpr::VK_DTPOFF:
864 Type = ELF::R_386_TLS_LDO_32;
865 break;
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000866 }
867 break;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000868 case FK_Data_2: Type = ELF::R_386_16; break;
869 case X86::reloc_pcrel_1byte:
870 case FK_Data_1: Type = ELF::R_386_8; break;
871 }
Matt Fleming3565a062010-08-16 18:57:57 +0000872 }
873 }
874
Rafael Espindolaa0a2f872010-10-28 14:22:44 +0000875 if (RelocNeedsGOT(Modifier))
Rafael Espindola5c77c162010-10-05 15:48:37 +0000876 NeedsGOT = true;
877
Benjamin Kramere5b57342010-08-17 18:20:28 +0000878 ELFRelocationEntry ERE;
879
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000880 ERE.Index = Index;
881 ERE.Type = Type;
Rafael Espindola29129722010-10-28 19:08:03 +0000882 ERE.Symbol = Renamed;
Matt Fleming3565a062010-08-16 18:57:57 +0000883
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000884 ERE.r_offset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
Benjamin Kramere5b57342010-08-17 18:20:28 +0000885
Matt Fleming3565a062010-08-16 18:57:57 +0000886 if (HasRelocationAddend)
887 ERE.r_addend = Addend;
Benjamin Kramer172d7d62010-08-17 00:33:24 +0000888 else
889 ERE.r_addend = 0; // Silence compiler warning.
Matt Fleming3565a062010-08-16 18:57:57 +0000890
891 Relocations[Fragment->getParent()].push_back(ERE);
892}
893
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000894uint64_t
895ELFObjectWriterImpl::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
896 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000897 MCSymbolData &SD = Asm.getSymbolData(*S);
Eli Friedmanf8020a32010-08-16 19:15:06 +0000898
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000899 // Local symbol.
900 if (!SD.isExternal() && !S->isUndefined())
901 return SD.getIndex() + /* empty symbol */ 1;
902
903 // External or undefined symbol.
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000904 return SD.getIndex() + NumRegularSections + /* empty symbol */ 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000905}
906
Rafael Espindola737cd212010-10-05 18:01:23 +0000907static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
Rafael Espindola88182132010-10-27 15:18:17 +0000908 bool Used, bool Renamed) {
Rafael Espindola484291c2010-11-01 14:28:48 +0000909 if (Data.getFlags() & ELF_Other_Weakref)
910 return false;
911
Rafael Espindolabd701182010-10-19 19:31:37 +0000912 if (Used)
913 return true;
914
Rafael Espindola88182132010-10-27 15:18:17 +0000915 if (Renamed)
916 return false;
917
Rafael Espindola737cd212010-10-05 18:01:23 +0000918 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000919
Rafael Espindolad1798862010-10-29 23:09:31 +0000920 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
921 return true;
922
Rafael Espindolaa6866962010-10-27 14:44:52 +0000923 const MCSymbol &A = AliasedSymbol(Symbol);
Rafael Espindolad1798862010-10-29 23:09:31 +0000924 if (!A.isVariable() && A.isUndefined() && !Data.isCommon())
Rafael Espindolaa6866962010-10-27 14:44:52 +0000925 return false;
926
Rafael Espindola737cd212010-10-05 18:01:23 +0000927 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
928 return false;
929
Rafael Espindolabd701182010-10-19 19:31:37 +0000930 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000931 return false;
932
933 return true;
934}
935
936static bool isLocal(const MCSymbolData &Data) {
937 if (Data.isExternal())
938 return false;
939
940 const MCSymbol &Symbol = Data.getSymbol();
941 if (Symbol.isUndefined() && !Symbol.isVariable())
942 return false;
943
944 return true;
945}
946
Matt Fleming3565a062010-08-16 18:57:57 +0000947void ELFObjectWriterImpl::ComputeSymbolTable(MCAssembler &Asm) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000948 // FIXME: Is this the correct place to do this?
949 if (NeedsGOT) {
950 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
951 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
952 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
953 Data.setExternal(true);
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000954 SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindola5c77c162010-10-05 15:48:37 +0000955 }
956
Matt Fleming3565a062010-08-16 18:57:57 +0000957 // Build section lookup table.
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000958 NumRegularSections = Asm.size();
Rafael Espindolaf5c347d2010-10-05 21:20:07 +0000959 DenseMap<const MCSection*, uint32_t> SectionIndexMap;
Matt Fleming3565a062010-08-16 18:57:57 +0000960 unsigned Index = 1;
961 for (MCAssembler::iterator it = Asm.begin(),
962 ie = Asm.end(); it != ie; ++it, ++Index)
963 SectionIndexMap[&it->getSection()] = Index;
964
965 // Index 0 is always the empty string.
966 StringMap<uint64_t> StringIndexMap;
967 StringTable += '\x00';
968
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000969 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000970 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
971 ie = Asm.symbol_end(); it != ie; ++it) {
972 const MCSymbol &Symbol = it->getSymbol();
973
Rafael Espindola484291c2010-11-01 14:28:48 +0000974 bool Used = UsedInReloc.count(&Symbol);
975 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
976 if (!isInSymtab(Asm, *it, Used || WeakrefUsed,
Rafael Espindola88182132010-10-27 15:18:17 +0000977 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000978 continue;
979
Matt Fleming3565a062010-08-16 18:57:57 +0000980 ELFSymbolData MSD;
981 MSD.SymbolData = it;
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000982 bool Local = isLocal(*it);
Rafael Espindola88182132010-10-27 15:18:17 +0000983 const MCSymbol &RefSymbol = AliasedSymbol(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000984
Rafael Espindola484291c2010-11-01 14:28:48 +0000985 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
986 SetBinding(*it, ELF::STB_WEAK);
987
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000988 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000989 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000990 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000991 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000992 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000993 } else if (RefSymbol.isUndefined()) {
Matt Fleming3565a062010-08-16 18:57:57 +0000994 MSD.SectionIndex = ELF::SHN_UNDEF;
Matt Fleming3565a062010-08-16 18:57:57 +0000995 } else {
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000996 MSD.SectionIndex = SectionIndexMap.lookup(&RefSymbol.getSection());
Rafael Espindola7be2c332010-10-31 00:16:26 +0000997 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
998 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000999 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +00001000 }
1001
Rafael Espindola88182132010-10-27 15:18:17 +00001002 // The @@@ in symbol version is replaced with @ in undefined symbols and
1003 // @@ in defined ones.
1004 StringRef Name = Symbol.getName();
1005 size_t Pos = Name.find("@@@");
1006 std::string FinalName;
1007 if (Pos != StringRef::npos) {
Benjamin Kramer07ee6322010-10-27 19:53:52 +00001008 StringRef Prefix = Name.substr(0, Pos);
Rafael Espindola88182132010-10-27 15:18:17 +00001009 unsigned n = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
Benjamin Kramer07ee6322010-10-27 19:53:52 +00001010 StringRef Suffix = Name.substr(Pos + n);
Rafael Espindola88182132010-10-27 15:18:17 +00001011 FinalName = Prefix.str() + Suffix.str();
1012 } else {
1013 FinalName = Name.str();
1014 }
1015
1016 uint64_t &Entry = StringIndexMap[FinalName];
Rafael Espindolaa6866962010-10-27 14:44:52 +00001017 if (!Entry) {
1018 Entry = StringTable.size();
Rafael Espindola88182132010-10-27 15:18:17 +00001019 StringTable += FinalName;
Rafael Espindolaa6866962010-10-27 14:44:52 +00001020 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +00001021 }
Rafael Espindolaa6866962010-10-27 14:44:52 +00001022 MSD.StringIndex = Entry;
1023 if (MSD.SectionIndex == ELF::SHN_UNDEF)
1024 UndefinedSymbolData.push_back(MSD);
1025 else if (Local)
1026 LocalSymbolData.push_back(MSD);
1027 else
1028 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +00001029 }
1030
1031 // Symbols are required to be in lexicographic order.
1032 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
1033 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
1034 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
1035
1036 // Set the symbol indices. Local symbols must come before all other
1037 // symbols with non-local bindings.
1038 Index = 0;
1039 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
1040 LocalSymbolData[i].SymbolData->setIndex(Index++);
1041 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
1042 ExternalSymbolData[i].SymbolData->setIndex(Index++);
1043 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
1044 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
1045}
1046
1047void ELFObjectWriterImpl::WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
1048 const MCSectionData &SD) {
1049 if (!Relocations[&SD].empty()) {
1050 MCContext &Ctx = Asm.getContext();
1051 const MCSection *RelaSection;
1052 const MCSectionELF &Section =
1053 static_cast<const MCSectionELF&>(SD.getSection());
1054
1055 const StringRef SectionName = Section.getSectionName();
Benjamin Kramer377a5722010-08-17 17:30:07 +00001056 std::string RelaSectionName = HasRelocationAddend ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +00001057 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +00001058
1059 unsigned EntrySize;
1060 if (HasRelocationAddend)
1061 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
1062 else
1063 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +00001064
Benjamin Kramer377a5722010-08-17 17:30:07 +00001065 RelaSection = Ctx.getELFSection(RelaSectionName, HasRelocationAddend ?
1066 ELF::SHT_RELA : ELF::SHT_REL, 0,
Matt Fleming3565a062010-08-16 18:57:57 +00001067 SectionKind::getReadOnly(),
Rafael Espindolae61a1ac2010-11-09 22:37:44 +00001068 EntrySize);
Matt Fleming3565a062010-08-16 18:57:57 +00001069
1070 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001071 RelaSD.setAlignment(Is64Bit ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +00001072
1073 MCDataFragment *F = new MCDataFragment(&RelaSD);
1074
1075 WriteRelocationsFragment(Asm, F, &SD);
1076
Rafael Espindola70703872010-09-30 02:22:20 +00001077 Asm.AddSectionToTheEnd(*Writer, RelaSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +00001078 }
1079}
1080
1081void ELFObjectWriterImpl::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
1082 uint64_t Flags, uint64_t Address,
1083 uint64_t Offset, uint64_t Size,
1084 uint32_t Link, uint32_t Info,
1085 uint64_t Alignment,
1086 uint64_t EntrySize) {
1087 Write32(Name); // sh_name: index into string table
1088 Write32(Type); // sh_type
1089 WriteWord(Flags); // sh_flags
1090 WriteWord(Address); // sh_addr
1091 WriteWord(Offset); // sh_offset
1092 WriteWord(Size); // sh_size
1093 Write32(Link); // sh_link
1094 Write32(Info); // sh_info
1095 WriteWord(Alignment); // sh_addralign
1096 WriteWord(EntrySize); // sh_entsize
1097}
1098
1099void ELFObjectWriterImpl::WriteRelocationsFragment(const MCAssembler &Asm,
1100 MCDataFragment *F,
1101 const MCSectionData *SD) {
1102 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
1103 // sort by the r_offset just like gnu as does
1104 array_pod_sort(Relocs.begin(), Relocs.end());
1105
1106 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1107 ELFRelocationEntry entry = Relocs[e - i - 1];
1108
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001109 if (entry.Index < 0)
1110 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
1111 else
1112 entry.Index += LocalSymbolData.size() + 1;
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001113 if (Is64Bit) {
1114 char buf[8];
Matt Fleming3565a062010-08-16 18:57:57 +00001115
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001116 String64(buf, entry.r_offset);
1117 F->getContents() += StringRef(buf, 8);
1118
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001119 struct ELF::Elf64_Rela ERE64;
1120 ERE64.setSymbolAndType(entry.Index, entry.Type);
1121 String64(buf, ERE64.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001122 F->getContents() += StringRef(buf, 8);
1123
1124 if (HasRelocationAddend) {
1125 String64(buf, entry.r_addend);
1126 F->getContents() += StringRef(buf, 8);
1127 }
1128 } else {
1129 char buf[4];
1130
1131 String32(buf, entry.r_offset);
1132 F->getContents() += StringRef(buf, 4);
1133
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001134 struct ELF::Elf32_Rela ERE32;
1135 ERE32.setSymbolAndType(entry.Index, entry.Type);
1136 String32(buf, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001137 F->getContents() += StringRef(buf, 4);
1138
1139 if (HasRelocationAddend) {
1140 String32(buf, entry.r_addend);
1141 F->getContents() += StringRef(buf, 4);
1142 }
1143 }
Matt Fleming3565a062010-08-16 18:57:57 +00001144 }
1145}
1146
1147void ELFObjectWriterImpl::CreateMetadataSections(MCAssembler &Asm,
1148 MCAsmLayout &Layout) {
1149 MCContext &Ctx = Asm.getContext();
1150 MCDataFragment *F;
1151
Matt Fleming3565a062010-08-16 18:57:57 +00001152 unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
1153
Rafael Espindola71859c62010-09-16 19:46:31 +00001154 unsigned NumRegularSections = Asm.size();
1155
Rafael Espindola38738bf2010-09-22 19:04:41 +00001156 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola7be2c332010-10-31 00:16:26 +00001157 const MCSection *ShstrtabSection =
1158 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
1159 SectionKind::getReadOnly(), false);
Rafael Espindola71859c62010-09-16 19:46:31 +00001160 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1161 ShstrtabSD.setAlignment(1);
1162 ShstrtabIndex = Asm.size();
1163
Rafael Espindola7be2c332010-10-31 00:16:26 +00001164 const MCSection *SymtabSection =
1165 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1166 SectionKind::getReadOnly(),
Rafael Espindolae61a1ac2010-11-09 22:37:44 +00001167 EntrySize);
Matt Fleming3565a062010-08-16 18:57:57 +00001168 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Matt Fleming3565a062010-08-16 18:57:57 +00001169 SymtabSD.setAlignment(Is64Bit ? 8 : 4);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001170 SymbolTableIndex = Asm.size();
1171
1172 MCSectionData *SymtabShndxSD = NULL;
1173
1174 if (NeedsSymtabShndx) {
1175 const MCSection *SymtabShndxSection =
1176 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
Rafael Espindolae61a1ac2010-11-09 22:37:44 +00001177 SectionKind::getReadOnly(), 4);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001178 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
1179 SymtabShndxSD->setAlignment(4);
1180 }
Matt Fleming3565a062010-08-16 18:57:57 +00001181
Matt Fleming3565a062010-08-16 18:57:57 +00001182 const MCSection *StrtabSection;
1183 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
1184 SectionKind::getReadOnly(), false);
Matt Fleming3565a062010-08-16 18:57:57 +00001185 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1186 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +00001187 StringTableIndex = Asm.size();
1188
Rafael Espindolac3c413f2010-09-27 22:04:54 +00001189 WriteRelocations(Asm, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001190
1191 // Symbol table
1192 F = new MCDataFragment(&SymtabSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001193 MCDataFragment *ShndxF = NULL;
1194 if (NeedsSymtabShndx) {
1195 ShndxF = new MCDataFragment(SymtabShndxSD);
1196 Asm.AddSectionToTheEnd(*Writer, *SymtabShndxSD, Layout);
1197 }
1198 WriteSymbolTable(F, ShndxF, Asm, Layout, NumRegularSections);
Rafael Espindola70703872010-09-30 02:22:20 +00001199 Asm.AddSectionToTheEnd(*Writer, SymtabSD, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001200
Matt Fleming3565a062010-08-16 18:57:57 +00001201 F = new MCDataFragment(&StrtabSD);
1202 F->getContents().append(StringTable.begin(), StringTable.end());
Rafael Espindola70703872010-09-30 02:22:20 +00001203 Asm.AddSectionToTheEnd(*Writer, StrtabSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +00001204
Matt Fleming3565a062010-08-16 18:57:57 +00001205 F = new MCDataFragment(&ShstrtabSD);
1206
Matt Fleming3565a062010-08-16 18:57:57 +00001207 // Section header string table.
1208 //
1209 // The first entry of a string table holds a null character so skip
1210 // section 0.
1211 uint64_t Index = 1;
1212 F->getContents() += '\x00';
1213
1214 for (MCAssembler::const_iterator it = Asm.begin(),
1215 ie = Asm.end(); it != ie; ++it) {
Matt Fleming3565a062010-08-16 18:57:57 +00001216 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +00001217 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola51efe7a2010-09-23 14:14:56 +00001218 // FIXME: We could merge suffixes like in .text and .rela.text.
Matt Fleming3565a062010-08-16 18:57:57 +00001219
1220 // Remember the index into the string table so we can write it
1221 // into the sh_name field of the section header table.
1222 SectionStringTableIndex[&it->getSection()] = Index;
1223
1224 Index += Section.getSectionName().size() + 1;
1225 F->getContents() += Section.getSectionName();
1226 F->getContents() += '\x00';
1227 }
1228
Rafael Espindola70703872010-09-30 02:22:20 +00001229 Asm.AddSectionToTheEnd(*Writer, ShstrtabSD, Layout);
1230}
1231
1232bool ELFObjectWriterImpl::IsFixupFullyResolved(const MCAssembler &Asm,
1233 const MCValue Target,
1234 bool IsPCRel,
1235 const MCFragment *DF) const {
1236 // If this is a PCrel relocation, find the section this fixup value is
1237 // relative to.
1238 const MCSection *BaseSection = 0;
1239 if (IsPCRel) {
1240 BaseSection = &DF->getParent()->getSection();
1241 assert(BaseSection);
1242 }
1243
1244 const MCSection *SectionA = 0;
1245 const MCSymbol *SymbolA = 0;
1246 if (const MCSymbolRefExpr *A = Target.getSymA()) {
1247 SymbolA = &A->getSymbol();
1248 SectionA = &SymbolA->getSection();
1249 }
1250
1251 const MCSection *SectionB = 0;
1252 if (const MCSymbolRefExpr *B = Target.getSymB()) {
1253 SectionB = &B->getSymbol().getSection();
1254 }
1255
1256 if (!BaseSection)
1257 return SectionA == SectionB;
1258
1259 const MCSymbolData &DataA = Asm.getSymbolData(*SymbolA);
1260 if (DataA.isExternal())
1261 return false;
1262
1263 return !SectionB && BaseSection == SectionA;
Matt Fleming3565a062010-08-16 18:57:57 +00001264}
1265
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001266void ELFObjectWriterImpl::WriteObject(MCAssembler &Asm,
Matt Fleming3565a062010-08-16 18:57:57 +00001267 const MCAsmLayout &Layout) {
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001268 // Compute symbol table information.
1269 ComputeSymbolTable(Asm);
1270
Matt Fleming3565a062010-08-16 18:57:57 +00001271 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
1272 const_cast<MCAsmLayout&>(Layout));
1273
1274 // Add 1 for the null section.
1275 unsigned NumSections = Asm.size() + 1;
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001276 uint64_t NaturalAlignment = Is64Bit ? 8 : 4;
1277 uint64_t HeaderSize = Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr);
1278 uint64_t FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001279
1280 for (MCAssembler::const_iterator it = Asm.begin(),
1281 ie = Asm.end(); it != ie; ++it) {
1282 const MCSectionData &SD = *it;
Matt Fleming3565a062010-08-16 18:57:57 +00001283
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001284 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1285
Matt Fleming3565a062010-08-16 18:57:57 +00001286 // Get the size of the section in the output file (including padding).
1287 uint64_t Size = Layout.getSectionFileSize(&SD);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001288
1289 FileOff += Size;
Matt Fleming3565a062010-08-16 18:57:57 +00001290 }
1291
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001292 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1293
Matt Fleming3565a062010-08-16 18:57:57 +00001294 // Write out the ELF header ...
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001295 WriteHeader(FileOff - HeaderSize, NumSections);
1296
1297 FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001298
1299 // ... then all of the sections ...
1300 DenseMap<const MCSection*, uint64_t> SectionOffsetMap;
1301
Rafael Espindolad8e0bfe2010-10-06 22:28:19 +00001302 DenseMap<const MCSection*, uint32_t> SectionIndexMap;
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001303
1304 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +00001305 for (MCAssembler::const_iterator it = Asm.begin(),
1306 ie = Asm.end(); it != ie; ++it) {
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001307 const MCSectionData &SD = *it;
1308
1309 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1310 WriteZeros(Padding);
1311 FileOff += Padding;
1312
Matt Fleming3565a062010-08-16 18:57:57 +00001313 // Remember the offset into the file for this section.
1314 SectionOffsetMap[&it->getSection()] = FileOff;
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001315 SectionIndexMap[&it->getSection()] = Index++;
1316
Matt Fleming3565a062010-08-16 18:57:57 +00001317 FileOff += Layout.getSectionFileSize(&SD);
1318
1319 Asm.WriteSectionData(it, Layout, Writer);
1320 }
1321
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001322 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1323 WriteZeros(Padding);
1324 FileOff += Padding;
1325
Matt Fleming3565a062010-08-16 18:57:57 +00001326 // ... and then the section header table.
1327 // Should we align the section header table?
1328 //
1329 // Null section first.
Rafael Espindola7be2c332010-10-31 00:16:26 +00001330 uint64_t FirstSectionSize =
1331 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1332 uint32_t FirstSectionLink =
1333 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1334 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming3565a062010-08-16 18:57:57 +00001335
1336 for (MCAssembler::const_iterator it = Asm.begin(),
1337 ie = Asm.end(); it != ie; ++it) {
1338 const MCSectionData &SD = *it;
1339 const MCSectionELF &Section =
1340 static_cast<const MCSectionELF&>(SD.getSection());
1341
1342 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[&it->getSection()];
1348 sh_info = 0;
1349 break;
1350
1351 case ELF::SHT_REL:
Eli Friedmanf8020a32010-08-16 19:15:06 +00001352 case ELF::SHT_RELA: {
Matt Fleming3565a062010-08-16 18:57:57 +00001353 const MCSection *SymtabSection;
1354 const MCSection *InfoSection;
Matt Fleming3565a062010-08-16 18:57:57 +00001355
1356 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1357 SectionKind::getReadOnly(),
Eli Friedmanf8020a32010-08-16 19:15:06 +00001358 false);
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001359 sh_link = SectionIndexMap[SymtabSection];
Matt Fleming3565a062010-08-16 18:57:57 +00001360
Benjamin Kramer377a5722010-08-17 17:30:07 +00001361 // Remove ".rel" and ".rela" prefixes.
1362 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1363 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
1364
Eli Friedmanf8020a32010-08-16 19:15:06 +00001365 InfoSection = Asm.getContext().getELFSection(SectionName,
Matt Fleming3565a062010-08-16 18:57:57 +00001366 ELF::SHT_PROGBITS, 0,
Eli Friedmanf8020a32010-08-16 19:15:06 +00001367 SectionKind::getReadOnly(),
1368 false);
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001369 sh_info = SectionIndexMap[InfoSection];
Matt Fleming3565a062010-08-16 18:57:57 +00001370 break;
Eli Friedmanf8020a32010-08-16 19:15:06 +00001371 }
Matt Fleming3565a062010-08-16 18:57:57 +00001372
1373 case ELF::SHT_SYMTAB:
1374 case ELF::SHT_DYNSYM:
1375 sh_link = StringTableIndex;
1376 sh_info = LastLocalSymbolIndex;
1377 break;
1378
Rafael Espindola7be2c332010-10-31 00:16:26 +00001379 case ELF::SHT_SYMTAB_SHNDX:
1380 sh_link = SymbolTableIndex;
1381 break;
1382
Matt Fleming3565a062010-08-16 18:57:57 +00001383 case ELF::SHT_PROGBITS:
1384 case ELF::SHT_STRTAB:
1385 case ELF::SHT_NOBITS:
Benjamin Kramer19dc7fa2010-08-31 17:03:33 +00001386 case ELF::SHT_NULL:
Rafael Espindolacecbc3d2010-10-25 17:50:35 +00001387 case ELF::SHT_ARM_ATTRIBUTES:
Matt Fleming3565a062010-08-16 18:57:57 +00001388 // Nothing to do.
1389 break;
1390
Matt Fleming3565a062010-08-16 18:57:57 +00001391 default:
1392 assert(0 && "FIXME: sh_type value not supported!");
1393 break;
1394 }
1395
1396 WriteSecHdrEntry(SectionStringTableIndex[&it->getSection()],
1397 Section.getType(), Section.getFlags(),
Rafael Espindola71859c62010-09-16 19:46:31 +00001398 0,
Matt Fleming3565a062010-08-16 18:57:57 +00001399 SectionOffsetMap.lookup(&SD.getSection()),
1400 Layout.getSectionSize(&SD), sh_link,
1401 sh_info, SD.getAlignment(),
1402 Section.getEntrySize());
1403 }
1404}
1405
1406ELFObjectWriter::ELFObjectWriter(raw_ostream &OS,
1407 bool Is64Bit,
Roman Divacky5baf79e2010-09-09 17:57:50 +00001408 Triple::OSType OSType,
Wesley Peckeecb8582010-10-22 15:52:49 +00001409 uint16_t EMachine,
Matt Fleming3565a062010-08-16 18:57:57 +00001410 bool IsLittleEndian,
1411 bool HasRelocationAddend)
1412 : MCObjectWriter(OS, IsLittleEndian)
1413{
Wesley Peckeecb8582010-10-22 15:52:49 +00001414 Impl = new ELFObjectWriterImpl(this, Is64Bit, EMachine,
1415 HasRelocationAddend, OSType);
Matt Fleming3565a062010-08-16 18:57:57 +00001416}
1417
1418ELFObjectWriter::~ELFObjectWriter() {
1419 delete (ELFObjectWriterImpl*) Impl;
1420}
1421
1422void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm) {
1423 ((ELFObjectWriterImpl*) Impl)->ExecutePostLayoutBinding(Asm);
1424}
1425
1426void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
1427 const MCAsmLayout &Layout,
1428 const MCFragment *Fragment,
1429 const MCFixup &Fixup, MCValue Target,
1430 uint64_t &FixedValue) {
1431 ((ELFObjectWriterImpl*) Impl)->RecordRelocation(Asm, Layout, Fragment, Fixup,
1432 Target, FixedValue);
1433}
1434
Rafael Espindola70703872010-09-30 02:22:20 +00001435bool ELFObjectWriter::IsFixupFullyResolved(const MCAssembler &Asm,
1436 const MCValue Target,
1437 bool IsPCRel,
1438 const MCFragment *DF) const {
1439 return ((ELFObjectWriterImpl*) Impl)->IsFixupFullyResolved(Asm, Target,
1440 IsPCRel, DF);
1441}
1442
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001443void ELFObjectWriter::WriteObject(MCAssembler &Asm,
Matt Fleming3565a062010-08-16 18:57:57 +00001444 const MCAsmLayout &Layout) {
1445 ((ELFObjectWriterImpl*) Impl)->WriteObject(Asm, Layout);
1446}