blob: 0bc17ef3543ee16fcd9f790b40de46bc097baa5a [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
Craig Topper34a61bc2014-03-08 07:02:02 +0000315 void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
Rafael Espindola7fe7e052015-02-17 20:40:59 +0000316 void writeSection(MCAssembler &Asm,
Rafael Espindola29abd972011-12-22 03:38:00 +0000317 const SectionIndexMapTy &SectionIndexMap,
Rafael Espindola7fe7e052015-02-17 20:40:59 +0000318 const RelMapTy &RelMap,
Rafael Espindola29abd972011-12-22 03:38:00 +0000319 uint32_t GroupSymbolIndex,
320 uint64_t Offset, uint64_t Size, uint64_t Alignment,
321 const MCSectionELF &Section);
322 };
323}
324
Rafael Espindola0ce09712014-03-25 22:43:53 +0000325FragmentWriter::FragmentWriter(bool IsLittleEndian)
326 : IsLittleEndian(IsLittleEndian) {}
327
328template <typename T> void FragmentWriter::write(MCDataFragment &F, T Val) {
329 if (IsLittleEndian)
330 Val = support::endian::byte_swap<T, support::little>(Val);
331 else
332 Val = support::endian::byte_swap<T, support::big>(Val);
333 const char *Start = (const char *)&Val;
334 F.getContents().append(Start, Start + sizeof(T));
335}
336
Rafael Espindola10be0832014-03-25 23:44:25 +0000337void SymbolTableWriter::createSymtabShndx() {
338 if (ShndxF)
339 return;
340
341 MCContext &Ctx = Asm.getContext();
342 const MCSectionELF *SymtabShndxSection =
Rafael Espindolaba31e272015-01-29 17:33:21 +0000343 Ctx.getELFSection(".symtab_shndxr", ELF::SHT_SYMTAB_SHNDX, 0, 4, "");
Rafael Espindola10be0832014-03-25 23:44:25 +0000344 MCSectionData *SymtabShndxSD =
345 &Asm.getOrCreateSectionData(*SymtabShndxSection);
346 SymtabShndxSD->setAlignment(4);
347 ShndxF = new MCDataFragment(SymtabShndxSD);
348 unsigned Index = SectionIndexMap.size() + 1;
349 SectionIndexMap[SymtabShndxSection] = Index;
350
351 for (unsigned I = 0; I < NumWritten; ++I)
352 write(*ShndxF, uint32_t(0));
353}
354
355template <typename T>
356void SymbolTableWriter::write(MCDataFragment &F, T Value) {
357 FWriter.write(F, Value);
358}
359
360SymbolTableWriter::SymbolTableWriter(MCAssembler &Asm, FragmentWriter &FWriter,
361 bool Is64Bit,
362 SectionIndexMapTy &SectionIndexMap,
363 MCDataFragment *SymtabF)
364 : Asm(Asm), FWriter(FWriter), Is64Bit(Is64Bit),
365 SectionIndexMap(SectionIndexMap), SymtabF(SymtabF), ShndxF(nullptr),
366 NumWritten(0) {}
367
368void SymbolTableWriter::writeSymbol(uint32_t name, uint8_t info, uint64_t value,
369 uint64_t size, uint8_t other,
370 uint32_t shndx, bool Reserved) {
371 bool LargeIndex = shndx >= ELF::SHN_LORESERVE && !Reserved;
372
373 if (LargeIndex)
374 createSymtabShndx();
375
376 if (ShndxF) {
377 if (LargeIndex)
378 write(*ShndxF, shndx);
379 else
380 write(*ShndxF, uint32_t(0));
381 }
382
383 uint16_t Index = LargeIndex ? uint16_t(ELF::SHN_XINDEX) : shndx;
384
385 raw_svector_ostream OS(SymtabF->getContents());
386
387 if (Is64Bit) {
388 write(*SymtabF, name); // st_name
389 write(*SymtabF, info); // st_info
390 write(*SymtabF, other); // st_other
391 write(*SymtabF, Index); // st_shndx
392 write(*SymtabF, value); // st_value
393 write(*SymtabF, size); // st_size
394 } else {
395 write(*SymtabF, name); // st_name
396 write(*SymtabF, uint32_t(value)); // st_value
397 write(*SymtabF, uint32_t(size)); // st_size
398 write(*SymtabF, info); // st_info
399 write(*SymtabF, other); // st_other
400 write(*SymtabF, Index); // st_shndx
401 }
402
403 ++NumWritten;
404}
405
Jan Sjödin30a52de2011-02-28 21:45:04 +0000406bool ELFObjectWriter::isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) {
407 const MCFixupKindInfo &FKI =
408 Asm.getBackend().getFixupKindInfo((MCFixupKind) Kind);
409
410 return FKI.Flags & MCFixupKindInfo::FKF_IsPCRel;
411}
412
413bool ELFObjectWriter::RelocNeedsGOT(MCSymbolRefExpr::VariantKind Variant) {
414 switch (Variant) {
415 default:
416 return false;
417 case MCSymbolRefExpr::VK_GOT:
418 case MCSymbolRefExpr::VK_PLT:
419 case MCSymbolRefExpr::VK_GOTPCREL:
Rafael Espindola940a0ee2011-06-05 01:20:06 +0000420 case MCSymbolRefExpr::VK_GOTOFF:
Jan Sjödin30a52de2011-02-28 21:45:04 +0000421 case MCSymbolRefExpr::VK_TPOFF:
422 case MCSymbolRefExpr::VK_TLSGD:
423 case MCSymbolRefExpr::VK_GOTTPOFF:
424 case MCSymbolRefExpr::VK_INDNTPOFF:
425 case MCSymbolRefExpr::VK_NTPOFF:
426 case MCSymbolRefExpr::VK_GOTNTPOFF:
427 case MCSymbolRefExpr::VK_TLSLDM:
428 case MCSymbolRefExpr::VK_DTPOFF:
429 case MCSymbolRefExpr::VK_TLSLD:
430 return true;
431 }
432}
433
Jason W Kim96f4c012010-11-15 16:18:39 +0000434ELFObjectWriter::~ELFObjectWriter()
435{}
436
Matt Fleming6c1ad482010-08-16 18:57:57 +0000437// Emit the ELF header.
Jack Carter1bd90ff2013-01-30 02:09:52 +0000438void ELFObjectWriter::WriteHeader(const MCAssembler &Asm,
439 uint64_t SectionDataSize,
Daniel Dunbarfe0c28f2010-11-13 07:33:40 +0000440 unsigned NumberOfSections) {
Matt Fleming6c1ad482010-08-16 18:57:57 +0000441 // ELF Header
442 // ----------
443 //
444 // Note
445 // ----
446 // emitWord method behaves differently for ELF32 and ELF64, writing
447 // 4 bytes in the former and 8 in the latter.
448
449 Write8(0x7f); // e_ident[EI_MAG0]
450 Write8('E'); // e_ident[EI_MAG1]
451 Write8('L'); // e_ident[EI_MAG2]
452 Write8('F'); // e_ident[EI_MAG3]
453
Rafael Espindolafdaae0d2010-12-18 03:27:34 +0000454 Write8(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
Matt Fleming6c1ad482010-08-16 18:57:57 +0000455
456 // e_ident[EI_DATA]
Daniel Dunbarfe0c28f2010-11-13 07:33:40 +0000457 Write8(isLittleEndian() ? ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000458
459 Write8(ELF::EV_CURRENT); // e_ident[EI_VERSION]
Roman Divacky3b727f52010-09-09 17:57:50 +0000460 // e_ident[EI_OSABI]
Rafael Espindola1ad40952011-12-21 17:00:36 +0000461 Write8(TargetObjectWriter->getOSABI());
Matt Fleming6c1ad482010-08-16 18:57:57 +0000462 Write8(0); // e_ident[EI_ABIVERSION]
463
464 WriteZeros(ELF::EI_NIDENT - ELF::EI_PAD);
465
466 Write16(ELF::ET_REL); // e_type
467
Rafael Espindolafdaae0d2010-12-18 03:27:34 +0000468 Write16(TargetObjectWriter->getEMachine()); // e_machine = target
Matt Fleming6c1ad482010-08-16 18:57:57 +0000469
470 Write32(ELF::EV_CURRENT); // e_version
471 WriteWord(0); // e_entry, no entry point in .o file
472 WriteWord(0); // e_phoff, no program header for .o
Rafael Espindolafdaae0d2010-12-18 03:27:34 +0000473 WriteWord(SectionDataSize + (is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
Benjamin Kramer896bd7e2010-08-17 17:02:29 +0000474 sizeof(ELF::Elf32_Ehdr))); // e_shoff = sec hdr table off in bytes
Matt Fleming6c1ad482010-08-16 18:57:57 +0000475
Jason W Kim4761fba2011-02-04 21:41:11 +0000476 // e_flags = whatever the target wants
Jack Carter1bd90ff2013-01-30 02:09:52 +0000477 Write32(Asm.getELFHeaderEFlags());
Matt Fleming6c1ad482010-08-16 18:57:57 +0000478
479 // e_ehsize = ELF header size
Rafael Espindolafdaae0d2010-12-18 03:27:34 +0000480 Write16(is64Bit() ? sizeof(ELF::Elf64_Ehdr) : sizeof(ELF::Elf32_Ehdr));
Matt Fleming6c1ad482010-08-16 18:57:57 +0000481
482 Write16(0); // e_phentsize = prog header entry size
483 Write16(0); // e_phnum = # prog header entries = 0
484
485 // e_shentsize = Section header entry size
Rafael Espindolafdaae0d2010-12-18 03:27:34 +0000486 Write16(is64Bit() ? sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr));
Matt Fleming6c1ad482010-08-16 18:57:57 +0000487
488 // e_shnum = # of section header ents
Rafael Espindola3fe87a12010-10-31 00:16:26 +0000489 if (NumberOfSections >= ELF::SHN_LORESERVE)
Nick Lewycky4eb11432011-10-07 20:56:23 +0000490 Write16(ELF::SHN_UNDEF);
Rafael Espindola3fe87a12010-10-31 00:16:26 +0000491 else
492 Write16(NumberOfSections);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000493
494 // e_shstrndx = Section # of '.shstrtab'
Nick Lewycky8b02d362011-10-07 20:58:24 +0000495 if (ShstrtabIndex >= ELF::SHN_LORESERVE)
Rafael Espindola3fe87a12010-10-31 00:16:26 +0000496 Write16(ELF::SHN_XINDEX);
497 else
498 Write16(ShstrtabIndex);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000499}
500
Rafael Espindolafee224f2014-04-30 21:51:13 +0000501uint64_t ELFObjectWriter::SymbolValue(MCSymbolData &Data,
Jan Sjödin30a52de2011-02-28 21:45:04 +0000502 const MCAsmLayout &Layout) {
Rafael Espindolafee224f2014-04-30 21:51:13 +0000503 if (Data.isCommon() && Data.isExternal())
504 return Data.getCommonAlignment();
Rafael Espindola9735f4f2010-09-27 21:23:02 +0000505
Rafael Espindolafee224f2014-04-30 21:51:13 +0000506 uint64_t Res;
507 if (!Layout.getSymbolOffset(&Data, Res))
Rafael Espindola553e5eb2014-04-30 16:59:35 +0000508 return 0;
509
Rafael Espindolafee224f2014-04-30 21:51:13 +0000510 if (Layout.getAssembler().isThumbFunc(&Data.getSymbol()))
Rafael Espindola66f96fe2014-03-21 22:00:29 +0000511 Res |= 1;
Rafael Espindola9735f4f2010-09-27 21:23:02 +0000512
Rafael Espindola66f96fe2014-03-21 22:00:29 +0000513 return Res;
Rafael Espindola9735f4f2010-09-27 21:23:02 +0000514}
515
Rafael Espindola93e3cf02010-12-07 00:27:36 +0000516void ELFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
517 const MCAsmLayout &Layout) {
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000518 // The presence of symbol versions causes undefined symbols and
519 // versions declared with @@@ to be renamed.
520
David Blaikie583a31c2014-04-18 18:24:25 +0000521 for (MCSymbolData &OriginalData : Asm.symbols()) {
522 const MCSymbol &Alias = OriginalData.getSymbol();
Rafael Espindola6cd76e62010-10-28 18:33:03 +0000523
Rafael Espindola6cd76e62010-10-28 18:33:03 +0000524 // Not an alias.
Rafael Espindola6efaf112014-04-28 17:05:36 +0000525 if (!Alias.isVariable())
Rafael Espindola6cd76e62010-10-28 18:33:03 +0000526 continue;
Rafael Espindola6efaf112014-04-28 17:05:36 +0000527 auto *Ref = dyn_cast<MCSymbolRefExpr>(Alias.getVariableValue());
528 if (!Ref)
529 continue;
530 const MCSymbol &Symbol = Ref->getSymbol();
531 MCSymbolData &SD = Asm.getSymbolData(Symbol);
Rafael Espindola6cd76e62010-10-28 18:33:03 +0000532
Benjamin Kramer14807272010-10-27 19:53:52 +0000533 StringRef AliasName = Alias.getName();
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000534 size_t Pos = AliasName.find('@');
535 if (Pos == StringRef::npos)
536 continue;
537
Rafael Espindola6cd76e62010-10-28 18:33:03 +0000538 // Aliases defined with .symvar copy the binding from the symbol they alias.
539 // This is the first place we are able to copy this information.
David Blaikie583a31c2014-04-18 18:24:25 +0000540 OriginalData.setExternal(SD.isExternal());
541 MCELF::SetBinding(OriginalData, MCELF::GetBinding(SD));
Rafael Espindola6cd76e62010-10-28 18:33:03 +0000542
Benjamin Kramer14807272010-10-27 19:53:52 +0000543 StringRef Rest = AliasName.substr(Pos);
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000544 if (!Symbol.isUndefined() && !Rest.startswith("@@@"))
545 continue;
546
Rafael Espindola26496e62010-10-27 17:56:18 +0000547 // FIXME: produce a better error message.
548 if (Symbol.isUndefined() && Rest.startswith("@@") &&
549 !Rest.startswith("@@@"))
550 report_fatal_error("A @@ version cannot be undefined");
551
Benjamin Kramer14807272010-10-27 19:53:52 +0000552 Renames.insert(std::make_pair(&Symbol, &Alias));
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000553 }
554}
555
Roman Divacky5a1c5492014-01-07 20:17:03 +0000556static uint8_t mergeTypeForSet(uint8_t origType, uint8_t newType) {
557 uint8_t Type = newType;
558
559 // Propagation rules:
560 // IFUNC > FUNC > OBJECT > NOTYPE
561 // TLS_OBJECT > OBJECT > NOTYPE
562 //
563 // dont let the new type degrade the old type
564 switch (origType) {
565 default:
566 break;
567 case ELF::STT_GNU_IFUNC:
568 if (Type == ELF::STT_FUNC || Type == ELF::STT_OBJECT ||
569 Type == ELF::STT_NOTYPE || Type == ELF::STT_TLS)
570 Type = ELF::STT_GNU_IFUNC;
571 break;
572 case ELF::STT_FUNC:
573 if (Type == ELF::STT_OBJECT || Type == ELF::STT_NOTYPE ||
574 Type == ELF::STT_TLS)
575 Type = ELF::STT_FUNC;
576 break;
577 case ELF::STT_OBJECT:
578 if (Type == ELF::STT_NOTYPE)
579 Type = ELF::STT_OBJECT;
580 break;
581 case ELF::STT_TLS:
582 if (Type == ELF::STT_OBJECT || Type == ELF::STT_NOTYPE ||
583 Type == ELF::STT_GNU_IFUNC || Type == ELF::STT_FUNC)
584 Type = ELF::STT_TLS;
585 break;
586 }
587
588 return Type;
589}
590
Rafael Espindola10be0832014-03-25 23:44:25 +0000591void ELFObjectWriter::WriteSymbol(SymbolTableWriter &Writer, ELFSymbolData &MSD,
Daniel Dunbarfe0c28f2010-11-13 07:33:40 +0000592 const MCAsmLayout &Layout) {
Rafael Espindola5f2d6a52010-10-06 21:02:29 +0000593 MCSymbolData &OrigData = *MSD.SymbolData;
David Blaikieb5956d22014-04-19 00:50:15 +0000594 assert((!OrigData.getFragment() ||
595 (&OrigData.getFragment()->getParent()->getSection() ==
596 &OrigData.getSymbol().getSection())) &&
597 "The symbol's section doesn't match the fragment's symbol");
Rafael Espindola2aeac7a2014-05-01 13:24:25 +0000598 const MCSymbol *Base = Layout.getBaseSymbol(OrigData.getSymbol());
Rafael Espindola85a84912014-03-26 00:16:43 +0000599
600 // This has to be in sync with when computeSymbolTable uses SHN_ABS or
601 // SHN_COMMON.
602 bool IsReserved = !Base || OrigData.isCommon();
Rafael Espindola3fe87a12010-10-31 00:16:26 +0000603
Jack Carter2f8d9d92013-02-19 21:57:35 +0000604 // Binding and Type share the same byte as upper and lower nibbles
Jan Sjödin30a52de2011-02-28 21:45:04 +0000605 uint8_t Binding = MCELF::GetBinding(OrigData);
Rafael Espindolaa6e3a592014-03-23 03:33:20 +0000606 uint8_t Type = MCELF::GetType(OrigData);
Rafael Espindolaa041ef12014-03-27 00:28:24 +0000607 MCSymbolData *BaseSD = nullptr;
Rafael Espindolaa6e3a592014-03-23 03:33:20 +0000608 if (Base) {
Rafael Espindolaa041ef12014-03-27 00:28:24 +0000609 BaseSD = &Layout.getAssembler().getSymbolData(*Base);
610 Type = mergeTypeForSet(Type, MCELF::GetType(*BaseSD));
Rafael Espindolaa6e3a592014-03-23 03:33:20 +0000611 }
Rafael Espindola5f2d6a52010-10-06 21:02:29 +0000612 uint8_t Info = (Binding << ELF_STB_Shift) | (Type << ELF_STT_Shift);
Jack Carter2f8d9d92013-02-19 21:57:35 +0000613
Joerg Sonnenbergerfc184732013-10-29 01:06:17 +0000614 // Other and Visibility share the same byte with Visibility using the lower
Jack Carter2f8d9d92013-02-19 21:57:35 +0000615 // 2 bits
616 uint8_t Visibility = MCELF::GetVisibility(OrigData);
Logan Chienee365952013-12-05 00:34:11 +0000617 uint8_t Other = MCELF::getOther(OrigData) << (ELF_STO_Shift - ELF_STV_Shift);
Jack Carter2f8d9d92013-02-19 21:57:35 +0000618 Other |= Visibility;
Rafael Espindola5f2d6a52010-10-06 21:02:29 +0000619
Rafael Espindola66f96fe2014-03-21 22:00:29 +0000620 uint64_t Value = SymbolValue(OrigData, Layout);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000621 uint64_t Size = 0;
Matt Fleming6c1ad482010-08-16 18:57:57 +0000622
Rafael Espindolaa041ef12014-03-27 00:28:24 +0000623 const MCExpr *ESize = OrigData.getSize();
624 if (!ESize && Base)
625 ESize = BaseSD->getSize();
Rafael Espindolaf0591c12010-09-21 00:24:38 +0000626
Rafael Espindola73c0ae72010-12-22 16:03:00 +0000627 if (ESize) {
628 int64_t Res;
629 if (!ESize->EvaluateAsAbsolute(Res, Layout))
630 report_fatal_error("Size expression must be absolute.");
631 Size = Res;
Matt Fleming6c1ad482010-08-16 18:57:57 +0000632 }
633
634 // Write out the symbol table entry
Rafael Espindola10be0832014-03-25 23:44:25 +0000635 Writer.writeSymbol(MSD.StringIndex, Info, Value, Size, Other,
636 MSD.SectionIndex, IsReserved);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000637}
638
Daniel Dunbarfe0c28f2010-11-13 07:33:40 +0000639void ELFObjectWriter::WriteSymbolTable(MCDataFragment *SymtabF,
Rafael Espindola10be0832014-03-25 23:44:25 +0000640 MCAssembler &Asm,
Daniel Dunbarfe0c28f2010-11-13 07:33:40 +0000641 const MCAsmLayout &Layout,
Rafael Espindola10be0832014-03-25 23:44:25 +0000642 SectionIndexMapTy &SectionIndexMap) {
Matt Fleming6c1ad482010-08-16 18:57:57 +0000643 // The string table must be emitted first because we need the index
644 // into the string table for all the symbol names.
Matt Fleming6c1ad482010-08-16 18:57:57 +0000645
646 // FIXME: Make sure the start of the symbol table is aligned.
647
Rafael Espindola10be0832014-03-25 23:44:25 +0000648 SymbolTableWriter Writer(Asm, FWriter, is64Bit(), SectionIndexMap, SymtabF);
649
Matt Fleming6c1ad482010-08-16 18:57:57 +0000650 // The first entry is the undefined symbol entry.
Rafael Espindola10be0832014-03-25 23:44:25 +0000651 Writer.writeSymbol(0, 0, 0, 0, 0, 0, false);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000652
Joerg Sonnenbergerfc184732013-10-29 01:06:17 +0000653 for (unsigned i = 0, e = FileSymbolData.size(); i != e; ++i) {
Rafael Espindola10be0832014-03-25 23:44:25 +0000654 Writer.writeSymbol(FileSymbolData[i], ELF::STT_FILE | ELF::STB_LOCAL, 0, 0,
655 ELF::STV_DEFAULT, ELF::SHN_ABS, true);
Joerg Sonnenbergerfc184732013-10-29 01:06:17 +0000656 }
657
Matt Fleming6c1ad482010-08-16 18:57:57 +0000658 // Write the symbol table entries.
Joerg Sonnenbergerfc184732013-10-29 01:06:17 +0000659 LastLocalSymbolIndex = FileSymbolData.size() + LocalSymbolData.size() + 1;
660
Matt Fleming6c1ad482010-08-16 18:57:57 +0000661 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i) {
662 ELFSymbolData &MSD = LocalSymbolData[i];
Rafael Espindola10be0832014-03-25 23:44:25 +0000663 WriteSymbol(Writer, MSD, Layout);
Matt Fleming6c1ad482010-08-16 18:57:57 +0000664 }
665
Matt Fleming6c1ad482010-08-16 18:57:57 +0000666 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i) {
667 ELFSymbolData &MSD = ExternalSymbolData[i];
668 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola83b2a332010-10-06 16:47:31 +0000669 assert(((Data.getFlags() & ELF_STB_Global) ||
670 (Data.getFlags() & ELF_STB_Weak)) &&
671 "External symbol requires STB_GLOBAL or STB_WEAK flag");
Rafael Espindola10be0832014-03-25 23:44:25 +0000672 WriteSymbol(Writer, MSD, Layout);
Jan Sjödin30a52de2011-02-28 21:45:04 +0000673 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming6c1ad482010-08-16 18:57:57 +0000674 LastLocalSymbolIndex++;
675 }
676
677 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i) {
678 ELFSymbolData &MSD = UndefinedSymbolData[i];
679 MCSymbolData &Data = *MSD.SymbolData;
Rafael Espindola10be0832014-03-25 23:44:25 +0000680 WriteSymbol(Writer, MSD, Layout);
Jan Sjödin30a52de2011-02-28 21:45:04 +0000681 if (MCELF::GetBinding(Data) == ELF::STB_LOCAL)
Matt Fleming6c1ad482010-08-16 18:57:57 +0000682 LastLocalSymbolIndex++;
683 }
684}
685
Rafael Espindola5904e122014-03-29 06:26:49 +0000686// It is always valid to create a relocation with a symbol. It is preferable
687// to use a relocation with a section if that is possible. Using the section
688// allows us to omit some local symbols from the symbol table.
Rafael Espindolab60c8292014-04-29 12:46:50 +0000689bool ELFObjectWriter::shouldRelocateWithSymbol(const MCAssembler &Asm,
690 const MCSymbolRefExpr *RefA,
Rafael Espindola5904e122014-03-29 06:26:49 +0000691 const MCSymbolData *SD,
692 uint64_t C,
693 unsigned Type) const {
694 // A PCRel relocation to an absolute value has no symbol (or section). We
695 // represent that with a relocation to a null section.
696 if (!RefA)
697 return false;
Rafael Espindola240028d2010-11-14 23:53:26 +0000698
Rafael Espindola5904e122014-03-29 06:26:49 +0000699 MCSymbolRefExpr::VariantKind Kind = RefA->getKind();
700 switch (Kind) {
701 default:
702 break;
703 // The .odp creation emits a relocation against the symbol ".TOC." which
704 // create a R_PPC64_TOC relocation. However the relocation symbol name
705 // in final object creation should be NULL, since the symbol does not
706 // really exist, it is just the reference to TOC base for the current
707 // object file. Since the symbol is undefined, returning false results
708 // in a relocation with a null section which is the desired result.
709 case MCSymbolRefExpr::VK_PPC_TOCBASE:
710 return false;
711
712 // These VariantKind cause the relocation to refer to something other than
713 // the symbol itself, like a linker generated table. Since the address of
714 // symbol is not relevant, we cannot replace the symbol with the
715 // section and patch the difference in the addend.
716 case MCSymbolRefExpr::VK_GOT:
717 case MCSymbolRefExpr::VK_PLT:
718 case MCSymbolRefExpr::VK_GOTPCREL:
719 case MCSymbolRefExpr::VK_Mips_GOT:
720 case MCSymbolRefExpr::VK_PPC_GOT_LO:
721 case MCSymbolRefExpr::VK_PPC_GOT_HI:
722 case MCSymbolRefExpr::VK_PPC_GOT_HA:
723 return true;
Rafael Espindola240028d2010-11-14 23:53:26 +0000724 }
Rafael Espindola240028d2010-11-14 23:53:26 +0000725
Rafael Espindola5904e122014-03-29 06:26:49 +0000726 // An undefined symbol is not in any section, so the relocation has to point
727 // to the symbol itself.
728 const MCSymbol &Sym = SD->getSymbol();
729 if (Sym.isUndefined())
730 return true;
731
732 unsigned Binding = MCELF::GetBinding(*SD);
733 switch(Binding) {
734 default:
735 llvm_unreachable("Invalid Binding");
736 case ELF::STB_LOCAL:
737 break;
738 case ELF::STB_WEAK:
739 // If the symbol is weak, it might be overridden by a symbol in another
740 // file. The relocation has to point to the symbol so that the linker
741 // can update it.
742 return true;
743 case ELF::STB_GLOBAL:
744 // Global ELF symbols can be preempted by the dynamic linker. The relocation
745 // has to point to the symbol for a reason analogous to the STB_WEAK case.
746 return true;
Rafael Espindola8c3039b2010-11-15 16:33:49 +0000747 }
Rafael Espindola75d65b92010-09-25 05:42:19 +0000748
Rafael Espindola5904e122014-03-29 06:26:49 +0000749 // If a relocation points to a mergeable section, we have to be careful.
750 // If the offset is zero, a relocation with the section will encode the
751 // same information. With a non-zero offset, the situation is different.
752 // For example, a relocation can point 42 bytes past the end of a string.
753 // If we change such a relocation to use the section, the linker would think
754 // that it pointed to another string and subtracting 42 at runtime will
755 // produce the wrong value.
756 auto &Sec = cast<MCSectionELF>(Sym.getSection());
757 unsigned Flags = Sec.getFlags();
758 if (Flags & ELF::SHF_MERGE) {
759 if (C != 0)
760 return true;
Rafael Espindolab1b49782014-04-02 12:15:20 +0000761
762 // It looks like gold has a bug (http://sourceware.org/PR16794) and can
763 // only handle section relocations to mergeable sections if using RELA.
764 if (!hasRelocationAddend())
765 return true;
Rafael Espindola9f75d5d2010-11-24 21:57:39 +0000766 }
767
Rafael Espindola5904e122014-03-29 06:26:49 +0000768 // Most TLS relocations use a got, so they need the symbol. Even those that
Rafael Espindola11527a12014-10-06 12:33:27 +0000769 // are just an offset (@tpoff), require a symbol in gold versions before
770 // 5efeedf61e4fe720fd3e9a08e6c91c10abb66d42 (2014-09-26) which fixed
771 // http://sourceware.org/PR16773.
Rafael Espindola5904e122014-03-29 06:26:49 +0000772 if (Flags & ELF::SHF_TLS)
773 return true;
Rafael Espindolad7565c32010-10-05 23:57:26 +0000774
Rafael Espindola9ef84412014-04-11 19:18:01 +0000775 // If the symbol is a thumb function the final relocation must set the lowest
776 // bit. With a symbol that is done by just having the symbol have that bit
777 // set, so we would lose the bit if we relocated with the section.
778 // FIXME: We could use the section but add the bit to the relocation value.
Rafael Espindolab60c8292014-04-29 12:46:50 +0000779 if (Asm.isThumbFunc(&Sym))
Rafael Espindola9ef84412014-04-11 19:18:01 +0000780 return true;
781
Ulrich Weigand46797c62014-07-20 23:15:06 +0000782 if (TargetObjectWriter->needsRelocateWithSymbol(*SD, Type))
Rafael Espindola5904e122014-03-29 06:26:49 +0000783 return true;
784 return false;
Rafael Espindola75d65b92010-09-25 05:42:19 +0000785}
786
Rafael Espindola3d082fa2014-05-03 19:57:04 +0000787static const MCSymbol *getWeakRef(const MCSymbolRefExpr &Ref) {
788 const MCSymbol &Sym = Ref.getSymbol();
789
790 if (Ref.getKind() == MCSymbolRefExpr::VK_WEAKREF)
791 return &Sym;
792
793 if (!Sym.isVariable())
794 return nullptr;
795
796 const MCExpr *Expr = Sym.getVariableValue();
797 const auto *Inner = dyn_cast<MCSymbolRefExpr>(Expr);
798 if (!Inner)
799 return nullptr;
800
801 if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF)
802 return &Inner->getSymbol();
803 return nullptr;
804}
805
Rafael Espindolac9e70682015-03-24 23:48:44 +0000806static bool isWeak(const MCSymbolData &D) {
807 return D.getFlags() & ELF_STB_Weak || MCELF::GetType(D) == ELF::STT_GNU_IFUNC;
808}
809
Rafael Espindola26585542015-01-19 21:11:14 +0000810void ELFObjectWriter::RecordRelocation(MCAssembler &Asm,
Jason W Kim495c2bb2010-12-06 21:57:34 +0000811 const MCAsmLayout &Layout,
812 const MCFragment *Fragment,
Rafael Espindola26585542015-01-19 21:11:14 +0000813 const MCFixup &Fixup, MCValue Target,
814 bool &IsPCRel, uint64_t &FixedValue) {
Rafael Espindola5904e122014-03-29 06:26:49 +0000815 const MCSectionData *FixupSection = Fragment->getParent();
816 uint64_t C = Target.getConstant();
817 uint64_t FixupOffset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
Jason W Kim495c2bb2010-12-06 21:57:34 +0000818
Rafael Espindola5904e122014-03-29 06:26:49 +0000819 if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
820 assert(RefB->getKind() == MCSymbolRefExpr::VK_None &&
821 "Should not have constructed this");
Jason W Kim495c2bb2010-12-06 21:57:34 +0000822
Rafael Espindola5904e122014-03-29 06:26:49 +0000823 // Let A, B and C being the components of Target and R be the location of
824 // the fixup. If the fixup is not pcrel, we want to compute (A - B + C).
825 // If it is pcrel, we want to compute (A - B + C - R).
Jason W Kim495c2bb2010-12-06 21:57:34 +0000826
Rafael Espindola5904e122014-03-29 06:26:49 +0000827 // In general, ELF has no relocations for -B. It can only represent (A + C)
828 // or (A + C - R). If B = R + K and the relocation is not pcrel, we can
829 // replace B to implement it: (A - R - K + C)
830 if (IsPCRel)
831 Asm.getContext().FatalError(
832 Fixup.getLoc(),
833 "No relocation available to represent this relative expression");
David Majnemera45a1762014-01-06 07:39:46 +0000834
Rafael Espindola5904e122014-03-29 06:26:49 +0000835 const MCSymbol &SymB = RefB->getSymbol();
Jason W Kim495c2bb2010-12-06 21:57:34 +0000836
Rafael Espindola5904e122014-03-29 06:26:49 +0000837 if (SymB.isUndefined())
838 Asm.getContext().FatalError(
839 Fixup.getLoc(),
840 Twine("symbol '") + SymB.getName() +
841 "' can not be undefined in a subtraction expression");
Jason W Kim495c2bb2010-12-06 21:57:34 +0000842
Rafael Espindola5904e122014-03-29 06:26:49 +0000843 assert(!SymB.isAbsolute() && "Should have been folded");
844 const MCSection &SecB = SymB.getSection();
845 if (&SecB != &FixupSection->getSection())
846 Asm.getContext().FatalError(
847 Fixup.getLoc(), "Cannot represent a difference across sections");
Jason W Kim495c2bb2010-12-06 21:57:34 +0000848
Rafael Espindola5904e122014-03-29 06:26:49 +0000849 const MCSymbolData &SymBD = Asm.getSymbolData(SymB);
Rafael Espindolac9e70682015-03-24 23:48:44 +0000850 if (isWeak(SymBD))
851 Asm.getContext().FatalError(
852 Fixup.getLoc(), "Cannot represent a subtraction with a weak symbol");
853
Rafael Espindola5904e122014-03-29 06:26:49 +0000854 uint64_t SymBOffset = Layout.getSymbolOffset(&SymBD);
855 uint64_t K = SymBOffset - FixupOffset;
856 IsPCRel = true;
857 C -= K;
Jason W Kim495c2bb2010-12-06 21:57:34 +0000858 }
859
Rafael Espindola5904e122014-03-29 06:26:49 +0000860 // We either rejected the fixup or folded B into C at this point.
861 const MCSymbolRefExpr *RefA = Target.getSymA();
862 const MCSymbol *SymA = RefA ? &RefA->getSymbol() : nullptr;
863 const MCSymbolData *SymAD = SymA ? &Asm.getSymbolData(*SymA) : nullptr;
864
Rafael Espindolac03f44c2014-03-27 20:49:35 +0000865 unsigned Type = GetRelocType(Target, Fixup, IsPCRel);
Rafael Espindolab60c8292014-04-29 12:46:50 +0000866 bool RelocateWithSymbol = shouldRelocateWithSymbol(Asm, RefA, SymAD, C, Type);
Rafael Espindola5904e122014-03-29 06:26:49 +0000867 if (!RelocateWithSymbol && SymA && !SymA->isUndefined())
868 C += Layout.getSymbolOffset(SymAD);
869
870 uint64_t Addend = 0;
871 if (hasRelocationAddend()) {
872 Addend = C;
873 C = 0;
874 }
875
876 FixedValue = C;
877
878 // FIXME: What is this!?!?
879 MCSymbolRefExpr::VariantKind Modifier =
880 RefA ? RefA->getKind() : MCSymbolRefExpr::VK_None;
Rafael Espindola9e252bf2011-12-21 14:26:29 +0000881 if (RelocNeedsGOT(Modifier))
882 NeedsGOT = true;
Jan Sjödin30a52de2011-02-28 21:45:04 +0000883
Rafael Espindola5904e122014-03-29 06:26:49 +0000884 if (!RelocateWithSymbol) {
885 const MCSection *SecA =
886 (SymA && !SymA->isUndefined()) ? &SymA->getSection() : nullptr;
Rafael Espindolab6613022014-10-17 01:48:58 +0000887 auto *ELFSec = cast_or_null<MCSectionELF>(SecA);
888 MCSymbol *SectionSymbol =
889 ELFSec ? Asm.getContext().getOrCreateSectionSymbol(*ELFSec)
890 : nullptr;
891 ELFRelocationEntry Rec(FixupOffset, SectionSymbol, Type, Addend);
Rafael Espindola5904e122014-03-29 06:26:49 +0000892 Relocations[FixupSection].push_back(Rec);
893 return;
894 }
Roman Divackydfbecd12011-08-04 19:08:19 +0000895
Rafael Espindola5904e122014-03-29 06:26:49 +0000896 if (SymA) {
897 if (const MCSymbol *R = Renames.lookup(SymA))
898 SymA = R;
Rafael Espindolad7facaf2011-08-04 13:05:26 +0000899
Rafael Espindola3d082fa2014-05-03 19:57:04 +0000900 if (const MCSymbol *WeakRef = getWeakRef(*RefA))
901 WeakrefUsedInReloc.insert(WeakRef);
Rafael Espindola5904e122014-03-29 06:26:49 +0000902 else
903 UsedInReloc.insert(SymA);
904 }
905 ELFRelocationEntry Rec(FixupOffset, SymA, Type, Addend);
906 Relocations[FixupSection].push_back(Rec);
907 return;
Jason W Kim495c2bb2010-12-06 21:57:34 +0000908}
909
910
Benjamin Kramer86511dc2010-08-23 21:19:37 +0000911uint64_t
Daniel Dunbarfe0c28f2010-11-13 07:33:40 +0000912ELFObjectWriter::getSymbolIndexInSymbolTable(const MCAssembler &Asm,
913 const MCSymbol *S) {
David Blaikie908f4d42014-04-24 16:59:40 +0000914 const MCSymbolData &SD = Asm.getSymbolData(*S);
Rafael Espindola0e3decf2010-11-14 03:12:24 +0000915 return SD.getIndex();
Matt Fleming6c1ad482010-08-16 18:57:57 +0000916}
917
Rafael Espindola3b5ee552014-04-28 13:39:57 +0000918bool ELFObjectWriter::isInSymtab(const MCAsmLayout &Layout,
919 const MCSymbolData &Data, bool Used,
920 bool Renamed) {
Rafael Espindola7fadc0e2014-03-20 02:12:01 +0000921 const MCSymbol &Symbol = Data.getSymbol();
922 if (Symbol.isVariable()) {
923 const MCExpr *Expr = Symbol.getVariableValue();
924 if (const MCSymbolRefExpr *Ref = dyn_cast<MCSymbolRefExpr>(Expr)) {
925 if (Ref->getKind() == MCSymbolRefExpr::VK_WEAKREF)
926 return false;
927 }
928 }
Rafael Espindola16145972010-11-01 14:28:48 +0000929
Rafael Espindolaee8d1512010-10-19 19:31:37 +0000930 if (Used)
931 return true;
932
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +0000933 if (Renamed)
934 return false;
935
Rafael Espindola45834a02010-10-29 23:09:31 +0000936 if (Symbol.getName() == "_GLOBAL_OFFSET_TABLE_")
937 return true;
938
Rafael Espindola3b5ee552014-04-28 13:39:57 +0000939 if (Symbol.isVariable()) {
Rafael Espindola2aeac7a2014-05-01 13:24:25 +0000940 const MCSymbol *Base = Layout.getBaseSymbol(Symbol);
Rafael Espindola3b5ee552014-04-28 13:39:57 +0000941 if (Base && Base->isUndefined())
942 return false;
943 }
Rafael Espindola9e18e962011-02-23 20:22:07 +0000944
Jan Sjödin30a52de2011-02-28 21:45:04 +0000945 bool IsGlobal = MCELF::GetBinding(Data) == ELF::STB_GLOBAL;
Rafael Espindola9e18e962011-02-23 20:22:07 +0000946 if (!Symbol.isVariable() && Symbol.isUndefined() && !IsGlobal)
Rafael Espindolaa5efd6a2010-10-27 14:44:52 +0000947 return false;
948
Rafael Espindolaee8d1512010-10-19 19:31:37 +0000949 if (Symbol.isTemporary())
Rafael Espindolab1d07892010-10-05 18:01:23 +0000950 return false;
951
952 return true;
953}
954
Rafael Espindola39f50422014-04-28 14:24:44 +0000955bool ELFObjectWriter::isLocal(const MCSymbolData &Data, bool isUsedInReloc) {
Rafael Espindolab1d07892010-10-05 18:01:23 +0000956 if (Data.isExternal())
957 return false;
958
959 const MCSymbol &Symbol = Data.getSymbol();
Rafael Espindola39f50422014-04-28 14:24:44 +0000960 if (Symbol.isDefined())
961 return true;
Rafael Espindola7d0ba342010-11-14 04:17:37 +0000962
Rafael Espindola39f50422014-04-28 14:24:44 +0000963 if (isUsedInReloc)
Rafael Espindolab1d07892010-10-05 18:01:23 +0000964 return false;
965
966 return true;
967}
968
Rafael Espindolaead85492015-02-17 20:31:13 +0000969void ELFObjectWriter::computeIndexMap(MCAssembler &Asm,
Rafael Espindola1557fd62011-03-20 18:44:20 +0000970 SectionIndexMapTy &SectionIndexMap,
Rafael Espindolaead85492015-02-17 20:31:13 +0000971 RelMapTy &RelMap) {
Rafael Espindolad6340032010-11-10 21:51:05 +0000972 unsigned Index = 1;
973 for (MCAssembler::iterator it = Asm.begin(),
974 ie = Asm.end(); it != ie; ++it) {
975 const MCSectionELF &Section =
976 static_cast<const MCSectionELF &>(it->getSection());
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000977 if (Section.getType() != ELF::SHT_GROUP)
978 continue;
979 SectionIndexMap[&Section] = Index++;
980 }
981
982 for (MCAssembler::iterator it = Asm.begin(),
983 ie = Asm.end(); it != ie; ++it) {
Rafael Espindolaead85492015-02-17 20:31:13 +0000984 const MCSectionData &SD = *it;
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000985 const MCSectionELF &Section =
Rafael Espindolaead85492015-02-17 20:31:13 +0000986 static_cast<const MCSectionELF &>(SD.getSection());
Rafael Espindola1557fd62011-03-20 18:44:20 +0000987 if (Section.getType() == ELF::SHT_GROUP ||
988 Section.getType() == ELF::SHT_REL ||
989 Section.getType() == ELF::SHT_RELA)
Rafael Espindolaa3e9a222010-11-11 18:13:52 +0000990 continue;
Rafael Espindolad6340032010-11-10 21:51:05 +0000991 SectionIndexMap[&Section] = Index++;
Rafael Espindolaead85492015-02-17 20:31:13 +0000992 if (MCSectionData *RelSD = createRelocationSection(Asm, SD)) {
993 const MCSectionELF *RelSection =
994 static_cast<const MCSectionELF *>(&RelSD->getSection());
Rafael Espindola7fe7e052015-02-17 20:40:59 +0000995 RelMap[RelSection] = &Section;
Rafael Espindola1557fd62011-03-20 18:44:20 +0000996 SectionIndexMap[RelSection] = Index++;
Rafael Espindolaead85492015-02-17 20:31:13 +0000997 }
Rafael Espindolad6340032010-11-10 21:51:05 +0000998 }
999}
1000
Rafael Espindola022bb762014-03-24 03:43:21 +00001001void
1002ELFObjectWriter::computeSymbolTable(MCAssembler &Asm, const MCAsmLayout &Layout,
1003 const SectionIndexMapTy &SectionIndexMap,
Benjamin Kramer04f9da82014-09-19 12:26:38 +00001004 const RevGroupMapTy &RevGroupMap,
Rafael Espindola022bb762014-03-24 03:43:21 +00001005 unsigned NumRegularSections) {
Rafael Espindolad03e81d2010-10-05 15:48:37 +00001006 // FIXME: Is this the correct place to do this?
Rafael Espindola940a0ee2011-06-05 01:20:06 +00001007 // FIXME: Why is an undefined reference to _GLOBAL_OFFSET_TABLE_ needed?
Rafael Espindolad03e81d2010-10-05 15:48:37 +00001008 if (NeedsGOT) {
Dmitri Gribenko226fea52013-01-13 16:01:15 +00001009 StringRef Name = "_GLOBAL_OFFSET_TABLE_";
Rafael Espindolad03e81d2010-10-05 15:48:37 +00001010 MCSymbol *Sym = Asm.getContext().GetOrCreateSymbol(Name);
1011 MCSymbolData &Data = Asm.getOrCreateSymbolData(*Sym);
1012 Data.setExternal(true);
Jan Sjödin30a52de2011-02-28 21:45:04 +00001013 MCELF::SetBinding(Data, ELF::STB_GLOBAL);
Rafael Espindolad03e81d2010-10-05 15:48:37 +00001014 }
1015
Rafael Espindolabee6e9f2010-10-14 16:34:44 +00001016 // Add the data for the symbols.
David Blaikie583a31c2014-04-18 18:24:25 +00001017 for (MCSymbolData &SD : Asm.symbols()) {
1018 const MCSymbol &Symbol = SD.getSymbol();
Matt Fleming6c1ad482010-08-16 18:57:57 +00001019
Rafael Espindola16145972010-11-01 14:28:48 +00001020 bool Used = UsedInReloc.count(&Symbol);
1021 bool WeakrefUsed = WeakrefUsedInReloc.count(&Symbol);
Rafael Espindola7d0ba342010-11-14 04:17:37 +00001022 bool isSignature = RevGroupMap.count(&Symbol);
1023
Rafael Espindola3b5ee552014-04-28 13:39:57 +00001024 if (!isInSymtab(Layout, SD,
Rafael Espindola7d0ba342010-11-14 04:17:37 +00001025 Used || WeakrefUsed || isSignature,
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +00001026 Renames.count(&Symbol)))
Matt Fleming6c1ad482010-08-16 18:57:57 +00001027 continue;
1028
Matt Fleming6c1ad482010-08-16 18:57:57 +00001029 ELFSymbolData MSD;
David Blaikie583a31c2014-04-18 18:24:25 +00001030 MSD.SymbolData = &SD;
Rafael Espindola2aeac7a2014-05-01 13:24:25 +00001031 const MCSymbol *BaseSymbol = Layout.getBaseSymbol(Symbol);
Matt Fleming6c1ad482010-08-16 18:57:57 +00001032
Rafael Espindola7d0ba342010-11-14 04:17:37 +00001033 // Undefined symbols are global, but this is the first place we
1034 // are able to set it.
Rafael Espindola39f50422014-04-28 14:24:44 +00001035 bool Local = isLocal(SD, Used);
David Blaikie583a31c2014-04-18 18:24:25 +00001036 if (!Local && MCELF::GetBinding(SD) == ELF::STB_LOCAL) {
Rafael Espindola022bb762014-03-24 03:43:21 +00001037 assert(BaseSymbol);
David Blaikie583a31c2014-04-18 18:24:25 +00001038 MCSymbolData &BaseData = Asm.getSymbolData(*BaseSymbol);
Jan Sjödin30a52de2011-02-28 21:45:04 +00001039 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
David Blaikie583a31c2014-04-18 18:24:25 +00001040 MCELF::SetBinding(BaseData, ELF::STB_GLOBAL);
Rafael Espindola7d0ba342010-11-14 04:17:37 +00001041 }
1042
Rafael Espindola022bb762014-03-24 03:43:21 +00001043 if (!BaseSymbol) {
1044 MSD.SectionIndex = ELF::SHN_ABS;
David Blaikie583a31c2014-04-18 18:24:25 +00001045 } else if (SD.isCommon()) {
Rafael Espindolabee6e9f2010-10-14 16:34:44 +00001046 assert(!Local);
Rafael Espindolaf0591c12010-09-21 00:24:38 +00001047 MSD.SectionIndex = ELF::SHN_COMMON;
Rafael Espindola022bb762014-03-24 03:43:21 +00001048 } else if (BaseSymbol->isUndefined()) {
Rafael Espindola7d0ba342010-11-14 04:17:37 +00001049 if (isSignature && !Used)
Benjamin Kramer04f9da82014-09-19 12:26:38 +00001050 MSD.SectionIndex = SectionIndexMap.lookup(RevGroupMap.lookup(&Symbol));
Rafael Espindola7d0ba342010-11-14 04:17:37 +00001051 else
1052 MSD.SectionIndex = ELF::SHN_UNDEF;
Rafael Espindola022bb762014-03-24 03:43:21 +00001053 if (!Used && WeakrefUsed)
David Blaikie583a31c2014-04-18 18:24:25 +00001054 MCELF::SetBinding(SD, ELF::STB_WEAK);
Matt Fleming6c1ad482010-08-16 18:57:57 +00001055 } else {
Rafael Espindolad6340032010-11-10 21:51:05 +00001056 const MCSectionELF &Section =
Rafael Espindola022bb762014-03-24 03:43:21 +00001057 static_cast<const MCSectionELF&>(BaseSymbol->getSection());
Rafael Espindolad6340032010-11-10 21:51:05 +00001058 MSD.SectionIndex = SectionIndexMap.lookup(&Section);
Matt Fleming6c1ad482010-08-16 18:57:57 +00001059 assert(MSD.SectionIndex && "Invalid section index!");
Rafael Espindolafbcf0db2010-10-15 15:39:06 +00001060 }
1061
Rafael Espindola5a67ed12015-01-22 14:20:45 +00001062 // The @@@ in symbol version is replaced with @ in undefined symbols and @@
1063 // in defined ones.
1064 //
1065 // FIXME: All name handling should be done before we get to the writer,
NAKAMURA Takumib01d86b2015-02-25 11:02:00 +00001066 // including dealing with GNU-style version suffixes. Fixing this isn't
Rafael Espindola5a67ed12015-01-22 14:20:45 +00001067 // trivial.
1068 //
1069 // We thus have to be careful to not perform the symbol version replacement
1070 // blindly:
1071 //
1072 // The ELF format is used on Windows by the MCJIT engine. Thus, on
1073 // Windows, the ELFObjectWriter can encounter symbols mangled using the MS
1074 // Visual Studio C++ name mangling scheme. Symbols mangled using the MSVC
1075 // C++ name mangling can legally have "@@@" as a sub-string. In that case,
1076 // the EFLObjectWriter should not interpret the "@@@" sub-string as
1077 // specifying GNU-style symbol versioning. The ELFObjectWriter therefore
1078 // checks for the MSVC C++ name mangling prefix which is either "?", "@?",
1079 // "__imp_?" or "__imp_@?".
1080 //
1081 // It would have been interesting to perform the MS mangling prefix check
1082 // only when the target triple is of the form *-pc-windows-elf. But, it
1083 // seems that this information is not easily accessible from the
1084 // ELFObjectWriter.
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +00001085 StringRef Name = Symbol.getName();
Rafael Espindola5a67ed12015-01-22 14:20:45 +00001086 if (!Name.startswith("?") && !Name.startswith("@?") &&
1087 !Name.startswith("__imp_?") && !Name.startswith("__imp_@?")) {
1088 // This symbol isn't following the MSVC C++ name mangling convention. We
1089 // can thus safely interpret the @@@ in symbol names as specifying symbol
1090 // versioning.
1091 SmallString<32> Buf;
1092 size_t Pos = Name.find("@@@");
1093 if (Pos != StringRef::npos) {
1094 Buf += Name.substr(0, Pos);
1095 unsigned Skip = MSD.SectionIndex == ELF::SHN_UNDEF ? 2 : 1;
1096 Buf += Name.substr(Pos + Skip);
1097 Name = Buf;
1098 }
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +00001099 }
Rafael Espindolab6613022014-10-17 01:48:58 +00001100
1101 // Sections have their own string table
1102 if (MCELF::GetType(SD) != ELF::STT_SECTION)
1103 MSD.Name = StrTabBuilder.add(Name);
Rafael Espindolaeb0c2c12010-10-27 15:18:17 +00001104
Rafael Espindolaa5efd6a2010-10-27 14:44:52 +00001105 if (MSD.SectionIndex == ELF::SHN_UNDEF)
1106 UndefinedSymbolData.push_back(MSD);
1107 else if (Local)
1108 LocalSymbolData.push_back(MSD);
1109 else
1110 ExternalSymbolData.push_back(MSD);
Matt Fleming6c1ad482010-08-16 18:57:57 +00001111 }
1112
Hans Wennborg83e6e1e2014-04-30 16:25:02 +00001113 for (auto i = Asm.file_names_begin(), e = Asm.file_names_end(); i != e; ++i)
1114 StrTabBuilder.add(*i);
1115
Hans Wennborgf26bfc12014-09-29 22:43:20 +00001116 StrTabBuilder.finalize(StringTableBuilder::ELF);
Hans Wennborg83e6e1e2014-04-30 16:25:02 +00001117
1118 for (auto i = Asm.file_names_begin(), e = Asm.file_names_end(); i != e; ++i)
1119 FileSymbolData.push_back(StrTabBuilder.getOffset(*i));
1120
Rafael Espindolab6613022014-10-17 01:48:58 +00001121 for (ELFSymbolData &MSD : LocalSymbolData)
1122 MSD.StringIndex = MCELF::GetType(*MSD.SymbolData) == ELF::STT_SECTION
1123 ? 0
1124 : StrTabBuilder.getOffset(MSD.Name);
1125 for (ELFSymbolData &MSD : ExternalSymbolData)
Hans Wennborg83e6e1e2014-04-30 16:25:02 +00001126 MSD.StringIndex = StrTabBuilder.getOffset(MSD.Name);
1127 for (ELFSymbolData& MSD : UndefinedSymbolData)
1128 MSD.StringIndex = StrTabBuilder.getOffset(MSD.Name);
1129
Matt Fleming6c1ad482010-08-16 18:57:57 +00001130 // Symbols are required to be in lexicographic order.
1131 array_pod_sort(LocalSymbolData.begin(), LocalSymbolData.end());
1132 array_pod_sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
1133 array_pod_sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
1134
1135 // Set the symbol indices. Local symbols must come before all other
1136 // symbols with non-local bindings.
Joerg Sonnenbergerfc184732013-10-29 01:06:17 +00001137 unsigned Index = FileSymbolData.size() + 1;
Matt Fleming6c1ad482010-08-16 18:57:57 +00001138 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
1139 LocalSymbolData[i].SymbolData->setIndex(Index++);
Rafael Espindola0e3decf2010-11-14 03:12:24 +00001140
Matt Fleming6c1ad482010-08-16 18:57:57 +00001141 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
1142 ExternalSymbolData[i].SymbolData->setIndex(Index++);
1143 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
1144 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
1145}
1146
Rafael Espindolaead85492015-02-17 20:31:13 +00001147MCSectionData *
1148ELFObjectWriter::createRelocationSection(MCAssembler &Asm,
1149 const MCSectionData &SD) {
1150 if (Relocations[&SD].empty())
1151 return nullptr;
Rafael Espindola1557fd62011-03-20 18:44:20 +00001152
Rafael Espindolaead85492015-02-17 20:31:13 +00001153 MCContext &Ctx = Asm.getContext();
1154 const MCSectionELF &Section =
1155 static_cast<const MCSectionELF &>(SD.getSection());
Matt Fleming6c1ad482010-08-16 18:57:57 +00001156
Rafael Espindolaead85492015-02-17 20:31:13 +00001157 const StringRef SectionName = Section.getSectionName();
1158 std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
1159 RelaSectionName += SectionName;
Benjamin Kramerfd054152010-08-17 17:56:13 +00001160
Rafael Espindolaead85492015-02-17 20:31:13 +00001161 unsigned EntrySize;
1162 if (hasRelocationAddend())
1163 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
1164 else
1165 EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
Matt Fleming6c1ad482010-08-16 18:57:57 +00001166
Rafael Espindolaead85492015-02-17 20:31:13 +00001167 unsigned Flags = 0;
1168 StringRef Group = "";
1169 if (Section.getFlags() & ELF::SHF_GROUP) {
1170 Flags = ELF::SHF_GROUP;
1171 Group = Section.getGroup()->getName();
Rafael Espindola1557fd62011-03-20 18:44:20 +00001172 }
Rafael Espindolaead85492015-02-17 20:31:13 +00001173
1174 const MCSectionELF *RelaSection = Ctx.getELFSection(
1175 RelaSectionName, hasRelocationAddend() ? ELF::SHT_RELA : ELF::SHT_REL,
Rafael Espindola68fa2492015-02-17 20:48:01 +00001176 Flags, EntrySize, Group, true);
Rafael Espindolaead85492015-02-17 20:31:13 +00001177 return &Asm.getOrCreateSectionData(*RelaSection);
Rafael Espindola1557fd62011-03-20 18:44:20 +00001178}
Matt Fleming6c1ad482010-08-16 18:57:57 +00001179
David Blaikiee06e8012014-04-11 22:11:50 +00001180static SmallVector<char, 128>
1181getUncompressedData(MCAsmLayout &Layout,
1182 MCSectionData::FragmentListType &Fragments) {
David Blaikie8019bf82014-04-10 21:53:53 +00001183 SmallVector<char, 128> UncompressedData;
1184 for (const MCFragment &F : Fragments) {
1185 const SmallVectorImpl<char> *Contents;
1186 switch (F.getKind()) {
1187 case MCFragment::FT_Data:
1188 Contents = &cast<MCDataFragment>(F).getContents();
1189 break;
1190 case MCFragment::FT_Dwarf:
1191 Contents = &cast<MCDwarfLineAddrFragment>(F).getContents();
1192 break;
1193 case MCFragment::FT_DwarfFrame:
1194 Contents = &cast<MCDwarfCallFrameFragment>(F).getContents();
1195 break;
1196 default:
1197 llvm_unreachable(
1198 "Not expecting any other fragment types in a debug_* section");
1199 }
1200 UncompressedData.append(Contents->begin(), Contents->end());
1201 }
1202 return UncompressedData;
1203}
1204
1205// Include the debug info compression header:
1206// "ZLIB" followed by 8 bytes representing the uncompressed size of the section,
1207// useful for consumers to preallocate a buffer to decompress into.
David Blaikie76d3a3c2014-04-18 21:52:26 +00001208static bool
David Blaikiee06e8012014-04-11 22:11:50 +00001209prependCompressionHeader(uint64_t Size,
1210 SmallVectorImpl<char> &CompressedContents) {
Benjamin Kramer7149aab2015-03-01 18:09:56 +00001211 const StringRef Magic = "ZLIB";
David Blaikie76d3a3c2014-04-18 21:52:26 +00001212 if (Size <= Magic.size() + sizeof(Size) + CompressedContents.size())
1213 return false;
David Blaikie8019bf82014-04-10 21:53:53 +00001214 if (sys::IsLittleEndianHost)
Artyom Skrobov9aea8432014-06-14 13:18:07 +00001215 sys::swapByteOrder(Size);
David Blaikie8019bf82014-04-10 21:53:53 +00001216 CompressedContents.insert(CompressedContents.begin(),
1217 Magic.size() + sizeof(Size), 0);
1218 std::copy(Magic.begin(), Magic.end(), CompressedContents.begin());
1219 std::copy(reinterpret_cast<char *>(&Size),
1220 reinterpret_cast<char *>(&Size + 1),
1221 CompressedContents.begin() + Magic.size());
David Blaikie76d3a3c2014-04-18 21:52:26 +00001222 return true;
David Blaikie8019bf82014-04-10 21:53:53 +00001223}
1224
1225// Return a single fragment containing the compressed contents of the whole
1226// section. Null if the section was not compressed for any reason.
David Blaikiee06e8012014-04-11 22:11:50 +00001227static std::unique_ptr<MCDataFragment>
1228getCompressedFragment(MCAsmLayout &Layout,
1229 MCSectionData::FragmentListType &Fragments) {
David Blaikie8019bf82014-04-10 21:53:53 +00001230 std::unique_ptr<MCDataFragment> CompressedFragment(new MCDataFragment());
1231
1232 // Gather the uncompressed data from all the fragments, recording the
1233 // alignment fragment, if seen, and any fixups.
1234 SmallVector<char, 128> UncompressedData =
1235 getUncompressedData(Layout, Fragments);
1236
1237 SmallVectorImpl<char> &CompressedContents = CompressedFragment->getContents();
1238
1239 zlib::Status Success = zlib::compress(
1240 StringRef(UncompressedData.data(), UncompressedData.size()),
1241 CompressedContents);
1242 if (Success != zlib::StatusOK)
1243 return nullptr;
1244
David Blaikie76d3a3c2014-04-18 21:52:26 +00001245 if (!prependCompressionHeader(UncompressedData.size(), CompressedContents))
1246 return nullptr;
David Blaikie8019bf82014-04-10 21:53:53 +00001247
1248 return CompressedFragment;
1249}
1250
David Blaikie39fa6a22014-04-25 00:48:01 +00001251typedef DenseMap<const MCSectionData *, std::vector<MCSymbolData *>>
1252DefiningSymbolMap;
1253
1254static void UpdateSymbols(const MCAsmLayout &Layout,
1255 const std::vector<MCSymbolData *> &Symbols,
1256 MCFragment &NewFragment) {
1257 for (MCSymbolData *Sym : Symbols) {
1258 Sym->setOffset(Sym->getOffset() +
1259 Layout.getFragmentOffset(Sym->getFragment()));
1260 Sym->setFragment(&NewFragment);
David Blaikiec029ab42014-04-18 21:24:12 +00001261 }
1262}
1263
David Blaikie8019bf82014-04-10 21:53:53 +00001264static void CompressDebugSection(MCAssembler &Asm, MCAsmLayout &Layout,
David Blaikie39fa6a22014-04-25 00:48:01 +00001265 const DefiningSymbolMap &DefiningSymbols,
David Blaikie8019bf82014-04-10 21:53:53 +00001266 const MCSectionELF &Section,
1267 MCSectionData &SD) {
1268 StringRef SectionName = Section.getSectionName();
1269 MCSectionData::FragmentListType &Fragments = SD.getFragmentList();
1270
1271 std::unique_ptr<MCDataFragment> CompressedFragment =
1272 getCompressedFragment(Layout, Fragments);
1273
1274 // Leave the section as-is if the fragments could not be compressed.
1275 if (!CompressedFragment)
1276 return;
1277
David Blaikiec029ab42014-04-18 21:24:12 +00001278 // Update the fragment+offsets of any symbols referring to fragments in this
1279 // section to refer to the new fragment.
David Blaikie39fa6a22014-04-25 00:48:01 +00001280 auto I = DefiningSymbols.find(&SD);
1281 if (I != DefiningSymbols.end())
1282 UpdateSymbols(Layout, I->second, *CompressedFragment);
David Blaikiec029ab42014-04-18 21:24:12 +00001283
David Blaikie8019bf82014-04-10 21:53:53 +00001284 // Invalidate the layout for the whole section since it will have new and
1285 // different fragments now.
1286 Layout.invalidateFragmentsFrom(&Fragments.front());
1287 Fragments.clear();
1288
1289 // Complete the initialization of the new fragment
1290 CompressedFragment->setParent(&SD);
1291 CompressedFragment->setLayoutOrder(0);
1292 Fragments.push_back(CompressedFragment.release());
1293
1294 // Rename from .debug_* to .zdebug_*
1295 Asm.getContext().renameELFSection(&Section,
1296 (".z" + SectionName.drop_front(1)).str());
1297}
1298
1299void ELFObjectWriter::CompressDebugSections(MCAssembler &Asm,
1300 MCAsmLayout &Layout) {
1301 if (!Asm.getContext().getAsmInfo()->compressDebugSections())
1302 return;
1303
David Blaikie39fa6a22014-04-25 00:48:01 +00001304 DefiningSymbolMap DefiningSymbols;
1305
1306 for (MCSymbolData &SD : Asm.symbols())
1307 if (MCFragment *F = SD.getFragment())
1308 DefiningSymbols[F->getParent()].push_back(&SD);
1309
David Blaikie8019bf82014-04-10 21:53:53 +00001310 for (MCSectionData &SD : Asm) {
David Blaikiee06e8012014-04-11 22:11:50 +00001311 const MCSectionELF &Section =
1312 static_cast<const MCSectionELF &>(SD.getSection());
David Blaikie8019bf82014-04-10 21:53:53 +00001313 StringRef SectionName = Section.getSectionName();
1314
1315 // Compressing debug_frame requires handling alignment fragments which is
1316 // more work (possibly generalizing MCAssembler.cpp:writeFragment to allow
1317 // for writing to arbitrary buffers) for little benefit.
1318 if (!SectionName.startswith(".debug_") || SectionName == ".debug_frame")
1319 continue;
1320
David Blaikie39fa6a22014-04-25 00:48:01 +00001321 CompressDebugSection(Asm, Layout, DefiningSymbols, Section, SD);
David Blaikie8019bf82014-04-10 21:53:53 +00001322 }
1323}
1324
Rafael Espindola1557fd62011-03-20 18:44:20 +00001325void ELFObjectWriter::WriteRelocations(MCAssembler &Asm, MCAsmLayout &Layout,
1326 const RelMapTy &RelMap) {
Rafael Espindola7fe7e052015-02-17 20:40:59 +00001327 for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it) {
1328 MCSectionData &RelSD = *it;
1329 const MCSectionELF &RelSection =
1330 static_cast<const MCSectionELF &>(RelSD.getSection());
Rafael Espindola1557fd62011-03-20 18:44:20 +00001331
Rafael Espindola7fe7e052015-02-17 20:40:59 +00001332 unsigned Type = RelSection.getType();
1333 if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
Rafael Espindola1557fd62011-03-20 18:44:20 +00001334 continue;
Matt Fleming6c1ad482010-08-16 18:57:57 +00001335
Rafael Espindola7fe7e052015-02-17 20:40:59 +00001336 const MCSectionELF *Section = RelMap.lookup(&RelSection);
1337 MCSectionData &SD = Asm.getOrCreateSectionData(*Section);
1338 RelSD.setAlignment(is64Bit() ? 8 : 4);
1339
1340 MCDataFragment *F = new MCDataFragment(&RelSD);
1341 WriteRelocationsFragment(Asm, F, &SD);
Matt Fleming6c1ad482010-08-16 18:57:57 +00001342 }
1343}
1344
Daniel Dunbarfe0c28f2010-11-13 07:33:40 +00001345void ELFObjectWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type,
1346 uint64_t Flags, uint64_t Address,
1347 uint64_t Offset, uint64_t Size,
1348 uint32_t Link, uint32_t Info,
1349 uint64_t Alignment,
1350 uint64_t EntrySize) {
Matt Fleming6c1ad482010-08-16 18:57:57 +00001351 Write32(Name); // sh_name: index into string table
1352 Write32(Type); // sh_type
1353 WriteWord(Flags); // sh_flags
1354 WriteWord(Address); // sh_addr
1355 WriteWord(Offset); // sh_offset
1356 WriteWord(Size); // sh_size
1357 Write32(Link); // sh_link
1358 Write32(Info); // sh_info
1359 WriteWord(Alignment); // sh_addralign
1360 WriteWord(EntrySize); // sh_entsize
1361}
1362
Rafael Espindola5904e122014-03-29 06:26:49 +00001363// ELF doesn't require relocations to be in any order. We sort by the r_offset,
1364// just to match gnu as for easier comparison. The use type is an arbitrary way
1365// of making the sort deterministic.
1366static int cmpRel(const ELFRelocationEntry *AP, const ELFRelocationEntry *BP) {
1367 const ELFRelocationEntry &A = *AP;
1368 const ELFRelocationEntry &B = *BP;
1369 if (A.Offset != B.Offset)
1370 return B.Offset - A.Offset;
1371 if (B.Type != A.Type)
1372 return A.Type - B.Type;
Daniel Sanders60f1db02015-03-13 12:45:09 +00001373 //llvm_unreachable("ELFRelocs might be unstable!");
1374 return 0;
Rafael Espindola5904e122014-03-29 06:26:49 +00001375}
1376
1377static void sortRelocs(const MCAssembler &Asm,
1378 std::vector<ELFRelocationEntry> &Relocs) {
1379 array_pod_sort(Relocs.begin(), Relocs.end(), cmpRel);
1380}
1381
Daniel Dunbarfe0c28f2010-11-13 07:33:40 +00001382void ELFObjectWriter::WriteRelocationsFragment(const MCAssembler &Asm,
1383 MCDataFragment *F,
1384 const MCSectionData *SD) {
Matt Fleming6c1ad482010-08-16 18:57:57 +00001385 std::vector<ELFRelocationEntry> &Relocs = Relocations[SD];
Akira Hatanaka64ad2cf2012-03-23 23:06:45 +00001386
Rafael Espindola5904e122014-03-29 06:26:49 +00001387 sortRelocs(Asm, Relocs);
Matt Fleming6c1ad482010-08-16 18:57:57 +00001388
1389 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
Rafael Espindola5904e122014-03-29 06:26:49 +00001390 const ELFRelocationEntry &Entry = Relocs[e - i - 1];
Rafael Espindolab6613022014-10-17 01:48:58 +00001391 unsigned Index =
1392 Entry.Symbol ? getSymbolIndexInSymbolTable(Asm, Entry.Symbol) : 0;
Rafael Espindola5904e122014-03-29 06:26:49 +00001393
Rafael Espindolafdaae0d2010-12-18 03:27:34 +00001394 if (is64Bit()) {
Rafael Espindola5904e122014-03-29 06:26:49 +00001395 write(*F, Entry.Offset);
Jack Carter8ad0c272012-06-27 22:28:30 +00001396 if (TargetObjectWriter->isN64()) {
Rafael Espindola5904e122014-03-29 06:26:49 +00001397 write(*F, uint32_t(Index));
Benjamin Kramer6c3c3492010-09-09 18:01:29 +00001398
Rafael Espindola5904e122014-03-29 06:26:49 +00001399 write(*F, TargetObjectWriter->getRSsym(Entry.Type));
1400 write(*F, TargetObjectWriter->getRType3(Entry.Type));
1401 write(*F, TargetObjectWriter->getRType2(Entry.Type));
1402 write(*F, TargetObjectWriter->getRType(Entry.Type));
1403 } else {
Jack Carter8ad0c272012-06-27 22:28:30 +00001404 struct ELF::Elf64_Rela ERE64;
Rafael Espindola5904e122014-03-29 06:26:49 +00001405 ERE64.setSymbolAndType(Index, Entry.Type);
Rafael Espindola0ce09712014-03-25 22:43:53 +00001406 write(*F, ERE64.r_info);
Jack Carter8ad0c272012-06-27 22:28:30 +00001407 }
Rafael Espindolafdaae0d2010-12-18 03:27:34 +00001408 if (hasRelocationAddend())
Rafael Espindola5904e122014-03-29 06:26:49 +00001409 write(*F, Entry.Addend);
Benjamin Kramer6c3c3492010-09-09 18:01:29 +00001410 } else {
Rafael Espindola5904e122014-03-29 06:26:49 +00001411 write(*F, uint32_t(Entry.Offset));
Benjamin Kramer6c3c3492010-09-09 18:01:29 +00001412
Rafael Espindolabce26a12010-10-05 15:11:03 +00001413 struct ELF::Elf32_Rela ERE32;
Rafael Espindola5904e122014-03-29 06:26:49 +00001414 ERE32.setSymbolAndType(Index, Entry.Type);
Rafael Espindola0ce09712014-03-25 22:43:53 +00001415 write(*F, ERE32.r_info);
Benjamin Kramer6c3c3492010-09-09 18:01:29 +00001416
Rafael Espindolafdaae0d2010-12-18 03:27:34 +00001417 if (hasRelocationAddend())
Rafael Espindola5904e122014-03-29 06:26:49 +00001418 write(*F, uint32_t(Entry.Addend));
Benjamin Kramer6c3c3492010-09-09 18:01:29 +00001419 }
Matt Fleming6c1ad482010-08-16 18:57:57 +00001420 }
1421}
1422
Rafael Espindolaef6baea2015-02-11 23:11:18 +00001423void ELFObjectWriter::CreateMetadataSections(
1424 MCAssembler &Asm, MCAsmLayout &Layout, SectionIndexMapTy &SectionIndexMap) {
Matt Fleming6c1ad482010-08-16 18:57:57 +00001425 MCContext &Ctx = Asm.getContext();
1426 MCDataFragment *F;
1427
Rafael Espindolafdaae0d2010-12-18 03:27:34 +00001428 unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
Matt Fleming6c1ad482010-08-16 18:57:57 +00001429
Rafael Espindola0e527b72010-09-22 19:04:41 +00001430 // We construct .shstrtab, .symtab and .strtab in this order to match gnu as.
Rafael Espindola36ef57d2010-11-10 19:05:07 +00001431 const MCSectionELF *ShstrtabSection =
Rafael Espindolaba31e272015-01-29 17:33:21 +00001432 Ctx.getELFSection(".shstrtab", ELF::SHT_STRTAB, 0);
Rafael Espindola44bf2662010-09-16 19:46:31 +00001433 MCSectionData &ShstrtabSD = Asm.getOrCreateSectionData(*ShstrtabSection);
1434 ShstrtabSD.setAlignment(1);
Rafael Espindolafbd0ddf2015-02-11 22:41:26 +00001435 ShstrtabIndex = SectionIndexMap.size() + 1;
1436 SectionIndexMap[ShstrtabSection] = ShstrtabIndex;
Rafael Espindola44bf2662010-09-16 19:46:31 +00001437
Rafael Espindola36ef57d2010-11-10 19:05:07 +00001438 const MCSectionELF *SymtabSection =
Rafael Espindola3fe87a12010-10-31 00:16:26 +00001439 Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0,
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001440 EntrySize, "");
Matt Fleming6c1ad482010-08-16 18:57:57 +00001441 MCSectionData &SymtabSD = Asm.getOrCreateSectionData(*SymtabSection);
Rafael Espindolafdaae0d2010-12-18 03:27:34 +00001442 SymtabSD.setAlignment(is64Bit() ? 8 : 4);
Rafael Espindolafbd0ddf2015-02-11 22:41:26 +00001443 SymbolTableIndex = SectionIndexMap.size() + 1;
1444 SectionIndexMap[SymtabSection] = SymbolTableIndex;
Rafael Espindola3fe87a12010-10-31 00:16:26 +00001445
Rafael Espindola1557fd62011-03-20 18:44:20 +00001446 const MCSectionELF *StrtabSection;
Rafael Espindolaba31e272015-01-29 17:33:21 +00001447 StrtabSection = Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0);
Matt Fleming6c1ad482010-08-16 18:57:57 +00001448 MCSectionData &StrtabSD = Asm.getOrCreateSectionData(*StrtabSection);
1449 StrtabSD.setAlignment(1);
Rafael Espindolafbd0ddf2015-02-11 22:41:26 +00001450 StringTableIndex = SectionIndexMap.size() + 1;
1451 SectionIndexMap[StrtabSection] = StringTableIndex;
Rafael Espindola44bf2662010-09-16 19:46:31 +00001452
1453 // Symbol table
1454 F = new MCDataFragment(&SymtabSD);
Rafael Espindola10be0832014-03-25 23:44:25 +00001455 WriteSymbolTable(F, Asm, Layout, SectionIndexMap);
Rafael Espindola44bf2662010-09-16 19:46:31 +00001456
Matt Fleming6c1ad482010-08-16 18:57:57 +00001457 F = new MCDataFragment(&StrtabSD);
Hans Wennborg83e6e1e2014-04-30 16:25:02 +00001458 F->getContents().append(StrTabBuilder.data().begin(),
1459 StrTabBuilder.data().end());
Matt Fleming6c1ad482010-08-16 18:57:57 +00001460
Matt Fleming6c1ad482010-08-16 18:57:57 +00001461 F = new MCDataFragment(&ShstrtabSD);
1462
Hans Wennborg83e6e1e2014-04-30 16:25:02 +00001463 // Section header string table.
1464 for (auto it = Asm.begin(), ie = Asm.end(); it != ie; ++it) {
Rafael Espindolab80875e2011-04-07 23:21:52 +00001465 const MCSectionELF &Section =
1466 static_cast<const MCSectionELF&>(it->getSection());
Hans Wennborg83e6e1e2014-04-30 16:25:02 +00001467 ShStrTabBuilder.add(Section.getSectionName());
Rafael Espindolab80875e2011-04-07 23:21:52 +00001468 }
Hans Wennborgf26bfc12014-09-29 22:43:20 +00001469 ShStrTabBuilder.finalize(StringTableBuilder::ELF);
Hans Wennborg83e6e1e2014-04-30 16:25:02 +00001470 F->getContents().append(ShStrTabBuilder.data().begin(),
1471 ShStrTabBuilder.data().end());
Rafael Espindola2ebaee92010-09-30 02:22:20 +00001472}
1473
Rafael Espindolaead85492015-02-17 20:31:13 +00001474void ELFObjectWriter::createIndexedSections(MCAssembler &Asm,
Rafael Espindolab3eca9b2011-01-23 17:55:27 +00001475 MCAsmLayout &Layout,
1476 GroupMapTy &GroupMap,
Rafael Espindola1557fd62011-03-20 18:44:20 +00001477 RevGroupMapTy &RevGroupMap,
1478 SectionIndexMapTy &SectionIndexMap,
Rafael Espindolaead85492015-02-17 20:31:13 +00001479 RelMapTy &RelMap) {
Rafael Espindolab3eca9b2011-01-23 17:55:27 +00001480 MCContext &Ctx = Asm.getContext();
Rafael Espindolab3eca9b2011-01-23 17:55:27 +00001481
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001482 // Build the groups
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001483 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
1484 it != ie; ++it) {
1485 const MCSectionELF &Section =
1486 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola0e7e34e2011-01-23 04:43:11 +00001487 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001488 continue;
1489
1490 const MCSymbol *SignatureSymbol = Section.getGroup();
1491 Asm.getOrCreateSymbolData(*SignatureSymbol);
Rafael Espindola7d0ba342010-11-14 04:17:37 +00001492 const MCSectionELF *&Group = RevGroupMap[SignatureSymbol];
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001493 if (!Group) {
Rafael Espindolab3eca9b2011-01-23 17:55:27 +00001494 Group = Ctx.CreateELFGroupSection();
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001495 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1496 Data.setAlignment(4);
1497 MCDataFragment *F = new MCDataFragment(&Data);
Rafael Espindola0ce09712014-03-25 22:43:53 +00001498 write(*F, uint32_t(ELF::GRP_COMDAT));
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001499 }
1500 GroupMap[Group] = SignatureSymbol;
1501 }
1502
Rafael Espindolaead85492015-02-17 20:31:13 +00001503 computeIndexMap(Asm, SectionIndexMap, RelMap);
Rafael Espindola1557fd62011-03-20 18:44:20 +00001504
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001505 // Add sections to the groups
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001506 for (MCAssembler::const_iterator it = Asm.begin(), ie = Asm.end();
Rafael Espindola1557fd62011-03-20 18:44:20 +00001507 it != ie; ++it) {
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001508 const MCSectionELF &Section =
1509 static_cast<const MCSectionELF&>(it->getSection());
Rafael Espindola0e7e34e2011-01-23 04:43:11 +00001510 if (!(Section.getFlags() & ELF::SHF_GROUP))
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001511 continue;
Rafael Espindola7d0ba342010-11-14 04:17:37 +00001512 const MCSectionELF *Group = RevGroupMap[Section.getGroup()];
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001513 MCSectionData &Data = Asm.getOrCreateSectionData(*Group);
1514 // FIXME: we could use the previous fragment
1515 MCDataFragment *F = new MCDataFragment(&Data);
Rafael Espindola0ce09712014-03-25 22:43:53 +00001516 uint32_t Index = SectionIndexMap.lookup(&Section);
1517 write(*F, Index);
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001518 }
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001519}
1520
Rafael Espindola7fe7e052015-02-17 20:40:59 +00001521void ELFObjectWriter::writeSection(MCAssembler &Asm,
Daniel Dunbarfe0c28f2010-11-13 07:33:40 +00001522 const SectionIndexMapTy &SectionIndexMap,
Rafael Espindola7fe7e052015-02-17 20:40:59 +00001523 const RelMapTy &RelMap,
Daniel Dunbarfe0c28f2010-11-13 07:33:40 +00001524 uint32_t GroupSymbolIndex,
1525 uint64_t Offset, uint64_t Size,
1526 uint64_t Alignment,
1527 const MCSectionELF &Section) {
Rafael Espindola5a8d7812010-11-10 23:36:59 +00001528 uint64_t sh_link = 0;
1529 uint64_t sh_info = 0;
1530
1531 switch(Section.getType()) {
1532 case ELF::SHT_DYNAMIC:
Hans Wennborg83e6e1e2014-04-30 16:25:02 +00001533 sh_link = ShStrTabBuilder.getOffset(Section.getSectionName());
Rafael Espindola5a8d7812010-11-10 23:36:59 +00001534 sh_info = 0;
1535 break;
1536
1537 case ELF::SHT_REL:
1538 case ELF::SHT_RELA: {
Rafael Espindola3a7c0eb2015-02-17 20:37:50 +00001539 sh_link = SymbolTableIndex;
Rafael Espindola5a8d7812010-11-10 23:36:59 +00001540 assert(sh_link && ".symtab not found");
Rafael Espindola7fe7e052015-02-17 20:40:59 +00001541 const MCSectionELF *InfoSection = RelMap.find(&Section)->second;
Rafael Espindola5a8d7812010-11-10 23:36:59 +00001542 sh_info = SectionIndexMap.lookup(InfoSection);
1543 break;
1544 }
1545
1546 case ELF::SHT_SYMTAB:
1547 case ELF::SHT_DYNSYM:
1548 sh_link = StringTableIndex;
1549 sh_info = LastLocalSymbolIndex;
1550 break;
1551
1552 case ELF::SHT_SYMTAB_SHNDX:
1553 sh_link = SymbolTableIndex;
1554 break;
1555
1556 case ELF::SHT_PROGBITS:
1557 case ELF::SHT_STRTAB:
1558 case ELF::SHT_NOBITS:
Rafael Espindola9ae2d052010-12-26 21:30:59 +00001559 case ELF::SHT_NOTE:
Rafael Espindola5a8d7812010-11-10 23:36:59 +00001560 case ELF::SHT_NULL:
1561 case ELF::SHT_ARM_ATTRIBUTES:
Jason W Kim9c5b65d2011-01-12 00:19:25 +00001562 case ELF::SHT_INIT_ARRAY:
1563 case ELF::SHT_FINI_ARRAY:
1564 case ELF::SHT_PREINIT_ARRAY:
Rafael Espindola4b7b7fb2011-01-23 05:43:40 +00001565 case ELF::SHT_X86_64_UNWIND:
Jack Carterc1b17ed2013-01-18 21:20:38 +00001566 case ELF::SHT_MIPS_REGINFO:
1567 case ELF::SHT_MIPS_OPTIONS:
Vladimir Medicfb8a2a92014-07-08 08:59:22 +00001568 case ELF::SHT_MIPS_ABIFLAGS:
Rafael Espindola5a8d7812010-11-10 23:36:59 +00001569 // Nothing to do.
1570 break;
1571
Nick Lewyckyc6ac5f72011-10-07 23:28:32 +00001572 case ELF::SHT_GROUP:
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001573 sh_link = SymbolTableIndex;
1574 sh_info = GroupSymbolIndex;
1575 break;
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001576
Rafael Espindola5a8d7812010-11-10 23:36:59 +00001577 default:
Craig Topper35b2f752014-06-19 06:10:58 +00001578 llvm_unreachable("FIXME: sh_type value not supported!");
Rafael Espindola5a8d7812010-11-10 23:36:59 +00001579 }
1580
Logan Chien4b724422013-02-05 14:18:59 +00001581 if (TargetObjectWriter->getEMachine() == ELF::EM_ARM &&
1582 Section.getType() == ELF::SHT_ARM_EXIDX) {
1583 StringRef SecName(Section.getSectionName());
1584 if (SecName == ".ARM.exidx") {
Rafael Espindolaba31e272015-01-29 17:33:21 +00001585 sh_link = SectionIndexMap.lookup(Asm.getContext().getELFSection(
1586 ".text", ELF::SHT_PROGBITS, ELF::SHF_EXECINSTR | ELF::SHF_ALLOC));
Logan Chien4b724422013-02-05 14:18:59 +00001587 } else if (SecName.startswith(".ARM.exidx")) {
David Blaikie15b25df2013-10-22 23:41:52 +00001588 StringRef GroupName =
1589 Section.getGroup() ? Section.getGroup()->getName() : "";
1590 sh_link = SectionIndexMap.lookup(Asm.getContext().getELFSection(
1591 SecName.substr(sizeof(".ARM.exidx") - 1), ELF::SHT_PROGBITS,
Rafael Espindolaba31e272015-01-29 17:33:21 +00001592 ELF::SHF_EXECINSTR | ELF::SHF_ALLOC, 0, GroupName));
Logan Chien4b724422013-02-05 14:18:59 +00001593 }
1594 }
1595
Hans Wennborg83e6e1e2014-04-30 16:25:02 +00001596 WriteSecHdrEntry(ShStrTabBuilder.getOffset(Section.getSectionName()),
1597 Section.getType(),
Rafael Espindola5a8d7812010-11-10 23:36:59 +00001598 Section.getFlags(), 0, Offset, Size, sh_link, sh_info,
1599 Alignment, Section.getEntrySize());
1600}
1601
Jan Sjödin30a52de2011-02-28 21:45:04 +00001602bool ELFObjectWriter::IsELFMetaDataSection(const MCSectionData &SD) {
Rafael Espindolabaf2f3b2010-12-06 03:48:09 +00001603 return SD.getOrdinal() == ~UINT32_C(0) &&
Rafael Espindola60ebca92010-12-02 03:09:06 +00001604 !SD.getSection().isVirtualSection();
1605}
1606
Jan Sjödin30a52de2011-02-28 21:45:04 +00001607uint64_t ELFObjectWriter::DataSectionSize(const MCSectionData &SD) {
Rafael Espindola60ebca92010-12-02 03:09:06 +00001608 uint64_t Ret = 0;
1609 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1610 ++i) {
1611 const MCFragment &F = *i;
1612 assert(F.getKind() == MCFragment::FT_Data);
1613 Ret += cast<MCDataFragment>(F).getContents().size();
1614 }
1615 return Ret;
1616}
1617
Jan Sjödin30a52de2011-02-28 21:45:04 +00001618uint64_t ELFObjectWriter::GetSectionFileSize(const MCAsmLayout &Layout,
1619 const MCSectionData &SD) {
Rafael Espindola60ebca92010-12-02 03:09:06 +00001620 if (IsELFMetaDataSection(SD))
1621 return DataSectionSize(SD);
1622 return Layout.getSectionFileSize(&SD);
1623}
1624
Jan Sjödin30a52de2011-02-28 21:45:04 +00001625uint64_t ELFObjectWriter::GetSectionAddressSize(const MCAsmLayout &Layout,
1626 const MCSectionData &SD) {
Rafael Espindola60ebca92010-12-02 03:09:06 +00001627 if (IsELFMetaDataSection(SD))
1628 return DataSectionSize(SD);
Rafael Espindola93e3cf02010-12-07 00:27:36 +00001629 return Layout.getSectionAddressSize(&SD);
Rafael Espindola60ebca92010-12-02 03:09:06 +00001630}
1631
Rafael Espindola1557fd62011-03-20 18:44:20 +00001632void ELFObjectWriter::WriteDataSectionData(MCAssembler &Asm,
1633 const MCAsmLayout &Layout,
1634 const MCSectionELF &Section) {
Rafael Espindola1557fd62011-03-20 18:44:20 +00001635 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1636
Benjamin Kramer084b9f42012-01-20 14:42:32 +00001637 uint64_t Padding = OffsetToAlignment(OS.tell(), SD.getAlignment());
Rafael Espindola1557fd62011-03-20 18:44:20 +00001638 WriteZeros(Padding);
Rafael Espindola1557fd62011-03-20 18:44:20 +00001639
1640 if (IsELFMetaDataSection(SD)) {
1641 for (MCSectionData::const_iterator i = SD.begin(), e = SD.end(); i != e;
1642 ++i) {
1643 const MCFragment &F = *i;
1644 assert(F.getKind() == MCFragment::FT_Data);
Eli Bendersky84b2a792012-12-07 22:06:56 +00001645 WriteBytes(cast<MCDataFragment>(F).getContents());
Rafael Espindola1557fd62011-03-20 18:44:20 +00001646 }
1647 } else {
Jim Grosbach18e2fe42011-12-06 00:03:48 +00001648 Asm.writeSectionData(&SD, Layout);
Rafael Espindola60ebca92010-12-02 03:09:06 +00001649 }
1650}
1651
Rafael Espindola7fe7e052015-02-17 20:40:59 +00001652void ELFObjectWriter::writeSectionHeader(
1653 MCAssembler &Asm, const GroupMapTy &GroupMap, const MCAsmLayout &Layout,
1654 const SectionIndexMapTy &SectionIndexMap, const RelMapTy &RelMap,
1655 const SectionOffsetMapTy &SectionOffsetMap) {
Rafael Espindola1557fd62011-03-20 18:44:20 +00001656 const unsigned NumSections = Asm.size() + 1;
Matt Fleming6c1ad482010-08-16 18:57:57 +00001657
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001658 std::vector<const MCSectionELF*> Sections;
Rafael Espindola1557fd62011-03-20 18:44:20 +00001659 Sections.resize(NumSections - 1);
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001660
1661 for (SectionIndexMapTy::const_iterator i=
1662 SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
1663 const std::pair<const MCSectionELF*, uint32_t> &p = *i;
Rafael Espindola1557fd62011-03-20 18:44:20 +00001664 Sections[p.second - 1] = p.first;
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001665 }
1666
Matt Fleming6c1ad482010-08-16 18:57:57 +00001667 // Null section first.
Rafael Espindola3fe87a12010-10-31 00:16:26 +00001668 uint64_t FirstSectionSize =
1669 NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
1670 uint32_t FirstSectionLink =
1671 ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
1672 WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
Matt Fleming6c1ad482010-08-16 18:57:57 +00001673
Rafael Espindola1557fd62011-03-20 18:44:20 +00001674 for (unsigned i = 0; i < NumSections - 1; ++i) {
Rafael Espindolaa3e9a222010-11-11 18:13:52 +00001675 const MCSectionELF &Section = *Sections[i];
1676 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1677 uint32_t GroupSymbolIndex;
1678 if (Section.getType() != ELF::SHT_GROUP)
1679 GroupSymbolIndex = 0;
1680 else
Rafael Espindola1557fd62011-03-20 18:44:20 +00001681 GroupSymbolIndex = getSymbolIndexInSymbolTable(Asm,
1682 GroupMap.lookup(&Section));
Matt Fleming6c1ad482010-08-16 18:57:57 +00001683
Rafael Espindola93e3cf02010-12-07 00:27:36 +00001684 uint64_t Size = GetSectionAddressSize(Layout, SD);
Rafael Espindola60ebca92010-12-02 03:09:06 +00001685
Rafael Espindola7fe7e052015-02-17 20:40:59 +00001686 writeSection(Asm, SectionIndexMap, RelMap, GroupSymbolIndex,
Rafael Espindola1557fd62011-03-20 18:44:20 +00001687 SectionOffsetMap.lookup(&Section), Size,
Rafael Espindola5a8d7812010-11-10 23:36:59 +00001688 SD.getAlignment(), Section);
Matt Fleming6c1ad482010-08-16 18:57:57 +00001689 }
1690}
1691
Rafael Espindola1557fd62011-03-20 18:44:20 +00001692void ELFObjectWriter::ComputeSectionOrder(MCAssembler &Asm,
1693 std::vector<const MCSectionELF*> &Sections) {
1694 for (MCAssembler::iterator it = Asm.begin(),
1695 ie = Asm.end(); it != ie; ++it) {
1696 const MCSectionELF &Section =
1697 static_cast<const MCSectionELF &>(it->getSection());
1698 if (Section.getType() == ELF::SHT_GROUP)
1699 Sections.push_back(&Section);
1700 }
1701
1702 for (MCAssembler::iterator it = Asm.begin(),
1703 ie = Asm.end(); it != ie; ++it) {
1704 const MCSectionELF &Section =
1705 static_cast<const MCSectionELF &>(it->getSection());
1706 if (Section.getType() != ELF::SHT_GROUP &&
1707 Section.getType() != ELF::SHT_REL &&
1708 Section.getType() != ELF::SHT_RELA)
1709 Sections.push_back(&Section);
1710 }
1711
1712 for (MCAssembler::iterator it = Asm.begin(),
1713 ie = Asm.end(); it != ie; ++it) {
1714 const MCSectionELF &Section =
1715 static_cast<const MCSectionELF &>(it->getSection());
1716 if (Section.getType() == ELF::SHT_REL ||
1717 Section.getType() == ELF::SHT_RELA)
1718 Sections.push_back(&Section);
1719 }
1720}
1721
1722void ELFObjectWriter::WriteObject(MCAssembler &Asm,
1723 const MCAsmLayout &Layout) {
1724 GroupMapTy GroupMap;
1725 RevGroupMapTy RevGroupMap;
1726 SectionIndexMapTy SectionIndexMap;
1727
1728 unsigned NumUserSections = Asm.size();
1729
David Blaikiee06e8012014-04-11 22:11:50 +00001730 CompressDebugSections(Asm, const_cast<MCAsmLayout &>(Layout));
David Blaikie8019bf82014-04-10 21:53:53 +00001731
Rafael Espindola1557fd62011-03-20 18:44:20 +00001732 DenseMap<const MCSectionELF*, const MCSectionELF*> RelMap;
Rafael Espindola1557fd62011-03-20 18:44:20 +00001733 const unsigned NumUserAndRelocSections = Asm.size();
Rafael Espindolaead85492015-02-17 20:31:13 +00001734 createIndexedSections(Asm, const_cast<MCAsmLayout&>(Layout), GroupMap,
Rafael Espindola1557fd62011-03-20 18:44:20 +00001735 RevGroupMap, SectionIndexMap, RelMap);
1736 const unsigned AllSections = Asm.size();
1737 const unsigned NumIndexedSections = AllSections - NumUserAndRelocSections;
1738
1739 unsigned NumRegularSections = NumUserSections + NumIndexedSections;
1740
1741 // Compute symbol table information.
Rafael Espindola022bb762014-03-24 03:43:21 +00001742 computeSymbolTable(Asm, Layout, SectionIndexMap, RevGroupMap,
1743 NumRegularSections);
Rafael Espindola1557fd62011-03-20 18:44:20 +00001744
1745 WriteRelocations(Asm, const_cast<MCAsmLayout&>(Layout), RelMap);
1746
1747 CreateMetadataSections(const_cast<MCAssembler&>(Asm),
1748 const_cast<MCAsmLayout&>(Layout),
Rafael Espindolaef6baea2015-02-11 23:11:18 +00001749 SectionIndexMap);
Rafael Espindola1557fd62011-03-20 18:44:20 +00001750
1751 uint64_t NaturalAlignment = is64Bit() ? 8 : 4;
1752 uint64_t HeaderSize = is64Bit() ? sizeof(ELF::Elf64_Ehdr) :
1753 sizeof(ELF::Elf32_Ehdr);
1754 uint64_t FileOff = HeaderSize;
1755
1756 std::vector<const MCSectionELF*> Sections;
1757 ComputeSectionOrder(Asm, Sections);
1758 unsigned NumSections = Sections.size();
1759 SectionOffsetMapTy SectionOffsetMap;
1760 for (unsigned i = 0; i < NumRegularSections + 1; ++i) {
1761 const MCSectionELF &Section = *Sections[i];
1762 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1763
1764 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1765
1766 // Remember the offset into the file for this section.
1767 SectionOffsetMap[&Section] = FileOff;
1768
1769 // Get the size of the section in the output file (including padding).
1770 FileOff += GetSectionFileSize(Layout, SD);
1771 }
1772
1773 FileOff = RoundUpToAlignment(FileOff, NaturalAlignment);
1774
1775 const unsigned SectionHeaderOffset = FileOff - HeaderSize;
1776
1777 uint64_t SectionHeaderEntrySize = is64Bit() ?
1778 sizeof(ELF::Elf64_Shdr) : sizeof(ELF::Elf32_Shdr);
1779 FileOff += (NumSections + 1) * SectionHeaderEntrySize;
1780
1781 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i) {
1782 const MCSectionELF &Section = *Sections[i];
1783 const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
1784
1785 FileOff = RoundUpToAlignment(FileOff, SD.getAlignment());
1786
1787 // Remember the offset into the file for this section.
1788 SectionOffsetMap[&Section] = FileOff;
1789
1790 // Get the size of the section in the output file (including padding).
1791 FileOff += GetSectionFileSize(Layout, SD);
1792 }
1793
1794 // Write out the ELF header ...
Jack Carter1bd90ff2013-01-30 02:09:52 +00001795 WriteHeader(Asm, SectionHeaderOffset, NumSections + 1);
Rafael Espindola1557fd62011-03-20 18:44:20 +00001796
1797 // ... then the regular sections ...
1798 // + because of .shstrtab
1799 for (unsigned i = 0; i < NumRegularSections + 1; ++i)
1800 WriteDataSectionData(Asm, Layout, *Sections[i]);
1801
Benjamin Kramer084b9f42012-01-20 14:42:32 +00001802 uint64_t Padding = OffsetToAlignment(OS.tell(), NaturalAlignment);
Rafael Espindola1557fd62011-03-20 18:44:20 +00001803 WriteZeros(Padding);
1804
1805 // ... then the section header table ...
Rafael Espindola7fe7e052015-02-17 20:40:59 +00001806 writeSectionHeader(Asm, GroupMap, Layout, SectionIndexMap, RelMap,
Rafael Espindola1557fd62011-03-20 18:44:20 +00001807 SectionOffsetMap);
1808
Nick Lewycky4eb11432011-10-07 20:56:23 +00001809 // ... and then the remaining sections ...
Rafael Espindola1557fd62011-03-20 18:44:20 +00001810 for (unsigned i = NumRegularSections + 1; i < NumSections; ++i)
1811 WriteDataSectionData(Asm, Layout, *Sections[i]);
1812}
1813
Eli Friedmand92d17b2011-03-03 07:24:36 +00001814bool
1815ELFObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
1816 const MCSymbolData &DataA,
1817 const MCFragment &FB,
1818 bool InSet,
1819 bool IsPCRel) const {
Rafael Espindolac9e70682015-03-24 23:48:44 +00001820 if (isWeak(DataA))
Eli Friedmand92d17b2011-03-03 07:24:36 +00001821 return false;
1822 return MCObjectWriter::IsSymbolRefDifferenceFullyResolvedImpl(
1823 Asm, DataA, FB,InSet, IsPCRel);
1824}
1825
Rafael Espindola6b5e56c2010-12-17 17:45:22 +00001826MCObjectWriter *llvm::createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
1827 raw_ostream &OS,
Rafael Espindolafdaae0d2010-12-18 03:27:34 +00001828 bool IsLittleEndian) {
Rafael Espindola34a68af2011-12-22 03:24:43 +00001829 return new ELFObjectWriter(MOTW, OS, IsLittleEndian);
Rafael Espindolab264d332011-12-21 17:30:17 +00001830}