blob: 6a48d62c8e12dd5f58657660742a78491727c12f [file] [log] [blame]
Nick Lewycky4288f9e2013-03-09 09:32:16 +00001//===- lib/MC/ELFObjectWriter.cpp - ELF File Writer -----------------------===//
Matt Fleming6c1ad482010-08-16 18:57:57 +00002//
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
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "llvm/MC/MCELFObjectWriter.h"
Rafael Espindola29abd972011-12-22 03:38:00 +000015#include "llvm/ADT/OwningPtr.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/STLExtras.h"
Rafael Espindola29abd972011-12-22 03:38:00 +000017#include "llvm/ADT/SmallPtrSet.h"
18#include "llvm/ADT/SmallString.h"
Matt Fleming6c1ad482010-08-16 18:57:57 +000019#include "llvm/ADT/StringMap.h"
Evan Cheng5928e692011-07-25 23:24:55 +000020#include "llvm/MC/MCAsmBackend.h"
Matt Fleming6c1ad482010-08-16 18:57:57 +000021#include "llvm/MC/MCAsmLayout.h"
Rafael Espindola29abd972011-12-22 03:38:00 +000022#include "llvm/MC/MCAssembler.h"
Matt Fleming6c1ad482010-08-16 18:57:57 +000023#include "llvm/MC/MCContext.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000024#include "llvm/MC/MCELF.h"
Rafael Espindola29abd972011-12-22 03:38:00 +000025#include "llvm/MC/MCELFSymbolFlags.h"
Matt Fleming6c1ad482010-08-16 18:57:57 +000026#include "llvm/MC/MCExpr.h"
Craig Topper6e80c282012-03-26 06:58:25 +000027#include "llvm/MC/MCFixupKindInfo.h"
28#include "llvm/MC/MCObjectWriter.h"
Matt Fleming6c1ad482010-08-16 18:57:57 +000029#include "llvm/MC/MCSectionELF.h"
Matt Fleming6c1ad482010-08-16 18:57:57 +000030#include "llvm/MC/MCValue.h"
31#include "llvm/Support/Debug.h"
Matt Fleming6c1ad482010-08-16 18:57:57 +000032#include "llvm/Support/ELF.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000033#include "llvm/Support/ErrorHandling.h"
Matt Fleming6c1ad482010-08-16 18:57:57 +000034#include <vector>
35using namespace llvm;
36
Jason W Kimc09e7262011-05-11 22:53:06 +000037#undef DEBUG_TYPE
38#define DEBUG_TYPE "reloc-info"
39
Rafael Espindola29abd972011-12-22 03:38:00 +000040namespace {
41class ELFObjectWriter : public MCObjectWriter {
42 protected:
43
44 static bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind);
45 static bool RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant);
46 static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout);
47 static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
48 bool Used, bool Renamed);
49 static bool isLocal(const MCSymbolData &Data, bool isSignature,
50 bool isUsedInReloc);
51 static bool IsELFMetaDataSection(const MCSectionData &SD);
52 static uint64_t DataSectionSize(const MCSectionData &SD);
53 static uint64_t GetSectionFileSize(const MCAsmLayout &Layout,
54 const MCSectionData &SD);
55 static uint64_t GetSectionAddressSize(const MCAsmLayout &Layout,
56 const MCSectionData &SD);
57
58 void WriteDataSectionData(MCAssembler &Asm,
59 const MCAsmLayout &Layout,
60 const MCSectionELF &Section);
61
62 /*static bool isFixupKindX86RIPRel(unsigned Kind) {
63 return Kind == X86::reloc_riprel_4byte ||
64 Kind == X86::reloc_riprel_4byte_movq_load;
65 }*/
66
67 /// ELFSymbolData - Helper struct for containing some precomputed
68 /// information on symbols.
69 struct ELFSymbolData {
70 MCSymbolData *SymbolData;
71 uint64_t StringIndex;
72 uint32_t SectionIndex;
73
74 // Support lexicographic sorting.
75 bool operator<(const ELFSymbolData &RHS) const {
Rafael Espindola29abd972011-12-22 03:38:00 +000076 return SymbolData->getSymbol().getName() <
77 RHS.SymbolData->getSymbol().getName();
78 }
79 };
80
Rafael Espindola29abd972011-12-22 03:38:00 +000081 /// The target specific ELF writer instance.
82 llvm::OwningPtr<MCELFObjectTargetWriter> TargetObjectWriter;
83
84 SmallPtrSet<const MCSymbol *, 16> UsedInReloc;
85 SmallPtrSet<const MCSymbol *, 16> WeakrefUsedInReloc;
86 DenseMap<const MCSymbol *, const MCSymbol *> Renames;
87
88 llvm::DenseMap<const MCSectionData*,
89 std::vector<ELFRelocationEntry> > Relocations;
90 DenseMap<const MCSection*, uint64_t> SectionStringTableIndex;
91
92 /// @}
93 /// @name Symbol Table Data
94 /// @{
95
96 SmallString<256> StringTable;
Joerg Sonnenbergerfc184732013-10-29 01:06:17 +000097 std::vector<uint64_t> FileSymbolData;
Rafael Espindola29abd972011-12-22 03:38:00 +000098 std::vector<ELFSymbolData> LocalSymbolData;
99 std::vector<ELFSymbolData> ExternalSymbolData;
100 std::vector<ELFSymbolData> UndefinedSymbolData;
101
102 /// @}
103
104 bool NeedsGOT;
105
106 bool NeedsSymtabShndx;
107
108 // This holds the symbol table index of the last local symbol.
109 unsigned LastLocalSymbolIndex;
110 // This holds the .strtab section index.
111 unsigned StringTableIndex;
112 // This holds the .symtab section index.
113 unsigned SymbolTableIndex;
114
115 unsigned ShstrtabIndex;
116
117
118 const MCSymbol *SymbolToReloc(const MCAssembler &Asm,
119 const MCValue &Target,
120 const MCFragment &F,
121 const MCFixup &Fixup,
122 bool IsPCRel) const;
123
124 // TargetObjectWriter wrappers.
125 const MCSymbol *ExplicitRelSym(const MCAssembler &Asm,
126 const MCValue &Target,
127 const MCFragment &F,
128 const MCFixup &Fixup,
129 bool IsPCRel) const {
130 return TargetObjectWriter->ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
131 }
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +0000132 const MCSymbol *undefinedExplicitRelSym(const MCValue &Target,
133 const MCFixup &Fixup,
134 bool IsPCRel) const {
Jack Carterf6622ba2013-02-12 21:29:39 +0000135 return TargetObjectWriter->undefinedExplicitRelSym(Target, Fixup,
136 IsPCRel);
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +0000137 }
Rafael Espindola29abd972011-12-22 03:38:00 +0000138
139 bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
140 bool hasRelocationAddend() const {
141 return TargetObjectWriter->hasRelocationAddend();
142 }
Rafael Espindola29abd972011-12-22 03:38:00 +0000143 unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
144 bool IsPCRel, bool IsRelocWithSymbol,
145 int64_t Addend) const {
146 return TargetObjectWriter->GetRelocType(Target, Fixup, IsPCRel,
147 IsRelocWithSymbol, Addend);
148 }
149
Rafael Espindola29abd972011-12-22 03:38:00 +0000150 public:
151 ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
152 raw_ostream &_OS, bool IsLittleEndian)
153 : MCObjectWriter(_OS, IsLittleEndian),
154 TargetObjectWriter(MOTW),
Nick Lewycky4288f9e2013-03-09 09:32:16 +0000155 NeedsGOT(false), NeedsSymtabShndx(false) {
Rafael Espindola29abd972011-12-22 03:38:00 +0000156 }
157
158 virtual ~ELFObjectWriter();
159
160 void WriteWord(uint64_t W) {
161 if (is64Bit())
162 Write64(W);
163 else
164 Write32(W);
165 }
166
167 void StringLE16(char *buf, uint16_t Value) {
168 buf[0] = char(Value >> 0);
169 buf[1] = char(Value >> 8);
170 }
171
172 void StringLE32(char *buf, uint32_t Value) {
173 StringLE16(buf, uint16_t(Value >> 0));
174 StringLE16(buf + 2, uint16_t(Value >> 16));
175 }
176
177 void StringLE64(char *buf, uint64_t Value) {
178 StringLE32(buf, uint32_t(Value >> 0));
179 StringLE32(buf + 4, uint32_t(Value >> 32));
180 }
181
182 void StringBE16(char *buf ,uint16_t Value) {
183 buf[0] = char(Value >> 8);
184 buf[1] = char(Value >> 0);
185 }
186
187 void StringBE32(char *buf, uint32_t Value) {
188 StringBE16(buf, uint16_t(Value >> 16));
189 StringBE16(buf + 2, uint16_t(Value >> 0));
190 }
191
192 void StringBE64(char *buf, uint64_t Value) {
193 StringBE32(buf, uint32_t(Value >> 32));
194 StringBE32(buf + 4, uint32_t(Value >> 0));
195 }
196
197 void String8(MCDataFragment &F, uint8_t Value) {
198 char buf[1];
199 buf[0] = Value;
Eli Bendersky84b2a792012-12-07 22:06:56 +0000200 F.getContents().append(&buf[0], &buf[1]);
Rafael Espindola29abd972011-12-22 03:38:00 +0000201 }
202
203 void String16(MCDataFragment &F, uint16_t Value) {
204 char buf[2];
205 if (isLittleEndian())
206 StringLE16(buf, Value);
207 else
208 StringBE16(buf, Value);
Eli Bendersky84b2a792012-12-07 22:06:56 +0000209 F.getContents().append(&buf[0], &buf[2]);
Rafael Espindola29abd972011-12-22 03:38:00 +0000210 }
211
212 void String32(MCDataFragment &F, uint32_t Value) {
213 char buf[4];
214 if (isLittleEndian())
215 StringLE32(buf, Value);
216 else
217 StringBE32(buf, Value);
Eli Bendersky84b2a792012-12-07 22:06:56 +0000218 F.getContents().append(&buf[0], &buf[4]);
Rafael Espindola29abd972011-12-22 03:38:00 +0000219 }
220
221 void String64(MCDataFragment &F, uint64_t Value) {
222 char buf[8];
223 if (isLittleEndian())
224 StringLE64(buf, Value);
225 else
226 StringBE64(buf, Value);
Eli Bendersky84b2a792012-12-07 22:06:56 +0000227 F.getContents().append(&buf[0], &buf[8]);
Rafael Espindola29abd972011-12-22 03:38:00 +0000228 }
229
Jack Carter1bd90ff2013-01-30 02:09:52 +0000230 void WriteHeader(const MCAssembler &Asm,
231 uint64_t SectionDataSize,
Rafael Espindola29abd972011-12-22 03:38:00 +0000232 unsigned NumberOfSections);
233
234 void WriteSymbolEntry(MCDataFragment *SymtabF,
235 MCDataFragment *ShndxF,
236 uint64_t name, uint8_t info,
237 uint64_t value, uint64_t size,
238 uint8_t other, uint32_t shndx,
239 bool Reserved);
240
241 void WriteSymbol(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
242 ELFSymbolData &MSD,
243 const MCAsmLayout &Layout);
244
245 typedef DenseMap<const MCSectionELF*, uint32_t> SectionIndexMapTy;
246 void WriteSymbolTable(MCDataFragment *SymtabF,
247 MCDataFragment *ShndxF,
248 const MCAssembler &Asm,
249 const MCAsmLayout &Layout,
250 const SectionIndexMapTy &SectionIndexMap);
251
252 virtual void RecordRelocation(const MCAssembler &Asm,
253 const MCAsmLayout &Layout,
254 const MCFragment *Fragment,
255 const MCFixup &Fixup,
256 MCValue Target, uint64_t &FixedValue);
257
258 uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm,
259 const MCSymbol *S);
260
261 // Map from a group section to the signature symbol
262 typedef DenseMap<const MCSectionELF*, const MCSymbol*> GroupMapTy;
263 // Map from a signature symbol to the group section
264 typedef DenseMap<const MCSymbol*, const MCSectionELF*> RevGroupMapTy;
265 // Map from a section to the section with the relocations
266 typedef DenseMap<const MCSectionELF*, const MCSectionELF*> RelMapTy;
267 // Map from a section to its offset
268 typedef DenseMap<const MCSectionELF*, uint64_t> SectionOffsetMapTy;
269
270 /// ComputeSymbolTable - Compute the symbol table data
271 ///
Rafael Espindola073ee7d2012-08-27 16:04:24 +0000272 /// \param Asm - The assembler.
273 /// \param SectionIndexMap - Maps a section to its index.
274 /// \param RevGroupMap - Maps a signature symbol to the group section.
275 /// \param NumRegularSections - Number of non-relocation sections.
Rafael Espindola29abd972011-12-22 03:38:00 +0000276 void ComputeSymbolTable(MCAssembler &Asm,
277 const SectionIndexMapTy &SectionIndexMap,
278 RevGroupMapTy RevGroupMap,
279 unsigned NumRegularSections);
280
281 void ComputeIndexMap(MCAssembler &Asm,
282 SectionIndexMapTy &SectionIndexMap,
283 const RelMapTy &RelMap);
284
285 void CreateRelocationSections(MCAssembler &Asm, MCAsmLayout &Layout,
286 RelMapTy &RelMap);
287
288 void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout,
289 const RelMapTy &RelMap);
290
291 void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout,
292 SectionIndexMapTy &SectionIndexMap,
293 const RelMapTy &RelMap);
294
295 // Create the sections that show up in the symbol table. Currently
296 // those are the .note.GNU-stack section and the group sections.
297 void CreateIndexedSections(MCAssembler &Asm, MCAsmLayout &Layout,
298 GroupMapTy &GroupMap,
299 RevGroupMapTy &RevGroupMap,
300 SectionIndexMapTy &SectionIndexMap,
301 const RelMapTy &RelMap);
302
303 virtual void ExecutePostLayoutBinding(MCAssembler &Asm,
304 const MCAsmLayout &Layout);
305
306 void WriteSectionHeader(MCAssembler &Asm, const GroupMapTy &GroupMap,
307 const MCAsmLayout &Layout,
308 const SectionIndexMapTy &SectionIndexMap,
309 const SectionOffsetMapTy &SectionOffsetMap);
310
311 void ComputeSectionOrder(MCAssembler &Asm,
312 std::vector<const MCSectionELF*> &Sections);
313
314 void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
315 uint64_t Address, uint64_t Offset,
316 uint64_t Size, uint32_t Link, uint32_t Info,
317 uint64_t Alignment, uint64_t EntrySize);
318
319 void WriteRelocationsFragment(const MCAssembler &Asm,
320 MCDataFragment *F,
321 const MCSectionData *SD);
322
323 virtual bool
324 IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
325 const MCSymbolData &DataA,
326 const MCFragment &FB,
327 bool InSet,
328 bool IsPCRel) const;
329
330 virtual void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout);
331 void WriteSection(MCAssembler &Asm,
332 const SectionIndexMapTy &SectionIndexMap,
333 uint32_t GroupSymbolIndex,
334 uint64_t Offset, uint64_t Size, uint64_t Alignment,
335 const MCSectionELF &Section);
336 };
337}
338
Jan Sjödin30a52de2011-02-28 21:45:04 +0000339bool ELFObjectWriter::isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) {
340 const MCFixupKindInfo &FKI =
341 Asm.getBackend().getFixupKindInfo((MCFixupKind) Kind);
342
343 return FKI.Flags & MCFixupKindInfo::FKF_IsPCRel;
344}
345
346bool ELFObjectWriter::RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant) {
347 switch (Variant) {
348 default:
349 return false;
350 case MCSymbolRefExpr::VK_GOT:
351 case MCSymbolRefExpr::VK_PLT:
352 case MCSymbolRefExpr::VK_GOTPCREL:
Rafael Espindola940a0ee2011-06-05 01:20:06 +0000353 case MCSymbolRefExpr::VK_GOTOFF:
Jan Sjödin30a52de2011-02-28 21:45:04 +0000354 case MCSymbolRefExpr::VK_TPOFF:
355 case MCSymbolRefExpr::VK_TLSGD:
356 case MCSymbolRefExpr::VK_GOTTPOFF:
357 case MCSymbolRefExpr::VK_INDNTPOFF:
358 case MCSymbolRefExpr::VK_NTPOFF:
359 case MCSymbolRefExpr::VK_GOTNTPOFF:
360 case MCSymbolRefExpr::VK_TLSLDM:
361 case MCSymbolRefExpr::VK_DTPOFF:
362 case MCSymbolRefExpr::VK_TLSLD:
363 return true;
364 }
365}
366
Jason W Kim96f4c012010-11-15 16:18:39 +0000367ELFObjectWriter::~ELFObjectWriter()
368{}
369
Matt Fleming6c1ad482010-08-16 18:57:57 +0000370// Emit the ELF header.
Jack Carter1bd90ff2013-01-30 02:09:52 +0000371void ELFObjectWriter::WriteHeader(const MCAssembler &Asm,
372 uint64_t SectionDataSize,
Daniel Dunbarfe0c28f2010-11-13 07:33:40 +0000373 unsigned NumberOfSections) {
Matt Fleming6c1ad482010-08-16 18:57:57 +0000374 // ELF Header
375 // ----------
376 //
377 // Note
378 // ----
379 // emitWord method behaves differently for ELF32 and ELF64, writing
380 // 4 bytes in the former and 8 in the latter.
381
382 Write8(0x7f); // e_ident[EI_MAG0]
383 Write8('E'); // e_ident[EI_MAG1]
384 Write8('L'); // e_ident[EI_MAG2]
385 Write8('F'); // e_ident[EI_MAG3]
386
Rafael Espindolafdaae0d2010-12-18 03:27:34 +0000387 Write8(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
Matt Fleming6c1ad482010-08-16 18:57:57 +0000388
389 // e_ident[EI_DATA]
Daniel Dunbarfe0c28f2010-11-13 07:33:40 +0000390 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000391
392 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky3b727f52010-09-09 17:57:50 +0000393 // e_ident[EI_OSABI]
Rafael Espindola1ad40952011-12-21 17:00:36 +0000394 Write8(TargetObjectWriter->getOSABI());
Matt Fleming6c1ad482010-08-16 18:57:57 +0000395 Write8(0); // e_ident[EI_ABIVERSION]
396
397 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
398
399 Write16(ELF::ET_REL); // e_type
400
Rafael Espindolafdaae0d2010-12-18 03:27:34 +0000401 Write16(TargetObjectWriter->getEMachine()); // e_machine = target
Matt Fleming6c1ad482010-08-16 18:57:57 +0000402
403 Write32(ELF::EV_CURRENT); // e_version
404 WriteWord(0); // e_entry, no entry point in .o file
405 WriteWord(0); // e_phoff, no program header for .o
Rafael Espindolafdaae0d2010-12-18 03:27:34 +0000406 WriteWord(SectionDataSize + (is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
Benjamin Kramer896bd7e2010-08-17 17:02:29 +0000407 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming6c1ad482010-08-16 18:57:57 +0000408
Jason W Kim4761fba2011-02-04 21:41:11 +0000409 // e_flags = whatever the target wants
Jack Carter1bd90ff2013-01-30 02:09:52 +0000410 Write32(Asm.getELFHeaderEFlags());
Matt Fleming6c1ad482010-08-16 18:57:57 +0000411
412 // e_ehsize = ELF header size
Rafael Espindolafdaae0d2010-12-18 03:27:34 +0000413 Write16(is64Bit() ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
Matt Fleming6c1ad482010-08-16 18:57:57 +0000414
415 Write16(0); // e_phentsize = prog header entry size
416 Write16(0); // e_phnum = # prog header entries = 0
417
418 // e_shentsize = Section header entry size
Rafael Espindolafdaae0d2010-12-18 03:27:34 +0000419 Write16(is64Bit() ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
Matt Fleming6c1ad482010-08-16 18:57:57 +0000420
421 // e_shnum = # of section header ents
Rafael Espindola3fe87a12010-10-31 00:16:26 +0000422 if (NumberOfSections >= ELF::SHN_LORESERVE)
Nick Lewycky4eb11432011-10-07 20:56:23 +0000423 Write16(ELF::SHN_UNDEF);
Rafael Espindola3fe87a12010-10-31 00:16:26 +0000424 else
425 Write16(NumberOfSections);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000426
427 // e_shstrndx = Section # of '.shstrtab'
Nick Lewycky8b02d362011-10-07 20:58:24 +0000428 if (ShstrtabIndex >= ELF::SHN_LORESERVE)
Rafael Espindola3fe87a12010-10-31 00:16:26 +0000429 Write16(ELF::SHN_XINDEX);
430 else
431 Write16(ShstrtabIndex);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000432}
433
Daniel Dunbarfe0c28f2010-11-13 07:33:40 +0000434void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
435 MCDataFragment *ShndxF,
436 uint64_t name,
437 uint8_t info, uint64_t value,
438 uint64_t size, uint8_t other,
439 uint32_t shndx,
440 bool Reserved) {
Rafael Espindola3fe87a12010-10-31 00:16:26 +0000441 if (ShndxF) {
Rafael Espindola3fe87a12010-10-31 00:16:26 +0000442 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
Rafael Espindola51d68332010-11-10 20:02:59 +0000443 String32(*ShndxF, shndx);
Rafael Espindola3fe87a12010-10-31 00:16:26 +0000444 else
Rafael Espindola51d68332010-11-10 20:02:59 +0000445 String32(*ShndxF, 0);
Rafael Espindola3fe87a12010-10-31 00:16:26 +0000446 }
447
Rafael Espindola51d68332010-11-10 20:02:59 +0000448 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
449 uint16_t(ELF::SHN_XINDEX) : shndx;
450
Rafael Espindolafdaae0d2010-12-18 03:27:34 +0000451 if (is64Bit()) {
Rafael Espindola51d68332010-11-10 20:02:59 +0000452 String32(*SymtabF, name); // st_name
453 String8(*SymtabF, info); // st_info
454 String8(*SymtabF, other); // st_other
455 String16(*SymtabF, Index); // st_shndx
456 String64(*SymtabF, value); // st_value
457 String64(*SymtabF, size); // st_size
Matt Fleming6c1ad482010-08-16 18:57:57 +0000458 } else {
Rafael Espindola51d68332010-11-10 20:02:59 +0000459 String32(*SymtabF, name); // st_name
460 String32(*SymtabF, value); // st_value
461 String32(*SymtabF, size); // st_size
462 String8(*SymtabF, info); // st_info
463 String8(*SymtabF, other); // st_other
464 String16(*SymtabF, Index); // st_shndx
Matt Fleming6c1ad482010-08-16 18:57:57 +0000465 }
466}
467
Jan Sjödin30a52de2011-02-28 21:45:04 +0000468uint64_t ELFObjectWriter::SymbolValue(MCSymbolData &Data,
469 const MCAsmLayout &Layout) {
Rafael Espindola9735f4f2010-09-27 21:23:02 +0000470 if (Data.isCommon() && Data.isExternal())
471 return Data.getCommonAlignment();
472
473 const MCSymbol &Symbol = Data.getSymbol();
Roman Divacky55184dd2010-12-20 21:14:39 +0000474
475 if (Symbol.isAbsolute() && Symbol.isVariable()) {
476 if (const MCExpr *Value = Symbol.getVariableValue()) {
477 int64_t IntValue;
478 if (Value->EvaluateAsAbsolute(IntValue, Layout))
Jim Grosbach46be3012011-12-06 00:13:09 +0000479 return (uint64_t)IntValue;
Roman Divacky55184dd2010-12-20 21:14:39 +0000480 }
481 }
482
Rafael Espindola9735f4f2010-09-27 21:23:02 +0000483 if (!Symbol.isInSection())
484 return 0;
485
Rafael Espindolae90c1cb2011-05-16 16:17:21 +0000486
487 if (Data.getFragment()) {
488 if (Data.getFlags() & ELF_Other_ThumbFunc)
489 return Layout.getSymbolOffset(&Data)+1;
490 else
491 return Layout.getSymbolOffset(&Data);
492 }
Rafael Espindola9735f4f2010-09-27 21:23:02 +0000493
494 return 0;
495}
496
Rafael Espindola93e3cf02010-12-07 00:27:36 +0000497void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
498 const MCAsmLayout &Layout) {
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000499 // The presence of symbol versions causes undefined symbols and
500 // versions declared with @@@ to be renamed.
501
502 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
503 ie = Asm.symbol_end(); it != ie; ++it) {
504 const MCSymbol &Alias = it->getSymbol();
Rafael Espindola8c3039b2010-11-15 16:33:49 +0000505 const MCSymbol &Symbol = Alias.AliasedSymbol();
Rafael Espindola6cd76e62010-10-28 18:33:03 +0000506 MCSymbolData &SD = Asm.getSymbolData(Symbol);
507
Rafael Espindola6cd76e62010-10-28 18:33:03 +0000508 // Not an alias.
509 if (&Symbol == &Alias)
510 continue;
511
Benjamin Kramer14807272010-10-27 19:53:52 +0000512 StringRef AliasName = Alias.getName();
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000513 size_t Pos = AliasName.find('@');
514 if (Pos == StringRef::npos)
515 continue;
516
Rafael Espindola6cd76e62010-10-28 18:33:03 +0000517 // Aliases defined with .symvar copy the binding from the symbol they alias.
518 // This is the first place we are able to copy this information.
519 it->setExternal(SD.isExternal());
Jan Sjödin30a52de2011-02-28 21:45:04 +0000520 MCELF::SetBinding(*it, MCELF::GetBinding(SD));
Rafael Espindola6cd76e62010-10-28 18:33:03 +0000521
Benjamin Kramer14807272010-10-27 19:53:52 +0000522 StringRef Rest = AliasName.substr(Pos);
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000523 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
524 continue;
525
Rafael Espindola26496e62010-10-27 17:56:18 +0000526 // FIXME: produce a better error message.
527 if (Symbol.isUndefined() && Rest.startswith("@@") &&
528 !Rest.startswith("@@@"))
529 report_fatal_error("A @@ version cannot be undefined");
530
Benjamin Kramer14807272010-10-27 19:53:52 +0000531 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000532 }
533}
534
Daniel Dunbarfe0c28f2010-11-13 07:33:40 +0000535void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
536 MCDataFragment *ShndxF,
537 ELFSymbolData &MSD,
538 const MCAsmLayout &Layout) {
Rafael Espindola5f2d6a52010-10-06 21:02:29 +0000539 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindola84378f02010-10-15 18:25:33 +0000540 MCSymbolData &Data =
Rafael Espindola8c3039b2010-11-15 16:33:49 +0000541 Layout.getAssembler().getSymbolData(OrigData.getSymbol().AliasedSymbol());
Rafael Espindola5f2d6a52010-10-06 21:02:29 +0000542
Rafael Espindola3fe87a12010-10-31 00:16:26 +0000543 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
544 Data.getSymbol().isVariable();
545
Jack Carter2f8d9d92013-02-19 21:57:35 +0000546 // Binding and Type share the same byte as upper and lower nibbles
Jan Sjödin30a52de2011-02-28 21:45:04 +0000547 uint8_t Binding = MCELF::GetBinding(OrigData);
Jan Sjödin30a52de2011-02-28 21:45:04 +0000548 uint8_t Type = MCELF::GetType(Data);
Rafael Espindola5f2d6a52010-10-06 21:02:29 +0000549 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
Jack Carter2f8d9d92013-02-19 21:57:35 +0000550
Joerg Sonnenbergerfc184732013-10-29 01:06:17 +0000551 // Other and Visibility share the same byte with Visibility using the lower
Jack Carter2f8d9d92013-02-19 21:57:35 +0000552 // 2 bits
553 uint8_t Visibility = MCELF::GetVisibility(OrigData);
Logan Chienee365952013-12-05 00:34:11 +0000554 uint8_t Other = MCELF::getOther(OrigData) << (ELF_STO_Shift - ELF_STV_Shift);
Jack Carter2f8d9d92013-02-19 21:57:35 +0000555 Other |= Visibility;
Rafael Espindola5f2d6a52010-10-06 21:02:29 +0000556
Rafael Espindola9735f4f2010-09-27 21:23:02 +0000557 uint64_t Value = SymbolValue(Data, Layout);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000558 uint64_t Size = 0;
Matt Fleming6c1ad482010-08-16 18:57:57 +0000559
Rafael Espindolaf0591c12010-09-21 00:24:38 +0000560 assert(!(Data.isCommon() && !Data.isExternal()));
561
Rafael Espindola73c0ae72010-12-22 16:03:00 +0000562 const MCExpr *ESize = Data.getSize();
563 if (ESize) {
564 int64_t Res;
565 if (!ESize->EvaluateAsAbsolute(Res, Layout))
566 report_fatal_error("Size expression must be absolute.");
567 Size = Res;
Matt Fleming6c1ad482010-08-16 18:57:57 +0000568 }
569
570 // Write out the symbol table entry
Rafael Espindola3fe87a12010-10-31 00:16:26 +0000571 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
572 Size, Other, MSD.SectionIndex, IsReserved);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000573}
574
Daniel Dunbarfe0c28f2010-11-13 07:33:40 +0000575void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
576 MCDataFragment *ShndxF,
577 const MCAssembler &Asm,
578 const MCAsmLayout &Layout,
Bruno Cardoso Lopesc9473e92011-11-04 22:24:36 +0000579 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming6c1ad482010-08-16 18:57:57 +0000580 // The string table must be emitted first because we need the index
581 // into the string table for all the symbol names.
582 assert(StringTable.size() && "Missing string table");
583
584 // FIXME: Make sure the start of the symbol table is aligned.
585
586 // The first entry is the undefined symbol entry.
Rafael Espindola3fe87a12010-10-31 00:16:26 +0000587 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000588
Joerg Sonnenbergerfc184732013-10-29 01:06:17 +0000589 for (unsigned i = 0, e = FileSymbolData.size(); i != e; ++i) {
590 WriteSymbolEntry(SymtabF, ShndxF, FileSymbolData[i],
591 ELF::STT_FILE | ELF::STB_LOCAL, 0, 0,
592 ELF::STV_DEFAULT, ELF::SHN_ABS, true);
593 }
594
Matt Fleming6c1ad482010-08-16 18:57:57 +0000595 // Write the symbol table entries.
Joerg Sonnenbergerfc184732013-10-29 01:06:17 +0000596 LastLocalSymbolIndex = FileSymbolData.size() + LocalSymbolData.size() + 1;
597
Matt Fleming6c1ad482010-08-16 18:57:57 +0000598 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
599 ELFSymbolData &MSD = LocalSymbolData[i];
Rafael Espindola3fe87a12010-10-31 00:16:26 +0000600 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000601 }
602
Rafael Espindola44bf2662010-09-16 19:46:31 +0000603 // Write out a symbol table entry for each regular section.
Rafael Espindola18014102010-11-10 22:16:43 +0000604 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
605 ++i) {
Eli Friedman1fe0d532010-08-16 21:17:09 +0000606 const MCSectionELF &Section =
Rafael Espindola18014102010-11-10 22:16:43 +0000607 static_cast<const MCSectionELF&>(i->getSection());
608 if (Section.getType() == ELF::SHT_RELA ||
609 Section.getType() == ELF::SHT_REL ||
610 Section.getType() == ELF::SHT_STRTAB ||
Nick Lewycky133a1682011-10-07 23:29:53 +0000611 Section.getType() == ELF::SHT_SYMTAB ||
612 Section.getType() == ELF::SHT_SYMTAB_SHNDX)
Eli Friedman1fe0d532010-08-16 21:17:09 +0000613 continue;
Rafael Espindola3fe87a12010-10-31 00:16:26 +0000614 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
Bruno Cardoso Lopesc9473e92011-11-04 22:24:36 +0000615 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section),
616 false);
Eli Friedman1fe0d532010-08-16 21:17:09 +0000617 LastLocalSymbolIndex++;
618 }
Matt Fleming6c1ad482010-08-16 18:57:57 +0000619
620 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
621 ELFSymbolData &MSD = ExternalSymbolData[i];
622 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola83b2a332010-10-06 16:47:31 +0000623 assert(((Data.getFlags() & ELF_STB_Global) ||
624 (Data.getFlags() & ELF_STB_Weak)) &&
625 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola3fe87a12010-10-31 00:16:26 +0000626 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Jan Sjödin30a52de2011-02-28 21:45:04 +0000627 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming6c1ad482010-08-16 18:57:57 +0000628 LastLocalSymbolIndex++;
629 }
630
631 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
632 ELFSymbolData &MSD = UndefinedSymbolData[i];
633 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3fe87a12010-10-31 00:16:26 +0000634 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Jan Sjödin30a52de2011-02-28 21:45:04 +0000635 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming6c1ad482010-08-16 18:57:57 +0000636 LastLocalSymbolIndex++;
637 }
638}
639
Rafael Espindola240028d2010-11-14 23:53:26 +0000640const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm,
641 const MCValue &Target,
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000642 const MCFragment &F,
Jason W Kimc09e7262011-05-11 22:53:06 +0000643 const MCFixup &Fixup,
644 bool IsPCRel) const {
Rafael Espindola240028d2010-11-14 23:53:26 +0000645 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
Rafael Espindola8c3039b2010-11-15 16:33:49 +0000646 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
647 const MCSymbol *Renamed = Renames.lookup(&Symbol);
648 const MCSymbolData &SD = Asm.getSymbolData(Symbol);
Rafael Espindola240028d2010-11-14 23:53:26 +0000649
Rafael Espindola8c3039b2010-11-15 16:33:49 +0000650 if (ASymbol.isUndefined()) {
651 if (Renamed)
652 return Renamed;
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +0000653 return undefinedExplicitRelSym(Target, Fixup, IsPCRel);
Rafael Espindola240028d2010-11-14 23:53:26 +0000654 }
Rafael Espindola240028d2010-11-14 23:53:26 +0000655
Rafael Espindola8c3039b2010-11-15 16:33:49 +0000656 if (SD.isExternal()) {
657 if (Renamed)
658 return Renamed;
659 return &Symbol;
660 }
Rafael Espindola75d65b92010-09-25 05:42:19 +0000661
Rafael Espindolaf987d5e2010-09-30 20:18:35 +0000662 const MCSectionELF &Section =
Rafael Espindola8c3039b2010-11-15 16:33:49 +0000663 static_cast<const MCSectionELF&>(ASymbol.getSection());
Rafael Espindola9f75d5d2010-11-24 21:57:39 +0000664 const SectionKind secKind = Section.getKind();
Rafael Espindolaf987d5e2010-09-30 20:18:35 +0000665
Rafael Espindola9f75d5d2010-11-24 21:57:39 +0000666 if (secKind.isBSS())
Jason W Kimc09e7262011-05-11 22:53:06 +0000667 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
Rafael Espindolaf987d5e2010-09-30 20:18:35 +0000668
Rafael Espindola9f75d5d2010-11-24 21:57:39 +0000669 if (secKind.isThreadLocal()) {
670 if (Renamed)
671 return Renamed;
672 return &Symbol;
673 }
674
Rafael Espindola8f3d2c92010-10-06 16:23:36 +0000675 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindolad7565c32010-10-05 23:57:26 +0000676 const MCSectionELF &Sec2 =
677 static_cast<const MCSectionELF&>(F.getParent()->getSection());
678
Rafael Espindola8f3d2c92010-10-06 16:23:36 +0000679 if (&Sec2 != &Section &&
Rafael Espindola4464e082010-10-18 16:38:04 +0000680 (Kind == MCSymbolRefExpr::VK_PLT ||
681 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
Rafael Espindola9f75d5d2010-11-24 21:57:39 +0000682 Kind == MCSymbolRefExpr::VK_GOTOFF)) {
Rafael Espindola8c3039b2010-11-15 16:33:49 +0000683 if (Renamed)
684 return Renamed;
685 return &Symbol;
686 }
Rafael Espindolad7565c32010-10-05 23:57:26 +0000687
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000688 if (Section.getFlags() & ELF::SHF_MERGE) {
Rafael Espindola8c3039b2010-11-15 16:33:49 +0000689 if (Target.getConstant() == 0)
Jason W Kimc09e7262011-05-11 22:53:06 +0000690 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
Rafael Espindola8c3039b2010-11-15 16:33:49 +0000691 if (Renamed)
692 return Renamed;
693 return &Symbol;
Rafael Espindola240028d2010-11-14 23:53:26 +0000694 }
Rafael Espindola4464e082010-10-18 16:38:04 +0000695
Jason W Kimc09e7262011-05-11 22:53:06 +0000696 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
697
Rafael Espindola75d65b92010-09-25 05:42:19 +0000698}
699
Matt Fleming6c1ad482010-08-16 18:57:57 +0000700
Jason W Kim495c2bb2010-12-06 21:57:34 +0000701void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
702 const MCAsmLayout &Layout,
703 const MCFragment *Fragment,
704 const MCFixup &Fixup,
705 MCValue Target,
706 uint64_t &FixedValue) {
707 int64_t Addend = 0;
708 int Index = 0;
709 int64_t Value = Target.getConstant();
710 const MCSymbol *RelocSymbol = NULL;
711
Rafael Espindoladf633352010-12-17 07:28:17 +0000712 bool IsPCRel = isFixupKindPCRel(Asm, Fixup.getKind());
Jason W Kim495c2bb2010-12-06 21:57:34 +0000713 if (!Target.isAbsolute()) {
714 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
715 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
Jason W Kimc09e7262011-05-11 22:53:06 +0000716 RelocSymbol = SymbolToReloc(Asm, Target, *Fragment, Fixup, IsPCRel);
Jason W Kim495c2bb2010-12-06 21:57:34 +0000717
718 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
719 const MCSymbol &SymbolB = RefB->getSymbol();
720 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
721 IsPCRel = true;
722
David Majnemera45a1762014-01-06 07:39:46 +0000723 if (!SDB.getFragment())
724 Asm.getContext().FatalError(
725 Fixup.getLoc(),
726 Twine("symbol '") + SymbolB.getName() +
727 "' can not be undefined in a subtraction expression");
728
Jason W Kim495c2bb2010-12-06 21:57:34 +0000729 // Offset of the symbol in the section
730 int64_t a = Layout.getSymbolOffset(&SDB);
731
Bruno Cardoso Lopesc9473e92011-11-04 22:24:36 +0000732 // Offset of the relocation in the section
Jason W Kim495c2bb2010-12-06 21:57:34 +0000733 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
734 Value += b - a;
735 }
736
737 if (!RelocSymbol) {
738 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
739 MCFragment *F = SD.getFragment();
740
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +0000741 if (F) {
742 Index = F->getParent()->getOrdinal() + 1;
743 // Offset of the symbol in the section
744 Value += Layout.getSymbolOffset(&SD);
745 } else {
746 Index = 0;
747 }
Jason W Kim495c2bb2010-12-06 21:57:34 +0000748 } else {
749 if (Asm.getSymbolData(Symbol).getFlags() & ELF_Other_Weakref)
750 WeakrefUsedInReloc.insert(RelocSymbol);
751 else
752 UsedInReloc.insert(RelocSymbol);
753 Index = -1;
754 }
755 Addend = Value;
Michael Liaod6f31682012-10-16 19:49:51 +0000756 if (hasRelocationAddend())
Jason W Kim495c2bb2010-12-06 21:57:34 +0000757 Value = 0;
758 }
759
760 FixedValue = Value;
761 unsigned Type = GetRelocType(Target, Fixup, IsPCRel,
762 (RelocSymbol != 0), Addend);
Rafael Espindola9e252bf2011-12-21 14:26:29 +0000763 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
764 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
765 if (RelocNeedsGOT(Modifier))
766 NeedsGOT = true;
Jan Sjödin30a52de2011-02-28 21:45:04 +0000767
Jason W Kim495c2bb2010-12-06 21:57:34 +0000768 uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
769 Fixup.getOffset();
Roman Divackydfbecd12011-08-04 19:08:19 +0000770
Rafael Espindolafdaae0d2010-12-18 03:27:34 +0000771 if (!hasRelocationAddend())
772 Addend = 0;
Rafael Espindolad7facaf2011-08-04 13:05:26 +0000773
774 if (is64Bit())
775 assert(isInt<64>(Addend));
776 else
777 assert(isInt<32>(Addend));
778
Akira Hatanaka64ad2cf2012-03-23 23:06:45 +0000779 ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend, Fixup);
Jason W Kim495c2bb2010-12-06 21:57:34 +0000780 Relocations[Fragment->getParent()].push_back(ERE);
781}
782
783
Benjamin Kramer86511dc2010-08-23 21:19:37 +0000784uint64_t
Daniel Dunbarfe0c28f2010-11-13 07:33:40 +0000785ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
786 const MCSymbol *S) {
Benjamin Kramer37b384c2010-08-25 20:09:43 +0000787 MCSymbolData &SD = Asm.getSymbolData(*S);
Rafael Espindola0e3decf2010-11-14 03:12:24 +0000788 return SD.getIndex();
Matt Fleming6c1ad482010-08-16 18:57:57 +0000789}
790
Jan Sjödin30a52de2011-02-28 21:45:04 +0000791bool ELFObjectWriter::isInSymtab(const MCAssembler &Asm,
792 const MCSymbolData &Data,
793 bool Used, bool Renamed) {
Rafael Espindola16145972010-11-01 14:28:48 +0000794 if (Data.getFlags() & ELF_Other_Weakref)
795 return false;
796
Rafael Espindolaee8d1512010-10-19 19:31:37 +0000797 if (Used)
798 return true;
799
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000800 if (Renamed)
801 return false;
802
Rafael Espindolab1d07892010-10-05 18:01:23 +0000803 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindolaa5efd6a2010-10-27 14:44:52 +0000804
Rafael Espindola45834a02010-10-29 23:09:31 +0000805 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
806 return true;
807
Rafael Espindola8c3039b2010-11-15 16:33:49 +0000808 const MCSymbol &A = Symbol.AliasedSymbol();
Rafael Espindola9e18e962011-02-23 20:22:07 +0000809 if (Symbol.isVariable() && !A.isVariable() && A.isUndefined())
810 return false;
811
Jan Sjödin30a52de2011-02-28 21:45:04 +0000812 bool IsGlobal = MCELF::GetBinding(Data) == ELF::STB_GLOBAL;
Rafael Espindola9e18e962011-02-23 20:22:07 +0000813 if (!Symbol.isVariable() && Symbol.isUndefined() && !IsGlobal)
Rafael Espindolaa5efd6a2010-10-27 14:44:52 +0000814 return false;
815
Rafael Espindolab1d07892010-10-05 18:01:23 +0000816 if (!Asm.isSymbolLinkerVisible(Symbol) && !Symbol.isUndefined())
817 return false;
818
Rafael Espindolaee8d1512010-10-19 19:31:37 +0000819 if (Symbol.isTemporary())
Rafael Espindolab1d07892010-10-05 18:01:23 +0000820 return false;
821
822 return true;
823}
824
Jan Sjödin30a52de2011-02-28 21:45:04 +0000825bool ELFObjectWriter::isLocal(const MCSymbolData &Data, bool isSignature,
826 bool isUsedInReloc) {
Rafael Espindolab1d07892010-10-05 18:01:23 +0000827 if (Data.isExternal())
828 return false;
829
830 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindola8c3039b2010-11-15 16:33:49 +0000831 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Rafael Espindola7d0ba342010-11-14 04:17:37 +0000832
833 if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
834 if (isSignature && !isUsedInReloc)
835 return true;
836
Rafael Espindolab1d07892010-10-05 18:01:23 +0000837 return false;
Rafael Espindola7d0ba342010-11-14 04:17:37 +0000838 }
Rafael Espindolab1d07892010-10-05 18:01:23 +0000839
840 return true;
841}
842
Daniel Dunbarfe0c28f2010-11-13 07:33:40 +0000843void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
Rafael Espindola1557fd62011-03-20 18:44:20 +0000844 SectionIndexMapTy &SectionIndexMap,
845 const RelMapTy &RelMap) {
Rafael Espindolad6340032010-11-10 21:51:05 +0000846 unsigned Index = 1;
847 for (MCAssembler::iterator it = Asm.begin(),
848 ie = Asm.end(); it != ie; ++it) {
849 const MCSectionELF &Section =
850 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000851 if (Section.getType() != ELF::SHT_GROUP)
852 continue;
853 SectionIndexMap[&Section] = Index++;
854 }
855
856 for (MCAssembler::iterator it = Asm.begin(),
857 ie = Asm.end(); it != ie; ++it) {
858 const MCSectionELF &Section =
859 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola1557fd62011-03-20 18:44:20 +0000860 if (Section.getType() == ELF::SHT_GROUP ||
861 Section.getType() == ELF::SHT_REL ||
862 Section.getType() == ELF::SHT_RELA)
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000863 continue;
Rafael Espindolad6340032010-11-10 21:51:05 +0000864 SectionIndexMap[&Section] = Index++;
Rafael Espindola1557fd62011-03-20 18:44:20 +0000865 const MCSectionELF *RelSection = RelMap.lookup(&Section);
866 if (RelSection)
867 SectionIndexMap[RelSection] = Index++;
Rafael Espindolad6340032010-11-10 21:51:05 +0000868 }
869}
870
Daniel Dunbarfe0c28f2010-11-13 07:33:40 +0000871void ELFObjectWriter::ComputeSymbolTable(MCAssembler &Asm,
Rafael Espindola7d0ba342010-11-14 04:17:37 +0000872 const SectionIndexMapTy &SectionIndexMap,
Rafael Espindola1557fd62011-03-20 18:44:20 +0000873 RevGroupMapTy RevGroupMap,
874 unsigned NumRegularSections) {
Rafael Espindolad03e81d2010-10-05 15:48:37 +0000875 // FIXME: Is this the correct place to do this?
Rafael Espindola940a0ee2011-06-05 01:20:06 +0000876 // FIXME: Why is an undefined reference to _GLOBAL_OFFSET_TABLE_ needed?
Rafael Espindolad03e81d2010-10-05 15:48:37 +0000877 if (NeedsGOT) {
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000878 StringRef Name = "_GLOBAL_OFFSET_TABLE_";
Rafael Espindolad03e81d2010-10-05 15:48:37 +0000879 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
880 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
881 Data.setExternal(true);
Jan Sjödin30a52de2011-02-28 21:45:04 +0000882 MCELF::SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindolad03e81d2010-10-05 15:48:37 +0000883 }
884
Matt Fleming6c1ad482010-08-16 18:57:57 +0000885 // Index 0 is always the empty string.
886 StringMap<uint64_t> StringIndexMap;
887 StringTable += '\x00';
888
Rafael Espindolab80875e2011-04-07 23:21:52 +0000889 // FIXME: We could optimize suffixes in strtab in the same way we
890 // optimize them in shstrtab.
891
Joerg Sonnenbergerfc184732013-10-29 01:06:17 +0000892 for (MCAssembler::const_file_name_iterator it = Asm.file_names_begin(),
893 ie = Asm.file_names_end();
894 it != ie;
895 ++it) {
896 StringRef Name = *it;
897 uint64_t &Entry = StringIndexMap[Name];
898 if (!Entry) {
899 Entry = StringTable.size();
900 StringTable += Name;
901 StringTable += '\x00';
902 }
903 FileSymbolData.push_back(Entry);
904 }
905
Rafael Espindolabee6e9f2010-10-14 16:34:44 +0000906 // Add the data for the symbols.
Matt Fleming6c1ad482010-08-16 18:57:57 +0000907 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
908 ie = Asm.symbol_end(); it != ie; ++it) {
909 const MCSymbol &Symbol = it->getSymbol();
910
Rafael Espindola16145972010-11-01 14:28:48 +0000911 bool Used = UsedInReloc.count(&Symbol);
912 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
Rafael Espindola7d0ba342010-11-14 04:17:37 +0000913 bool isSignature = RevGroupMap.count(&Symbol);
914
915 if (!isInSymtab(Asm, *it,
916 Used || WeakrefUsed || isSignature,
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000917 Renames.count(&Symbol)))
Matt Fleming6c1ad482010-08-16 18:57:57 +0000918 continue;
919
Matt Fleming6c1ad482010-08-16 18:57:57 +0000920 ELFSymbolData MSD;
921 MSD.SymbolData = it;
Rafael Espindola8c3039b2010-11-15 16:33:49 +0000922 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Matt Fleming6c1ad482010-08-16 18:57:57 +0000923
Rafael Espindola7d0ba342010-11-14 04:17:37 +0000924 // Undefined symbols are global, but this is the first place we
925 // are able to set it.
926 bool Local = isLocal(*it, isSignature, Used);
Jan Sjödin30a52de2011-02-28 21:45:04 +0000927 if (!Local && MCELF::GetBinding(*it) == ELF::STB_LOCAL) {
Rafael Espindola7d0ba342010-11-14 04:17:37 +0000928 MCSymbolData &SD = Asm.getSymbolData(RefSymbol);
Jan Sjödin30a52de2011-02-28 21:45:04 +0000929 MCELF::SetBinding(*it, ELF::STB_GLOBAL);
930 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
Rafael Espindola7d0ba342010-11-14 04:17:37 +0000931 }
932
Rafael Espindola16145972010-11-01 14:28:48 +0000933 if (RefSymbol.isUndefined() && !Used && WeakrefUsed)
Jan Sjödin30a52de2011-02-28 21:45:04 +0000934 MCELF::SetBinding(*it, ELF::STB_WEAK);
Rafael Espindola16145972010-11-01 14:28:48 +0000935
Rafael Espindolaf0591c12010-09-21 00:24:38 +0000936 if (it->isCommon()) {
Rafael Espindolabee6e9f2010-10-14 16:34:44 +0000937 assert(!Local);
Rafael Espindolaf0591c12010-09-21 00:24:38 +0000938 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindolacc1b1682010-10-27 16:04:30 +0000939 } else if (Symbol.isAbsolute() || RefSymbol.isVariable()) {
Rafael Espindolabee6e9f2010-10-14 16:34:44 +0000940 MSD.SectionIndex = ELF::SHN_ABS;
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000941 } else if (RefSymbol.isUndefined()) {
Rafael Espindola7d0ba342010-11-14 04:17:37 +0000942 if (isSignature && !Used)
943 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
944 else
945 MSD.SectionIndex = ELF::SHN_UNDEF;
Matt Fleming6c1ad482010-08-16 18:57:57 +0000946 } else {
Rafael Espindolad6340032010-11-10 21:51:05 +0000947 const MCSectionELF &Section =
948 static_cast<const MCSectionELF&>(RefSymbol.getSection());
949 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Rafael Espindola3fe87a12010-10-31 00:16:26 +0000950 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
951 NeedsSymtabShndx = true;
Matt Fleming6c1ad482010-08-16 18:57:57 +0000952 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindolafbcf0db2010-10-15 15:39:06 +0000953 }
954
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000955 // The @@@ in symbol version is replaced with @ in undefined symbols and
956 // @@ in defined ones.
957 StringRef Name = Symbol.getName();
Benjamin Kramerdcc77322010-11-12 19:26:04 +0000958 SmallString<32> Buf;
959
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000960 size_t Pos = Name.find("@@@");
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000961 if (Pos != StringRef::npos) {
Benjamin Kramerdcc77322010-11-12 19:26:04 +0000962 Buf += Name.substr(0, Pos);
963 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
964 Buf += Name.substr(Pos + Skip);
965 Name = Buf;
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000966 }
967
Benjamin Kramerdcc77322010-11-12 19:26:04 +0000968 uint64_t &Entry = StringIndexMap[Name];
Rafael Espindolaa5efd6a2010-10-27 14:44:52 +0000969 if (!Entry) {
970 Entry = StringTable.size();
Benjamin Kramerdcc77322010-11-12 19:26:04 +0000971 StringTable += Name;
Rafael Espindolaa5efd6a2010-10-27 14:44:52 +0000972 StringTable += '\x00';
Matt Fleming6c1ad482010-08-16 18:57:57 +0000973 }
Rafael Espindolaa5efd6a2010-10-27 14:44:52 +0000974 MSD.StringIndex = Entry;
975 if (MSD.SectionIndex == ELF::SHN_UNDEF)
976 UndefinedSymbolData.push_back(MSD);
977 else if (Local)
978 LocalSymbolData.push_back(MSD);
979 else
980 ExternalSymbolData.push_back(MSD);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000981 }
982
983 // Symbols are required to be in lexicographic order.
984 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
985 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
986 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
987
988 // Set the symbol indices. Local symbols must come before all other
989 // symbols with non-local bindings.
Joerg Sonnenbergerfc184732013-10-29 01:06:17 +0000990 unsigned Index = FileSymbolData.size() + 1;
Matt Fleming6c1ad482010-08-16 18:57:57 +0000991 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
992 LocalSymbolData[i].SymbolData->setIndex(Index++);
Rafael Espindola0e3decf2010-11-14 03:12:24 +0000993
994 Index += NumRegularSections;
995
Matt Fleming6c1ad482010-08-16 18:57:57 +0000996 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
997 ExternalSymbolData[i].SymbolData->setIndex(Index++);
998 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
999 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
Nick Lewycky29e7b312011-10-11 03:54:50 +00001000
Nick Lewycky4288f9e2013-03-09 09:32:16 +00001001 if (Index >= ELF::SHN_LORESERVE)
Nick Lewycky29e7b312011-10-11 03:54:50 +00001002 NeedsSymtabShndx = true;
Matt Fleming6c1ad482010-08-16 18:57:57 +00001003}
1004
Rafael Espindola1557fd62011-03-20 18:44:20 +00001005void ELFObjectWriter::CreateRelocationSections(MCAssembler &Asm,
1006 MCAsmLayout &Layout,
1007 RelMapTy &RelMap) {
1008 for (MCAssembler::const_iterator it = Asm.begin(),
1009 ie = Asm.end(); it != ie; ++it) {
1010 const MCSectionData &SD = *it;
1011 if (Relocations[&SD].empty())
1012 continue;
1013
Matt Fleming6c1ad482010-08-16 18:57:57 +00001014 MCContext &Ctx = Asm.getContext();
Matt Fleming6c1ad482010-08-16 18:57:57 +00001015 const MCSectionELF &Section =
1016 static_cast<const MCSectionELF&>(SD.getSection());
1017
1018 const StringRef SectionName = Section.getSectionName();
Rafael Espindolafdaae0d2010-12-18 03:27:34 +00001019 std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
Matt Fleming6c1ad482010-08-16 18:57:57 +00001020 RelaSectionName += SectionName;
Benjamin Kramerfd054152010-08-17 17:56:13 +00001021
1022 unsigned EntrySize;
Rafael Espindolafdaae0d2010-12-18 03:27:34 +00001023 if (hasRelocationAddend())
1024 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
Benjamin Kramerfd054152010-08-17 17:56:13 +00001025 else
Rafael Espindolafdaae0d2010-12-18 03:27:34 +00001026 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming6c1ad482010-08-16 18:57:57 +00001027
Tim Northovera630fb02013-07-10 20:58:17 +00001028 unsigned Flags = 0;
1029 StringRef Group = "";
1030 if (Section.getFlags() & ELF::SHF_GROUP) {
David Blaikie1e412ea2013-10-22 20:34:30 +00001031 Flags = ELF::SHF_GROUP;
1032 Group = Section.getGroup()->getName();
Tim Northovera630fb02013-07-10 20:58:17 +00001033 }
1034
Rafael Espindola1557fd62011-03-20 18:44:20 +00001035 const MCSectionELF *RelaSection =
1036 Ctx.getELFSection(RelaSectionName, hasRelocationAddend() ?
Tim Northovera630fb02013-07-10 20:58:17 +00001037 ELF::SHT_RELA : ELF::SHT_REL, Flags,
Rafael Espindola1557fd62011-03-20 18:44:20 +00001038 SectionKind::getReadOnly(),
Tim Northovera630fb02013-07-10 20:58:17 +00001039 EntrySize, Group);
Rafael Espindola1557fd62011-03-20 18:44:20 +00001040 RelMap[&Section] = RelaSection;
1041 Asm.getOrCreateSectionData(*RelaSection);
1042 }
1043}
Matt Fleming6c1ad482010-08-16 18:57:57 +00001044
Rafael Espindola1557fd62011-03-20 18:44:20 +00001045void ELFObjectWriter::WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout,
1046 const RelMapTy &RelMap) {
1047 for (MCAssembler::const_iterator it = Asm.begin(),
1048 ie = Asm.end(); it != ie; ++it) {
1049 const MCSectionData &SD = *it;
1050 const MCSectionELF &Section =
1051 static_cast<const MCSectionELF&>(SD.getSection());
1052
1053 const MCSectionELF *RelaSection = RelMap.lookup(&Section);
1054 if (!RelaSection)
1055 continue;
Matt Fleming6c1ad482010-08-16 18:57:57 +00001056 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Rafael Espindolafdaae0d2010-12-18 03:27:34 +00001057 RelaSD.setAlignment(is64Bit() ? 8 : 4);
Matt Fleming6c1ad482010-08-16 18:57:57 +00001058
1059 MCDataFragment *F = new MCDataFragment(&RelaSD);
Rafael Espindola1557fd62011-03-20 18:44:20 +00001060 WriteRelocationsFragment(Asm, F, &*it);
Matt Fleming6c1ad482010-08-16 18:57:57 +00001061 }
1062}
1063
Daniel Dunbarfe0c28f2010-11-13 07:33:40 +00001064void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
1065 uint64_t Flags, uint64_t Address,
1066 uint64_t Offset, uint64_t Size,
1067 uint32_t Link, uint32_t Info,
1068 uint64_t Alignment,
1069 uint64_t EntrySize) {
Matt Fleming6c1ad482010-08-16 18:57:57 +00001070 Write32(Name); // sh_name: index into string table
1071 Write32(Type); // sh_type
1072 WriteWord(Flags); // sh_flags
1073 WriteWord(Address); // sh_addr
1074 WriteWord(Offset); // sh_offset
1075 WriteWord(Size); // sh_size
1076 Write32(Link); // sh_link
1077 Write32(Info); // sh_info
1078 WriteWord(Alignment); // sh_addralign
1079 WriteWord(EntrySize); // sh_entsize
1080}
1081
Daniel Dunbarfe0c28f2010-11-13 07:33:40 +00001082void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
1083 MCDataFragment *F,
1084 const MCSectionData *SD) {
Matt Fleming6c1ad482010-08-16 18:57:57 +00001085 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
Akira Hatanaka64ad2cf2012-03-23 23:06:45 +00001086
1087 // Sort the relocation entries. Most targets just sort by r_offset, but some
1088 // (e.g., MIPS) have additional constraints.
1089 TargetObjectWriter->sortRelocs(Asm, Relocs);
Matt Fleming6c1ad482010-08-16 18:57:57 +00001090
1091 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1092 ELFRelocationEntry entry = Relocs[e - i - 1];
1093
Rafael Espindola26cb15a2010-11-21 00:48:25 +00001094 if (!entry.Index)
1095 ;
1096 else if (entry.Index < 0)
Rafael Espindolabce26a12010-10-05 15:11:03 +00001097 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
1098 else
Joerg Sonnenbergerfc184732013-10-29 01:06:17 +00001099 entry.Index += FileSymbolData.size() + LocalSymbolData.size();
Rafael Espindolafdaae0d2010-12-18 03:27:34 +00001100 if (is64Bit()) {
Rafael Espindola51d68332010-11-10 20:02:59 +00001101 String64(*F, entry.r_offset);
Jack Carter8ad0c272012-06-27 22:28:30 +00001102 if (TargetObjectWriter->isN64()) {
1103 String32(*F, entry.Index);
Benjamin Kramer6c3c3492010-09-09 18:01:29 +00001104
Jack Carter8ad0c272012-06-27 22:28:30 +00001105 String8(*F, TargetObjectWriter->getRSsym(entry.Type));
1106 String8(*F, TargetObjectWriter->getRType3(entry.Type));
1107 String8(*F, TargetObjectWriter->getRType2(entry.Type));
1108 String8(*F, TargetObjectWriter->getRType(entry.Type));
1109 }
1110 else {
1111 struct ELF::Elf64_Rela ERE64;
1112 ERE64.setSymbolAndType(entry.Index, entry.Type);
1113 String64(*F, ERE64.r_info);
1114 }
Rafael Espindolafdaae0d2010-12-18 03:27:34 +00001115 if (hasRelocationAddend())
Rafael Espindola51d68332010-11-10 20:02:59 +00001116 String64(*F, entry.r_addend);
Benjamin Kramer6c3c3492010-09-09 18:01:29 +00001117 } else {
Rafael Espindola51d68332010-11-10 20:02:59 +00001118 String32(*F, entry.r_offset);
Benjamin Kramer6c3c3492010-09-09 18:01:29 +00001119
Rafael Espindolabce26a12010-10-05 15:11:03 +00001120 struct ELF::Elf32_Rela ERE32;
1121 ERE32.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindola51d68332010-11-10 20:02:59 +00001122 String32(*F, ERE32.r_info);
Benjamin Kramer6c3c3492010-09-09 18:01:29 +00001123
Rafael Espindolafdaae0d2010-12-18 03:27:34 +00001124 if (hasRelocationAddend())
Rafael Espindola51d68332010-11-10 20:02:59 +00001125 String32(*F, entry.r_addend);
Benjamin Kramer6c3c3492010-09-09 18:01:29 +00001126 }
Matt Fleming6c1ad482010-08-16 18:57:57 +00001127 }
1128}
1129
Benjamin Kramer8817cca2013-09-22 14:09:50 +00001130static int compareBySuffix(const MCSectionELF *const *a,
1131 const MCSectionELF *const *b) {
1132 const StringRef &NameA = (*a)->getSectionName();
1133 const StringRef &NameB = (*b)->getSectionName();
Rafael Espindolab80875e2011-04-07 23:21:52 +00001134 const unsigned sizeA = NameA.size();
1135 const unsigned sizeB = NameB.size();
1136 const unsigned len = std::min(sizeA, sizeB);
1137 for (unsigned int i = 0; i < len; ++i) {
1138 char ca = NameA[sizeA - i - 1];
1139 char cb = NameB[sizeB - i - 1];
1140 if (ca != cb)
1141 return cb - ca;
1142 }
1143
1144 return sizeB - sizeA;
1145}
1146
Daniel Dunbarfe0c28f2010-11-13 07:33:40 +00001147void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
1148 MCAsmLayout &Layout,
Rafael Espindola1557fd62011-03-20 18:44:20 +00001149 SectionIndexMapTy &SectionIndexMap,
1150 const RelMapTy &RelMap) {
Matt Fleming6c1ad482010-08-16 18:57:57 +00001151 MCContext &Ctx = Asm.getContext();
1152 MCDataFragment *F;
1153
Rafael Espindolafdaae0d2010-12-18 03:27:34 +00001154 unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
Matt Fleming6c1ad482010-08-16 18:57:57 +00001155
Rafael Espindola0e527b72010-09-22 19:04:41 +00001156 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola36ef57d2010-11-10 19:05:07 +00001157 const MCSectionELF *ShstrtabSection =
Rafael Espindola3fe87a12010-10-31 00:16:26 +00001158 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
Rafael Espindola19fa3802010-11-11 03:40:25 +00001159 SectionKind::getReadOnly());
Rafael Espindola44bf2662010-09-16 19:46:31 +00001160 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1161 ShstrtabSD.setAlignment(1);
Rafael Espindola44bf2662010-09-16 19:46:31 +00001162
Rafael Espindola36ef57d2010-11-10 19:05:07 +00001163 const MCSectionELF *SymtabSection =
Rafael Espindola3fe87a12010-10-31 00:16:26 +00001164 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1165 SectionKind::getReadOnly(),
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001166 EntrySize, "");
Matt Fleming6c1ad482010-08-16 18:57:57 +00001167 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Rafael Espindolafdaae0d2010-12-18 03:27:34 +00001168 SymtabSD.setAlignment(is64Bit() ? 8 : 4);
Rafael Espindola3fe87a12010-10-31 00:16:26 +00001169
1170 MCSectionData *SymtabShndxSD = NULL;
1171
1172 if (NeedsSymtabShndx) {
Rafael Espindola36ef57d2010-11-10 19:05:07 +00001173 const MCSectionELF *SymtabShndxSection =
Rafael Espindola3fe87a12010-10-31 00:16:26 +00001174 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001175 SectionKind::getReadOnly(), 4, "");
Rafael Espindola3fe87a12010-10-31 00:16:26 +00001176 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
1177 SymtabShndxSD->setAlignment(4);
1178 }
Matt Fleming6c1ad482010-08-16 18:57:57 +00001179
Rafael Espindola1557fd62011-03-20 18:44:20 +00001180 const MCSectionELF *StrtabSection;
Matt Fleming6c1ad482010-08-16 18:57:57 +00001181 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
Rafael Espindola19fa3802010-11-11 03:40:25 +00001182 SectionKind::getReadOnly());
Matt Fleming6c1ad482010-08-16 18:57:57 +00001183 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1184 StrtabSD.setAlignment(1);
Matt Fleming6c1ad482010-08-16 18:57:57 +00001185
Rafael Espindola1557fd62011-03-20 18:44:20 +00001186 ComputeIndexMap(Asm, SectionIndexMap, RelMap);
1187
1188 ShstrtabIndex = SectionIndexMap.lookup(ShstrtabSection);
1189 SymbolTableIndex = SectionIndexMap.lookup(SymtabSection);
1190 StringTableIndex = SectionIndexMap.lookup(StrtabSection);
Rafael Espindola44bf2662010-09-16 19:46:31 +00001191
1192 // Symbol table
1193 F = new MCDataFragment(&SymtabSD);
Rafael Espindola3fe87a12010-10-31 00:16:26 +00001194 MCDataFragment *ShndxF = NULL;
1195 if (NeedsSymtabShndx) {
1196 ShndxF = new MCDataFragment(SymtabShndxSD);
Rafael Espindola3fe87a12010-10-31 00:16:26 +00001197 }
Rafael Espindola18014102010-11-10 22:16:43 +00001198 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
Rafael Espindola44bf2662010-09-16 19:46:31 +00001199
Matt Fleming6c1ad482010-08-16 18:57:57 +00001200 F = new MCDataFragment(&StrtabSD);
1201 F->getContents().append(StringTable.begin(), StringTable.end());
Matt Fleming6c1ad482010-08-16 18:57:57 +00001202
Matt Fleming6c1ad482010-08-16 18:57:57 +00001203 F = new MCDataFragment(&ShstrtabSD);
1204
Rafael Espindolab80875e2011-04-07 23:21:52 +00001205 std::vector<const MCSectionELF*> Sections;
1206 for (MCAssembler::const_iterator it = Asm.begin(),
1207 ie = Asm.end(); it != ie; ++it) {
1208 const MCSectionELF &Section =
1209 static_cast<const MCSectionELF&>(it->getSection());
1210 Sections.push_back(&Section);
1211 }
1212 array_pod_sort(Sections.begin(), Sections.end(), compareBySuffix);
1213
Matt Fleming6c1ad482010-08-16 18:57:57 +00001214 // Section header string table.
1215 //
1216 // The first entry of a string table holds a null character so skip
1217 // section 0.
1218 uint64_t Index = 1;
Eli Bendersky84b2a792012-12-07 22:06:56 +00001219 F->getContents().push_back('\x00');
Matt Fleming6c1ad482010-08-16 18:57:57 +00001220
Rafael Espindolab80875e2011-04-07 23:21:52 +00001221 for (unsigned int I = 0, E = Sections.size(); I != E; ++I) {
1222 const MCSectionELF &Section = *Sections[I];
Matt Fleming6c1ad482010-08-16 18:57:57 +00001223
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001224 StringRef Name = Section.getSectionName();
Rafael Espindolab80875e2011-04-07 23:21:52 +00001225 if (I != 0) {
1226 StringRef PreviousName = Sections[I - 1]->getSectionName();
1227 if (PreviousName.endswith(Name)) {
1228 SectionStringTableIndex[&Section] = Index - Name.size() - 1;
1229 continue;
1230 }
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001231 }
Matt Fleming6c1ad482010-08-16 18:57:57 +00001232 // Remember the index into the string table so we can write it
1233 // into the sh_name field of the section header table.
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001234 SectionStringTableIndex[&Section] = Index;
Matt Fleming6c1ad482010-08-16 18:57:57 +00001235
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001236 Index += Name.size() + 1;
Eli Bendersky84b2a792012-12-07 22:06:56 +00001237 F->getContents().append(Name.begin(), Name.end());
1238 F->getContents().push_back('\x00');
Matt Fleming6c1ad482010-08-16 18:57:57 +00001239 }
Rafael Espindola2ebaee92010-09-30 02:22:20 +00001240}
1241
Rafael Espindolab3eca9b2011-01-23 17:55:27 +00001242void ELFObjectWriter::CreateIndexedSections(MCAssembler &Asm,
1243 MCAsmLayout &Layout,
1244 GroupMapTy &GroupMap,
Rafael Espindola1557fd62011-03-20 18:44:20 +00001245 RevGroupMapTy &RevGroupMap,
1246 SectionIndexMapTy &SectionIndexMap,
1247 const RelMapTy &RelMap) {
Rafael Espindolab3eca9b2011-01-23 17:55:27 +00001248 // Create the .note.GNU-stack section if needed.
1249 MCContext &Ctx = Asm.getContext();
1250 if (Asm.getNoExecStack()) {
1251 const MCSectionELF *GnuStackSection =
1252 Ctx.getELFSection(".note.GNU-stack", ELF::SHT_PROGBITS, 0,
1253 SectionKind::getReadOnly());
1254 Asm.getOrCreateSectionData(*GnuStackSection);
1255 }
1256
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001257 // Build the groups
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001258 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1259 it != ie; ++it) {
1260 const MCSectionELF &Section =
1261 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola0e7e34e2011-01-23 04:43:11 +00001262 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001263 continue;
1264
1265 const MCSymbol *SignatureSymbol = Section.getGroup();
1266 Asm.getOrCreateSymbolData(*SignatureSymbol);
Rafael Espindola7d0ba342010-11-14 04:17:37 +00001267 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001268 if (!Group) {
Rafael Espindolab3eca9b2011-01-23 17:55:27 +00001269 Group = Ctx.CreateELFGroupSection();
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001270 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1271 Data.setAlignment(4);
1272 MCDataFragment *F = new MCDataFragment(&Data);
1273 String32(*F, ELF::GRP_COMDAT);
1274 }
1275 GroupMap[Group] = SignatureSymbol;
1276 }
1277
Rafael Espindola1557fd62011-03-20 18:44:20 +00001278 ComputeIndexMap(Asm, SectionIndexMap, RelMap);
1279
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001280 // Add sections to the groups
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001281 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
Rafael Espindola1557fd62011-03-20 18:44:20 +00001282 it != ie; ++it) {
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001283 const MCSectionELF &Section =
1284 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola0e7e34e2011-01-23 04:43:11 +00001285 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001286 continue;
Rafael Espindola7d0ba342010-11-14 04:17:37 +00001287 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001288 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1289 // FIXME: we could use the previous fragment
1290 MCDataFragment *F = new MCDataFragment(&Data);
Rafael Espindola1557fd62011-03-20 18:44:20 +00001291 unsigned Index = SectionIndexMap.lookup(&Section);
1292 String32(*F, Index);
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001293 }
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001294}
1295
Daniel Dunbarfe0c28f2010-11-13 07:33:40 +00001296void ELFObjectWriter::WriteSection(MCAssembler &Asm,
1297 const SectionIndexMapTy &SectionIndexMap,
1298 uint32_t GroupSymbolIndex,
1299 uint64_t Offset, uint64_t Size,
1300 uint64_t Alignment,
1301 const MCSectionELF &Section) {
Rafael Espindola5a8d7812010-11-10 23:36:59 +00001302 uint64_t sh_link = 0;
1303 uint64_t sh_info = 0;
1304
1305 switch(Section.getType()) {
1306 case ELF::SHT_DYNAMIC:
1307 sh_link = SectionStringTableIndex[&Section];
1308 sh_info = 0;
1309 break;
1310
1311 case ELF::SHT_REL:
1312 case ELF::SHT_RELA: {
1313 const MCSectionELF *SymtabSection;
1314 const MCSectionELF *InfoSection;
1315 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
1316 0,
Rafael Espindola19fa3802010-11-11 03:40:25 +00001317 SectionKind::getReadOnly());
Rafael Espindola5a8d7812010-11-10 23:36:59 +00001318 sh_link = SectionIndexMap.lookup(SymtabSection);
1319 assert(sh_link && ".symtab not found");
1320
1321 // Remove ".rel" and ".rela" prefixes.
1322 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1323 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
David Blaikie15b25df2013-10-22 23:41:52 +00001324 StringRef GroupName =
1325 Section.getGroup() ? Section.getGroup()->getName() : "";
Rafael Espindola5a8d7812010-11-10 23:36:59 +00001326
David Blaikie15b25df2013-10-22 23:41:52 +00001327 InfoSection = Asm.getContext().getELFSection(SectionName, ELF::SHT_PROGBITS,
1328 0, SectionKind::getReadOnly(),
1329 0, GroupName);
Rafael Espindola5a8d7812010-11-10 23:36:59 +00001330 sh_info = SectionIndexMap.lookup(InfoSection);
1331 break;
1332 }
1333
1334 case ELF::SHT_SYMTAB:
1335 case ELF::SHT_DYNSYM:
1336 sh_link = StringTableIndex;
1337 sh_info = LastLocalSymbolIndex;
1338 break;
1339
1340 case ELF::SHT_SYMTAB_SHNDX:
1341 sh_link = SymbolTableIndex;
1342 break;
1343
1344 case ELF::SHT_PROGBITS:
1345 case ELF::SHT_STRTAB:
1346 case ELF::SHT_NOBITS:
Rafael Espindola9ae2d052010-12-26 21:30:59 +00001347 case ELF::SHT_NOTE:
Rafael Espindola5a8d7812010-11-10 23:36:59 +00001348 case ELF::SHT_NULL:
1349 case ELF::SHT_ARM_ATTRIBUTES:
Jason W Kim9c5b65d2011-01-12 00:19:25 +00001350 case ELF::SHT_INIT_ARRAY:
1351 case ELF::SHT_FINI_ARRAY:
1352 case ELF::SHT_PREINIT_ARRAY:
Rafael Espindola4b7b7fb2011-01-23 05:43:40 +00001353 case ELF::SHT_X86_64_UNWIND:
Jack Carterc1b17ed2013-01-18 21:20:38 +00001354 case ELF::SHT_MIPS_REGINFO:
1355 case ELF::SHT_MIPS_OPTIONS:
Rafael Espindola5a8d7812010-11-10 23:36:59 +00001356 // Nothing to do.
1357 break;
1358
Nick Lewyckyc6ac5f72011-10-07 23:28:32 +00001359 case ELF::SHT_GROUP:
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001360 sh_link = SymbolTableIndex;
1361 sh_info = GroupSymbolIndex;
1362 break;
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001363
Rafael Espindola5a8d7812010-11-10 23:36:59 +00001364 default:
1365 assert(0 && "FIXME: sh_type value not supported!");
1366 break;
1367 }
1368
Logan Chien4b724422013-02-05 14:18:59 +00001369 if (TargetObjectWriter->getEMachine() == ELF::EM_ARM &&
1370 Section.getType() == ELF::SHT_ARM_EXIDX) {
1371 StringRef SecName(Section.getSectionName());
1372 if (SecName == ".ARM.exidx") {
1373 sh_link = SectionIndexMap.lookup(
1374 Asm.getContext().getELFSection(".text",
1375 ELF::SHT_PROGBITS,
1376 ELF::SHF_EXECINSTR | ELF::SHF_ALLOC,
1377 SectionKind::getText()));
1378 } else if (SecName.startswith(".ARM.exidx")) {
David Blaikie15b25df2013-10-22 23:41:52 +00001379 StringRef GroupName =
1380 Section.getGroup() ? Section.getGroup()->getName() : "";
1381 sh_link = SectionIndexMap.lookup(Asm.getContext().getELFSection(
1382 SecName.substr(sizeof(".ARM.exidx") - 1), ELF::SHT_PROGBITS,
1383 ELF::SHF_EXECINSTR | ELF::SHF_ALLOC, SectionKind::getText(), 0,
1384 GroupName));
Logan Chien4b724422013-02-05 14:18:59 +00001385 }
1386 }
1387
Rafael Espindola5a8d7812010-11-10 23:36:59 +00001388 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1389 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1390 Alignment, Section.getEntrySize());
1391}
1392
Jan Sjödin30a52de2011-02-28 21:45:04 +00001393bool ELFObjectWriter::IsELFMetaDataSection(const MCSectionData &SD) {
Rafael Espindolabaf2f3b2010-12-06 03:48:09 +00001394 return SD.getOrdinal() == ~UINT32_C(0) &&
Rafael Espindola60ebca92010-12-02 03:09:06 +00001395 !SD.getSection().isVirtualSection();
1396}
1397
Jan Sjödin30a52de2011-02-28 21:45:04 +00001398uint64_t ELFObjectWriter::DataSectionSize(const MCSectionData &SD) {
Rafael Espindola60ebca92010-12-02 03:09:06 +00001399 uint64_t Ret = 0;
1400 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1401 ++i) {
1402 const MCFragment &F = *i;
1403 assert(F.getKind() == MCFragment::FT_Data);
1404 Ret += cast<MCDataFragment>(F).getContents().size();
1405 }
1406 return Ret;
1407}
1408
Jan Sjödin30a52de2011-02-28 21:45:04 +00001409uint64_t ELFObjectWriter::GetSectionFileSize(const MCAsmLayout &Layout,
1410 const MCSectionData &SD) {
Rafael Espindola60ebca92010-12-02 03:09:06 +00001411 if (IsELFMetaDataSection(SD))
1412 return DataSectionSize(SD);
1413 return Layout.getSectionFileSize(&SD);
1414}
1415
Jan Sjödin30a52de2011-02-28 21:45:04 +00001416uint64_t ELFObjectWriter::GetSectionAddressSize(const MCAsmLayout &Layout,
1417 const MCSectionData &SD) {
Rafael Espindola60ebca92010-12-02 03:09:06 +00001418 if (IsELFMetaDataSection(SD))
1419 return DataSectionSize(SD);
Rafael Espindola93e3cf02010-12-07 00:27:36 +00001420 return Layout.getSectionAddressSize(&SD);
Rafael Espindola60ebca92010-12-02 03:09:06 +00001421}
1422
Rafael Espindola1557fd62011-03-20 18:44:20 +00001423void ELFObjectWriter::WriteDataSectionData(MCAssembler &Asm,
1424 const MCAsmLayout &Layout,
1425 const MCSectionELF &Section) {
Rafael Espindola1557fd62011-03-20 18:44:20 +00001426 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1427
Benjamin Kramer084b9f42012-01-20 14:42:32 +00001428 uint64_t Padding = OffsetToAlignment(OS.tell(), SD.getAlignment());
Rafael Espindola1557fd62011-03-20 18:44:20 +00001429 WriteZeros(Padding);
Rafael Espindola1557fd62011-03-20 18:44:20 +00001430
1431 if (IsELFMetaDataSection(SD)) {
1432 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1433 ++i) {
1434 const MCFragment &F = *i;
1435 assert(F.getKind() == MCFragment::FT_Data);
Eli Bendersky84b2a792012-12-07 22:06:56 +00001436 WriteBytes(cast<MCDataFragment>(F).getContents());
Rafael Espindola1557fd62011-03-20 18:44:20 +00001437 }
1438 } else {
Jim Grosbach18e2fe42011-12-06 00:03:48 +00001439 Asm.writeSectionData(&SD, Layout);
Rafael Espindola60ebca92010-12-02 03:09:06 +00001440 }
1441}
1442
Rafael Espindola1557fd62011-03-20 18:44:20 +00001443void ELFObjectWriter::WriteSectionHeader(MCAssembler &Asm,
1444 const GroupMapTy &GroupMap,
1445 const MCAsmLayout &Layout,
1446 const SectionIndexMapTy &SectionIndexMap,
1447 const SectionOffsetMapTy &SectionOffsetMap) {
1448 const unsigned NumSections = Asm.size() + 1;
Matt Fleming6c1ad482010-08-16 18:57:57 +00001449
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001450 std::vector<const MCSectionELF*> Sections;
Rafael Espindola1557fd62011-03-20 18:44:20 +00001451 Sections.resize(NumSections - 1);
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001452
1453 for (SectionIndexMapTy::const_iterator i=
1454 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1455 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
Rafael Espindola1557fd62011-03-20 18:44:20 +00001456 Sections[p.second - 1] = p.first;
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001457 }
1458
Matt Fleming6c1ad482010-08-16 18:57:57 +00001459 // Null section first.
Rafael Espindola3fe87a12010-10-31 00:16:26 +00001460 uint64_t FirstSectionSize =
1461 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1462 uint32_t FirstSectionLink =
1463 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1464 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming6c1ad482010-08-16 18:57:57 +00001465
Rafael Espindola1557fd62011-03-20 18:44:20 +00001466 for (unsigned i = 0; i < NumSections - 1; ++i) {
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001467 const MCSectionELF &Section = *Sections[i];
1468 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1469 uint32_t GroupSymbolIndex;
1470 if (Section.getType() != ELF::SHT_GROUP)
1471 GroupSymbolIndex = 0;
1472 else
Rafael Espindola1557fd62011-03-20 18:44:20 +00001473 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm,
1474 GroupMap.lookup(&Section));
Matt Fleming6c1ad482010-08-16 18:57:57 +00001475
Rafael Espindola93e3cf02010-12-07 00:27:36 +00001476 uint64_t Size = GetSectionAddressSize(Layout, SD);
Rafael Espindola60ebca92010-12-02 03:09:06 +00001477
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001478 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
Rafael Espindola1557fd62011-03-20 18:44:20 +00001479 SectionOffsetMap.lookup(&Section), Size,
Rafael Espindola5a8d7812010-11-10 23:36:59 +00001480 SD.getAlignment(), Section);
Matt Fleming6c1ad482010-08-16 18:57:57 +00001481 }
1482}
1483
Rafael Espindola1557fd62011-03-20 18:44:20 +00001484void ELFObjectWriter::ComputeSectionOrder(MCAssembler &Asm,
1485 std::vector<const MCSectionELF*> &Sections) {
1486 for (MCAssembler::iterator it = Asm.begin(),
1487 ie = Asm.end(); it != ie; ++it) {
1488 const MCSectionELF &Section =
1489 static_cast<const MCSectionELF &>(it->getSection());
1490 if (Section.getType() == ELF::SHT_GROUP)
1491 Sections.push_back(&Section);
1492 }
1493
1494 for (MCAssembler::iterator it = Asm.begin(),
1495 ie = Asm.end(); it != ie; ++it) {
1496 const MCSectionELF &Section =
1497 static_cast<const MCSectionELF &>(it->getSection());
1498 if (Section.getType() != ELF::SHT_GROUP &&
1499 Section.getType() != ELF::SHT_REL &&
1500 Section.getType() != ELF::SHT_RELA)
1501 Sections.push_back(&Section);
1502 }
1503
1504 for (MCAssembler::iterator it = Asm.begin(),
1505 ie = Asm.end(); it != ie; ++it) {
1506 const MCSectionELF &Section =
1507 static_cast<const MCSectionELF &>(it->getSection());
1508 if (Section.getType() == ELF::SHT_REL ||
1509 Section.getType() == ELF::SHT_RELA)
1510 Sections.push_back(&Section);
1511 }
1512}
1513
1514void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1515 const MCAsmLayout &Layout) {
1516 GroupMapTy GroupMap;
1517 RevGroupMapTy RevGroupMap;
1518 SectionIndexMapTy SectionIndexMap;
1519
1520 unsigned NumUserSections = Asm.size();
1521
1522 DenseMap<const MCSectionELF*, const MCSectionELF*> RelMap;
1523 CreateRelocationSections(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1524
1525 const unsigned NumUserAndRelocSections = Asm.size();
1526 CreateIndexedSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1527 RevGroupMap, SectionIndexMap, RelMap);
1528 const unsigned AllSections = Asm.size();
1529 const unsigned NumIndexedSections = AllSections - NumUserAndRelocSections;
1530
1531 unsigned NumRegularSections = NumUserSections + NumIndexedSections;
1532
1533 // Compute symbol table information.
1534 ComputeSymbolTable(Asm, SectionIndexMap, RevGroupMap, NumRegularSections);
1535
1536
1537 WriteRelocations(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1538
1539 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
1540 const_cast<MCAsmLayout&>(Layout),
1541 SectionIndexMap,
1542 RelMap);
1543
1544 uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
1545 uint64_t HeaderSize = is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
1546 sizeof(ELF::Elf32_Ehdr);
1547 uint64_t FileOff = HeaderSize;
1548
1549 std::vector<const MCSectionELF*> Sections;
1550 ComputeSectionOrder(Asm, Sections);
1551 unsigned NumSections = Sections.size();
1552 SectionOffsetMapTy SectionOffsetMap;
1553 for (unsigned i = 0; i < NumRegularSections + 1; ++i) {
1554 const MCSectionELF &Section = *Sections[i];
1555 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1556
1557 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1558
1559 // Remember the offset into the file for this section.
1560 SectionOffsetMap[&Section] = FileOff;
1561
1562 // Get the size of the section in the output file (including padding).
1563 FileOff += GetSectionFileSize(Layout, SD);
1564 }
1565
1566 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1567
1568 const unsigned SectionHeaderOffset = FileOff - HeaderSize;
1569
1570 uint64_t SectionHeaderEntrySize = is64Bit() ?
1571 sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr);
1572 FileOff += (NumSections + 1) * SectionHeaderEntrySize;
1573
1574 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i) {
1575 const MCSectionELF &Section = *Sections[i];
1576 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1577
1578 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1579
1580 // Remember the offset into the file for this section.
1581 SectionOffsetMap[&Section] = FileOff;
1582
1583 // Get the size of the section in the output file (including padding).
1584 FileOff += GetSectionFileSize(Layout, SD);
1585 }
1586
1587 // Write out the ELF header ...
Jack Carter1bd90ff2013-01-30 02:09:52 +00001588 WriteHeader(Asm, SectionHeaderOffset, NumSections + 1);
Rafael Espindola1557fd62011-03-20 18:44:20 +00001589
1590 // ... then the regular sections ...
1591 // + because of .shstrtab
1592 for (unsigned i = 0; i < NumRegularSections + 1; ++i)
1593 WriteDataSectionData(Asm, Layout, *Sections[i]);
1594
Benjamin Kramer084b9f42012-01-20 14:42:32 +00001595 uint64_t Padding = OffsetToAlignment(OS.tell(), NaturalAlignment);
Rafael Espindola1557fd62011-03-20 18:44:20 +00001596 WriteZeros(Padding);
1597
1598 // ... then the section header table ...
1599 WriteSectionHeader(Asm, GroupMap, Layout, SectionIndexMap,
1600 SectionOffsetMap);
1601
Nick Lewycky4eb11432011-10-07 20:56:23 +00001602 // ... and then the remaining sections ...
Rafael Espindola1557fd62011-03-20 18:44:20 +00001603 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i)
1604 WriteDataSectionData(Asm, Layout, *Sections[i]);
1605}
1606
Eli Friedmand92d17b2011-03-03 07:24:36 +00001607bool
1608ELFObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
1609 const MCSymbolData &DataA,
1610 const MCFragment &FB,
1611 bool InSet,
1612 bool IsPCRel) const {
1613 if (DataA.getFlags() & ELF_STB_Weak)
1614 return false;
1615 return MCObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(
1616 Asm, DataA, FB,InSet, IsPCRel);
1617}
1618
Rafael Espindola6b5e56c2010-12-17 17:45:22 +00001619MCObjectWriter *llvm::createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1620 raw_ostream &OS,
Rafael Espindolafdaae0d2010-12-18 03:27:34 +00001621 bool IsLittleEndian) {
Rafael Espindola34a68af2011-12-22 03:24:43 +00001622 return new ELFObjectWriter(MOTW, OS, IsLittleEndian);
Rafael Espindolab264d332011-12-21 17:30:17 +00001623}