1 //===- lib/MC/ELFObjectWriter.cpp - ELF File Writer -----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements ELF object file writer information.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/Twine.h"
20 #include "llvm/BinaryFormat/ELF.h"
21 #include "llvm/MC/MCAsmBackend.h"
22 #include "llvm/MC/MCAsmInfo.h"
23 #include "llvm/MC/MCAsmLayout.h"
24 #include "llvm/MC/MCAssembler.h"
25 #include "llvm/MC/MCContext.h"
26 #include "llvm/MC/MCELFObjectWriter.h"
27 #include "llvm/MC/MCExpr.h"
28 #include "llvm/MC/MCFixup.h"
29 #include "llvm/MC/MCFixupKindInfo.h"
30 #include "llvm/MC/MCFragment.h"
31 #include "llvm/MC/MCObjectFileInfo.h"
32 #include "llvm/MC/MCObjectWriter.h"
33 #include "llvm/MC/MCSection.h"
34 #include "llvm/MC/MCSectionELF.h"
35 #include "llvm/MC/MCSymbol.h"
36 #include "llvm/MC/MCSymbolELF.h"
37 #include "llvm/MC/MCValue.h"
38 #include "llvm/MC/StringTableBuilder.h"
39 #include "llvm/Support/Alignment.h"
40 #include "llvm/Support/Allocator.h"
41 #include "llvm/Support/Casting.h"
42 #include "llvm/Support/Compression.h"
43 #include "llvm/Support/EndianStream.h"
44 #include "llvm/Support/Error.h"
45 #include "llvm/Support/ErrorHandling.h"
46 #include "llvm/Support/Host.h"
47 #include "llvm/Support/LEB128.h"
48 #include "llvm/Support/MathExtras.h"
49 #include "llvm/Support/SMLoc.h"
50 #include "llvm/Support/StringSaver.h"
51 #include "llvm/Support/SwapByteOrder.h"
52 #include "llvm/Support/raw_ostream.h"
53 #include <algorithm>
54 #include <cassert>
55 #include <cstddef>
56 #include <cstdint>
57 #include <map>
58 #include <memory>
59 #include <string>
60 #include <utility>
61 #include <vector>
62 
63 using namespace llvm;
64 
65 #undef  DEBUG_TYPE
66 #define DEBUG_TYPE "reloc-info"
67 
68 namespace {
69 
70 using SectionIndexMapTy = DenseMap<const MCSectionELF *, uint32_t>;
71 
72 class ELFObjectWriter;
73 struct ELFWriter;
74 
isDwoSection(const MCSectionELF & Sec)75 bool isDwoSection(const MCSectionELF &Sec) {
76   return Sec.getName().endswith(".dwo");
77 }
78 
79 class SymbolTableWriter {
80   ELFWriter &EWriter;
81   bool Is64Bit;
82 
83   // indexes we are going to write to .symtab_shndx.
84   std::vector<uint32_t> ShndxIndexes;
85 
86   // The numbel of symbols written so far.
87   unsigned NumWritten;
88 
89   void createSymtabShndx();
90 
91   template <typename T> void write(T Value);
92 
93 public:
94   SymbolTableWriter(ELFWriter &EWriter, bool Is64Bit);
95 
96   void writeSymbol(uint32_t name, uint8_t info, uint64_t value, uint64_t size,
97                    uint8_t other, uint32_t shndx, bool Reserved);
98 
getShndxIndexes() const99   ArrayRef<uint32_t> getShndxIndexes() const { return ShndxIndexes; }
100 };
101 
102 struct ELFWriter {
103   ELFObjectWriter &OWriter;
104   support::endian::Writer W;
105 
106   enum DwoMode {
107     AllSections,
108     NonDwoOnly,
109     DwoOnly,
110   } Mode;
111 
112   static uint64_t SymbolValue(const MCSymbol &Sym, const MCAsmLayout &Layout);
113   static bool isInSymtab(const MCAsmLayout &Layout, const MCSymbolELF &Symbol,
114                          bool Used, bool Renamed);
115 
116   /// Helper struct for containing some precomputed information on symbols.
117   struct ELFSymbolData {
118     const MCSymbolELF *Symbol;
119     StringRef Name;
120     uint32_t SectionIndex;
121     uint32_t Order;
122   };
123 
124   /// @}
125   /// @name Symbol Table Data
126   /// @{
127 
128   StringTableBuilder StrTabBuilder{StringTableBuilder::ELF};
129 
130   /// @}
131 
132   // This holds the symbol table index of the last local symbol.
133   unsigned LastLocalSymbolIndex;
134   // This holds the .strtab section index.
135   unsigned StringTableIndex;
136   // This holds the .symtab section index.
137   unsigned SymbolTableIndex;
138 
139   // Sections in the order they are to be output in the section table.
140   std::vector<const MCSectionELF *> SectionTable;
141   unsigned addToSectionTable(const MCSectionELF *Sec);
142 
143   // TargetObjectWriter wrappers.
144   bool is64Bit() const;
145   bool usesRela(const MCSectionELF &Sec) const;
146 
147   uint64_t align(unsigned Alignment);
148 
149   bool maybeWriteCompression(uint64_t Size,
150                              SmallVectorImpl<char> &CompressedContents,
151                              bool ZLibStyle, unsigned Alignment);
152 
153 public:
ELFWriter__anon94c593740111::ELFWriter154   ELFWriter(ELFObjectWriter &OWriter, raw_pwrite_stream &OS,
155             bool IsLittleEndian, DwoMode Mode)
156       : OWriter(OWriter),
157         W(OS, IsLittleEndian ? support::little : support::big), Mode(Mode) {}
158 
WriteWord__anon94c593740111::ELFWriter159   void WriteWord(uint64_t Word) {
160     if (is64Bit())
161       W.write<uint64_t>(Word);
162     else
163       W.write<uint32_t>(Word);
164   }
165 
write__anon94c593740111::ELFWriter166   template <typename T> void write(T Val) {
167     W.write(Val);
168   }
169 
170   void writeHeader(const MCAssembler &Asm);
171 
172   void writeSymbol(SymbolTableWriter &Writer, uint32_t StringIndex,
173                    ELFSymbolData &MSD, const MCAsmLayout &Layout);
174 
175   // Start and end offset of each section
176   using SectionOffsetsTy =
177       std::map<const MCSectionELF *, std::pair<uint64_t, uint64_t>>;
178 
179   // Map from a signature symbol to the group section index
180   using RevGroupMapTy = DenseMap<const MCSymbol *, unsigned>;
181 
182   /// Compute the symbol table data
183   ///
184   /// \param Asm - The assembler.
185   /// \param SectionIndexMap - Maps a section to its index.
186   /// \param RevGroupMap - Maps a signature symbol to the group section.
187   void computeSymbolTable(MCAssembler &Asm, const MCAsmLayout &Layout,
188                           const SectionIndexMapTy &SectionIndexMap,
189                           const RevGroupMapTy &RevGroupMap,
190                           SectionOffsetsTy &SectionOffsets);
191 
192   void writeAddrsigSection();
193 
194   MCSectionELF *createRelocationSection(MCContext &Ctx,
195                                         const MCSectionELF &Sec);
196 
197   void writeSectionHeader(const MCAsmLayout &Layout,
198                           const SectionIndexMapTy &SectionIndexMap,
199                           const SectionOffsetsTy &SectionOffsets);
200 
201   void writeSectionData(const MCAssembler &Asm, MCSection &Sec,
202                         const MCAsmLayout &Layout);
203 
204   void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
205                         uint64_t Address, uint64_t Offset, uint64_t Size,
206                         uint32_t Link, uint32_t Info, uint64_t Alignment,
207                         uint64_t EntrySize);
208 
209   void writeRelocations(const MCAssembler &Asm, const MCSectionELF &Sec);
210 
211   uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout);
212   void writeSection(const SectionIndexMapTy &SectionIndexMap,
213                     uint32_t GroupSymbolIndex, uint64_t Offset, uint64_t Size,
214                     const MCSectionELF &Section);
215 };
216 
217 class ELFObjectWriter : public MCObjectWriter {
218   /// The target specific ELF writer instance.
219   std::unique_ptr<MCELFObjectTargetWriter> TargetObjectWriter;
220 
221   DenseMap<const MCSectionELF *, std::vector<ELFRelocationEntry>> Relocations;
222 
223   DenseMap<const MCSymbolELF *, const MCSymbolELF *> Renames;
224 
225   bool SeenGnuAbi = false;
226   bool EmitAddrsigSection = false;
227   std::vector<const MCSymbol *> AddrsigSyms;
228 
229   bool hasRelocationAddend() const;
230 
231   bool shouldRelocateWithSymbol(const MCAssembler &Asm,
232                                 const MCSymbolRefExpr *RefA,
233                                 const MCSymbolELF *Sym, uint64_t C,
234                                 unsigned Type) const;
235 
236 public:
ELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW)237   ELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW)
238       : TargetObjectWriter(std::move(MOTW)) {}
239 
reset()240   void reset() override {
241     SeenGnuAbi = false;
242     Relocations.clear();
243     Renames.clear();
244     MCObjectWriter::reset();
245   }
246 
247   bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
248                                               const MCSymbol &SymA,
249                                               const MCFragment &FB, bool InSet,
250                                               bool IsPCRel) const override;
251 
checkRelocation(MCContext & Ctx,SMLoc Loc,const MCSectionELF * From,const MCSectionELF * To)252   virtual bool checkRelocation(MCContext &Ctx, SMLoc Loc,
253                                const MCSectionELF *From,
254                                const MCSectionELF *To) {
255     return true;
256   }
257 
258   void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
259                         const MCFragment *Fragment, const MCFixup &Fixup,
260                         MCValue Target, uint64_t &FixedValue) override;
261 
262   void executePostLayoutBinding(MCAssembler &Asm,
263                                 const MCAsmLayout &Layout) override;
264 
markGnuAbi()265   void markGnuAbi() override { SeenGnuAbi = true; }
seenGnuAbi() const266   bool seenGnuAbi() const { return SeenGnuAbi; }
emitAddrsigSection()267   void emitAddrsigSection() override { EmitAddrsigSection = true; }
addAddrsigSymbol(const MCSymbol * Sym)268   void addAddrsigSymbol(const MCSymbol *Sym) override {
269     AddrsigSyms.push_back(Sym);
270   }
271 
272   friend struct ELFWriter;
273 };
274 
275 class ELFSingleObjectWriter : public ELFObjectWriter {
276   raw_pwrite_stream &OS;
277   bool IsLittleEndian;
278 
279 public:
ELFSingleObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,raw_pwrite_stream & OS,bool IsLittleEndian)280   ELFSingleObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
281                         raw_pwrite_stream &OS, bool IsLittleEndian)
282       : ELFObjectWriter(std::move(MOTW)), OS(OS),
283         IsLittleEndian(IsLittleEndian) {}
284 
writeObject(MCAssembler & Asm,const MCAsmLayout & Layout)285   uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override {
286     return ELFWriter(*this, OS, IsLittleEndian, ELFWriter::AllSections)
287         .writeObject(Asm, Layout);
288   }
289 
290   friend struct ELFWriter;
291 };
292 
293 class ELFDwoObjectWriter : public ELFObjectWriter {
294   raw_pwrite_stream &OS, &DwoOS;
295   bool IsLittleEndian;
296 
297 public:
ELFDwoObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,raw_pwrite_stream & OS,raw_pwrite_stream & DwoOS,bool IsLittleEndian)298   ELFDwoObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
299                      raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS,
300                      bool IsLittleEndian)
301       : ELFObjectWriter(std::move(MOTW)), OS(OS), DwoOS(DwoOS),
302         IsLittleEndian(IsLittleEndian) {}
303 
checkRelocation(MCContext & Ctx,SMLoc Loc,const MCSectionELF * From,const MCSectionELF * To)304   virtual bool checkRelocation(MCContext &Ctx, SMLoc Loc,
305                                const MCSectionELF *From,
306                                const MCSectionELF *To) override {
307     if (isDwoSection(*From)) {
308       Ctx.reportError(Loc, "A dwo section may not contain relocations");
309       return false;
310     }
311     if (To && isDwoSection(*To)) {
312       Ctx.reportError(Loc, "A relocation may not refer to a dwo section");
313       return false;
314     }
315     return true;
316   }
317 
writeObject(MCAssembler & Asm,const MCAsmLayout & Layout)318   uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override {
319     uint64_t Size = ELFWriter(*this, OS, IsLittleEndian, ELFWriter::NonDwoOnly)
320                         .writeObject(Asm, Layout);
321     Size += ELFWriter(*this, DwoOS, IsLittleEndian, ELFWriter::DwoOnly)
322                 .writeObject(Asm, Layout);
323     return Size;
324   }
325 };
326 
327 } // end anonymous namespace
328 
align(unsigned Alignment)329 uint64_t ELFWriter::align(unsigned Alignment) {
330   uint64_t Offset = W.OS.tell(), NewOffset = alignTo(Offset, Alignment);
331   W.OS.write_zeros(NewOffset - Offset);
332   return NewOffset;
333 }
334 
addToSectionTable(const MCSectionELF * Sec)335 unsigned ELFWriter::addToSectionTable(const MCSectionELF *Sec) {
336   SectionTable.push_back(Sec);
337   StrTabBuilder.add(Sec->getName());
338   return SectionTable.size();
339 }
340 
createSymtabShndx()341 void SymbolTableWriter::createSymtabShndx() {
342   if (!ShndxIndexes.empty())
343     return;
344 
345   ShndxIndexes.resize(NumWritten);
346 }
347 
write(T Value)348 template <typename T> void SymbolTableWriter::write(T Value) {
349   EWriter.write(Value);
350 }
351 
SymbolTableWriter(ELFWriter & EWriter,bool Is64Bit)352 SymbolTableWriter::SymbolTableWriter(ELFWriter &EWriter, bool Is64Bit)
353     : EWriter(EWriter), Is64Bit(Is64Bit), NumWritten(0) {}
354 
writeSymbol(uint32_t name,uint8_t info,uint64_t value,uint64_t size,uint8_t other,uint32_t shndx,bool Reserved)355 void SymbolTableWriter::writeSymbol(uint32_t name, uint8_t info, uint64_t value,
356                                     uint64_t size, uint8_t other,
357                                     uint32_t shndx, bool Reserved) {
358   bool LargeIndex = shndx >= ELF::SHN_LORESERVE && !Reserved;
359 
360   if (LargeIndex)
361     createSymtabShndx();
362 
363   if (!ShndxIndexes.empty()) {
364     if (LargeIndex)
365       ShndxIndexes.push_back(shndx);
366     else
367       ShndxIndexes.push_back(0);
368   }
369 
370   uint16_t Index = LargeIndex ? uint16_t(ELF::SHN_XINDEX) : shndx;
371 
372   if (Is64Bit) {
373     write(name);  // st_name
374     write(info);  // st_info
375     write(other); // st_other
376     write(Index); // st_shndx
377     write(value); // st_value
378     write(size);  // st_size
379   } else {
380     write(name);            // st_name
381     write(uint32_t(value)); // st_value
382     write(uint32_t(size));  // st_size
383     write(info);            // st_info
384     write(other);           // st_other
385     write(Index);           // st_shndx
386   }
387 
388   ++NumWritten;
389 }
390 
is64Bit() const391 bool ELFWriter::is64Bit() const {
392   return OWriter.TargetObjectWriter->is64Bit();
393 }
394 
usesRela(const MCSectionELF & Sec) const395 bool ELFWriter::usesRela(const MCSectionELF &Sec) const {
396   return OWriter.hasRelocationAddend() &&
397          Sec.getType() != ELF::SHT_LLVM_CALL_GRAPH_PROFILE;
398 }
399 
400 // Emit the ELF header.
writeHeader(const MCAssembler & Asm)401 void ELFWriter::writeHeader(const MCAssembler &Asm) {
402   // ELF Header
403   // ----------
404   //
405   // Note
406   // ----
407   // emitWord method behaves differently for ELF32 and ELF64, writing
408   // 4 bytes in the former and 8 in the latter.
409 
410   W.OS << ELF::ElfMagic; // e_ident[EI_MAG0] to e_ident[EI_MAG3]
411 
412   W.OS << char(is64Bit() ? ELF::ELFCLASS64 : ELF::ELFCLASS32); // e_ident[EI_CLASS]
413 
414   // e_ident[EI_DATA]
415   W.OS << char(W.Endian == support::little ? ELF::ELFDATA2LSB
416                                            : ELF::ELFDATA2MSB);
417 
418   W.OS << char(ELF::EV_CURRENT);        // e_ident[EI_VERSION]
419   // e_ident[EI_OSABI]
420   uint8_t OSABI = OWriter.TargetObjectWriter->getOSABI();
421   W.OS << char(OSABI == ELF::ELFOSABI_NONE && OWriter.seenGnuAbi()
422                    ? int(ELF::ELFOSABI_GNU)
423                    : OSABI);
424   // e_ident[EI_ABIVERSION]
425   W.OS << char(OWriter.TargetObjectWriter->getABIVersion());
426 
427   W.OS.write_zeros(ELF::EI_NIDENT - ELF::EI_PAD);
428 
429   W.write<uint16_t>(ELF::ET_REL);             // e_type
430 
431   W.write<uint16_t>(OWriter.TargetObjectWriter->getEMachine()); // e_machine = target
432 
433   W.write<uint32_t>(ELF::EV_CURRENT);         // e_version
434   WriteWord(0);                    // e_entry, no entry point in .o file
435   WriteWord(0);                    // e_phoff, no program header for .o
436   WriteWord(0);                     // e_shoff = sec hdr table off in bytes
437 
438   // e_flags = whatever the target wants
439   W.write<uint32_t>(Asm.getELFHeaderEFlags());
440 
441   // e_ehsize = ELF header size
442   W.write<uint16_t>(is64Bit() ? sizeof(ELF::Elf64_Ehdr)
443                               : sizeof(ELF::Elf32_Ehdr));
444 
445   W.write<uint16_t>(0);                  // e_phentsize = prog header entry size
446   W.write<uint16_t>(0);                  // e_phnum = # prog header entries = 0
447 
448   // e_shentsize = Section header entry size
449   W.write<uint16_t>(is64Bit() ? sizeof(ELF::Elf64_Shdr)
450                               : sizeof(ELF::Elf32_Shdr));
451 
452   // e_shnum     = # of section header ents
453   W.write<uint16_t>(0);
454 
455   // e_shstrndx  = Section # of '.strtab'
456   assert(StringTableIndex < ELF::SHN_LORESERVE);
457   W.write<uint16_t>(StringTableIndex);
458 }
459 
SymbolValue(const MCSymbol & Sym,const MCAsmLayout & Layout)460 uint64_t ELFWriter::SymbolValue(const MCSymbol &Sym,
461                                 const MCAsmLayout &Layout) {
462   if (Sym.isCommon())
463     return Sym.getCommonAlignment();
464 
465   uint64_t Res;
466   if (!Layout.getSymbolOffset(Sym, Res))
467     return 0;
468 
469   if (Layout.getAssembler().isThumbFunc(&Sym))
470     Res |= 1;
471 
472   return Res;
473 }
474 
mergeTypeForSet(uint8_t origType,uint8_t newType)475 static uint8_t mergeTypeForSet(uint8_t origType, uint8_t newType) {
476   uint8_t Type = newType;
477 
478   // Propagation rules:
479   // IFUNC > FUNC > OBJECT > NOTYPE
480   // TLS_OBJECT > OBJECT > NOTYPE
481   //
482   // dont let the new type degrade the old type
483   switch (origType) {
484   default:
485     break;
486   case ELF::STT_GNU_IFUNC:
487     if (Type == ELF::STT_FUNC || Type == ELF::STT_OBJECT ||
488         Type == ELF::STT_NOTYPE || Type == ELF::STT_TLS)
489       Type = ELF::STT_GNU_IFUNC;
490     break;
491   case ELF::STT_FUNC:
492     if (Type == ELF::STT_OBJECT || Type == ELF::STT_NOTYPE ||
493         Type == ELF::STT_TLS)
494       Type = ELF::STT_FUNC;
495     break;
496   case ELF::STT_OBJECT:
497     if (Type == ELF::STT_NOTYPE)
498       Type = ELF::STT_OBJECT;
499     break;
500   case ELF::STT_TLS:
501     if (Type == ELF::STT_OBJECT || Type == ELF::STT_NOTYPE ||
502         Type == ELF::STT_GNU_IFUNC || Type == ELF::STT_FUNC)
503       Type = ELF::STT_TLS;
504     break;
505   }
506 
507   return Type;
508 }
509 
isIFunc(const MCSymbolELF * Symbol)510 static bool isIFunc(const MCSymbolELF *Symbol) {
511   while (Symbol->getType() != ELF::STT_GNU_IFUNC) {
512     const MCSymbolRefExpr *Value;
513     if (!Symbol->isVariable() ||
514         !(Value = dyn_cast<MCSymbolRefExpr>(Symbol->getVariableValue())) ||
515         Value->getKind() != MCSymbolRefExpr::VK_None ||
516         mergeTypeForSet(Symbol->getType(), ELF::STT_GNU_IFUNC) != ELF::STT_GNU_IFUNC)
517       return false;
518     Symbol = &cast<MCSymbolELF>(Value->getSymbol());
519   }
520   return true;
521 }
522 
writeSymbol(SymbolTableWriter & Writer,uint32_t StringIndex,ELFSymbolData & MSD,const MCAsmLayout & Layout)523 void ELFWriter::writeSymbol(SymbolTableWriter &Writer, uint32_t StringIndex,
524                             ELFSymbolData &MSD, const MCAsmLayout &Layout) {
525   const auto &Symbol = cast<MCSymbolELF>(*MSD.Symbol);
526   const MCSymbolELF *Base =
527       cast_or_null<MCSymbolELF>(Layout.getBaseSymbol(Symbol));
528 
529   // This has to be in sync with when computeSymbolTable uses SHN_ABS or
530   // SHN_COMMON.
531   bool IsReserved = !Base || Symbol.isCommon();
532 
533   // Binding and Type share the same byte as upper and lower nibbles
534   uint8_t Binding = Symbol.getBinding();
535   uint8_t Type = Symbol.getType();
536   if (isIFunc(&Symbol))
537     Type = ELF::STT_GNU_IFUNC;
538   if (Base) {
539     Type = mergeTypeForSet(Type, Base->getType());
540   }
541   uint8_t Info = (Binding << 4) | Type;
542 
543   // Other and Visibility share the same byte with Visibility using the lower
544   // 2 bits
545   uint8_t Visibility = Symbol.getVisibility();
546   uint8_t Other = Symbol.getOther() | Visibility;
547 
548   uint64_t Value = SymbolValue(*MSD.Symbol, Layout);
549   uint64_t Size = 0;
550 
551   const MCExpr *ESize = MSD.Symbol->getSize();
552   if (!ESize && Base)
553     ESize = Base->getSize();
554 
555   if (ESize) {
556     int64_t Res;
557     if (!ESize->evaluateKnownAbsolute(Res, Layout))
558       report_fatal_error("Size expression must be absolute.");
559     Size = Res;
560   }
561 
562   // Write out the symbol table entry
563   Writer.writeSymbol(StringIndex, Info, Value, Size, Other, MSD.SectionIndex,
564                      IsReserved);
565 }
566 
isInSymtab(const MCAsmLayout & Layout,const MCSymbolELF & Symbol,bool Used,bool Renamed)567 bool ELFWriter::isInSymtab(const MCAsmLayout &Layout, const MCSymbolELF &Symbol,
568                            bool Used, bool Renamed) {
569   if (Symbol.isVariable()) {
570     const MCExpr *Expr = Symbol.getVariableValue();
571     // Target Expressions that are always inlined do not appear in the symtab
572     if (const auto *T = dyn_cast<MCTargetExpr>(Expr))
573       if (T->inlineAssignedExpr())
574         return false;
575     if (const MCSymbolRefExpr *Ref = dyn_cast<MCSymbolRefExpr>(Expr)) {
576       if (Ref->getKind() == MCSymbolRefExpr::VK_WEAKREF)
577         return false;
578     }
579   }
580 
581   if (Used)
582     return true;
583 
584   if (Renamed)
585     return false;
586 
587   if (Symbol.isVariable() && Symbol.isUndefined()) {
588     // FIXME: this is here just to diagnose the case of a var = commmon_sym.
589     Layout.getBaseSymbol(Symbol);
590     return false;
591   }
592 
593   if (Symbol.isTemporary())
594     return false;
595 
596   if (Symbol.getType() == ELF::STT_SECTION)
597     return false;
598 
599   return true;
600 }
601 
computeSymbolTable(MCAssembler & Asm,const MCAsmLayout & Layout,const SectionIndexMapTy & SectionIndexMap,const RevGroupMapTy & RevGroupMap,SectionOffsetsTy & SectionOffsets)602 void ELFWriter::computeSymbolTable(
603     MCAssembler &Asm, const MCAsmLayout &Layout,
604     const SectionIndexMapTy &SectionIndexMap, const RevGroupMapTy &RevGroupMap,
605     SectionOffsetsTy &SectionOffsets) {
606   MCContext &Ctx = Asm.getContext();
607   SymbolTableWriter Writer(*this, is64Bit());
608 
609   // Symbol table
610   unsigned EntrySize = is64Bit() ? ELF::SYMENTRY_SIZE64 : ELF::SYMENTRY_SIZE32;
611   MCSectionELF *SymtabSection =
612       Ctx.getELFSection(".symtab", ELF::SHT_SYMTAB, 0, EntrySize);
613   SymtabSection->setAlignment(is64Bit() ? Align(8) : Align(4));
614   SymbolTableIndex = addToSectionTable(SymtabSection);
615 
616   uint64_t SecStart = align(SymtabSection->getAlignment());
617 
618   // The first entry is the undefined symbol entry.
619   Writer.writeSymbol(0, 0, 0, 0, 0, 0, false);
620 
621   std::vector<ELFSymbolData> LocalSymbolData;
622   std::vector<ELFSymbolData> ExternalSymbolData;
623   MutableArrayRef<std::pair<std::string, size_t>> FileNames =
624       Asm.getFileNames();
625   for (const std::pair<std::string, size_t> &F : FileNames)
626     StrTabBuilder.add(F.first);
627 
628   // Add the data for the symbols.
629   bool HasLargeSectionIndex = false;
630   for (auto It : llvm::enumerate(Asm.symbols())) {
631     const auto &Symbol = cast<MCSymbolELF>(It.value());
632     bool Used = Symbol.isUsedInReloc();
633     bool WeakrefUsed = Symbol.isWeakrefUsedInReloc();
634     bool isSignature = Symbol.isSignature();
635 
636     if (!isInSymtab(Layout, Symbol, Used || WeakrefUsed || isSignature,
637                     OWriter.Renames.count(&Symbol)))
638       continue;
639 
640     if (Symbol.isTemporary() && Symbol.isUndefined()) {
641       Ctx.reportError(SMLoc(), "Undefined temporary symbol " + Symbol.getName());
642       continue;
643     }
644 
645     ELFSymbolData MSD;
646     MSD.Symbol = cast<MCSymbolELF>(&Symbol);
647     MSD.Order = It.index();
648 
649     bool Local = Symbol.getBinding() == ELF::STB_LOCAL;
650     assert(Local || !Symbol.isTemporary());
651 
652     if (Symbol.isAbsolute()) {
653       MSD.SectionIndex = ELF::SHN_ABS;
654     } else if (Symbol.isCommon()) {
655       if (Symbol.isTargetCommon()) {
656         MSD.SectionIndex = Symbol.getIndex();
657       } else {
658         assert(!Local);
659         MSD.SectionIndex = ELF::SHN_COMMON;
660       }
661     } else if (Symbol.isUndefined()) {
662       if (isSignature && !Used) {
663         MSD.SectionIndex = RevGroupMap.lookup(&Symbol);
664         if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
665           HasLargeSectionIndex = true;
666       } else {
667         MSD.SectionIndex = ELF::SHN_UNDEF;
668       }
669     } else {
670       const MCSectionELF &Section =
671           static_cast<const MCSectionELF &>(Symbol.getSection());
672 
673       // We may end up with a situation when section symbol is technically
674       // defined, but should not be. That happens because we explicitly
675       // pre-create few .debug_* sections to have accessors.
676       // And if these sections were not really defined in the code, but were
677       // referenced, we simply error out.
678       if (!Section.isRegistered()) {
679         assert(static_cast<const MCSymbolELF &>(Symbol).getType() ==
680                ELF::STT_SECTION);
681         Ctx.reportError(SMLoc(),
682                         "Undefined section reference: " + Symbol.getName());
683         continue;
684       }
685 
686       if (Mode == NonDwoOnly && isDwoSection(Section))
687         continue;
688       MSD.SectionIndex = SectionIndexMap.lookup(&Section);
689       assert(MSD.SectionIndex && "Invalid section index!");
690       if (MSD.SectionIndex >= ELF::SHN_LORESERVE)
691         HasLargeSectionIndex = true;
692     }
693 
694     StringRef Name = Symbol.getName();
695 
696     // Sections have their own string table
697     if (Symbol.getType() != ELF::STT_SECTION) {
698       MSD.Name = Name;
699       StrTabBuilder.add(Name);
700     }
701 
702     if (Local)
703       LocalSymbolData.push_back(MSD);
704     else
705       ExternalSymbolData.push_back(MSD);
706   }
707 
708   // This holds the .symtab_shndx section index.
709   unsigned SymtabShndxSectionIndex = 0;
710 
711   if (HasLargeSectionIndex) {
712     MCSectionELF *SymtabShndxSection =
713         Ctx.getELFSection(".symtab_shndx", ELF::SHT_SYMTAB_SHNDX, 0, 4);
714     SymtabShndxSectionIndex = addToSectionTable(SymtabShndxSection);
715     SymtabShndxSection->setAlignment(Align(4));
716   }
717 
718   StrTabBuilder.finalize();
719 
720   // Make the first STT_FILE precede previous local symbols.
721   unsigned Index = 1;
722   auto FileNameIt = FileNames.begin();
723   if (!FileNames.empty())
724     FileNames[0].second = 0;
725 
726   for (ELFSymbolData &MSD : LocalSymbolData) {
727     // Emit STT_FILE symbols before their associated local symbols.
728     for (; FileNameIt != FileNames.end() && FileNameIt->second <= MSD.Order;
729          ++FileNameIt) {
730       Writer.writeSymbol(StrTabBuilder.getOffset(FileNameIt->first),
731                          ELF::STT_FILE | ELF::STB_LOCAL, 0, 0, ELF::STV_DEFAULT,
732                          ELF::SHN_ABS, true);
733       ++Index;
734     }
735 
736     unsigned StringIndex = MSD.Symbol->getType() == ELF::STT_SECTION
737                                ? 0
738                                : StrTabBuilder.getOffset(MSD.Name);
739     MSD.Symbol->setIndex(Index++);
740     writeSymbol(Writer, StringIndex, MSD, Layout);
741   }
742   for (; FileNameIt != FileNames.end(); ++FileNameIt) {
743     Writer.writeSymbol(StrTabBuilder.getOffset(FileNameIt->first),
744                        ELF::STT_FILE | ELF::STB_LOCAL, 0, 0, ELF::STV_DEFAULT,
745                        ELF::SHN_ABS, true);
746     ++Index;
747   }
748 
749   // Write the symbol table entries.
750   LastLocalSymbolIndex = Index;
751 
752   for (ELFSymbolData &MSD : ExternalSymbolData) {
753     unsigned StringIndex = StrTabBuilder.getOffset(MSD.Name);
754     MSD.Symbol->setIndex(Index++);
755     writeSymbol(Writer, StringIndex, MSD, Layout);
756     assert(MSD.Symbol->getBinding() != ELF::STB_LOCAL);
757   }
758 
759   uint64_t SecEnd = W.OS.tell();
760   SectionOffsets[SymtabSection] = std::make_pair(SecStart, SecEnd);
761 
762   ArrayRef<uint32_t> ShndxIndexes = Writer.getShndxIndexes();
763   if (ShndxIndexes.empty()) {
764     assert(SymtabShndxSectionIndex == 0);
765     return;
766   }
767   assert(SymtabShndxSectionIndex != 0);
768 
769   SecStart = W.OS.tell();
770   const MCSectionELF *SymtabShndxSection =
771       SectionTable[SymtabShndxSectionIndex - 1];
772   for (uint32_t Index : ShndxIndexes)
773     write(Index);
774   SecEnd = W.OS.tell();
775   SectionOffsets[SymtabShndxSection] = std::make_pair(SecStart, SecEnd);
776 }
777 
writeAddrsigSection()778 void ELFWriter::writeAddrsigSection() {
779   for (const MCSymbol *Sym : OWriter.AddrsigSyms)
780     encodeULEB128(Sym->getIndex(), W.OS);
781 }
782 
createRelocationSection(MCContext & Ctx,const MCSectionELF & Sec)783 MCSectionELF *ELFWriter::createRelocationSection(MCContext &Ctx,
784                                                  const MCSectionELF &Sec) {
785   if (OWriter.Relocations[&Sec].empty())
786     return nullptr;
787 
788   const StringRef SectionName = Sec.getName();
789   bool Rela = usesRela(Sec);
790   std::string RelaSectionName = Rela ? ".rela" : ".rel";
791   RelaSectionName += SectionName;
792 
793   unsigned EntrySize;
794   if (Rela)
795     EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rela) : sizeof(ELF::Elf32_Rela);
796   else
797     EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
798 
799   unsigned Flags = 0;
800   if (Sec.getFlags() & ELF::SHF_GROUP)
801     Flags = ELF::SHF_GROUP;
802 
803   MCSectionELF *RelaSection = Ctx.createELFRelSection(
804       RelaSectionName, Rela ? ELF::SHT_RELA : ELF::SHT_REL, Flags, EntrySize,
805       Sec.getGroup(), &Sec);
806   RelaSection->setAlignment(is64Bit() ? Align(8) : Align(4));
807   return RelaSection;
808 }
809 
810 // Include the debug info compression header.
maybeWriteCompression(uint64_t Size,SmallVectorImpl<char> & CompressedContents,bool ZLibStyle,unsigned Alignment)811 bool ELFWriter::maybeWriteCompression(
812     uint64_t Size, SmallVectorImpl<char> &CompressedContents, bool ZLibStyle,
813     unsigned Alignment) {
814   if (ZLibStyle) {
815     uint64_t HdrSize =
816         is64Bit() ? sizeof(ELF::Elf32_Chdr) : sizeof(ELF::Elf64_Chdr);
817     if (Size <= HdrSize + CompressedContents.size())
818       return false;
819     // Platform specific header is followed by compressed data.
820     if (is64Bit()) {
821       // Write Elf64_Chdr header.
822       write(static_cast<ELF::Elf64_Word>(ELF::ELFCOMPRESS_ZLIB));
823       write(static_cast<ELF::Elf64_Word>(0)); // ch_reserved field.
824       write(static_cast<ELF::Elf64_Xword>(Size));
825       write(static_cast<ELF::Elf64_Xword>(Alignment));
826     } else {
827       // Write Elf32_Chdr header otherwise.
828       write(static_cast<ELF::Elf32_Word>(ELF::ELFCOMPRESS_ZLIB));
829       write(static_cast<ELF::Elf32_Word>(Size));
830       write(static_cast<ELF::Elf32_Word>(Alignment));
831     }
832     return true;
833   }
834 
835   // "ZLIB" followed by 8 bytes representing the uncompressed size of the section,
836   // useful for consumers to preallocate a buffer to decompress into.
837   const StringRef Magic = "ZLIB";
838   if (Size <= Magic.size() + sizeof(Size) + CompressedContents.size())
839     return false;
840   W.OS << Magic;
841   support::endian::write(W.OS, Size, support::big);
842   return true;
843 }
844 
writeSectionData(const MCAssembler & Asm,MCSection & Sec,const MCAsmLayout & Layout)845 void ELFWriter::writeSectionData(const MCAssembler &Asm, MCSection &Sec,
846                                  const MCAsmLayout &Layout) {
847   MCSectionELF &Section = static_cast<MCSectionELF &>(Sec);
848   StringRef SectionName = Section.getName();
849 
850   auto &MC = Asm.getContext();
851   const auto &MAI = MC.getAsmInfo();
852 
853   // Compressing debug_frame requires handling alignment fragments which is
854   // more work (possibly generalizing MCAssembler.cpp:writeFragment to allow
855   // for writing to arbitrary buffers) for little benefit.
856   bool CompressionEnabled =
857       MAI->compressDebugSections() != DebugCompressionType::None;
858   if (!CompressionEnabled || !SectionName.startswith(".debug_") ||
859       SectionName == ".debug_frame") {
860     Asm.writeSectionData(W.OS, &Section, Layout);
861     return;
862   }
863 
864   assert((MAI->compressDebugSections() == DebugCompressionType::Z ||
865           MAI->compressDebugSections() == DebugCompressionType::GNU) &&
866          "expected zlib or zlib-gnu style compression");
867 
868   SmallVector<char, 128> UncompressedData;
869   raw_svector_ostream VecOS(UncompressedData);
870   Asm.writeSectionData(VecOS, &Section, Layout);
871 
872   SmallVector<char, 128> CompressedContents;
873   if (Error E = zlib::compress(
874           StringRef(UncompressedData.data(), UncompressedData.size()),
875           CompressedContents)) {
876     consumeError(std::move(E));
877     W.OS << UncompressedData;
878     return;
879   }
880 
881   bool ZlibStyle = MAI->compressDebugSections() == DebugCompressionType::Z;
882   if (!maybeWriteCompression(UncompressedData.size(), CompressedContents,
883                              ZlibStyle, Sec.getAlignment())) {
884     W.OS << UncompressedData;
885     return;
886   }
887 
888   if (ZlibStyle) {
889     // Set the compressed flag. That is zlib style.
890     Section.setFlags(Section.getFlags() | ELF::SHF_COMPRESSED);
891     // Alignment field should reflect the requirements of
892     // the compressed section header.
893     Section.setAlignment(is64Bit() ? Align(8) : Align(4));
894   } else {
895     // Add "z" prefix to section name. This is zlib-gnu style.
896     MC.renameELFSection(&Section, (".z" + SectionName.drop_front(1)).str());
897   }
898   W.OS << CompressedContents;
899 }
900 
WriteSecHdrEntry(uint32_t Name,uint32_t Type,uint64_t Flags,uint64_t Address,uint64_t Offset,uint64_t Size,uint32_t Link,uint32_t Info,uint64_t Alignment,uint64_t EntrySize)901 void ELFWriter::WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
902                                  uint64_t Address, uint64_t Offset,
903                                  uint64_t Size, uint32_t Link, uint32_t Info,
904                                  uint64_t Alignment, uint64_t EntrySize) {
905   W.write<uint32_t>(Name);        // sh_name: index into string table
906   W.write<uint32_t>(Type);        // sh_type
907   WriteWord(Flags);     // sh_flags
908   WriteWord(Address);   // sh_addr
909   WriteWord(Offset);    // sh_offset
910   WriteWord(Size);      // sh_size
911   W.write<uint32_t>(Link);        // sh_link
912   W.write<uint32_t>(Info);        // sh_info
913   WriteWord(Alignment); // sh_addralign
914   WriteWord(EntrySize); // sh_entsize
915 }
916 
writeRelocations(const MCAssembler & Asm,const MCSectionELF & Sec)917 void ELFWriter::writeRelocations(const MCAssembler &Asm,
918                                        const MCSectionELF &Sec) {
919   std::vector<ELFRelocationEntry> &Relocs = OWriter.Relocations[&Sec];
920 
921   // We record relocations by pushing to the end of a vector. Reverse the vector
922   // to get the relocations in the order they were created.
923   // In most cases that is not important, but it can be for special sections
924   // (.eh_frame) or specific relocations (TLS optimizations on SystemZ).
925   std::reverse(Relocs.begin(), Relocs.end());
926 
927   // Sort the relocation entries. MIPS needs this.
928   OWriter.TargetObjectWriter->sortRelocs(Asm, Relocs);
929 
930   const bool Rela = usesRela(Sec);
931   for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
932     const ELFRelocationEntry &Entry = Relocs[e - i - 1];
933     unsigned Index = Entry.Symbol ? Entry.Symbol->getIndex() : 0;
934 
935     if (is64Bit()) {
936       write(Entry.Offset);
937       if (OWriter.TargetObjectWriter->getEMachine() == ELF::EM_MIPS) {
938         write(uint32_t(Index));
939 
940         write(OWriter.TargetObjectWriter->getRSsym(Entry.Type));
941         write(OWriter.TargetObjectWriter->getRType3(Entry.Type));
942         write(OWriter.TargetObjectWriter->getRType2(Entry.Type));
943         write(OWriter.TargetObjectWriter->getRType(Entry.Type));
944       } else {
945         struct ELF::Elf64_Rela ERE64;
946         ERE64.setSymbolAndType(Index, Entry.Type);
947         write(ERE64.r_info);
948       }
949       if (Rela)
950         write(Entry.Addend);
951     } else {
952       write(uint32_t(Entry.Offset));
953 
954       struct ELF::Elf32_Rela ERE32;
955       ERE32.setSymbolAndType(Index, Entry.Type);
956       write(ERE32.r_info);
957 
958       if (Rela)
959         write(uint32_t(Entry.Addend));
960 
961       if (OWriter.TargetObjectWriter->getEMachine() == ELF::EM_MIPS) {
962         if (uint32_t RType =
963                 OWriter.TargetObjectWriter->getRType2(Entry.Type)) {
964           write(uint32_t(Entry.Offset));
965 
966           ERE32.setSymbolAndType(0, RType);
967           write(ERE32.r_info);
968           write(uint32_t(0));
969         }
970         if (uint32_t RType =
971                 OWriter.TargetObjectWriter->getRType3(Entry.Type)) {
972           write(uint32_t(Entry.Offset));
973 
974           ERE32.setSymbolAndType(0, RType);
975           write(ERE32.r_info);
976           write(uint32_t(0));
977         }
978       }
979     }
980   }
981 }
982 
writeSection(const SectionIndexMapTy & SectionIndexMap,uint32_t GroupSymbolIndex,uint64_t Offset,uint64_t Size,const MCSectionELF & Section)983 void ELFWriter::writeSection(const SectionIndexMapTy &SectionIndexMap,
984                              uint32_t GroupSymbolIndex, uint64_t Offset,
985                              uint64_t Size, const MCSectionELF &Section) {
986   uint64_t sh_link = 0;
987   uint64_t sh_info = 0;
988 
989   switch(Section.getType()) {
990   default:
991     // Nothing to do.
992     break;
993 
994   case ELF::SHT_DYNAMIC:
995     llvm_unreachable("SHT_DYNAMIC in a relocatable object");
996 
997   case ELF::SHT_REL:
998   case ELF::SHT_RELA: {
999     sh_link = SymbolTableIndex;
1000     assert(sh_link && ".symtab not found");
1001     const MCSection *InfoSection = Section.getLinkedToSection();
1002     sh_info = SectionIndexMap.lookup(cast<MCSectionELF>(InfoSection));
1003     break;
1004   }
1005 
1006   case ELF::SHT_SYMTAB:
1007     sh_link = StringTableIndex;
1008     sh_info = LastLocalSymbolIndex;
1009     break;
1010 
1011   case ELF::SHT_SYMTAB_SHNDX:
1012   case ELF::SHT_LLVM_CALL_GRAPH_PROFILE:
1013   case ELF::SHT_LLVM_ADDRSIG:
1014     sh_link = SymbolTableIndex;
1015     break;
1016 
1017   case ELF::SHT_GROUP:
1018     sh_link = SymbolTableIndex;
1019     sh_info = GroupSymbolIndex;
1020     break;
1021   }
1022 
1023   if (Section.getFlags() & ELF::SHF_LINK_ORDER) {
1024     // If the value in the associated metadata is not a definition, Sym will be
1025     // undefined. Represent this with sh_link=0.
1026     const MCSymbol *Sym = Section.getLinkedToSymbol();
1027     if (Sym && Sym->isInSection()) {
1028       const MCSectionELF *Sec = cast<MCSectionELF>(&Sym->getSection());
1029       sh_link = SectionIndexMap.lookup(Sec);
1030     }
1031   }
1032 
1033   WriteSecHdrEntry(StrTabBuilder.getOffset(Section.getName()),
1034                    Section.getType(), Section.getFlags(), 0, Offset, Size,
1035                    sh_link, sh_info, Section.getAlignment(),
1036                    Section.getEntrySize());
1037 }
1038 
writeSectionHeader(const MCAsmLayout & Layout,const SectionIndexMapTy & SectionIndexMap,const SectionOffsetsTy & SectionOffsets)1039 void ELFWriter::writeSectionHeader(
1040     const MCAsmLayout &Layout, const SectionIndexMapTy &SectionIndexMap,
1041     const SectionOffsetsTy &SectionOffsets) {
1042   const unsigned NumSections = SectionTable.size();
1043 
1044   // Null section first.
1045   uint64_t FirstSectionSize =
1046       (NumSections + 1) >= ELF::SHN_LORESERVE ? NumSections + 1 : 0;
1047   WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, 0, 0, 0, 0);
1048 
1049   for (const MCSectionELF *Section : SectionTable) {
1050     uint32_t GroupSymbolIndex;
1051     unsigned Type = Section->getType();
1052     if (Type != ELF::SHT_GROUP)
1053       GroupSymbolIndex = 0;
1054     else
1055       GroupSymbolIndex = Section->getGroup()->getIndex();
1056 
1057     const std::pair<uint64_t, uint64_t> &Offsets =
1058         SectionOffsets.find(Section)->second;
1059     uint64_t Size;
1060     if (Type == ELF::SHT_NOBITS)
1061       Size = Layout.getSectionAddressSize(Section);
1062     else
1063       Size = Offsets.second - Offsets.first;
1064 
1065     writeSection(SectionIndexMap, GroupSymbolIndex, Offsets.first, Size,
1066                  *Section);
1067   }
1068 }
1069 
writeObject(MCAssembler & Asm,const MCAsmLayout & Layout)1070 uint64_t ELFWriter::writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) {
1071   uint64_t StartOffset = W.OS.tell();
1072 
1073   MCContext &Ctx = Asm.getContext();
1074   MCSectionELF *StrtabSection =
1075       Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0);
1076   StringTableIndex = addToSectionTable(StrtabSection);
1077 
1078   RevGroupMapTy RevGroupMap;
1079   SectionIndexMapTy SectionIndexMap;
1080 
1081   std::map<const MCSymbol *, std::vector<const MCSectionELF *>> GroupMembers;
1082 
1083   // Write out the ELF header ...
1084   writeHeader(Asm);
1085 
1086   // ... then the sections ...
1087   SectionOffsetsTy SectionOffsets;
1088   std::vector<MCSectionELF *> Groups;
1089   std::vector<MCSectionELF *> Relocations;
1090   for (MCSection &Sec : Asm) {
1091     MCSectionELF &Section = static_cast<MCSectionELF &>(Sec);
1092     if (Mode == NonDwoOnly && isDwoSection(Section))
1093       continue;
1094     if (Mode == DwoOnly && !isDwoSection(Section))
1095       continue;
1096 
1097     // Remember the offset into the file for this section.
1098     const uint64_t SecStart = align(Section.getAlignment());
1099 
1100     const MCSymbolELF *SignatureSymbol = Section.getGroup();
1101     writeSectionData(Asm, Section, Layout);
1102 
1103     uint64_t SecEnd = W.OS.tell();
1104     SectionOffsets[&Section] = std::make_pair(SecStart, SecEnd);
1105 
1106     MCSectionELF *RelSection = createRelocationSection(Ctx, Section);
1107 
1108     if (SignatureSymbol) {
1109       unsigned &GroupIdx = RevGroupMap[SignatureSymbol];
1110       if (!GroupIdx) {
1111         MCSectionELF *Group =
1112             Ctx.createELFGroupSection(SignatureSymbol, Section.isComdat());
1113         GroupIdx = addToSectionTable(Group);
1114         Group->setAlignment(Align(4));
1115         Groups.push_back(Group);
1116       }
1117       std::vector<const MCSectionELF *> &Members =
1118           GroupMembers[SignatureSymbol];
1119       Members.push_back(&Section);
1120       if (RelSection)
1121         Members.push_back(RelSection);
1122     }
1123 
1124     SectionIndexMap[&Section] = addToSectionTable(&Section);
1125     if (RelSection) {
1126       SectionIndexMap[RelSection] = addToSectionTable(RelSection);
1127       Relocations.push_back(RelSection);
1128     }
1129 
1130     OWriter.TargetObjectWriter->addTargetSectionFlags(Ctx, Section);
1131   }
1132 
1133   for (MCSectionELF *Group : Groups) {
1134     // Remember the offset into the file for this section.
1135     const uint64_t SecStart = align(Group->getAlignment());
1136 
1137     const MCSymbol *SignatureSymbol = Group->getGroup();
1138     assert(SignatureSymbol);
1139     write(uint32_t(Group->isComdat() ? unsigned(ELF::GRP_COMDAT) : 0));
1140     for (const MCSectionELF *Member : GroupMembers[SignatureSymbol]) {
1141       uint32_t SecIndex = SectionIndexMap.lookup(Member);
1142       write(SecIndex);
1143     }
1144 
1145     uint64_t SecEnd = W.OS.tell();
1146     SectionOffsets[Group] = std::make_pair(SecStart, SecEnd);
1147   }
1148 
1149   if (Mode == DwoOnly) {
1150     // dwo files don't have symbol tables or relocations, but they do have
1151     // string tables.
1152     StrTabBuilder.finalize();
1153   } else {
1154     MCSectionELF *AddrsigSection;
1155     if (OWriter.EmitAddrsigSection) {
1156       AddrsigSection = Ctx.getELFSection(".llvm_addrsig", ELF::SHT_LLVM_ADDRSIG,
1157                                          ELF::SHF_EXCLUDE);
1158       addToSectionTable(AddrsigSection);
1159     }
1160 
1161     // Compute symbol table information.
1162     computeSymbolTable(Asm, Layout, SectionIndexMap, RevGroupMap,
1163                        SectionOffsets);
1164 
1165     for (MCSectionELF *RelSection : Relocations) {
1166       // Remember the offset into the file for this section.
1167       const uint64_t SecStart = align(RelSection->getAlignment());
1168 
1169       writeRelocations(Asm,
1170                        cast<MCSectionELF>(*RelSection->getLinkedToSection()));
1171 
1172       uint64_t SecEnd = W.OS.tell();
1173       SectionOffsets[RelSection] = std::make_pair(SecStart, SecEnd);
1174     }
1175 
1176     if (OWriter.EmitAddrsigSection) {
1177       uint64_t SecStart = W.OS.tell();
1178       writeAddrsigSection();
1179       uint64_t SecEnd = W.OS.tell();
1180       SectionOffsets[AddrsigSection] = std::make_pair(SecStart, SecEnd);
1181     }
1182   }
1183 
1184   {
1185     uint64_t SecStart = W.OS.tell();
1186     StrTabBuilder.write(W.OS);
1187     SectionOffsets[StrtabSection] = std::make_pair(SecStart, W.OS.tell());
1188   }
1189 
1190   const uint64_t SectionHeaderOffset = align(is64Bit() ? 8 : 4);
1191 
1192   // ... then the section header table ...
1193   writeSectionHeader(Layout, SectionIndexMap, SectionOffsets);
1194 
1195   uint16_t NumSections = support::endian::byte_swap<uint16_t>(
1196       (SectionTable.size() + 1 >= ELF::SHN_LORESERVE) ? (uint16_t)ELF::SHN_UNDEF
1197                                                       : SectionTable.size() + 1,
1198       W.Endian);
1199   unsigned NumSectionsOffset;
1200 
1201   auto &Stream = static_cast<raw_pwrite_stream &>(W.OS);
1202   if (is64Bit()) {
1203     uint64_t Val =
1204         support::endian::byte_swap<uint64_t>(SectionHeaderOffset, W.Endian);
1205     Stream.pwrite(reinterpret_cast<char *>(&Val), sizeof(Val),
1206                   offsetof(ELF::Elf64_Ehdr, e_shoff));
1207     NumSectionsOffset = offsetof(ELF::Elf64_Ehdr, e_shnum);
1208   } else {
1209     uint32_t Val =
1210         support::endian::byte_swap<uint32_t>(SectionHeaderOffset, W.Endian);
1211     Stream.pwrite(reinterpret_cast<char *>(&Val), sizeof(Val),
1212                   offsetof(ELF::Elf32_Ehdr, e_shoff));
1213     NumSectionsOffset = offsetof(ELF::Elf32_Ehdr, e_shnum);
1214   }
1215   Stream.pwrite(reinterpret_cast<char *>(&NumSections), sizeof(NumSections),
1216                 NumSectionsOffset);
1217 
1218   return W.OS.tell() - StartOffset;
1219 }
1220 
hasRelocationAddend() const1221 bool ELFObjectWriter::hasRelocationAddend() const {
1222   return TargetObjectWriter->hasRelocationAddend();
1223 }
1224 
executePostLayoutBinding(MCAssembler & Asm,const MCAsmLayout & Layout)1225 void ELFObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
1226                                                const MCAsmLayout &Layout) {
1227   // The presence of symbol versions causes undefined symbols and
1228   // versions declared with @@@ to be renamed.
1229   for (const MCAssembler::Symver &S : Asm.Symvers) {
1230     StringRef AliasName = S.Name;
1231     const auto &Symbol = cast<MCSymbolELF>(*S.Sym);
1232     size_t Pos = AliasName.find('@');
1233     assert(Pos != StringRef::npos);
1234 
1235     StringRef Prefix = AliasName.substr(0, Pos);
1236     StringRef Rest = AliasName.substr(Pos);
1237     StringRef Tail = Rest;
1238     if (Rest.startswith("@@@"))
1239       Tail = Rest.substr(Symbol.isUndefined() ? 2 : 1);
1240 
1241     auto *Alias =
1242         cast<MCSymbolELF>(Asm.getContext().getOrCreateSymbol(Prefix + Tail));
1243     Asm.registerSymbol(*Alias);
1244     const MCExpr *Value = MCSymbolRefExpr::create(&Symbol, Asm.getContext());
1245     Alias->setVariableValue(Value);
1246 
1247     // Aliases defined with .symvar copy the binding from the symbol they alias.
1248     // This is the first place we are able to copy this information.
1249     Alias->setBinding(Symbol.getBinding());
1250     Alias->setVisibility(Symbol.getVisibility());
1251     Alias->setOther(Symbol.getOther());
1252 
1253     if (!Symbol.isUndefined() && S.KeepOriginalSym)
1254       continue;
1255 
1256     if (Symbol.isUndefined() && Rest.startswith("@@") &&
1257         !Rest.startswith("@@@")) {
1258       Asm.getContext().reportError(S.Loc, "default version symbol " +
1259                                               AliasName + " must be defined");
1260       continue;
1261     }
1262 
1263     if (Renames.count(&Symbol) && Renames[&Symbol] != Alias) {
1264       Asm.getContext().reportError(S.Loc, Twine("multiple versions for ") +
1265                                               Symbol.getName());
1266       continue;
1267     }
1268 
1269     Renames.insert(std::make_pair(&Symbol, Alias));
1270   }
1271 
1272   for (const MCSymbol *&Sym : AddrsigSyms) {
1273     if (const MCSymbol *R = Renames.lookup(cast<MCSymbolELF>(Sym)))
1274       Sym = R;
1275     if (Sym->isInSection() && Sym->getName().startswith(".L"))
1276       Sym = Sym->getSection().getBeginSymbol();
1277     Sym->setUsedInReloc();
1278   }
1279 }
1280 
1281 // It is always valid to create a relocation with a symbol. It is preferable
1282 // to use a relocation with a section if that is possible. Using the section
1283 // allows us to omit some local symbols from the symbol table.
shouldRelocateWithSymbol(const MCAssembler & Asm,const MCSymbolRefExpr * RefA,const MCSymbolELF * Sym,uint64_t C,unsigned Type) const1284 bool ELFObjectWriter::shouldRelocateWithSymbol(const MCAssembler &Asm,
1285                                                const MCSymbolRefExpr *RefA,
1286                                                const MCSymbolELF *Sym,
1287                                                uint64_t C,
1288                                                unsigned Type) const {
1289   // A PCRel relocation to an absolute value has no symbol (or section). We
1290   // represent that with a relocation to a null section.
1291   if (!RefA)
1292     return false;
1293 
1294   MCSymbolRefExpr::VariantKind Kind = RefA->getKind();
1295   switch (Kind) {
1296   default:
1297     break;
1298   // The .odp creation emits a relocation against the symbol ".TOC." which
1299   // create a R_PPC64_TOC relocation. However the relocation symbol name
1300   // in final object creation should be NULL, since the symbol does not
1301   // really exist, it is just the reference to TOC base for the current
1302   // object file. Since the symbol is undefined, returning false results
1303   // in a relocation with a null section which is the desired result.
1304   case MCSymbolRefExpr::VK_PPC_TOCBASE:
1305     return false;
1306 
1307   // These VariantKind cause the relocation to refer to something other than
1308   // the symbol itself, like a linker generated table. Since the address of
1309   // symbol is not relevant, we cannot replace the symbol with the
1310   // section and patch the difference in the addend.
1311   case MCSymbolRefExpr::VK_GOT:
1312   case MCSymbolRefExpr::VK_PLT:
1313   case MCSymbolRefExpr::VK_GOTPCREL:
1314   case MCSymbolRefExpr::VK_PPC_GOT_LO:
1315   case MCSymbolRefExpr::VK_PPC_GOT_HI:
1316   case MCSymbolRefExpr::VK_PPC_GOT_HA:
1317     return true;
1318   }
1319 
1320   // An undefined symbol is not in any section, so the relocation has to point
1321   // to the symbol itself.
1322   assert(Sym && "Expected a symbol");
1323   if (Sym->isUndefined())
1324     return true;
1325 
1326   unsigned Binding = Sym->getBinding();
1327   switch(Binding) {
1328   default:
1329     llvm_unreachable("Invalid Binding");
1330   case ELF::STB_LOCAL:
1331     break;
1332   case ELF::STB_WEAK:
1333     // If the symbol is weak, it might be overridden by a symbol in another
1334     // file. The relocation has to point to the symbol so that the linker
1335     // can update it.
1336     return true;
1337   case ELF::STB_GLOBAL:
1338     // Global ELF symbols can be preempted by the dynamic linker. The relocation
1339     // has to point to the symbol for a reason analogous to the STB_WEAK case.
1340     return true;
1341   }
1342 
1343   // Keep symbol type for a local ifunc because it may result in an IRELATIVE
1344   // reloc that the dynamic loader will use to resolve the address at startup
1345   // time.
1346   if (Sym->getType() == ELF::STT_GNU_IFUNC)
1347     return true;
1348 
1349   // If a relocation points to a mergeable section, we have to be careful.
1350   // If the offset is zero, a relocation with the section will encode the
1351   // same information. With a non-zero offset, the situation is different.
1352   // For example, a relocation can point 42 bytes past the end of a string.
1353   // If we change such a relocation to use the section, the linker would think
1354   // that it pointed to another string and subtracting 42 at runtime will
1355   // produce the wrong value.
1356   if (Sym->isInSection()) {
1357     auto &Sec = cast<MCSectionELF>(Sym->getSection());
1358     unsigned Flags = Sec.getFlags();
1359     if (Flags & ELF::SHF_MERGE) {
1360       if (C != 0)
1361         return true;
1362 
1363       // gold<2.34 incorrectly ignored the addend for R_386_GOTOFF (9)
1364       // (http://sourceware.org/PR16794).
1365       if (TargetObjectWriter->getEMachine() == ELF::EM_386 &&
1366           Type == ELF::R_386_GOTOFF)
1367         return true;
1368 
1369       // ld.lld handles R_MIPS_HI16/R_MIPS_LO16 separately, not as a whole, so
1370       // it doesn't know that an R_MIPS_HI16 with implicit addend 1 and an
1371       // R_MIPS_LO16 with implicit addend -32768 represents 32768, which is in
1372       // range of a MergeInputSection. We could introduce a new RelExpr member
1373       // (like R_RISCV_PC_INDIRECT for R_RISCV_PCREL_HI20 / R_RISCV_PCREL_LO12)
1374       // but the complexity is unnecessary given that GNU as keeps the original
1375       // symbol for this case as well.
1376       if (TargetObjectWriter->getEMachine() == ELF::EM_MIPS &&
1377           !hasRelocationAddend())
1378         return true;
1379     }
1380 
1381     // Most TLS relocations use a got, so they need the symbol. Even those that
1382     // are just an offset (@tpoff), require a symbol in gold versions before
1383     // 5efeedf61e4fe720fd3e9a08e6c91c10abb66d42 (2014-09-26) which fixed
1384     // http://sourceware.org/PR16773.
1385     if (Flags & ELF::SHF_TLS)
1386       return true;
1387   }
1388 
1389   // If the symbol is a thumb function the final relocation must set the lowest
1390   // bit. With a symbol that is done by just having the symbol have that bit
1391   // set, so we would lose the bit if we relocated with the section.
1392   // FIXME: We could use the section but add the bit to the relocation value.
1393   if (Asm.isThumbFunc(Sym))
1394     return true;
1395 
1396   if (TargetObjectWriter->needsRelocateWithSymbol(*Sym, Type))
1397     return true;
1398   return false;
1399 }
1400 
recordRelocation(MCAssembler & Asm,const MCAsmLayout & Layout,const MCFragment * Fragment,const MCFixup & Fixup,MCValue Target,uint64_t & FixedValue)1401 void ELFObjectWriter::recordRelocation(MCAssembler &Asm,
1402                                        const MCAsmLayout &Layout,
1403                                        const MCFragment *Fragment,
1404                                        const MCFixup &Fixup, MCValue Target,
1405                                        uint64_t &FixedValue) {
1406   MCAsmBackend &Backend = Asm.getBackend();
1407   bool IsPCRel = Backend.getFixupKindInfo(Fixup.getKind()).Flags &
1408                  MCFixupKindInfo::FKF_IsPCRel;
1409   const MCSectionELF &FixupSection = cast<MCSectionELF>(*Fragment->getParent());
1410   uint64_t C = Target.getConstant();
1411   uint64_t FixupOffset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
1412   MCContext &Ctx = Asm.getContext();
1413 
1414   if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
1415     const auto &SymB = cast<MCSymbolELF>(RefB->getSymbol());
1416     if (SymB.isUndefined()) {
1417       Ctx.reportError(Fixup.getLoc(),
1418                       Twine("symbol '") + SymB.getName() +
1419                           "' can not be undefined in a subtraction expression");
1420       return;
1421     }
1422 
1423     assert(!SymB.isAbsolute() && "Should have been folded");
1424     const MCSection &SecB = SymB.getSection();
1425     if (&SecB != &FixupSection) {
1426       Ctx.reportError(Fixup.getLoc(),
1427                       "Cannot represent a difference across sections");
1428       return;
1429     }
1430 
1431     assert(!IsPCRel && "should have been folded");
1432     IsPCRel = true;
1433     C += FixupOffset - Layout.getSymbolOffset(SymB);
1434   }
1435 
1436   // We either rejected the fixup or folded B into C at this point.
1437   const MCSymbolRefExpr *RefA = Target.getSymA();
1438   const auto *SymA = RefA ? cast<MCSymbolELF>(&RefA->getSymbol()) : nullptr;
1439 
1440   bool ViaWeakRef = false;
1441   if (SymA && SymA->isVariable()) {
1442     const MCExpr *Expr = SymA->getVariableValue();
1443     if (const auto *Inner = dyn_cast<MCSymbolRefExpr>(Expr)) {
1444       if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF) {
1445         SymA = cast<MCSymbolELF>(&Inner->getSymbol());
1446         ViaWeakRef = true;
1447       }
1448     }
1449   }
1450 
1451   const MCSectionELF *SecA = (SymA && SymA->isInSection())
1452                                  ? cast<MCSectionELF>(&SymA->getSection())
1453                                  : nullptr;
1454   if (!checkRelocation(Ctx, Fixup.getLoc(), &FixupSection, SecA))
1455     return;
1456 
1457   unsigned Type = TargetObjectWriter->getRelocType(Ctx, Target, Fixup, IsPCRel);
1458   const auto *Parent = cast<MCSectionELF>(Fragment->getParent());
1459   // Emiting relocation with sybmol for CG Profile to  help with --cg-profile.
1460   bool RelocateWithSymbol =
1461       shouldRelocateWithSymbol(Asm, RefA, SymA, C, Type) ||
1462       (Parent->getType() == ELF::SHT_LLVM_CALL_GRAPH_PROFILE);
1463   uint64_t Addend = 0;
1464 
1465   FixedValue = !RelocateWithSymbol && SymA && !SymA->isUndefined()
1466                    ? C + Layout.getSymbolOffset(*SymA)
1467                    : C;
1468   if (hasRelocationAddend()) {
1469     Addend = FixedValue;
1470     FixedValue = 0;
1471   }
1472 
1473   if (!RelocateWithSymbol) {
1474     const auto *SectionSymbol =
1475         SecA ? cast<MCSymbolELF>(SecA->getBeginSymbol()) : nullptr;
1476     if (SectionSymbol)
1477       SectionSymbol->setUsedInReloc();
1478     ELFRelocationEntry Rec(FixupOffset, SectionSymbol, Type, Addend, SymA, C);
1479     Relocations[&FixupSection].push_back(Rec);
1480     return;
1481   }
1482 
1483   const MCSymbolELF *RenamedSymA = SymA;
1484   if (SymA) {
1485     if (const MCSymbolELF *R = Renames.lookup(SymA))
1486       RenamedSymA = R;
1487 
1488     if (ViaWeakRef)
1489       RenamedSymA->setIsWeakrefUsedInReloc();
1490     else
1491       RenamedSymA->setUsedInReloc();
1492   }
1493   ELFRelocationEntry Rec(FixupOffset, RenamedSymA, Type, Addend, SymA, C);
1494   Relocations[&FixupSection].push_back(Rec);
1495 }
1496 
isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler & Asm,const MCSymbol & SA,const MCFragment & FB,bool InSet,bool IsPCRel) const1497 bool ELFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
1498     const MCAssembler &Asm, const MCSymbol &SA, const MCFragment &FB,
1499     bool InSet, bool IsPCRel) const {
1500   const auto &SymA = cast<MCSymbolELF>(SA);
1501   if (IsPCRel) {
1502     assert(!InSet);
1503     if (SymA.getBinding() != ELF::STB_LOCAL ||
1504         SymA.getType() == ELF::STT_GNU_IFUNC)
1505       return false;
1506   }
1507   return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB,
1508                                                                 InSet, IsPCRel);
1509 }
1510 
1511 std::unique_ptr<MCObjectWriter>
createELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,raw_pwrite_stream & OS,bool IsLittleEndian)1512 llvm::createELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
1513                             raw_pwrite_stream &OS, bool IsLittleEndian) {
1514   return std::make_unique<ELFSingleObjectWriter>(std::move(MOTW), OS,
1515                                                   IsLittleEndian);
1516 }
1517 
1518 std::unique_ptr<MCObjectWriter>
createELFDwoObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,raw_pwrite_stream & OS,raw_pwrite_stream & DwoOS,bool IsLittleEndian)1519 llvm::createELFDwoObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
1520                                raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS,
1521                                bool IsLittleEndian) {
1522   return std::make_unique<ELFDwoObjectWriter>(std::move(MOTW), OS, DwoOS,
1523                                                IsLittleEndian);
1524 }
1525