1//===-- llvm/MC/WinCOFFObjectWriter.cpp -------------------------*- C++ -*-===//
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/MC/MCWinCOFFObjectWriter.h"
15#include "llvm/ADT/DenseMap.h"
16#include "llvm/ADT/STLExtras.h"
17#include "llvm/ADT/StringMap.h"
18#include "llvm/ADT/StringRef.h"
19#include "llvm/ADT/Twine.h"
20#include "llvm/Config/config.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/MCObjectFileInfo.h"
26#include "llvm/MC/MCObjectWriter.h"
27#include "llvm/MC/MCSection.h"
28#include "llvm/MC/MCSectionCOFF.h"
29#include "llvm/MC/MCSymbolCOFF.h"
30#include "llvm/MC/MCValue.h"
31#include "llvm/MC/StringTableBuilder.h"
32#include "llvm/Support/COFF.h"
33#include "llvm/Support/Debug.h"
34#include "llvm/Support/Endian.h"
35#include "llvm/Support/ErrorHandling.h"
36#include "llvm/Support/JamCRC.h"
37#include "llvm/Support/TimeValue.h"
38#include <cstdio>
39#include <ctime>
40
41using namespace llvm;
42
43#define DEBUG_TYPE "WinCOFFObjectWriter"
44
45namespace {
46typedef SmallString<COFF::NameSize> name;
47
48enum AuxiliaryType {
49  ATFunctionDefinition,
50  ATbfAndefSymbol,
51  ATWeakExternal,
52  ATFile,
53  ATSectionDefinition
54};
55
56struct AuxSymbol {
57  AuxiliaryType AuxType;
58  COFF::Auxiliary Aux;
59};
60
61class COFFSymbol;
62class COFFSection;
63
64class COFFSymbol {
65public:
66  COFF::symbol Data;
67
68  typedef SmallVector<AuxSymbol, 1> AuxiliarySymbols;
69
70  name Name;
71  int Index;
72  AuxiliarySymbols Aux;
73  COFFSymbol *Other;
74  COFFSection *Section;
75  int Relocations;
76
77  const MCSymbol *MC;
78
79  COFFSymbol(StringRef name);
80  void set_name_offset(uint32_t Offset);
81
82  int64_t getIndex() const { return Index; }
83  void setIndex(int Value) {
84    Index = Value;
85    if (MC)
86      MC->setIndex(static_cast<uint32_t>(Value));
87  }
88};
89
90// This class contains staging data for a COFF relocation entry.
91struct COFFRelocation {
92  COFF::relocation Data;
93  COFFSymbol *Symb;
94
95  COFFRelocation() : Symb(nullptr) {}
96  static size_t size() { return COFF::RelocationSize; }
97};
98
99typedef std::vector<COFFRelocation> relocations;
100
101class COFFSection {
102public:
103  COFF::section Header;
104
105  std::string Name;
106  int Number;
107  MCSectionCOFF const *MCSection;
108  COFFSymbol *Symbol;
109  relocations Relocations;
110
111  COFFSection(StringRef name);
112  static size_t size();
113};
114
115class WinCOFFObjectWriter : public MCObjectWriter {
116public:
117  typedef std::vector<std::unique_ptr<COFFSymbol>> symbols;
118  typedef std::vector<std::unique_ptr<COFFSection>> sections;
119
120  typedef DenseMap<MCSymbol const *, COFFSymbol *> symbol_map;
121  typedef DenseMap<MCSection const *, COFFSection *> section_map;
122
123  std::unique_ptr<MCWinCOFFObjectTargetWriter> TargetObjectWriter;
124
125  // Root level file contents.
126  COFF::header Header;
127  sections Sections;
128  symbols Symbols;
129  StringTableBuilder Strings{StringTableBuilder::WinCOFF};
130
131  // Maps used during object file creation.
132  section_map SectionMap;
133  symbol_map SymbolMap;
134
135  bool UseBigObj;
136
137  WinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW, raw_pwrite_stream &OS);
138
139  void reset() override {
140    memset(&Header, 0, sizeof(Header));
141    Header.Machine = TargetObjectWriter->getMachine();
142    Sections.clear();
143    Symbols.clear();
144    Strings.clear();
145    SectionMap.clear();
146    SymbolMap.clear();
147    MCObjectWriter::reset();
148  }
149
150  COFFSymbol *createSymbol(StringRef Name);
151  COFFSymbol *GetOrCreateCOFFSymbol(const MCSymbol *Symbol);
152  COFFSection *createSection(StringRef Name);
153
154  template <typename object_t, typename list_t>
155  object_t *createCOFFEntity(StringRef Name, list_t &List);
156
157  void defineSection(MCSectionCOFF const &Sec);
158  void DefineSymbol(const MCSymbol &Symbol, MCAssembler &Assembler,
159                    const MCAsmLayout &Layout);
160
161  void SetSymbolName(COFFSymbol &S);
162  void SetSectionName(COFFSection &S);
163
164  bool IsPhysicalSection(COFFSection *S);
165
166  // Entity writing methods.
167
168  void WriteFileHeader(const COFF::header &Header);
169  void WriteSymbol(const COFFSymbol &S);
170  void WriteAuxiliarySymbols(const COFFSymbol::AuxiliarySymbols &S);
171  void writeSectionHeader(const COFF::section &S);
172  void WriteRelocation(const COFF::relocation &R);
173
174  // MCObjectWriter interface implementation.
175
176  void executePostLayoutBinding(MCAssembler &Asm,
177                                const MCAsmLayout &Layout) override;
178
179  bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
180                                              const MCSymbol &SymA,
181                                              const MCFragment &FB, bool InSet,
182                                              bool IsPCRel) const override;
183
184  bool isWeak(const MCSymbol &Sym) const override;
185
186  void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
187                        const MCFragment *Fragment, const MCFixup &Fixup,
188                        MCValue Target, bool &IsPCRel,
189                        uint64_t &FixedValue) override;
190
191  void writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
192};
193}
194
195static inline void write_uint32_le(void *Data, uint32_t Value) {
196  support::endian::write<uint32_t, support::little, support::unaligned>(Data,
197                                                                        Value);
198}
199
200//------------------------------------------------------------------------------
201// Symbol class implementation
202
203COFFSymbol::COFFSymbol(StringRef name)
204    : Name(name.begin(), name.end()), Other(nullptr), Section(nullptr),
205      Relocations(0), MC(nullptr) {
206  memset(&Data, 0, sizeof(Data));
207}
208
209// In the case that the name does not fit within 8 bytes, the offset
210// into the string table is stored in the last 4 bytes instead, leaving
211// the first 4 bytes as 0.
212void COFFSymbol::set_name_offset(uint32_t Offset) {
213  write_uint32_le(Data.Name + 0, 0);
214  write_uint32_le(Data.Name + 4, Offset);
215}
216
217//------------------------------------------------------------------------------
218// Section class implementation
219
220COFFSection::COFFSection(StringRef name)
221    : Name(name), MCSection(nullptr), Symbol(nullptr) {
222  memset(&Header, 0, sizeof(Header));
223}
224
225size_t COFFSection::size() { return COFF::SectionSize; }
226
227//------------------------------------------------------------------------------
228// WinCOFFObjectWriter class implementation
229
230WinCOFFObjectWriter::WinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW,
231                                         raw_pwrite_stream &OS)
232    : MCObjectWriter(OS, true), TargetObjectWriter(MOTW) {
233  memset(&Header, 0, sizeof(Header));
234
235  Header.Machine = TargetObjectWriter->getMachine();
236}
237
238COFFSymbol *WinCOFFObjectWriter::createSymbol(StringRef Name) {
239  return createCOFFEntity<COFFSymbol>(Name, Symbols);
240}
241
242COFFSymbol *WinCOFFObjectWriter::GetOrCreateCOFFSymbol(const MCSymbol *Symbol) {
243  symbol_map::iterator i = SymbolMap.find(Symbol);
244  if (i != SymbolMap.end())
245    return i->second;
246  COFFSymbol *RetSymbol =
247      createCOFFEntity<COFFSymbol>(Symbol->getName(), Symbols);
248  SymbolMap[Symbol] = RetSymbol;
249  return RetSymbol;
250}
251
252COFFSection *WinCOFFObjectWriter::createSection(StringRef Name) {
253  return createCOFFEntity<COFFSection>(Name, Sections);
254}
255
256/// A template used to lookup or create a symbol/section, and initialize it if
257/// needed.
258template <typename object_t, typename list_t>
259object_t *WinCOFFObjectWriter::createCOFFEntity(StringRef Name, list_t &List) {
260  List.push_back(make_unique<object_t>(Name));
261
262  return List.back().get();
263}
264
265/// This function takes a section data object from the assembler
266/// and creates the associated COFF section staging object.
267void WinCOFFObjectWriter::defineSection(MCSectionCOFF const &Sec) {
268  COFFSection *coff_section = createSection(Sec.getSectionName());
269  COFFSymbol *coff_symbol = createSymbol(Sec.getSectionName());
270  if (Sec.getSelection() != COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) {
271    if (const MCSymbol *S = Sec.getCOMDATSymbol()) {
272      COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(S);
273      if (COMDATSymbol->Section)
274        report_fatal_error("two sections have the same comdat");
275      COMDATSymbol->Section = coff_section;
276    }
277  }
278
279  coff_section->Symbol = coff_symbol;
280  coff_symbol->Section = coff_section;
281  coff_symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_STATIC;
282
283  // In this case the auxiliary symbol is a Section Definition.
284  coff_symbol->Aux.resize(1);
285  memset(&coff_symbol->Aux[0], 0, sizeof(coff_symbol->Aux[0]));
286  coff_symbol->Aux[0].AuxType = ATSectionDefinition;
287  coff_symbol->Aux[0].Aux.SectionDefinition.Selection = Sec.getSelection();
288
289  coff_section->Header.Characteristics = Sec.getCharacteristics();
290
291  uint32_t &Characteristics = coff_section->Header.Characteristics;
292  switch (Sec.getAlignment()) {
293  case 1:
294    Characteristics |= COFF::IMAGE_SCN_ALIGN_1BYTES;
295    break;
296  case 2:
297    Characteristics |= COFF::IMAGE_SCN_ALIGN_2BYTES;
298    break;
299  case 4:
300    Characteristics |= COFF::IMAGE_SCN_ALIGN_4BYTES;
301    break;
302  case 8:
303    Characteristics |= COFF::IMAGE_SCN_ALIGN_8BYTES;
304    break;
305  case 16:
306    Characteristics |= COFF::IMAGE_SCN_ALIGN_16BYTES;
307    break;
308  case 32:
309    Characteristics |= COFF::IMAGE_SCN_ALIGN_32BYTES;
310    break;
311  case 64:
312    Characteristics |= COFF::IMAGE_SCN_ALIGN_64BYTES;
313    break;
314  case 128:
315    Characteristics |= COFF::IMAGE_SCN_ALIGN_128BYTES;
316    break;
317  case 256:
318    Characteristics |= COFF::IMAGE_SCN_ALIGN_256BYTES;
319    break;
320  case 512:
321    Characteristics |= COFF::IMAGE_SCN_ALIGN_512BYTES;
322    break;
323  case 1024:
324    Characteristics |= COFF::IMAGE_SCN_ALIGN_1024BYTES;
325    break;
326  case 2048:
327    Characteristics |= COFF::IMAGE_SCN_ALIGN_2048BYTES;
328    break;
329  case 4096:
330    Characteristics |= COFF::IMAGE_SCN_ALIGN_4096BYTES;
331    break;
332  case 8192:
333    Characteristics |= COFF::IMAGE_SCN_ALIGN_8192BYTES;
334    break;
335  default:
336    llvm_unreachable("unsupported section alignment");
337  }
338
339  // Bind internal COFF section to MC section.
340  coff_section->MCSection = &Sec;
341  SectionMap[&Sec] = coff_section;
342}
343
344static uint64_t getSymbolValue(const MCSymbol &Symbol,
345                               const MCAsmLayout &Layout) {
346  if (Symbol.isCommon() && Symbol.isExternal())
347    return Symbol.getCommonSize();
348
349  uint64_t Res;
350  if (!Layout.getSymbolOffset(Symbol, Res))
351    return 0;
352
353  return Res;
354}
355
356/// This function takes a symbol data object from the assembler
357/// and creates the associated COFF symbol staging object.
358void WinCOFFObjectWriter::DefineSymbol(const MCSymbol &Symbol,
359                                       MCAssembler &Assembler,
360                                       const MCAsmLayout &Layout) {
361  COFFSymbol *coff_symbol = GetOrCreateCOFFSymbol(&Symbol);
362
363  if (cast<MCSymbolCOFF>(Symbol).isWeakExternal()) {
364    coff_symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
365
366    if (Symbol.isVariable()) {
367      const MCSymbolRefExpr *SymRef =
368          dyn_cast<MCSymbolRefExpr>(Symbol.getVariableValue());
369
370      if (!SymRef)
371        report_fatal_error("Weak externals may only alias symbols");
372
373      coff_symbol->Other = GetOrCreateCOFFSymbol(&SymRef->getSymbol());
374    } else {
375      std::string WeakName = (".weak." + Symbol.getName() + ".default").str();
376      COFFSymbol *WeakDefault = createSymbol(WeakName);
377      WeakDefault->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
378      WeakDefault->Data.StorageClass = COFF::IMAGE_SYM_CLASS_EXTERNAL;
379      WeakDefault->Data.Type = 0;
380      WeakDefault->Data.Value = 0;
381      coff_symbol->Other = WeakDefault;
382    }
383
384    // Setup the Weak External auxiliary symbol.
385    coff_symbol->Aux.resize(1);
386    memset(&coff_symbol->Aux[0], 0, sizeof(coff_symbol->Aux[0]));
387    coff_symbol->Aux[0].AuxType = ATWeakExternal;
388    coff_symbol->Aux[0].Aux.WeakExternal.TagIndex = 0;
389    coff_symbol->Aux[0].Aux.WeakExternal.Characteristics =
390        COFF::IMAGE_WEAK_EXTERN_SEARCH_LIBRARY;
391
392    coff_symbol->MC = &Symbol;
393  } else {
394    const MCSymbol *Base = Layout.getBaseSymbol(Symbol);
395    coff_symbol->Data.Value = getSymbolValue(Symbol, Layout);
396
397    const MCSymbolCOFF &SymbolCOFF = cast<MCSymbolCOFF>(Symbol);
398    coff_symbol->Data.Type = SymbolCOFF.getType();
399    coff_symbol->Data.StorageClass = SymbolCOFF.getClass();
400
401    // If no storage class was specified in the streamer, define it here.
402    if (coff_symbol->Data.StorageClass == COFF::IMAGE_SYM_CLASS_NULL) {
403      bool IsExternal = Symbol.isExternal() ||
404                        (!Symbol.getFragment() && !Symbol.isVariable());
405
406      coff_symbol->Data.StorageClass = IsExternal
407                                           ? COFF::IMAGE_SYM_CLASS_EXTERNAL
408                                           : COFF::IMAGE_SYM_CLASS_STATIC;
409    }
410
411    if (!Base) {
412      coff_symbol->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
413    } else {
414      if (Base->getFragment()) {
415        COFFSection *Sec = SectionMap[Base->getFragment()->getParent()];
416
417        if (coff_symbol->Section && coff_symbol->Section != Sec)
418          report_fatal_error("conflicting sections for symbol");
419
420        coff_symbol->Section = Sec;
421      }
422    }
423
424    coff_symbol->MC = &Symbol;
425  }
426}
427
428// Maximum offsets for different string table entry encodings.
429static const unsigned Max6DecimalOffset = 999999;
430static const unsigned Max7DecimalOffset = 9999999;
431static const uint64_t MaxBase64Offset = 0xFFFFFFFFFULL; // 64^6, including 0
432
433// Encode a string table entry offset in base 64, padded to 6 chars, and
434// prefixed with a double slash: '//AAAAAA', '//AAAAAB', ...
435// Buffer must be at least 8 bytes large. No terminating null appended.
436static void encodeBase64StringEntry(char *Buffer, uint64_t Value) {
437  assert(Value > Max7DecimalOffset && Value <= MaxBase64Offset &&
438         "Illegal section name encoding for value");
439
440  static const char Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
441                                 "abcdefghijklmnopqrstuvwxyz"
442                                 "0123456789+/";
443
444  Buffer[0] = '/';
445  Buffer[1] = '/';
446
447  char *Ptr = Buffer + 7;
448  for (unsigned i = 0; i < 6; ++i) {
449    unsigned Rem = Value % 64;
450    Value /= 64;
451    *(Ptr--) = Alphabet[Rem];
452  }
453}
454
455void WinCOFFObjectWriter::SetSectionName(COFFSection &S) {
456  if (S.Name.size() > COFF::NameSize) {
457    uint64_t StringTableEntry = Strings.getOffset(S.Name);
458
459    if (StringTableEntry <= Max6DecimalOffset) {
460      std::sprintf(S.Header.Name, "/%d", unsigned(StringTableEntry));
461    } else if (StringTableEntry <= Max7DecimalOffset) {
462      // With seven digits, we have to skip the terminating null. Because
463      // sprintf always appends it, we use a larger temporary buffer.
464      char buffer[9] = {};
465      std::sprintf(buffer, "/%d", unsigned(StringTableEntry));
466      std::memcpy(S.Header.Name, buffer, 8);
467    } else if (StringTableEntry <= MaxBase64Offset) {
468      // Starting with 10,000,000, offsets are encoded as base64.
469      encodeBase64StringEntry(S.Header.Name, StringTableEntry);
470    } else {
471      report_fatal_error("COFF string table is greater than 64 GB.");
472    }
473  } else
474    std::memcpy(S.Header.Name, S.Name.c_str(), S.Name.size());
475}
476
477void WinCOFFObjectWriter::SetSymbolName(COFFSymbol &S) {
478  if (S.Name.size() > COFF::NameSize)
479    S.set_name_offset(Strings.getOffset(S.Name));
480  else
481    std::memcpy(S.Data.Name, S.Name.c_str(), S.Name.size());
482}
483
484bool WinCOFFObjectWriter::IsPhysicalSection(COFFSection *S) {
485  return (S->Header.Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) ==
486         0;
487}
488
489//------------------------------------------------------------------------------
490// entity writing methods
491
492void WinCOFFObjectWriter::WriteFileHeader(const COFF::header &Header) {
493  if (UseBigObj) {
494    writeLE16(COFF::IMAGE_FILE_MACHINE_UNKNOWN);
495    writeLE16(0xFFFF);
496    writeLE16(COFF::BigObjHeader::MinBigObjectVersion);
497    writeLE16(Header.Machine);
498    writeLE32(Header.TimeDateStamp);
499    writeBytes(StringRef(COFF::BigObjMagic, sizeof(COFF::BigObjMagic)));
500    writeLE32(0);
501    writeLE32(0);
502    writeLE32(0);
503    writeLE32(0);
504    writeLE32(Header.NumberOfSections);
505    writeLE32(Header.PointerToSymbolTable);
506    writeLE32(Header.NumberOfSymbols);
507  } else {
508    writeLE16(Header.Machine);
509    writeLE16(static_cast<int16_t>(Header.NumberOfSections));
510    writeLE32(Header.TimeDateStamp);
511    writeLE32(Header.PointerToSymbolTable);
512    writeLE32(Header.NumberOfSymbols);
513    writeLE16(Header.SizeOfOptionalHeader);
514    writeLE16(Header.Characteristics);
515  }
516}
517
518void WinCOFFObjectWriter::WriteSymbol(const COFFSymbol &S) {
519  writeBytes(StringRef(S.Data.Name, COFF::NameSize));
520  writeLE32(S.Data.Value);
521  if (UseBigObj)
522    writeLE32(S.Data.SectionNumber);
523  else
524    writeLE16(static_cast<int16_t>(S.Data.SectionNumber));
525  writeLE16(S.Data.Type);
526  write8(S.Data.StorageClass);
527  write8(S.Data.NumberOfAuxSymbols);
528  WriteAuxiliarySymbols(S.Aux);
529}
530
531void WinCOFFObjectWriter::WriteAuxiliarySymbols(
532    const COFFSymbol::AuxiliarySymbols &S) {
533  for (COFFSymbol::AuxiliarySymbols::const_iterator i = S.begin(), e = S.end();
534       i != e; ++i) {
535    switch (i->AuxType) {
536    case ATFunctionDefinition:
537      writeLE32(i->Aux.FunctionDefinition.TagIndex);
538      writeLE32(i->Aux.FunctionDefinition.TotalSize);
539      writeLE32(i->Aux.FunctionDefinition.PointerToLinenumber);
540      writeLE32(i->Aux.FunctionDefinition.PointerToNextFunction);
541      WriteZeros(sizeof(i->Aux.FunctionDefinition.unused));
542      if (UseBigObj)
543        WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
544      break;
545    case ATbfAndefSymbol:
546      WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused1));
547      writeLE16(i->Aux.bfAndefSymbol.Linenumber);
548      WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused2));
549      writeLE32(i->Aux.bfAndefSymbol.PointerToNextFunction);
550      WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused3));
551      if (UseBigObj)
552        WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
553      break;
554    case ATWeakExternal:
555      writeLE32(i->Aux.WeakExternal.TagIndex);
556      writeLE32(i->Aux.WeakExternal.Characteristics);
557      WriteZeros(sizeof(i->Aux.WeakExternal.unused));
558      if (UseBigObj)
559        WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
560      break;
561    case ATFile:
562      writeBytes(
563          StringRef(reinterpret_cast<const char *>(&i->Aux),
564                    UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size));
565      break;
566    case ATSectionDefinition:
567      writeLE32(i->Aux.SectionDefinition.Length);
568      writeLE16(i->Aux.SectionDefinition.NumberOfRelocations);
569      writeLE16(i->Aux.SectionDefinition.NumberOfLinenumbers);
570      writeLE32(i->Aux.SectionDefinition.CheckSum);
571      writeLE16(static_cast<int16_t>(i->Aux.SectionDefinition.Number));
572      write8(i->Aux.SectionDefinition.Selection);
573      WriteZeros(sizeof(i->Aux.SectionDefinition.unused));
574      writeLE16(static_cast<int16_t>(i->Aux.SectionDefinition.Number >> 16));
575      if (UseBigObj)
576        WriteZeros(COFF::Symbol32Size - COFF::Symbol16Size);
577      break;
578    }
579  }
580}
581
582void WinCOFFObjectWriter::writeSectionHeader(const COFF::section &S) {
583  writeBytes(StringRef(S.Name, COFF::NameSize));
584
585  writeLE32(S.VirtualSize);
586  writeLE32(S.VirtualAddress);
587  writeLE32(S.SizeOfRawData);
588  writeLE32(S.PointerToRawData);
589  writeLE32(S.PointerToRelocations);
590  writeLE32(S.PointerToLineNumbers);
591  writeLE16(S.NumberOfRelocations);
592  writeLE16(S.NumberOfLineNumbers);
593  writeLE32(S.Characteristics);
594}
595
596void WinCOFFObjectWriter::WriteRelocation(const COFF::relocation &R) {
597  writeLE32(R.VirtualAddress);
598  writeLE32(R.SymbolTableIndex);
599  writeLE16(R.Type);
600}
601
602////////////////////////////////////////////////////////////////////////////////
603// MCObjectWriter interface implementations
604
605void WinCOFFObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
606                                                   const MCAsmLayout &Layout) {
607  // "Define" each section & symbol. This creates section & symbol
608  // entries in the staging area.
609  for (const auto &Section : Asm)
610    defineSection(static_cast<const MCSectionCOFF &>(Section));
611
612  for (const MCSymbol &Symbol : Asm.symbols())
613    if (!Symbol.isTemporary())
614      DefineSymbol(Symbol, Asm, Layout);
615}
616
617bool WinCOFFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
618    const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB,
619    bool InSet, bool IsPCRel) const {
620  // MS LINK expects to be able to replace all references to a function with a
621  // thunk to implement their /INCREMENTAL feature.  Make sure we don't optimize
622  // away any relocations to functions.
623  uint16_t Type = cast<MCSymbolCOFF>(SymA).getType();
624  if (Asm.isIncrementalLinkerCompatible() &&
625      (Type >> COFF::SCT_COMPLEX_TYPE_SHIFT) == COFF::IMAGE_SYM_DTYPE_FUNCTION)
626    return false;
627  return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB,
628                                                                InSet, IsPCRel);
629}
630
631bool WinCOFFObjectWriter::isWeak(const MCSymbol &Sym) const {
632  if (!Sym.isExternal())
633    return false;
634
635  if (!Sym.isInSection())
636    return false;
637
638  const auto &Sec = cast<MCSectionCOFF>(Sym.getSection());
639  if (!Sec.getCOMDATSymbol())
640    return false;
641
642  // It looks like for COFF it is invalid to replace a reference to a global
643  // in a comdat with a reference to a local.
644  // FIXME: Add a specification reference if available.
645  return true;
646}
647
648void WinCOFFObjectWriter::recordRelocation(
649    MCAssembler &Asm, const MCAsmLayout &Layout, const MCFragment *Fragment,
650    const MCFixup &Fixup, MCValue Target, bool &IsPCRel, uint64_t &FixedValue) {
651  assert(Target.getSymA() && "Relocation must reference a symbol!");
652
653  const MCSymbol &A = Target.getSymA()->getSymbol();
654  if (!A.isRegistered()) {
655    Asm.getContext().reportError(Fixup.getLoc(),
656                                      Twine("symbol '") + A.getName() +
657                                          "' can not be undefined");
658    return;
659  }
660  if (A.isTemporary() && A.isUndefined()) {
661    Asm.getContext().reportError(Fixup.getLoc(),
662                                      Twine("assembler label '") + A.getName() +
663                                          "' can not be undefined");
664    return;
665  }
666
667  MCSection *Section = Fragment->getParent();
668
669  // Mark this symbol as requiring an entry in the symbol table.
670  assert(SectionMap.find(Section) != SectionMap.end() &&
671         "Section must already have been defined in executePostLayoutBinding!");
672
673  COFFSection *coff_section = SectionMap[Section];
674  const MCSymbolRefExpr *SymB = Target.getSymB();
675  bool CrossSection = false;
676
677  if (SymB) {
678    const MCSymbol *B = &SymB->getSymbol();
679    if (!B->getFragment()) {
680      Asm.getContext().reportError(
681          Fixup.getLoc(),
682          Twine("symbol '") + B->getName() +
683              "' can not be undefined in a subtraction expression");
684      return;
685    }
686
687    if (!A.getFragment()) {
688      Asm.getContext().reportError(
689          Fixup.getLoc(),
690          Twine("symbol '") + A.getName() +
691              "' can not be undefined in a subtraction expression");
692      return;
693    }
694
695    CrossSection = &A.getSection() != &B->getSection();
696
697    // Offset of the symbol in the section
698    int64_t OffsetOfB = Layout.getSymbolOffset(*B);
699
700    // In the case where we have SymbA and SymB, we just need to store the delta
701    // between the two symbols.  Update FixedValue to account for the delta, and
702    // skip recording the relocation.
703    if (!CrossSection) {
704      int64_t OffsetOfA = Layout.getSymbolOffset(A);
705      FixedValue = (OffsetOfA - OffsetOfB) + Target.getConstant();
706      return;
707    }
708
709    // Offset of the relocation in the section
710    int64_t OffsetOfRelocation =
711        Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
712
713    FixedValue = (OffsetOfRelocation - OffsetOfB) + Target.getConstant();
714  } else {
715    FixedValue = Target.getConstant();
716  }
717
718  COFFRelocation Reloc;
719
720  Reloc.Data.SymbolTableIndex = 0;
721  Reloc.Data.VirtualAddress = Layout.getFragmentOffset(Fragment);
722
723  // Turn relocations for temporary symbols into section relocations.
724  if (A.isTemporary() || CrossSection) {
725    MCSection *TargetSection = &A.getSection();
726    assert(
727        SectionMap.find(TargetSection) != SectionMap.end() &&
728        "Section must already have been defined in executePostLayoutBinding!");
729    Reloc.Symb = SectionMap[TargetSection]->Symbol;
730    FixedValue += Layout.getSymbolOffset(A);
731  } else {
732    assert(
733        SymbolMap.find(&A) != SymbolMap.end() &&
734        "Symbol must already have been defined in executePostLayoutBinding!");
735    Reloc.Symb = SymbolMap[&A];
736  }
737
738  ++Reloc.Symb->Relocations;
739
740  Reloc.Data.VirtualAddress += Fixup.getOffset();
741  Reloc.Data.Type = TargetObjectWriter->getRelocType(
742      Target, Fixup, CrossSection, Asm.getBackend());
743
744  // FIXME: Can anyone explain what this does other than adjust for the size
745  // of the offset?
746  if ((Header.Machine == COFF::IMAGE_FILE_MACHINE_AMD64 &&
747       Reloc.Data.Type == COFF::IMAGE_REL_AMD64_REL32) ||
748      (Header.Machine == COFF::IMAGE_FILE_MACHINE_I386 &&
749       Reloc.Data.Type == COFF::IMAGE_REL_I386_REL32))
750    FixedValue += 4;
751
752  if (Header.Machine == COFF::IMAGE_FILE_MACHINE_ARMNT) {
753    switch (Reloc.Data.Type) {
754    case COFF::IMAGE_REL_ARM_ABSOLUTE:
755    case COFF::IMAGE_REL_ARM_ADDR32:
756    case COFF::IMAGE_REL_ARM_ADDR32NB:
757    case COFF::IMAGE_REL_ARM_TOKEN:
758    case COFF::IMAGE_REL_ARM_SECTION:
759    case COFF::IMAGE_REL_ARM_SECREL:
760      break;
761    case COFF::IMAGE_REL_ARM_BRANCH11:
762    case COFF::IMAGE_REL_ARM_BLX11:
763    // IMAGE_REL_ARM_BRANCH11 and IMAGE_REL_ARM_BLX11 are only used for
764    // pre-ARMv7, which implicitly rules it out of ARMNT (it would be valid
765    // for Windows CE).
766    case COFF::IMAGE_REL_ARM_BRANCH24:
767    case COFF::IMAGE_REL_ARM_BLX24:
768    case COFF::IMAGE_REL_ARM_MOV32A:
769      // IMAGE_REL_ARM_BRANCH24, IMAGE_REL_ARM_BLX24, IMAGE_REL_ARM_MOV32A are
770      // only used for ARM mode code, which is documented as being unsupported
771      // by Windows on ARM.  Empirical proof indicates that masm is able to
772      // generate the relocations however the rest of the MSVC toolchain is
773      // unable to handle it.
774      llvm_unreachable("unsupported relocation");
775      break;
776    case COFF::IMAGE_REL_ARM_MOV32T:
777      break;
778    case COFF::IMAGE_REL_ARM_BRANCH20T:
779    case COFF::IMAGE_REL_ARM_BRANCH24T:
780    case COFF::IMAGE_REL_ARM_BLX23T:
781      // IMAGE_REL_BRANCH20T, IMAGE_REL_ARM_BRANCH24T, IMAGE_REL_ARM_BLX23T all
782      // perform a 4 byte adjustment to the relocation.  Relative branches are
783      // offset by 4 on ARM, however, because there is no RELA relocations, all
784      // branches are offset by 4.
785      FixedValue = FixedValue + 4;
786      break;
787    }
788  }
789
790  if (TargetObjectWriter->recordRelocation(Fixup))
791    coff_section->Relocations.push_back(Reloc);
792}
793
794void WinCOFFObjectWriter::writeObject(MCAssembler &Asm,
795                                      const MCAsmLayout &Layout) {
796  size_t SectionsSize = Sections.size();
797  if (SectionsSize > static_cast<size_t>(INT32_MAX))
798    report_fatal_error(
799        "PE COFF object files can't have more than 2147483647 sections");
800
801  // Assign symbol and section indexes and offsets.
802  int32_t NumberOfSections = static_cast<int32_t>(SectionsSize);
803
804  UseBigObj = NumberOfSections > COFF::MaxNumberOfSections16;
805
806  // Assign section numbers.
807  size_t Number = 1;
808  for (const auto &Section : Sections) {
809    Section->Number = Number;
810    Section->Symbol->Data.SectionNumber = Number;
811    Section->Symbol->Aux[0].Aux.SectionDefinition.Number = Number;
812    ++Number;
813  }
814
815  Header.NumberOfSections = NumberOfSections;
816  Header.NumberOfSymbols = 0;
817
818  for (const std::string &Name : Asm.getFileNames()) {
819    // round up to calculate the number of auxiliary symbols required
820    unsigned SymbolSize = UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size;
821    unsigned Count = (Name.size() + SymbolSize - 1) / SymbolSize;
822
823    COFFSymbol *file = createSymbol(".file");
824    file->Data.SectionNumber = COFF::IMAGE_SYM_DEBUG;
825    file->Data.StorageClass = COFF::IMAGE_SYM_CLASS_FILE;
826    file->Aux.resize(Count);
827
828    unsigned Offset = 0;
829    unsigned Length = Name.size();
830    for (auto &Aux : file->Aux) {
831      Aux.AuxType = ATFile;
832
833      if (Length > SymbolSize) {
834        memcpy(&Aux.Aux, Name.c_str() + Offset, SymbolSize);
835        Length = Length - SymbolSize;
836      } else {
837        memcpy(&Aux.Aux, Name.c_str() + Offset, Length);
838        memset((char *)&Aux.Aux + Length, 0, SymbolSize - Length);
839        break;
840      }
841
842      Offset += SymbolSize;
843    }
844  }
845
846  for (auto &Symbol : Symbols) {
847    // Update section number & offset for symbols that have them.
848    if (Symbol->Section)
849      Symbol->Data.SectionNumber = Symbol->Section->Number;
850    Symbol->setIndex(Header.NumberOfSymbols++);
851    // Update auxiliary symbol info.
852    Symbol->Data.NumberOfAuxSymbols = Symbol->Aux.size();
853    Header.NumberOfSymbols += Symbol->Data.NumberOfAuxSymbols;
854  }
855
856  // Build string table.
857  for (const auto &S : Sections)
858    if (S->Name.size() > COFF::NameSize)
859      Strings.add(S->Name);
860  for (const auto &S : Symbols)
861    if (S->Name.size() > COFF::NameSize)
862      Strings.add(S->Name);
863  Strings.finalize();
864
865  // Set names.
866  for (const auto &S : Sections)
867    SetSectionName(*S);
868  for (auto &S : Symbols)
869    SetSymbolName(*S);
870
871  // Fixup weak external references.
872  for (auto &Symbol : Symbols) {
873    if (Symbol->Other) {
874      assert(Symbol->getIndex() != -1);
875      assert(Symbol->Aux.size() == 1 && "Symbol must contain one aux symbol!");
876      assert(Symbol->Aux[0].AuxType == ATWeakExternal &&
877             "Symbol's aux symbol must be a Weak External!");
878      Symbol->Aux[0].Aux.WeakExternal.TagIndex = Symbol->Other->getIndex();
879    }
880  }
881
882  // Fixup associative COMDAT sections.
883  for (auto &Section : Sections) {
884    if (Section->Symbol->Aux[0].Aux.SectionDefinition.Selection !=
885        COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
886      continue;
887
888    const MCSectionCOFF &MCSec = *Section->MCSection;
889
890    const MCSymbol *COMDAT = MCSec.getCOMDATSymbol();
891    assert(COMDAT);
892    COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(COMDAT);
893    assert(COMDATSymbol);
894    COFFSection *Assoc = COMDATSymbol->Section;
895    if (!Assoc)
896      report_fatal_error(
897          Twine("Missing associated COMDAT section for section ") +
898          MCSec.getSectionName());
899
900    // Skip this section if the associated section is unused.
901    if (Assoc->Number == -1)
902      continue;
903
904    Section->Symbol->Aux[0].Aux.SectionDefinition.Number = Assoc->Number;
905  }
906
907  // Assign file offsets to COFF object file structures.
908
909  unsigned offset = getInitialOffset();
910
911  if (UseBigObj)
912    offset += COFF::Header32Size;
913  else
914    offset += COFF::Header16Size;
915  offset += COFF::SectionSize * Header.NumberOfSections;
916
917  for (const auto &Section : Asm) {
918    COFFSection *Sec = SectionMap[&Section];
919
920    if (Sec->Number == -1)
921      continue;
922
923    Sec->Header.SizeOfRawData = Layout.getSectionAddressSize(&Section);
924
925    if (IsPhysicalSection(Sec)) {
926      // Align the section data to a four byte boundary.
927      offset = RoundUpToAlignment(offset, 4);
928      Sec->Header.PointerToRawData = offset;
929
930      offset += Sec->Header.SizeOfRawData;
931    }
932
933    if (Sec->Relocations.size() > 0) {
934      bool RelocationsOverflow = Sec->Relocations.size() >= 0xffff;
935
936      if (RelocationsOverflow) {
937        // Signal overflow by setting NumberOfRelocations to max value. Actual
938        // size is found in reloc #0. Microsoft tools understand this.
939        Sec->Header.NumberOfRelocations = 0xffff;
940      } else {
941        Sec->Header.NumberOfRelocations = Sec->Relocations.size();
942      }
943      Sec->Header.PointerToRelocations = offset;
944
945      if (RelocationsOverflow) {
946        // Reloc #0 will contain actual count, so make room for it.
947        offset += COFF::RelocationSize;
948      }
949
950      offset += COFF::RelocationSize * Sec->Relocations.size();
951
952      for (auto &Relocation : Sec->Relocations) {
953        assert(Relocation.Symb->getIndex() != -1);
954        Relocation.Data.SymbolTableIndex = Relocation.Symb->getIndex();
955      }
956    }
957
958    assert(Sec->Symbol->Aux.size() == 1 &&
959           "Section's symbol must have one aux!");
960    AuxSymbol &Aux = Sec->Symbol->Aux[0];
961    assert(Aux.AuxType == ATSectionDefinition &&
962           "Section's symbol's aux symbol must be a Section Definition!");
963    Aux.Aux.SectionDefinition.Length = Sec->Header.SizeOfRawData;
964    Aux.Aux.SectionDefinition.NumberOfRelocations =
965        Sec->Header.NumberOfRelocations;
966    Aux.Aux.SectionDefinition.NumberOfLinenumbers =
967        Sec->Header.NumberOfLineNumbers;
968  }
969
970  Header.PointerToSymbolTable = offset;
971
972  // FIXME: Remove the #else branch and make the #if branch unconditional once
973  // LLVM's self host configuration is aware of /Brepro.
974#if (ENABLE_TIMESTAMPS == 1)
975  // MS LINK expects to be able to use this timestamp to implement their
976  // /INCREMENTAL feature.
977  if (Asm.isIncrementalLinkerCompatible()) {
978    std::time_t Now = time(nullptr);
979    if (Now < 0 || !isUInt<32>(Now))
980      Now = UINT32_MAX;
981    Header.TimeDateStamp = Now;
982  } else {
983    Header.TimeDateStamp = 0;
984  }
985#else
986  // We want a deterministic output. It looks like GNU as also writes 0 in here.
987  Header.TimeDateStamp = 0;
988#endif
989
990  // Write it all to disk...
991  WriteFileHeader(Header);
992
993  {
994    sections::iterator i, ie;
995    MCAssembler::iterator j, je;
996
997    for (auto &Section : Sections) {
998      if (Section->Number != -1) {
999        if (Section->Relocations.size() >= 0xffff)
1000          Section->Header.Characteristics |= COFF::IMAGE_SCN_LNK_NRELOC_OVFL;
1001        writeSectionHeader(Section->Header);
1002      }
1003    }
1004
1005    SmallVector<char, 128> SectionContents;
1006    for (i = Sections.begin(), ie = Sections.end(), j = Asm.begin(),
1007        je = Asm.end();
1008         (i != ie) && (j != je); ++i, ++j) {
1009
1010      if ((*i)->Number == -1)
1011        continue;
1012
1013      if ((*i)->Header.PointerToRawData != 0) {
1014        assert(getStream().tell() <= (*i)->Header.PointerToRawData &&
1015               "Section::PointerToRawData is insane!");
1016
1017        unsigned SectionDataPadding =
1018            (*i)->Header.PointerToRawData - getStream().tell();
1019        assert(SectionDataPadding < 4 &&
1020               "Should only need at most three bytes of padding!");
1021
1022        WriteZeros(SectionDataPadding);
1023
1024        // Save the contents of the section to a temporary buffer, we need this
1025        // to CRC the data before we dump it into the object file.
1026        SectionContents.clear();
1027        raw_svector_ostream VecOS(SectionContents);
1028        raw_pwrite_stream &OldStream = getStream();
1029        // Redirect the output stream to our buffer.
1030        setStream(VecOS);
1031        // Fill our buffer with the section data.
1032        Asm.writeSectionData(&*j, Layout);
1033        // Reset the stream back to what it was before.
1034        setStream(OldStream);
1035
1036        // Calculate our CRC with an initial value of '0', this is not how
1037        // JamCRC is specified but it aligns with the expected output.
1038        JamCRC JC(/*Init=*/0x00000000U);
1039        JC.update(SectionContents);
1040
1041        // Write the section contents to the object file.
1042        getStream() << SectionContents;
1043
1044        // Update the section definition auxiliary symbol to record the CRC.
1045        COFFSection *Sec = SectionMap[&*j];
1046        COFFSymbol::AuxiliarySymbols &AuxSyms = Sec->Symbol->Aux;
1047        assert(AuxSyms.size() == 1 &&
1048               AuxSyms[0].AuxType == ATSectionDefinition);
1049        AuxSymbol &SecDef = AuxSyms[0];
1050        SecDef.Aux.SectionDefinition.CheckSum = JC.getCRC();
1051      }
1052
1053      if ((*i)->Relocations.size() > 0) {
1054        assert(getStream().tell() == (*i)->Header.PointerToRelocations &&
1055               "Section::PointerToRelocations is insane!");
1056
1057        if ((*i)->Relocations.size() >= 0xffff) {
1058          // In case of overflow, write actual relocation count as first
1059          // relocation. Including the synthetic reloc itself (+ 1).
1060          COFF::relocation r;
1061          r.VirtualAddress = (*i)->Relocations.size() + 1;
1062          r.SymbolTableIndex = 0;
1063          r.Type = 0;
1064          WriteRelocation(r);
1065        }
1066
1067        for (const auto &Relocation : (*i)->Relocations)
1068          WriteRelocation(Relocation.Data);
1069      } else
1070        assert((*i)->Header.PointerToRelocations == 0 &&
1071               "Section::PointerToRelocations is insane!");
1072    }
1073  }
1074
1075  assert(getStream().tell() == Header.PointerToSymbolTable &&
1076         "Header::PointerToSymbolTable is insane!");
1077
1078  for (auto &Symbol : Symbols)
1079    if (Symbol->getIndex() != -1)
1080      WriteSymbol(*Symbol);
1081
1082  getStream().write(Strings.data().data(), Strings.data().size());
1083}
1084
1085MCWinCOFFObjectTargetWriter::MCWinCOFFObjectTargetWriter(unsigned Machine_)
1086    : Machine(Machine_) {}
1087
1088// Pin the vtable to this file.
1089void MCWinCOFFObjectTargetWriter::anchor() {}
1090
1091//------------------------------------------------------------------------------
1092// WinCOFFObjectWriter factory function
1093
1094MCObjectWriter *
1095llvm::createWinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW,
1096                                raw_pwrite_stream &OS) {
1097  return new WinCOFFObjectWriter(MOTW, OS);
1098}
1099