blob: ef2901d7497e599afecab9e43e3ec7b28e5d2146 [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();
Rafael Espindola88182132010-10-27 15:18:17 +0000468 const MCSymbol &Symbol = AliasedSymbol(Alias);
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000469 MCSymbolData &SD = Asm.getSymbolData(Symbol);
470
471 // Undefined symbols are global, but this is the first place we
472 // are able to set it.
473 if (Symbol.isUndefined() && !Symbol.isVariable()) {
474 if (GetBinding(SD) == ELF::STB_LOCAL) {
475 SetBinding(SD, ELF::STB_GLOBAL);
476 SetBinding(*it, ELF::STB_GLOBAL);
477 }
478 }
479
480 // Not an alias.
481 if (&Symbol == &Alias)
482 continue;
483
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000484 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000485 size_t Pos = AliasName.find('@');
486 if (Pos == StringRef::npos)
487 continue;
488
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000489 // Aliases defined with .symvar copy the binding from the symbol they alias.
490 // This is the first place we are able to copy this information.
491 it->setExternal(SD.isExternal());
492 SetBinding(*it, GetBinding(SD));
493
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000494 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000495 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
496 continue;
497
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000498 // FIXME: produce a better error message.
499 if (Symbol.isUndefined() && Rest.startswith("@@") &&
500 !Rest.startswith("@@@"))
501 report_fatal_error("A @@ version cannot be undefined");
502
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000503 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000504 }
505}
506
Matt Fleming3565a062010-08-16 18:57:57 +0000507void ELFObjectWriterImpl::WriteSymbol(MCDataFragment *F, ELFSymbolData &MSD,
508 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000509 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000510 MCSymbolData &Data =
511 Layout.getAssembler().getSymbolData(AliasedSymbol(OrigData.getSymbol()));
Rafael Espindola152c1062010-10-06 21:02:29 +0000512
513 uint8_t Binding = GetBinding(OrigData);
514 uint8_t Visibility = GetVisibility(OrigData);
515 uint8_t Type = GetType(Data);
516
517 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
518 uint8_t Other = Visibility;
519
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000520 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000521 uint64_t Size = 0;
522 const MCExpr *ESize;
523
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000524 assert(!(Data.isCommon() && !Data.isExternal()));
525
Matt Fleming3565a062010-08-16 18:57:57 +0000526 ESize = Data.getSize();
527 if (Data.getSize()) {
528 MCValue Res;
529 if (ESize->getKind() == MCExpr::Binary) {
530 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(ESize);
531
532 if (BE->EvaluateAsRelocatable(Res, &Layout)) {
Benjamin Kramer24f12062010-10-17 07:38:40 +0000533 assert(!Res.getSymA() || !Res.getSymA()->getSymbol().isDefined());
534 assert(!Res.getSymB() || !Res.getSymB()->getSymbol().isDefined());
Rafael Espindolaf230df92010-10-16 18:23:53 +0000535 Size = Res.getConstant();
Matt Fleming3565a062010-08-16 18:57:57 +0000536 }
537 } else if (ESize->getKind() == MCExpr::Constant) {
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000538 Size = static_cast<const MCConstantExpr *>(ESize)->getValue();
Matt Fleming3565a062010-08-16 18:57:57 +0000539 } else {
540 assert(0 && "Unsupported size expression");
541 }
542 }
543
544 // Write out the symbol table entry
545 WriteSymbolEntry(F, MSD.StringIndex, Info, Value,
546 Size, Other, MSD.SectionIndex);
547}
548
549void ELFObjectWriterImpl::WriteSymbolTable(MCDataFragment *F,
550 const MCAssembler &Asm,
Rafael Espindola71859c62010-09-16 19:46:31 +0000551 const MCAsmLayout &Layout,
552 unsigned NumRegularSections) {
Matt Fleming3565a062010-08-16 18:57:57 +0000553 // The string table must be emitted first because we need the index
554 // into the string table for all the symbol names.
555 assert(StringTable.size() && "Missing string table");
556
557 // FIXME: Make sure the start of the symbol table is aligned.
558
559 // The first entry is the undefined symbol entry.
560 unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000561 F->getContents().append(EntrySize, '\x00');
Matt Fleming3565a062010-08-16 18:57:57 +0000562
563 // Write the symbol table entries.
564 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
565 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
566 ELFSymbolData &MSD = LocalSymbolData[i];
567 WriteSymbol(F, MSD, Layout);
568 }
569
Rafael Espindola71859c62010-09-16 19:46:31 +0000570 // Write out a symbol table entry for each regular section.
Eli Friedmana44fa242010-08-16 21:17:09 +0000571 unsigned Index = 1;
Rafael Espindola71859c62010-09-16 19:46:31 +0000572 for (MCAssembler::const_iterator it = Asm.begin();
573 Index <= NumRegularSections; ++it, ++Index) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000574 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000575 static_cast<const MCSectionELF&>(it->getSection());
Eli Friedmana44fa242010-08-16 21:17:09 +0000576 // Leave out relocations so we don't have indexes within
577 // the relocations messed up
Benjamin Kramer377a5722010-08-17 17:30:07 +0000578 if (Section.getType() == ELF::SHT_RELA || Section.getType() == ELF::SHT_REL)
Eli Friedmana44fa242010-08-16 21:17:09 +0000579 continue;
Matt Fleming3565a062010-08-16 18:57:57 +0000580 WriteSymbolEntry(F, 0, ELF::STT_SECTION, 0, 0, ELF::STV_DEFAULT, Index);
Eli Friedmana44fa242010-08-16 21:17:09 +0000581 LastLocalSymbolIndex++;
582 }
Matt Fleming3565a062010-08-16 18:57:57 +0000583
584 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
585 ELFSymbolData &MSD = ExternalSymbolData[i];
586 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000587 assert(((Data.getFlags() & ELF_STB_Global) ||
588 (Data.getFlags() & ELF_STB_Weak)) &&
589 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Matt Fleming3565a062010-08-16 18:57:57 +0000590 WriteSymbol(F, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000591 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000592 LastLocalSymbolIndex++;
593 }
594
595 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
596 ELFSymbolData &MSD = UndefinedSymbolData[i];
597 MCSymbolData &Data = *MSD.SymbolData;
Matt Fleming3565a062010-08-16 18:57:57 +0000598 WriteSymbol(F, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000599 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000600 LastLocalSymbolIndex++;
601 }
602}
603
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000604static bool ShouldRelocOnSymbol(const MCSymbolData &SD,
Rafael Espindola3729d002010-10-05 23:57:26 +0000605 const MCValue &Target,
606 const MCFragment &F) {
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000607 const MCSymbol &Symbol = SD.getSymbol();
608 if (Symbol.isUndefined())
609 return true;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000610
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000611 const MCSectionELF &Section =
612 static_cast<const MCSectionELF&>(Symbol.getSection());
613
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000614 if (SD.isExternal())
615 return true;
616
Rafael Espindola8cecf252010-10-06 16:23:36 +0000617 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000618 const MCSectionELF &Sec2 =
619 static_cast<const MCSectionELF&>(F.getParent()->getSection());
620
Rafael Espindolace2d3c52010-10-18 20:25:33 +0000621 if (Section.getKind().isBSS())
622 return false;
623
Rafael Espindola8cecf252010-10-06 16:23:36 +0000624 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000625 (Kind == MCSymbolRefExpr::VK_PLT ||
626 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
627 Kind == MCSymbolRefExpr::VK_GOTOFF))
Rafael Espindola3729d002010-10-05 23:57:26 +0000628 return true;
629
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000630 if (Section.getFlags() & MCSectionELF::SHF_MERGE)
631 return Target.getConstant() != 0;
632
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000633 return false;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000634}
635
Benjamin Kramere5b57342010-08-17 18:20:28 +0000636// FIXME: this is currently X86/X86_64 only
Matt Fleming3565a062010-08-16 18:57:57 +0000637void ELFObjectWriterImpl::RecordRelocation(const MCAssembler &Asm,
638 const MCAsmLayout &Layout,
639 const MCFragment *Fragment,
640 const MCFixup &Fixup,
641 MCValue Target,
642 uint64_t &FixedValue) {
Matt Fleming3565a062010-08-16 18:57:57 +0000643 int64_t Addend = 0;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000644 int Index = 0;
Benjamin Kramer95c602a2010-08-27 10:38:39 +0000645 int64_t Value = Target.getConstant();
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000646 const MCSymbol *Symbol = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000647
Rafael Espindola00074892010-09-17 22:34:41 +0000648 bool IsPCRel = isFixupKindX86PCRel(Fixup.getKind());
Benjamin Kramer81cfb852010-08-17 19:45:05 +0000649 if (!Target.isAbsolute()) {
Rafael Espindolade89b012010-10-15 18:25:33 +0000650 Symbol = &AliasedSymbol(Target.getSymA()->getSymbol());
Rafael Espindola88182132010-10-27 15:18:17 +0000651 const MCSymbol *Renamed = Renames.lookup(Symbol);
652 if (Renamed)
653 Symbol = Renamed;
Matt Fleming3565a062010-08-16 18:57:57 +0000654 MCSymbolData &SD = Asm.getSymbolData(*Symbol);
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000655 MCFragment *F = SD.getFragment();
Matt Fleming3565a062010-08-16 18:57:57 +0000656
Rafael Espindola9d8b7552010-10-03 00:46:57 +0000657 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
658 const MCSymbol &SymbolB = RefB->getSymbol();
659 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
660 IsPCRel = true;
Rafael Espindola55fb1022010-10-04 15:59:01 +0000661 MCSectionData *Sec = Fragment->getParent();
662
663 // Offset of the symbol in the section
664 int64_t a = Layout.getSymbolAddress(&SDB) - Layout.getSectionAddress(Sec);
665
666 // Ofeset of the relocation in the section
667 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
668 Value += b - a;
Rafael Espindola9d8b7552010-10-03 00:46:57 +0000669 }
670
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000671 // Check that this case has already been fully resolved before we get
672 // here.
Rafael Espindola00074892010-09-17 22:34:41 +0000673 if (Symbol->isDefined() && !SD.isExternal() &&
674 IsPCRel &&
675 &Fragment->getParent()->getSection() == &Symbol->getSection()) {
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000676 llvm_unreachable("We don't need a relocation in this case.");
Rafael Espindola00074892010-09-17 22:34:41 +0000677 return;
678 }
679
Rafael Espindola3729d002010-10-05 23:57:26 +0000680 bool RelocOnSymbol = ShouldRelocOnSymbol(SD, Target, *Fragment);
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000681 if (!RelocOnSymbol) {
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000682 Index = F->getParent()->getOrdinal();
Rafael Espindolaa6489182010-09-24 21:19:03 +0000683
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000684 MCSectionData *FSD = F->getParent();
685 // Offset of the symbol in the section
686 Value += Layout.getSymbolAddress(&SD) - Layout.getSectionAddress(FSD);
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000687 } else {
688 UsedInReloc.insert(Symbol);
689 Index = -1;
690 }
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000691 Addend = Value;
692 // Compensate for the addend on i386.
693 if (Is64Bit)
694 Value = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000695 }
696
Benjamin Kramer95c602a2010-08-27 10:38:39 +0000697 FixedValue = Value;
698
Matt Fleming3565a062010-08-16 18:57:57 +0000699 // determine the type of the relocation
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000700
701 MCSymbolRefExpr::VariantKind Modifier = Target.getSymA()->getKind();
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000702 unsigned Type;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000703 if (Is64Bit) {
704 if (IsPCRel) {
Rafael Espindola92bf6682010-10-04 19:04:13 +0000705 switch (Modifier) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000706 default:
707 llvm_unreachable("Unimplemented");
Rafael Espindola92bf6682010-10-04 19:04:13 +0000708 case MCSymbolRefExpr::VK_None:
709 Type = ELF::R_X86_64_PC32;
710 break;
711 case MCSymbolRefExpr::VK_PLT:
712 Type = ELF::R_X86_64_PLT32;
713 break;
Rafael Espindolaa0a2f872010-10-28 14:22:44 +0000714 case MCSymbolRefExpr::VK_GOTPCREL:
Rafael Espindola607d1f62010-10-04 19:51:39 +0000715 Type = ELF::R_X86_64_GOTPCREL;
716 break;
Rafael Espindolabc82d8b2010-10-27 20:28:07 +0000717 case MCSymbolRefExpr::VK_GOTTPOFF:
718 Type = ELF::R_X86_64_GOTTPOFF;
719 break;
720 case MCSymbolRefExpr::VK_TLSGD:
721 Type = ELF::R_X86_64_TLSGD;
722 break;
Rafael Espindolab4d17212010-10-28 15:02:40 +0000723 case MCSymbolRefExpr::VK_TLSLD:
724 Type = ELF::R_X86_64_TLSLD;
725 break;
Rafael Espindola92bf6682010-10-04 19:04:13 +0000726 }
Benjamin Kramere5b57342010-08-17 18:20:28 +0000727 } else {
728 switch ((unsigned)Fixup.getKind()) {
729 default: llvm_unreachable("invalid fixup kind!");
730 case FK_Data_8: Type = ELF::R_X86_64_64; break;
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000731 case X86::reloc_signed_4byte:
Benjamin Kramere5b57342010-08-17 18:20:28 +0000732 case X86::reloc_pcrel_4byte:
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000733 assert(isInt<32>(Target.getConstant()));
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000734 switch (Modifier) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000735 default:
736 llvm_unreachable("Unimplemented");
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000737 case MCSymbolRefExpr::VK_None:
738 Type = ELF::R_X86_64_32S;
739 break;
740 case MCSymbolRefExpr::VK_GOT:
741 Type = ELF::R_X86_64_GOT32;
742 break;
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000743 case MCSymbolRefExpr::VK_GOTPCREL:
Rafael Espindola8cecf252010-10-06 16:23:36 +0000744 Type = ELF::R_X86_64_GOTPCREL;
745 break;
Rafael Espindolabc82d8b2010-10-27 20:28:07 +0000746 case MCSymbolRefExpr::VK_TPOFF:
747 Type = ELF::R_X86_64_TPOFF32;
748 break;
Rafael Espindolaaa8f1f02010-10-28 15:11:03 +0000749 case MCSymbolRefExpr::VK_DTPOFF:
750 Type = ELF::R_X86_64_DTPOFF32;
751 break;
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000752 }
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000753 break;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000754 case FK_Data_4:
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000755 Type = ELF::R_X86_64_32;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000756 break;
757 case FK_Data_2: Type = ELF::R_X86_64_16; break;
758 case X86::reloc_pcrel_1byte:
759 case FK_Data_1: Type = ELF::R_X86_64_8; break;
760 }
761 }
Matt Fleming3565a062010-08-16 18:57:57 +0000762 } else {
Benjamin Kramere5b57342010-08-17 18:20:28 +0000763 if (IsPCRel) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000764 switch (Modifier) {
765 default:
Rafael Espindola24dc9ec2010-10-18 19:33:01 +0000766 llvm_unreachable("Unimplemented");
767 case MCSymbolRefExpr::VK_None:
Rafael Espindola9baee3b2010-10-18 18:03:28 +0000768 Type = ELF::R_386_PC32;
Rafael Espindola9baee3b2010-10-18 18:03:28 +0000769 break;
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000770 case MCSymbolRefExpr::VK_PLT:
771 Type = ELF::R_386_PLT32;
772 break;
773 }
Benjamin Kramere5b57342010-08-17 18:20:28 +0000774 } else {
775 switch ((unsigned)Fixup.getKind()) {
776 default: llvm_unreachable("invalid fixup kind!");
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000777
Rafael Espindola24ba4f72010-10-24 17:35:42 +0000778 case X86::reloc_global_offset_table:
779 Type = ELF::R_386_GOTPC;
780 break;
781
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000782 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
783 // instead?
784 case X86::reloc_signed_4byte:
Benjamin Kramere5b57342010-08-17 18:20:28 +0000785 case X86::reloc_pcrel_4byte:
Rafael Espindolabd701182010-10-19 19:31:37 +0000786 case FK_Data_4:
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000787 switch (Modifier) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000788 default:
789 llvm_unreachable("Unimplemented");
Rafael Espindolabd701182010-10-19 19:31:37 +0000790 case MCSymbolRefExpr::VK_None:
Rafael Espindola24ba4f72010-10-24 17:35:42 +0000791 Type = ELF::R_386_32;
Rafael Espindolabd701182010-10-19 19:31:37 +0000792 break;
Rafael Espindolaeada3042010-10-18 20:47:21 +0000793 case MCSymbolRefExpr::VK_GOT:
794 Type = ELF::R_386_GOT32;
795 break;
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000796 case MCSymbolRefExpr::VK_GOTOFF:
797 Type = ELF::R_386_GOTOFF;
798 break;
Rafael Espindola3cede2d2010-10-27 21:23:52 +0000799 case MCSymbolRefExpr::VK_TLSGD:
800 Type = ELF::R_386_TLS_GD;
801 break;
802 case MCSymbolRefExpr::VK_TPOFF:
803 Type = ELF::R_386_TLS_LE_32;
804 break;
805 case MCSymbolRefExpr::VK_INDNTPOFF:
806 Type = ELF::R_386_TLS_IE;
807 break;
808 case MCSymbolRefExpr::VK_NTPOFF:
809 Type = ELF::R_386_TLS_LE;
810 break;
Rafael Espindolaa0a2f872010-10-28 14:22:44 +0000811 case MCSymbolRefExpr::VK_GOTNTPOFF:
812 Type = ELF::R_386_TLS_GOTIE;
813 break;
Rafael Espindolaa264f722010-10-28 14:37:09 +0000814 case MCSymbolRefExpr::VK_TLSLDM:
815 Type = ELF::R_386_TLS_LDM;
816 break;
Rafael Espindola0cf15d62010-10-28 14:48:59 +0000817 case MCSymbolRefExpr::VK_DTPOFF:
818 Type = ELF::R_386_TLS_LDO_32;
819 break;
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000820 }
821 break;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000822 case FK_Data_2: Type = ELF::R_386_16; break;
823 case X86::reloc_pcrel_1byte:
824 case FK_Data_1: Type = ELF::R_386_8; break;
825 }
Matt Fleming3565a062010-08-16 18:57:57 +0000826 }
827 }
828
Rafael Espindolaa0a2f872010-10-28 14:22:44 +0000829 if (RelocNeedsGOT(Modifier))
Rafael Espindola5c77c162010-10-05 15:48:37 +0000830 NeedsGOT = true;
831
Benjamin Kramere5b57342010-08-17 18:20:28 +0000832 ELFRelocationEntry ERE;
833
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000834 ERE.Index = Index;
835 ERE.Type = Type;
836 ERE.Symbol = Symbol;
Matt Fleming3565a062010-08-16 18:57:57 +0000837
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000838 ERE.r_offset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
Benjamin Kramere5b57342010-08-17 18:20:28 +0000839
Matt Fleming3565a062010-08-16 18:57:57 +0000840 if (HasRelocationAddend)
841 ERE.r_addend = Addend;
Benjamin Kramer172d7d62010-08-17 00:33:24 +0000842 else
843 ERE.r_addend = 0; // Silence compiler warning.
Matt Fleming3565a062010-08-16 18:57:57 +0000844
845 Relocations[Fragment->getParent()].push_back(ERE);
846}
847
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000848uint64_t
849ELFObjectWriterImpl::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
850 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000851 MCSymbolData &SD = Asm.getSymbolData(*S);
Eli Friedmanf8020a32010-08-16 19:15:06 +0000852
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000853 // Local symbol.
854 if (!SD.isExternal() && !S->isUndefined())
855 return SD.getIndex() + /* empty symbol */ 1;
856
857 // External or undefined symbol.
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000858 return SD.getIndex() + NumRegularSections + /* empty symbol */ 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000859}
860
Rafael Espindola737cd212010-10-05 18:01:23 +0000861static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
Rafael Espindola88182132010-10-27 15:18:17 +0000862 bool Used, bool Renamed) {
Rafael Espindolabd701182010-10-19 19:31:37 +0000863 if (Used)
864 return true;
865
Rafael Espindola88182132010-10-27 15:18:17 +0000866 if (Renamed)
867 return false;
868
Rafael Espindola737cd212010-10-05 18:01:23 +0000869 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000870
871 const MCSymbol &A = AliasedSymbol(Symbol);
872 if (&A != &Symbol && A.isUndefined())
873 return false;
874
Rafael Espindola737cd212010-10-05 18:01:23 +0000875 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
876 return false;
877
Rafael Espindolabd701182010-10-19 19:31:37 +0000878 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000879 return false;
880
881 return true;
882}
883
884static bool isLocal(const MCSymbolData &Data) {
885 if (Data.isExternal())
886 return false;
887
888 const MCSymbol &Symbol = Data.getSymbol();
889 if (Symbol.isUndefined() && !Symbol.isVariable())
890 return false;
891
892 return true;
893}
894
Matt Fleming3565a062010-08-16 18:57:57 +0000895void ELFObjectWriterImpl::ComputeSymbolTable(MCAssembler &Asm) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000896 // FIXME: Is this the correct place to do this?
897 if (NeedsGOT) {
898 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
899 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
900 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
901 Data.setExternal(true);
Rafael Espindolaf571f9a2010-10-28 18:33:03 +0000902 SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindola5c77c162010-10-05 15:48:37 +0000903 }
904
Matt Fleming3565a062010-08-16 18:57:57 +0000905 // Build section lookup table.
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000906 NumRegularSections = Asm.size();
Rafael Espindolaf5c347d2010-10-05 21:20:07 +0000907 DenseMap<const MCSection*, uint32_t> SectionIndexMap;
Matt Fleming3565a062010-08-16 18:57:57 +0000908 unsigned Index = 1;
909 for (MCAssembler::iterator it = Asm.begin(),
910 ie = Asm.end(); it != ie; ++it, ++Index)
911 SectionIndexMap[&it->getSection()] = Index;
912
913 // Index 0 is always the empty string.
914 StringMap<uint64_t> StringIndexMap;
915 StringTable += '\x00';
916
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000917 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000918 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
919 ie = Asm.symbol_end(); it != ie; ++it) {
920 const MCSymbol &Symbol = it->getSymbol();
921
Rafael Espindola88182132010-10-27 15:18:17 +0000922 if (!isInSymtab(Asm, *it, UsedInReloc.count(&Symbol),
923 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000924 continue;
925
Matt Fleming3565a062010-08-16 18:57:57 +0000926 ELFSymbolData MSD;
927 MSD.SymbolData = it;
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000928 bool Local = isLocal(*it);
Rafael Espindola88182132010-10-27 15:18:17 +0000929 const MCSymbol &RefSymbol = AliasedSymbol(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000930
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000931 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000932 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000933 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000934 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000935 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000936 } else if (RefSymbol.isUndefined()) {
Matt Fleming3565a062010-08-16 18:57:57 +0000937 MSD.SectionIndex = ELF::SHN_UNDEF;
Matt Fleming3565a062010-08-16 18:57:57 +0000938 } else {
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000939 MSD.SectionIndex = SectionIndexMap.lookup(&RefSymbol.getSection());
Matt Fleming3565a062010-08-16 18:57:57 +0000940 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000941 }
942
Rafael Espindola88182132010-10-27 15:18:17 +0000943 // The @@@ in symbol version is replaced with @ in undefined symbols and
944 // @@ in defined ones.
945 StringRef Name = Symbol.getName();
946 size_t Pos = Name.find("@@@");
947 std::string FinalName;
948 if (Pos != StringRef::npos) {
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000949 StringRef Prefix = Name.substr(0, Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000950 unsigned n = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000951 StringRef Suffix = Name.substr(Pos + n);
Rafael Espindola88182132010-10-27 15:18:17 +0000952 FinalName = Prefix.str() + Suffix.str();
953 } else {
954 FinalName = Name.str();
955 }
956
957 uint64_t &Entry = StringIndexMap[FinalName];
Rafael Espindolaa6866962010-10-27 14:44:52 +0000958 if (!Entry) {
959 Entry = StringTable.size();
Rafael Espindola88182132010-10-27 15:18:17 +0000960 StringTable += FinalName;
Rafael Espindolaa6866962010-10-27 14:44:52 +0000961 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +0000962 }
Rafael Espindolaa6866962010-10-27 14:44:52 +0000963 MSD.StringIndex = Entry;
964 if (MSD.SectionIndex == ELF::SHN_UNDEF)
965 UndefinedSymbolData.push_back(MSD);
966 else if (Local)
967 LocalSymbolData.push_back(MSD);
968 else
969 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +0000970 }
971
972 // Symbols are required to be in lexicographic order.
973 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
974 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
975 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
976
977 // Set the symbol indices. Local symbols must come before all other
978 // symbols with non-local bindings.
979 Index = 0;
980 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
981 LocalSymbolData[i].SymbolData->setIndex(Index++);
982 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
983 ExternalSymbolData[i].SymbolData->setIndex(Index++);
984 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
985 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
986}
987
988void ELFObjectWriterImpl::WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
989 const MCSectionData &SD) {
990 if (!Relocations[&SD].empty()) {
991 MCContext &Ctx = Asm.getContext();
992 const MCSection *RelaSection;
993 const MCSectionELF &Section =
994 static_cast<const MCSectionELF&>(SD.getSection());
995
996 const StringRef SectionName = Section.getSectionName();
Benjamin Kramer377a5722010-08-17 17:30:07 +0000997 std::string RelaSectionName = HasRelocationAddend ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +0000998 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000999
1000 unsigned EntrySize;
1001 if (HasRelocationAddend)
1002 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
1003 else
1004 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +00001005
Benjamin Kramer377a5722010-08-17 17:30:07 +00001006 RelaSection = Ctx.getELFSection(RelaSectionName, HasRelocationAddend ?
1007 ELF::SHT_RELA : ELF::SHT_REL, 0,
Matt Fleming3565a062010-08-16 18:57:57 +00001008 SectionKind::getReadOnly(),
1009 false, EntrySize);
1010
1011 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001012 RelaSD.setAlignment(Is64Bit ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +00001013
1014 MCDataFragment *F = new MCDataFragment(&RelaSD);
1015
1016 WriteRelocationsFragment(Asm, F, &SD);
1017
Rafael Espindola70703872010-09-30 02:22:20 +00001018 Asm.AddSectionToTheEnd(*Writer, RelaSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +00001019 }
1020}
1021
1022void ELFObjectWriterImpl::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
1023 uint64_t Flags, uint64_t Address,
1024 uint64_t Offset, uint64_t Size,
1025 uint32_t Link, uint32_t Info,
1026 uint64_t Alignment,
1027 uint64_t EntrySize) {
1028 Write32(Name); // sh_name: index into string table
1029 Write32(Type); // sh_type
1030 WriteWord(Flags); // sh_flags
1031 WriteWord(Address); // sh_addr
1032 WriteWord(Offset); // sh_offset
1033 WriteWord(Size); // sh_size
1034 Write32(Link); // sh_link
1035 Write32(Info); // sh_info
1036 WriteWord(Alignment); // sh_addralign
1037 WriteWord(EntrySize); // sh_entsize
1038}
1039
1040void ELFObjectWriterImpl::WriteRelocationsFragment(const MCAssembler &Asm,
1041 MCDataFragment *F,
1042 const MCSectionData *SD) {
1043 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
1044 // sort by the r_offset just like gnu as does
1045 array_pod_sort(Relocs.begin(), Relocs.end());
1046
1047 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1048 ELFRelocationEntry entry = Relocs[e - i - 1];
1049
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001050 if (entry.Index < 0)
1051 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
1052 else
1053 entry.Index += LocalSymbolData.size() + 1;
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001054 if (Is64Bit) {
1055 char buf[8];
Matt Fleming3565a062010-08-16 18:57:57 +00001056
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001057 String64(buf, entry.r_offset);
1058 F->getContents() += StringRef(buf, 8);
1059
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001060 struct ELF::Elf64_Rela ERE64;
1061 ERE64.setSymbolAndType(entry.Index, entry.Type);
1062 String64(buf, ERE64.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001063 F->getContents() += StringRef(buf, 8);
1064
1065 if (HasRelocationAddend) {
1066 String64(buf, entry.r_addend);
1067 F->getContents() += StringRef(buf, 8);
1068 }
1069 } else {
1070 char buf[4];
1071
1072 String32(buf, entry.r_offset);
1073 F->getContents() += StringRef(buf, 4);
1074
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001075 struct ELF::Elf32_Rela ERE32;
1076 ERE32.setSymbolAndType(entry.Index, entry.Type);
1077 String32(buf, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001078 F->getContents() += StringRef(buf, 4);
1079
1080 if (HasRelocationAddend) {
1081 String32(buf, entry.r_addend);
1082 F->getContents() += StringRef(buf, 4);
1083 }
1084 }
Matt Fleming3565a062010-08-16 18:57:57 +00001085 }
1086}
1087
1088void ELFObjectWriterImpl::CreateMetadataSections(MCAssembler &Asm,
1089 MCAsmLayout &Layout) {
1090 MCContext &Ctx = Asm.getContext();
1091 MCDataFragment *F;
1092
Matt Fleming3565a062010-08-16 18:57:57 +00001093 const MCSection *SymtabSection;
1094 unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
1095
Rafael Espindola71859c62010-09-16 19:46:31 +00001096 unsigned NumRegularSections = Asm.size();
1097
Rafael Espindola38738bf2010-09-22 19:04:41 +00001098 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola71859c62010-09-16 19:46:31 +00001099 const MCSection *ShstrtabSection;
1100 ShstrtabSection = Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
1101 SectionKind::getReadOnly(), false);
1102 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1103 ShstrtabSD.setAlignment(1);
1104 ShstrtabIndex = Asm.size();
1105
Matt Fleming3565a062010-08-16 18:57:57 +00001106 SymtabSection = Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1107 SectionKind::getReadOnly(),
1108 false, EntrySize);
Matt Fleming3565a062010-08-16 18:57:57 +00001109 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Matt Fleming3565a062010-08-16 18:57:57 +00001110 SymtabSD.setAlignment(Is64Bit ? 8 : 4);
1111
Matt Fleming3565a062010-08-16 18:57:57 +00001112 const MCSection *StrtabSection;
1113 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
1114 SectionKind::getReadOnly(), false);
Matt Fleming3565a062010-08-16 18:57:57 +00001115 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1116 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +00001117 StringTableIndex = Asm.size();
1118
Rafael Espindolac3c413f2010-09-27 22:04:54 +00001119 WriteRelocations(Asm, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001120
1121 // Symbol table
1122 F = new MCDataFragment(&SymtabSD);
1123 WriteSymbolTable(F, Asm, Layout, NumRegularSections);
Rafael Espindola70703872010-09-30 02:22:20 +00001124 Asm.AddSectionToTheEnd(*Writer, SymtabSD, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001125
Matt Fleming3565a062010-08-16 18:57:57 +00001126 F = new MCDataFragment(&StrtabSD);
1127 F->getContents().append(StringTable.begin(), StringTable.end());
Rafael Espindola70703872010-09-30 02:22:20 +00001128 Asm.AddSectionToTheEnd(*Writer, StrtabSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +00001129
Matt Fleming3565a062010-08-16 18:57:57 +00001130 F = new MCDataFragment(&ShstrtabSD);
1131
Matt Fleming3565a062010-08-16 18:57:57 +00001132 // Section header string table.
1133 //
1134 // The first entry of a string table holds a null character so skip
1135 // section 0.
1136 uint64_t Index = 1;
1137 F->getContents() += '\x00';
1138
1139 for (MCAssembler::const_iterator it = Asm.begin(),
1140 ie = Asm.end(); it != ie; ++it) {
Matt Fleming3565a062010-08-16 18:57:57 +00001141 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +00001142 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola51efe7a2010-09-23 14:14:56 +00001143 // FIXME: We could merge suffixes like in .text and .rela.text.
Matt Fleming3565a062010-08-16 18:57:57 +00001144
1145 // Remember the index into the string table so we can write it
1146 // into the sh_name field of the section header table.
1147 SectionStringTableIndex[&it->getSection()] = Index;
1148
1149 Index += Section.getSectionName().size() + 1;
1150 F->getContents() += Section.getSectionName();
1151 F->getContents() += '\x00';
1152 }
1153
Rafael Espindola70703872010-09-30 02:22:20 +00001154 Asm.AddSectionToTheEnd(*Writer, ShstrtabSD, Layout);
1155}
1156
1157bool ELFObjectWriterImpl::IsFixupFullyResolved(const MCAssembler &Asm,
1158 const MCValue Target,
1159 bool IsPCRel,
1160 const MCFragment *DF) const {
1161 // If this is a PCrel relocation, find the section this fixup value is
1162 // relative to.
1163 const MCSection *BaseSection = 0;
1164 if (IsPCRel) {
1165 BaseSection = &DF->getParent()->getSection();
1166 assert(BaseSection);
1167 }
1168
1169 const MCSection *SectionA = 0;
1170 const MCSymbol *SymbolA = 0;
1171 if (const MCSymbolRefExpr *A = Target.getSymA()) {
1172 SymbolA = &A->getSymbol();
1173 SectionA = &SymbolA->getSection();
1174 }
1175
1176 const MCSection *SectionB = 0;
1177 if (const MCSymbolRefExpr *B = Target.getSymB()) {
1178 SectionB = &B->getSymbol().getSection();
1179 }
1180
1181 if (!BaseSection)
1182 return SectionA == SectionB;
1183
1184 const MCSymbolData &DataA = Asm.getSymbolData(*SymbolA);
1185 if (DataA.isExternal())
1186 return false;
1187
1188 return !SectionB && BaseSection == SectionA;
Matt Fleming3565a062010-08-16 18:57:57 +00001189}
1190
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001191void ELFObjectWriterImpl::WriteObject(MCAssembler &Asm,
Matt Fleming3565a062010-08-16 18:57:57 +00001192 const MCAsmLayout &Layout) {
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001193 // Compute symbol table information.
1194 ComputeSymbolTable(Asm);
1195
Matt Fleming3565a062010-08-16 18:57:57 +00001196 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
1197 const_cast<MCAsmLayout&>(Layout));
1198
1199 // Add 1 for the null section.
1200 unsigned NumSections = Asm.size() + 1;
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001201 uint64_t NaturalAlignment = Is64Bit ? 8 : 4;
1202 uint64_t HeaderSize = Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr);
1203 uint64_t FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001204
1205 for (MCAssembler::const_iterator it = Asm.begin(),
1206 ie = Asm.end(); it != ie; ++it) {
1207 const MCSectionData &SD = *it;
Matt Fleming3565a062010-08-16 18:57:57 +00001208
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001209 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1210
Matt Fleming3565a062010-08-16 18:57:57 +00001211 // Get the size of the section in the output file (including padding).
1212 uint64_t Size = Layout.getSectionFileSize(&SD);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001213
1214 FileOff += Size;
Matt Fleming3565a062010-08-16 18:57:57 +00001215 }
1216
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001217 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1218
Matt Fleming3565a062010-08-16 18:57:57 +00001219 // Write out the ELF header ...
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001220 WriteHeader(FileOff - HeaderSize, NumSections);
1221
1222 FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001223
1224 // ... then all of the sections ...
1225 DenseMap<const MCSection*, uint64_t> SectionOffsetMap;
1226
Rafael Espindolad8e0bfe2010-10-06 22:28:19 +00001227 DenseMap<const MCSection*, uint32_t> SectionIndexMap;
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001228
1229 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +00001230 for (MCAssembler::const_iterator it = Asm.begin(),
1231 ie = Asm.end(); it != ie; ++it) {
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001232 const MCSectionData &SD = *it;
1233
1234 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1235 WriteZeros(Padding);
1236 FileOff += Padding;
1237
Matt Fleming3565a062010-08-16 18:57:57 +00001238 // Remember the offset into the file for this section.
1239 SectionOffsetMap[&it->getSection()] = FileOff;
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001240 SectionIndexMap[&it->getSection()] = Index++;
1241
Matt Fleming3565a062010-08-16 18:57:57 +00001242 FileOff += Layout.getSectionFileSize(&SD);
1243
1244 Asm.WriteSectionData(it, Layout, Writer);
1245 }
1246
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001247 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1248 WriteZeros(Padding);
1249 FileOff += Padding;
1250
Matt Fleming3565a062010-08-16 18:57:57 +00001251 // ... and then the section header table.
1252 // Should we align the section header table?
1253 //
1254 // Null section first.
1255 WriteSecHdrEntry(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1256
1257 for (MCAssembler::const_iterator it = Asm.begin(),
1258 ie = Asm.end(); it != ie; ++it) {
1259 const MCSectionData &SD = *it;
1260 const MCSectionELF &Section =
1261 static_cast<const MCSectionELF&>(SD.getSection());
1262
1263 uint64_t sh_link = 0;
1264 uint64_t sh_info = 0;
1265
1266 switch(Section.getType()) {
1267 case ELF::SHT_DYNAMIC:
1268 sh_link = SectionStringTableIndex[&it->getSection()];
1269 sh_info = 0;
1270 break;
1271
1272 case ELF::SHT_REL:
Eli Friedmanf8020a32010-08-16 19:15:06 +00001273 case ELF::SHT_RELA: {
Matt Fleming3565a062010-08-16 18:57:57 +00001274 const MCSection *SymtabSection;
1275 const MCSection *InfoSection;
Matt Fleming3565a062010-08-16 18:57:57 +00001276
1277 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1278 SectionKind::getReadOnly(),
Eli Friedmanf8020a32010-08-16 19:15:06 +00001279 false);
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001280 sh_link = SectionIndexMap[SymtabSection];
Matt Fleming3565a062010-08-16 18:57:57 +00001281
Benjamin Kramer377a5722010-08-17 17:30:07 +00001282 // Remove ".rel" and ".rela" prefixes.
1283 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1284 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
1285
Eli Friedmanf8020a32010-08-16 19:15:06 +00001286 InfoSection = Asm.getContext().getELFSection(SectionName,
Matt Fleming3565a062010-08-16 18:57:57 +00001287 ELF::SHT_PROGBITS, 0,
Eli Friedmanf8020a32010-08-16 19:15:06 +00001288 SectionKind::getReadOnly(),
1289 false);
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001290 sh_info = SectionIndexMap[InfoSection];
Matt Fleming3565a062010-08-16 18:57:57 +00001291 break;
Eli Friedmanf8020a32010-08-16 19:15:06 +00001292 }
Matt Fleming3565a062010-08-16 18:57:57 +00001293
1294 case ELF::SHT_SYMTAB:
1295 case ELF::SHT_DYNSYM:
1296 sh_link = StringTableIndex;
1297 sh_info = LastLocalSymbolIndex;
1298 break;
1299
1300 case ELF::SHT_PROGBITS:
1301 case ELF::SHT_STRTAB:
1302 case ELF::SHT_NOBITS:
Benjamin Kramer19dc7fa2010-08-31 17:03:33 +00001303 case ELF::SHT_NULL:
Rafael Espindolacecbc3d2010-10-25 17:50:35 +00001304 case ELF::SHT_ARM_ATTRIBUTES:
Matt Fleming3565a062010-08-16 18:57:57 +00001305 // Nothing to do.
1306 break;
1307
Matt Fleming3565a062010-08-16 18:57:57 +00001308 default:
1309 assert(0 && "FIXME: sh_type value not supported!");
1310 break;
1311 }
1312
1313 WriteSecHdrEntry(SectionStringTableIndex[&it->getSection()],
1314 Section.getType(), Section.getFlags(),
Rafael Espindola71859c62010-09-16 19:46:31 +00001315 0,
Matt Fleming3565a062010-08-16 18:57:57 +00001316 SectionOffsetMap.lookup(&SD.getSection()),
1317 Layout.getSectionSize(&SD), sh_link,
1318 sh_info, SD.getAlignment(),
1319 Section.getEntrySize());
1320 }
1321}
1322
1323ELFObjectWriter::ELFObjectWriter(raw_ostream &OS,
1324 bool Is64Bit,
Roman Divacky5baf79e2010-09-09 17:57:50 +00001325 Triple::OSType OSType,
Wesley Peckeecb8582010-10-22 15:52:49 +00001326 uint16_t EMachine,
Matt Fleming3565a062010-08-16 18:57:57 +00001327 bool IsLittleEndian,
1328 bool HasRelocationAddend)
1329 : MCObjectWriter(OS, IsLittleEndian)
1330{
Wesley Peckeecb8582010-10-22 15:52:49 +00001331 Impl = new ELFObjectWriterImpl(this, Is64Bit, EMachine,
1332 HasRelocationAddend, OSType);
Matt Fleming3565a062010-08-16 18:57:57 +00001333}
1334
1335ELFObjectWriter::~ELFObjectWriter() {
1336 delete (ELFObjectWriterImpl*) Impl;
1337}
1338
1339void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm) {
1340 ((ELFObjectWriterImpl*) Impl)->ExecutePostLayoutBinding(Asm);
1341}
1342
1343void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
1344 const MCAsmLayout &Layout,
1345 const MCFragment *Fragment,
1346 const MCFixup &Fixup, MCValue Target,
1347 uint64_t &FixedValue) {
1348 ((ELFObjectWriterImpl*) Impl)->RecordRelocation(Asm, Layout, Fragment, Fixup,
1349 Target, FixedValue);
1350}
1351
Rafael Espindola70703872010-09-30 02:22:20 +00001352bool ELFObjectWriter::IsFixupFullyResolved(const MCAssembler &Asm,
1353 const MCValue Target,
1354 bool IsPCRel,
1355 const MCFragment *DF) const {
1356 return ((ELFObjectWriterImpl*) Impl)->IsFixupFullyResolved(Asm, Target,
1357 IsPCRel, DF);
1358}
1359
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001360void ELFObjectWriter::WriteObject(MCAssembler &Asm,
Matt Fleming3565a062010-08-16 18:57:57 +00001361 const MCAsmLayout &Layout) {
1362 ((ELFObjectWriterImpl*) Impl)->WriteObject(Asm, Layout);
1363}