blob: cd7eb1c6acde0237f63429b8a64ee2f1da1a5ef4 [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
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000288 // Map from a group section to the signature symbol
289 typedef DenseMap<const MCSectionELF*, const MCSymbol*> GroupMapTy;
290 // Map from a signature symbol to the group section
291 typedef DenseMap<const MCSymbol*, const MCSectionELF*> RevGroupMapTy;
292
Matt Fleming3565a062010-08-16 18:57:57 +0000293 /// ComputeSymbolTable - Compute the symbol table data
294 ///
295 /// \param StringTable [out] - The string table data.
296 /// \param StringIndexMap [out] - Map from symbol names to offsets in the
297 /// string table.
Rafael Espindolabab2a802010-11-10 21:51:05 +0000298 void ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000299 const SectionIndexMapTy &SectionIndexMap,
300 RevGroupMapTy RevGroupMap);
Rafael Espindolabab2a802010-11-10 21:51:05 +0000301
302 void ComputeIndexMap(MCAssembler &Asm,
303 SectionIndexMapTy &SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +0000304
305 void WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
306 const MCSectionData &SD);
307
308 void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout) {
309 for (MCAssembler::const_iterator it = Asm.begin(),
310 ie = Asm.end(); it != ie; ++it) {
311 WriteRelocation(Asm, Layout, *it);
312 }
313 }
314
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000315 void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout,
316 const SectionIndexMapTy &SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +0000317
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000318 void CreateGroupSections(MCAssembler &Asm, MCAsmLayout &Layout,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000319 GroupMapTy &GroupMap, RevGroupMapTy &RevGroupMap);
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000320
Rafael Espindola88182132010-10-27 15:18:17 +0000321 void ExecutePostLayoutBinding(MCAssembler &Asm);
Matt Fleming3565a062010-08-16 18:57:57 +0000322
323 void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
324 uint64_t Address, uint64_t Offset,
325 uint64_t Size, uint32_t Link, uint32_t Info,
326 uint64_t Alignment, uint64_t EntrySize);
327
328 void WriteRelocationsFragment(const MCAssembler &Asm, MCDataFragment *F,
329 const MCSectionData *SD);
330
Rafael Espindola70703872010-09-30 02:22:20 +0000331 bool IsFixupFullyResolved(const MCAssembler &Asm,
332 const MCValue Target,
333 bool IsPCRel,
334 const MCFragment *DF) const;
335
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000336 void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000337 void WriteSection(MCAssembler &Asm,
338 const SectionIndexMapTy &SectionIndexMap,
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000339 uint32_t GroupSymbolIndex,
Rafael Espindolac87a94a2010-11-10 23:36:59 +0000340 uint64_t Offset, uint64_t Size, uint64_t Alignment,
341 const MCSectionELF &Section);
Matt Fleming3565a062010-08-16 18:57:57 +0000342 };
343
344}
345
346// Emit the ELF header.
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000347void ELFObjectWriter::WriteHeader(uint64_t SectionDataSize,
348 unsigned NumberOfSections) {
Matt Fleming3565a062010-08-16 18:57:57 +0000349 // ELF Header
350 // ----------
351 //
352 // Note
353 // ----
354 // emitWord method behaves differently for ELF32 and ELF64, writing
355 // 4 bytes in the former and 8 in the latter.
356
357 Write8(0x7f); // e_ident[EI_MAG0]
358 Write8('E'); // e_ident[EI_MAG1]
359 Write8('L'); // e_ident[EI_MAG2]
360 Write8('F'); // e_ident[EI_MAG3]
361
362 Write8(Is64Bit ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
363
364 // e_ident[EI_DATA]
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000365 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
Matt Fleming3565a062010-08-16 18:57:57 +0000366
367 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky5baf79e2010-09-09 17:57:50 +0000368 // e_ident[EI_OSABI]
369 switch (OSType) {
370 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
371 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
372 default: Write8(ELF::ELFOSABI_NONE); break;
373 }
Matt Fleming3565a062010-08-16 18:57:57 +0000374 Write8(0); // e_ident[EI_ABIVERSION]
375
376 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
377
378 Write16(ELF::ET_REL); // e_type
379
Wesley Peckeecb8582010-10-22 15:52:49 +0000380 Write16(EMachine); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +0000381
382 Write32(ELF::EV_CURRENT); // e_version
383 WriteWord(0); // e_entry, no entry point in .o file
384 WriteWord(0); // e_phoff, no program header for .o
Benjamin Kramereb976772010-08-17 17:02:29 +0000385 WriteWord(SectionDataSize + (Is64Bit ? sizeof(ELF::Elf64_Ehdr) :
386 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000387
388 // FIXME: Make this configurable.
389 Write32(0); // e_flags = whatever the target wants
390
391 // e_ehsize = ELF header size
392 Write16(Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
393
394 Write16(0); // e_phentsize = prog header entry size
395 Write16(0); // e_phnum = # prog header entries = 0
396
397 // e_shentsize = Section header entry size
398 Write16(Is64Bit ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
399
400 // e_shnum = # of section header ents
Rafael Espindola7be2c332010-10-31 00:16:26 +0000401 if (NumberOfSections >= ELF::SHN_LORESERVE)
402 Write16(0);
403 else
404 Write16(NumberOfSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000405
406 // e_shstrndx = Section # of '.shstrtab'
Rafael Espindola7be2c332010-10-31 00:16:26 +0000407 if (NumberOfSections >= ELF::SHN_LORESERVE)
408 Write16(ELF::SHN_XINDEX);
409 else
410 Write16(ShstrtabIndex);
Matt Fleming3565a062010-08-16 18:57:57 +0000411}
412
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000413void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
414 MCDataFragment *ShndxF,
415 uint64_t name,
416 uint8_t info, uint64_t value,
417 uint64_t size, uint8_t other,
418 uint32_t shndx,
419 bool Reserved) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000420 if (ShndxF) {
Rafael Espindola7be2c332010-10-31 00:16:26 +0000421 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000422 String32(*ShndxF, shndx);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000423 else
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000424 String32(*ShndxF, 0);
Rafael Espindola7be2c332010-10-31 00:16:26 +0000425 }
426
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000427 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
428 uint16_t(ELF::SHN_XINDEX) : shndx;
429
Matt Fleming3565a062010-08-16 18:57:57 +0000430 if (Is64Bit) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000431 String32(*SymtabF, name); // st_name
432 String8(*SymtabF, info); // st_info
433 String8(*SymtabF, other); // st_other
434 String16(*SymtabF, Index); // st_shndx
435 String64(*SymtabF, value); // st_value
436 String64(*SymtabF, size); // st_size
Matt Fleming3565a062010-08-16 18:57:57 +0000437 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +0000438 String32(*SymtabF, name); // st_name
439 String32(*SymtabF, value); // st_value
440 String32(*SymtabF, size); // st_size
441 String8(*SymtabF, info); // st_info
442 String8(*SymtabF, other); // st_other
443 String16(*SymtabF, Index); // st_shndx
Matt Fleming3565a062010-08-16 18:57:57 +0000444 }
445}
446
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000447static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout) {
448 if (Data.isCommon() && Data.isExternal())
449 return Data.getCommonAlignment();
450
451 const MCSymbol &Symbol = Data.getSymbol();
452 if (!Symbol.isInSection())
453 return 0;
454
Rafael Espindola1973d432010-10-28 19:39:57 +0000455 if (MCFragment *FF = Data.getFragment())
456 return Layout.getSymbolAddress(&Data) -
457 Layout.getSectionAddress(FF->getParent());
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000458
459 return 0;
460}
461
Rafael Espindolade89b012010-10-15 18:25:33 +0000462static const MCSymbol &AliasedSymbol(const MCSymbol &Symbol) {
463 const MCSymbol *S = &Symbol;
464 while (S->isVariable()) {
465 const MCExpr *Value = S->getVariableValue();
Rafael Espindola9302bd62010-11-11 17:24:43 +0000466 MCExpr::ExprKind Kind = Value->getKind();
467 switch (Kind) {
468 case MCExpr::SymbolRef: {
469 const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr*>(Value);
470 S = &Ref->getSymbol();
471 break;
472 }
473 case MCExpr::Target: {
474 const MCTargetExpr *TExp = static_cast<const MCTargetExpr*>(Value);
475 MCValue Res;
476 TExp->EvaluateAsRelocatableImpl(Res, NULL);
477 S = &Res.getSymA()->getSymbol();
478 break;
479 }
480 default:
Rafael Espindolaa6866962010-10-27 14:44:52 +0000481 return *S;
Rafael Espindola9302bd62010-11-11 17:24:43 +0000482 }
Rafael Espindolade89b012010-10-15 18:25:33 +0000483 }
484 return *S;
485}
486
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000487void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm) {
Rafael Espindola88182132010-10-27 15:18:17 +0000488 // The presence of symbol versions causes undefined symbols and
489 // versions declared with @@@ to be renamed.
490
491 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
492 ie = Asm.symbol_end(); it != ie; ++it) {
493 const MCSymbol &Alias = it->getSymbol();
Rafael Espindola88182132010-10-27 15:18:17 +0000494 const MCSymbol &Symbol = AliasedSymbol(Alias);
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000495 MCSymbolData &SD = Asm.getSymbolData(Symbol);
496
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000497 // Not an alias.
498 if (&Symbol == &Alias)
499 continue;
500
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000501 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000502 size_t Pos = AliasName.find('@');
503 if (Pos == StringRef::npos)
504 continue;
505
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000506 // Aliases defined with .symvar copy the binding from the symbol they alias.
507 // This is the first place we are able to copy this information.
508 it->setExternal(SD.isExternal());
509 SetBinding(*it, GetBinding(SD));
510
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000511 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000512 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
513 continue;
514
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000515 // FIXME: produce a better error message.
516 if (Symbol.isUndefined() && Rest.startswith("@@") &&
517 !Rest.startswith("@@@"))
518 report_fatal_error("A @@ version cannot be undefined");
519
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000520 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000521 }
522}
523
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000524void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
525 MCDataFragment *ShndxF,
526 ELFSymbolData &MSD,
527 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000528 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000529 MCSymbolData &Data =
530 Layout.getAssembler().getSymbolData(AliasedSymbol(OrigData.getSymbol()));
Rafael Espindola152c1062010-10-06 21:02:29 +0000531
Rafael Espindola7be2c332010-10-31 00:16:26 +0000532 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
533 Data.getSymbol().isVariable();
534
Rafael Espindola152c1062010-10-06 21:02:29 +0000535 uint8_t Binding = GetBinding(OrigData);
536 uint8_t Visibility = GetVisibility(OrigData);
537 uint8_t Type = GetType(Data);
538
539 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
540 uint8_t Other = Visibility;
541
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000542 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000543 uint64_t Size = 0;
544 const MCExpr *ESize;
545
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000546 assert(!(Data.isCommon() && !Data.isExternal()));
547
Matt Fleming3565a062010-08-16 18:57:57 +0000548 ESize = Data.getSize();
549 if (Data.getSize()) {
550 MCValue Res;
551 if (ESize->getKind() == MCExpr::Binary) {
552 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(ESize);
553
554 if (BE->EvaluateAsRelocatable(Res, &Layout)) {
Benjamin Kramer24f12062010-10-17 07:38:40 +0000555 assert(!Res.getSymA() || !Res.getSymA()->getSymbol().isDefined());
556 assert(!Res.getSymB() || !Res.getSymB()->getSymbol().isDefined());
Rafael Espindolaf230df92010-10-16 18:23:53 +0000557 Size = Res.getConstant();
Matt Fleming3565a062010-08-16 18:57:57 +0000558 }
559 } else if (ESize->getKind() == MCExpr::Constant) {
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000560 Size = static_cast<const MCConstantExpr *>(ESize)->getValue();
Matt Fleming3565a062010-08-16 18:57:57 +0000561 } else {
562 assert(0 && "Unsupported size expression");
563 }
564 }
565
566 // Write out the symbol table entry
Rafael Espindola7be2c332010-10-31 00:16:26 +0000567 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
568 Size, Other, MSD.SectionIndex, IsReserved);
Matt Fleming3565a062010-08-16 18:57:57 +0000569}
570
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000571void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
572 MCDataFragment *ShndxF,
573 const MCAssembler &Asm,
574 const MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000575 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +0000576 // The string table must be emitted first because we need the index
577 // into the string table for all the symbol names.
578 assert(StringTable.size() && "Missing string table");
579
580 // FIXME: Make sure the start of the symbol table is aligned.
581
582 // The first entry is the undefined symbol entry.
Rafael Espindola7be2c332010-10-31 00:16:26 +0000583 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
Matt Fleming3565a062010-08-16 18:57:57 +0000584
585 // Write the symbol table entries.
586 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
587 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
588 ELFSymbolData &MSD = LocalSymbolData[i];
Rafael Espindola7be2c332010-10-31 00:16:26 +0000589 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000590 }
591
Rafael Espindola71859c62010-09-16 19:46:31 +0000592 // Write out a symbol table entry for each regular section.
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000593 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
594 ++i) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000595 const MCSectionELF &Section =
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000596 static_cast<const MCSectionELF&>(i->getSection());
597 if (Section.getType() == ELF::SHT_RELA ||
598 Section.getType() == ELF::SHT_REL ||
599 Section.getType() == ELF::SHT_STRTAB ||
600 Section.getType() == ELF::SHT_SYMTAB)
Eli Friedmana44fa242010-08-16 21:17:09 +0000601 continue;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000602 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
Rafael Espindola4beee3d2010-11-10 22:16:43 +0000603 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section), false);
Eli Friedmana44fa242010-08-16 21:17:09 +0000604 LastLocalSymbolIndex++;
605 }
Matt Fleming3565a062010-08-16 18:57:57 +0000606
607 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
608 ELFSymbolData &MSD = ExternalSymbolData[i];
609 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000610 assert(((Data.getFlags() & ELF_STB_Global) ||
611 (Data.getFlags() & ELF_STB_Weak)) &&
612 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola7be2c332010-10-31 00:16:26 +0000613 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000614 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000615 LastLocalSymbolIndex++;
616 }
617
618 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
619 ELFSymbolData &MSD = UndefinedSymbolData[i];
620 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola7be2c332010-10-31 00:16:26 +0000621 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000622 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000623 LastLocalSymbolIndex++;
624 }
625}
626
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000627static bool ShouldRelocOnSymbol(const MCSymbolData &SD,
Rafael Espindola3729d002010-10-05 23:57:26 +0000628 const MCValue &Target,
629 const MCFragment &F) {
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000630 const MCSymbol &Symbol = SD.getSymbol();
631 if (Symbol.isUndefined())
632 return true;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000633
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000634 const MCSectionELF &Section =
635 static_cast<const MCSectionELF&>(Symbol.getSection());
636
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000637 if (SD.isExternal())
638 return true;
639
Rafael Espindola8cecf252010-10-06 16:23:36 +0000640 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000641 const MCSectionELF &Sec2 =
642 static_cast<const MCSectionELF&>(F.getParent()->getSection());
643
Rafael Espindolace2d3c52010-10-18 20:25:33 +0000644 if (Section.getKind().isBSS())
645 return false;
646
Rafael Espindola8cecf252010-10-06 16:23:36 +0000647 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000648 (Kind == MCSymbolRefExpr::VK_PLT ||
649 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
650 Kind == MCSymbolRefExpr::VK_GOTOFF))
Rafael Espindola3729d002010-10-05 23:57:26 +0000651 return true;
652
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000653 if (Section.getFlags() & MCSectionELF::SHF_MERGE)
654 return Target.getConstant() != 0;
655
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000656 return false;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000657}
658
Benjamin Kramere5b57342010-08-17 18:20:28 +0000659// FIXME: this is currently X86/X86_64 only
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000660void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
661 const MCAsmLayout &Layout,
662 const MCFragment *Fragment,
663 const MCFixup &Fixup,
664 MCValue Target,
665 uint64_t &FixedValue) {
Matt Fleming3565a062010-08-16 18:57:57 +0000666 int64_t Addend = 0;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000667 int Index = 0;
Benjamin Kramer95c602a2010-08-27 10:38:39 +0000668 int64_t Value = Target.getConstant();
Rafael Espindola03f1b742010-11-11 16:48:11 +0000669 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
670 const MCSymbol &ASymbol = AliasedSymbol(Symbol);
671 const MCSymbol *RenamedP = Renames.lookup(&Symbol);
672 if (!RenamedP)
673 RenamedP = &ASymbol;
674 const MCSymbol &Renamed = *RenamedP;
Matt Fleming3565a062010-08-16 18:57:57 +0000675
Rafael Espindola00074892010-09-17 22:34:41 +0000676 bool IsPCRel = isFixupKindX86PCRel(Fixup.getKind());
Benjamin Kramer81cfb852010-08-17 19:45:05 +0000677 if (!Target.isAbsolute()) {
Rafael Espindola03f1b742010-11-11 16:48:11 +0000678 MCSymbolData &SD = Asm.getSymbolData(Symbol);
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000679 MCFragment *F = SD.getFragment();
Matt Fleming3565a062010-08-16 18:57:57 +0000680
Rafael Espindola9d8b7552010-10-03 00:46:57 +0000681 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
682 const MCSymbol &SymbolB = RefB->getSymbol();
683 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
684 IsPCRel = true;
Rafael Espindola55fb1022010-10-04 15:59:01 +0000685 MCSectionData *Sec = Fragment->getParent();
686
687 // Offset of the symbol in the section
688 int64_t a = Layout.getSymbolAddress(&SDB) - Layout.getSectionAddress(Sec);
689
690 // Ofeset of the relocation in the section
691 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
692 Value += b - a;
Rafael Espindola9d8b7552010-10-03 00:46:57 +0000693 }
694
Rafael Espindola3729d002010-10-05 23:57:26 +0000695 bool RelocOnSymbol = ShouldRelocOnSymbol(SD, Target, *Fragment);
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000696 if (!RelocOnSymbol) {
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000697 Index = F->getParent()->getOrdinal();
Rafael Espindolaa6489182010-09-24 21:19:03 +0000698
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000699 MCSectionData *FSD = F->getParent();
700 // Offset of the symbol in the section
701 Value += Layout.getSymbolAddress(&SD) - Layout.getSectionAddress(FSD);
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000702 } else {
Rafael Espindola03f1b742010-11-11 16:48:11 +0000703 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
704 WeakrefUsedInReloc.insert(&Renamed);
705 else
706 UsedInReloc.insert(&Renamed);
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000707 Index = -1;
708 }
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000709 Addend = Value;
710 // Compensate for the addend on i386.
711 if (Is64Bit)
712 Value = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000713 }
714
Benjamin Kramer95c602a2010-08-27 10:38:39 +0000715 FixedValue = Value;
716
Matt Fleming3565a062010-08-16 18:57:57 +0000717 // determine the type of the relocation
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000718
719 MCSymbolRefExpr::VariantKind Modifier = Target.getSymA()->getKind();
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000720 unsigned Type;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000721 if (Is64Bit) {
722 if (IsPCRel) {
Rafael Espindola92bf6682010-10-04 19:04:13 +0000723 switch (Modifier) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000724 default:
725 llvm_unreachable("Unimplemented");
Rafael Espindola92bf6682010-10-04 19:04:13 +0000726 case MCSymbolRefExpr::VK_None:
727 Type = ELF::R_X86_64_PC32;
728 break;
729 case MCSymbolRefExpr::VK_PLT:
730 Type = ELF::R_X86_64_PLT32;
731 break;
Rafael Espindolaa0a2f872010-10-28 14:22:44 +0000732 case MCSymbolRefExpr::VK_GOTPCREL:
Rafael Espindola607d1f62010-10-04 19:51:39 +0000733 Type = ELF::R_X86_64_GOTPCREL;
734 break;
Rafael Espindolabc82d8b2010-10-27 20:28:07 +0000735 case MCSymbolRefExpr::VK_GOTTPOFF:
736 Type = ELF::R_X86_64_GOTTPOFF;
737 break;
738 case MCSymbolRefExpr::VK_TLSGD:
739 Type = ELF::R_X86_64_TLSGD;
740 break;
Rafael Espindolab4d17212010-10-28 15:02:40 +0000741 case MCSymbolRefExpr::VK_TLSLD:
742 Type = ELF::R_X86_64_TLSLD;
743 break;
Rafael Espindola92bf6682010-10-04 19:04:13 +0000744 }
Benjamin Kramere5b57342010-08-17 18:20:28 +0000745 } else {
746 switch ((unsigned)Fixup.getKind()) {
747 default: llvm_unreachable("invalid fixup kind!");
748 case FK_Data_8: Type = ELF::R_X86_64_64; break;
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000749 case X86::reloc_signed_4byte:
Benjamin Kramere5b57342010-08-17 18:20:28 +0000750 case X86::reloc_pcrel_4byte:
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000751 assert(isInt<32>(Target.getConstant()));
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000752 switch (Modifier) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000753 default:
754 llvm_unreachable("Unimplemented");
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000755 case MCSymbolRefExpr::VK_None:
756 Type = ELF::R_X86_64_32S;
757 break;
758 case MCSymbolRefExpr::VK_GOT:
759 Type = ELF::R_X86_64_GOT32;
760 break;
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000761 case MCSymbolRefExpr::VK_GOTPCREL:
Rafael Espindola8cecf252010-10-06 16:23:36 +0000762 Type = ELF::R_X86_64_GOTPCREL;
763 break;
Rafael Espindolabc82d8b2010-10-27 20:28:07 +0000764 case MCSymbolRefExpr::VK_TPOFF:
765 Type = ELF::R_X86_64_TPOFF32;
766 break;
Rafael Espindolaaa8f1f02010-10-28 15:11:03 +0000767 case MCSymbolRefExpr::VK_DTPOFF:
768 Type = ELF::R_X86_64_DTPOFF32;
769 break;
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000770 }
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000771 break;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000772 case FK_Data_4:
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000773 Type = ELF::R_X86_64_32;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000774 break;
775 case FK_Data_2: Type = ELF::R_X86_64_16; break;
776 case X86::reloc_pcrel_1byte:
777 case FK_Data_1: Type = ELF::R_X86_64_8; break;
778 }
779 }
Matt Fleming3565a062010-08-16 18:57:57 +0000780 } else {
Benjamin Kramere5b57342010-08-17 18:20:28 +0000781 if (IsPCRel) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000782 switch (Modifier) {
783 default:
Rafael Espindola24dc9ec2010-10-18 19:33:01 +0000784 llvm_unreachable("Unimplemented");
785 case MCSymbolRefExpr::VK_None:
Rafael Espindola9baee3b2010-10-18 18:03:28 +0000786 Type = ELF::R_386_PC32;
Rafael Espindola9baee3b2010-10-18 18:03:28 +0000787 break;
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000788 case MCSymbolRefExpr::VK_PLT:
789 Type = ELF::R_386_PLT32;
790 break;
791 }
Benjamin Kramere5b57342010-08-17 18:20:28 +0000792 } else {
793 switch ((unsigned)Fixup.getKind()) {
794 default: llvm_unreachable("invalid fixup kind!");
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000795
Rafael Espindola24ba4f72010-10-24 17:35:42 +0000796 case X86::reloc_global_offset_table:
797 Type = ELF::R_386_GOTPC;
798 break;
799
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000800 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
801 // instead?
802 case X86::reloc_signed_4byte:
Benjamin Kramere5b57342010-08-17 18:20:28 +0000803 case X86::reloc_pcrel_4byte:
Rafael Espindolabd701182010-10-19 19:31:37 +0000804 case FK_Data_4:
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000805 switch (Modifier) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000806 default:
807 llvm_unreachable("Unimplemented");
Rafael Espindolabd701182010-10-19 19:31:37 +0000808 case MCSymbolRefExpr::VK_None:
Rafael Espindola24ba4f72010-10-24 17:35:42 +0000809 Type = ELF::R_386_32;
Rafael Espindolabd701182010-10-19 19:31:37 +0000810 break;
Rafael Espindolaeada3042010-10-18 20:47:21 +0000811 case MCSymbolRefExpr::VK_GOT:
812 Type = ELF::R_386_GOT32;
813 break;
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000814 case MCSymbolRefExpr::VK_GOTOFF:
815 Type = ELF::R_386_GOTOFF;
816 break;
Rafael Espindola3cede2d2010-10-27 21:23:52 +0000817 case MCSymbolRefExpr::VK_TLSGD:
818 Type = ELF::R_386_TLS_GD;
819 break;
820 case MCSymbolRefExpr::VK_TPOFF:
821 Type = ELF::R_386_TLS_LE_32;
822 break;
823 case MCSymbolRefExpr::VK_INDNTPOFF:
824 Type = ELF::R_386_TLS_IE;
825 break;
826 case MCSymbolRefExpr::VK_NTPOFF:
827 Type = ELF::R_386_TLS_LE;
828 break;
Rafael Espindolaa0a2f872010-10-28 14:22:44 +0000829 case MCSymbolRefExpr::VK_GOTNTPOFF:
830 Type = ELF::R_386_TLS_GOTIE;
831 break;
Rafael Espindolaa264f722010-10-28 14:37:09 +0000832 case MCSymbolRefExpr::VK_TLSLDM:
833 Type = ELF::R_386_TLS_LDM;
834 break;
Rafael Espindola0cf15d62010-10-28 14:48:59 +0000835 case MCSymbolRefExpr::VK_DTPOFF:
836 Type = ELF::R_386_TLS_LDO_32;
837 break;
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000838 }
839 break;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000840 case FK_Data_2: Type = ELF::R_386_16; break;
841 case X86::reloc_pcrel_1byte:
842 case FK_Data_1: Type = ELF::R_386_8; break;
843 }
Matt Fleming3565a062010-08-16 18:57:57 +0000844 }
845 }
846
Rafael Espindolaa0a2f872010-10-28 14:22:44 +0000847 if (RelocNeedsGOT(Modifier))
Rafael Espindola5c77c162010-10-05 15:48:37 +0000848 NeedsGOT = true;
849
Benjamin Kramere5b57342010-08-17 18:20:28 +0000850 ELFRelocationEntry ERE;
851
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000852 ERE.Index = Index;
853 ERE.Type = Type;
Rafael Espindola03f1b742010-11-11 16:48:11 +0000854 ERE.Symbol = &Renamed;
Matt Fleming3565a062010-08-16 18:57:57 +0000855
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000856 ERE.r_offset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
Benjamin Kramere5b57342010-08-17 18:20:28 +0000857
Matt Fleming3565a062010-08-16 18:57:57 +0000858 if (HasRelocationAddend)
859 ERE.r_addend = Addend;
Benjamin Kramer172d7d62010-08-17 00:33:24 +0000860 else
861 ERE.r_addend = 0; // Silence compiler warning.
Matt Fleming3565a062010-08-16 18:57:57 +0000862
863 Relocations[Fragment->getParent()].push_back(ERE);
864}
865
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000866uint64_t
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000867ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
868 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000869 MCSymbolData &SD = Asm.getSymbolData(*S);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000870 return SD.getIndex();
Matt Fleming3565a062010-08-16 18:57:57 +0000871}
872
Rafael Espindola737cd212010-10-05 18:01:23 +0000873static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
Rafael Espindola88182132010-10-27 15:18:17 +0000874 bool Used, bool Renamed) {
Rafael Espindola484291c2010-11-01 14:28:48 +0000875 if (Data.getFlags() & ELF_Other_Weakref)
876 return false;
877
Rafael Espindolabd701182010-10-19 19:31:37 +0000878 if (Used)
879 return true;
880
Rafael Espindola88182132010-10-27 15:18:17 +0000881 if (Renamed)
882 return false;
883
Rafael Espindola737cd212010-10-05 18:01:23 +0000884 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000885
Rafael Espindolad1798862010-10-29 23:09:31 +0000886 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
887 return true;
888
Rafael Espindolaa6866962010-10-27 14:44:52 +0000889 const MCSymbol &A = AliasedSymbol(Symbol);
Rafael Espindolad1798862010-10-29 23:09:31 +0000890 if (!A.isVariable() && A.isUndefined() && !Data.isCommon())
Rafael Espindolaa6866962010-10-27 14:44:52 +0000891 return false;
892
Rafael Espindola737cd212010-10-05 18:01:23 +0000893 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
894 return false;
895
Rafael Espindolabd701182010-10-19 19:31:37 +0000896 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000897 return false;
898
899 return true;
900}
901
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000902static bool isLocal(const MCSymbolData &Data, bool isSignature,
903 bool isUsedInReloc) {
Rafael Espindola737cd212010-10-05 18:01:23 +0000904 if (Data.isExternal())
905 return false;
906
907 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000908 const MCSymbol &RefSymbol = AliasedSymbol(Symbol);
909
910 if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
911 if (isSignature && !isUsedInReloc)
912 return true;
913
Rafael Espindola737cd212010-10-05 18:01:23 +0000914 return false;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000915 }
Rafael Espindola737cd212010-10-05 18:01:23 +0000916
917 return true;
918}
919
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000920void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
921 SectionIndexMapTy &SectionIndexMap) {
Rafael Espindolabab2a802010-11-10 21:51:05 +0000922 unsigned Index = 1;
923 for (MCAssembler::iterator it = Asm.begin(),
924 ie = Asm.end(); it != ie; ++it) {
925 const MCSectionELF &Section =
926 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola2ff9e832010-11-11 18:13:52 +0000927 if (Section.getType() != ELF::SHT_GROUP)
928 continue;
929 SectionIndexMap[&Section] = Index++;
930 }
931
932 for (MCAssembler::iterator it = Asm.begin(),
933 ie = Asm.end(); it != ie; ++it) {
934 const MCSectionELF &Section =
935 static_cast<const MCSectionELF &>(it->getSection());
936 if (Section.getType() == ELF::SHT_GROUP)
937 continue;
Rafael Espindolabab2a802010-11-10 21:51:05 +0000938 SectionIndexMap[&Section] = Index++;
939 }
940}
941
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000942void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000943 const SectionIndexMapTy &SectionIndexMap,
944 RevGroupMapTy RevGroupMap) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000945 // FIXME: Is this the correct place to do this?
946 if (NeedsGOT) {
947 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
948 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
949 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
950 Data.setExternal(true);
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000951 SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindola5c77c162010-10-05 15:48:37 +0000952 }
953
Matt Fleming3565a062010-08-16 18:57:57 +0000954 // Build section lookup table.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +0000955 int NumRegularSections = Asm.size();
Matt Fleming3565a062010-08-16 18:57:57 +0000956
957 // Index 0 is always the empty string.
958 StringMap<uint64_t> StringIndexMap;
959 StringTable += '\x00';
960
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000961 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000962 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
963 ie = Asm.symbol_end(); it != ie; ++it) {
964 const MCSymbol &Symbol = it->getSymbol();
965
Rafael Espindola484291c2010-11-01 14:28:48 +0000966 bool Used = UsedInReloc.count(&Symbol);
967 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000968 bool isSignature = RevGroupMap.count(&Symbol);
969
970 if (!isInSymtab(Asm, *it,
971 Used || WeakrefUsed || isSignature,
Rafael Espindola88182132010-10-27 15:18:17 +0000972 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000973 continue;
974
Matt Fleming3565a062010-08-16 18:57:57 +0000975 ELFSymbolData MSD;
976 MSD.SymbolData = it;
Rafael Espindola88182132010-10-27 15:18:17 +0000977 const MCSymbol &RefSymbol = AliasedSymbol(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000978
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000979 // Undefined symbols are global, but this is the first place we
980 // are able to set it.
981 bool Local = isLocal(*it, isSignature, Used);
982 if (!Local && GetBinding(*it) == ELF::STB_LOCAL) {
983 MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
984 SetBinding(*it, ELF::STB_GLOBAL);
985 SetBinding(SD, ELF::STB_GLOBAL);
986 }
987
Rafael Espindola484291c2010-11-01 14:28:48 +0000988 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
989 SetBinding(*it, ELF::STB_WEAK);
990
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000991 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000992 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000993 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000994 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000995 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000996 } else if (RefSymbol.isUndefined()) {
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000997 if (isSignature && !Used)
998 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
999 else
1000 MSD.SectionIndex = ELF::SHN_UNDEF;
Matt Fleming3565a062010-08-16 18:57:57 +00001001 } else {
Rafael Espindolabab2a802010-11-10 21:51:05 +00001002 const MCSectionELF &Section =
1003 static_cast<const MCSectionELF&>(RefSymbol.getSection());
1004 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001005 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
1006 NeedsSymtabShndx = true;
Matt Fleming3565a062010-08-16 18:57:57 +00001007 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +00001008 }
1009
Rafael Espindola88182132010-10-27 15:18:17 +00001010 // The @@@ in symbol version is replaced with @ in undefined symbols and
1011 // @@ in defined ones.
1012 StringRef Name = Symbol.getName();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +00001013 SmallString<32> Buf;
1014
Rafael Espindola88182132010-10-27 15:18:17 +00001015 size_t Pos = Name.find("@@@");
Rafael Espindola88182132010-10-27 15:18:17 +00001016 if (Pos != StringRef::npos) {
Benjamin Kramer1261a2f2010-11-12 19:26:04 +00001017 Buf += Name.substr(0, Pos);
1018 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
1019 Buf += Name.substr(Pos + Skip);
1020 Name = Buf;
Rafael Espindola88182132010-10-27 15:18:17 +00001021 }
1022
Benjamin Kramer1261a2f2010-11-12 19:26:04 +00001023 uint64_t &Entry = StringIndexMap[Name];
Rafael Espindolaa6866962010-10-27 14:44:52 +00001024 if (!Entry) {
1025 Entry = StringTable.size();
Benjamin Kramer1261a2f2010-11-12 19:26:04 +00001026 StringTable += Name;
Rafael Espindolaa6866962010-10-27 14:44:52 +00001027 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +00001028 }
Rafael Espindolaa6866962010-10-27 14:44:52 +00001029 MSD.StringIndex = Entry;
1030 if (MSD.SectionIndex == ELF::SHN_UNDEF)
1031 UndefinedSymbolData.push_back(MSD);
1032 else if (Local)
1033 LocalSymbolData.push_back(MSD);
1034 else
1035 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +00001036 }
1037
1038 // Symbols are required to be in lexicographic order.
1039 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
1040 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
1041 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
1042
1043 // Set the symbol indices. Local symbols must come before all other
1044 // symbols with non-local bindings.
Rafael Espindolaab4a7af2010-11-14 03:12:24 +00001045 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +00001046 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
1047 LocalSymbolData[i].SymbolData->setIndex(Index++);
Rafael Espindolaab4a7af2010-11-14 03:12:24 +00001048
1049 Index += NumRegularSections;
1050
Matt Fleming3565a062010-08-16 18:57:57 +00001051 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
1052 ExternalSymbolData[i].SymbolData->setIndex(Index++);
1053 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
1054 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
1055}
1056
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001057void ELFObjectWriter::WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
1058 const MCSectionData &SD) {
Matt Fleming3565a062010-08-16 18:57:57 +00001059 if (!Relocations[&SD].empty()) {
1060 MCContext &Ctx = Asm.getContext();
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001061 const MCSectionELF *RelaSection;
Matt Fleming3565a062010-08-16 18:57:57 +00001062 const MCSectionELF &Section =
1063 static_cast<const MCSectionELF&>(SD.getSection());
1064
1065 const StringRef SectionName = Section.getSectionName();
Benjamin Kramer377a5722010-08-17 17:30:07 +00001066 std::string RelaSectionName = HasRelocationAddend ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +00001067 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +00001068
1069 unsigned EntrySize;
1070 if (HasRelocationAddend)
1071 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
1072 else
1073 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +00001074
Benjamin Kramer377a5722010-08-17 17:30:07 +00001075 RelaSection = Ctx.getELFSection(RelaSectionName, HasRelocationAddend ?
1076 ELF::SHT_RELA : ELF::SHT_REL, 0,
Matt Fleming3565a062010-08-16 18:57:57 +00001077 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001078 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001079
1080 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001081 RelaSD.setAlignment(Is64Bit ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +00001082
1083 MCDataFragment *F = new MCDataFragment(&RelaSD);
1084
1085 WriteRelocationsFragment(Asm, F, &SD);
1086
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001087 Asm.AddSectionToTheEnd(*this, RelaSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +00001088 }
1089}
1090
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001091void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
1092 uint64_t Flags, uint64_t Address,
1093 uint64_t Offset, uint64_t Size,
1094 uint32_t Link, uint32_t Info,
1095 uint64_t Alignment,
1096 uint64_t EntrySize) {
Matt Fleming3565a062010-08-16 18:57:57 +00001097 Write32(Name); // sh_name: index into string table
1098 Write32(Type); // sh_type
1099 WriteWord(Flags); // sh_flags
1100 WriteWord(Address); // sh_addr
1101 WriteWord(Offset); // sh_offset
1102 WriteWord(Size); // sh_size
1103 Write32(Link); // sh_link
1104 Write32(Info); // sh_info
1105 WriteWord(Alignment); // sh_addralign
1106 WriteWord(EntrySize); // sh_entsize
1107}
1108
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001109void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
1110 MCDataFragment *F,
1111 const MCSectionData *SD) {
Matt Fleming3565a062010-08-16 18:57:57 +00001112 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
1113 // sort by the r_offset just like gnu as does
1114 array_pod_sort(Relocs.begin(), Relocs.end());
1115
1116 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1117 ELFRelocationEntry entry = Relocs[e - i - 1];
1118
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001119 if (entry.Index < 0)
1120 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
1121 else
1122 entry.Index += LocalSymbolData.size() + 1;
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001123 if (Is64Bit) {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001124 String64(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001125
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001126 struct ELF::Elf64_Rela ERE64;
1127 ERE64.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001128 String64(*F, ERE64.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001129
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001130 if (HasRelocationAddend)
1131 String64(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001132 } else {
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001133 String32(*F, entry.r_offset);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001134
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001135 struct ELF::Elf32_Rela ERE32;
1136 ERE32.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001137 String32(*F, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001138
Rafael Espindolaaf3d38f2010-11-10 20:02:59 +00001139 if (HasRelocationAddend)
1140 String32(*F, entry.r_addend);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001141 }
Matt Fleming3565a062010-08-16 18:57:57 +00001142 }
1143}
1144
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001145void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
1146 MCAsmLayout &Layout,
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001147 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming3565a062010-08-16 18:57:57 +00001148 MCContext &Ctx = Asm.getContext();
1149 MCDataFragment *F;
1150
Matt Fleming3565a062010-08-16 18:57:57 +00001151 unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
1152
Rafael Espindola38738bf2010-09-22 19:04:41 +00001153 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001154 const MCSectionELF *ShstrtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001155 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001156 SectionKind::getReadOnly());
Rafael Espindola71859c62010-09-16 19:46:31 +00001157 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1158 ShstrtabSD.setAlignment(1);
1159 ShstrtabIndex = Asm.size();
1160
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001161 const MCSectionELF *SymtabSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001162 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1163 SectionKind::getReadOnly(),
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001164 EntrySize, "");
Matt Fleming3565a062010-08-16 18:57:57 +00001165 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Matt Fleming3565a062010-08-16 18:57:57 +00001166 SymtabSD.setAlignment(Is64Bit ? 8 : 4);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001167 SymbolTableIndex = Asm.size();
1168
1169 MCSectionData *SymtabShndxSD = NULL;
1170
1171 if (NeedsSymtabShndx) {
Rafael Espindola4283f4b2010-11-10 19:05:07 +00001172 const MCSectionELF *SymtabShndxSection =
Rafael Espindola7be2c332010-10-31 00:16:26 +00001173 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001174 SectionKind::getReadOnly(), 4, "");
Rafael Espindola7be2c332010-10-31 00:16:26 +00001175 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
1176 SymtabShndxSD->setAlignment(4);
1177 }
Matt Fleming3565a062010-08-16 18:57:57 +00001178
Matt Fleming3565a062010-08-16 18:57:57 +00001179 const MCSection *StrtabSection;
1180 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001181 SectionKind::getReadOnly());
Matt Fleming3565a062010-08-16 18:57:57 +00001182 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1183 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +00001184 StringTableIndex = Asm.size();
1185
Rafael Espindolac3c413f2010-09-27 22:04:54 +00001186 WriteRelocations(Asm, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001187
1188 // Symbol table
1189 F = new MCDataFragment(&SymtabSD);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001190 MCDataFragment *ShndxF = NULL;
1191 if (NeedsSymtabShndx) {
1192 ShndxF = new MCDataFragment(SymtabShndxSD);
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001193 Asm.AddSectionToTheEnd(*this, *SymtabShndxSD, Layout);
Rafael Espindola7be2c332010-10-31 00:16:26 +00001194 }
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001195 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001196 Asm.AddSectionToTheEnd(*this, SymtabSD, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001197
Matt Fleming3565a062010-08-16 18:57:57 +00001198 F = new MCDataFragment(&StrtabSD);
1199 F->getContents().append(StringTable.begin(), StringTable.end());
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001200 Asm.AddSectionToTheEnd(*this, StrtabSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +00001201
Matt Fleming3565a062010-08-16 18:57:57 +00001202 F = new MCDataFragment(&ShstrtabSD);
1203
Matt Fleming3565a062010-08-16 18:57:57 +00001204 // Section header string table.
1205 //
1206 // The first entry of a string table holds a null character so skip
1207 // section 0.
1208 uint64_t Index = 1;
1209 F->getContents() += '\x00';
1210
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001211 StringMap<uint64_t> SecStringMap;
Matt Fleming3565a062010-08-16 18:57:57 +00001212 for (MCAssembler::const_iterator it = Asm.begin(),
1213 ie = Asm.end(); it != ie; ++it) {
Matt Fleming3565a062010-08-16 18:57:57 +00001214 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +00001215 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola51efe7a2010-09-23 14:14:56 +00001216 // FIXME: We could merge suffixes like in .text and .rela.text.
Matt Fleming3565a062010-08-16 18:57:57 +00001217
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001218 StringRef Name = Section.getSectionName();
1219 if (SecStringMap.count(Name)) {
1220 SectionStringTableIndex[&Section] = SecStringMap[Name];
1221 continue;
1222 }
Matt Fleming3565a062010-08-16 18:57:57 +00001223 // Remember the index into the string table so we can write it
1224 // into the sh_name field of the section header table.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001225 SectionStringTableIndex[&Section] = Index;
1226 SecStringMap[Name] = Index;
Matt Fleming3565a062010-08-16 18:57:57 +00001227
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001228 Index += Name.size() + 1;
1229 F->getContents() += Name;
Matt Fleming3565a062010-08-16 18:57:57 +00001230 F->getContents() += '\x00';
1231 }
1232
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001233 Asm.AddSectionToTheEnd(*this, ShstrtabSD, Layout);
Rafael Espindola70703872010-09-30 02:22:20 +00001234}
1235
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001236bool ELFObjectWriter::IsFixupFullyResolved(const MCAssembler &Asm,
1237 const MCValue Target,
1238 bool IsPCRel,
1239 const MCFragment *DF) const {
Rafael Espindola70703872010-09-30 02:22:20 +00001240 // If this is a PCrel relocation, find the section this fixup value is
1241 // relative to.
1242 const MCSection *BaseSection = 0;
1243 if (IsPCRel) {
1244 BaseSection = &DF->getParent()->getSection();
1245 assert(BaseSection);
1246 }
1247
1248 const MCSection *SectionA = 0;
1249 const MCSymbol *SymbolA = 0;
1250 if (const MCSymbolRefExpr *A = Target.getSymA()) {
1251 SymbolA = &A->getSymbol();
1252 SectionA = &SymbolA->getSection();
1253 }
1254
1255 const MCSection *SectionB = 0;
1256 if (const MCSymbolRefExpr *B = Target.getSymB()) {
1257 SectionB = &B->getSymbol().getSection();
1258 }
1259
1260 if (!BaseSection)
1261 return SectionA == SectionB;
1262
1263 const MCSymbolData &DataA = Asm.getSymbolData(*SymbolA);
1264 if (DataA.isExternal())
1265 return false;
1266
1267 return !SectionB && BaseSection == SectionA;
Matt Fleming3565a062010-08-16 18:57:57 +00001268}
1269
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001270void ELFObjectWriter::CreateGroupSections(MCAssembler &Asm,
1271 MCAsmLayout &Layout,
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001272 GroupMapTy &GroupMap,
1273 RevGroupMapTy &RevGroupMap) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001274 // Build the groups
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001275 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1276 it != ie; ++it) {
1277 const MCSectionELF &Section =
1278 static_cast<const MCSectionELF&>(it->getSection());
1279 if (!(Section.getFlags() & MCSectionELF::SHF_GROUP))
1280 continue;
1281
1282 const MCSymbol *SignatureSymbol = Section.getGroup();
1283 Asm.getOrCreateSymbolData(*SignatureSymbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001284 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001285 if (!Group) {
1286 Group = Asm.getContext().CreateELFGroupSection();
1287 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1288 Data.setAlignment(4);
1289 MCDataFragment *F = new MCDataFragment(&Data);
1290 String32(*F, ELF::GRP_COMDAT);
1291 }
1292 GroupMap[Group] = SignatureSymbol;
1293 }
1294
1295 // Add sections to the groups
1296 unsigned Index = 1;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001297 unsigned NumGroups = RevGroupMap.size();
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001298 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1299 it != ie; ++it, ++Index) {
1300 const MCSectionELF &Section =
1301 static_cast<const MCSectionELF&>(it->getSection());
1302 if (!(Section.getFlags() & MCSectionELF::SHF_GROUP))
1303 continue;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001304 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001305 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1306 // FIXME: we could use the previous fragment
1307 MCDataFragment *F = new MCDataFragment(&Data);
1308 String32(*F, NumGroups + Index);
1309 }
1310
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001311 for (RevGroupMapTy::const_iterator i = RevGroupMap.begin(),
1312 e = RevGroupMap.end(); i != e; ++i) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001313 const MCSectionELF *Group = i->second;
1314 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001315 Asm.AddSectionToTheEnd(*this, Data, Layout);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001316 }
1317}
1318
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001319void ELFObjectWriter::WriteSection(MCAssembler &Asm,
1320 const SectionIndexMapTy &SectionIndexMap,
1321 uint32_t GroupSymbolIndex,
1322 uint64_t Offset, uint64_t Size,
1323 uint64_t Alignment,
1324 const MCSectionELF &Section) {
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001325 uint64_t sh_link = 0;
1326 uint64_t sh_info = 0;
1327
1328 switch(Section.getType()) {
1329 case ELF::SHT_DYNAMIC:
1330 sh_link = SectionStringTableIndex[&Section];
1331 sh_info = 0;
1332 break;
1333
1334 case ELF::SHT_REL:
1335 case ELF::SHT_RELA: {
1336 const MCSectionELF *SymtabSection;
1337 const MCSectionELF *InfoSection;
1338 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
1339 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001340 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001341 sh_link = SectionIndexMap.lookup(SymtabSection);
1342 assert(sh_link && ".symtab not found");
1343
1344 // Remove ".rel" and ".rela" prefixes.
1345 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1346 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
1347
1348 InfoSection = Asm.getContext().getELFSection(SectionName,
1349 ELF::SHT_PROGBITS, 0,
Rafael Espindola3f2d13c2010-11-11 03:40:25 +00001350 SectionKind::getReadOnly());
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001351 sh_info = SectionIndexMap.lookup(InfoSection);
1352 break;
1353 }
1354
1355 case ELF::SHT_SYMTAB:
1356 case ELF::SHT_DYNSYM:
1357 sh_link = StringTableIndex;
1358 sh_info = LastLocalSymbolIndex;
1359 break;
1360
1361 case ELF::SHT_SYMTAB_SHNDX:
1362 sh_link = SymbolTableIndex;
1363 break;
1364
1365 case ELF::SHT_PROGBITS:
1366 case ELF::SHT_STRTAB:
1367 case ELF::SHT_NOBITS:
1368 case ELF::SHT_NULL:
1369 case ELF::SHT_ARM_ATTRIBUTES:
1370 // Nothing to do.
1371 break;
1372
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001373 case ELF::SHT_GROUP: {
1374 sh_link = SymbolTableIndex;
1375 sh_info = GroupSymbolIndex;
1376 break;
1377 }
1378
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001379 default:
1380 assert(0 && "FIXME: sh_type value not supported!");
1381 break;
1382 }
1383
1384 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1385 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1386 Alignment, Section.getEntrySize());
1387}
1388
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001389void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1390 const MCAsmLayout &Layout) {
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001391 GroupMapTy GroupMap;
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001392 RevGroupMapTy RevGroupMap;
1393 CreateGroupSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1394 RevGroupMap);
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001395
Rafael Espindolabab2a802010-11-10 21:51:05 +00001396 SectionIndexMapTy SectionIndexMap;
1397
1398 ComputeIndexMap(Asm, SectionIndexMap);
1399
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001400 // Compute symbol table information.
Rafael Espindola1f4f9e32010-11-14 04:17:37 +00001401 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap);
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001402
Matt Fleming3565a062010-08-16 18:57:57 +00001403 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
Rafael Espindola4beee3d2010-11-10 22:16:43 +00001404 const_cast<MCAsmLayout&>(Layout),
1405 SectionIndexMap);
Matt Fleming3565a062010-08-16 18:57:57 +00001406
Rafael Espindola1d739a02010-11-10 22:34:07 +00001407 // Update to include the metadata sections.
1408 ComputeIndexMap(Asm, SectionIndexMap);
1409
Matt Fleming3565a062010-08-16 18:57:57 +00001410 // Add 1 for the null section.
1411 unsigned NumSections = Asm.size() + 1;
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001412 uint64_t NaturalAlignment = Is64Bit ? 8 : 4;
1413 uint64_t HeaderSize = Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr);
1414 uint64_t FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001415
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001416 std::vector<const MCSectionELF*> Sections;
1417 Sections.resize(NumSections);
1418
1419 for (SectionIndexMapTy::const_iterator i=
1420 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1421 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
1422 Sections[p.second] = p.first;
1423 }
1424
1425 for (unsigned i = 1; i < NumSections; ++i) {
1426 const MCSectionELF &Section = *Sections[i];
1427 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001428
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001429 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1430
Matt Fleming3565a062010-08-16 18:57:57 +00001431 // Get the size of the section in the output file (including padding).
1432 uint64_t Size = Layout.getSectionFileSize(&SD);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001433
1434 FileOff += Size;
Matt Fleming3565a062010-08-16 18:57:57 +00001435 }
1436
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001437 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1438
Matt Fleming3565a062010-08-16 18:57:57 +00001439 // Write out the ELF header ...
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001440 WriteHeader(FileOff - HeaderSize, NumSections);
1441
1442 FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001443
1444 // ... then all of the sections ...
1445 DenseMap<const MCSection*, uint64_t> SectionOffsetMap;
1446
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001447 for (unsigned i = 1; i < NumSections; ++i) {
1448 const MCSectionELF &Section = *Sections[i];
1449 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001450
1451 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1452 WriteZeros(Padding);
1453 FileOff += Padding;
1454
Matt Fleming3565a062010-08-16 18:57:57 +00001455 // Remember the offset into the file for this section.
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001456 SectionOffsetMap[&Section] = FileOff;
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001457
Matt Fleming3565a062010-08-16 18:57:57 +00001458 FileOff += Layout.getSectionFileSize(&SD);
1459
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001460 Asm.WriteSectionData(&SD, Layout, this);
Matt Fleming3565a062010-08-16 18:57:57 +00001461 }
1462
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001463 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1464 WriteZeros(Padding);
1465 FileOff += Padding;
1466
Matt Fleming3565a062010-08-16 18:57:57 +00001467 // ... and then the section header table.
1468 // Should we align the section header table?
1469 //
1470 // Null section first.
Rafael Espindola7be2c332010-10-31 00:16:26 +00001471 uint64_t FirstSectionSize =
1472 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1473 uint32_t FirstSectionLink =
1474 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1475 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming3565a062010-08-16 18:57:57 +00001476
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001477 for (unsigned i = 1; i < NumSections; ++i) {
1478 const MCSectionELF &Section = *Sections[i];
1479 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1480 uint32_t GroupSymbolIndex;
1481 if (Section.getType() != ELF::SHT_GROUP)
1482 GroupSymbolIndex = 0;
1483 else
1484 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm, GroupMap[&Section]);
Matt Fleming3565a062010-08-16 18:57:57 +00001485
Rafael Espindola2ff9e832010-11-11 18:13:52 +00001486 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
1487 SectionOffsetMap[&Section], Layout.getSectionSize(&SD),
Rafael Espindolac87a94a2010-11-10 23:36:59 +00001488 SD.getAlignment(), Section);
Matt Fleming3565a062010-08-16 18:57:57 +00001489 }
1490}
1491
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001492MCObjectWriter *llvm::createELFObjectWriter(raw_ostream &OS,
1493 bool Is64Bit,
1494 Triple::OSType OSType,
1495 uint16_t EMachine,
1496 bool IsLittleEndian,
1497 bool HasRelocationAddend) {
1498 return new ELFObjectWriter(OS, Is64Bit, IsLittleEndian, EMachine,
1499 HasRelocationAddend, OSType);
Matt Fleming3565a062010-08-16 18:57:57 +00001500}