blob: 085b301b6ec0781e67d26177176dd480bc1962e1 [file] [log] [blame]
Matt Fleming3565a062010-08-16 18:57:57 +00001//===- lib/MC/ELFObjectWriter.cpp - ELF File Writer -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements ELF object file writer information.
11//
12//===----------------------------------------------------------------------===//
13
Rafael Espindola8f413fa2010-10-05 15:11:03 +000014#include "llvm/ADT/SmallPtrSet.h"
Matt Fleming3565a062010-08-16 18:57:57 +000015#include "llvm/ADT/STLExtras.h"
16#include "llvm/ADT/StringMap.h"
17#include "llvm/ADT/Twine.h"
18#include "llvm/MC/MCAssembler.h"
19#include "llvm/MC/MCAsmLayout.h"
20#include "llvm/MC/MCContext.h"
21#include "llvm/MC/MCELFSymbolFlags.h"
22#include "llvm/MC/MCExpr.h"
23#include "llvm/MC/MCObjectWriter.h"
24#include "llvm/MC/MCSectionELF.h"
25#include "llvm/MC/MCSymbol.h"
26#include "llvm/MC/MCValue.h"
27#include "llvm/Support/Debug.h"
28#include "llvm/Support/ErrorHandling.h"
29#include "llvm/Support/ELF.h"
30#include "llvm/Target/TargetAsmBackend.h"
31
32#include "../Target/X86/X86FixupKinds.h"
33
34#include <vector>
35using namespace llvm;
36
Rafael Espindolaad49cf52010-09-18 15:03:21 +000037static unsigned GetType(const MCSymbolData &SD) {
38 uint32_t Type = (SD.getFlags() & (0xf << ELF_STT_Shift)) >> ELF_STT_Shift;
39 assert(Type == ELF::STT_NOTYPE || Type == ELF::STT_OBJECT ||
40 Type == ELF::STT_FUNC || Type == ELF::STT_SECTION ||
41 Type == ELF::STT_FILE || Type == ELF::STT_COMMON ||
42 Type == ELF::STT_TLS);
43 return Type;
44}
45
Rafael Espindolae15eb4e2010-09-23 19:55:14 +000046static unsigned GetBinding(const MCSymbolData &SD) {
47 uint32_t Binding = (SD.getFlags() & (0xf << ELF_STB_Shift)) >> ELF_STB_Shift;
48 assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
49 Binding == ELF::STB_WEAK);
50 return Binding;
51}
52
53static void SetBinding(MCSymbolData &SD, unsigned Binding) {
54 assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
55 Binding == ELF::STB_WEAK);
56 uint32_t OtherFlags = SD.getFlags() & ~(0xf << ELF_STB_Shift);
57 SD.setFlags(OtherFlags | (Binding << ELF_STB_Shift));
58}
59
Rafael Espindola152c1062010-10-06 21:02:29 +000060static unsigned GetVisibility(MCSymbolData &SD) {
61 unsigned Visibility =
62 (SD.getFlags() & (0xf << ELF_STV_Shift)) >> ELF_STV_Shift;
63 assert(Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_INTERNAL ||
64 Visibility == ELF::STV_HIDDEN || Visibility == ELF::STV_PROTECTED);
65 return Visibility;
66}
67
Rafael Espindolacebdc012010-10-04 19:46:28 +000068static bool isFixupKindX86PCRel(unsigned Kind) {
69 switch (Kind) {
70 default:
71 return false;
72 case X86::reloc_pcrel_1byte:
73 case X86::reloc_pcrel_4byte:
74 case X86::reloc_riprel_4byte:
75 case X86::reloc_riprel_4byte_movq_load:
76 return true;
77 }
78}
79
Rafael Espindolaa0a2f872010-10-28 14:22:44 +000080static bool RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant) {
81 switch (Variant) {
Rafael Espindola5c77c162010-10-05 15:48:37 +000082 default:
83 return false;
Rafael Espindolaa0a2f872010-10-28 14:22:44 +000084 case MCSymbolRefExpr::VK_GOT:
85 case MCSymbolRefExpr::VK_PLT:
86 case MCSymbolRefExpr::VK_GOTPCREL:
87 case MCSymbolRefExpr::VK_TPOFF:
88 case MCSymbolRefExpr::VK_TLSGD:
89 case MCSymbolRefExpr::VK_GOTTPOFF:
90 case MCSymbolRefExpr::VK_INDNTPOFF:
91 case MCSymbolRefExpr::VK_NTPOFF:
92 case MCSymbolRefExpr::VK_GOTNTPOFF:
Rafael Espindolaa264f722010-10-28 14:37:09 +000093 case MCSymbolRefExpr::VK_TLSLDM:
Rafael Espindola0cf15d62010-10-28 14:48:59 +000094 case MCSymbolRefExpr::VK_DTPOFF:
Rafael Espindolab4d17212010-10-28 15:02:40 +000095 case MCSymbolRefExpr::VK_TLSLD:
Rafael Espindola5c77c162010-10-05 15:48:37 +000096 return true;
97 }
98}
99
Matt Fleming3565a062010-08-16 18:57:57 +0000100namespace {
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000101 class ELFObjectWriter : public MCObjectWriter {
Chris Lattnerb188a372010-08-28 03:21:03 +0000102 /*static bool isFixupKindX86RIPRel(unsigned Kind) {
Matt Fleming3565a062010-08-16 18:57:57 +0000103 return Kind == X86::reloc_riprel_4byte ||
104 Kind == X86::reloc_riprel_4byte_movq_load;
Chris Lattnerb188a372010-08-28 03:21:03 +0000105 }*/
Matt Fleming3565a062010-08-16 18:57:57 +0000106
107
108 /// ELFSymbolData - Helper struct for containing some precomputed information
109 /// on symbols.
110 struct ELFSymbolData {
111 MCSymbolData *SymbolData;
112 uint64_t StringIndex;
113 uint32_t SectionIndex;
114
115 // Support lexicographic sorting.
116 bool operator<(const ELFSymbolData &RHS) const {
Rafael Espindolaad49cf52010-09-18 15:03:21 +0000117 if (GetType(*SymbolData) == ELF::STT_FILE)
118 return true;
119 if (GetType(*RHS.SymbolData) == ELF::STT_FILE)
120 return false;
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000121 return SymbolData->getSymbol().getName() <
122 RHS.SymbolData->getSymbol().getName();
Matt Fleming3565a062010-08-16 18:57:57 +0000123 }
124 };
125
126 /// @name Relocation Data
127 /// @{
128
129 struct ELFRelocationEntry {
130 // Make these big enough for both 32-bit and 64-bit
131 uint64_t r_offset;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000132 int Index;
133 unsigned Type;
134 const MCSymbol *Symbol;
Matt Fleming3565a062010-08-16 18:57:57 +0000135 uint64_t r_addend;
136
137 // Support lexicographic sorting.
138 bool operator<(const ELFRelocationEntry &RE) const {
139 return RE.r_offset < r_offset;
140 }
141 };
142
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000143 SmallPtrSet<const MCSymbol *, 16> UsedInReloc;
Rafael Espindola484291c2010-11-01 14:28:48 +0000144 SmallPtrSet<const MCSymbol *, 16> WeakrefUsedInReloc;
Rafael Espindola88182132010-10-27 15:18:17 +0000145 DenseMap<const MCSymbol *, const MCSymbol *> Renames;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000146
Matt Fleming3565a062010-08-16 18:57:57 +0000147 llvm::DenseMap<const MCSectionData*,
148 std::vector<ELFRelocationEntry> > Relocations;
149 DenseMap<const MCSection*, uint64_t> SectionStringTableIndex;
150
151 /// @}
152 /// @name Symbol Table Data
153 /// @{
154
155 SmallString<256> StringTable;
156 std::vector<ELFSymbolData> LocalSymbolData;
157 std::vector<ELFSymbolData> ExternalSymbolData;
158 std::vector<ELFSymbolData> UndefinedSymbolData;
159
160 /// @}
161
Rafael Espindola5c77c162010-10-05 15:48:37 +0000162 bool NeedsGOT;
163
Rafael Espindola7be2c332010-10-31 00:16:26 +0000164 bool NeedsSymtabShndx;
165
Matt Fleming3565a062010-08-16 18:57:57 +0000166 unsigned Is64Bit : 1;
167
168 bool HasRelocationAddend;
169
Roman Divacky5baf79e2010-09-09 17:57:50 +0000170 Triple::OSType OSType;
171
Wesley Peckeecb8582010-10-22 15:52:49 +0000172 uint16_t EMachine;
173
Matt Fleming3565a062010-08-16 18:57:57 +0000174 // This holds the symbol table index of the last local symbol.
175 unsigned LastLocalSymbolIndex;
176 // This holds the .strtab section index.
177 unsigned StringTableIndex;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000178 // This holds the .symtab section index.
179 unsigned SymbolTableIndex;
Matt Fleming3565a062010-08-16 18:57:57 +0000180
181 unsigned ShstrtabIndex;
182
183 public:
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000184 ELFObjectWriter(raw_ostream &_OS, bool _Is64Bit, bool IsLittleEndian,
185 uint16_t _EMachine, bool _HasRelAddend,
186 Triple::OSType _OSType)
187 : MCObjectWriter(_OS, IsLittleEndian),
188 NeedsGOT(false), NeedsSymtabShndx(false),
Roman Divacky5baf79e2010-09-09 17:57:50 +0000189 Is64Bit(_Is64Bit), HasRelocationAddend(_HasRelAddend),
Wesley Peckeecb8582010-10-22 15:52:49 +0000190 OSType(_OSType), EMachine(_EMachine) {
Matt Fleming3565a062010-08-16 18:57:57 +0000191 }
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000192
Matt Fleming3565a062010-08-16 18:57:57 +0000193 void WriteWord(uint64_t W) {
Chris Lattnerb188a372010-08-28 03:21:03 +0000194 if (Is64Bit)
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000195 Write64(W);
Chris Lattnerb188a372010-08-28 03:21:03 +0000196 else
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000197 Write32(W);
Matt Fleming3565a062010-08-16 18:57:57 +0000198 }
199
Matt Fleming3565a062010-08-16 18:57:57 +0000200 void StringLE16(char *buf, uint16_t Value) {
201 buf[0] = char(Value >> 0);
202 buf[1] = char(Value >> 8);
203 }
204
205 void StringLE32(char *buf, uint32_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000206 StringLE16(buf, uint16_t(Value >> 0));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000207 StringLE16(buf + 2, uint16_t(Value >> 16));
Matt Fleming3565a062010-08-16 18:57:57 +0000208 }
209
210 void StringLE64(char *buf, uint64_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000211 StringLE32(buf, uint32_t(Value >> 0));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000212 StringLE32(buf + 4, uint32_t(Value >> 32));
Matt Fleming3565a062010-08-16 18:57:57 +0000213 }
214
215 void StringBE16(char *buf ,uint16_t Value) {
216 buf[0] = char(Value >> 8);
217 buf[1] = char(Value >> 0);
218 }
219
220 void StringBE32(char *buf, uint32_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000221 StringBE16(buf, uint16_t(Value >> 16));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000222 StringBE16(buf + 2, uint16_t(Value >> 0));
Matt Fleming3565a062010-08-16 18:57:57 +0000223 }
224
225 void StringBE64(char *buf, uint64_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000226 StringBE32(buf, uint32_t(Value >> 32));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000227 StringBE32(buf + 4, uint32_t(Value >> 0));
Matt Fleming3565a062010-08-16 18:57:57 +0000228 }
229
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000230 void String8(MCDataFragment &F, uint8_t Value) {
231 char buf[1];
232 buf[0] = Value;
233 F.getContents() += StringRef(buf, 1);
234 }
235
236 void String16(MCDataFragment &F, uint16_t Value) {
237 char buf[2];
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000238 if (isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000239 StringLE16(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000240 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000241 StringBE16(buf, Value);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000242 F.getContents() += StringRef(buf, 2);
Matt Fleming3565a062010-08-16 18:57:57 +0000243 }
244
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000245 void String32(MCDataFragment &F, uint32_t Value) {
246 char buf[4];
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000247 if (isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000248 StringLE32(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000249 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000250 StringBE32(buf, Value);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000251 F.getContents() += StringRef(buf, 4);
Matt Fleming3565a062010-08-16 18:57:57 +0000252 }
253
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000254 void String64(MCDataFragment &F, uint64_t Value) {
255 char buf[8];
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000256 if (isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000257 StringLE64(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000258 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000259 StringBE64(buf, Value);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000260 F.getContents() += StringRef(buf, 8);
Matt Fleming3565a062010-08-16 18:57:57 +0000261 }
262
263 void WriteHeader(uint64_t SectionDataSize, unsigned NumberOfSections);
264
Rafael Espindola7be2c332010-10-31 00:16:26 +0000265 void WriteSymbolEntry(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
266 uint64_t name, uint8_t info,
Matt Fleming3565a062010-08-16 18:57:57 +0000267 uint64_t value, uint64_t size,
Rafael Espindola7be2c332010-10-31 00:16:26 +0000268 uint8_t other, uint32_t shndx,
269 bool Reserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000270
Rafael Espindola7be2c332010-10-31 00:16:26 +0000271 void WriteSymbol(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
272 ELFSymbolData &MSD,
Matt Fleming3565a062010-08-16 18:57:57 +0000273 const MCAsmLayout &Layout);
274
Rafael Espindolabab2a802010-11-10 21:51:05 +0000275 typedef DenseMap<const MCSectionELF*, uint32_t> SectionIndexMapTy;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000276 void WriteSymbolTable(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
277 const MCAssembler &Asm,
Rafael Espindola71859c62010-09-16 19:46:31 +0000278 const MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000279 const SectionIndexMapTy &SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +0000280
281 void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
282 const MCFragment *Fragment, const MCFixup &Fixup,
283 MCValue Target, uint64_t &FixedValue);
284
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000285 uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm,
286 const MCSymbol *S);
Matt Fleming3565a062010-08-16 18:57:57 +0000287
288 /// ComputeSymbolTable - Compute the symbol table data
289 ///
290 /// \param StringTable [out] - The string table data.
291 /// \param StringIndexMap [out] - Map from symbol names to offsets in the
292 /// string table.
Rafael Espindolabab2a802010-11-10 21:51:05 +0000293 void ComputeSymbolTable(MCAssembler &Asm,
294 const SectionIndexMapTy &SectionIndexMap);
295
296 void ComputeIndexMap(MCAssembler &Asm,
297 SectionIndexMapTy &SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +0000298
299 void WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
300 const MCSectionData &SD);
301
302 void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout) {
303 for (MCAssembler::const_iterator it = Asm.begin(),
304 ie = Asm.end(); it != ie; ++it) {
305 WriteRelocation(Asm, Layout, *it);
306 }
307 }
308
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000309 void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout,
310 const SectionIndexMapTy &SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +0000311
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000312 // Map from a group section to the signature symbol
313 typedef DenseMap<const MCSectionELF*, const MCSymbol*> GroupMapTy;
314 void CreateGroupSections(MCAssembler &Asm, MCAsmLayout &Layout,
315 GroupMapTy &GroupMap);
316
Rafael Espindola88182132010-10-27 15:18:17 +0000317 void ExecutePostLayoutBinding(MCAssembler &Asm);
Matt Fleming3565a062010-08-16 18:57:57 +0000318
319 void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
320 uint64_t Address, uint64_t Offset,
321 uint64_t Size, uint32_t Link, uint32_t Info,
322 uint64_t Alignment, uint64_t EntrySize);
323
324 void WriteRelocationsFragment(const MCAssembler &Asm, MCDataFragment *F,
325 const MCSectionData *SD);
326
Rafael Espindola70703872010-09-30 02:22:20 +0000327 bool IsFixupFullyResolved(const MCAssembler &Asm,
328 const MCValue Target,
329 bool IsPCRel,
330 const MCFragment *DF) const;
331
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000332 void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000333 void WriteSection(MCAssembler &Asm,
334 const SectionIndexMapTy &SectionIndexMap,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000335 uint32_t GroupSymbolIndex,
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000336 uint64_t Offset, uint64_t Size, uint64_t Alignment,
337 const MCSectionELF &Section);
Matt Fleming3565a062010-08-16 18:57:57 +0000338 };
339
340}
341
342// Emit the ELF header.
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000343void ELFObjectWriter::WriteHeader(uint64_t SectionDataSize,
344 unsigned NumberOfSections) {
Matt Fleming3565a062010-08-16 18:57:57 +0000345 // ELF Header
346 // ----------
347 //
348 // Note
349 // ----
350 // emitWord method behaves differently for ELF32 and ELF64, writing
351 // 4 bytes in the former and 8 in the latter.
352
353 Write8(0x7f); // e_ident[EI_MAG0]
354 Write8('E'); // e_ident[EI_MAG1]
355 Write8('L'); // e_ident[EI_MAG2]
356 Write8('F'); // e_ident[EI_MAG3]
357
358 Write8(Is64Bit ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
359
360 // e_ident[EI_DATA]
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000361 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
Matt Fleming3565a062010-08-16 18:57:57 +0000362
363 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky5baf79e2010-09-09 17:57:50 +0000364 // e_ident[EI_OSABI]
365 switch (OSType) {
366 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
367 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
368 default: Write8(ELF::ELFOSABI_NONE); break;
369 }
Matt Fleming3565a062010-08-16 18:57:57 +0000370 Write8(0); // e_ident[EI_ABIVERSION]
371
372 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
373
374 Write16(ELF::ET_REL); // e_type
375
Wesley Peckeecb8582010-10-22 15:52:49 +0000376 Write16(EMachine); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +0000377
378 Write32(ELF::EV_CURRENT); // e_version
379 WriteWord(0); // e_entry, no entry point in .o file
380 WriteWord(0); // e_phoff, no program header for .o
Benjamin Kramereb976772010-08-17 17:02:29 +0000381 WriteWord(SectionDataSize + (Is64Bit ? sizeof(ELF::Elf64_Ehdr) :
382 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000383
384 // FIXME: Make this configurable.
385 Write32(0); // e_flags = whatever the target wants
386
387 // e_ehsize = ELF header size
388 Write16(Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
389
390 Write16(0); // e_phentsize = prog header entry size
391 Write16(0); // e_phnum = # prog header entries = 0
392
393 // e_shentsize = Section header entry size
394 Write16(Is64Bit ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
395
396 // e_shnum = # of section header ents
Rafael Espindola7be2c332010-10-31 00:16:26 +0000397 if (NumberOfSections >= ELF::SHN_LORESERVE)
398 Write16(0);
399 else
400 Write16(NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000401
402 // e_shstrndx = Section # of '.shstrtab'
Rafael Espindola7be2c332010-10-31 00:16:26 +0000403 if (NumberOfSections >= ELF::SHN_LORESERVE)
404 Write16(ELF::SHN_XINDEX);
405 else
406 Write16(ShstrtabIndex);
Matt Fleming3565a062010-08-16 18:57:57 +0000407}
408
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000409void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
410 MCDataFragment *ShndxF,
411 uint64_t name,
412 uint8_t info, uint64_t value,
413 uint64_t size, uint8_t other,
414 uint32_t shndx,
415 bool Reserved) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000416 if (ShndxF) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000417 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000418 String32(*ShndxF, shndx);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000419 else
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000420 String32(*ShndxF, 0);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000421 }
422
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000423 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
424 uint16_t(ELF::SHN_XINDEX) : shndx;
425
Matt Fleming3565a062010-08-16 18:57:57 +0000426 if (Is64Bit) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000427 String32(*SymtabF, name); // st_name
428 String8(*SymtabF, info); // st_info
429 String8(*SymtabF, other); // st_other
430 String16(*SymtabF, Index); // st_shndx
431 String64(*SymtabF, value); // st_value
432 String64(*SymtabF, size); // st_size
Matt Fleming3565a062010-08-16 18:57:57 +0000433 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000434 String32(*SymtabF, name); // st_name
435 String32(*SymtabF, value); // st_value
436 String32(*SymtabF, size); // st_size
437 String8(*SymtabF, info); // st_info
438 String8(*SymtabF, other); // st_other
439 String16(*SymtabF, Index); // st_shndx
Matt Fleming3565a062010-08-16 18:57:57 +0000440 }
441}
442
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000443static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout) {
444 if (Data.isCommon() && Data.isExternal())
445 return Data.getCommonAlignment();
446
447 const MCSymbol &Symbol = Data.getSymbol();
448 if (!Symbol.isInSection())
449 return 0;
450
Rafael Espindola1973d432010-10-28 19:39:57 +0000451 if (MCFragment *FF = Data.getFragment())
452 return Layout.getSymbolAddress(&Data) -
453 Layout.getSectionAddress(FF->getParent());
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000454
455 return 0;
456}
457
Rafael Espindolade89b012010-10-15 18:25:33 +0000458static const MCSymbol &AliasedSymbol(const MCSymbol &Symbol) {
459 const MCSymbol *S = &Symbol;
460 while (S->isVariable()) {
461 const MCExpr *Value = S->getVariableValue();
Rafael Espindola9302bd62010-11-11 17:24:43 +0000462 MCExpr::ExprKind Kind = Value->getKind();
463 switch (Kind) {
464 case MCExpr::SymbolRef: {
465 const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr*>(Value);
466 S = &Ref->getSymbol();
467 break;
468 }
469 case MCExpr::Target: {
470 const MCTargetExpr *TExp = static_cast<const MCTargetExpr*>(Value);
471 MCValue Res;
472 TExp->EvaluateAsRelocatableImpl(Res, NULL);
473 S = &Res.getSymA()->getSymbol();
474 break;
475 }
476 default:
Rafael Espindolaa6866962010-10-27 14:44:52 +0000477 return *S;
Rafael Espindola9302bd62010-11-11 17:24:43 +0000478 }
Rafael Espindolade89b012010-10-15 18:25:33 +0000479 }
480 return *S;
481}
482
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000483void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm) {
Rafael Espindola88182132010-10-27 15:18:17 +0000484 // The presence of symbol versions causes undefined symbols and
485 // versions declared with @@@ to be renamed.
486
487 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
488 ie = Asm.symbol_end(); it != ie; ++it) {
489 const MCSymbol &Alias = it->getSymbol();
Rafael Espindola88182132010-10-27 15:18:17 +0000490 const MCSymbol &Symbol = AliasedSymbol(Alias);
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000491 MCSymbolData &SD = Asm.getSymbolData(Symbol);
492
493 // Undefined symbols are global, but this is the first place we
494 // are able to set it.
495 if (Symbol.isUndefined() && !Symbol.isVariable()) {
496 if (GetBinding(SD) == ELF::STB_LOCAL) {
497 SetBinding(SD, ELF::STB_GLOBAL);
498 SetBinding(*it, ELF::STB_GLOBAL);
499 }
500 }
501
502 // Not an alias.
503 if (&Symbol == &Alias)
504 continue;
505
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000506 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000507 size_t Pos = AliasName.find('@');
508 if (Pos == StringRef::npos)
509 continue;
510
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000511 // Aliases defined with .symvar copy the binding from the symbol they alias.
512 // This is the first place we are able to copy this information.
513 it->setExternal(SD.isExternal());
514 SetBinding(*it, GetBinding(SD));
515
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000516 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000517 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
518 continue;
519
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000520 // FIXME: produce a better error message.
521 if (Symbol.isUndefined() && Rest.startswith("@@") &&
522 !Rest.startswith("@@@"))
523 report_fatal_error("A @@ version cannot be undefined");
524
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000525 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000526 }
527}
528
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000529void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
530 MCDataFragment *ShndxF,
531 ELFSymbolData &MSD,
532 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000533 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000534 MCSymbolData &Data =
535 Layout.getAssembler().getSymbolData(AliasedSymbol(OrigData.getSymbol()));
Rafael Espindola152c1062010-10-06 21:02:29 +0000536
Rafael Espindola7be2c332010-10-31 00:16:26 +0000537 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
538 Data.getSymbol().isVariable();
539
Rafael Espindola152c1062010-10-06 21:02:29 +0000540 uint8_t Binding = GetBinding(OrigData);
541 uint8_t Visibility = GetVisibility(OrigData);
542 uint8_t Type = GetType(Data);
543
544 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
545 uint8_t Other = Visibility;
546
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000547 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000548 uint64_t Size = 0;
549 const MCExpr *ESize;
550
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000551 assert(!(Data.isCommon() && !Data.isExternal()));
552
Matt Fleming3565a062010-08-16 18:57:57 +0000553 ESize = Data.getSize();
554 if (Data.getSize()) {
555 MCValue Res;
556 if (ESize->getKind() == MCExpr::Binary) {
557 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(ESize);
558
559 if (BE->EvaluateAsRelocatable(Res, &Layout)) {
Benjamin Kramer24f12062010-10-17 07:38:40 +0000560 assert(!Res.getSymA() || !Res.getSymA()->getSymbol().isDefined());
561 assert(!Res.getSymB() || !Res.getSymB()->getSymbol().isDefined());
Rafael Espindolaf230df92010-10-16 18:23:53 +0000562 Size = Res.getConstant();
Matt Fleming3565a062010-08-16 18:57:57 +0000563 }
564 } else if (ESize->getKind() == MCExpr::Constant) {
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000565 Size = static_cast<const MCConstantExpr *>(ESize)->getValue();
Matt Fleming3565a062010-08-16 18:57:57 +0000566 } else {
567 assert(0 && "Unsupported size expression");
568 }
569 }
570
571 // Write out the symbol table entry
Rafael Espindola7be2c332010-10-31 00:16:26 +0000572 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
573 Size, Other, MSD.SectionIndex, IsReserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000574}
575
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000576void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
577 MCDataFragment *ShndxF,
578 const MCAssembler &Asm,
579 const MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000580 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000581 // The string table must be emitted first because we need the index
582 // into the string table for all the symbol names.
583 assert(StringTable.size() && "Missing string table");
584
585 // FIXME: Make sure the start of the symbol table is aligned.
586
587 // The first entry is the undefined symbol entry.
Rafael Espindola7be2c332010-10-31 00:16:26 +0000588 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
Matt Fleming3565a062010-08-16 18:57:57 +0000589
590 // Write the symbol table entries.
591 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
592 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
593 ELFSymbolData &MSD = LocalSymbolData[i];
Rafael Espindola7be2c332010-10-31 00:16:26 +0000594 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000595 }
596
Rafael Espindola71859c62010-09-16 19:46:31 +0000597 // Write out a symbol table entry for each regular section.
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000598 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
599 ++i) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000600 const MCSectionELF &Section =
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000601 static_cast<const MCSectionELF&>(i->getSection());
602 if (Section.getType() == ELF::SHT_RELA ||
603 Section.getType() == ELF::SHT_REL ||
604 Section.getType() == ELF::SHT_STRTAB ||
605 Section.getType() == ELF::SHT_SYMTAB)
Eli Friedmana44fa242010-08-16 21:17:09 +0000606 continue;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000607 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000608 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section), false);
Eli Friedmana44fa242010-08-16 21:17:09 +0000609 LastLocalSymbolIndex++;
610 }
Matt Fleming3565a062010-08-16 18:57:57 +0000611
612 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
613 ELFSymbolData &MSD = ExternalSymbolData[i];
614 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000615 assert(((Data.getFlags() & ELF_STB_Global) ||
616 (Data.getFlags() & ELF_STB_Weak)) &&
617 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000618 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000619 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000620 LastLocalSymbolIndex++;
621 }
622
623 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
624 ELFSymbolData &MSD = UndefinedSymbolData[i];
625 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000626 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000627 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000628 LastLocalSymbolIndex++;
629 }
630}
631
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000632static bool ShouldRelocOnSymbol(const MCSymbolData &SD,
Rafael Espindola3729d002010-10-05 23:57:26 +0000633 const MCValue &Target,
634 const MCFragment &F) {
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000635 const MCSymbol &Symbol = SD.getSymbol();
636 if (Symbol.isUndefined())
637 return true;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000638
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000639 const MCSectionELF &Section =
640 static_cast<const MCSectionELF&>(Symbol.getSection());
641
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000642 if (SD.isExternal())
643 return true;
644
Rafael Espindola8cecf252010-10-06 16:23:36 +0000645 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000646 const MCSectionELF &Sec2 =
647 static_cast<const MCSectionELF&>(F.getParent()->getSection());
648
Rafael Espindolace2d3c52010-10-18 20:25:33 +0000649 if (Section.getKind().isBSS())
650 return false;
651
Rafael Espindola8cecf252010-10-06 16:23:36 +0000652 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000653 (Kind == MCSymbolRefExpr::VK_PLT ||
654 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
655 Kind == MCSymbolRefExpr::VK_GOTOFF))
Rafael Espindola3729d002010-10-05 23:57:26 +0000656 return true;
657
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000658 if (Section.getFlags() & MCSectionELF::SHF_MERGE)
659 return Target.getConstant() != 0;
660
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000661 return false;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000662}
663
Benjamin Kramere5b57342010-08-17 18:20:28 +0000664// FIXME: this is currently X86/X86_64 only
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000665void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
666 const MCAsmLayout &Layout,
667 const MCFragment *Fragment,
668 const MCFixup &Fixup,
669 MCValue Target,
670 uint64_t &FixedValue) {
Matt Fleming3565a062010-08-16 18:57:57 +0000671 int64_t Addend = 0;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000672 int Index = 0;
Benjamin Kramer95c602a2010-08-27 10:38:39 +0000673 int64_t Value = Target.getConstant();
Rafael Espindola03f1b742010-11-11 16:48:11 +0000674 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
675 const MCSymbol &ASymbol = AliasedSymbol(Symbol);
676 const MCSymbol *RenamedP = Renames.lookup(&Symbol);
677 if (!RenamedP)
678 RenamedP = &ASymbol;
679 const MCSymbol &Renamed = *RenamedP;
Matt Fleming3565a062010-08-16 18:57:57 +0000680
Rafael Espindola00074892010-09-17 22:34:41 +0000681 bool IsPCRel = isFixupKindX86PCRel(Fixup.getKind());
Benjamin Kramer81cfb852010-08-17 19:45:05 +0000682 if (!Target.isAbsolute()) {
Rafael Espindola03f1b742010-11-11 16:48:11 +0000683 MCSymbolData &SD = Asm.getSymbolData(Symbol);
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000684 MCFragment *F = SD.getFragment();
Matt Fleming3565a062010-08-16 18:57:57 +0000685
Rafael Espindola9d8b7552010-10-03 00:46:57 +0000686 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
687 const MCSymbol &SymbolB = RefB->getSymbol();
688 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
689 IsPCRel = true;
Rafael Espindola55fb1022010-10-04 15:59:01 +0000690 MCSectionData *Sec = Fragment->getParent();
691
692 // Offset of the symbol in the section
693 int64_t a = Layout.getSymbolAddress(&SDB) - Layout.getSectionAddress(Sec);
694
695 // Ofeset of the relocation in the section
696 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
697 Value += b - a;
Rafael Espindola9d8b7552010-10-03 00:46:57 +0000698 }
699
Rafael Espindola3729d002010-10-05 23:57:26 +0000700 bool RelocOnSymbol = ShouldRelocOnSymbol(SD, Target, *Fragment);
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000701 if (!RelocOnSymbol) {
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000702 Index = F->getParent()->getOrdinal();
Rafael Espindolaa6489182010-09-24 21:19:03 +0000703
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000704 MCSectionData *FSD = F->getParent();
705 // Offset of the symbol in the section
706 Value += Layout.getSymbolAddress(&SD) - Layout.getSectionAddress(FSD);
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000707 } else {
Rafael Espindola03f1b742010-11-11 16:48:11 +0000708 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
709 WeakrefUsedInReloc.insert(&Renamed);
710 else
711 UsedInReloc.insert(&Renamed);
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000712 Index = -1;
713 }
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000714 Addend = Value;
715 // Compensate for the addend on i386.
716 if (Is64Bit)
717 Value = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000718 }
719
Benjamin Kramer95c602a2010-08-27 10:38:39 +0000720 FixedValue = Value;
721
Matt Fleming3565a062010-08-16 18:57:57 +0000722 // determine the type of the relocation
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000723
724 MCSymbolRefExpr::VariantKind Modifier = Target.getSymA()->getKind();
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000725 unsigned Type;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000726 if (Is64Bit) {
727 if (IsPCRel) {
Rafael Espindola92bf6682010-10-04 19:04:13 +0000728 switch (Modifier) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000729 default:
730 llvm_unreachable("Unimplemented");
Rafael Espindola92bf6682010-10-04 19:04:13 +0000731 case MCSymbolRefExpr::VK_None:
732 Type = ELF::R_X86_64_PC32;
733 break;
734 case MCSymbolRefExpr::VK_PLT:
735 Type = ELF::R_X86_64_PLT32;
736 break;
Rafael Espindolaa0a2f872010-10-28 14:22:44 +0000737 case MCSymbolRefExpr::VK_GOTPCREL:
Rafael Espindola607d1f62010-10-04 19:51:39 +0000738 Type = ELF::R_X86_64_GOTPCREL;
739 break;
Rafael Espindolabc82d8b2010-10-27 20:28:07 +0000740 case MCSymbolRefExpr::VK_GOTTPOFF:
741 Type = ELF::R_X86_64_GOTTPOFF;
742 break;
743 case MCSymbolRefExpr::VK_TLSGD:
744 Type = ELF::R_X86_64_TLSGD;
745 break;
Rafael Espindolab4d17212010-10-28 15:02:40 +0000746 case MCSymbolRefExpr::VK_TLSLD:
747 Type = ELF::R_X86_64_TLSLD;
748 break;
Rafael Espindola92bf6682010-10-04 19:04:13 +0000749 }
Benjamin Kramere5b57342010-08-17 18:20:28 +0000750 } else {
751 switch ((unsigned)Fixup.getKind()) {
752 default: llvm_unreachable("invalid fixup kind!");
753 case FK_Data_8: Type = ELF::R_X86_64_64; break;
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000754 case X86::reloc_signed_4byte:
Benjamin Kramere5b57342010-08-17 18:20:28 +0000755 case X86::reloc_pcrel_4byte:
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000756 assert(isInt<32>(Target.getConstant()));
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000757 switch (Modifier) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000758 default:
759 llvm_unreachable("Unimplemented");
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000760 case MCSymbolRefExpr::VK_None:
761 Type = ELF::R_X86_64_32S;
762 break;
763 case MCSymbolRefExpr::VK_GOT:
764 Type = ELF::R_X86_64_GOT32;
765 break;
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000766 case MCSymbolRefExpr::VK_GOTPCREL:
Rafael Espindola8cecf252010-10-06 16:23:36 +0000767 Type = ELF::R_X86_64_GOTPCREL;
768 break;
Rafael Espindolabc82d8b2010-10-27 20:28:07 +0000769 case MCSymbolRefExpr::VK_TPOFF:
770 Type = ELF::R_X86_64_TPOFF32;
771 break;
Rafael Espindolaaa8f1f02010-10-28 15:11:03 +0000772 case MCSymbolRefExpr::VK_DTPOFF:
773 Type = ELF::R_X86_64_DTPOFF32;
774 break;
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000775 }
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000776 break;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000777 case FK_Data_4:
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000778 Type = ELF::R_X86_64_32;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000779 break;
780 case FK_Data_2: Type = ELF::R_X86_64_16; break;
781 case X86::reloc_pcrel_1byte:
782 case FK_Data_1: Type = ELF::R_X86_64_8; break;
783 }
784 }
Matt Fleming3565a062010-08-16 18:57:57 +0000785 } else {
Benjamin Kramere5b57342010-08-17 18:20:28 +0000786 if (IsPCRel) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000787 switch (Modifier) {
788 default:
Rafael Espindola24dc9ec2010-10-18 19:33:01 +0000789 llvm_unreachable("Unimplemented");
790 case MCSymbolRefExpr::VK_None:
Rafael Espindola9baee3b2010-10-18 18:03:28 +0000791 Type = ELF::R_386_PC32;
Rafael Espindola9baee3b2010-10-18 18:03:28 +0000792 break;
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000793 case MCSymbolRefExpr::VK_PLT:
794 Type = ELF::R_386_PLT32;
795 break;
796 }
Benjamin Kramere5b57342010-08-17 18:20:28 +0000797 } else {
798 switch ((unsigned)Fixup.getKind()) {
799 default: llvm_unreachable("invalid fixup kind!");
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000800
Rafael Espindola24ba4f72010-10-24 17:35:42 +0000801 case X86::reloc_global_offset_table:
802 Type = ELF::R_386_GOTPC;
803 break;
804
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000805 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
806 // instead?
807 case X86::reloc_signed_4byte:
Benjamin Kramere5b57342010-08-17 18:20:28 +0000808 case X86::reloc_pcrel_4byte:
Rafael Espindolabd701182010-10-19 19:31:37 +0000809 case FK_Data_4:
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000810 switch (Modifier) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000811 default:
812 llvm_unreachable("Unimplemented");
Rafael Espindolabd701182010-10-19 19:31:37 +0000813 case MCSymbolRefExpr::VK_None:
Rafael Espindola24ba4f72010-10-24 17:35:42 +0000814 Type = ELF::R_386_32;
Rafael Espindolabd701182010-10-19 19:31:37 +0000815 break;
Rafael Espindolaeada3042010-10-18 20:47:21 +0000816 case MCSymbolRefExpr::VK_GOT:
817 Type = ELF::R_386_GOT32;
818 break;
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000819 case MCSymbolRefExpr::VK_GOTOFF:
820 Type = ELF::R_386_GOTOFF;
821 break;
Rafael Espindola3cede2d2010-10-27 21:23:52 +0000822 case MCSymbolRefExpr::VK_TLSGD:
823 Type = ELF::R_386_TLS_GD;
824 break;
825 case MCSymbolRefExpr::VK_TPOFF:
826 Type = ELF::R_386_TLS_LE_32;
827 break;
828 case MCSymbolRefExpr::VK_INDNTPOFF:
829 Type = ELF::R_386_TLS_IE;
830 break;
831 case MCSymbolRefExpr::VK_NTPOFF:
832 Type = ELF::R_386_TLS_LE;
833 break;
Rafael Espindolaa0a2f872010-10-28 14:22:44 +0000834 case MCSymbolRefExpr::VK_GOTNTPOFF:
835 Type = ELF::R_386_TLS_GOTIE;
836 break;
Rafael Espindolaa264f722010-10-28 14:37:09 +0000837 case MCSymbolRefExpr::VK_TLSLDM:
838 Type = ELF::R_386_TLS_LDM;
839 break;
Rafael Espindola0cf15d62010-10-28 14:48:59 +0000840 case MCSymbolRefExpr::VK_DTPOFF:
841 Type = ELF::R_386_TLS_LDO_32;
842 break;
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000843 }
844 break;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000845 case FK_Data_2: Type = ELF::R_386_16; break;
846 case X86::reloc_pcrel_1byte:
847 case FK_Data_1: Type = ELF::R_386_8; break;
848 }
Matt Fleming3565a062010-08-16 18:57:57 +0000849 }
850 }
851
Rafael Espindolaa0a2f872010-10-28 14:22:44 +0000852 if (RelocNeedsGOT(Modifier))
Rafael Espindola5c77c162010-10-05 15:48:37 +0000853 NeedsGOT = true;
854
Benjamin Kramere5b57342010-08-17 18:20:28 +0000855 ELFRelocationEntry ERE;
856
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000857 ERE.Index = Index;
858 ERE.Type = Type;
Rafael Espindola03f1b742010-11-11 16:48:11 +0000859 ERE.Symbol = &Renamed;
Matt Fleming3565a062010-08-16 18:57:57 +0000860
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000861 ERE.r_offset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
Benjamin Kramere5b57342010-08-17 18:20:28 +0000862
Matt Fleming3565a062010-08-16 18:57:57 +0000863 if (HasRelocationAddend)
864 ERE.r_addend = Addend;
Benjamin Kramer172d7d62010-08-17 00:33:24 +0000865 else
866 ERE.r_addend = 0; // Silence compiler warning.
Matt Fleming3565a062010-08-16 18:57:57 +0000867
868 Relocations[Fragment->getParent()].push_back(ERE);
869}
870
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000871uint64_t
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000872ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
873 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000874 MCSymbolData &SD = Asm.getSymbolData(*S);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000875 return SD.getIndex();
Matt Fleming3565a062010-08-16 18:57:57 +0000876}
877
Rafael Espindola737cd212010-10-05 18:01:23 +0000878static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
Rafael Espindola88182132010-10-27 15:18:17 +0000879 bool Used, bool Renamed) {
Rafael Espindola484291c2010-11-01 14:28:48 +0000880 if (Data.getFlags() & ELF_Other_Weakref)
881 return false;
882
Rafael Espindolabd701182010-10-19 19:31:37 +0000883 if (Used)
884 return true;
885
Rafael Espindola88182132010-10-27 15:18:17 +0000886 if (Renamed)
887 return false;
888
Rafael Espindola737cd212010-10-05 18:01:23 +0000889 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000890
Rafael Espindolad1798862010-10-29 23:09:31 +0000891 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
892 return true;
893
Rafael Espindolaa6866962010-10-27 14:44:52 +0000894 const MCSymbol &A = AliasedSymbol(Symbol);
Rafael Espindolad1798862010-10-29 23:09:31 +0000895 if (!A.isVariable() && A.isUndefined() && !Data.isCommon())
Rafael Espindolaa6866962010-10-27 14:44:52 +0000896 return false;
897
Rafael Espindola737cd212010-10-05 18:01:23 +0000898 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
899 return false;
900
Rafael Espindolabd701182010-10-19 19:31:37 +0000901 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000902 return false;
903
904 return true;
905}
906
907static bool isLocal(const MCSymbolData &Data) {
908 if (Data.isExternal())
909 return false;
910
911 const MCSymbol &Symbol = Data.getSymbol();
912 if (Symbol.isUndefined() && !Symbol.isVariable())
913 return false;
914
915 return true;
916}
917
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000918void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
919 SectionIndexMapTy &SectionIndexMap) {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000920 unsigned Index = 1;
921 for (MCAssembler::iterator it = Asm.begin(),
922 ie = Asm.end(); it != ie; ++it) {
923 const MCSectionELF &Section =
924 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000925 if (Section.getType() != ELF::SHT_GROUP)
926 continue;
927 SectionIndexMap[&Section] = Index++;
928 }
929
930 for (MCAssembler::iterator it = Asm.begin(),
931 ie = Asm.end(); it != ie; ++it) {
932 const MCSectionELF &Section =
933 static_cast<const MCSectionELF &>(it->getSection());
934 if (Section.getType() == ELF::SHT_GROUP)
935 continue;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000936 SectionIndexMap[&Section] = Index++;
937 }
938}
939
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000940void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindolabab2a802010-11-10 21:51:05 +0000941 const SectionIndexMapTy &SectionIndexMap) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000942 // FIXME: Is this the correct place to do this?
943 if (NeedsGOT) {
944 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
945 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
946 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
947 Data.setExternal(true);
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000948 SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindola5c77c162010-10-05 15:48:37 +0000949 }
950
Matt Fleming3565a062010-08-16 18:57:57 +0000951 // Build section lookup table.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000952 int NumRegularSections = Asm.size();
Matt Fleming3565a062010-08-16 18:57:57 +0000953
954 // Index 0 is always the empty string.
955 StringMap<uint64_t> StringIndexMap;
956 StringTable += '\x00';
957
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000958 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000959 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
960 ie = Asm.symbol_end(); it != ie; ++it) {
961 const MCSymbol &Symbol = it->getSymbol();
962
Rafael Espindola484291c2010-11-01 14:28:48 +0000963 bool Used = UsedInReloc.count(&Symbol);
964 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
965 if (!isInSymtab(Asm, *it, Used || WeakrefUsed,
Rafael Espindola88182132010-10-27 15:18:17 +0000966 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000967 continue;
968
Matt Fleming3565a062010-08-16 18:57:57 +0000969 ELFSymbolData MSD;
970 MSD.SymbolData = it;
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000971 bool Local = isLocal(*it);
Rafael Espindola88182132010-10-27 15:18:17 +0000972 const MCSymbol &RefSymbol = AliasedSymbol(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000973
Rafael Espindola484291c2010-11-01 14:28:48 +0000974 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
975 SetBinding(*it, ELF::STB_WEAK);
976
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000977 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000978 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000979 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000980 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000981 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000982 } else if (RefSymbol.isUndefined()) {
Matt Fleming3565a062010-08-16 18:57:57 +0000983 MSD.SectionIndex = ELF::SHN_UNDEF;
Matt Fleming3565a062010-08-16 18:57:57 +0000984 } else {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000985 const MCSectionELF &Section =
986 static_cast<const MCSectionELF&>(RefSymbol.getSection());
987 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000988 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
989 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000990 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000991 }
992
Rafael Espindola88182132010-10-27 15:18:17 +0000993 // The @@@ in symbol version is replaced with @ in undefined symbols and
994 // @@ in defined ones.
995 StringRef Name = Symbol.getName();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +0000996 SmallString<32> Buf;
997
Rafael Espindola88182132010-10-27 15:18:17 +0000998 size_t Pos = Name.find("@@@");
Rafael Espindola88182132010-10-27 15:18:17 +0000999 if (Pos != StringRef::npos) {
Benjamin Kramer1261a2f2010-11-12 19:26:04 +00001000 Buf += Name.substr(0, Pos);
1001 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
1002 Buf += Name.substr(Pos + Skip);
1003 Name = Buf;
Rafael Espindola88182132010-10-27 15:18:17 +00001004 }
1005
Benjamin Kramer1261a2f2010-11-12 19:26:04 +00001006 uint64_t &Entry = StringIndexMap[Name];
Rafael Espindolaa6866962010-10-27 14:44:52 +00001007 if (!Entry) {
1008 Entry = StringTable.size();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +00001009 StringTable += Name;
Rafael Espindolaa6866962010-10-27 14:44:52 +00001010 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +00001011 }
Rafael Espindolaa6866962010-10-27 14:44:52 +00001012 MSD.StringIndex = Entry;
1013 if (MSD.SectionIndex == ELF::SHN_UNDEF)
1014 UndefinedSymbolData.push_back(MSD);
1015 else if (Local)
1016 LocalSymbolData.push_back(MSD);
1017 else
1018 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +00001019 }
1020
1021 // Symbols are required to be in lexicographic order.
1022 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
1023 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
1024 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
1025
1026 // Set the symbol indices. Local symbols must come before all other
1027 // symbols with non-local bindings.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +00001028 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +00001029 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
1030 LocalSymbolData[i].SymbolData->setIndex(Index++);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +00001031
1032 Index += NumRegularSections;
1033
Matt Fleming3565a062010-08-16 18:57:57 +00001034 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
1035 ExternalSymbolData[i].SymbolData->setIndex(Index++);
1036 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
1037 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
1038}
1039
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001040void ELFObjectWriter::WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
1041 const MCSectionData &SD) {
Matt Fleming3565a062010-08-16 18:57:57 +00001042 if (!Relocations[&SD].empty()) {
1043 MCContext &Ctx = Asm.getContext();
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001044 const MCSectionELF *RelaSection;
Matt Fleming3565a062010-08-16 18:57:57 +00001045 const MCSectionELF &Section =
1046 static_cast<const MCSectionELF&>(SD.getSection());
1047
1048 const StringRef SectionName = Section.getSectionName();
Benjamin Kramer377a5722010-08-17 17:30:07 +00001049 std::string RelaSectionName = HasRelocationAddend ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +00001050 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +00001051
1052 unsigned EntrySize;
1053 if (HasRelocationAddend)
1054 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
1055 else
1056 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +00001057
Benjamin Kramer377a5722010-08-17 17:30:07 +00001058 RelaSection = Ctx.getELFSection(RelaSectionName, HasRelocationAddend ?
1059 ELF::SHT_RELA : ELF::SHT_REL, 0,
Matt Fleming3565a062010-08-16 18:57:57 +00001060 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001061 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001062
1063 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001064 RelaSD.setAlignment(Is64Bit ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +00001065
1066 MCDataFragment *F = new MCDataFragment(&RelaSD);
1067
1068 WriteRelocationsFragment(Asm, F, &SD);
1069
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001070 Asm.AddSectionToTheEnd(*this, RelaSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +00001071 }
1072}
1073
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001074void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
1075 uint64_t Flags, uint64_t Address,
1076 uint64_t Offset, uint64_t Size,
1077 uint32_t Link, uint32_t Info,
1078 uint64_t Alignment,
1079 uint64_t EntrySize) {
Matt Fleming3565a062010-08-16 18:57:57 +00001080 Write32(Name); // sh_name: index into string table
1081 Write32(Type); // sh_type
1082 WriteWord(Flags); // sh_flags
1083 WriteWord(Address); // sh_addr
1084 WriteWord(Offset); // sh_offset
1085 WriteWord(Size); // sh_size
1086 Write32(Link); // sh_link
1087 Write32(Info); // sh_info
1088 WriteWord(Alignment); // sh_addralign
1089 WriteWord(EntrySize); // sh_entsize
1090}
1091
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001092void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
1093 MCDataFragment *F,
1094 const MCSectionData *SD) {
Matt Fleming3565a062010-08-16 18:57:57 +00001095 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
1096 // sort by the r_offset just like gnu as does
1097 array_pod_sort(Relocs.begin(), Relocs.end());
1098
1099 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1100 ELFRelocationEntry entry = Relocs[e - i - 1];
1101
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001102 if (entry.Index < 0)
1103 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
1104 else
1105 entry.Index += LocalSymbolData.size() + 1;
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001106 if (Is64Bit) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001107 String64(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001108
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001109 struct ELF::Elf64_Rela ERE64;
1110 ERE64.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001111 String64(*F, ERE64.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001112
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001113 if (HasRelocationAddend)
1114 String64(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001115 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001116 String32(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001117
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001118 struct ELF::Elf32_Rela ERE32;
1119 ERE32.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001120 String32(*F, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001121
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001122 if (HasRelocationAddend)
1123 String32(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001124 }
Matt Fleming3565a062010-08-16 18:57:57 +00001125 }
1126}
1127
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001128void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
1129 MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001130 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +00001131 MCContext &Ctx = Asm.getContext();
1132 MCDataFragment *F;
1133
Matt Fleming3565a062010-08-16 18:57:57 +00001134 unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
1135
Rafael Espindola38738bf2010-09-22 19:04:41 +00001136 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001137 const MCSectionELF *ShstrtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001138 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001139 SectionKind::getReadOnly());
Rafael Espindola71859c62010-09-16 19:46:31 +00001140 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1141 ShstrtabSD.setAlignment(1);
1142 ShstrtabIndex = Asm.size();
1143
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001144 const MCSectionELF *SymtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001145 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1146 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001147 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001148 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Matt Fleming3565a062010-08-16 18:57:57 +00001149 SymtabSD.setAlignment(Is64Bit ? 8 : 4);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001150 SymbolTableIndex = Asm.size();
1151
1152 MCSectionData *SymtabShndxSD = NULL;
1153
1154 if (NeedsSymtabShndx) {
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001155 const MCSectionELF *SymtabShndxSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001156 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001157 SectionKind::getReadOnly(), 4, "");
Rafael Espindola7be2c332010-10-31 00:16:26 +00001158 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
1159 SymtabShndxSD->setAlignment(4);
1160 }
Matt Fleming3565a062010-08-16 18:57:57 +00001161
Matt Fleming3565a062010-08-16 18:57:57 +00001162 const MCSection *StrtabSection;
1163 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001164 SectionKind::getReadOnly());
Matt Fleming3565a062010-08-16 18:57:57 +00001165 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1166 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +00001167 StringTableIndex = Asm.size();
1168
Rafael Espindolac3c413f2010-09-27 22:04:54 +00001169 WriteRelocations(Asm, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001170
1171 // Symbol table
1172 F = new MCDataFragment(&SymtabSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001173 MCDataFragment *ShndxF = NULL;
1174 if (NeedsSymtabShndx) {
1175 ShndxF = new MCDataFragment(SymtabShndxSD);
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001176 Asm.AddSectionToTheEnd(*this, *SymtabShndxSD, Layout);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001177 }
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001178 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001179 Asm.AddSectionToTheEnd(*this, SymtabSD, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001180
Matt Fleming3565a062010-08-16 18:57:57 +00001181 F = new MCDataFragment(&StrtabSD);
1182 F->getContents().append(StringTable.begin(), StringTable.end());
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001183 Asm.AddSectionToTheEnd(*this, StrtabSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +00001184
Matt Fleming3565a062010-08-16 18:57:57 +00001185 F = new MCDataFragment(&ShstrtabSD);
1186
Matt Fleming3565a062010-08-16 18:57:57 +00001187 // Section header string table.
1188 //
1189 // The first entry of a string table holds a null character so skip
1190 // section 0.
1191 uint64_t Index = 1;
1192 F->getContents() += '\x00';
1193
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001194 StringMap<uint64_t> SecStringMap;
Matt Fleming3565a062010-08-16 18:57:57 +00001195 for (MCAssembler::const_iterator it = Asm.begin(),
1196 ie = Asm.end(); it != ie; ++it) {
Matt Fleming3565a062010-08-16 18:57:57 +00001197 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +00001198 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola51efe7a2010-09-23 14:14:56 +00001199 // FIXME: We could merge suffixes like in .text and .rela.text.
Matt Fleming3565a062010-08-16 18:57:57 +00001200
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001201 StringRef Name = Section.getSectionName();
1202 if (SecStringMap.count(Name)) {
1203 SectionStringTableIndex[&Section] = SecStringMap[Name];
1204 continue;
1205 }
Matt Fleming3565a062010-08-16 18:57:57 +00001206 // Remember the index into the string table so we can write it
1207 // into the sh_name field of the section header table.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001208 SectionStringTableIndex[&Section] = Index;
1209 SecStringMap[Name] = Index;
Matt Fleming3565a062010-08-16 18:57:57 +00001210
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001211 Index += Name.size() + 1;
1212 F->getContents() += Name;
Matt Fleming3565a062010-08-16 18:57:57 +00001213 F->getContents() += '\x00';
1214 }
1215
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001216 Asm.AddSectionToTheEnd(*this, ShstrtabSD, Layout);
Rafael Espindola70703872010-09-30 02:22:20 +00001217}
1218
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001219bool ELFObjectWriter::IsFixupFullyResolved(const MCAssembler &Asm,
1220 const MCValue Target,
1221 bool IsPCRel,
1222 const MCFragment *DF) const {
Rafael Espindola70703872010-09-30 02:22:20 +00001223 // If this is a PCrel relocation, find the section this fixup value is
1224 // relative to.
1225 const MCSection *BaseSection = 0;
1226 if (IsPCRel) {
1227 BaseSection = &DF->getParent()->getSection();
1228 assert(BaseSection);
1229 }
1230
1231 const MCSection *SectionA = 0;
1232 const MCSymbol *SymbolA = 0;
1233 if (const MCSymbolRefExpr *A = Target.getSymA()) {
1234 SymbolA = &A->getSymbol();
1235 SectionA = &SymbolA->getSection();
1236 }
1237
1238 const MCSection *SectionB = 0;
1239 if (const MCSymbolRefExpr *B = Target.getSymB()) {
1240 SectionB = &B->getSymbol().getSection();
1241 }
1242
1243 if (!BaseSection)
1244 return SectionA == SectionB;
1245
1246 const MCSymbolData &DataA = Asm.getSymbolData(*SymbolA);
1247 if (DataA.isExternal())
1248 return false;
1249
1250 return !SectionB && BaseSection == SectionA;
Matt Fleming3565a062010-08-16 18:57:57 +00001251}
1252
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001253void ELFObjectWriter::CreateGroupSections(MCAssembler &Asm,
1254 MCAsmLayout &Layout,
1255 GroupMapTy &GroupMap) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001256 typedef DenseMap<const MCSymbol*, const MCSectionELF*> RevGroupMapTy;
1257 // Build the groups
1258 RevGroupMapTy Groups;
1259 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1260 it != ie; ++it) {
1261 const MCSectionELF &Section =
1262 static_cast<const MCSectionELF&>(it->getSection());
1263 if (!(Section.getFlags() & MCSectionELF::SHF_GROUP))
1264 continue;
1265
1266 const MCSymbol *SignatureSymbol = Section.getGroup();
1267 Asm.getOrCreateSymbolData(*SignatureSymbol);
1268 const MCSectionELF *&Group = Groups[SignatureSymbol];
1269 if (!Group) {
1270 Group = Asm.getContext().CreateELFGroupSection();
1271 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1272 Data.setAlignment(4);
1273 MCDataFragment *F = new MCDataFragment(&Data);
1274 String32(*F, ELF::GRP_COMDAT);
1275 }
1276 GroupMap[Group] = SignatureSymbol;
1277 }
1278
1279 // Add sections to the groups
1280 unsigned Index = 1;
1281 unsigned NumGroups = Groups.size();
1282 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1283 it != ie; ++it, ++Index) {
1284 const MCSectionELF &Section =
1285 static_cast<const MCSectionELF&>(it->getSection());
1286 if (!(Section.getFlags() & MCSectionELF::SHF_GROUP))
1287 continue;
1288 const MCSectionELF *Group = Groups[Section.getGroup()];
1289 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1290 // FIXME: we could use the previous fragment
1291 MCDataFragment *F = new MCDataFragment(&Data);
1292 String32(*F, NumGroups + Index);
1293 }
1294
1295 for (RevGroupMapTy::const_iterator i = Groups.begin(), e = Groups.end();
1296 i != e; ++i) {
1297 const MCSectionELF *Group = i->second;
1298 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001299 Asm.AddSectionToTheEnd(*this, Data, Layout);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001300 }
1301}
1302
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001303void ELFObjectWriter::WriteSection(MCAssembler &Asm,
1304 const SectionIndexMapTy &SectionIndexMap,
1305 uint32_t GroupSymbolIndex,
1306 uint64_t Offset, uint64_t Size,
1307 uint64_t Alignment,
1308 const MCSectionELF &Section) {
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001309 uint64_t sh_link = 0;
1310 uint64_t sh_info = 0;
1311
1312 switch(Section.getType()) {
1313 case ELF::SHT_DYNAMIC:
1314 sh_link = SectionStringTableIndex[&Section];
1315 sh_info = 0;
1316 break;
1317
1318 case ELF::SHT_REL:
1319 case ELF::SHT_RELA: {
1320 const MCSectionELF *SymtabSection;
1321 const MCSectionELF *InfoSection;
1322 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
1323 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001324 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001325 sh_link = SectionIndexMap.lookup(SymtabSection);
1326 assert(sh_link && ".symtab not found");
1327
1328 // Remove ".rel" and ".rela" prefixes.
1329 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1330 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
1331
1332 InfoSection = Asm.getContext().getELFSection(SectionName,
1333 ELF::SHT_PROGBITS, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001334 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001335 sh_info = SectionIndexMap.lookup(InfoSection);
1336 break;
1337 }
1338
1339 case ELF::SHT_SYMTAB:
1340 case ELF::SHT_DYNSYM:
1341 sh_link = StringTableIndex;
1342 sh_info = LastLocalSymbolIndex;
1343 break;
1344
1345 case ELF::SHT_SYMTAB_SHNDX:
1346 sh_link = SymbolTableIndex;
1347 break;
1348
1349 case ELF::SHT_PROGBITS:
1350 case ELF::SHT_STRTAB:
1351 case ELF::SHT_NOBITS:
1352 case ELF::SHT_NULL:
1353 case ELF::SHT_ARM_ATTRIBUTES:
1354 // Nothing to do.
1355 break;
1356
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001357 case ELF::SHT_GROUP: {
1358 sh_link = SymbolTableIndex;
1359 sh_info = GroupSymbolIndex;
1360 break;
1361 }
1362
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001363 default:
1364 assert(0 && "FIXME: sh_type value not supported!");
1365 break;
1366 }
1367
1368 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1369 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1370 Alignment, Section.getEntrySize());
1371}
1372
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001373void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1374 const MCAsmLayout &Layout) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001375 GroupMapTy GroupMap;
1376 CreateGroupSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap);
1377
Rafael Espindolabab2a802010-11-10 21:51:05 +00001378 SectionIndexMapTy SectionIndexMap;
1379
1380 ComputeIndexMap(Asm, SectionIndexMap);
1381
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001382 // Compute symbol table information.
Rafael Espindolabab2a802010-11-10 21:51:05 +00001383 ComputeSymbolTable(Asm, SectionIndexMap);
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001384
Matt Fleming3565a062010-08-16 18:57:57 +00001385 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001386 const_cast<MCAsmLayout&>(Layout),
1387 SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +00001388
Rafael Espindola1d739a02010-11-10 22:34:07 +00001389 // Update to include the metadata sections.
1390 ComputeIndexMap(Asm, SectionIndexMap);
1391
Matt Fleming3565a062010-08-16 18:57:57 +00001392 // Add 1 for the null section.
1393 unsigned NumSections = Asm.size() + 1;
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001394 uint64_t NaturalAlignment = Is64Bit ? 8 : 4;
1395 uint64_t HeaderSize = Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr);
1396 uint64_t FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001397
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001398 std::vector<const MCSectionELF*> Sections;
1399 Sections.resize(NumSections);
1400
1401 for (SectionIndexMapTy::const_iterator i=
1402 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1403 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
1404 Sections[p.second] = p.first;
1405 }
1406
1407 for (unsigned i = 1; i < NumSections; ++i) {
1408 const MCSectionELF &Section = *Sections[i];
1409 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001410
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001411 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1412
Matt Fleming3565a062010-08-16 18:57:57 +00001413 // Get the size of the section in the output file (including padding).
1414 uint64_t Size = Layout.getSectionFileSize(&SD);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001415
1416 FileOff += Size;
Matt Fleming3565a062010-08-16 18:57:57 +00001417 }
1418
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001419 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1420
Matt Fleming3565a062010-08-16 18:57:57 +00001421 // Write out the ELF header ...
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001422 WriteHeader(FileOff - HeaderSize, NumSections);
1423
1424 FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001425
1426 // ... then all of the sections ...
1427 DenseMap<const MCSection*, uint64_t> SectionOffsetMap;
1428
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001429 for (unsigned i = 1; i < NumSections; ++i) {
1430 const MCSectionELF &Section = *Sections[i];
1431 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001432
1433 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1434 WriteZeros(Padding);
1435 FileOff += Padding;
1436
Matt Fleming3565a062010-08-16 18:57:57 +00001437 // Remember the offset into the file for this section.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001438 SectionOffsetMap[&Section] = FileOff;
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001439
Matt Fleming3565a062010-08-16 18:57:57 +00001440 FileOff += Layout.getSectionFileSize(&SD);
1441
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001442 Asm.WriteSectionData(&SD, Layout, this);
Matt Fleming3565a062010-08-16 18:57:57 +00001443 }
1444
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001445 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1446 WriteZeros(Padding);
1447 FileOff += Padding;
1448
Matt Fleming3565a062010-08-16 18:57:57 +00001449 // ... and then the section header table.
1450 // Should we align the section header table?
1451 //
1452 // Null section first.
Rafael Espindola7be2c332010-10-31 00:16:26 +00001453 uint64_t FirstSectionSize =
1454 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1455 uint32_t FirstSectionLink =
1456 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1457 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming3565a062010-08-16 18:57:57 +00001458
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001459 for (unsigned i = 1; i < NumSections; ++i) {
1460 const MCSectionELF &Section = *Sections[i];
1461 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1462 uint32_t GroupSymbolIndex;
1463 if (Section.getType() != ELF::SHT_GROUP)
1464 GroupSymbolIndex = 0;
1465 else
1466 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm, GroupMap[&Section]);
Matt Fleming3565a062010-08-16 18:57:57 +00001467
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001468 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
1469 SectionOffsetMap[&Section], Layout.getSectionSize(&SD),
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001470 SD.getAlignment(), Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001471 }
1472}
1473
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001474MCObjectWriter *llvm::createELFObjectWriter(raw_ostream &OS,
1475 bool Is64Bit,
1476 Triple::OSType OSType,
1477 uint16_t EMachine,
1478 bool IsLittleEndian,
1479 bool HasRelocationAddend) {
1480 return new ELFObjectWriter(OS, Is64Bit, IsLittleEndian, EMachine,
1481 HasRelocationAddend, OSType);
Matt Fleming3565a062010-08-16 18:57:57 +00001482}