blob: fda8ac47c36ba51c85e7b2b672183d1a4db790e1 [file] [log] [blame]
Matt Fleming3565a062010-08-16 18:57:57 +00001//===- lib/MC/ELFObjectWriter.cpp - ELF File Writer -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements ELF object file writer information.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/MC/ELFObjectWriter.h"
Rafael Espindola8f413fa2010-10-05 15:11:03 +000015#include "llvm/ADT/SmallPtrSet.h"
Matt Fleming3565a062010-08-16 18:57:57 +000016#include "llvm/ADT/STLExtras.h"
17#include "llvm/ADT/StringMap.h"
18#include "llvm/ADT/Twine.h"
19#include "llvm/MC/MCAssembler.h"
20#include "llvm/MC/MCAsmLayout.h"
21#include "llvm/MC/MCContext.h"
22#include "llvm/MC/MCELFSymbolFlags.h"
23#include "llvm/MC/MCExpr.h"
24#include "llvm/MC/MCObjectWriter.h"
25#include "llvm/MC/MCSectionELF.h"
26#include "llvm/MC/MCSymbol.h"
27#include "llvm/MC/MCValue.h"
28#include "llvm/Support/Debug.h"
29#include "llvm/Support/ErrorHandling.h"
30#include "llvm/Support/ELF.h"
31#include "llvm/Target/TargetAsmBackend.h"
32
33#include "../Target/X86/X86FixupKinds.h"
34
35#include <vector>
36using namespace llvm;
37
Rafael Espindolaad49cf52010-09-18 15:03:21 +000038static unsigned GetType(const MCSymbolData &SD) {
39 uint32_t Type = (SD.getFlags() & (0xf << ELF_STT_Shift)) >> ELF_STT_Shift;
40 assert(Type == ELF::STT_NOTYPE || Type == ELF::STT_OBJECT ||
41 Type == ELF::STT_FUNC || Type == ELF::STT_SECTION ||
42 Type == ELF::STT_FILE || Type == ELF::STT_COMMON ||
43 Type == ELF::STT_TLS);
44 return Type;
45}
46
Rafael Espindolae15eb4e2010-09-23 19:55:14 +000047static unsigned GetBinding(const MCSymbolData &SD) {
48 uint32_t Binding = (SD.getFlags() & (0xf << ELF_STB_Shift)) >> ELF_STB_Shift;
49 assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
50 Binding == ELF::STB_WEAK);
51 return Binding;
52}
53
54static void SetBinding(MCSymbolData &SD, unsigned Binding) {
55 assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
56 Binding == ELF::STB_WEAK);
57 uint32_t OtherFlags = SD.getFlags() & ~(0xf << ELF_STB_Shift);
58 SD.setFlags(OtherFlags | (Binding << ELF_STB_Shift));
59}
60
Rafael Espindola152c1062010-10-06 21:02:29 +000061static unsigned GetVisibility(MCSymbolData &SD) {
62 unsigned Visibility =
63 (SD.getFlags() & (0xf << ELF_STV_Shift)) >> ELF_STV_Shift;
64 assert(Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_INTERNAL ||
65 Visibility == ELF::STV_HIDDEN || Visibility == ELF::STV_PROTECTED);
66 return Visibility;
67}
68
Rafael Espindolacebdc012010-10-04 19:46:28 +000069static bool isFixupKindX86PCRel(unsigned Kind) {
70 switch (Kind) {
71 default:
72 return false;
73 case X86::reloc_pcrel_1byte:
74 case X86::reloc_pcrel_4byte:
75 case X86::reloc_riprel_4byte:
76 case X86::reloc_riprel_4byte_movq_load:
77 return true;
78 }
79}
80
Rafael Espindolaa0a2f872010-10-28 14:22:44 +000081static bool RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant) {
82 switch (Variant) {
Rafael Espindola5c77c162010-10-05 15:48:37 +000083 default:
84 return false;
Rafael Espindolaa0a2f872010-10-28 14:22:44 +000085 case MCSymbolRefExpr::VK_GOT:
86 case MCSymbolRefExpr::VK_PLT:
87 case MCSymbolRefExpr::VK_GOTPCREL:
88 case MCSymbolRefExpr::VK_TPOFF:
89 case MCSymbolRefExpr::VK_TLSGD:
90 case MCSymbolRefExpr::VK_GOTTPOFF:
91 case MCSymbolRefExpr::VK_INDNTPOFF:
92 case MCSymbolRefExpr::VK_NTPOFF:
93 case MCSymbolRefExpr::VK_GOTNTPOFF:
Rafael Espindola5c77c162010-10-05 15:48:37 +000094 return true;
95 }
96}
97
Matt Fleming3565a062010-08-16 18:57:57 +000098namespace {
99
100 class ELFObjectWriterImpl {
Chris Lattnerb188a372010-08-28 03:21:03 +0000101 /*static bool isFixupKindX86RIPRel(unsigned Kind) {
Matt Fleming3565a062010-08-16 18:57:57 +0000102 return Kind == X86::reloc_riprel_4byte ||
103 Kind == X86::reloc_riprel_4byte_movq_load;
Chris Lattnerb188a372010-08-28 03:21:03 +0000104 }*/
Matt Fleming3565a062010-08-16 18:57:57 +0000105
106
107 /// ELFSymbolData - Helper struct for containing some precomputed information
108 /// on symbols.
109 struct ELFSymbolData {
110 MCSymbolData *SymbolData;
111 uint64_t StringIndex;
112 uint32_t SectionIndex;
113
114 // Support lexicographic sorting.
115 bool operator<(const ELFSymbolData &RHS) const {
Rafael Espindolaad49cf52010-09-18 15:03:21 +0000116 if (GetType(*SymbolData) == ELF::STT_FILE)
117 return true;
118 if (GetType(*RHS.SymbolData) == ELF::STT_FILE)
119 return false;
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000120 return SymbolData->getSymbol().getName() <
121 RHS.SymbolData->getSymbol().getName();
Matt Fleming3565a062010-08-16 18:57:57 +0000122 }
123 };
124
125 /// @name Relocation Data
126 /// @{
127
128 struct ELFRelocationEntry {
129 // Make these big enough for both 32-bit and 64-bit
130 uint64_t r_offset;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000131 int Index;
132 unsigned Type;
133 const MCSymbol *Symbol;
Matt Fleming3565a062010-08-16 18:57:57 +0000134 uint64_t r_addend;
135
136 // Support lexicographic sorting.
137 bool operator<(const ELFRelocationEntry &RE) const {
138 return RE.r_offset < r_offset;
139 }
140 };
141
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000142 SmallPtrSet<const MCSymbol *, 16> UsedInReloc;
Rafael Espindola88182132010-10-27 15:18:17 +0000143 DenseMap<const MCSymbol *, const MCSymbol *> Renames;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000144
Matt Fleming3565a062010-08-16 18:57:57 +0000145 llvm::DenseMap<const MCSectionData*,
146 std::vector<ELFRelocationEntry> > Relocations;
147 DenseMap<const MCSection*, uint64_t> SectionStringTableIndex;
148
149 /// @}
150 /// @name Symbol Table Data
151 /// @{
152
153 SmallString<256> StringTable;
154 std::vector<ELFSymbolData> LocalSymbolData;
155 std::vector<ELFSymbolData> ExternalSymbolData;
156 std::vector<ELFSymbolData> UndefinedSymbolData;
157
158 /// @}
159
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000160 int NumRegularSections;
161
Rafael Espindola5c77c162010-10-05 15:48:37 +0000162 bool NeedsGOT;
163
Matt Fleming3565a062010-08-16 18:57:57 +0000164 ELFObjectWriter *Writer;
165
166 raw_ostream &OS;
167
Matt Fleming3565a062010-08-16 18:57:57 +0000168 unsigned Is64Bit : 1;
169
170 bool HasRelocationAddend;
171
Roman Divacky5baf79e2010-09-09 17:57:50 +0000172 Triple::OSType OSType;
173
Wesley Peckeecb8582010-10-22 15:52:49 +0000174 uint16_t EMachine;
175
Matt Fleming3565a062010-08-16 18:57:57 +0000176 // This holds the symbol table index of the last local symbol.
177 unsigned LastLocalSymbolIndex;
178 // This holds the .strtab section index.
179 unsigned StringTableIndex;
180
181 unsigned ShstrtabIndex;
182
183 public:
184 ELFObjectWriterImpl(ELFObjectWriter *_Writer, bool _Is64Bit,
Wesley Peckeecb8582010-10-22 15:52:49 +0000185 uint16_t _EMachine, bool _HasRelAddend,
186 Triple::OSType _OSType)
Rafael Espindola5c77c162010-10-05 15:48:37 +0000187 : NeedsGOT(false), Writer(_Writer), OS(Writer->getStream()),
Roman Divacky5baf79e2010-09-09 17:57:50 +0000188 Is64Bit(_Is64Bit), HasRelocationAddend(_HasRelAddend),
Wesley Peckeecb8582010-10-22 15:52:49 +0000189 OSType(_OSType), EMachine(_EMachine) {
Matt Fleming3565a062010-08-16 18:57:57 +0000190 }
191
192 void Write8(uint8_t Value) { Writer->Write8(Value); }
193 void Write16(uint16_t Value) { Writer->Write16(Value); }
194 void Write32(uint32_t Value) { Writer->Write32(Value); }
Chris Lattnerb188a372010-08-28 03:21:03 +0000195 //void Write64(uint64_t Value) { Writer->Write64(Value); }
Matt Fleming3565a062010-08-16 18:57:57 +0000196 void WriteZeros(unsigned N) { Writer->WriteZeros(N); }
Chris Lattnerb188a372010-08-28 03:21:03 +0000197 //void WriteBytes(StringRef Str, unsigned ZeroFillSize = 0) {
198 // Writer->WriteBytes(Str, ZeroFillSize);
199 //}
Matt Fleming3565a062010-08-16 18:57:57 +0000200
201 void WriteWord(uint64_t W) {
Chris Lattnerb188a372010-08-28 03:21:03 +0000202 if (Is64Bit)
Matt Fleming3565a062010-08-16 18:57:57 +0000203 Writer->Write64(W);
Chris Lattnerb188a372010-08-28 03:21:03 +0000204 else
Matt Fleming3565a062010-08-16 18:57:57 +0000205 Writer->Write32(W);
Matt Fleming3565a062010-08-16 18:57:57 +0000206 }
207
208 void String8(char *buf, uint8_t Value) {
209 buf[0] = Value;
210 }
211
212 void StringLE16(char *buf, uint16_t Value) {
213 buf[0] = char(Value >> 0);
214 buf[1] = char(Value >> 8);
215 }
216
217 void StringLE32(char *buf, uint32_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000218 StringLE16(buf, uint16_t(Value >> 0));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000219 StringLE16(buf + 2, uint16_t(Value >> 16));
Matt Fleming3565a062010-08-16 18:57:57 +0000220 }
221
222 void StringLE64(char *buf, uint64_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000223 StringLE32(buf, uint32_t(Value >> 0));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000224 StringLE32(buf + 4, uint32_t(Value >> 32));
Matt Fleming3565a062010-08-16 18:57:57 +0000225 }
226
227 void StringBE16(char *buf ,uint16_t Value) {
228 buf[0] = char(Value >> 8);
229 buf[1] = char(Value >> 0);
230 }
231
232 void StringBE32(char *buf, uint32_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000233 StringBE16(buf, uint16_t(Value >> 16));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000234 StringBE16(buf + 2, uint16_t(Value >> 0));
Matt Fleming3565a062010-08-16 18:57:57 +0000235 }
236
237 void StringBE64(char *buf, uint64_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000238 StringBE32(buf, uint32_t(Value >> 32));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000239 StringBE32(buf + 4, uint32_t(Value >> 0));
Matt Fleming3565a062010-08-16 18:57:57 +0000240 }
241
242 void String16(char *buf, uint16_t Value) {
243 if (Writer->isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000244 StringLE16(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000245 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000246 StringBE16(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000247 }
248
249 void String32(char *buf, uint32_t Value) {
250 if (Writer->isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000251 StringLE32(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000252 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000253 StringBE32(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000254 }
255
256 void String64(char *buf, uint64_t Value) {
257 if (Writer->isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000258 StringLE64(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000259 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000260 StringBE64(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000261 }
262
263 void WriteHeader(uint64_t SectionDataSize, unsigned NumberOfSections);
264
265 void WriteSymbolEntry(MCDataFragment *F, uint64_t name, uint8_t info,
266 uint64_t value, uint64_t size,
267 uint8_t other, uint16_t shndx);
268
269 void WriteSymbol(MCDataFragment *F, ELFSymbolData &MSD,
270 const MCAsmLayout &Layout);
271
272 void WriteSymbolTable(MCDataFragment *F, const MCAssembler &Asm,
Rafael Espindola71859c62010-09-16 19:46:31 +0000273 const MCAsmLayout &Layout,
274 unsigned NumRegularSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000275
276 void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
277 const MCFragment *Fragment, const MCFixup &Fixup,
278 MCValue Target, uint64_t &FixedValue);
279
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000280 uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm,
281 const MCSymbol *S);
Matt Fleming3565a062010-08-16 18:57:57 +0000282
283 /// ComputeSymbolTable - Compute the symbol table data
284 ///
285 /// \param StringTable [out] - The string table data.
286 /// \param StringIndexMap [out] - Map from symbol names to offsets in the
287 /// string table.
288 void ComputeSymbolTable(MCAssembler &Asm);
289
290 void WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
291 const MCSectionData &SD);
292
293 void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout) {
294 for (MCAssembler::const_iterator it = Asm.begin(),
295 ie = Asm.end(); it != ie; ++it) {
296 WriteRelocation(Asm, Layout, *it);
297 }
298 }
299
300 void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout);
301
Rafael Espindola88182132010-10-27 15:18:17 +0000302 void ExecutePostLayoutBinding(MCAssembler &Asm);
Matt Fleming3565a062010-08-16 18:57:57 +0000303
304 void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
305 uint64_t Address, uint64_t Offset,
306 uint64_t Size, uint32_t Link, uint32_t Info,
307 uint64_t Alignment, uint64_t EntrySize);
308
309 void WriteRelocationsFragment(const MCAssembler &Asm, MCDataFragment *F,
310 const MCSectionData *SD);
311
Rafael Espindola70703872010-09-30 02:22:20 +0000312 bool IsFixupFullyResolved(const MCAssembler &Asm,
313 const MCValue Target,
314 bool IsPCRel,
315 const MCFragment *DF) const;
316
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000317 void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000318 };
319
320}
321
322// Emit the ELF header.
323void ELFObjectWriterImpl::WriteHeader(uint64_t SectionDataSize,
324 unsigned NumberOfSections) {
325 // ELF Header
326 // ----------
327 //
328 // Note
329 // ----
330 // emitWord method behaves differently for ELF32 and ELF64, writing
331 // 4 bytes in the former and 8 in the latter.
332
333 Write8(0x7f); // e_ident[EI_MAG0]
334 Write8('E'); // e_ident[EI_MAG1]
335 Write8('L'); // e_ident[EI_MAG2]
336 Write8('F'); // e_ident[EI_MAG3]
337
338 Write8(Is64Bit ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
339
340 // e_ident[EI_DATA]
341 Write8(Writer->isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
342
343 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky5baf79e2010-09-09 17:57:50 +0000344 // e_ident[EI_OSABI]
345 switch (OSType) {
346 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
347 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
348 default: Write8(ELF::ELFOSABI_NONE); break;
349 }
Matt Fleming3565a062010-08-16 18:57:57 +0000350 Write8(0); // e_ident[EI_ABIVERSION]
351
352 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
353
354 Write16(ELF::ET_REL); // e_type
355
Wesley Peckeecb8582010-10-22 15:52:49 +0000356 Write16(EMachine); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +0000357
358 Write32(ELF::EV_CURRENT); // e_version
359 WriteWord(0); // e_entry, no entry point in .o file
360 WriteWord(0); // e_phoff, no program header for .o
Benjamin Kramereb976772010-08-17 17:02:29 +0000361 WriteWord(SectionDataSize + (Is64Bit ? sizeof(ELF::Elf64_Ehdr) :
362 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000363
364 // FIXME: Make this configurable.
365 Write32(0); // e_flags = whatever the target wants
366
367 // e_ehsize = ELF header size
368 Write16(Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
369
370 Write16(0); // e_phentsize = prog header entry size
371 Write16(0); // e_phnum = # prog header entries = 0
372
373 // e_shentsize = Section header entry size
374 Write16(Is64Bit ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
375
376 // e_shnum = # of section header ents
377 Write16(NumberOfSections);
378
379 // e_shstrndx = Section # of '.shstrtab'
380 Write16(ShstrtabIndex);
381}
382
383void ELFObjectWriterImpl::WriteSymbolEntry(MCDataFragment *F, uint64_t name,
384 uint8_t info, uint64_t value,
385 uint64_t size, uint8_t other,
386 uint16_t shndx) {
387 if (Is64Bit) {
388 char buf[8];
389
390 String32(buf, name);
391 F->getContents() += StringRef(buf, 4); // st_name
392
393 String8(buf, info);
394 F->getContents() += StringRef(buf, 1); // st_info
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000395
Matt Fleming3565a062010-08-16 18:57:57 +0000396 String8(buf, other);
397 F->getContents() += StringRef(buf, 1); // st_other
398
399 String16(buf, shndx);
400 F->getContents() += StringRef(buf, 2); // st_shndx
401
402 String64(buf, value);
403 F->getContents() += StringRef(buf, 8); // st_value
404
405 String64(buf, size);
406 F->getContents() += StringRef(buf, 8); // st_size
407 } else {
408 char buf[4];
409
410 String32(buf, name);
411 F->getContents() += StringRef(buf, 4); // st_name
412
413 String32(buf, value);
414 F->getContents() += StringRef(buf, 4); // st_value
415
416 String32(buf, size);
417 F->getContents() += StringRef(buf, 4); // st_size
418
419 String8(buf, info);
420 F->getContents() += StringRef(buf, 1); // st_info
421
422 String8(buf, other);
423 F->getContents() += StringRef(buf, 1); // st_other
424
425 String16(buf, shndx);
426 F->getContents() += StringRef(buf, 2); // st_shndx
427 }
428}
429
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000430static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout) {
431 if (Data.isCommon() && Data.isExternal())
432 return Data.getCommonAlignment();
433
434 const MCSymbol &Symbol = Data.getSymbol();
435 if (!Symbol.isInSection())
436 return 0;
437
438 if (!Data.isCommon() && !(Data.getFlags() & ELF_STB_Weak))
439 if (MCFragment *FF = Data.getFragment())
440 return Layout.getSymbolAddress(&Data) -
441 Layout.getSectionAddress(FF->getParent());
442
443 return 0;
444}
445
Rafael Espindolade89b012010-10-15 18:25:33 +0000446static const MCSymbol &AliasedSymbol(const MCSymbol &Symbol) {
447 const MCSymbol *S = &Symbol;
448 while (S->isVariable()) {
449 const MCExpr *Value = S->getVariableValue();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000450 if (Value->getKind() != MCExpr::SymbolRef)
451 return *S;
Rafael Espindolade89b012010-10-15 18:25:33 +0000452 const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr*>(Value);
453 S = &Ref->getSymbol();
454 }
455 return *S;
456}
457
Rafael Espindola88182132010-10-27 15:18:17 +0000458void ELFObjectWriterImpl::ExecutePostLayoutBinding(MCAssembler &Asm) {
459 // The presence of symbol versions causes undefined symbols and
460 // versions declared with @@@ to be renamed.
461
462 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
463 ie = Asm.symbol_end(); it != ie; ++it) {
464 const MCSymbol &Alias = it->getSymbol();
465 if (!Alias.isVariable())
466 continue;
467 const MCSymbol &Symbol = AliasedSymbol(Alias);
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000468 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000469 size_t Pos = AliasName.find('@');
470 if (Pos == StringRef::npos)
471 continue;
472
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000473 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000474 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
475 continue;
476
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000477 // FIXME: produce a better error message.
478 if (Symbol.isUndefined() && Rest.startswith("@@") &&
479 !Rest.startswith("@@@"))
480 report_fatal_error("A @@ version cannot be undefined");
481
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000482 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000483 }
484}
485
Matt Fleming3565a062010-08-16 18:57:57 +0000486void ELFObjectWriterImpl::WriteSymbol(MCDataFragment *F, ELFSymbolData &MSD,
487 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000488 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000489 MCSymbolData &Data =
490 Layout.getAssembler().getSymbolData(AliasedSymbol(OrigData.getSymbol()));
Rafael Espindola152c1062010-10-06 21:02:29 +0000491
492 uint8_t Binding = GetBinding(OrigData);
493 uint8_t Visibility = GetVisibility(OrigData);
494 uint8_t Type = GetType(Data);
495
496 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
497 uint8_t Other = Visibility;
498
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000499 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000500 uint64_t Size = 0;
501 const MCExpr *ESize;
502
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000503 assert(!(Data.isCommon() && !Data.isExternal()));
504
Matt Fleming3565a062010-08-16 18:57:57 +0000505 ESize = Data.getSize();
506 if (Data.getSize()) {
507 MCValue Res;
508 if (ESize->getKind() == MCExpr::Binary) {
509 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(ESize);
510
511 if (BE->EvaluateAsRelocatable(Res, &Layout)) {
Benjamin Kramer24f12062010-10-17 07:38:40 +0000512 assert(!Res.getSymA() || !Res.getSymA()->getSymbol().isDefined());
513 assert(!Res.getSymB() || !Res.getSymB()->getSymbol().isDefined());
Rafael Espindolaf230df92010-10-16 18:23:53 +0000514 Size = Res.getConstant();
Matt Fleming3565a062010-08-16 18:57:57 +0000515 }
516 } else if (ESize->getKind() == MCExpr::Constant) {
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000517 Size = static_cast<const MCConstantExpr *>(ESize)->getValue();
Matt Fleming3565a062010-08-16 18:57:57 +0000518 } else {
519 assert(0 && "Unsupported size expression");
520 }
521 }
522
523 // Write out the symbol table entry
524 WriteSymbolEntry(F, MSD.StringIndex, Info, Value,
525 Size, Other, MSD.SectionIndex);
526}
527
528void ELFObjectWriterImpl::WriteSymbolTable(MCDataFragment *F,
529 const MCAssembler &Asm,
Rafael Espindola71859c62010-09-16 19:46:31 +0000530 const MCAsmLayout &Layout,
531 unsigned NumRegularSections) {
Matt Fleming3565a062010-08-16 18:57:57 +0000532 // The string table must be emitted first because we need the index
533 // into the string table for all the symbol names.
534 assert(StringTable.size() && "Missing string table");
535
536 // FIXME: Make sure the start of the symbol table is aligned.
537
538 // The first entry is the undefined symbol entry.
539 unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000540 F->getContents().append(EntrySize, '\x00');
Matt Fleming3565a062010-08-16 18:57:57 +0000541
542 // Write the symbol table entries.
543 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
544 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
545 ELFSymbolData &MSD = LocalSymbolData[i];
546 WriteSymbol(F, MSD, Layout);
547 }
548
Rafael Espindola71859c62010-09-16 19:46:31 +0000549 // Write out a symbol table entry for each regular section.
Eli Friedmana44fa242010-08-16 21:17:09 +0000550 unsigned Index = 1;
Rafael Espindola71859c62010-09-16 19:46:31 +0000551 for (MCAssembler::const_iterator it = Asm.begin();
552 Index <= NumRegularSections; ++it, ++Index) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000553 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000554 static_cast<const MCSectionELF&>(it->getSection());
Eli Friedmana44fa242010-08-16 21:17:09 +0000555 // Leave out relocations so we don't have indexes within
556 // the relocations messed up
Benjamin Kramer377a5722010-08-17 17:30:07 +0000557 if (Section.getType() == ELF::SHT_RELA || Section.getType() == ELF::SHT_REL)
Eli Friedmana44fa242010-08-16 21:17:09 +0000558 continue;
Matt Fleming3565a062010-08-16 18:57:57 +0000559 WriteSymbolEntry(F, 0, ELF::STT_SECTION, 0, 0, ELF::STV_DEFAULT, Index);
Eli Friedmana44fa242010-08-16 21:17:09 +0000560 LastLocalSymbolIndex++;
561 }
Matt Fleming3565a062010-08-16 18:57:57 +0000562
563 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
564 ELFSymbolData &MSD = ExternalSymbolData[i];
565 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000566 assert(((Data.getFlags() & ELF_STB_Global) ||
567 (Data.getFlags() & ELF_STB_Weak)) &&
568 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Matt Fleming3565a062010-08-16 18:57:57 +0000569 WriteSymbol(F, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000570 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000571 LastLocalSymbolIndex++;
572 }
573
574 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
575 ELFSymbolData &MSD = UndefinedSymbolData[i];
576 MCSymbolData &Data = *MSD.SymbolData;
Matt Fleming3565a062010-08-16 18:57:57 +0000577 WriteSymbol(F, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000578 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000579 LastLocalSymbolIndex++;
580 }
581}
582
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000583static bool ShouldRelocOnSymbol(const MCSymbolData &SD,
Rafael Espindola3729d002010-10-05 23:57:26 +0000584 const MCValue &Target,
585 const MCFragment &F) {
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000586 const MCSymbol &Symbol = SD.getSymbol();
587 if (Symbol.isUndefined())
588 return true;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000589
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000590 const MCSectionELF &Section =
591 static_cast<const MCSectionELF&>(Symbol.getSection());
592
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000593 if (SD.isExternal())
594 return true;
595
Rafael Espindola8cecf252010-10-06 16:23:36 +0000596 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000597 const MCSectionELF &Sec2 =
598 static_cast<const MCSectionELF&>(F.getParent()->getSection());
599
Rafael Espindolace2d3c52010-10-18 20:25:33 +0000600 if (Section.getKind().isBSS())
601 return false;
602
Rafael Espindola8cecf252010-10-06 16:23:36 +0000603 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000604 (Kind == MCSymbolRefExpr::VK_PLT ||
605 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
606 Kind == MCSymbolRefExpr::VK_GOTOFF))
Rafael Espindola3729d002010-10-05 23:57:26 +0000607 return true;
608
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000609 if (Section.getFlags() & MCSectionELF::SHF_MERGE)
610 return Target.getConstant() != 0;
611
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000612 return false;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000613}
614
Benjamin Kramere5b57342010-08-17 18:20:28 +0000615// FIXME: this is currently X86/X86_64 only
Matt Fleming3565a062010-08-16 18:57:57 +0000616void ELFObjectWriterImpl::RecordRelocation(const MCAssembler &Asm,
617 const MCAsmLayout &Layout,
618 const MCFragment *Fragment,
619 const MCFixup &Fixup,
620 MCValue Target,
621 uint64_t &FixedValue) {
Matt Fleming3565a062010-08-16 18:57:57 +0000622 int64_t Addend = 0;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000623 int Index = 0;
Benjamin Kramer95c602a2010-08-27 10:38:39 +0000624 int64_t Value = Target.getConstant();
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000625 const MCSymbol *Symbol = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000626
Rafael Espindola00074892010-09-17 22:34:41 +0000627 bool IsPCRel = isFixupKindX86PCRel(Fixup.getKind());
Benjamin Kramer81cfb852010-08-17 19:45:05 +0000628 if (!Target.isAbsolute()) {
Rafael Espindolade89b012010-10-15 18:25:33 +0000629 Symbol = &AliasedSymbol(Target.getSymA()->getSymbol());
Rafael Espindola88182132010-10-27 15:18:17 +0000630 const MCSymbol *Renamed = Renames.lookup(Symbol);
631 if (Renamed)
632 Symbol = Renamed;
Matt Fleming3565a062010-08-16 18:57:57 +0000633 MCSymbolData &SD = Asm.getSymbolData(*Symbol);
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000634 MCFragment *F = SD.getFragment();
Matt Fleming3565a062010-08-16 18:57:57 +0000635
Rafael Espindola9d8b7552010-10-03 00:46:57 +0000636 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
637 const MCSymbol &SymbolB = RefB->getSymbol();
638 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
639 IsPCRel = true;
Rafael Espindola55fb1022010-10-04 15:59:01 +0000640 MCSectionData *Sec = Fragment->getParent();
641
642 // Offset of the symbol in the section
643 int64_t a = Layout.getSymbolAddress(&SDB) - Layout.getSectionAddress(Sec);
644
645 // Ofeset of the relocation in the section
646 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
647 Value += b - a;
Rafael Espindola9d8b7552010-10-03 00:46:57 +0000648 }
649
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000650 // Check that this case has already been fully resolved before we get
651 // here.
Rafael Espindola00074892010-09-17 22:34:41 +0000652 if (Symbol->isDefined() && !SD.isExternal() &&
653 IsPCRel &&
654 &Fragment->getParent()->getSection() == &Symbol->getSection()) {
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000655 llvm_unreachable("We don't need a relocation in this case.");
Rafael Espindola00074892010-09-17 22:34:41 +0000656 return;
657 }
658
Rafael Espindola3729d002010-10-05 23:57:26 +0000659 bool RelocOnSymbol = ShouldRelocOnSymbol(SD, Target, *Fragment);
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000660 if (!RelocOnSymbol) {
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000661 Index = F->getParent()->getOrdinal();
Rafael Espindolaa6489182010-09-24 21:19:03 +0000662
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000663 MCSectionData *FSD = F->getParent();
664 // Offset of the symbol in the section
665 Value += Layout.getSymbolAddress(&SD) - Layout.getSectionAddress(FSD);
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000666 } else {
667 UsedInReloc.insert(Symbol);
668 Index = -1;
669 }
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000670 Addend = Value;
671 // Compensate for the addend on i386.
672 if (Is64Bit)
673 Value = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000674 }
675
Benjamin Kramer95c602a2010-08-27 10:38:39 +0000676 FixedValue = Value;
677
Matt Fleming3565a062010-08-16 18:57:57 +0000678 // determine the type of the relocation
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000679
680 MCSymbolRefExpr::VariantKind Modifier = Target.getSymA()->getKind();
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000681 unsigned Type;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000682 if (Is64Bit) {
683 if (IsPCRel) {
Rafael Espindola92bf6682010-10-04 19:04:13 +0000684 switch (Modifier) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000685 default:
686 llvm_unreachable("Unimplemented");
Rafael Espindola92bf6682010-10-04 19:04:13 +0000687 case MCSymbolRefExpr::VK_None:
688 Type = ELF::R_X86_64_PC32;
689 break;
690 case MCSymbolRefExpr::VK_PLT:
691 Type = ELF::R_X86_64_PLT32;
692 break;
Rafael Espindolaa0a2f872010-10-28 14:22:44 +0000693 case MCSymbolRefExpr::VK_GOTPCREL:
Rafael Espindola607d1f62010-10-04 19:51:39 +0000694 Type = ELF::R_X86_64_GOTPCREL;
695 break;
Rafael Espindolabc82d8b2010-10-27 20:28:07 +0000696 case MCSymbolRefExpr::VK_GOTTPOFF:
697 Type = ELF::R_X86_64_GOTTPOFF;
698 break;
699 case MCSymbolRefExpr::VK_TLSGD:
700 Type = ELF::R_X86_64_TLSGD;
701 break;
Rafael Espindola92bf6682010-10-04 19:04:13 +0000702 }
Benjamin Kramere5b57342010-08-17 18:20:28 +0000703 } else {
704 switch ((unsigned)Fixup.getKind()) {
705 default: llvm_unreachable("invalid fixup kind!");
706 case FK_Data_8: Type = ELF::R_X86_64_64; break;
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000707 case X86::reloc_signed_4byte:
Benjamin Kramere5b57342010-08-17 18:20:28 +0000708 case X86::reloc_pcrel_4byte:
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000709 assert(isInt<32>(Target.getConstant()));
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000710 switch (Modifier) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000711 default:
712 llvm_unreachable("Unimplemented");
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000713 case MCSymbolRefExpr::VK_None:
714 Type = ELF::R_X86_64_32S;
715 break;
716 case MCSymbolRefExpr::VK_GOT:
717 Type = ELF::R_X86_64_GOT32;
718 break;
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000719 case MCSymbolRefExpr::VK_GOTPCREL:
Rafael Espindola8cecf252010-10-06 16:23:36 +0000720 Type = ELF::R_X86_64_GOTPCREL;
721 break;
Rafael Espindolabc82d8b2010-10-27 20:28:07 +0000722 case MCSymbolRefExpr::VK_TPOFF:
723 Type = ELF::R_X86_64_TPOFF32;
724 break;
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000725 }
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000726 break;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000727 case FK_Data_4:
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000728 Type = ELF::R_X86_64_32;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000729 break;
730 case FK_Data_2: Type = ELF::R_X86_64_16; break;
731 case X86::reloc_pcrel_1byte:
732 case FK_Data_1: Type = ELF::R_X86_64_8; break;
733 }
734 }
Matt Fleming3565a062010-08-16 18:57:57 +0000735 } else {
Benjamin Kramere5b57342010-08-17 18:20:28 +0000736 if (IsPCRel) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000737 switch (Modifier) {
738 default:
Rafael Espindola24dc9ec2010-10-18 19:33:01 +0000739 llvm_unreachable("Unimplemented");
740 case MCSymbolRefExpr::VK_None:
Rafael Espindola9baee3b2010-10-18 18:03:28 +0000741 Type = ELF::R_386_PC32;
Rafael Espindola9baee3b2010-10-18 18:03:28 +0000742 break;
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000743 case MCSymbolRefExpr::VK_PLT:
744 Type = ELF::R_386_PLT32;
745 break;
746 }
Benjamin Kramere5b57342010-08-17 18:20:28 +0000747 } else {
748 switch ((unsigned)Fixup.getKind()) {
749 default: llvm_unreachable("invalid fixup kind!");
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000750
Rafael Espindola24ba4f72010-10-24 17:35:42 +0000751 case X86::reloc_global_offset_table:
752 Type = ELF::R_386_GOTPC;
753 break;
754
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000755 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
756 // instead?
757 case X86::reloc_signed_4byte:
Benjamin Kramere5b57342010-08-17 18:20:28 +0000758 case X86::reloc_pcrel_4byte:
Rafael Espindolabd701182010-10-19 19:31:37 +0000759 case FK_Data_4:
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000760 switch (Modifier) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000761 default:
762 llvm_unreachable("Unimplemented");
Rafael Espindolabd701182010-10-19 19:31:37 +0000763 case MCSymbolRefExpr::VK_None:
Rafael Espindola24ba4f72010-10-24 17:35:42 +0000764 Type = ELF::R_386_32;
Rafael Espindolabd701182010-10-19 19:31:37 +0000765 break;
Rafael Espindolaeada3042010-10-18 20:47:21 +0000766 case MCSymbolRefExpr::VK_GOT:
767 Type = ELF::R_386_GOT32;
768 break;
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000769 case MCSymbolRefExpr::VK_GOTOFF:
770 Type = ELF::R_386_GOTOFF;
771 break;
Rafael Espindola3cede2d2010-10-27 21:23:52 +0000772 case MCSymbolRefExpr::VK_TLSGD:
773 Type = ELF::R_386_TLS_GD;
774 break;
775 case MCSymbolRefExpr::VK_TPOFF:
776 Type = ELF::R_386_TLS_LE_32;
777 break;
778 case MCSymbolRefExpr::VK_INDNTPOFF:
779 Type = ELF::R_386_TLS_IE;
780 break;
781 case MCSymbolRefExpr::VK_NTPOFF:
782 Type = ELF::R_386_TLS_LE;
783 break;
Rafael Espindolaa0a2f872010-10-28 14:22:44 +0000784 case MCSymbolRefExpr::VK_GOTNTPOFF:
785 Type = ELF::R_386_TLS_GOTIE;
786 break;
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000787 }
788 break;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000789 case FK_Data_2: Type = ELF::R_386_16; break;
790 case X86::reloc_pcrel_1byte:
791 case FK_Data_1: Type = ELF::R_386_8; break;
792 }
Matt Fleming3565a062010-08-16 18:57:57 +0000793 }
794 }
795
Rafael Espindolaa0a2f872010-10-28 14:22:44 +0000796 if (RelocNeedsGOT(Modifier))
Rafael Espindola5c77c162010-10-05 15:48:37 +0000797 NeedsGOT = true;
798
Benjamin Kramere5b57342010-08-17 18:20:28 +0000799 ELFRelocationEntry ERE;
800
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000801 ERE.Index = Index;
802 ERE.Type = Type;
803 ERE.Symbol = Symbol;
Matt Fleming3565a062010-08-16 18:57:57 +0000804
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000805 ERE.r_offset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
Benjamin Kramere5b57342010-08-17 18:20:28 +0000806
Matt Fleming3565a062010-08-16 18:57:57 +0000807 if (HasRelocationAddend)
808 ERE.r_addend = Addend;
Benjamin Kramer172d7d62010-08-17 00:33:24 +0000809 else
810 ERE.r_addend = 0; // Silence compiler warning.
Matt Fleming3565a062010-08-16 18:57:57 +0000811
812 Relocations[Fragment->getParent()].push_back(ERE);
813}
814
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000815uint64_t
816ELFObjectWriterImpl::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
817 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000818 MCSymbolData &SD = Asm.getSymbolData(*S);
Eli Friedmanf8020a32010-08-16 19:15:06 +0000819
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000820 // Local symbol.
821 if (!SD.isExternal() && !S->isUndefined())
822 return SD.getIndex() + /* empty symbol */ 1;
823
824 // External or undefined symbol.
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000825 return SD.getIndex() + NumRegularSections + /* empty symbol */ 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000826}
827
Rafael Espindola737cd212010-10-05 18:01:23 +0000828static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
Rafael Espindola88182132010-10-27 15:18:17 +0000829 bool Used, bool Renamed) {
Rafael Espindolabd701182010-10-19 19:31:37 +0000830 if (Used)
831 return true;
832
Rafael Espindola88182132010-10-27 15:18:17 +0000833 if (Renamed)
834 return false;
835
Rafael Espindola737cd212010-10-05 18:01:23 +0000836 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000837
838 const MCSymbol &A = AliasedSymbol(Symbol);
839 if (&A != &Symbol && A.isUndefined())
840 return false;
841
Rafael Espindola737cd212010-10-05 18:01:23 +0000842 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
843 return false;
844
Rafael Espindolabd701182010-10-19 19:31:37 +0000845 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000846 return false;
847
848 return true;
849}
850
851static bool isLocal(const MCSymbolData &Data) {
852 if (Data.isExternal())
853 return false;
854
855 const MCSymbol &Symbol = Data.getSymbol();
856 if (Symbol.isUndefined() && !Symbol.isVariable())
857 return false;
858
859 return true;
860}
861
Matt Fleming3565a062010-08-16 18:57:57 +0000862void ELFObjectWriterImpl::ComputeSymbolTable(MCAssembler &Asm) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000863 // FIXME: Is this the correct place to do this?
864 if (NeedsGOT) {
865 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
866 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
867 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
868 Data.setExternal(true);
869 }
870
Matt Fleming3565a062010-08-16 18:57:57 +0000871 // Build section lookup table.
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000872 NumRegularSections = Asm.size();
Rafael Espindolaf5c347d2010-10-05 21:20:07 +0000873 DenseMap<const MCSection*, uint32_t> SectionIndexMap;
Matt Fleming3565a062010-08-16 18:57:57 +0000874 unsigned Index = 1;
875 for (MCAssembler::iterator it = Asm.begin(),
876 ie = Asm.end(); it != ie; ++it, ++Index)
877 SectionIndexMap[&it->getSection()] = Index;
878
879 // Index 0 is always the empty string.
880 StringMap<uint64_t> StringIndexMap;
881 StringTable += '\x00';
882
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000883 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000884 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
885 ie = Asm.symbol_end(); it != ie; ++it) {
886 const MCSymbol &Symbol = it->getSymbol();
887
Rafael Espindola88182132010-10-27 15:18:17 +0000888 if (!isInSymtab(Asm, *it, UsedInReloc.count(&Symbol),
889 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000890 continue;
891
Matt Fleming3565a062010-08-16 18:57:57 +0000892 ELFSymbolData MSD;
893 MSD.SymbolData = it;
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000894 bool Local = isLocal(*it);
Rafael Espindola88182132010-10-27 15:18:17 +0000895 const MCSymbol &RefSymbol = AliasedSymbol(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000896
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000897 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000898 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000899 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000900 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000901 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000902 } else if (RefSymbol.isUndefined()) {
Matt Fleming3565a062010-08-16 18:57:57 +0000903 MSD.SectionIndex = ELF::SHN_UNDEF;
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000904 // FIXME: Undefined symbols are global, but this is the first place we
905 // are able to set it.
906 if (GetBinding(*it) == ELF::STB_LOCAL)
907 SetBinding(*it, ELF::STB_GLOBAL);
Matt Fleming3565a062010-08-16 18:57:57 +0000908 } else {
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000909 MSD.SectionIndex = SectionIndexMap.lookup(&RefSymbol.getSection());
Matt Fleming3565a062010-08-16 18:57:57 +0000910 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000911 }
912
Rafael Espindola88182132010-10-27 15:18:17 +0000913 // The @@@ in symbol version is replaced with @ in undefined symbols and
914 // @@ in defined ones.
915 StringRef Name = Symbol.getName();
916 size_t Pos = Name.find("@@@");
917 std::string FinalName;
918 if (Pos != StringRef::npos) {
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000919 StringRef Prefix = Name.substr(0, Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000920 unsigned n = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000921 StringRef Suffix = Name.substr(Pos + n);
Rafael Espindola88182132010-10-27 15:18:17 +0000922 FinalName = Prefix.str() + Suffix.str();
923 } else {
924 FinalName = Name.str();
925 }
926
927 uint64_t &Entry = StringIndexMap[FinalName];
Rafael Espindolaa6866962010-10-27 14:44:52 +0000928 if (!Entry) {
929 Entry = StringTable.size();
Rafael Espindola88182132010-10-27 15:18:17 +0000930 StringTable += FinalName;
Rafael Espindolaa6866962010-10-27 14:44:52 +0000931 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +0000932 }
Rafael Espindolaa6866962010-10-27 14:44:52 +0000933 MSD.StringIndex = Entry;
934 if (MSD.SectionIndex == ELF::SHN_UNDEF)
935 UndefinedSymbolData.push_back(MSD);
936 else if (Local)
937 LocalSymbolData.push_back(MSD);
938 else
939 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +0000940 }
941
942 // Symbols are required to be in lexicographic order.
943 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
944 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
945 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
946
947 // Set the symbol indices. Local symbols must come before all other
948 // symbols with non-local bindings.
949 Index = 0;
950 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
951 LocalSymbolData[i].SymbolData->setIndex(Index++);
952 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
953 ExternalSymbolData[i].SymbolData->setIndex(Index++);
954 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
955 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
956}
957
958void ELFObjectWriterImpl::WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
959 const MCSectionData &SD) {
960 if (!Relocations[&SD].empty()) {
961 MCContext &Ctx = Asm.getContext();
962 const MCSection *RelaSection;
963 const MCSectionELF &Section =
964 static_cast<const MCSectionELF&>(SD.getSection());
965
966 const StringRef SectionName = Section.getSectionName();
Benjamin Kramer377a5722010-08-17 17:30:07 +0000967 std::string RelaSectionName = HasRelocationAddend ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +0000968 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000969
970 unsigned EntrySize;
971 if (HasRelocationAddend)
972 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
973 else
974 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +0000975
Benjamin Kramer377a5722010-08-17 17:30:07 +0000976 RelaSection = Ctx.getELFSection(RelaSectionName, HasRelocationAddend ?
977 ELF::SHT_RELA : ELF::SHT_REL, 0,
Matt Fleming3565a062010-08-16 18:57:57 +0000978 SectionKind::getReadOnly(),
979 false, EntrySize);
980
981 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Benjamin Kramera9eadca2010-09-06 16:11:52 +0000982 RelaSD.setAlignment(Is64Bit ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +0000983
984 MCDataFragment *F = new MCDataFragment(&RelaSD);
985
986 WriteRelocationsFragment(Asm, F, &SD);
987
Rafael Espindola70703872010-09-30 02:22:20 +0000988 Asm.AddSectionToTheEnd(*Writer, RelaSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000989 }
990}
991
992void ELFObjectWriterImpl::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
993 uint64_t Flags, uint64_t Address,
994 uint64_t Offset, uint64_t Size,
995 uint32_t Link, uint32_t Info,
996 uint64_t Alignment,
997 uint64_t EntrySize) {
998 Write32(Name); // sh_name: index into string table
999 Write32(Type); // sh_type
1000 WriteWord(Flags); // sh_flags
1001 WriteWord(Address); // sh_addr
1002 WriteWord(Offset); // sh_offset
1003 WriteWord(Size); // sh_size
1004 Write32(Link); // sh_link
1005 Write32(Info); // sh_info
1006 WriteWord(Alignment); // sh_addralign
1007 WriteWord(EntrySize); // sh_entsize
1008}
1009
1010void ELFObjectWriterImpl::WriteRelocationsFragment(const MCAssembler &Asm,
1011 MCDataFragment *F,
1012 const MCSectionData *SD) {
1013 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
1014 // sort by the r_offset just like gnu as does
1015 array_pod_sort(Relocs.begin(), Relocs.end());
1016
1017 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1018 ELFRelocationEntry entry = Relocs[e - i - 1];
1019
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001020 if (entry.Index < 0)
1021 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
1022 else
1023 entry.Index += LocalSymbolData.size() + 1;
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001024 if (Is64Bit) {
1025 char buf[8];
Matt Fleming3565a062010-08-16 18:57:57 +00001026
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001027 String64(buf, entry.r_offset);
1028 F->getContents() += StringRef(buf, 8);
1029
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001030 struct ELF::Elf64_Rela ERE64;
1031 ERE64.setSymbolAndType(entry.Index, entry.Type);
1032 String64(buf, ERE64.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001033 F->getContents() += StringRef(buf, 8);
1034
1035 if (HasRelocationAddend) {
1036 String64(buf, entry.r_addend);
1037 F->getContents() += StringRef(buf, 8);
1038 }
1039 } else {
1040 char buf[4];
1041
1042 String32(buf, entry.r_offset);
1043 F->getContents() += StringRef(buf, 4);
1044
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001045 struct ELF::Elf32_Rela ERE32;
1046 ERE32.setSymbolAndType(entry.Index, entry.Type);
1047 String32(buf, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001048 F->getContents() += StringRef(buf, 4);
1049
1050 if (HasRelocationAddend) {
1051 String32(buf, entry.r_addend);
1052 F->getContents() += StringRef(buf, 4);
1053 }
1054 }
Matt Fleming3565a062010-08-16 18:57:57 +00001055 }
1056}
1057
1058void ELFObjectWriterImpl::CreateMetadataSections(MCAssembler &Asm,
1059 MCAsmLayout &Layout) {
1060 MCContext &Ctx = Asm.getContext();
1061 MCDataFragment *F;
1062
Matt Fleming3565a062010-08-16 18:57:57 +00001063 const MCSection *SymtabSection;
1064 unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
1065
Rafael Espindola71859c62010-09-16 19:46:31 +00001066 unsigned NumRegularSections = Asm.size();
1067
Rafael Espindola38738bf2010-09-22 19:04:41 +00001068 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola71859c62010-09-16 19:46:31 +00001069 const MCSection *ShstrtabSection;
1070 ShstrtabSection = Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
1071 SectionKind::getReadOnly(), false);
1072 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1073 ShstrtabSD.setAlignment(1);
1074 ShstrtabIndex = Asm.size();
1075
Matt Fleming3565a062010-08-16 18:57:57 +00001076 SymtabSection = Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1077 SectionKind::getReadOnly(),
1078 false, EntrySize);
Matt Fleming3565a062010-08-16 18:57:57 +00001079 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Matt Fleming3565a062010-08-16 18:57:57 +00001080 SymtabSD.setAlignment(Is64Bit ? 8 : 4);
1081
Matt Fleming3565a062010-08-16 18:57:57 +00001082 const MCSection *StrtabSection;
1083 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
1084 SectionKind::getReadOnly(), false);
Matt Fleming3565a062010-08-16 18:57:57 +00001085 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1086 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +00001087 StringTableIndex = Asm.size();
1088
Rafael Espindolac3c413f2010-09-27 22:04:54 +00001089 WriteRelocations(Asm, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001090
1091 // Symbol table
1092 F = new MCDataFragment(&SymtabSD);
1093 WriteSymbolTable(F, Asm, Layout, NumRegularSections);
Rafael Espindola70703872010-09-30 02:22:20 +00001094 Asm.AddSectionToTheEnd(*Writer, SymtabSD, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001095
Matt Fleming3565a062010-08-16 18:57:57 +00001096 F = new MCDataFragment(&StrtabSD);
1097 F->getContents().append(StringTable.begin(), StringTable.end());
Rafael Espindola70703872010-09-30 02:22:20 +00001098 Asm.AddSectionToTheEnd(*Writer, StrtabSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +00001099
Matt Fleming3565a062010-08-16 18:57:57 +00001100 F = new MCDataFragment(&ShstrtabSD);
1101
Matt Fleming3565a062010-08-16 18:57:57 +00001102 // Section header string table.
1103 //
1104 // The first entry of a string table holds a null character so skip
1105 // section 0.
1106 uint64_t Index = 1;
1107 F->getContents() += '\x00';
1108
1109 for (MCAssembler::const_iterator it = Asm.begin(),
1110 ie = Asm.end(); it != ie; ++it) {
Matt Fleming3565a062010-08-16 18:57:57 +00001111 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +00001112 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola51efe7a2010-09-23 14:14:56 +00001113 // FIXME: We could merge suffixes like in .text and .rela.text.
Matt Fleming3565a062010-08-16 18:57:57 +00001114
1115 // Remember the index into the string table so we can write it
1116 // into the sh_name field of the section header table.
1117 SectionStringTableIndex[&it->getSection()] = Index;
1118
1119 Index += Section.getSectionName().size() + 1;
1120 F->getContents() += Section.getSectionName();
1121 F->getContents() += '\x00';
1122 }
1123
Rafael Espindola70703872010-09-30 02:22:20 +00001124 Asm.AddSectionToTheEnd(*Writer, ShstrtabSD, Layout);
1125}
1126
1127bool ELFObjectWriterImpl::IsFixupFullyResolved(const MCAssembler &Asm,
1128 const MCValue Target,
1129 bool IsPCRel,
1130 const MCFragment *DF) const {
1131 // If this is a PCrel relocation, find the section this fixup value is
1132 // relative to.
1133 const MCSection *BaseSection = 0;
1134 if (IsPCRel) {
1135 BaseSection = &DF->getParent()->getSection();
1136 assert(BaseSection);
1137 }
1138
1139 const MCSection *SectionA = 0;
1140 const MCSymbol *SymbolA = 0;
1141 if (const MCSymbolRefExpr *A = Target.getSymA()) {
1142 SymbolA = &A->getSymbol();
1143 SectionA = &SymbolA->getSection();
1144 }
1145
1146 const MCSection *SectionB = 0;
1147 if (const MCSymbolRefExpr *B = Target.getSymB()) {
1148 SectionB = &B->getSymbol().getSection();
1149 }
1150
1151 if (!BaseSection)
1152 return SectionA == SectionB;
1153
1154 const MCSymbolData &DataA = Asm.getSymbolData(*SymbolA);
1155 if (DataA.isExternal())
1156 return false;
1157
1158 return !SectionB && BaseSection == SectionA;
Matt Fleming3565a062010-08-16 18:57:57 +00001159}
1160
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001161void ELFObjectWriterImpl::WriteObject(MCAssembler &Asm,
Matt Fleming3565a062010-08-16 18:57:57 +00001162 const MCAsmLayout &Layout) {
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001163 // Compute symbol table information.
1164 ComputeSymbolTable(Asm);
1165
Matt Fleming3565a062010-08-16 18:57:57 +00001166 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
1167 const_cast<MCAsmLayout&>(Layout));
1168
1169 // Add 1 for the null section.
1170 unsigned NumSections = Asm.size() + 1;
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001171 uint64_t NaturalAlignment = Is64Bit ? 8 : 4;
1172 uint64_t HeaderSize = Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr);
1173 uint64_t FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001174
1175 for (MCAssembler::const_iterator it = Asm.begin(),
1176 ie = Asm.end(); it != ie; ++it) {
1177 const MCSectionData &SD = *it;
Matt Fleming3565a062010-08-16 18:57:57 +00001178
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001179 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1180
Matt Fleming3565a062010-08-16 18:57:57 +00001181 // Get the size of the section in the output file (including padding).
1182 uint64_t Size = Layout.getSectionFileSize(&SD);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001183
1184 FileOff += Size;
Matt Fleming3565a062010-08-16 18:57:57 +00001185 }
1186
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001187 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1188
Matt Fleming3565a062010-08-16 18:57:57 +00001189 // Write out the ELF header ...
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001190 WriteHeader(FileOff - HeaderSize, NumSections);
1191
1192 FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001193
1194 // ... then all of the sections ...
1195 DenseMap<const MCSection*, uint64_t> SectionOffsetMap;
1196
Rafael Espindolad8e0bfe2010-10-06 22:28:19 +00001197 DenseMap<const MCSection*, uint32_t> SectionIndexMap;
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001198
1199 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +00001200 for (MCAssembler::const_iterator it = Asm.begin(),
1201 ie = Asm.end(); it != ie; ++it) {
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001202 const MCSectionData &SD = *it;
1203
1204 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1205 WriteZeros(Padding);
1206 FileOff += Padding;
1207
Matt Fleming3565a062010-08-16 18:57:57 +00001208 // Remember the offset into the file for this section.
1209 SectionOffsetMap[&it->getSection()] = FileOff;
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001210 SectionIndexMap[&it->getSection()] = Index++;
1211
Matt Fleming3565a062010-08-16 18:57:57 +00001212 FileOff += Layout.getSectionFileSize(&SD);
1213
1214 Asm.WriteSectionData(it, Layout, Writer);
1215 }
1216
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001217 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1218 WriteZeros(Padding);
1219 FileOff += Padding;
1220
Matt Fleming3565a062010-08-16 18:57:57 +00001221 // ... and then the section header table.
1222 // Should we align the section header table?
1223 //
1224 // Null section first.
1225 WriteSecHdrEntry(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1226
1227 for (MCAssembler::const_iterator it = Asm.begin(),
1228 ie = Asm.end(); it != ie; ++it) {
1229 const MCSectionData &SD = *it;
1230 const MCSectionELF &Section =
1231 static_cast<const MCSectionELF&>(SD.getSection());
1232
1233 uint64_t sh_link = 0;
1234 uint64_t sh_info = 0;
1235
1236 switch(Section.getType()) {
1237 case ELF::SHT_DYNAMIC:
1238 sh_link = SectionStringTableIndex[&it->getSection()];
1239 sh_info = 0;
1240 break;
1241
1242 case ELF::SHT_REL:
Eli Friedmanf8020a32010-08-16 19:15:06 +00001243 case ELF::SHT_RELA: {
Matt Fleming3565a062010-08-16 18:57:57 +00001244 const MCSection *SymtabSection;
1245 const MCSection *InfoSection;
Matt Fleming3565a062010-08-16 18:57:57 +00001246
1247 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1248 SectionKind::getReadOnly(),
Eli Friedmanf8020a32010-08-16 19:15:06 +00001249 false);
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001250 sh_link = SectionIndexMap[SymtabSection];
Matt Fleming3565a062010-08-16 18:57:57 +00001251
Benjamin Kramer377a5722010-08-17 17:30:07 +00001252 // Remove ".rel" and ".rela" prefixes.
1253 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1254 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
1255
Eli Friedmanf8020a32010-08-16 19:15:06 +00001256 InfoSection = Asm.getContext().getELFSection(SectionName,
Matt Fleming3565a062010-08-16 18:57:57 +00001257 ELF::SHT_PROGBITS, 0,
Eli Friedmanf8020a32010-08-16 19:15:06 +00001258 SectionKind::getReadOnly(),
1259 false);
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001260 sh_info = SectionIndexMap[InfoSection];
Matt Fleming3565a062010-08-16 18:57:57 +00001261 break;
Eli Friedmanf8020a32010-08-16 19:15:06 +00001262 }
Matt Fleming3565a062010-08-16 18:57:57 +00001263
1264 case ELF::SHT_SYMTAB:
1265 case ELF::SHT_DYNSYM:
1266 sh_link = StringTableIndex;
1267 sh_info = LastLocalSymbolIndex;
1268 break;
1269
1270 case ELF::SHT_PROGBITS:
1271 case ELF::SHT_STRTAB:
1272 case ELF::SHT_NOBITS:
Benjamin Kramer19dc7fa2010-08-31 17:03:33 +00001273 case ELF::SHT_NULL:
Rafael Espindolacecbc3d2010-10-25 17:50:35 +00001274 case ELF::SHT_ARM_ATTRIBUTES:
Matt Fleming3565a062010-08-16 18:57:57 +00001275 // Nothing to do.
1276 break;
1277
Matt Fleming3565a062010-08-16 18:57:57 +00001278 default:
1279 assert(0 && "FIXME: sh_type value not supported!");
1280 break;
1281 }
1282
1283 WriteSecHdrEntry(SectionStringTableIndex[&it->getSection()],
1284 Section.getType(), Section.getFlags(),
Rafael Espindola71859c62010-09-16 19:46:31 +00001285 0,
Matt Fleming3565a062010-08-16 18:57:57 +00001286 SectionOffsetMap.lookup(&SD.getSection()),
1287 Layout.getSectionSize(&SD), sh_link,
1288 sh_info, SD.getAlignment(),
1289 Section.getEntrySize());
1290 }
1291}
1292
1293ELFObjectWriter::ELFObjectWriter(raw_ostream &OS,
1294 bool Is64Bit,
Roman Divacky5baf79e2010-09-09 17:57:50 +00001295 Triple::OSType OSType,
Wesley Peckeecb8582010-10-22 15:52:49 +00001296 uint16_t EMachine,
Matt Fleming3565a062010-08-16 18:57:57 +00001297 bool IsLittleEndian,
1298 bool HasRelocationAddend)
1299 : MCObjectWriter(OS, IsLittleEndian)
1300{
Wesley Peckeecb8582010-10-22 15:52:49 +00001301 Impl = new ELFObjectWriterImpl(this, Is64Bit, EMachine,
1302 HasRelocationAddend, OSType);
Matt Fleming3565a062010-08-16 18:57:57 +00001303}
1304
1305ELFObjectWriter::~ELFObjectWriter() {
1306 delete (ELFObjectWriterImpl*) Impl;
1307}
1308
1309void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm) {
1310 ((ELFObjectWriterImpl*) Impl)->ExecutePostLayoutBinding(Asm);
1311}
1312
1313void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
1314 const MCAsmLayout &Layout,
1315 const MCFragment *Fragment,
1316 const MCFixup &Fixup, MCValue Target,
1317 uint64_t &FixedValue) {
1318 ((ELFObjectWriterImpl*) Impl)->RecordRelocation(Asm, Layout, Fragment, Fixup,
1319 Target, FixedValue);
1320}
1321
Rafael Espindola70703872010-09-30 02:22:20 +00001322bool ELFObjectWriter::IsFixupFullyResolved(const MCAssembler &Asm,
1323 const MCValue Target,
1324 bool IsPCRel,
1325 const MCFragment *DF) const {
1326 return ((ELFObjectWriterImpl*) Impl)->IsFixupFullyResolved(Asm, Target,
1327 IsPCRel, DF);
1328}
1329
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001330void ELFObjectWriter::WriteObject(MCAssembler &Asm,
Matt Fleming3565a062010-08-16 18:57:57 +00001331 const MCAsmLayout &Layout) {
1332 ((ELFObjectWriterImpl*) Impl)->WriteObject(Asm, Layout);
1333}