blob: 35f3f104c5b2ef74b3e602e763ac787facbb2e04 [file] [log] [blame]
Matt Fleming3565a062010-08-16 18:57:57 +00001//===- lib/MC/ELFObjectWriter.cpp - ELF File Writer -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements ELF object file writer information.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/MC/ELFObjectWriter.h"
Rafael Espindola8f413fa2010-10-05 15:11:03 +000015#include "llvm/ADT/SmallPtrSet.h"
Matt Fleming3565a062010-08-16 18:57:57 +000016#include "llvm/ADT/STLExtras.h"
17#include "llvm/ADT/StringMap.h"
18#include "llvm/ADT/Twine.h"
19#include "llvm/MC/MCAssembler.h"
20#include "llvm/MC/MCAsmLayout.h"
21#include "llvm/MC/MCContext.h"
22#include "llvm/MC/MCELFSymbolFlags.h"
23#include "llvm/MC/MCExpr.h"
24#include "llvm/MC/MCObjectWriter.h"
25#include "llvm/MC/MCSectionELF.h"
26#include "llvm/MC/MCSymbol.h"
27#include "llvm/MC/MCValue.h"
28#include "llvm/Support/Debug.h"
29#include "llvm/Support/ErrorHandling.h"
30#include "llvm/Support/ELF.h"
31#include "llvm/Target/TargetAsmBackend.h"
32
33#include "../Target/X86/X86FixupKinds.h"
34
35#include <vector>
36using namespace llvm;
37
Rafael Espindolaad49cf52010-09-18 15:03:21 +000038static unsigned GetType(const MCSymbolData &SD) {
39 uint32_t Type = (SD.getFlags() & (0xf << ELF_STT_Shift)) >> ELF_STT_Shift;
40 assert(Type == ELF::STT_NOTYPE || Type == ELF::STT_OBJECT ||
41 Type == ELF::STT_FUNC || Type == ELF::STT_SECTION ||
42 Type == ELF::STT_FILE || Type == ELF::STT_COMMON ||
43 Type == ELF::STT_TLS);
44 return Type;
45}
46
Rafael Espindolae15eb4e2010-09-23 19:55:14 +000047static unsigned GetBinding(const MCSymbolData &SD) {
48 uint32_t Binding = (SD.getFlags() & (0xf << ELF_STB_Shift)) >> ELF_STB_Shift;
49 assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
50 Binding == ELF::STB_WEAK);
51 return Binding;
52}
53
54static void SetBinding(MCSymbolData &SD, unsigned Binding) {
55 assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
56 Binding == ELF::STB_WEAK);
57 uint32_t OtherFlags = SD.getFlags() & ~(0xf << ELF_STB_Shift);
58 SD.setFlags(OtherFlags | (Binding << ELF_STB_Shift));
59}
60
Rafael Espindola152c1062010-10-06 21:02:29 +000061static unsigned GetVisibility(MCSymbolData &SD) {
62 unsigned Visibility =
63 (SD.getFlags() & (0xf << ELF_STV_Shift)) >> ELF_STV_Shift;
64 assert(Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_INTERNAL ||
65 Visibility == ELF::STV_HIDDEN || Visibility == ELF::STV_PROTECTED);
66 return Visibility;
67}
68
Rafael Espindolacebdc012010-10-04 19:46:28 +000069static bool isFixupKindX86PCRel(unsigned Kind) {
70 switch (Kind) {
71 default:
72 return false;
73 case X86::reloc_pcrel_1byte:
74 case X86::reloc_pcrel_4byte:
75 case X86::reloc_riprel_4byte:
76 case X86::reloc_riprel_4byte_movq_load:
77 return true;
78 }
79}
80
Rafael Espindolaa0a2f872010-10-28 14:22:44 +000081static bool RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant) {
82 switch (Variant) {
Rafael Espindola5c77c162010-10-05 15:48:37 +000083 default:
84 return false;
Rafael Espindolaa0a2f872010-10-28 14:22:44 +000085 case MCSymbolRefExpr::VK_GOT:
86 case MCSymbolRefExpr::VK_PLT:
87 case MCSymbolRefExpr::VK_GOTPCREL:
88 case MCSymbolRefExpr::VK_TPOFF:
89 case MCSymbolRefExpr::VK_TLSGD:
90 case MCSymbolRefExpr::VK_GOTTPOFF:
91 case MCSymbolRefExpr::VK_INDNTPOFF:
92 case MCSymbolRefExpr::VK_NTPOFF:
93 case MCSymbolRefExpr::VK_GOTNTPOFF:
Rafael Espindolaa264f722010-10-28 14:37:09 +000094 case MCSymbolRefExpr::VK_TLSLDM:
Rafael Espindola0cf15d62010-10-28 14:48:59 +000095 case MCSymbolRefExpr::VK_DTPOFF:
Rafael Espindolab4d17212010-10-28 15:02:40 +000096 case MCSymbolRefExpr::VK_TLSLD:
Rafael Espindola5c77c162010-10-05 15:48:37 +000097 return true;
98 }
99}
100
Matt Fleming3565a062010-08-16 18:57:57 +0000101namespace {
102
103 class ELFObjectWriterImpl {
Chris Lattnerb188a372010-08-28 03:21:03 +0000104 /*static bool isFixupKindX86RIPRel(unsigned Kind) {
Matt Fleming3565a062010-08-16 18:57:57 +0000105 return Kind == X86::reloc_riprel_4byte ||
106 Kind == X86::reloc_riprel_4byte_movq_load;
Chris Lattnerb188a372010-08-28 03:21:03 +0000107 }*/
Matt Fleming3565a062010-08-16 18:57:57 +0000108
109
110 /// ELFSymbolData - Helper struct for containing some precomputed information
111 /// on symbols.
112 struct ELFSymbolData {
113 MCSymbolData *SymbolData;
114 uint64_t StringIndex;
115 uint32_t SectionIndex;
116
117 // Support lexicographic sorting.
118 bool operator<(const ELFSymbolData &RHS) const {
Rafael Espindolaad49cf52010-09-18 15:03:21 +0000119 if (GetType(*SymbolData) == ELF::STT_FILE)
120 return true;
121 if (GetType(*RHS.SymbolData) == ELF::STT_FILE)
122 return false;
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000123 return SymbolData->getSymbol().getName() <
124 RHS.SymbolData->getSymbol().getName();
Matt Fleming3565a062010-08-16 18:57:57 +0000125 }
126 };
127
128 /// @name Relocation Data
129 /// @{
130
131 struct ELFRelocationEntry {
132 // Make these big enough for both 32-bit and 64-bit
133 uint64_t r_offset;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000134 int Index;
135 unsigned Type;
136 const MCSymbol *Symbol;
Matt Fleming3565a062010-08-16 18:57:57 +0000137 uint64_t r_addend;
138
139 // Support lexicographic sorting.
140 bool operator<(const ELFRelocationEntry &RE) const {
141 return RE.r_offset < r_offset;
142 }
143 };
144
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000145 SmallPtrSet<const MCSymbol *, 16> UsedInReloc;
Rafael Espindola88182132010-10-27 15:18:17 +0000146 DenseMap<const MCSymbol *, const MCSymbol *> Renames;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000147
Matt Fleming3565a062010-08-16 18:57:57 +0000148 llvm::DenseMap<const MCSectionData*,
149 std::vector<ELFRelocationEntry> > Relocations;
150 DenseMap<const MCSection*, uint64_t> SectionStringTableIndex;
151
152 /// @}
153 /// @name Symbol Table Data
154 /// @{
155
156 SmallString<256> StringTable;
157 std::vector<ELFSymbolData> LocalSymbolData;
158 std::vector<ELFSymbolData> ExternalSymbolData;
159 std::vector<ELFSymbolData> UndefinedSymbolData;
160
161 /// @}
162
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000163 int NumRegularSections;
164
Rafael Espindola5c77c162010-10-05 15:48:37 +0000165 bool NeedsGOT;
166
Matt Fleming3565a062010-08-16 18:57:57 +0000167 ELFObjectWriter *Writer;
168
169 raw_ostream &OS;
170
Matt Fleming3565a062010-08-16 18:57:57 +0000171 unsigned Is64Bit : 1;
172
173 bool HasRelocationAddend;
174
Roman Divacky5baf79e2010-09-09 17:57:50 +0000175 Triple::OSType OSType;
176
Wesley Peckeecb8582010-10-22 15:52:49 +0000177 uint16_t EMachine;
178
Matt Fleming3565a062010-08-16 18:57:57 +0000179 // This holds the symbol table index of the last local symbol.
180 unsigned LastLocalSymbolIndex;
181 // This holds the .strtab section index.
182 unsigned StringTableIndex;
183
184 unsigned ShstrtabIndex;
185
186 public:
187 ELFObjectWriterImpl(ELFObjectWriter *_Writer, bool _Is64Bit,
Wesley Peckeecb8582010-10-22 15:52:49 +0000188 uint16_t _EMachine, bool _HasRelAddend,
189 Triple::OSType _OSType)
Rafael Espindola5c77c162010-10-05 15:48:37 +0000190 : NeedsGOT(false), Writer(_Writer), OS(Writer->getStream()),
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 }
194
195 void Write8(uint8_t Value) { Writer->Write8(Value); }
196 void Write16(uint16_t Value) { Writer->Write16(Value); }
197 void Write32(uint32_t Value) { Writer->Write32(Value); }
Chris Lattnerb188a372010-08-28 03:21:03 +0000198 //void Write64(uint64_t Value) { Writer->Write64(Value); }
Matt Fleming3565a062010-08-16 18:57:57 +0000199 void WriteZeros(unsigned N) { Writer->WriteZeros(N); }
Chris Lattnerb188a372010-08-28 03:21:03 +0000200 //void WriteBytes(StringRef Str, unsigned ZeroFillSize = 0) {
201 // Writer->WriteBytes(Str, ZeroFillSize);
202 //}
Matt Fleming3565a062010-08-16 18:57:57 +0000203
204 void WriteWord(uint64_t W) {
Chris Lattnerb188a372010-08-28 03:21:03 +0000205 if (Is64Bit)
Matt Fleming3565a062010-08-16 18:57:57 +0000206 Writer->Write64(W);
Chris Lattnerb188a372010-08-28 03:21:03 +0000207 else
Matt Fleming3565a062010-08-16 18:57:57 +0000208 Writer->Write32(W);
Matt Fleming3565a062010-08-16 18:57:57 +0000209 }
210
211 void String8(char *buf, uint8_t Value) {
212 buf[0] = Value;
213 }
214
215 void StringLE16(char *buf, uint16_t Value) {
216 buf[0] = char(Value >> 0);
217 buf[1] = char(Value >> 8);
218 }
219
220 void StringLE32(char *buf, uint32_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000221 StringLE16(buf, uint16_t(Value >> 0));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000222 StringLE16(buf + 2, uint16_t(Value >> 16));
Matt Fleming3565a062010-08-16 18:57:57 +0000223 }
224
225 void StringLE64(char *buf, uint64_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000226 StringLE32(buf, uint32_t(Value >> 0));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000227 StringLE32(buf + 4, uint32_t(Value >> 32));
Matt Fleming3565a062010-08-16 18:57:57 +0000228 }
229
230 void StringBE16(char *buf ,uint16_t Value) {
231 buf[0] = char(Value >> 8);
232 buf[1] = char(Value >> 0);
233 }
234
235 void StringBE32(char *buf, uint32_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000236 StringBE16(buf, uint16_t(Value >> 16));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000237 StringBE16(buf + 2, uint16_t(Value >> 0));
Matt Fleming3565a062010-08-16 18:57:57 +0000238 }
239
240 void StringBE64(char *buf, uint64_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000241 StringBE32(buf, uint32_t(Value >> 32));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000242 StringBE32(buf + 4, uint32_t(Value >> 0));
Matt Fleming3565a062010-08-16 18:57:57 +0000243 }
244
245 void String16(char *buf, uint16_t Value) {
246 if (Writer->isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000247 StringLE16(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000248 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000249 StringBE16(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000250 }
251
252 void String32(char *buf, uint32_t Value) {
253 if (Writer->isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000254 StringLE32(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000255 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000256 StringBE32(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000257 }
258
259 void String64(char *buf, uint64_t Value) {
260 if (Writer->isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000261 StringLE64(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000262 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000263 StringBE64(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000264 }
265
266 void WriteHeader(uint64_t SectionDataSize, unsigned NumberOfSections);
267
268 void WriteSymbolEntry(MCDataFragment *F, uint64_t name, uint8_t info,
269 uint64_t value, uint64_t size,
270 uint8_t other, uint16_t shndx);
271
272 void WriteSymbol(MCDataFragment *F, ELFSymbolData &MSD,
273 const MCAsmLayout &Layout);
274
275 void WriteSymbolTable(MCDataFragment *F, const MCAssembler &Asm,
Rafael Espindola71859c62010-09-16 19:46:31 +0000276 const MCAsmLayout &Layout,
277 unsigned NumRegularSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000278
279 void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
280 const MCFragment *Fragment, const MCFixup &Fixup,
281 MCValue Target, uint64_t &FixedValue);
282
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000283 uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm,
284 const MCSymbol *S);
Matt Fleming3565a062010-08-16 18:57:57 +0000285
286 /// ComputeSymbolTable - Compute the symbol table data
287 ///
288 /// \param StringTable [out] - The string table data.
289 /// \param StringIndexMap [out] - Map from symbol names to offsets in the
290 /// string table.
291 void ComputeSymbolTable(MCAssembler &Asm);
292
293 void WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
294 const MCSectionData &SD);
295
296 void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout) {
297 for (MCAssembler::const_iterator it = Asm.begin(),
298 ie = Asm.end(); it != ie; ++it) {
299 WriteRelocation(Asm, Layout, *it);
300 }
301 }
302
303 void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout);
304
Rafael Espindola88182132010-10-27 15:18:17 +0000305 void ExecutePostLayoutBinding(MCAssembler &Asm);
Matt Fleming3565a062010-08-16 18:57:57 +0000306
307 void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
308 uint64_t Address, uint64_t Offset,
309 uint64_t Size, uint32_t Link, uint32_t Info,
310 uint64_t Alignment, uint64_t EntrySize);
311
312 void WriteRelocationsFragment(const MCAssembler &Asm, MCDataFragment *F,
313 const MCSectionData *SD);
314
Rafael Espindola70703872010-09-30 02:22:20 +0000315 bool IsFixupFullyResolved(const MCAssembler &Asm,
316 const MCValue Target,
317 bool IsPCRel,
318 const MCFragment *DF) const;
319
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000320 void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000321 };
322
323}
324
325// Emit the ELF header.
326void ELFObjectWriterImpl::WriteHeader(uint64_t SectionDataSize,
327 unsigned NumberOfSections) {
328 // ELF Header
329 // ----------
330 //
331 // Note
332 // ----
333 // emitWord method behaves differently for ELF32 and ELF64, writing
334 // 4 bytes in the former and 8 in the latter.
335
336 Write8(0x7f); // e_ident[EI_MAG0]
337 Write8('E'); // e_ident[EI_MAG1]
338 Write8('L'); // e_ident[EI_MAG2]
339 Write8('F'); // e_ident[EI_MAG3]
340
341 Write8(Is64Bit ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
342
343 // e_ident[EI_DATA]
344 Write8(Writer->isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
345
346 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky5baf79e2010-09-09 17:57:50 +0000347 // e_ident[EI_OSABI]
348 switch (OSType) {
349 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
350 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
351 default: Write8(ELF::ELFOSABI_NONE); break;
352 }
Matt Fleming3565a062010-08-16 18:57:57 +0000353 Write8(0); // e_ident[EI_ABIVERSION]
354
355 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
356
357 Write16(ELF::ET_REL); // e_type
358
Wesley Peckeecb8582010-10-22 15:52:49 +0000359 Write16(EMachine); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +0000360
361 Write32(ELF::EV_CURRENT); // e_version
362 WriteWord(0); // e_entry, no entry point in .o file
363 WriteWord(0); // e_phoff, no program header for .o
Benjamin Kramereb976772010-08-17 17:02:29 +0000364 WriteWord(SectionDataSize + (Is64Bit ? sizeof(ELF::Elf64_Ehdr) :
365 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000366
367 // FIXME: Make this configurable.
368 Write32(0); // e_flags = whatever the target wants
369
370 // e_ehsize = ELF header size
371 Write16(Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
372
373 Write16(0); // e_phentsize = prog header entry size
374 Write16(0); // e_phnum = # prog header entries = 0
375
376 // e_shentsize = Section header entry size
377 Write16(Is64Bit ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
378
379 // e_shnum = # of section header ents
380 Write16(NumberOfSections);
381
382 // e_shstrndx = Section # of '.shstrtab'
383 Write16(ShstrtabIndex);
384}
385
386void ELFObjectWriterImpl::WriteSymbolEntry(MCDataFragment *F, uint64_t name,
387 uint8_t info, uint64_t value,
388 uint64_t size, uint8_t other,
389 uint16_t shndx) {
390 if (Is64Bit) {
391 char buf[8];
392
393 String32(buf, name);
394 F->getContents() += StringRef(buf, 4); // st_name
395
396 String8(buf, info);
397 F->getContents() += StringRef(buf, 1); // st_info
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000398
Matt Fleming3565a062010-08-16 18:57:57 +0000399 String8(buf, other);
400 F->getContents() += StringRef(buf, 1); // st_other
401
402 String16(buf, shndx);
403 F->getContents() += StringRef(buf, 2); // st_shndx
404
405 String64(buf, value);
406 F->getContents() += StringRef(buf, 8); // st_value
407
408 String64(buf, size);
409 F->getContents() += StringRef(buf, 8); // st_size
410 } else {
411 char buf[4];
412
413 String32(buf, name);
414 F->getContents() += StringRef(buf, 4); // st_name
415
416 String32(buf, value);
417 F->getContents() += StringRef(buf, 4); // st_value
418
419 String32(buf, size);
420 F->getContents() += StringRef(buf, 4); // st_size
421
422 String8(buf, info);
423 F->getContents() += StringRef(buf, 1); // st_info
424
425 String8(buf, other);
426 F->getContents() += StringRef(buf, 1); // st_other
427
428 String16(buf, shndx);
429 F->getContents() += StringRef(buf, 2); // st_shndx
430 }
431}
432
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000433static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout) {
434 if (Data.isCommon() && Data.isExternal())
435 return Data.getCommonAlignment();
436
437 const MCSymbol &Symbol = Data.getSymbol();
438 if (!Symbol.isInSection())
439 return 0;
440
441 if (!Data.isCommon() && !(Data.getFlags() & ELF_STB_Weak))
442 if (MCFragment *FF = Data.getFragment())
443 return Layout.getSymbolAddress(&Data) -
444 Layout.getSectionAddress(FF->getParent());
445
446 return 0;
447}
448
Rafael Espindolade89b012010-10-15 18:25:33 +0000449static const MCSymbol &AliasedSymbol(const MCSymbol &Symbol) {
450 const MCSymbol *S = &Symbol;
451 while (S->isVariable()) {
452 const MCExpr *Value = S->getVariableValue();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000453 if (Value->getKind() != MCExpr::SymbolRef)
454 return *S;
Rafael Espindolade89b012010-10-15 18:25:33 +0000455 const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr*>(Value);
456 S = &Ref->getSymbol();
457 }
458 return *S;
459}
460
Rafael Espindola88182132010-10-27 15:18:17 +0000461void ELFObjectWriterImpl::ExecutePostLayoutBinding(MCAssembler &Asm) {
462 // The presence of symbol versions causes undefined symbols and
463 // versions declared with @@@ to be renamed.
464
465 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
466 ie = Asm.symbol_end(); it != ie; ++it) {
467 const MCSymbol &Alias = it->getSymbol();
468 if (!Alias.isVariable())
469 continue;
470 const MCSymbol &Symbol = AliasedSymbol(Alias);
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000471 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000472 size_t Pos = AliasName.find('@');
473 if (Pos == StringRef::npos)
474 continue;
475
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000476 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000477 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
478 continue;
479
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000480 // FIXME: produce a better error message.
481 if (Symbol.isUndefined() && Rest.startswith("@@") &&
482 !Rest.startswith("@@@"))
483 report_fatal_error("A @@ version cannot be undefined");
484
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000485 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000486 }
487}
488
Matt Fleming3565a062010-08-16 18:57:57 +0000489void ELFObjectWriterImpl::WriteSymbol(MCDataFragment *F, ELFSymbolData &MSD,
490 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000491 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000492 MCSymbolData &Data =
493 Layout.getAssembler().getSymbolData(AliasedSymbol(OrigData.getSymbol()));
Rafael Espindola152c1062010-10-06 21:02:29 +0000494
495 uint8_t Binding = GetBinding(OrigData);
496 uint8_t Visibility = GetVisibility(OrigData);
497 uint8_t Type = GetType(Data);
498
499 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
500 uint8_t Other = Visibility;
501
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000502 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000503 uint64_t Size = 0;
504 const MCExpr *ESize;
505
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000506 assert(!(Data.isCommon() && !Data.isExternal()));
507
Matt Fleming3565a062010-08-16 18:57:57 +0000508 ESize = Data.getSize();
509 if (Data.getSize()) {
510 MCValue Res;
511 if (ESize->getKind() == MCExpr::Binary) {
512 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(ESize);
513
514 if (BE->EvaluateAsRelocatable(Res, &Layout)) {
Benjamin Kramer24f12062010-10-17 07:38:40 +0000515 assert(!Res.getSymA() || !Res.getSymA()->getSymbol().isDefined());
516 assert(!Res.getSymB() || !Res.getSymB()->getSymbol().isDefined());
Rafael Espindolaf230df92010-10-16 18:23:53 +0000517 Size = Res.getConstant();
Matt Fleming3565a062010-08-16 18:57:57 +0000518 }
519 } else if (ESize->getKind() == MCExpr::Constant) {
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000520 Size = static_cast<const MCConstantExpr *>(ESize)->getValue();
Matt Fleming3565a062010-08-16 18:57:57 +0000521 } else {
522 assert(0 && "Unsupported size expression");
523 }
524 }
525
526 // Write out the symbol table entry
527 WriteSymbolEntry(F, MSD.StringIndex, Info, Value,
528 Size, Other, MSD.SectionIndex);
529}
530
531void ELFObjectWriterImpl::WriteSymbolTable(MCDataFragment *F,
532 const MCAssembler &Asm,
Rafael Espindola71859c62010-09-16 19:46:31 +0000533 const MCAsmLayout &Layout,
534 unsigned NumRegularSections) {
Matt Fleming3565a062010-08-16 18:57:57 +0000535 // The string table must be emitted first because we need the index
536 // into the string table for all the symbol names.
537 assert(StringTable.size() && "Missing string table");
538
539 // FIXME: Make sure the start of the symbol table is aligned.
540
541 // The first entry is the undefined symbol entry.
542 unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000543 F->getContents().append(EntrySize, '\x00');
Matt Fleming3565a062010-08-16 18:57:57 +0000544
545 // Write the symbol table entries.
546 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
547 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
548 ELFSymbolData &MSD = LocalSymbolData[i];
549 WriteSymbol(F, MSD, Layout);
550 }
551
Rafael Espindola71859c62010-09-16 19:46:31 +0000552 // Write out a symbol table entry for each regular section.
Eli Friedmana44fa242010-08-16 21:17:09 +0000553 unsigned Index = 1;
Rafael Espindola71859c62010-09-16 19:46:31 +0000554 for (MCAssembler::const_iterator it = Asm.begin();
555 Index <= NumRegularSections; ++it, ++Index) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000556 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000557 static_cast<const MCSectionELF&>(it->getSection());
Eli Friedmana44fa242010-08-16 21:17:09 +0000558 // Leave out relocations so we don't have indexes within
559 // the relocations messed up
Benjamin Kramer377a5722010-08-17 17:30:07 +0000560 if (Section.getType() == ELF::SHT_RELA || Section.getType() == ELF::SHT_REL)
Eli Friedmana44fa242010-08-16 21:17:09 +0000561 continue;
Matt Fleming3565a062010-08-16 18:57:57 +0000562 WriteSymbolEntry(F, 0, ELF::STT_SECTION, 0, 0, ELF::STV_DEFAULT, Index);
Eli Friedmana44fa242010-08-16 21:17:09 +0000563 LastLocalSymbolIndex++;
564 }
Matt Fleming3565a062010-08-16 18:57:57 +0000565
566 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
567 ELFSymbolData &MSD = ExternalSymbolData[i];
568 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000569 assert(((Data.getFlags() & ELF_STB_Global) ||
570 (Data.getFlags() & ELF_STB_Weak)) &&
571 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Matt Fleming3565a062010-08-16 18:57:57 +0000572 WriteSymbol(F, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000573 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000574 LastLocalSymbolIndex++;
575 }
576
577 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
578 ELFSymbolData &MSD = UndefinedSymbolData[i];
579 MCSymbolData &Data = *MSD.SymbolData;
Matt Fleming3565a062010-08-16 18:57:57 +0000580 WriteSymbol(F, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000581 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000582 LastLocalSymbolIndex++;
583 }
584}
585
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000586static bool ShouldRelocOnSymbol(const MCSymbolData &SD,
Rafael Espindola3729d002010-10-05 23:57:26 +0000587 const MCValue &Target,
588 const MCFragment &F) {
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000589 const MCSymbol &Symbol = SD.getSymbol();
590 if (Symbol.isUndefined())
591 return true;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000592
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000593 const MCSectionELF &Section =
594 static_cast<const MCSectionELF&>(Symbol.getSection());
595
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000596 if (SD.isExternal())
597 return true;
598
Rafael Espindola8cecf252010-10-06 16:23:36 +0000599 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000600 const MCSectionELF &Sec2 =
601 static_cast<const MCSectionELF&>(F.getParent()->getSection());
602
Rafael Espindolace2d3c52010-10-18 20:25:33 +0000603 if (Section.getKind().isBSS())
604 return false;
605
Rafael Espindola8cecf252010-10-06 16:23:36 +0000606 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000607 (Kind == MCSymbolRefExpr::VK_PLT ||
608 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
609 Kind == MCSymbolRefExpr::VK_GOTOFF))
Rafael Espindola3729d002010-10-05 23:57:26 +0000610 return true;
611
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000612 if (Section.getFlags() & MCSectionELF::SHF_MERGE)
613 return Target.getConstant() != 0;
614
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000615 return false;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000616}
617
Benjamin Kramere5b57342010-08-17 18:20:28 +0000618// FIXME: this is currently X86/X86_64 only
Matt Fleming3565a062010-08-16 18:57:57 +0000619void ELFObjectWriterImpl::RecordRelocation(const MCAssembler &Asm,
620 const MCAsmLayout &Layout,
621 const MCFragment *Fragment,
622 const MCFixup &Fixup,
623 MCValue Target,
624 uint64_t &FixedValue) {
Matt Fleming3565a062010-08-16 18:57:57 +0000625 int64_t Addend = 0;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000626 int Index = 0;
Benjamin Kramer95c602a2010-08-27 10:38:39 +0000627 int64_t Value = Target.getConstant();
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000628 const MCSymbol *Symbol = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000629
Rafael Espindola00074892010-09-17 22:34:41 +0000630 bool IsPCRel = isFixupKindX86PCRel(Fixup.getKind());
Benjamin Kramer81cfb852010-08-17 19:45:05 +0000631 if (!Target.isAbsolute()) {
Rafael Espindolade89b012010-10-15 18:25:33 +0000632 Symbol = &AliasedSymbol(Target.getSymA()->getSymbol());
Rafael Espindola88182132010-10-27 15:18:17 +0000633 const MCSymbol *Renamed = Renames.lookup(Symbol);
634 if (Renamed)
635 Symbol = Renamed;
Matt Fleming3565a062010-08-16 18:57:57 +0000636 MCSymbolData &SD = Asm.getSymbolData(*Symbol);
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000637 MCFragment *F = SD.getFragment();
Matt Fleming3565a062010-08-16 18:57:57 +0000638
Rafael Espindola9d8b7552010-10-03 00:46:57 +0000639 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
640 const MCSymbol &SymbolB = RefB->getSymbol();
641 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
642 IsPCRel = true;
Rafael Espindola55fb1022010-10-04 15:59:01 +0000643 MCSectionData *Sec = Fragment->getParent();
644
645 // Offset of the symbol in the section
646 int64_t a = Layout.getSymbolAddress(&SDB) - Layout.getSectionAddress(Sec);
647
648 // Ofeset of the relocation in the section
649 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
650 Value += b - a;
Rafael Espindola9d8b7552010-10-03 00:46:57 +0000651 }
652
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000653 // Check that this case has already been fully resolved before we get
654 // here.
Rafael Espindola00074892010-09-17 22:34:41 +0000655 if (Symbol->isDefined() && !SD.isExternal() &&
656 IsPCRel &&
657 &Fragment->getParent()->getSection() == &Symbol->getSection()) {
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000658 llvm_unreachable("We don't need a relocation in this case.");
Rafael Espindola00074892010-09-17 22:34:41 +0000659 return;
660 }
661
Rafael Espindola3729d002010-10-05 23:57:26 +0000662 bool RelocOnSymbol = ShouldRelocOnSymbol(SD, Target, *Fragment);
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000663 if (!RelocOnSymbol) {
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000664 Index = F->getParent()->getOrdinal();
Rafael Espindolaa6489182010-09-24 21:19:03 +0000665
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000666 MCSectionData *FSD = F->getParent();
667 // Offset of the symbol in the section
668 Value += Layout.getSymbolAddress(&SD) - Layout.getSectionAddress(FSD);
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000669 } else {
670 UsedInReloc.insert(Symbol);
671 Index = -1;
672 }
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000673 Addend = Value;
674 // Compensate for the addend on i386.
675 if (Is64Bit)
676 Value = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000677 }
678
Benjamin Kramer95c602a2010-08-27 10:38:39 +0000679 FixedValue = Value;
680
Matt Fleming3565a062010-08-16 18:57:57 +0000681 // determine the type of the relocation
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000682
683 MCSymbolRefExpr::VariantKind Modifier = Target.getSymA()->getKind();
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000684 unsigned Type;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000685 if (Is64Bit) {
686 if (IsPCRel) {
Rafael Espindola92bf6682010-10-04 19:04:13 +0000687 switch (Modifier) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000688 default:
689 llvm_unreachable("Unimplemented");
Rafael Espindola92bf6682010-10-04 19:04:13 +0000690 case MCSymbolRefExpr::VK_None:
691 Type = ELF::R_X86_64_PC32;
692 break;
693 case MCSymbolRefExpr::VK_PLT:
694 Type = ELF::R_X86_64_PLT32;
695 break;
Rafael Espindolaa0a2f872010-10-28 14:22:44 +0000696 case MCSymbolRefExpr::VK_GOTPCREL:
Rafael Espindola607d1f62010-10-04 19:51:39 +0000697 Type = ELF::R_X86_64_GOTPCREL;
698 break;
Rafael Espindolabc82d8b2010-10-27 20:28:07 +0000699 case MCSymbolRefExpr::VK_GOTTPOFF:
700 Type = ELF::R_X86_64_GOTTPOFF;
701 break;
702 case MCSymbolRefExpr::VK_TLSGD:
703 Type = ELF::R_X86_64_TLSGD;
704 break;
Rafael Espindolab4d17212010-10-28 15:02:40 +0000705 case MCSymbolRefExpr::VK_TLSLD:
706 Type = ELF::R_X86_64_TLSLD;
707 break;
Rafael Espindola92bf6682010-10-04 19:04:13 +0000708 }
Benjamin Kramere5b57342010-08-17 18:20:28 +0000709 } else {
710 switch ((unsigned)Fixup.getKind()) {
711 default: llvm_unreachable("invalid fixup kind!");
712 case FK_Data_8: Type = ELF::R_X86_64_64; break;
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000713 case X86::reloc_signed_4byte:
Benjamin Kramere5b57342010-08-17 18:20:28 +0000714 case X86::reloc_pcrel_4byte:
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000715 assert(isInt<32>(Target.getConstant()));
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000716 switch (Modifier) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000717 default:
718 llvm_unreachable("Unimplemented");
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000719 case MCSymbolRefExpr::VK_None:
720 Type = ELF::R_X86_64_32S;
721 break;
722 case MCSymbolRefExpr::VK_GOT:
723 Type = ELF::R_X86_64_GOT32;
724 break;
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000725 case MCSymbolRefExpr::VK_GOTPCREL:
Rafael Espindola8cecf252010-10-06 16:23:36 +0000726 Type = ELF::R_X86_64_GOTPCREL;
727 break;
Rafael Espindolabc82d8b2010-10-27 20:28:07 +0000728 case MCSymbolRefExpr::VK_TPOFF:
729 Type = ELF::R_X86_64_TPOFF32;
730 break;
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000731 }
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000732 break;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000733 case FK_Data_4:
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000734 Type = ELF::R_X86_64_32;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000735 break;
736 case FK_Data_2: Type = ELF::R_X86_64_16; break;
737 case X86::reloc_pcrel_1byte:
738 case FK_Data_1: Type = ELF::R_X86_64_8; break;
739 }
740 }
Matt Fleming3565a062010-08-16 18:57:57 +0000741 } else {
Benjamin Kramere5b57342010-08-17 18:20:28 +0000742 if (IsPCRel) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000743 switch (Modifier) {
744 default:
Rafael Espindola24dc9ec2010-10-18 19:33:01 +0000745 llvm_unreachable("Unimplemented");
746 case MCSymbolRefExpr::VK_None:
Rafael Espindola9baee3b2010-10-18 18:03:28 +0000747 Type = ELF::R_386_PC32;
Rafael Espindola9baee3b2010-10-18 18:03:28 +0000748 break;
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000749 case MCSymbolRefExpr::VK_PLT:
750 Type = ELF::R_386_PLT32;
751 break;
752 }
Benjamin Kramere5b57342010-08-17 18:20:28 +0000753 } else {
754 switch ((unsigned)Fixup.getKind()) {
755 default: llvm_unreachable("invalid fixup kind!");
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000756
Rafael Espindola24ba4f72010-10-24 17:35:42 +0000757 case X86::reloc_global_offset_table:
758 Type = ELF::R_386_GOTPC;
759 break;
760
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000761 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
762 // instead?
763 case X86::reloc_signed_4byte:
Benjamin Kramere5b57342010-08-17 18:20:28 +0000764 case X86::reloc_pcrel_4byte:
Rafael Espindolabd701182010-10-19 19:31:37 +0000765 case FK_Data_4:
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000766 switch (Modifier) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000767 default:
768 llvm_unreachable("Unimplemented");
Rafael Espindolabd701182010-10-19 19:31:37 +0000769 case MCSymbolRefExpr::VK_None:
Rafael Espindola24ba4f72010-10-24 17:35:42 +0000770 Type = ELF::R_386_32;
Rafael Espindolabd701182010-10-19 19:31:37 +0000771 break;
Rafael Espindolaeada3042010-10-18 20:47:21 +0000772 case MCSymbolRefExpr::VK_GOT:
773 Type = ELF::R_386_GOT32;
774 break;
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000775 case MCSymbolRefExpr::VK_GOTOFF:
776 Type = ELF::R_386_GOTOFF;
777 break;
Rafael Espindola3cede2d2010-10-27 21:23:52 +0000778 case MCSymbolRefExpr::VK_TLSGD:
779 Type = ELF::R_386_TLS_GD;
780 break;
781 case MCSymbolRefExpr::VK_TPOFF:
782 Type = ELF::R_386_TLS_LE_32;
783 break;
784 case MCSymbolRefExpr::VK_INDNTPOFF:
785 Type = ELF::R_386_TLS_IE;
786 break;
787 case MCSymbolRefExpr::VK_NTPOFF:
788 Type = ELF::R_386_TLS_LE;
789 break;
Rafael Espindolaa0a2f872010-10-28 14:22:44 +0000790 case MCSymbolRefExpr::VK_GOTNTPOFF:
791 Type = ELF::R_386_TLS_GOTIE;
792 break;
Rafael Espindolaa264f722010-10-28 14:37:09 +0000793 case MCSymbolRefExpr::VK_TLSLDM:
794 Type = ELF::R_386_TLS_LDM;
795 break;
Rafael Espindola0cf15d62010-10-28 14:48:59 +0000796 case MCSymbolRefExpr::VK_DTPOFF:
797 Type = ELF::R_386_TLS_LDO_32;
798 break;
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000799 }
800 break;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000801 case FK_Data_2: Type = ELF::R_386_16; break;
802 case X86::reloc_pcrel_1byte:
803 case FK_Data_1: Type = ELF::R_386_8; break;
804 }
Matt Fleming3565a062010-08-16 18:57:57 +0000805 }
806 }
807
Rafael Espindolaa0a2f872010-10-28 14:22:44 +0000808 if (RelocNeedsGOT(Modifier))
Rafael Espindola5c77c162010-10-05 15:48:37 +0000809 NeedsGOT = true;
810
Benjamin Kramere5b57342010-08-17 18:20:28 +0000811 ELFRelocationEntry ERE;
812
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000813 ERE.Index = Index;
814 ERE.Type = Type;
815 ERE.Symbol = Symbol;
Matt Fleming3565a062010-08-16 18:57:57 +0000816
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000817 ERE.r_offset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
Benjamin Kramere5b57342010-08-17 18:20:28 +0000818
Matt Fleming3565a062010-08-16 18:57:57 +0000819 if (HasRelocationAddend)
820 ERE.r_addend = Addend;
Benjamin Kramer172d7d62010-08-17 00:33:24 +0000821 else
822 ERE.r_addend = 0; // Silence compiler warning.
Matt Fleming3565a062010-08-16 18:57:57 +0000823
824 Relocations[Fragment->getParent()].push_back(ERE);
825}
826
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000827uint64_t
828ELFObjectWriterImpl::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
829 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000830 MCSymbolData &SD = Asm.getSymbolData(*S);
Eli Friedmanf8020a32010-08-16 19:15:06 +0000831
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000832 // Local symbol.
833 if (!SD.isExternal() && !S->isUndefined())
834 return SD.getIndex() + /* empty symbol */ 1;
835
836 // External or undefined symbol.
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000837 return SD.getIndex() + NumRegularSections + /* empty symbol */ 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000838}
839
Rafael Espindola737cd212010-10-05 18:01:23 +0000840static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
Rafael Espindola88182132010-10-27 15:18:17 +0000841 bool Used, bool Renamed) {
Rafael Espindolabd701182010-10-19 19:31:37 +0000842 if (Used)
843 return true;
844
Rafael Espindola88182132010-10-27 15:18:17 +0000845 if (Renamed)
846 return false;
847
Rafael Espindola737cd212010-10-05 18:01:23 +0000848 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000849
850 const MCSymbol &A = AliasedSymbol(Symbol);
851 if (&A != &Symbol && A.isUndefined())
852 return false;
853
Rafael Espindola737cd212010-10-05 18:01:23 +0000854 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
855 return false;
856
Rafael Espindolabd701182010-10-19 19:31:37 +0000857 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000858 return false;
859
860 return true;
861}
862
863static bool isLocal(const MCSymbolData &Data) {
864 if (Data.isExternal())
865 return false;
866
867 const MCSymbol &Symbol = Data.getSymbol();
868 if (Symbol.isUndefined() && !Symbol.isVariable())
869 return false;
870
871 return true;
872}
873
Matt Fleming3565a062010-08-16 18:57:57 +0000874void ELFObjectWriterImpl::ComputeSymbolTable(MCAssembler &Asm) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000875 // FIXME: Is this the correct place to do this?
876 if (NeedsGOT) {
877 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
878 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
879 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
880 Data.setExternal(true);
881 }
882
Matt Fleming3565a062010-08-16 18:57:57 +0000883 // Build section lookup table.
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000884 NumRegularSections = Asm.size();
Rafael Espindolaf5c347d2010-10-05 21:20:07 +0000885 DenseMap<const MCSection*, uint32_t> SectionIndexMap;
Matt Fleming3565a062010-08-16 18:57:57 +0000886 unsigned Index = 1;
887 for (MCAssembler::iterator it = Asm.begin(),
888 ie = Asm.end(); it != ie; ++it, ++Index)
889 SectionIndexMap[&it->getSection()] = Index;
890
891 // Index 0 is always the empty string.
892 StringMap<uint64_t> StringIndexMap;
893 StringTable += '\x00';
894
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000895 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000896 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
897 ie = Asm.symbol_end(); it != ie; ++it) {
898 const MCSymbol &Symbol = it->getSymbol();
899
Rafael Espindola88182132010-10-27 15:18:17 +0000900 if (!isInSymtab(Asm, *it, UsedInReloc.count(&Symbol),
901 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000902 continue;
903
Matt Fleming3565a062010-08-16 18:57:57 +0000904 ELFSymbolData MSD;
905 MSD.SymbolData = it;
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000906 bool Local = isLocal(*it);
Rafael Espindola88182132010-10-27 15:18:17 +0000907 const MCSymbol &RefSymbol = AliasedSymbol(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000908
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000909 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000910 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000911 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000912 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000913 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000914 } else if (RefSymbol.isUndefined()) {
Matt Fleming3565a062010-08-16 18:57:57 +0000915 MSD.SectionIndex = ELF::SHN_UNDEF;
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000916 // FIXME: Undefined symbols are global, but this is the first place we
917 // are able to set it.
918 if (GetBinding(*it) == ELF::STB_LOCAL)
919 SetBinding(*it, ELF::STB_GLOBAL);
Matt Fleming3565a062010-08-16 18:57:57 +0000920 } else {
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000921 MSD.SectionIndex = SectionIndexMap.lookup(&RefSymbol.getSection());
Matt Fleming3565a062010-08-16 18:57:57 +0000922 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000923 }
924
Rafael Espindola88182132010-10-27 15:18:17 +0000925 // The @@@ in symbol version is replaced with @ in undefined symbols and
926 // @@ in defined ones.
927 StringRef Name = Symbol.getName();
928 size_t Pos = Name.find("@@@");
929 std::string FinalName;
930 if (Pos != StringRef::npos) {
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000931 StringRef Prefix = Name.substr(0, Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000932 unsigned n = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000933 StringRef Suffix = Name.substr(Pos + n);
Rafael Espindola88182132010-10-27 15:18:17 +0000934 FinalName = Prefix.str() + Suffix.str();
935 } else {
936 FinalName = Name.str();
937 }
938
939 uint64_t &Entry = StringIndexMap[FinalName];
Rafael Espindolaa6866962010-10-27 14:44:52 +0000940 if (!Entry) {
941 Entry = StringTable.size();
Rafael Espindola88182132010-10-27 15:18:17 +0000942 StringTable += FinalName;
Rafael Espindolaa6866962010-10-27 14:44:52 +0000943 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +0000944 }
Rafael Espindolaa6866962010-10-27 14:44:52 +0000945 MSD.StringIndex = Entry;
946 if (MSD.SectionIndex == ELF::SHN_UNDEF)
947 UndefinedSymbolData.push_back(MSD);
948 else if (Local)
949 LocalSymbolData.push_back(MSD);
950 else
951 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +0000952 }
953
954 // Symbols are required to be in lexicographic order.
955 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
956 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
957 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
958
959 // Set the symbol indices. Local symbols must come before all other
960 // symbols with non-local bindings.
961 Index = 0;
962 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
963 LocalSymbolData[i].SymbolData->setIndex(Index++);
964 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
965 ExternalSymbolData[i].SymbolData->setIndex(Index++);
966 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
967 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
968}
969
970void ELFObjectWriterImpl::WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
971 const MCSectionData &SD) {
972 if (!Relocations[&SD].empty()) {
973 MCContext &Ctx = Asm.getContext();
974 const MCSection *RelaSection;
975 const MCSectionELF &Section =
976 static_cast<const MCSectionELF&>(SD.getSection());
977
978 const StringRef SectionName = Section.getSectionName();
Benjamin Kramer377a5722010-08-17 17:30:07 +0000979 std::string RelaSectionName = HasRelocationAddend ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +0000980 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000981
982 unsigned EntrySize;
983 if (HasRelocationAddend)
984 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
985 else
986 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +0000987
Benjamin Kramer377a5722010-08-17 17:30:07 +0000988 RelaSection = Ctx.getELFSection(RelaSectionName, HasRelocationAddend ?
989 ELF::SHT_RELA : ELF::SHT_REL, 0,
Matt Fleming3565a062010-08-16 18:57:57 +0000990 SectionKind::getReadOnly(),
991 false, EntrySize);
992
993 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Benjamin Kramera9eadca2010-09-06 16:11:52 +0000994 RelaSD.setAlignment(Is64Bit ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +0000995
996 MCDataFragment *F = new MCDataFragment(&RelaSD);
997
998 WriteRelocationsFragment(Asm, F, &SD);
999
Rafael Espindola70703872010-09-30 02:22:20 +00001000 Asm.AddSectionToTheEnd(*Writer, RelaSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +00001001 }
1002}
1003
1004void ELFObjectWriterImpl::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
1005 uint64_t Flags, uint64_t Address,
1006 uint64_t Offset, uint64_t Size,
1007 uint32_t Link, uint32_t Info,
1008 uint64_t Alignment,
1009 uint64_t EntrySize) {
1010 Write32(Name); // sh_name: index into string table
1011 Write32(Type); // sh_type
1012 WriteWord(Flags); // sh_flags
1013 WriteWord(Address); // sh_addr
1014 WriteWord(Offset); // sh_offset
1015 WriteWord(Size); // sh_size
1016 Write32(Link); // sh_link
1017 Write32(Info); // sh_info
1018 WriteWord(Alignment); // sh_addralign
1019 WriteWord(EntrySize); // sh_entsize
1020}
1021
1022void ELFObjectWriterImpl::WriteRelocationsFragment(const MCAssembler &Asm,
1023 MCDataFragment *F,
1024 const MCSectionData *SD) {
1025 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
1026 // sort by the r_offset just like gnu as does
1027 array_pod_sort(Relocs.begin(), Relocs.end());
1028
1029 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1030 ELFRelocationEntry entry = Relocs[e - i - 1];
1031
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001032 if (entry.Index < 0)
1033 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
1034 else
1035 entry.Index += LocalSymbolData.size() + 1;
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001036 if (Is64Bit) {
1037 char buf[8];
Matt Fleming3565a062010-08-16 18:57:57 +00001038
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001039 String64(buf, entry.r_offset);
1040 F->getContents() += StringRef(buf, 8);
1041
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001042 struct ELF::Elf64_Rela ERE64;
1043 ERE64.setSymbolAndType(entry.Index, entry.Type);
1044 String64(buf, ERE64.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001045 F->getContents() += StringRef(buf, 8);
1046
1047 if (HasRelocationAddend) {
1048 String64(buf, entry.r_addend);
1049 F->getContents() += StringRef(buf, 8);
1050 }
1051 } else {
1052 char buf[4];
1053
1054 String32(buf, entry.r_offset);
1055 F->getContents() += StringRef(buf, 4);
1056
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001057 struct ELF::Elf32_Rela ERE32;
1058 ERE32.setSymbolAndType(entry.Index, entry.Type);
1059 String32(buf, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001060 F->getContents() += StringRef(buf, 4);
1061
1062 if (HasRelocationAddend) {
1063 String32(buf, entry.r_addend);
1064 F->getContents() += StringRef(buf, 4);
1065 }
1066 }
Matt Fleming3565a062010-08-16 18:57:57 +00001067 }
1068}
1069
1070void ELFObjectWriterImpl::CreateMetadataSections(MCAssembler &Asm,
1071 MCAsmLayout &Layout) {
1072 MCContext &Ctx = Asm.getContext();
1073 MCDataFragment *F;
1074
Matt Fleming3565a062010-08-16 18:57:57 +00001075 const MCSection *SymtabSection;
1076 unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
1077
Rafael Espindola71859c62010-09-16 19:46:31 +00001078 unsigned NumRegularSections = Asm.size();
1079
Rafael Espindola38738bf2010-09-22 19:04:41 +00001080 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola71859c62010-09-16 19:46:31 +00001081 const MCSection *ShstrtabSection;
1082 ShstrtabSection = Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
1083 SectionKind::getReadOnly(), false);
1084 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1085 ShstrtabSD.setAlignment(1);
1086 ShstrtabIndex = Asm.size();
1087
Matt Fleming3565a062010-08-16 18:57:57 +00001088 SymtabSection = Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1089 SectionKind::getReadOnly(),
1090 false, EntrySize);
Matt Fleming3565a062010-08-16 18:57:57 +00001091 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Matt Fleming3565a062010-08-16 18:57:57 +00001092 SymtabSD.setAlignment(Is64Bit ? 8 : 4);
1093
Matt Fleming3565a062010-08-16 18:57:57 +00001094 const MCSection *StrtabSection;
1095 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
1096 SectionKind::getReadOnly(), false);
Matt Fleming3565a062010-08-16 18:57:57 +00001097 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1098 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +00001099 StringTableIndex = Asm.size();
1100
Rafael Espindolac3c413f2010-09-27 22:04:54 +00001101 WriteRelocations(Asm, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001102
1103 // Symbol table
1104 F = new MCDataFragment(&SymtabSD);
1105 WriteSymbolTable(F, Asm, Layout, NumRegularSections);
Rafael Espindola70703872010-09-30 02:22:20 +00001106 Asm.AddSectionToTheEnd(*Writer, SymtabSD, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001107
Matt Fleming3565a062010-08-16 18:57:57 +00001108 F = new MCDataFragment(&StrtabSD);
1109 F->getContents().append(StringTable.begin(), StringTable.end());
Rafael Espindola70703872010-09-30 02:22:20 +00001110 Asm.AddSectionToTheEnd(*Writer, StrtabSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +00001111
Matt Fleming3565a062010-08-16 18:57:57 +00001112 F = new MCDataFragment(&ShstrtabSD);
1113
Matt Fleming3565a062010-08-16 18:57:57 +00001114 // Section header string table.
1115 //
1116 // The first entry of a string table holds a null character so skip
1117 // section 0.
1118 uint64_t Index = 1;
1119 F->getContents() += '\x00';
1120
1121 for (MCAssembler::const_iterator it = Asm.begin(),
1122 ie = Asm.end(); it != ie; ++it) {
Matt Fleming3565a062010-08-16 18:57:57 +00001123 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +00001124 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola51efe7a2010-09-23 14:14:56 +00001125 // FIXME: We could merge suffixes like in .text and .rela.text.
Matt Fleming3565a062010-08-16 18:57:57 +00001126
1127 // Remember the index into the string table so we can write it
1128 // into the sh_name field of the section header table.
1129 SectionStringTableIndex[&it->getSection()] = Index;
1130
1131 Index += Section.getSectionName().size() + 1;
1132 F->getContents() += Section.getSectionName();
1133 F->getContents() += '\x00';
1134 }
1135
Rafael Espindola70703872010-09-30 02:22:20 +00001136 Asm.AddSectionToTheEnd(*Writer, ShstrtabSD, Layout);
1137}
1138
1139bool ELFObjectWriterImpl::IsFixupFullyResolved(const MCAssembler &Asm,
1140 const MCValue Target,
1141 bool IsPCRel,
1142 const MCFragment *DF) const {
1143 // If this is a PCrel relocation, find the section this fixup value is
1144 // relative to.
1145 const MCSection *BaseSection = 0;
1146 if (IsPCRel) {
1147 BaseSection = &DF->getParent()->getSection();
1148 assert(BaseSection);
1149 }
1150
1151 const MCSection *SectionA = 0;
1152 const MCSymbol *SymbolA = 0;
1153 if (const MCSymbolRefExpr *A = Target.getSymA()) {
1154 SymbolA = &A->getSymbol();
1155 SectionA = &SymbolA->getSection();
1156 }
1157
1158 const MCSection *SectionB = 0;
1159 if (const MCSymbolRefExpr *B = Target.getSymB()) {
1160 SectionB = &B->getSymbol().getSection();
1161 }
1162
1163 if (!BaseSection)
1164 return SectionA == SectionB;
1165
1166 const MCSymbolData &DataA = Asm.getSymbolData(*SymbolA);
1167 if (DataA.isExternal())
1168 return false;
1169
1170 return !SectionB && BaseSection == SectionA;
Matt Fleming3565a062010-08-16 18:57:57 +00001171}
1172
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001173void ELFObjectWriterImpl::WriteObject(MCAssembler &Asm,
Matt Fleming3565a062010-08-16 18:57:57 +00001174 const MCAsmLayout &Layout) {
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001175 // Compute symbol table information.
1176 ComputeSymbolTable(Asm);
1177
Matt Fleming3565a062010-08-16 18:57:57 +00001178 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
1179 const_cast<MCAsmLayout&>(Layout));
1180
1181 // Add 1 for the null section.
1182 unsigned NumSections = Asm.size() + 1;
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001183 uint64_t NaturalAlignment = Is64Bit ? 8 : 4;
1184 uint64_t HeaderSize = Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr);
1185 uint64_t FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001186
1187 for (MCAssembler::const_iterator it = Asm.begin(),
1188 ie = Asm.end(); it != ie; ++it) {
1189 const MCSectionData &SD = *it;
Matt Fleming3565a062010-08-16 18:57:57 +00001190
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001191 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1192
Matt Fleming3565a062010-08-16 18:57:57 +00001193 // Get the size of the section in the output file (including padding).
1194 uint64_t Size = Layout.getSectionFileSize(&SD);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001195
1196 FileOff += Size;
Matt Fleming3565a062010-08-16 18:57:57 +00001197 }
1198
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001199 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1200
Matt Fleming3565a062010-08-16 18:57:57 +00001201 // Write out the ELF header ...
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001202 WriteHeader(FileOff - HeaderSize, NumSections);
1203
1204 FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001205
1206 // ... then all of the sections ...
1207 DenseMap<const MCSection*, uint64_t> SectionOffsetMap;
1208
Rafael Espindolad8e0bfe2010-10-06 22:28:19 +00001209 DenseMap<const MCSection*, uint32_t> SectionIndexMap;
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001210
1211 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +00001212 for (MCAssembler::const_iterator it = Asm.begin(),
1213 ie = Asm.end(); it != ie; ++it) {
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001214 const MCSectionData &SD = *it;
1215
1216 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1217 WriteZeros(Padding);
1218 FileOff += Padding;
1219
Matt Fleming3565a062010-08-16 18:57:57 +00001220 // Remember the offset into the file for this section.
1221 SectionOffsetMap[&it->getSection()] = FileOff;
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001222 SectionIndexMap[&it->getSection()] = Index++;
1223
Matt Fleming3565a062010-08-16 18:57:57 +00001224 FileOff += Layout.getSectionFileSize(&SD);
1225
1226 Asm.WriteSectionData(it, Layout, Writer);
1227 }
1228
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001229 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1230 WriteZeros(Padding);
1231 FileOff += Padding;
1232
Matt Fleming3565a062010-08-16 18:57:57 +00001233 // ... and then the section header table.
1234 // Should we align the section header table?
1235 //
1236 // Null section first.
1237 WriteSecHdrEntry(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1238
1239 for (MCAssembler::const_iterator it = Asm.begin(),
1240 ie = Asm.end(); it != ie; ++it) {
1241 const MCSectionData &SD = *it;
1242 const MCSectionELF &Section =
1243 static_cast<const MCSectionELF&>(SD.getSection());
1244
1245 uint64_t sh_link = 0;
1246 uint64_t sh_info = 0;
1247
1248 switch(Section.getType()) {
1249 case ELF::SHT_DYNAMIC:
1250 sh_link = SectionStringTableIndex[&it->getSection()];
1251 sh_info = 0;
1252 break;
1253
1254 case ELF::SHT_REL:
Eli Friedmanf8020a32010-08-16 19:15:06 +00001255 case ELF::SHT_RELA: {
Matt Fleming3565a062010-08-16 18:57:57 +00001256 const MCSection *SymtabSection;
1257 const MCSection *InfoSection;
Matt Fleming3565a062010-08-16 18:57:57 +00001258
1259 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1260 SectionKind::getReadOnly(),
Eli Friedmanf8020a32010-08-16 19:15:06 +00001261 false);
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001262 sh_link = SectionIndexMap[SymtabSection];
Matt Fleming3565a062010-08-16 18:57:57 +00001263
Benjamin Kramer377a5722010-08-17 17:30:07 +00001264 // Remove ".rel" and ".rela" prefixes.
1265 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1266 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
1267
Eli Friedmanf8020a32010-08-16 19:15:06 +00001268 InfoSection = Asm.getContext().getELFSection(SectionName,
Matt Fleming3565a062010-08-16 18:57:57 +00001269 ELF::SHT_PROGBITS, 0,
Eli Friedmanf8020a32010-08-16 19:15:06 +00001270 SectionKind::getReadOnly(),
1271 false);
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001272 sh_info = SectionIndexMap[InfoSection];
Matt Fleming3565a062010-08-16 18:57:57 +00001273 break;
Eli Friedmanf8020a32010-08-16 19:15:06 +00001274 }
Matt Fleming3565a062010-08-16 18:57:57 +00001275
1276 case ELF::SHT_SYMTAB:
1277 case ELF::SHT_DYNSYM:
1278 sh_link = StringTableIndex;
1279 sh_info = LastLocalSymbolIndex;
1280 break;
1281
1282 case ELF::SHT_PROGBITS:
1283 case ELF::SHT_STRTAB:
1284 case ELF::SHT_NOBITS:
Benjamin Kramer19dc7fa2010-08-31 17:03:33 +00001285 case ELF::SHT_NULL:
Rafael Espindolacecbc3d2010-10-25 17:50:35 +00001286 case ELF::SHT_ARM_ATTRIBUTES:
Matt Fleming3565a062010-08-16 18:57:57 +00001287 // Nothing to do.
1288 break;
1289
Matt Fleming3565a062010-08-16 18:57:57 +00001290 default:
1291 assert(0 && "FIXME: sh_type value not supported!");
1292 break;
1293 }
1294
1295 WriteSecHdrEntry(SectionStringTableIndex[&it->getSection()],
1296 Section.getType(), Section.getFlags(),
Rafael Espindola71859c62010-09-16 19:46:31 +00001297 0,
Matt Fleming3565a062010-08-16 18:57:57 +00001298 SectionOffsetMap.lookup(&SD.getSection()),
1299 Layout.getSectionSize(&SD), sh_link,
1300 sh_info, SD.getAlignment(),
1301 Section.getEntrySize());
1302 }
1303}
1304
1305ELFObjectWriter::ELFObjectWriter(raw_ostream &OS,
1306 bool Is64Bit,
Roman Divacky5baf79e2010-09-09 17:57:50 +00001307 Triple::OSType OSType,
Wesley Peckeecb8582010-10-22 15:52:49 +00001308 uint16_t EMachine,
Matt Fleming3565a062010-08-16 18:57:57 +00001309 bool IsLittleEndian,
1310 bool HasRelocationAddend)
1311 : MCObjectWriter(OS, IsLittleEndian)
1312{
Wesley Peckeecb8582010-10-22 15:52:49 +00001313 Impl = new ELFObjectWriterImpl(this, Is64Bit, EMachine,
1314 HasRelocationAddend, OSType);
Matt Fleming3565a062010-08-16 18:57:57 +00001315}
1316
1317ELFObjectWriter::~ELFObjectWriter() {
1318 delete (ELFObjectWriterImpl*) Impl;
1319}
1320
1321void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm) {
1322 ((ELFObjectWriterImpl*) Impl)->ExecutePostLayoutBinding(Asm);
1323}
1324
1325void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
1326 const MCAsmLayout &Layout,
1327 const MCFragment *Fragment,
1328 const MCFixup &Fixup, MCValue Target,
1329 uint64_t &FixedValue) {
1330 ((ELFObjectWriterImpl*) Impl)->RecordRelocation(Asm, Layout, Fragment, Fixup,
1331 Target, FixedValue);
1332}
1333
Rafael Espindola70703872010-09-30 02:22:20 +00001334bool ELFObjectWriter::IsFixupFullyResolved(const MCAssembler &Asm,
1335 const MCValue Target,
1336 bool IsPCRel,
1337 const MCFragment *DF) const {
1338 return ((ELFObjectWriterImpl*) Impl)->IsFixupFullyResolved(Asm, Target,
1339 IsPCRel, DF);
1340}
1341
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001342void ELFObjectWriter::WriteObject(MCAssembler &Asm,
Matt Fleming3565a062010-08-16 18:57:57 +00001343 const MCAsmLayout &Layout) {
1344 ((ELFObjectWriterImpl*) Impl)->WriteObject(Asm, Layout);
1345}