blob: e019e185272856dd51c3fe1b1b65c4e746170ec0 [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 Espindola8f413fa2010-10-05 15:11:03 +0000162 int NumRegularSections;
163
Rafael Espindola5c77c162010-10-05 15:48:37 +0000164 bool NeedsGOT;
165
Rafael Espindola7be2c332010-10-31 00:16:26 +0000166 bool NeedsSymtabShndx;
167
Matt Fleming3565a062010-08-16 18:57:57 +0000168 unsigned Is64Bit : 1;
169
170 bool HasRelocationAddend;
171
Roman Divacky5baf79e2010-09-09 17:57:50 +0000172 Triple::OSType OSType;
173
Wesley Peckeecb8582010-10-22 15:52:49 +0000174 uint16_t EMachine;
175
Matt Fleming3565a062010-08-16 18:57:57 +0000176 // This holds the symbol table index of the last local symbol.
177 unsigned LastLocalSymbolIndex;
178 // This holds the .strtab section index.
179 unsigned StringTableIndex;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000180 // This holds the .symtab section index.
181 unsigned SymbolTableIndex;
Matt Fleming3565a062010-08-16 18:57:57 +0000182
183 unsigned ShstrtabIndex;
184
185 public:
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000186 ELFObjectWriter(raw_ostream &_OS, bool _Is64Bit, bool IsLittleEndian,
187 uint16_t _EMachine, bool _HasRelAddend,
188 Triple::OSType _OSType)
189 : MCObjectWriter(_OS, IsLittleEndian),
190 NeedsGOT(false), NeedsSymtabShndx(false),
Roman Divacky5baf79e2010-09-09 17:57:50 +0000191 Is64Bit(_Is64Bit), HasRelocationAddend(_HasRelAddend),
Wesley Peckeecb8582010-10-22 15:52:49 +0000192 OSType(_OSType), EMachine(_EMachine) {
Matt Fleming3565a062010-08-16 18:57:57 +0000193 }
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000194
Matt Fleming3565a062010-08-16 18:57:57 +0000195 void WriteWord(uint64_t W) {
Chris Lattnerb188a372010-08-28 03:21:03 +0000196 if (Is64Bit)
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000197 Write64(W);
Chris Lattnerb188a372010-08-28 03:21:03 +0000198 else
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000199 Write32(W);
Matt Fleming3565a062010-08-16 18:57:57 +0000200 }
201
Matt Fleming3565a062010-08-16 18:57:57 +0000202 void StringLE16(char *buf, uint16_t Value) {
203 buf[0] = char(Value >> 0);
204 buf[1] = char(Value >> 8);
205 }
206
207 void StringLE32(char *buf, uint32_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000208 StringLE16(buf, uint16_t(Value >> 0));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000209 StringLE16(buf + 2, uint16_t(Value >> 16));
Matt Fleming3565a062010-08-16 18:57:57 +0000210 }
211
212 void StringLE64(char *buf, uint64_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000213 StringLE32(buf, uint32_t(Value >> 0));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000214 StringLE32(buf + 4, uint32_t(Value >> 32));
Matt Fleming3565a062010-08-16 18:57:57 +0000215 }
216
217 void StringBE16(char *buf ,uint16_t Value) {
218 buf[0] = char(Value >> 8);
219 buf[1] = char(Value >> 0);
220 }
221
222 void StringBE32(char *buf, uint32_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000223 StringBE16(buf, uint16_t(Value >> 16));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000224 StringBE16(buf + 2, uint16_t(Value >> 0));
Matt Fleming3565a062010-08-16 18:57:57 +0000225 }
226
227 void StringBE64(char *buf, uint64_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000228 StringBE32(buf, uint32_t(Value >> 32));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000229 StringBE32(buf + 4, uint32_t(Value >> 0));
Matt Fleming3565a062010-08-16 18:57:57 +0000230 }
231
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000232 void String8(MCDataFragment &F, uint8_t Value) {
233 char buf[1];
234 buf[0] = Value;
235 F.getContents() += StringRef(buf, 1);
236 }
237
238 void String16(MCDataFragment &F, uint16_t Value) {
239 char buf[2];
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000240 if (isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000241 StringLE16(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000242 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000243 StringBE16(buf, Value);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000244 F.getContents() += StringRef(buf, 2);
Matt Fleming3565a062010-08-16 18:57:57 +0000245 }
246
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000247 void String32(MCDataFragment &F, uint32_t Value) {
248 char buf[4];
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000249 if (isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000250 StringLE32(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000251 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000252 StringBE32(buf, Value);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000253 F.getContents() += StringRef(buf, 4);
Matt Fleming3565a062010-08-16 18:57:57 +0000254 }
255
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000256 void String64(MCDataFragment &F, uint64_t Value) {
257 char buf[8];
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000258 if (isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000259 StringLE64(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000260 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000261 StringBE64(buf, Value);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000262 F.getContents() += StringRef(buf, 8);
Matt Fleming3565a062010-08-16 18:57:57 +0000263 }
264
265 void WriteHeader(uint64_t SectionDataSize, unsigned NumberOfSections);
266
Rafael Espindola7be2c332010-10-31 00:16:26 +0000267 void WriteSymbolEntry(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
268 uint64_t name, uint8_t info,
Matt Fleming3565a062010-08-16 18:57:57 +0000269 uint64_t value, uint64_t size,
Rafael Espindola7be2c332010-10-31 00:16:26 +0000270 uint8_t other, uint32_t shndx,
271 bool Reserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000272
Rafael Espindola7be2c332010-10-31 00:16:26 +0000273 void WriteSymbol(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
274 ELFSymbolData &MSD,
Matt Fleming3565a062010-08-16 18:57:57 +0000275 const MCAsmLayout &Layout);
276
Rafael Espindolabab2a802010-11-10 21:51:05 +0000277 typedef DenseMap<const MCSectionELF*, uint32_t> SectionIndexMapTy;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000278 void WriteSymbolTable(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
279 const MCAssembler &Asm,
Rafael Espindola71859c62010-09-16 19:46:31 +0000280 const MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000281 const SectionIndexMapTy &SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +0000282
283 void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
284 const MCFragment *Fragment, const MCFixup &Fixup,
285 MCValue Target, uint64_t &FixedValue);
286
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000287 uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm,
288 const MCSymbol *S);
Matt Fleming3565a062010-08-16 18:57:57 +0000289
290 /// ComputeSymbolTable - Compute the symbol table data
291 ///
292 /// \param StringTable [out] - The string table data.
293 /// \param StringIndexMap [out] - Map from symbol names to offsets in the
294 /// string table.
Rafael Espindolabab2a802010-11-10 21:51:05 +0000295 void ComputeSymbolTable(MCAssembler &Asm,
296 const SectionIndexMapTy &SectionIndexMap);
297
298 void ComputeIndexMap(MCAssembler &Asm,
299 SectionIndexMapTy &SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +0000300
301 void WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
302 const MCSectionData &SD);
303
304 void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout) {
305 for (MCAssembler::const_iterator it = Asm.begin(),
306 ie = Asm.end(); it != ie; ++it) {
307 WriteRelocation(Asm, Layout, *it);
308 }
309 }
310
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000311 void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout,
312 const SectionIndexMapTy &SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +0000313
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000314 // Map from a group section to the signature symbol
315 typedef DenseMap<const MCSectionELF*, const MCSymbol*> GroupMapTy;
316 void CreateGroupSections(MCAssembler &Asm, MCAsmLayout &Layout,
317 GroupMapTy &GroupMap);
318
Rafael Espindola88182132010-10-27 15:18:17 +0000319 void ExecutePostLayoutBinding(MCAssembler &Asm);
Matt Fleming3565a062010-08-16 18:57:57 +0000320
321 void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
322 uint64_t Address, uint64_t Offset,
323 uint64_t Size, uint32_t Link, uint32_t Info,
324 uint64_t Alignment, uint64_t EntrySize);
325
326 void WriteRelocationsFragment(const MCAssembler &Asm, MCDataFragment *F,
327 const MCSectionData *SD);
328
Rafael Espindola70703872010-09-30 02:22:20 +0000329 bool IsFixupFullyResolved(const MCAssembler &Asm,
330 const MCValue Target,
331 bool IsPCRel,
332 const MCFragment *DF) const;
333
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000334 void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000335 void WriteSection(MCAssembler &Asm,
336 const SectionIndexMapTy &SectionIndexMap,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000337 uint32_t GroupSymbolIndex,
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000338 uint64_t Offset, uint64_t Size, uint64_t Alignment,
339 const MCSectionELF &Section);
Matt Fleming3565a062010-08-16 18:57:57 +0000340 };
341
342}
343
344// Emit the ELF header.
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000345void ELFObjectWriter::WriteHeader(uint64_t SectionDataSize,
346 unsigned NumberOfSections) {
Matt Fleming3565a062010-08-16 18:57:57 +0000347 // ELF Header
348 // ----------
349 //
350 // Note
351 // ----
352 // emitWord method behaves differently for ELF32 and ELF64, writing
353 // 4 bytes in the former and 8 in the latter.
354
355 Write8(0x7f); // e_ident[EI_MAG0]
356 Write8('E'); // e_ident[EI_MAG1]
357 Write8('L'); // e_ident[EI_MAG2]
358 Write8('F'); // e_ident[EI_MAG3]
359
360 Write8(Is64Bit ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
361
362 // e_ident[EI_DATA]
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000363 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
Matt Fleming3565a062010-08-16 18:57:57 +0000364
365 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky5baf79e2010-09-09 17:57:50 +0000366 // e_ident[EI_OSABI]
367 switch (OSType) {
368 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
369 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
370 default: Write8(ELF::ELFOSABI_NONE); break;
371 }
Matt Fleming3565a062010-08-16 18:57:57 +0000372 Write8(0); // e_ident[EI_ABIVERSION]
373
374 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
375
376 Write16(ELF::ET_REL); // e_type
377
Wesley Peckeecb8582010-10-22 15:52:49 +0000378 Write16(EMachine); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +0000379
380 Write32(ELF::EV_CURRENT); // e_version
381 WriteWord(0); // e_entry, no entry point in .o file
382 WriteWord(0); // e_phoff, no program header for .o
Benjamin Kramereb976772010-08-17 17:02:29 +0000383 WriteWord(SectionDataSize + (Is64Bit ? sizeof(ELF::Elf64_Ehdr) :
384 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000385
386 // FIXME: Make this configurable.
387 Write32(0); // e_flags = whatever the target wants
388
389 // e_ehsize = ELF header size
390 Write16(Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
391
392 Write16(0); // e_phentsize = prog header entry size
393 Write16(0); // e_phnum = # prog header entries = 0
394
395 // e_shentsize = Section header entry size
396 Write16(Is64Bit ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
397
398 // e_shnum = # of section header ents
Rafael Espindola7be2c332010-10-31 00:16:26 +0000399 if (NumberOfSections >= ELF::SHN_LORESERVE)
400 Write16(0);
401 else
402 Write16(NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000403
404 // e_shstrndx = Section # of '.shstrtab'
Rafael Espindola7be2c332010-10-31 00:16:26 +0000405 if (NumberOfSections >= ELF::SHN_LORESERVE)
406 Write16(ELF::SHN_XINDEX);
407 else
408 Write16(ShstrtabIndex);
Matt Fleming3565a062010-08-16 18:57:57 +0000409}
410
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000411void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
412 MCDataFragment *ShndxF,
413 uint64_t name,
414 uint8_t info, uint64_t value,
415 uint64_t size, uint8_t other,
416 uint32_t shndx,
417 bool Reserved) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000418 if (ShndxF) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000419 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000420 String32(*ShndxF, shndx);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000421 else
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000422 String32(*ShndxF, 0);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000423 }
424
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000425 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
426 uint16_t(ELF::SHN_XINDEX) : shndx;
427
Matt Fleming3565a062010-08-16 18:57:57 +0000428 if (Is64Bit) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000429 String32(*SymtabF, name); // st_name
430 String8(*SymtabF, info); // st_info
431 String8(*SymtabF, other); // st_other
432 String16(*SymtabF, Index); // st_shndx
433 String64(*SymtabF, value); // st_value
434 String64(*SymtabF, size); // st_size
Matt Fleming3565a062010-08-16 18:57:57 +0000435 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000436 String32(*SymtabF, name); // st_name
437 String32(*SymtabF, value); // st_value
438 String32(*SymtabF, size); // st_size
439 String8(*SymtabF, info); // st_info
440 String8(*SymtabF, other); // st_other
441 String16(*SymtabF, Index); // st_shndx
Matt Fleming3565a062010-08-16 18:57:57 +0000442 }
443}
444
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000445static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout) {
446 if (Data.isCommon() && Data.isExternal())
447 return Data.getCommonAlignment();
448
449 const MCSymbol &Symbol = Data.getSymbol();
450 if (!Symbol.isInSection())
451 return 0;
452
Rafael Espindola1973d432010-10-28 19:39:57 +0000453 if (MCFragment *FF = Data.getFragment())
454 return Layout.getSymbolAddress(&Data) -
455 Layout.getSectionAddress(FF->getParent());
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000456
457 return 0;
458}
459
Rafael Espindolade89b012010-10-15 18:25:33 +0000460static const MCSymbol &AliasedSymbol(const MCSymbol &Symbol) {
461 const MCSymbol *S = &Symbol;
462 while (S->isVariable()) {
463 const MCExpr *Value = S->getVariableValue();
Rafael Espindola9302bd62010-11-11 17:24:43 +0000464 MCExpr::ExprKind Kind = Value->getKind();
465 switch (Kind) {
466 case MCExpr::SymbolRef: {
467 const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr*>(Value);
468 S = &Ref->getSymbol();
469 break;
470 }
471 case MCExpr::Target: {
472 const MCTargetExpr *TExp = static_cast<const MCTargetExpr*>(Value);
473 MCValue Res;
474 TExp->EvaluateAsRelocatableImpl(Res, NULL);
475 S = &Res.getSymA()->getSymbol();
476 break;
477 }
478 default:
Rafael Espindolaa6866962010-10-27 14:44:52 +0000479 return *S;
Rafael Espindola9302bd62010-11-11 17:24:43 +0000480 }
Rafael Espindolade89b012010-10-15 18:25:33 +0000481 }
482 return *S;
483}
484
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000485void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm) {
Rafael Espindola88182132010-10-27 15:18:17 +0000486 // The presence of symbol versions causes undefined symbols and
487 // versions declared with @@@ to be renamed.
488
489 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
490 ie = Asm.symbol_end(); it != ie; ++it) {
491 const MCSymbol &Alias = it->getSymbol();
Rafael Espindola88182132010-10-27 15:18:17 +0000492 const MCSymbol &Symbol = AliasedSymbol(Alias);
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000493 MCSymbolData &SD = Asm.getSymbolData(Symbol);
494
495 // Undefined symbols are global, but this is the first place we
496 // are able to set it.
497 if (Symbol.isUndefined() && !Symbol.isVariable()) {
498 if (GetBinding(SD) == ELF::STB_LOCAL) {
499 SetBinding(SD, ELF::STB_GLOBAL);
500 SetBinding(*it, ELF::STB_GLOBAL);
501 }
502 }
503
504 // Not an alias.
505 if (&Symbol == &Alias)
506 continue;
507
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000508 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000509 size_t Pos = AliasName.find('@');
510 if (Pos == StringRef::npos)
511 continue;
512
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000513 // Aliases defined with .symvar copy the binding from the symbol they alias.
514 // This is the first place we are able to copy this information.
515 it->setExternal(SD.isExternal());
516 SetBinding(*it, GetBinding(SD));
517
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000518 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000519 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
520 continue;
521
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000522 // FIXME: produce a better error message.
523 if (Symbol.isUndefined() && Rest.startswith("@@") &&
524 !Rest.startswith("@@@"))
525 report_fatal_error("A @@ version cannot be undefined");
526
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000527 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000528 }
529}
530
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000531void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
532 MCDataFragment *ShndxF,
533 ELFSymbolData &MSD,
534 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000535 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000536 MCSymbolData &Data =
537 Layout.getAssembler().getSymbolData(AliasedSymbol(OrigData.getSymbol()));
Rafael Espindola152c1062010-10-06 21:02:29 +0000538
Rafael Espindola7be2c332010-10-31 00:16:26 +0000539 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
540 Data.getSymbol().isVariable();
541
Rafael Espindola152c1062010-10-06 21:02:29 +0000542 uint8_t Binding = GetBinding(OrigData);
543 uint8_t Visibility = GetVisibility(OrigData);
544 uint8_t Type = GetType(Data);
545
546 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
547 uint8_t Other = Visibility;
548
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000549 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000550 uint64_t Size = 0;
551 const MCExpr *ESize;
552
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000553 assert(!(Data.isCommon() && !Data.isExternal()));
554
Matt Fleming3565a062010-08-16 18:57:57 +0000555 ESize = Data.getSize();
556 if (Data.getSize()) {
557 MCValue Res;
558 if (ESize->getKind() == MCExpr::Binary) {
559 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(ESize);
560
561 if (BE->EvaluateAsRelocatable(Res, &Layout)) {
Benjamin Kramer24f12062010-10-17 07:38:40 +0000562 assert(!Res.getSymA() || !Res.getSymA()->getSymbol().isDefined());
563 assert(!Res.getSymB() || !Res.getSymB()->getSymbol().isDefined());
Rafael Espindolaf230df92010-10-16 18:23:53 +0000564 Size = Res.getConstant();
Matt Fleming3565a062010-08-16 18:57:57 +0000565 }
566 } else if (ESize->getKind() == MCExpr::Constant) {
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000567 Size = static_cast<const MCConstantExpr *>(ESize)->getValue();
Matt Fleming3565a062010-08-16 18:57:57 +0000568 } else {
569 assert(0 && "Unsupported size expression");
570 }
571 }
572
573 // Write out the symbol table entry
Rafael Espindola7be2c332010-10-31 00:16:26 +0000574 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
575 Size, Other, MSD.SectionIndex, IsReserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000576}
577
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000578void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
579 MCDataFragment *ShndxF,
580 const MCAssembler &Asm,
581 const MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000582 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000583 // The string table must be emitted first because we need the index
584 // into the string table for all the symbol names.
585 assert(StringTable.size() && "Missing string table");
586
587 // FIXME: Make sure the start of the symbol table is aligned.
588
589 // The first entry is the undefined symbol entry.
Rafael Espindola7be2c332010-10-31 00:16:26 +0000590 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
Matt Fleming3565a062010-08-16 18:57:57 +0000591
592 // Write the symbol table entries.
593 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
594 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
595 ELFSymbolData &MSD = LocalSymbolData[i];
Rafael Espindola7be2c332010-10-31 00:16:26 +0000596 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000597 }
598
Rafael Espindola71859c62010-09-16 19:46:31 +0000599 // Write out a symbol table entry for each regular section.
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000600 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
601 ++i) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000602 const MCSectionELF &Section =
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000603 static_cast<const MCSectionELF&>(i->getSection());
604 if (Section.getType() == ELF::SHT_RELA ||
605 Section.getType() == ELF::SHT_REL ||
606 Section.getType() == ELF::SHT_STRTAB ||
607 Section.getType() == ELF::SHT_SYMTAB)
Eli Friedmana44fa242010-08-16 21:17:09 +0000608 continue;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000609 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000610 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section), false);
Eli Friedmana44fa242010-08-16 21:17:09 +0000611 LastLocalSymbolIndex++;
612 }
Matt Fleming3565a062010-08-16 18:57:57 +0000613
614 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
615 ELFSymbolData &MSD = ExternalSymbolData[i];
616 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000617 assert(((Data.getFlags() & ELF_STB_Global) ||
618 (Data.getFlags() & ELF_STB_Weak)) &&
619 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000620 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000621 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000622 LastLocalSymbolIndex++;
623 }
624
625 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
626 ELFSymbolData &MSD = UndefinedSymbolData[i];
627 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000628 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000629 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000630 LastLocalSymbolIndex++;
631 }
632}
633
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000634static bool ShouldRelocOnSymbol(const MCSymbolData &SD,
Rafael Espindola3729d002010-10-05 23:57:26 +0000635 const MCValue &Target,
636 const MCFragment &F) {
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000637 const MCSymbol &Symbol = SD.getSymbol();
638 if (Symbol.isUndefined())
639 return true;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000640
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000641 const MCSectionELF &Section =
642 static_cast<const MCSectionELF&>(Symbol.getSection());
643
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000644 if (SD.isExternal())
645 return true;
646
Rafael Espindola8cecf252010-10-06 16:23:36 +0000647 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000648 const MCSectionELF &Sec2 =
649 static_cast<const MCSectionELF&>(F.getParent()->getSection());
650
Rafael Espindolace2d3c52010-10-18 20:25:33 +0000651 if (Section.getKind().isBSS())
652 return false;
653
Rafael Espindola8cecf252010-10-06 16:23:36 +0000654 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000655 (Kind == MCSymbolRefExpr::VK_PLT ||
656 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
657 Kind == MCSymbolRefExpr::VK_GOTOFF))
Rafael Espindola3729d002010-10-05 23:57:26 +0000658 return true;
659
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000660 if (Section.getFlags() & MCSectionELF::SHF_MERGE)
661 return Target.getConstant() != 0;
662
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000663 return false;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000664}
665
Benjamin Kramere5b57342010-08-17 18:20:28 +0000666// FIXME: this is currently X86/X86_64 only
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000667void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
668 const MCAsmLayout &Layout,
669 const MCFragment *Fragment,
670 const MCFixup &Fixup,
671 MCValue Target,
672 uint64_t &FixedValue) {
Matt Fleming3565a062010-08-16 18:57:57 +0000673 int64_t Addend = 0;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000674 int Index = 0;
Benjamin Kramer95c602a2010-08-27 10:38:39 +0000675 int64_t Value = Target.getConstant();
Rafael Espindola03f1b742010-11-11 16:48:11 +0000676 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
677 const MCSymbol &ASymbol = AliasedSymbol(Symbol);
678 const MCSymbol *RenamedP = Renames.lookup(&Symbol);
679 if (!RenamedP)
680 RenamedP = &ASymbol;
681 const MCSymbol &Renamed = *RenamedP;
Matt Fleming3565a062010-08-16 18:57:57 +0000682
Rafael Espindola00074892010-09-17 22:34:41 +0000683 bool IsPCRel = isFixupKindX86PCRel(Fixup.getKind());
Benjamin Kramer81cfb852010-08-17 19:45:05 +0000684 if (!Target.isAbsolute()) {
Rafael Espindola03f1b742010-11-11 16:48:11 +0000685 MCSymbolData &SD = Asm.getSymbolData(Symbol);
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000686 MCFragment *F = SD.getFragment();
Matt Fleming3565a062010-08-16 18:57:57 +0000687
Rafael Espindola9d8b7552010-10-03 00:46:57 +0000688 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
689 const MCSymbol &SymbolB = RefB->getSymbol();
690 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
691 IsPCRel = true;
Rafael Espindola55fb1022010-10-04 15:59:01 +0000692 MCSectionData *Sec = Fragment->getParent();
693
694 // Offset of the symbol in the section
695 int64_t a = Layout.getSymbolAddress(&SDB) - Layout.getSectionAddress(Sec);
696
697 // Ofeset of the relocation in the section
698 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
699 Value += b - a;
Rafael Espindola9d8b7552010-10-03 00:46:57 +0000700 }
701
Rafael Espindola3729d002010-10-05 23:57:26 +0000702 bool RelocOnSymbol = ShouldRelocOnSymbol(SD, Target, *Fragment);
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000703 if (!RelocOnSymbol) {
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000704 Index = F->getParent()->getOrdinal();
Rafael Espindolaa6489182010-09-24 21:19:03 +0000705
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000706 MCSectionData *FSD = F->getParent();
707 // Offset of the symbol in the section
708 Value += Layout.getSymbolAddress(&SD) - Layout.getSectionAddress(FSD);
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000709 } else {
Rafael Espindola03f1b742010-11-11 16:48:11 +0000710 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
711 WeakrefUsedInReloc.insert(&Renamed);
712 else
713 UsedInReloc.insert(&Renamed);
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000714 Index = -1;
715 }
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000716 Addend = Value;
717 // Compensate for the addend on i386.
718 if (Is64Bit)
719 Value = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000720 }
721
Benjamin Kramer95c602a2010-08-27 10:38:39 +0000722 FixedValue = Value;
723
Matt Fleming3565a062010-08-16 18:57:57 +0000724 // determine the type of the relocation
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000725
726 MCSymbolRefExpr::VariantKind Modifier = Target.getSymA()->getKind();
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000727 unsigned Type;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000728 if (Is64Bit) {
729 if (IsPCRel) {
Rafael Espindola92bf6682010-10-04 19:04:13 +0000730 switch (Modifier) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000731 default:
732 llvm_unreachable("Unimplemented");
Rafael Espindola92bf6682010-10-04 19:04:13 +0000733 case MCSymbolRefExpr::VK_None:
734 Type = ELF::R_X86_64_PC32;
735 break;
736 case MCSymbolRefExpr::VK_PLT:
737 Type = ELF::R_X86_64_PLT32;
738 break;
Rafael Espindolaa0a2f872010-10-28 14:22:44 +0000739 case MCSymbolRefExpr::VK_GOTPCREL:
Rafael Espindola607d1f62010-10-04 19:51:39 +0000740 Type = ELF::R_X86_64_GOTPCREL;
741 break;
Rafael Espindolabc82d8b2010-10-27 20:28:07 +0000742 case MCSymbolRefExpr::VK_GOTTPOFF:
743 Type = ELF::R_X86_64_GOTTPOFF;
744 break;
745 case MCSymbolRefExpr::VK_TLSGD:
746 Type = ELF::R_X86_64_TLSGD;
747 break;
Rafael Espindolab4d17212010-10-28 15:02:40 +0000748 case MCSymbolRefExpr::VK_TLSLD:
749 Type = ELF::R_X86_64_TLSLD;
750 break;
Rafael Espindola92bf6682010-10-04 19:04:13 +0000751 }
Benjamin Kramere5b57342010-08-17 18:20:28 +0000752 } else {
753 switch ((unsigned)Fixup.getKind()) {
754 default: llvm_unreachable("invalid fixup kind!");
755 case FK_Data_8: Type = ELF::R_X86_64_64; break;
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000756 case X86::reloc_signed_4byte:
Benjamin Kramere5b57342010-08-17 18:20:28 +0000757 case X86::reloc_pcrel_4byte:
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000758 assert(isInt<32>(Target.getConstant()));
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000759 switch (Modifier) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000760 default:
761 llvm_unreachable("Unimplemented");
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000762 case MCSymbolRefExpr::VK_None:
763 Type = ELF::R_X86_64_32S;
764 break;
765 case MCSymbolRefExpr::VK_GOT:
766 Type = ELF::R_X86_64_GOT32;
767 break;
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000768 case MCSymbolRefExpr::VK_GOTPCREL:
Rafael Espindola8cecf252010-10-06 16:23:36 +0000769 Type = ELF::R_X86_64_GOTPCREL;
770 break;
Rafael Espindolabc82d8b2010-10-27 20:28:07 +0000771 case MCSymbolRefExpr::VK_TPOFF:
772 Type = ELF::R_X86_64_TPOFF32;
773 break;
Rafael Espindolaaa8f1f02010-10-28 15:11:03 +0000774 case MCSymbolRefExpr::VK_DTPOFF:
775 Type = ELF::R_X86_64_DTPOFF32;
776 break;
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000777 }
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000778 break;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000779 case FK_Data_4:
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000780 Type = ELF::R_X86_64_32;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000781 break;
782 case FK_Data_2: Type = ELF::R_X86_64_16; break;
783 case X86::reloc_pcrel_1byte:
784 case FK_Data_1: Type = ELF::R_X86_64_8; break;
785 }
786 }
Matt Fleming3565a062010-08-16 18:57:57 +0000787 } else {
Benjamin Kramere5b57342010-08-17 18:20:28 +0000788 if (IsPCRel) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000789 switch (Modifier) {
790 default:
Rafael Espindola24dc9ec2010-10-18 19:33:01 +0000791 llvm_unreachable("Unimplemented");
792 case MCSymbolRefExpr::VK_None:
Rafael Espindola9baee3b2010-10-18 18:03:28 +0000793 Type = ELF::R_386_PC32;
Rafael Espindola9baee3b2010-10-18 18:03:28 +0000794 break;
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000795 case MCSymbolRefExpr::VK_PLT:
796 Type = ELF::R_386_PLT32;
797 break;
798 }
Benjamin Kramere5b57342010-08-17 18:20:28 +0000799 } else {
800 switch ((unsigned)Fixup.getKind()) {
801 default: llvm_unreachable("invalid fixup kind!");
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000802
Rafael Espindola24ba4f72010-10-24 17:35:42 +0000803 case X86::reloc_global_offset_table:
804 Type = ELF::R_386_GOTPC;
805 break;
806
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000807 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
808 // instead?
809 case X86::reloc_signed_4byte:
Benjamin Kramere5b57342010-08-17 18:20:28 +0000810 case X86::reloc_pcrel_4byte:
Rafael Espindolabd701182010-10-19 19:31:37 +0000811 case FK_Data_4:
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000812 switch (Modifier) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000813 default:
814 llvm_unreachable("Unimplemented");
Rafael Espindolabd701182010-10-19 19:31:37 +0000815 case MCSymbolRefExpr::VK_None:
Rafael Espindola24ba4f72010-10-24 17:35:42 +0000816 Type = ELF::R_386_32;
Rafael Espindolabd701182010-10-19 19:31:37 +0000817 break;
Rafael Espindolaeada3042010-10-18 20:47:21 +0000818 case MCSymbolRefExpr::VK_GOT:
819 Type = ELF::R_386_GOT32;
820 break;
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000821 case MCSymbolRefExpr::VK_GOTOFF:
822 Type = ELF::R_386_GOTOFF;
823 break;
Rafael Espindola3cede2d2010-10-27 21:23:52 +0000824 case MCSymbolRefExpr::VK_TLSGD:
825 Type = ELF::R_386_TLS_GD;
826 break;
827 case MCSymbolRefExpr::VK_TPOFF:
828 Type = ELF::R_386_TLS_LE_32;
829 break;
830 case MCSymbolRefExpr::VK_INDNTPOFF:
831 Type = ELF::R_386_TLS_IE;
832 break;
833 case MCSymbolRefExpr::VK_NTPOFF:
834 Type = ELF::R_386_TLS_LE;
835 break;
Rafael Espindolaa0a2f872010-10-28 14:22:44 +0000836 case MCSymbolRefExpr::VK_GOTNTPOFF:
837 Type = ELF::R_386_TLS_GOTIE;
838 break;
Rafael Espindolaa264f722010-10-28 14:37:09 +0000839 case MCSymbolRefExpr::VK_TLSLDM:
840 Type = ELF::R_386_TLS_LDM;
841 break;
Rafael Espindola0cf15d62010-10-28 14:48:59 +0000842 case MCSymbolRefExpr::VK_DTPOFF:
843 Type = ELF::R_386_TLS_LDO_32;
844 break;
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000845 }
846 break;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000847 case FK_Data_2: Type = ELF::R_386_16; break;
848 case X86::reloc_pcrel_1byte:
849 case FK_Data_1: Type = ELF::R_386_8; break;
850 }
Matt Fleming3565a062010-08-16 18:57:57 +0000851 }
852 }
853
Rafael Espindolaa0a2f872010-10-28 14:22:44 +0000854 if (RelocNeedsGOT(Modifier))
Rafael Espindola5c77c162010-10-05 15:48:37 +0000855 NeedsGOT = true;
856
Benjamin Kramere5b57342010-08-17 18:20:28 +0000857 ELFRelocationEntry ERE;
858
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000859 ERE.Index = Index;
860 ERE.Type = Type;
Rafael Espindola03f1b742010-11-11 16:48:11 +0000861 ERE.Symbol = &Renamed;
Matt Fleming3565a062010-08-16 18:57:57 +0000862
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000863 ERE.r_offset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
Benjamin Kramere5b57342010-08-17 18:20:28 +0000864
Matt Fleming3565a062010-08-16 18:57:57 +0000865 if (HasRelocationAddend)
866 ERE.r_addend = Addend;
Benjamin Kramer172d7d62010-08-17 00:33:24 +0000867 else
868 ERE.r_addend = 0; // Silence compiler warning.
Matt Fleming3565a062010-08-16 18:57:57 +0000869
870 Relocations[Fragment->getParent()].push_back(ERE);
871}
872
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000873uint64_t
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000874ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
875 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000876 MCSymbolData &SD = Asm.getSymbolData(*S);
Eli Friedmanf8020a32010-08-16 19:15:06 +0000877
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000878 // Local symbol.
879 if (!SD.isExternal() && !S->isUndefined())
880 return SD.getIndex() + /* empty symbol */ 1;
881
882 // External or undefined symbol.
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000883 return SD.getIndex() + NumRegularSections + /* empty symbol */ 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000884}
885
Rafael Espindola737cd212010-10-05 18:01:23 +0000886static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
Rafael Espindola88182132010-10-27 15:18:17 +0000887 bool Used, bool Renamed) {
Rafael Espindola484291c2010-11-01 14:28:48 +0000888 if (Data.getFlags() & ELF_Other_Weakref)
889 return false;
890
Rafael Espindolabd701182010-10-19 19:31:37 +0000891 if (Used)
892 return true;
893
Rafael Espindola88182132010-10-27 15:18:17 +0000894 if (Renamed)
895 return false;
896
Rafael Espindola737cd212010-10-05 18:01:23 +0000897 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000898
Rafael Espindolad1798862010-10-29 23:09:31 +0000899 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
900 return true;
901
Rafael Espindolaa6866962010-10-27 14:44:52 +0000902 const MCSymbol &A = AliasedSymbol(Symbol);
Rafael Espindolad1798862010-10-29 23:09:31 +0000903 if (!A.isVariable() && A.isUndefined() && !Data.isCommon())
Rafael Espindolaa6866962010-10-27 14:44:52 +0000904 return false;
905
Rafael Espindola737cd212010-10-05 18:01:23 +0000906 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
907 return false;
908
Rafael Espindolabd701182010-10-19 19:31:37 +0000909 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000910 return false;
911
912 return true;
913}
914
915static bool isLocal(const MCSymbolData &Data) {
916 if (Data.isExternal())
917 return false;
918
919 const MCSymbol &Symbol = Data.getSymbol();
920 if (Symbol.isUndefined() && !Symbol.isVariable())
921 return false;
922
923 return true;
924}
925
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000926void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
927 SectionIndexMapTy &SectionIndexMap) {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000928 unsigned Index = 1;
929 for (MCAssembler::iterator it = Asm.begin(),
930 ie = Asm.end(); it != ie; ++it) {
931 const MCSectionELF &Section =
932 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000933 if (Section.getType() != ELF::SHT_GROUP)
934 continue;
935 SectionIndexMap[&Section] = Index++;
936 }
937
938 for (MCAssembler::iterator it = Asm.begin(),
939 ie = Asm.end(); it != ie; ++it) {
940 const MCSectionELF &Section =
941 static_cast<const MCSectionELF &>(it->getSection());
942 if (Section.getType() == ELF::SHT_GROUP)
943 continue;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000944 SectionIndexMap[&Section] = Index++;
945 }
946}
947
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000948void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindolabab2a802010-11-10 21:51:05 +0000949 const SectionIndexMapTy &SectionIndexMap) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000950 // FIXME: Is this the correct place to do this?
951 if (NeedsGOT) {
952 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
953 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
954 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
955 Data.setExternal(true);
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000956 SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindola5c77c162010-10-05 15:48:37 +0000957 }
958
Matt Fleming3565a062010-08-16 18:57:57 +0000959 // Build section lookup table.
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000960 NumRegularSections = Asm.size();
Matt Fleming3565a062010-08-16 18:57:57 +0000961
962 // Index 0 is always the empty string.
963 StringMap<uint64_t> StringIndexMap;
964 StringTable += '\x00';
965
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000966 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000967 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
968 ie = Asm.symbol_end(); it != ie; ++it) {
969 const MCSymbol &Symbol = it->getSymbol();
970
Rafael Espindola484291c2010-11-01 14:28:48 +0000971 bool Used = UsedInReloc.count(&Symbol);
972 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
973 if (!isInSymtab(Asm, *it, Used || WeakrefUsed,
Rafael Espindola88182132010-10-27 15:18:17 +0000974 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000975 continue;
976
Matt Fleming3565a062010-08-16 18:57:57 +0000977 ELFSymbolData MSD;
978 MSD.SymbolData = it;
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000979 bool Local = isLocal(*it);
Rafael Espindola88182132010-10-27 15:18:17 +0000980 const MCSymbol &RefSymbol = AliasedSymbol(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000981
Rafael Espindola484291c2010-11-01 14:28:48 +0000982 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
983 SetBinding(*it, ELF::STB_WEAK);
984
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000985 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000986 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000987 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000988 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000989 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000990 } else if (RefSymbol.isUndefined()) {
Matt Fleming3565a062010-08-16 18:57:57 +0000991 MSD.SectionIndex = ELF::SHN_UNDEF;
Matt Fleming3565a062010-08-16 18:57:57 +0000992 } else {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000993 const MCSectionELF &Section =
994 static_cast<const MCSectionELF&>(RefSymbol.getSection());
995 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000996 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
997 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +0000998 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000999 }
1000
Rafael Espindola88182132010-10-27 15:18:17 +00001001 // The @@@ in symbol version is replaced with @ in undefined symbols and
1002 // @@ in defined ones.
1003 StringRef Name = Symbol.getName();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +00001004 SmallString<32> Buf;
1005
Rafael Espindola88182132010-10-27 15:18:17 +00001006 size_t Pos = Name.find("@@@");
Rafael Espindola88182132010-10-27 15:18:17 +00001007 if (Pos != StringRef::npos) {
Benjamin Kramer1261a2f2010-11-12 19:26:04 +00001008 Buf += Name.substr(0, Pos);
1009 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
1010 Buf += Name.substr(Pos + Skip);
1011 Name = Buf;
Rafael Espindola88182132010-10-27 15:18:17 +00001012 }
1013
Benjamin Kramer1261a2f2010-11-12 19:26:04 +00001014 uint64_t &Entry = StringIndexMap[Name];
Rafael Espindolaa6866962010-10-27 14:44:52 +00001015 if (!Entry) {
1016 Entry = StringTable.size();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +00001017 StringTable += Name;
Rafael Espindolaa6866962010-10-27 14:44:52 +00001018 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +00001019 }
Rafael Espindolaa6866962010-10-27 14:44:52 +00001020 MSD.StringIndex = Entry;
1021 if (MSD.SectionIndex == ELF::SHN_UNDEF)
1022 UndefinedSymbolData.push_back(MSD);
1023 else if (Local)
1024 LocalSymbolData.push_back(MSD);
1025 else
1026 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +00001027 }
1028
1029 // Symbols are required to be in lexicographic order.
1030 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
1031 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
1032 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
1033
1034 // Set the symbol indices. Local symbols must come before all other
1035 // symbols with non-local bindings.
Rafael Espindolabab2a802010-11-10 21:51:05 +00001036 unsigned Index = 0;
Matt Fleming3565a062010-08-16 18:57:57 +00001037 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
1038 LocalSymbolData[i].SymbolData->setIndex(Index++);
1039 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
1040 ExternalSymbolData[i].SymbolData->setIndex(Index++);
1041 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
1042 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
1043}
1044
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001045void ELFObjectWriter::WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
1046 const MCSectionData &SD) {
Matt Fleming3565a062010-08-16 18:57:57 +00001047 if (!Relocations[&SD].empty()) {
1048 MCContext &Ctx = Asm.getContext();
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001049 const MCSectionELF *RelaSection;
Matt Fleming3565a062010-08-16 18:57:57 +00001050 const MCSectionELF &Section =
1051 static_cast<const MCSectionELF&>(SD.getSection());
1052
1053 const StringRef SectionName = Section.getSectionName();
Benjamin Kramer377a5722010-08-17 17:30:07 +00001054 std::string RelaSectionName = HasRelocationAddend ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +00001055 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +00001056
1057 unsigned EntrySize;
1058 if (HasRelocationAddend)
1059 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
1060 else
1061 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +00001062
Benjamin Kramer377a5722010-08-17 17:30:07 +00001063 RelaSection = Ctx.getELFSection(RelaSectionName, HasRelocationAddend ?
1064 ELF::SHT_RELA : ELF::SHT_REL, 0,
Matt Fleming3565a062010-08-16 18:57:57 +00001065 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001066 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001067
1068 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001069 RelaSD.setAlignment(Is64Bit ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +00001070
1071 MCDataFragment *F = new MCDataFragment(&RelaSD);
1072
1073 WriteRelocationsFragment(Asm, F, &SD);
1074
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001075 Asm.AddSectionToTheEnd(*this, RelaSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +00001076 }
1077}
1078
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001079void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
1080 uint64_t Flags, uint64_t Address,
1081 uint64_t Offset, uint64_t Size,
1082 uint32_t Link, uint32_t Info,
1083 uint64_t Alignment,
1084 uint64_t EntrySize) {
Matt Fleming3565a062010-08-16 18:57:57 +00001085 Write32(Name); // sh_name: index into string table
1086 Write32(Type); // sh_type
1087 WriteWord(Flags); // sh_flags
1088 WriteWord(Address); // sh_addr
1089 WriteWord(Offset); // sh_offset
1090 WriteWord(Size); // sh_size
1091 Write32(Link); // sh_link
1092 Write32(Info); // sh_info
1093 WriteWord(Alignment); // sh_addralign
1094 WriteWord(EntrySize); // sh_entsize
1095}
1096
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001097void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
1098 MCDataFragment *F,
1099 const MCSectionData *SD) {
Matt Fleming3565a062010-08-16 18:57:57 +00001100 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
1101 // sort by the r_offset just like gnu as does
1102 array_pod_sort(Relocs.begin(), Relocs.end());
1103
1104 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1105 ELFRelocationEntry entry = Relocs[e - i - 1];
1106
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001107 if (entry.Index < 0)
1108 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
1109 else
1110 entry.Index += LocalSymbolData.size() + 1;
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001111 if (Is64Bit) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001112 String64(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001113
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001114 struct ELF::Elf64_Rela ERE64;
1115 ERE64.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001116 String64(*F, ERE64.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001117
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001118 if (HasRelocationAddend)
1119 String64(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001120 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001121 String32(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001122
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001123 struct ELF::Elf32_Rela ERE32;
1124 ERE32.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001125 String32(*F, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001126
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001127 if (HasRelocationAddend)
1128 String32(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001129 }
Matt Fleming3565a062010-08-16 18:57:57 +00001130 }
1131}
1132
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001133void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
1134 MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001135 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +00001136 MCContext &Ctx = Asm.getContext();
1137 MCDataFragment *F;
1138
Matt Fleming3565a062010-08-16 18:57:57 +00001139 unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
1140
Rafael Espindola38738bf2010-09-22 19:04:41 +00001141 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001142 const MCSectionELF *ShstrtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001143 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001144 SectionKind::getReadOnly());
Rafael Espindola71859c62010-09-16 19:46:31 +00001145 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1146 ShstrtabSD.setAlignment(1);
1147 ShstrtabIndex = Asm.size();
1148
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001149 const MCSectionELF *SymtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001150 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1151 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001152 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001153 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Matt Fleming3565a062010-08-16 18:57:57 +00001154 SymtabSD.setAlignment(Is64Bit ? 8 : 4);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001155 SymbolTableIndex = Asm.size();
1156
1157 MCSectionData *SymtabShndxSD = NULL;
1158
1159 if (NeedsSymtabShndx) {
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001160 const MCSectionELF *SymtabShndxSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001161 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001162 SectionKind::getReadOnly(), 4, "");
Rafael Espindola7be2c332010-10-31 00:16:26 +00001163 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
1164 SymtabShndxSD->setAlignment(4);
1165 }
Matt Fleming3565a062010-08-16 18:57:57 +00001166
Matt Fleming3565a062010-08-16 18:57:57 +00001167 const MCSection *StrtabSection;
1168 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001169 SectionKind::getReadOnly());
Matt Fleming3565a062010-08-16 18:57:57 +00001170 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1171 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +00001172 StringTableIndex = Asm.size();
1173
Rafael Espindolac3c413f2010-09-27 22:04:54 +00001174 WriteRelocations(Asm, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001175
1176 // Symbol table
1177 F = new MCDataFragment(&SymtabSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001178 MCDataFragment *ShndxF = NULL;
1179 if (NeedsSymtabShndx) {
1180 ShndxF = new MCDataFragment(SymtabShndxSD);
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001181 Asm.AddSectionToTheEnd(*this, *SymtabShndxSD, Layout);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001182 }
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001183 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001184 Asm.AddSectionToTheEnd(*this, SymtabSD, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001185
Matt Fleming3565a062010-08-16 18:57:57 +00001186 F = new MCDataFragment(&StrtabSD);
1187 F->getContents().append(StringTable.begin(), StringTable.end());
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001188 Asm.AddSectionToTheEnd(*this, StrtabSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +00001189
Matt Fleming3565a062010-08-16 18:57:57 +00001190 F = new MCDataFragment(&ShstrtabSD);
1191
Matt Fleming3565a062010-08-16 18:57:57 +00001192 // Section header string table.
1193 //
1194 // The first entry of a string table holds a null character so skip
1195 // section 0.
1196 uint64_t Index = 1;
1197 F->getContents() += '\x00';
1198
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001199 StringMap<uint64_t> SecStringMap;
Matt Fleming3565a062010-08-16 18:57:57 +00001200 for (MCAssembler::const_iterator it = Asm.begin(),
1201 ie = Asm.end(); it != ie; ++it) {
Matt Fleming3565a062010-08-16 18:57:57 +00001202 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +00001203 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola51efe7a2010-09-23 14:14:56 +00001204 // FIXME: We could merge suffixes like in .text and .rela.text.
Matt Fleming3565a062010-08-16 18:57:57 +00001205
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001206 StringRef Name = Section.getSectionName();
1207 if (SecStringMap.count(Name)) {
1208 SectionStringTableIndex[&Section] = SecStringMap[Name];
1209 continue;
1210 }
Matt Fleming3565a062010-08-16 18:57:57 +00001211 // Remember the index into the string table so we can write it
1212 // into the sh_name field of the section header table.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001213 SectionStringTableIndex[&Section] = Index;
1214 SecStringMap[Name] = Index;
Matt Fleming3565a062010-08-16 18:57:57 +00001215
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001216 Index += Name.size() + 1;
1217 F->getContents() += Name;
Matt Fleming3565a062010-08-16 18:57:57 +00001218 F->getContents() += '\x00';
1219 }
1220
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001221 Asm.AddSectionToTheEnd(*this, ShstrtabSD, Layout);
Rafael Espindola70703872010-09-30 02:22:20 +00001222}
1223
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001224bool ELFObjectWriter::IsFixupFullyResolved(const MCAssembler &Asm,
1225 const MCValue Target,
1226 bool IsPCRel,
1227 const MCFragment *DF) const {
Rafael Espindola70703872010-09-30 02:22:20 +00001228 // If this is a PCrel relocation, find the section this fixup value is
1229 // relative to.
1230 const MCSection *BaseSection = 0;
1231 if (IsPCRel) {
1232 BaseSection = &DF->getParent()->getSection();
1233 assert(BaseSection);
1234 }
1235
1236 const MCSection *SectionA = 0;
1237 const MCSymbol *SymbolA = 0;
1238 if (const MCSymbolRefExpr *A = Target.getSymA()) {
1239 SymbolA = &A->getSymbol();
1240 SectionA = &SymbolA->getSection();
1241 }
1242
1243 const MCSection *SectionB = 0;
1244 if (const MCSymbolRefExpr *B = Target.getSymB()) {
1245 SectionB = &B->getSymbol().getSection();
1246 }
1247
1248 if (!BaseSection)
1249 return SectionA == SectionB;
1250
1251 const MCSymbolData &DataA = Asm.getSymbolData(*SymbolA);
1252 if (DataA.isExternal())
1253 return false;
1254
1255 return !SectionB && BaseSection == SectionA;
Matt Fleming3565a062010-08-16 18:57:57 +00001256}
1257
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001258void ELFObjectWriter::CreateGroupSections(MCAssembler &Asm,
1259 MCAsmLayout &Layout,
1260 GroupMapTy &GroupMap) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001261 typedef DenseMap<const MCSymbol*, const MCSectionELF*> RevGroupMapTy;
1262 // Build the groups
1263 RevGroupMapTy Groups;
1264 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1265 it != ie; ++it) {
1266 const MCSectionELF &Section =
1267 static_cast<const MCSectionELF&>(it->getSection());
1268 if (!(Section.getFlags() & MCSectionELF::SHF_GROUP))
1269 continue;
1270
1271 const MCSymbol *SignatureSymbol = Section.getGroup();
1272 Asm.getOrCreateSymbolData(*SignatureSymbol);
1273 const MCSectionELF *&Group = Groups[SignatureSymbol];
1274 if (!Group) {
1275 Group = Asm.getContext().CreateELFGroupSection();
1276 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1277 Data.setAlignment(4);
1278 MCDataFragment *F = new MCDataFragment(&Data);
1279 String32(*F, ELF::GRP_COMDAT);
1280 }
1281 GroupMap[Group] = SignatureSymbol;
1282 }
1283
1284 // Add sections to the groups
1285 unsigned Index = 1;
1286 unsigned NumGroups = Groups.size();
1287 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1288 it != ie; ++it, ++Index) {
1289 const MCSectionELF &Section =
1290 static_cast<const MCSectionELF&>(it->getSection());
1291 if (!(Section.getFlags() & MCSectionELF::SHF_GROUP))
1292 continue;
1293 const MCSectionELF *Group = Groups[Section.getGroup()];
1294 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1295 // FIXME: we could use the previous fragment
1296 MCDataFragment *F = new MCDataFragment(&Data);
1297 String32(*F, NumGroups + Index);
1298 }
1299
1300 for (RevGroupMapTy::const_iterator i = Groups.begin(), e = Groups.end();
1301 i != e; ++i) {
1302 const MCSectionELF *Group = i->second;
1303 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001304 Asm.AddSectionToTheEnd(*this, Data, Layout);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001305 }
1306}
1307
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001308void ELFObjectWriter::WriteSection(MCAssembler &Asm,
1309 const SectionIndexMapTy &SectionIndexMap,
1310 uint32_t GroupSymbolIndex,
1311 uint64_t Offset, uint64_t Size,
1312 uint64_t Alignment,
1313 const MCSectionELF &Section) {
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001314 uint64_t sh_link = 0;
1315 uint64_t sh_info = 0;
1316
1317 switch(Section.getType()) {
1318 case ELF::SHT_DYNAMIC:
1319 sh_link = SectionStringTableIndex[&Section];
1320 sh_info = 0;
1321 break;
1322
1323 case ELF::SHT_REL:
1324 case ELF::SHT_RELA: {
1325 const MCSectionELF *SymtabSection;
1326 const MCSectionELF *InfoSection;
1327 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
1328 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001329 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001330 sh_link = SectionIndexMap.lookup(SymtabSection);
1331 assert(sh_link && ".symtab not found");
1332
1333 // Remove ".rel" and ".rela" prefixes.
1334 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1335 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
1336
1337 InfoSection = Asm.getContext().getELFSection(SectionName,
1338 ELF::SHT_PROGBITS, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001339 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001340 sh_info = SectionIndexMap.lookup(InfoSection);
1341 break;
1342 }
1343
1344 case ELF::SHT_SYMTAB:
1345 case ELF::SHT_DYNSYM:
1346 sh_link = StringTableIndex;
1347 sh_info = LastLocalSymbolIndex;
1348 break;
1349
1350 case ELF::SHT_SYMTAB_SHNDX:
1351 sh_link = SymbolTableIndex;
1352 break;
1353
1354 case ELF::SHT_PROGBITS:
1355 case ELF::SHT_STRTAB:
1356 case ELF::SHT_NOBITS:
1357 case ELF::SHT_NULL:
1358 case ELF::SHT_ARM_ATTRIBUTES:
1359 // Nothing to do.
1360 break;
1361
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001362 case ELF::SHT_GROUP: {
1363 sh_link = SymbolTableIndex;
1364 sh_info = GroupSymbolIndex;
1365 break;
1366 }
1367
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001368 default:
1369 assert(0 && "FIXME: sh_type value not supported!");
1370 break;
1371 }
1372
1373 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1374 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1375 Alignment, Section.getEntrySize());
1376}
1377
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001378void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1379 const MCAsmLayout &Layout) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001380 GroupMapTy GroupMap;
1381 CreateGroupSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap);
1382
Rafael Espindolabab2a802010-11-10 21:51:05 +00001383 SectionIndexMapTy SectionIndexMap;
1384
1385 ComputeIndexMap(Asm, SectionIndexMap);
1386
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001387 // Compute symbol table information.
Rafael Espindolabab2a802010-11-10 21:51:05 +00001388 ComputeSymbolTable(Asm, SectionIndexMap);
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001389
Matt Fleming3565a062010-08-16 18:57:57 +00001390 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001391 const_cast<MCAsmLayout&>(Layout),
1392 SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +00001393
Rafael Espindola1d739a02010-11-10 22:34:07 +00001394 // Update to include the metadata sections.
1395 ComputeIndexMap(Asm, SectionIndexMap);
1396
Matt Fleming3565a062010-08-16 18:57:57 +00001397 // Add 1 for the null section.
1398 unsigned NumSections = Asm.size() + 1;
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001399 uint64_t NaturalAlignment = Is64Bit ? 8 : 4;
1400 uint64_t HeaderSize = Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr);
1401 uint64_t FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001402
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001403 std::vector<const MCSectionELF*> Sections;
1404 Sections.resize(NumSections);
1405
1406 for (SectionIndexMapTy::const_iterator i=
1407 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1408 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
1409 Sections[p.second] = p.first;
1410 }
1411
1412 for (unsigned i = 1; i < NumSections; ++i) {
1413 const MCSectionELF &Section = *Sections[i];
1414 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001415
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001416 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1417
Matt Fleming3565a062010-08-16 18:57:57 +00001418 // Get the size of the section in the output file (including padding).
1419 uint64_t Size = Layout.getSectionFileSize(&SD);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001420
1421 FileOff += Size;
Matt Fleming3565a062010-08-16 18:57:57 +00001422 }
1423
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001424 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1425
Matt Fleming3565a062010-08-16 18:57:57 +00001426 // Write out the ELF header ...
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001427 WriteHeader(FileOff - HeaderSize, NumSections);
1428
1429 FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001430
1431 // ... then all of the sections ...
1432 DenseMap<const MCSection*, uint64_t> SectionOffsetMap;
1433
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001434 for (unsigned i = 1; i < NumSections; ++i) {
1435 const MCSectionELF &Section = *Sections[i];
1436 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001437
1438 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1439 WriteZeros(Padding);
1440 FileOff += Padding;
1441
Matt Fleming3565a062010-08-16 18:57:57 +00001442 // Remember the offset into the file for this section.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001443 SectionOffsetMap[&Section] = FileOff;
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001444
Matt Fleming3565a062010-08-16 18:57:57 +00001445 FileOff += Layout.getSectionFileSize(&SD);
1446
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001447 Asm.WriteSectionData(&SD, Layout, this);
Matt Fleming3565a062010-08-16 18:57:57 +00001448 }
1449
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001450 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1451 WriteZeros(Padding);
1452 FileOff += Padding;
1453
Matt Fleming3565a062010-08-16 18:57:57 +00001454 // ... and then the section header table.
1455 // Should we align the section header table?
1456 //
1457 // Null section first.
Rafael Espindola7be2c332010-10-31 00:16:26 +00001458 uint64_t FirstSectionSize =
1459 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1460 uint32_t FirstSectionLink =
1461 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1462 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming3565a062010-08-16 18:57:57 +00001463
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001464 for (unsigned i = 1; i < NumSections; ++i) {
1465 const MCSectionELF &Section = *Sections[i];
1466 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1467 uint32_t GroupSymbolIndex;
1468 if (Section.getType() != ELF::SHT_GROUP)
1469 GroupSymbolIndex = 0;
1470 else
1471 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm, GroupMap[&Section]);
Matt Fleming3565a062010-08-16 18:57:57 +00001472
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001473 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
1474 SectionOffsetMap[&Section], Layout.getSectionSize(&SD),
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001475 SD.getAlignment(), Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001476 }
1477}
1478
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001479MCObjectWriter *llvm::createELFObjectWriter(raw_ostream &OS,
1480 bool Is64Bit,
1481 Triple::OSType OSType,
1482 uint16_t EMachine,
1483 bool IsLittleEndian,
1484 bool HasRelocationAddend) {
1485 return new ELFObjectWriter(OS, Is64Bit, IsLittleEndian, EMachine,
1486 HasRelocationAddend, OSType);
Matt Fleming3565a062010-08-16 18:57:57 +00001487}