blob: 90473dbac9c166f0866994bb732086a3db6686fa [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:
Rafael Espindolabc82d8b2010-10-27 20:28:07 +000088 case ELF::R_X86_64_TPOFF32:
89 case ELF::R_X86_64_TLSGD:
90 case ELF::R_X86_64_GOTTPOFF:
Rafael Espindola5c77c162010-10-05 15:48:37 +000091 return true;
92 }
93}
94
Matt Fleming3565a062010-08-16 18:57:57 +000095namespace {
96
97 class ELFObjectWriterImpl {
Chris Lattnerb188a372010-08-28 03:21:03 +000098 /*static bool isFixupKindX86RIPRel(unsigned Kind) {
Matt Fleming3565a062010-08-16 18:57:57 +000099 return Kind == X86::reloc_riprel_4byte ||
100 Kind == X86::reloc_riprel_4byte_movq_load;
Chris Lattnerb188a372010-08-28 03:21:03 +0000101 }*/
Matt Fleming3565a062010-08-16 18:57:57 +0000102
103
104 /// ELFSymbolData - Helper struct for containing some precomputed information
105 /// on symbols.
106 struct ELFSymbolData {
107 MCSymbolData *SymbolData;
108 uint64_t StringIndex;
109 uint32_t SectionIndex;
110
111 // Support lexicographic sorting.
112 bool operator<(const ELFSymbolData &RHS) const {
Rafael Espindolaad49cf52010-09-18 15:03:21 +0000113 if (GetType(*SymbolData) == ELF::STT_FILE)
114 return true;
115 if (GetType(*RHS.SymbolData) == ELF::STT_FILE)
116 return false;
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000117 return SymbolData->getSymbol().getName() <
118 RHS.SymbolData->getSymbol().getName();
Matt Fleming3565a062010-08-16 18:57:57 +0000119 }
120 };
121
122 /// @name Relocation Data
123 /// @{
124
125 struct ELFRelocationEntry {
126 // Make these big enough for both 32-bit and 64-bit
127 uint64_t r_offset;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000128 int Index;
129 unsigned Type;
130 const MCSymbol *Symbol;
Matt Fleming3565a062010-08-16 18:57:57 +0000131 uint64_t r_addend;
132
133 // Support lexicographic sorting.
134 bool operator<(const ELFRelocationEntry &RE) const {
135 return RE.r_offset < r_offset;
136 }
137 };
138
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000139 SmallPtrSet<const MCSymbol *, 16> UsedInReloc;
Rafael Espindola88182132010-10-27 15:18:17 +0000140 DenseMap<const MCSymbol *, const MCSymbol *> Renames;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000141
Matt Fleming3565a062010-08-16 18:57:57 +0000142 llvm::DenseMap<const MCSectionData*,
143 std::vector<ELFRelocationEntry> > Relocations;
144 DenseMap<const MCSection*, uint64_t> SectionStringTableIndex;
145
146 /// @}
147 /// @name Symbol Table Data
148 /// @{
149
150 SmallString<256> StringTable;
151 std::vector<ELFSymbolData> LocalSymbolData;
152 std::vector<ELFSymbolData> ExternalSymbolData;
153 std::vector<ELFSymbolData> UndefinedSymbolData;
154
155 /// @}
156
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000157 int NumRegularSections;
158
Rafael Espindola5c77c162010-10-05 15:48:37 +0000159 bool NeedsGOT;
160
Matt Fleming3565a062010-08-16 18:57:57 +0000161 ELFObjectWriter *Writer;
162
163 raw_ostream &OS;
164
Matt Fleming3565a062010-08-16 18:57:57 +0000165 unsigned Is64Bit : 1;
166
167 bool HasRelocationAddend;
168
Roman Divacky5baf79e2010-09-09 17:57:50 +0000169 Triple::OSType OSType;
170
Wesley Peckeecb8582010-10-22 15:52:49 +0000171 uint16_t EMachine;
172
Matt Fleming3565a062010-08-16 18:57:57 +0000173 // This holds the symbol table index of the last local symbol.
174 unsigned LastLocalSymbolIndex;
175 // This holds the .strtab section index.
176 unsigned StringTableIndex;
177
178 unsigned ShstrtabIndex;
179
180 public:
181 ELFObjectWriterImpl(ELFObjectWriter *_Writer, bool _Is64Bit,
Wesley Peckeecb8582010-10-22 15:52:49 +0000182 uint16_t _EMachine, bool _HasRelAddend,
183 Triple::OSType _OSType)
Rafael Espindola5c77c162010-10-05 15:48:37 +0000184 : NeedsGOT(false), Writer(_Writer), OS(Writer->getStream()),
Roman Divacky5baf79e2010-09-09 17:57:50 +0000185 Is64Bit(_Is64Bit), HasRelocationAddend(_HasRelAddend),
Wesley Peckeecb8582010-10-22 15:52:49 +0000186 OSType(_OSType), EMachine(_EMachine) {
Matt Fleming3565a062010-08-16 18:57:57 +0000187 }
188
189 void Write8(uint8_t Value) { Writer->Write8(Value); }
190 void Write16(uint16_t Value) { Writer->Write16(Value); }
191 void Write32(uint32_t Value) { Writer->Write32(Value); }
Chris Lattnerb188a372010-08-28 03:21:03 +0000192 //void Write64(uint64_t Value) { Writer->Write64(Value); }
Matt Fleming3565a062010-08-16 18:57:57 +0000193 void WriteZeros(unsigned N) { Writer->WriteZeros(N); }
Chris Lattnerb188a372010-08-28 03:21:03 +0000194 //void WriteBytes(StringRef Str, unsigned ZeroFillSize = 0) {
195 // Writer->WriteBytes(Str, ZeroFillSize);
196 //}
Matt Fleming3565a062010-08-16 18:57:57 +0000197
198 void WriteWord(uint64_t W) {
Chris Lattnerb188a372010-08-28 03:21:03 +0000199 if (Is64Bit)
Matt Fleming3565a062010-08-16 18:57:57 +0000200 Writer->Write64(W);
Chris Lattnerb188a372010-08-28 03:21:03 +0000201 else
Matt Fleming3565a062010-08-16 18:57:57 +0000202 Writer->Write32(W);
Matt Fleming3565a062010-08-16 18:57:57 +0000203 }
204
205 void String8(char *buf, uint8_t Value) {
206 buf[0] = Value;
207 }
208
209 void StringLE16(char *buf, uint16_t Value) {
210 buf[0] = char(Value >> 0);
211 buf[1] = char(Value >> 8);
212 }
213
214 void StringLE32(char *buf, uint32_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000215 StringLE16(buf, uint16_t(Value >> 0));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000216 StringLE16(buf + 2, uint16_t(Value >> 16));
Matt Fleming3565a062010-08-16 18:57:57 +0000217 }
218
219 void StringLE64(char *buf, uint64_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000220 StringLE32(buf, uint32_t(Value >> 0));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000221 StringLE32(buf + 4, uint32_t(Value >> 32));
Matt Fleming3565a062010-08-16 18:57:57 +0000222 }
223
224 void StringBE16(char *buf ,uint16_t Value) {
225 buf[0] = char(Value >> 8);
226 buf[1] = char(Value >> 0);
227 }
228
229 void StringBE32(char *buf, uint32_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000230 StringBE16(buf, uint16_t(Value >> 16));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000231 StringBE16(buf + 2, uint16_t(Value >> 0));
Matt Fleming3565a062010-08-16 18:57:57 +0000232 }
233
234 void StringBE64(char *buf, uint64_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000235 StringBE32(buf, uint32_t(Value >> 32));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000236 StringBE32(buf + 4, uint32_t(Value >> 0));
Matt Fleming3565a062010-08-16 18:57:57 +0000237 }
238
239 void String16(char *buf, uint16_t Value) {
240 if (Writer->isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000241 StringLE16(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000242 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000243 StringBE16(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000244 }
245
246 void String32(char *buf, uint32_t Value) {
247 if (Writer->isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000248 StringLE32(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000249 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000250 StringBE32(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000251 }
252
253 void String64(char *buf, uint64_t Value) {
254 if (Writer->isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000255 StringLE64(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000256 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000257 StringBE64(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000258 }
259
260 void WriteHeader(uint64_t SectionDataSize, unsigned NumberOfSections);
261
262 void WriteSymbolEntry(MCDataFragment *F, uint64_t name, uint8_t info,
263 uint64_t value, uint64_t size,
264 uint8_t other, uint16_t shndx);
265
266 void WriteSymbol(MCDataFragment *F, ELFSymbolData &MSD,
267 const MCAsmLayout &Layout);
268
269 void WriteSymbolTable(MCDataFragment *F, const MCAssembler &Asm,
Rafael Espindola71859c62010-09-16 19:46:31 +0000270 const MCAsmLayout &Layout,
271 unsigned NumRegularSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000272
273 void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
274 const MCFragment *Fragment, const MCFixup &Fixup,
275 MCValue Target, uint64_t &FixedValue);
276
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000277 uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm,
278 const MCSymbol *S);
Matt Fleming3565a062010-08-16 18:57:57 +0000279
280 /// ComputeSymbolTable - Compute the symbol table data
281 ///
282 /// \param StringTable [out] - The string table data.
283 /// \param StringIndexMap [out] - Map from symbol names to offsets in the
284 /// string table.
285 void ComputeSymbolTable(MCAssembler &Asm);
286
287 void WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
288 const MCSectionData &SD);
289
290 void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout) {
291 for (MCAssembler::const_iterator it = Asm.begin(),
292 ie = Asm.end(); it != ie; ++it) {
293 WriteRelocation(Asm, Layout, *it);
294 }
295 }
296
297 void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout);
298
Rafael Espindola88182132010-10-27 15:18:17 +0000299 void ExecutePostLayoutBinding(MCAssembler &Asm);
Matt Fleming3565a062010-08-16 18:57:57 +0000300
301 void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
302 uint64_t Address, uint64_t Offset,
303 uint64_t Size, uint32_t Link, uint32_t Info,
304 uint64_t Alignment, uint64_t EntrySize);
305
306 void WriteRelocationsFragment(const MCAssembler &Asm, MCDataFragment *F,
307 const MCSectionData *SD);
308
Rafael Espindola70703872010-09-30 02:22:20 +0000309 bool IsFixupFullyResolved(const MCAssembler &Asm,
310 const MCValue Target,
311 bool IsPCRel,
312 const MCFragment *DF) const;
313
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000314 void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000315 };
316
317}
318
319// Emit the ELF header.
320void ELFObjectWriterImpl::WriteHeader(uint64_t SectionDataSize,
321 unsigned NumberOfSections) {
322 // ELF Header
323 // ----------
324 //
325 // Note
326 // ----
327 // emitWord method behaves differently for ELF32 and ELF64, writing
328 // 4 bytes in the former and 8 in the latter.
329
330 Write8(0x7f); // e_ident[EI_MAG0]
331 Write8('E'); // e_ident[EI_MAG1]
332 Write8('L'); // e_ident[EI_MAG2]
333 Write8('F'); // e_ident[EI_MAG3]
334
335 Write8(Is64Bit ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
336
337 // e_ident[EI_DATA]
338 Write8(Writer->isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
339
340 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky5baf79e2010-09-09 17:57:50 +0000341 // e_ident[EI_OSABI]
342 switch (OSType) {
343 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
344 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
345 default: Write8(ELF::ELFOSABI_NONE); break;
346 }
Matt Fleming3565a062010-08-16 18:57:57 +0000347 Write8(0); // e_ident[EI_ABIVERSION]
348
349 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
350
351 Write16(ELF::ET_REL); // e_type
352
Wesley Peckeecb8582010-10-22 15:52:49 +0000353 Write16(EMachine); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +0000354
355 Write32(ELF::EV_CURRENT); // e_version
356 WriteWord(0); // e_entry, no entry point in .o file
357 WriteWord(0); // e_phoff, no program header for .o
Benjamin Kramereb976772010-08-17 17:02:29 +0000358 WriteWord(SectionDataSize + (Is64Bit ? sizeof(ELF::Elf64_Ehdr) :
359 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000360
361 // FIXME: Make this configurable.
362 Write32(0); // e_flags = whatever the target wants
363
364 // e_ehsize = ELF header size
365 Write16(Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
366
367 Write16(0); // e_phentsize = prog header entry size
368 Write16(0); // e_phnum = # prog header entries = 0
369
370 // e_shentsize = Section header entry size
371 Write16(Is64Bit ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
372
373 // e_shnum = # of section header ents
374 Write16(NumberOfSections);
375
376 // e_shstrndx = Section # of '.shstrtab'
377 Write16(ShstrtabIndex);
378}
379
380void ELFObjectWriterImpl::WriteSymbolEntry(MCDataFragment *F, uint64_t name,
381 uint8_t info, uint64_t value,
382 uint64_t size, uint8_t other,
383 uint16_t shndx) {
384 if (Is64Bit) {
385 char buf[8];
386
387 String32(buf, name);
388 F->getContents() += StringRef(buf, 4); // st_name
389
390 String8(buf, info);
391 F->getContents() += StringRef(buf, 1); // st_info
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000392
Matt Fleming3565a062010-08-16 18:57:57 +0000393 String8(buf, other);
394 F->getContents() += StringRef(buf, 1); // st_other
395
396 String16(buf, shndx);
397 F->getContents() += StringRef(buf, 2); // st_shndx
398
399 String64(buf, value);
400 F->getContents() += StringRef(buf, 8); // st_value
401
402 String64(buf, size);
403 F->getContents() += StringRef(buf, 8); // st_size
404 } else {
405 char buf[4];
406
407 String32(buf, name);
408 F->getContents() += StringRef(buf, 4); // st_name
409
410 String32(buf, value);
411 F->getContents() += StringRef(buf, 4); // st_value
412
413 String32(buf, size);
414 F->getContents() += StringRef(buf, 4); // st_size
415
416 String8(buf, info);
417 F->getContents() += StringRef(buf, 1); // st_info
418
419 String8(buf, other);
420 F->getContents() += StringRef(buf, 1); // st_other
421
422 String16(buf, shndx);
423 F->getContents() += StringRef(buf, 2); // st_shndx
424 }
425}
426
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000427static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout) {
428 if (Data.isCommon() && Data.isExternal())
429 return Data.getCommonAlignment();
430
431 const MCSymbol &Symbol = Data.getSymbol();
432 if (!Symbol.isInSection())
433 return 0;
434
435 if (!Data.isCommon() && !(Data.getFlags() & ELF_STB_Weak))
436 if (MCFragment *FF = Data.getFragment())
437 return Layout.getSymbolAddress(&Data) -
438 Layout.getSectionAddress(FF->getParent());
439
440 return 0;
441}
442
Rafael Espindolade89b012010-10-15 18:25:33 +0000443static const MCSymbol &AliasedSymbol(const MCSymbol &Symbol) {
444 const MCSymbol *S = &Symbol;
445 while (S->isVariable()) {
446 const MCExpr *Value = S->getVariableValue();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000447 if (Value->getKind() != MCExpr::SymbolRef)
448 return *S;
Rafael Espindolade89b012010-10-15 18:25:33 +0000449 const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr*>(Value);
450 S = &Ref->getSymbol();
451 }
452 return *S;
453}
454
Rafael Espindola88182132010-10-27 15:18:17 +0000455void ELFObjectWriterImpl::ExecutePostLayoutBinding(MCAssembler &Asm) {
456 // The presence of symbol versions causes undefined symbols and
457 // versions declared with @@@ to be renamed.
458
459 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
460 ie = Asm.symbol_end(); it != ie; ++it) {
461 const MCSymbol &Alias = it->getSymbol();
462 if (!Alias.isVariable())
463 continue;
464 const MCSymbol &Symbol = AliasedSymbol(Alias);
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000465 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000466 size_t Pos = AliasName.find('@');
467 if (Pos == StringRef::npos)
468 continue;
469
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000470 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000471 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
472 continue;
473
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000474 // FIXME: produce a better error message.
475 if (Symbol.isUndefined() && Rest.startswith("@@") &&
476 !Rest.startswith("@@@"))
477 report_fatal_error("A @@ version cannot be undefined");
478
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000479 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000480 }
481}
482
Matt Fleming3565a062010-08-16 18:57:57 +0000483void ELFObjectWriterImpl::WriteSymbol(MCDataFragment *F, ELFSymbolData &MSD,
484 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000485 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000486 MCSymbolData &Data =
487 Layout.getAssembler().getSymbolData(AliasedSymbol(OrigData.getSymbol()));
Rafael Espindola152c1062010-10-06 21:02:29 +0000488
489 uint8_t Binding = GetBinding(OrigData);
490 uint8_t Visibility = GetVisibility(OrigData);
491 uint8_t Type = GetType(Data);
492
493 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
494 uint8_t Other = Visibility;
495
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000496 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000497 uint64_t Size = 0;
498 const MCExpr *ESize;
499
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000500 assert(!(Data.isCommon() && !Data.isExternal()));
501
Matt Fleming3565a062010-08-16 18:57:57 +0000502 ESize = Data.getSize();
503 if (Data.getSize()) {
504 MCValue Res;
505 if (ESize->getKind() == MCExpr::Binary) {
506 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(ESize);
507
508 if (BE->EvaluateAsRelocatable(Res, &Layout)) {
Benjamin Kramer24f12062010-10-17 07:38:40 +0000509 assert(!Res.getSymA() || !Res.getSymA()->getSymbol().isDefined());
510 assert(!Res.getSymB() || !Res.getSymB()->getSymbol().isDefined());
Rafael Espindolaf230df92010-10-16 18:23:53 +0000511 Size = Res.getConstant();
Matt Fleming3565a062010-08-16 18:57:57 +0000512 }
513 } else if (ESize->getKind() == MCExpr::Constant) {
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000514 Size = static_cast<const MCConstantExpr *>(ESize)->getValue();
Matt Fleming3565a062010-08-16 18:57:57 +0000515 } else {
516 assert(0 && "Unsupported size expression");
517 }
518 }
519
520 // Write out the symbol table entry
521 WriteSymbolEntry(F, MSD.StringIndex, Info, Value,
522 Size, Other, MSD.SectionIndex);
523}
524
525void ELFObjectWriterImpl::WriteSymbolTable(MCDataFragment *F,
526 const MCAssembler &Asm,
Rafael Espindola71859c62010-09-16 19:46:31 +0000527 const MCAsmLayout &Layout,
528 unsigned NumRegularSections) {
Matt Fleming3565a062010-08-16 18:57:57 +0000529 // The string table must be emitted first because we need the index
530 // into the string table for all the symbol names.
531 assert(StringTable.size() && "Missing string table");
532
533 // FIXME: Make sure the start of the symbol table is aligned.
534
535 // The first entry is the undefined symbol entry.
536 unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000537 F->getContents().append(EntrySize, '\x00');
Matt Fleming3565a062010-08-16 18:57:57 +0000538
539 // Write the symbol table entries.
540 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
541 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
542 ELFSymbolData &MSD = LocalSymbolData[i];
543 WriteSymbol(F, MSD, Layout);
544 }
545
Rafael Espindola71859c62010-09-16 19:46:31 +0000546 // Write out a symbol table entry for each regular section.
Eli Friedmana44fa242010-08-16 21:17:09 +0000547 unsigned Index = 1;
Rafael Espindola71859c62010-09-16 19:46:31 +0000548 for (MCAssembler::const_iterator it = Asm.begin();
549 Index <= NumRegularSections; ++it, ++Index) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000550 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000551 static_cast<const MCSectionELF&>(it->getSection());
Eli Friedmana44fa242010-08-16 21:17:09 +0000552 // Leave out relocations so we don't have indexes within
553 // the relocations messed up
Benjamin Kramer377a5722010-08-17 17:30:07 +0000554 if (Section.getType() == ELF::SHT_RELA || Section.getType() == ELF::SHT_REL)
Eli Friedmana44fa242010-08-16 21:17:09 +0000555 continue;
Matt Fleming3565a062010-08-16 18:57:57 +0000556 WriteSymbolEntry(F, 0, ELF::STT_SECTION, 0, 0, ELF::STV_DEFAULT, Index);
Eli Friedmana44fa242010-08-16 21:17:09 +0000557 LastLocalSymbolIndex++;
558 }
Matt Fleming3565a062010-08-16 18:57:57 +0000559
560 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
561 ELFSymbolData &MSD = ExternalSymbolData[i];
562 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000563 assert(((Data.getFlags() & ELF_STB_Global) ||
564 (Data.getFlags() & ELF_STB_Weak)) &&
565 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Matt Fleming3565a062010-08-16 18:57:57 +0000566 WriteSymbol(F, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000567 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000568 LastLocalSymbolIndex++;
569 }
570
571 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
572 ELFSymbolData &MSD = UndefinedSymbolData[i];
573 MCSymbolData &Data = *MSD.SymbolData;
Matt Fleming3565a062010-08-16 18:57:57 +0000574 WriteSymbol(F, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000575 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000576 LastLocalSymbolIndex++;
577 }
578}
579
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000580static bool ShouldRelocOnSymbol(const MCSymbolData &SD,
Rafael Espindola3729d002010-10-05 23:57:26 +0000581 const MCValue &Target,
582 const MCFragment &F) {
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000583 const MCSymbol &Symbol = SD.getSymbol();
584 if (Symbol.isUndefined())
585 return true;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000586
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000587 const MCSectionELF &Section =
588 static_cast<const MCSectionELF&>(Symbol.getSection());
589
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000590 if (SD.isExternal())
591 return true;
592
Rafael Espindola8cecf252010-10-06 16:23:36 +0000593 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000594 const MCSectionELF &Sec2 =
595 static_cast<const MCSectionELF&>(F.getParent()->getSection());
596
Rafael Espindolace2d3c52010-10-18 20:25:33 +0000597 if (Section.getKind().isBSS())
598 return false;
599
Rafael Espindola8cecf252010-10-06 16:23:36 +0000600 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000601 (Kind == MCSymbolRefExpr::VK_PLT ||
602 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
603 Kind == MCSymbolRefExpr::VK_GOTOFF))
Rafael Espindola3729d002010-10-05 23:57:26 +0000604 return true;
605
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000606 if (Section.getFlags() & MCSectionELF::SHF_MERGE)
607 return Target.getConstant() != 0;
608
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000609 return false;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000610}
611
Benjamin Kramere5b57342010-08-17 18:20:28 +0000612// FIXME: this is currently X86/X86_64 only
Matt Fleming3565a062010-08-16 18:57:57 +0000613void ELFObjectWriterImpl::RecordRelocation(const MCAssembler &Asm,
614 const MCAsmLayout &Layout,
615 const MCFragment *Fragment,
616 const MCFixup &Fixup,
617 MCValue Target,
618 uint64_t &FixedValue) {
Matt Fleming3565a062010-08-16 18:57:57 +0000619 int64_t Addend = 0;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000620 int Index = 0;
Benjamin Kramer95c602a2010-08-27 10:38:39 +0000621 int64_t Value = Target.getConstant();
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000622 const MCSymbol *Symbol = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000623
Rafael Espindola00074892010-09-17 22:34:41 +0000624 bool IsPCRel = isFixupKindX86PCRel(Fixup.getKind());
Benjamin Kramer81cfb852010-08-17 19:45:05 +0000625 if (!Target.isAbsolute()) {
Rafael Espindolade89b012010-10-15 18:25:33 +0000626 Symbol = &AliasedSymbol(Target.getSymA()->getSymbol());
Rafael Espindola88182132010-10-27 15:18:17 +0000627 const MCSymbol *Renamed = Renames.lookup(Symbol);
628 if (Renamed)
629 Symbol = Renamed;
Matt Fleming3565a062010-08-16 18:57:57 +0000630 MCSymbolData &SD = Asm.getSymbolData(*Symbol);
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000631 MCFragment *F = SD.getFragment();
Matt Fleming3565a062010-08-16 18:57:57 +0000632
Rafael Espindola9d8b7552010-10-03 00:46:57 +0000633 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
634 const MCSymbol &SymbolB = RefB->getSymbol();
635 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
636 IsPCRel = true;
Rafael Espindola55fb1022010-10-04 15:59:01 +0000637 MCSectionData *Sec = Fragment->getParent();
638
639 // Offset of the symbol in the section
640 int64_t a = Layout.getSymbolAddress(&SDB) - Layout.getSectionAddress(Sec);
641
642 // Ofeset of the relocation in the section
643 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
644 Value += b - a;
Rafael Espindola9d8b7552010-10-03 00:46:57 +0000645 }
646
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000647 // Check that this case has already been fully resolved before we get
648 // here.
Rafael Espindola00074892010-09-17 22:34:41 +0000649 if (Symbol->isDefined() && !SD.isExternal() &&
650 IsPCRel &&
651 &Fragment->getParent()->getSection() == &Symbol->getSection()) {
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000652 llvm_unreachable("We don't need a relocation in this case.");
Rafael Espindola00074892010-09-17 22:34:41 +0000653 return;
654 }
655
Rafael Espindola3729d002010-10-05 23:57:26 +0000656 bool RelocOnSymbol = ShouldRelocOnSymbol(SD, Target, *Fragment);
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000657 if (!RelocOnSymbol) {
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000658 Index = F->getParent()->getOrdinal();
Rafael Espindolaa6489182010-09-24 21:19:03 +0000659
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000660 MCSectionData *FSD = F->getParent();
661 // Offset of the symbol in the section
662 Value += Layout.getSymbolAddress(&SD) - Layout.getSectionAddress(FSD);
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000663 } else {
664 UsedInReloc.insert(Symbol);
665 Index = -1;
666 }
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000667 Addend = Value;
668 // Compensate for the addend on i386.
669 if (Is64Bit)
670 Value = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000671 }
672
Benjamin Kramer95c602a2010-08-27 10:38:39 +0000673 FixedValue = Value;
674
Matt Fleming3565a062010-08-16 18:57:57 +0000675 // determine the type of the relocation
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000676
677 MCSymbolRefExpr::VariantKind Modifier = Target.getSymA()->getKind();
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000678 unsigned Type;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000679 if (Is64Bit) {
680 if (IsPCRel) {
Rafael Espindola92bf6682010-10-04 19:04:13 +0000681 switch (Modifier) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000682 default:
683 llvm_unreachable("Unimplemented");
Rafael Espindola92bf6682010-10-04 19:04:13 +0000684 case MCSymbolRefExpr::VK_None:
685 Type = ELF::R_X86_64_PC32;
686 break;
687 case MCSymbolRefExpr::VK_PLT:
688 Type = ELF::R_X86_64_PLT32;
689 break;
Rafael Espindola607d1f62010-10-04 19:51:39 +0000690 case llvm::MCSymbolRefExpr::VK_GOTPCREL:
691 Type = ELF::R_X86_64_GOTPCREL;
692 break;
Rafael Espindolabc82d8b2010-10-27 20:28:07 +0000693 case MCSymbolRefExpr::VK_GOTTPOFF:
694 Type = ELF::R_X86_64_GOTTPOFF;
695 break;
696 case MCSymbolRefExpr::VK_TLSGD:
697 Type = ELF::R_X86_64_TLSGD;
698 break;
Rafael Espindola92bf6682010-10-04 19:04:13 +0000699 }
Benjamin Kramere5b57342010-08-17 18:20:28 +0000700 } else {
701 switch ((unsigned)Fixup.getKind()) {
702 default: llvm_unreachable("invalid fixup kind!");
703 case FK_Data_8: Type = ELF::R_X86_64_64; break;
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000704 case X86::reloc_signed_4byte:
Benjamin Kramere5b57342010-08-17 18:20:28 +0000705 case X86::reloc_pcrel_4byte:
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000706 assert(isInt<32>(Target.getConstant()));
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000707 switch (Modifier) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000708 default:
709 llvm_unreachable("Unimplemented");
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000710 case MCSymbolRefExpr::VK_None:
711 Type = ELF::R_X86_64_32S;
712 break;
713 case MCSymbolRefExpr::VK_GOT:
714 Type = ELF::R_X86_64_GOT32;
715 break;
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000716 case MCSymbolRefExpr::VK_GOTPCREL:
Rafael Espindola8cecf252010-10-06 16:23:36 +0000717 Type = ELF::R_X86_64_GOTPCREL;
718 break;
Rafael Espindolabc82d8b2010-10-27 20:28:07 +0000719 case MCSymbolRefExpr::VK_TPOFF:
720 Type = ELF::R_X86_64_TPOFF32;
721 break;
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000722 }
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000723 break;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000724 case FK_Data_4:
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000725 Type = ELF::R_X86_64_32;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000726 break;
727 case FK_Data_2: Type = ELF::R_X86_64_16; break;
728 case X86::reloc_pcrel_1byte:
729 case FK_Data_1: Type = ELF::R_X86_64_8; break;
730 }
731 }
Matt Fleming3565a062010-08-16 18:57:57 +0000732 } else {
Benjamin Kramere5b57342010-08-17 18:20:28 +0000733 if (IsPCRel) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000734 switch (Modifier) {
735 default:
Rafael Espindola24dc9ec2010-10-18 19:33:01 +0000736 llvm_unreachable("Unimplemented");
737 case MCSymbolRefExpr::VK_None:
Rafael Espindola9baee3b2010-10-18 18:03:28 +0000738 Type = ELF::R_386_PC32;
Rafael Espindola9baee3b2010-10-18 18:03:28 +0000739 break;
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000740 case MCSymbolRefExpr::VK_PLT:
741 Type = ELF::R_386_PLT32;
742 break;
743 }
Benjamin Kramere5b57342010-08-17 18:20:28 +0000744 } else {
745 switch ((unsigned)Fixup.getKind()) {
746 default: llvm_unreachable("invalid fixup kind!");
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000747
Rafael Espindola24ba4f72010-10-24 17:35:42 +0000748 case X86::reloc_global_offset_table:
749 Type = ELF::R_386_GOTPC;
750 break;
751
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000752 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
753 // instead?
754 case X86::reloc_signed_4byte:
Benjamin Kramere5b57342010-08-17 18:20:28 +0000755 case X86::reloc_pcrel_4byte:
Rafael Espindolabd701182010-10-19 19:31:37 +0000756 case FK_Data_4:
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000757 switch (Modifier) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000758 default:
759 llvm_unreachable("Unimplemented");
Rafael Espindolabd701182010-10-19 19:31:37 +0000760 case MCSymbolRefExpr::VK_None:
Rafael Espindola24ba4f72010-10-24 17:35:42 +0000761 Type = ELF::R_386_32;
Rafael Espindolabd701182010-10-19 19:31:37 +0000762 break;
Rafael Espindolaeada3042010-10-18 20:47:21 +0000763 case MCSymbolRefExpr::VK_GOT:
764 Type = ELF::R_386_GOT32;
765 break;
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000766 case MCSymbolRefExpr::VK_GOTOFF:
767 Type = ELF::R_386_GOTOFF;
768 break;
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000769 }
770 break;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000771 case FK_Data_2: Type = ELF::R_386_16; break;
772 case X86::reloc_pcrel_1byte:
773 case FK_Data_1: Type = ELF::R_386_8; break;
774 }
Matt Fleming3565a062010-08-16 18:57:57 +0000775 }
776 }
777
Rafael Espindola5c77c162010-10-05 15:48:37 +0000778 if (RelocNeedsGOT(Type))
779 NeedsGOT = true;
780
Benjamin Kramere5b57342010-08-17 18:20:28 +0000781 ELFRelocationEntry ERE;
782
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000783 ERE.Index = Index;
784 ERE.Type = Type;
785 ERE.Symbol = Symbol;
Matt Fleming3565a062010-08-16 18:57:57 +0000786
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000787 ERE.r_offset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
Benjamin Kramere5b57342010-08-17 18:20:28 +0000788
Matt Fleming3565a062010-08-16 18:57:57 +0000789 if (HasRelocationAddend)
790 ERE.r_addend = Addend;
Benjamin Kramer172d7d62010-08-17 00:33:24 +0000791 else
792 ERE.r_addend = 0; // Silence compiler warning.
Matt Fleming3565a062010-08-16 18:57:57 +0000793
794 Relocations[Fragment->getParent()].push_back(ERE);
795}
796
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000797uint64_t
798ELFObjectWriterImpl::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
799 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000800 MCSymbolData &SD = Asm.getSymbolData(*S);
Eli Friedmanf8020a32010-08-16 19:15:06 +0000801
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000802 // Local symbol.
803 if (!SD.isExternal() && !S->isUndefined())
804 return SD.getIndex() + /* empty symbol */ 1;
805
806 // External or undefined symbol.
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000807 return SD.getIndex() + NumRegularSections + /* empty symbol */ 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000808}
809
Rafael Espindola737cd212010-10-05 18:01:23 +0000810static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
Rafael Espindola88182132010-10-27 15:18:17 +0000811 bool Used, bool Renamed) {
Rafael Espindolabd701182010-10-19 19:31:37 +0000812 if (Used)
813 return true;
814
Rafael Espindola88182132010-10-27 15:18:17 +0000815 if (Renamed)
816 return false;
817
Rafael Espindola737cd212010-10-05 18:01:23 +0000818 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000819
820 const MCSymbol &A = AliasedSymbol(Symbol);
821 if (&A != &Symbol && A.isUndefined())
822 return false;
823
Rafael Espindola737cd212010-10-05 18:01:23 +0000824 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
825 return false;
826
Rafael Espindolabd701182010-10-19 19:31:37 +0000827 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000828 return false;
829
830 return true;
831}
832
833static bool isLocal(const MCSymbolData &Data) {
834 if (Data.isExternal())
835 return false;
836
837 const MCSymbol &Symbol = Data.getSymbol();
838 if (Symbol.isUndefined() && !Symbol.isVariable())
839 return false;
840
841 return true;
842}
843
Matt Fleming3565a062010-08-16 18:57:57 +0000844void ELFObjectWriterImpl::ComputeSymbolTable(MCAssembler &Asm) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000845 // FIXME: Is this the correct place to do this?
846 if (NeedsGOT) {
847 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
848 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
849 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
850 Data.setExternal(true);
851 }
852
Matt Fleming3565a062010-08-16 18:57:57 +0000853 // Build section lookup table.
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000854 NumRegularSections = Asm.size();
Rafael Espindolaf5c347d2010-10-05 21:20:07 +0000855 DenseMap<const MCSection*, uint32_t> SectionIndexMap;
Matt Fleming3565a062010-08-16 18:57:57 +0000856 unsigned Index = 1;
857 for (MCAssembler::iterator it = Asm.begin(),
858 ie = Asm.end(); it != ie; ++it, ++Index)
859 SectionIndexMap[&it->getSection()] = Index;
860
861 // Index 0 is always the empty string.
862 StringMap<uint64_t> StringIndexMap;
863 StringTable += '\x00';
864
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000865 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000866 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
867 ie = Asm.symbol_end(); it != ie; ++it) {
868 const MCSymbol &Symbol = it->getSymbol();
869
Rafael Espindola88182132010-10-27 15:18:17 +0000870 if (!isInSymtab(Asm, *it, UsedInReloc.count(&Symbol),
871 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000872 continue;
873
Matt Fleming3565a062010-08-16 18:57:57 +0000874 ELFSymbolData MSD;
875 MSD.SymbolData = it;
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000876 bool Local = isLocal(*it);
Rafael Espindola88182132010-10-27 15:18:17 +0000877 const MCSymbol &RefSymbol = AliasedSymbol(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000878
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000879 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000880 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000881 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000882 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000883 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000884 } else if (RefSymbol.isUndefined()) {
Matt Fleming3565a062010-08-16 18:57:57 +0000885 MSD.SectionIndex = ELF::SHN_UNDEF;
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000886 // FIXME: Undefined symbols are global, but this is the first place we
887 // are able to set it.
888 if (GetBinding(*it) == ELF::STB_LOCAL)
889 SetBinding(*it, ELF::STB_GLOBAL);
Matt Fleming3565a062010-08-16 18:57:57 +0000890 } else {
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000891 MSD.SectionIndex = SectionIndexMap.lookup(&RefSymbol.getSection());
Matt Fleming3565a062010-08-16 18:57:57 +0000892 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000893 }
894
Rafael Espindola88182132010-10-27 15:18:17 +0000895 // The @@@ in symbol version is replaced with @ in undefined symbols and
896 // @@ in defined ones.
897 StringRef Name = Symbol.getName();
898 size_t Pos = Name.find("@@@");
899 std::string FinalName;
900 if (Pos != StringRef::npos) {
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000901 StringRef Prefix = Name.substr(0, Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000902 unsigned n = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000903 StringRef Suffix = Name.substr(Pos + n);
Rafael Espindola88182132010-10-27 15:18:17 +0000904 FinalName = Prefix.str() + Suffix.str();
905 } else {
906 FinalName = Name.str();
907 }
908
909 uint64_t &Entry = StringIndexMap[FinalName];
Rafael Espindolaa6866962010-10-27 14:44:52 +0000910 if (!Entry) {
911 Entry = StringTable.size();
Rafael Espindola88182132010-10-27 15:18:17 +0000912 StringTable += FinalName;
Rafael Espindolaa6866962010-10-27 14:44:52 +0000913 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +0000914 }
Rafael Espindolaa6866962010-10-27 14:44:52 +0000915 MSD.StringIndex = Entry;
916 if (MSD.SectionIndex == ELF::SHN_UNDEF)
917 UndefinedSymbolData.push_back(MSD);
918 else if (Local)
919 LocalSymbolData.push_back(MSD);
920 else
921 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +0000922 }
923
924 // Symbols are required to be in lexicographic order.
925 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
926 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
927 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
928
929 // Set the symbol indices. Local symbols must come before all other
930 // symbols with non-local bindings.
931 Index = 0;
932 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
933 LocalSymbolData[i].SymbolData->setIndex(Index++);
934 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
935 ExternalSymbolData[i].SymbolData->setIndex(Index++);
936 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
937 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
938}
939
940void ELFObjectWriterImpl::WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
941 const MCSectionData &SD) {
942 if (!Relocations[&SD].empty()) {
943 MCContext &Ctx = Asm.getContext();
944 const MCSection *RelaSection;
945 const MCSectionELF &Section =
946 static_cast<const MCSectionELF&>(SD.getSection());
947
948 const StringRef SectionName = Section.getSectionName();
Benjamin Kramer377a5722010-08-17 17:30:07 +0000949 std::string RelaSectionName = HasRelocationAddend ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +0000950 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000951
952 unsigned EntrySize;
953 if (HasRelocationAddend)
954 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
955 else
956 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +0000957
Benjamin Kramer377a5722010-08-17 17:30:07 +0000958 RelaSection = Ctx.getELFSection(RelaSectionName, HasRelocationAddend ?
959 ELF::SHT_RELA : ELF::SHT_REL, 0,
Matt Fleming3565a062010-08-16 18:57:57 +0000960 SectionKind::getReadOnly(),
961 false, EntrySize);
962
963 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Benjamin Kramera9eadca2010-09-06 16:11:52 +0000964 RelaSD.setAlignment(Is64Bit ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +0000965
966 MCDataFragment *F = new MCDataFragment(&RelaSD);
967
968 WriteRelocationsFragment(Asm, F, &SD);
969
Rafael Espindola70703872010-09-30 02:22:20 +0000970 Asm.AddSectionToTheEnd(*Writer, RelaSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000971 }
972}
973
974void ELFObjectWriterImpl::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
975 uint64_t Flags, uint64_t Address,
976 uint64_t Offset, uint64_t Size,
977 uint32_t Link, uint32_t Info,
978 uint64_t Alignment,
979 uint64_t EntrySize) {
980 Write32(Name); // sh_name: index into string table
981 Write32(Type); // sh_type
982 WriteWord(Flags); // sh_flags
983 WriteWord(Address); // sh_addr
984 WriteWord(Offset); // sh_offset
985 WriteWord(Size); // sh_size
986 Write32(Link); // sh_link
987 Write32(Info); // sh_info
988 WriteWord(Alignment); // sh_addralign
989 WriteWord(EntrySize); // sh_entsize
990}
991
992void ELFObjectWriterImpl::WriteRelocationsFragment(const MCAssembler &Asm,
993 MCDataFragment *F,
994 const MCSectionData *SD) {
995 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
996 // sort by the r_offset just like gnu as does
997 array_pod_sort(Relocs.begin(), Relocs.end());
998
999 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1000 ELFRelocationEntry entry = Relocs[e - i - 1];
1001
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001002 if (entry.Index < 0)
1003 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
1004 else
1005 entry.Index += LocalSymbolData.size() + 1;
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001006 if (Is64Bit) {
1007 char buf[8];
Matt Fleming3565a062010-08-16 18:57:57 +00001008
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001009 String64(buf, entry.r_offset);
1010 F->getContents() += StringRef(buf, 8);
1011
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001012 struct ELF::Elf64_Rela ERE64;
1013 ERE64.setSymbolAndType(entry.Index, entry.Type);
1014 String64(buf, ERE64.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001015 F->getContents() += StringRef(buf, 8);
1016
1017 if (HasRelocationAddend) {
1018 String64(buf, entry.r_addend);
1019 F->getContents() += StringRef(buf, 8);
1020 }
1021 } else {
1022 char buf[4];
1023
1024 String32(buf, entry.r_offset);
1025 F->getContents() += StringRef(buf, 4);
1026
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001027 struct ELF::Elf32_Rela ERE32;
1028 ERE32.setSymbolAndType(entry.Index, entry.Type);
1029 String32(buf, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001030 F->getContents() += StringRef(buf, 4);
1031
1032 if (HasRelocationAddend) {
1033 String32(buf, entry.r_addend);
1034 F->getContents() += StringRef(buf, 4);
1035 }
1036 }
Matt Fleming3565a062010-08-16 18:57:57 +00001037 }
1038}
1039
1040void ELFObjectWriterImpl::CreateMetadataSections(MCAssembler &Asm,
1041 MCAsmLayout &Layout) {
1042 MCContext &Ctx = Asm.getContext();
1043 MCDataFragment *F;
1044
Matt Fleming3565a062010-08-16 18:57:57 +00001045 const MCSection *SymtabSection;
1046 unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
1047
Rafael Espindola71859c62010-09-16 19:46:31 +00001048 unsigned NumRegularSections = Asm.size();
1049
Rafael Espindola38738bf2010-09-22 19:04:41 +00001050 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola71859c62010-09-16 19:46:31 +00001051 const MCSection *ShstrtabSection;
1052 ShstrtabSection = Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
1053 SectionKind::getReadOnly(), false);
1054 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1055 ShstrtabSD.setAlignment(1);
1056 ShstrtabIndex = Asm.size();
1057
Matt Fleming3565a062010-08-16 18:57:57 +00001058 SymtabSection = Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1059 SectionKind::getReadOnly(),
1060 false, EntrySize);
Matt Fleming3565a062010-08-16 18:57:57 +00001061 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Matt Fleming3565a062010-08-16 18:57:57 +00001062 SymtabSD.setAlignment(Is64Bit ? 8 : 4);
1063
Matt Fleming3565a062010-08-16 18:57:57 +00001064 const MCSection *StrtabSection;
1065 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
1066 SectionKind::getReadOnly(), false);
Matt Fleming3565a062010-08-16 18:57:57 +00001067 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1068 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +00001069 StringTableIndex = Asm.size();
1070
Rafael Espindolac3c413f2010-09-27 22:04:54 +00001071 WriteRelocations(Asm, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001072
1073 // Symbol table
1074 F = new MCDataFragment(&SymtabSD);
1075 WriteSymbolTable(F, Asm, Layout, NumRegularSections);
Rafael Espindola70703872010-09-30 02:22:20 +00001076 Asm.AddSectionToTheEnd(*Writer, SymtabSD, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001077
Matt Fleming3565a062010-08-16 18:57:57 +00001078 F = new MCDataFragment(&StrtabSD);
1079 F->getContents().append(StringTable.begin(), StringTable.end());
Rafael Espindola70703872010-09-30 02:22:20 +00001080 Asm.AddSectionToTheEnd(*Writer, StrtabSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +00001081
Matt Fleming3565a062010-08-16 18:57:57 +00001082 F = new MCDataFragment(&ShstrtabSD);
1083
Matt Fleming3565a062010-08-16 18:57:57 +00001084 // Section header string table.
1085 //
1086 // The first entry of a string table holds a null character so skip
1087 // section 0.
1088 uint64_t Index = 1;
1089 F->getContents() += '\x00';
1090
1091 for (MCAssembler::const_iterator it = Asm.begin(),
1092 ie = Asm.end(); it != ie; ++it) {
Matt Fleming3565a062010-08-16 18:57:57 +00001093 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +00001094 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola51efe7a2010-09-23 14:14:56 +00001095 // FIXME: We could merge suffixes like in .text and .rela.text.
Matt Fleming3565a062010-08-16 18:57:57 +00001096
1097 // Remember the index into the string table so we can write it
1098 // into the sh_name field of the section header table.
1099 SectionStringTableIndex[&it->getSection()] = Index;
1100
1101 Index += Section.getSectionName().size() + 1;
1102 F->getContents() += Section.getSectionName();
1103 F->getContents() += '\x00';
1104 }
1105
Rafael Espindola70703872010-09-30 02:22:20 +00001106 Asm.AddSectionToTheEnd(*Writer, ShstrtabSD, Layout);
1107}
1108
1109bool ELFObjectWriterImpl::IsFixupFullyResolved(const MCAssembler &Asm,
1110 const MCValue Target,
1111 bool IsPCRel,
1112 const MCFragment *DF) const {
1113 // If this is a PCrel relocation, find the section this fixup value is
1114 // relative to.
1115 const MCSection *BaseSection = 0;
1116 if (IsPCRel) {
1117 BaseSection = &DF->getParent()->getSection();
1118 assert(BaseSection);
1119 }
1120
1121 const MCSection *SectionA = 0;
1122 const MCSymbol *SymbolA = 0;
1123 if (const MCSymbolRefExpr *A = Target.getSymA()) {
1124 SymbolA = &A->getSymbol();
1125 SectionA = &SymbolA->getSection();
1126 }
1127
1128 const MCSection *SectionB = 0;
1129 if (const MCSymbolRefExpr *B = Target.getSymB()) {
1130 SectionB = &B->getSymbol().getSection();
1131 }
1132
1133 if (!BaseSection)
1134 return SectionA == SectionB;
1135
1136 const MCSymbolData &DataA = Asm.getSymbolData(*SymbolA);
1137 if (DataA.isExternal())
1138 return false;
1139
1140 return !SectionB && BaseSection == SectionA;
Matt Fleming3565a062010-08-16 18:57:57 +00001141}
1142
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001143void ELFObjectWriterImpl::WriteObject(MCAssembler &Asm,
Matt Fleming3565a062010-08-16 18:57:57 +00001144 const MCAsmLayout &Layout) {
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001145 // Compute symbol table information.
1146 ComputeSymbolTable(Asm);
1147
Matt Fleming3565a062010-08-16 18:57:57 +00001148 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
1149 const_cast<MCAsmLayout&>(Layout));
1150
1151 // Add 1 for the null section.
1152 unsigned NumSections = Asm.size() + 1;
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001153 uint64_t NaturalAlignment = Is64Bit ? 8 : 4;
1154 uint64_t HeaderSize = Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr);
1155 uint64_t FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001156
1157 for (MCAssembler::const_iterator it = Asm.begin(),
1158 ie = Asm.end(); it != ie; ++it) {
1159 const MCSectionData &SD = *it;
Matt Fleming3565a062010-08-16 18:57:57 +00001160
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001161 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1162
Matt Fleming3565a062010-08-16 18:57:57 +00001163 // Get the size of the section in the output file (including padding).
1164 uint64_t Size = Layout.getSectionFileSize(&SD);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001165
1166 FileOff += Size;
Matt Fleming3565a062010-08-16 18:57:57 +00001167 }
1168
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001169 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1170
Matt Fleming3565a062010-08-16 18:57:57 +00001171 // Write out the ELF header ...
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001172 WriteHeader(FileOff - HeaderSize, NumSections);
1173
1174 FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001175
1176 // ... then all of the sections ...
1177 DenseMap<const MCSection*, uint64_t> SectionOffsetMap;
1178
Rafael Espindolad8e0bfe2010-10-06 22:28:19 +00001179 DenseMap<const MCSection*, uint32_t> SectionIndexMap;
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001180
1181 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +00001182 for (MCAssembler::const_iterator it = Asm.begin(),
1183 ie = Asm.end(); it != ie; ++it) {
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001184 const MCSectionData &SD = *it;
1185
1186 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1187 WriteZeros(Padding);
1188 FileOff += Padding;
1189
Matt Fleming3565a062010-08-16 18:57:57 +00001190 // Remember the offset into the file for this section.
1191 SectionOffsetMap[&it->getSection()] = FileOff;
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001192 SectionIndexMap[&it->getSection()] = Index++;
1193
Matt Fleming3565a062010-08-16 18:57:57 +00001194 FileOff += Layout.getSectionFileSize(&SD);
1195
1196 Asm.WriteSectionData(it, Layout, Writer);
1197 }
1198
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001199 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1200 WriteZeros(Padding);
1201 FileOff += Padding;
1202
Matt Fleming3565a062010-08-16 18:57:57 +00001203 // ... and then the section header table.
1204 // Should we align the section header table?
1205 //
1206 // Null section first.
1207 WriteSecHdrEntry(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1208
1209 for (MCAssembler::const_iterator it = Asm.begin(),
1210 ie = Asm.end(); it != ie; ++it) {
1211 const MCSectionData &SD = *it;
1212 const MCSectionELF &Section =
1213 static_cast<const MCSectionELF&>(SD.getSection());
1214
1215 uint64_t sh_link = 0;
1216 uint64_t sh_info = 0;
1217
1218 switch(Section.getType()) {
1219 case ELF::SHT_DYNAMIC:
1220 sh_link = SectionStringTableIndex[&it->getSection()];
1221 sh_info = 0;
1222 break;
1223
1224 case ELF::SHT_REL:
Eli Friedmanf8020a32010-08-16 19:15:06 +00001225 case ELF::SHT_RELA: {
Matt Fleming3565a062010-08-16 18:57:57 +00001226 const MCSection *SymtabSection;
1227 const MCSection *InfoSection;
Matt Fleming3565a062010-08-16 18:57:57 +00001228
1229 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1230 SectionKind::getReadOnly(),
Eli Friedmanf8020a32010-08-16 19:15:06 +00001231 false);
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001232 sh_link = SectionIndexMap[SymtabSection];
Matt Fleming3565a062010-08-16 18:57:57 +00001233
Benjamin Kramer377a5722010-08-17 17:30:07 +00001234 // Remove ".rel" and ".rela" prefixes.
1235 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1236 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
1237
Eli Friedmanf8020a32010-08-16 19:15:06 +00001238 InfoSection = Asm.getContext().getELFSection(SectionName,
Matt Fleming3565a062010-08-16 18:57:57 +00001239 ELF::SHT_PROGBITS, 0,
Eli Friedmanf8020a32010-08-16 19:15:06 +00001240 SectionKind::getReadOnly(),
1241 false);
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001242 sh_info = SectionIndexMap[InfoSection];
Matt Fleming3565a062010-08-16 18:57:57 +00001243 break;
Eli Friedmanf8020a32010-08-16 19:15:06 +00001244 }
Matt Fleming3565a062010-08-16 18:57:57 +00001245
1246 case ELF::SHT_SYMTAB:
1247 case ELF::SHT_DYNSYM:
1248 sh_link = StringTableIndex;
1249 sh_info = LastLocalSymbolIndex;
1250 break;
1251
1252 case ELF::SHT_PROGBITS:
1253 case ELF::SHT_STRTAB:
1254 case ELF::SHT_NOBITS:
Benjamin Kramer19dc7fa2010-08-31 17:03:33 +00001255 case ELF::SHT_NULL:
Rafael Espindolacecbc3d2010-10-25 17:50:35 +00001256 case ELF::SHT_ARM_ATTRIBUTES:
Matt Fleming3565a062010-08-16 18:57:57 +00001257 // Nothing to do.
1258 break;
1259
Matt Fleming3565a062010-08-16 18:57:57 +00001260 default:
1261 assert(0 && "FIXME: sh_type value not supported!");
1262 break;
1263 }
1264
1265 WriteSecHdrEntry(SectionStringTableIndex[&it->getSection()],
1266 Section.getType(), Section.getFlags(),
Rafael Espindola71859c62010-09-16 19:46:31 +00001267 0,
Matt Fleming3565a062010-08-16 18:57:57 +00001268 SectionOffsetMap.lookup(&SD.getSection()),
1269 Layout.getSectionSize(&SD), sh_link,
1270 sh_info, SD.getAlignment(),
1271 Section.getEntrySize());
1272 }
1273}
1274
1275ELFObjectWriter::ELFObjectWriter(raw_ostream &OS,
1276 bool Is64Bit,
Roman Divacky5baf79e2010-09-09 17:57:50 +00001277 Triple::OSType OSType,
Wesley Peckeecb8582010-10-22 15:52:49 +00001278 uint16_t EMachine,
Matt Fleming3565a062010-08-16 18:57:57 +00001279 bool IsLittleEndian,
1280 bool HasRelocationAddend)
1281 : MCObjectWriter(OS, IsLittleEndian)
1282{
Wesley Peckeecb8582010-10-22 15:52:49 +00001283 Impl = new ELFObjectWriterImpl(this, Is64Bit, EMachine,
1284 HasRelocationAddend, OSType);
Matt Fleming3565a062010-08-16 18:57:57 +00001285}
1286
1287ELFObjectWriter::~ELFObjectWriter() {
1288 delete (ELFObjectWriterImpl*) Impl;
1289}
1290
1291void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm) {
1292 ((ELFObjectWriterImpl*) Impl)->ExecutePostLayoutBinding(Asm);
1293}
1294
1295void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
1296 const MCAsmLayout &Layout,
1297 const MCFragment *Fragment,
1298 const MCFixup &Fixup, MCValue Target,
1299 uint64_t &FixedValue) {
1300 ((ELFObjectWriterImpl*) Impl)->RecordRelocation(Asm, Layout, Fragment, Fixup,
1301 Target, FixedValue);
1302}
1303
Rafael Espindola70703872010-09-30 02:22:20 +00001304bool ELFObjectWriter::IsFixupFullyResolved(const MCAssembler &Asm,
1305 const MCValue Target,
1306 bool IsPCRel,
1307 const MCFragment *DF) const {
1308 return ((ELFObjectWriterImpl*) Impl)->IsFixupFullyResolved(Asm, Target,
1309 IsPCRel, DF);
1310}
1311
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001312void ELFObjectWriter::WriteObject(MCAssembler &Asm,
Matt Fleming3565a062010-08-16 18:57:57 +00001313 const MCAsmLayout &Layout) {
1314 ((ELFObjectWriterImpl*) Impl)->WriteObject(Asm, Layout);
1315}