blob: c99a3ee5e2601818f525b12d2c14dd9447b051b4 [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"
David Blaikie8019bf82014-04-10 21:53:53 +000020#include "llvm/MC/MCAsmInfo.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"
Rafael Espindola97de4742014-07-03 02:01:39 +000031#include "llvm/MC/StringTableBuilder.h"
David Blaikie8019bf82014-04-10 21:53:53 +000032#include "llvm/Support/Compression.h"
Matt Fleming6c1ad482010-08-16 18:57:57 +000033#include "llvm/Support/Debug.h"
Matt Fleming6c1ad482010-08-16 18:57:57 +000034#include "llvm/Support/ELF.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000035#include "llvm/Support/Endian.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000036#include "llvm/Support/ErrorHandling.h"
Matt Fleming6c1ad482010-08-16 18:57:57 +000037#include <vector>
38using namespace llvm;
39
Jason W Kimc09e7262011-05-11 22:53:06 +000040#undef DEBUG_TYPE
41#define DEBUG_TYPE "reloc-info"
42
Rafael Espindola29abd972011-12-22 03:38:00 +000043namespace {
Rafael Espindola0ce09712014-03-25 22:43:53 +000044class FragmentWriter {
45 bool IsLittleEndian;
46
47public:
48 FragmentWriter(bool IsLittleEndian);
49 template <typename T> void write(MCDataFragment &F, T Val);
50};
51
Rafael Espindola10be0832014-03-25 23:44:25 +000052typedef DenseMap<const MCSectionELF *, uint32_t> SectionIndexMapTy;
53
54class SymbolTableWriter {
55 MCAssembler &Asm;
56 FragmentWriter &FWriter;
57 bool Is64Bit;
58 SectionIndexMapTy &SectionIndexMap;
59
60 // The symbol .symtab fragment we are writting to.
61 MCDataFragment *SymtabF;
62
63 // .symtab_shndx fragment we are writting to.
64 MCDataFragment *ShndxF;
65
66 // The numbel of symbols written so far.
67 unsigned NumWritten;
68
69 void createSymtabShndx();
70
71 template <typename T> void write(MCDataFragment &F, T Value);
72
73public:
74 SymbolTableWriter(MCAssembler &Asm, FragmentWriter &FWriter, bool Is64Bit,
75 SectionIndexMapTy &SectionIndexMap,
76 MCDataFragment *SymtabF);
77
78 void writeSymbol(uint32_t name, uint8_t info, uint64_t value, uint64_t size,
79 uint8_t other, uint32_t shndx, bool Reserved);
80};
81
Rafael Espindola5904e122014-03-29 06:26:49 +000082struct ELFRelocationEntry {
83 uint64_t Offset; // Where is the relocation.
Rafael Espindolab6613022014-10-17 01:48:58 +000084 const MCSymbol *Symbol; // The symbol to relocate with.
Rafael Espindola5904e122014-03-29 06:26:49 +000085 unsigned Type; // The type of the relocation.
86 uint64_t Addend; // The addend to use.
87
88 ELFRelocationEntry(uint64_t Offset, const MCSymbol *Symbol, unsigned Type,
89 uint64_t Addend)
Rafael Espindolab6613022014-10-17 01:48:58 +000090 : Offset(Offset), Symbol(Symbol), Type(Type), Addend(Addend) {}
Rafael Espindola5904e122014-03-29 06:26:49 +000091};
92
Rafael Espindola29abd972011-12-22 03:38:00 +000093class ELFObjectWriter : public MCObjectWriter {
Rafael Espindola0ce09712014-03-25 22:43:53 +000094 FragmentWriter FWriter;
95
Rafael Espindola29abd972011-12-22 03:38:00 +000096 protected:
97
98 static bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind);
99 static bool RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant);
100 static uint64_t SymbolValue(MCSymbolData &Data, const MCAsmLayout &Layout);
Rafael Espindola3b5ee552014-04-28 13:39:57 +0000101 static bool isInSymtab(const MCAsmLayout &Layout, const MCSymbolData &Data,
Rafael Espindola29abd972011-12-22 03:38:00 +0000102 bool Used, bool Renamed);
Rafael Espindola39f50422014-04-28 14:24:44 +0000103 static bool isLocal(const MCSymbolData &Data, bool isUsedInReloc);
Rafael Espindola29abd972011-12-22 03:38:00 +0000104 static bool IsELFMetaDataSection(const MCSectionData &SD);
105 static uint64_t DataSectionSize(const MCSectionData &SD);
106 static uint64_t GetSectionFileSize(const MCAsmLayout &Layout,
107 const MCSectionData &SD);
108 static uint64_t GetSectionAddressSize(const MCAsmLayout &Layout,
109 const MCSectionData &SD);
110
111 void WriteDataSectionData(MCAssembler &Asm,
112 const MCAsmLayout &Layout,
113 const MCSectionELF &Section);
114
115 /*static bool isFixupKindX86RIPRel(unsigned Kind) {
116 return Kind == X86::reloc_riprel_4byte ||
117 Kind == X86::reloc_riprel_4byte_movq_load;
118 }*/
119
120 /// ELFSymbolData - Helper struct for containing some precomputed
121 /// information on symbols.
122 struct ELFSymbolData {
123 MCSymbolData *SymbolData;
124 uint64_t StringIndex;
125 uint32_t SectionIndex;
Hans Wennborg83e6e1e2014-04-30 16:25:02 +0000126 StringRef Name;
Rafael Espindola29abd972011-12-22 03:38:00 +0000127
128 // Support lexicographic sorting.
129 bool operator<(const ELFSymbolData &RHS) const {
Rafael Espindolab6613022014-10-17 01:48:58 +0000130 unsigned LHSType = MCELF::GetType(*SymbolData);
131 unsigned RHSType = MCELF::GetType(*RHS.SymbolData);
132 if (LHSType == ELF::STT_SECTION && RHSType != ELF::STT_SECTION)
133 return false;
134 if (LHSType != ELF::STT_SECTION && RHSType == ELF::STT_SECTION)
135 return true;
136 if (LHSType == ELF::STT_SECTION && RHSType == ELF::STT_SECTION)
137 return SectionIndex < RHS.SectionIndex;
Hans Wennborg83e6e1e2014-04-30 16:25:02 +0000138 return Name < RHS.Name;
Rafael Espindola29abd972011-12-22 03:38:00 +0000139 }
140 };
141
Rafael Espindola29abd972011-12-22 03:38:00 +0000142 /// The target specific ELF writer instance.
Ahmed Charles56440fd2014-03-06 05:51:42 +0000143 std::unique_ptr<MCELFObjectTargetWriter> TargetObjectWriter;
Rafael Espindola29abd972011-12-22 03:38:00 +0000144
145 SmallPtrSet<const MCSymbol *, 16> UsedInReloc;
146 SmallPtrSet<const MCSymbol *, 16> WeakrefUsedInReloc;
147 DenseMap<const MCSymbol *, const MCSymbol *> Renames;
148
Rafael Espindola5904e122014-03-29 06:26:49 +0000149 llvm::DenseMap<const MCSectionData *, std::vector<ELFRelocationEntry>>
150 Relocations;
Hans Wennborg83e6e1e2014-04-30 16:25:02 +0000151 StringTableBuilder ShStrTabBuilder;
Rafael Espindola29abd972011-12-22 03:38:00 +0000152
153 /// @}
154 /// @name Symbol Table Data
155 /// @{
156
Hans Wennborg83e6e1e2014-04-30 16:25:02 +0000157 StringTableBuilder StrTabBuilder;
Joerg Sonnenbergerfc184732013-10-29 01:06:17 +0000158 std::vector<uint64_t> FileSymbolData;
Rafael Espindola29abd972011-12-22 03:38:00 +0000159 std::vector<ELFSymbolData> LocalSymbolData;
160 std::vector<ELFSymbolData> ExternalSymbolData;
161 std::vector<ELFSymbolData> UndefinedSymbolData;
162
163 /// @}
164
165 bool NeedsGOT;
166
Rafael Espindola29abd972011-12-22 03:38:00 +0000167 // This holds the symbol table index of the last local symbol.
168 unsigned LastLocalSymbolIndex;
169 // This holds the .strtab section index.
170 unsigned StringTableIndex;
171 // This holds the .symtab section index.
172 unsigned SymbolTableIndex;
173
174 unsigned ShstrtabIndex;
175
176
Rafael Espindola29abd972011-12-22 03:38:00 +0000177 // TargetObjectWriter wrappers.
Rafael Espindola29abd972011-12-22 03:38:00 +0000178 bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
179 bool hasRelocationAddend() const {
180 return TargetObjectWriter->hasRelocationAddend();
181 }
Rafael Espindola29abd972011-12-22 03:38:00 +0000182 unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
Rafael Espindolac03f44c2014-03-27 20:49:35 +0000183 bool IsPCRel) const {
184 return TargetObjectWriter->GetRelocType(Target, Fixup, IsPCRel);
Rafael Espindola29abd972011-12-22 03:38:00 +0000185 }
186
Rafael Espindola29abd972011-12-22 03:38:00 +0000187 public:
David Blaikie9f380a32015-03-16 18:06:57 +0000188 ELFObjectWriter(MCELFObjectTargetWriter *MOTW, raw_ostream &OS,
Rafael Espindola0ce09712014-03-25 22:43:53 +0000189 bool IsLittleEndian)
David Blaikie9f380a32015-03-16 18:06:57 +0000190 : MCObjectWriter(OS, IsLittleEndian), FWriter(IsLittleEndian),
Rafael Espindola10be0832014-03-25 23:44:25 +0000191 TargetObjectWriter(MOTW), NeedsGOT(false) {}
Rafael Espindola29abd972011-12-22 03:38:00 +0000192
Yaron Keren7773a722015-03-23 18:35:01 +0000193 void reset() override {
194 UsedInReloc.clear();
195 WeakrefUsedInReloc.clear();
196 Renames.clear();
197 Relocations.clear();
198 ShStrTabBuilder.clear();
199 StrTabBuilder.clear();
200 FileSymbolData.clear();
201 LocalSymbolData.clear();
202 ExternalSymbolData.clear();
203 UndefinedSymbolData.clear();
204 MCObjectWriter::reset();
205 }
206
Rafael Espindola29abd972011-12-22 03:38:00 +0000207 virtual ~ELFObjectWriter();
208
209 void WriteWord(uint64_t W) {
210 if (is64Bit())
211 Write64(W);
212 else
213 Write32(W);
214 }
215
Rafael Espindola0ce09712014-03-25 22:43:53 +0000216 template <typename T> void write(MCDataFragment &F, T Value) {
217 FWriter.write(F, Value);
Rafael Espindola29abd972011-12-22 03:38:00 +0000218 }
219
Jack Carter1bd90ff2013-01-30 02:09:52 +0000220 void WriteHeader(const MCAssembler &Asm,
221 uint64_t SectionDataSize,
Rafael Espindola29abd972011-12-22 03:38:00 +0000222 unsigned NumberOfSections);
223
Rafael Espindola10be0832014-03-25 23:44:25 +0000224 void WriteSymbol(SymbolTableWriter &Writer, ELFSymbolData &MSD,
Rafael Espindola29abd972011-12-22 03:38:00 +0000225 const MCAsmLayout &Layout);
226
Rafael Espindola10be0832014-03-25 23:44:25 +0000227 void WriteSymbolTable(MCDataFragment *SymtabF, MCAssembler &Asm,
Rafael Espindola29abd972011-12-22 03:38:00 +0000228 const MCAsmLayout &Layout,
Rafael Espindola10be0832014-03-25 23:44:25 +0000229 SectionIndexMapTy &SectionIndexMap);
Rafael Espindola29abd972011-12-22 03:38:00 +0000230
Rafael Espindolab60c8292014-04-29 12:46:50 +0000231 bool shouldRelocateWithSymbol(const MCAssembler &Asm,
232 const MCSymbolRefExpr *RefA,
Rafael Espindola5904e122014-03-29 06:26:49 +0000233 const MCSymbolData *SD, uint64_t C,
234 unsigned Type) const;
235
Rafael Espindola26585542015-01-19 21:11:14 +0000236 void RecordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
Craig Topper34a61bc2014-03-08 07:02:02 +0000237 const MCFragment *Fragment, const MCFixup &Fixup,
Rafael Espindola5904e122014-03-29 06:26:49 +0000238 MCValue Target, bool &IsPCRel,
239 uint64_t &FixedValue) override;
Rafael Espindola29abd972011-12-22 03:38:00 +0000240
241 uint64_t getSymbolIndexInSymbolTable(const MCAssembler &Asm,
242 const MCSymbol *S);
243
244 // Map from a group section to the signature symbol
245 typedef DenseMap<const MCSectionELF*, const MCSymbol*> GroupMapTy;
246 // Map from a signature symbol to the group section
247 typedef DenseMap<const MCSymbol*, const MCSectionELF*> RevGroupMapTy;
248 // Map from a section to the section with the relocations
249 typedef DenseMap<const MCSectionELF*, const MCSectionELF*> RelMapTy;
250 // Map from a section to its offset
251 typedef DenseMap<const MCSectionELF*, uint64_t> SectionOffsetMapTy;
252
Rafael Espindola022bb762014-03-24 03:43:21 +0000253 /// Compute the symbol table data
Rafael Espindola29abd972011-12-22 03:38:00 +0000254 ///
Rafael Espindola073ee7d2012-08-27 16:04:24 +0000255 /// \param Asm - The assembler.
256 /// \param SectionIndexMap - Maps a section to its index.
257 /// \param RevGroupMap - Maps a signature symbol to the group section.
258 /// \param NumRegularSections - Number of non-relocation sections.
Rafael Espindola022bb762014-03-24 03:43:21 +0000259 void computeSymbolTable(MCAssembler &Asm, const MCAsmLayout &Layout,
Rafael Espindola29abd972011-12-22 03:38:00 +0000260 const SectionIndexMapTy &SectionIndexMap,
Benjamin Kramer04f9da82014-09-19 12:26:38 +0000261 const RevGroupMapTy &RevGroupMap,
Rafael Espindola29abd972011-12-22 03:38:00 +0000262 unsigned NumRegularSections);
263
Rafael Espindolaead85492015-02-17 20:31:13 +0000264 void computeIndexMap(MCAssembler &Asm,
Rafael Espindola29abd972011-12-22 03:38:00 +0000265 SectionIndexMapTy &SectionIndexMap,
Rafael Espindolaead85492015-02-17 20:31:13 +0000266 RelMapTy &RelMap);
Rafael Espindola29abd972011-12-22 03:38:00 +0000267
Rafael Espindolaead85492015-02-17 20:31:13 +0000268 MCSectionData *createRelocationSection(MCAssembler &Asm,
269 const MCSectionData &SD);
Rafael Espindola29abd972011-12-22 03:38:00 +0000270
David Blaikie8019bf82014-04-10 21:53:53 +0000271 void CompressDebugSections(MCAssembler &Asm, MCAsmLayout &Layout);
272
Rafael Espindola29abd972011-12-22 03:38:00 +0000273 void WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout,
274 const RelMapTy &RelMap);
275
276 void CreateMetadataSections(MCAssembler &Asm, MCAsmLayout &Layout,
Rafael Espindolaef6baea2015-02-11 23:11:18 +0000277 SectionIndexMapTy &SectionIndexMap);
Rafael Espindola29abd972011-12-22 03:38:00 +0000278
279 // Create the sections that show up in the symbol table. Currently
280 // those are the .note.GNU-stack section and the group sections.
Rafael Espindolaead85492015-02-17 20:31:13 +0000281 void createIndexedSections(MCAssembler &Asm, MCAsmLayout &Layout,
Rafael Espindola29abd972011-12-22 03:38:00 +0000282 GroupMapTy &GroupMap,
283 RevGroupMapTy &RevGroupMap,
284 SectionIndexMapTy &SectionIndexMap,
Rafael Espindolaead85492015-02-17 20:31:13 +0000285 RelMapTy &RelMap);
Rafael Espindola29abd972011-12-22 03:38:00 +0000286
Craig Topper34a61bc2014-03-08 07:02:02 +0000287 void ExecutePostLayoutBinding(MCAssembler &Asm,
288 const MCAsmLayout &Layout) override;
Rafael Espindola29abd972011-12-22 03:38:00 +0000289
Rafael Espindola7fe7e052015-02-17 20:40:59 +0000290 void writeSectionHeader(MCAssembler &Asm, const GroupMapTy &GroupMap,
Rafael Espindola29abd972011-12-22 03:38:00 +0000291 const MCAsmLayout &Layout,
292 const SectionIndexMapTy &SectionIndexMap,
Rafael Espindola7fe7e052015-02-17 20:40:59 +0000293 const RelMapTy &RelMap,
Rafael Espindola29abd972011-12-22 03:38:00 +0000294 const SectionOffsetMapTy &SectionOffsetMap);
295
296 void ComputeSectionOrder(MCAssembler &Asm,
297 std::vector<const MCSectionELF*> &Sections);
298
299 void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
300 uint64_t Address, uint64_t Offset,
301 uint64_t Size, uint32_t Link, uint32_t Info,
302 uint64_t Alignment, uint64_t EntrySize);
303
304 void WriteRelocationsFragment(const MCAssembler &Asm,
305 MCDataFragment *F,
306 const MCSectionData *SD);
307
Craig Topper34a61bc2014-03-08 07:02:02 +0000308 bool
Rafael Espindola29abd972011-12-22 03:38:00 +0000309 IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
310 const MCSymbolData &DataA,
311 const MCFragment &FB,
312 bool InSet,
Craig Topper34a61bc2014-03-08 07:02:02 +0000313 bool IsPCRel) const override;
Rafael Espindola29abd972011-12-22 03:38:00 +0000314
Rafael Espindolaf275ad82015-03-25 13:16:53 +0000315 bool isWeak(const MCSymbolData &SD) const override;
316
Craig Topper34a61bc2014-03-08 07:02:02 +0000317 void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
Rafael Espindola7fe7e052015-02-17 20:40:59 +0000318 void writeSection(MCAssembler &Asm,
Rafael Espindola29abd972011-12-22 03:38:00 +0000319 const SectionIndexMapTy &SectionIndexMap,
Rafael Espindola7fe7e052015-02-17 20:40:59 +0000320 const RelMapTy &RelMap,
Rafael Espindola29abd972011-12-22 03:38:00 +0000321 uint32_t GroupSymbolIndex,
322 uint64_t Offset, uint64_t Size, uint64_t Alignment,
323 const MCSectionELF &Section);
324 };
325}
326
Rafael Espindola0ce09712014-03-25 22:43:53 +0000327FragmentWriter::FragmentWriter(bool IsLittleEndian)
328 : IsLittleEndian(IsLittleEndian) {}
329
330template <typename T> void FragmentWriter::write(MCDataFragment &F, T Val) {
331 if (IsLittleEndian)
332 Val = support::endian::byte_swap<T, support::little>(Val);
333 else
334 Val = support::endian::byte_swap<T, support::big>(Val);
335 const char *Start = (const char *)&Val;
336 F.getContents().append(Start, Start + sizeof(T));
337}
338
Rafael Espindola10be0832014-03-25 23:44:25 +0000339void SymbolTableWriter::createSymtabShndx() {
340 if (ShndxF)
341 return;
342
343 MCContext &Ctx = Asm.getContext();
344 const MCSectionELF *SymtabShndxSection =
Rafael Espindolaba31e272015-01-29 17:33:21 +0000345 Ctx.getELFSection(".symtab_shndxr", ELF::SHT_SYMTAB_SHNDX, 0, 4, "");
Rafael Espindola10be0832014-03-25 23:44:25 +0000346 MCSectionData *SymtabShndxSD =
347 &Asm.getOrCreateSectionData(*SymtabShndxSection);
348 SymtabShndxSD->setAlignment(4);
349 ShndxF = new MCDataFragment(SymtabShndxSD);
350 unsigned Index = SectionIndexMap.size() + 1;
351 SectionIndexMap[SymtabShndxSection] = Index;
352
353 for (unsigned I = 0; I < NumWritten; ++I)
354 write(*ShndxF, uint32_t(0));
355}
356
357template <typename T>
358void SymbolTableWriter::write(MCDataFragment &F, T Value) {
359 FWriter.write(F, Value);
360}
361
362SymbolTableWriter::SymbolTableWriter(MCAssembler &Asm, FragmentWriter &FWriter,
363 bool Is64Bit,
364 SectionIndexMapTy &SectionIndexMap,
365 MCDataFragment *SymtabF)
366 : Asm(Asm), FWriter(FWriter), Is64Bit(Is64Bit),
367 SectionIndexMap(SectionIndexMap), SymtabF(SymtabF), ShndxF(nullptr),
368 NumWritten(0) {}
369
370void SymbolTableWriter::writeSymbol(uint32_t name, uint8_t info, uint64_t value,
371 uint64_t size, uint8_t other,
372 uint32_t shndx, bool Reserved) {
373 bool LargeIndex = shndx >= ELF::SHN_LORESERVE && !Reserved;
374
375 if (LargeIndex)
376 createSymtabShndx();
377
378 if (ShndxF) {
379 if (LargeIndex)
380 write(*ShndxF, shndx);
381 else
382 write(*ShndxF, uint32_t(0));
383 }
384
385 uint16_t Index = LargeIndex ? uint16_t(ELF::SHN_XINDEX) : shndx;
386
387 raw_svector_ostream OS(SymtabF->getContents());
388
389 if (Is64Bit) {
390 write(*SymtabF, name); // st_name
391 write(*SymtabF, info); // st_info
392 write(*SymtabF, other); // st_other
393 write(*SymtabF, Index); // st_shndx
394 write(*SymtabF, value); // st_value
395 write(*SymtabF, size); // st_size
396 } else {
397 write(*SymtabF, name); // st_name
398 write(*SymtabF, uint32_t(value)); // st_value
399 write(*SymtabF, uint32_t(size)); // st_size
400 write(*SymtabF, info); // st_info
401 write(*SymtabF, other); // st_other
402 write(*SymtabF, Index); // st_shndx
403 }
404
405 ++NumWritten;
406}
407
Jan Sjödin30a52de2011-02-28 21:45:04 +0000408bool ELFObjectWriter::isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) {
409 const MCFixupKindInfo &FKI =
410 Asm.getBackend().getFixupKindInfo((MCFixupKind) Kind);
411
412 return FKI.Flags & MCFixupKindInfo::FKF_IsPCRel;
413}
414
415bool ELFObjectWriter::RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant) {
416 switch (Variant) {
417 default:
418 return false;
419 case MCSymbolRefExpr::VK_GOT:
420 case MCSymbolRefExpr::VK_PLT:
421 case MCSymbolRefExpr::VK_GOTPCREL:
Rafael Espindola940a0ee2011-06-05 01:20:06 +0000422 case MCSymbolRefExpr::VK_GOTOFF:
Jan Sjödin30a52de2011-02-28 21:45:04 +0000423 case MCSymbolRefExpr::VK_TPOFF:
424 case MCSymbolRefExpr::VK_TLSGD:
425 case MCSymbolRefExpr::VK_GOTTPOFF:
426 case MCSymbolRefExpr::VK_INDNTPOFF:
427 case MCSymbolRefExpr::VK_NTPOFF:
428 case MCSymbolRefExpr::VK_GOTNTPOFF:
429 case MCSymbolRefExpr::VK_TLSLDM:
430 case MCSymbolRefExpr::VK_DTPOFF:
431 case MCSymbolRefExpr::VK_TLSLD:
432 return true;
433 }
434}
435
Jason W Kim96f4c012010-11-15 16:18:39 +0000436ELFObjectWriter::~ELFObjectWriter()
437{}
438
Matt Fleming6c1ad482010-08-16 18:57:57 +0000439// Emit the ELF header.
Jack Carter1bd90ff2013-01-30 02:09:52 +0000440void ELFObjectWriter::WriteHeader(const MCAssembler &Asm,
441 uint64_t SectionDataSize,
Daniel Dunbarfe0c28f2010-11-13 07:33:40 +0000442 unsigned NumberOfSections) {
Matt Fleming6c1ad482010-08-16 18:57:57 +0000443 // ELF Header
444 // ----------
445 //
446 // Note
447 // ----
448 // emitWord method behaves differently for ELF32 and ELF64, writing
449 // 4 bytes in the former and 8 in the latter.
450
451 Write8(0x7f); // e_ident[EI_MAG0]
452 Write8('E'); // e_ident[EI_MAG1]
453 Write8('L'); // e_ident[EI_MAG2]
454 Write8('F'); // e_ident[EI_MAG3]
455
Rafael Espindolafdaae0d2010-12-18 03:27:34 +0000456 Write8(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
Matt Fleming6c1ad482010-08-16 18:57:57 +0000457
458 // e_ident[EI_DATA]
Daniel Dunbarfe0c28f2010-11-13 07:33:40 +0000459 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000460
461 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky3b727f52010-09-09 17:57:50 +0000462 // e_ident[EI_OSABI]
Rafael Espindola1ad40952011-12-21 17:00:36 +0000463 Write8(TargetObjectWriter->getOSABI());
Matt Fleming6c1ad482010-08-16 18:57:57 +0000464 Write8(0); // e_ident[EI_ABIVERSION]
465
466 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
467
468 Write16(ELF::ET_REL); // e_type
469
Rafael Espindolafdaae0d2010-12-18 03:27:34 +0000470 Write16(TargetObjectWriter->getEMachine()); // e_machine = target
Matt Fleming6c1ad482010-08-16 18:57:57 +0000471
472 Write32(ELF::EV_CURRENT); // e_version
473 WriteWord(0); // e_entry, no entry point in .o file
474 WriteWord(0); // e_phoff, no program header for .o
Rafael Espindolafdaae0d2010-12-18 03:27:34 +0000475 WriteWord(SectionDataSize + (is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
Benjamin Kramer896bd7e2010-08-17 17:02:29 +0000476 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming6c1ad482010-08-16 18:57:57 +0000477
Jason W Kim4761fba2011-02-04 21:41:11 +0000478 // e_flags = whatever the target wants
Jack Carter1bd90ff2013-01-30 02:09:52 +0000479 Write32(Asm.getELFHeaderEFlags());
Matt Fleming6c1ad482010-08-16 18:57:57 +0000480
481 // e_ehsize = ELF header size
Rafael Espindolafdaae0d2010-12-18 03:27:34 +0000482 Write16(is64Bit() ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
Matt Fleming6c1ad482010-08-16 18:57:57 +0000483
484 Write16(0); // e_phentsize = prog header entry size
485 Write16(0); // e_phnum = # prog header entries = 0
486
487 // e_shentsize = Section header entry size
Rafael Espindolafdaae0d2010-12-18 03:27:34 +0000488 Write16(is64Bit() ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
Matt Fleming6c1ad482010-08-16 18:57:57 +0000489
490 // e_shnum = # of section header ents
Rafael Espindola3fe87a12010-10-31 00:16:26 +0000491 if (NumberOfSections >= ELF::SHN_LORESERVE)
Nick Lewycky4eb11432011-10-07 20:56:23 +0000492 Write16(ELF::SHN_UNDEF);
Rafael Espindola3fe87a12010-10-31 00:16:26 +0000493 else
494 Write16(NumberOfSections);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000495
496 // e_shstrndx = Section # of '.shstrtab'
Nick Lewycky8b02d362011-10-07 20:58:24 +0000497 if (ShstrtabIndex >= ELF::SHN_LORESERVE)
Rafael Espindola3fe87a12010-10-31 00:16:26 +0000498 Write16(ELF::SHN_XINDEX);
499 else
500 Write16(ShstrtabIndex);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000501}
502
Rafael Espindolafee224f2014-04-30 21:51:13 +0000503uint64_t ELFObjectWriter::SymbolValue(MCSymbolData &Data,
Jan Sjödin30a52de2011-02-28 21:45:04 +0000504 const MCAsmLayout &Layout) {
Rafael Espindolafee224f2014-04-30 21:51:13 +0000505 if (Data.isCommon() && Data.isExternal())
506 return Data.getCommonAlignment();
Rafael Espindola9735f4f2010-09-27 21:23:02 +0000507
Rafael Espindolafee224f2014-04-30 21:51:13 +0000508 uint64_t Res;
509 if (!Layout.getSymbolOffset(&Data, Res))
Rafael Espindola553e5eb2014-04-30 16:59:35 +0000510 return 0;
511
Rafael Espindolafee224f2014-04-30 21:51:13 +0000512 if (Layout.getAssembler().isThumbFunc(&Data.getSymbol()))
Rafael Espindola66f96fe2014-03-21 22:00:29 +0000513 Res |= 1;
Rafael Espindola9735f4f2010-09-27 21:23:02 +0000514
Rafael Espindola66f96fe2014-03-21 22:00:29 +0000515 return Res;
Rafael Espindola9735f4f2010-09-27 21:23:02 +0000516}
517
Rafael Espindola93e3cf02010-12-07 00:27:36 +0000518void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
519 const MCAsmLayout &Layout) {
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000520 // The presence of symbol versions causes undefined symbols and
521 // versions declared with @@@ to be renamed.
522
David Blaikie583a31c2014-04-18 18:24:25 +0000523 for (MCSymbolData &OriginalData : Asm.symbols()) {
524 const MCSymbol &Alias = OriginalData.getSymbol();
Rafael Espindola6cd76e62010-10-28 18:33:03 +0000525
Rafael Espindola6cd76e62010-10-28 18:33:03 +0000526 // Not an alias.
Rafael Espindola6efaf112014-04-28 17:05:36 +0000527 if (!Alias.isVariable())
Rafael Espindola6cd76e62010-10-28 18:33:03 +0000528 continue;
Rafael Espindola6efaf112014-04-28 17:05:36 +0000529 auto *Ref = dyn_cast<MCSymbolRefExpr>(Alias.getVariableValue());
530 if (!Ref)
531 continue;
532 const MCSymbol &Symbol = Ref->getSymbol();
533 MCSymbolData &SD = Asm.getSymbolData(Symbol);
Rafael Espindola6cd76e62010-10-28 18:33:03 +0000534
Benjamin Kramer14807272010-10-27 19:53:52 +0000535 StringRef AliasName = Alias.getName();
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000536 size_t Pos = AliasName.find('@');
537 if (Pos == StringRef::npos)
538 continue;
539
Rafael Espindola6cd76e62010-10-28 18:33:03 +0000540 // Aliases defined with .symvar copy the binding from the symbol they alias.
541 // This is the first place we are able to copy this information.
David Blaikie583a31c2014-04-18 18:24:25 +0000542 OriginalData.setExternal(SD.isExternal());
543 MCELF::SetBinding(OriginalData, MCELF::GetBinding(SD));
Rafael Espindola6cd76e62010-10-28 18:33:03 +0000544
Benjamin Kramer14807272010-10-27 19:53:52 +0000545 StringRef Rest = AliasName.substr(Pos);
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000546 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
547 continue;
548
Rafael Espindola26496e62010-10-27 17:56:18 +0000549 // FIXME: produce a better error message.
550 if (Symbol.isUndefined() && Rest.startswith("@@") &&
551 !Rest.startswith("@@@"))
552 report_fatal_error("A @@ version cannot be undefined");
553
Benjamin Kramer14807272010-10-27 19:53:52 +0000554 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000555 }
556}
557
Roman Divacky5a1c5492014-01-07 20:17:03 +0000558static uint8_t mergeTypeForSet(uint8_t origType, uint8_t newType) {
559 uint8_t Type = newType;
560
561 // Propagation rules:
562 // IFUNC > FUNC > OBJECT > NOTYPE
563 // TLS_OBJECT > OBJECT > NOTYPE
564 //
565 // dont let the new type degrade the old type
566 switch (origType) {
567 default:
568 break;
569 case ELF::STT_GNU_IFUNC:
570 if (Type == ELF::STT_FUNC || Type == ELF::STT_OBJECT ||
571 Type == ELF::STT_NOTYPE || Type == ELF::STT_TLS)
572 Type = ELF::STT_GNU_IFUNC;
573 break;
574 case ELF::STT_FUNC:
575 if (Type == ELF::STT_OBJECT || Type == ELF::STT_NOTYPE ||
576 Type == ELF::STT_TLS)
577 Type = ELF::STT_FUNC;
578 break;
579 case ELF::STT_OBJECT:
580 if (Type == ELF::STT_NOTYPE)
581 Type = ELF::STT_OBJECT;
582 break;
583 case ELF::STT_TLS:
584 if (Type == ELF::STT_OBJECT || Type == ELF::STT_NOTYPE ||
585 Type == ELF::STT_GNU_IFUNC || Type == ELF::STT_FUNC)
586 Type = ELF::STT_TLS;
587 break;
588 }
589
590 return Type;
591}
592
Rafael Espindola10be0832014-03-25 23:44:25 +0000593void ELFObjectWriter::WriteSymbol(SymbolTableWriter &Writer, ELFSymbolData &MSD,
Daniel Dunbarfe0c28f2010-11-13 07:33:40 +0000594 const MCAsmLayout &Layout) {
Rafael Espindola5f2d6a52010-10-06 21:02:29 +0000595 MCSymbolData &OrigData = *MSD.SymbolData;
David Blaikieb5956d22014-04-19 00:50:15 +0000596 assert((!OrigData.getFragment() ||
597 (&OrigData.getFragment()->getParent()->getSection() ==
598 &OrigData.getSymbol().getSection())) &&
599 "The symbol's section doesn't match the fragment's symbol");
Rafael Espindola2aeac7a2014-05-01 13:24:25 +0000600 const MCSymbol *Base = Layout.getBaseSymbol(OrigData.getSymbol());
Rafael Espindola85a84912014-03-26 00:16:43 +0000601
602 // This has to be in sync with when computeSymbolTable uses SHN_ABS or
603 // SHN_COMMON.
604 bool IsReserved = !Base || OrigData.isCommon();
Rafael Espindola3fe87a12010-10-31 00:16:26 +0000605
Jack Carter2f8d9d92013-02-19 21:57:35 +0000606 // Binding and Type share the same byte as upper and lower nibbles
Jan Sjödin30a52de2011-02-28 21:45:04 +0000607 uint8_t Binding = MCELF::GetBinding(OrigData);
Rafael Espindolaa6e3a592014-03-23 03:33:20 +0000608 uint8_t Type = MCELF::GetType(OrigData);
Rafael Espindolaa041ef12014-03-27 00:28:24 +0000609 MCSymbolData *BaseSD = nullptr;
Rafael Espindolaa6e3a592014-03-23 03:33:20 +0000610 if (Base) {
Rafael Espindolaa041ef12014-03-27 00:28:24 +0000611 BaseSD = &Layout.getAssembler().getSymbolData(*Base);
612 Type = mergeTypeForSet(Type, MCELF::GetType(*BaseSD));
Rafael Espindolaa6e3a592014-03-23 03:33:20 +0000613 }
Rafael Espindola5f2d6a52010-10-06 21:02:29 +0000614 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
Jack Carter2f8d9d92013-02-19 21:57:35 +0000615
Joerg Sonnenbergerfc184732013-10-29 01:06:17 +0000616 // Other and Visibility share the same byte with Visibility using the lower
Jack Carter2f8d9d92013-02-19 21:57:35 +0000617 // 2 bits
618 uint8_t Visibility = MCELF::GetVisibility(OrigData);
Logan Chienee365952013-12-05 00:34:11 +0000619 uint8_t Other = MCELF::getOther(OrigData) << (ELF_STO_Shift - ELF_STV_Shift);
Jack Carter2f8d9d92013-02-19 21:57:35 +0000620 Other |= Visibility;
Rafael Espindola5f2d6a52010-10-06 21:02:29 +0000621
Rafael Espindola66f96fe2014-03-21 22:00:29 +0000622 uint64_t Value = SymbolValue(OrigData, Layout);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000623 uint64_t Size = 0;
Matt Fleming6c1ad482010-08-16 18:57:57 +0000624
Rafael Espindolaa041ef12014-03-27 00:28:24 +0000625 const MCExpr *ESize = OrigData.getSize();
626 if (!ESize && Base)
627 ESize = BaseSD->getSize();
Rafael Espindolaf0591c12010-09-21 00:24:38 +0000628
Rafael Espindola73c0ae72010-12-22 16:03:00 +0000629 if (ESize) {
630 int64_t Res;
631 if (!ESize->EvaluateAsAbsolute(Res, Layout))
632 report_fatal_error("Size expression must be absolute.");
633 Size = Res;
Matt Fleming6c1ad482010-08-16 18:57:57 +0000634 }
635
636 // Write out the symbol table entry
Rafael Espindola10be0832014-03-25 23:44:25 +0000637 Writer.writeSymbol(MSD.StringIndex, Info, Value, Size, Other,
638 MSD.SectionIndex, IsReserved);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000639}
640
Daniel Dunbarfe0c28f2010-11-13 07:33:40 +0000641void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
Rafael Espindola10be0832014-03-25 23:44:25 +0000642 MCAssembler &Asm,
Daniel Dunbarfe0c28f2010-11-13 07:33:40 +0000643 const MCAsmLayout &Layout,
Rafael Espindola10be0832014-03-25 23:44:25 +0000644 SectionIndexMapTy &SectionIndexMap) {
Matt Fleming6c1ad482010-08-16 18:57:57 +0000645 // The string table must be emitted first because we need the index
646 // into the string table for all the symbol names.
Matt Fleming6c1ad482010-08-16 18:57:57 +0000647
648 // FIXME: Make sure the start of the symbol table is aligned.
649
Rafael Espindola10be0832014-03-25 23:44:25 +0000650 SymbolTableWriter Writer(Asm, FWriter, is64Bit(), SectionIndexMap, SymtabF);
651
Matt Fleming6c1ad482010-08-16 18:57:57 +0000652 // The first entry is the undefined symbol entry.
Rafael Espindola10be0832014-03-25 23:44:25 +0000653 Writer.writeSymbol(0, 0, 0, 0, 0, 0, false);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000654
Joerg Sonnenbergerfc184732013-10-29 01:06:17 +0000655 for (unsigned i = 0, e = FileSymbolData.size(); i != e; ++i) {
Rafael Espindola10be0832014-03-25 23:44:25 +0000656 Writer.writeSymbol(FileSymbolData[i], ELF::STT_FILE | ELF::STB_LOCAL, 0, 0,
657 ELF::STV_DEFAULT, ELF::SHN_ABS, true);
Joerg Sonnenbergerfc184732013-10-29 01:06:17 +0000658 }
659
Matt Fleming6c1ad482010-08-16 18:57:57 +0000660 // Write the symbol table entries.
Joerg Sonnenbergerfc184732013-10-29 01:06:17 +0000661 LastLocalSymbolIndex = FileSymbolData.size() + LocalSymbolData.size() + 1;
662
Matt Fleming6c1ad482010-08-16 18:57:57 +0000663 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
664 ELFSymbolData &MSD = LocalSymbolData[i];
Rafael Espindola10be0832014-03-25 23:44:25 +0000665 WriteSymbol(Writer, MSD, Layout);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000666 }
667
Matt Fleming6c1ad482010-08-16 18:57:57 +0000668 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
669 ELFSymbolData &MSD = ExternalSymbolData[i];
670 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola83b2a332010-10-06 16:47:31 +0000671 assert(((Data.getFlags() & ELF_STB_Global) ||
672 (Data.getFlags() & ELF_STB_Weak)) &&
673 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola10be0832014-03-25 23:44:25 +0000674 WriteSymbol(Writer, MSD, Layout);
Jan Sjödin30a52de2011-02-28 21:45:04 +0000675 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming6c1ad482010-08-16 18:57:57 +0000676 LastLocalSymbolIndex++;
677 }
678
679 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
680 ELFSymbolData &MSD = UndefinedSymbolData[i];
681 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola10be0832014-03-25 23:44:25 +0000682 WriteSymbol(Writer, MSD, Layout);
Jan Sjödin30a52de2011-02-28 21:45:04 +0000683 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming6c1ad482010-08-16 18:57:57 +0000684 LastLocalSymbolIndex++;
685 }
686}
687
Rafael Espindola5904e122014-03-29 06:26:49 +0000688// It is always valid to create a relocation with a symbol. It is preferable
689// to use a relocation with a section if that is possible. Using the section
690// allows us to omit some local symbols from the symbol table.
Rafael Espindolab60c8292014-04-29 12:46:50 +0000691bool ELFObjectWriter::shouldRelocateWithSymbol(const MCAssembler &Asm,
692 const MCSymbolRefExpr *RefA,
Rafael Espindola5904e122014-03-29 06:26:49 +0000693 const MCSymbolData *SD,
694 uint64_t C,
695 unsigned Type) const {
696 // A PCRel relocation to an absolute value has no symbol (or section). We
697 // represent that with a relocation to a null section.
698 if (!RefA)
699 return false;
Rafael Espindola240028d2010-11-14 23:53:26 +0000700
Rafael Espindola5904e122014-03-29 06:26:49 +0000701 MCSymbolRefExpr::VariantKind Kind = RefA->getKind();
702 switch (Kind) {
703 default:
704 break;
705 // The .odp creation emits a relocation against the symbol ".TOC." which
706 // create a R_PPC64_TOC relocation. However the relocation symbol name
707 // in final object creation should be NULL, since the symbol does not
708 // really exist, it is just the reference to TOC base for the current
709 // object file. Since the symbol is undefined, returning false results
710 // in a relocation with a null section which is the desired result.
711 case MCSymbolRefExpr::VK_PPC_TOCBASE:
712 return false;
713
714 // These VariantKind cause the relocation to refer to something other than
715 // the symbol itself, like a linker generated table. Since the address of
716 // symbol is not relevant, we cannot replace the symbol with the
717 // section and patch the difference in the addend.
718 case MCSymbolRefExpr::VK_GOT:
719 case MCSymbolRefExpr::VK_PLT:
720 case MCSymbolRefExpr::VK_GOTPCREL:
721 case MCSymbolRefExpr::VK_Mips_GOT:
722 case MCSymbolRefExpr::VK_PPC_GOT_LO:
723 case MCSymbolRefExpr::VK_PPC_GOT_HI:
724 case MCSymbolRefExpr::VK_PPC_GOT_HA:
725 return true;
Rafael Espindola240028d2010-11-14 23:53:26 +0000726 }
Rafael Espindola240028d2010-11-14 23:53:26 +0000727
Rafael Espindola5904e122014-03-29 06:26:49 +0000728 // An undefined symbol is not in any section, so the relocation has to point
729 // to the symbol itself.
730 const MCSymbol &Sym = SD->getSymbol();
731 if (Sym.isUndefined())
732 return true;
733
734 unsigned Binding = MCELF::GetBinding(*SD);
735 switch(Binding) {
736 default:
737 llvm_unreachable("Invalid Binding");
738 case ELF::STB_LOCAL:
739 break;
740 case ELF::STB_WEAK:
741 // If the symbol is weak, it might be overridden by a symbol in another
742 // file. The relocation has to point to the symbol so that the linker
743 // can update it.
744 return true;
745 case ELF::STB_GLOBAL:
746 // Global ELF symbols can be preempted by the dynamic linker. The relocation
747 // has to point to the symbol for a reason analogous to the STB_WEAK case.
748 return true;
Rafael Espindola8c3039b2010-11-15 16:33:49 +0000749 }
Rafael Espindola75d65b92010-09-25 05:42:19 +0000750
Rafael Espindola5904e122014-03-29 06:26:49 +0000751 // If a relocation points to a mergeable section, we have to be careful.
752 // If the offset is zero, a relocation with the section will encode the
753 // same information. With a non-zero offset, the situation is different.
754 // For example, a relocation can point 42 bytes past the end of a string.
755 // If we change such a relocation to use the section, the linker would think
756 // that it pointed to another string and subtracting 42 at runtime will
757 // produce the wrong value.
758 auto &Sec = cast<MCSectionELF>(Sym.getSection());
759 unsigned Flags = Sec.getFlags();
760 if (Flags & ELF::SHF_MERGE) {
761 if (C != 0)
762 return true;
Rafael Espindolab1b49782014-04-02 12:15:20 +0000763
764 // It looks like gold has a bug (http://sourceware.org/PR16794) and can
765 // only handle section relocations to mergeable sections if using RELA.
766 if (!hasRelocationAddend())
767 return true;
Rafael Espindola9f75d5d2010-11-24 21:57:39 +0000768 }
769
Rafael Espindola5904e122014-03-29 06:26:49 +0000770 // Most TLS relocations use a got, so they need the symbol. Even those that
Rafael Espindola11527a12014-10-06 12:33:27 +0000771 // are just an offset (@tpoff), require a symbol in gold versions before
772 // 5efeedf61e4fe720fd3e9a08e6c91c10abb66d42 (2014-09-26) which fixed
773 // http://sourceware.org/PR16773.
Rafael Espindola5904e122014-03-29 06:26:49 +0000774 if (Flags & ELF::SHF_TLS)
775 return true;
Rafael Espindolad7565c32010-10-05 23:57:26 +0000776
Rafael Espindola9ef84412014-04-11 19:18:01 +0000777 // If the symbol is a thumb function the final relocation must set the lowest
778 // bit. With a symbol that is done by just having the symbol have that bit
779 // set, so we would lose the bit if we relocated with the section.
780 // FIXME: We could use the section but add the bit to the relocation value.
Rafael Espindolab60c8292014-04-29 12:46:50 +0000781 if (Asm.isThumbFunc(&Sym))
Rafael Espindola9ef84412014-04-11 19:18:01 +0000782 return true;
783
Ulrich Weigand46797c62014-07-20 23:15:06 +0000784 if (TargetObjectWriter->needsRelocateWithSymbol(*SD, Type))
Rafael Espindola5904e122014-03-29 06:26:49 +0000785 return true;
786 return false;
Rafael Espindola75d65b92010-09-25 05:42:19 +0000787}
788
Rafael Espindola3d082fa2014-05-03 19:57:04 +0000789static const MCSymbol *getWeakRef(const MCSymbolRefExpr &Ref) {
790 const MCSymbol &Sym = Ref.getSymbol();
791
792 if (Ref.getKind() == MCSymbolRefExpr::VK_WEAKREF)
793 return &Sym;
794
795 if (!Sym.isVariable())
796 return nullptr;
797
798 const MCExpr *Expr = Sym.getVariableValue();
799 const auto *Inner = dyn_cast<MCSymbolRefExpr>(Expr);
800 if (!Inner)
801 return nullptr;
802
803 if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF)
804 return &Inner->getSymbol();
805 return nullptr;
806}
807
Rafael Espindolac9e70682015-03-24 23:48:44 +0000808static bool isWeak(const MCSymbolData &D) {
809 return D.getFlags() & ELF_STB_Weak || MCELF::GetType(D) == ELF::STT_GNU_IFUNC;
810}
811
Rafael Espindola26585542015-01-19 21:11:14 +0000812void ELFObjectWriter::RecordRelocation(MCAssembler &Asm,
Jason W Kim495c2bb2010-12-06 21:57:34 +0000813 const MCAsmLayout &Layout,
814 const MCFragment *Fragment,
Rafael Espindola26585542015-01-19 21:11:14 +0000815 const MCFixup &Fixup, MCValue Target,
816 bool &IsPCRel, uint64_t &FixedValue) {
Rafael Espindola5904e122014-03-29 06:26:49 +0000817 const MCSectionData *FixupSection = Fragment->getParent();
818 uint64_t C = Target.getConstant();
819 uint64_t FixupOffset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
Jason W Kim495c2bb2010-12-06 21:57:34 +0000820
Rafael Espindola5904e122014-03-29 06:26:49 +0000821 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
822 assert(RefB->getKind() == MCSymbolRefExpr::VK_None &&
823 "Should not have constructed this");
Jason W Kim495c2bb2010-12-06 21:57:34 +0000824
Rafael Espindola5904e122014-03-29 06:26:49 +0000825 // Let A, B and C being the components of Target and R be the location of
826 // the fixup. If the fixup is not pcrel, we want to compute (A - B + C).
827 // If it is pcrel, we want to compute (A - B + C - R).
Jason W Kim495c2bb2010-12-06 21:57:34 +0000828
Rafael Espindola5904e122014-03-29 06:26:49 +0000829 // In general, ELF has no relocations for -B. It can only represent (A + C)
830 // or (A + C - R). If B = R + K and the relocation is not pcrel, we can
831 // replace B to implement it: (A - R - K + C)
832 if (IsPCRel)
833 Asm.getContext().FatalError(
834 Fixup.getLoc(),
835 "No relocation available to represent this relative expression");
David Majnemera45a1762014-01-06 07:39:46 +0000836
Rafael Espindola5904e122014-03-29 06:26:49 +0000837 const MCSymbol &SymB = RefB->getSymbol();
Jason W Kim495c2bb2010-12-06 21:57:34 +0000838
Rafael Espindola5904e122014-03-29 06:26:49 +0000839 if (SymB.isUndefined())
840 Asm.getContext().FatalError(
841 Fixup.getLoc(),
842 Twine("symbol '") + SymB.getName() +
843 "' can not be undefined in a subtraction expression");
Jason W Kim495c2bb2010-12-06 21:57:34 +0000844
Rafael Espindola5904e122014-03-29 06:26:49 +0000845 assert(!SymB.isAbsolute() && "Should have been folded");
846 const MCSection &SecB = SymB.getSection();
847 if (&SecB != &FixupSection->getSection())
848 Asm.getContext().FatalError(
849 Fixup.getLoc(), "Cannot represent a difference across sections");
Jason W Kim495c2bb2010-12-06 21:57:34 +0000850
Rafael Espindola5904e122014-03-29 06:26:49 +0000851 const MCSymbolData &SymBD = Asm.getSymbolData(SymB);
Rafael Espindolaf275ad82015-03-25 13:16:53 +0000852 if (::isWeak(SymBD))
Rafael Espindolac9e70682015-03-24 23:48:44 +0000853 Asm.getContext().FatalError(
854 Fixup.getLoc(), "Cannot represent a subtraction with a weak symbol");
855
Rafael Espindola5904e122014-03-29 06:26:49 +0000856 uint64_t SymBOffset = Layout.getSymbolOffset(&SymBD);
857 uint64_t K = SymBOffset - FixupOffset;
858 IsPCRel = true;
859 C -= K;
Jason W Kim495c2bb2010-12-06 21:57:34 +0000860 }
861
Rafael Espindola5904e122014-03-29 06:26:49 +0000862 // We either rejected the fixup or folded B into C at this point.
863 const MCSymbolRefExpr *RefA = Target.getSymA();
864 const MCSymbol *SymA = RefA ? &RefA->getSymbol() : nullptr;
865 const MCSymbolData *SymAD = SymA ? &Asm.getSymbolData(*SymA) : nullptr;
866
Rafael Espindolac03f44c2014-03-27 20:49:35 +0000867 unsigned Type = GetRelocType(Target, Fixup, IsPCRel);
Rafael Espindolab60c8292014-04-29 12:46:50 +0000868 bool RelocateWithSymbol = shouldRelocateWithSymbol(Asm, RefA, SymAD, C, Type);
Rafael Espindola5904e122014-03-29 06:26:49 +0000869 if (!RelocateWithSymbol && SymA && !SymA->isUndefined())
870 C += Layout.getSymbolOffset(SymAD);
871
872 uint64_t Addend = 0;
873 if (hasRelocationAddend()) {
874 Addend = C;
875 C = 0;
876 }
877
878 FixedValue = C;
879
880 // FIXME: What is this!?!?
881 MCSymbolRefExpr::VariantKind Modifier =
882 RefA ? RefA->getKind() : MCSymbolRefExpr::VK_None;
Rafael Espindola9e252bf2011-12-21 14:26:29 +0000883 if (RelocNeedsGOT(Modifier))
884 NeedsGOT = true;
Jan Sjödin30a52de2011-02-28 21:45:04 +0000885
Rafael Espindola5904e122014-03-29 06:26:49 +0000886 if (!RelocateWithSymbol) {
887 const MCSection *SecA =
888 (SymA && !SymA->isUndefined()) ? &SymA->getSection() : nullptr;
Rafael Espindolab6613022014-10-17 01:48:58 +0000889 auto *ELFSec = cast_or_null<MCSectionELF>(SecA);
890 MCSymbol *SectionSymbol =
891 ELFSec ? Asm.getContext().getOrCreateSectionSymbol(*ELFSec)
892 : nullptr;
893 ELFRelocationEntry Rec(FixupOffset, SectionSymbol, Type, Addend);
Rafael Espindola5904e122014-03-29 06:26:49 +0000894 Relocations[FixupSection].push_back(Rec);
895 return;
896 }
Roman Divackydfbecd12011-08-04 19:08:19 +0000897
Rafael Espindola5904e122014-03-29 06:26:49 +0000898 if (SymA) {
899 if (const MCSymbol *R = Renames.lookup(SymA))
900 SymA = R;
Rafael Espindolad7facaf2011-08-04 13:05:26 +0000901
Rafael Espindola3d082fa2014-05-03 19:57:04 +0000902 if (const MCSymbol *WeakRef = getWeakRef(*RefA))
903 WeakrefUsedInReloc.insert(WeakRef);
Rafael Espindola5904e122014-03-29 06:26:49 +0000904 else
905 UsedInReloc.insert(SymA);
906 }
907 ELFRelocationEntry Rec(FixupOffset, SymA, Type, Addend);
908 Relocations[FixupSection].push_back(Rec);
909 return;
Jason W Kim495c2bb2010-12-06 21:57:34 +0000910}
911
912
Benjamin Kramer86511dc2010-08-23 21:19:37 +0000913uint64_t
Daniel Dunbarfe0c28f2010-11-13 07:33:40 +0000914ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
915 const MCSymbol *S) {
David Blaikie908f4d42014-04-24 16:59:40 +0000916 const MCSymbolData &SD = Asm.getSymbolData(*S);
Rafael Espindola0e3decf2010-11-14 03:12:24 +0000917 return SD.getIndex();
Matt Fleming6c1ad482010-08-16 18:57:57 +0000918}
919
Rafael Espindola3b5ee552014-04-28 13:39:57 +0000920bool ELFObjectWriter::isInSymtab(const MCAsmLayout &Layout,
921 const MCSymbolData &Data, bool Used,
922 bool Renamed) {
Rafael Espindola7fadc0e2014-03-20 02:12:01 +0000923 const MCSymbol &Symbol = Data.getSymbol();
924 if (Symbol.isVariable()) {
925 const MCExpr *Expr = Symbol.getVariableValue();
926 if (const MCSymbolRefExpr *Ref = dyn_cast<MCSymbolRefExpr>(Expr)) {
927 if (Ref->getKind() == MCSymbolRefExpr::VK_WEAKREF)
928 return false;
929 }
930 }
Rafael Espindola16145972010-11-01 14:28:48 +0000931
Rafael Espindolaee8d1512010-10-19 19:31:37 +0000932 if (Used)
933 return true;
934
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000935 if (Renamed)
936 return false;
937
Rafael Espindola45834a02010-10-29 23:09:31 +0000938 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
939 return true;
940
Rafael Espindola3b5ee552014-04-28 13:39:57 +0000941 if (Symbol.isVariable()) {
Rafael Espindola2aeac7a2014-05-01 13:24:25 +0000942 const MCSymbol *Base = Layout.getBaseSymbol(Symbol);
Rafael Espindola3b5ee552014-04-28 13:39:57 +0000943 if (Base && Base->isUndefined())
944 return false;
945 }
Rafael Espindola9e18e962011-02-23 20:22:07 +0000946
Jan Sjödin30a52de2011-02-28 21:45:04 +0000947 bool IsGlobal = MCELF::GetBinding(Data) == ELF::STB_GLOBAL;
Rafael Espindola9e18e962011-02-23 20:22:07 +0000948 if (!Symbol.isVariable() && Symbol.isUndefined() && !IsGlobal)
Rafael Espindolaa5efd6a2010-10-27 14:44:52 +0000949 return false;
950
Rafael Espindolaee8d1512010-10-19 19:31:37 +0000951 if (Symbol.isTemporary())
Rafael Espindolab1d07892010-10-05 18:01:23 +0000952 return false;
953
954 return true;
955}
956
Rafael Espindola39f50422014-04-28 14:24:44 +0000957bool ELFObjectWriter::isLocal(const MCSymbolData &Data, bool isUsedInReloc) {
Rafael Espindolab1d07892010-10-05 18:01:23 +0000958 if (Data.isExternal())
959 return false;
960
961 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindola39f50422014-04-28 14:24:44 +0000962 if (Symbol.isDefined())
963 return true;
Rafael Espindola7d0ba342010-11-14 04:17:37 +0000964
Rafael Espindola39f50422014-04-28 14:24:44 +0000965 if (isUsedInReloc)
Rafael Espindolab1d07892010-10-05 18:01:23 +0000966 return false;
967
968 return true;
969}
970
Rafael Espindolaead85492015-02-17 20:31:13 +0000971void ELFObjectWriter::computeIndexMap(MCAssembler &Asm,
Rafael Espindola1557fd62011-03-20 18:44:20 +0000972 SectionIndexMapTy &SectionIndexMap,
Rafael Espindolaead85492015-02-17 20:31:13 +0000973 RelMapTy &RelMap) {
Rafael Espindolad6340032010-11-10 21:51:05 +0000974 unsigned Index = 1;
975 for (MCAssembler::iterator it = Asm.begin(),
976 ie = Asm.end(); it != ie; ++it) {
977 const MCSectionELF &Section =
978 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000979 if (Section.getType() != ELF::SHT_GROUP)
980 continue;
981 SectionIndexMap[&Section] = Index++;
982 }
983
984 for (MCAssembler::iterator it = Asm.begin(),
985 ie = Asm.end(); it != ie; ++it) {
Rafael Espindolaead85492015-02-17 20:31:13 +0000986 const MCSectionData &SD = *it;
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000987 const MCSectionELF &Section =
Rafael Espindolaead85492015-02-17 20:31:13 +0000988 static_cast<const MCSectionELF &>(SD.getSection());
Rafael Espindola1557fd62011-03-20 18:44:20 +0000989 if (Section.getType() == ELF::SHT_GROUP ||
990 Section.getType() == ELF::SHT_REL ||
991 Section.getType() == ELF::SHT_RELA)
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000992 continue;
Rafael Espindolad6340032010-11-10 21:51:05 +0000993 SectionIndexMap[&Section] = Index++;
Rafael Espindolaead85492015-02-17 20:31:13 +0000994 if (MCSectionData *RelSD = createRelocationSection(Asm, SD)) {
995 const MCSectionELF *RelSection =
996 static_cast<const MCSectionELF *>(&RelSD->getSection());
Rafael Espindola7fe7e052015-02-17 20:40:59 +0000997 RelMap[RelSection] = &Section;
Rafael Espindola1557fd62011-03-20 18:44:20 +0000998 SectionIndexMap[RelSection] = Index++;
Rafael Espindolaead85492015-02-17 20:31:13 +0000999 }
Rafael Espindolad6340032010-11-10 21:51:05 +00001000 }
1001}
1002
Rafael Espindola022bb762014-03-24 03:43:21 +00001003void
1004ELFObjectWriter::computeSymbolTable(MCAssembler &Asm, const MCAsmLayout &Layout,
1005 const SectionIndexMapTy &SectionIndexMap,
Benjamin Kramer04f9da82014-09-19 12:26:38 +00001006 const RevGroupMapTy &RevGroupMap,
Rafael Espindola022bb762014-03-24 03:43:21 +00001007 unsigned NumRegularSections) {
Rafael Espindolad03e81d2010-10-05 15:48:37 +00001008 // FIXME: Is this the correct place to do this?
Rafael Espindola940a0ee2011-06-05 01:20:06 +00001009 // FIXME: Why is an undefined reference to _GLOBAL_OFFSET_TABLE_ needed?
Rafael Espindolad03e81d2010-10-05 15:48:37 +00001010 if (NeedsGOT) {
Dmitri Gribenko226fea52013-01-13 16:01:15 +00001011 StringRef Name = "_GLOBAL_OFFSET_TABLE_";
Rafael Espindolad03e81d2010-10-05 15:48:37 +00001012 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
1013 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
1014 Data.setExternal(true);
Jan Sjödin30a52de2011-02-28 21:45:04 +00001015 MCELF::SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindolad03e81d2010-10-05 15:48:37 +00001016 }
1017
Rafael Espindolabee6e9f2010-10-14 16:34:44 +00001018 // Add the data for the symbols.
David Blaikie583a31c2014-04-18 18:24:25 +00001019 for (MCSymbolData &SD : Asm.symbols()) {
1020 const MCSymbol &Symbol = SD.getSymbol();
Matt Fleming6c1ad482010-08-16 18:57:57 +00001021
Rafael Espindola16145972010-11-01 14:28:48 +00001022 bool Used = UsedInReloc.count(&Symbol);
1023 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
Rafael Espindola7d0ba342010-11-14 04:17:37 +00001024 bool isSignature = RevGroupMap.count(&Symbol);
1025
Rafael Espindola3b5ee552014-04-28 13:39:57 +00001026 if (!isInSymtab(Layout, SD,
Rafael Espindola7d0ba342010-11-14 04:17:37 +00001027 Used || WeakrefUsed || isSignature,
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +00001028 Renames.count(&Symbol)))
Matt Fleming6c1ad482010-08-16 18:57:57 +00001029 continue;
1030
Matt Fleming6c1ad482010-08-16 18:57:57 +00001031 ELFSymbolData MSD;
David Blaikie583a31c2014-04-18 18:24:25 +00001032 MSD.SymbolData = &SD;
Rafael Espindola2aeac7a2014-05-01 13:24:25 +00001033 const MCSymbol *BaseSymbol = Layout.getBaseSymbol(Symbol);
Matt Fleming6c1ad482010-08-16 18:57:57 +00001034
Rafael Espindola7d0ba342010-11-14 04:17:37 +00001035 // Undefined symbols are global, but this is the first place we
1036 // are able to set it.
Rafael Espindola39f50422014-04-28 14:24:44 +00001037 bool Local = isLocal(SD, Used);
David Blaikie583a31c2014-04-18 18:24:25 +00001038 if (!Local && MCELF::GetBinding(SD) == ELF::STB_LOCAL) {
Rafael Espindola022bb762014-03-24 03:43:21 +00001039 assert(BaseSymbol);
David Blaikie583a31c2014-04-18 18:24:25 +00001040 MCSymbolData &BaseData = Asm.getSymbolData(*BaseSymbol);
Jan Sjödin30a52de2011-02-28 21:45:04 +00001041 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
David Blaikie583a31c2014-04-18 18:24:25 +00001042 MCELF::SetBinding(BaseData, ELF::STB_GLOBAL);
Rafael Espindola7d0ba342010-11-14 04:17:37 +00001043 }
1044
Rafael Espindola022bb762014-03-24 03:43:21 +00001045 if (!BaseSymbol) {
1046 MSD.SectionIndex = ELF::SHN_ABS;
David Blaikie583a31c2014-04-18 18:24:25 +00001047 } else if (SD.isCommon()) {
Rafael Espindolabee6e9f2010-10-14 16:34:44 +00001048 assert(!Local);
Rafael Espindolaf0591c12010-09-21 00:24:38 +00001049 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindola022bb762014-03-24 03:43:21 +00001050 } else if (BaseSymbol->isUndefined()) {
Rafael Espindola7d0ba342010-11-14 04:17:37 +00001051 if (isSignature && !Used)
Benjamin Kramer04f9da82014-09-19 12:26:38 +00001052 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap.lookup(&Symbol));
Rafael Espindola7d0ba342010-11-14 04:17:37 +00001053 else
1054 MSD.SectionIndex = ELF::SHN_UNDEF;
Rafael Espindola022bb762014-03-24 03:43:21 +00001055 if (!Used && WeakrefUsed)
David Blaikie583a31c2014-04-18 18:24:25 +00001056 MCELF::SetBinding(SD, ELF::STB_WEAK);
Matt Fleming6c1ad482010-08-16 18:57:57 +00001057 } else {
Rafael Espindolad6340032010-11-10 21:51:05 +00001058 const MCSectionELF &Section =
Rafael Espindola022bb762014-03-24 03:43:21 +00001059 static_cast<const MCSectionELF&>(BaseSymbol->getSection());
Rafael Espindolad6340032010-11-10 21:51:05 +00001060 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Matt Fleming6c1ad482010-08-16 18:57:57 +00001061 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindolafbcf0db2010-10-15 15:39:06 +00001062 }
1063
Rafael Espindola5a67ed12015-01-22 14:20:45 +00001064 // The @@@ in symbol version is replaced with @ in undefined symbols and @@
1065 // in defined ones.
1066 //
1067 // FIXME: All name handling should be done before we get to the writer,
NAKAMURA Takumib01d86b2015-02-25 11:02:00 +00001068 // including dealing with GNU-style version suffixes. Fixing this isn't
Rafael Espindola5a67ed12015-01-22 14:20:45 +00001069 // trivial.
1070 //
1071 // We thus have to be careful to not perform the symbol version replacement
1072 // blindly:
1073 //
1074 // The ELF format is used on Windows by the MCJIT engine. Thus, on
1075 // Windows, the ELFObjectWriter can encounter symbols mangled using the MS
1076 // Visual Studio C++ name mangling scheme. Symbols mangled using the MSVC
1077 // C++ name mangling can legally have "@@@" as a sub-string. In that case,
1078 // the EFLObjectWriter should not interpret the "@@@" sub-string as
1079 // specifying GNU-style symbol versioning. The ELFObjectWriter therefore
1080 // checks for the MSVC C++ name mangling prefix which is either "?", "@?",
1081 // "__imp_?" or "__imp_@?".
1082 //
1083 // It would have been interesting to perform the MS mangling prefix check
1084 // only when the target triple is of the form *-pc-windows-elf. But, it
1085 // seems that this information is not easily accessible from the
1086 // ELFObjectWriter.
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +00001087 StringRef Name = Symbol.getName();
Rafael Espindola5a67ed12015-01-22 14:20:45 +00001088 if (!Name.startswith("?") && !Name.startswith("@?") &&
1089 !Name.startswith("__imp_?") && !Name.startswith("__imp_@?")) {
1090 // This symbol isn't following the MSVC C++ name mangling convention. We
1091 // can thus safely interpret the @@@ in symbol names as specifying symbol
1092 // versioning.
1093 SmallString<32> Buf;
1094 size_t Pos = Name.find("@@@");
1095 if (Pos != StringRef::npos) {
1096 Buf += Name.substr(0, Pos);
1097 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
1098 Buf += Name.substr(Pos + Skip);
1099 Name = Buf;
1100 }
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +00001101 }
Rafael Espindolab6613022014-10-17 01:48:58 +00001102
1103 // Sections have their own string table
1104 if (MCELF::GetType(SD) != ELF::STT_SECTION)
1105 MSD.Name = StrTabBuilder.add(Name);
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +00001106
Rafael Espindolaa5efd6a2010-10-27 14:44:52 +00001107 if (MSD.SectionIndex == ELF::SHN_UNDEF)
1108 UndefinedSymbolData.push_back(MSD);
1109 else if (Local)
1110 LocalSymbolData.push_back(MSD);
1111 else
1112 ExternalSymbolData.push_back(MSD);
Matt Fleming6c1ad482010-08-16 18:57:57 +00001113 }
1114
Hans Wennborg83e6e1e2014-04-30 16:25:02 +00001115 for (auto i = Asm.file_names_begin(), e = Asm.file_names_end(); i != e; ++i)
1116 StrTabBuilder.add(*i);
1117
Hans Wennborgf26bfc12014-09-29 22:43:20 +00001118 StrTabBuilder.finalize(StringTableBuilder::ELF);
Hans Wennborg83e6e1e2014-04-30 16:25:02 +00001119
1120 for (auto i = Asm.file_names_begin(), e = Asm.file_names_end(); i != e; ++i)
1121 FileSymbolData.push_back(StrTabBuilder.getOffset(*i));
1122
Rafael Espindolab6613022014-10-17 01:48:58 +00001123 for (ELFSymbolData &MSD : LocalSymbolData)
1124 MSD.StringIndex = MCELF::GetType(*MSD.SymbolData) == ELF::STT_SECTION
1125 ? 0
1126 : StrTabBuilder.getOffset(MSD.Name);
1127 for (ELFSymbolData &MSD : ExternalSymbolData)
Hans Wennborg83e6e1e2014-04-30 16:25:02 +00001128 MSD.StringIndex = StrTabBuilder.getOffset(MSD.Name);
1129 for (ELFSymbolData& MSD : UndefinedSymbolData)
1130 MSD.StringIndex = StrTabBuilder.getOffset(MSD.Name);
1131
Matt Fleming6c1ad482010-08-16 18:57:57 +00001132 // Symbols are required to be in lexicographic order.
1133 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
1134 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
1135 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
1136
1137 // Set the symbol indices. Local symbols must come before all other
1138 // symbols with non-local bindings.
Joerg Sonnenbergerfc184732013-10-29 01:06:17 +00001139 unsigned Index = FileSymbolData.size() + 1;
Matt Fleming6c1ad482010-08-16 18:57:57 +00001140 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
1141 LocalSymbolData[i].SymbolData->setIndex(Index++);
Rafael Espindola0e3decf2010-11-14 03:12:24 +00001142
Matt Fleming6c1ad482010-08-16 18:57:57 +00001143 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
1144 ExternalSymbolData[i].SymbolData->setIndex(Index++);
1145 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
1146 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
1147}
1148
Rafael Espindolaead85492015-02-17 20:31:13 +00001149MCSectionData *
1150ELFObjectWriter::createRelocationSection(MCAssembler &Asm,
1151 const MCSectionData &SD) {
1152 if (Relocations[&SD].empty())
1153 return nullptr;
Rafael Espindola1557fd62011-03-20 18:44:20 +00001154
Rafael Espindolaead85492015-02-17 20:31:13 +00001155 MCContext &Ctx = Asm.getContext();
1156 const MCSectionELF &Section =
1157 static_cast<const MCSectionELF &>(SD.getSection());
Matt Fleming6c1ad482010-08-16 18:57:57 +00001158
Rafael Espindolaead85492015-02-17 20:31:13 +00001159 const StringRef SectionName = Section.getSectionName();
1160 std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
1161 RelaSectionName += SectionName;
Benjamin Kramerfd054152010-08-17 17:56:13 +00001162
Rafael Espindolaead85492015-02-17 20:31:13 +00001163 unsigned EntrySize;
1164 if (hasRelocationAddend())
1165 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
1166 else
1167 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming6c1ad482010-08-16 18:57:57 +00001168
Rafael Espindolaead85492015-02-17 20:31:13 +00001169 unsigned Flags = 0;
1170 StringRef Group = "";
1171 if (Section.getFlags() & ELF::SHF_GROUP) {
1172 Flags = ELF::SHF_GROUP;
1173 Group = Section.getGroup()->getName();
Rafael Espindola1557fd62011-03-20 18:44:20 +00001174 }
Rafael Espindolaead85492015-02-17 20:31:13 +00001175
1176 const MCSectionELF *RelaSection = Ctx.getELFSection(
1177 RelaSectionName, hasRelocationAddend() ? ELF::SHT_RELA : ELF::SHT_REL,
Rafael Espindola68fa2492015-02-17 20:48:01 +00001178 Flags, EntrySize, Group, true);
Rafael Espindolaead85492015-02-17 20:31:13 +00001179 return &Asm.getOrCreateSectionData(*RelaSection);
Rafael Espindola1557fd62011-03-20 18:44:20 +00001180}
Matt Fleming6c1ad482010-08-16 18:57:57 +00001181
David Blaikiee06e8012014-04-11 22:11:50 +00001182static SmallVector<char, 128>
1183getUncompressedData(MCAsmLayout &Layout,
1184 MCSectionData::FragmentListType &Fragments) {
David Blaikie8019bf82014-04-10 21:53:53 +00001185 SmallVector<char, 128> UncompressedData;
1186 for (const MCFragment &F : Fragments) {
1187 const SmallVectorImpl<char> *Contents;
1188 switch (F.getKind()) {
1189 case MCFragment::FT_Data:
1190 Contents = &cast<MCDataFragment>(F).getContents();
1191 break;
1192 case MCFragment::FT_Dwarf:
1193 Contents = &cast<MCDwarfLineAddrFragment>(F).getContents();
1194 break;
1195 case MCFragment::FT_DwarfFrame:
1196 Contents = &cast<MCDwarfCallFrameFragment>(F).getContents();
1197 break;
1198 default:
1199 llvm_unreachable(
1200 "Not expecting any other fragment types in a debug_* section");
1201 }
1202 UncompressedData.append(Contents->begin(), Contents->end());
1203 }
1204 return UncompressedData;
1205}
1206
1207// Include the debug info compression header:
1208// "ZLIB" followed by 8 bytes representing the uncompressed size of the section,
1209// useful for consumers to preallocate a buffer to decompress into.
David Blaikie76d3a3c2014-04-18 21:52:26 +00001210static bool
David Blaikiee06e8012014-04-11 22:11:50 +00001211prependCompressionHeader(uint64_t Size,
1212 SmallVectorImpl<char> &CompressedContents) {
Benjamin Kramer7149aab2015-03-01 18:09:56 +00001213 const StringRef Magic = "ZLIB";
David Blaikie76d3a3c2014-04-18 21:52:26 +00001214 if (Size <= Magic.size() + sizeof(Size) + CompressedContents.size())
1215 return false;
David Blaikie8019bf82014-04-10 21:53:53 +00001216 if (sys::IsLittleEndianHost)
Artyom Skrobov9aea8432014-06-14 13:18:07 +00001217 sys::swapByteOrder(Size);
David Blaikie8019bf82014-04-10 21:53:53 +00001218 CompressedContents.insert(CompressedContents.begin(),
1219 Magic.size() + sizeof(Size), 0);
1220 std::copy(Magic.begin(), Magic.end(), CompressedContents.begin());
1221 std::copy(reinterpret_cast<char *>(&Size),
1222 reinterpret_cast<char *>(&Size + 1),
1223 CompressedContents.begin() + Magic.size());
David Blaikie76d3a3c2014-04-18 21:52:26 +00001224 return true;
David Blaikie8019bf82014-04-10 21:53:53 +00001225}
1226
1227// Return a single fragment containing the compressed contents of the whole
1228// section. Null if the section was not compressed for any reason.
David Blaikiee06e8012014-04-11 22:11:50 +00001229static std::unique_ptr<MCDataFragment>
1230getCompressedFragment(MCAsmLayout &Layout,
1231 MCSectionData::FragmentListType &Fragments) {
David Blaikie8019bf82014-04-10 21:53:53 +00001232 std::unique_ptr<MCDataFragment> CompressedFragment(new MCDataFragment());
1233
1234 // Gather the uncompressed data from all the fragments, recording the
1235 // alignment fragment, if seen, and any fixups.
1236 SmallVector<char, 128> UncompressedData =
1237 getUncompressedData(Layout, Fragments);
1238
1239 SmallVectorImpl<char> &CompressedContents = CompressedFragment->getContents();
1240
1241 zlib::Status Success = zlib::compress(
1242 StringRef(UncompressedData.data(), UncompressedData.size()),
1243 CompressedContents);
1244 if (Success != zlib::StatusOK)
1245 return nullptr;
1246
David Blaikie76d3a3c2014-04-18 21:52:26 +00001247 if (!prependCompressionHeader(UncompressedData.size(), CompressedContents))
1248 return nullptr;
David Blaikie8019bf82014-04-10 21:53:53 +00001249
1250 return CompressedFragment;
1251}
1252
David Blaikie39fa6a22014-04-25 00:48:01 +00001253typedef DenseMap<const MCSectionData *, std::vector<MCSymbolData *>>
1254DefiningSymbolMap;
1255
1256static void UpdateSymbols(const MCAsmLayout &Layout,
1257 const std::vector<MCSymbolData *> &Symbols,
1258 MCFragment &NewFragment) {
1259 for (MCSymbolData *Sym : Symbols) {
1260 Sym->setOffset(Sym->getOffset() +
1261 Layout.getFragmentOffset(Sym->getFragment()));
1262 Sym->setFragment(&NewFragment);
David Blaikiec029ab42014-04-18 21:24:12 +00001263 }
1264}
1265
David Blaikie8019bf82014-04-10 21:53:53 +00001266static void CompressDebugSection(MCAssembler &Asm, MCAsmLayout &Layout,
David Blaikie39fa6a22014-04-25 00:48:01 +00001267 const DefiningSymbolMap &DefiningSymbols,
David Blaikie8019bf82014-04-10 21:53:53 +00001268 const MCSectionELF &Section,
1269 MCSectionData &SD) {
1270 StringRef SectionName = Section.getSectionName();
1271 MCSectionData::FragmentListType &Fragments = SD.getFragmentList();
1272
1273 std::unique_ptr<MCDataFragment> CompressedFragment =
1274 getCompressedFragment(Layout, Fragments);
1275
1276 // Leave the section as-is if the fragments could not be compressed.
1277 if (!CompressedFragment)
1278 return;
1279
David Blaikiec029ab42014-04-18 21:24:12 +00001280 // Update the fragment+offsets of any symbols referring to fragments in this
1281 // section to refer to the new fragment.
David Blaikie39fa6a22014-04-25 00:48:01 +00001282 auto I = DefiningSymbols.find(&SD);
1283 if (I != DefiningSymbols.end())
1284 UpdateSymbols(Layout, I->second, *CompressedFragment);
David Blaikiec029ab42014-04-18 21:24:12 +00001285
David Blaikie8019bf82014-04-10 21:53:53 +00001286 // Invalidate the layout for the whole section since it will have new and
1287 // different fragments now.
1288 Layout.invalidateFragmentsFrom(&Fragments.front());
1289 Fragments.clear();
1290
1291 // Complete the initialization of the new fragment
1292 CompressedFragment->setParent(&SD);
1293 CompressedFragment->setLayoutOrder(0);
1294 Fragments.push_back(CompressedFragment.release());
1295
1296 // Rename from .debug_* to .zdebug_*
1297 Asm.getContext().renameELFSection(&Section,
1298 (".z" + SectionName.drop_front(1)).str());
1299}
1300
1301void ELFObjectWriter::CompressDebugSections(MCAssembler &Asm,
1302 MCAsmLayout &Layout) {
1303 if (!Asm.getContext().getAsmInfo()->compressDebugSections())
1304 return;
1305
David Blaikie39fa6a22014-04-25 00:48:01 +00001306 DefiningSymbolMap DefiningSymbols;
1307
1308 for (MCSymbolData &SD : Asm.symbols())
1309 if (MCFragment *F = SD.getFragment())
1310 DefiningSymbols[F->getParent()].push_back(&SD);
1311
David Blaikie8019bf82014-04-10 21:53:53 +00001312 for (MCSectionData &SD : Asm) {
David Blaikiee06e8012014-04-11 22:11:50 +00001313 const MCSectionELF &Section =
1314 static_cast<const MCSectionELF &>(SD.getSection());
David Blaikie8019bf82014-04-10 21:53:53 +00001315 StringRef SectionName = Section.getSectionName();
1316
1317 // Compressing debug_frame requires handling alignment fragments which is
1318 // more work (possibly generalizing MCAssembler.cpp:writeFragment to allow
1319 // for writing to arbitrary buffers) for little benefit.
1320 if (!SectionName.startswith(".debug_") || SectionName == ".debug_frame")
1321 continue;
1322
David Blaikie39fa6a22014-04-25 00:48:01 +00001323 CompressDebugSection(Asm, Layout, DefiningSymbols, Section, SD);
David Blaikie8019bf82014-04-10 21:53:53 +00001324 }
1325}
1326
Rafael Espindola1557fd62011-03-20 18:44:20 +00001327void ELFObjectWriter::WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout,
1328 const RelMapTy &RelMap) {
Rafael Espindola7fe7e052015-02-17 20:40:59 +00001329 for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it) {
1330 MCSectionData &RelSD = *it;
1331 const MCSectionELF &RelSection =
1332 static_cast<const MCSectionELF &>(RelSD.getSection());
Rafael Espindola1557fd62011-03-20 18:44:20 +00001333
Rafael Espindola7fe7e052015-02-17 20:40:59 +00001334 unsigned Type = RelSection.getType();
1335 if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
Rafael Espindola1557fd62011-03-20 18:44:20 +00001336 continue;
Matt Fleming6c1ad482010-08-16 18:57:57 +00001337
Rafael Espindola7fe7e052015-02-17 20:40:59 +00001338 const MCSectionELF *Section = RelMap.lookup(&RelSection);
1339 MCSectionData &SD = Asm.getOrCreateSectionData(*Section);
1340 RelSD.setAlignment(is64Bit() ? 8 : 4);
1341
1342 MCDataFragment *F = new MCDataFragment(&RelSD);
1343 WriteRelocationsFragment(Asm, F, &SD);
Matt Fleming6c1ad482010-08-16 18:57:57 +00001344 }
1345}
1346
Daniel Dunbarfe0c28f2010-11-13 07:33:40 +00001347void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
1348 uint64_t Flags, uint64_t Address,
1349 uint64_t Offset, uint64_t Size,
1350 uint32_t Link, uint32_t Info,
1351 uint64_t Alignment,
1352 uint64_t EntrySize) {
Matt Fleming6c1ad482010-08-16 18:57:57 +00001353 Write32(Name); // sh_name: index into string table
1354 Write32(Type); // sh_type
1355 WriteWord(Flags); // sh_flags
1356 WriteWord(Address); // sh_addr
1357 WriteWord(Offset); // sh_offset
1358 WriteWord(Size); // sh_size
1359 Write32(Link); // sh_link
1360 Write32(Info); // sh_info
1361 WriteWord(Alignment); // sh_addralign
1362 WriteWord(EntrySize); // sh_entsize
1363}
1364
Rafael Espindola5904e122014-03-29 06:26:49 +00001365// ELF doesn't require relocations to be in any order. We sort by the r_offset,
1366// just to match gnu as for easier comparison. The use type is an arbitrary way
1367// of making the sort deterministic.
1368static int cmpRel(const ELFRelocationEntry *AP, const ELFRelocationEntry *BP) {
1369 const ELFRelocationEntry &A = *AP;
1370 const ELFRelocationEntry &B = *BP;
1371 if (A.Offset != B.Offset)
1372 return B.Offset - A.Offset;
1373 if (B.Type != A.Type)
1374 return A.Type - B.Type;
Daniel Sanders60f1db02015-03-13 12:45:09 +00001375 //llvm_unreachable("ELFRelocs might be unstable!");
1376 return 0;
Rafael Espindola5904e122014-03-29 06:26:49 +00001377}
1378
1379static void sortRelocs(const MCAssembler &Asm,
1380 std::vector<ELFRelocationEntry> &Relocs) {
1381 array_pod_sort(Relocs.begin(), Relocs.end(), cmpRel);
1382}
1383
Daniel Dunbarfe0c28f2010-11-13 07:33:40 +00001384void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
1385 MCDataFragment *F,
1386 const MCSectionData *SD) {
Matt Fleming6c1ad482010-08-16 18:57:57 +00001387 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
Akira Hatanaka64ad2cf2012-03-23 23:06:45 +00001388
Rafael Espindola5904e122014-03-29 06:26:49 +00001389 sortRelocs(Asm, Relocs);
Matt Fleming6c1ad482010-08-16 18:57:57 +00001390
1391 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
Rafael Espindola5904e122014-03-29 06:26:49 +00001392 const ELFRelocationEntry &Entry = Relocs[e - i - 1];
Rafael Espindolab6613022014-10-17 01:48:58 +00001393 unsigned Index =
1394 Entry.Symbol ? getSymbolIndexInSymbolTable(Asm, Entry.Symbol) : 0;
Rafael Espindola5904e122014-03-29 06:26:49 +00001395
Rafael Espindolafdaae0d2010-12-18 03:27:34 +00001396 if (is64Bit()) {
Rafael Espindola5904e122014-03-29 06:26:49 +00001397 write(*F, Entry.Offset);
Jack Carter8ad0c272012-06-27 22:28:30 +00001398 if (TargetObjectWriter->isN64()) {
Rafael Espindola5904e122014-03-29 06:26:49 +00001399 write(*F, uint32_t(Index));
Benjamin Kramer6c3c3492010-09-09 18:01:29 +00001400
Rafael Espindola5904e122014-03-29 06:26:49 +00001401 write(*F, TargetObjectWriter->getRSsym(Entry.Type));
1402 write(*F, TargetObjectWriter->getRType3(Entry.Type));
1403 write(*F, TargetObjectWriter->getRType2(Entry.Type));
1404 write(*F, TargetObjectWriter->getRType(Entry.Type));
1405 } else {
Jack Carter8ad0c272012-06-27 22:28:30 +00001406 struct ELF::Elf64_Rela ERE64;
Rafael Espindola5904e122014-03-29 06:26:49 +00001407 ERE64.setSymbolAndType(Index, Entry.Type);
Rafael Espindola0ce09712014-03-25 22:43:53 +00001408 write(*F, ERE64.r_info);
Jack Carter8ad0c272012-06-27 22:28:30 +00001409 }
Rafael Espindolafdaae0d2010-12-18 03:27:34 +00001410 if (hasRelocationAddend())
Rafael Espindola5904e122014-03-29 06:26:49 +00001411 write(*F, Entry.Addend);
Benjamin Kramer6c3c3492010-09-09 18:01:29 +00001412 } else {
Rafael Espindola5904e122014-03-29 06:26:49 +00001413 write(*F, uint32_t(Entry.Offset));
Benjamin Kramer6c3c3492010-09-09 18:01:29 +00001414
Rafael Espindolabce26a12010-10-05 15:11:03 +00001415 struct ELF::Elf32_Rela ERE32;
Rafael Espindola5904e122014-03-29 06:26:49 +00001416 ERE32.setSymbolAndType(Index, Entry.Type);
Rafael Espindola0ce09712014-03-25 22:43:53 +00001417 write(*F, ERE32.r_info);
Benjamin Kramer6c3c3492010-09-09 18:01:29 +00001418
Rafael Espindolafdaae0d2010-12-18 03:27:34 +00001419 if (hasRelocationAddend())
Rafael Espindola5904e122014-03-29 06:26:49 +00001420 write(*F, uint32_t(Entry.Addend));
Benjamin Kramer6c3c3492010-09-09 18:01:29 +00001421 }
Matt Fleming6c1ad482010-08-16 18:57:57 +00001422 }
1423}
1424
Rafael Espindolaef6baea2015-02-11 23:11:18 +00001425void ELFObjectWriter::CreateMetadataSections(
1426 MCAssembler &Asm, MCAsmLayout &Layout, SectionIndexMapTy &SectionIndexMap) {
Matt Fleming6c1ad482010-08-16 18:57:57 +00001427 MCContext &Ctx = Asm.getContext();
1428 MCDataFragment *F;
1429
Rafael Espindolafdaae0d2010-12-18 03:27:34 +00001430 unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
Matt Fleming6c1ad482010-08-16 18:57:57 +00001431
Rafael Espindola0e527b72010-09-22 19:04:41 +00001432 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola36ef57d2010-11-10 19:05:07 +00001433 const MCSectionELF *ShstrtabSection =
Rafael Espindolaba31e272015-01-29 17:33:21 +00001434 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0);
Rafael Espindola44bf2662010-09-16 19:46:31 +00001435 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1436 ShstrtabSD.setAlignment(1);
Rafael Espindolafbd0ddf2015-02-11 22:41:26 +00001437 ShstrtabIndex = SectionIndexMap.size() + 1;
1438 SectionIndexMap[ShstrtabSection] = ShstrtabIndex;
Rafael Espindola44bf2662010-09-16 19:46:31 +00001439
Rafael Espindola36ef57d2010-11-10 19:05:07 +00001440 const MCSectionELF *SymtabSection =
Rafael Espindola3fe87a12010-10-31 00:16:26 +00001441 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001442 EntrySize, "");
Matt Fleming6c1ad482010-08-16 18:57:57 +00001443 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Rafael Espindolafdaae0d2010-12-18 03:27:34 +00001444 SymtabSD.setAlignment(is64Bit() ? 8 : 4);
Rafael Espindolafbd0ddf2015-02-11 22:41:26 +00001445 SymbolTableIndex = SectionIndexMap.size() + 1;
1446 SectionIndexMap[SymtabSection] = SymbolTableIndex;
Rafael Espindola3fe87a12010-10-31 00:16:26 +00001447
Rafael Espindola1557fd62011-03-20 18:44:20 +00001448 const MCSectionELF *StrtabSection;
Rafael Espindolaba31e272015-01-29 17:33:21 +00001449 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0);
Matt Fleming6c1ad482010-08-16 18:57:57 +00001450 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1451 StrtabSD.setAlignment(1);
Rafael Espindolafbd0ddf2015-02-11 22:41:26 +00001452 StringTableIndex = SectionIndexMap.size() + 1;
1453 SectionIndexMap[StrtabSection] = StringTableIndex;
Rafael Espindola44bf2662010-09-16 19:46:31 +00001454
1455 // Symbol table
1456 F = new MCDataFragment(&SymtabSD);
Rafael Espindola10be0832014-03-25 23:44:25 +00001457 WriteSymbolTable(F, Asm, Layout, SectionIndexMap);
Rafael Espindola44bf2662010-09-16 19:46:31 +00001458
Matt Fleming6c1ad482010-08-16 18:57:57 +00001459 F = new MCDataFragment(&StrtabSD);
Hans Wennborg83e6e1e2014-04-30 16:25:02 +00001460 F->getContents().append(StrTabBuilder.data().begin(),
1461 StrTabBuilder.data().end());
Matt Fleming6c1ad482010-08-16 18:57:57 +00001462
Matt Fleming6c1ad482010-08-16 18:57:57 +00001463 F = new MCDataFragment(&ShstrtabSD);
1464
Hans Wennborg83e6e1e2014-04-30 16:25:02 +00001465 // Section header string table.
1466 for (auto it = Asm.begin(), ie = Asm.end(); it != ie; ++it) {
Rafael Espindolab80875e2011-04-07 23:21:52 +00001467 const MCSectionELF &Section =
1468 static_cast<const MCSectionELF&>(it->getSection());
Hans Wennborg83e6e1e2014-04-30 16:25:02 +00001469 ShStrTabBuilder.add(Section.getSectionName());
Rafael Espindolab80875e2011-04-07 23:21:52 +00001470 }
Hans Wennborgf26bfc12014-09-29 22:43:20 +00001471 ShStrTabBuilder.finalize(StringTableBuilder::ELF);
Hans Wennborg83e6e1e2014-04-30 16:25:02 +00001472 F->getContents().append(ShStrTabBuilder.data().begin(),
1473 ShStrTabBuilder.data().end());
Rafael Espindola2ebaee92010-09-30 02:22:20 +00001474}
1475
Rafael Espindolaead85492015-02-17 20:31:13 +00001476void ELFObjectWriter::createIndexedSections(MCAssembler &Asm,
Rafael Espindolab3eca9b2011-01-23 17:55:27 +00001477 MCAsmLayout &Layout,
1478 GroupMapTy &GroupMap,
Rafael Espindola1557fd62011-03-20 18:44:20 +00001479 RevGroupMapTy &RevGroupMap,
1480 SectionIndexMapTy &SectionIndexMap,
Rafael Espindolaead85492015-02-17 20:31:13 +00001481 RelMapTy &RelMap) {
Rafael Espindolab3eca9b2011-01-23 17:55:27 +00001482 MCContext &Ctx = Asm.getContext();
Rafael Espindolab3eca9b2011-01-23 17:55:27 +00001483
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001484 // Build the groups
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001485 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1486 it != ie; ++it) {
1487 const MCSectionELF &Section =
1488 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola0e7e34e2011-01-23 04:43:11 +00001489 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001490 continue;
1491
1492 const MCSymbol *SignatureSymbol = Section.getGroup();
1493 Asm.getOrCreateSymbolData(*SignatureSymbol);
Rafael Espindola7d0ba342010-11-14 04:17:37 +00001494 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001495 if (!Group) {
Rafael Espindolab3eca9b2011-01-23 17:55:27 +00001496 Group = Ctx.CreateELFGroupSection();
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001497 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1498 Data.setAlignment(4);
1499 MCDataFragment *F = new MCDataFragment(&Data);
Rafael Espindola0ce09712014-03-25 22:43:53 +00001500 write(*F, uint32_t(ELF::GRP_COMDAT));
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001501 }
1502 GroupMap[Group] = SignatureSymbol;
1503 }
1504
Rafael Espindolaead85492015-02-17 20:31:13 +00001505 computeIndexMap(Asm, SectionIndexMap, RelMap);
Rafael Espindola1557fd62011-03-20 18:44:20 +00001506
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001507 // Add sections to the groups
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001508 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
Rafael Espindola1557fd62011-03-20 18:44:20 +00001509 it != ie; ++it) {
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001510 const MCSectionELF &Section =
1511 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola0e7e34e2011-01-23 04:43:11 +00001512 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001513 continue;
Rafael Espindola7d0ba342010-11-14 04:17:37 +00001514 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001515 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1516 // FIXME: we could use the previous fragment
1517 MCDataFragment *F = new MCDataFragment(&Data);
Rafael Espindola0ce09712014-03-25 22:43:53 +00001518 uint32_t Index = SectionIndexMap.lookup(&Section);
1519 write(*F, Index);
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001520 }
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001521}
1522
Rafael Espindola7fe7e052015-02-17 20:40:59 +00001523void ELFObjectWriter::writeSection(MCAssembler &Asm,
Daniel Dunbarfe0c28f2010-11-13 07:33:40 +00001524 const SectionIndexMapTy &SectionIndexMap,
Rafael Espindola7fe7e052015-02-17 20:40:59 +00001525 const RelMapTy &RelMap,
Daniel Dunbarfe0c28f2010-11-13 07:33:40 +00001526 uint32_t GroupSymbolIndex,
1527 uint64_t Offset, uint64_t Size,
1528 uint64_t Alignment,
1529 const MCSectionELF &Section) {
Rafael Espindola5a8d7812010-11-10 23:36:59 +00001530 uint64_t sh_link = 0;
1531 uint64_t sh_info = 0;
1532
1533 switch(Section.getType()) {
1534 case ELF::SHT_DYNAMIC:
Hans Wennborg83e6e1e2014-04-30 16:25:02 +00001535 sh_link = ShStrTabBuilder.getOffset(Section.getSectionName());
Rafael Espindola5a8d7812010-11-10 23:36:59 +00001536 sh_info = 0;
1537 break;
1538
1539 case ELF::SHT_REL:
1540 case ELF::SHT_RELA: {
Rafael Espindola3a7c0eb2015-02-17 20:37:50 +00001541 sh_link = SymbolTableIndex;
Rafael Espindola5a8d7812010-11-10 23:36:59 +00001542 assert(sh_link && ".symtab not found");
Rafael Espindola7fe7e052015-02-17 20:40:59 +00001543 const MCSectionELF *InfoSection = RelMap.find(&Section)->second;
Rafael Espindola5a8d7812010-11-10 23:36:59 +00001544 sh_info = SectionIndexMap.lookup(InfoSection);
1545 break;
1546 }
1547
1548 case ELF::SHT_SYMTAB:
1549 case ELF::SHT_DYNSYM:
1550 sh_link = StringTableIndex;
1551 sh_info = LastLocalSymbolIndex;
1552 break;
1553
1554 case ELF::SHT_SYMTAB_SHNDX:
1555 sh_link = SymbolTableIndex;
1556 break;
1557
1558 case ELF::SHT_PROGBITS:
1559 case ELF::SHT_STRTAB:
1560 case ELF::SHT_NOBITS:
Rafael Espindola9ae2d052010-12-26 21:30:59 +00001561 case ELF::SHT_NOTE:
Rafael Espindola5a8d7812010-11-10 23:36:59 +00001562 case ELF::SHT_NULL:
1563 case ELF::SHT_ARM_ATTRIBUTES:
Jason W Kim9c5b65d2011-01-12 00:19:25 +00001564 case ELF::SHT_INIT_ARRAY:
1565 case ELF::SHT_FINI_ARRAY:
1566 case ELF::SHT_PREINIT_ARRAY:
Rafael Espindola4b7b7fb2011-01-23 05:43:40 +00001567 case ELF::SHT_X86_64_UNWIND:
Jack Carterc1b17ed2013-01-18 21:20:38 +00001568 case ELF::SHT_MIPS_REGINFO:
1569 case ELF::SHT_MIPS_OPTIONS:
Vladimir Medicfb8a2a92014-07-08 08:59:22 +00001570 case ELF::SHT_MIPS_ABIFLAGS:
Rafael Espindola5a8d7812010-11-10 23:36:59 +00001571 // Nothing to do.
1572 break;
1573
Nick Lewyckyc6ac5f72011-10-07 23:28:32 +00001574 case ELF::SHT_GROUP:
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001575 sh_link = SymbolTableIndex;
1576 sh_info = GroupSymbolIndex;
1577 break;
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001578
Rafael Espindola5a8d7812010-11-10 23:36:59 +00001579 default:
Craig Topper35b2f752014-06-19 06:10:58 +00001580 llvm_unreachable("FIXME: sh_type value not supported!");
Rafael Espindola5a8d7812010-11-10 23:36:59 +00001581 }
1582
Logan Chien4b724422013-02-05 14:18:59 +00001583 if (TargetObjectWriter->getEMachine() == ELF::EM_ARM &&
1584 Section.getType() == ELF::SHT_ARM_EXIDX) {
1585 StringRef SecName(Section.getSectionName());
1586 if (SecName == ".ARM.exidx") {
Rafael Espindolaba31e272015-01-29 17:33:21 +00001587 sh_link = SectionIndexMap.lookup(Asm.getContext().getELFSection(
1588 ".text", ELF::SHT_PROGBITS, ELF::SHF_EXECINSTR | ELF::SHF_ALLOC));
Logan Chien4b724422013-02-05 14:18:59 +00001589 } else if (SecName.startswith(".ARM.exidx")) {
David Blaikie15b25df2013-10-22 23:41:52 +00001590 StringRef GroupName =
1591 Section.getGroup() ? Section.getGroup()->getName() : "";
1592 sh_link = SectionIndexMap.lookup(Asm.getContext().getELFSection(
1593 SecName.substr(sizeof(".ARM.exidx") - 1), ELF::SHT_PROGBITS,
Rafael Espindolaba31e272015-01-29 17:33:21 +00001594 ELF::SHF_EXECINSTR | ELF::SHF_ALLOC, 0, GroupName));
Logan Chien4b724422013-02-05 14:18:59 +00001595 }
1596 }
1597
Hans Wennborg83e6e1e2014-04-30 16:25:02 +00001598 WriteSecHdrEntry(ShStrTabBuilder.getOffset(Section.getSectionName()),
1599 Section.getType(),
Rafael Espindola5a8d7812010-11-10 23:36:59 +00001600 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1601 Alignment, Section.getEntrySize());
1602}
1603
Jan Sjödin30a52de2011-02-28 21:45:04 +00001604bool ELFObjectWriter::IsELFMetaDataSection(const MCSectionData &SD) {
Rafael Espindolabaf2f3b2010-12-06 03:48:09 +00001605 return SD.getOrdinal() == ~UINT32_C(0) &&
Rafael Espindola60ebca92010-12-02 03:09:06 +00001606 !SD.getSection().isVirtualSection();
1607}
1608
Jan Sjödin30a52de2011-02-28 21:45:04 +00001609uint64_t ELFObjectWriter::DataSectionSize(const MCSectionData &SD) {
Rafael Espindola60ebca92010-12-02 03:09:06 +00001610 uint64_t Ret = 0;
1611 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1612 ++i) {
1613 const MCFragment &F = *i;
1614 assert(F.getKind() == MCFragment::FT_Data);
1615 Ret += cast<MCDataFragment>(F).getContents().size();
1616 }
1617 return Ret;
1618}
1619
Jan Sjödin30a52de2011-02-28 21:45:04 +00001620uint64_t ELFObjectWriter::GetSectionFileSize(const MCAsmLayout &Layout,
1621 const MCSectionData &SD) {
Rafael Espindola60ebca92010-12-02 03:09:06 +00001622 if (IsELFMetaDataSection(SD))
1623 return DataSectionSize(SD);
1624 return Layout.getSectionFileSize(&SD);
1625}
1626
Jan Sjödin30a52de2011-02-28 21:45:04 +00001627uint64_t ELFObjectWriter::GetSectionAddressSize(const MCAsmLayout &Layout,
1628 const MCSectionData &SD) {
Rafael Espindola60ebca92010-12-02 03:09:06 +00001629 if (IsELFMetaDataSection(SD))
1630 return DataSectionSize(SD);
Rafael Espindola93e3cf02010-12-07 00:27:36 +00001631 return Layout.getSectionAddressSize(&SD);
Rafael Espindola60ebca92010-12-02 03:09:06 +00001632}
1633
Rafael Espindola1557fd62011-03-20 18:44:20 +00001634void ELFObjectWriter::WriteDataSectionData(MCAssembler &Asm,
1635 const MCAsmLayout &Layout,
1636 const MCSectionELF &Section) {
Rafael Espindola1557fd62011-03-20 18:44:20 +00001637 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1638
Benjamin Kramer084b9f42012-01-20 14:42:32 +00001639 uint64_t Padding = OffsetToAlignment(OS.tell(), SD.getAlignment());
Rafael Espindola1557fd62011-03-20 18:44:20 +00001640 WriteZeros(Padding);
Rafael Espindola1557fd62011-03-20 18:44:20 +00001641
1642 if (IsELFMetaDataSection(SD)) {
1643 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1644 ++i) {
1645 const MCFragment &F = *i;
1646 assert(F.getKind() == MCFragment::FT_Data);
Eli Bendersky84b2a792012-12-07 22:06:56 +00001647 WriteBytes(cast<MCDataFragment>(F).getContents());
Rafael Espindola1557fd62011-03-20 18:44:20 +00001648 }
1649 } else {
Jim Grosbach18e2fe42011-12-06 00:03:48 +00001650 Asm.writeSectionData(&SD, Layout);
Rafael Espindola60ebca92010-12-02 03:09:06 +00001651 }
1652}
1653
Rafael Espindola7fe7e052015-02-17 20:40:59 +00001654void ELFObjectWriter::writeSectionHeader(
1655 MCAssembler &Asm, const GroupMapTy &GroupMap, const MCAsmLayout &Layout,
1656 const SectionIndexMapTy &SectionIndexMap, const RelMapTy &RelMap,
1657 const SectionOffsetMapTy &SectionOffsetMap) {
Rafael Espindola1557fd62011-03-20 18:44:20 +00001658 const unsigned NumSections = Asm.size() + 1;
Matt Fleming6c1ad482010-08-16 18:57:57 +00001659
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001660 std::vector<const MCSectionELF*> Sections;
Rafael Espindola1557fd62011-03-20 18:44:20 +00001661 Sections.resize(NumSections - 1);
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001662
1663 for (SectionIndexMapTy::const_iterator i=
1664 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1665 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
Rafael Espindola1557fd62011-03-20 18:44:20 +00001666 Sections[p.second - 1] = p.first;
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001667 }
1668
Matt Fleming6c1ad482010-08-16 18:57:57 +00001669 // Null section first.
Rafael Espindola3fe87a12010-10-31 00:16:26 +00001670 uint64_t FirstSectionSize =
1671 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1672 uint32_t FirstSectionLink =
1673 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1674 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming6c1ad482010-08-16 18:57:57 +00001675
Rafael Espindola1557fd62011-03-20 18:44:20 +00001676 for (unsigned i = 0; i < NumSections - 1; ++i) {
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001677 const MCSectionELF &Section = *Sections[i];
1678 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1679 uint32_t GroupSymbolIndex;
1680 if (Section.getType() != ELF::SHT_GROUP)
1681 GroupSymbolIndex = 0;
1682 else
Rafael Espindola1557fd62011-03-20 18:44:20 +00001683 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm,
1684 GroupMap.lookup(&Section));
Matt Fleming6c1ad482010-08-16 18:57:57 +00001685
Rafael Espindola93e3cf02010-12-07 00:27:36 +00001686 uint64_t Size = GetSectionAddressSize(Layout, SD);
Rafael Espindola60ebca92010-12-02 03:09:06 +00001687
Rafael Espindola7fe7e052015-02-17 20:40:59 +00001688 writeSection(Asm, SectionIndexMap, RelMap, GroupSymbolIndex,
Rafael Espindola1557fd62011-03-20 18:44:20 +00001689 SectionOffsetMap.lookup(&Section), Size,
Rafael Espindola5a8d7812010-11-10 23:36:59 +00001690 SD.getAlignment(), Section);
Matt Fleming6c1ad482010-08-16 18:57:57 +00001691 }
1692}
1693
Rafael Espindola1557fd62011-03-20 18:44:20 +00001694void ELFObjectWriter::ComputeSectionOrder(MCAssembler &Asm,
1695 std::vector<const MCSectionELF*> &Sections) {
1696 for (MCAssembler::iterator it = Asm.begin(),
1697 ie = Asm.end(); it != ie; ++it) {
1698 const MCSectionELF &Section =
1699 static_cast<const MCSectionELF &>(it->getSection());
1700 if (Section.getType() == ELF::SHT_GROUP)
1701 Sections.push_back(&Section);
1702 }
1703
1704 for (MCAssembler::iterator it = Asm.begin(),
1705 ie = Asm.end(); it != ie; ++it) {
1706 const MCSectionELF &Section =
1707 static_cast<const MCSectionELF &>(it->getSection());
1708 if (Section.getType() != ELF::SHT_GROUP &&
1709 Section.getType() != ELF::SHT_REL &&
1710 Section.getType() != ELF::SHT_RELA)
1711 Sections.push_back(&Section);
1712 }
1713
1714 for (MCAssembler::iterator it = Asm.begin(),
1715 ie = Asm.end(); it != ie; ++it) {
1716 const MCSectionELF &Section =
1717 static_cast<const MCSectionELF &>(it->getSection());
1718 if (Section.getType() == ELF::SHT_REL ||
1719 Section.getType() == ELF::SHT_RELA)
1720 Sections.push_back(&Section);
1721 }
1722}
1723
1724void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1725 const MCAsmLayout &Layout) {
1726 GroupMapTy GroupMap;
1727 RevGroupMapTy RevGroupMap;
1728 SectionIndexMapTy SectionIndexMap;
1729
1730 unsigned NumUserSections = Asm.size();
1731
David Blaikiee06e8012014-04-11 22:11:50 +00001732 CompressDebugSections(Asm, const_cast<MCAsmLayout &>(Layout));
David Blaikie8019bf82014-04-10 21:53:53 +00001733
Rafael Espindola1557fd62011-03-20 18:44:20 +00001734 DenseMap<const MCSectionELF*, const MCSectionELF*> RelMap;
Rafael Espindola1557fd62011-03-20 18:44:20 +00001735 const unsigned NumUserAndRelocSections = Asm.size();
Rafael Espindolaead85492015-02-17 20:31:13 +00001736 createIndexedSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
Rafael Espindola1557fd62011-03-20 18:44:20 +00001737 RevGroupMap, SectionIndexMap, RelMap);
1738 const unsigned AllSections = Asm.size();
1739 const unsigned NumIndexedSections = AllSections - NumUserAndRelocSections;
1740
1741 unsigned NumRegularSections = NumUserSections + NumIndexedSections;
1742
1743 // Compute symbol table information.
Rafael Espindola022bb762014-03-24 03:43:21 +00001744 computeSymbolTable(Asm, Layout, SectionIndexMap, RevGroupMap,
1745 NumRegularSections);
Rafael Espindola1557fd62011-03-20 18:44:20 +00001746
1747 WriteRelocations(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1748
1749 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
1750 const_cast<MCAsmLayout&>(Layout),
Rafael Espindolaef6baea2015-02-11 23:11:18 +00001751 SectionIndexMap);
Rafael Espindola1557fd62011-03-20 18:44:20 +00001752
1753 uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
1754 uint64_t HeaderSize = is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
1755 sizeof(ELF::Elf32_Ehdr);
1756 uint64_t FileOff = HeaderSize;
1757
1758 std::vector<const MCSectionELF*> Sections;
1759 ComputeSectionOrder(Asm, Sections);
1760 unsigned NumSections = Sections.size();
1761 SectionOffsetMapTy SectionOffsetMap;
1762 for (unsigned i = 0; i < NumRegularSections + 1; ++i) {
1763 const MCSectionELF &Section = *Sections[i];
1764 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1765
1766 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1767
1768 // Remember the offset into the file for this section.
1769 SectionOffsetMap[&Section] = FileOff;
1770
1771 // Get the size of the section in the output file (including padding).
1772 FileOff += GetSectionFileSize(Layout, SD);
1773 }
1774
1775 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1776
1777 const unsigned SectionHeaderOffset = FileOff - HeaderSize;
1778
1779 uint64_t SectionHeaderEntrySize = is64Bit() ?
1780 sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr);
1781 FileOff += (NumSections + 1) * SectionHeaderEntrySize;
1782
1783 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i) {
1784 const MCSectionELF &Section = *Sections[i];
1785 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1786
1787 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1788
1789 // Remember the offset into the file for this section.
1790 SectionOffsetMap[&Section] = FileOff;
1791
1792 // Get the size of the section in the output file (including padding).
1793 FileOff += GetSectionFileSize(Layout, SD);
1794 }
1795
1796 // Write out the ELF header ...
Jack Carter1bd90ff2013-01-30 02:09:52 +00001797 WriteHeader(Asm, SectionHeaderOffset, NumSections + 1);
Rafael Espindola1557fd62011-03-20 18:44:20 +00001798
1799 // ... then the regular sections ...
1800 // + because of .shstrtab
1801 for (unsigned i = 0; i < NumRegularSections + 1; ++i)
1802 WriteDataSectionData(Asm, Layout, *Sections[i]);
1803
Benjamin Kramer084b9f42012-01-20 14:42:32 +00001804 uint64_t Padding = OffsetToAlignment(OS.tell(), NaturalAlignment);
Rafael Espindola1557fd62011-03-20 18:44:20 +00001805 WriteZeros(Padding);
1806
1807 // ... then the section header table ...
Rafael Espindola7fe7e052015-02-17 20:40:59 +00001808 writeSectionHeader(Asm, GroupMap, Layout, SectionIndexMap, RelMap,
Rafael Espindola1557fd62011-03-20 18:44:20 +00001809 SectionOffsetMap);
1810
Nick Lewycky4eb11432011-10-07 20:56:23 +00001811 // ... and then the remaining sections ...
Rafael Espindola1557fd62011-03-20 18:44:20 +00001812 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i)
1813 WriteDataSectionData(Asm, Layout, *Sections[i]);
1814}
1815
Eli Friedmand92d17b2011-03-03 07:24:36 +00001816bool
1817ELFObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
1818 const MCSymbolData &DataA,
1819 const MCFragment &FB,
1820 bool InSet,
1821 bool IsPCRel) const {
Rafael Espindolaf275ad82015-03-25 13:16:53 +00001822 if (::isWeak(DataA))
Eli Friedmand92d17b2011-03-03 07:24:36 +00001823 return false;
1824 return MCObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(
1825 Asm, DataA, FB,InSet, IsPCRel);
1826}
1827
Rafael Espindolaf275ad82015-03-25 13:16:53 +00001828bool ELFObjectWriter::isWeak(const MCSymbolData &SD) const {
1829 return ::isWeak(SD);
1830}
1831
Rafael Espindola6b5e56c2010-12-17 17:45:22 +00001832MCObjectWriter *llvm::createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1833 raw_ostream &OS,
Rafael Espindolafdaae0d2010-12-18 03:27:34 +00001834 bool IsLittleEndian) {
Rafael Espindola34a68af2011-12-22 03:24:43 +00001835 return new ELFObjectWriter(MOTW, OS, IsLittleEndian);
Rafael Espindolab264d332011-12-21 17:30:17 +00001836}