blob: 1e4484d91b8f6266d82d53923fc411231fb7455d [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) {
Rafael Espindola3cede2d2010-10-27 21:23:52 +000082 // FIXME: Can we use the VariantKind?
Rafael Espindola5c77c162010-10-05 15:48:37 +000083 switch (Type) {
84 default:
85 return false;
86 case ELF::R_X86_64_GOT32:
87 case ELF::R_X86_64_PLT32:
88 case ELF::R_X86_64_GOTPCREL:
Rafael Espindolabc82d8b2010-10-27 20:28:07 +000089 case ELF::R_X86_64_TPOFF32:
90 case ELF::R_X86_64_TLSGD:
91 case ELF::R_X86_64_GOTTPOFF:
Rafael Espindola3cede2d2010-10-27 21:23:52 +000092 case ELF::R_386_TLS_GD:
93 case ELF::R_386_TLS_LE_32:
94 case ELF::R_386_TLS_IE:
95 case ELF::R_386_TLS_LE:
Rafael Espindola5c77c162010-10-05 15:48:37 +000096 return true;
97 }
98}
99
Matt Fleming3565a062010-08-16 18:57:57 +0000100namespace {
101
102 class ELFObjectWriterImpl {
Chris Lattnerb188a372010-08-28 03:21:03 +0000103 /*static bool isFixupKindX86RIPRel(unsigned Kind) {
Matt Fleming3565a062010-08-16 18:57:57 +0000104 return Kind == X86::reloc_riprel_4byte ||
105 Kind == X86::reloc_riprel_4byte_movq_load;
Chris Lattnerb188a372010-08-28 03:21:03 +0000106 }*/
Matt Fleming3565a062010-08-16 18:57:57 +0000107
108
109 /// ELFSymbolData - Helper struct for containing some precomputed information
110 /// on symbols.
111 struct ELFSymbolData {
112 MCSymbolData *SymbolData;
113 uint64_t StringIndex;
114 uint32_t SectionIndex;
115
116 // Support lexicographic sorting.
117 bool operator<(const ELFSymbolData &RHS) const {
Rafael Espindolaad49cf52010-09-18 15:03:21 +0000118 if (GetType(*SymbolData) == ELF::STT_FILE)
119 return true;
120 if (GetType(*RHS.SymbolData) == ELF::STT_FILE)
121 return false;
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000122 return SymbolData->getSymbol().getName() <
123 RHS.SymbolData->getSymbol().getName();
Matt Fleming3565a062010-08-16 18:57:57 +0000124 }
125 };
126
127 /// @name Relocation Data
128 /// @{
129
130 struct ELFRelocationEntry {
131 // Make these big enough for both 32-bit and 64-bit
132 uint64_t r_offset;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000133 int Index;
134 unsigned Type;
135 const MCSymbol *Symbol;
Matt Fleming3565a062010-08-16 18:57:57 +0000136 uint64_t r_addend;
137
138 // Support lexicographic sorting.
139 bool operator<(const ELFRelocationEntry &RE) const {
140 return RE.r_offset < r_offset;
141 }
142 };
143
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000144 SmallPtrSet<const MCSymbol *, 16> UsedInReloc;
Rafael Espindola88182132010-10-27 15:18:17 +0000145 DenseMap<const MCSymbol *, const MCSymbol *> Renames;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000146
Matt Fleming3565a062010-08-16 18:57:57 +0000147 llvm::DenseMap<const MCSectionData*,
148 std::vector<ELFRelocationEntry> > Relocations;
149 DenseMap<const MCSection*, uint64_t> SectionStringTableIndex;
150
151 /// @}
152 /// @name Symbol Table Data
153 /// @{
154
155 SmallString<256> StringTable;
156 std::vector<ELFSymbolData> LocalSymbolData;
157 std::vector<ELFSymbolData> ExternalSymbolData;
158 std::vector<ELFSymbolData> UndefinedSymbolData;
159
160 /// @}
161
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000162 int NumRegularSections;
163
Rafael Espindola5c77c162010-10-05 15:48:37 +0000164 bool NeedsGOT;
165
Matt Fleming3565a062010-08-16 18:57:57 +0000166 ELFObjectWriter *Writer;
167
168 raw_ostream &OS;
169
Matt Fleming3565a062010-08-16 18:57:57 +0000170 unsigned Is64Bit : 1;
171
172 bool HasRelocationAddend;
173
Roman Divacky5baf79e2010-09-09 17:57:50 +0000174 Triple::OSType OSType;
175
Wesley Peckeecb8582010-10-22 15:52:49 +0000176 uint16_t EMachine;
177
Matt Fleming3565a062010-08-16 18:57:57 +0000178 // This holds the symbol table index of the last local symbol.
179 unsigned LastLocalSymbolIndex;
180 // This holds the .strtab section index.
181 unsigned StringTableIndex;
182
183 unsigned ShstrtabIndex;
184
185 public:
186 ELFObjectWriterImpl(ELFObjectWriter *_Writer, bool _Is64Bit,
Wesley Peckeecb8582010-10-22 15:52:49 +0000187 uint16_t _EMachine, bool _HasRelAddend,
188 Triple::OSType _OSType)
Rafael Espindola5c77c162010-10-05 15:48:37 +0000189 : NeedsGOT(false), Writer(_Writer), OS(Writer->getStream()),
Roman Divacky5baf79e2010-09-09 17:57:50 +0000190 Is64Bit(_Is64Bit), HasRelocationAddend(_HasRelAddend),
Wesley Peckeecb8582010-10-22 15:52:49 +0000191 OSType(_OSType), EMachine(_EMachine) {
Matt Fleming3565a062010-08-16 18:57:57 +0000192 }
193
194 void Write8(uint8_t Value) { Writer->Write8(Value); }
195 void Write16(uint16_t Value) { Writer->Write16(Value); }
196 void Write32(uint32_t Value) { Writer->Write32(Value); }
Chris Lattnerb188a372010-08-28 03:21:03 +0000197 //void Write64(uint64_t Value) { Writer->Write64(Value); }
Matt Fleming3565a062010-08-16 18:57:57 +0000198 void WriteZeros(unsigned N) { Writer->WriteZeros(N); }
Chris Lattnerb188a372010-08-28 03:21:03 +0000199 //void WriteBytes(StringRef Str, unsigned ZeroFillSize = 0) {
200 // Writer->WriteBytes(Str, ZeroFillSize);
201 //}
Matt Fleming3565a062010-08-16 18:57:57 +0000202
203 void WriteWord(uint64_t W) {
Chris Lattnerb188a372010-08-28 03:21:03 +0000204 if (Is64Bit)
Matt Fleming3565a062010-08-16 18:57:57 +0000205 Writer->Write64(W);
Chris Lattnerb188a372010-08-28 03:21:03 +0000206 else
Matt Fleming3565a062010-08-16 18:57:57 +0000207 Writer->Write32(W);
Matt Fleming3565a062010-08-16 18:57:57 +0000208 }
209
210 void String8(char *buf, uint8_t Value) {
211 buf[0] = Value;
212 }
213
214 void StringLE16(char *buf, uint16_t Value) {
215 buf[0] = char(Value >> 0);
216 buf[1] = char(Value >> 8);
217 }
218
219 void StringLE32(char *buf, uint32_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000220 StringLE16(buf, uint16_t(Value >> 0));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000221 StringLE16(buf + 2, uint16_t(Value >> 16));
Matt Fleming3565a062010-08-16 18:57:57 +0000222 }
223
224 void StringLE64(char *buf, uint64_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000225 StringLE32(buf, uint32_t(Value >> 0));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000226 StringLE32(buf + 4, uint32_t(Value >> 32));
Matt Fleming3565a062010-08-16 18:57:57 +0000227 }
228
229 void StringBE16(char *buf ,uint16_t Value) {
230 buf[0] = char(Value >> 8);
231 buf[1] = char(Value >> 0);
232 }
233
234 void StringBE32(char *buf, uint32_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000235 StringBE16(buf, uint16_t(Value >> 16));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000236 StringBE16(buf + 2, uint16_t(Value >> 0));
Matt Fleming3565a062010-08-16 18:57:57 +0000237 }
238
239 void StringBE64(char *buf, uint64_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000240 StringBE32(buf, uint32_t(Value >> 32));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000241 StringBE32(buf + 4, uint32_t(Value >> 0));
Matt Fleming3565a062010-08-16 18:57:57 +0000242 }
243
244 void String16(char *buf, uint16_t Value) {
245 if (Writer->isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000246 StringLE16(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000247 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000248 StringBE16(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000249 }
250
251 void String32(char *buf, uint32_t Value) {
252 if (Writer->isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000253 StringLE32(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000254 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000255 StringBE32(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000256 }
257
258 void String64(char *buf, uint64_t Value) {
259 if (Writer->isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000260 StringLE64(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000261 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000262 StringBE64(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000263 }
264
265 void WriteHeader(uint64_t SectionDataSize, unsigned NumberOfSections);
266
267 void WriteSymbolEntry(MCDataFragment *F, uint64_t name, uint8_t info,
268 uint64_t value, uint64_t size,
269 uint8_t other, uint16_t shndx);
270
271 void WriteSymbol(MCDataFragment *F, ELFSymbolData &MSD,
272 const MCAsmLayout &Layout);
273
274 void WriteSymbolTable(MCDataFragment *F, const MCAssembler &Asm,
Rafael Espindola71859c62010-09-16 19:46:31 +0000275 const MCAsmLayout &Layout,
276 unsigned NumRegularSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000277
278 void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
279 const MCFragment *Fragment, const MCFixup &Fixup,
280 MCValue Target, uint64_t &FixedValue);
281
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000282 uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm,
283 const MCSymbol *S);
Matt Fleming3565a062010-08-16 18:57:57 +0000284
285 /// ComputeSymbolTable - Compute the symbol table data
286 ///
287 /// \param StringTable [out] - The string table data.
288 /// \param StringIndexMap [out] - Map from symbol names to offsets in the
289 /// string table.
290 void ComputeSymbolTable(MCAssembler &Asm);
291
292 void WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
293 const MCSectionData &SD);
294
295 void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout) {
296 for (MCAssembler::const_iterator it = Asm.begin(),
297 ie = Asm.end(); it != ie; ++it) {
298 WriteRelocation(Asm, Layout, *it);
299 }
300 }
301
302 void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout);
303
Rafael Espindola88182132010-10-27 15:18:17 +0000304 void ExecutePostLayoutBinding(MCAssembler &Asm);
Matt Fleming3565a062010-08-16 18:57:57 +0000305
306 void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
307 uint64_t Address, uint64_t Offset,
308 uint64_t Size, uint32_t Link, uint32_t Info,
309 uint64_t Alignment, uint64_t EntrySize);
310
311 void WriteRelocationsFragment(const MCAssembler &Asm, MCDataFragment *F,
312 const MCSectionData *SD);
313
Rafael Espindola70703872010-09-30 02:22:20 +0000314 bool IsFixupFullyResolved(const MCAssembler &Asm,
315 const MCValue Target,
316 bool IsPCRel,
317 const MCFragment *DF) const;
318
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000319 void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000320 };
321
322}
323
324// Emit the ELF header.
325void ELFObjectWriterImpl::WriteHeader(uint64_t SectionDataSize,
326 unsigned NumberOfSections) {
327 // ELF Header
328 // ----------
329 //
330 // Note
331 // ----
332 // emitWord method behaves differently for ELF32 and ELF64, writing
333 // 4 bytes in the former and 8 in the latter.
334
335 Write8(0x7f); // e_ident[EI_MAG0]
336 Write8('E'); // e_ident[EI_MAG1]
337 Write8('L'); // e_ident[EI_MAG2]
338 Write8('F'); // e_ident[EI_MAG3]
339
340 Write8(Is64Bit ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
341
342 // e_ident[EI_DATA]
343 Write8(Writer->isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
344
345 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky5baf79e2010-09-09 17:57:50 +0000346 // e_ident[EI_OSABI]
347 switch (OSType) {
348 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
349 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
350 default: Write8(ELF::ELFOSABI_NONE); break;
351 }
Matt Fleming3565a062010-08-16 18:57:57 +0000352 Write8(0); // e_ident[EI_ABIVERSION]
353
354 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
355
356 Write16(ELF::ET_REL); // e_type
357
Wesley Peckeecb8582010-10-22 15:52:49 +0000358 Write16(EMachine); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +0000359
360 Write32(ELF::EV_CURRENT); // e_version
361 WriteWord(0); // e_entry, no entry point in .o file
362 WriteWord(0); // e_phoff, no program header for .o
Benjamin Kramereb976772010-08-17 17:02:29 +0000363 WriteWord(SectionDataSize + (Is64Bit ? sizeof(ELF::Elf64_Ehdr) :
364 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000365
366 // FIXME: Make this configurable.
367 Write32(0); // e_flags = whatever the target wants
368
369 // e_ehsize = ELF header size
370 Write16(Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
371
372 Write16(0); // e_phentsize = prog header entry size
373 Write16(0); // e_phnum = # prog header entries = 0
374
375 // e_shentsize = Section header entry size
376 Write16(Is64Bit ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
377
378 // e_shnum = # of section header ents
379 Write16(NumberOfSections);
380
381 // e_shstrndx = Section # of '.shstrtab'
382 Write16(ShstrtabIndex);
383}
384
385void ELFObjectWriterImpl::WriteSymbolEntry(MCDataFragment *F, uint64_t name,
386 uint8_t info, uint64_t value,
387 uint64_t size, uint8_t other,
388 uint16_t shndx) {
389 if (Is64Bit) {
390 char buf[8];
391
392 String32(buf, name);
393 F->getContents() += StringRef(buf, 4); // st_name
394
395 String8(buf, info);
396 F->getContents() += StringRef(buf, 1); // st_info
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000397
Matt Fleming3565a062010-08-16 18:57:57 +0000398 String8(buf, other);
399 F->getContents() += StringRef(buf, 1); // st_other
400
401 String16(buf, shndx);
402 F->getContents() += StringRef(buf, 2); // st_shndx
403
404 String64(buf, value);
405 F->getContents() += StringRef(buf, 8); // st_value
406
407 String64(buf, size);
408 F->getContents() += StringRef(buf, 8); // st_size
409 } else {
410 char buf[4];
411
412 String32(buf, name);
413 F->getContents() += StringRef(buf, 4); // st_name
414
415 String32(buf, value);
416 F->getContents() += StringRef(buf, 4); // st_value
417
418 String32(buf, size);
419 F->getContents() += StringRef(buf, 4); // st_size
420
421 String8(buf, info);
422 F->getContents() += StringRef(buf, 1); // st_info
423
424 String8(buf, other);
425 F->getContents() += StringRef(buf, 1); // st_other
426
427 String16(buf, shndx);
428 F->getContents() += StringRef(buf, 2); // st_shndx
429 }
430}
431
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000432static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout) {
433 if (Data.isCommon() && Data.isExternal())
434 return Data.getCommonAlignment();
435
436 const MCSymbol &Symbol = Data.getSymbol();
437 if (!Symbol.isInSection())
438 return 0;
439
440 if (!Data.isCommon() && !(Data.getFlags() & ELF_STB_Weak))
441 if (MCFragment *FF = Data.getFragment())
442 return Layout.getSymbolAddress(&Data) -
443 Layout.getSectionAddress(FF->getParent());
444
445 return 0;
446}
447
Rafael Espindolade89b012010-10-15 18:25:33 +0000448static const MCSymbol &AliasedSymbol(const MCSymbol &Symbol) {
449 const MCSymbol *S = &Symbol;
450 while (S->isVariable()) {
451 const MCExpr *Value = S->getVariableValue();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000452 if (Value->getKind() != MCExpr::SymbolRef)
453 return *S;
Rafael Espindolade89b012010-10-15 18:25:33 +0000454 const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr*>(Value);
455 S = &Ref->getSymbol();
456 }
457 return *S;
458}
459
Rafael Espindola88182132010-10-27 15:18:17 +0000460void ELFObjectWriterImpl::ExecutePostLayoutBinding(MCAssembler &Asm) {
461 // The presence of symbol versions causes undefined symbols and
462 // versions declared with @@@ to be renamed.
463
464 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
465 ie = Asm.symbol_end(); it != ie; ++it) {
466 const MCSymbol &Alias = it->getSymbol();
467 if (!Alias.isVariable())
468 continue;
469 const MCSymbol &Symbol = AliasedSymbol(Alias);
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000470 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000471 size_t Pos = AliasName.find('@');
472 if (Pos == StringRef::npos)
473 continue;
474
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000475 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000476 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
477 continue;
478
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000479 // FIXME: produce a better error message.
480 if (Symbol.isUndefined() && Rest.startswith("@@") &&
481 !Rest.startswith("@@@"))
482 report_fatal_error("A @@ version cannot be undefined");
483
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000484 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000485 }
486}
487
Matt Fleming3565a062010-08-16 18:57:57 +0000488void ELFObjectWriterImpl::WriteSymbol(MCDataFragment *F, ELFSymbolData &MSD,
489 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000490 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000491 MCSymbolData &Data =
492 Layout.getAssembler().getSymbolData(AliasedSymbol(OrigData.getSymbol()));
Rafael Espindola152c1062010-10-06 21:02:29 +0000493
494 uint8_t Binding = GetBinding(OrigData);
495 uint8_t Visibility = GetVisibility(OrigData);
496 uint8_t Type = GetType(Data);
497
498 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
499 uint8_t Other = Visibility;
500
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000501 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000502 uint64_t Size = 0;
503 const MCExpr *ESize;
504
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000505 assert(!(Data.isCommon() && !Data.isExternal()));
506
Matt Fleming3565a062010-08-16 18:57:57 +0000507 ESize = Data.getSize();
508 if (Data.getSize()) {
509 MCValue Res;
510 if (ESize->getKind() == MCExpr::Binary) {
511 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(ESize);
512
513 if (BE->EvaluateAsRelocatable(Res, &Layout)) {
Benjamin Kramer24f12062010-10-17 07:38:40 +0000514 assert(!Res.getSymA() || !Res.getSymA()->getSymbol().isDefined());
515 assert(!Res.getSymB() || !Res.getSymB()->getSymbol().isDefined());
Rafael Espindolaf230df92010-10-16 18:23:53 +0000516 Size = Res.getConstant();
Matt Fleming3565a062010-08-16 18:57:57 +0000517 }
518 } else if (ESize->getKind() == MCExpr::Constant) {
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000519 Size = static_cast<const MCConstantExpr *>(ESize)->getValue();
Matt Fleming3565a062010-08-16 18:57:57 +0000520 } else {
521 assert(0 && "Unsupported size expression");
522 }
523 }
524
525 // Write out the symbol table entry
526 WriteSymbolEntry(F, MSD.StringIndex, Info, Value,
527 Size, Other, MSD.SectionIndex);
528}
529
530void ELFObjectWriterImpl::WriteSymbolTable(MCDataFragment *F,
531 const MCAssembler &Asm,
Rafael Espindola71859c62010-09-16 19:46:31 +0000532 const MCAsmLayout &Layout,
533 unsigned NumRegularSections) {
Matt Fleming3565a062010-08-16 18:57:57 +0000534 // The string table must be emitted first because we need the index
535 // into the string table for all the symbol names.
536 assert(StringTable.size() && "Missing string table");
537
538 // FIXME: Make sure the start of the symbol table is aligned.
539
540 // The first entry is the undefined symbol entry.
541 unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000542 F->getContents().append(EntrySize, '\x00');
Matt Fleming3565a062010-08-16 18:57:57 +0000543
544 // Write the symbol table entries.
545 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
546 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
547 ELFSymbolData &MSD = LocalSymbolData[i];
548 WriteSymbol(F, MSD, Layout);
549 }
550
Rafael Espindola71859c62010-09-16 19:46:31 +0000551 // Write out a symbol table entry for each regular section.
Eli Friedmana44fa242010-08-16 21:17:09 +0000552 unsigned Index = 1;
Rafael Espindola71859c62010-09-16 19:46:31 +0000553 for (MCAssembler::const_iterator it = Asm.begin();
554 Index <= NumRegularSections; ++it, ++Index) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000555 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000556 static_cast<const MCSectionELF&>(it->getSection());
Eli Friedmana44fa242010-08-16 21:17:09 +0000557 // Leave out relocations so we don't have indexes within
558 // the relocations messed up
Benjamin Kramer377a5722010-08-17 17:30:07 +0000559 if (Section.getType() == ELF::SHT_RELA || Section.getType() == ELF::SHT_REL)
Eli Friedmana44fa242010-08-16 21:17:09 +0000560 continue;
Matt Fleming3565a062010-08-16 18:57:57 +0000561 WriteSymbolEntry(F, 0, ELF::STT_SECTION, 0, 0, ELF::STV_DEFAULT, Index);
Eli Friedmana44fa242010-08-16 21:17:09 +0000562 LastLocalSymbolIndex++;
563 }
Matt Fleming3565a062010-08-16 18:57:57 +0000564
565 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
566 ELFSymbolData &MSD = ExternalSymbolData[i];
567 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000568 assert(((Data.getFlags() & ELF_STB_Global) ||
569 (Data.getFlags() & ELF_STB_Weak)) &&
570 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Matt Fleming3565a062010-08-16 18:57:57 +0000571 WriteSymbol(F, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000572 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000573 LastLocalSymbolIndex++;
574 }
575
576 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
577 ELFSymbolData &MSD = UndefinedSymbolData[i];
578 MCSymbolData &Data = *MSD.SymbolData;
Matt Fleming3565a062010-08-16 18:57:57 +0000579 WriteSymbol(F, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000580 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000581 LastLocalSymbolIndex++;
582 }
583}
584
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000585static bool ShouldRelocOnSymbol(const MCSymbolData &SD,
Rafael Espindola3729d002010-10-05 23:57:26 +0000586 const MCValue &Target,
587 const MCFragment &F) {
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000588 const MCSymbol &Symbol = SD.getSymbol();
589 if (Symbol.isUndefined())
590 return true;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000591
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000592 const MCSectionELF &Section =
593 static_cast<const MCSectionELF&>(Symbol.getSection());
594
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000595 if (SD.isExternal())
596 return true;
597
Rafael Espindola8cecf252010-10-06 16:23:36 +0000598 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000599 const MCSectionELF &Sec2 =
600 static_cast<const MCSectionELF&>(F.getParent()->getSection());
601
Rafael Espindolace2d3c52010-10-18 20:25:33 +0000602 if (Section.getKind().isBSS())
603 return false;
604
Rafael Espindola8cecf252010-10-06 16:23:36 +0000605 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000606 (Kind == MCSymbolRefExpr::VK_PLT ||
607 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
608 Kind == MCSymbolRefExpr::VK_GOTOFF))
Rafael Espindola3729d002010-10-05 23:57:26 +0000609 return true;
610
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000611 if (Section.getFlags() & MCSectionELF::SHF_MERGE)
612 return Target.getConstant() != 0;
613
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000614 return false;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000615}
616
Benjamin Kramere5b57342010-08-17 18:20:28 +0000617// FIXME: this is currently X86/X86_64 only
Matt Fleming3565a062010-08-16 18:57:57 +0000618void ELFObjectWriterImpl::RecordRelocation(const MCAssembler &Asm,
619 const MCAsmLayout &Layout,
620 const MCFragment *Fragment,
621 const MCFixup &Fixup,
622 MCValue Target,
623 uint64_t &FixedValue) {
Matt Fleming3565a062010-08-16 18:57:57 +0000624 int64_t Addend = 0;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000625 int Index = 0;
Benjamin Kramer95c602a2010-08-27 10:38:39 +0000626 int64_t Value = Target.getConstant();
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000627 const MCSymbol *Symbol = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000628
Rafael Espindola00074892010-09-17 22:34:41 +0000629 bool IsPCRel = isFixupKindX86PCRel(Fixup.getKind());
Benjamin Kramer81cfb852010-08-17 19:45:05 +0000630 if (!Target.isAbsolute()) {
Rafael Espindolade89b012010-10-15 18:25:33 +0000631 Symbol = &AliasedSymbol(Target.getSymA()->getSymbol());
Rafael Espindola88182132010-10-27 15:18:17 +0000632 const MCSymbol *Renamed = Renames.lookup(Symbol);
633 if (Renamed)
634 Symbol = Renamed;
Matt Fleming3565a062010-08-16 18:57:57 +0000635 MCSymbolData &SD = Asm.getSymbolData(*Symbol);
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000636 MCFragment *F = SD.getFragment();
Matt Fleming3565a062010-08-16 18:57:57 +0000637
Rafael Espindola9d8b7552010-10-03 00:46:57 +0000638 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
639 const MCSymbol &SymbolB = RefB->getSymbol();
640 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
641 IsPCRel = true;
Rafael Espindola55fb1022010-10-04 15:59:01 +0000642 MCSectionData *Sec = Fragment->getParent();
643
644 // Offset of the symbol in the section
645 int64_t a = Layout.getSymbolAddress(&SDB) - Layout.getSectionAddress(Sec);
646
647 // Ofeset of the relocation in the section
648 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
649 Value += b - a;
Rafael Espindola9d8b7552010-10-03 00:46:57 +0000650 }
651
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000652 // Check that this case has already been fully resolved before we get
653 // here.
Rafael Espindola00074892010-09-17 22:34:41 +0000654 if (Symbol->isDefined() && !SD.isExternal() &&
655 IsPCRel &&
656 &Fragment->getParent()->getSection() == &Symbol->getSection()) {
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000657 llvm_unreachable("We don't need a relocation in this case.");
Rafael Espindola00074892010-09-17 22:34:41 +0000658 return;
659 }
660
Rafael Espindola3729d002010-10-05 23:57:26 +0000661 bool RelocOnSymbol = ShouldRelocOnSymbol(SD, Target, *Fragment);
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000662 if (!RelocOnSymbol) {
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000663 Index = F->getParent()->getOrdinal();
Rafael Espindolaa6489182010-09-24 21:19:03 +0000664
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000665 MCSectionData *FSD = F->getParent();
666 // Offset of the symbol in the section
667 Value += Layout.getSymbolAddress(&SD) - Layout.getSectionAddress(FSD);
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000668 } else {
669 UsedInReloc.insert(Symbol);
670 Index = -1;
671 }
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000672 Addend = Value;
673 // Compensate for the addend on i386.
674 if (Is64Bit)
675 Value = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000676 }
677
Benjamin Kramer95c602a2010-08-27 10:38:39 +0000678 FixedValue = Value;
679
Matt Fleming3565a062010-08-16 18:57:57 +0000680 // determine the type of the relocation
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000681
682 MCSymbolRefExpr::VariantKind Modifier = Target.getSymA()->getKind();
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000683 unsigned Type;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000684 if (Is64Bit) {
685 if (IsPCRel) {
Rafael Espindola92bf6682010-10-04 19:04:13 +0000686 switch (Modifier) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000687 default:
688 llvm_unreachable("Unimplemented");
Rafael Espindola92bf6682010-10-04 19:04:13 +0000689 case MCSymbolRefExpr::VK_None:
690 Type = ELF::R_X86_64_PC32;
691 break;
692 case MCSymbolRefExpr::VK_PLT:
693 Type = ELF::R_X86_64_PLT32;
694 break;
Rafael Espindola607d1f62010-10-04 19:51:39 +0000695 case llvm::MCSymbolRefExpr::VK_GOTPCREL:
696 Type = ELF::R_X86_64_GOTPCREL;
697 break;
Rafael Espindolabc82d8b2010-10-27 20:28:07 +0000698 case MCSymbolRefExpr::VK_GOTTPOFF:
699 Type = ELF::R_X86_64_GOTTPOFF;
700 break;
701 case MCSymbolRefExpr::VK_TLSGD:
702 Type = ELF::R_X86_64_TLSGD;
703 break;
Rafael Espindola92bf6682010-10-04 19:04:13 +0000704 }
Benjamin Kramere5b57342010-08-17 18:20:28 +0000705 } else {
706 switch ((unsigned)Fixup.getKind()) {
707 default: llvm_unreachable("invalid fixup kind!");
708 case FK_Data_8: Type = ELF::R_X86_64_64; break;
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000709 case X86::reloc_signed_4byte:
Benjamin Kramere5b57342010-08-17 18:20:28 +0000710 case X86::reloc_pcrel_4byte:
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000711 assert(isInt<32>(Target.getConstant()));
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000712 switch (Modifier) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000713 default:
714 llvm_unreachable("Unimplemented");
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000715 case MCSymbolRefExpr::VK_None:
716 Type = ELF::R_X86_64_32S;
717 break;
718 case MCSymbolRefExpr::VK_GOT:
719 Type = ELF::R_X86_64_GOT32;
720 break;
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000721 case MCSymbolRefExpr::VK_GOTPCREL:
Rafael Espindola8cecf252010-10-06 16:23:36 +0000722 Type = ELF::R_X86_64_GOTPCREL;
723 break;
Rafael Espindolabc82d8b2010-10-27 20:28:07 +0000724 case MCSymbolRefExpr::VK_TPOFF:
725 Type = ELF::R_X86_64_TPOFF32;
726 break;
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000727 }
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000728 break;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000729 case FK_Data_4:
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000730 Type = ELF::R_X86_64_32;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000731 break;
732 case FK_Data_2: Type = ELF::R_X86_64_16; break;
733 case X86::reloc_pcrel_1byte:
734 case FK_Data_1: Type = ELF::R_X86_64_8; break;
735 }
736 }
Matt Fleming3565a062010-08-16 18:57:57 +0000737 } else {
Benjamin Kramere5b57342010-08-17 18:20:28 +0000738 if (IsPCRel) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000739 switch (Modifier) {
740 default:
Rafael Espindola24dc9ec2010-10-18 19:33:01 +0000741 llvm_unreachable("Unimplemented");
742 case MCSymbolRefExpr::VK_None:
Rafael Espindola9baee3b2010-10-18 18:03:28 +0000743 Type = ELF::R_386_PC32;
Rafael Espindola9baee3b2010-10-18 18:03:28 +0000744 break;
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000745 case MCSymbolRefExpr::VK_PLT:
746 Type = ELF::R_386_PLT32;
747 break;
748 }
Benjamin Kramere5b57342010-08-17 18:20:28 +0000749 } else {
750 switch ((unsigned)Fixup.getKind()) {
751 default: llvm_unreachable("invalid fixup kind!");
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000752
Rafael Espindola24ba4f72010-10-24 17:35:42 +0000753 case X86::reloc_global_offset_table:
754 Type = ELF::R_386_GOTPC;
755 break;
756
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000757 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
758 // instead?
759 case X86::reloc_signed_4byte:
Benjamin Kramere5b57342010-08-17 18:20:28 +0000760 case X86::reloc_pcrel_4byte:
Rafael Espindolabd701182010-10-19 19:31:37 +0000761 case FK_Data_4:
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000762 switch (Modifier) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000763 default:
764 llvm_unreachable("Unimplemented");
Rafael Espindolabd701182010-10-19 19:31:37 +0000765 case MCSymbolRefExpr::VK_None:
Rafael Espindola24ba4f72010-10-24 17:35:42 +0000766 Type = ELF::R_386_32;
Rafael Espindolabd701182010-10-19 19:31:37 +0000767 break;
Rafael Espindolaeada3042010-10-18 20:47:21 +0000768 case MCSymbolRefExpr::VK_GOT:
769 Type = ELF::R_386_GOT32;
770 break;
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000771 case MCSymbolRefExpr::VK_GOTOFF:
772 Type = ELF::R_386_GOTOFF;
773 break;
Rafael Espindola3cede2d2010-10-27 21:23:52 +0000774 case MCSymbolRefExpr::VK_TLSGD:
775 Type = ELF::R_386_TLS_GD;
776 break;
777 case MCSymbolRefExpr::VK_TPOFF:
778 Type = ELF::R_386_TLS_LE_32;
779 break;
780 case MCSymbolRefExpr::VK_INDNTPOFF:
781 Type = ELF::R_386_TLS_IE;
782 break;
783 case MCSymbolRefExpr::VK_NTPOFF:
784 Type = ELF::R_386_TLS_LE;
785 break;
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000786 }
787 break;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000788 case FK_Data_2: Type = ELF::R_386_16; break;
789 case X86::reloc_pcrel_1byte:
790 case FK_Data_1: Type = ELF::R_386_8; break;
791 }
Matt Fleming3565a062010-08-16 18:57:57 +0000792 }
793 }
794
Rafael Espindola5c77c162010-10-05 15:48:37 +0000795 if (RelocNeedsGOT(Type))
796 NeedsGOT = true;
797
Benjamin Kramere5b57342010-08-17 18:20:28 +0000798 ELFRelocationEntry ERE;
799
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000800 ERE.Index = Index;
801 ERE.Type = Type;
802 ERE.Symbol = Symbol;
Matt Fleming3565a062010-08-16 18:57:57 +0000803
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000804 ERE.r_offset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
Benjamin Kramere5b57342010-08-17 18:20:28 +0000805
Matt Fleming3565a062010-08-16 18:57:57 +0000806 if (HasRelocationAddend)
807 ERE.r_addend = Addend;
Benjamin Kramer172d7d62010-08-17 00:33:24 +0000808 else
809 ERE.r_addend = 0; // Silence compiler warning.
Matt Fleming3565a062010-08-16 18:57:57 +0000810
811 Relocations[Fragment->getParent()].push_back(ERE);
812}
813
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000814uint64_t
815ELFObjectWriterImpl::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
816 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000817 MCSymbolData &SD = Asm.getSymbolData(*S);
Eli Friedmanf8020a32010-08-16 19:15:06 +0000818
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000819 // Local symbol.
820 if (!SD.isExternal() && !S->isUndefined())
821 return SD.getIndex() + /* empty symbol */ 1;
822
823 // External or undefined symbol.
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000824 return SD.getIndex() + NumRegularSections + /* empty symbol */ 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000825}
826
Rafael Espindola737cd212010-10-05 18:01:23 +0000827static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
Rafael Espindola88182132010-10-27 15:18:17 +0000828 bool Used, bool Renamed) {
Rafael Espindolabd701182010-10-19 19:31:37 +0000829 if (Used)
830 return true;
831
Rafael Espindola88182132010-10-27 15:18:17 +0000832 if (Renamed)
833 return false;
834
Rafael Espindola737cd212010-10-05 18:01:23 +0000835 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000836
837 const MCSymbol &A = AliasedSymbol(Symbol);
838 if (&A != &Symbol && A.isUndefined())
839 return false;
840
Rafael Espindola737cd212010-10-05 18:01:23 +0000841 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
842 return false;
843
Rafael Espindolabd701182010-10-19 19:31:37 +0000844 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000845 return false;
846
847 return true;
848}
849
850static bool isLocal(const MCSymbolData &Data) {
851 if (Data.isExternal())
852 return false;
853
854 const MCSymbol &Symbol = Data.getSymbol();
855 if (Symbol.isUndefined() && !Symbol.isVariable())
856 return false;
857
858 return true;
859}
860
Matt Fleming3565a062010-08-16 18:57:57 +0000861void ELFObjectWriterImpl::ComputeSymbolTable(MCAssembler &Asm) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000862 // FIXME: Is this the correct place to do this?
863 if (NeedsGOT) {
864 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
865 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
866 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
867 Data.setExternal(true);
868 }
869
Matt Fleming3565a062010-08-16 18:57:57 +0000870 // Build section lookup table.
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000871 NumRegularSections = Asm.size();
Rafael Espindolaf5c347d2010-10-05 21:20:07 +0000872 DenseMap<const MCSection*, uint32_t> SectionIndexMap;
Matt Fleming3565a062010-08-16 18:57:57 +0000873 unsigned Index = 1;
874 for (MCAssembler::iterator it = Asm.begin(),
875 ie = Asm.end(); it != ie; ++it, ++Index)
876 SectionIndexMap[&it->getSection()] = Index;
877
878 // Index 0 is always the empty string.
879 StringMap<uint64_t> StringIndexMap;
880 StringTable += '\x00';
881
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000882 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000883 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
884 ie = Asm.symbol_end(); it != ie; ++it) {
885 const MCSymbol &Symbol = it->getSymbol();
886
Rafael Espindola88182132010-10-27 15:18:17 +0000887 if (!isInSymtab(Asm, *it, UsedInReloc.count(&Symbol),
888 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000889 continue;
890
Matt Fleming3565a062010-08-16 18:57:57 +0000891 ELFSymbolData MSD;
892 MSD.SymbolData = it;
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000893 bool Local = isLocal(*it);
Rafael Espindola88182132010-10-27 15:18:17 +0000894 const MCSymbol &RefSymbol = AliasedSymbol(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000895
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000896 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000897 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000898 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000899 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000900 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000901 } else if (RefSymbol.isUndefined()) {
Matt Fleming3565a062010-08-16 18:57:57 +0000902 MSD.SectionIndex = ELF::SHN_UNDEF;
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000903 // FIXME: Undefined symbols are global, but this is the first place we
904 // are able to set it.
905 if (GetBinding(*it) == ELF::STB_LOCAL)
906 SetBinding(*it, ELF::STB_GLOBAL);
Matt Fleming3565a062010-08-16 18:57:57 +0000907 } else {
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000908 MSD.SectionIndex = SectionIndexMap.lookup(&RefSymbol.getSection());
Matt Fleming3565a062010-08-16 18:57:57 +0000909 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000910 }
911
Rafael Espindola88182132010-10-27 15:18:17 +0000912 // The @@@ in symbol version is replaced with @ in undefined symbols and
913 // @@ in defined ones.
914 StringRef Name = Symbol.getName();
915 size_t Pos = Name.find("@@@");
916 std::string FinalName;
917 if (Pos != StringRef::npos) {
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000918 StringRef Prefix = Name.substr(0, Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000919 unsigned n = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000920 StringRef Suffix = Name.substr(Pos + n);
Rafael Espindola88182132010-10-27 15:18:17 +0000921 FinalName = Prefix.str() + Suffix.str();
922 } else {
923 FinalName = Name.str();
924 }
925
926 uint64_t &Entry = StringIndexMap[FinalName];
Rafael Espindolaa6866962010-10-27 14:44:52 +0000927 if (!Entry) {
928 Entry = StringTable.size();
Rafael Espindola88182132010-10-27 15:18:17 +0000929 StringTable += FinalName;
Rafael Espindolaa6866962010-10-27 14:44:52 +0000930 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +0000931 }
Rafael Espindolaa6866962010-10-27 14:44:52 +0000932 MSD.StringIndex = Entry;
933 if (MSD.SectionIndex == ELF::SHN_UNDEF)
934 UndefinedSymbolData.push_back(MSD);
935 else if (Local)
936 LocalSymbolData.push_back(MSD);
937 else
938 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +0000939 }
940
941 // Symbols are required to be in lexicographic order.
942 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
943 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
944 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
945
946 // Set the symbol indices. Local symbols must come before all other
947 // symbols with non-local bindings.
948 Index = 0;
949 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
950 LocalSymbolData[i].SymbolData->setIndex(Index++);
951 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
952 ExternalSymbolData[i].SymbolData->setIndex(Index++);
953 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
954 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
955}
956
957void ELFObjectWriterImpl::WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
958 const MCSectionData &SD) {
959 if (!Relocations[&SD].empty()) {
960 MCContext &Ctx = Asm.getContext();
961 const MCSection *RelaSection;
962 const MCSectionELF &Section =
963 static_cast<const MCSectionELF&>(SD.getSection());
964
965 const StringRef SectionName = Section.getSectionName();
Benjamin Kramer377a5722010-08-17 17:30:07 +0000966 std::string RelaSectionName = HasRelocationAddend ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +0000967 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000968
969 unsigned EntrySize;
970 if (HasRelocationAddend)
971 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
972 else
973 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +0000974
Benjamin Kramer377a5722010-08-17 17:30:07 +0000975 RelaSection = Ctx.getELFSection(RelaSectionName, HasRelocationAddend ?
976 ELF::SHT_RELA : ELF::SHT_REL, 0,
Matt Fleming3565a062010-08-16 18:57:57 +0000977 SectionKind::getReadOnly(),
978 false, EntrySize);
979
980 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Benjamin Kramera9eadca2010-09-06 16:11:52 +0000981 RelaSD.setAlignment(Is64Bit ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +0000982
983 MCDataFragment *F = new MCDataFragment(&RelaSD);
984
985 WriteRelocationsFragment(Asm, F, &SD);
986
Rafael Espindola70703872010-09-30 02:22:20 +0000987 Asm.AddSectionToTheEnd(*Writer, RelaSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000988 }
989}
990
991void ELFObjectWriterImpl::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
992 uint64_t Flags, uint64_t Address,
993 uint64_t Offset, uint64_t Size,
994 uint32_t Link, uint32_t Info,
995 uint64_t Alignment,
996 uint64_t EntrySize) {
997 Write32(Name); // sh_name: index into string table
998 Write32(Type); // sh_type
999 WriteWord(Flags); // sh_flags
1000 WriteWord(Address); // sh_addr
1001 WriteWord(Offset); // sh_offset
1002 WriteWord(Size); // sh_size
1003 Write32(Link); // sh_link
1004 Write32(Info); // sh_info
1005 WriteWord(Alignment); // sh_addralign
1006 WriteWord(EntrySize); // sh_entsize
1007}
1008
1009void ELFObjectWriterImpl::WriteRelocationsFragment(const MCAssembler &Asm,
1010 MCDataFragment *F,
1011 const MCSectionData *SD) {
1012 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
1013 // sort by the r_offset just like gnu as does
1014 array_pod_sort(Relocs.begin(), Relocs.end());
1015
1016 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1017 ELFRelocationEntry entry = Relocs[e - i - 1];
1018
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001019 if (entry.Index < 0)
1020 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
1021 else
1022 entry.Index += LocalSymbolData.size() + 1;
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001023 if (Is64Bit) {
1024 char buf[8];
Matt Fleming3565a062010-08-16 18:57:57 +00001025
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001026 String64(buf, entry.r_offset);
1027 F->getContents() += StringRef(buf, 8);
1028
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001029 struct ELF::Elf64_Rela ERE64;
1030 ERE64.setSymbolAndType(entry.Index, entry.Type);
1031 String64(buf, ERE64.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001032 F->getContents() += StringRef(buf, 8);
1033
1034 if (HasRelocationAddend) {
1035 String64(buf, entry.r_addend);
1036 F->getContents() += StringRef(buf, 8);
1037 }
1038 } else {
1039 char buf[4];
1040
1041 String32(buf, entry.r_offset);
1042 F->getContents() += StringRef(buf, 4);
1043
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001044 struct ELF::Elf32_Rela ERE32;
1045 ERE32.setSymbolAndType(entry.Index, entry.Type);
1046 String32(buf, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001047 F->getContents() += StringRef(buf, 4);
1048
1049 if (HasRelocationAddend) {
1050 String32(buf, entry.r_addend);
1051 F->getContents() += StringRef(buf, 4);
1052 }
1053 }
Matt Fleming3565a062010-08-16 18:57:57 +00001054 }
1055}
1056
1057void ELFObjectWriterImpl::CreateMetadataSections(MCAssembler &Asm,
1058 MCAsmLayout &Layout) {
1059 MCContext &Ctx = Asm.getContext();
1060 MCDataFragment *F;
1061
Matt Fleming3565a062010-08-16 18:57:57 +00001062 const MCSection *SymtabSection;
1063 unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
1064
Rafael Espindola71859c62010-09-16 19:46:31 +00001065 unsigned NumRegularSections = Asm.size();
1066
Rafael Espindola38738bf2010-09-22 19:04:41 +00001067 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola71859c62010-09-16 19:46:31 +00001068 const MCSection *ShstrtabSection;
1069 ShstrtabSection = Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
1070 SectionKind::getReadOnly(), false);
1071 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1072 ShstrtabSD.setAlignment(1);
1073 ShstrtabIndex = Asm.size();
1074
Matt Fleming3565a062010-08-16 18:57:57 +00001075 SymtabSection = Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1076 SectionKind::getReadOnly(),
1077 false, EntrySize);
Matt Fleming3565a062010-08-16 18:57:57 +00001078 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Matt Fleming3565a062010-08-16 18:57:57 +00001079 SymtabSD.setAlignment(Is64Bit ? 8 : 4);
1080
Matt Fleming3565a062010-08-16 18:57:57 +00001081 const MCSection *StrtabSection;
1082 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
1083 SectionKind::getReadOnly(), false);
Matt Fleming3565a062010-08-16 18:57:57 +00001084 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1085 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +00001086 StringTableIndex = Asm.size();
1087
Rafael Espindolac3c413f2010-09-27 22:04:54 +00001088 WriteRelocations(Asm, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001089
1090 // Symbol table
1091 F = new MCDataFragment(&SymtabSD);
1092 WriteSymbolTable(F, Asm, Layout, NumRegularSections);
Rafael Espindola70703872010-09-30 02:22:20 +00001093 Asm.AddSectionToTheEnd(*Writer, SymtabSD, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001094
Matt Fleming3565a062010-08-16 18:57:57 +00001095 F = new MCDataFragment(&StrtabSD);
1096 F->getContents().append(StringTable.begin(), StringTable.end());
Rafael Espindola70703872010-09-30 02:22:20 +00001097 Asm.AddSectionToTheEnd(*Writer, StrtabSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +00001098
Matt Fleming3565a062010-08-16 18:57:57 +00001099 F = new MCDataFragment(&ShstrtabSD);
1100
Matt Fleming3565a062010-08-16 18:57:57 +00001101 // Section header string table.
1102 //
1103 // The first entry of a string table holds a null character so skip
1104 // section 0.
1105 uint64_t Index = 1;
1106 F->getContents() += '\x00';
1107
1108 for (MCAssembler::const_iterator it = Asm.begin(),
1109 ie = Asm.end(); it != ie; ++it) {
Matt Fleming3565a062010-08-16 18:57:57 +00001110 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +00001111 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola51efe7a2010-09-23 14:14:56 +00001112 // FIXME: We could merge suffixes like in .text and .rela.text.
Matt Fleming3565a062010-08-16 18:57:57 +00001113
1114 // Remember the index into the string table so we can write it
1115 // into the sh_name field of the section header table.
1116 SectionStringTableIndex[&it->getSection()] = Index;
1117
1118 Index += Section.getSectionName().size() + 1;
1119 F->getContents() += Section.getSectionName();
1120 F->getContents() += '\x00';
1121 }
1122
Rafael Espindola70703872010-09-30 02:22:20 +00001123 Asm.AddSectionToTheEnd(*Writer, ShstrtabSD, Layout);
1124}
1125
1126bool ELFObjectWriterImpl::IsFixupFullyResolved(const MCAssembler &Asm,
1127 const MCValue Target,
1128 bool IsPCRel,
1129 const MCFragment *DF) const {
1130 // If this is a PCrel relocation, find the section this fixup value is
1131 // relative to.
1132 const MCSection *BaseSection = 0;
1133 if (IsPCRel) {
1134 BaseSection = &DF->getParent()->getSection();
1135 assert(BaseSection);
1136 }
1137
1138 const MCSection *SectionA = 0;
1139 const MCSymbol *SymbolA = 0;
1140 if (const MCSymbolRefExpr *A = Target.getSymA()) {
1141 SymbolA = &A->getSymbol();
1142 SectionA = &SymbolA->getSection();
1143 }
1144
1145 const MCSection *SectionB = 0;
1146 if (const MCSymbolRefExpr *B = Target.getSymB()) {
1147 SectionB = &B->getSymbol().getSection();
1148 }
1149
1150 if (!BaseSection)
1151 return SectionA == SectionB;
1152
1153 const MCSymbolData &DataA = Asm.getSymbolData(*SymbolA);
1154 if (DataA.isExternal())
1155 return false;
1156
1157 return !SectionB && BaseSection == SectionA;
Matt Fleming3565a062010-08-16 18:57:57 +00001158}
1159
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001160void ELFObjectWriterImpl::WriteObject(MCAssembler &Asm,
Matt Fleming3565a062010-08-16 18:57:57 +00001161 const MCAsmLayout &Layout) {
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001162 // Compute symbol table information.
1163 ComputeSymbolTable(Asm);
1164
Matt Fleming3565a062010-08-16 18:57:57 +00001165 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
1166 const_cast<MCAsmLayout&>(Layout));
1167
1168 // Add 1 for the null section.
1169 unsigned NumSections = Asm.size() + 1;
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001170 uint64_t NaturalAlignment = Is64Bit ? 8 : 4;
1171 uint64_t HeaderSize = Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr);
1172 uint64_t FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001173
1174 for (MCAssembler::const_iterator it = Asm.begin(),
1175 ie = Asm.end(); it != ie; ++it) {
1176 const MCSectionData &SD = *it;
Matt Fleming3565a062010-08-16 18:57:57 +00001177
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001178 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1179
Matt Fleming3565a062010-08-16 18:57:57 +00001180 // Get the size of the section in the output file (including padding).
1181 uint64_t Size = Layout.getSectionFileSize(&SD);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001182
1183 FileOff += Size;
Matt Fleming3565a062010-08-16 18:57:57 +00001184 }
1185
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001186 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1187
Matt Fleming3565a062010-08-16 18:57:57 +00001188 // Write out the ELF header ...
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001189 WriteHeader(FileOff - HeaderSize, NumSections);
1190
1191 FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001192
1193 // ... then all of the sections ...
1194 DenseMap<const MCSection*, uint64_t> SectionOffsetMap;
1195
Rafael Espindolad8e0bfe2010-10-06 22:28:19 +00001196 DenseMap<const MCSection*, uint32_t> SectionIndexMap;
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001197
1198 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +00001199 for (MCAssembler::const_iterator it = Asm.begin(),
1200 ie = Asm.end(); it != ie; ++it) {
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001201 const MCSectionData &SD = *it;
1202
1203 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1204 WriteZeros(Padding);
1205 FileOff += Padding;
1206
Matt Fleming3565a062010-08-16 18:57:57 +00001207 // Remember the offset into the file for this section.
1208 SectionOffsetMap[&it->getSection()] = FileOff;
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001209 SectionIndexMap[&it->getSection()] = Index++;
1210
Matt Fleming3565a062010-08-16 18:57:57 +00001211 FileOff += Layout.getSectionFileSize(&SD);
1212
1213 Asm.WriteSectionData(it, Layout, Writer);
1214 }
1215
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001216 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1217 WriteZeros(Padding);
1218 FileOff += Padding;
1219
Matt Fleming3565a062010-08-16 18:57:57 +00001220 // ... and then the section header table.
1221 // Should we align the section header table?
1222 //
1223 // Null section first.
1224 WriteSecHdrEntry(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1225
1226 for (MCAssembler::const_iterator it = Asm.begin(),
1227 ie = Asm.end(); it != ie; ++it) {
1228 const MCSectionData &SD = *it;
1229 const MCSectionELF &Section =
1230 static_cast<const MCSectionELF&>(SD.getSection());
1231
1232 uint64_t sh_link = 0;
1233 uint64_t sh_info = 0;
1234
1235 switch(Section.getType()) {
1236 case ELF::SHT_DYNAMIC:
1237 sh_link = SectionStringTableIndex[&it->getSection()];
1238 sh_info = 0;
1239 break;
1240
1241 case ELF::SHT_REL:
Eli Friedmanf8020a32010-08-16 19:15:06 +00001242 case ELF::SHT_RELA: {
Matt Fleming3565a062010-08-16 18:57:57 +00001243 const MCSection *SymtabSection;
1244 const MCSection *InfoSection;
Matt Fleming3565a062010-08-16 18:57:57 +00001245
1246 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1247 SectionKind::getReadOnly(),
Eli Friedmanf8020a32010-08-16 19:15:06 +00001248 false);
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001249 sh_link = SectionIndexMap[SymtabSection];
Matt Fleming3565a062010-08-16 18:57:57 +00001250
Benjamin Kramer377a5722010-08-17 17:30:07 +00001251 // Remove ".rel" and ".rela" prefixes.
1252 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1253 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
1254
Eli Friedmanf8020a32010-08-16 19:15:06 +00001255 InfoSection = Asm.getContext().getELFSection(SectionName,
Matt Fleming3565a062010-08-16 18:57:57 +00001256 ELF::SHT_PROGBITS, 0,
Eli Friedmanf8020a32010-08-16 19:15:06 +00001257 SectionKind::getReadOnly(),
1258 false);
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001259 sh_info = SectionIndexMap[InfoSection];
Matt Fleming3565a062010-08-16 18:57:57 +00001260 break;
Eli Friedmanf8020a32010-08-16 19:15:06 +00001261 }
Matt Fleming3565a062010-08-16 18:57:57 +00001262
1263 case ELF::SHT_SYMTAB:
1264 case ELF::SHT_DYNSYM:
1265 sh_link = StringTableIndex;
1266 sh_info = LastLocalSymbolIndex;
1267 break;
1268
1269 case ELF::SHT_PROGBITS:
1270 case ELF::SHT_STRTAB:
1271 case ELF::SHT_NOBITS:
Benjamin Kramer19dc7fa2010-08-31 17:03:33 +00001272 case ELF::SHT_NULL:
Rafael Espindolacecbc3d2010-10-25 17:50:35 +00001273 case ELF::SHT_ARM_ATTRIBUTES:
Matt Fleming3565a062010-08-16 18:57:57 +00001274 // Nothing to do.
1275 break;
1276
Matt Fleming3565a062010-08-16 18:57:57 +00001277 default:
1278 assert(0 && "FIXME: sh_type value not supported!");
1279 break;
1280 }
1281
1282 WriteSecHdrEntry(SectionStringTableIndex[&it->getSection()],
1283 Section.getType(), Section.getFlags(),
Rafael Espindola71859c62010-09-16 19:46:31 +00001284 0,
Matt Fleming3565a062010-08-16 18:57:57 +00001285 SectionOffsetMap.lookup(&SD.getSection()),
1286 Layout.getSectionSize(&SD), sh_link,
1287 sh_info, SD.getAlignment(),
1288 Section.getEntrySize());
1289 }
1290}
1291
1292ELFObjectWriter::ELFObjectWriter(raw_ostream &OS,
1293 bool Is64Bit,
Roman Divacky5baf79e2010-09-09 17:57:50 +00001294 Triple::OSType OSType,
Wesley Peckeecb8582010-10-22 15:52:49 +00001295 uint16_t EMachine,
Matt Fleming3565a062010-08-16 18:57:57 +00001296 bool IsLittleEndian,
1297 bool HasRelocationAddend)
1298 : MCObjectWriter(OS, IsLittleEndian)
1299{
Wesley Peckeecb8582010-10-22 15:52:49 +00001300 Impl = new ELFObjectWriterImpl(this, Is64Bit, EMachine,
1301 HasRelocationAddend, OSType);
Matt Fleming3565a062010-08-16 18:57:57 +00001302}
1303
1304ELFObjectWriter::~ELFObjectWriter() {
1305 delete (ELFObjectWriterImpl*) Impl;
1306}
1307
1308void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm) {
1309 ((ELFObjectWriterImpl*) Impl)->ExecutePostLayoutBinding(Asm);
1310}
1311
1312void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
1313 const MCAsmLayout &Layout,
1314 const MCFragment *Fragment,
1315 const MCFixup &Fixup, MCValue Target,
1316 uint64_t &FixedValue) {
1317 ((ELFObjectWriterImpl*) Impl)->RecordRelocation(Asm, Layout, Fragment, Fixup,
1318 Target, FixedValue);
1319}
1320
Rafael Espindola70703872010-09-30 02:22:20 +00001321bool ELFObjectWriter::IsFixupFullyResolved(const MCAssembler &Asm,
1322 const MCValue Target,
1323 bool IsPCRel,
1324 const MCFragment *DF) const {
1325 return ((ELFObjectWriterImpl*) Impl)->IsFixupFullyResolved(Asm, Target,
1326 IsPCRel, DF);
1327}
1328
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001329void ELFObjectWriter::WriteObject(MCAssembler &Asm,
Matt Fleming3565a062010-08-16 18:57:57 +00001330 const MCAsmLayout &Layout) {
1331 ((ELFObjectWriterImpl*) Impl)->WriteObject(Asm, Layout);
1332}