blob: 8295dacd1568393bbc33da5c383965daffd25e06 [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"
15#include "llvm/ADT/STLExtras.h"
16#include "llvm/ADT/StringMap.h"
17#include "llvm/ADT/Twine.h"
18#include "llvm/MC/MCAssembler.h"
19#include "llvm/MC/MCAsmLayout.h"
20#include "llvm/MC/MCContext.h"
21#include "llvm/MC/MCELFSymbolFlags.h"
22#include "llvm/MC/MCExpr.h"
23#include "llvm/MC/MCObjectWriter.h"
24#include "llvm/MC/MCSectionELF.h"
25#include "llvm/MC/MCSymbol.h"
26#include "llvm/MC/MCValue.h"
27#include "llvm/Support/Debug.h"
28#include "llvm/Support/ErrorHandling.h"
29#include "llvm/Support/ELF.h"
30#include "llvm/Target/TargetAsmBackend.h"
31
32#include "../Target/X86/X86FixupKinds.h"
33
34#include <vector>
35using namespace llvm;
36
37namespace {
38
39 class ELFObjectWriterImpl {
40 static bool isFixupKindX86PCRel(unsigned Kind) {
41 switch (Kind) {
42 default:
43 return false;
44 case X86::reloc_pcrel_1byte:
45 case X86::reloc_pcrel_4byte:
46 case X86::reloc_riprel_4byte:
47 case X86::reloc_riprel_4byte_movq_load:
48 return true;
49 }
50 }
51
Chris Lattnerb188a372010-08-28 03:21:03 +000052 /*static bool isFixupKindX86RIPRel(unsigned Kind) {
Matt Fleming3565a062010-08-16 18:57:57 +000053 return Kind == X86::reloc_riprel_4byte ||
54 Kind == X86::reloc_riprel_4byte_movq_load;
Chris Lattnerb188a372010-08-28 03:21:03 +000055 }*/
Matt Fleming3565a062010-08-16 18:57:57 +000056
57
58 /// ELFSymbolData - Helper struct for containing some precomputed information
59 /// on symbols.
60 struct ELFSymbolData {
61 MCSymbolData *SymbolData;
62 uint64_t StringIndex;
63 uint32_t SectionIndex;
64
65 // Support lexicographic sorting.
66 bool operator<(const ELFSymbolData &RHS) const {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +000067 return SymbolData->getSymbol().getName() <
68 RHS.SymbolData->getSymbol().getName();
Matt Fleming3565a062010-08-16 18:57:57 +000069 }
70 };
71
72 /// @name Relocation Data
73 /// @{
74
75 struct ELFRelocationEntry {
76 // Make these big enough for both 32-bit and 64-bit
77 uint64_t r_offset;
78 uint64_t r_info;
79 uint64_t r_addend;
80
81 // Support lexicographic sorting.
82 bool operator<(const ELFRelocationEntry &RE) const {
83 return RE.r_offset < r_offset;
84 }
85 };
86
87 llvm::DenseMap<const MCSectionData*,
88 std::vector<ELFRelocationEntry> > Relocations;
89 DenseMap<const MCSection*, uint64_t> SectionStringTableIndex;
90
91 /// @}
92 /// @name Symbol Table Data
93 /// @{
94
95 SmallString<256> StringTable;
96 std::vector<ELFSymbolData> LocalSymbolData;
97 std::vector<ELFSymbolData> ExternalSymbolData;
98 std::vector<ELFSymbolData> UndefinedSymbolData;
99
100 /// @}
101
102 ELFObjectWriter *Writer;
103
104 raw_ostream &OS;
105
Matt Fleming3565a062010-08-16 18:57:57 +0000106 unsigned Is64Bit : 1;
107
108 bool HasRelocationAddend;
109
Roman Divacky5baf79e2010-09-09 17:57:50 +0000110 Triple::OSType OSType;
111
Matt Fleming3565a062010-08-16 18:57:57 +0000112 // This holds the symbol table index of the last local symbol.
113 unsigned LastLocalSymbolIndex;
114 // This holds the .strtab section index.
115 unsigned StringTableIndex;
116
117 unsigned ShstrtabIndex;
118
119 public:
120 ELFObjectWriterImpl(ELFObjectWriter *_Writer, bool _Is64Bit,
Roman Divacky5baf79e2010-09-09 17:57:50 +0000121 bool _HasRelAddend, Triple::OSType _OSType)
Benjamin Kramera9eadca2010-09-06 16:11:52 +0000122 : Writer(_Writer), OS(Writer->getStream()),
Roman Divacky5baf79e2010-09-09 17:57:50 +0000123 Is64Bit(_Is64Bit), HasRelocationAddend(_HasRelAddend),
124 OSType(_OSType) {
Matt Fleming3565a062010-08-16 18:57:57 +0000125 }
126
127 void Write8(uint8_t Value) { Writer->Write8(Value); }
128 void Write16(uint16_t Value) { Writer->Write16(Value); }
129 void Write32(uint32_t Value) { Writer->Write32(Value); }
Chris Lattnerb188a372010-08-28 03:21:03 +0000130 //void Write64(uint64_t Value) { Writer->Write64(Value); }
Matt Fleming3565a062010-08-16 18:57:57 +0000131 void WriteZeros(unsigned N) { Writer->WriteZeros(N); }
Chris Lattnerb188a372010-08-28 03:21:03 +0000132 //void WriteBytes(StringRef Str, unsigned ZeroFillSize = 0) {
133 // Writer->WriteBytes(Str, ZeroFillSize);
134 //}
Matt Fleming3565a062010-08-16 18:57:57 +0000135
136 void WriteWord(uint64_t W) {
Chris Lattnerb188a372010-08-28 03:21:03 +0000137 if (Is64Bit)
Matt Fleming3565a062010-08-16 18:57:57 +0000138 Writer->Write64(W);
Chris Lattnerb188a372010-08-28 03:21:03 +0000139 else
Matt Fleming3565a062010-08-16 18:57:57 +0000140 Writer->Write32(W);
Matt Fleming3565a062010-08-16 18:57:57 +0000141 }
142
143 void String8(char *buf, uint8_t Value) {
144 buf[0] = Value;
145 }
146
147 void StringLE16(char *buf, uint16_t Value) {
148 buf[0] = char(Value >> 0);
149 buf[1] = char(Value >> 8);
150 }
151
152 void StringLE32(char *buf, uint32_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000153 StringLE16(buf, uint16_t(Value >> 0));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000154 StringLE16(buf + 2, uint16_t(Value >> 16));
Matt Fleming3565a062010-08-16 18:57:57 +0000155 }
156
157 void StringLE64(char *buf, uint64_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000158 StringLE32(buf, uint32_t(Value >> 0));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000159 StringLE32(buf + 4, uint32_t(Value >> 32));
Matt Fleming3565a062010-08-16 18:57:57 +0000160 }
161
162 void StringBE16(char *buf ,uint16_t Value) {
163 buf[0] = char(Value >> 8);
164 buf[1] = char(Value >> 0);
165 }
166
167 void StringBE32(char *buf, uint32_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000168 StringBE16(buf, uint16_t(Value >> 16));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000169 StringBE16(buf + 2, uint16_t(Value >> 0));
Matt Fleming3565a062010-08-16 18:57:57 +0000170 }
171
172 void StringBE64(char *buf, uint64_t Value) {
Benjamin Kramer36c6dc22010-08-23 21:23:52 +0000173 StringBE32(buf, uint32_t(Value >> 32));
Benjamin Kramerc522f6e2010-08-23 21:32:00 +0000174 StringBE32(buf + 4, uint32_t(Value >> 0));
Matt Fleming3565a062010-08-16 18:57:57 +0000175 }
176
177 void String16(char *buf, uint16_t Value) {
178 if (Writer->isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000179 StringLE16(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000180 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000181 StringBE16(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000182 }
183
184 void String32(char *buf, uint32_t Value) {
185 if (Writer->isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000186 StringLE32(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000187 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000188 StringBE32(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000189 }
190
191 void String64(char *buf, uint64_t Value) {
192 if (Writer->isLittleEndian())
Eli Friedmanf8020a32010-08-16 19:15:06 +0000193 StringLE64(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000194 else
Eli Friedmanf8020a32010-08-16 19:15:06 +0000195 StringBE64(buf, Value);
Matt Fleming3565a062010-08-16 18:57:57 +0000196 }
197
198 void WriteHeader(uint64_t SectionDataSize, unsigned NumberOfSections);
199
200 void WriteSymbolEntry(MCDataFragment *F, uint64_t name, uint8_t info,
201 uint64_t value, uint64_t size,
202 uint8_t other, uint16_t shndx);
203
204 void WriteSymbol(MCDataFragment *F, ELFSymbolData &MSD,
205 const MCAsmLayout &Layout);
206
207 void WriteSymbolTable(MCDataFragment *F, const MCAssembler &Asm,
Rafael Espindola71859c62010-09-16 19:46:31 +0000208 const MCAsmLayout &Layout,
209 unsigned NumRegularSections);
Matt Fleming3565a062010-08-16 18:57:57 +0000210
211 void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
212 const MCFragment *Fragment, const MCFixup &Fixup,
213 MCValue Target, uint64_t &FixedValue);
214
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000215 uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm,
216 const MCSymbol *S);
Matt Fleming3565a062010-08-16 18:57:57 +0000217
218 /// ComputeSymbolTable - Compute the symbol table data
219 ///
220 /// \param StringTable [out] - The string table data.
221 /// \param StringIndexMap [out] - Map from symbol names to offsets in the
222 /// string table.
223 void ComputeSymbolTable(MCAssembler &Asm);
224
225 void WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
226 const MCSectionData &SD);
227
228 void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout) {
229 for (MCAssembler::const_iterator it = Asm.begin(),
230 ie = Asm.end(); it != ie; ++it) {
231 WriteRelocation(Asm, Layout, *it);
232 }
233 }
234
235 void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout);
236
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000237 void ExecutePostLayoutBinding(MCAssembler &Asm) {
238 // Compute symbol table information.
239 ComputeSymbolTable(Asm);
240 }
Matt Fleming3565a062010-08-16 18:57:57 +0000241
242 void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
243 uint64_t Address, uint64_t Offset,
244 uint64_t Size, uint32_t Link, uint32_t Info,
245 uint64_t Alignment, uint64_t EntrySize);
246
247 void WriteRelocationsFragment(const MCAssembler &Asm, MCDataFragment *F,
248 const MCSectionData *SD);
249
250 void WriteObject(const MCAssembler &Asm, const MCAsmLayout &Layout);
251 };
252
253}
254
255// Emit the ELF header.
256void ELFObjectWriterImpl::WriteHeader(uint64_t SectionDataSize,
257 unsigned NumberOfSections) {
258 // ELF Header
259 // ----------
260 //
261 // Note
262 // ----
263 // emitWord method behaves differently for ELF32 and ELF64, writing
264 // 4 bytes in the former and 8 in the latter.
265
266 Write8(0x7f); // e_ident[EI_MAG0]
267 Write8('E'); // e_ident[EI_MAG1]
268 Write8('L'); // e_ident[EI_MAG2]
269 Write8('F'); // e_ident[EI_MAG3]
270
271 Write8(Is64Bit ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
272
273 // e_ident[EI_DATA]
274 Write8(Writer->isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
275
276 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky5baf79e2010-09-09 17:57:50 +0000277 // e_ident[EI_OSABI]
278 switch (OSType) {
279 case Triple::FreeBSD: Write8(ELF::ELFOSABI_FREEBSD); break;
280 case Triple::Linux: Write8(ELF::ELFOSABI_LINUX); break;
281 default: Write8(ELF::ELFOSABI_NONE); break;
282 }
Matt Fleming3565a062010-08-16 18:57:57 +0000283 Write8(0); // e_ident[EI_ABIVERSION]
284
285 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
286
287 Write16(ELF::ET_REL); // e_type
288
289 // FIXME: Make this configurable
Benjamin Kramereb976772010-08-17 17:02:29 +0000290 Write16(Is64Bit ? ELF::EM_X86_64 : ELF::EM_386); // e_machine = target
Matt Fleming3565a062010-08-16 18:57:57 +0000291
292 Write32(ELF::EV_CURRENT); // e_version
293 WriteWord(0); // e_entry, no entry point in .o file
294 WriteWord(0); // e_phoff, no program header for .o
Benjamin Kramereb976772010-08-17 17:02:29 +0000295 WriteWord(SectionDataSize + (Is64Bit ? sizeof(ELF::Elf64_Ehdr) :
296 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming3565a062010-08-16 18:57:57 +0000297
298 // FIXME: Make this configurable.
299 Write32(0); // e_flags = whatever the target wants
300
301 // e_ehsize = ELF header size
302 Write16(Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
303
304 Write16(0); // e_phentsize = prog header entry size
305 Write16(0); // e_phnum = # prog header entries = 0
306
307 // e_shentsize = Section header entry size
308 Write16(Is64Bit ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
309
310 // e_shnum = # of section header ents
311 Write16(NumberOfSections);
312
313 // e_shstrndx = Section # of '.shstrtab'
314 Write16(ShstrtabIndex);
315}
316
317void ELFObjectWriterImpl::WriteSymbolEntry(MCDataFragment *F, uint64_t name,
318 uint8_t info, uint64_t value,
319 uint64_t size, uint8_t other,
320 uint16_t shndx) {
321 if (Is64Bit) {
322 char buf[8];
323
324 String32(buf, name);
325 F->getContents() += StringRef(buf, 4); // st_name
326
327 String8(buf, info);
328 F->getContents() += StringRef(buf, 1); // st_info
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000329
Matt Fleming3565a062010-08-16 18:57:57 +0000330 String8(buf, other);
331 F->getContents() += StringRef(buf, 1); // st_other
332
333 String16(buf, shndx);
334 F->getContents() += StringRef(buf, 2); // st_shndx
335
336 String64(buf, value);
337 F->getContents() += StringRef(buf, 8); // st_value
338
339 String64(buf, size);
340 F->getContents() += StringRef(buf, 8); // st_size
341 } else {
342 char buf[4];
343
344 String32(buf, name);
345 F->getContents() += StringRef(buf, 4); // st_name
346
347 String32(buf, value);
348 F->getContents() += StringRef(buf, 4); // st_value
349
350 String32(buf, size);
351 F->getContents() += StringRef(buf, 4); // st_size
352
353 String8(buf, info);
354 F->getContents() += StringRef(buf, 1); // st_info
355
356 String8(buf, other);
357 F->getContents() += StringRef(buf, 1); // st_other
358
359 String16(buf, shndx);
360 F->getContents() += StringRef(buf, 2); // st_shndx
361 }
362}
363
364void ELFObjectWriterImpl::WriteSymbol(MCDataFragment *F, ELFSymbolData &MSD,
365 const MCAsmLayout &Layout) {
366 MCSymbolData &Data = *MSD.SymbolData;
Matt Fleming3565a062010-08-16 18:57:57 +0000367 uint8_t Info = (Data.getFlags() & 0xff);
368 uint8_t Other = ((Data.getFlags() & 0xf00) >> ELF_STV_Shift);
369 uint64_t Value = 0;
370 uint64_t Size = 0;
371 const MCExpr *ESize;
372
373 if (Data.isCommon() && Data.isExternal())
374 Value = Data.getCommonAlignment();
375
Roman Divacky563b38a2010-09-08 14:29:45 +0000376 if (!Data.isCommon() && !(Data.getFlags() & ELF_STB_Weak))
Benjamin Kramer6cc53be2010-08-30 17:20:17 +0000377 if (MCFragment *FF = Data.getFragment())
378 Value = Layout.getSymbolAddress(&Data) -
379 Layout.getSectionAddress(FF->getParent());
380
Matt Fleming3565a062010-08-16 18:57:57 +0000381 ESize = Data.getSize();
382 if (Data.getSize()) {
383 MCValue Res;
384 if (ESize->getKind() == MCExpr::Binary) {
385 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(ESize);
386
387 if (BE->EvaluateAsRelocatable(Res, &Layout)) {
388 MCSymbolData &A =
389 Layout.getAssembler().getSymbolData(Res.getSymA()->getSymbol());
390 MCSymbolData &B =
391 Layout.getAssembler().getSymbolData(Res.getSymB()->getSymbol());
392
393 Size = Layout.getSymbolAddress(&A) - Layout.getSymbolAddress(&B);
Matt Fleming3565a062010-08-16 18:57:57 +0000394 }
395 } else if (ESize->getKind() == MCExpr::Constant) {
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000396 Size = static_cast<const MCConstantExpr *>(ESize)->getValue();
Matt Fleming3565a062010-08-16 18:57:57 +0000397 } else {
398 assert(0 && "Unsupported size expression");
399 }
400 }
401
402 // Write out the symbol table entry
403 WriteSymbolEntry(F, MSD.StringIndex, Info, Value,
404 Size, Other, MSD.SectionIndex);
405}
406
407void ELFObjectWriterImpl::WriteSymbolTable(MCDataFragment *F,
408 const MCAssembler &Asm,
Rafael Espindola71859c62010-09-16 19:46:31 +0000409 const MCAsmLayout &Layout,
410 unsigned NumRegularSections) {
Matt Fleming3565a062010-08-16 18:57:57 +0000411 // The string table must be emitted first because we need the index
412 // into the string table for all the symbol names.
413 assert(StringTable.size() && "Missing string table");
414
415 // FIXME: Make sure the start of the symbol table is aligned.
416
417 // The first entry is the undefined symbol entry.
418 unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000419 F->getContents().append(EntrySize, '\x00');
Matt Fleming3565a062010-08-16 18:57:57 +0000420
421 // Write the symbol table entries.
422 LastLocalSymbolIndex = LocalSymbolData.size() + 1;
423 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
424 ELFSymbolData &MSD = LocalSymbolData[i];
425 WriteSymbol(F, MSD, Layout);
426 }
427
Rafael Espindola71859c62010-09-16 19:46:31 +0000428 // Write out a symbol table entry for each regular section.
Eli Friedmana44fa242010-08-16 21:17:09 +0000429 unsigned Index = 1;
Rafael Espindola71859c62010-09-16 19:46:31 +0000430 for (MCAssembler::const_iterator it = Asm.begin();
431 Index <= NumRegularSections; ++it, ++Index) {
Eli Friedmana44fa242010-08-16 21:17:09 +0000432 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000433 static_cast<const MCSectionELF&>(it->getSection());
Eli Friedmana44fa242010-08-16 21:17:09 +0000434 // Leave out relocations so we don't have indexes within
435 // the relocations messed up
Benjamin Kramer377a5722010-08-17 17:30:07 +0000436 if (Section.getType() == ELF::SHT_RELA || Section.getType() == ELF::SHT_REL)
Eli Friedmana44fa242010-08-16 21:17:09 +0000437 continue;
Matt Fleming3565a062010-08-16 18:57:57 +0000438 WriteSymbolEntry(F, 0, ELF::STT_SECTION, 0, 0, ELF::STV_DEFAULT, Index);
Eli Friedmana44fa242010-08-16 21:17:09 +0000439 LastLocalSymbolIndex++;
440 }
Matt Fleming3565a062010-08-16 18:57:57 +0000441
442 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
443 ELFSymbolData &MSD = ExternalSymbolData[i];
444 MCSymbolData &Data = *MSD.SymbolData;
445 assert((Data.getFlags() & ELF_STB_Global) &&
446 "External symbol requires STB_GLOBAL flag");
447 WriteSymbol(F, MSD, Layout);
Roman Divackyb629dcc2010-09-08 18:08:40 +0000448 if ((Data.getFlags() & (0xf << ELF_STB_Shift)) == ELF_STB_Local)
Matt Fleming3565a062010-08-16 18:57:57 +0000449 LastLocalSymbolIndex++;
450 }
451
452 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
453 ELFSymbolData &MSD = UndefinedSymbolData[i];
454 MCSymbolData &Data = *MSD.SymbolData;
455 Data.setFlags(Data.getFlags() | ELF_STB_Global);
456 WriteSymbol(F, MSD, Layout);
Roman Divackyb629dcc2010-09-08 18:08:40 +0000457 if ((Data.getFlags() & (0xf << ELF_STB_Shift)) == ELF_STB_Local)
Matt Fleming3565a062010-08-16 18:57:57 +0000458 LastLocalSymbolIndex++;
459 }
460}
461
Benjamin Kramere5b57342010-08-17 18:20:28 +0000462// FIXME: this is currently X86/X86_64 only
Matt Fleming3565a062010-08-16 18:57:57 +0000463void ELFObjectWriterImpl::RecordRelocation(const MCAssembler &Asm,
464 const MCAsmLayout &Layout,
465 const MCFragment *Fragment,
466 const MCFixup &Fixup,
467 MCValue Target,
468 uint64_t &FixedValue) {
Matt Fleming3565a062010-08-16 18:57:57 +0000469 int64_t Addend = 0;
470 unsigned Index = 0;
Benjamin Kramer95c602a2010-08-27 10:38:39 +0000471 int64_t Value = Target.getConstant();
Matt Fleming3565a062010-08-16 18:57:57 +0000472
Benjamin Kramer81cfb852010-08-17 19:45:05 +0000473 if (!Target.isAbsolute()) {
Matt Fleming3565a062010-08-16 18:57:57 +0000474 const MCSymbol *Symbol = &Target.getSymA()->getSymbol();
475 MCSymbolData &SD = Asm.getSymbolData(*Symbol);
476 const MCSymbolData *Base = Asm.getAtom(Layout, &SD);
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000477 MCFragment *F = SD.getFragment();
Matt Fleming3565a062010-08-16 18:57:57 +0000478
479 if (Base) {
Benjamin Kramer679d2362010-08-30 11:59:29 +0000480 if (F && (!Symbol->isInSection() || SD.isCommon()) && !SD.isExternal()) {
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000481 Index = F->getParent()->getOrdinal() + LocalSymbolData.size() + 1;
Benjamin Kramerbcf2db62010-08-23 19:05:46 +0000482 Value += Layout.getSymbolAddress(&SD);
483 } else
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000484 Index = getSymbolIndexInSymbolTable(Asm, Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000485 if (Base != &SD)
486 Value += Layout.getSymbolAddress(&SD) - Layout.getSymbolAddress(Base);
487 Addend = Value;
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000488 // Compensate for the addend on i386.
Benjamin Kramer4ba1b302010-08-26 18:12:04 +0000489 if (Is64Bit)
490 Value = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000491 } else {
Matt Fleming3565a062010-08-16 18:57:57 +0000492 if (F) {
493 // Index of the section in .symtab against this symbol
494 // is being relocated + 2 (empty section + abs. symbols).
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000495 Index = F->getParent()->getOrdinal() + LocalSymbolData.size() + 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000496
497 MCSectionData *FSD = F->getParent();
498 // Offset of the symbol in the section
499 Addend = Layout.getSymbolAddress(&SD) - Layout.getSectionAddress(FSD);
500 } else {
501 FixedValue = Value;
502 return;
503 }
504 }
505 }
506
Benjamin Kramer95c602a2010-08-27 10:38:39 +0000507 FixedValue = Value;
508
Matt Fleming3565a062010-08-16 18:57:57 +0000509 // determine the type of the relocation
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000510 bool IsPCRel = isFixupKindX86PCRel(Fixup.getKind());
511 unsigned Type;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000512 if (Is64Bit) {
513 if (IsPCRel) {
514 Type = ELF::R_X86_64_PC32;
515 } else {
516 switch ((unsigned)Fixup.getKind()) {
517 default: llvm_unreachable("invalid fixup kind!");
518 case FK_Data_8: Type = ELF::R_X86_64_64; break;
519 case X86::reloc_pcrel_4byte:
520 case FK_Data_4:
521 // check that the offset fits within a signed long
522 if (isInt<32>(Target.getConstant()))
523 Type = ELF::R_X86_64_32S;
524 else
525 Type = ELF::R_X86_64_32;
526 break;
527 case FK_Data_2: Type = ELF::R_X86_64_16; break;
528 case X86::reloc_pcrel_1byte:
529 case FK_Data_1: Type = ELF::R_X86_64_8; break;
530 }
531 }
Matt Fleming3565a062010-08-16 18:57:57 +0000532 } else {
Benjamin Kramere5b57342010-08-17 18:20:28 +0000533 if (IsPCRel) {
534 Type = ELF::R_386_PC32;
535 } else {
536 switch ((unsigned)Fixup.getKind()) {
537 default: llvm_unreachable("invalid fixup kind!");
538 case X86::reloc_pcrel_4byte:
539 case FK_Data_4: Type = ELF::R_386_32; break;
540 case FK_Data_2: Type = ELF::R_386_16; break;
541 case X86::reloc_pcrel_1byte:
542 case FK_Data_1: Type = ELF::R_386_8; break;
543 }
Matt Fleming3565a062010-08-16 18:57:57 +0000544 }
545 }
546
Benjamin Kramere5b57342010-08-17 18:20:28 +0000547 ELFRelocationEntry ERE;
548
549 if (Is64Bit) {
550 struct ELF::Elf64_Rela ERE64;
551 ERE64.setSymbolAndType(Index, Type);
552 ERE.r_info = ERE64.r_info;
553 } else {
554 struct ELF::Elf32_Rela ERE32;
555 ERE32.setSymbolAndType(Index, Type);
556 ERE.r_info = ERE32.r_info;
557 }
Matt Fleming3565a062010-08-16 18:57:57 +0000558
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000559 ERE.r_offset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
Benjamin Kramere5b57342010-08-17 18:20:28 +0000560
Matt Fleming3565a062010-08-16 18:57:57 +0000561 if (HasRelocationAddend)
562 ERE.r_addend = Addend;
Benjamin Kramer172d7d62010-08-17 00:33:24 +0000563 else
564 ERE.r_addend = 0; // Silence compiler warning.
Matt Fleming3565a062010-08-16 18:57:57 +0000565
566 Relocations[Fragment->getParent()].push_back(ERE);
567}
568
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000569uint64_t
570ELFObjectWriterImpl::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
571 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000572 MCSymbolData &SD = Asm.getSymbolData(*S);
Eli Friedmanf8020a32010-08-16 19:15:06 +0000573
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000574 // Local symbol.
575 if (!SD.isExternal() && !S->isUndefined())
576 return SD.getIndex() + /* empty symbol */ 1;
577
578 // External or undefined symbol.
579 return SD.getIndex() + Asm.size() + /* empty symbol */ 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000580}
581
582void ELFObjectWriterImpl::ComputeSymbolTable(MCAssembler &Asm) {
583 // Build section lookup table.
584 DenseMap<const MCSection*, uint8_t> SectionIndexMap;
585 unsigned Index = 1;
586 for (MCAssembler::iterator it = Asm.begin(),
587 ie = Asm.end(); it != ie; ++it, ++Index)
588 SectionIndexMap[&it->getSection()] = Index;
589
590 // Index 0 is always the empty string.
591 StringMap<uint64_t> StringIndexMap;
592 StringTable += '\x00';
593
594 // Add the data for local symbols.
595 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
596 ie = Asm.symbol_end(); it != ie; ++it) {
597 const MCSymbol &Symbol = it->getSymbol();
598
599 // Ignore non-linker visible symbols.
600 if (!Asm.isSymbolLinkerVisible(Symbol))
601 continue;
602
603 if (it->isExternal() || Symbol.isUndefined())
604 continue;
605
606 uint64_t &Entry = StringIndexMap[Symbol.getName()];
607 if (!Entry) {
608 Entry = StringTable.size();
609 StringTable += Symbol.getName();
610 StringTable += '\x00';
611 }
612
613 ELFSymbolData MSD;
614 MSD.SymbolData = it;
615 MSD.StringIndex = Entry;
616
617 if (Symbol.isAbsolute()) {
618 MSD.SectionIndex = ELF::SHN_ABS;
619 LocalSymbolData.push_back(MSD);
620 } else {
621 MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection());
622 assert(MSD.SectionIndex && "Invalid section index!");
623 LocalSymbolData.push_back(MSD);
624 }
625 }
626
627 // Now add non-local symbols.
628 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
629 ie = Asm.symbol_end(); it != ie; ++it) {
630 const MCSymbol &Symbol = it->getSymbol();
631
632 // Ignore non-linker visible symbols.
633 if (!Asm.isSymbolLinkerVisible(Symbol))
634 continue;
635
636 if (!it->isExternal() && !Symbol.isUndefined())
637 continue;
638
639 uint64_t &Entry = StringIndexMap[Symbol.getName()];
640 if (!Entry) {
641 Entry = StringTable.size();
642 StringTable += Symbol.getName();
643 StringTable += '\x00';
644 }
645
646 ELFSymbolData MSD;
647 MSD.SymbolData = it;
648 MSD.StringIndex = Entry;
649
650 if (Symbol.isUndefined()) {
651 MSD.SectionIndex = ELF::SHN_UNDEF;
652 // XXX: for some reason we dont Emit* this
653 it->setFlags(it->getFlags() | ELF_STB_Global);
654 UndefinedSymbolData.push_back(MSD);
655 } else if (Symbol.isAbsolute()) {
656 MSD.SectionIndex = ELF::SHN_ABS;
657 ExternalSymbolData.push_back(MSD);
658 } else if (it->isCommon()) {
659 MSD.SectionIndex = ELF::SHN_COMMON;
660 ExternalSymbolData.push_back(MSD);
661 } else {
662 MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection());
663 assert(MSD.SectionIndex && "Invalid section index!");
664 ExternalSymbolData.push_back(MSD);
665 }
666 }
667
668 // Symbols are required to be in lexicographic order.
669 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
670 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
671 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
672
673 // Set the symbol indices. Local symbols must come before all other
674 // symbols with non-local bindings.
675 Index = 0;
676 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
677 LocalSymbolData[i].SymbolData->setIndex(Index++);
678 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
679 ExternalSymbolData[i].SymbolData->setIndex(Index++);
680 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
681 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
682}
683
684void ELFObjectWriterImpl::WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
685 const MCSectionData &SD) {
686 if (!Relocations[&SD].empty()) {
687 MCContext &Ctx = Asm.getContext();
688 const MCSection *RelaSection;
689 const MCSectionELF &Section =
690 static_cast<const MCSectionELF&>(SD.getSection());
691
692 const StringRef SectionName = Section.getSectionName();
Benjamin Kramer377a5722010-08-17 17:30:07 +0000693 std::string RelaSectionName = HasRelocationAddend ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +0000694 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000695
696 unsigned EntrySize;
697 if (HasRelocationAddend)
698 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
699 else
700 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +0000701
Benjamin Kramer377a5722010-08-17 17:30:07 +0000702 RelaSection = Ctx.getELFSection(RelaSectionName, HasRelocationAddend ?
703 ELF::SHT_RELA : ELF::SHT_REL, 0,
Matt Fleming3565a062010-08-16 18:57:57 +0000704 SectionKind::getReadOnly(),
705 false, EntrySize);
706
707 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Benjamin Kramera9eadca2010-09-06 16:11:52 +0000708 RelaSD.setAlignment(Is64Bit ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +0000709
710 MCDataFragment *F = new MCDataFragment(&RelaSD);
711
712 WriteRelocationsFragment(Asm, F, &SD);
713
714 Asm.AddSectionToTheEnd(RelaSD, Layout);
715 }
716}
717
718void ELFObjectWriterImpl::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
719 uint64_t Flags, uint64_t Address,
720 uint64_t Offset, uint64_t Size,
721 uint32_t Link, uint32_t Info,
722 uint64_t Alignment,
723 uint64_t EntrySize) {
724 Write32(Name); // sh_name: index into string table
725 Write32(Type); // sh_type
726 WriteWord(Flags); // sh_flags
727 WriteWord(Address); // sh_addr
728 WriteWord(Offset); // sh_offset
729 WriteWord(Size); // sh_size
730 Write32(Link); // sh_link
731 Write32(Info); // sh_info
732 WriteWord(Alignment); // sh_addralign
733 WriteWord(EntrySize); // sh_entsize
734}
735
736void ELFObjectWriterImpl::WriteRelocationsFragment(const MCAssembler &Asm,
737 MCDataFragment *F,
738 const MCSectionData *SD) {
739 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
740 // sort by the r_offset just like gnu as does
741 array_pod_sort(Relocs.begin(), Relocs.end());
742
743 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
744 ELFRelocationEntry entry = Relocs[e - i - 1];
745
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000746 if (Is64Bit) {
747 char buf[8];
Matt Fleming3565a062010-08-16 18:57:57 +0000748
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000749 String64(buf, entry.r_offset);
750 F->getContents() += StringRef(buf, 8);
751
752 String64(buf, entry.r_info);
753 F->getContents() += StringRef(buf, 8);
754
755 if (HasRelocationAddend) {
756 String64(buf, entry.r_addend);
757 F->getContents() += StringRef(buf, 8);
758 }
759 } else {
760 char buf[4];
761
762 String32(buf, entry.r_offset);
763 F->getContents() += StringRef(buf, 4);
764
765 String32(buf, entry.r_info);
766 F->getContents() += StringRef(buf, 4);
767
768 if (HasRelocationAddend) {
769 String32(buf, entry.r_addend);
770 F->getContents() += StringRef(buf, 4);
771 }
772 }
Matt Fleming3565a062010-08-16 18:57:57 +0000773 }
774}
775
776void ELFObjectWriterImpl::CreateMetadataSections(MCAssembler &Asm,
777 MCAsmLayout &Layout) {
778 MCContext &Ctx = Asm.getContext();
779 MCDataFragment *F;
780
781 WriteRelocations(Asm, Layout);
782
783 const MCSection *SymtabSection;
784 unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
785
Rafael Espindola71859c62010-09-16 19:46:31 +0000786 unsigned NumRegularSections = Asm.size();
787
788 // We construct .shstrtab, .symtab and .strtab is this order to match gnu as.
789 const MCSection *ShstrtabSection;
790 ShstrtabSection = Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
791 SectionKind::getReadOnly(), false);
792 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
793 ShstrtabSD.setAlignment(1);
794 ShstrtabIndex = Asm.size();
795
Matt Fleming3565a062010-08-16 18:57:57 +0000796 SymtabSection = Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
797 SectionKind::getReadOnly(),
798 false, EntrySize);
Matt Fleming3565a062010-08-16 18:57:57 +0000799 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Matt Fleming3565a062010-08-16 18:57:57 +0000800 SymtabSD.setAlignment(Is64Bit ? 8 : 4);
801
Matt Fleming3565a062010-08-16 18:57:57 +0000802 const MCSection *StrtabSection;
803 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
804 SectionKind::getReadOnly(), false);
Matt Fleming3565a062010-08-16 18:57:57 +0000805 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
806 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +0000807 StringTableIndex = Asm.size();
808
Rafael Espindola71859c62010-09-16 19:46:31 +0000809
810 // Symbol table
811 F = new MCDataFragment(&SymtabSD);
812 WriteSymbolTable(F, Asm, Layout, NumRegularSections);
813 Asm.AddSectionToTheEnd(SymtabSD, Layout);
814
Matt Fleming3565a062010-08-16 18:57:57 +0000815 F = new MCDataFragment(&StrtabSD);
816 F->getContents().append(StringTable.begin(), StringTable.end());
817 Asm.AddSectionToTheEnd(StrtabSD, Layout);
818
Matt Fleming3565a062010-08-16 18:57:57 +0000819 F = new MCDataFragment(&ShstrtabSD);
820
Matt Fleming3565a062010-08-16 18:57:57 +0000821 // Section header string table.
822 //
823 // The first entry of a string table holds a null character so skip
824 // section 0.
825 uint64_t Index = 1;
826 F->getContents() += '\x00';
827
828 for (MCAssembler::const_iterator it = Asm.begin(),
829 ie = Asm.end(); it != ie; ++it) {
Matt Fleming3565a062010-08-16 18:57:57 +0000830 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000831 static_cast<const MCSectionELF&>(it->getSection());
Matt Fleming3565a062010-08-16 18:57:57 +0000832
833 // Remember the index into the string table so we can write it
834 // into the sh_name field of the section header table.
835 SectionStringTableIndex[&it->getSection()] = Index;
836
837 Index += Section.getSectionName().size() + 1;
838 F->getContents() += Section.getSectionName();
839 F->getContents() += '\x00';
840 }
841
842 Asm.AddSectionToTheEnd(ShstrtabSD, Layout);
843}
844
845void ELFObjectWriterImpl::WriteObject(const MCAssembler &Asm,
846 const MCAsmLayout &Layout) {
Matt Fleming3565a062010-08-16 18:57:57 +0000847 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
848 const_cast<MCAsmLayout&>(Layout));
849
850 // Add 1 for the null section.
851 unsigned NumSections = Asm.size() + 1;
Benjamin Kramera9eadca2010-09-06 16:11:52 +0000852 uint64_t NaturalAlignment = Is64Bit ? 8 : 4;
853 uint64_t HeaderSize = Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr);
854 uint64_t FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +0000855
856 for (MCAssembler::const_iterator it = Asm.begin(),
857 ie = Asm.end(); it != ie; ++it) {
858 const MCSectionData &SD = *it;
Matt Fleming3565a062010-08-16 18:57:57 +0000859
Benjamin Kramera9eadca2010-09-06 16:11:52 +0000860 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
861
Matt Fleming3565a062010-08-16 18:57:57 +0000862 // Get the size of the section in the output file (including padding).
863 uint64_t Size = Layout.getSectionFileSize(&SD);
Benjamin Kramera9eadca2010-09-06 16:11:52 +0000864
865 FileOff += Size;
Matt Fleming3565a062010-08-16 18:57:57 +0000866 }
867
Benjamin Kramera9eadca2010-09-06 16:11:52 +0000868 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
869
Matt Fleming3565a062010-08-16 18:57:57 +0000870 // Write out the ELF header ...
Benjamin Kramera9eadca2010-09-06 16:11:52 +0000871 WriteHeader(FileOff - HeaderSize, NumSections);
872
873 FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +0000874
875 // ... then all of the sections ...
876 DenseMap<const MCSection*, uint64_t> SectionOffsetMap;
877
Benjamin Kramer44cbde82010-08-19 13:44:49 +0000878 DenseMap<const MCSection*, uint8_t> SectionIndexMap;
879
880 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000881 for (MCAssembler::const_iterator it = Asm.begin(),
882 ie = Asm.end(); it != ie; ++it) {
Benjamin Kramera9eadca2010-09-06 16:11:52 +0000883 const MCSectionData &SD = *it;
884
885 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
886 WriteZeros(Padding);
887 FileOff += Padding;
888
Matt Fleming3565a062010-08-16 18:57:57 +0000889 // Remember the offset into the file for this section.
890 SectionOffsetMap[&it->getSection()] = FileOff;
Benjamin Kramer44cbde82010-08-19 13:44:49 +0000891 SectionIndexMap[&it->getSection()] = Index++;
892
Matt Fleming3565a062010-08-16 18:57:57 +0000893 FileOff += Layout.getSectionFileSize(&SD);
894
895 Asm.WriteSectionData(it, Layout, Writer);
896 }
897
Benjamin Kramera9eadca2010-09-06 16:11:52 +0000898 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
899 WriteZeros(Padding);
900 FileOff += Padding;
901
Matt Fleming3565a062010-08-16 18:57:57 +0000902 // ... and then the section header table.
903 // Should we align the section header table?
904 //
905 // Null section first.
906 WriteSecHdrEntry(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
907
908 for (MCAssembler::const_iterator it = Asm.begin(),
909 ie = Asm.end(); it != ie; ++it) {
910 const MCSectionData &SD = *it;
911 const MCSectionELF &Section =
912 static_cast<const MCSectionELF&>(SD.getSection());
913
914 uint64_t sh_link = 0;
915 uint64_t sh_info = 0;
916
917 switch(Section.getType()) {
918 case ELF::SHT_DYNAMIC:
919 sh_link = SectionStringTableIndex[&it->getSection()];
920 sh_info = 0;
921 break;
922
923 case ELF::SHT_REL:
Eli Friedmanf8020a32010-08-16 19:15:06 +0000924 case ELF::SHT_RELA: {
Matt Fleming3565a062010-08-16 18:57:57 +0000925 const MCSection *SymtabSection;
926 const MCSection *InfoSection;
Matt Fleming3565a062010-08-16 18:57:57 +0000927
928 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
929 SectionKind::getReadOnly(),
Eli Friedmanf8020a32010-08-16 19:15:06 +0000930 false);
Benjamin Kramer44cbde82010-08-19 13:44:49 +0000931 sh_link = SectionIndexMap[SymtabSection];
Matt Fleming3565a062010-08-16 18:57:57 +0000932
Benjamin Kramer377a5722010-08-17 17:30:07 +0000933 // Remove ".rel" and ".rela" prefixes.
934 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
935 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
936
Eli Friedmanf8020a32010-08-16 19:15:06 +0000937 InfoSection = Asm.getContext().getELFSection(SectionName,
Matt Fleming3565a062010-08-16 18:57:57 +0000938 ELF::SHT_PROGBITS, 0,
Eli Friedmanf8020a32010-08-16 19:15:06 +0000939 SectionKind::getReadOnly(),
940 false);
Benjamin Kramer44cbde82010-08-19 13:44:49 +0000941 sh_info = SectionIndexMap[InfoSection];
Matt Fleming3565a062010-08-16 18:57:57 +0000942 break;
Eli Friedmanf8020a32010-08-16 19:15:06 +0000943 }
Matt Fleming3565a062010-08-16 18:57:57 +0000944
945 case ELF::SHT_SYMTAB:
946 case ELF::SHT_DYNSYM:
947 sh_link = StringTableIndex;
948 sh_info = LastLocalSymbolIndex;
949 break;
950
951 case ELF::SHT_PROGBITS:
952 case ELF::SHT_STRTAB:
953 case ELF::SHT_NOBITS:
Benjamin Kramer19dc7fa2010-08-31 17:03:33 +0000954 case ELF::SHT_NULL:
Matt Fleming3565a062010-08-16 18:57:57 +0000955 // Nothing to do.
956 break;
957
958 case ELF::SHT_HASH:
959 case ELF::SHT_GROUP:
960 case ELF::SHT_SYMTAB_SHNDX:
961 default:
962 assert(0 && "FIXME: sh_type value not supported!");
963 break;
964 }
965
966 WriteSecHdrEntry(SectionStringTableIndex[&it->getSection()],
967 Section.getType(), Section.getFlags(),
Rafael Espindola71859c62010-09-16 19:46:31 +0000968 0,
Matt Fleming3565a062010-08-16 18:57:57 +0000969 SectionOffsetMap.lookup(&SD.getSection()),
970 Layout.getSectionSize(&SD), sh_link,
971 sh_info, SD.getAlignment(),
972 Section.getEntrySize());
973 }
974}
975
976ELFObjectWriter::ELFObjectWriter(raw_ostream &OS,
977 bool Is64Bit,
Roman Divacky5baf79e2010-09-09 17:57:50 +0000978 Triple::OSType OSType,
Matt Fleming3565a062010-08-16 18:57:57 +0000979 bool IsLittleEndian,
980 bool HasRelocationAddend)
981 : MCObjectWriter(OS, IsLittleEndian)
982{
Roman Divacky5baf79e2010-09-09 17:57:50 +0000983 Impl = new ELFObjectWriterImpl(this, Is64Bit, HasRelocationAddend, OSType);
Matt Fleming3565a062010-08-16 18:57:57 +0000984}
985
986ELFObjectWriter::~ELFObjectWriter() {
987 delete (ELFObjectWriterImpl*) Impl;
988}
989
990void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm) {
991 ((ELFObjectWriterImpl*) Impl)->ExecutePostLayoutBinding(Asm);
992}
993
994void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
995 const MCAsmLayout &Layout,
996 const MCFragment *Fragment,
997 const MCFixup &Fixup, MCValue Target,
998 uint64_t &FixedValue) {
999 ((ELFObjectWriterImpl*) Impl)->RecordRelocation(Asm, Layout, Fragment, Fixup,
1000 Target, FixedValue);
1001}
1002
1003void ELFObjectWriter::WriteObject(const MCAssembler &Asm,
1004 const MCAsmLayout &Layout) {
1005 ((ELFObjectWriterImpl*) Impl)->WriteObject(Asm, Layout);
1006}