blob: fcf6d87e421b88407b2dd5897492eed1684197ff [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"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/STLExtras.h"
Rafael Espindola29abd972011-12-22 03:38:00 +000016#include "llvm/ADT/SmallPtrSet.h"
17#include "llvm/ADT/SmallString.h"
Matt Fleming6c1ad482010-08-16 18:57:57 +000018#include "llvm/ADT/StringMap.h"
Evan Cheng5928e692011-07-25 23:24:55 +000019#include "llvm/MC/MCAsmBackend.h"
Matt Fleming6c1ad482010-08-16 18:57:57 +000020#include "llvm/MC/MCAsmLayout.h"
Rafael Espindola29abd972011-12-22 03:38:00 +000021#include "llvm/MC/MCAssembler.h"
Matt Fleming6c1ad482010-08-16 18:57:57 +000022#include "llvm/MC/MCContext.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000023#include "llvm/MC/MCELF.h"
Rafael Espindola29abd972011-12-22 03:38:00 +000024#include "llvm/MC/MCELFSymbolFlags.h"
Matt Fleming6c1ad482010-08-16 18:57:57 +000025#include "llvm/MC/MCExpr.h"
Craig Topper6e80c282012-03-26 06:58:25 +000026#include "llvm/MC/MCFixupKindInfo.h"
27#include "llvm/MC/MCObjectWriter.h"
Matt Fleming6c1ad482010-08-16 18:57:57 +000028#include "llvm/MC/MCSectionELF.h"
Matt Fleming6c1ad482010-08-16 18:57:57 +000029#include "llvm/MC/MCValue.h"
30#include "llvm/Support/Debug.h"
Matt Fleming6c1ad482010-08-16 18:57:57 +000031#include "llvm/Support/ELF.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/Support/ErrorHandling.h"
Matt Fleming6c1ad482010-08-16 18:57:57 +000033#include <vector>
34using namespace llvm;
35
Jason W Kimc09e7262011-05-11 22:53:06 +000036#undef DEBUG_TYPE
37#define DEBUG_TYPE "reloc-info"
38
Rafael Espindola29abd972011-12-22 03:38:00 +000039namespace {
40class ELFObjectWriter : public MCObjectWriter {
41 protected:
42
43 static bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind);
44 static bool RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant);
45 static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout);
46 static bool isInSymtab(const MCAssembler &Asm, const MCSymbolData &Data,
47 bool Used, bool Renamed);
48 static bool isLocal(const MCSymbolData &Data, bool isSignature,
49 bool isUsedInReloc);
50 static bool IsELFMetaDataSection(const MCSectionData &SD);
51 static uint64_t DataSectionSize(const MCSectionData &SD);
52 static uint64_t GetSectionFileSize(const MCAsmLayout &Layout,
53 const MCSectionData &SD);
54 static uint64_t GetSectionAddressSize(const MCAsmLayout &Layout,
55 const MCSectionData &SD);
56
57 void WriteDataSectionData(MCAssembler &Asm,
58 const MCAsmLayout &Layout,
59 const MCSectionELF &Section);
60
61 /*static bool isFixupKindX86RIPRel(unsigned Kind) {
62 return Kind == X86::reloc_riprel_4byte ||
63 Kind == X86::reloc_riprel_4byte_movq_load;
64 }*/
65
66 /// ELFSymbolData - Helper struct for containing some precomputed
67 /// information on symbols.
68 struct ELFSymbolData {
69 MCSymbolData *SymbolData;
70 uint64_t StringIndex;
71 uint32_t SectionIndex;
72
73 // Support lexicographic sorting.
74 bool operator<(const ELFSymbolData &RHS) const {
Rafael Espindola29abd972011-12-22 03:38:00 +000075 return SymbolData->getSymbol().getName() <
76 RHS.SymbolData->getSymbol().getName();
77 }
78 };
79
Rafael Espindola29abd972011-12-22 03:38:00 +000080 /// The target specific ELF writer instance.
Ahmed Charles56440fd2014-03-06 05:51:42 +000081 std::unique_ptr<MCELFObjectTargetWriter> TargetObjectWriter;
Rafael Espindola29abd972011-12-22 03:38:00 +000082
83 SmallPtrSet<const MCSymbol *, 16> UsedInReloc;
84 SmallPtrSet<const MCSymbol *, 16> WeakrefUsedInReloc;
85 DenseMap<const MCSymbol *, const MCSymbol *> Renames;
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;
Joerg Sonnenbergerfc184732013-10-29 01:06:17 +000096 std::vector<uint64_t> FileSymbolData;
Rafael Espindola29abd972011-12-22 03:38:00 +000097 std::vector<ELFSymbolData> LocalSymbolData;
98 std::vector<ELFSymbolData> ExternalSymbolData;
99 std::vector<ELFSymbolData> UndefinedSymbolData;
100
101 /// @}
102
103 bool NeedsGOT;
104
105 bool NeedsSymtabShndx;
106
107 // This holds the symbol table index of the last local symbol.
108 unsigned LastLocalSymbolIndex;
109 // This holds the .strtab section index.
110 unsigned StringTableIndex;
111 // This holds the .symtab section index.
112 unsigned SymbolTableIndex;
113
114 unsigned ShstrtabIndex;
115
116
117 const MCSymbol *SymbolToReloc(const MCAssembler &Asm,
118 const MCValue &Target,
119 const MCFragment &F,
120 const MCFixup &Fixup,
121 bool IsPCRel) const;
122
123 // TargetObjectWriter wrappers.
124 const MCSymbol *ExplicitRelSym(const MCAssembler &Asm,
125 const MCValue &Target,
126 const MCFragment &F,
127 const MCFixup &Fixup,
128 bool IsPCRel) const {
129 return TargetObjectWriter->ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
130 }
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +0000131 const MCSymbol *undefinedExplicitRelSym(const MCValue &Target,
132 const MCFixup &Fixup,
133 bool IsPCRel) const {
Jack Carterf6622ba2013-02-12 21:29:39 +0000134 return TargetObjectWriter->undefinedExplicitRelSym(Target, Fixup,
135 IsPCRel);
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +0000136 }
Rafael Espindola29abd972011-12-22 03:38:00 +0000137
138 bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
139 bool hasRelocationAddend() const {
140 return TargetObjectWriter->hasRelocationAddend();
141 }
Rafael Espindola29abd972011-12-22 03:38:00 +0000142 unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
143 bool IsPCRel, bool IsRelocWithSymbol,
144 int64_t Addend) const {
145 return TargetObjectWriter->GetRelocType(Target, Fixup, IsPCRel,
146 IsRelocWithSymbol, Addend);
147 }
148
Rafael Espindola29abd972011-12-22 03:38:00 +0000149 public:
150 ELFObjectWriter(MCELFObjectTargetWriter *MOTW,
151 raw_ostream &_OS, bool IsLittleEndian)
152 : MCObjectWriter(_OS, IsLittleEndian),
153 TargetObjectWriter(MOTW),
Nick Lewycky4288f9e2013-03-09 09:32:16 +0000154 NeedsGOT(false), NeedsSymtabShndx(false) {
Rafael Espindola29abd972011-12-22 03:38:00 +0000155 }
156
157 virtual ~ELFObjectWriter();
158
159 void WriteWord(uint64_t W) {
160 if (is64Bit())
161 Write64(W);
162 else
163 Write32(W);
164 }
165
166 void StringLE16(char *buf, uint16_t Value) {
167 buf[0] = char(Value >> 0);
168 buf[1] = char(Value >> 8);
169 }
170
171 void StringLE32(char *buf, uint32_t Value) {
172 StringLE16(buf, uint16_t(Value >> 0));
173 StringLE16(buf + 2, uint16_t(Value >> 16));
174 }
175
176 void StringLE64(char *buf, uint64_t Value) {
177 StringLE32(buf, uint32_t(Value >> 0));
178 StringLE32(buf + 4, uint32_t(Value >> 32));
179 }
180
181 void StringBE16(char *buf ,uint16_t Value) {
182 buf[0] = char(Value >> 8);
183 buf[1] = char(Value >> 0);
184 }
185
186 void StringBE32(char *buf, uint32_t Value) {
187 StringBE16(buf, uint16_t(Value >> 16));
188 StringBE16(buf + 2, uint16_t(Value >> 0));
189 }
190
191 void StringBE64(char *buf, uint64_t Value) {
192 StringBE32(buf, uint32_t(Value >> 32));
193 StringBE32(buf + 4, uint32_t(Value >> 0));
194 }
195
196 void String8(MCDataFragment &F, uint8_t Value) {
197 char buf[1];
198 buf[0] = Value;
Eli Bendersky84b2a792012-12-07 22:06:56 +0000199 F.getContents().append(&buf[0], &buf[1]);
Rafael Espindola29abd972011-12-22 03:38:00 +0000200 }
201
202 void String16(MCDataFragment &F, uint16_t Value) {
203 char buf[2];
204 if (isLittleEndian())
205 StringLE16(buf, Value);
206 else
207 StringBE16(buf, Value);
Eli Bendersky84b2a792012-12-07 22:06:56 +0000208 F.getContents().append(&buf[0], &buf[2]);
Rafael Espindola29abd972011-12-22 03:38:00 +0000209 }
210
211 void String32(MCDataFragment &F, uint32_t Value) {
212 char buf[4];
213 if (isLittleEndian())
214 StringLE32(buf, Value);
215 else
216 StringBE32(buf, Value);
Eli Bendersky84b2a792012-12-07 22:06:56 +0000217 F.getContents().append(&buf[0], &buf[4]);
Rafael Espindola29abd972011-12-22 03:38:00 +0000218 }
219
220 void String64(MCDataFragment &F, uint64_t Value) {
221 char buf[8];
222 if (isLittleEndian())
223 StringLE64(buf, Value);
224 else
225 StringBE64(buf, Value);
Eli Bendersky84b2a792012-12-07 22:06:56 +0000226 F.getContents().append(&buf[0], &buf[8]);
Rafael Espindola29abd972011-12-22 03:38:00 +0000227 }
228
Jack Carter1bd90ff2013-01-30 02:09:52 +0000229 void WriteHeader(const MCAssembler &Asm,
230 uint64_t SectionDataSize,
Rafael Espindola29abd972011-12-22 03:38:00 +0000231 unsigned NumberOfSections);
232
233 void WriteSymbolEntry(MCDataFragment *SymtabF,
234 MCDataFragment *ShndxF,
235 uint64_t name, uint8_t info,
236 uint64_t value, uint64_t size,
237 uint8_t other, uint32_t shndx,
238 bool Reserved);
239
240 void WriteSymbol(MCDataFragment *SymtabF, MCDataFragment *ShndxF,
241 ELFSymbolData &MSD,
242 const MCAsmLayout &Layout);
243
244 typedef DenseMap<const MCSectionELF*, uint32_t> SectionIndexMapTy;
245 void WriteSymbolTable(MCDataFragment *SymtabF,
246 MCDataFragment *ShndxF,
247 const MCAssembler &Asm,
248 const MCAsmLayout &Layout,
249 const SectionIndexMapTy &SectionIndexMap);
250
Craig Topper34a61bc2014-03-08 07:02:02 +0000251 void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
252 const MCFragment *Fragment, const MCFixup &Fixup,
253 MCValue Target, uint64_t &FixedValue) override;
Rafael Espindola29abd972011-12-22 03:38:00 +0000254
255 uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm,
256 const MCSymbol *S);
257
258 // Map from a group section to the signature symbol
259 typedef DenseMap<const MCSectionELF*, const MCSymbol*> GroupMapTy;
260 // Map from a signature symbol to the group section
261 typedef DenseMap<const MCSymbol*, const MCSectionELF*> RevGroupMapTy;
262 // Map from a section to the section with the relocations
263 typedef DenseMap<const MCSectionELF*, const MCSectionELF*> RelMapTy;
264 // Map from a section to its offset
265 typedef DenseMap<const MCSectionELF*, uint64_t> SectionOffsetMapTy;
266
Rafael Espindola022bb762014-03-24 03:43:21 +0000267 /// Compute the symbol table data
Rafael Espindola29abd972011-12-22 03:38:00 +0000268 ///
Rafael Espindola073ee7d2012-08-27 16:04:24 +0000269 /// \param Asm - The assembler.
270 /// \param SectionIndexMap - Maps a section to its index.
271 /// \param RevGroupMap - Maps a signature symbol to the group section.
272 /// \param NumRegularSections - Number of non-relocation sections.
Rafael Espindola022bb762014-03-24 03:43:21 +0000273 void computeSymbolTable(MCAssembler &Asm, const MCAsmLayout &Layout,
Rafael Espindola29abd972011-12-22 03:38:00 +0000274 const SectionIndexMapTy &SectionIndexMap,
275 RevGroupMapTy RevGroupMap,
276 unsigned NumRegularSections);
277
278 void ComputeIndexMap(MCAssembler &Asm,
279 SectionIndexMapTy &SectionIndexMap,
280 const RelMapTy &RelMap);
281
282 void CreateRelocationSections(MCAssembler &Asm, MCAsmLayout &Layout,
283 RelMapTy &RelMap);
284
285 void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout,
286 const RelMapTy &RelMap);
287
288 void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout,
289 SectionIndexMapTy &SectionIndexMap,
290 const RelMapTy &RelMap);
291
292 // Create the sections that show up in the symbol table. Currently
293 // those are the .note.GNU-stack section and the group sections.
294 void CreateIndexedSections(MCAssembler &Asm, MCAsmLayout &Layout,
295 GroupMapTy &GroupMap,
296 RevGroupMapTy &RevGroupMap,
297 SectionIndexMapTy &SectionIndexMap,
298 const RelMapTy &RelMap);
299
Craig Topper34a61bc2014-03-08 07:02:02 +0000300 void ExecutePostLayoutBinding(MCAssembler &Asm,
301 const MCAsmLayout &Layout) override;
Rafael Espindola29abd972011-12-22 03:38:00 +0000302
303 void WriteSectionHeader(MCAssembler &Asm, const GroupMapTy &GroupMap,
304 const MCAsmLayout &Layout,
305 const SectionIndexMapTy &SectionIndexMap,
306 const SectionOffsetMapTy &SectionOffsetMap);
307
308 void ComputeSectionOrder(MCAssembler &Asm,
309 std::vector<const MCSectionELF*> &Sections);
310
311 void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
312 uint64_t Address, uint64_t Offset,
313 uint64_t Size, uint32_t Link, uint32_t Info,
314 uint64_t Alignment, uint64_t EntrySize);
315
316 void WriteRelocationsFragment(const MCAssembler &Asm,
317 MCDataFragment *F,
318 const MCSectionData *SD);
319
Craig Topper34a61bc2014-03-08 07:02:02 +0000320 bool
Rafael Espindola29abd972011-12-22 03:38:00 +0000321 IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
322 const MCSymbolData &DataA,
323 const MCFragment &FB,
324 bool InSet,
Craig Topper34a61bc2014-03-08 07:02:02 +0000325 bool IsPCRel) const override;
Rafael Espindola29abd972011-12-22 03:38:00 +0000326
Craig Topper34a61bc2014-03-08 07:02:02 +0000327 void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
Rafael Espindola29abd972011-12-22 03:38:00 +0000328 void WriteSection(MCAssembler &Asm,
329 const SectionIndexMapTy &SectionIndexMap,
330 uint32_t GroupSymbolIndex,
331 uint64_t Offset, uint64_t Size, uint64_t Alignment,
332 const MCSectionELF &Section);
333 };
334}
335
Jan Sjödin30a52de2011-02-28 21:45:04 +0000336bool ELFObjectWriter::isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) {
337 const MCFixupKindInfo &FKI =
338 Asm.getBackend().getFixupKindInfo((MCFixupKind) Kind);
339
340 return FKI.Flags & MCFixupKindInfo::FKF_IsPCRel;
341}
342
343bool ELFObjectWriter::RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant) {
344 switch (Variant) {
345 default:
346 return false;
347 case MCSymbolRefExpr::VK_GOT:
348 case MCSymbolRefExpr::VK_PLT:
349 case MCSymbolRefExpr::VK_GOTPCREL:
Rafael Espindola940a0ee2011-06-05 01:20:06 +0000350 case MCSymbolRefExpr::VK_GOTOFF:
Jan Sjödin30a52de2011-02-28 21:45:04 +0000351 case MCSymbolRefExpr::VK_TPOFF:
352 case MCSymbolRefExpr::VK_TLSGD:
353 case MCSymbolRefExpr::VK_GOTTPOFF:
354 case MCSymbolRefExpr::VK_INDNTPOFF:
355 case MCSymbolRefExpr::VK_NTPOFF:
356 case MCSymbolRefExpr::VK_GOTNTPOFF:
357 case MCSymbolRefExpr::VK_TLSLDM:
358 case MCSymbolRefExpr::VK_DTPOFF:
359 case MCSymbolRefExpr::VK_TLSLD:
360 return true;
361 }
362}
363
Jason W Kim96f4c012010-11-15 16:18:39 +0000364ELFObjectWriter::~ELFObjectWriter()
365{}
366
Matt Fleming6c1ad482010-08-16 18:57:57 +0000367// Emit the ELF header.
Jack Carter1bd90ff2013-01-30 02:09:52 +0000368void ELFObjectWriter::WriteHeader(const MCAssembler &Asm,
369 uint64_t SectionDataSize,
Daniel Dunbarfe0c28f2010-11-13 07:33:40 +0000370 unsigned NumberOfSections) {
Matt Fleming6c1ad482010-08-16 18:57:57 +0000371 // ELF Header
372 // ----------
373 //
374 // Note
375 // ----
376 // emitWord method behaves differently for ELF32 and ELF64, writing
377 // 4 bytes in the former and 8 in the latter.
378
379 Write8(0x7f); // e_ident[EI_MAG0]
380 Write8('E'); // e_ident[EI_MAG1]
381 Write8('L'); // e_ident[EI_MAG2]
382 Write8('F'); // e_ident[EI_MAG3]
383
Rafael Espindolafdaae0d2010-12-18 03:27:34 +0000384 Write8(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
Matt Fleming6c1ad482010-08-16 18:57:57 +0000385
386 // e_ident[EI_DATA]
Daniel Dunbarfe0c28f2010-11-13 07:33:40 +0000387 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000388
389 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky3b727f52010-09-09 17:57:50 +0000390 // e_ident[EI_OSABI]
Rafael Espindola1ad40952011-12-21 17:00:36 +0000391 Write8(TargetObjectWriter->getOSABI());
Matt Fleming6c1ad482010-08-16 18:57:57 +0000392 Write8(0); // e_ident[EI_ABIVERSION]
393
394 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
395
396 Write16(ELF::ET_REL); // e_type
397
Rafael Espindolafdaae0d2010-12-18 03:27:34 +0000398 Write16(TargetObjectWriter->getEMachine()); // e_machine = target
Matt Fleming6c1ad482010-08-16 18:57:57 +0000399
400 Write32(ELF::EV_CURRENT); // e_version
401 WriteWord(0); // e_entry, no entry point in .o file
402 WriteWord(0); // e_phoff, no program header for .o
Rafael Espindolafdaae0d2010-12-18 03:27:34 +0000403 WriteWord(SectionDataSize + (is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
Benjamin Kramer896bd7e2010-08-17 17:02:29 +0000404 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming6c1ad482010-08-16 18:57:57 +0000405
Jason W Kim4761fba2011-02-04 21:41:11 +0000406 // e_flags = whatever the target wants
Jack Carter1bd90ff2013-01-30 02:09:52 +0000407 Write32(Asm.getELFHeaderEFlags());
Matt Fleming6c1ad482010-08-16 18:57:57 +0000408
409 // e_ehsize = ELF header size
Rafael Espindolafdaae0d2010-12-18 03:27:34 +0000410 Write16(is64Bit() ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
Matt Fleming6c1ad482010-08-16 18:57:57 +0000411
412 Write16(0); // e_phentsize = prog header entry size
413 Write16(0); // e_phnum = # prog header entries = 0
414
415 // e_shentsize = Section header entry size
Rafael Espindolafdaae0d2010-12-18 03:27:34 +0000416 Write16(is64Bit() ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
Matt Fleming6c1ad482010-08-16 18:57:57 +0000417
418 // e_shnum = # of section header ents
Rafael Espindola3fe87a12010-10-31 00:16:26 +0000419 if (NumberOfSections >= ELF::SHN_LORESERVE)
Nick Lewycky4eb11432011-10-07 20:56:23 +0000420 Write16(ELF::SHN_UNDEF);
Rafael Espindola3fe87a12010-10-31 00:16:26 +0000421 else
422 Write16(NumberOfSections);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000423
424 // e_shstrndx = Section # of '.shstrtab'
Nick Lewycky8b02d362011-10-07 20:58:24 +0000425 if (ShstrtabIndex >= ELF::SHN_LORESERVE)
Rafael Espindola3fe87a12010-10-31 00:16:26 +0000426 Write16(ELF::SHN_XINDEX);
427 else
428 Write16(ShstrtabIndex);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000429}
430
Daniel Dunbarfe0c28f2010-11-13 07:33:40 +0000431void ELFObjectWriter::WriteSymbolEntry(MCDataFragment *SymtabF,
432 MCDataFragment *ShndxF,
433 uint64_t name,
434 uint8_t info, uint64_t value,
435 uint64_t size, uint8_t other,
436 uint32_t shndx,
437 bool Reserved) {
Rafael Espindola3fe87a12010-10-31 00:16:26 +0000438 if (ShndxF) {
Rafael Espindola3fe87a12010-10-31 00:16:26 +0000439 if (shndx >= ELF::SHN_LORESERVE && !Reserved)
Rafael Espindola51d68332010-11-10 20:02:59 +0000440 String32(*ShndxF, shndx);
Rafael Espindola3fe87a12010-10-31 00:16:26 +0000441 else
Rafael Espindola51d68332010-11-10 20:02:59 +0000442 String32(*ShndxF, 0);
Rafael Espindola3fe87a12010-10-31 00:16:26 +0000443 }
444
Rafael Espindola51d68332010-11-10 20:02:59 +0000445 uint16_t Index = (shndx >= ELF::SHN_LORESERVE && !Reserved) ?
446 uint16_t(ELF::SHN_XINDEX) : shndx;
447
Rafael Espindolafdaae0d2010-12-18 03:27:34 +0000448 if (is64Bit()) {
Rafael Espindola51d68332010-11-10 20:02:59 +0000449 String32(*SymtabF, name); // st_name
450 String8(*SymtabF, info); // st_info
451 String8(*SymtabF, other); // st_other
452 String16(*SymtabF, Index); // st_shndx
453 String64(*SymtabF, value); // st_value
454 String64(*SymtabF, size); // st_size
Matt Fleming6c1ad482010-08-16 18:57:57 +0000455 } else {
Rafael Espindola51d68332010-11-10 20:02:59 +0000456 String32(*SymtabF, name); // st_name
457 String32(*SymtabF, value); // st_value
458 String32(*SymtabF, size); // st_size
459 String8(*SymtabF, info); // st_info
460 String8(*SymtabF, other); // st_other
461 String16(*SymtabF, Index); // st_shndx
Matt Fleming6c1ad482010-08-16 18:57:57 +0000462 }
463}
464
Rafael Espindola66f96fe2014-03-21 22:00:29 +0000465uint64_t ELFObjectWriter::SymbolValue(MCSymbolData &OrigData,
Jan Sjödin30a52de2011-02-28 21:45:04 +0000466 const MCAsmLayout &Layout) {
Rafael Espindola66f96fe2014-03-21 22:00:29 +0000467 MCSymbolData *Data = &OrigData;
468 if (Data->isCommon() && Data->isExternal())
469 return Data->getCommonAlignment();
Rafael Espindola9735f4f2010-09-27 21:23:02 +0000470
Rafael Espindola66f96fe2014-03-21 22:00:29 +0000471 const MCSymbol *Symbol = &Data->getSymbol();
472 bool IsThumbFunc = OrigData.getFlags() & ELF_Other_ThumbFunc;
Roman Divacky55184dd2010-12-20 21:14:39 +0000473
Rafael Espindola66f96fe2014-03-21 22:00:29 +0000474 uint64_t Res = 0;
475 if (Symbol->isVariable()) {
476 const MCExpr *Expr = Symbol->getVariableValue();
477 MCValue Value;
478 if (!Expr->EvaluateAsRelocatable(Value, &Layout))
479 llvm_unreachable("Invalid expression");
480
481 assert(!Value.getSymB());
482
483 Res = Value.getConstant();
484
485 if (const MCSymbolRefExpr *A = Value.getSymA()) {
486 Symbol = &A->getSymbol();
487 Data = &Layout.getAssembler().getSymbolData(*Symbol);
488 } else {
489 Symbol = 0;
490 Data = 0;
Rafael Espindola7bbd5c22014-03-19 00:13:43 +0000491 }
Roman Divacky55184dd2010-12-20 21:14:39 +0000492 }
493
Rafael Espindola66f96fe2014-03-21 22:00:29 +0000494 if (IsThumbFunc)
495 Res |= 1;
Rafael Espindola9735f4f2010-09-27 21:23:02 +0000496
Rafael Espindola66f96fe2014-03-21 22:00:29 +0000497 if (!Symbol || !Symbol->isInSection())
498 return Res;
Rafael Espindola9735f4f2010-09-27 21:23:02 +0000499
Rafael Espindola66f96fe2014-03-21 22:00:29 +0000500 Res += Layout.getSymbolOffset(Data);
501
502 return Res;
Rafael Espindola9735f4f2010-09-27 21:23:02 +0000503}
504
Rafael Espindola93e3cf02010-12-07 00:27:36 +0000505void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
506 const MCAsmLayout &Layout) {
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000507 // The presence of symbol versions causes undefined symbols and
508 // versions declared with @@@ to be renamed.
509
510 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
511 ie = Asm.symbol_end(); it != ie; ++it) {
512 const MCSymbol &Alias = it->getSymbol();
Rafael Espindola8c3039b2010-11-15 16:33:49 +0000513 const MCSymbol &Symbol = Alias.AliasedSymbol();
Rafael Espindola6cd76e62010-10-28 18:33:03 +0000514 MCSymbolData &SD = Asm.getSymbolData(Symbol);
515
Rafael Espindola6cd76e62010-10-28 18:33:03 +0000516 // Not an alias.
517 if (&Symbol == &Alias)
518 continue;
519
Benjamin Kramer14807272010-10-27 19:53:52 +0000520 StringRef AliasName = Alias.getName();
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000521 size_t Pos = AliasName.find('@');
522 if (Pos == StringRef::npos)
523 continue;
524
Rafael Espindola6cd76e62010-10-28 18:33:03 +0000525 // Aliases defined with .symvar copy the binding from the symbol they alias.
526 // This is the first place we are able to copy this information.
527 it->setExternal(SD.isExternal());
Jan Sjödin30a52de2011-02-28 21:45:04 +0000528 MCELF::SetBinding(*it, MCELF::GetBinding(SD));
Rafael Espindola6cd76e62010-10-28 18:33:03 +0000529
Benjamin Kramer14807272010-10-27 19:53:52 +0000530 StringRef Rest = AliasName.substr(Pos);
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000531 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
532 continue;
533
Rafael Espindola26496e62010-10-27 17:56:18 +0000534 // FIXME: produce a better error message.
535 if (Symbol.isUndefined() && Rest.startswith("@@") &&
536 !Rest.startswith("@@@"))
537 report_fatal_error("A @@ version cannot be undefined");
538
Benjamin Kramer14807272010-10-27 19:53:52 +0000539 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000540 }
541}
542
Roman Divacky5a1c5492014-01-07 20:17:03 +0000543static uint8_t mergeTypeForSet(uint8_t origType, uint8_t newType) {
544 uint8_t Type = newType;
545
546 // Propagation rules:
547 // IFUNC > FUNC > OBJECT > NOTYPE
548 // TLS_OBJECT > OBJECT > NOTYPE
549 //
550 // dont let the new type degrade the old type
551 switch (origType) {
552 default:
553 break;
554 case ELF::STT_GNU_IFUNC:
555 if (Type == ELF::STT_FUNC || Type == ELF::STT_OBJECT ||
556 Type == ELF::STT_NOTYPE || Type == ELF::STT_TLS)
557 Type = ELF::STT_GNU_IFUNC;
558 break;
559 case ELF::STT_FUNC:
560 if (Type == ELF::STT_OBJECT || Type == ELF::STT_NOTYPE ||
561 Type == ELF::STT_TLS)
562 Type = ELF::STT_FUNC;
563 break;
564 case ELF::STT_OBJECT:
565 if (Type == ELF::STT_NOTYPE)
566 Type = ELF::STT_OBJECT;
567 break;
568 case ELF::STT_TLS:
569 if (Type == ELF::STT_OBJECT || Type == ELF::STT_NOTYPE ||
570 Type == ELF::STT_GNU_IFUNC || Type == ELF::STT_FUNC)
571 Type = ELF::STT_TLS;
572 break;
573 }
574
575 return Type;
576}
577
Rafael Espindolaa6e3a592014-03-23 03:33:20 +0000578static const MCSymbol *getBaseSymbol(const MCAsmLayout &Layout,
579 const MCSymbol &Symbol) {
580 if (!Symbol.isVariable())
581 return &Symbol;
582
583 const MCExpr *Expr = Symbol.getVariableValue();
584 MCValue Value;
585 if (!Expr->EvaluateAsRelocatable(Value, &Layout))
586 llvm_unreachable("Invalid Expression");
587 assert(!Value.getSymB());
588 const MCSymbolRefExpr *A = Value.getSymA();
589 if (!A)
590 return nullptr;
591 return getBaseSymbol(Layout, A->getSymbol());
592}
593
Daniel Dunbarfe0c28f2010-11-13 07:33:40 +0000594void ELFObjectWriter::WriteSymbol(MCDataFragment *SymtabF,
595 MCDataFragment *ShndxF,
596 ELFSymbolData &MSD,
597 const MCAsmLayout &Layout) {
Rafael Espindola5f2d6a52010-10-06 21:02:29 +0000598 MCSymbolData &OrigData = *MSD.SymbolData;
Rafael Espindola7bbd5c22014-03-19 00:13:43 +0000599 MCSymbolData &Data =
600 Layout.getAssembler().getSymbolData(OrigData.getSymbol().AliasedSymbol());
Rafael Espindola5f2d6a52010-10-06 21:02:29 +0000601
Rafael Espindola3fe87a12010-10-31 00:16:26 +0000602 bool IsReserved = Data.isCommon() || Data.getSymbol().isAbsolute() ||
603 Data.getSymbol().isVariable();
604
Jack Carter2f8d9d92013-02-19 21:57:35 +0000605 // Binding and Type share the same byte as upper and lower nibbles
Jan Sjödin30a52de2011-02-28 21:45:04 +0000606 uint8_t Binding = MCELF::GetBinding(OrigData);
Rafael Espindolaa6e3a592014-03-23 03:33:20 +0000607 uint8_t Type = MCELF::GetType(OrigData);
608 const MCSymbol *Base = getBaseSymbol(Layout, OrigData.getSymbol());
609 if (Base) {
610 MCSymbolData BaseSD = Layout.getAssembler().getSymbolData(*Base);
611 Type = mergeTypeForSet(Type, MCELF::GetType(BaseSD));
612 }
Saleem Abdulrasool39f773f2014-03-20 06:05:33 +0000613 if (OrigData.getFlags() & ELF_Other_ThumbFunc)
614 Type = ELF::STT_FUNC;
Rafael Espindola5f2d6a52010-10-06 21:02:29 +0000615 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
Jack Carter2f8d9d92013-02-19 21:57:35 +0000616
Joerg Sonnenbergerfc184732013-10-29 01:06:17 +0000617 // Other and Visibility share the same byte with Visibility using the lower
Jack Carter2f8d9d92013-02-19 21:57:35 +0000618 // 2 bits
619 uint8_t Visibility = MCELF::GetVisibility(OrigData);
Logan Chienee365952013-12-05 00:34:11 +0000620 uint8_t Other = MCELF::getOther(OrigData) << (ELF_STO_Shift - ELF_STV_Shift);
Jack Carter2f8d9d92013-02-19 21:57:35 +0000621 Other |= Visibility;
Rafael Espindola5f2d6a52010-10-06 21:02:29 +0000622
Rafael Espindola66f96fe2014-03-21 22:00:29 +0000623 uint64_t Value = SymbolValue(OrigData, Layout);
Saleem Abdulrasool39f773f2014-03-20 06:05:33 +0000624 if (OrigData.getFlags() & ELF_Other_ThumbFunc)
625 Value |= 1;
Matt Fleming6c1ad482010-08-16 18:57:57 +0000626 uint64_t Size = 0;
Matt Fleming6c1ad482010-08-16 18:57:57 +0000627
Rafael Espindolaf0591c12010-09-21 00:24:38 +0000628 assert(!(Data.isCommon() && !Data.isExternal()));
629
Rafael Espindola73c0ae72010-12-22 16:03:00 +0000630 const MCExpr *ESize = Data.getSize();
631 if (ESize) {
632 int64_t Res;
633 if (!ESize->EvaluateAsAbsolute(Res, Layout))
634 report_fatal_error("Size expression must be absolute.");
635 Size = Res;
Matt Fleming6c1ad482010-08-16 18:57:57 +0000636 }
637
638 // Write out the symbol table entry
Rafael Espindola3fe87a12010-10-31 00:16:26 +0000639 WriteSymbolEntry(SymtabF, ShndxF, MSD.StringIndex, Info, Value,
640 Size, Other, MSD.SectionIndex, IsReserved);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000641}
642
Daniel Dunbarfe0c28f2010-11-13 07:33:40 +0000643void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
644 MCDataFragment *ShndxF,
645 const MCAssembler &Asm,
646 const MCAsmLayout &Layout,
Bruno Cardoso Lopesc9473e92011-11-04 22:24:36 +0000647 const SectionIndexMapTy &SectionIndexMap) {
Matt Fleming6c1ad482010-08-16 18:57:57 +0000648 // The string table must be emitted first because we need the index
649 // into the string table for all the symbol names.
650 assert(StringTable.size() && "Missing string table");
651
652 // FIXME: Make sure the start of the symbol table is aligned.
653
654 // The first entry is the undefined symbol entry.
Rafael Espindola3fe87a12010-10-31 00:16:26 +0000655 WriteSymbolEntry(SymtabF, ShndxF, 0, 0, 0, 0, 0, 0, false);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000656
Joerg Sonnenbergerfc184732013-10-29 01:06:17 +0000657 for (unsigned i = 0, e = FileSymbolData.size(); i != e; ++i) {
658 WriteSymbolEntry(SymtabF, ShndxF, FileSymbolData[i],
659 ELF::STT_FILE | ELF::STB_LOCAL, 0, 0,
660 ELF::STV_DEFAULT, ELF::SHN_ABS, true);
661 }
662
Matt Fleming6c1ad482010-08-16 18:57:57 +0000663 // Write the symbol table entries.
Joerg Sonnenbergerfc184732013-10-29 01:06:17 +0000664 LastLocalSymbolIndex = FileSymbolData.size() + LocalSymbolData.size() + 1;
665
Matt Fleming6c1ad482010-08-16 18:57:57 +0000666 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
667 ELFSymbolData &MSD = LocalSymbolData[i];
Rafael Espindola3fe87a12010-10-31 00:16:26 +0000668 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000669 }
670
Rafael Espindola44bf2662010-09-16 19:46:31 +0000671 // Write out a symbol table entry for each regular section.
Rafael Espindola18014102010-11-10 22:16:43 +0000672 for (MCAssembler::const_iterator i = Asm.begin(), e = Asm.end(); i != e;
673 ++i) {
Eli Friedman1fe0d532010-08-16 21:17:09 +0000674 const MCSectionELF &Section =
Rafael Espindola18014102010-11-10 22:16:43 +0000675 static_cast<const MCSectionELF&>(i->getSection());
676 if (Section.getType() == ELF::SHT_RELA ||
677 Section.getType() == ELF::SHT_REL ||
678 Section.getType() == ELF::SHT_STRTAB ||
Nick Lewycky133a1682011-10-07 23:29:53 +0000679 Section.getType() == ELF::SHT_SYMTAB ||
680 Section.getType() == ELF::SHT_SYMTAB_SHNDX)
Eli Friedman1fe0d532010-08-16 21:17:09 +0000681 continue;
Rafael Espindola3fe87a12010-10-31 00:16:26 +0000682 WriteSymbolEntry(SymtabF, ShndxF, 0, ELF::STT_SECTION, 0, 0,
Bruno Cardoso Lopesc9473e92011-11-04 22:24:36 +0000683 ELF::STV_DEFAULT, SectionIndexMap.lookup(&Section),
684 false);
Eli Friedman1fe0d532010-08-16 21:17:09 +0000685 LastLocalSymbolIndex++;
686 }
Matt Fleming6c1ad482010-08-16 18:57:57 +0000687
688 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
689 ELFSymbolData &MSD = ExternalSymbolData[i];
690 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola83b2a332010-10-06 16:47:31 +0000691 assert(((Data.getFlags() & ELF_STB_Global) ||
692 (Data.getFlags() & ELF_STB_Weak)) &&
693 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola3fe87a12010-10-31 00:16:26 +0000694 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Jan Sjödin30a52de2011-02-28 21:45:04 +0000695 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming6c1ad482010-08-16 18:57:57 +0000696 LastLocalSymbolIndex++;
697 }
698
699 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
700 ELFSymbolData &MSD = UndefinedSymbolData[i];
701 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola3fe87a12010-10-31 00:16:26 +0000702 WriteSymbol(SymtabF, ShndxF, MSD, Layout);
Jan Sjödin30a52de2011-02-28 21:45:04 +0000703 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming6c1ad482010-08-16 18:57:57 +0000704 LastLocalSymbolIndex++;
705 }
706}
707
Rafael Espindola240028d2010-11-14 23:53:26 +0000708const MCSymbol *ELFObjectWriter::SymbolToReloc(const MCAssembler &Asm,
709 const MCValue &Target,
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000710 const MCFragment &F,
Jason W Kimc09e7262011-05-11 22:53:06 +0000711 const MCFixup &Fixup,
712 bool IsPCRel) const {
Rafael Espindola240028d2010-11-14 23:53:26 +0000713 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
Rafael Espindola8c3039b2010-11-15 16:33:49 +0000714 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
715 const MCSymbol *Renamed = Renames.lookup(&Symbol);
716 const MCSymbolData &SD = Asm.getSymbolData(Symbol);
Rafael Espindola240028d2010-11-14 23:53:26 +0000717
Rafael Espindola8c3039b2010-11-15 16:33:49 +0000718 if (ASymbol.isUndefined()) {
719 if (Renamed)
720 return Renamed;
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +0000721 return undefinedExplicitRelSym(Target, Fixup, IsPCRel);
Rafael Espindola240028d2010-11-14 23:53:26 +0000722 }
Rafael Espindola240028d2010-11-14 23:53:26 +0000723
Rafael Espindola8c3039b2010-11-15 16:33:49 +0000724 if (SD.isExternal()) {
725 if (Renamed)
726 return Renamed;
727 return &Symbol;
728 }
Rafael Espindola75d65b92010-09-25 05:42:19 +0000729
Rafael Espindolaf987d5e2010-09-30 20:18:35 +0000730 const MCSectionELF &Section =
Rafael Espindola8c3039b2010-11-15 16:33:49 +0000731 static_cast<const MCSectionELF&>(ASymbol.getSection());
Rafael Espindola9f75d5d2010-11-24 21:57:39 +0000732 const SectionKind secKind = Section.getKind();
Rafael Espindolaf987d5e2010-09-30 20:18:35 +0000733
Rafael Espindola9f75d5d2010-11-24 21:57:39 +0000734 if (secKind.isBSS())
Jason W Kimc09e7262011-05-11 22:53:06 +0000735 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
Rafael Espindolaf987d5e2010-09-30 20:18:35 +0000736
Rafael Espindola9f75d5d2010-11-24 21:57:39 +0000737 if (secKind.isThreadLocal()) {
738 if (Renamed)
739 return Renamed;
740 return &Symbol;
741 }
742
Rafael Espindola8f3d2c92010-10-06 16:23:36 +0000743 MCSymbolRefExpr::VariantKind Kind = Target.getSymA()->getKind();
Rafael Espindolad7565c32010-10-05 23:57:26 +0000744 const MCSectionELF &Sec2 =
745 static_cast<const MCSectionELF&>(F.getParent()->getSection());
746
Rafael Espindola8f3d2c92010-10-06 16:23:36 +0000747 if (&Sec2 != &Section &&
Rafael Espindola4464e082010-10-18 16:38:04 +0000748 (Kind == MCSymbolRefExpr::VK_PLT ||
749 Kind == MCSymbolRefExpr::VK_GOTPCREL ||
Rafael Espindola9f75d5d2010-11-24 21:57:39 +0000750 Kind == MCSymbolRefExpr::VK_GOTOFF)) {
Rafael Espindola8c3039b2010-11-15 16:33:49 +0000751 if (Renamed)
752 return Renamed;
753 return &Symbol;
754 }
Rafael Espindolad7565c32010-10-05 23:57:26 +0000755
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000756 if (Section.getFlags() & ELF::SHF_MERGE) {
Rafael Espindola8c3039b2010-11-15 16:33:49 +0000757 if (Target.getConstant() == 0)
Jason W Kimc09e7262011-05-11 22:53:06 +0000758 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
Rafael Espindola8c3039b2010-11-15 16:33:49 +0000759 if (Renamed)
760 return Renamed;
761 return &Symbol;
Rafael Espindola240028d2010-11-14 23:53:26 +0000762 }
Rafael Espindola4464e082010-10-18 16:38:04 +0000763
Jason W Kimc09e7262011-05-11 22:53:06 +0000764 return ExplicitRelSym(Asm, Target, F, Fixup, IsPCRel);
765
Rafael Espindola75d65b92010-09-25 05:42:19 +0000766}
767
Matt Fleming6c1ad482010-08-16 18:57:57 +0000768
Jason W Kim495c2bb2010-12-06 21:57:34 +0000769void ELFObjectWriter::RecordRelocation(const MCAssembler &Asm,
770 const MCAsmLayout &Layout,
771 const MCFragment *Fragment,
772 const MCFixup &Fixup,
773 MCValue Target,
774 uint64_t &FixedValue) {
775 int64_t Addend = 0;
776 int Index = 0;
777 int64_t Value = Target.getConstant();
778 const MCSymbol *RelocSymbol = NULL;
779
Rafael Espindoladf633352010-12-17 07:28:17 +0000780 bool IsPCRel = isFixupKindPCRel(Asm, Fixup.getKind());
Jason W Kim495c2bb2010-12-06 21:57:34 +0000781 if (!Target.isAbsolute()) {
782 const MCSymbol &Symbol = Target.getSymA()->getSymbol();
783 const MCSymbol &ASymbol = Symbol.AliasedSymbol();
Jason W Kimc09e7262011-05-11 22:53:06 +0000784 RelocSymbol = SymbolToReloc(Asm, Target, *Fragment, Fixup, IsPCRel);
Jason W Kim495c2bb2010-12-06 21:57:34 +0000785
786 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
787 const MCSymbol &SymbolB = RefB->getSymbol();
788 MCSymbolData &SDB = Asm.getSymbolData(SymbolB);
789 IsPCRel = true;
790
David Majnemera45a1762014-01-06 07:39:46 +0000791 if (!SDB.getFragment())
792 Asm.getContext().FatalError(
793 Fixup.getLoc(),
794 Twine("symbol '") + SymbolB.getName() +
795 "' can not be undefined in a subtraction expression");
796
Jason W Kim495c2bb2010-12-06 21:57:34 +0000797 // Offset of the symbol in the section
798 int64_t a = Layout.getSymbolOffset(&SDB);
799
Bruno Cardoso Lopesc9473e92011-11-04 22:24:36 +0000800 // Offset of the relocation in the section
Jason W Kim495c2bb2010-12-06 21:57:34 +0000801 int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
802 Value += b - a;
803 }
804
805 if (!RelocSymbol) {
806 MCSymbolData &SD = Asm.getSymbolData(ASymbol);
807 MCFragment *F = SD.getFragment();
808
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +0000809 if (F) {
810 Index = F->getParent()->getOrdinal() + 1;
811 // Offset of the symbol in the section
812 Value += Layout.getSymbolOffset(&SD);
813 } else {
814 Index = 0;
815 }
Jason W Kim495c2bb2010-12-06 21:57:34 +0000816 } else {
Rafael Espindola7fadc0e2014-03-20 02:12:01 +0000817 if (Target.getSymA()->getKind() == MCSymbolRefExpr::VK_WEAKREF)
Jason W Kim495c2bb2010-12-06 21:57:34 +0000818 WeakrefUsedInReloc.insert(RelocSymbol);
819 else
820 UsedInReloc.insert(RelocSymbol);
821 Index = -1;
822 }
823 Addend = Value;
Michael Liaod6f31682012-10-16 19:49:51 +0000824 if (hasRelocationAddend())
Jason W Kim495c2bb2010-12-06 21:57:34 +0000825 Value = 0;
826 }
827
828 FixedValue = Value;
829 unsigned Type = GetRelocType(Target, Fixup, IsPCRel,
830 (RelocSymbol != 0), Addend);
Rafael Espindola9e252bf2011-12-21 14:26:29 +0000831 MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
832 MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
833 if (RelocNeedsGOT(Modifier))
834 NeedsGOT = true;
Jan Sjödin30a52de2011-02-28 21:45:04 +0000835
Jason W Kim495c2bb2010-12-06 21:57:34 +0000836 uint64_t RelocOffset = Layout.getFragmentOffset(Fragment) +
837 Fixup.getOffset();
Roman Divackydfbecd12011-08-04 19:08:19 +0000838
Rafael Espindolafdaae0d2010-12-18 03:27:34 +0000839 if (!hasRelocationAddend())
840 Addend = 0;
Rafael Espindolad7facaf2011-08-04 13:05:26 +0000841
842 if (is64Bit())
843 assert(isInt<64>(Addend));
844 else
845 assert(isInt<32>(Addend));
846
Akira Hatanaka64ad2cf2012-03-23 23:06:45 +0000847 ELFRelocationEntry ERE(RelocOffset, Index, Type, RelocSymbol, Addend, Fixup);
Jason W Kim495c2bb2010-12-06 21:57:34 +0000848 Relocations[Fragment->getParent()].push_back(ERE);
849}
850
851
Benjamin Kramer86511dc2010-08-23 21:19:37 +0000852uint64_t
Daniel Dunbarfe0c28f2010-11-13 07:33:40 +0000853ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
854 const MCSymbol *S) {
Benjamin Kramer37b384c2010-08-25 20:09:43 +0000855 MCSymbolData &SD = Asm.getSymbolData(*S);
Rafael Espindola0e3decf2010-11-14 03:12:24 +0000856 return SD.getIndex();
Matt Fleming6c1ad482010-08-16 18:57:57 +0000857}
858
Jan Sjödin30a52de2011-02-28 21:45:04 +0000859bool ELFObjectWriter::isInSymtab(const MCAssembler &Asm,
860 const MCSymbolData &Data,
861 bool Used, bool Renamed) {
Rafael Espindola7fadc0e2014-03-20 02:12:01 +0000862 const MCSymbol &Symbol = Data.getSymbol();
863 if (Symbol.isVariable()) {
864 const MCExpr *Expr = Symbol.getVariableValue();
865 if (const MCSymbolRefExpr *Ref = dyn_cast<MCSymbolRefExpr>(Expr)) {
866 if (Ref->getKind() == MCSymbolRefExpr::VK_WEAKREF)
867 return false;
868 }
869 }
Rafael Espindola16145972010-11-01 14:28:48 +0000870
Rafael Espindolaee8d1512010-10-19 19:31:37 +0000871 if (Used)
872 return true;
873
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000874 if (Renamed)
875 return false;
876
Rafael Espindola45834a02010-10-29 23:09:31 +0000877 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
878 return true;
879
Rafael Espindola8c3039b2010-11-15 16:33:49 +0000880 const MCSymbol &A = Symbol.AliasedSymbol();
Rafael Espindola9e18e962011-02-23 20:22:07 +0000881 if (Symbol.isVariable() && !A.isVariable() && A.isUndefined())
882 return false;
883
Jan Sjödin30a52de2011-02-28 21:45:04 +0000884 bool IsGlobal = MCELF::GetBinding(Data) == ELF::STB_GLOBAL;
Rafael Espindola9e18e962011-02-23 20:22:07 +0000885 if (!Symbol.isVariable() && Symbol.isUndefined() && !IsGlobal)
Rafael Espindolaa5efd6a2010-10-27 14:44:52 +0000886 return false;
887
Rafael Espindolaee8d1512010-10-19 19:31:37 +0000888 if (Symbol.isTemporary())
Rafael Espindolab1d07892010-10-05 18:01:23 +0000889 return false;
890
891 return true;
892}
893
Jan Sjödin30a52de2011-02-28 21:45:04 +0000894bool ELFObjectWriter::isLocal(const MCSymbolData &Data, bool isSignature,
895 bool isUsedInReloc) {
Rafael Espindolab1d07892010-10-05 18:01:23 +0000896 if (Data.isExternal())
897 return false;
898
899 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindola8c3039b2010-11-15 16:33:49 +0000900 const MCSymbol &RefSymbol = Symbol.AliasedSymbol();
Rafael Espindola7d0ba342010-11-14 04:17:37 +0000901
902 if (RefSymbol.isUndefined() && !RefSymbol.isVariable()) {
903 if (isSignature && !isUsedInReloc)
904 return true;
905
Rafael Espindolab1d07892010-10-05 18:01:23 +0000906 return false;
Rafael Espindola7d0ba342010-11-14 04:17:37 +0000907 }
Rafael Espindolab1d07892010-10-05 18:01:23 +0000908
909 return true;
910}
911
Daniel Dunbarfe0c28f2010-11-13 07:33:40 +0000912void ELFObjectWriter::ComputeIndexMap(MCAssembler &Asm,
Rafael Espindola1557fd62011-03-20 18:44:20 +0000913 SectionIndexMapTy &SectionIndexMap,
914 const RelMapTy &RelMap) {
Rafael Espindolad6340032010-11-10 21:51:05 +0000915 unsigned Index = 1;
916 for (MCAssembler::iterator it = Asm.begin(),
917 ie = Asm.end(); it != ie; ++it) {
918 const MCSectionELF &Section =
919 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000920 if (Section.getType() != ELF::SHT_GROUP)
921 continue;
922 SectionIndexMap[&Section] = Index++;
923 }
924
925 for (MCAssembler::iterator it = Asm.begin(),
926 ie = Asm.end(); it != ie; ++it) {
927 const MCSectionELF &Section =
928 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindola1557fd62011-03-20 18:44:20 +0000929 if (Section.getType() == ELF::SHT_GROUP ||
930 Section.getType() == ELF::SHT_REL ||
931 Section.getType() == ELF::SHT_RELA)
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000932 continue;
Rafael Espindolad6340032010-11-10 21:51:05 +0000933 SectionIndexMap[&Section] = Index++;
Rafael Espindola1557fd62011-03-20 18:44:20 +0000934 const MCSectionELF *RelSection = RelMap.lookup(&Section);
935 if (RelSection)
936 SectionIndexMap[RelSection] = Index++;
Rafael Espindolad6340032010-11-10 21:51:05 +0000937 }
938}
939
Rafael Espindola022bb762014-03-24 03:43:21 +0000940void
941ELFObjectWriter::computeSymbolTable(MCAssembler &Asm, const MCAsmLayout &Layout,
942 const SectionIndexMapTy &SectionIndexMap,
943 RevGroupMapTy RevGroupMap,
944 unsigned NumRegularSections) {
Rafael Espindolad03e81d2010-10-05 15:48:37 +0000945 // FIXME: Is this the correct place to do this?
Rafael Espindola940a0ee2011-06-05 01:20:06 +0000946 // FIXME: Why is an undefined reference to _GLOBAL_OFFSET_TABLE_ needed?
Rafael Espindolad03e81d2010-10-05 15:48:37 +0000947 if (NeedsGOT) {
Dmitri Gribenko226fea52013-01-13 16:01:15 +0000948 StringRef Name = "_GLOBAL_OFFSET_TABLE_";
Rafael Espindolad03e81d2010-10-05 15:48:37 +0000949 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
950 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
951 Data.setExternal(true);
Jan Sjödin30a52de2011-02-28 21:45:04 +0000952 MCELF::SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindolad03e81d2010-10-05 15:48:37 +0000953 }
954
Matt Fleming6c1ad482010-08-16 18:57:57 +0000955 // Index 0 is always the empty string.
956 StringMap<uint64_t> StringIndexMap;
957 StringTable += '\x00';
958
Rafael Espindolab80875e2011-04-07 23:21:52 +0000959 // FIXME: We could optimize suffixes in strtab in the same way we
960 // optimize them in shstrtab.
961
Joerg Sonnenbergerfc184732013-10-29 01:06:17 +0000962 for (MCAssembler::const_file_name_iterator it = Asm.file_names_begin(),
963 ie = Asm.file_names_end();
964 it != ie;
965 ++it) {
966 StringRef Name = *it;
967 uint64_t &Entry = StringIndexMap[Name];
968 if (!Entry) {
969 Entry = StringTable.size();
970 StringTable += Name;
971 StringTable += '\x00';
972 }
973 FileSymbolData.push_back(Entry);
974 }
975
Rafael Espindolabee6e9f2010-10-14 16:34:44 +0000976 // Add the data for the symbols.
Matt Fleming6c1ad482010-08-16 18:57:57 +0000977 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
978 ie = Asm.symbol_end(); it != ie; ++it) {
979 const MCSymbol &Symbol = it->getSymbol();
980
Rafael Espindola16145972010-11-01 14:28:48 +0000981 bool Used = UsedInReloc.count(&Symbol);
982 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
Rafael Espindola7d0ba342010-11-14 04:17:37 +0000983 bool isSignature = RevGroupMap.count(&Symbol);
984
985 if (!isInSymtab(Asm, *it,
986 Used || WeakrefUsed || isSignature,
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000987 Renames.count(&Symbol)))
Matt Fleming6c1ad482010-08-16 18:57:57 +0000988 continue;
989
Matt Fleming6c1ad482010-08-16 18:57:57 +0000990 ELFSymbolData MSD;
991 MSD.SymbolData = it;
Rafael Espindola022bb762014-03-24 03:43:21 +0000992 const MCSymbol *BaseSymbol = getBaseSymbol(Layout, Symbol);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000993
Rafael Espindola7d0ba342010-11-14 04:17:37 +0000994 // Undefined symbols are global, but this is the first place we
995 // are able to set it.
996 bool Local = isLocal(*it, isSignature, Used);
Jan Sjödin30a52de2011-02-28 21:45:04 +0000997 if (!Local && MCELF::GetBinding(*it) == ELF::STB_LOCAL) {
Rafael Espindola022bb762014-03-24 03:43:21 +0000998 assert(BaseSymbol);
999 MCSymbolData &SD = Asm.getSymbolData(*BaseSymbol);
Jan Sjödin30a52de2011-02-28 21:45:04 +00001000 MCELF::SetBinding(*it, ELF::STB_GLOBAL);
1001 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
Rafael Espindola7d0ba342010-11-14 04:17:37 +00001002 }
1003
Rafael Espindola022bb762014-03-24 03:43:21 +00001004 if (!BaseSymbol) {
1005 MSD.SectionIndex = ELF::SHN_ABS;
1006 } else if (it->isCommon()) {
Rafael Espindolabee6e9f2010-10-14 16:34:44 +00001007 assert(!Local);
Rafael Espindolaf0591c12010-09-21 00:24:38 +00001008 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindola022bb762014-03-24 03:43:21 +00001009 } else if (BaseSymbol->isUndefined()) {
Rafael Espindola7d0ba342010-11-14 04:17:37 +00001010 if (isSignature && !Used)
1011 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap[&Symbol]);
1012 else
1013 MSD.SectionIndex = ELF::SHN_UNDEF;
Rafael Espindola022bb762014-03-24 03:43:21 +00001014 if (!Used && WeakrefUsed)
1015 MCELF::SetBinding(*it, ELF::STB_WEAK);
Matt Fleming6c1ad482010-08-16 18:57:57 +00001016 } else {
Rafael Espindolad6340032010-11-10 21:51:05 +00001017 const MCSectionELF &Section =
Rafael Espindola022bb762014-03-24 03:43:21 +00001018 static_cast<const MCSectionELF&>(BaseSymbol->getSection());
Rafael Espindolad6340032010-11-10 21:51:05 +00001019 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Rafael Espindola3fe87a12010-10-31 00:16:26 +00001020 if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
1021 NeedsSymtabShndx = true;
Matt Fleming6c1ad482010-08-16 18:57:57 +00001022 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindolafbcf0db2010-10-15 15:39:06 +00001023 }
1024
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +00001025 // The @@@ in symbol version is replaced with @ in undefined symbols and
1026 // @@ in defined ones.
1027 StringRef Name = Symbol.getName();
Benjamin Kramerdcc77322010-11-12 19:26:04 +00001028 SmallString<32> Buf;
1029
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +00001030 size_t Pos = Name.find("@@@");
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +00001031 if (Pos != StringRef::npos) {
Benjamin Kramerdcc77322010-11-12 19:26:04 +00001032 Buf += Name.substr(0, Pos);
1033 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
1034 Buf += Name.substr(Pos + Skip);
1035 Name = Buf;
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +00001036 }
1037
Benjamin Kramerdcc77322010-11-12 19:26:04 +00001038 uint64_t &Entry = StringIndexMap[Name];
Rafael Espindolaa5efd6a2010-10-27 14:44:52 +00001039 if (!Entry) {
1040 Entry = StringTable.size();
Benjamin Kramerdcc77322010-11-12 19:26:04 +00001041 StringTable += Name;
Rafael Espindolaa5efd6a2010-10-27 14:44:52 +00001042 StringTable += '\x00';
Matt Fleming6c1ad482010-08-16 18:57:57 +00001043 }
Rafael Espindolaa5efd6a2010-10-27 14:44:52 +00001044 MSD.StringIndex = Entry;
1045 if (MSD.SectionIndex == ELF::SHN_UNDEF)
1046 UndefinedSymbolData.push_back(MSD);
1047 else if (Local)
1048 LocalSymbolData.push_back(MSD);
1049 else
1050 ExternalSymbolData.push_back(MSD);
Matt Fleming6c1ad482010-08-16 18:57:57 +00001051 }
1052
1053 // Symbols are required to be in lexicographic order.
1054 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
1055 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
1056 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
1057
1058 // Set the symbol indices. Local symbols must come before all other
1059 // symbols with non-local bindings.
Joerg Sonnenbergerfc184732013-10-29 01:06:17 +00001060 unsigned Index = FileSymbolData.size() + 1;
Matt Fleming6c1ad482010-08-16 18:57:57 +00001061 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
1062 LocalSymbolData[i].SymbolData->setIndex(Index++);
Rafael Espindola0e3decf2010-11-14 03:12:24 +00001063
1064 Index += NumRegularSections;
1065
Matt Fleming6c1ad482010-08-16 18:57:57 +00001066 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
1067 ExternalSymbolData[i].SymbolData->setIndex(Index++);
1068 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
1069 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
Nick Lewycky29e7b312011-10-11 03:54:50 +00001070
Nick Lewycky4288f9e2013-03-09 09:32:16 +00001071 if (Index >= ELF::SHN_LORESERVE)
Nick Lewycky29e7b312011-10-11 03:54:50 +00001072 NeedsSymtabShndx = true;
Matt Fleming6c1ad482010-08-16 18:57:57 +00001073}
1074
Rafael Espindola1557fd62011-03-20 18:44:20 +00001075void ELFObjectWriter::CreateRelocationSections(MCAssembler &Asm,
1076 MCAsmLayout &Layout,
1077 RelMapTy &RelMap) {
1078 for (MCAssembler::const_iterator it = Asm.begin(),
1079 ie = Asm.end(); it != ie; ++it) {
1080 const MCSectionData &SD = *it;
1081 if (Relocations[&SD].empty())
1082 continue;
1083
Matt Fleming6c1ad482010-08-16 18:57:57 +00001084 MCContext &Ctx = Asm.getContext();
Matt Fleming6c1ad482010-08-16 18:57:57 +00001085 const MCSectionELF &Section =
1086 static_cast<const MCSectionELF&>(SD.getSection());
1087
1088 const StringRef SectionName = Section.getSectionName();
Rafael Espindolafdaae0d2010-12-18 03:27:34 +00001089 std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
Matt Fleming6c1ad482010-08-16 18:57:57 +00001090 RelaSectionName += SectionName;
Benjamin Kramerfd054152010-08-17 17:56:13 +00001091
1092 unsigned EntrySize;
Rafael Espindolafdaae0d2010-12-18 03:27:34 +00001093 if (hasRelocationAddend())
1094 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
Benjamin Kramerfd054152010-08-17 17:56:13 +00001095 else
Rafael Espindolafdaae0d2010-12-18 03:27:34 +00001096 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming6c1ad482010-08-16 18:57:57 +00001097
Tim Northovera630fb02013-07-10 20:58:17 +00001098 unsigned Flags = 0;
1099 StringRef Group = "";
1100 if (Section.getFlags() & ELF::SHF_GROUP) {
David Blaikie1e412ea2013-10-22 20:34:30 +00001101 Flags = ELF::SHF_GROUP;
1102 Group = Section.getGroup()->getName();
Tim Northovera630fb02013-07-10 20:58:17 +00001103 }
1104
Rafael Espindola1557fd62011-03-20 18:44:20 +00001105 const MCSectionELF *RelaSection =
1106 Ctx.getELFSection(RelaSectionName, hasRelocationAddend() ?
Tim Northovera630fb02013-07-10 20:58:17 +00001107 ELF::SHT_RELA : ELF::SHT_REL, Flags,
Rafael Espindola1557fd62011-03-20 18:44:20 +00001108 SectionKind::getReadOnly(),
Tim Northovera630fb02013-07-10 20:58:17 +00001109 EntrySize, Group);
Rafael Espindola1557fd62011-03-20 18:44:20 +00001110 RelMap[&Section] = RelaSection;
1111 Asm.getOrCreateSectionData(*RelaSection);
1112 }
1113}
Matt Fleming6c1ad482010-08-16 18:57:57 +00001114
Rafael Espindola1557fd62011-03-20 18:44:20 +00001115void ELFObjectWriter::WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout,
1116 const RelMapTy &RelMap) {
1117 for (MCAssembler::const_iterator it = Asm.begin(),
1118 ie = Asm.end(); it != ie; ++it) {
1119 const MCSectionData &SD = *it;
1120 const MCSectionELF &Section =
1121 static_cast<const MCSectionELF&>(SD.getSection());
1122
1123 const MCSectionELF *RelaSection = RelMap.lookup(&Section);
1124 if (!RelaSection)
1125 continue;
Matt Fleming6c1ad482010-08-16 18:57:57 +00001126 MCSectionData &RelaSD = Asm.getOrCreateSectionData(*RelaSection);
Rafael Espindolafdaae0d2010-12-18 03:27:34 +00001127 RelaSD.setAlignment(is64Bit() ? 8 : 4);
Matt Fleming6c1ad482010-08-16 18:57:57 +00001128
1129 MCDataFragment *F = new MCDataFragment(&RelaSD);
Rafael Espindola1557fd62011-03-20 18:44:20 +00001130 WriteRelocationsFragment(Asm, F, &*it);
Matt Fleming6c1ad482010-08-16 18:57:57 +00001131 }
1132}
1133
Daniel Dunbarfe0c28f2010-11-13 07:33:40 +00001134void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
1135 uint64_t Flags, uint64_t Address,
1136 uint64_t Offset, uint64_t Size,
1137 uint32_t Link, uint32_t Info,
1138 uint64_t Alignment,
1139 uint64_t EntrySize) {
Matt Fleming6c1ad482010-08-16 18:57:57 +00001140 Write32(Name); // sh_name: index into string table
1141 Write32(Type); // sh_type
1142 WriteWord(Flags); // sh_flags
1143 WriteWord(Address); // sh_addr
1144 WriteWord(Offset); // sh_offset
1145 WriteWord(Size); // sh_size
1146 Write32(Link); // sh_link
1147 Write32(Info); // sh_info
1148 WriteWord(Alignment); // sh_addralign
1149 WriteWord(EntrySize); // sh_entsize
1150}
1151
Daniel Dunbarfe0c28f2010-11-13 07:33:40 +00001152void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
1153 MCDataFragment *F,
1154 const MCSectionData *SD) {
Matt Fleming6c1ad482010-08-16 18:57:57 +00001155 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
Akira Hatanaka64ad2cf2012-03-23 23:06:45 +00001156
1157 // Sort the relocation entries. Most targets just sort by r_offset, but some
1158 // (e.g., MIPS) have additional constraints.
1159 TargetObjectWriter->sortRelocs(Asm, Relocs);
Matt Fleming6c1ad482010-08-16 18:57:57 +00001160
1161 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1162 ELFRelocationEntry entry = Relocs[e - i - 1];
1163
Rafael Espindola26cb15a2010-11-21 00:48:25 +00001164 if (!entry.Index)
1165 ;
1166 else if (entry.Index < 0)
Rafael Espindolabce26a12010-10-05 15:11:03 +00001167 entry.Index = getSymbolIndexInSymbolTable(Asm, entry.Symbol);
1168 else
Joerg Sonnenbergerfc184732013-10-29 01:06:17 +00001169 entry.Index += FileSymbolData.size() + LocalSymbolData.size();
Rafael Espindolafdaae0d2010-12-18 03:27:34 +00001170 if (is64Bit()) {
Rafael Espindola51d68332010-11-10 20:02:59 +00001171 String64(*F, entry.r_offset);
Jack Carter8ad0c272012-06-27 22:28:30 +00001172 if (TargetObjectWriter->isN64()) {
1173 String32(*F, entry.Index);
Benjamin Kramer6c3c3492010-09-09 18:01:29 +00001174
Jack Carter8ad0c272012-06-27 22:28:30 +00001175 String8(*F, TargetObjectWriter->getRSsym(entry.Type));
1176 String8(*F, TargetObjectWriter->getRType3(entry.Type));
1177 String8(*F, TargetObjectWriter->getRType2(entry.Type));
1178 String8(*F, TargetObjectWriter->getRType(entry.Type));
1179 }
1180 else {
1181 struct ELF::Elf64_Rela ERE64;
1182 ERE64.setSymbolAndType(entry.Index, entry.Type);
1183 String64(*F, ERE64.r_info);
1184 }
Rafael Espindolafdaae0d2010-12-18 03:27:34 +00001185 if (hasRelocationAddend())
Rafael Espindola51d68332010-11-10 20:02:59 +00001186 String64(*F, entry.r_addend);
Benjamin Kramer6c3c3492010-09-09 18:01:29 +00001187 } else {
Rafael Espindola51d68332010-11-10 20:02:59 +00001188 String32(*F, entry.r_offset);
Benjamin Kramer6c3c3492010-09-09 18:01:29 +00001189
Rafael Espindolabce26a12010-10-05 15:11:03 +00001190 struct ELF::Elf32_Rela ERE32;
1191 ERE32.setSymbolAndType(entry.Index, entry.Type);
Rafael Espindola51d68332010-11-10 20:02:59 +00001192 String32(*F, ERE32.r_info);
Benjamin Kramer6c3c3492010-09-09 18:01:29 +00001193
Rafael Espindolafdaae0d2010-12-18 03:27:34 +00001194 if (hasRelocationAddend())
Rafael Espindola51d68332010-11-10 20:02:59 +00001195 String32(*F, entry.r_addend);
Benjamin Kramer6c3c3492010-09-09 18:01:29 +00001196 }
Matt Fleming6c1ad482010-08-16 18:57:57 +00001197 }
1198}
1199
Benjamin Kramer8817cca2013-09-22 14:09:50 +00001200static int compareBySuffix(const MCSectionELF *const *a,
1201 const MCSectionELF *const *b) {
1202 const StringRef &NameA = (*a)->getSectionName();
1203 const StringRef &NameB = (*b)->getSectionName();
Rafael Espindolab80875e2011-04-07 23:21:52 +00001204 const unsigned sizeA = NameA.size();
1205 const unsigned sizeB = NameB.size();
1206 const unsigned len = std::min(sizeA, sizeB);
1207 for (unsigned int i = 0; i < len; ++i) {
1208 char ca = NameA[sizeA - i - 1];
1209 char cb = NameB[sizeB - i - 1];
1210 if (ca != cb)
1211 return cb - ca;
1212 }
1213
1214 return sizeB - sizeA;
1215}
1216
Daniel Dunbarfe0c28f2010-11-13 07:33:40 +00001217void ELFObjectWriter::CreateMetadataSections(MCAssembler &Asm,
1218 MCAsmLayout &Layout,
Rafael Espindola1557fd62011-03-20 18:44:20 +00001219 SectionIndexMapTy &SectionIndexMap,
1220 const RelMapTy &RelMap) {
Matt Fleming6c1ad482010-08-16 18:57:57 +00001221 MCContext &Ctx = Asm.getContext();
1222 MCDataFragment *F;
1223
Rafael Espindolafdaae0d2010-12-18 03:27:34 +00001224 unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
Matt Fleming6c1ad482010-08-16 18:57:57 +00001225
Rafael Espindola0e527b72010-09-22 19:04:41 +00001226 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola36ef57d2010-11-10 19:05:07 +00001227 const MCSectionELF *ShstrtabSection =
Rafael Espindola3fe87a12010-10-31 00:16:26 +00001228 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0,
Rafael Espindola19fa3802010-11-11 03:40:25 +00001229 SectionKind::getReadOnly());
Rafael Espindola44bf2662010-09-16 19:46:31 +00001230 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1231 ShstrtabSD.setAlignment(1);
Rafael Espindola44bf2662010-09-16 19:46:31 +00001232
Rafael Espindola36ef57d2010-11-10 19:05:07 +00001233 const MCSectionELF *SymtabSection =
Rafael Espindola3fe87a12010-10-31 00:16:26 +00001234 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
1235 SectionKind::getReadOnly(),
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001236 EntrySize, "");
Matt Fleming6c1ad482010-08-16 18:57:57 +00001237 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Rafael Espindolafdaae0d2010-12-18 03:27:34 +00001238 SymtabSD.setAlignment(is64Bit() ? 8 : 4);
Rafael Espindola3fe87a12010-10-31 00:16:26 +00001239
1240 MCSectionData *SymtabShndxSD = NULL;
1241
1242 if (NeedsSymtabShndx) {
Rafael Espindola36ef57d2010-11-10 19:05:07 +00001243 const MCSectionELF *SymtabShndxSection =
Rafael Espindola3fe87a12010-10-31 00:16:26 +00001244 Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0,
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001245 SectionKind::getReadOnly(), 4, "");
Rafael Espindola3fe87a12010-10-31 00:16:26 +00001246 SymtabShndxSD = &Asm.getOrCreateSectionData(*SymtabShndxSection);
1247 SymtabShndxSD->setAlignment(4);
1248 }
Matt Fleming6c1ad482010-08-16 18:57:57 +00001249
Rafael Espindola1557fd62011-03-20 18:44:20 +00001250 const MCSectionELF *StrtabSection;
Matt Fleming6c1ad482010-08-16 18:57:57 +00001251 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0,
Rafael Espindola19fa3802010-11-11 03:40:25 +00001252 SectionKind::getReadOnly());
Matt Fleming6c1ad482010-08-16 18:57:57 +00001253 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1254 StrtabSD.setAlignment(1);
Matt Fleming6c1ad482010-08-16 18:57:57 +00001255
Rafael Espindola1557fd62011-03-20 18:44:20 +00001256 ComputeIndexMap(Asm, SectionIndexMap, RelMap);
1257
1258 ShstrtabIndex = SectionIndexMap.lookup(ShstrtabSection);
1259 SymbolTableIndex = SectionIndexMap.lookup(SymtabSection);
1260 StringTableIndex = SectionIndexMap.lookup(StrtabSection);
Rafael Espindola44bf2662010-09-16 19:46:31 +00001261
1262 // Symbol table
1263 F = new MCDataFragment(&SymtabSD);
Rafael Espindola3fe87a12010-10-31 00:16:26 +00001264 MCDataFragment *ShndxF = NULL;
1265 if (NeedsSymtabShndx) {
1266 ShndxF = new MCDataFragment(SymtabShndxSD);
Rafael Espindola3fe87a12010-10-31 00:16:26 +00001267 }
Rafael Espindola18014102010-11-10 22:16:43 +00001268 WriteSymbolTable(F, ShndxF, Asm, Layout, SectionIndexMap);
Rafael Espindola44bf2662010-09-16 19:46:31 +00001269
Matt Fleming6c1ad482010-08-16 18:57:57 +00001270 F = new MCDataFragment(&StrtabSD);
1271 F->getContents().append(StringTable.begin(), StringTable.end());
Matt Fleming6c1ad482010-08-16 18:57:57 +00001272
Matt Fleming6c1ad482010-08-16 18:57:57 +00001273 F = new MCDataFragment(&ShstrtabSD);
1274
Rafael Espindolab80875e2011-04-07 23:21:52 +00001275 std::vector<const MCSectionELF*> Sections;
1276 for (MCAssembler::const_iterator it = Asm.begin(),
1277 ie = Asm.end(); it != ie; ++it) {
1278 const MCSectionELF &Section =
1279 static_cast<const MCSectionELF&>(it->getSection());
1280 Sections.push_back(&Section);
1281 }
1282 array_pod_sort(Sections.begin(), Sections.end(), compareBySuffix);
1283
Matt Fleming6c1ad482010-08-16 18:57:57 +00001284 // Section header string table.
1285 //
1286 // The first entry of a string table holds a null character so skip
1287 // section 0.
1288 uint64_t Index = 1;
Eli Bendersky84b2a792012-12-07 22:06:56 +00001289 F->getContents().push_back('\x00');
Matt Fleming6c1ad482010-08-16 18:57:57 +00001290
Rafael Espindolab80875e2011-04-07 23:21:52 +00001291 for (unsigned int I = 0, E = Sections.size(); I != E; ++I) {
1292 const MCSectionELF &Section = *Sections[I];
Matt Fleming6c1ad482010-08-16 18:57:57 +00001293
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001294 StringRef Name = Section.getSectionName();
Rafael Espindolab80875e2011-04-07 23:21:52 +00001295 if (I != 0) {
1296 StringRef PreviousName = Sections[I - 1]->getSectionName();
1297 if (PreviousName.endswith(Name)) {
1298 SectionStringTableIndex[&Section] = Index - Name.size() - 1;
1299 continue;
1300 }
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001301 }
Matt Fleming6c1ad482010-08-16 18:57:57 +00001302 // Remember the index into the string table so we can write it
1303 // into the sh_name field of the section header table.
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001304 SectionStringTableIndex[&Section] = Index;
Matt Fleming6c1ad482010-08-16 18:57:57 +00001305
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001306 Index += Name.size() + 1;
Eli Bendersky84b2a792012-12-07 22:06:56 +00001307 F->getContents().append(Name.begin(), Name.end());
1308 F->getContents().push_back('\x00');
Matt Fleming6c1ad482010-08-16 18:57:57 +00001309 }
Rafael Espindola2ebaee92010-09-30 02:22:20 +00001310}
1311
Rafael Espindolab3eca9b2011-01-23 17:55:27 +00001312void ELFObjectWriter::CreateIndexedSections(MCAssembler &Asm,
1313 MCAsmLayout &Layout,
1314 GroupMapTy &GroupMap,
Rafael Espindola1557fd62011-03-20 18:44:20 +00001315 RevGroupMapTy &RevGroupMap,
1316 SectionIndexMapTy &SectionIndexMap,
1317 const RelMapTy &RelMap) {
Rafael Espindolab3eca9b2011-01-23 17:55:27 +00001318 // Create the .note.GNU-stack section if needed.
1319 MCContext &Ctx = Asm.getContext();
1320 if (Asm.getNoExecStack()) {
1321 const MCSectionELF *GnuStackSection =
1322 Ctx.getELFSection(".note.GNU-stack", ELF::SHT_PROGBITS, 0,
1323 SectionKind::getReadOnly());
1324 Asm.getOrCreateSectionData(*GnuStackSection);
1325 }
1326
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001327 // Build the groups
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001328 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1329 it != ie; ++it) {
1330 const MCSectionELF &Section =
1331 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola0e7e34e2011-01-23 04:43:11 +00001332 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001333 continue;
1334
1335 const MCSymbol *SignatureSymbol = Section.getGroup();
1336 Asm.getOrCreateSymbolData(*SignatureSymbol);
Rafael Espindola7d0ba342010-11-14 04:17:37 +00001337 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001338 if (!Group) {
Rafael Espindolab3eca9b2011-01-23 17:55:27 +00001339 Group = Ctx.CreateELFGroupSection();
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001340 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1341 Data.setAlignment(4);
1342 MCDataFragment *F = new MCDataFragment(&Data);
1343 String32(*F, ELF::GRP_COMDAT);
1344 }
1345 GroupMap[Group] = SignatureSymbol;
1346 }
1347
Rafael Espindola1557fd62011-03-20 18:44:20 +00001348 ComputeIndexMap(Asm, SectionIndexMap, RelMap);
1349
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001350 // Add sections to the groups
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001351 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
Rafael Espindola1557fd62011-03-20 18:44:20 +00001352 it != ie; ++it) {
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001353 const MCSectionELF &Section =
1354 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola0e7e34e2011-01-23 04:43:11 +00001355 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001356 continue;
Rafael Espindola7d0ba342010-11-14 04:17:37 +00001357 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001358 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1359 // FIXME: we could use the previous fragment
1360 MCDataFragment *F = new MCDataFragment(&Data);
Rafael Espindola1557fd62011-03-20 18:44:20 +00001361 unsigned Index = SectionIndexMap.lookup(&Section);
1362 String32(*F, Index);
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001363 }
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001364}
1365
Daniel Dunbarfe0c28f2010-11-13 07:33:40 +00001366void ELFObjectWriter::WriteSection(MCAssembler &Asm,
1367 const SectionIndexMapTy &SectionIndexMap,
1368 uint32_t GroupSymbolIndex,
1369 uint64_t Offset, uint64_t Size,
1370 uint64_t Alignment,
1371 const MCSectionELF &Section) {
Rafael Espindola5a8d7812010-11-10 23:36:59 +00001372 uint64_t sh_link = 0;
1373 uint64_t sh_info = 0;
1374
1375 switch(Section.getType()) {
1376 case ELF::SHT_DYNAMIC:
1377 sh_link = SectionStringTableIndex[&Section];
1378 sh_info = 0;
1379 break;
1380
1381 case ELF::SHT_REL:
1382 case ELF::SHT_RELA: {
1383 const MCSectionELF *SymtabSection;
1384 const MCSectionELF *InfoSection;
1385 SymtabSection = Asm.getContext().getELFSection(".symtab", ELF::SHT_SYMTAB,
1386 0,
Rafael Espindola19fa3802010-11-11 03:40:25 +00001387 SectionKind::getReadOnly());
Rafael Espindola5a8d7812010-11-10 23:36:59 +00001388 sh_link = SectionIndexMap.lookup(SymtabSection);
1389 assert(sh_link && ".symtab not found");
1390
1391 // Remove ".rel" and ".rela" prefixes.
1392 unsigned SecNameLen = (Section.getType() == ELF::SHT_REL) ? 4 : 5;
1393 StringRef SectionName = Section.getSectionName().substr(SecNameLen);
David Blaikie15b25df2013-10-22 23:41:52 +00001394 StringRef GroupName =
1395 Section.getGroup() ? Section.getGroup()->getName() : "";
Rafael Espindola5a8d7812010-11-10 23:36:59 +00001396
David Blaikie15b25df2013-10-22 23:41:52 +00001397 InfoSection = Asm.getContext().getELFSection(SectionName, ELF::SHT_PROGBITS,
1398 0, SectionKind::getReadOnly(),
1399 0, GroupName);
Rafael Espindola5a8d7812010-11-10 23:36:59 +00001400 sh_info = SectionIndexMap.lookup(InfoSection);
1401 break;
1402 }
1403
1404 case ELF::SHT_SYMTAB:
1405 case ELF::SHT_DYNSYM:
1406 sh_link = StringTableIndex;
1407 sh_info = LastLocalSymbolIndex;
1408 break;
1409
1410 case ELF::SHT_SYMTAB_SHNDX:
1411 sh_link = SymbolTableIndex;
1412 break;
1413
1414 case ELF::SHT_PROGBITS:
1415 case ELF::SHT_STRTAB:
1416 case ELF::SHT_NOBITS:
Rafael Espindola9ae2d052010-12-26 21:30:59 +00001417 case ELF::SHT_NOTE:
Rafael Espindola5a8d7812010-11-10 23:36:59 +00001418 case ELF::SHT_NULL:
1419 case ELF::SHT_ARM_ATTRIBUTES:
Jason W Kim9c5b65d2011-01-12 00:19:25 +00001420 case ELF::SHT_INIT_ARRAY:
1421 case ELF::SHT_FINI_ARRAY:
1422 case ELF::SHT_PREINIT_ARRAY:
Rafael Espindola4b7b7fb2011-01-23 05:43:40 +00001423 case ELF::SHT_X86_64_UNWIND:
Jack Carterc1b17ed2013-01-18 21:20:38 +00001424 case ELF::SHT_MIPS_REGINFO:
1425 case ELF::SHT_MIPS_OPTIONS:
Rafael Espindola5a8d7812010-11-10 23:36:59 +00001426 // Nothing to do.
1427 break;
1428
Nick Lewyckyc6ac5f72011-10-07 23:28:32 +00001429 case ELF::SHT_GROUP:
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001430 sh_link = SymbolTableIndex;
1431 sh_info = GroupSymbolIndex;
1432 break;
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001433
Rafael Espindola5a8d7812010-11-10 23:36:59 +00001434 default:
1435 assert(0 && "FIXME: sh_type value not supported!");
1436 break;
1437 }
1438
Logan Chien4b724422013-02-05 14:18:59 +00001439 if (TargetObjectWriter->getEMachine() == ELF::EM_ARM &&
1440 Section.getType() == ELF::SHT_ARM_EXIDX) {
1441 StringRef SecName(Section.getSectionName());
1442 if (SecName == ".ARM.exidx") {
1443 sh_link = SectionIndexMap.lookup(
1444 Asm.getContext().getELFSection(".text",
1445 ELF::SHT_PROGBITS,
1446 ELF::SHF_EXECINSTR | ELF::SHF_ALLOC,
1447 SectionKind::getText()));
1448 } else if (SecName.startswith(".ARM.exidx")) {
David Blaikie15b25df2013-10-22 23:41:52 +00001449 StringRef GroupName =
1450 Section.getGroup() ? Section.getGroup()->getName() : "";
1451 sh_link = SectionIndexMap.lookup(Asm.getContext().getELFSection(
1452 SecName.substr(sizeof(".ARM.exidx") - 1), ELF::SHT_PROGBITS,
1453 ELF::SHF_EXECINSTR | ELF::SHF_ALLOC, SectionKind::getText(), 0,
1454 GroupName));
Logan Chien4b724422013-02-05 14:18:59 +00001455 }
1456 }
1457
Rafael Espindola5a8d7812010-11-10 23:36:59 +00001458 WriteSecHdrEntry(SectionStringTableIndex[&Section], Section.getType(),
1459 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1460 Alignment, Section.getEntrySize());
1461}
1462
Jan Sjödin30a52de2011-02-28 21:45:04 +00001463bool ELFObjectWriter::IsELFMetaDataSection(const MCSectionData &SD) {
Rafael Espindolabaf2f3b2010-12-06 03:48:09 +00001464 return SD.getOrdinal() == ~UINT32_C(0) &&
Rafael Espindola60ebca92010-12-02 03:09:06 +00001465 !SD.getSection().isVirtualSection();
1466}
1467
Jan Sjödin30a52de2011-02-28 21:45:04 +00001468uint64_t ELFObjectWriter::DataSectionSize(const MCSectionData &SD) {
Rafael Espindola60ebca92010-12-02 03:09:06 +00001469 uint64_t Ret = 0;
1470 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1471 ++i) {
1472 const MCFragment &F = *i;
1473 assert(F.getKind() == MCFragment::FT_Data);
1474 Ret += cast<MCDataFragment>(F).getContents().size();
1475 }
1476 return Ret;
1477}
1478
Jan Sjödin30a52de2011-02-28 21:45:04 +00001479uint64_t ELFObjectWriter::GetSectionFileSize(const MCAsmLayout &Layout,
1480 const MCSectionData &SD) {
Rafael Espindola60ebca92010-12-02 03:09:06 +00001481 if (IsELFMetaDataSection(SD))
1482 return DataSectionSize(SD);
1483 return Layout.getSectionFileSize(&SD);
1484}
1485
Jan Sjödin30a52de2011-02-28 21:45:04 +00001486uint64_t ELFObjectWriter::GetSectionAddressSize(const MCAsmLayout &Layout,
1487 const MCSectionData &SD) {
Rafael Espindola60ebca92010-12-02 03:09:06 +00001488 if (IsELFMetaDataSection(SD))
1489 return DataSectionSize(SD);
Rafael Espindola93e3cf02010-12-07 00:27:36 +00001490 return Layout.getSectionAddressSize(&SD);
Rafael Espindola60ebca92010-12-02 03:09:06 +00001491}
1492
Rafael Espindola1557fd62011-03-20 18:44:20 +00001493void ELFObjectWriter::WriteDataSectionData(MCAssembler &Asm,
1494 const MCAsmLayout &Layout,
1495 const MCSectionELF &Section) {
Rafael Espindola1557fd62011-03-20 18:44:20 +00001496 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1497
Benjamin Kramer084b9f42012-01-20 14:42:32 +00001498 uint64_t Padding = OffsetToAlignment(OS.tell(), SD.getAlignment());
Rafael Espindola1557fd62011-03-20 18:44:20 +00001499 WriteZeros(Padding);
Rafael Espindola1557fd62011-03-20 18:44:20 +00001500
1501 if (IsELFMetaDataSection(SD)) {
1502 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1503 ++i) {
1504 const MCFragment &F = *i;
1505 assert(F.getKind() == MCFragment::FT_Data);
Eli Bendersky84b2a792012-12-07 22:06:56 +00001506 WriteBytes(cast<MCDataFragment>(F).getContents());
Rafael Espindola1557fd62011-03-20 18:44:20 +00001507 }
1508 } else {
Jim Grosbach18e2fe42011-12-06 00:03:48 +00001509 Asm.writeSectionData(&SD, Layout);
Rafael Espindola60ebca92010-12-02 03:09:06 +00001510 }
1511}
1512
Rafael Espindola1557fd62011-03-20 18:44:20 +00001513void ELFObjectWriter::WriteSectionHeader(MCAssembler &Asm,
1514 const GroupMapTy &GroupMap,
1515 const MCAsmLayout &Layout,
1516 const SectionIndexMapTy &SectionIndexMap,
1517 const SectionOffsetMapTy &SectionOffsetMap) {
1518 const unsigned NumSections = Asm.size() + 1;
Matt Fleming6c1ad482010-08-16 18:57:57 +00001519
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001520 std::vector<const MCSectionELF*> Sections;
Rafael Espindola1557fd62011-03-20 18:44:20 +00001521 Sections.resize(NumSections - 1);
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001522
1523 for (SectionIndexMapTy::const_iterator i=
1524 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1525 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
Rafael Espindola1557fd62011-03-20 18:44:20 +00001526 Sections[p.second - 1] = p.first;
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001527 }
1528
Matt Fleming6c1ad482010-08-16 18:57:57 +00001529 // Null section first.
Rafael Espindola3fe87a12010-10-31 00:16:26 +00001530 uint64_t FirstSectionSize =
1531 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1532 uint32_t FirstSectionLink =
1533 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1534 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming6c1ad482010-08-16 18:57:57 +00001535
Rafael Espindola1557fd62011-03-20 18:44:20 +00001536 for (unsigned i = 0; i < NumSections - 1; ++i) {
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001537 const MCSectionELF &Section = *Sections[i];
1538 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1539 uint32_t GroupSymbolIndex;
1540 if (Section.getType() != ELF::SHT_GROUP)
1541 GroupSymbolIndex = 0;
1542 else
Rafael Espindola1557fd62011-03-20 18:44:20 +00001543 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm,
1544 GroupMap.lookup(&Section));
Matt Fleming6c1ad482010-08-16 18:57:57 +00001545
Rafael Espindola93e3cf02010-12-07 00:27:36 +00001546 uint64_t Size = GetSectionAddressSize(Layout, SD);
Rafael Espindola60ebca92010-12-02 03:09:06 +00001547
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001548 WriteSection(Asm, SectionIndexMap, GroupSymbolIndex,
Rafael Espindola1557fd62011-03-20 18:44:20 +00001549 SectionOffsetMap.lookup(&Section), Size,
Rafael Espindola5a8d7812010-11-10 23:36:59 +00001550 SD.getAlignment(), Section);
Matt Fleming6c1ad482010-08-16 18:57:57 +00001551 }
1552}
1553
Rafael Espindola1557fd62011-03-20 18:44:20 +00001554void ELFObjectWriter::ComputeSectionOrder(MCAssembler &Asm,
1555 std::vector<const MCSectionELF*> &Sections) {
1556 for (MCAssembler::iterator it = Asm.begin(),
1557 ie = Asm.end(); it != ie; ++it) {
1558 const MCSectionELF &Section =
1559 static_cast<const MCSectionELF &>(it->getSection());
1560 if (Section.getType() == ELF::SHT_GROUP)
1561 Sections.push_back(&Section);
1562 }
1563
1564 for (MCAssembler::iterator it = Asm.begin(),
1565 ie = Asm.end(); it != ie; ++it) {
1566 const MCSectionELF &Section =
1567 static_cast<const MCSectionELF &>(it->getSection());
1568 if (Section.getType() != ELF::SHT_GROUP &&
1569 Section.getType() != ELF::SHT_REL &&
1570 Section.getType() != ELF::SHT_RELA)
1571 Sections.push_back(&Section);
1572 }
1573
1574 for (MCAssembler::iterator it = Asm.begin(),
1575 ie = Asm.end(); it != ie; ++it) {
1576 const MCSectionELF &Section =
1577 static_cast<const MCSectionELF &>(it->getSection());
1578 if (Section.getType() == ELF::SHT_REL ||
1579 Section.getType() == ELF::SHT_RELA)
1580 Sections.push_back(&Section);
1581 }
1582}
1583
1584void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1585 const MCAsmLayout &Layout) {
1586 GroupMapTy GroupMap;
1587 RevGroupMapTy RevGroupMap;
1588 SectionIndexMapTy SectionIndexMap;
1589
1590 unsigned NumUserSections = Asm.size();
1591
1592 DenseMap<const MCSectionELF*, const MCSectionELF*> RelMap;
1593 CreateRelocationSections(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1594
1595 const unsigned NumUserAndRelocSections = Asm.size();
1596 CreateIndexedSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
1597 RevGroupMap, SectionIndexMap, RelMap);
1598 const unsigned AllSections = Asm.size();
1599 const unsigned NumIndexedSections = AllSections - NumUserAndRelocSections;
1600
1601 unsigned NumRegularSections = NumUserSections + NumIndexedSections;
1602
1603 // Compute symbol table information.
Rafael Espindola022bb762014-03-24 03:43:21 +00001604 computeSymbolTable(Asm, Layout, SectionIndexMap, RevGroupMap,
1605 NumRegularSections);
Rafael Espindola1557fd62011-03-20 18:44:20 +00001606
1607 WriteRelocations(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1608
1609 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
1610 const_cast<MCAsmLayout&>(Layout),
1611 SectionIndexMap,
1612 RelMap);
1613
1614 uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
1615 uint64_t HeaderSize = is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
1616 sizeof(ELF::Elf32_Ehdr);
1617 uint64_t FileOff = HeaderSize;
1618
1619 std::vector<const MCSectionELF*> Sections;
1620 ComputeSectionOrder(Asm, Sections);
1621 unsigned NumSections = Sections.size();
1622 SectionOffsetMapTy SectionOffsetMap;
1623 for (unsigned i = 0; i < NumRegularSections + 1; ++i) {
1624 const MCSectionELF &Section = *Sections[i];
1625 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1626
1627 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1628
1629 // Remember the offset into the file for this section.
1630 SectionOffsetMap[&Section] = FileOff;
1631
1632 // Get the size of the section in the output file (including padding).
1633 FileOff += GetSectionFileSize(Layout, SD);
1634 }
1635
1636 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1637
1638 const unsigned SectionHeaderOffset = FileOff - HeaderSize;
1639
1640 uint64_t SectionHeaderEntrySize = is64Bit() ?
1641 sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr);
1642 FileOff += (NumSections + 1) * SectionHeaderEntrySize;
1643
1644 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i) {
1645 const MCSectionELF &Section = *Sections[i];
1646 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1647
1648 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1649
1650 // Remember the offset into the file for this section.
1651 SectionOffsetMap[&Section] = FileOff;
1652
1653 // Get the size of the section in the output file (including padding).
1654 FileOff += GetSectionFileSize(Layout, SD);
1655 }
1656
1657 // Write out the ELF header ...
Jack Carter1bd90ff2013-01-30 02:09:52 +00001658 WriteHeader(Asm, SectionHeaderOffset, NumSections + 1);
Rafael Espindola1557fd62011-03-20 18:44:20 +00001659
1660 // ... then the regular sections ...
1661 // + because of .shstrtab
1662 for (unsigned i = 0; i < NumRegularSections + 1; ++i)
1663 WriteDataSectionData(Asm, Layout, *Sections[i]);
1664
Benjamin Kramer084b9f42012-01-20 14:42:32 +00001665 uint64_t Padding = OffsetToAlignment(OS.tell(), NaturalAlignment);
Rafael Espindola1557fd62011-03-20 18:44:20 +00001666 WriteZeros(Padding);
1667
1668 // ... then the section header table ...
1669 WriteSectionHeader(Asm, GroupMap, Layout, SectionIndexMap,
1670 SectionOffsetMap);
1671
Nick Lewycky4eb11432011-10-07 20:56:23 +00001672 // ... and then the remaining sections ...
Rafael Espindola1557fd62011-03-20 18:44:20 +00001673 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i)
1674 WriteDataSectionData(Asm, Layout, *Sections[i]);
1675}
1676
Eli Friedmand92d17b2011-03-03 07:24:36 +00001677bool
1678ELFObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
1679 const MCSymbolData &DataA,
1680 const MCFragment &FB,
1681 bool InSet,
1682 bool IsPCRel) const {
Roman Divackyfb4d3902014-01-08 18:50:32 +00001683 if (DataA.getFlags() & ELF_STB_Weak || MCELF::GetType(DataA) == ELF::STT_GNU_IFUNC)
Eli Friedmand92d17b2011-03-03 07:24:36 +00001684 return false;
1685 return MCObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(
1686 Asm, DataA, FB,InSet, IsPCRel);
1687}
1688
Rafael Espindola6b5e56c2010-12-17 17:45:22 +00001689MCObjectWriter *llvm::createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1690 raw_ostream &OS,
Rafael Espindolafdaae0d2010-12-18 03:27:34 +00001691 bool IsLittleEndian) {
Rafael Espindola34a68af2011-12-22 03:24:43 +00001692 return new ELFObjectWriter(MOTW, OS, IsLittleEndian);
Rafael Espindolab264d332011-12-21 17:30:17 +00001693}