LLVM  14.0.0git
WinCOFFObjectWriter.cpp
Go to the documentation of this file.
1 //===- llvm/MC/WinCOFFObjectWriter.cpp ------------------------------------===//
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 contains an implementation of a Win32 COFF object file writer.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/ADT/DenseSet.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/COFF.h"
21 #include "llvm/MC/MCAsmLayout.h"
22 #include "llvm/MC/MCAssembler.h"
23 #include "llvm/MC/MCContext.h"
24 #include "llvm/MC/MCExpr.h"
25 #include "llvm/MC/MCFixup.h"
26 #include "llvm/MC/MCFragment.h"
27 #include "llvm/MC/MCObjectWriter.h"
28 #include "llvm/MC/MCSection.h"
29 #include "llvm/MC/MCSectionCOFF.h"
30 #include "llvm/MC/MCSymbol.h"
31 #include "llvm/MC/MCSymbolCOFF.h"
32 #include "llvm/MC/MCValue.h"
35 #include "llvm/Support/CRC.h"
36 #include "llvm/Support/Casting.h"
39 #include "llvm/Support/LEB128.h"
42 #include <algorithm>
43 #include <cassert>
44 #include <cstddef>
45 #include <cstdint>
46 #include <cstring>
47 #include <ctime>
48 #include <memory>
49 #include <string>
50 #include <vector>
51 
52 using namespace llvm;
54 
55 #define DEBUG_TYPE "WinCOFFObjectWriter"
56 
57 namespace {
58 
60 
61 enum AuxiliaryType {
62  ATWeakExternal,
63  ATFile,
64  ATSectionDefinition
65 };
66 
67 struct AuxSymbol {
68  AuxiliaryType AuxType;
69  COFF::Auxiliary Aux;
70 };
71 
72 class COFFSection;
73 
74 class COFFSymbol {
75 public:
76  COFF::symbol Data = {};
77 
78  using AuxiliarySymbols = SmallVector<AuxSymbol, 1>;
79 
80  name Name;
81  int Index;
82  AuxiliarySymbols Aux;
83  COFFSymbol *Other = nullptr;
84  COFFSection *Section = nullptr;
85  int Relocations = 0;
86  const MCSymbol *MC = nullptr;
87 
88  COFFSymbol(StringRef Name) : Name(Name) {}
89 
90  void set_name_offset(uint32_t Offset);
91 
92  int64_t getIndex() const { return Index; }
93  void setIndex(int Value) {
94  Index = Value;
95  if (MC)
96  MC->setIndex(static_cast<uint32_t>(Value));
97  }
98 };
99 
100 // This class contains staging data for a COFF relocation entry.
101 struct COFFRelocation {
103  COFFSymbol *Symb = nullptr;
104 
105  COFFRelocation() = default;
106 
107  static size_t size() { return COFF::RelocationSize; }
108 };
109 
110 using relocations = std::vector<COFFRelocation>;
111 
112 class COFFSection {
113 public:
114  COFF::section Header = {};
115 
116  std::string Name;
117  int Number;
118  MCSectionCOFF const *MCSection = nullptr;
119  COFFSymbol *Symbol = nullptr;
120  relocations Relocations;
121 
122  COFFSection(StringRef Name) : Name(std::string(Name)) {}
123 };
124 
125 class WinCOFFObjectWriter : public MCObjectWriter {
126 public:
128 
129  using symbols = std::vector<std::unique_ptr<COFFSymbol>>;
130  using sections = std::vector<std::unique_ptr<COFFSection>>;
131 
132  using symbol_map = DenseMap<MCSymbol const *, COFFSymbol *>;
133  using section_map = DenseMap<MCSection const *, COFFSection *>;
134 
135  using symbol_list = DenseSet<COFFSymbol *>;
136 
137  std::unique_ptr<MCWinCOFFObjectTargetWriter> TargetObjectWriter;
138 
139  // Root level file contents.
140  COFF::header Header = {};
141  sections Sections;
142  symbols Symbols;
144 
145  // Maps used during object file creation.
146  section_map SectionMap;
147  symbol_map SymbolMap;
148 
149  symbol_list WeakDefaults;
150 
151  bool UseBigObj;
152 
153  bool EmitAddrsigSection = false;
154  MCSectionCOFF *AddrsigSection;
155  std::vector<const MCSymbol *> AddrsigSyms;
156 
157  MCSectionCOFF *CGProfileSection = nullptr;
158 
159  WinCOFFObjectWriter(std::unique_ptr<MCWinCOFFObjectTargetWriter> MOTW,
160  raw_pwrite_stream &OS);
161 
162  void reset() override {
163  memset(&Header, 0, sizeof(Header));
164  Header.Machine = TargetObjectWriter->getMachine();
165  Sections.clear();
166  Symbols.clear();
167  Strings.clear();
168  SectionMap.clear();
169  SymbolMap.clear();
171  }
172 
173  COFFSymbol *createSymbol(StringRef Name);
174  COFFSymbol *GetOrCreateCOFFSymbol(const MCSymbol *Symbol);
175  COFFSection *createSection(StringRef Name);
176 
177  void defineSection(MCSectionCOFF const &Sec);
178 
179  COFFSymbol *getLinkedSymbol(const MCSymbol &Symbol);
180  void DefineSymbol(const MCSymbol &Symbol, MCAssembler &Assembler,
181  const MCAsmLayout &Layout);
182 
183  void SetSymbolName(COFFSymbol &S);
184  void SetSectionName(COFFSection &S);
185 
186  bool IsPhysicalSection(COFFSection *S);
187 
188  // Entity writing methods.
189 
190  void WriteFileHeader(const COFF::header &Header);
191  void WriteSymbol(const COFFSymbol &S);
192  void WriteAuxiliarySymbols(const COFFSymbol::AuxiliarySymbols &S);
193  void writeSectionHeaders();
194  void WriteRelocation(const COFF::relocation &R);
195  uint32_t writeSectionContents(MCAssembler &Asm, const MCAsmLayout &Layout,
196  const MCSection &MCSec);
197  void writeSection(MCAssembler &Asm, const MCAsmLayout &Layout,
198  const COFFSection &Sec, const MCSection &MCSec);
199 
200  // MCObjectWriter interface implementation.
201 
202  void executePostLayoutBinding(MCAssembler &Asm,
203  const MCAsmLayout &Layout) override;
204 
205  bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
206  const MCSymbol &SymA,
207  const MCFragment &FB, bool InSet,
208  bool IsPCRel) const override;
209 
210  void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
211  const MCFragment *Fragment, const MCFixup &Fixup,
212  MCValue Target, uint64_t &FixedValue) override;
213 
214  void createFileSymbols(MCAssembler &Asm);
215  void setWeakDefaultNames();
216  void assignSectionNumbers();
217  void assignFileOffsets(MCAssembler &Asm, const MCAsmLayout &Layout);
218 
219  void emitAddrsigSection() override { EmitAddrsigSection = true; }
220  void addAddrsigSymbol(const MCSymbol *Sym) override {
221  AddrsigSyms.push_back(Sym);
222  }
223 
224  uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
225 };
226 
227 } // end anonymous namespace
228 
229 //------------------------------------------------------------------------------
230 // Symbol class implementation
231 
232 // In the case that the name does not fit within 8 bytes, the offset
233 // into the string table is stored in the last 4 bytes instead, leaving
234 // the first 4 bytes as 0.
235 void COFFSymbol::set_name_offset(uint32_t Offset) {
236  write32le(Data.Name + 0, 0);
237  write32le(Data.Name + 4, Offset);
238 }
239 
240 //------------------------------------------------------------------------------
241 // WinCOFFObjectWriter class implementation
242 
243 WinCOFFObjectWriter::WinCOFFObjectWriter(
244  std::unique_ptr<MCWinCOFFObjectTargetWriter> MOTW, raw_pwrite_stream &OS)
245  : W(OS, support::little), TargetObjectWriter(std::move(MOTW)) {
246  Header.Machine = TargetObjectWriter->getMachine();
247 }
248 
249 COFFSymbol *WinCOFFObjectWriter::createSymbol(StringRef Name) {
250  Symbols.push_back(std::make_unique<COFFSymbol>(Name));
251  return Symbols.back().get();
252 }
253 
254 COFFSymbol *WinCOFFObjectWriter::GetOrCreateCOFFSymbol(const MCSymbol *Symbol) {
255  COFFSymbol *&Ret = SymbolMap[Symbol];
256  if (!Ret)
257  Ret = createSymbol(Symbol->getName());
258  return Ret;
259 }
260 
261 COFFSection *WinCOFFObjectWriter::createSection(StringRef Name) {
262  Sections.emplace_back(std::make_unique<COFFSection>(Name));
263  return Sections.back().get();
264 }
265 
266 static uint32_t getAlignment(const MCSectionCOFF &Sec) {
267  switch (Sec.getAlignment()) {
268  case 1:
270  case 2:
272  case 4:
274  case 8:
276  case 16:
278  case 32:
280  case 64:
282  case 128:
284  case 256:
286  case 512:
288  case 1024:
290  case 2048:
292  case 4096:
294  case 8192:
296  }
297  llvm_unreachable("unsupported section alignment");
298 }
299 
300 /// This function takes a section data object from the assembler
301 /// and creates the associated COFF section staging object.
302 void WinCOFFObjectWriter::defineSection(const MCSectionCOFF &MCSec) {
303  COFFSection *Section = createSection(MCSec.getName());
304  COFFSymbol *Symbol = createSymbol(MCSec.getName());
305  Section->Symbol = Symbol;
306  Symbol->Section = Section;
307  Symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_STATIC;
308 
309  // Create a COMDAT symbol if needed.
311  if (const MCSymbol *S = MCSec.getCOMDATSymbol()) {
312  COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(S);
313  if (COMDATSymbol->Section)
314  report_fatal_error("two sections have the same comdat");
315  COMDATSymbol->Section = Section;
316  }
317  }
318 
319  // In this case the auxiliary symbol is a Section Definition.
320  Symbol->Aux.resize(1);
321  Symbol->Aux[0] = {};
322  Symbol->Aux[0].AuxType = ATSectionDefinition;
323  Symbol->Aux[0].Aux.SectionDefinition.Selection = MCSec.getSelection();
324 
325  // Set section alignment.
326  Section->Header.Characteristics = MCSec.getCharacteristics();
327  Section->Header.Characteristics |= getAlignment(MCSec);
328 
329  // Bind internal COFF section to MC section.
330  Section->MCSection = &MCSec;
331  SectionMap[&MCSec] = Section;
332 }
333 
335  const MCAsmLayout &Layout) {
336  if (Symbol.isCommon() && Symbol.isExternal())
337  return Symbol.getCommonSize();
338 
339  uint64_t Res;
340  if (!Layout.getSymbolOffset(Symbol, Res))
341  return 0;
342 
343  return Res;
344 }
345 
346 COFFSymbol *WinCOFFObjectWriter::getLinkedSymbol(const MCSymbol &Symbol) {
347  if (!Symbol.isVariable())
348  return nullptr;
349 
350  const MCSymbolRefExpr *SymRef =
351  dyn_cast<MCSymbolRefExpr>(Symbol.getVariableValue());
352  if (!SymRef)
353  return nullptr;
354 
355  const MCSymbol &Aliasee = SymRef->getSymbol();
356  if (Aliasee.isUndefined() || Aliasee.isExternal())
357  return GetOrCreateCOFFSymbol(&Aliasee);
358  else
359  return nullptr;
360 }
361 
362 /// This function takes a symbol data object from the assembler
363 /// and creates the associated COFF symbol staging object.
364 void WinCOFFObjectWriter::DefineSymbol(const MCSymbol &MCSym,
365  MCAssembler &Assembler,
366  const MCAsmLayout &Layout) {
367  COFFSymbol *Sym = GetOrCreateCOFFSymbol(&MCSym);
368  const MCSymbol *Base = Layout.getBaseSymbol(MCSym);
369  COFFSection *Sec = nullptr;
370  if (Base && Base->getFragment()) {
371  Sec = SectionMap[Base->getFragment()->getParent()];
372  if (Sym->Section && Sym->Section != Sec)
373  report_fatal_error("conflicting sections for symbol");
374  }
375 
376  COFFSymbol *Local = nullptr;
377  if (cast<MCSymbolCOFF>(MCSym).isWeakExternal()) {
378  Sym->Data.StorageClass = COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
379  Sym->Section = nullptr;
380 
381  COFFSymbol *WeakDefault = getLinkedSymbol(MCSym);
382  if (!WeakDefault) {
383  std::string WeakName = (".weak." + MCSym.getName() + ".default").str();
384  WeakDefault = createSymbol(WeakName);
385  if (!Sec)
386  WeakDefault->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
387  else
388  WeakDefault->Section = Sec;
389  WeakDefaults.insert(WeakDefault);
390  Local = WeakDefault;
391  }
392 
393  Sym->Other = WeakDefault;
394 
395  // Setup the Weak External auxiliary symbol.
396  Sym->Aux.resize(1);
397  memset(&Sym->Aux[0], 0, sizeof(Sym->Aux[0]));
398  Sym->Aux[0].AuxType = ATWeakExternal;
399  Sym->Aux[0].Aux.WeakExternal.TagIndex = 0;
400  Sym->Aux[0].Aux.WeakExternal.Characteristics =
402  } else {
403  if (!Base)
404  Sym->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
405  else
406  Sym->Section = Sec;
407  Local = Sym;
408  }
409 
410  if (Local) {
411  Local->Data.Value = getSymbolValue(MCSym, Layout);
412 
413  const MCSymbolCOFF &SymbolCOFF = cast<MCSymbolCOFF>(MCSym);
414  Local->Data.Type = SymbolCOFF.getType();
415  Local->Data.StorageClass = SymbolCOFF.getClass();
416 
417  // If no storage class was specified in the streamer, define it here.
418  if (Local->Data.StorageClass == COFF::IMAGE_SYM_CLASS_NULL) {
419  bool IsExternal = MCSym.isExternal() ||
420  (!MCSym.getFragment() && !MCSym.isVariable());
421 
422  Local->Data.StorageClass = IsExternal ? COFF::IMAGE_SYM_CLASS_EXTERNAL
424  }
425  }
426 
427  Sym->MC = &MCSym;
428 }
429 
430 // Maximum offsets for different string table entry encodings.
431 enum : unsigned { Max7DecimalOffset = 9999999U };
432 enum : uint64_t { MaxBase64Offset = 0xFFFFFFFFFULL }; // 64^6, including 0
433 
434 // Encode a string table entry offset in base 64, padded to 6 chars, and
435 // prefixed with a double slash: '//AAAAAA', '//AAAAAB', ...
436 // Buffer must be at least 8 bytes large. No terminating null appended.
437 static void encodeBase64StringEntry(char *Buffer, uint64_t Value) {
439  "Illegal section name encoding for value");
440 
441  static const char Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
442  "abcdefghijklmnopqrstuvwxyz"
443  "0123456789+/";
444 
445  Buffer[0] = '/';
446  Buffer[1] = '/';
447 
448  char *Ptr = Buffer + 7;
449  for (unsigned i = 0; i < 6; ++i) {
450  unsigned Rem = Value % 64;
451  Value /= 64;
452  *(Ptr--) = Alphabet[Rem];
453  }
454 }
455 
456 void WinCOFFObjectWriter::SetSectionName(COFFSection &S) {
457  if (S.Name.size() <= COFF::NameSize) {
458  std::memcpy(S.Header.Name, S.Name.c_str(), S.Name.size());
459  return;
460  }
461 
462  uint64_t StringTableEntry = Strings.getOffset(S.Name);
463  if (StringTableEntry <= Max7DecimalOffset) {
465  Twine('/').concat(Twine(StringTableEntry)).toVector(Buffer);
466  assert(Buffer.size() <= COFF::NameSize && Buffer.size() >= 2);
467  std::memcpy(S.Header.Name, Buffer.data(), Buffer.size());
468  return;
469  }
470  if (StringTableEntry <= MaxBase64Offset) {
471  // Starting with 10,000,000, offsets are encoded as base64.
472  encodeBase64StringEntry(S.Header.Name, StringTableEntry);
473  return;
474  }
475  report_fatal_error("COFF string table is greater than 64 GB.");
476 }
477 
478 void WinCOFFObjectWriter::SetSymbolName(COFFSymbol &S) {
479  if (S.Name.size() > COFF::NameSize)
480  S.set_name_offset(Strings.getOffset(S.Name));
481  else
482  std::memcpy(S.Data.Name, S.Name.c_str(), S.Name.size());
483 }
484 
485 bool WinCOFFObjectWriter::IsPhysicalSection(COFFSection *S) {
486  return (S->Header.Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) ==
487  0;
488 }
489 
490 //------------------------------------------------------------------------------
491 // entity writing methods
492 
493 void WinCOFFObjectWriter::WriteFileHeader(const COFF::header &Header) {
494  if (UseBigObj) {
496  W.write<uint16_t>(0xFFFF);
497  W.write<uint16_t>(COFF::BigObjHeader::MinBigObjectVersion);
498  W.write<uint16_t>(Header.Machine);
499  W.write<uint32_t>(Header.TimeDateStamp);
500  W.OS.write(COFF::BigObjMagic, sizeof(COFF::BigObjMagic));
501  W.write<uint32_t>(0);
502  W.write<uint32_t>(0);
503  W.write<uint32_t>(0);
504  W.write<uint32_t>(0);
505  W.write<uint32_t>(Header.NumberOfSections);
506  W.write<uint32_t>(Header.PointerToSymbolTable);
507  W.write<uint32_t>(Header.NumberOfSymbols);
508  } else {
509  W.write<uint16_t>(Header.Machine);
510  W.write<uint16_t>(static_cast<int16_t>(Header.NumberOfSections));
511  W.write<uint32_t>(Header.TimeDateStamp);
512  W.write<uint32_t>(Header.PointerToSymbolTable);
513  W.write<uint32_t>(Header.NumberOfSymbols);
514  W.write<uint16_t>(Header.SizeOfOptionalHeader);
515  W.write<uint16_t>(Header.Characteristics);
516  }
517 }
518 
519 void WinCOFFObjectWriter::WriteSymbol(const COFFSymbol &S) {
520  W.OS.write(S.Data.Name, COFF::NameSize);
521  W.write<uint32_t>(S.Data.Value);
522  if (UseBigObj)
523  W.write<uint32_t>(S.Data.SectionNumber);
524  else
525  W.write<uint16_t>(static_cast<int16_t>(S.Data.SectionNumber));
526  W.write<uint16_t>(S.Data.Type);
527  W.OS << char(S.Data.StorageClass);
528  W.OS << char(S.Data.NumberOfAuxSymbols);
529  WriteAuxiliarySymbols(S.Aux);
530 }
531 
532 void WinCOFFObjectWriter::WriteAuxiliarySymbols(
534  for (const AuxSymbol &i : S) {
535  switch (i.AuxType) {
536  case ATWeakExternal:
537  W.write<uint32_t>(i.Aux.WeakExternal.TagIndex);
538  W.write<uint32_t>(i.Aux.WeakExternal.Characteristics);
539  W.OS.write_zeros(sizeof(i.Aux.WeakExternal.unused));
540  if (UseBigObj)
541  W.OS.write_zeros(COFF::Symbol32Size - COFF::Symbol16Size);
542  break;
543  case ATFile:
544  W.OS.write(reinterpret_cast<const char *>(&i.Aux),
545  UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size);
546  break;
547  case ATSectionDefinition:
548  W.write<uint32_t>(i.Aux.SectionDefinition.Length);
549  W.write<uint16_t>(i.Aux.SectionDefinition.NumberOfRelocations);
550  W.write<uint16_t>(i.Aux.SectionDefinition.NumberOfLinenumbers);
551  W.write<uint32_t>(i.Aux.SectionDefinition.CheckSum);
552  W.write<uint16_t>(static_cast<int16_t>(i.Aux.SectionDefinition.Number));
553  W.OS << char(i.Aux.SectionDefinition.Selection);
554  W.OS.write_zeros(sizeof(i.Aux.SectionDefinition.unused));
555  W.write<uint16_t>(static_cast<int16_t>(i.Aux.SectionDefinition.Number >> 16));
556  if (UseBigObj)
557  W.OS.write_zeros(COFF::Symbol32Size - COFF::Symbol16Size);
558  break;
559  }
560  }
561 }
562 
563 // Write the section header.
564 void WinCOFFObjectWriter::writeSectionHeaders() {
565  // Section numbers must be monotonically increasing in the section
566  // header, but our Sections array is not sorted by section number,
567  // so make a copy of Sections and sort it.
568  std::vector<COFFSection *> Arr;
569  for (auto &Section : Sections)
570  Arr.push_back(Section.get());
571  llvm::sort(Arr, [](const COFFSection *A, const COFFSection *B) {
572  return A->Number < B->Number;
573  });
574 
575  for (auto &Section : Arr) {
576  if (Section->Number == -1)
577  continue;
578 
579  COFF::section &S = Section->Header;
580  if (Section->Relocations.size() >= 0xffff)
581  S.Characteristics |= COFF::IMAGE_SCN_LNK_NRELOC_OVFL;
582  W.OS.write(S.Name, COFF::NameSize);
583  W.write<uint32_t>(S.VirtualSize);
584  W.write<uint32_t>(S.VirtualAddress);
585  W.write<uint32_t>(S.SizeOfRawData);
586  W.write<uint32_t>(S.PointerToRawData);
587  W.write<uint32_t>(S.PointerToRelocations);
588  W.write<uint32_t>(S.PointerToLineNumbers);
589  W.write<uint16_t>(S.NumberOfRelocations);
590  W.write<uint16_t>(S.NumberOfLineNumbers);
591  W.write<uint32_t>(S.Characteristics);
592  }
593 }
594 
595 void WinCOFFObjectWriter::WriteRelocation(const COFF::relocation &R) {
596  W.write<uint32_t>(R.VirtualAddress);
597  W.write<uint32_t>(R.SymbolTableIndex);
598  W.write<uint16_t>(R.Type);
599 }
600 
601 // Write MCSec's contents. What this function does is essentially
602 // "Asm.writeSectionData(&MCSec, Layout)", but it's a bit complicated
603 // because it needs to compute a CRC.
604 uint32_t WinCOFFObjectWriter::writeSectionContents(MCAssembler &Asm,
605  const MCAsmLayout &Layout,
606  const MCSection &MCSec) {
607  // Save the contents of the section to a temporary buffer, we need this
608  // to CRC the data before we dump it into the object file.
610  raw_svector_ostream VecOS(Buf);
611  Asm.writeSectionData(VecOS, &MCSec, Layout);
612 
613  // Write the section contents to the object file.
614  W.OS << Buf;
615 
616  // Calculate our CRC with an initial value of '0', this is not how
617  // JamCRC is specified but it aligns with the expected output.
618  JamCRC JC(/*Init=*/0);
619  JC.update(makeArrayRef(reinterpret_cast<uint8_t*>(Buf.data()), Buf.size()));
620  return JC.getCRC();
621 }
622 
623 void WinCOFFObjectWriter::writeSection(MCAssembler &Asm,
624  const MCAsmLayout &Layout,
625  const COFFSection &Sec,
626  const MCSection &MCSec) {
627  if (Sec.Number == -1)
628  return;
629 
630  // Write the section contents.
631  if (Sec.Header.PointerToRawData != 0) {
632  assert(W.OS.tell() == Sec.Header.PointerToRawData &&
633  "Section::PointerToRawData is insane!");
634 
635  uint32_t CRC = writeSectionContents(Asm, Layout, MCSec);
636 
637  // Update the section definition auxiliary symbol to record the CRC.
638  COFFSection *Sec = SectionMap[&MCSec];
639  COFFSymbol::AuxiliarySymbols &AuxSyms = Sec->Symbol->Aux;
640  assert(AuxSyms.size() == 1 && AuxSyms[0].AuxType == ATSectionDefinition);
641  AuxSymbol &SecDef = AuxSyms[0];
642  SecDef.Aux.SectionDefinition.CheckSum = CRC;
643  }
644 
645  // Write relocations for this section.
646  if (Sec.Relocations.empty()) {
647  assert(Sec.Header.PointerToRelocations == 0 &&
648  "Section::PointerToRelocations is insane!");
649  return;
650  }
651 
652  assert(W.OS.tell() == Sec.Header.PointerToRelocations &&
653  "Section::PointerToRelocations is insane!");
654 
655  if (Sec.Relocations.size() >= 0xffff) {
656  // In case of overflow, write actual relocation count as first
657  // relocation. Including the synthetic reloc itself (+ 1).
659  R.VirtualAddress = Sec.Relocations.size() + 1;
660  R.SymbolTableIndex = 0;
661  R.Type = 0;
662  WriteRelocation(R);
663  }
664 
665  for (const auto &Relocation : Sec.Relocations)
666  WriteRelocation(Relocation.Data);
667 }
668 
669 ////////////////////////////////////////////////////////////////////////////////
670 // MCObjectWriter interface implementations
671 
672 void WinCOFFObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
673  const MCAsmLayout &Layout) {
674  if (EmitAddrsigSection) {
675  AddrsigSection = Asm.getContext().getCOFFSection(
676  ".llvm_addrsig", COFF::IMAGE_SCN_LNK_REMOVE,
677  SectionKind::getMetadata());
678  Asm.registerSection(*AddrsigSection);
679  }
680 
681  if (!Asm.CGProfile.empty()) {
682  CGProfileSection = Asm.getContext().getCOFFSection(
683  ".llvm.call-graph-profile", COFF::IMAGE_SCN_LNK_REMOVE,
684  SectionKind::getMetadata());
685  Asm.registerSection(*CGProfileSection);
686  }
687 
688  // "Define" each section & symbol. This creates section & symbol
689  // entries in the staging area.
690  for (const auto &Section : Asm)
691  defineSection(static_cast<const MCSectionCOFF &>(Section));
692 
693  for (const MCSymbol &Symbol : Asm.symbols())
694  if (!Symbol.isTemporary())
695  DefineSymbol(Symbol, Asm, Layout);
696 }
697 
698 bool WinCOFFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
699  const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB,
700  bool InSet, bool IsPCRel) const {
701  // Don't drop relocations between functions, even if they are in the same text
702  // section. Multiple Visual C++ linker features depend on having the
703  // relocations present. The /INCREMENTAL flag will cause these relocations to
704  // point to thunks, and the /GUARD:CF flag assumes that it can use relocations
705  // to approximate the set of all address taken functions. LLD's implementation
706  // of /GUARD:CF also relies on the existance of these relocations.
707  uint16_t Type = cast<MCSymbolCOFF>(SymA).getType();
709  return false;
710  return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB,
711  InSet, IsPCRel);
712 }
713 
714 void WinCOFFObjectWriter::recordRelocation(MCAssembler &Asm,
715  const MCAsmLayout &Layout,
716  const MCFragment *Fragment,
717  const MCFixup &Fixup, MCValue Target,
718  uint64_t &FixedValue) {
719  assert(Target.getSymA() && "Relocation must reference a symbol!");
720 
721  const MCSymbol &A = Target.getSymA()->getSymbol();
722  if (!A.isRegistered()) {
723  Asm.getContext().reportError(Fixup.getLoc(),
724  Twine("symbol '") + A.getName() +
725  "' can not be undefined");
726  return;
727  }
728  if (A.isTemporary() && A.isUndefined()) {
729  Asm.getContext().reportError(Fixup.getLoc(),
730  Twine("assembler label '") + A.getName() +
731  "' can not be undefined");
732  return;
733  }
734 
735  MCSection *MCSec = Fragment->getParent();
736 
737  // Mark this symbol as requiring an entry in the symbol table.
738  assert(SectionMap.find(MCSec) != SectionMap.end() &&
739  "Section must already have been defined in executePostLayoutBinding!");
740 
741  COFFSection *Sec = SectionMap[MCSec];
742  const MCSymbolRefExpr *SymB = Target.getSymB();
743 
744  if (SymB) {
745  const MCSymbol *B = &SymB->getSymbol();
746  if (!B->getFragment()) {
747  Asm.getContext().reportError(
748  Fixup.getLoc(),
749  Twine("symbol '") + B->getName() +
750  "' can not be undefined in a subtraction expression");
751  return;
752  }
753 
754  // Offset of the symbol in the section
755  int64_t OffsetOfB = Layout.getSymbolOffset(*B);
756 
757  // Offset of the relocation in the section
758  int64_t OffsetOfRelocation =
759  Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
760 
761  FixedValue = (OffsetOfRelocation - OffsetOfB) + Target.getConstant();
762  } else {
763  FixedValue = Target.getConstant();
764  }
765 
766  COFFRelocation Reloc;
767 
768  Reloc.Data.SymbolTableIndex = 0;
769  Reloc.Data.VirtualAddress = Layout.getFragmentOffset(Fragment);
770 
771  // Turn relocations for temporary symbols into section relocations.
772  if (A.isTemporary()) {
773  MCSection *TargetSection = &A.getSection();
774  assert(
775  SectionMap.find(TargetSection) != SectionMap.end() &&
776  "Section must already have been defined in executePostLayoutBinding!");
777  Reloc.Symb = SectionMap[TargetSection]->Symbol;
778  FixedValue += Layout.getSymbolOffset(A);
779  } else {
780  assert(
781  SymbolMap.find(&A) != SymbolMap.end() &&
782  "Symbol must already have been defined in executePostLayoutBinding!");
783  Reloc.Symb = SymbolMap[&A];
784  }
785 
786  ++Reloc.Symb->Relocations;
787 
788  Reloc.Data.VirtualAddress += Fixup.getOffset();
789  Reloc.Data.Type = TargetObjectWriter->getRelocType(
790  Asm.getContext(), Target, Fixup, SymB, Asm.getBackend());
791 
792  // The *_REL32 relocations are relative to the end of the relocation,
793  // not to the start.
794  if ((Header.Machine == COFF::IMAGE_FILE_MACHINE_AMD64 &&
795  Reloc.Data.Type == COFF::IMAGE_REL_AMD64_REL32) ||
797  Reloc.Data.Type == COFF::IMAGE_REL_I386_REL32) ||
799  Reloc.Data.Type == COFF::IMAGE_REL_ARM_REL32) ||
801  Reloc.Data.Type == COFF::IMAGE_REL_ARM64_REL32))
802  FixedValue += 4;
803 
804  if (Header.Machine == COFF::IMAGE_FILE_MACHINE_ARMNT) {
805  switch (Reloc.Data.Type) {
812  break;
815  // IMAGE_REL_ARM_BRANCH11 and IMAGE_REL_ARM_BLX11 are only used for
816  // pre-ARMv7, which implicitly rules it out of ARMNT (it would be valid
817  // for Windows CE).
821  // IMAGE_REL_ARM_BRANCH24, IMAGE_REL_ARM_BLX24, IMAGE_REL_ARM_MOV32A are
822  // only used for ARM mode code, which is documented as being unsupported
823  // by Windows on ARM. Empirical proof indicates that masm is able to
824  // generate the relocations however the rest of the MSVC toolchain is
825  // unable to handle it.
826  llvm_unreachable("unsupported relocation");
827  break;
829  break;
833  // IMAGE_REL_BRANCH20T, IMAGE_REL_ARM_BRANCH24T, IMAGE_REL_ARM_BLX23T all
834  // perform a 4 byte adjustment to the relocation. Relative branches are
835  // offset by 4 on ARM, however, because there is no RELA relocations, all
836  // branches are offset by 4.
837  FixedValue = FixedValue + 4;
838  break;
839  }
840  }
841 
842  // The fixed value never makes sense for section indices, ignore it.
843  if (Fixup.getKind() == FK_SecRel_2)
844  FixedValue = 0;
845 
846  if (TargetObjectWriter->recordRelocation(Fixup))
847  Sec->Relocations.push_back(Reloc);
848 }
849 
850 static std::time_t getTime() {
851  std::time_t Now = time(nullptr);
852  if (Now < 0 || !isUInt<32>(Now))
853  return UINT32_MAX;
854  return Now;
855 }
856 
857 // Create .file symbols.
858 void WinCOFFObjectWriter::createFileSymbols(MCAssembler &Asm) {
859  for (const std::pair<std::string, size_t> &It : Asm.getFileNames()) {
860  // round up to calculate the number of auxiliary symbols required
861  const std::string &Name = It.first;
862  unsigned SymbolSize = UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size;
863  unsigned Count = (Name.size() + SymbolSize - 1) / SymbolSize;
864 
865  COFFSymbol *File = createSymbol(".file");
866  File->Data.SectionNumber = COFF::IMAGE_SYM_DEBUG;
867  File->Data.StorageClass = COFF::IMAGE_SYM_CLASS_FILE;
868  File->Aux.resize(Count);
869 
870  unsigned Offset = 0;
871  unsigned Length = Name.size();
872  for (auto &Aux : File->Aux) {
873  Aux.AuxType = ATFile;
874 
875  if (Length > SymbolSize) {
876  memcpy(&Aux.Aux, Name.c_str() + Offset, SymbolSize);
877  Length = Length - SymbolSize;
878  } else {
879  memcpy(&Aux.Aux, Name.c_str() + Offset, Length);
880  memset((char *)&Aux.Aux + Length, 0, SymbolSize - Length);
881  break;
882  }
883 
884  Offset += SymbolSize;
885  }
886  }
887 }
888 
889 void WinCOFFObjectWriter::setWeakDefaultNames() {
890  if (WeakDefaults.empty())
891  return;
892 
893  // If multiple object files use a weak symbol (either with a regular
894  // defined default, or an absolute zero symbol as default), the defaults
895  // cause duplicate definitions unless their names are made unique. Look
896  // for a defined extern symbol, that isn't comdat - that should be unique
897  // unless there are other duplicate definitions. And if none is found,
898  // allow picking a comdat symbol, as that's still better than nothing.
899 
900  COFFSymbol *Unique = nullptr;
901  for (bool AllowComdat : {false, true}) {
902  for (auto &Sym : Symbols) {
903  // Don't include the names of the defaults themselves
904  if (WeakDefaults.count(Sym.get()))
905  continue;
906  // Only consider external symbols
907  if (Sym->Data.StorageClass != COFF::IMAGE_SYM_CLASS_EXTERNAL)
908  continue;
909  // Only consider symbols defined in a section or that are absolute
910  if (!Sym->Section && Sym->Data.SectionNumber != COFF::IMAGE_SYM_ABSOLUTE)
911  continue;
912  if (!AllowComdat && Sym->Section &&
913  Sym->Section->Header.Characteristics & COFF::IMAGE_SCN_LNK_COMDAT)
914  continue;
915  Unique = Sym.get();
916  break;
917  }
918  if (Unique)
919  break;
920  }
921  // If we didn't find any unique symbol to use for the names, just skip this.
922  if (!Unique)
923  return;
924  for (auto *Sym : WeakDefaults) {
925  Sym->Name.append(".");
926  Sym->Name.append(Unique->Name);
927  }
928 }
929 
930 static bool isAssociative(const COFFSection &Section) {
931  return Section.Symbol->Aux[0].Aux.SectionDefinition.Selection ==
933 }
934 
935 void WinCOFFObjectWriter::assignSectionNumbers() {
936  size_t I = 1;
937  auto Assign = [&](COFFSection &Section) {
938  Section.Number = I;
939  Section.Symbol->Data.SectionNumber = I;
940  Section.Symbol->Aux[0].Aux.SectionDefinition.Number = I;
941  ++I;
942  };
943 
944  // Although it is not explicitly requested by the Microsoft COFF spec,
945  // we should avoid emitting forward associative section references,
946  // because MSVC link.exe as of 2017 cannot handle that.
947  for (const std::unique_ptr<COFFSection> &Section : Sections)
948  if (!isAssociative(*Section))
949  Assign(*Section);
950  for (const std::unique_ptr<COFFSection> &Section : Sections)
951  if (isAssociative(*Section))
952  Assign(*Section);
953 }
954 
955 // Assign file offsets to COFF object file structures.
956 void WinCOFFObjectWriter::assignFileOffsets(MCAssembler &Asm,
957  const MCAsmLayout &Layout) {
958  unsigned Offset = W.OS.tell();
959 
962 
963  for (const auto &Section : Asm) {
964  COFFSection *Sec = SectionMap[&Section];
965 
966  if (Sec->Number == -1)
967  continue;
968 
969  Sec->Header.SizeOfRawData = Layout.getSectionAddressSize(&Section);
970 
971  if (IsPhysicalSection(Sec)) {
972  Sec->Header.PointerToRawData = Offset;
973  Offset += Sec->Header.SizeOfRawData;
974  }
975 
976  if (!Sec->Relocations.empty()) {
977  bool RelocationsOverflow = Sec->Relocations.size() >= 0xffff;
978 
979  if (RelocationsOverflow) {
980  // Signal overflow by setting NumberOfRelocations to max value. Actual
981  // size is found in reloc #0. Microsoft tools understand this.
982  Sec->Header.NumberOfRelocations = 0xffff;
983  } else {
984  Sec->Header.NumberOfRelocations = Sec->Relocations.size();
985  }
986  Sec->Header.PointerToRelocations = Offset;
987 
988  if (RelocationsOverflow) {
989  // Reloc #0 will contain actual count, so make room for it.
991  }
992 
993  Offset += COFF::RelocationSize * Sec->Relocations.size();
994 
995  for (auto &Relocation : Sec->Relocations) {
996  assert(Relocation.Symb->getIndex() != -1);
997  Relocation.Data.SymbolTableIndex = Relocation.Symb->getIndex();
998  }
999  }
1000 
1001  assert(Sec->Symbol->Aux.size() == 1 &&
1002  "Section's symbol must have one aux!");
1003  AuxSymbol &Aux = Sec->Symbol->Aux[0];
1004  assert(Aux.AuxType == ATSectionDefinition &&
1005  "Section's symbol's aux symbol must be a Section Definition!");
1006  Aux.Aux.SectionDefinition.Length = Sec->Header.SizeOfRawData;
1007  Aux.Aux.SectionDefinition.NumberOfRelocations =
1008  Sec->Header.NumberOfRelocations;
1009  Aux.Aux.SectionDefinition.NumberOfLinenumbers =
1010  Sec->Header.NumberOfLineNumbers;
1011  }
1012 
1013  Header.PointerToSymbolTable = Offset;
1014 }
1015 
1016 uint64_t WinCOFFObjectWriter::writeObject(MCAssembler &Asm,
1017  const MCAsmLayout &Layout) {
1018  uint64_t StartOffset = W.OS.tell();
1019 
1020  if (Sections.size() > INT32_MAX)
1022  "PE COFF object files can't have more than 2147483647 sections");
1023 
1024  UseBigObj = Sections.size() > COFF::MaxNumberOfSections16;
1025  Header.NumberOfSections = Sections.size();
1026  Header.NumberOfSymbols = 0;
1027 
1028  setWeakDefaultNames();
1029  assignSectionNumbers();
1030  createFileSymbols(Asm);
1031 
1032  for (auto &Symbol : Symbols) {
1033  // Update section number & offset for symbols that have them.
1034  if (Symbol->Section)
1035  Symbol->Data.SectionNumber = Symbol->Section->Number;
1036  Symbol->setIndex(Header.NumberOfSymbols++);
1037  // Update auxiliary symbol info.
1038  Symbol->Data.NumberOfAuxSymbols = Symbol->Aux.size();
1039  Header.NumberOfSymbols += Symbol->Data.NumberOfAuxSymbols;
1040  }
1041 
1042  // Build string table.
1043  for (const auto &S : Sections)
1044  if (S->Name.size() > COFF::NameSize)
1045  Strings.add(S->Name);
1046  for (const auto &S : Symbols)
1047  if (S->Name.size() > COFF::NameSize)
1048  Strings.add(S->Name);
1049  Strings.finalize();
1050 
1051  // Set names.
1052  for (const auto &S : Sections)
1053  SetSectionName(*S);
1054  for (auto &S : Symbols)
1055  SetSymbolName(*S);
1056 
1057  // Fixup weak external references.
1058  for (auto &Symbol : Symbols) {
1059  if (Symbol->Other) {
1060  assert(Symbol->getIndex() != -1);
1061  assert(Symbol->Aux.size() == 1 && "Symbol must contain one aux symbol!");
1062  assert(Symbol->Aux[0].AuxType == ATWeakExternal &&
1063  "Symbol's aux symbol must be a Weak External!");
1064  Symbol->Aux[0].Aux.WeakExternal.TagIndex = Symbol->Other->getIndex();
1065  }
1066  }
1067 
1068  // Fixup associative COMDAT sections.
1069  for (auto &Section : Sections) {
1070  if (Section->Symbol->Aux[0].Aux.SectionDefinition.Selection !=
1072  continue;
1073 
1074  const MCSectionCOFF &MCSec = *Section->MCSection;
1075  const MCSymbol *AssocMCSym = MCSec.getCOMDATSymbol();
1076  assert(AssocMCSym);
1077 
1078  // It's an error to try to associate with an undefined symbol or a symbol
1079  // without a section.
1080  if (!AssocMCSym->isInSection()) {
1081  Asm.getContext().reportError(
1082  SMLoc(), Twine("cannot make section ") + MCSec.getName() +
1083  Twine(" associative with sectionless symbol ") +
1084  AssocMCSym->getName());
1085  continue;
1086  }
1087 
1088  const auto *AssocMCSec = cast<MCSectionCOFF>(&AssocMCSym->getSection());
1089  assert(SectionMap.count(AssocMCSec));
1090  COFFSection *AssocSec = SectionMap[AssocMCSec];
1091 
1092  // Skip this section if the associated section is unused.
1093  if (AssocSec->Number == -1)
1094  continue;
1095 
1096  Section->Symbol->Aux[0].Aux.SectionDefinition.Number = AssocSec->Number;
1097  }
1098 
1099  // Create the contents of the .llvm_addrsig section.
1100  if (EmitAddrsigSection) {
1101  auto Frag = new MCDataFragment(AddrsigSection);
1102  Frag->setLayoutOrder(0);
1103  raw_svector_ostream OS(Frag->getContents());
1104  for (const MCSymbol *S : AddrsigSyms) {
1105  if (!S->isTemporary()) {
1106  encodeULEB128(S->getIndex(), OS);
1107  continue;
1108  }
1109 
1110  MCSection *TargetSection = &S->getSection();
1111  assert(SectionMap.find(TargetSection) != SectionMap.end() &&
1112  "Section must already have been defined in "
1113  "executePostLayoutBinding!");
1114  encodeULEB128(SectionMap[TargetSection]->Symbol->getIndex(), OS);
1115  }
1116  }
1117 
1118  // Create the contents of the .llvm.call-graph-profile section.
1119  if (CGProfileSection) {
1120  auto *Frag = new MCDataFragment(CGProfileSection);
1121  Frag->setLayoutOrder(0);
1122  raw_svector_ostream OS(Frag->getContents());
1123  for (const MCAssembler::CGProfileEntry &CGPE : Asm.CGProfile) {
1124  uint32_t FromIndex = CGPE.From->getSymbol().getIndex();
1125  uint32_t ToIndex = CGPE.To->getSymbol().getIndex();
1126  support::endian::write(OS, FromIndex, W.Endian);
1127  support::endian::write(OS, ToIndex, W.Endian);
1128  support::endian::write(OS, CGPE.Count, W.Endian);
1129  }
1130  }
1131 
1132  assignFileOffsets(Asm, Layout);
1133 
1134  // MS LINK expects to be able to use this timestamp to implement their
1135  // /INCREMENTAL feature.
1136  if (Asm.isIncrementalLinkerCompatible()) {
1137  Header.TimeDateStamp = getTime();
1138  } else {
1139  // Have deterministic output if /INCREMENTAL isn't needed. Also matches GNU.
1140  Header.TimeDateStamp = 0;
1141  }
1142 
1143  // Write it all to disk...
1144  WriteFileHeader(Header);
1145  writeSectionHeaders();
1146 
1147  // Write section contents.
1148  sections::iterator I = Sections.begin();
1149  sections::iterator IE = Sections.end();
1150  MCAssembler::iterator J = Asm.begin();
1151  MCAssembler::iterator JE = Asm.end();
1152  for (; I != IE && J != JE; ++I, ++J)
1153  writeSection(Asm, Layout, **I, *J);
1154 
1155  assert(W.OS.tell() == Header.PointerToSymbolTable &&
1156  "Header::PointerToSymbolTable is insane!");
1157 
1158  // Write a symbol table.
1159  for (auto &Symbol : Symbols)
1160  if (Symbol->getIndex() != -1)
1161  WriteSymbol(*Symbol);
1162 
1163  // Write a string table, which completes the entire COFF file.
1164  Strings.write(W.OS);
1165 
1166  return W.OS.tell() - StartOffset;
1167 }
1168 
1169 MCWinCOFFObjectTargetWriter::MCWinCOFFObjectTargetWriter(unsigned Machine_)
1170  : Machine(Machine_) {}
1171 
1172 // Pin the vtable to this file.
1173 void MCWinCOFFObjectTargetWriter::anchor() {}
1174 
1175 //------------------------------------------------------------------------------
1176 // WinCOFFObjectWriter factory function
1177 
1178 std::unique_ptr<MCObjectWriter> llvm::createWinCOFFObjectWriter(
1179  std::unique_ptr<MCWinCOFFObjectTargetWriter> MOTW, raw_pwrite_stream &OS) {
1180  return std::make_unique<WinCOFFObjectWriter>(std::move(MOTW), OS);
1181 }
i
i
Definition: README.txt:29
llvm::COFF::IMAGE_SCN_ALIGN_64BYTES
@ IMAGE_SCN_ALIGN_64BYTES
Definition: COFF.h:306
getAlignment
static uint32_t getAlignment(const MCSectionCOFF &Sec)
Definition: WinCOFFObjectWriter.cpp:266
llvm::COFF::IMAGE_SCN_ALIGN_1BYTES
@ IMAGE_SCN_ALIGN_1BYTES
Definition: COFF.h:300
llvm::COFF::IMAGE_SCN_ALIGN_2048BYTES
@ IMAGE_SCN_ALIGN_2048BYTES
Definition: COFF.h:311
llvm::MCSectionCOFF::getSelection
int getSelection() const
Definition: MCSectionCOFF.h:68
MathExtras.h
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
llvm::COFF::IMAGE_SCN_LNK_NRELOC_OVFL
@ IMAGE_SCN_LNK_NRELOC_OVFL
Definition: COFF.h:315
llvm::COFF::IMAGE_SCN_ALIGN_16BYTES
@ IMAGE_SCN_ALIGN_16BYTES
Definition: COFF.h:304
llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA
@ IMAGE_SCN_CNT_UNINITIALIZED_DATA
Definition: COFF.h:290
llvm::MCSymbol
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
llvm::COFF::IMAGE_SCN_ALIGN_4BYTES
@ IMAGE_SCN_ALIGN_4BYTES
Definition: COFF.h:302
llvm::COFF::header::Machine
uint16_t Machine
Definition: COFF.h:66
llvm::COFF::Symbol32Size
@ Symbol32Size
Definition: COFF.h:60
llvm::MCSymbol::isExternal
bool isExternal() const
Definition: MCSymbol.h:401
llvm::COFF::IMAGE_SYM_CLASS_STATIC
@ IMAGE_SYM_CLASS_STATIC
Static.
Definition: COFF.h:210
llvm::COFF::IMAGE_SCN_ALIGN_256BYTES
@ IMAGE_SCN_ALIGN_256BYTES
Definition: COFF.h:308
StringRef.h
llvm::Target
Target - Wrapper for Target specific information.
Definition: TargetRegistry.h:137
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1168
llvm::RISCVFenceField::W
@ W
Definition: RISCVBaseInfo.h:199
llvm::support::endian::write32le
void write32le(void *P, uint32_t V)
Definition: Endian.h:416
llvm::MCSymbolCOFF::getClass
uint16_t getClass() const
Definition: MCSymbolCOFF.h:40
llvm::COFF::IMAGE_SYM_CLASS_EXTERNAL
@ IMAGE_SYM_CLASS_EXTERNAL
External symbol.
Definition: COFF.h:209
ErrorHandling.h
MCSectionCOFF.h
MCFragment.h
llvm::COFF::IMAGE_REL_ARM_MOV32A
@ IMAGE_REL_ARM_MOV32A
Definition: COFF.h:377
llvm::COFF::IMAGE_FILE_MACHINE_AMD64
@ IMAGE_FILE_MACHINE_AMD64
Definition: COFF.h:98
llvm::COFF::IMAGE_FILE_MACHINE_UNKNOWN
@ IMAGE_FILE_MACHINE_UNKNOWN
Definition: COFF.h:96
llvm::MCSymbol::isUndefined
bool isUndefined(bool SetUsed=true) const
isUndefined - Check if this symbol undefined (i.e., implicitly defined).
Definition: MCSymbol.h:252
llvm::COFF::SCT_COMPLEX_TYPE_SHIFT
@ SCT_COMPLEX_TYPE_SHIFT
Type is formed as (base + (derived << SCT_COMPLEX_TYPE_SHIFT))
Definition: COFF.h:265
MCAssembler.h
COFF.h
llvm::COFF::NameSize
@ NameSize
Definition: COFF.h:58
llvm::MCSymbol::getFragment
MCFragment * getFragment(bool SetUsed=true) const
Definition: MCSymbol.h:392
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
DenseMap.h
llvm::COFF::header::Characteristics
uint16_t Characteristics
Definition: COFF.h:72
llvm::COFF::IMAGE_REL_AMD64_REL32
@ IMAGE_REL_AMD64_REL32
Definition: COFF.h:350
llvm::COFF::Auxiliary
Definition: COFF.h:463
llvm::COFF::IMAGE_FILE_MACHINE_ARMNT
@ IMAGE_FILE_MACHINE_ARMNT
Definition: COFF.h:100
llvm::MCFragment::getParent
MCSection * getParent() const
Definition: MCFragment.h:96
llvm::COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE
@ IMAGE_COMDAT_SELECT_ASSOCIATIVE
Definition: COFF.h:411
llvm::MCAssembler::CGProfileEntry::From
const MCSymbolRefExpr * From
Definition: MCAssembler.h:437
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:81
llvm::JamCRC
Definition: CRC.h:45
llvm::COFF::IMAGE_SCN_ALIGN_512BYTES
@ IMAGE_SCN_ALIGN_512BYTES
Definition: COFF.h:309
llvm::COFF::Header16Size
@ Header16Size
Definition: COFF.h:56
llvm::MipsISD::Ret
@ Ret
Definition: MipsISelLowering.h:116
STLExtras.h
llvm::COFF::IMAGE_REL_ARM_BRANCH20T
@ IMAGE_REL_ARM_BRANCH20T
Definition: COFF.h:379
llvm::COFF::IMAGE_WEAK_EXTERN_SEARCH_ALIAS
@ IMAGE_WEAK_EXTERN_SEARCH_ALIAS
Definition: COFF.h:442
llvm::support::endian::Writer
Adapter to write values to a stream in a particular byte order.
Definition: EndianStream.h:52
llvm::COFF::IMAGE_SYM_ABSOLUTE
@ IMAGE_SYM_ABSOLUTE
Definition: COFF.h:198
llvm::Data
@ Data
Definition: SIMachineScheduler.h:55
llvm::COFF::header::PointerToSymbolTable
uint32_t PointerToSymbolTable
Definition: COFF.h:69
llvm::RISCVFenceField::R
@ R
Definition: RISCVBaseInfo.h:198
llvm::COFF::Symbol16Size
@ Symbol16Size
Definition: COFF.h:59
llvm::createWinCOFFObjectWriter
std::unique_ptr< MCObjectWriter > createWinCOFFObjectWriter(std::unique_ptr< MCWinCOFFObjectTargetWriter > MOTW, raw_pwrite_stream &OS)
Construct a new Win COFF writer instance.
Definition: WinCOFFObjectWriter.cpp:1178
llvm::ARMBuildAttrs::Section
@ Section
Legacy Tags.
Definition: ARMBuildAttributes.h:78
llvm::MCFragment
Definition: MCFragment.h:31
llvm::COFF::IMAGE_SYM_CLASS_NULL
@ IMAGE_SYM_CLASS_NULL
No symbol.
Definition: COFF.h:207
llvm::COFF::IMAGE_REL_ARM_SECREL
@ IMAGE_REL_ARM_SECREL
Definition: COFF.h:376
llvm::COFF::IMAGE_FILE_MACHINE_I386
@ IMAGE_FILE_MACHINE_I386
Definition: COFF.h:103
StringTableBuilder.h
llvm::ms_demangle::IntrinsicFunctionKind::Assign
@ Assign
llvm::COFF::IMAGE_SYM_CLASS_FILE
@ IMAGE_SYM_CLASS_FILE
File name.
Definition: COFF.h:231
llvm::MCSymbol::getSection
MCSection & getSection() const
Get the section associated with a defined, non-absolute symbol.
Definition: MCSymbol.h:262
llvm::COFF::IMAGE_REL_ARM_MOV32T
@ IMAGE_REL_ARM_MOV32T
Definition: COFF.h:378
llvm::SMLoc
Represents a location in source code.
Definition: SMLoc.h:23
llvm::COFF::IMAGE_SCN_LNK_REMOVE
@ IMAGE_SCN_LNK_REMOVE
Definition: COFF.h:293
SmallString.h
llvm::support::little
@ little
Definition: Endian.h:27
Twine.h
llvm::MCAssembler::CGProfileEntry::Count
uint64_t Count
Definition: MCAssembler.h:439
MCContext.h
MCSymbol.h
CRC.h
DenseSet.h
llvm::FK_SecRel_2
@ FK_SecRel_2
A two-byte section relative fixup.
Definition: MCFixup.h:41
llvm::orc::SymbolMap
DenseMap< SymbolStringPtr, JITEvaluatedSymbol > SymbolMap
A map from symbol names (as SymbolStringPtrs) to JITSymbols (address/flags pairs).
Definition: Core.h:113
B
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
llvm::COFF::IMAGE_REL_ARM_ADDR32NB
@ IMAGE_REL_ARM_ADDR32NB
Definition: COFF.h:368
llvm::MCSymbolCOFF
Definition: MCSymbolCOFF.h:17
llvm::COFF::IMAGE_REL_ARM_TOKEN
@ IMAGE_REL_ARM_TOKEN
Definition: COFF.h:371
llvm::StringTableBuilder::WinCOFF
@ WinCOFF
Definition: StringTableBuilder.h:27
llvm::report_fatal_error
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:140
llvm::MCSymbol::setIndex
void setIndex(uint32_t Value) const
Set the (implementation defined) index.
Definition: MCSymbol.h:314
llvm::MCSymbolRefExpr::getSymbol
const MCSymbol & getSymbol() const
Definition: MCExpr.h:398
llvm::COFF::IMAGE_SCN_ALIGN_32BYTES
@ IMAGE_SCN_ALIGN_32BYTES
Definition: COFF.h:305
llvm::MCSymbol::getName
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:198
llvm::COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL
@ IMAGE_SYM_CLASS_WEAK_EXTERNAL
Duplicate tag.
Definition: COFF.h:234
llvm::MCSymbol::getIndex
uint32_t getIndex() const
Get the (implementation defined) index.
Definition: MCSymbol.h:309
llvm::SmallString
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:25
llvm::ARM_PROC::IE
@ IE
Definition: ARMBaseInfo.h:27
llvm::COFF::symbol
Definition: COFF.h:187
llvm::MCSectionCOFF::getCharacteristics
unsigned getCharacteristics() const
Definition: MCSectionCOFF.h:66
llvm::DenseSet
Implements a dense probed hash-table based set.
Definition: DenseSet.h:268
llvm::COFF::relocation
Definition: COFF.h:325
llvm::MCAssembler
Definition: MCAssembler.h:60
llvm::COFF::IMAGE_SCN_ALIGN_8192BYTES
@ IMAGE_SCN_ALIGN_8192BYTES
Definition: COFF.h:313
llvm::COFF::IMAGE_REL_I386_REL32
@ IMAGE_REL_I386_REL32
Definition: COFF.h:342
llvm::COFF::IMAGE_SCN_ALIGN_2BYTES
@ IMAGE_SCN_ALIGN_2BYTES
Definition: COFF.h:301
llvm::DenseMapBase::clear
void clear()
Definition: DenseMap.h:111
Index
uint32_t Index
Definition: ELFObjHandler.cpp:84
uint64_t
llvm::raw_pwrite_stream
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:417
llvm::MCAsmLayout::getBaseSymbol
const MCSymbol * getBaseSymbol(const MCSymbol &Symbol) const
If this symbol is equivalent to A + Constant, return A.
Definition: MCFragment.cpp:162
llvm::COFF::IMAGE_REL_ARM_BRANCH24T
@ IMAGE_REL_ARM_BRANCH24T
Definition: COFF.h:380
llvm::MCSectionCOFF::getCOMDATSymbol
MCSymbol * getCOMDATSymbol() const
Definition: MCSectionCOFF.h:67
llvm::COFF::IMAGE_REL_ARM_BRANCH24
@ IMAGE_REL_ARM_BRANCH24
Definition: COFF.h:369
llvm::COFF::IMAGE_REL_ARM_BRANCH11
@ IMAGE_REL_ARM_BRANCH11
Definition: COFF.h:370
LEB128.h
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::Twine::toVector
void toVector(SmallVectorImpl< char > &Out) const
Append the concatenated string into the given SmallString or SmallVector.
Definition: Twine.cpp:32
llvm::DenseMap
Definition: DenseMap.h:714
llvm::COFF::header::TimeDateStamp
uint32_t TimeDateStamp
Definition: COFF.h:68
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::MCSymbol::isVariable
bool isVariable() const
isVariable - Check if this is a variable symbol.
Definition: MCSymbol.h:293
llvm::isUInt< 32 >
constexpr bool isUInt< 32 >(uint64_t x)
Definition: MathExtras.h:411
llvm::DenseMapBase::find
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:150
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::MCSectionCOFF
This represents a section on Windows.
Definition: MCSectionCOFF.h:26
llvm::move
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1609
memcpy
<%struct.s * > cast struct s *S to sbyte *< sbyte * > sbyte uint cast struct s *agg result to sbyte *< sbyte * > sbyte uint cast struct s *memtmp to sbyte *< sbyte * > sbyte uint ret void llc ends up issuing two memcpy or custom lower memcpy(of small size) to be ldmia/stmia. I think option 2 is better but the current register allocator cannot allocate a chunk of registers at a time. A feasible temporary solution is to use specific physical registers at the lowering time for small(<
llvm::MCAssembler::CGProfileEntry::To
const MCSymbolRefExpr * To
Definition: MCAssembler.h:438
llvm::COFF::header::SizeOfOptionalHeader
uint16_t SizeOfOptionalHeader
Definition: COFF.h:71
llvm::COFF::IMAGE_SCN_LNK_COMDAT
@ IMAGE_SCN_LNK_COMDAT
Definition: COFF.h:294
llvm::COFF::IMAGE_REL_ARM_ABSOLUTE
@ IMAGE_REL_ARM_ABSOLUTE
Definition: COFF.h:366
AuxType
COFFYAML::AuxSymbolType AuxType
Definition: COFFYAML.cpp:348
llvm::MCSymbolRefExpr
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:192
MCSection.h
llvm::size
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1532
isAssociative
static bool isAssociative(const COFFSection &Section)
Definition: WinCOFFObjectWriter.cpp:930
llvm::COFF::BigObjMagic
static const char BigObjMagic[]
Definition: COFF.h:38
llvm::MCSection::getAlignment
unsigned getAlignment() const
Definition: MCSection.h:138
Fixup
PowerPC TLS Dynamic Call Fixup
Definition: PPCTLSDynamicCall.cpp:235
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::COFF::IMAGE_SCN_ALIGN_4096BYTES
@ IMAGE_SCN_ALIGN_4096BYTES
Definition: COFF.h:312
llvm::COFF::IMAGE_SCN_ALIGN_128BYTES
@ IMAGE_SCN_ALIGN_128BYTES
Definition: COFF.h:307
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:134
A
* A
Definition: README_ALTIVEC.txt:89
Number
uint32_t Number
Definition: Profile.cpp:47
llvm::COFF::IMAGE_SCN_ALIGN_8BYTES
@ IMAGE_SCN_ALIGN_8BYTES
Definition: COFF.h:303
uint32_t
MCFixup.h
llvm::COFF::SectionSize
@ SectionSize
Definition: COFF.h:61
llvm::MCObjectWriter
Defines the object file and target independent interfaces used by the assembler backend to write nati...
Definition: MCObjectWriter.h:33
llvm::MCSection
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:39
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::COFF::IMAGE_REL_ARM_REL32
@ IMAGE_REL_ARM_REL32
Definition: COFF.h:374
llvm::COFF::IMAGE_FILE_MACHINE_ARM64
@ IMAGE_FILE_MACHINE_ARM64
Definition: COFF.h:101
llvm::COFF::IMAGE_REL_ARM_BLX24
@ IMAGE_REL_ARM_BLX24
Definition: COFF.h:372
llvm::MCAsmLayout::getFragmentOffset
uint64_t getFragmentOffset(const MCFragment *F) const
Get the offset of the given fragment inside its containing section.
Definition: MCFragment.cpp:96
llvm::COFF::IMAGE_REL_ARM64_REL32
@ IMAGE_REL_ARM64_REL32
Definition: COFF.h:403
MCAsmLayout.h
llvm::MCSection::getName
StringRef getName() const
Definition: MCSection.h:122
llvm::pdb::PDB_DataKind::Local
@ Local
llvm::MCSymbolCOFF::getType
uint16_t getType() const
Definition: MCSymbolCOFF.h:33
llvm::COFF::header::NumberOfSections
int32_t NumberOfSections
Definition: COFF.h:67
llvm::COFF::IMAGE_REL_ARM_SECTION
@ IMAGE_REL_ARM_SECTION
Definition: COFF.h:375
name
static const char * name
Definition: SVEIntrinsicOpts.cpp:78
llvm::COFF::RelocationSize
@ RelocationSize
Definition: COFF.h:62
MCObjectWriter.h
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:83
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
std
Definition: BitVector.h:838
EndianStream.h
llvm::COFF::MaxNumberOfSections16
const int32_t MaxNumberOfSections16
Definition: COFF.h:33
llvm::MCObjectWriter::reset
virtual void reset()
lifetime management
Definition: MCObjectWriter.h:43
uint16_t
llvm::MCAsmLayout
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:28
llvm::DenseMapBase::end
iterator end()
Definition: DenseMap.h:83
llvm::COFF::section
Definition: COFF.h:270
Casting.h
MCSymbolCOFF.h
llvm::sort
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1492
llvm::TargetStackID::Value
Value
Definition: TargetFrameLowering.h:27
llvm::COFF::IMAGE_REL_ARM_BLX23T
@ IMAGE_REL_ARM_BLX23T
Definition: COFF.h:381
llvm::MCAsmLayout::getSymbolOffset
bool getSymbolOffset(const MCSymbol &S, uint64_t &Val) const
Get the offset of the given symbol, as computed in the current layout.
Definition: MCFragment.cpp:152
llvm::COFF::IMAGE_SCN_ALIGN_1024BYTES
@ IMAGE_SCN_ALIGN_1024BYTES
Definition: COFF.h:310
llvm::StringTableBuilder
Utility for building string tables with deduplicated suffixes.
Definition: StringTableBuilder.h:23
MCValue.h
encodeBase64StringEntry
static void encodeBase64StringEntry(char *Buffer, uint64_t Value)
Definition: WinCOFFObjectWriter.cpp:437
llvm::COFF::header::NumberOfSymbols
uint32_t NumberOfSymbols
Definition: COFF.h:70
llvm::makeArrayRef
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:476
llvm::ARMBuildAttrs::Symbol
@ Symbol
Definition: ARMBuildAttributes.h:79
Machine
COFF::MachineTypes Machine
Definition: COFFYAML.cpp:366
SmallVector.h
Max7DecimalOffset
@ Max7DecimalOffset
Definition: WinCOFFObjectWriter.cpp:431
llvm::COFF::Header32Size
@ Header32Size
Definition: COFF.h:57
llvm::MCSymbol::isInSection
bool isInSection() const
isInSection - Check if this symbol is defined in some section (i.e., it is defined but not absolute).
Definition: MCSymbol.h:247
support
Reimplement select in terms of SEL *We would really like to support but we need to prove that the add doesn t need to overflow between the two bit chunks *Implement pre post increment support(e.g. PR935) *Implement smarter const ant generation for binops with large immediates. A few ARMv6T2 ops should be pattern matched
Definition: README.txt:10
getSymbolValue
static uint64_t getSymbolValue(const MCSymbol &Symbol, const MCAsmLayout &Layout)
Definition: WinCOFFObjectWriter.cpp:334
llvm::COFF::header
Definition: COFF.h:65
llvm::raw_svector_ostream
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:658
llvm::encodeULEB128
unsigned encodeULEB128(uint64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a ULEB128 value to an output stream.
Definition: LEB128.h:80
MaxBase64Offset
@ MaxBase64Offset
Definition: WinCOFFObjectWriter.cpp:432
llvm::HexStyle::Asm
@ Asm
0ffh
Definition: MCInstPrinter.h:34
llvm::MCValue
This represents an "assembler immediate".
Definition: MCValue.h:37
llvm::pointee_iterator
An iterator type that allows iterating over the pointees via some other iterator.
Definition: iterator.h:314
MCWinCOFFObjectWriter.h
llvm::MCAsmLayout::getSectionAddressSize
uint64_t getSectionAddressSize(const MCSection *Sec) const
Get the address space size of the given section, as it effects layout.
Definition: MCFragment.cpp:198
raw_ostream.h
llvm::COFF::IMAGE_REL_ARM_BLX11
@ IMAGE_REL_ARM_BLX11
Definition: COFF.h:373
llvm::COFF::IMAGE_REL_ARM_ADDR32
@ IMAGE_REL_ARM_ADDR32
Definition: COFF.h:367
llvm::COFF::IMAGE_SYM_DTYPE_FUNCTION
@ IMAGE_SYM_DTYPE_FUNCTION
A function that returns a base type.
Definition: COFF.h:261
llvm::MCDataFragment
Fragment for data and encoded instructions.
Definition: MCFragment.h:242
llvm::COFF::IMAGE_SYM_DEBUG
@ IMAGE_SYM_DEBUG
Definition: COFF.h:197
MCExpr.h
llvm::MCFixup
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:71
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
File
Instrumentation for Order File
Definition: InstrOrderFile.cpp:205
llvm::MCAssembler::CGProfileEntry
Definition: MCAssembler.h:436
llvm::sampleprof::Base
@ Base
Definition: Discriminator.h:58
Other
Optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:1184
write
static void write(bool isBE, void *P, T V)
Definition: RuntimeDyldELF.cpp:37
llvm::Twine::concat
Twine concat(const Twine &Suffix) const
Definition: Twine.h:510
getTime
static std::time_t getTime()
Definition: WinCOFFObjectWriter.cpp:850