blob: 22005022851a2b3fab9e9f80dfbeb8d55afca69e [file] [log] [blame]
Matt Fleming3565a062010-08-16 18:57:57 +00001//===- lib/MC/ELFObjectWriter.cpp - ELF File Writer -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements ELF object file writer information.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/MC/ELFObjectWriter.h"
Rafael Espindola8f413fa2010-10-05 15:11:03 +000015#include "llvm/ADT/SmallPtrSet.h"
Matt Fleming3565a062010-08-16 18:57:57 +000016#include "llvm/ADT/STLExtras.h"
17#include "llvm/ADT/StringMap.h"
18#include "llvm/ADT/Twine.h"
19#include "llvm/MC/MCAssembler.h"
20#include "llvm/MC/MCAsmLayout.h"
21#include "llvm/MC/MCContext.h"
22#include "llvm/MC/MCELFSymbolFlags.h"
23#include "llvm/MC/MCExpr.h"
24#include "llvm/MC/MCObjectWriter.h"
25#include "llvm/MC/MCSectionELF.h"
26#include "llvm/MC/MCSymbol.h"
27#include "llvm/MC/MCValue.h"
28#include "llvm/Support/Debug.h"
29#include "llvm/Support/ErrorHandling.h"
30#include "llvm/Support/ELF.h"
31#include "llvm/Target/TargetAsmBackend.h"
32
33#include "../Target/X86/X86FixupKinds.h"
34
35#include <vector>
36using namespace llvm;
37
Rafael Espindolaad49cf52010-09-18 15:03:21 +000038static unsigned GetType(const MCSymbolData &SD) {
39 uint32_t Type = (SD.getFlags() & (0xf << ELF_STT_Shift)) >> ELF_STT_Shift;
40 assert(Type == ELF::STT_NOTYPE || Type == ELF::STT_OBJECT ||
41 Type == ELF::STT_FUNC || Type == ELF::STT_SECTION ||
42 Type == ELF::STT_FILE || Type == ELF::STT_COMMON ||
43 Type == ELF::STT_TLS);
44 return Type;
45}
46
Rafael Espindolae15eb4e2010-09-23 19:55:14 +000047static unsigned GetBinding(const MCSymbolData &SD) {
48 uint32_t Binding = (SD.getFlags() & (0xf << ELF_STB_Shift)) >> ELF_STB_Shift;
49 assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
50 Binding == ELF::STB_WEAK);
51 return Binding;
52}
53
54static void SetBinding(MCSymbolData &SD, unsigned Binding) {
55 assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
56 Binding == ELF::STB_WEAK);
57 uint32_t OtherFlags = SD.getFlags() & ~(0xf << ELF_STB_Shift);
58 SD.setFlags(OtherFlags | (Binding << ELF_STB_Shift));
59}
60
Rafael Espindola152c1062010-10-06 21:02:29 +000061static unsigned GetVisibility(MCSymbolData &SD) {
62 unsigned Visibility =
63 (SD.getFlags() & (0xf << ELF_STV_Shift)) >> ELF_STV_Shift;
64 assert(Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_INTERNAL ||
65 Visibility == ELF::STV_HIDDEN || Visibility == ELF::STV_PROTECTED);
66 return Visibility;
67}
68
Rafael Espindolacebdc012010-10-04 19:46:28 +000069static bool isFixupKindX86PCRel(unsigned Kind) {
70 switch (Kind) {
71 default:
72 return false;
73 case X86::reloc_pcrel_1byte:
74 case X86::reloc_pcrel_4byte:
75 case X86::reloc_riprel_4byte:
76 case X86::reloc_riprel_4byte_movq_load:
77 return true;
78 }
79}
80
Rafael Espindolaa0a2f872010-10-28 14:22:44 +000081static bool RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant) {
82 switch (Variant) {
Rafael Espindola5c77c162010-10-05 15:48:37 +000083 default:
84 return false;
Rafael Espindolaa0a2f872010-10-28 14:22:44 +000085 case MCSymbolRefExpr::VK_GOT:
86 case MCSymbolRefExpr::VK_PLT:
87 case MCSymbolRefExpr::VK_GOTPCREL:
88 case MCSymbolRefExpr::VK_TPOFF:
89 case MCSymbolRefExpr::VK_TLSGD:
90 case MCSymbolRefExpr::VK_GOTTPOFF:
91 case MCSymbolRefExpr::VK_INDNTPOFF:
92 case MCSymbolRefExpr::VK_NTPOFF:
93 case MCSymbolRefExpr::VK_GOTNTPOFF:
Rafael Espindolaa264f722010-10-28 14:37:09 +000094 case MCSymbolRefExpr::VK_TLSLDM:
Rafael Espindola5c77c162010-10-05 15:48:37 +000095 return true;
96 }
97}
98
Matt Fleming3565a062010-08-16 18:57:57 +000099namespace {
100
101 class ELFObjectWriterImpl {
Chris Lattnerb188a372010-08-28 03:21:03 +0000102 /*static bool isFixupKindX86RIPRel(unsigned Kind) {
Matt Fleming3565a062010-08-16 18:57:57 +0000103 return Kind == X86::reloc_riprel_4byte ||
104 Kind == X86::reloc_riprel_4byte_movq_load;
Chris Lattnerb188a372010-08-28 03:21:03 +0000105 }*/
Matt Fleming3565a062010-08-16 18:57:57 +0000106
107
108 /// ELFSymbolData - Helper struct for containing some precomputed information
109 /// on symbols.
110 struct ELFSymbolData {
111 MCSymbolData *SymbolData;
112 uint64_t StringIndex;
113 uint32_t SectionIndex;
114
115 // Support lexicographic sorting.
116 bool operator<(const ELFSymbolData &RHS) const {
Rafael Espindolaad49cf52010-09-18 15:03:21 +0000117 if (GetType(*SymbolData) == ELF::STT_FILE)
118 return true;
119 if (GetType(*RHS.SymbolData) == ELF::STT_FILE)
120 return false;
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000121 return SymbolData->getSymbol().getName() <
122 RHS.SymbolData->getSymbol().getName();
Matt Fleming3565a062010-08-16 18:57:57 +0000123 }
124 };
125
126 /// @name Relocation Data
127 /// @{
128
129 struct ELFRelocationEntry {
130 // Make these big enough for both 32-bit and 64-bit
131 uint64_t r_offset;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000132 int Index;
133 unsigned Type;
134 const MCSymbol *Symbol;
Matt Fleming3565a062010-08-16 18:57:57 +0000135 uint64_t r_addend;
136
137 // Support lexicographic sorting.
138 bool operator<(const ELFRelocationEntry &RE) const {
139 return RE.r_offset < r_offset;
140 }
141 };
142
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000143 SmallPtrSet<const MCSymbol *, 16> UsedInReloc;
Rafael Espindola88182132010-10-27 15:18:17 +0000144 DenseMap<const MCSymbol *, const MCSymbol *> Renames;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000145
Matt Fleming3565a062010-08-16 18:57:57 +0000146 llvm::DenseMap<const MCSectionData*,
147 std::vector<ELFRelocationEntry> > Relocations;
148 DenseMap<const MCSection*, uint64_t> SectionStringTableIndex;
149
150 /// @}
151 /// @name Symbol Table Data
152 /// @{
153
154 SmallString<256> StringTable;
155 std::vector<ELFSymbolData> LocalSymbolData;
156 std::vector<ELFSymbolData> ExternalSymbolData;
157 std::vector<ELFSymbolData> UndefinedSymbolData;
158
159 /// @}
160
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000161 int NumRegularSections;
162
Rafael Espindola5c77c162010-10-05 15:48:37 +0000163 bool NeedsGOT;
164
Matt Fleming3565a062010-08-16 18:57:57 +0000165 ELFObjectWriter *Writer;
166
167 raw_ostream &OS;
168
Matt Fleming3565a062010-08-16 18:57:57 +0000169 unsigned Is64Bit : 1;
170
171 bool HasRelocationAddend;
172
Roman Divacky5baf79e2010-09-09 17:57:50 +0000173 Triple::OSType OSType;
174
Wesley Peckeecb8582010-10-22 15:52:49 +0000175 uint16_t EMachine;
176
Matt Fleming3565a062010-08-16 18:57:57 +0000177 // This holds the symbol table index of the last local symbol.
178 unsigned LastLocalSymbolIndex;
179 // This holds the .strtab section index.
180 unsigned StringTableIndex;
181
182 unsigned ShstrtabIndex;
183
184 public:
185 ELFObjectWriterImpl(ELFObjectWriter *_Writer, bool _Is64Bit,
Wesley Peckeecb8582010-10-22 15:52:49 +0000186 uint16_t _EMachine, bool _HasRelAddend,
187 Triple::OSType _OSType)
Rafael Espindola5c77c162010-10-05 15:48:37 +0000188 : NeedsGOT(false), Writer(_Writer), OS(Writer->getStream()),
Roman Divacky5baf79e2010-09-09 17:57:50 +0000189 Is64Bit(_Is64Bit), HasRelocationAddend(_HasRelAddend),
Wesley Peckeecb8582010-10-22 15:52:49 +0000190 OSType(_OSType), EMachine(_EMachine) {
Matt Fleming3565a062010-08-16 18:57:57 +0000191 }
192
193 void Write8(uint8_t Value) { Writer->Write8(Value); }
194 void Write16(uint16_t Value) { Writer->Write16(Value); }
195 void Write32(uint32_t Value) { Writer->Write32(Value); }
Chris Lattnerb188a372010-08-28 03:21:03 +0000196 //void Write64(uint64_t Value) { Writer->Write64(Value); }
Matt Fleming3565a062010-08-16 18:57:57 +0000197 void WriteZeros(unsigned N) { Writer->WriteZeros(N); }
Chris Lattnerb188a372010-08-28 03:21:03 +0000198 //void WriteBytes(StringRef Str, unsigned ZeroFillSize = 0) {
199 // Writer->WriteBytes(Str, ZeroFillSize);
200 //}
Matt Fleming3565a062010-08-16 18:57:57 +0000201
202 void WriteWord(uint64_t W) {
Chris Lattnerb188a372010-08-28 03:21:03 +0000203 if (Is64Bit)
Matt Fleming3565a062010-08-16 18:57:57 +0000204 Writer->Write64(W);
Chris Lattnerb188a372010-08-28 03:21:03 +0000205 else
Matt Fleming3565a062010-08-16 18:57:57 +0000206 Writer->Write32(W);
Matt Fleming3565a062010-08-16 18:57:57 +0000207 }
208
209 void String8(char *buf, uint8_t Value) {
210 buf[0] = Value;
211 }
212
213 void StringLE16(char *buf, uint16_t Value) {
214 buf[0] = char(Value >> 0);
215 buf[1] = char(Value >> 8);
216 }
217
218 void StringLE32(char *buf, uint32_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000219 StringLE16(buf, uint16_t(Value >> 0));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000220 StringLE16(buf + 2, uint16_t(Value >> 16));
Matt Fleming3565a062010-08-16 18:57:57 +0000221 }
222
223 void StringLE64(char *buf, uint64_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000224 StringLE32(buf, uint32_t(Value >> 0));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000225 StringLE32(buf + 4, uint32_t(Value >> 32));
Matt Fleming3565a062010-08-16 18:57:57 +0000226 }
227
228 void StringBE16(char *buf ,uint16_t Value) {
229 buf[0] = char(Value >> 8);
230 buf[1] = char(Value >> 0);
231 }
232
233 void StringBE32(char *buf, uint32_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000234 StringBE16(buf, uint16_t(Value >> 16));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000235 StringBE16(buf + 2, uint16_t(Value >> 0));
Matt Fleming3565a062010-08-16 18:57:57 +0000236 }
237
238 void StringBE64(char *buf, uint64_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000239 StringBE32(buf, uint32_t(Value >> 32));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000240 StringBE32(buf + 4, uint32_t(Value >> 0));
Matt Fleming3565a062010-08-16 18:57:57 +0000241 }
242
243 void String16(char *buf, uint16_t Value) {
244 if (Writer->isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000245 StringLE16(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000246 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000247 StringBE16(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000248 }
249
250 void String32(char *buf, uint32_t Value) {
251 if (Writer->isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000252 StringLE32(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000253 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000254 StringBE32(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000255 }
256
257 void String64(char *buf, uint64_t Value) {
258 if (Writer->isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000259 StringLE64(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000260 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000261 StringBE64(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000262 }
263
264 void WriteHeader(uint64_t SectionDataSize, unsigned NumberOfSections);
265
266 void WriteSymbolEntry(MCDataFragment *F, uint64_t name, uint8_t info,
267 uint64_t value, uint64_t size,
268 uint8_t other, uint16_t shndx);
269
270 void WriteSymbol(MCDataFragment *F, ELFSymbolData &MSD,
271 const MCAsmLayout &Layout);
272
273 void WriteSymbolTable(MCDataFragment *F, const MCAssembler &Asm,
Rafael Espindola71859c62010-09-16 19:46:31 +0000274 const MCAsmLayout &Layout,
275 unsigned NumRegularSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000276
277 void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
278 const MCFragment *Fragment, const MCFixup &Fixup,
279 MCValue Target, uint64_t &FixedValue);
280
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000281 uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm,
282 const MCSymbol *S);
Matt Fleming3565a062010-08-16 18:57:57 +0000283
284 /// ComputeSymbolTable - Compute the symbol table data
285 ///
286 /// \param StringTable [out] - The string table data.
287 /// \param StringIndexMap [out] - Map from symbol names to offsets in the
288 /// string table.
289 void ComputeSymbolTable(MCAssembler &Asm);
290
291 void WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
292 const MCSectionData &SD);
293
294 void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout) {
295 for (MCAssembler::const_iterator it = Asm.begin(),
296 ie = Asm.end(); it != ie; ++it) {
297 WriteRelocation(Asm, Layout, *it);
298 }
299 }
300
301 void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout);
302
Rafael Espindola88182132010-10-27 15:18:17 +0000303 void ExecutePostLayoutBinding(MCAssembler &Asm);
Matt Fleming3565a062010-08-16 18:57:57 +0000304
305 void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
306 uint64_t Address, uint64_t Offset,
307 uint64_t Size, uint32_t Link, uint32_t Info,
308 uint64_t Alignment, uint64_t EntrySize);
309
310 void WriteRelocationsFragment(const MCAssembler &Asm, MCDataFragment *F,
311 const MCSectionData *SD);
312
Rafael Espindola70703872010-09-30 02:22:20 +0000313 bool IsFixupFullyResolved(const MCAssembler &Asm,
314 const MCValue Target,
315 bool IsPCRel,
316 const MCFragment *DF) const;
317
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000318 void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000319 };
320
321}
322
323// Emit the ELF header.
324void ELFObjectWriterImpl::WriteHeader(uint64_t SectionDataSize,
325 unsigned NumberOfSections) {
326 // ELF Header
327 // ----------
328 //
329 // Note
330 // ----
331 // emitWord method behaves differently for ELF32 and ELF64, writing
332 // 4 bytes in the former and 8 in the latter.
333
334 Write8(0x7f); // e_ident[EI_MAG0]
335 Write8('E'); // e_ident[EI_MAG1]
336 Write8('L'); // e_ident[EI_MAG2]
337 Write8('F'); // e_ident[EI_MAG3]
338
339 Write8(Is64Bit ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
340
341 // e_ident[EI_DATA]
342 Write8(Writer->isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
343
344 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky5baf79e2010-09-09 17:57:50 +0000345 // e_ident[EI_OSABI]
346 switch (OSType) {
347 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
348 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
349 default: Write8(ELF::ELFOSABI_NONE); break;
350 }
Matt Fleming3565a062010-08-16 18:57:57 +0000351 Write8(0); // e_ident[EI_ABIVERSION]
352
353 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
354
355 Write16(ELF::ET_REL); // e_type
356
Wesley Peckeecb8582010-10-22 15:52:49 +0000357 Write16(EMachine); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +0000358
359 Write32(ELF::EV_CURRENT); // e_version
360 WriteWord(0); // e_entry, no entry point in .o file
361 WriteWord(0); // e_phoff, no program header for .o
Benjamin Kramereb976772010-08-17 17:02:29 +0000362 WriteWord(SectionDataSize + (Is64Bit ? sizeof(ELF::Elf64_Ehdr) :
363 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000364
365 // FIXME: Make this configurable.
366 Write32(0); // e_flags = whatever the target wants
367
368 // e_ehsize = ELF header size
369 Write16(Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
370
371 Write16(0); // e_phentsize = prog header entry size
372 Write16(0); // e_phnum = # prog header entries = 0
373
374 // e_shentsize = Section header entry size
375 Write16(Is64Bit ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
376
377 // e_shnum = # of section header ents
378 Write16(NumberOfSections);
379
380 // e_shstrndx = Section # of '.shstrtab'
381 Write16(ShstrtabIndex);
382}
383
384void ELFObjectWriterImpl::WriteSymbolEntry(MCDataFragment *F, uint64_t name,
385 uint8_t info, uint64_t value,
386 uint64_t size, uint8_t other,
387 uint16_t shndx) {
388 if (Is64Bit) {
389 char buf[8];
390
391 String32(buf, name);
392 F->getContents() += StringRef(buf, 4); // st_name
393
394 String8(buf, info);
395 F->getContents() += StringRef(buf, 1); // st_info
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000396
Matt Fleming3565a062010-08-16 18:57:57 +0000397 String8(buf, other);
398 F->getContents() += StringRef(buf, 1); // st_other
399
400 String16(buf, shndx);
401 F->getContents() += StringRef(buf, 2); // st_shndx
402
403 String64(buf, value);
404 F->getContents() += StringRef(buf, 8); // st_value
405
406 String64(buf, size);
407 F->getContents() += StringRef(buf, 8); // st_size
408 } else {
409 char buf[4];
410
411 String32(buf, name);
412 F->getContents() += StringRef(buf, 4); // st_name
413
414 String32(buf, value);
415 F->getContents() += StringRef(buf, 4); // st_value
416
417 String32(buf, size);
418 F->getContents() += StringRef(buf, 4); // st_size
419
420 String8(buf, info);
421 F->getContents() += StringRef(buf, 1); // st_info
422
423 String8(buf, other);
424 F->getContents() += StringRef(buf, 1); // st_other
425
426 String16(buf, shndx);
427 F->getContents() += StringRef(buf, 2); // st_shndx
428 }
429}
430
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000431static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout) {
432 if (Data.isCommon() && Data.isExternal())
433 return Data.getCommonAlignment();
434
435 const MCSymbol &Symbol = Data.getSymbol();
436 if (!Symbol.isInSection())
437 return 0;
438
439 if (!Data.isCommon() && !(Data.getFlags() & ELF_STB_Weak))
440 if (MCFragment *FF = Data.getFragment())
441 return Layout.getSymbolAddress(&Data) -
442 Layout.getSectionAddress(FF->getParent());
443
444 return 0;
445}
446
Rafael Espindolade89b012010-10-15 18:25:33 +0000447static const MCSymbol &AliasedSymbol(const MCSymbol &Symbol) {
448 const MCSymbol *S = &Symbol;
449 while (S->isVariable()) {
450 const MCExpr *Value = S->getVariableValue();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000451 if (Value->getKind() != MCExpr::SymbolRef)
452 return *S;
Rafael Espindolade89b012010-10-15 18:25:33 +0000453 const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr*>(Value);
454 S = &Ref->getSymbol();
455 }
456 return *S;
457}
458
Rafael Espindola88182132010-10-27 15:18:17 +0000459void ELFObjectWriterImpl::ExecutePostLayoutBinding(MCAssembler &Asm) {
460 // The presence of symbol versions causes undefined symbols and
461 // versions declared with @@@ to be renamed.
462
463 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
464 ie = Asm.symbol_end(); it != ie; ++it) {
465 const MCSymbol &Alias = it->getSymbol();
466 if (!Alias.isVariable())
467 continue;
468 const MCSymbol &Symbol = AliasedSymbol(Alias);
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000469 StringRef AliasName = Alias.getName();
Rafael Espindola88182132010-10-27 15:18:17 +0000470 size_t Pos = AliasName.find('@');
471 if (Pos == StringRef::npos)
472 continue;
473
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000474 StringRef Rest = AliasName.substr(Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000475 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
476 continue;
477
Rafael Espindola83ff4d22010-10-27 17:56:18 +0000478 // FIXME: produce a better error message.
479 if (Symbol.isUndefined() && Rest.startswith("@@") &&
480 !Rest.startswith("@@@"))
481 report_fatal_error("A @@ version cannot be undefined");
482
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000483 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindola88182132010-10-27 15:18:17 +0000484 }
485}
486
Matt Fleming3565a062010-08-16 18:57:57 +0000487void ELFObjectWriterImpl::WriteSymbol(MCDataFragment *F, ELFSymbolData &MSD,
488 const MCAsmLayout &Layout) {
Rafael Espindola152c1062010-10-06 21:02:29 +0000489 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindolade89b012010-10-15 18:25:33 +0000490 MCSymbolData &Data =
491 Layout.getAssembler().getSymbolData(AliasedSymbol(OrigData.getSymbol()));
Rafael Espindola152c1062010-10-06 21:02:29 +0000492
493 uint8_t Binding = GetBinding(OrigData);
494 uint8_t Visibility = GetVisibility(OrigData);
495 uint8_t Type = GetType(Data);
496
497 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
498 uint8_t Other = Visibility;
499
Rafael Espindola2c6ec312010-09-27 21:23:02 +0000500 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000501 uint64_t Size = 0;
502 const MCExpr *ESize;
503
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000504 assert(!(Data.isCommon() && !Data.isExternal()));
505
Matt Fleming3565a062010-08-16 18:57:57 +0000506 ESize = Data.getSize();
507 if (Data.getSize()) {
508 MCValue Res;
509 if (ESize->getKind() == MCExpr::Binary) {
510 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(ESize);
511
512 if (BE->EvaluateAsRelocatable(Res, &Layout)) {
Benjamin Kramer24f12062010-10-17 07:38:40 +0000513 assert(!Res.getSymA() || !Res.getSymA()->getSymbol().isDefined());
514 assert(!Res.getSymB() || !Res.getSymB()->getSymbol().isDefined());
Rafael Espindolaf230df92010-10-16 18:23:53 +0000515 Size = Res.getConstant();
Matt Fleming3565a062010-08-16 18:57:57 +0000516 }
517 } else if (ESize->getKind() == MCExpr::Constant) {
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000518 Size = static_cast<const MCConstantExpr *>(ESize)->getValue();
Matt Fleming3565a062010-08-16 18:57:57 +0000519 } else {
520 assert(0 && "Unsupported size expression");
521 }
522 }
523
524 // Write out the symbol table entry
525 WriteSymbolEntry(F, MSD.StringIndex, Info, Value,
526 Size, Other, MSD.SectionIndex);
527}
528
529void ELFObjectWriterImpl::WriteSymbolTable(MCDataFragment *F,
530 const MCAssembler &Asm,
Rafael Espindola71859c62010-09-16 19:46:31 +0000531 const MCAsmLayout &Layout,
532 unsigned NumRegularSections) {
Matt Fleming3565a062010-08-16 18:57:57 +0000533 // The string table must be emitted first because we need the index
534 // into the string table for all the symbol names.
535 assert(StringTable.size() && "Missing string table");
536
537 // FIXME: Make sure the start of the symbol table is aligned.
538
539 // The first entry is the undefined symbol entry.
540 unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000541 F->getContents().append(EntrySize, '\x00');
Matt Fleming3565a062010-08-16 18:57:57 +0000542
543 // Write the symbol table entries.
544 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
545 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
546 ELFSymbolData &MSD = LocalSymbolData[i];
547 WriteSymbol(F, MSD, Layout);
548 }
549
Rafael Espindola71859c62010-09-16 19:46:31 +0000550 // Write out a symbol table entry for each regular section.
Eli Friedmana44fa242010-08-16 21:17:09 +0000551 unsigned Index = 1;
Rafael Espindola71859c62010-09-16 19:46:31 +0000552 for (MCAssembler::const_iterator it = Asm.begin();
553 Index <= NumRegularSections; ++it, ++Index) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000554 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000555 static_cast<const MCSectionELF&>(it->getSection());
Eli Friedmana44fa242010-08-16 21:17:09 +0000556 // Leave out relocations so we don't have indexes within
557 // the relocations messed up
Benjamin Kramer377a5722010-08-17 17:30:07 +0000558 if (Section.getType() == ELF::SHT_RELA || Section.getType() == ELF::SHT_REL)
Eli Friedmana44fa242010-08-16 21:17:09 +0000559 continue;
Matt Fleming3565a062010-08-16 18:57:57 +0000560 WriteSymbolEntry(F, 0, ELF::STT_SECTION, 0, 0, ELF::STV_DEFAULT, Index);
Eli Friedmana44fa242010-08-16 21:17:09 +0000561 LastLocalSymbolIndex++;
562 }
Matt Fleming3565a062010-08-16 18:57:57 +0000563
564 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
565 ELFSymbolData &MSD = ExternalSymbolData[i];
566 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3223f192010-10-06 16:47:31 +0000567 assert(((Data.getFlags() & ELF_STB_Global) ||
568 (Data.getFlags() & ELF_STB_Weak)) &&
569 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Matt Fleming3565a062010-08-16 18:57:57 +0000570 WriteSymbol(F, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000571 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000572 LastLocalSymbolIndex++;
573 }
574
575 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
576 ELFSymbolData &MSD = UndefinedSymbolData[i];
577 MCSymbolData &Data = *MSD.SymbolData;
Matt Fleming3565a062010-08-16 18:57:57 +0000578 WriteSymbol(F, MSD, Layout);
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000579 if (GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming3565a062010-08-16 18:57:57 +0000580 LastLocalSymbolIndex++;
581 }
582}
583
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000584static bool ShouldRelocOnSymbol(const MCSymbolData &SD,
Rafael Espindola3729d002010-10-05 23:57:26 +0000585 const MCValue &Target,
586 const MCFragment &F) {
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000587 const MCSymbol &Symbol = SD.getSymbol();
588 if (Symbol.isUndefined())
589 return true;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000590
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000591 const MCSectionELF &Section =
592 static_cast<const MCSectionELF&>(Symbol.getSection());
593
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000594 if (SD.isExternal())
595 return true;
596
Rafael Espindola8cecf252010-10-06 16:23:36 +0000597 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindola3729d002010-10-05 23:57:26 +0000598 const MCSectionELF &Sec2 =
599 static_cast<const MCSectionELF&>(F.getParent()->getSection());
600
Rafael Espindolace2d3c52010-10-18 20:25:33 +0000601 if (Section.getKind().isBSS())
602 return false;
603
Rafael Espindola8cecf252010-10-06 16:23:36 +0000604 if (&Sec2 != &Section &&
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000605 (Kind == MCSymbolRefExpr::VK_PLT ||
606 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
607 Kind == MCSymbolRefExpr::VK_GOTOFF))
Rafael Espindola3729d002010-10-05 23:57:26 +0000608 return true;
609
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000610 if (Section.getFlags() & MCSectionELF::SHF_MERGE)
611 return Target.getConstant() != 0;
612
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000613 return false;
Rafael Espindola73ffea42010-09-25 05:42:19 +0000614}
615
Benjamin Kramere5b57342010-08-17 18:20:28 +0000616// FIXME: this is currently X86/X86_64 only
Matt Fleming3565a062010-08-16 18:57:57 +0000617void ELFObjectWriterImpl::RecordRelocation(const MCAssembler &Asm,
618 const MCAsmLayout &Layout,
619 const MCFragment *Fragment,
620 const MCFixup &Fixup,
621 MCValue Target,
622 uint64_t &FixedValue) {
Matt Fleming3565a062010-08-16 18:57:57 +0000623 int64_t Addend = 0;
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000624 int Index = 0;
Benjamin Kramer95c602a2010-08-27 10:38:39 +0000625 int64_t Value = Target.getConstant();
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000626 const MCSymbol *Symbol = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000627
Rafael Espindola00074892010-09-17 22:34:41 +0000628 bool IsPCRel = isFixupKindX86PCRel(Fixup.getKind());
Benjamin Kramer81cfb852010-08-17 19:45:05 +0000629 if (!Target.isAbsolute()) {
Rafael Espindolade89b012010-10-15 18:25:33 +0000630 Symbol = &AliasedSymbol(Target.getSymA()->getSymbol());
Rafael Espindola88182132010-10-27 15:18:17 +0000631 const MCSymbol *Renamed = Renames.lookup(Symbol);
632 if (Renamed)
633 Symbol = Renamed;
Matt Fleming3565a062010-08-16 18:57:57 +0000634 MCSymbolData &SD = Asm.getSymbolData(*Symbol);
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000635 MCFragment *F = SD.getFragment();
Matt Fleming3565a062010-08-16 18:57:57 +0000636
Rafael Espindola9d8b7552010-10-03 00:46:57 +0000637 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
638 const MCSymbol &SymbolB = RefB->getSymbol();
639 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
640 IsPCRel = true;
Rafael Espindola55fb1022010-10-04 15:59:01 +0000641 MCSectionData *Sec = Fragment->getParent();
642
643 // Offset of the symbol in the section
644 int64_t a = Layout.getSymbolAddress(&SDB) - Layout.getSectionAddress(Sec);
645
646 // Ofeset of the relocation in the section
647 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
648 Value += b - a;
Rafael Espindola9d8b7552010-10-03 00:46:57 +0000649 }
650
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000651 // Check that this case has already been fully resolved before we get
652 // here.
Rafael Espindola00074892010-09-17 22:34:41 +0000653 if (Symbol->isDefined() && !SD.isExternal() &&
654 IsPCRel &&
655 &Fragment->getParent()->getSection() == &Symbol->getSection()) {
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000656 llvm_unreachable("We don't need a relocation in this case.");
Rafael Espindola00074892010-09-17 22:34:41 +0000657 return;
658 }
659
Rafael Espindola3729d002010-10-05 23:57:26 +0000660 bool RelocOnSymbol = ShouldRelocOnSymbol(SD, Target, *Fragment);
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000661 if (!RelocOnSymbol) {
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000662 Index = F->getParent()->getOrdinal();
Rafael Espindolaa6489182010-09-24 21:19:03 +0000663
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000664 MCSectionData *FSD = F->getParent();
665 // Offset of the symbol in the section
666 Value += Layout.getSymbolAddress(&SD) - Layout.getSectionAddress(FSD);
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000667 } else {
668 UsedInReloc.insert(Symbol);
669 Index = -1;
670 }
Rafael Espindola7eae36b2010-09-30 20:18:35 +0000671 Addend = Value;
672 // Compensate for the addend on i386.
673 if (Is64Bit)
674 Value = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000675 }
676
Benjamin Kramer95c602a2010-08-27 10:38:39 +0000677 FixedValue = Value;
678
Matt Fleming3565a062010-08-16 18:57:57 +0000679 // determine the type of the relocation
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000680
681 MCSymbolRefExpr::VariantKind Modifier = Target.getSymA()->getKind();
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000682 unsigned Type;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000683 if (Is64Bit) {
684 if (IsPCRel) {
Rafael Espindola92bf6682010-10-04 19:04:13 +0000685 switch (Modifier) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000686 default:
687 llvm_unreachable("Unimplemented");
Rafael Espindola92bf6682010-10-04 19:04:13 +0000688 case MCSymbolRefExpr::VK_None:
689 Type = ELF::R_X86_64_PC32;
690 break;
691 case MCSymbolRefExpr::VK_PLT:
692 Type = ELF::R_X86_64_PLT32;
693 break;
Rafael Espindolaa0a2f872010-10-28 14:22:44 +0000694 case MCSymbolRefExpr::VK_GOTPCREL:
Rafael Espindola607d1f62010-10-04 19:51:39 +0000695 Type = ELF::R_X86_64_GOTPCREL;
696 break;
Rafael Espindolabc82d8b2010-10-27 20:28:07 +0000697 case MCSymbolRefExpr::VK_GOTTPOFF:
698 Type = ELF::R_X86_64_GOTTPOFF;
699 break;
700 case MCSymbolRefExpr::VK_TLSGD:
701 Type = ELF::R_X86_64_TLSGD;
702 break;
Rafael Espindola92bf6682010-10-04 19:04:13 +0000703 }
Benjamin Kramere5b57342010-08-17 18:20:28 +0000704 } else {
705 switch ((unsigned)Fixup.getKind()) {
706 default: llvm_unreachable("invalid fixup kind!");
707 case FK_Data_8: Type = ELF::R_X86_64_64; break;
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000708 case X86::reloc_signed_4byte:
Benjamin Kramere5b57342010-08-17 18:20:28 +0000709 case X86::reloc_pcrel_4byte:
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000710 assert(isInt<32>(Target.getConstant()));
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000711 switch (Modifier) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000712 default:
713 llvm_unreachable("Unimplemented");
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000714 case MCSymbolRefExpr::VK_None:
715 Type = ELF::R_X86_64_32S;
716 break;
717 case MCSymbolRefExpr::VK_GOT:
718 Type = ELF::R_X86_64_GOT32;
719 break;
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000720 case MCSymbolRefExpr::VK_GOTPCREL:
Rafael Espindola8cecf252010-10-06 16:23:36 +0000721 Type = ELF::R_X86_64_GOTPCREL;
722 break;
Rafael Espindolabc82d8b2010-10-27 20:28:07 +0000723 case MCSymbolRefExpr::VK_TPOFF:
724 Type = ELF::R_X86_64_TPOFF32;
725 break;
Rafael Espindola28f9ac82010-10-04 18:44:25 +0000726 }
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000727 break;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000728 case FK_Data_4:
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000729 Type = ELF::R_X86_64_32;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000730 break;
731 case FK_Data_2: Type = ELF::R_X86_64_16; break;
732 case X86::reloc_pcrel_1byte:
733 case FK_Data_1: Type = ELF::R_X86_64_8; break;
734 }
735 }
Matt Fleming3565a062010-08-16 18:57:57 +0000736 } else {
Benjamin Kramere5b57342010-08-17 18:20:28 +0000737 if (IsPCRel) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000738 switch (Modifier) {
739 default:
Rafael Espindola24dc9ec2010-10-18 19:33:01 +0000740 llvm_unreachable("Unimplemented");
741 case MCSymbolRefExpr::VK_None:
Rafael Espindola9baee3b2010-10-18 18:03:28 +0000742 Type = ELF::R_386_PC32;
Rafael Espindola9baee3b2010-10-18 18:03:28 +0000743 break;
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000744 case MCSymbolRefExpr::VK_PLT:
745 Type = ELF::R_386_PLT32;
746 break;
747 }
Benjamin Kramere5b57342010-08-17 18:20:28 +0000748 } else {
749 switch ((unsigned)Fixup.getKind()) {
750 default: llvm_unreachable("invalid fixup kind!");
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000751
Rafael Espindola24ba4f72010-10-24 17:35:42 +0000752 case X86::reloc_global_offset_table:
753 Type = ELF::R_386_GOTPC;
754 break;
755
Rafael Espindolaa8c02c32010-09-30 03:11:42 +0000756 // FIXME: Should we avoid selecting reloc_signed_4byte in 32 bit mode
757 // instead?
758 case X86::reloc_signed_4byte:
Benjamin Kramere5b57342010-08-17 18:20:28 +0000759 case X86::reloc_pcrel_4byte:
Rafael Espindolabd701182010-10-19 19:31:37 +0000760 case FK_Data_4:
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000761 switch (Modifier) {
Rafael Espindola9edab3a2010-10-18 16:58:03 +0000762 default:
763 llvm_unreachable("Unimplemented");
Rafael Espindolabd701182010-10-19 19:31:37 +0000764 case MCSymbolRefExpr::VK_None:
Rafael Espindola24ba4f72010-10-24 17:35:42 +0000765 Type = ELF::R_386_32;
Rafael Espindolabd701182010-10-19 19:31:37 +0000766 break;
Rafael Espindolaeada3042010-10-18 20:47:21 +0000767 case MCSymbolRefExpr::VK_GOT:
768 Type = ELF::R_386_GOT32;
769 break;
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000770 case MCSymbolRefExpr::VK_GOTOFF:
771 Type = ELF::R_386_GOTOFF;
772 break;
Rafael Espindola3cede2d2010-10-27 21:23:52 +0000773 case MCSymbolRefExpr::VK_TLSGD:
774 Type = ELF::R_386_TLS_GD;
775 break;
776 case MCSymbolRefExpr::VK_TPOFF:
777 Type = ELF::R_386_TLS_LE_32;
778 break;
779 case MCSymbolRefExpr::VK_INDNTPOFF:
780 Type = ELF::R_386_TLS_IE;
781 break;
782 case MCSymbolRefExpr::VK_NTPOFF:
783 Type = ELF::R_386_TLS_LE;
784 break;
Rafael Espindolaa0a2f872010-10-28 14:22:44 +0000785 case MCSymbolRefExpr::VK_GOTNTPOFF:
786 Type = ELF::R_386_TLS_GOTIE;
787 break;
Rafael Espindolaa264f722010-10-28 14:37:09 +0000788 case MCSymbolRefExpr::VK_TLSLDM:
789 Type = ELF::R_386_TLS_LDM;
790 break;
Rafael Espindolac97f80e2010-10-18 16:38:04 +0000791 }
792 break;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000793 case FK_Data_2: Type = ELF::R_386_16; break;
794 case X86::reloc_pcrel_1byte:
795 case FK_Data_1: Type = ELF::R_386_8; break;
796 }
Matt Fleming3565a062010-08-16 18:57:57 +0000797 }
798 }
799
Rafael Espindolaa0a2f872010-10-28 14:22:44 +0000800 if (RelocNeedsGOT(Modifier))
Rafael Espindola5c77c162010-10-05 15:48:37 +0000801 NeedsGOT = true;
802
Benjamin Kramere5b57342010-08-17 18:20:28 +0000803 ELFRelocationEntry ERE;
804
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000805 ERE.Index = Index;
806 ERE.Type = Type;
807 ERE.Symbol = Symbol;
Matt Fleming3565a062010-08-16 18:57:57 +0000808
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000809 ERE.r_offset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
Benjamin Kramere5b57342010-08-17 18:20:28 +0000810
Matt Fleming3565a062010-08-16 18:57:57 +0000811 if (HasRelocationAddend)
812 ERE.r_addend = Addend;
Benjamin Kramer172d7d62010-08-17 00:33:24 +0000813 else
814 ERE.r_addend = 0; // Silence compiler warning.
Matt Fleming3565a062010-08-16 18:57:57 +0000815
816 Relocations[Fragment->getParent()].push_back(ERE);
817}
818
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000819uint64_t
820ELFObjectWriterImpl::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
821 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000822 MCSymbolData &SD = Asm.getSymbolData(*S);
Eli Friedmanf8020a32010-08-16 19:15:06 +0000823
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000824 // Local symbol.
825 if (!SD.isExternal() && !S->isUndefined())
826 return SD.getIndex() + /* empty symbol */ 1;
827
828 // External or undefined symbol.
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000829 return SD.getIndex() + NumRegularSections + /* empty symbol */ 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000830}
831
Rafael Espindola737cd212010-10-05 18:01:23 +0000832static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
Rafael Espindola88182132010-10-27 15:18:17 +0000833 bool Used, bool Renamed) {
Rafael Espindolabd701182010-10-19 19:31:37 +0000834 if (Used)
835 return true;
836
Rafael Espindola88182132010-10-27 15:18:17 +0000837 if (Renamed)
838 return false;
839
Rafael Espindola737cd212010-10-05 18:01:23 +0000840 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa6866962010-10-27 14:44:52 +0000841
842 const MCSymbol &A = AliasedSymbol(Symbol);
843 if (&A != &Symbol && A.isUndefined())
844 return false;
845
Rafael Espindola737cd212010-10-05 18:01:23 +0000846 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
847 return false;
848
Rafael Espindolabd701182010-10-19 19:31:37 +0000849 if (Symbol.isTemporary())
Rafael Espindola737cd212010-10-05 18:01:23 +0000850 return false;
851
852 return true;
853}
854
855static bool isLocal(const MCSymbolData &Data) {
856 if (Data.isExternal())
857 return false;
858
859 const MCSymbol &Symbol = Data.getSymbol();
860 if (Symbol.isUndefined() && !Symbol.isVariable())
861 return false;
862
863 return true;
864}
865
Matt Fleming3565a062010-08-16 18:57:57 +0000866void ELFObjectWriterImpl::ComputeSymbolTable(MCAssembler &Asm) {
Rafael Espindola5c77c162010-10-05 15:48:37 +0000867 // FIXME: Is this the correct place to do this?
868 if (NeedsGOT) {
869 llvm::StringRef Name = "_GLOBAL_OFFSET_TABLE_";
870 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
871 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
872 Data.setExternal(true);
873 }
874
Matt Fleming3565a062010-08-16 18:57:57 +0000875 // Build section lookup table.
Rafael Espindola8f413fa2010-10-05 15:11:03 +0000876 NumRegularSections = Asm.size();
Rafael Espindolaf5c347d2010-10-05 21:20:07 +0000877 DenseMap<const MCSection*, uint32_t> SectionIndexMap;
Matt Fleming3565a062010-08-16 18:57:57 +0000878 unsigned Index = 1;
879 for (MCAssembler::iterator it = Asm.begin(),
880 ie = Asm.end(); it != ie; ++it, ++Index)
881 SectionIndexMap[&it->getSection()] = Index;
882
883 // Index 0 is always the empty string.
884 StringMap<uint64_t> StringIndexMap;
885 StringTable += '\x00';
886
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000887 // Add the data for the symbols.
Matt Fleming3565a062010-08-16 18:57:57 +0000888 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
889 ie = Asm.symbol_end(); it != ie; ++it) {
890 const MCSymbol &Symbol = it->getSymbol();
891
Rafael Espindola88182132010-10-27 15:18:17 +0000892 if (!isInSymtab(Asm, *it, UsedInReloc.count(&Symbol),
893 Renames.count(&Symbol)))
Matt Fleming3565a062010-08-16 18:57:57 +0000894 continue;
895
Matt Fleming3565a062010-08-16 18:57:57 +0000896 ELFSymbolData MSD;
897 MSD.SymbolData = it;
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000898 bool Local = isLocal(*it);
Rafael Espindola88182132010-10-27 15:18:17 +0000899 const MCSymbol &RefSymbol = AliasedSymbol(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000900
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000901 if (it->isCommon()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000902 assert(!Local);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000903 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000904 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolaa0949b52010-10-14 16:34:44 +0000905 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindola88182132010-10-27 15:18:17 +0000906 } else if (RefSymbol.isUndefined()) {
Matt Fleming3565a062010-08-16 18:57:57 +0000907 MSD.SectionIndex = ELF::SHN_UNDEF;
Rafael Espindolae15eb4e2010-09-23 19:55:14 +0000908 // FIXME: Undefined symbols are global, but this is the first place we
909 // are able to set it.
910 if (GetBinding(*it) == ELF::STB_LOCAL)
911 SetBinding(*it, ELF::STB_GLOBAL);
Matt Fleming3565a062010-08-16 18:57:57 +0000912 } else {
Rafael Espindolabf052ac2010-10-27 16:04:30 +0000913 MSD.SectionIndex = SectionIndexMap.lookup(&RefSymbol.getSection());
Matt Fleming3565a062010-08-16 18:57:57 +0000914 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindola5df0b652010-10-15 15:39:06 +0000915 }
916
Rafael Espindola88182132010-10-27 15:18:17 +0000917 // The @@@ in symbol version is replaced with @ in undefined symbols and
918 // @@ in defined ones.
919 StringRef Name = Symbol.getName();
920 size_t Pos = Name.find("@@@");
921 std::string FinalName;
922 if (Pos != StringRef::npos) {
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000923 StringRef Prefix = Name.substr(0, Pos);
Rafael Espindola88182132010-10-27 15:18:17 +0000924 unsigned n = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
Benjamin Kramer07ee6322010-10-27 19:53:52 +0000925 StringRef Suffix = Name.substr(Pos + n);
Rafael Espindola88182132010-10-27 15:18:17 +0000926 FinalName = Prefix.str() + Suffix.str();
927 } else {
928 FinalName = Name.str();
929 }
930
931 uint64_t &Entry = StringIndexMap[FinalName];
Rafael Espindolaa6866962010-10-27 14:44:52 +0000932 if (!Entry) {
933 Entry = StringTable.size();
Rafael Espindola88182132010-10-27 15:18:17 +0000934 StringTable += FinalName;
Rafael Espindolaa6866962010-10-27 14:44:52 +0000935 StringTable += '\x00';
Matt Fleming3565a062010-08-16 18:57:57 +0000936 }
Rafael Espindolaa6866962010-10-27 14:44:52 +0000937 MSD.StringIndex = Entry;
938 if (MSD.SectionIndex == ELF::SHN_UNDEF)
939 UndefinedSymbolData.push_back(MSD);
940 else if (Local)
941 LocalSymbolData.push_back(MSD);
942 else
943 ExternalSymbolData.push_back(MSD);
Matt Fleming3565a062010-08-16 18:57:57 +0000944 }
945
946 // Symbols are required to be in lexicographic order.
947 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
948 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
949 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
950
951 // Set the symbol indices. Local symbols must come before all other
952 // symbols with non-local bindings.
953 Index = 0;
954 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
955 LocalSymbolData[i].SymbolData->setIndex(Index++);
956 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
957 ExternalSymbolData[i].SymbolData->setIndex(Index++);
958 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
959 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
960}
961
962void ELFObjectWriterImpl::WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
963 const MCSectionData &SD) {
964 if (!Relocations[&SD].empty()) {
965 MCContext &Ctx = Asm.getContext();
966 const MCSection *RelaSection;
967 const MCSectionELF &Section =
968 static_cast<const MCSectionELF&>(SD.getSection());
969
970 const StringRef SectionName = Section.getSectionName();
Benjamin Kramer377a5722010-08-17 17:30:07 +0000971 std::string RelaSectionName = HasRelocationAddend ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +0000972 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000973
974 unsigned EntrySize;
975 if (HasRelocationAddend)
976 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
977 else
978 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +0000979
Benjamin Kramer377a5722010-08-17 17:30:07 +0000980 RelaSection = Ctx.getELFSection(RelaSectionName, HasRelocationAddend ?
981 ELF::SHT_RELA : ELF::SHT_REL, 0,
Matt Fleming3565a062010-08-16 18:57:57 +0000982 SectionKind::getReadOnly(),
983 false, EntrySize);
984
985 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Benjamin Kramera9eadca2010-09-06 16:11:52 +0000986 RelaSD.setAlignment(Is64Bit ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +0000987
988 MCDataFragment *F = new MCDataFragment(&RelaSD);
989
990 WriteRelocationsFragment(Asm, F, &SD);
991
Rafael Espindola70703872010-09-30 02:22:20 +0000992 Asm.AddSectionToTheEnd(*Writer, RelaSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +0000993 }
994}
995
996void ELFObjectWriterImpl::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
997 uint64_t Flags, uint64_t Address,
998 uint64_t Offset, uint64_t Size,
999 uint32_t Link, uint32_t Info,
1000 uint64_t Alignment,
1001 uint64_t EntrySize) {
1002 Write32(Name); // sh_name: index into string table
1003 Write32(Type); // sh_type
1004 WriteWord(Flags); // sh_flags
1005 WriteWord(Address); // sh_addr
1006 WriteWord(Offset); // sh_offset
1007 WriteWord(Size); // sh_size
1008 Write32(Link); // sh_link
1009 Write32(Info); // sh_info
1010 WriteWord(Alignment); // sh_addralign
1011 WriteWord(EntrySize); // sh_entsize
1012}
1013
1014void ELFObjectWriterImpl::WriteRelocationsFragment(const MCAssembler &Asm,
1015 MCDataFragment *F,
1016 const MCSectionData *SD) {
1017 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
1018 // sort by the r_offset just like gnu as does
1019 array_pod_sort(Relocs.begin(), Relocs.end());
1020
1021 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1022 ELFRelocationEntry entry = Relocs[e - i - 1];
1023
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001024 if (entry.Index < 0)
1025 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
1026 else
1027 entry.Index += LocalSymbolData.size() + 1;
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001028 if (Is64Bit) {
1029 char buf[8];
Matt Fleming3565a062010-08-16 18:57:57 +00001030
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001031 String64(buf, entry.r_offset);
1032 F->getContents() += StringRef(buf, 8);
1033
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001034 struct ELF::Elf64_Rela ERE64;
1035 ERE64.setSymbolAndType(entry.Index, entry.Type);
1036 String64(buf, ERE64.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001037 F->getContents() += StringRef(buf, 8);
1038
1039 if (HasRelocationAddend) {
1040 String64(buf, entry.r_addend);
1041 F->getContents() += StringRef(buf, 8);
1042 }
1043 } else {
1044 char buf[4];
1045
1046 String32(buf, entry.r_offset);
1047 F->getContents() += StringRef(buf, 4);
1048
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001049 struct ELF::Elf32_Rela ERE32;
1050 ERE32.setSymbolAndType(entry.Index, entry.Type);
1051 String32(buf, ERE32.r_info);
Benjamin Kramer5e492e82010-09-09 18:01:29 +00001052 F->getContents() += StringRef(buf, 4);
1053
1054 if (HasRelocationAddend) {
1055 String32(buf, entry.r_addend);
1056 F->getContents() += StringRef(buf, 4);
1057 }
1058 }
Matt Fleming3565a062010-08-16 18:57:57 +00001059 }
1060}
1061
1062void ELFObjectWriterImpl::CreateMetadataSections(MCAssembler &Asm,
1063 MCAsmLayout &Layout) {
1064 MCContext &Ctx = Asm.getContext();
1065 MCDataFragment *F;
1066
Matt Fleming3565a062010-08-16 18:57:57 +00001067 const MCSection *SymtabSection;
1068 unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
1069
Rafael Espindola71859c62010-09-16 19:46:31 +00001070 unsigned NumRegularSections = Asm.size();
1071
Rafael Espindola38738bf2010-09-22 19:04:41 +00001072 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola71859c62010-09-16 19:46:31 +00001073 const MCSection *ShstrtabSection;
1074 ShstrtabSection = Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
1075 SectionKind::getReadOnly(), false);
1076 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1077 ShstrtabSD.setAlignment(1);
1078 ShstrtabIndex = Asm.size();
1079
Matt Fleming3565a062010-08-16 18:57:57 +00001080 SymtabSection = Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1081 SectionKind::getReadOnly(),
1082 false, EntrySize);
Matt Fleming3565a062010-08-16 18:57:57 +00001083 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Matt Fleming3565a062010-08-16 18:57:57 +00001084 SymtabSD.setAlignment(Is64Bit ? 8 : 4);
1085
Matt Fleming3565a062010-08-16 18:57:57 +00001086 const MCSection *StrtabSection;
1087 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
1088 SectionKind::getReadOnly(), false);
Matt Fleming3565a062010-08-16 18:57:57 +00001089 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1090 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +00001091 StringTableIndex = Asm.size();
1092
Rafael Espindolac3c413f2010-09-27 22:04:54 +00001093 WriteRelocations(Asm, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001094
1095 // Symbol table
1096 F = new MCDataFragment(&SymtabSD);
1097 WriteSymbolTable(F, Asm, Layout, NumRegularSections);
Rafael Espindola70703872010-09-30 02:22:20 +00001098 Asm.AddSectionToTheEnd(*Writer, SymtabSD, Layout);
Rafael Espindola71859c62010-09-16 19:46:31 +00001099
Matt Fleming3565a062010-08-16 18:57:57 +00001100 F = new MCDataFragment(&StrtabSD);
1101 F->getContents().append(StringTable.begin(), StringTable.end());
Rafael Espindola70703872010-09-30 02:22:20 +00001102 Asm.AddSectionToTheEnd(*Writer, StrtabSD, Layout);
Matt Fleming3565a062010-08-16 18:57:57 +00001103
Matt Fleming3565a062010-08-16 18:57:57 +00001104 F = new MCDataFragment(&ShstrtabSD);
1105
Matt Fleming3565a062010-08-16 18:57:57 +00001106 // Section header string table.
1107 //
1108 // The first entry of a string table holds a null character so skip
1109 // section 0.
1110 uint64_t Index = 1;
1111 F->getContents() += '\x00';
1112
1113 for (MCAssembler::const_iterator it = Asm.begin(),
1114 ie = Asm.end(); it != ie; ++it) {
Matt Fleming3565a062010-08-16 18:57:57 +00001115 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +00001116 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola51efe7a2010-09-23 14:14:56 +00001117 // FIXME: We could merge suffixes like in .text and .rela.text.
Matt Fleming3565a062010-08-16 18:57:57 +00001118
1119 // Remember the index into the string table so we can write it
1120 // into the sh_name field of the section header table.
1121 SectionStringTableIndex[&it->getSection()] = Index;
1122
1123 Index += Section.getSectionName().size() + 1;
1124 F->getContents() += Section.getSectionName();
1125 F->getContents() += '\x00';
1126 }
1127
Rafael Espindola70703872010-09-30 02:22:20 +00001128 Asm.AddSectionToTheEnd(*Writer, ShstrtabSD, Layout);
1129}
1130
1131bool ELFObjectWriterImpl::IsFixupFullyResolved(const MCAssembler &Asm,
1132 const MCValue Target,
1133 bool IsPCRel,
1134 const MCFragment *DF) const {
1135 // If this is a PCrel relocation, find the section this fixup value is
1136 // relative to.
1137 const MCSection *BaseSection = 0;
1138 if (IsPCRel) {
1139 BaseSection = &DF->getParent()->getSection();
1140 assert(BaseSection);
1141 }
1142
1143 const MCSection *SectionA = 0;
1144 const MCSymbol *SymbolA = 0;
1145 if (const MCSymbolRefExpr *A = Target.getSymA()) {
1146 SymbolA = &A->getSymbol();
1147 SectionA = &SymbolA->getSection();
1148 }
1149
1150 const MCSection *SectionB = 0;
1151 if (const MCSymbolRefExpr *B = Target.getSymB()) {
1152 SectionB = &B->getSymbol().getSection();
1153 }
1154
1155 if (!BaseSection)
1156 return SectionA == SectionB;
1157
1158 const MCSymbolData &DataA = Asm.getSymbolData(*SymbolA);
1159 if (DataA.isExternal())
1160 return false;
1161
1162 return !SectionB && BaseSection == SectionA;
Matt Fleming3565a062010-08-16 18:57:57 +00001163}
1164
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001165void ELFObjectWriterImpl::WriteObject(MCAssembler &Asm,
Matt Fleming3565a062010-08-16 18:57:57 +00001166 const MCAsmLayout &Layout) {
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001167 // Compute symbol table information.
1168 ComputeSymbolTable(Asm);
1169
Matt Fleming3565a062010-08-16 18:57:57 +00001170 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
1171 const_cast<MCAsmLayout&>(Layout));
1172
1173 // Add 1 for the null section.
1174 unsigned NumSections = Asm.size() + 1;
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001175 uint64_t NaturalAlignment = Is64Bit ? 8 : 4;
1176 uint64_t HeaderSize = Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr);
1177 uint64_t FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001178
1179 for (MCAssembler::const_iterator it = Asm.begin(),
1180 ie = Asm.end(); it != ie; ++it) {
1181 const MCSectionData &SD = *it;
Matt Fleming3565a062010-08-16 18:57:57 +00001182
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001183 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1184
Matt Fleming3565a062010-08-16 18:57:57 +00001185 // Get the size of the section in the output file (including padding).
1186 uint64_t Size = Layout.getSectionFileSize(&SD);
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001187
1188 FileOff += Size;
Matt Fleming3565a062010-08-16 18:57:57 +00001189 }
1190
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001191 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1192
Matt Fleming3565a062010-08-16 18:57:57 +00001193 // Write out the ELF header ...
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001194 WriteHeader(FileOff - HeaderSize, NumSections);
1195
1196 FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +00001197
1198 // ... then all of the sections ...
1199 DenseMap<const MCSection*, uint64_t> SectionOffsetMap;
1200
Rafael Espindolad8e0bfe2010-10-06 22:28:19 +00001201 DenseMap<const MCSection*, uint32_t> SectionIndexMap;
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001202
1203 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +00001204 for (MCAssembler::const_iterator it = Asm.begin(),
1205 ie = Asm.end(); it != ie; ++it) {
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001206 const MCSectionData &SD = *it;
1207
1208 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
1209 WriteZeros(Padding);
1210 FileOff += Padding;
1211
Matt Fleming3565a062010-08-16 18:57:57 +00001212 // Remember the offset into the file for this section.
1213 SectionOffsetMap[&it->getSection()] = FileOff;
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001214 SectionIndexMap[&it->getSection()] = Index++;
1215
Matt Fleming3565a062010-08-16 18:57:57 +00001216 FileOff += Layout.getSectionFileSize(&SD);
1217
1218 Asm.WriteSectionData(it, Layout, Writer);
1219 }
1220
Benjamin Kramera9eadca2010-09-06 16:11:52 +00001221 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
1222 WriteZeros(Padding);
1223 FileOff += Padding;
1224
Matt Fleming3565a062010-08-16 18:57:57 +00001225 // ... and then the section header table.
1226 // Should we align the section header table?
1227 //
1228 // Null section first.
1229 WriteSecHdrEntry(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
1230
1231 for (MCAssembler::const_iterator it = Asm.begin(),
1232 ie = Asm.end(); it != ie; ++it) {
1233 const MCSectionData &SD = *it;
1234 const MCSectionELF &Section =
1235 static_cast<const MCSectionELF&>(SD.getSection());
1236
1237 uint64_t sh_link = 0;
1238 uint64_t sh_info = 0;
1239
1240 switch(Section.getType()) {
1241 case ELF::SHT_DYNAMIC:
1242 sh_link = SectionStringTableIndex[&it->getSection()];
1243 sh_info = 0;
1244 break;
1245
1246 case ELF::SHT_REL:
Eli Friedmanf8020a32010-08-16 19:15:06 +00001247 case ELF::SHT_RELA: {
Matt Fleming3565a062010-08-16 18:57:57 +00001248 const MCSection *SymtabSection;
1249 const MCSection *InfoSection;
Matt Fleming3565a062010-08-16 18:57:57 +00001250
1251 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1252 SectionKind::getReadOnly(),
Eli Friedmanf8020a32010-08-16 19:15:06 +00001253 false);
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001254 sh_link = SectionIndexMap[SymtabSection];
Matt Fleming3565a062010-08-16 18:57:57 +00001255
Benjamin Kramer377a5722010-08-17 17:30:07 +00001256 // Remove ".rel" and ".rela" prefixes.
1257 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1258 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
1259
Eli Friedmanf8020a32010-08-16 19:15:06 +00001260 InfoSection = Asm.getContext().getELFSection(SectionName,
Matt Fleming3565a062010-08-16 18:57:57 +00001261 ELF::SHT_PROGBITS, 0,
Eli Friedmanf8020a32010-08-16 19:15:06 +00001262 SectionKind::getReadOnly(),
1263 false);
Benjamin Kramer44cbde82010-08-19 13:44:49 +00001264 sh_info = SectionIndexMap[InfoSection];
Matt Fleming3565a062010-08-16 18:57:57 +00001265 break;
Eli Friedmanf8020a32010-08-16 19:15:06 +00001266 }
Matt Fleming3565a062010-08-16 18:57:57 +00001267
1268 case ELF::SHT_SYMTAB:
1269 case ELF::SHT_DYNSYM:
1270 sh_link = StringTableIndex;
1271 sh_info = LastLocalSymbolIndex;
1272 break;
1273
1274 case ELF::SHT_PROGBITS:
1275 case ELF::SHT_STRTAB:
1276 case ELF::SHT_NOBITS:
Benjamin Kramer19dc7fa2010-08-31 17:03:33 +00001277 case ELF::SHT_NULL:
Rafael Espindolacecbc3d2010-10-25 17:50:35 +00001278 case ELF::SHT_ARM_ATTRIBUTES:
Matt Fleming3565a062010-08-16 18:57:57 +00001279 // Nothing to do.
1280 break;
1281
Matt Fleming3565a062010-08-16 18:57:57 +00001282 default:
1283 assert(0 && "FIXME: sh_type value not supported!");
1284 break;
1285 }
1286
1287 WriteSecHdrEntry(SectionStringTableIndex[&it->getSection()],
1288 Section.getType(), Section.getFlags(),
Rafael Espindola71859c62010-09-16 19:46:31 +00001289 0,
Matt Fleming3565a062010-08-16 18:57:57 +00001290 SectionOffsetMap.lookup(&SD.getSection()),
1291 Layout.getSectionSize(&SD), sh_link,
1292 sh_info, SD.getAlignment(),
1293 Section.getEntrySize());
1294 }
1295}
1296
1297ELFObjectWriter::ELFObjectWriter(raw_ostream &OS,
1298 bool Is64Bit,
Roman Divacky5baf79e2010-09-09 17:57:50 +00001299 Triple::OSType OSType,
Wesley Peckeecb8582010-10-22 15:52:49 +00001300 uint16_t EMachine,
Matt Fleming3565a062010-08-16 18:57:57 +00001301 bool IsLittleEndian,
1302 bool HasRelocationAddend)
1303 : MCObjectWriter(OS, IsLittleEndian)
1304{
Wesley Peckeecb8582010-10-22 15:52:49 +00001305 Impl = new ELFObjectWriterImpl(this, Is64Bit, EMachine,
1306 HasRelocationAddend, OSType);
Matt Fleming3565a062010-08-16 18:57:57 +00001307}
1308
1309ELFObjectWriter::~ELFObjectWriter() {
1310 delete (ELFObjectWriterImpl*) Impl;
1311}
1312
1313void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm) {
1314 ((ELFObjectWriterImpl*) Impl)->ExecutePostLayoutBinding(Asm);
1315}
1316
1317void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
1318 const MCAsmLayout &Layout,
1319 const MCFragment *Fragment,
1320 const MCFixup &Fixup, MCValue Target,
1321 uint64_t &FixedValue) {
1322 ((ELFObjectWriterImpl*) Impl)->RecordRelocation(Asm, Layout, Fragment, Fixup,
1323 Target, FixedValue);
1324}
1325
Rafael Espindola70703872010-09-30 02:22:20 +00001326bool ELFObjectWriter::IsFixupFullyResolved(const MCAssembler &Asm,
1327 const MCValue Target,
1328 bool IsPCRel,
1329 const MCFragment *DF) const {
1330 return ((ELFObjectWriterImpl*) Impl)->IsFixupFullyResolved(Asm, Target,
1331 IsPCRel, DF);
1332}
1333
Rafael Espindola8f413fa2010-10-05 15:11:03 +00001334void ELFObjectWriter::WriteObject(MCAssembler &Asm,
Matt Fleming3565a062010-08-16 18:57:57 +00001335 const MCAsmLayout &Layout) {
1336 ((ELFObjectWriterImpl*) Impl)->WriteObject(Asm, Layout);
1337}