blob: d0fc9cac4793187a311ecc779e62a64f57153a20 [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
Rafael Espindola00074892010-09-17 22:34:41 +0000473 bool IsPCRel = isFixupKindX86PCRel(Fixup.getKind());
Benjamin Kramer81cfb852010-08-17 19:45:05 +0000474 if (!Target.isAbsolute()) {
Matt Fleming3565a062010-08-16 18:57:57 +0000475 const MCSymbol *Symbol = &Target.getSymA()->getSymbol();
476 MCSymbolData &SD = Asm.getSymbolData(*Symbol);
477 const MCSymbolData *Base = Asm.getAtom(Layout, &SD);
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000478 MCFragment *F = SD.getFragment();
Matt Fleming3565a062010-08-16 18:57:57 +0000479
Rafael Espindola00074892010-09-17 22:34:41 +0000480 // Avoid relocations for cases like jumps and calls in the same file.
481 if (Symbol->isDefined() && !SD.isExternal() &&
482 IsPCRel &&
483 &Fragment->getParent()->getSection() == &Symbol->getSection()) {
484 uint64_t FixupAddr = Layout.getFragmentAddress(Fragment) + Fixup.getOffset();
485 FixedValue = Layout.getSymbolAddress(&SD) + Target.getConstant() - FixupAddr;
486 return;
487 }
488
Matt Fleming3565a062010-08-16 18:57:57 +0000489 if (Base) {
Benjamin Kramer679d2362010-08-30 11:59:29 +0000490 if (F && (!Symbol->isInSection() || SD.isCommon()) && !SD.isExternal()) {
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000491 Index = F->getParent()->getOrdinal() + LocalSymbolData.size() + 1;
Benjamin Kramerbcf2db62010-08-23 19:05:46 +0000492 Value += Layout.getSymbolAddress(&SD);
493 } else
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000494 Index = getSymbolIndexInSymbolTable(Asm, Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000495 if (Base != &SD)
496 Value += Layout.getSymbolAddress(&SD) - Layout.getSymbolAddress(Base);
497 Addend = Value;
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000498 // Compensate for the addend on i386.
Benjamin Kramer4ba1b302010-08-26 18:12:04 +0000499 if (Is64Bit)
500 Value = 0;
Matt Fleming3565a062010-08-16 18:57:57 +0000501 } else {
Matt Fleming3565a062010-08-16 18:57:57 +0000502 if (F) {
503 // Index of the section in .symtab against this symbol
504 // is being relocated + 2 (empty section + abs. symbols).
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000505 Index = F->getParent()->getOrdinal() + LocalSymbolData.size() + 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000506
507 MCSectionData *FSD = F->getParent();
508 // Offset of the symbol in the section
509 Addend = Layout.getSymbolAddress(&SD) - Layout.getSectionAddress(FSD);
510 } else {
511 FixedValue = Value;
512 return;
513 }
514 }
515 }
516
Benjamin Kramer95c602a2010-08-27 10:38:39 +0000517 FixedValue = Value;
518
Matt Fleming3565a062010-08-16 18:57:57 +0000519 // determine the type of the relocation
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000520 unsigned Type;
Benjamin Kramere5b57342010-08-17 18:20:28 +0000521 if (Is64Bit) {
522 if (IsPCRel) {
523 Type = ELF::R_X86_64_PC32;
524 } else {
525 switch ((unsigned)Fixup.getKind()) {
526 default: llvm_unreachable("invalid fixup kind!");
527 case FK_Data_8: Type = ELF::R_X86_64_64; break;
528 case X86::reloc_pcrel_4byte:
529 case FK_Data_4:
530 // check that the offset fits within a signed long
531 if (isInt<32>(Target.getConstant()))
532 Type = ELF::R_X86_64_32S;
533 else
534 Type = ELF::R_X86_64_32;
535 break;
536 case FK_Data_2: Type = ELF::R_X86_64_16; break;
537 case X86::reloc_pcrel_1byte:
538 case FK_Data_1: Type = ELF::R_X86_64_8; break;
539 }
540 }
Matt Fleming3565a062010-08-16 18:57:57 +0000541 } else {
Benjamin Kramere5b57342010-08-17 18:20:28 +0000542 if (IsPCRel) {
543 Type = ELF::R_386_PC32;
544 } else {
545 switch ((unsigned)Fixup.getKind()) {
546 default: llvm_unreachable("invalid fixup kind!");
547 case X86::reloc_pcrel_4byte:
548 case FK_Data_4: Type = ELF::R_386_32; break;
549 case FK_Data_2: Type = ELF::R_386_16; break;
550 case X86::reloc_pcrel_1byte:
551 case FK_Data_1: Type = ELF::R_386_8; break;
552 }
Matt Fleming3565a062010-08-16 18:57:57 +0000553 }
554 }
555
Benjamin Kramere5b57342010-08-17 18:20:28 +0000556 ELFRelocationEntry ERE;
557
558 if (Is64Bit) {
559 struct ELF::Elf64_Rela ERE64;
560 ERE64.setSymbolAndType(Index, Type);
561 ERE.r_info = ERE64.r_info;
562 } else {
563 struct ELF::Elf32_Rela ERE32;
564 ERE32.setSymbolAndType(Index, Type);
565 ERE.r_info = ERE32.r_info;
566 }
Matt Fleming3565a062010-08-16 18:57:57 +0000567
Benjamin Kramer63d37b92010-08-26 17:23:02 +0000568 ERE.r_offset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
Benjamin Kramere5b57342010-08-17 18:20:28 +0000569
Matt Fleming3565a062010-08-16 18:57:57 +0000570 if (HasRelocationAddend)
571 ERE.r_addend = Addend;
Benjamin Kramer172d7d62010-08-17 00:33:24 +0000572 else
573 ERE.r_addend = 0; // Silence compiler warning.
Matt Fleming3565a062010-08-16 18:57:57 +0000574
575 Relocations[Fragment->getParent()].push_back(ERE);
576}
577
Benjamin Kramer0b6cbfe2010-08-23 21:19:37 +0000578uint64_t
579ELFObjectWriterImpl::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
580 const MCSymbol *S) {
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000581 MCSymbolData &SD = Asm.getSymbolData(*S);
Eli Friedmanf8020a32010-08-16 19:15:06 +0000582
Benjamin Kramer7b83c262010-08-25 20:09:43 +0000583 // Local symbol.
584 if (!SD.isExternal() && !S->isUndefined())
585 return SD.getIndex() + /* empty symbol */ 1;
586
587 // External or undefined symbol.
588 return SD.getIndex() + Asm.size() + /* empty symbol */ 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000589}
590
591void ELFObjectWriterImpl::ComputeSymbolTable(MCAssembler &Asm) {
592 // Build section lookup table.
593 DenseMap<const MCSection*, uint8_t> SectionIndexMap;
594 unsigned Index = 1;
595 for (MCAssembler::iterator it = Asm.begin(),
596 ie = Asm.end(); it != ie; ++it, ++Index)
597 SectionIndexMap[&it->getSection()] = Index;
598
599 // Index 0 is always the empty string.
600 StringMap<uint64_t> StringIndexMap;
601 StringTable += '\x00';
602
603 // Add the data for local symbols.
604 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
605 ie = Asm.symbol_end(); it != ie; ++it) {
606 const MCSymbol &Symbol = it->getSymbol();
607
608 // Ignore non-linker visible symbols.
609 if (!Asm.isSymbolLinkerVisible(Symbol))
610 continue;
611
612 if (it->isExternal() || Symbol.isUndefined())
613 continue;
614
615 uint64_t &Entry = StringIndexMap[Symbol.getName()];
616 if (!Entry) {
617 Entry = StringTable.size();
618 StringTable += Symbol.getName();
619 StringTable += '\x00';
620 }
621
622 ELFSymbolData MSD;
623 MSD.SymbolData = it;
624 MSD.StringIndex = Entry;
625
626 if (Symbol.isAbsolute()) {
627 MSD.SectionIndex = ELF::SHN_ABS;
628 LocalSymbolData.push_back(MSD);
629 } else {
630 MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection());
631 assert(MSD.SectionIndex && "Invalid section index!");
632 LocalSymbolData.push_back(MSD);
633 }
634 }
635
636 // Now add non-local symbols.
637 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
638 ie = Asm.symbol_end(); it != ie; ++it) {
639 const MCSymbol &Symbol = it->getSymbol();
640
641 // Ignore non-linker visible symbols.
642 if (!Asm.isSymbolLinkerVisible(Symbol))
643 continue;
644
645 if (!it->isExternal() && !Symbol.isUndefined())
646 continue;
647
648 uint64_t &Entry = StringIndexMap[Symbol.getName()];
649 if (!Entry) {
650 Entry = StringTable.size();
651 StringTable += Symbol.getName();
652 StringTable += '\x00';
653 }
654
655 ELFSymbolData MSD;
656 MSD.SymbolData = it;
657 MSD.StringIndex = Entry;
658
659 if (Symbol.isUndefined()) {
660 MSD.SectionIndex = ELF::SHN_UNDEF;
661 // XXX: for some reason we dont Emit* this
662 it->setFlags(it->getFlags() | ELF_STB_Global);
663 UndefinedSymbolData.push_back(MSD);
664 } else if (Symbol.isAbsolute()) {
665 MSD.SectionIndex = ELF::SHN_ABS;
666 ExternalSymbolData.push_back(MSD);
667 } else if (it->isCommon()) {
668 MSD.SectionIndex = ELF::SHN_COMMON;
669 ExternalSymbolData.push_back(MSD);
670 } else {
671 MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection());
672 assert(MSD.SectionIndex && "Invalid section index!");
673 ExternalSymbolData.push_back(MSD);
674 }
675 }
676
677 // Symbols are required to be in lexicographic order.
678 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
679 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
680 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
681
682 // Set the symbol indices. Local symbols must come before all other
683 // symbols with non-local bindings.
684 Index = 0;
685 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
686 LocalSymbolData[i].SymbolData->setIndex(Index++);
687 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
688 ExternalSymbolData[i].SymbolData->setIndex(Index++);
689 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
690 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
691}
692
693void ELFObjectWriterImpl::WriteRelocation(MCAssembler &Asm, MCAsmLayout &Layout,
694 const MCSectionData &SD) {
695 if (!Relocations[&SD].empty()) {
696 MCContext &Ctx = Asm.getContext();
697 const MCSection *RelaSection;
698 const MCSectionELF &Section =
699 static_cast<const MCSectionELF&>(SD.getSection());
700
701 const StringRef SectionName = Section.getSectionName();
Benjamin Kramer377a5722010-08-17 17:30:07 +0000702 std::string RelaSectionName = HasRelocationAddend ? ".rela" : ".rel";
Matt Fleming3565a062010-08-16 18:57:57 +0000703 RelaSectionName += SectionName;
Benjamin Kramer299fbe32010-08-17 17:56:13 +0000704
705 unsigned EntrySize;
706 if (HasRelocationAddend)
707 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
708 else
709 EntrySize = Is64Bit ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming3565a062010-08-16 18:57:57 +0000710
Benjamin Kramer377a5722010-08-17 17:30:07 +0000711 RelaSection = Ctx.getELFSection(RelaSectionName, HasRelocationAddend ?
712 ELF::SHT_RELA : ELF::SHT_REL, 0,
Matt Fleming3565a062010-08-16 18:57:57 +0000713 SectionKind::getReadOnly(),
714 false, EntrySize);
715
716 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Benjamin Kramera9eadca2010-09-06 16:11:52 +0000717 RelaSD.setAlignment(Is64Bit ? 8 : 4);
Matt Fleming3565a062010-08-16 18:57:57 +0000718
719 MCDataFragment *F = new MCDataFragment(&RelaSD);
720
721 WriteRelocationsFragment(Asm, F, &SD);
722
723 Asm.AddSectionToTheEnd(RelaSD, Layout);
724 }
725}
726
727void ELFObjectWriterImpl::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
728 uint64_t Flags, uint64_t Address,
729 uint64_t Offset, uint64_t Size,
730 uint32_t Link, uint32_t Info,
731 uint64_t Alignment,
732 uint64_t EntrySize) {
733 Write32(Name); // sh_name: index into string table
734 Write32(Type); // sh_type
735 WriteWord(Flags); // sh_flags
736 WriteWord(Address); // sh_addr
737 WriteWord(Offset); // sh_offset
738 WriteWord(Size); // sh_size
739 Write32(Link); // sh_link
740 Write32(Info); // sh_info
741 WriteWord(Alignment); // sh_addralign
742 WriteWord(EntrySize); // sh_entsize
743}
744
745void ELFObjectWriterImpl::WriteRelocationsFragment(const MCAssembler &Asm,
746 MCDataFragment *F,
747 const MCSectionData *SD) {
748 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
749 // sort by the r_offset just like gnu as does
750 array_pod_sort(Relocs.begin(), Relocs.end());
751
752 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
753 ELFRelocationEntry entry = Relocs[e - i - 1];
754
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000755 if (Is64Bit) {
756 char buf[8];
Matt Fleming3565a062010-08-16 18:57:57 +0000757
Benjamin Kramer5e492e82010-09-09 18:01:29 +0000758 String64(buf, entry.r_offset);
759 F->getContents() += StringRef(buf, 8);
760
761 String64(buf, entry.r_info);
762 F->getContents() += StringRef(buf, 8);
763
764 if (HasRelocationAddend) {
765 String64(buf, entry.r_addend);
766 F->getContents() += StringRef(buf, 8);
767 }
768 } else {
769 char buf[4];
770
771 String32(buf, entry.r_offset);
772 F->getContents() += StringRef(buf, 4);
773
774 String32(buf, entry.r_info);
775 F->getContents() += StringRef(buf, 4);
776
777 if (HasRelocationAddend) {
778 String32(buf, entry.r_addend);
779 F->getContents() += StringRef(buf, 4);
780 }
781 }
Matt Fleming3565a062010-08-16 18:57:57 +0000782 }
783}
784
785void ELFObjectWriterImpl::CreateMetadataSections(MCAssembler &Asm,
786 MCAsmLayout &Layout) {
787 MCContext &Ctx = Asm.getContext();
788 MCDataFragment *F;
789
790 WriteRelocations(Asm, Layout);
791
792 const MCSection *SymtabSection;
793 unsigned EntrySize = Is64Bit ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
794
Rafael Espindola71859c62010-09-16 19:46:31 +0000795 unsigned NumRegularSections = Asm.size();
796
797 // We construct .shstrtab, .symtab and .strtab is this order to match gnu as.
798 const MCSection *ShstrtabSection;
799 ShstrtabSection = Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
800 SectionKind::getReadOnly(), false);
801 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
802 ShstrtabSD.setAlignment(1);
803 ShstrtabIndex = Asm.size();
804
Matt Fleming3565a062010-08-16 18:57:57 +0000805 SymtabSection = Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
806 SectionKind::getReadOnly(),
807 false, EntrySize);
Matt Fleming3565a062010-08-16 18:57:57 +0000808 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Matt Fleming3565a062010-08-16 18:57:57 +0000809 SymtabSD.setAlignment(Is64Bit ? 8 : 4);
810
Matt Fleming3565a062010-08-16 18:57:57 +0000811 const MCSection *StrtabSection;
812 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
813 SectionKind::getReadOnly(), false);
Matt Fleming3565a062010-08-16 18:57:57 +0000814 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
815 StrtabSD.setAlignment(1);
Matt Fleming3565a062010-08-16 18:57:57 +0000816 StringTableIndex = Asm.size();
817
Rafael Espindola71859c62010-09-16 19:46:31 +0000818
819 // Symbol table
820 F = new MCDataFragment(&SymtabSD);
821 WriteSymbolTable(F, Asm, Layout, NumRegularSections);
822 Asm.AddSectionToTheEnd(SymtabSD, Layout);
823
Matt Fleming3565a062010-08-16 18:57:57 +0000824 F = new MCDataFragment(&StrtabSD);
825 F->getContents().append(StringTable.begin(), StringTable.end());
826 Asm.AddSectionToTheEnd(StrtabSD, Layout);
827
Matt Fleming3565a062010-08-16 18:57:57 +0000828 F = new MCDataFragment(&ShstrtabSD);
829
Matt Fleming3565a062010-08-16 18:57:57 +0000830 // Section header string table.
831 //
832 // The first entry of a string table holds a null character so skip
833 // section 0.
834 uint64_t Index = 1;
835 F->getContents() += '\x00';
836
837 for (MCAssembler::const_iterator it = Asm.begin(),
838 ie = Asm.end(); it != ie; ++it) {
Matt Fleming3565a062010-08-16 18:57:57 +0000839 const MCSectionELF &Section =
Benjamin Kramer368ae7e2010-08-17 00:00:46 +0000840 static_cast<const MCSectionELF&>(it->getSection());
Matt Fleming3565a062010-08-16 18:57:57 +0000841
842 // Remember the index into the string table so we can write it
843 // into the sh_name field of the section header table.
844 SectionStringTableIndex[&it->getSection()] = Index;
845
846 Index += Section.getSectionName().size() + 1;
847 F->getContents() += Section.getSectionName();
848 F->getContents() += '\x00';
849 }
850
851 Asm.AddSectionToTheEnd(ShstrtabSD, Layout);
852}
853
854void ELFObjectWriterImpl::WriteObject(const MCAssembler &Asm,
855 const MCAsmLayout &Layout) {
Matt Fleming3565a062010-08-16 18:57:57 +0000856 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
857 const_cast<MCAsmLayout&>(Layout));
858
859 // Add 1 for the null section.
860 unsigned NumSections = Asm.size() + 1;
Benjamin Kramera9eadca2010-09-06 16:11:52 +0000861 uint64_t NaturalAlignment = Is64Bit ? 8 : 4;
862 uint64_t HeaderSize = Is64Bit ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr);
863 uint64_t FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +0000864
865 for (MCAssembler::const_iterator it = Asm.begin(),
866 ie = Asm.end(); it != ie; ++it) {
867 const MCSectionData &SD = *it;
Matt Fleming3565a062010-08-16 18:57:57 +0000868
Benjamin Kramera9eadca2010-09-06 16:11:52 +0000869 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
870
Matt Fleming3565a062010-08-16 18:57:57 +0000871 // Get the size of the section in the output file (including padding).
872 uint64_t Size = Layout.getSectionFileSize(&SD);
Benjamin Kramera9eadca2010-09-06 16:11:52 +0000873
874 FileOff += Size;
Matt Fleming3565a062010-08-16 18:57:57 +0000875 }
876
Benjamin Kramera9eadca2010-09-06 16:11:52 +0000877 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
878
Matt Fleming3565a062010-08-16 18:57:57 +0000879 // Write out the ELF header ...
Benjamin Kramera9eadca2010-09-06 16:11:52 +0000880 WriteHeader(FileOff - HeaderSize, NumSections);
881
882 FileOff = HeaderSize;
Matt Fleming3565a062010-08-16 18:57:57 +0000883
884 // ... then all of the sections ...
885 DenseMap<const MCSection*, uint64_t> SectionOffsetMap;
886
Benjamin Kramer44cbde82010-08-19 13:44:49 +0000887 DenseMap<const MCSection*, uint8_t> SectionIndexMap;
888
889 unsigned Index = 1;
Matt Fleming3565a062010-08-16 18:57:57 +0000890 for (MCAssembler::const_iterator it = Asm.begin(),
891 ie = Asm.end(); it != ie; ++it) {
Benjamin Kramera9eadca2010-09-06 16:11:52 +0000892 const MCSectionData &SD = *it;
893
894 uint64_t Padding = OffsetToAlignment(FileOff, SD.getAlignment());
895 WriteZeros(Padding);
896 FileOff += Padding;
897
Matt Fleming3565a062010-08-16 18:57:57 +0000898 // Remember the offset into the file for this section.
899 SectionOffsetMap[&it->getSection()] = FileOff;
Benjamin Kramer44cbde82010-08-19 13:44:49 +0000900 SectionIndexMap[&it->getSection()] = Index++;
901
Matt Fleming3565a062010-08-16 18:57:57 +0000902 FileOff += Layout.getSectionFileSize(&SD);
903
904 Asm.WriteSectionData(it, Layout, Writer);
905 }
906
Benjamin Kramera9eadca2010-09-06 16:11:52 +0000907 uint64_t Padding = OffsetToAlignment(FileOff, NaturalAlignment);
908 WriteZeros(Padding);
909 FileOff += Padding;
910
Matt Fleming3565a062010-08-16 18:57:57 +0000911 // ... and then the section header table.
912 // Should we align the section header table?
913 //
914 // Null section first.
915 WriteSecHdrEntry(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
916
917 for (MCAssembler::const_iterator it = Asm.begin(),
918 ie = Asm.end(); it != ie; ++it) {
919 const MCSectionData &SD = *it;
920 const MCSectionELF &Section =
921 static_cast<const MCSectionELF&>(SD.getSection());
922
923 uint64_t sh_link = 0;
924 uint64_t sh_info = 0;
925
926 switch(Section.getType()) {
927 case ELF::SHT_DYNAMIC:
928 sh_link = SectionStringTableIndex[&it->getSection()];
929 sh_info = 0;
930 break;
931
932 case ELF::SHT_REL:
Eli Friedmanf8020a32010-08-16 19:15:06 +0000933 case ELF::SHT_RELA: {
Matt Fleming3565a062010-08-16 18:57:57 +0000934 const MCSection *SymtabSection;
935 const MCSection *InfoSection;
Matt Fleming3565a062010-08-16 18:57:57 +0000936
937 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
938 SectionKind::getReadOnly(),
Eli Friedmanf8020a32010-08-16 19:15:06 +0000939 false);
Benjamin Kramer44cbde82010-08-19 13:44:49 +0000940 sh_link = SectionIndexMap[SymtabSection];
Matt Fleming3565a062010-08-16 18:57:57 +0000941
Benjamin Kramer377a5722010-08-17 17:30:07 +0000942 // Remove ".rel" and ".rela" prefixes.
943 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
944 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
945
Eli Friedmanf8020a32010-08-16 19:15:06 +0000946 InfoSection = Asm.getContext().getELFSection(SectionName,
Matt Fleming3565a062010-08-16 18:57:57 +0000947 ELF::SHT_PROGBITS, 0,
Eli Friedmanf8020a32010-08-16 19:15:06 +0000948 SectionKind::getReadOnly(),
949 false);
Benjamin Kramer44cbde82010-08-19 13:44:49 +0000950 sh_info = SectionIndexMap[InfoSection];
Matt Fleming3565a062010-08-16 18:57:57 +0000951 break;
Eli Friedmanf8020a32010-08-16 19:15:06 +0000952 }
Matt Fleming3565a062010-08-16 18:57:57 +0000953
954 case ELF::SHT_SYMTAB:
955 case ELF::SHT_DYNSYM:
956 sh_link = StringTableIndex;
957 sh_info = LastLocalSymbolIndex;
958 break;
959
960 case ELF::SHT_PROGBITS:
961 case ELF::SHT_STRTAB:
962 case ELF::SHT_NOBITS:
Benjamin Kramer19dc7fa2010-08-31 17:03:33 +0000963 case ELF::SHT_NULL:
Matt Fleming3565a062010-08-16 18:57:57 +0000964 // Nothing to do.
965 break;
966
967 case ELF::SHT_HASH:
968 case ELF::SHT_GROUP:
969 case ELF::SHT_SYMTAB_SHNDX:
970 default:
971 assert(0 && "FIXME: sh_type value not supported!");
972 break;
973 }
974
975 WriteSecHdrEntry(SectionStringTableIndex[&it->getSection()],
976 Section.getType(), Section.getFlags(),
Rafael Espindola71859c62010-09-16 19:46:31 +0000977 0,
Matt Fleming3565a062010-08-16 18:57:57 +0000978 SectionOffsetMap.lookup(&SD.getSection()),
979 Layout.getSectionSize(&SD), sh_link,
980 sh_info, SD.getAlignment(),
981 Section.getEntrySize());
982 }
983}
984
985ELFObjectWriter::ELFObjectWriter(raw_ostream &OS,
986 bool Is64Bit,
Roman Divacky5baf79e2010-09-09 17:57:50 +0000987 Triple::OSType OSType,
Matt Fleming3565a062010-08-16 18:57:57 +0000988 bool IsLittleEndian,
989 bool HasRelocationAddend)
990 : MCObjectWriter(OS, IsLittleEndian)
991{
Roman Divacky5baf79e2010-09-09 17:57:50 +0000992 Impl = new ELFObjectWriterImpl(this, Is64Bit, HasRelocationAddend, OSType);
Matt Fleming3565a062010-08-16 18:57:57 +0000993}
994
995ELFObjectWriter::~ELFObjectWriter() {
996 delete (ELFObjectWriterImpl*) Impl;
997}
998
999void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm) {
1000 ((ELFObjectWriterImpl*) Impl)->ExecutePostLayoutBinding(Asm);
1001}
1002
1003void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
1004 const MCAsmLayout &Layout,
1005 const MCFragment *Fragment,
1006 const MCFixup &Fixup, MCValue Target,
1007 uint64_t &FixedValue) {
1008 ((ELFObjectWriterImpl*) Impl)->RecordRelocation(Asm, Layout, Fragment, Fixup,
1009 Target, FixedValue);
1010}
1011
1012void ELFObjectWriter::WriteObject(const MCAssembler &Asm,
1013 const MCAsmLayout &Layout) {
1014 ((ELFObjectWriterImpl*) Impl)->WriteObject(Asm, Layout);
1015}