blob: 5e11a1553f80c67675139717e7bc20702c5c88c1 [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 Espindola5c77c162010-10-05 15:48:37 +000081static bool RelocNeedsGOT(unsigned Type) {
82 switch (Type) {
83 default:
84 return false;
85 case ELF::R_X86_64_GOT32:
86 case ELF::R_X86_64_PLT32:
87 case ELF::R_X86_64_GOTPCREL:
88 return true;
89 }
90}
91
Matt Fleming3565a062010-08-16 18:57:57 +000092namespace {
93
94 class ELFObjectWriterImpl {
Chris Lattnerb188a372010-08-28 03:21:03 +000095 /*static bool isFixupKindX86RIPRel(unsigned Kind) {
Matt Fleming3565a062010-08-16 18:57:57 +000096 return Kind == X86::reloc_riprel_4byte ||
97 Kind == X86::reloc_riprel_4byte_movq_load;
Chris Lattnerb188a372010-08-28 03:21:03 +000098 }*/
Matt Fleming3565a062010-08-16 18:57:57 +000099
100
101 /// ELFSymbolData - Helper struct for containing some precomputed information
102 /// on symbols.
103 struct ELFSymbolData {
104 MCSymbolData *SymbolData;
105 uint64_t StringIndex;
106 uint32_t SectionIndex;
107
108 // Support lexicographic sorting.
109 bool operator<(const ELFSymbolData &RHS) const {
Rafael Espindolaad49cf52010-09-18 15:03:21 +0000110 if (GetType(*SymbolData) == ELF::STT_FILE)
111 return true;
112 if (GetType(*RHS.SymbolData) == ELF::STT_FILE)
113 return false;
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000114 return SymbolData->getSymbol().getName() <
115 RHS.SymbolData->getSymbol().getName();
Matt Fleming3565a062010-08-16 18:57:57 +0000116 }
117 };
118
119 /// @name Relocation Data
120 /// @{
121
122 struct ELFRelocationEntry {
123 // Make these big enough for both 32-bit and 64-bit
124 uint64_t r_offset;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000125 int Index;
126 unsigned Type;
127 const MCSymbol *Symbol;
Matt Fleming3565a062010-08-16 18:57:57 +0000128 uint64_t r_addend;
129
130 // Support lexicographic sorting.
131 bool operator<(const ELFRelocationEntry &RE) const {
132 return RE.r_offset < r_offset;
133 }
134 };
135
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000136 SmallPtrSet<const MCSymbol *, 16> UsedInReloc;
Rafael Espindola88182132010-10-27 15:18:17 +0000137 DenseMap<const MCSymbol *, const MCSymbol *> Renames;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000138
Matt Fleming3565a062010-08-16 18:57:57 +0000139 llvm::DenseMap<const MCSectionData*,
140 std::vector<ELFRelocationEntry> > Relocations;
141 DenseMap<const MCSection*, uint64_t> SectionStringTableIndex;
142
143 /// @}
144 /// @name Symbol Table Data
145 /// @{
146
147 SmallString<256> StringTable;
148 std::vector<ELFSymbolData> LocalSymbolData;
149 std::vector<ELFSymbolData> ExternalSymbolData;
150 std::vector<ELFSymbolData> UndefinedSymbolData;
151
152 /// @}
153
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000154 int NumRegularSections;
155
Rafael Espindola5c77c162010-10-05 15:48:37 +0000156 bool NeedsGOT;
157
Matt Fleming3565a062010-08-16 18:57:57 +0000158 ELFObjectWriter *Writer;
159
160 raw_ostream &OS;
161
Matt Fleming3565a062010-08-16 18:57:57 +0000162 unsigned Is64Bit : 1;
163
164 bool HasRelocationAddend;
165
Roman Divacky5baf79e2010-09-09 17:57:50 +0000166 Triple::OSType OSType;
167
Wesley Peckeecb8582010-10-22 15:52:49 +0000168 uint16_t EMachine;
169
Matt Fleming3565a062010-08-16 18:57:57 +0000170 // This holds the symbol table index of the last local symbol.
171 unsigned LastLocalSymbolIndex;
172 // This holds the .strtab section index.
173 unsigned StringTableIndex;
174
175 unsigned ShstrtabIndex;
176
177 public:
178 ELFObjectWriterImpl(ELFObjectWriter *_Writer, bool _Is64Bit,
Wesley Peckeecb8582010-10-22 15:52:49 +0000179 uint16_t _EMachine, bool _HasRelAddend,
180 Triple::OSType _OSType)
Rafael Espindola5c77c162010-10-05 15:48:37 +0000181 : NeedsGOT(false), Writer(_Writer), OS(Writer->getStream()),
Roman Divacky5baf79e2010-09-09 17:57:50 +0000182 Is64Bit(_Is64Bit), HasRelocationAddend(_HasRelAddend),
Wesley Peckeecb8582010-10-22 15:52:49 +0000183 OSType(_OSType), EMachine(_EMachine) {
Matt Fleming3565a062010-08-16 18:57:57 +0000184 }
185
186 void Write8(uint8_t Value) { Writer->Write8(Value); }
187 void Write16(uint16_t Value) { Writer->Write16(Value); }
188 void Write32(uint32_t Value) { Writer->Write32(Value); }
Chris Lattnerb188a372010-08-28 03:21:03 +0000189 //void Write64(uint64_t Value) { Writer->Write64(Value); }
Matt Fleming3565a062010-08-16 18:57:57 +0000190 void WriteZeros(unsigned N) { Writer->WriteZeros(N); }
Chris Lattnerb188a372010-08-28 03:21:03 +0000191 //void WriteBytes(StringRef Str, unsigned ZeroFillSize = 0) {
192 // Writer->WriteBytes(Str, ZeroFillSize);
193 //}
Matt Fleming3565a062010-08-16 18:57:57 +0000194
195 void WriteWord(uint64_t W) {
Chris Lattnerb188a372010-08-28 03:21:03 +0000196 if (Is64Bit)
Matt Fleming3565a062010-08-16 18:57:57 +0000197 Writer->Write64(W);
Chris Lattnerb188a372010-08-28 03:21:03 +0000198 else
Matt Fleming3565a062010-08-16 18:57:57 +0000199 Writer->Write32(W);
Matt Fleming3565a062010-08-16 18:57:57 +0000200 }
201
202 void String8(char *buf, uint8_t Value) {
203 buf[0] = Value;
204 }
205
206 void StringLE16(char *buf, uint16_t Value) {
207 buf[0] = char(Value >> 0);
208 buf[1] = char(Value >> 8);
209 }
210
211 void StringLE32(char *buf, uint32_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000212 StringLE16(buf, uint16_t(Value >> 0));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000213 StringLE16(buf + 2, uint16_t(Value >> 16));
Matt Fleming3565a062010-08-16 18:57:57 +0000214 }
215
216 void StringLE64(char *buf, uint64_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000217 StringLE32(buf, uint32_t(Value >> 0));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000218 StringLE32(buf + 4, uint32_t(Value >> 32));
Matt Fleming3565a062010-08-16 18:57:57 +0000219 }
220
221 void StringBE16(char *buf ,uint16_t Value) {
222 buf[0] = char(Value >> 8);
223 buf[1] = char(Value >> 0);
224 }
225
226 void StringBE32(char *buf, uint32_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000227 StringBE16(buf, uint16_t(Value >> 16));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000228 StringBE16(buf + 2, uint16_t(Value >> 0));
Matt Fleming3565a062010-08-16 18:57:57 +0000229 }
230
231 void StringBE64(char *buf, uint64_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000232 StringBE32(buf, uint32_t(Value >> 32));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000233 StringBE32(buf + 4, uint32_t(Value >> 0));
Matt Fleming3565a062010-08-16 18:57:57 +0000234 }
235
236 void String16(char *buf, uint16_t Value) {
237 if (Writer->isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000238 StringLE16(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000239 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000240 StringBE16(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000241 }
242
243 void String32(char *buf, uint32_t Value) {
244 if (Writer->isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000245 StringLE32(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000246 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000247 StringBE32(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000248 }
249
250 void String64(char *buf, uint64_t Value) {
251 if (Writer->isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000252 StringLE64(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000253 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000254 StringBE64(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000255 }
256
257 void WriteHeader(uint64_t SectionDataSize, unsigned NumberOfSections);
258
259 void WriteSymbolEntry(MCDataFragment *F, uint64_t name, uint8_t info,
260 uint64_t value, uint64_t size,
261 uint8_t other, uint16_t shndx);
262
263 void WriteSymbol(MCDataFragment *F, ELFSymbolData &MSD,
264 const MCAsmLayout &Layout);
265
266 void WriteSymbolTable(MCDataFragment *F, const MCAssembler &Asm,
Rafael Espindola71859c62010-09-16 19:46:31 +0000267 const MCAsmLayout &Layout,
268 unsigned NumRegularSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000269
270 void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
271 const MCFragment *Fragment, const MCFixup &Fixup,
272 MCValue Target, uint64_t &FixedValue);
273
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000274 uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm,
275 const MCSymbol *S);
Matt Fleming3565a062010-08-16 18:57:57 +0000276
277 /// ComputeSymbolTable - Compute the symbol table data
278 ///
279 /// \param StringTable [out] - The string table data.
280 /// \param StringIndexMap [out] - Map from symbol names to offsets in the
281 /// string table.
282 void ComputeSymbolTable(MCAssembler &Asm);
283
284 void WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
285 const MCSectionData &SD);
286
287 void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout) {
288 for (MCAssembler::const_iterator it = Asm.begin(),
289 ie = Asm.end(); it != ie; ++it) {
290 WriteRelocation(Asm, Layout, *it);
291 }
292 }
293
294 void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout);
295
Rafael Espindola88182132010-10-27 15:18:17 +0000296 void ExecutePostLayoutBinding(MCAssembler &Asm);
Matt Fleming3565a062010-08-16 18:57:57 +0000297
298 void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
299 uint64_t Address, uint64_t Offset,
300 uint64_t Size, uint32_t Link, uint32_t Info,
301 uint64_t Alignment, uint64_t EntrySize);
302
303 void WriteRelocationsFragment(const MCAssembler &Asm, MCDataFragment *F,
304 const MCSectionData *SD);
305
Rafael Espindola70703872010-09-30 02:22:20 +0000306 bool IsFixupFullyResolved(const MCAssembler &Asm,
307 const MCValue Target,
308 bool IsPCRel,
309 const MCFragment *DF) const;
310
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000311 void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000312 };
313
314}
315
316// Emit the ELF header.
317void ELFObjectWriterImpl::WriteHeader(uint64_t SectionDataSize,
318 unsigned NumberOfSections) {
319 // ELF Header
320 // ----------
321 //
322 // Note
323 // ----
324 // emitWord method behaves differently for ELF32 and ELF64, writing
325 // 4 bytes in the former and 8 in the latter.
326
327 Write8(0x7f); // e_ident[EI_MAG0]
328 Write8('E'); // e_ident[EI_MAG1]
329 Write8('L'); // e_ident[EI_MAG2]
330 Write8('F'); // e_ident[EI_MAG3]
331
332 Write8(Is64Bit ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
333
334 // e_ident[EI_DATA]
335 Write8(Writer->isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
336
337 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky5baf79e2010-09-09 17:57:50 +0000338 // e_ident[EI_OSABI]
339 switch (OSType) {
340 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
341 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
342 default: Write8(ELF::ELFOSABI_NONE); break;
343 }
Matt Fleming3565a062010-08-16 18:57:57 +0000344 Write8(0); // e_ident[EI_ABIVERSION]
345
346 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
347
348 Write16(ELF::ET_REL); // e_type
349
Wesley Peckeecb8582010-10-22 15:52:49 +0000350 Write16(EMachine); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +0000351
352 Write32(ELF::EV_CURRENT); // e_version
353 WriteWord(0); // e_entry, no entry point in .o file
354 WriteWord(0); // e_phoff, no program header for .o
Benjamin Kramereb976772010-08-17 17:02:29 +0000355 WriteWord(SectionDataSize + (Is64Bit ? sizeof(ELF::Elf64_Ehdr) :
356 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000357
358 // FIXME: Make this configurable.
359 Write32(0); // e_flags = whatever the target wants
360
361 // e_ehsize = ELF header size
362 Write16(Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
363
364 Write16(0); // e_phentsize = prog header entry size
365 Write16(0); // e_phnum = # prog header entries = 0
366
367 // e_shentsize = Section header entry size
368 Write16(Is64Bit ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
369
370 // e_shnum = # of section header ents
371 Write16(NumberOfSections);
372
373 // e_shstrndx = Section # of '.shstrtab'
374 Write16(ShstrtabIndex);
375}
376
377void ELFObjectWriterImpl::WriteSymbolEntry(MCDataFragment *F, uint64_t name,
378 uint8_t info, uint64_t value,
379 uint64_t size, uint8_t other,
380 uint16_t shndx) {
381 if (Is64Bit) {
382 char buf[8];
383
384 String32(buf, name);
385 F->getContents() += StringRef(buf, 4); // st_name
386
387 String8(buf, info);
388 F->getContents() += StringRef(buf, 1); // st_info
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000389
Matt Fleming3565a062010-08-16 18:57:57 +0000390 String8(buf, other);
391 F->getContents() += StringRef(buf, 1); // st_other
392
393 String16(buf, shndx);
394 F->getContents() += StringRef(buf, 2); // st_shndx
395
396 String64(buf, value);
397 F->getContents() += StringRef(buf, 8); // st_value
398
399 String64(buf, size);
400 F->getContents() += StringRef(buf, 8); // st_size
401 } else {
402 char buf[4];
403
404 String32(buf, name);
405 F->getContents() += StringRef(buf, 4); // st_name
406
407 String32(buf, value);
408 F->getContents() += StringRef(buf, 4); // st_value
409
410 String32(buf, size);
411 F->getContents() += StringRef(buf, 4); // st_size
412
413 String8(buf, info);
414 F->getContents() += StringRef(buf, 1); // st_info
415
416 String8(buf, other);
417 F->getContents() += StringRef(buf, 1); // st_other
418
419 String16(buf, shndx);
420 F->getContents() += StringRef(buf, 2); // st_shndx
421 }
422}
423
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000424static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout) {
425 if (Data.isCommon() && Data.isExternal())
426 return Data.getCommonAlignment();
427
428 const MCSymbol &Symbol = Data.getSymbol();
429 if (!Symbol.isInSection())
430 return 0;
431
432 if (!Data.isCommon() && !(Data.getFlags() & ELF_STB_Weak))
433 if (MCFragment *FF = Data.getFragment())
434 return Layout.getSymbolAddress(&Data) -
435 Layout.getSectionAddress(FF->getParent());
436
437 return 0;
438}
439
Rafael Espindolade89b012010-10-15 18:25:33 +0000440static const MCSymbol &AliasedSymbol(const MCSymbol &Symbol) {
441 const MCSymbol *S = &Symbol;
442 while (S->isVariable()) {
443 const MCExpr *Value = S->getVariableValue();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000444 if (Value->getKind() != MCExpr::SymbolRef)
445 return *S;
Rafael Espindolade89b012010-10-15 18:25:33 +0000446 const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr*>(Value);
447 S = &Ref->getSymbol();
448 }
449 return *S;
450}
451
Rafael Espindola88182132010-10-27 15:18:17 +0000452void ELFObjectWriterImpl::ExecutePostLayoutBinding(MCAssembler &Asm) {
453 // The presence of symbol versions causes undefined symbols and
454 // versions declared with @@@ to be renamed.
455
456 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
457 ie = Asm.symbol_end(); it != ie; ++it) {
458 const MCSymbol &Alias = it->getSymbol();
459 if (!Alias.isVariable())
460 continue;
461 const MCSymbol &Symbol = AliasedSymbol(Alias);
462 const StringRef &AliasName = Alias.getName();
463 size_t Pos = AliasName.find('@');
464 if (Pos == StringRef::npos)
465 continue;
466
467 StringRef Rest(AliasName.begin() + Pos);
468 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
469 continue;
470
471 std::pair<const MCSymbol *, const MCSymbol *> t(&Symbol, &Alias);
472 Renames.insert(t);
473 }
474}
475
Matt Fleming3565a062010-08-16 18:57:57 +0000476void ELFObjectWriterImpl::WriteSymbol(MCDataFragment *F, ELFSymbolData &MSD,
477 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000478 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000479 MCSymbolData &Data =
480 Layout.getAssembler().getSymbolData(AliasedSymbol(OrigData.getSymbol()));
Rafael Espindola152c1062010-10-06 21:02:29 +0000481
482 uint8_t Binding = GetBinding(OrigData);
483 uint8_t Visibility = GetVisibility(OrigData);
484 uint8_t Type = GetType(Data);
485
486 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
487 uint8_t Other = Visibility;
488
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000489 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000490 uint64_t Size = 0;
491 const MCExpr *ESize;
492
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000493 assert(!(Data.isCommon() && !Data.isExternal()));
494
Matt Fleming3565a062010-08-16 18:57:57 +0000495 ESize = Data.getSize();
496 if (Data.getSize()) {
497 MCValue Res;
498 if (ESize->getKind() == MCExpr::Binary) {
499 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(ESize);
500
501 if (BE->EvaluateAsRelocatable(Res, &Layout)) {
Benjamin Kramer24f12062010-10-17 07:38:40 +0000502 assert(!Res.getSymA() || !Res.getSymA()->getSymbol().isDefined());
503 assert(!Res.getSymB() || !Res.getSymB()->getSymbol().isDefined());
Rafael Espindolaf230df92010-10-16 18:23:53 +0000504 Size = Res.getConstant();
Matt Fleming3565a062010-08-16 18:57:57 +0000505 }
506 } else if (ESize->getKind() == MCExpr::Constant) {
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000507 Size = static_cast<const MCConstantExpr *>(ESize)->getValue();
Matt Fleming3565a062010-08-16 18:57:57 +0000508 } else {
509 assert(0 && "Unsupported size expression");
510 }
511 }
512
513 // Write out the symbol table entry
514 WriteSymbolEntry(F, MSD.StringIndex, Info, Value,
515 Size, Other, MSD.SectionIndex);
516}
517
518void ELFObjectWriterImpl::WriteSymbolTable(MCDataFragment *F,
519 const MCAssembler &Asm,
Rafael Espindola71859c62010-09-16 19:46:31 +0000520 const MCAsmLayout &Layout,
521 unsigned NumRegularSections) {
Matt Fleming3565a062010-08-16 18:57:57 +0000522 // The string table must be emitted first because we need the index
523 // into the string table for all the symbol names.
524 assert(StringTable.size() && "Missing string table");
525
526 // FIXME: Make sure the start of the symbol table is aligned.
527
528 // The first entry is the undefined symbol entry.
529 unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000530 F->getContents().append(EntrySize, '\x00');
Matt Fleming3565a062010-08-16 18:57:57 +0000531
532 // Write the symbol table entries.
533 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
534 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
535 ELFSymbolData &MSD = LocalSymbolData[i];
536 WriteSymbol(F, MSD, Layout);
537 }
538
Rafael Espindola71859c62010-09-16 19:46:31 +0000539 // Write out a symbol table entry for each regular section.
Eli Friedmana44fa242010-08-16 21:17:09 +0000540 unsigned Index = 1;
Rafael Espindola71859c62010-09-16 19:46:31 +0000541 for (MCAssembler::const_iterator it = Asm.begin();
542 Index <= NumRegularSections; ++it, ++Index) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000543 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000544 static_cast<const MCSectionELF&>(it->getSection());
Eli Friedmana44fa242010-08-16 21:17:09 +0000545 // Leave out relocations so we don't have indexes within
546 // the relocations messed up
Benjamin Kramer377a5722010-08-17 17:30:07 +0000547 if (Section.getType() == ELF::SHT_RELA || Section.getType() == ELF::SHT_REL)
Eli Friedmana44fa242010-08-16 21:17:09 +0000548 continue;
Matt Fleming3565a062010-08-16 18:57:57 +0000549 WriteSymbolEntry(F, 0, ELF::STT_SECTION, 0, 0, ELF::STV_DEFAULT, Index);
Eli Friedmana44fa242010-08-16 21:17:09 +0000550 LastLocalSymbolIndex++;
551 }
Matt Fleming3565a062010-08-16 18:57:57 +0000552
553 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
554 ELFSymbolData &MSD = ExternalSymbolData[i];
555 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000556 assert(((Data.getFlags() & ELF_STB_Global) ||
557 (Data.getFlags() & ELF_STB_Weak)) &&
558 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Matt Fleming3565a062010-08-16 18:57:57 +0000559 WriteSymbol(F, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000560 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000561 LastLocalSymbolIndex++;
562 }
563
564 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
565 ELFSymbolData &MSD = UndefinedSymbolData[i];
566 MCSymbolData &Data = *MSD.SymbolData;
Matt Fleming3565a062010-08-16 18:57:57 +0000567 WriteSymbol(F, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000568 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000569 LastLocalSymbolIndex++;
570 }
571}
572
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000573static bool ShouldRelocOnSymbol(const MCSymbolData &SD,
Rafael Espindola3729d002010-10-05 23:57:26 +0000574 const MCValue &Target,
575 const MCFragment &F) {
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000576 const MCSymbol &Symbol = SD.getSymbol();
577 if (Symbol.isUndefined())
578 return true;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000579
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000580 const MCSectionELF &Section =
581 static_cast<const MCSectionELF&>(Symbol.getSection());
582
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000583 if (SD.isExternal())
584 return true;
585
Rafael Espindola8cecf252010-10-06 16:23:36 +0000586 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000587 const MCSectionELF &Sec2 =
588 static_cast<const MCSectionELF&>(F.getParent()->getSection());
589
Rafael Espindolace2d3c52010-10-18 20:25:33 +0000590 if (Section.getKind().isBSS())
591 return false;
592
Rafael Espindola8cecf252010-10-06 16:23:36 +0000593 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000594 (Kind == MCSymbolRefExpr::VK_PLT ||
595 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
596 Kind == MCSymbolRefExpr::VK_GOTOFF))
Rafael Espindola3729d002010-10-05 23:57:26 +0000597 return true;
598
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000599 if (Section.getFlags() & MCSectionELF::SHF_MERGE)
600 return Target.getConstant() != 0;
601
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000602 return false;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000603}
604
Benjamin Kramere5b57342010-08-17 18:20:28 +0000605// FIXME: this is currently X86/X86_64 only
Matt Fleming3565a062010-08-16 18:57:57 +0000606void ELFObjectWriterImpl::RecordRelocation(const MCAssembler &Asm,
607 const MCAsmLayout &Layout,
608 const MCFragment *Fragment,
609 const MCFixup &Fixup,
610 MCValue Target,
611 uint64_t &FixedValue) {
Matt Fleming3565a062010-08-16 18:57:57 +0000612 int64_t Addend = 0;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000613 int Index = 0;
Benjamin Kramer95c602a2010-08-27 10:38:39 +0000614 int64_t Value = Target.getConstant();
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000615 const MCSymbol *Symbol = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000616
Rafael Espindola00074892010-09-17 22:34:41 +0000617 bool IsPCRel = isFixupKindX86PCRel(Fixup.getKind());
Benjamin Kramer81cfb852010-08-17 19:45:05 +0000618 if (!Target.isAbsolute()) {
Rafael Espindolade89b012010-10-15 18:25:33 +0000619 Symbol = &AliasedSymbol(Target.getSymA()->getSymbol());
Rafael Espindola88182132010-10-27 15:18:17 +0000620 const MCSymbol *Renamed = Renames.lookup(Symbol);
621 if (Renamed)
622 Symbol = Renamed;
Matt Fleming3565a062010-08-16 18:57:57 +0000623 MCSymbolData &SD = Asm.getSymbolData(*Symbol);
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000624 MCFragment *F = SD.getFragment();
Matt Fleming3565a062010-08-16 18:57:57 +0000625
Rafael Espindola9d8b7552010-10-03 00:46:57 +0000626 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
627 const MCSymbol &SymbolB = RefB->getSymbol();
628 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
629 IsPCRel = true;
Rafael Espindola55fb1022010-10-04 15:59:01 +0000630 MCSectionData *Sec = Fragment->getParent();
631
632 // Offset of the symbol in the section
633 int64_t a = Layout.getSymbolAddress(&SDB) - Layout.getSectionAddress(Sec);
634
635 // Ofeset of the relocation in the section
636 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
637 Value += b - a;
Rafael Espindola9d8b7552010-10-03 00:46:57 +0000638 }
639
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000640 // Check that this case has already been fully resolved before we get
641 // here.
Rafael Espindola00074892010-09-17 22:34:41 +0000642 if (Symbol->isDefined() && !SD.isExternal() &&
643 IsPCRel &&
644 &Fragment->getParent()->getSection() == &Symbol->getSection()) {
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000645 llvm_unreachable("We don't need a relocation in this case.");
Rafael Espindola00074892010-09-17 22:34:41 +0000646 return;
647 }
648
Rafael Espindola3729d002010-10-05 23:57:26 +0000649 bool RelocOnSymbol = ShouldRelocOnSymbol(SD, Target, *Fragment);
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000650 if (!RelocOnSymbol) {
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000651 Index = F->getParent()->getOrdinal();
Rafael Espindolaa6489182010-09-24 21:19:03 +0000652
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000653 MCSectionData *FSD = F->getParent();
654 // Offset of the symbol in the section
655 Value += Layout.getSymbolAddress(&SD) - Layout.getSectionAddress(FSD);
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000656 } else {
657 UsedInReloc.insert(Symbol);
658 Index = -1;
659 }
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000660 Addend = Value;
661 // Compensate for the addend on i386.
662 if (Is64Bit)
663 Value = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000664 }
665
Benjamin Kramer95c602a2010-08-27 10:38:39 +0000666 FixedValue = Value;
667
Matt Fleming3565a062010-08-16 18:57:57 +0000668 // determine the type of the relocation
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000669
670 MCSymbolRefExpr::VariantKind Modifier = Target.getSymA()->getKind();
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000671 unsigned Type;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000672 if (Is64Bit) {
673 if (IsPCRel) {
Rafael Espindola92bf6682010-10-04 19:04:13 +0000674 switch (Modifier) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000675 default:
676 llvm_unreachable("Unimplemented");
Rafael Espindola92bf6682010-10-04 19:04:13 +0000677 case MCSymbolRefExpr::VK_None:
678 Type = ELF::R_X86_64_PC32;
679 break;
680 case MCSymbolRefExpr::VK_PLT:
681 Type = ELF::R_X86_64_PLT32;
682 break;
Rafael Espindola607d1f62010-10-04 19:51:39 +0000683 case llvm::MCSymbolRefExpr::VK_GOTPCREL:
684 Type = ELF::R_X86_64_GOTPCREL;
685 break;
Rafael Espindola92bf6682010-10-04 19:04:13 +0000686 }
Benjamin Kramere5b57342010-08-17 18:20:28 +0000687 } else {
688 switch ((unsigned)Fixup.getKind()) {
689 default: llvm_unreachable("invalid fixup kind!");
690 case FK_Data_8: Type = ELF::R_X86_64_64; break;
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000691 case X86::reloc_signed_4byte:
Benjamin Kramere5b57342010-08-17 18:20:28 +0000692 case X86::reloc_pcrel_4byte:
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000693 assert(isInt<32>(Target.getConstant()));
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000694 switch (Modifier) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000695 default:
696 llvm_unreachable("Unimplemented");
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000697 case MCSymbolRefExpr::VK_None:
698 Type = ELF::R_X86_64_32S;
699 break;
700 case MCSymbolRefExpr::VK_GOT:
701 Type = ELF::R_X86_64_GOT32;
702 break;
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000703 case MCSymbolRefExpr::VK_GOTPCREL:
Rafael Espindola8cecf252010-10-06 16:23:36 +0000704 Type = ELF::R_X86_64_GOTPCREL;
705 break;
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000706 }
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000707 break;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000708 case FK_Data_4:
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000709 Type = ELF::R_X86_64_32;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000710 break;
711 case FK_Data_2: Type = ELF::R_X86_64_16; break;
712 case X86::reloc_pcrel_1byte:
713 case FK_Data_1: Type = ELF::R_X86_64_8; break;
714 }
715 }
Matt Fleming3565a062010-08-16 18:57:57 +0000716 } else {
Benjamin Kramere5b57342010-08-17 18:20:28 +0000717 if (IsPCRel) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000718 switch (Modifier) {
719 default:
Rafael Espindola24dc9ec2010-10-18 19:33:01 +0000720 llvm_unreachable("Unimplemented");
721 case MCSymbolRefExpr::VK_None:
Rafael Espindola9baee3b2010-10-18 18:03:28 +0000722 Type = ELF::R_386_PC32;
Rafael Espindola9baee3b2010-10-18 18:03:28 +0000723 break;
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000724 case MCSymbolRefExpr::VK_PLT:
725 Type = ELF::R_386_PLT32;
726 break;
727 }
Benjamin Kramere5b57342010-08-17 18:20:28 +0000728 } else {
729 switch ((unsigned)Fixup.getKind()) {
730 default: llvm_unreachable("invalid fixup kind!");
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000731
Rafael Espindola24ba4f72010-10-24 17:35:42 +0000732 case X86::reloc_global_offset_table:
733 Type = ELF::R_386_GOTPC;
734 break;
735
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000736 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
737 // instead?
738 case X86::reloc_signed_4byte:
Benjamin Kramere5b57342010-08-17 18:20:28 +0000739 case X86::reloc_pcrel_4byte:
Rafael Espindolabd701182010-10-19 19:31:37 +0000740 case FK_Data_4:
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000741 switch (Modifier) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000742 default:
743 llvm_unreachable("Unimplemented");
Rafael Espindolabd701182010-10-19 19:31:37 +0000744 case MCSymbolRefExpr::VK_None:
Rafael Espindola24ba4f72010-10-24 17:35:42 +0000745 Type = ELF::R_386_32;
Rafael Espindolabd701182010-10-19 19:31:37 +0000746 break;
Rafael Espindolaeada3042010-10-18 20:47:21 +0000747 case MCSymbolRefExpr::VK_GOT:
748 Type = ELF::R_386_GOT32;
749 break;
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000750 case MCSymbolRefExpr::VK_GOTOFF:
751 Type = ELF::R_386_GOTOFF;
752 break;
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000753 }
754 break;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000755 case FK_Data_2: Type = ELF::R_386_16; break;
756 case X86::reloc_pcrel_1byte:
757 case FK_Data_1: Type = ELF::R_386_8; break;
758 }
Matt Fleming3565a062010-08-16 18:57:57 +0000759 }
760 }
761
Rafael Espindola5c77c162010-10-05 15:48:37 +0000762 if (RelocNeedsGOT(Type))
763 NeedsGOT = true;
764
Benjamin Kramere5b57342010-08-17 18:20:28 +0000765 ELFRelocationEntry ERE;
766
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000767 ERE.Index = Index;
768 ERE.Type = Type;
769 ERE.Symbol = Symbol;
Matt Fleming3565a062010-08-16 18:57:57 +0000770
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000771 ERE.r_offset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
Benjamin Kramere5b57342010-08-17 18:20:28 +0000772
Matt Fleming3565a062010-08-16 18:57:57 +0000773 if (HasRelocationAddend)
774 ERE.r_addend = Addend;
Benjamin Kramer172d7d62010-08-17 00:33:24 +0000775 else
776 ERE.r_addend = 0; // Silence compiler warning.
Matt Fleming3565a062010-08-16 18:57:57 +0000777
778 Relocations[Fragment->getParent()].push_back(ERE);
779}
780
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000781uint64_t
782ELFObjectWriterImpl::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
783 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000784 MCSymbolData &SD = Asm.getSymbolData(*S);
Eli Friedmanf8020a32010-08-16 19:15:06 +0000785
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000786 // Local symbol.
787 if (!SD.isExternal() && !S->isUndefined())
788 return SD.getIndex() + /* empty symbol */ 1;
789
790 // External or undefined symbol.
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000791 return SD.getIndex() + NumRegularSections + /* empty symbol */ 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000792}
793
Rafael Espindola737cd212010-10-05 18:01:23 +0000794static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
Rafael Espindola88182132010-10-27 15:18:17 +0000795 bool Used, bool Renamed) {
Rafael Espindolabd701182010-10-19 19:31:37 +0000796 if (Used)
797 return true;
798
Rafael Espindola88182132010-10-27 15:18:17 +0000799 if (Renamed)
800 return false;
801
Rafael Espindola737cd212010-10-05 18:01:23 +0000802 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000803
804 const MCSymbol &A = AliasedSymbol(Symbol);
805 if (&A != &Symbol && A.isUndefined())
806 return false;
807
Rafael Espindola737cd212010-10-05 18:01:23 +0000808 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
809 return false;
810
Rafael Espindolabd701182010-10-19 19:31:37 +0000811 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000812 return false;
813
814 return true;
815}
816
817static bool isLocal(const MCSymbolData &Data) {
818 if (Data.isExternal())
819 return false;
820
821 const MCSymbol &Symbol = Data.getSymbol();
822 if (Symbol.isUndefined() && !Symbol.isVariable())
823 return false;
824
825 return true;
826}
827
Matt Fleming3565a062010-08-16 18:57:57 +0000828void ELFObjectWriterImpl::ComputeSymbolTable(MCAssembler &Asm) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000829 // FIXME: Is this the correct place to do this?
830 if (NeedsGOT) {
831 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
832 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
833 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
834 Data.setExternal(true);
835 }
836
Matt Fleming3565a062010-08-16 18:57:57 +0000837 // Build section lookup table.
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000838 NumRegularSections = Asm.size();
Rafael Espindolaf5c347d2010-10-05 21:20:07 +0000839 DenseMap<const MCSection*, uint32_t> SectionIndexMap;
Matt Fleming3565a062010-08-16 18:57:57 +0000840 unsigned Index = 1;
841 for (MCAssembler::iterator it = Asm.begin(),
842 ie = Asm.end(); it != ie; ++it, ++Index)
843 SectionIndexMap[&it->getSection()] = Index;
844
845 // Index 0 is always the empty string.
846 StringMap<uint64_t> StringIndexMap;
847 StringTable += '\x00';
848
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000849 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000850 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
851 ie = Asm.symbol_end(); it != ie; ++it) {
852 const MCSymbol &Symbol = it->getSymbol();
853
Rafael Espindola88182132010-10-27 15:18:17 +0000854 if (!isInSymtab(Asm, *it, UsedInReloc.count(&Symbol),
855 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000856 continue;
857
Matt Fleming3565a062010-08-16 18:57:57 +0000858 ELFSymbolData MSD;
859 MSD.SymbolData = it;
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000860 bool Local = isLocal(*it);
Rafael Espindola88182132010-10-27 15:18:17 +0000861 const MCSymbol &RefSymbol = AliasedSymbol(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000862
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000863 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000864 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000865 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000866 } else if (Symbol.isAbsolute()) {
867 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000868 } else if (RefSymbol.isUndefined()) {
Matt Fleming3565a062010-08-16 18:57:57 +0000869 MSD.SectionIndex = ELF::SHN_UNDEF;
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000870 // FIXME: Undefined symbols are global, but this is the first place we
871 // are able to set it.
872 if (GetBinding(*it) == ELF::STB_LOCAL)
873 SetBinding(*it, ELF::STB_GLOBAL);
Rafael Espindola88182132010-10-27 15:18:17 +0000874 } else if (Symbol.isVariable()) {
875 MSD.SectionIndex = SectionIndexMap.lookup(&RefSymbol.getSection());
876 assert(MSD.SectionIndex && "Invalid section index!");
Matt Fleming3565a062010-08-16 18:57:57 +0000877 } else {
878 MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection());
879 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000880 }
881
Rafael Espindola88182132010-10-27 15:18:17 +0000882 // The @@@ in symbol version is replaced with @ in undefined symbols and
883 // @@ in defined ones.
884 StringRef Name = Symbol.getName();
885 size_t Pos = Name.find("@@@");
886 std::string FinalName;
887 if (Pos != StringRef::npos) {
888 StringRef Prefix(Name.begin(), Pos);
889 unsigned n = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
890 StringRef Suffix(Name.begin() + Pos + n);
891 FinalName = Prefix.str() + Suffix.str();
892 } else {
893 FinalName = Name.str();
894 }
895
896 uint64_t &Entry = StringIndexMap[FinalName];
Rafael Espindolaa6866962010-10-27 14:44:52 +0000897 if (!Entry) {
898 Entry = StringTable.size();
Rafael Espindola88182132010-10-27 15:18:17 +0000899 StringTable += FinalName;
Rafael Espindolaa6866962010-10-27 14:44:52 +0000900 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +0000901 }
Rafael Espindolaa6866962010-10-27 14:44:52 +0000902 MSD.StringIndex = Entry;
903 if (MSD.SectionIndex == ELF::SHN_UNDEF)
904 UndefinedSymbolData.push_back(MSD);
905 else if (Local)
906 LocalSymbolData.push_back(MSD);
907 else
908 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +0000909 }
910
911 // Symbols are required to be in lexicographic order.
912 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
913 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
914 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
915
916 // Set the symbol indices. Local symbols must come before all other
917 // symbols with non-local bindings.
918 Index = 0;
919 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
920 LocalSymbolData[i].SymbolData->setIndex(Index++);
921 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
922 ExternalSymbolData[i].SymbolData->setIndex(Index++);
923 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
924 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
925}
926
927void ELFObjectWriterImpl::WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
928 const MCSectionData &SD) {
929 if (!Relocations[&SD].empty()) {
930 MCContext &Ctx = Asm.getContext();
931 const MCSection *RelaSection;
932 const MCSectionELF &Section =
933 static_cast<const MCSectionELF&>(SD.getSection());
934
935 const StringRef SectionName = Section.getSectionName();
Benjamin Kramer377a5722010-08-17 17:30:07 +0000936 std::string RelaSectionName = HasRelocationAddend ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +0000937 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000938
939 unsigned EntrySize;
940 if (HasRelocationAddend)
941 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
942 else
943 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +0000944
Benjamin Kramer377a5722010-08-17 17:30:07 +0000945 RelaSection = Ctx.getELFSection(RelaSectionName, HasRelocationAddend ?
946 ELF::SHT_RELA : ELF::SHT_REL, 0,
Matt Fleming3565a062010-08-16 18:57:57 +0000947 SectionKind::getReadOnly(),
948 false, EntrySize);
949
950 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Benjamin Kramera9eadca2010-09-06 16:11:52 +0000951 RelaSD.setAlignment(Is64Bit ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +0000952
953 MCDataFragment *F = new MCDataFragment(&RelaSD);
954
955 WriteRelocationsFragment(Asm, F, &SD);
956
Rafael Espindola70703872010-09-30 02:22:20 +0000957 Asm.AddSectionToTheEnd(*Writer, RelaSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000958 }
959}
960
961void ELFObjectWriterImpl::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
962 uint64_t Flags, uint64_t Address,
963 uint64_t Offset, uint64_t Size,
964 uint32_t Link, uint32_t Info,
965 uint64_t Alignment,
966 uint64_t EntrySize) {
967 Write32(Name); // sh_name: index into string table
968 Write32(Type); // sh_type
969 WriteWord(Flags); // sh_flags
970 WriteWord(Address); // sh_addr
971 WriteWord(Offset); // sh_offset
972 WriteWord(Size); // sh_size
973 Write32(Link); // sh_link
974 Write32(Info); // sh_info
975 WriteWord(Alignment); // sh_addralign
976 WriteWord(EntrySize); // sh_entsize
977}
978
979void ELFObjectWriterImpl::WriteRelocationsFragment(const MCAssembler &Asm,
980 MCDataFragment *F,
981 const MCSectionData *SD) {
982 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
983 // sort by the r_offset just like gnu as does
984 array_pod_sort(Relocs.begin(), Relocs.end());
985
986 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
987 ELFRelocationEntry entry = Relocs[e - i - 1];
988
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000989 if (entry.Index < 0)
990 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
991 else
992 entry.Index += LocalSymbolData.size() + 1;
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000993 if (Is64Bit) {
994 char buf[8];
Matt Fleming3565a062010-08-16 18:57:57 +0000995
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000996 String64(buf, entry.r_offset);
997 F->getContents() += StringRef(buf, 8);
998
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000999 struct ELF::Elf64_Rela ERE64;
1000 ERE64.setSymbolAndType(entry.Index, entry.Type);
1001 String64(buf, ERE64.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001002 F->getContents() += StringRef(buf, 8);
1003
1004 if (HasRelocationAddend) {
1005 String64(buf, entry.r_addend);
1006 F->getContents() += StringRef(buf, 8);
1007 }
1008 } else {
1009 char buf[4];
1010
1011 String32(buf, entry.r_offset);
1012 F->getContents() += StringRef(buf, 4);
1013
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001014 struct ELF::Elf32_Rela ERE32;
1015 ERE32.setSymbolAndType(entry.Index, entry.Type);
1016 String32(buf, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001017 F->getContents() += StringRef(buf, 4);
1018
1019 if (HasRelocationAddend) {
1020 String32(buf, entry.r_addend);
1021 F->getContents() += StringRef(buf, 4);
1022 }
1023 }
Matt Fleming3565a062010-08-16 18:57:57 +00001024 }
1025}
1026
1027void ELFObjectWriterImpl::CreateMetadataSections(MCAssembler &Asm,
1028 MCAsmLayout &Layout) {
1029 MCContext &Ctx = Asm.getContext();
1030 MCDataFragment *F;
1031
Matt Fleming3565a062010-08-16 18:57:57 +00001032 const MCSection *SymtabSection;
1033 unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
1034
Rafael Espindola71859c62010-09-16 19:46:31 +00001035 unsigned NumRegularSections = Asm.size();
1036
Rafael Espindola38738bf2010-09-22 19:04:41 +00001037 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola71859c62010-09-16 19:46:31 +00001038 const MCSection *ShstrtabSection;
1039 ShstrtabSection = Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
1040 SectionKind::getReadOnly(), false);
1041 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1042 ShstrtabSD.setAlignment(1);
1043 ShstrtabIndex = Asm.size();
1044
Matt Fleming3565a062010-08-16 18:57:57 +00001045 SymtabSection = Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1046 SectionKind::getReadOnly(),
1047 false, EntrySize);
Matt Fleming3565a062010-08-16 18:57:57 +00001048 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Matt Fleming3565a062010-08-16 18:57:57 +00001049 SymtabSD.setAlignment(Is64Bit ? 8 : 4);
1050
Matt Fleming3565a062010-08-16 18:57:57 +00001051 const MCSection *StrtabSection;
1052 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
1053 SectionKind::getReadOnly(), false);
Matt Fleming3565a062010-08-16 18:57:57 +00001054 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1055 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +00001056 StringTableIndex = Asm.size();
1057
Rafael Espindolac3c413f2010-09-27 22:04:54 +00001058 WriteRelocations(Asm, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001059
1060 // Symbol table
1061 F = new MCDataFragment(&SymtabSD);
1062 WriteSymbolTable(F, Asm, Layout, NumRegularSections);
Rafael Espindola70703872010-09-30 02:22:20 +00001063 Asm.AddSectionToTheEnd(*Writer, SymtabSD, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001064
Matt Fleming3565a062010-08-16 18:57:57 +00001065 F = new MCDataFragment(&StrtabSD);
1066 F->getContents().append(StringTable.begin(), StringTable.end());
Rafael Espindola70703872010-09-30 02:22:20 +00001067 Asm.AddSectionToTheEnd(*Writer, StrtabSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +00001068
Matt Fleming3565a062010-08-16 18:57:57 +00001069 F = new MCDataFragment(&ShstrtabSD);
1070
Matt Fleming3565a062010-08-16 18:57:57 +00001071 // Section header string table.
1072 //
1073 // The first entry of a string table holds a null character so skip
1074 // section 0.
1075 uint64_t Index = 1;
1076 F->getContents() += '\x00';
1077
1078 for (MCAssembler::const_iterator it = Asm.begin(),
1079 ie = Asm.end(); it != ie; ++it) {
Matt Fleming3565a062010-08-16 18:57:57 +00001080 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +00001081 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola51efe7a2010-09-23 14:14:56 +00001082 // FIXME: We could merge suffixes like in .text and .rela.text.
Matt Fleming3565a062010-08-16 18:57:57 +00001083
1084 // Remember the index into the string table so we can write it
1085 // into the sh_name field of the section header table.
1086 SectionStringTableIndex[&it->getSection()] = Index;
1087
1088 Index += Section.getSectionName().size() + 1;
1089 F->getContents() += Section.getSectionName();
1090 F->getContents() += '\x00';
1091 }
1092
Rafael Espindola70703872010-09-30 02:22:20 +00001093 Asm.AddSectionToTheEnd(*Writer, ShstrtabSD, Layout);
1094}
1095
1096bool ELFObjectWriterImpl::IsFixupFullyResolved(const MCAssembler &Asm,
1097 const MCValue Target,
1098 bool IsPCRel,
1099 const MCFragment *DF) const {
1100 // If this is a PCrel relocation, find the section this fixup value is
1101 // relative to.
1102 const MCSection *BaseSection = 0;
1103 if (IsPCRel) {
1104 BaseSection = &DF->getParent()->getSection();
1105 assert(BaseSection);
1106 }
1107
1108 const MCSection *SectionA = 0;
1109 const MCSymbol *SymbolA = 0;
1110 if (const MCSymbolRefExpr *A = Target.getSymA()) {
1111 SymbolA = &A->getSymbol();
1112 SectionA = &SymbolA->getSection();
1113 }
1114
1115 const MCSection *SectionB = 0;
1116 if (const MCSymbolRefExpr *B = Target.getSymB()) {
1117 SectionB = &B->getSymbol().getSection();
1118 }
1119
1120 if (!BaseSection)
1121 return SectionA == SectionB;
1122
1123 const MCSymbolData &DataA = Asm.getSymbolData(*SymbolA);
1124 if (DataA.isExternal())
1125 return false;
1126
1127 return !SectionB && BaseSection == SectionA;
Matt Fleming3565a062010-08-16 18:57:57 +00001128}
1129
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001130void ELFObjectWriterImpl::WriteObject(MCAssembler &Asm,
Matt Fleming3565a062010-08-16 18:57:57 +00001131 const MCAsmLayout &Layout) {
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001132 // Compute symbol table information.
1133 ComputeSymbolTable(Asm);
1134
Matt Fleming3565a062010-08-16 18:57:57 +00001135 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
1136 const_cast<MCAsmLayout&>(Layout));
1137
1138 // Add 1 for the null section.
1139 unsigned NumSections = Asm.size() + 1;
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001140 uint64_t NaturalAlignment = Is64Bit ? 8 : 4;
1141 uint64_t HeaderSize = Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr);
1142 uint64_t FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001143
1144 for (MCAssembler::const_iterator it = Asm.begin(),
1145 ie = Asm.end(); it != ie; ++it) {
1146 const MCSectionData &SD = *it;
Matt Fleming3565a062010-08-16 18:57:57 +00001147
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001148 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1149
Matt Fleming3565a062010-08-16 18:57:57 +00001150 // Get the size of the section in the output file (including padding).
1151 uint64_t Size = Layout.getSectionFileSize(&SD);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001152
1153 FileOff += Size;
Matt Fleming3565a062010-08-16 18:57:57 +00001154 }
1155
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001156 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1157
Matt Fleming3565a062010-08-16 18:57:57 +00001158 // Write out the ELF header ...
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001159 WriteHeader(FileOff - HeaderSize, NumSections);
1160
1161 FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001162
1163 // ... then all of the sections ...
1164 DenseMap<const MCSection*, uint64_t> SectionOffsetMap;
1165
Rafael Espindolad8e0bfe2010-10-06 22:28:19 +00001166 DenseMap<const MCSection*, uint32_t> SectionIndexMap;
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001167
1168 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +00001169 for (MCAssembler::const_iterator it = Asm.begin(),
1170 ie = Asm.end(); it != ie; ++it) {
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001171 const MCSectionData &SD = *it;
1172
1173 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1174 WriteZeros(Padding);
1175 FileOff += Padding;
1176
Matt Fleming3565a062010-08-16 18:57:57 +00001177 // Remember the offset into the file for this section.
1178 SectionOffsetMap[&it->getSection()] = FileOff;
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001179 SectionIndexMap[&it->getSection()] = Index++;
1180
Matt Fleming3565a062010-08-16 18:57:57 +00001181 FileOff += Layout.getSectionFileSize(&SD);
1182
1183 Asm.WriteSectionData(it, Layout, Writer);
1184 }
1185
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001186 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1187 WriteZeros(Padding);
1188 FileOff += Padding;
1189
Matt Fleming3565a062010-08-16 18:57:57 +00001190 // ... and then the section header table.
1191 // Should we align the section header table?
1192 //
1193 // Null section first.
1194 WriteSecHdrEntry(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1195
1196 for (MCAssembler::const_iterator it = Asm.begin(),
1197 ie = Asm.end(); it != ie; ++it) {
1198 const MCSectionData &SD = *it;
1199 const MCSectionELF &Section =
1200 static_cast<const MCSectionELF&>(SD.getSection());
1201
1202 uint64_t sh_link = 0;
1203 uint64_t sh_info = 0;
1204
1205 switch(Section.getType()) {
1206 case ELF::SHT_DYNAMIC:
1207 sh_link = SectionStringTableIndex[&it->getSection()];
1208 sh_info = 0;
1209 break;
1210
1211 case ELF::SHT_REL:
Eli Friedmanf8020a32010-08-16 19:15:06 +00001212 case ELF::SHT_RELA: {
Matt Fleming3565a062010-08-16 18:57:57 +00001213 const MCSection *SymtabSection;
1214 const MCSection *InfoSection;
Matt Fleming3565a062010-08-16 18:57:57 +00001215
1216 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1217 SectionKind::getReadOnly(),
Eli Friedmanf8020a32010-08-16 19:15:06 +00001218 false);
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001219 sh_link = SectionIndexMap[SymtabSection];
Matt Fleming3565a062010-08-16 18:57:57 +00001220
Benjamin Kramer377a5722010-08-17 17:30:07 +00001221 // Remove ".rel" and ".rela" prefixes.
1222 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1223 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
1224
Eli Friedmanf8020a32010-08-16 19:15:06 +00001225 InfoSection = Asm.getContext().getELFSection(SectionName,
Matt Fleming3565a062010-08-16 18:57:57 +00001226 ELF::SHT_PROGBITS, 0,
Eli Friedmanf8020a32010-08-16 19:15:06 +00001227 SectionKind::getReadOnly(),
1228 false);
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001229 sh_info = SectionIndexMap[InfoSection];
Matt Fleming3565a062010-08-16 18:57:57 +00001230 break;
Eli Friedmanf8020a32010-08-16 19:15:06 +00001231 }
Matt Fleming3565a062010-08-16 18:57:57 +00001232
1233 case ELF::SHT_SYMTAB:
1234 case ELF::SHT_DYNSYM:
1235 sh_link = StringTableIndex;
1236 sh_info = LastLocalSymbolIndex;
1237 break;
1238
1239 case ELF::SHT_PROGBITS:
1240 case ELF::SHT_STRTAB:
1241 case ELF::SHT_NOBITS:
Benjamin Kramer19dc7fa2010-08-31 17:03:33 +00001242 case ELF::SHT_NULL:
Rafael Espindolacecbc3d2010-10-25 17:50:35 +00001243 case ELF::SHT_ARM_ATTRIBUTES:
Matt Fleming3565a062010-08-16 18:57:57 +00001244 // Nothing to do.
1245 break;
1246
Matt Fleming3565a062010-08-16 18:57:57 +00001247 default:
1248 assert(0 && "FIXME: sh_type value not supported!");
1249 break;
1250 }
1251
1252 WriteSecHdrEntry(SectionStringTableIndex[&it->getSection()],
1253 Section.getType(), Section.getFlags(),
Rafael Espindola71859c62010-09-16 19:46:31 +00001254 0,
Matt Fleming3565a062010-08-16 18:57:57 +00001255 SectionOffsetMap.lookup(&SD.getSection()),
1256 Layout.getSectionSize(&SD), sh_link,
1257 sh_info, SD.getAlignment(),
1258 Section.getEntrySize());
1259 }
1260}
1261
1262ELFObjectWriter::ELFObjectWriter(raw_ostream &OS,
1263 bool Is64Bit,
Roman Divacky5baf79e2010-09-09 17:57:50 +00001264 Triple::OSType OSType,
Wesley Peckeecb8582010-10-22 15:52:49 +00001265 uint16_t EMachine,
Matt Fleming3565a062010-08-16 18:57:57 +00001266 bool IsLittleEndian,
1267 bool HasRelocationAddend)
1268 : MCObjectWriter(OS, IsLittleEndian)
1269{
Wesley Peckeecb8582010-10-22 15:52:49 +00001270 Impl = new ELFObjectWriterImpl(this, Is64Bit, EMachine,
1271 HasRelocationAddend, OSType);
Matt Fleming3565a062010-08-16 18:57:57 +00001272}
1273
1274ELFObjectWriter::~ELFObjectWriter() {
1275 delete (ELFObjectWriterImpl*) Impl;
1276}
1277
1278void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm) {
1279 ((ELFObjectWriterImpl*) Impl)->ExecutePostLayoutBinding(Asm);
1280}
1281
1282void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
1283 const MCAsmLayout &Layout,
1284 const MCFragment *Fragment,
1285 const MCFixup &Fixup, MCValue Target,
1286 uint64_t &FixedValue) {
1287 ((ELFObjectWriterImpl*) Impl)->RecordRelocation(Asm, Layout, Fragment, Fixup,
1288 Target, FixedValue);
1289}
1290
Rafael Espindola70703872010-09-30 02:22:20 +00001291bool ELFObjectWriter::IsFixupFullyResolved(const MCAssembler &Asm,
1292 const MCValue Target,
1293 bool IsPCRel,
1294 const MCFragment *DF) const {
1295 return ((ELFObjectWriterImpl*) Impl)->IsFixupFullyResolved(Asm, Target,
1296 IsPCRel, DF);
1297}
1298
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001299void ELFObjectWriter::WriteObject(MCAssembler &Asm,
Matt Fleming3565a062010-08-16 18:57:57 +00001300 const MCAsmLayout &Layout) {
1301 ((ELFObjectWriterImpl*) Impl)->WriteObject(Asm, Layout);
1302}