blob: 10600a5acda7f24998c57a097887b4b0b72e553b [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,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000296 const SectionIndexMapTy &SectionIndexMap);
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
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000326 void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout,
327 const SectionIndexMapTy &SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +0000328
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000329 // Map from a group section to the signature symbol
330 typedef DenseMap<const MCSectionELF*, const MCSymbol*> GroupMapTy;
331 void CreateGroupSections(MCAssembler &Asm, MCAsmLayout &Layout,
332 GroupMapTy &GroupMap);
333
Rafael Espindola88182132010-10-27 15:18:17 +0000334 void ExecutePostLayoutBinding(MCAssembler &Asm);
Matt Fleming3565a062010-08-16 18:57:57 +0000335
336 void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
337 uint64_t Address, uint64_t Offset,
338 uint64_t Size, uint32_t Link, uint32_t Info,
339 uint64_t Alignment, uint64_t EntrySize);
340
341 void WriteRelocationsFragment(const MCAssembler &Asm, MCDataFragment *F,
342 const MCSectionData *SD);
343
Rafael Espindola70703872010-09-30 02:22:20 +0000344 bool IsFixupFullyResolved(const MCAssembler &Asm,
345 const MCValue Target,
346 bool IsPCRel,
347 const MCFragment *DF) const;
348
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000349 void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000350 void WriteSection(MCAssembler &Asm,
351 const SectionIndexMapTy &SectionIndexMap,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000352 uint32_t GroupSymbolIndex,
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000353 uint64_t Offset, uint64_t Size, uint64_t Alignment,
354 const MCSectionELF &Section);
Matt Fleming3565a062010-08-16 18:57:57 +0000355 };
356
357}
358
359// Emit the ELF header.
360void ELFObjectWriterImpl::WriteHeader(uint64_t SectionDataSize,
361 unsigned NumberOfSections) {
362 // ELF Header
363 // ----------
364 //
365 // Note
366 // ----
367 // emitWord method behaves differently for ELF32 and ELF64, writing
368 // 4 bytes in the former and 8 in the latter.
369
370 Write8(0x7f); // e_ident[EI_MAG0]
371 Write8('E'); // e_ident[EI_MAG1]
372 Write8('L'); // e_ident[EI_MAG2]
373 Write8('F'); // e_ident[EI_MAG3]
374
375 Write8(Is64Bit ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
376
377 // e_ident[EI_DATA]
378 Write8(Writer->isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
379
380 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky5baf79e2010-09-09 17:57:50 +0000381 // e_ident[EI_OSABI]
382 switch (OSType) {
383 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
384 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
385 default: Write8(ELF::ELFOSABI_NONE); break;
386 }
Matt Fleming3565a062010-08-16 18:57:57 +0000387 Write8(0); // e_ident[EI_ABIVERSION]
388
389 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
390
391 Write16(ELF::ET_REL); // e_type
392
Wesley Peckeecb8582010-10-22 15:52:49 +0000393 Write16(EMachine); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +0000394
395 Write32(ELF::EV_CURRENT); // e_version
396 WriteWord(0); // e_entry, no entry point in .o file
397 WriteWord(0); // e_phoff, no program header for .o
Benjamin Kramereb976772010-08-17 17:02:29 +0000398 WriteWord(SectionDataSize + (Is64Bit ? sizeof(ELF::Elf64_Ehdr) :
399 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000400
401 // FIXME: Make this configurable.
402 Write32(0); // e_flags = whatever the target wants
403
404 // e_ehsize = ELF header size
405 Write16(Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
406
407 Write16(0); // e_phentsize = prog header entry size
408 Write16(0); // e_phnum = # prog header entries = 0
409
410 // e_shentsize = Section header entry size
411 Write16(Is64Bit ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
412
413 // e_shnum = # of section header ents
Rafael Espindola7be2c332010-10-31 00:16:26 +0000414 if (NumberOfSections >= ELF::SHN_LORESERVE)
415 Write16(0);
416 else
417 Write16(NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000418
419 // e_shstrndx = Section # of '.shstrtab'
Rafael Espindola7be2c332010-10-31 00:16:26 +0000420 if (NumberOfSections >= ELF::SHN_LORESERVE)
421 Write16(ELF::SHN_XINDEX);
422 else
423 Write16(ShstrtabIndex);
Matt Fleming3565a062010-08-16 18:57:57 +0000424}
425
Rafael Espindola7be2c332010-10-31 00:16:26 +0000426void ELFObjectWriterImpl::WriteSymbolEntry(MCDataFragment *SymtabF,
427 MCDataFragment *ShndxF,
428 uint64_t name,
Matt Fleming3565a062010-08-16 18:57:57 +0000429 uint8_t info, uint64_t value,
430 uint64_t size, uint8_t other,
Rafael Espindola7be2c332010-10-31 00:16:26 +0000431 uint32_t shndx,
432 bool Reserved) {
433 if (ShndxF) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000434 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000435 String32(*ShndxF, shndx);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000436 else
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000437 String32(*ShndxF, 0);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000438 }
439
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000440 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
441 uint16_t(ELF::SHN_XINDEX) : shndx;
442
Matt Fleming3565a062010-08-16 18:57:57 +0000443 if (Is64Bit) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000444 String32(*SymtabF, name); // st_name
445 String8(*SymtabF, info); // st_info
446 String8(*SymtabF, other); // st_other
447 String16(*SymtabF, Index); // st_shndx
448 String64(*SymtabF, value); // st_value
449 String64(*SymtabF, size); // st_size
Matt Fleming3565a062010-08-16 18:57:57 +0000450 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000451 String32(*SymtabF, name); // st_name
452 String32(*SymtabF, value); // st_value
453 String32(*SymtabF, size); // st_size
454 String8(*SymtabF, info); // st_info
455 String8(*SymtabF, other); // st_other
456 String16(*SymtabF, Index); // st_shndx
Matt Fleming3565a062010-08-16 18:57:57 +0000457 }
458}
459
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000460static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout) {
461 if (Data.isCommon() && Data.isExternal())
462 return Data.getCommonAlignment();
463
464 const MCSymbol &Symbol = Data.getSymbol();
465 if (!Symbol.isInSection())
466 return 0;
467
Rafael Espindola1973d432010-10-28 19:39:57 +0000468 if (MCFragment *FF = Data.getFragment())
469 return Layout.getSymbolAddress(&Data) -
470 Layout.getSectionAddress(FF->getParent());
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000471
472 return 0;
473}
474
Rafael Espindolade89b012010-10-15 18:25:33 +0000475static const MCSymbol &AliasedSymbol(const MCSymbol &Symbol) {
476 const MCSymbol *S = &Symbol;
477 while (S->isVariable()) {
478 const MCExpr *Value = S->getVariableValue();
Rafael Espindola9302bd62010-11-11 17:24:43 +0000479 MCExpr::ExprKind Kind = Value->getKind();
480 switch (Kind) {
481 case MCExpr::SymbolRef: {
482 const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr*>(Value);
483 S = &Ref->getSymbol();
484 break;
485 }
486 case MCExpr::Target: {
487 const MCTargetExpr *TExp = static_cast<const MCTargetExpr*>(Value);
488 MCValue Res;
489 TExp->EvaluateAsRelocatableImpl(Res, NULL);
490 S = &Res.getSymA()->getSymbol();
491 break;
492 }
493 default:
Rafael Espindolaa6866962010-10-27 14:44:52 +0000494 return *S;
Rafael Espindola9302bd62010-11-11 17:24:43 +0000495 }
Rafael Espindolade89b012010-10-15 18:25:33 +0000496 }
497 return *S;
498}
499
Rafael Espindola88182132010-10-27 15:18:17 +0000500void ELFObjectWriterImpl::ExecutePostLayoutBinding(MCAssembler &Asm) {
501 // The presence of symbol versions causes undefined symbols and
502 // versions declared with @@@ to be renamed.
503
504 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
505 ie = Asm.symbol_end(); it != ie; ++it) {
506 const MCSymbol &Alias = it->getSymbol();
Rafael Espindola88182132010-10-27 15:18:17 +0000507 const MCSymbol &Symbol = AliasedSymbol(Alias);
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000508 MCSymbolData &SD = Asm.getSymbolData(Symbol);
509
510 // Undefined symbols are global, but this is the first place we
511 // are able to set it.
512 if (Symbol.isUndefined() && !Symbol.isVariable()) {
513 if (GetBinding(SD) == ELF::STB_LOCAL) {
514 SetBinding(SD, ELF::STB_GLOBAL);
515 SetBinding(*it, ELF::STB_GLOBAL);
516 }
517 }
518
519 // Not an alias.
520 if (&Symbol == &Alias)
521 continue;
522
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000523 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000524 size_t Pos = AliasName.find('@');
525 if (Pos == StringRef::npos)
526 continue;
527
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000528 // Aliases defined with .symvar copy the binding from the symbol they alias.
529 // This is the first place we are able to copy this information.
530 it->setExternal(SD.isExternal());
531 SetBinding(*it, GetBinding(SD));
532
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000533 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000534 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
535 continue;
536
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000537 // FIXME: produce a better error message.
538 if (Symbol.isUndefined() && Rest.startswith("@@") &&
539 !Rest.startswith("@@@"))
540 report_fatal_error("A @@ version cannot be undefined");
541
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000542 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000543 }
544}
545
Rafael Espindola7be2c332010-10-31 00:16:26 +0000546void ELFObjectWriterImpl::WriteSymbol(MCDataFragment *SymtabF,
547 MCDataFragment *ShndxF,
548 ELFSymbolData &MSD,
Matt Fleming3565a062010-08-16 18:57:57 +0000549 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000550 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000551 MCSymbolData &Data =
552 Layout.getAssembler().getSymbolData(AliasedSymbol(OrigData.getSymbol()));
Rafael Espindola152c1062010-10-06 21:02:29 +0000553
Rafael Espindola7be2c332010-10-31 00:16:26 +0000554 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
555 Data.getSymbol().isVariable();
556
Rafael Espindola152c1062010-10-06 21:02:29 +0000557 uint8_t Binding = GetBinding(OrigData);
558 uint8_t Visibility = GetVisibility(OrigData);
559 uint8_t Type = GetType(Data);
560
561 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
562 uint8_t Other = Visibility;
563
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000564 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000565 uint64_t Size = 0;
566 const MCExpr *ESize;
567
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000568 assert(!(Data.isCommon() && !Data.isExternal()));
569
Matt Fleming3565a062010-08-16 18:57:57 +0000570 ESize = Data.getSize();
571 if (Data.getSize()) {
572 MCValue Res;
573 if (ESize->getKind() == MCExpr::Binary) {
574 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(ESize);
575
576 if (BE->EvaluateAsRelocatable(Res, &Layout)) {
Benjamin Kramer24f12062010-10-17 07:38:40 +0000577 assert(!Res.getSymA() || !Res.getSymA()->getSymbol().isDefined());
578 assert(!Res.getSymB() || !Res.getSymB()->getSymbol().isDefined());
Rafael Espindolaf230df92010-10-16 18:23:53 +0000579 Size = Res.getConstant();
Matt Fleming3565a062010-08-16 18:57:57 +0000580 }
581 } else if (ESize->getKind() == MCExpr::Constant) {
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000582 Size = static_cast<const MCConstantExpr *>(ESize)->getValue();
Matt Fleming3565a062010-08-16 18:57:57 +0000583 } else {
584 assert(0 && "Unsupported size expression");
585 }
586 }
587
588 // Write out the symbol table entry
Rafael Espindola7be2c332010-10-31 00:16:26 +0000589 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
590 Size, Other, MSD.SectionIndex, IsReserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000591}
592
Rafael Espindola7be2c332010-10-31 00:16:26 +0000593void ELFObjectWriterImpl::WriteSymbolTable(MCDataFragment *SymtabF,
594 MCDataFragment *ShndxF,
Matt Fleming3565a062010-08-16 18:57:57 +0000595 const MCAssembler &Asm,
Rafael Espindola71859c62010-09-16 19:46:31 +0000596 const MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000597 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000598 // The string table must be emitted first because we need the index
599 // into the string table for all the symbol names.
600 assert(StringTable.size() && "Missing string table");
601
602 // FIXME: Make sure the start of the symbol table is aligned.
603
604 // The first entry is the undefined symbol entry.
Rafael Espindola7be2c332010-10-31 00:16:26 +0000605 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
Matt Fleming3565a062010-08-16 18:57:57 +0000606
607 // Write the symbol table entries.
608 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
609 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
610 ELFSymbolData &MSD = LocalSymbolData[i];
Rafael Espindola7be2c332010-10-31 00:16:26 +0000611 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000612 }
613
Rafael Espindola71859c62010-09-16 19:46:31 +0000614 // Write out a symbol table entry for each regular section.
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000615 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
616 ++i) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000617 const MCSectionELF &Section =
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000618 static_cast<const MCSectionELF&>(i->getSection());
619 if (Section.getType() == ELF::SHT_RELA ||
620 Section.getType() == ELF::SHT_REL ||
621 Section.getType() == ELF::SHT_STRTAB ||
622 Section.getType() == ELF::SHT_SYMTAB)
Eli Friedmana44fa242010-08-16 21:17:09 +0000623 continue;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000624 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000625 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section), false);
Eli Friedmana44fa242010-08-16 21:17:09 +0000626 LastLocalSymbolIndex++;
627 }
Matt Fleming3565a062010-08-16 18:57:57 +0000628
629 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
630 ELFSymbolData &MSD = ExternalSymbolData[i];
631 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000632 assert(((Data.getFlags() & ELF_STB_Global) ||
633 (Data.getFlags() & ELF_STB_Weak)) &&
634 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000635 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000636 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000637 LastLocalSymbolIndex++;
638 }
639
640 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
641 ELFSymbolData &MSD = UndefinedSymbolData[i];
642 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000643 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000644 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000645 LastLocalSymbolIndex++;
646 }
647}
648
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000649static bool ShouldRelocOnSymbol(const MCSymbolData &SD,
Rafael Espindola3729d002010-10-05 23:57:26 +0000650 const MCValue &Target,
651 const MCFragment &F) {
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000652 const MCSymbol &Symbol = SD.getSymbol();
653 if (Symbol.isUndefined())
654 return true;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000655
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000656 const MCSectionELF &Section =
657 static_cast<const MCSectionELF&>(Symbol.getSection());
658
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000659 if (SD.isExternal())
660 return true;
661
Rafael Espindola8cecf252010-10-06 16:23:36 +0000662 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000663 const MCSectionELF &Sec2 =
664 static_cast<const MCSectionELF&>(F.getParent()->getSection());
665
Rafael Espindolace2d3c52010-10-18 20:25:33 +0000666 if (Section.getKind().isBSS())
667 return false;
668
Rafael Espindola8cecf252010-10-06 16:23:36 +0000669 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000670 (Kind == MCSymbolRefExpr::VK_PLT ||
671 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
672 Kind == MCSymbolRefExpr::VK_GOTOFF))
Rafael Espindola3729d002010-10-05 23:57:26 +0000673 return true;
674
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000675 if (Section.getFlags() & MCSectionELF::SHF_MERGE)
676 return Target.getConstant() != 0;
677
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000678 return false;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000679}
680
Benjamin Kramere5b57342010-08-17 18:20:28 +0000681// FIXME: this is currently X86/X86_64 only
Matt Fleming3565a062010-08-16 18:57:57 +0000682void ELFObjectWriterImpl::RecordRelocation(const MCAssembler &Asm,
683 const MCAsmLayout &Layout,
684 const MCFragment *Fragment,
685 const MCFixup &Fixup,
686 MCValue Target,
687 uint64_t &FixedValue) {
Matt Fleming3565a062010-08-16 18:57:57 +0000688 int64_t Addend = 0;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000689 int Index = 0;
Benjamin Kramer95c602a2010-08-27 10:38:39 +0000690 int64_t Value = Target.getConstant();
Rafael Espindola03f1b742010-11-11 16:48:11 +0000691 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
692 const MCSymbol &ASymbol = AliasedSymbol(Symbol);
693 const MCSymbol *RenamedP = Renames.lookup(&Symbol);
694 if (!RenamedP)
695 RenamedP = &ASymbol;
696 const MCSymbol &Renamed = *RenamedP;
Matt Fleming3565a062010-08-16 18:57:57 +0000697
Rafael Espindola00074892010-09-17 22:34:41 +0000698 bool IsPCRel = isFixupKindX86PCRel(Fixup.getKind());
Benjamin Kramer81cfb852010-08-17 19:45:05 +0000699 if (!Target.isAbsolute()) {
Rafael Espindola03f1b742010-11-11 16:48:11 +0000700 MCSymbolData &SD = Asm.getSymbolData(Symbol);
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000701 MCFragment *F = SD.getFragment();
Matt Fleming3565a062010-08-16 18:57:57 +0000702
Rafael Espindola9d8b7552010-10-03 00:46:57 +0000703 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
704 const MCSymbol &SymbolB = RefB->getSymbol();
705 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
706 IsPCRel = true;
Rafael Espindola55fb1022010-10-04 15:59:01 +0000707 MCSectionData *Sec = Fragment->getParent();
708
709 // Offset of the symbol in the section
710 int64_t a = Layout.getSymbolAddress(&SDB) - Layout.getSectionAddress(Sec);
711
712 // Ofeset of the relocation in the section
713 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
714 Value += b - a;
Rafael Espindola9d8b7552010-10-03 00:46:57 +0000715 }
716
Rafael Espindola3729d002010-10-05 23:57:26 +0000717 bool RelocOnSymbol = ShouldRelocOnSymbol(SD, Target, *Fragment);
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000718 if (!RelocOnSymbol) {
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000719 Index = F->getParent()->getOrdinal();
Rafael Espindolaa6489182010-09-24 21:19:03 +0000720
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000721 MCSectionData *FSD = F->getParent();
722 // Offset of the symbol in the section
723 Value += Layout.getSymbolAddress(&SD) - Layout.getSectionAddress(FSD);
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000724 } else {
Rafael Espindola03f1b742010-11-11 16:48:11 +0000725 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
726 WeakrefUsedInReloc.insert(&Renamed);
727 else
728 UsedInReloc.insert(&Renamed);
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000729 Index = -1;
730 }
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000731 Addend = Value;
732 // Compensate for the addend on i386.
733 if (Is64Bit)
734 Value = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000735 }
736
Benjamin Kramer95c602a2010-08-27 10:38:39 +0000737 FixedValue = Value;
738
Matt Fleming3565a062010-08-16 18:57:57 +0000739 // determine the type of the relocation
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000740
741 MCSymbolRefExpr::VariantKind Modifier = Target.getSymA()->getKind();
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000742 unsigned Type;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000743 if (Is64Bit) {
744 if (IsPCRel) {
Rafael Espindola92bf6682010-10-04 19:04:13 +0000745 switch (Modifier) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000746 default:
747 llvm_unreachable("Unimplemented");
Rafael Espindola92bf6682010-10-04 19:04:13 +0000748 case MCSymbolRefExpr::VK_None:
749 Type = ELF::R_X86_64_PC32;
750 break;
751 case MCSymbolRefExpr::VK_PLT:
752 Type = ELF::R_X86_64_PLT32;
753 break;
Rafael Espindolaa0a2f872010-10-28 14:22:44 +0000754 case MCSymbolRefExpr::VK_GOTPCREL:
Rafael Espindola607d1f62010-10-04 19:51:39 +0000755 Type = ELF::R_X86_64_GOTPCREL;
756 break;
Rafael Espindolabc82d8b2010-10-27 20:28:07 +0000757 case MCSymbolRefExpr::VK_GOTTPOFF:
758 Type = ELF::R_X86_64_GOTTPOFF;
759 break;
760 case MCSymbolRefExpr::VK_TLSGD:
761 Type = ELF::R_X86_64_TLSGD;
762 break;
Rafael Espindolab4d17212010-10-28 15:02:40 +0000763 case MCSymbolRefExpr::VK_TLSLD:
764 Type = ELF::R_X86_64_TLSLD;
765 break;
Rafael Espindola92bf6682010-10-04 19:04:13 +0000766 }
Benjamin Kramere5b57342010-08-17 18:20:28 +0000767 } else {
768 switch ((unsigned)Fixup.getKind()) {
769 default: llvm_unreachable("invalid fixup kind!");
770 case FK_Data_8: Type = ELF::R_X86_64_64; break;
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000771 case X86::reloc_signed_4byte:
Benjamin Kramere5b57342010-08-17 18:20:28 +0000772 case X86::reloc_pcrel_4byte:
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000773 assert(isInt<32>(Target.getConstant()));
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000774 switch (Modifier) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000775 default:
776 llvm_unreachable("Unimplemented");
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000777 case MCSymbolRefExpr::VK_None:
778 Type = ELF::R_X86_64_32S;
779 break;
780 case MCSymbolRefExpr::VK_GOT:
781 Type = ELF::R_X86_64_GOT32;
782 break;
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000783 case MCSymbolRefExpr::VK_GOTPCREL:
Rafael Espindola8cecf252010-10-06 16:23:36 +0000784 Type = ELF::R_X86_64_GOTPCREL;
785 break;
Rafael Espindolabc82d8b2010-10-27 20:28:07 +0000786 case MCSymbolRefExpr::VK_TPOFF:
787 Type = ELF::R_X86_64_TPOFF32;
788 break;
Rafael Espindolaaa8f1f02010-10-28 15:11:03 +0000789 case MCSymbolRefExpr::VK_DTPOFF:
790 Type = ELF::R_X86_64_DTPOFF32;
791 break;
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000792 }
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000793 break;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000794 case FK_Data_4:
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000795 Type = ELF::R_X86_64_32;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000796 break;
797 case FK_Data_2: Type = ELF::R_X86_64_16; break;
798 case X86::reloc_pcrel_1byte:
799 case FK_Data_1: Type = ELF::R_X86_64_8; break;
800 }
801 }
Matt Fleming3565a062010-08-16 18:57:57 +0000802 } else {
Benjamin Kramere5b57342010-08-17 18:20:28 +0000803 if (IsPCRel) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000804 switch (Modifier) {
805 default:
Rafael Espindola24dc9ec2010-10-18 19:33:01 +0000806 llvm_unreachable("Unimplemented");
807 case MCSymbolRefExpr::VK_None:
Rafael Espindola9baee3b2010-10-18 18:03:28 +0000808 Type = ELF::R_386_PC32;
Rafael Espindola9baee3b2010-10-18 18:03:28 +0000809 break;
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000810 case MCSymbolRefExpr::VK_PLT:
811 Type = ELF::R_386_PLT32;
812 break;
813 }
Benjamin Kramere5b57342010-08-17 18:20:28 +0000814 } else {
815 switch ((unsigned)Fixup.getKind()) {
816 default: llvm_unreachable("invalid fixup kind!");
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000817
Rafael Espindola24ba4f72010-10-24 17:35:42 +0000818 case X86::reloc_global_offset_table:
819 Type = ELF::R_386_GOTPC;
820 break;
821
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000822 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
823 // instead?
824 case X86::reloc_signed_4byte:
Benjamin Kramere5b57342010-08-17 18:20:28 +0000825 case X86::reloc_pcrel_4byte:
Rafael Espindolabd701182010-10-19 19:31:37 +0000826 case FK_Data_4:
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000827 switch (Modifier) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000828 default:
829 llvm_unreachable("Unimplemented");
Rafael Espindolabd701182010-10-19 19:31:37 +0000830 case MCSymbolRefExpr::VK_None:
Rafael Espindola24ba4f72010-10-24 17:35:42 +0000831 Type = ELF::R_386_32;
Rafael Espindolabd701182010-10-19 19:31:37 +0000832 break;
Rafael Espindolaeada3042010-10-18 20:47:21 +0000833 case MCSymbolRefExpr::VK_GOT:
834 Type = ELF::R_386_GOT32;
835 break;
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000836 case MCSymbolRefExpr::VK_GOTOFF:
837 Type = ELF::R_386_GOTOFF;
838 break;
Rafael Espindola3cede2d2010-10-27 21:23:52 +0000839 case MCSymbolRefExpr::VK_TLSGD:
840 Type = ELF::R_386_TLS_GD;
841 break;
842 case MCSymbolRefExpr::VK_TPOFF:
843 Type = ELF::R_386_TLS_LE_32;
844 break;
845 case MCSymbolRefExpr::VK_INDNTPOFF:
846 Type = ELF::R_386_TLS_IE;
847 break;
848 case MCSymbolRefExpr::VK_NTPOFF:
849 Type = ELF::R_386_TLS_LE;
850 break;
Rafael Espindolaa0a2f872010-10-28 14:22:44 +0000851 case MCSymbolRefExpr::VK_GOTNTPOFF:
852 Type = ELF::R_386_TLS_GOTIE;
853 break;
Rafael Espindolaa264f722010-10-28 14:37:09 +0000854 case MCSymbolRefExpr::VK_TLSLDM:
855 Type = ELF::R_386_TLS_LDM;
856 break;
Rafael Espindola0cf15d62010-10-28 14:48:59 +0000857 case MCSymbolRefExpr::VK_DTPOFF:
858 Type = ELF::R_386_TLS_LDO_32;
859 break;
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000860 }
861 break;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000862 case FK_Data_2: Type = ELF::R_386_16; break;
863 case X86::reloc_pcrel_1byte:
864 case FK_Data_1: Type = ELF::R_386_8; break;
865 }
Matt Fleming3565a062010-08-16 18:57:57 +0000866 }
867 }
868
Rafael Espindolaa0a2f872010-10-28 14:22:44 +0000869 if (RelocNeedsGOT(Modifier))
Rafael Espindola5c77c162010-10-05 15:48:37 +0000870 NeedsGOT = true;
871
Benjamin Kramere5b57342010-08-17 18:20:28 +0000872 ELFRelocationEntry ERE;
873
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000874 ERE.Index = Index;
875 ERE.Type = Type;
Rafael Espindola03f1b742010-11-11 16:48:11 +0000876 ERE.Symbol = &Renamed;
Matt Fleming3565a062010-08-16 18:57:57 +0000877
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000878 ERE.r_offset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
Benjamin Kramere5b57342010-08-17 18:20:28 +0000879
Matt Fleming3565a062010-08-16 18:57:57 +0000880 if (HasRelocationAddend)
881 ERE.r_addend = Addend;
Benjamin Kramer172d7d62010-08-17 00:33:24 +0000882 else
883 ERE.r_addend = 0; // Silence compiler warning.
Matt Fleming3565a062010-08-16 18:57:57 +0000884
885 Relocations[Fragment->getParent()].push_back(ERE);
886}
887
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000888uint64_t
889ELFObjectWriterImpl::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
890 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000891 MCSymbolData &SD = Asm.getSymbolData(*S);
Eli Friedmanf8020a32010-08-16 19:15:06 +0000892
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000893 // Local symbol.
894 if (!SD.isExternal() && !S->isUndefined())
895 return SD.getIndex() + /* empty symbol */ 1;
896
897 // External or undefined symbol.
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000898 return SD.getIndex() + NumRegularSections + /* empty symbol */ 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000899}
900
Rafael Espindola737cd212010-10-05 18:01:23 +0000901static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
Rafael Espindola88182132010-10-27 15:18:17 +0000902 bool Used, bool Renamed) {
Rafael Espindola484291c2010-11-01 14:28:48 +0000903 if (Data.getFlags() & ELF_Other_Weakref)
904 return false;
905
Rafael Espindolabd701182010-10-19 19:31:37 +0000906 if (Used)
907 return true;
908
Rafael Espindola88182132010-10-27 15:18:17 +0000909 if (Renamed)
910 return false;
911
Rafael Espindola737cd212010-10-05 18:01:23 +0000912 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000913
Rafael Espindolad1798862010-10-29 23:09:31 +0000914 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
915 return true;
916
Rafael Espindolaa6866962010-10-27 14:44:52 +0000917 const MCSymbol &A = AliasedSymbol(Symbol);
Rafael Espindolad1798862010-10-29 23:09:31 +0000918 if (!A.isVariable() && A.isUndefined() && !Data.isCommon())
Rafael Espindolaa6866962010-10-27 14:44:52 +0000919 return false;
920
Rafael Espindola737cd212010-10-05 18:01:23 +0000921 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
922 return false;
923
Rafael Espindolabd701182010-10-19 19:31:37 +0000924 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000925 return false;
926
927 return true;
928}
929
930static bool isLocal(const MCSymbolData &Data) {
931 if (Data.isExternal())
932 return false;
933
934 const MCSymbol &Symbol = Data.getSymbol();
935 if (Symbol.isUndefined() && !Symbol.isVariable())
936 return false;
937
938 return true;
939}
940
Rafael Espindolabab2a802010-11-10 21:51:05 +0000941void ELFObjectWriterImpl::ComputeIndexMap(MCAssembler &Asm,
942 SectionIndexMapTy &SectionIndexMap) {
943 unsigned Index = 1;
944 for (MCAssembler::iterator it = Asm.begin(),
945 ie = Asm.end(); it != ie; ++it) {
946 const MCSectionELF &Section =
947 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000948 if (Section.getType() != ELF::SHT_GROUP)
949 continue;
950 SectionIndexMap[&Section] = Index++;
951 }
952
953 for (MCAssembler::iterator it = Asm.begin(),
954 ie = Asm.end(); it != ie; ++it) {
955 const MCSectionELF &Section =
956 static_cast<const MCSectionELF &>(it->getSection());
957 if (Section.getType() == ELF::SHT_GROUP)
958 continue;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000959 SectionIndexMap[&Section] = Index++;
960 }
961}
962
963void ELFObjectWriterImpl::ComputeSymbolTable(MCAssembler &Asm,
964 const SectionIndexMapTy &SectionIndexMap) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000965 // FIXME: Is this the correct place to do this?
966 if (NeedsGOT) {
967 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
968 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
969 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
970 Data.setExternal(true);
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000971 SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindola5c77c162010-10-05 15:48:37 +0000972 }
973
Matt Fleming3565a062010-08-16 18:57:57 +0000974 // Build section lookup table.
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000975 NumRegularSections = Asm.size();
Matt Fleming3565a062010-08-16 18:57:57 +0000976
977 // Index 0 is always the empty string.
978 StringMap<uint64_t> StringIndexMap;
979 StringTable += '\x00';
980
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000981 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000982 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
983 ie = Asm.symbol_end(); it != ie; ++it) {
984 const MCSymbol &Symbol = it->getSymbol();
985
Rafael Espindola484291c2010-11-01 14:28:48 +0000986 bool Used = UsedInReloc.count(&Symbol);
987 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
988 if (!isInSymtab(Asm, *it, Used || WeakrefUsed,
Rafael Espindola88182132010-10-27 15:18:17 +0000989 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000990 continue;
991
Matt Fleming3565a062010-08-16 18:57:57 +0000992 ELFSymbolData MSD;
993 MSD.SymbolData = it;
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000994 bool Local = isLocal(*it);
Rafael Espindola88182132010-10-27 15:18:17 +0000995 const MCSymbol &RefSymbol = AliasedSymbol(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000996
Rafael Espindola484291c2010-11-01 14:28:48 +0000997 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
998 SetBinding(*it, ELF::STB_WEAK);
999
Rafael Espindolaf7c10a32010-09-21 00:24:38 +00001000 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +00001001 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +00001002 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +00001003 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +00001004 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +00001005 } else if (RefSymbol.isUndefined()) {
Matt Fleming3565a062010-08-16 18:57:57 +00001006 MSD.SectionIndex = ELF::SHN_UNDEF;
Matt Fleming3565a062010-08-16 18:57:57 +00001007 } else {
Rafael Espindolabab2a802010-11-10 21:51:05 +00001008 const MCSectionELF &Section =
1009 static_cast<const MCSectionELF&>(RefSymbol.getSection());
1010 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001011 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
1012 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +00001013 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +00001014 }
1015
Rafael Espindola88182132010-10-27 15:18:17 +00001016 // The @@@ in symbol version is replaced with @ in undefined symbols and
1017 // @@ in defined ones.
1018 StringRef Name = Symbol.getName();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +00001019 SmallString<32> Buf;
1020
Rafael Espindola88182132010-10-27 15:18:17 +00001021 size_t Pos = Name.find("@@@");
Rafael Espindola88182132010-10-27 15:18:17 +00001022 if (Pos != StringRef::npos) {
Benjamin Kramer1261a2f2010-11-12 19:26:04 +00001023 Buf += Name.substr(0, Pos);
1024 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
1025 Buf += Name.substr(Pos + Skip);
1026 Name = Buf;
Rafael Espindola88182132010-10-27 15:18:17 +00001027 }
1028
Benjamin Kramer1261a2f2010-11-12 19:26:04 +00001029 uint64_t &Entry = StringIndexMap[Name];
Rafael Espindolaa6866962010-10-27 14:44:52 +00001030 if (!Entry) {
1031 Entry = StringTable.size();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +00001032 StringTable += Name;
Rafael Espindolaa6866962010-10-27 14:44:52 +00001033 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +00001034 }
Rafael Espindolaa6866962010-10-27 14:44:52 +00001035 MSD.StringIndex = Entry;
1036 if (MSD.SectionIndex == ELF::SHN_UNDEF)
1037 UndefinedSymbolData.push_back(MSD);
1038 else if (Local)
1039 LocalSymbolData.push_back(MSD);
1040 else
1041 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +00001042 }
1043
1044 // Symbols are required to be in lexicographic order.
1045 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
1046 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
1047 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
1048
1049 // Set the symbol indices. Local symbols must come before all other
1050 // symbols with non-local bindings.
Rafael Espindolabab2a802010-11-10 21:51:05 +00001051 unsigned Index = 0;
Matt Fleming3565a062010-08-16 18:57:57 +00001052 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
1053 LocalSymbolData[i].SymbolData->setIndex(Index++);
1054 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
1055 ExternalSymbolData[i].SymbolData->setIndex(Index++);
1056 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
1057 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
1058}
1059
1060void ELFObjectWriterImpl::WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
1061 const MCSectionData &SD) {
1062 if (!Relocations[&SD].empty()) {
1063 MCContext &Ctx = Asm.getContext();
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001064 const MCSectionELF *RelaSection;
Matt Fleming3565a062010-08-16 18:57:57 +00001065 const MCSectionELF &Section =
1066 static_cast<const MCSectionELF&>(SD.getSection());
1067
1068 const StringRef SectionName = Section.getSectionName();
Benjamin Kramer377a5722010-08-17 17:30:07 +00001069 std::string RelaSectionName = HasRelocationAddend ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +00001070 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +00001071
1072 unsigned EntrySize;
1073 if (HasRelocationAddend)
1074 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
1075 else
1076 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +00001077
Benjamin Kramer377a5722010-08-17 17:30:07 +00001078 RelaSection = Ctx.getELFSection(RelaSectionName, HasRelocationAddend ?
1079 ELF::SHT_RELA : ELF::SHT_REL, 0,
Matt Fleming3565a062010-08-16 18:57:57 +00001080 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001081 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001082
1083 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001084 RelaSD.setAlignment(Is64Bit ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +00001085
1086 MCDataFragment *F = new MCDataFragment(&RelaSD);
1087
1088 WriteRelocationsFragment(Asm, F, &SD);
1089
Rafael Espindola70703872010-09-30 02:22:20 +00001090 Asm.AddSectionToTheEnd(*Writer, RelaSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +00001091 }
1092}
1093
1094void ELFObjectWriterImpl::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
1095 uint64_t Flags, uint64_t Address,
1096 uint64_t Offset, uint64_t Size,
1097 uint32_t Link, uint32_t Info,
1098 uint64_t Alignment,
1099 uint64_t EntrySize) {
1100 Write32(Name); // sh_name: index into string table
1101 Write32(Type); // sh_type
1102 WriteWord(Flags); // sh_flags
1103 WriteWord(Address); // sh_addr
1104 WriteWord(Offset); // sh_offset
1105 WriteWord(Size); // sh_size
1106 Write32(Link); // sh_link
1107 Write32(Info); // sh_info
1108 WriteWord(Alignment); // sh_addralign
1109 WriteWord(EntrySize); // sh_entsize
1110}
1111
1112void ELFObjectWriterImpl::WriteRelocationsFragment(const MCAssembler &Asm,
1113 MCDataFragment *F,
1114 const MCSectionData *SD) {
1115 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
1116 // sort by the r_offset just like gnu as does
1117 array_pod_sort(Relocs.begin(), Relocs.end());
1118
1119 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1120 ELFRelocationEntry entry = Relocs[e - i - 1];
1121
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001122 if (entry.Index < 0)
1123 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
1124 else
1125 entry.Index += LocalSymbolData.size() + 1;
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001126 if (Is64Bit) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001127 String64(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001128
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001129 struct ELF::Elf64_Rela ERE64;
1130 ERE64.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001131 String64(*F, ERE64.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001132
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001133 if (HasRelocationAddend)
1134 String64(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001135 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001136 String32(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001137
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001138 struct ELF::Elf32_Rela ERE32;
1139 ERE32.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001140 String32(*F, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001141
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001142 if (HasRelocationAddend)
1143 String32(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001144 }
Matt Fleming3565a062010-08-16 18:57:57 +00001145 }
1146}
1147
1148void ELFObjectWriterImpl::CreateMetadataSections(MCAssembler &Asm,
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001149 MCAsmLayout &Layout,
1150 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +00001151 MCContext &Ctx = Asm.getContext();
1152 MCDataFragment *F;
1153
Matt Fleming3565a062010-08-16 18:57:57 +00001154 unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
1155
Rafael Espindola38738bf2010-09-22 19:04:41 +00001156 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001157 const MCSectionELF *ShstrtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001158 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001159 SectionKind::getReadOnly());
Rafael Espindola71859c62010-09-16 19:46:31 +00001160 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1161 ShstrtabSD.setAlignment(1);
1162 ShstrtabIndex = Asm.size();
1163
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001164 const MCSectionELF *SymtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001165 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1166 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +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) {
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001175 const MCSectionELF *SymtabShndxSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001176 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
Rafael Espindola2ff9e832010-11-11 18:13:52 +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,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001184 SectionKind::getReadOnly());
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 }
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001198 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
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
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001214 StringMap<uint64_t> SecStringMap;
Matt Fleming3565a062010-08-16 18:57:57 +00001215 for (MCAssembler::const_iterator it = Asm.begin(),
1216 ie = Asm.end(); it != ie; ++it) {
Matt Fleming3565a062010-08-16 18:57:57 +00001217 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +00001218 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola51efe7a2010-09-23 14:14:56 +00001219 // FIXME: We could merge suffixes like in .text and .rela.text.
Matt Fleming3565a062010-08-16 18:57:57 +00001220
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001221 StringRef Name = Section.getSectionName();
1222 if (SecStringMap.count(Name)) {
1223 SectionStringTableIndex[&Section] = SecStringMap[Name];
1224 continue;
1225 }
Matt Fleming3565a062010-08-16 18:57:57 +00001226 // Remember the index into the string table so we can write it
1227 // into the sh_name field of the section header table.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001228 SectionStringTableIndex[&Section] = Index;
1229 SecStringMap[Name] = Index;
Matt Fleming3565a062010-08-16 18:57:57 +00001230
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001231 Index += Name.size() + 1;
1232 F->getContents() += Name;
Matt Fleming3565a062010-08-16 18:57:57 +00001233 F->getContents() += '\x00';
1234 }
1235
Rafael Espindola70703872010-09-30 02:22:20 +00001236 Asm.AddSectionToTheEnd(*Writer, ShstrtabSD, Layout);
1237}
1238
1239bool ELFObjectWriterImpl::IsFixupFullyResolved(const MCAssembler &Asm,
1240 const MCValue Target,
1241 bool IsPCRel,
1242 const MCFragment *DF) const {
1243 // If this is a PCrel relocation, find the section this fixup value is
1244 // relative to.
1245 const MCSection *BaseSection = 0;
1246 if (IsPCRel) {
1247 BaseSection = &DF->getParent()->getSection();
1248 assert(BaseSection);
1249 }
1250
1251 const MCSection *SectionA = 0;
1252 const MCSymbol *SymbolA = 0;
1253 if (const MCSymbolRefExpr *A = Target.getSymA()) {
1254 SymbolA = &A->getSymbol();
1255 SectionA = &SymbolA->getSection();
1256 }
1257
1258 const MCSection *SectionB = 0;
1259 if (const MCSymbolRefExpr *B = Target.getSymB()) {
1260 SectionB = &B->getSymbol().getSection();
1261 }
1262
1263 if (!BaseSection)
1264 return SectionA == SectionB;
1265
1266 const MCSymbolData &DataA = Asm.getSymbolData(*SymbolA);
1267 if (DataA.isExternal())
1268 return false;
1269
1270 return !SectionB && BaseSection == SectionA;
Matt Fleming3565a062010-08-16 18:57:57 +00001271}
1272
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001273void ELFObjectWriterImpl::CreateGroupSections(MCAssembler &Asm,
1274 MCAsmLayout &Layout,
1275 GroupMapTy &GroupMap) {
1276 typedef DenseMap<const MCSymbol*, const MCSectionELF*> RevGroupMapTy;
1277 // Build the groups
1278 RevGroupMapTy Groups;
1279 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1280 it != ie; ++it) {
1281 const MCSectionELF &Section =
1282 static_cast<const MCSectionELF&>(it->getSection());
1283 if (!(Section.getFlags() & MCSectionELF::SHF_GROUP))
1284 continue;
1285
1286 const MCSymbol *SignatureSymbol = Section.getGroup();
1287 Asm.getOrCreateSymbolData(*SignatureSymbol);
1288 const MCSectionELF *&Group = Groups[SignatureSymbol];
1289 if (!Group) {
1290 Group = Asm.getContext().CreateELFGroupSection();
1291 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1292 Data.setAlignment(4);
1293 MCDataFragment *F = new MCDataFragment(&Data);
1294 String32(*F, ELF::GRP_COMDAT);
1295 }
1296 GroupMap[Group] = SignatureSymbol;
1297 }
1298
1299 // Add sections to the groups
1300 unsigned Index = 1;
1301 unsigned NumGroups = Groups.size();
1302 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1303 it != ie; ++it, ++Index) {
1304 const MCSectionELF &Section =
1305 static_cast<const MCSectionELF&>(it->getSection());
1306 if (!(Section.getFlags() & MCSectionELF::SHF_GROUP))
1307 continue;
1308 const MCSectionELF *Group = Groups[Section.getGroup()];
1309 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1310 // FIXME: we could use the previous fragment
1311 MCDataFragment *F = new MCDataFragment(&Data);
1312 String32(*F, NumGroups + Index);
1313 }
1314
1315 for (RevGroupMapTy::const_iterator i = Groups.begin(), e = Groups.end();
1316 i != e; ++i) {
1317 const MCSectionELF *Group = i->second;
1318 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1319 Asm.AddSectionToTheEnd(*Writer, Data, Layout);
1320 }
1321}
1322
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001323void ELFObjectWriterImpl::WriteSection(MCAssembler &Asm,
1324 const SectionIndexMapTy &SectionIndexMap,
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001325 uint32_t GroupSymbolIndex,
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001326 uint64_t Offset, uint64_t Size,
1327 uint64_t Alignment,
1328 const MCSectionELF &Section) {
1329 uint64_t sh_link = 0;
1330 uint64_t sh_info = 0;
1331
1332 switch(Section.getType()) {
1333 case ELF::SHT_DYNAMIC:
1334 sh_link = SectionStringTableIndex[&Section];
1335 sh_info = 0;
1336 break;
1337
1338 case ELF::SHT_REL:
1339 case ELF::SHT_RELA: {
1340 const MCSectionELF *SymtabSection;
1341 const MCSectionELF *InfoSection;
1342 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
1343 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001344 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001345 sh_link = SectionIndexMap.lookup(SymtabSection);
1346 assert(sh_link && ".symtab not found");
1347
1348 // Remove ".rel" and ".rela" prefixes.
1349 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1350 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
1351
1352 InfoSection = Asm.getContext().getELFSection(SectionName,
1353 ELF::SHT_PROGBITS, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001354 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001355 sh_info = SectionIndexMap.lookup(InfoSection);
1356 break;
1357 }
1358
1359 case ELF::SHT_SYMTAB:
1360 case ELF::SHT_DYNSYM:
1361 sh_link = StringTableIndex;
1362 sh_info = LastLocalSymbolIndex;
1363 break;
1364
1365 case ELF::SHT_SYMTAB_SHNDX:
1366 sh_link = SymbolTableIndex;
1367 break;
1368
1369 case ELF::SHT_PROGBITS:
1370 case ELF::SHT_STRTAB:
1371 case ELF::SHT_NOBITS:
1372 case ELF::SHT_NULL:
1373 case ELF::SHT_ARM_ATTRIBUTES:
1374 // Nothing to do.
1375 break;
1376
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001377 case ELF::SHT_GROUP: {
1378 sh_link = SymbolTableIndex;
1379 sh_info = GroupSymbolIndex;
1380 break;
1381 }
1382
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001383 default:
1384 assert(0 && "FIXME: sh_type value not supported!");
1385 break;
1386 }
1387
1388 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1389 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1390 Alignment, Section.getEntrySize());
1391}
1392
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001393void ELFObjectWriterImpl::WriteObject(MCAssembler &Asm,
Matt Fleming3565a062010-08-16 18:57:57 +00001394 const MCAsmLayout &Layout) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001395
1396 GroupMapTy GroupMap;
1397 CreateGroupSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap);
1398
Rafael Espindolabab2a802010-11-10 21:51:05 +00001399 SectionIndexMapTy SectionIndexMap;
1400
1401 ComputeIndexMap(Asm, SectionIndexMap);
1402
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001403 // Compute symbol table information.
Rafael Espindolabab2a802010-11-10 21:51:05 +00001404 ComputeSymbolTable(Asm, SectionIndexMap);
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001405
Matt Fleming3565a062010-08-16 18:57:57 +00001406 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001407 const_cast<MCAsmLayout&>(Layout),
1408 SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +00001409
Rafael Espindola1d739a02010-11-10 22:34:07 +00001410 // Update to include the metadata sections.
1411 ComputeIndexMap(Asm, SectionIndexMap);
1412
Matt Fleming3565a062010-08-16 18:57:57 +00001413 // Add 1 for the null section.
1414 unsigned NumSections = Asm.size() + 1;
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001415 uint64_t NaturalAlignment = Is64Bit ? 8 : 4;
1416 uint64_t HeaderSize = Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr);
1417 uint64_t FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001418
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001419 std::vector<const MCSectionELF*> Sections;
1420 Sections.resize(NumSections);
1421
1422 for (SectionIndexMapTy::const_iterator i=
1423 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1424 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
1425 Sections[p.second] = p.first;
1426 }
1427
1428 for (unsigned i = 1; i < NumSections; ++i) {
1429 const MCSectionELF &Section = *Sections[i];
1430 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001431
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001432 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1433
Matt Fleming3565a062010-08-16 18:57:57 +00001434 // Get the size of the section in the output file (including padding).
1435 uint64_t Size = Layout.getSectionFileSize(&SD);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001436
1437 FileOff += Size;
Matt Fleming3565a062010-08-16 18:57:57 +00001438 }
1439
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001440 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1441
Matt Fleming3565a062010-08-16 18:57:57 +00001442 // Write out the ELF header ...
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001443 WriteHeader(FileOff - HeaderSize, NumSections);
1444
1445 FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001446
1447 // ... then all of the sections ...
1448 DenseMap<const MCSection*, uint64_t> SectionOffsetMap;
1449
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001450 for (unsigned i = 1; i < NumSections; ++i) {
1451 const MCSectionELF &Section = *Sections[i];
1452 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001453
1454 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1455 WriteZeros(Padding);
1456 FileOff += Padding;
1457
Matt Fleming3565a062010-08-16 18:57:57 +00001458 // Remember the offset into the file for this section.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001459 SectionOffsetMap[&Section] = FileOff;
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001460
Matt Fleming3565a062010-08-16 18:57:57 +00001461 FileOff += Layout.getSectionFileSize(&SD);
1462
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001463 Asm.WriteSectionData(&SD, Layout, Writer);
Matt Fleming3565a062010-08-16 18:57:57 +00001464 }
1465
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001466 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1467 WriteZeros(Padding);
1468 FileOff += Padding;
1469
Matt Fleming3565a062010-08-16 18:57:57 +00001470 // ... and then the section header table.
1471 // Should we align the section header table?
1472 //
1473 // Null section first.
Rafael Espindola7be2c332010-10-31 00:16:26 +00001474 uint64_t FirstSectionSize =
1475 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1476 uint32_t FirstSectionLink =
1477 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1478 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming3565a062010-08-16 18:57:57 +00001479
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001480 for (unsigned i = 1; i < NumSections; ++i) {
1481 const MCSectionELF &Section = *Sections[i];
1482 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1483 uint32_t GroupSymbolIndex;
1484 if (Section.getType() != ELF::SHT_GROUP)
1485 GroupSymbolIndex = 0;
1486 else
1487 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm, GroupMap[&Section]);
Matt Fleming3565a062010-08-16 18:57:57 +00001488
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001489 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
1490 SectionOffsetMap[&Section], Layout.getSectionSize(&SD),
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001491 SD.getAlignment(), Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001492 }
1493}
1494
1495ELFObjectWriter::ELFObjectWriter(raw_ostream &OS,
1496 bool Is64Bit,
Roman Divacky5baf79e2010-09-09 17:57:50 +00001497 Triple::OSType OSType,
Wesley Peckeecb8582010-10-22 15:52:49 +00001498 uint16_t EMachine,
Matt Fleming3565a062010-08-16 18:57:57 +00001499 bool IsLittleEndian,
1500 bool HasRelocationAddend)
1501 : MCObjectWriter(OS, IsLittleEndian)
1502{
Wesley Peckeecb8582010-10-22 15:52:49 +00001503 Impl = new ELFObjectWriterImpl(this, Is64Bit, EMachine,
1504 HasRelocationAddend, OSType);
Matt Fleming3565a062010-08-16 18:57:57 +00001505}
1506
1507ELFObjectWriter::~ELFObjectWriter() {
1508 delete (ELFObjectWriterImpl*) Impl;
1509}
1510
1511void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm) {
1512 ((ELFObjectWriterImpl*) Impl)->ExecutePostLayoutBinding(Asm);
1513}
1514
1515void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
1516 const MCAsmLayout &Layout,
1517 const MCFragment *Fragment,
1518 const MCFixup &Fixup, MCValue Target,
1519 uint64_t &FixedValue) {
1520 ((ELFObjectWriterImpl*) Impl)->RecordRelocation(Asm, Layout, Fragment, Fixup,
1521 Target, FixedValue);
1522}
1523
Rafael Espindola70703872010-09-30 02:22:20 +00001524bool ELFObjectWriter::IsFixupFullyResolved(const MCAssembler &Asm,
1525 const MCValue Target,
1526 bool IsPCRel,
1527 const MCFragment *DF) const {
1528 return ((ELFObjectWriterImpl*) Impl)->IsFixupFullyResolved(Asm, Target,
1529 IsPCRel, DF);
1530}
1531
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001532void ELFObjectWriter::WriteObject(MCAssembler &Asm,
Matt Fleming3565a062010-08-16 18:57:57 +00001533 const MCAsmLayout &Layout) {
1534 ((ELFObjectWriterImpl*) Impl)->WriteObject(Asm, Layout);
1535}