LLVM  7.0.0svn
WinCOFFObjectWriter.cpp
Go to the documentation of this file.
1 //===- llvm/MC/WinCOFFObjectWriter.cpp ------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains an implementation of a Win32 COFF object file writer.
11 //
12 //===----------------------------------------------------------------------===//
13 
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/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/Casting.h"
36 #include "llvm/Support/Endian.h"
38 #include "llvm/Support/JamCRC.h"
41 #include <algorithm>
42 #include <cassert>
43 #include <cstddef>
44 #include <cstdint>
45 #include <cstring>
46 #include <ctime>
47 #include <memory>
48 #include <string>
49 #include <vector>
50 
51 using namespace llvm;
53 
54 #define DEBUG_TYPE "WinCOFFObjectWriter"
55 
56 namespace {
57 
59 
61  ATFunctionDefinition,
62  ATbfAndefSymbol,
63  ATWeakExternal,
64  ATFile,
65  ATSectionDefinition
66 };
67 
68 struct AuxSymbol {
70  COFF::Auxiliary Aux;
71 };
72 
73 class COFFSection;
74 
75 class COFFSymbol {
76 public:
77  COFF::symbol Data = {};
78 
79  using AuxiliarySymbols = SmallVector<AuxSymbol, 1>;
80 
81  name Name;
82  int Index;
83  AuxiliarySymbols Aux;
84  COFFSymbol *Other = nullptr;
85  COFFSection *Section = nullptr;
86  int Relocations = 0;
87  const MCSymbol *MC = nullptr;
88 
89  COFFSymbol(StringRef Name) : Name(Name) {}
90 
91  void set_name_offset(uint32_t Offset);
92 
93  int64_t getIndex() const { return Index; }
94  void setIndex(int Value) {
95  Index = Value;
96  if (MC)
97  MC->setIndex(static_cast<uint32_t>(Value));
98  }
99 };
100 
101 // This class contains staging data for a COFF relocation entry.
102 struct COFFRelocation {
104  COFFSymbol *Symb = nullptr;
105 
106  COFFRelocation() = default;
107 
108  static size_t size() { return COFF::RelocationSize; }
109 };
110 
111 using relocations = std::vector<COFFRelocation>;
112 
113 class COFFSection {
114 public:
115  COFF::section Header = {};
116 
117  std::string Name;
118  int Number;
119  MCSectionCOFF const *MCSection = nullptr;
120  COFFSymbol *Symbol = nullptr;
121  relocations Relocations;
122 
123  COFFSection(StringRef Name) : Name(Name) {}
124 };
125 
126 class WinCOFFObjectWriter : public MCObjectWriter {
127 public:
129 
130  using symbols = std::vector<std::unique_ptr<COFFSymbol>>;
131  using sections = std::vector<std::unique_ptr<COFFSection>>;
132 
133  using symbol_map = DenseMap<MCSymbol const *, COFFSymbol *>;
134  using section_map = DenseMap<MCSection const *, COFFSection *>;
135 
136  std::unique_ptr<MCWinCOFFObjectTargetWriter> TargetObjectWriter;
137 
138  // Root level file contents.
139  COFF::header Header = {};
140  sections Sections;
141  symbols Symbols;
143 
144  // Maps used during object file creation.
145  section_map SectionMap;
146  symbol_map SymbolMap;
147 
148  bool UseBigObj;
149 
150  WinCOFFObjectWriter(std::unique_ptr<MCWinCOFFObjectTargetWriter> MOTW,
151  raw_pwrite_stream &OS);
152 
153  void reset() override {
154  memset(&Header, 0, sizeof(Header));
155  Header.Machine = TargetObjectWriter->getMachine();
156  Sections.clear();
157  Symbols.clear();
158  Strings.clear();
159  SectionMap.clear();
160  SymbolMap.clear();
162  }
163 
164  COFFSymbol *createSymbol(StringRef Name);
165  COFFSymbol *GetOrCreateCOFFSymbol(const MCSymbol *Symbol);
166  COFFSection *createSection(StringRef Name);
167 
168  void defineSection(MCSectionCOFF const &Sec);
169 
170  COFFSymbol *getLinkedSymbol(const MCSymbol &Symbol);
171  void DefineSymbol(const MCSymbol &Symbol, MCAssembler &Assembler,
172  const MCAsmLayout &Layout);
173 
174  void SetSymbolName(COFFSymbol &S);
175  void SetSectionName(COFFSection &S);
176 
177  bool IsPhysicalSection(COFFSection *S);
178 
179  // Entity writing methods.
180 
181  void WriteFileHeader(const COFF::header &Header);
182  void WriteSymbol(const COFFSymbol &S);
183  void WriteAuxiliarySymbols(const COFFSymbol::AuxiliarySymbols &S);
184  void writeSectionHeaders();
185  void WriteRelocation(const COFF::relocation &R);
186  uint32_t writeSectionContents(MCAssembler &Asm, const MCAsmLayout &Layout,
187  const MCSection &MCSec);
188  void writeSection(MCAssembler &Asm, const MCAsmLayout &Layout,
189  const COFFSection &Sec, const MCSection &MCSec);
190 
191  // MCObjectWriter interface implementation.
192 
193  void executePostLayoutBinding(MCAssembler &Asm,
194  const MCAsmLayout &Layout) override;
195 
196  bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
197  const MCSymbol &SymA,
198  const MCFragment &FB, bool InSet,
199  bool IsPCRel) const override;
200 
201  void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
202  const MCFragment *Fragment, const MCFixup &Fixup,
203  MCValue Target, uint64_t &FixedValue) override;
204 
205  void createFileSymbols(MCAssembler &Asm);
206  void assignSectionNumbers();
207  void assignFileOffsets(MCAssembler &Asm, const MCAsmLayout &Layout);
208 
209  uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
210 };
211 
212 } // end anonymous namespace
213 
214 //------------------------------------------------------------------------------
215 // Symbol class implementation
216 
217 // In the case that the name does not fit within 8 bytes, the offset
218 // into the string table is stored in the last 4 bytes instead, leaving
219 // the first 4 bytes as 0.
220 void COFFSymbol::set_name_offset(uint32_t Offset) {
221  write32le(Data.Name + 0, 0);
222  write32le(Data.Name + 4, Offset);
223 }
224 
225 //------------------------------------------------------------------------------
226 // WinCOFFObjectWriter class implementation
227 
228 WinCOFFObjectWriter::WinCOFFObjectWriter(
229  std::unique_ptr<MCWinCOFFObjectTargetWriter> MOTW, raw_pwrite_stream &OS)
230  : W(OS, support::little), TargetObjectWriter(std::move(MOTW)) {
231  Header.Machine = TargetObjectWriter->getMachine();
232 }
233 
234 COFFSymbol *WinCOFFObjectWriter::createSymbol(StringRef Name) {
235  Symbols.push_back(make_unique<COFFSymbol>(Name));
236  return Symbols.back().get();
237 }
238 
239 COFFSymbol *WinCOFFObjectWriter::GetOrCreateCOFFSymbol(const MCSymbol *Symbol) {
240  COFFSymbol *&Ret = SymbolMap[Symbol];
241  if (!Ret)
242  Ret = createSymbol(Symbol->getName());
243  return Ret;
244 }
245 
246 COFFSection *WinCOFFObjectWriter::createSection(StringRef Name) {
247  Sections.emplace_back(make_unique<COFFSection>(Name));
248  return Sections.back().get();
249 }
250 
251 static uint32_t getAlignment(const MCSectionCOFF &Sec) {
252  switch (Sec.getAlignment()) {
253  case 1:
255  case 2:
257  case 4:
259  case 8:
261  case 16:
263  case 32:
265  case 64:
267  case 128:
269  case 256:
271  case 512:
273  case 1024:
275  case 2048:
277  case 4096:
279  case 8192:
281  }
282  llvm_unreachable("unsupported section alignment");
283 }
284 
285 /// This function takes a section data object from the assembler
286 /// and creates the associated COFF section staging object.
287 void WinCOFFObjectWriter::defineSection(const MCSectionCOFF &MCSec) {
288  COFFSection *Section = createSection(MCSec.getSectionName());
289  COFFSymbol *Symbol = createSymbol(MCSec.getSectionName());
290  Section->Symbol = Symbol;
291  Symbol->Section = Section;
292  Symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_STATIC;
293 
294  // Create a COMDAT symbol if needed.
296  if (const MCSymbol *S = MCSec.getCOMDATSymbol()) {
297  COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(S);
298  if (COMDATSymbol->Section)
299  report_fatal_error("two sections have the same comdat");
300  COMDATSymbol->Section = Section;
301  }
302  }
303 
304  // In this case the auxiliary symbol is a Section Definition.
305  Symbol->Aux.resize(1);
306  Symbol->Aux[0] = {};
307  Symbol->Aux[0].AuxType = ATSectionDefinition;
308  Symbol->Aux[0].Aux.SectionDefinition.Selection = MCSec.getSelection();
309 
310  // Set section alignment.
311  Section->Header.Characteristics = MCSec.getCharacteristics();
312  Section->Header.Characteristics |= getAlignment(MCSec);
313 
314  // Bind internal COFF section to MC section.
315  Section->MCSection = &MCSec;
316  SectionMap[&MCSec] = Section;
317 }
318 
319 static uint64_t getSymbolValue(const MCSymbol &Symbol,
320  const MCAsmLayout &Layout) {
321  if (Symbol.isCommon() && Symbol.isExternal())
322  return Symbol.getCommonSize();
323 
324  uint64_t Res;
325  if (!Layout.getSymbolOffset(Symbol, Res))
326  return 0;
327 
328  return Res;
329 }
330 
331 COFFSymbol *WinCOFFObjectWriter::getLinkedSymbol(const MCSymbol &Symbol) {
332  if (!Symbol.isVariable())
333  return nullptr;
334 
335  const MCSymbolRefExpr *SymRef =
337  if (!SymRef)
338  return nullptr;
339 
340  const MCSymbol &Aliasee = SymRef->getSymbol();
341  if (!Aliasee.isUndefined())
342  return nullptr;
343  return GetOrCreateCOFFSymbol(&Aliasee);
344 }
345 
346 /// This function takes a symbol data object from the assembler
347 /// and creates the associated COFF symbol staging object.
348 void WinCOFFObjectWriter::DefineSymbol(const MCSymbol &MCSym,
349  MCAssembler &Assembler,
350  const MCAsmLayout &Layout) {
351  COFFSymbol *Sym = GetOrCreateCOFFSymbol(&MCSym);
352  const MCSymbol *Base = Layout.getBaseSymbol(MCSym);
353  COFFSection *Sec = nullptr;
354  if (Base && Base->getFragment()) {
355  Sec = SectionMap[Base->getFragment()->getParent()];
356  if (Sym->Section && Sym->Section != Sec)
357  report_fatal_error("conflicting sections for symbol");
358  }
359 
360  COFFSymbol *Local = nullptr;
361  if (cast<MCSymbolCOFF>(MCSym).isWeakExternal()) {
362  Sym->Data.StorageClass = COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
363 
364  COFFSymbol *WeakDefault = getLinkedSymbol(MCSym);
365  if (!WeakDefault) {
366  std::string WeakName = (".weak." + MCSym.getName() + ".default").str();
367  WeakDefault = createSymbol(WeakName);
368  if (!Sec)
369  WeakDefault->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
370  else
371  WeakDefault->Section = Sec;
372  Local = WeakDefault;
373  }
374 
375  Sym->Other = WeakDefault;
376 
377  // Setup the Weak External auxiliary symbol.
378  Sym->Aux.resize(1);
379  memset(&Sym->Aux[0], 0, sizeof(Sym->Aux[0]));
380  Sym->Aux[0].AuxType = ATWeakExternal;
381  Sym->Aux[0].Aux.WeakExternal.TagIndex = 0;
382  Sym->Aux[0].Aux.WeakExternal.Characteristics =
384  } else {
385  if (!Base)
386  Sym->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
387  else
388  Sym->Section = Sec;
389  Local = Sym;
390  }
391 
392  if (Local) {
393  Local->Data.Value = getSymbolValue(MCSym, Layout);
394 
395  const MCSymbolCOFF &SymbolCOFF = cast<MCSymbolCOFF>(MCSym);
396  Local->Data.Type = SymbolCOFF.getType();
397  Local->Data.StorageClass = SymbolCOFF.getClass();
398 
399  // If no storage class was specified in the streamer, define it here.
400  if (Local->Data.StorageClass == COFF::IMAGE_SYM_CLASS_NULL) {
401  bool IsExternal = MCSym.isExternal() ||
402  (!MCSym.getFragment() && !MCSym.isVariable());
403 
404  Local->Data.StorageClass = IsExternal ? COFF::IMAGE_SYM_CLASS_EXTERNAL
406  }
407  }
408 
409  Sym->MC = &MCSym;
410 }
411 
412 // Maximum offsets for different string table entry encodings.
413 enum : unsigned { Max7DecimalOffset = 9999999U };
414 enum : uint64_t { MaxBase64Offset = 0xFFFFFFFFFULL }; // 64^6, including 0
415 
416 // Encode a string table entry offset in base 64, padded to 6 chars, and
417 // prefixed with a double slash: '//AAAAAA', '//AAAAAB', ...
418 // Buffer must be at least 8 bytes large. No terminating null appended.
419 static void encodeBase64StringEntry(char *Buffer, uint64_t Value) {
420  assert(Value > Max7DecimalOffset && Value <= MaxBase64Offset &&
421  "Illegal section name encoding for value");
422 
423  static const char Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
424  "abcdefghijklmnopqrstuvwxyz"
425  "0123456789+/";
426 
427  Buffer[0] = '/';
428  Buffer[1] = '/';
429 
430  char *Ptr = Buffer + 7;
431  for (unsigned i = 0; i < 6; ++i) {
432  unsigned Rem = Value % 64;
433  Value /= 64;
434  *(Ptr--) = Alphabet[Rem];
435  }
436 }
437 
438 void WinCOFFObjectWriter::SetSectionName(COFFSection &S) {
439  if (S.Name.size() <= COFF::NameSize) {
440  std::memcpy(S.Header.Name, S.Name.c_str(), S.Name.size());
441  return;
442  }
443 
444  uint64_t StringTableEntry = Strings.getOffset(S.Name);
445  if (StringTableEntry <= Max7DecimalOffset) {
447  Twine('/').concat(Twine(StringTableEntry)).toVector(Buffer);
448  assert(Buffer.size() <= COFF::NameSize && Buffer.size() >= 2);
449  std::memcpy(S.Header.Name, Buffer.data(), Buffer.size());
450  return;
451  }
452  if (StringTableEntry <= MaxBase64Offset) {
453  // Starting with 10,000,000, offsets are encoded as base64.
454  encodeBase64StringEntry(S.Header.Name, StringTableEntry);
455  return;
456  }
457  report_fatal_error("COFF string table is greater than 64 GB.");
458 }
459 
460 void WinCOFFObjectWriter::SetSymbolName(COFFSymbol &S) {
461  if (S.Name.size() > COFF::NameSize)
462  S.set_name_offset(Strings.getOffset(S.Name));
463  else
464  std::memcpy(S.Data.Name, S.Name.c_str(), S.Name.size());
465 }
466 
467 bool WinCOFFObjectWriter::IsPhysicalSection(COFFSection *S) {
468  return (S->Header.Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) ==
469  0;
470 }
471 
472 //------------------------------------------------------------------------------
473 // entity writing methods
474 
475 void WinCOFFObjectWriter::WriteFileHeader(const COFF::header &Header) {
476  if (UseBigObj) {
477  W.write<uint16_t>(COFF::IMAGE_FILE_MACHINE_UNKNOWN);
478  W.write<uint16_t>(0xFFFF);
480  W.write<uint16_t>(Header.Machine);
481  W.write<uint32_t>(Header.TimeDateStamp);
482  W.OS.write(COFF::BigObjMagic, sizeof(COFF::BigObjMagic));
483  W.write<uint32_t>(0);
484  W.write<uint32_t>(0);
485  W.write<uint32_t>(0);
486  W.write<uint32_t>(0);
487  W.write<uint32_t>(Header.NumberOfSections);
488  W.write<uint32_t>(Header.PointerToSymbolTable);
489  W.write<uint32_t>(Header.NumberOfSymbols);
490  } else {
491  W.write<uint16_t>(Header.Machine);
492  W.write<uint16_t>(static_cast<int16_t>(Header.NumberOfSections));
493  W.write<uint32_t>(Header.TimeDateStamp);
494  W.write<uint32_t>(Header.PointerToSymbolTable);
495  W.write<uint32_t>(Header.NumberOfSymbols);
496  W.write<uint16_t>(Header.SizeOfOptionalHeader);
497  W.write<uint16_t>(Header.Characteristics);
498  }
499 }
500 
501 void WinCOFFObjectWriter::WriteSymbol(const COFFSymbol &S) {
502  W.OS.write(S.Data.Name, COFF::NameSize);
503  W.write<uint32_t>(S.Data.Value);
504  if (UseBigObj)
505  W.write<uint32_t>(S.Data.SectionNumber);
506  else
507  W.write<uint16_t>(static_cast<int16_t>(S.Data.SectionNumber));
508  W.write<uint16_t>(S.Data.Type);
509  W.OS << char(S.Data.StorageClass);
510  W.OS << char(S.Data.NumberOfAuxSymbols);
511  WriteAuxiliarySymbols(S.Aux);
512 }
513 
514 void WinCOFFObjectWriter::WriteAuxiliarySymbols(
515  const COFFSymbol::AuxiliarySymbols &S) {
516  for (const AuxSymbol &i : S) {
517  switch (i.AuxType) {
518  case ATFunctionDefinition:
519  W.write<uint32_t>(i.Aux.FunctionDefinition.TagIndex);
520  W.write<uint32_t>(i.Aux.FunctionDefinition.TotalSize);
521  W.write<uint32_t>(i.Aux.FunctionDefinition.PointerToLinenumber);
522  W.write<uint32_t>(i.Aux.FunctionDefinition.PointerToNextFunction);
523  W.OS.write_zeros(sizeof(i.Aux.FunctionDefinition.unused));
524  if (UseBigObj)
525  W.OS.write_zeros(COFF::Symbol32Size - COFF::Symbol16Size);
526  break;
527  case ATbfAndefSymbol:
528  W.OS.write_zeros(sizeof(i.Aux.bfAndefSymbol.unused1));
529  W.write<uint16_t>(i.Aux.bfAndefSymbol.Linenumber);
530  W.OS.write_zeros(sizeof(i.Aux.bfAndefSymbol.unused2));
531  W.write<uint32_t>(i.Aux.bfAndefSymbol.PointerToNextFunction);
532  W.OS.write_zeros(sizeof(i.Aux.bfAndefSymbol.unused3));
533  if (UseBigObj)
534  W.OS.write_zeros(COFF::Symbol32Size - COFF::Symbol16Size);
535  break;
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.begin(), Arr.end(),
572  [](const COFFSection *A, const COFFSection *B) {
573  return A->Number < B->Number;
574  });
575 
576  for (auto &Section : Arr) {
577  if (Section->Number == -1)
578  continue;
579 
580  COFF::section &S = Section->Header;
581  if (Section->Relocations.size() >= 0xffff)
583  W.OS.write(S.Name, COFF::NameSize);
584  W.write<uint32_t>(S.VirtualSize);
585  W.write<uint32_t>(S.VirtualAddress);
586  W.write<uint32_t>(S.SizeOfRawData);
587  W.write<uint32_t>(S.PointerToRawData);
588  W.write<uint32_t>(S.PointerToRelocations);
589  W.write<uint32_t>(S.PointerToLineNumbers);
590  W.write<uint16_t>(S.NumberOfRelocations);
591  W.write<uint16_t>(S.NumberOfLineNumbers);
592  W.write<uint32_t>(S.Characteristics);
593  }
594 }
595 
596 void WinCOFFObjectWriter::WriteRelocation(const COFF::relocation &R) {
597  W.write<uint32_t>(R.VirtualAddress);
598  W.write<uint32_t>(R.SymbolTableIndex);
599  W.write<uint16_t>(R.Type);
600 }
601 
602 // Write MCSec's contents. What this function does is essentially
603 // "Asm.writeSectionData(&MCSec, Layout)", but it's a bit complicated
604 // because it needs to compute a CRC.
605 uint32_t WinCOFFObjectWriter::writeSectionContents(MCAssembler &Asm,
606  const MCAsmLayout &Layout,
607  const MCSection &MCSec) {
608  // Save the contents of the section to a temporary buffer, we need this
609  // to CRC the data before we dump it into the object file.
611  raw_svector_ostream VecOS(Buf);
612  Asm.writeSectionData(VecOS, &MCSec, Layout);
613 
614  // Write the section contents to the object file.
615  W.OS << Buf;
616 
617  // Calculate our CRC with an initial value of '0', this is not how
618  // JamCRC is specified but it aligns with the expected output.
619  JamCRC JC(/*Init=*/0);
620  JC.update(Buf);
621  return JC.getCRC();
622 }
623 
624 void WinCOFFObjectWriter::writeSection(MCAssembler &Asm,
625  const MCAsmLayout &Layout,
626  const COFFSection &Sec,
627  const MCSection &MCSec) {
628  if (Sec.Number == -1)
629  return;
630 
631  // Write the section contents.
632  if (Sec.Header.PointerToRawData != 0) {
633  assert(W.OS.tell() <= Sec.Header.PointerToRawData &&
634  "Section::PointerToRawData is insane!");
635 
636  unsigned PaddingSize = Sec.Header.PointerToRawData - W.OS.tell();
637  assert(PaddingSize < 4 &&
638  "Should only need at most three bytes of padding!");
639  W.OS.write_zeros(PaddingSize);
640 
641  uint32_t CRC = writeSectionContents(Asm, Layout, MCSec);
642 
643  // Update the section definition auxiliary symbol to record the CRC.
644  COFFSection *Sec = SectionMap[&MCSec];
645  COFFSymbol::AuxiliarySymbols &AuxSyms = Sec->Symbol->Aux;
646  assert(AuxSyms.size() == 1 && AuxSyms[0].AuxType == ATSectionDefinition);
647  AuxSymbol &SecDef = AuxSyms[0];
648  SecDef.Aux.SectionDefinition.CheckSum = CRC;
649  }
650 
651  // Write relocations for this section.
652  if (Sec.Relocations.empty()) {
653  assert(Sec.Header.PointerToRelocations == 0 &&
654  "Section::PointerToRelocations is insane!");
655  return;
656  }
657 
658  assert(W.OS.tell() == Sec.Header.PointerToRelocations &&
659  "Section::PointerToRelocations is insane!");
660 
661  if (Sec.Relocations.size() >= 0xffff) {
662  // In case of overflow, write actual relocation count as first
663  // relocation. Including the synthetic reloc itself (+ 1).
665  R.VirtualAddress = Sec.Relocations.size() + 1;
666  R.SymbolTableIndex = 0;
667  R.Type = 0;
668  WriteRelocation(R);
669  }
670 
671  for (const auto &Relocation : Sec.Relocations)
672  WriteRelocation(Relocation.Data);
673 }
674 
675 ////////////////////////////////////////////////////////////////////////////////
676 // MCObjectWriter interface implementations
677 
678 void WinCOFFObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
679  const MCAsmLayout &Layout) {
680  // "Define" each section & symbol. This creates section & symbol
681  // entries in the staging area.
682  for (const auto &Section : Asm)
683  defineSection(static_cast<const MCSectionCOFF &>(Section));
684 
685  for (const MCSymbol &Symbol : Asm.symbols())
686  if (!Symbol.isTemporary())
687  DefineSymbol(Symbol, Asm, Layout);
688 }
689 
690 bool WinCOFFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
691  const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB,
692  bool InSet, bool IsPCRel) const {
693  // Don't drop relocations between functions, even if they are in the same text
694  // section. Multiple Visual C++ linker features depend on having the
695  // relocations present. The /INCREMENTAL flag will cause these relocations to
696  // point to thunks, and the /GUARD:CF flag assumes that it can use relocations
697  // to approximate the set of all address taken functions. LLD's implementation
698  // of /GUARD:CF also relies on the existance of these relocations.
699  uint16_t Type = cast<MCSymbolCOFF>(SymA).getType();
701  return false;
703  InSet, IsPCRel);
704 }
705 
706 void WinCOFFObjectWriter::recordRelocation(MCAssembler &Asm,
707  const MCAsmLayout &Layout,
708  const MCFragment *Fragment,
709  const MCFixup &Fixup, MCValue Target,
710  uint64_t &FixedValue) {
711  assert(Target.getSymA() && "Relocation must reference a symbol!");
712 
713  const MCSymbol &A = Target.getSymA()->getSymbol();
714  if (!A.isRegistered()) {
715  Asm.getContext().reportError(Fixup.getLoc(),
716  Twine("symbol '") + A.getName() +
717  "' can not be undefined");
718  return;
719  }
720  if (A.isTemporary() && A.isUndefined()) {
721  Asm.getContext().reportError(Fixup.getLoc(),
722  Twine("assembler label '") + A.getName() +
723  "' can not be undefined");
724  return;
725  }
726 
727  MCSection *MCSec = Fragment->getParent();
728 
729  // Mark this symbol as requiring an entry in the symbol table.
730  assert(SectionMap.find(MCSec) != SectionMap.end() &&
731  "Section must already have been defined in executePostLayoutBinding!");
732 
733  COFFSection *Sec = SectionMap[MCSec];
734  const MCSymbolRefExpr *SymB = Target.getSymB();
735 
736  if (SymB) {
737  const MCSymbol *B = &SymB->getSymbol();
738  if (!B->getFragment()) {
739  Asm.getContext().reportError(
740  Fixup.getLoc(),
741  Twine("symbol '") + B->getName() +
742  "' can not be undefined in a subtraction expression");
743  return;
744  }
745 
746  // Offset of the symbol in the section
747  int64_t OffsetOfB = Layout.getSymbolOffset(*B);
748 
749  // Offset of the relocation in the section
750  int64_t OffsetOfRelocation =
751  Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
752 
753  FixedValue = (OffsetOfRelocation - OffsetOfB) + Target.getConstant();
754  } else {
755  FixedValue = Target.getConstant();
756  }
757 
758  COFFRelocation Reloc;
759 
760  Reloc.Data.SymbolTableIndex = 0;
761  Reloc.Data.VirtualAddress = Layout.getFragmentOffset(Fragment);
762 
763  // Turn relocations for temporary symbols into section relocations.
764  if (A.isTemporary()) {
765  MCSection *TargetSection = &A.getSection();
766  assert(
767  SectionMap.find(TargetSection) != SectionMap.end() &&
768  "Section must already have been defined in executePostLayoutBinding!");
769  Reloc.Symb = SectionMap[TargetSection]->Symbol;
770  FixedValue += Layout.getSymbolOffset(A);
771  } else {
772  assert(
773  SymbolMap.find(&A) != SymbolMap.end() &&
774  "Symbol must already have been defined in executePostLayoutBinding!");
775  Reloc.Symb = SymbolMap[&A];
776  }
777 
778  ++Reloc.Symb->Relocations;
779 
780  Reloc.Data.VirtualAddress += Fixup.getOffset();
781  Reloc.Data.Type = TargetObjectWriter->getRelocType(
782  Asm.getContext(), Target, Fixup, SymB, Asm.getBackend());
783 
784  // FIXME: Can anyone explain what this does other than adjust for the size
785  // of the offset?
786  if ((Header.Machine == COFF::IMAGE_FILE_MACHINE_AMD64 &&
787  Reloc.Data.Type == COFF::IMAGE_REL_AMD64_REL32) ||
789  Reloc.Data.Type == COFF::IMAGE_REL_I386_REL32))
790  FixedValue += 4;
791 
792  if (Header.Machine == COFF::IMAGE_FILE_MACHINE_ARMNT) {
793  switch (Reloc.Data.Type) {
800  break;
803  // IMAGE_REL_ARM_BRANCH11 and IMAGE_REL_ARM_BLX11 are only used for
804  // pre-ARMv7, which implicitly rules it out of ARMNT (it would be valid
805  // for Windows CE).
809  // IMAGE_REL_ARM_BRANCH24, IMAGE_REL_ARM_BLX24, IMAGE_REL_ARM_MOV32A are
810  // only used for ARM mode code, which is documented as being unsupported
811  // by Windows on ARM. Empirical proof indicates that masm is able to
812  // generate the relocations however the rest of the MSVC toolchain is
813  // unable to handle it.
814  llvm_unreachable("unsupported relocation");
815  break;
817  break;
821  // IMAGE_REL_BRANCH20T, IMAGE_REL_ARM_BRANCH24T, IMAGE_REL_ARM_BLX23T all
822  // perform a 4 byte adjustment to the relocation. Relative branches are
823  // offset by 4 on ARM, however, because there is no RELA relocations, all
824  // branches are offset by 4.
825  FixedValue = FixedValue + 4;
826  break;
827  }
828  }
829 
830  // The fixed value never makes sense for section indices, ignore it.
831  if (Fixup.getKind() == FK_SecRel_2)
832  FixedValue = 0;
833 
834  if (TargetObjectWriter->recordRelocation(Fixup))
835  Sec->Relocations.push_back(Reloc);
836 }
837 
838 static std::time_t getTime() {
839  std::time_t Now = time(nullptr);
840  if (Now < 0 || !isUInt<32>(Now))
841  return UINT32_MAX;
842  return Now;
843 }
844 
845 // Create .file symbols.
846 void WinCOFFObjectWriter::createFileSymbols(MCAssembler &Asm) {
847  for (const std::string &Name : Asm.getFileNames()) {
848  // round up to calculate the number of auxiliary symbols required
849  unsigned SymbolSize = UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size;
850  unsigned Count = (Name.size() + SymbolSize - 1) / SymbolSize;
851 
852  COFFSymbol *File = createSymbol(".file");
853  File->Data.SectionNumber = COFF::IMAGE_SYM_DEBUG;
854  File->Data.StorageClass = COFF::IMAGE_SYM_CLASS_FILE;
855  File->Aux.resize(Count);
856 
857  unsigned Offset = 0;
858  unsigned Length = Name.size();
859  for (auto &Aux : File->Aux) {
860  Aux.AuxType = ATFile;
861 
862  if (Length > SymbolSize) {
863  memcpy(&Aux.Aux, Name.c_str() + Offset, SymbolSize);
864  Length = Length - SymbolSize;
865  } else {
866  memcpy(&Aux.Aux, Name.c_str() + Offset, Length);
867  memset((char *)&Aux.Aux + Length, 0, SymbolSize - Length);
868  break;
869  }
870 
871  Offset += SymbolSize;
872  }
873  }
874 }
875 
876 static bool isAssociative(const COFFSection &Section) {
877  return Section.Symbol->Aux[0].Aux.SectionDefinition.Selection ==
879 }
880 
881 void WinCOFFObjectWriter::assignSectionNumbers() {
882  size_t I = 1;
883  auto Assign = [&](COFFSection &Section) {
884  Section.Number = I;
885  Section.Symbol->Data.SectionNumber = I;
886  Section.Symbol->Aux[0].Aux.SectionDefinition.Number = I;
887  ++I;
888  };
889 
890  // Although it is not explicitly requested by the Microsoft COFF spec,
891  // we should avoid emitting forward associative section references,
892  // because MSVC link.exe as of 2017 cannot handle that.
893  for (const std::unique_ptr<COFFSection> &Section : Sections)
894  if (!isAssociative(*Section))
895  Assign(*Section);
896  for (const std::unique_ptr<COFFSection> &Section : Sections)
897  if (isAssociative(*Section))
898  Assign(*Section);
899 }
900 
901 // Assign file offsets to COFF object file structures.
902 void WinCOFFObjectWriter::assignFileOffsets(MCAssembler &Asm,
903  const MCAsmLayout &Layout) {
904  unsigned Offset = W.OS.tell();
905 
906  Offset += UseBigObj ? COFF::Header32Size : COFF::Header16Size;
907  Offset += COFF::SectionSize * Header.NumberOfSections;
908 
909  for (const auto &Section : Asm) {
910  COFFSection *Sec = SectionMap[&Section];
911 
912  if (Sec->Number == -1)
913  continue;
914 
915  Sec->Header.SizeOfRawData = Layout.getSectionAddressSize(&Section);
916 
917  if (IsPhysicalSection(Sec)) {
918  // Align the section data to a four byte boundary.
919  Offset = alignTo(Offset, 4);
920  Sec->Header.PointerToRawData = Offset;
921 
922  Offset += Sec->Header.SizeOfRawData;
923  }
924 
925  if (!Sec->Relocations.empty()) {
926  bool RelocationsOverflow = Sec->Relocations.size() >= 0xffff;
927 
928  if (RelocationsOverflow) {
929  // Signal overflow by setting NumberOfRelocations to max value. Actual
930  // size is found in reloc #0. Microsoft tools understand this.
931  Sec->Header.NumberOfRelocations = 0xffff;
932  } else {
933  Sec->Header.NumberOfRelocations = Sec->Relocations.size();
934  }
935  Sec->Header.PointerToRelocations = Offset;
936 
937  if (RelocationsOverflow) {
938  // Reloc #0 will contain actual count, so make room for it.
939  Offset += COFF::RelocationSize;
940  }
941 
942  Offset += COFF::RelocationSize * Sec->Relocations.size();
943 
944  for (auto &Relocation : Sec->Relocations) {
945  assert(Relocation.Symb->getIndex() != -1);
946  Relocation.Data.SymbolTableIndex = Relocation.Symb->getIndex();
947  }
948  }
949 
950  assert(Sec->Symbol->Aux.size() == 1 &&
951  "Section's symbol must have one aux!");
952  AuxSymbol &Aux = Sec->Symbol->Aux[0];
953  assert(Aux.AuxType == ATSectionDefinition &&
954  "Section's symbol's aux symbol must be a Section Definition!");
955  Aux.Aux.SectionDefinition.Length = Sec->Header.SizeOfRawData;
956  Aux.Aux.SectionDefinition.NumberOfRelocations =
957  Sec->Header.NumberOfRelocations;
958  Aux.Aux.SectionDefinition.NumberOfLinenumbers =
959  Sec->Header.NumberOfLineNumbers;
960  }
961 
962  Header.PointerToSymbolTable = Offset;
963 }
964 
965 uint64_t WinCOFFObjectWriter::writeObject(MCAssembler &Asm,
966  const MCAsmLayout &Layout) {
967  uint64_t StartOffset = W.OS.tell();
968 
969  if (Sections.size() > INT32_MAX)
971  "PE COFF object files can't have more than 2147483647 sections");
972 
973  UseBigObj = Sections.size() > COFF::MaxNumberOfSections16;
974  Header.NumberOfSections = Sections.size();
975  Header.NumberOfSymbols = 0;
976 
977  assignSectionNumbers();
978  createFileSymbols(Asm);
979 
980  for (auto &Symbol : Symbols) {
981  // Update section number & offset for symbols that have them.
982  if (Symbol->Section)
983  Symbol->Data.SectionNumber = Symbol->Section->Number;
984  Symbol->setIndex(Header.NumberOfSymbols++);
985  // Update auxiliary symbol info.
986  Symbol->Data.NumberOfAuxSymbols = Symbol->Aux.size();
987  Header.NumberOfSymbols += Symbol->Data.NumberOfAuxSymbols;
988  }
989 
990  // Build string table.
991  for (const auto &S : Sections)
992  if (S->Name.size() > COFF::NameSize)
993  Strings.add(S->Name);
994  for (const auto &S : Symbols)
995  if (S->Name.size() > COFF::NameSize)
996  Strings.add(S->Name);
997  Strings.finalize();
998 
999  // Set names.
1000  for (const auto &S : Sections)
1001  SetSectionName(*S);
1002  for (auto &S : Symbols)
1003  SetSymbolName(*S);
1004 
1005  // Fixup weak external references.
1006  for (auto &Symbol : Symbols) {
1007  if (Symbol->Other) {
1008  assert(Symbol->getIndex() != -1);
1009  assert(Symbol->Aux.size() == 1 && "Symbol must contain one aux symbol!");
1010  assert(Symbol->Aux[0].AuxType == ATWeakExternal &&
1011  "Symbol's aux symbol must be a Weak External!");
1012  Symbol->Aux[0].Aux.WeakExternal.TagIndex = Symbol->Other->getIndex();
1013  }
1014  }
1015 
1016  // Fixup associative COMDAT sections.
1017  for (auto &Section : Sections) {
1018  if (Section->Symbol->Aux[0].Aux.SectionDefinition.Selection !=
1020  continue;
1021 
1022  const MCSectionCOFF &MCSec = *Section->MCSection;
1023 
1024  const MCSymbol *COMDAT = MCSec.getCOMDATSymbol();
1025  assert(COMDAT);
1026  COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(COMDAT);
1027  assert(COMDATSymbol);
1028  COFFSection *Assoc = COMDATSymbol->Section;
1029  if (!Assoc)
1031  Twine("Missing associated COMDAT section for section ") +
1032  MCSec.getSectionName());
1033 
1034  // Skip this section if the associated section is unused.
1035  if (Assoc->Number == -1)
1036  continue;
1037 
1038  Section->Symbol->Aux[0].Aux.SectionDefinition.Number = Assoc->Number;
1039  }
1040 
1041  assignFileOffsets(Asm, Layout);
1042 
1043  // MS LINK expects to be able to use this timestamp to implement their
1044  // /INCREMENTAL feature.
1045  if (Asm.isIncrementalLinkerCompatible()) {
1046  Header.TimeDateStamp = getTime();
1047  } else {
1048  // Have deterministic output if /INCREMENTAL isn't needed. Also matches GNU.
1049  Header.TimeDateStamp = 0;
1050  }
1051 
1052  // Write it all to disk...
1053  WriteFileHeader(Header);
1054  writeSectionHeaders();
1055 
1056  // Write section contents.
1057  sections::iterator I = Sections.begin();
1058  sections::iterator IE = Sections.end();
1059  MCAssembler::iterator J = Asm.begin();
1060  MCAssembler::iterator JE = Asm.end();
1061  for (; I != IE && J != JE; ++I, ++J)
1062  writeSection(Asm, Layout, **I, *J);
1063 
1064  assert(W.OS.tell() == Header.PointerToSymbolTable &&
1065  "Header::PointerToSymbolTable is insane!");
1066 
1067  // Write a symbol table.
1068  for (auto &Symbol : Symbols)
1069  if (Symbol->getIndex() != -1)
1070  WriteSymbol(*Symbol);
1071 
1072  // Write a string table, which completes the entire COFF file.
1073  Strings.write(W.OS);
1074 
1075  return W.OS.tell() - StartOffset;
1076 }
1077 
1079  : Machine(Machine_) {}
1080 
1081 // Pin the vtable to this file.
1082 void MCWinCOFFObjectTargetWriter::anchor() {}
1083 
1084 //------------------------------------------------------------------------------
1085 // WinCOFFObjectWriter factory function
1086 
1087 std::unique_ptr<MCObjectWriter> llvm::createWinCOFFObjectWriter(
1088  std::unique_ptr<MCWinCOFFObjectTargetWriter> MOTW, raw_pwrite_stream &OS) {
1089  return llvm::make_unique<WinCOFFObjectWriter>(std::move(MOTW), OS);
1090 }
constexpr bool isUInt< 32 >(uint64_t x)
Definition: MathExtras.h:341
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:39
uint32_t getCRC() const
Definition: JamCRC.h:42
static uint64_t getSymbolValue(const MCSymbol &Symbol, const MCAsmLayout &Layout)
uint32_t getIndex() const
Get the (implementation defined) index.
Definition: MCSymbol.h:310
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:115
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
bool isVariable() const
isVariable - Check if this is a variable symbol.
Definition: MCSymbol.h:294
This represents an "assembler immediate".
Definition: MCValue.h:40
uint64_t getSectionAddressSize(const MCSection *Sec) const
Get the address space size of the given section, as it effects layout.
Definition: MCFragment.cpp:176
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:137
uint32_t SizeOfRawData
Definition: COFF.h:275
iterator begin()
Definition: MCAssembler.h:330
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
char Name[NameSize]
Definition: COFF.h:272
static void encodeBase64StringEntry(char *Buffer, uint64_t Value)
static std::time_t getTime()
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:504
void write32le(void *P, uint32_t V)
Definition: Endian.h:404
std::map< SymbolStringPtr, JITEvaluatedSymbol > SymbolMap
A map from symbol names (as SymbolStringPtrs) to JITSymbols (address/flags pairs).
Definition: Core.h:50
uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew=0)
Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Alig...
Definition: MathExtras.h:677
bool isCommon() const
Is this a &#39;common&#39; symbol.
Definition: MCSymbol.h:380
Defines the object file and target independent interfaces used by the assembler backend to write nati...
uint16_t Machine
Definition: COFF.h:67
static const char BigObjMagic[]
Definition: COFF.h:39
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:74
unsigned getAlignment() const
Definition: MCSection.h:117
This represents a section on Windows.
Definition: MCSectionCOFF.h:27
MCContext & getContext() const
Definition: MCAssembler.h:278
int64_t getConstant() const
Definition: MCValue.h:47
const MCSymbolRefExpr * getSymB() const
Definition: MCValue.h:49
static uint32_t getAlignment(const MCSectionCOFF &Sec)
uint16_t Characteristics
Definition: COFF.h:73
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:29
uint32_t PointerToSymbolTable
Definition: COFF.h:70
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:166
const int32_t MaxNumberOfSections16
Definition: COFF.h:34
ELFYAML::ELF_STO Other
Definition: ELFYAML.cpp:770
ArrayRef< std::string > getFileNames()
Definition: MCAssembler.h:436
Utility for building string tables with deduplicated suffixes.
A two-byte section relative fixup.
Definition: MCFixup.h:41
unsigned getCharacteristics() const
Definition: MCSectionCOFF.h:73
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
COFF::MachineTypes Machine
Definition: COFFYAML.cpp:363
Type is formed as (base + (derived << SCT_COMPLEX_TYPE_SHIFT))
Definition: COFF.h:266
iterator end()
Definition: MCAssembler.h:333
uint16_t SizeOfOptionalHeader
Definition: COFF.h:72
uint32_t SymbolTableIndex
Definition: COFF.h:327
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
virtual void reset()
lifetime management
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:130
bool isTemporary() const
isTemporary - Check if this is an assembler temporary symbol.
Definition: MCSymbol.h:220
const MCSymbolRefExpr * getSymA() const
Definition: MCValue.h:48
uint64_t getCommonSize() const
Return the size of a &#39;common&#39; symbol.
Definition: MCSymbol.h:336
void reportError(SMLoc L, const Twine &Msg)
Definition: MCContext.cpp:588
uint32_t getOffset() const
Definition: MCFixup.h:125
void writeSectionData(raw_ostream &OS, const MCSection *Section, const MCAsmLayout &Layout) const
Emit the section contents to OS.
void toVector(SmallVectorImpl< char > &Out) const
Append the concatenated string into the given SmallString or SmallVector.
Definition: Twine.cpp:33
bool isExternal() const
Definition: MCSymbol.h:393
StringRef getSectionName() const
Definition: MCSectionCOFF.h:72
static wasm::ValType getType(const TargetRegisterClass *RC)
uint16_t NumberOfLineNumbers
Definition: COFF.h:280
uint32_t VirtualAddress
Definition: COFF.h:274
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
std::unique_ptr< MCObjectWriter > createWinCOFFObjectWriter(std::unique_ptr< MCWinCOFFObjectTargetWriter > MOTW, raw_pwrite_stream &OS)
Construct a new Win COFF writer instance.
uint32_t TimeDateStamp
Definition: COFF.h:69
uint64_t getFragmentOffset(const MCFragment *F) const
Get the offset of the given fragment inside its containing section.
Definition: MCFragment.cpp:78
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:859
PowerPC TLS Dynamic Call Fixup
SMLoc getLoc() const
Definition: MCFixup.h:166
COFFYAML::AuxSymbolType AuxType
Definition: COFFYAML.cpp:345
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
Definition: STLExtras.h:1032
uint32_t PointerToRelocations
Definition: COFF.h:277
MCAsmBackend & getBackend() const
Definition: MCAssembler.h:286
MCSymbol * getCOMDATSymbol() const
Definition: MCSectionCOFF.h:74
const MCSymbol & getSymbol() const
Definition: MCExpr.h:334
bool isUndefined(bool SetUsed=true) const
isUndefined - Check if this symbol undefined (i.e., implicitly defined).
Definition: MCSymbol.h:257
MCFragment * getFragment(bool SetUsed=true) const
Definition: MCSymbol.h:384
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
An iterator type that allows iterating over the pointees via some other iterator. ...
Definition: iterator.h:289
uint32_t PointerToRawData
Definition: COFF.h:276
uint32_t VirtualSize
Definition: COFF.h:273
Target - Wrapper for Target specific information.
MCSection * getParent() const
Definition: MCFragment.h:99
int getSelection() const
Definition: MCSectionCOFF.h:75
uint32_t NumberOfSymbols
Definition: COFF.h:71
virtual bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm, const MCSymbol &A, const MCSymbol &B, bool InSet) const
Adapter to write values to a stream in a particular byte order.
Definition: EndianStream.h:52
pointer data()
Return a pointer to the vector&#39;s buffer, even if empty().
Definition: SmallVector.h:144
int32_t NumberOfSections
Definition: COFF.h:68
#define I(x, y, z)
Definition: MD5.cpp:58
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:323
uint16_t getClass() const
Definition: MCSymbolCOFF.h:41
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:203
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:341
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
uint16_t NumberOfRelocations
Definition: COFF.h:279
bool isIncrementalLinkerCompatible() const
Definition: MCAssembler.h:307
const MCExpr * getVariableValue(bool SetUsed=true) const
getVariableValue - Get the value for variable symbols.
Definition: MCSymbol.h:299
LLVM Value Representation.
Definition: Value.h:73
static const char * name
void update(ArrayRef< char > Data)
Definition: JamCRC.cpp:92
uint32_t PointerToLineNumbers
Definition: COFF.h:278
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
uint16_t getType() const
Definition: MCSymbolCOFF.h:34
static bool isAssociative(const COFFSection &Section)
uint32_t Characteristics
Definition: COFF.h:281
void setIndex(uint32_t Value) const
Set the (implementation defined) index.
Definition: MCSymbol.h:315
Twine concat(const Twine &Suffix) const
Definition: Twine.h:486
const MCSymbol * getBaseSymbol(const MCSymbol &Symbol) const
If this symbol is equivalent to A + Constant, return A.
Definition: MCFragment.cpp:140
uint32_t VirtualAddress
Definition: COFF.h:326
A function that returns a base type.
Definition: COFF.h:262