1b162290e39afd49d4c7d342333b331bc96232720Chris Lattner//===-- llvm/MC/WinCOFFObjectWriter.cpp -------------------------*- C++ -*-===//
2b162290e39afd49d4c7d342333b331bc96232720Chris Lattner//
3b162290e39afd49d4c7d342333b331bc96232720Chris Lattner//                     The LLVM Compiler Infrastructure
4b162290e39afd49d4c7d342333b331bc96232720Chris Lattner//
5b162290e39afd49d4c7d342333b331bc96232720Chris Lattner// This file is distributed under the University of Illinois Open Source
6b162290e39afd49d4c7d342333b331bc96232720Chris Lattner// License. See LICENSE.TXT for details.
7b162290e39afd49d4c7d342333b331bc96232720Chris Lattner//
8b162290e39afd49d4c7d342333b331bc96232720Chris Lattner//===----------------------------------------------------------------------===//
9b162290e39afd49d4c7d342333b331bc96232720Chris Lattner//
10b162290e39afd49d4c7d342333b331bc96232720Chris Lattner// This file contains an implementation of a Win32 COFF object file writer.
11b162290e39afd49d4c7d342333b331bc96232720Chris Lattner//
12b162290e39afd49d4c7d342333b331bc96232720Chris Lattner//===----------------------------------------------------------------------===//
13b162290e39afd49d4c7d342333b331bc96232720Chris Lattner
14df09270ae897e7fa64a7c162de163c32ee181a03Rafael Espindola#include "llvm/MC/MCWinCOFFObjectWriter.h"
15801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer#include "llvm/ADT/DenseMap.h"
16801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer#include "llvm/ADT/StringMap.h"
17801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer#include "llvm/ADT/StringRef.h"
18dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines#include "llvm/ADT/STLExtras.h"
1980646283796b20c6a1b7d8eb69ce6f0478d54383Nico Rieck#include "llvm/ADT/Twine.h"
20d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/MC/MCAsmLayout.h"
21d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/MC/MCAssembler.h"
22d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/MC/MCContext.h"
23d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/MC/MCExpr.h"
24d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/MC/MCObjectWriter.h"
25d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/MC/MCSection.h"
26d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/MC/MCSectionCOFF.h"
27d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/MC/MCSymbol.h"
28d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/MC/MCValue.h"
29801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer#include "llvm/Support/COFF.h"
30801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer#include "llvm/Support/Debug.h"
31801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer#include "llvm/Support/ErrorHandling.h"
321f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer#include "llvm/Support/TimeValue.h"
33801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer#include <cstdio>
34801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
35b162290e39afd49d4c7d342333b331bc96232720Chris Lattnerusing namespace llvm;
36b162290e39afd49d4c7d342333b331bc96232720Chris Lattner
37dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines#define DEBUG_TYPE "WinCOFFObjectWriter"
38dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
39b162290e39afd49d4c7d342333b331bc96232720Chris Lattnernamespace {
4096f498bd9f140a98321c478f517877c4767b94faDmitri Gribenkotypedef SmallString<COFF::NameSize> name;
41801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
42801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencerenum AuxiliaryType {
43801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  ATFunctionDefinition,
44801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  ATbfAndefSymbol,
45801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  ATWeakExternal,
46801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  ATFile,
47801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  ATSectionDefinition
48801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer};
49801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
50801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencerstruct AuxSymbol {
51801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  AuxiliaryType   AuxType;
52801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  COFF::Auxiliary Aux;
53801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer};
54801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
55a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencerclass COFFSymbol;
56a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencerclass COFFSection;
57a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer
58801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencerclass COFFSymbol {
59801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencerpublic:
60801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  COFF::symbol Data;
61801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
6296f498bd9f140a98321c478f517877c4767b94faDmitri Gribenko  typedef SmallVector<AuxSymbol, 1> AuxiliarySymbols;
63801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
64801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  name             Name;
659cf23a9ab466a900bc0f937bc930d398d6097766Michael J. Spencer  int              Index;
66801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  AuxiliarySymbols Aux;
67801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  COFFSymbol      *Other;
68a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer  COFFSection     *Section;
69a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer  int              Relocations;
70801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
71801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  MCSymbolData const *MCData;
72801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
7396f498bd9f140a98321c478f517877c4767b94faDmitri Gribenko  COFFSymbol(StringRef name);
74801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  size_t size() const;
75801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  void set_name_offset(uint32_t Offset);
76a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer
77a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer  bool should_keep() const;
78801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer};
79801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
80801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer// This class contains staging data for a COFF relocation entry.
81801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencerstruct COFFRelocation {
82801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  COFF::relocation Data;
83801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  COFFSymbol          *Symb;
84801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
85dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  COFFRelocation() : Symb(nullptr) {}
86801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  static size_t size() { return COFF::RelocationSize; }
87801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer};
88801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
89801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencertypedef std::vector<COFFRelocation> relocations;
90801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
91801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencerclass COFFSection {
92801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencerpublic:
93801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  COFF::section Header;
94801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
95801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  std::string          Name;
969cf23a9ab466a900bc0f937bc930d398d6097766Michael J. Spencer  int                  Number;
97801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  MCSectionData const *MCData;
98a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer  COFFSymbol          *Symbol;
99801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  relocations          Relocations;
100801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
10196f498bd9f140a98321c478f517877c4767b94faDmitri Gribenko  COFFSection(StringRef name);
102801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  static size_t size();
103801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer};
104801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
105801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer// This class holds the COFF string table.
106801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencerclass StringTable {
10796f498bd9f140a98321c478f517877c4767b94faDmitri Gribenko  typedef StringMap<size_t> map;
108801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  map Map;
109801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
110801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  void update_length();
111801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencerpublic:
112801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  std::vector<char> Data;
113801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
114801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  StringTable();
115801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  size_t size() const;
11696f498bd9f140a98321c478f517877c4767b94faDmitri Gribenko  size_t insert(StringRef String);
117801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer};
118801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
119801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencerclass WinCOFFObjectWriter : public MCObjectWriter {
120801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencerpublic:
121801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
122dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  typedef std::vector<std::unique_ptr<COFFSymbol>>  symbols;
123dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  typedef std::vector<std::unique_ptr<COFFSection>> sections;
124b162290e39afd49d4c7d342333b331bc96232720Chris Lattner
1254cee2890a66974af506f2125243114cc14bd5556Michael J. Spencer  typedef DenseMap<MCSymbol  const *, COFFSymbol *>   symbol_map;
1264cee2890a66974af506f2125243114cc14bd5556Michael J. Spencer  typedef DenseMap<MCSection const *, COFFSection *> section_map;
127b162290e39afd49d4c7d342333b331bc96232720Chris Lattner
12836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  std::unique_ptr<MCWinCOFFObjectTargetWriter> TargetObjectWriter;
129df09270ae897e7fa64a7c162de163c32ee181a03Rafael Espindola
130801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  // Root level file contents.
131801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  COFF::header Header;
132801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  sections     Sections;
133801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  symbols      Symbols;
134801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  StringTable  Strings;
135b162290e39afd49d4c7d342333b331bc96232720Chris Lattner
136801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  // Maps used during object file creation.
137801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  section_map SectionMap;
138801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  symbol_map  SymbolMap;
139801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
140df09270ae897e7fa64a7c162de163c32ee181a03Rafael Espindola  WinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW, raw_ostream &OS);
141801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
1424cee2890a66974af506f2125243114cc14bd5556Michael J. Spencer  COFFSymbol *createSymbol(StringRef Name);
1434cee2890a66974af506f2125243114cc14bd5556Michael J. Spencer  COFFSymbol *GetOrCreateCOFFSymbol(const MCSymbol * Symbol);
1444cee2890a66974af506f2125243114cc14bd5556Michael J. Spencer  COFFSection *createSection(StringRef Name);
145801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
146801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  template <typename object_t, typename list_t>
14796f498bd9f140a98321c478f517877c4767b94faDmitri Gribenko  object_t *createCOFFEntity(StringRef Name, list_t &List);
148801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
149801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  void DefineSection(MCSectionData const &SectionData);
15028860823ad34d41d4f58561dc14a982fb0843fddReid Kleckner  void DefineSymbol(MCSymbolData const &SymbolData, MCAssembler &Assembler,
15128860823ad34d41d4f58561dc14a982fb0843fddReid Kleckner                    const MCAsmLayout &Layout);
152801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
153a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer  void MakeSymbolReal(COFFSymbol &S, size_t Index);
154a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer  void MakeSectionReal(COFFSection &S, size_t Number);
155a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer
156a7b7a7d629c3101f6f6c87e6848e865734e0238cReid Kleckner  bool ExportSymbol(MCSymbolData const &SymbolData, MCAssembler &Asm);
157a7b7a7d629c3101f6f6c87e6848e865734e0238cReid Kleckner
158a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer  bool IsPhysicalSection(COFFSection *S);
159a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer
160801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  // Entity writing methods.
161801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
162801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  void WriteFileHeader(const COFF::header &Header);
163dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  void WriteSymbol(const COFFSymbol &S);
164801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  void WriteAuxiliarySymbols(const COFFSymbol::AuxiliarySymbols &S);
165801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  void WriteSectionHeader(const COFF::section &S);
166801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  void WriteRelocation(const COFF::relocation &R);
167801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
168801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  // MCObjectWriter interface implementation.
169801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
17036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  void ExecutePostLayoutBinding(MCAssembler &Asm,
17136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                                const MCAsmLayout &Layout) override;
172801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
17336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
17436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                        const MCFragment *Fragment, const MCFixup &Fixup,
17536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                        MCValue Target, bool &IsPCRel,
17636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                        uint64_t &FixedValue) override;
177801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
17836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
179801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer};
180801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer}
181801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
182801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencerstatic inline void write_uint32_le(void *Data, uint32_t const &Value) {
183801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  uint8_t *Ptr = reinterpret_cast<uint8_t *>(Data);
184801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  Ptr[0] = (Value & 0x000000FF) >>  0;
185801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  Ptr[1] = (Value & 0x0000FF00) >>  8;
186801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  Ptr[2] = (Value & 0x00FF0000) >> 16;
187801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  Ptr[3] = (Value & 0xFF000000) >> 24;
188801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer}
189801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
190801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer//------------------------------------------------------------------------------
191801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer// Symbol class implementation
192801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
19396f498bd9f140a98321c478f517877c4767b94faDmitri GribenkoCOFFSymbol::COFFSymbol(StringRef name)
194a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer  : Name(name.begin(), name.end())
195dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  , Other(nullptr)
196dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  , Section(nullptr)
197a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer  , Relocations(0)
198dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  , MCData(nullptr) {
199801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  memset(&Data, 0, sizeof(Data));
200801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer}
201b162290e39afd49d4c7d342333b331bc96232720Chris Lattner
202801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencersize_t COFFSymbol::size() const {
203801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  return COFF::SymbolSize + (Data.NumberOfAuxSymbols * COFF::SymbolSize);
204b162290e39afd49d4c7d342333b331bc96232720Chris Lattner}
205b162290e39afd49d4c7d342333b331bc96232720Chris Lattner
206801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer// In the case that the name does not fit within 8 bytes, the offset
207801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer// into the string table is stored in the last 4 bytes instead, leaving
208801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer// the first 4 bytes as 0.
209801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencervoid COFFSymbol::set_name_offset(uint32_t Offset) {
210801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  write_uint32_le(Data.Name + 0, 0);
211801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  write_uint32_le(Data.Name + 4, Offset);
212801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer}
213801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
214a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer/// logic to decide if the symbol should be reported in the symbol table
215a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencerbool COFFSymbol::should_keep() const {
216a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer  // no section means its external, keep it
217dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  if (!Section)
218a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer    return true;
219a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer
220a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer  // if it has relocations pointing at it, keep it
221a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer  if (Relocations > 0)   {
222a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer    assert(Section->Number != -1 && "Sections with relocations must be real!");
223a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer    return true;
224a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer  }
225a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer
226a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer  // if the section its in is being droped, drop it
227a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer  if (Section->Number == -1)
228a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer      return false;
229a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer
230a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer  // if it is the section symbol, keep it
231a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer  if (Section->Symbol == this)
232a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer    return true;
233a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer
234a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer  // if its temporary, drop it
235a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer  if (MCData && MCData->getSymbol().isTemporary())
236a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer      return false;
237a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer
238a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer  // otherwise, keep it
239a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer  return true;
240a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer}
241a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer
242801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer//------------------------------------------------------------------------------
243801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer// Section class implementation
244801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
24596f498bd9f140a98321c478f517877c4767b94faDmitri GribenkoCOFFSection::COFFSection(StringRef name)
246a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer  : Name(name)
247dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  , MCData(nullptr)
248dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  , Symbol(nullptr) {
249801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  memset(&Header, 0, sizeof(Header));
250801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer}
251801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
252801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencersize_t COFFSection::size() {
253801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  return COFF::SectionSize;
254801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer}
255801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
256801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer//------------------------------------------------------------------------------
257801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer// StringTable class implementation
258801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
259801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer/// Write the length of the string table into Data.
260801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer/// The length of the string table includes uint32 length header.
261801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencervoid StringTable::update_length() {
262801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  write_uint32_le(&Data.front(), Data.size());
263801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer}
264801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
265801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. SpencerStringTable::StringTable() {
266801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  // The string table data begins with the length of the entire string table
267801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  // including the length header. Allocate space for this header.
268801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  Data.resize(4);
2690d64632c9e8a2632b8804bcc3fbc3523588862a7Michael J. Spencer  update_length();
270801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer}
271801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
272801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencersize_t StringTable::size() const {
273801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  return Data.size();
274801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer}
275801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
27694c22716d60ff5edf6a98a3c67e0faa001be1142Sylvestre Ledru/// Add String to the table iff it is not already there.
277801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer/// @returns the index into the string table where the string is now located.
27896f498bd9f140a98321c478f517877c4767b94faDmitri Gribenkosize_t StringTable::insert(StringRef String) {
279801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  map::iterator i = Map.find(String);
280801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
281801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  if (i != Map.end())
282801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer    return i->second;
283801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
284801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  size_t Offset = Data.size();
285801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
286801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  // Insert string data into string table.
287801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  Data.insert(Data.end(), String.begin(), String.end());
288801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  Data.push_back('\0');
289801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
290801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  // Put a reference to it in the map.
291801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  Map[String] = Offset;
292801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
293801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  // Update the internal length field.
294801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  update_length();
295801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
296801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  return Offset;
297801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer}
298801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
299801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer//------------------------------------------------------------------------------
300801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer// WinCOFFObjectWriter class implementation
301801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
302df09270ae897e7fa64a7c162de163c32ee181a03Rafael EspindolaWinCOFFObjectWriter::WinCOFFObjectWriter(MCWinCOFFObjectTargetWriter *MOTW,
303df09270ae897e7fa64a7c162de163c32ee181a03Rafael Espindola                                         raw_ostream &OS)
30482c84fdd23669d23c02a07498c83b83702979829Michael J. Spencer  : MCObjectWriter(OS, true)
305df09270ae897e7fa64a7c162de163c32ee181a03Rafael Espindola  , TargetObjectWriter(MOTW) {
306801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  memset(&Header, 0, sizeof(Header));
307da0bfcdaf95d95a66e306ef6d45f638939272d34Michael J. Spencer
308df09270ae897e7fa64a7c162de163c32ee181a03Rafael Espindola  Header.Machine = TargetObjectWriter->getMachine();
309801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer}
310801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
3114cee2890a66974af506f2125243114cc14bd5556Michael J. SpencerCOFFSymbol *WinCOFFObjectWriter::createSymbol(StringRef Name) {
312801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  return createCOFFEntity<COFFSymbol>(Name, Symbols);
313801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer}
314801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
3154cee2890a66974af506f2125243114cc14bd5556Michael J. SpencerCOFFSymbol *WinCOFFObjectWriter::GetOrCreateCOFFSymbol(const MCSymbol * Symbol){
3164cee2890a66974af506f2125243114cc14bd5556Michael J. Spencer  symbol_map::iterator i = SymbolMap.find(Symbol);
3174cee2890a66974af506f2125243114cc14bd5556Michael J. Spencer  if (i != SymbolMap.end())
3184cee2890a66974af506f2125243114cc14bd5556Michael J. Spencer    return i->second;
3194cee2890a66974af506f2125243114cc14bd5556Michael J. Spencer  COFFSymbol *RetSymbol
3204cee2890a66974af506f2125243114cc14bd5556Michael J. Spencer    = createCOFFEntity<COFFSymbol>(Symbol->getName(), Symbols);
3214cee2890a66974af506f2125243114cc14bd5556Michael J. Spencer  SymbolMap[Symbol] = RetSymbol;
3224cee2890a66974af506f2125243114cc14bd5556Michael J. Spencer  return RetSymbol;
3234cee2890a66974af506f2125243114cc14bd5556Michael J. Spencer}
3244cee2890a66974af506f2125243114cc14bd5556Michael J. Spencer
32596f498bd9f140a98321c478f517877c4767b94faDmitri GribenkoCOFFSection *WinCOFFObjectWriter::createSection(StringRef Name) {
326801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  return createCOFFEntity<COFFSection>(Name, Sections);
327801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer}
328801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
329801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer/// A template used to lookup or create a symbol/section, and initialize it if
330801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer/// needed.
331801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencertemplate <typename object_t, typename list_t>
33296f498bd9f140a98321c478f517877c4767b94faDmitri Gribenkoobject_t *WinCOFFObjectWriter::createCOFFEntity(StringRef Name,
333801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer                                                list_t &List) {
334dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  List.push_back(make_unique<object_t>(Name));
335801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
336dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  return List.back().get();
337801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer}
338801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
339801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer/// This function takes a section data object from the assembler
340801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer/// and creates the associated COFF section staging object.
341801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencervoid WinCOFFObjectWriter::DefineSection(MCSectionData const &SectionData) {
342d47f4a9c982d264e46a6a2fe0f357288768bb5b9Michael J. Spencer  assert(SectionData.getSection().getVariant() == MCSection::SV_COFF
34336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    && "Got non-COFF section in the COFF backend!");
344801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  // FIXME: Not sure how to verify this (at least in a debug build).
345801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  MCSectionCOFF const &Sec =
346801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer    static_cast<MCSectionCOFF const &>(SectionData.getSection());
347801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
348801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  COFFSection *coff_section = createSection(Sec.getSectionName());
349801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  COFFSymbol  *coff_symbol = createSymbol(Sec.getSectionName());
350cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  if (Sec.getSelection() != COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) {
351cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines    if (const MCSymbol *S = Sec.getCOMDATSymbol()) {
352cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines      COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(S);
353cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines      if (COMDATSymbol->Section)
354cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines        report_fatal_error("two sections have the same comdat");
355cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines      COMDATSymbol->Section = coff_section;
356cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines    }
357cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  }
358801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
359a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer  coff_section->Symbol = coff_symbol;
360a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer  coff_symbol->Section = coff_section;
361801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  coff_symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_STATIC;
362801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
363801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  // In this case the auxiliary symbol is a Section Definition.
364801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  coff_symbol->Aux.resize(1);
365801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  memset(&coff_symbol->Aux[0], 0, sizeof(coff_symbol->Aux[0]));
366801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  coff_symbol->Aux[0].AuxType = ATSectionDefinition;
367801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  coff_symbol->Aux[0].Aux.SectionDefinition.Selection = Sec.getSelection();
368801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
369801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  coff_section->Header.Characteristics = Sec.getCharacteristics();
370801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
371801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  uint32_t &Characteristics = coff_section->Header.Characteristics;
372801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  switch (SectionData.getAlignment()) {
373801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  case 1:    Characteristics |= COFF::IMAGE_SCN_ALIGN_1BYTES;    break;
374801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  case 2:    Characteristics |= COFF::IMAGE_SCN_ALIGN_2BYTES;    break;
375801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  case 4:    Characteristics |= COFF::IMAGE_SCN_ALIGN_4BYTES;    break;
376801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  case 8:    Characteristics |= COFF::IMAGE_SCN_ALIGN_8BYTES;    break;
377801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  case 16:   Characteristics |= COFF::IMAGE_SCN_ALIGN_16BYTES;   break;
378801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  case 32:   Characteristics |= COFF::IMAGE_SCN_ALIGN_32BYTES;   break;
379801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  case 64:   Characteristics |= COFF::IMAGE_SCN_ALIGN_64BYTES;   break;
380801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  case 128:  Characteristics |= COFF::IMAGE_SCN_ALIGN_128BYTES;  break;
381801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  case 256:  Characteristics |= COFF::IMAGE_SCN_ALIGN_256BYTES;  break;
382801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  case 512:  Characteristics |= COFF::IMAGE_SCN_ALIGN_512BYTES;  break;
383801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  case 1024: Characteristics |= COFF::IMAGE_SCN_ALIGN_1024BYTES; break;
384801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  case 2048: Characteristics |= COFF::IMAGE_SCN_ALIGN_2048BYTES; break;
385801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  case 4096: Characteristics |= COFF::IMAGE_SCN_ALIGN_4096BYTES; break;
386801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  case 8192: Characteristics |= COFF::IMAGE_SCN_ALIGN_8192BYTES; break;
387801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  default:
388801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer    llvm_unreachable("unsupported section alignment");
389801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  }
390801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
391801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  // Bind internal COFF section to MC section.
392801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  coff_section->MCData = &SectionData;
3934cee2890a66974af506f2125243114cc14bd5556Michael J. Spencer  SectionMap[&SectionData.getSection()] = coff_section;
394801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer}
395801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
396dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hinesstatic uint64_t getSymbolValue(const MCSymbolData &Data,
397dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                               const MCAsmLayout &Layout) {
398dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  if (Data.isCommon() && Data.isExternal())
399dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    return Data.getCommonSize();
400dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
401dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  uint64_t Res;
402dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  if (!Layout.getSymbolOffset(&Data, Res))
403dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    return 0;
404dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
405dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  return Res;
406dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines}
407dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
408dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines/// This function takes a symbol data object from the assembler
409801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer/// and creates the associated COFF symbol staging object.
4104974b972e7dd94fad74ada4df32a12aba09c4de0Peter Collingbournevoid WinCOFFObjectWriter::DefineSymbol(MCSymbolData const &SymbolData,
41128860823ad34d41d4f58561dc14a982fb0843fddReid Kleckner                                       MCAssembler &Assembler,
41228860823ad34d41d4f58561dc14a982fb0843fddReid Kleckner                                       const MCAsmLayout &Layout) {
4134974b972e7dd94fad74ada4df32a12aba09c4de0Peter Collingbourne  MCSymbol const &Symbol = SymbolData.getSymbol();
4140933134a304b47d3767aad202df9f0e09743da6dMichael J. Spencer  COFFSymbol *coff_symbol = GetOrCreateCOFFSymbol(&Symbol);
4154974b972e7dd94fad74ada4df32a12aba09c4de0Peter Collingbourne  SymbolMap[&Symbol] = coff_symbol;
416801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
4174cee2890a66974af506f2125243114cc14bd5556Michael J. Spencer  if (SymbolData.getFlags() & COFF::SF_WeakExternal) {
418801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer    coff_symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
419801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
4200933134a304b47d3767aad202df9f0e09743da6dMichael J. Spencer    if (Symbol.isVariable()) {
4214974b972e7dd94fad74ada4df32a12aba09c4de0Peter Collingbourne      const MCSymbolRefExpr *SymRef =
4224974b972e7dd94fad74ada4df32a12aba09c4de0Peter Collingbourne        dyn_cast<MCSymbolRefExpr>(Symbol.getVariableValue());
423801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
4244974b972e7dd94fad74ada4df32a12aba09c4de0Peter Collingbourne      if (!SymRef)
4254974b972e7dd94fad74ada4df32a12aba09c4de0Peter Collingbourne        report_fatal_error("Weak externals may only alias symbols");
426801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
4274974b972e7dd94fad74ada4df32a12aba09c4de0Peter Collingbourne      coff_symbol->Other = GetOrCreateCOFFSymbol(&SymRef->getSymbol());
4284cee2890a66974af506f2125243114cc14bd5556Michael J. Spencer    } else {
4294cee2890a66974af506f2125243114cc14bd5556Michael J. Spencer      std::string WeakName = std::string(".weak.")
4300933134a304b47d3767aad202df9f0e09743da6dMichael J. Spencer                           +  Symbol.getName().str()
4314cee2890a66974af506f2125243114cc14bd5556Michael J. Spencer                           + ".default";
4324cee2890a66974af506f2125243114cc14bd5556Michael J. Spencer      COFFSymbol *WeakDefault = createSymbol(WeakName);
4334cee2890a66974af506f2125243114cc14bd5556Michael J. Spencer      WeakDefault->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
4344cee2890a66974af506f2125243114cc14bd5556Michael J. Spencer      WeakDefault->Data.StorageClass  = COFF::IMAGE_SYM_CLASS_EXTERNAL;
4354cee2890a66974af506f2125243114cc14bd5556Michael J. Spencer      WeakDefault->Data.Type          = 0;
4364cee2890a66974af506f2125243114cc14bd5556Michael J. Spencer      WeakDefault->Data.Value         = 0;
4374cee2890a66974af506f2125243114cc14bd5556Michael J. Spencer      coff_symbol->Other = WeakDefault;
4384cee2890a66974af506f2125243114cc14bd5556Michael J. Spencer    }
439801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
440801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer    // Setup the Weak External auxiliary symbol.
441801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer    coff_symbol->Aux.resize(1);
442801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer    memset(&coff_symbol->Aux[0], 0, sizeof(coff_symbol->Aux[0]));
443801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer    coff_symbol->Aux[0].AuxType = ATWeakExternal;
444801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer    coff_symbol->Aux[0].Aux.WeakExternal.TagIndex = 0;
445801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer    coff_symbol->Aux[0].Aux.WeakExternal.Characteristics =
4464cee2890a66974af506f2125243114cc14bd5556Michael J. Spencer      COFF::IMAGE_WEAK_EXTERN_SEARCH_LIBRARY;
4474cee2890a66974af506f2125243114cc14bd5556Michael J. Spencer
4484974b972e7dd94fad74ada4df32a12aba09c4de0Peter Collingbourne    coff_symbol->MCData = &SymbolData;
4494974b972e7dd94fad74ada4df32a12aba09c4de0Peter Collingbourne  } else {
450dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    const MCSymbolData &ResSymData = Assembler.getSymbolData(Symbol);
451dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    const MCSymbol *Base = Layout.getBaseSymbol(Symbol);
452dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    coff_symbol->Data.Value = getSymbolValue(ResSymData, Layout);
45328860823ad34d41d4f58561dc14a982fb0843fddReid Kleckner
4544974b972e7dd94fad74ada4df32a12aba09c4de0Peter Collingbourne    coff_symbol->Data.Type         = (ResSymData.getFlags() & 0x0000FFFF) >>  0;
4554974b972e7dd94fad74ada4df32a12aba09c4de0Peter Collingbourne    coff_symbol->Data.StorageClass = (ResSymData.getFlags() & 0x00FF0000) >> 16;
456801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
4574974b972e7dd94fad74ada4df32a12aba09c4de0Peter Collingbourne    // If no storage class was specified in the streamer, define it here.
4584974b972e7dd94fad74ada4df32a12aba09c4de0Peter Collingbourne    if (coff_symbol->Data.StorageClass == 0) {
459dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      bool external = ResSymData.isExternal() || !ResSymData.Fragment;
460a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer
4614974b972e7dd94fad74ada4df32a12aba09c4de0Peter Collingbourne      coff_symbol->Data.StorageClass =
4624974b972e7dd94fad74ada4df32a12aba09c4de0Peter Collingbourne       external ? COFF::IMAGE_SYM_CLASS_EXTERNAL : COFF::IMAGE_SYM_CLASS_STATIC;
4634974b972e7dd94fad74ada4df32a12aba09c4de0Peter Collingbourne    }
4644974b972e7dd94fad74ada4df32a12aba09c4de0Peter Collingbourne
465dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    if (!Base) {
46628860823ad34d41d4f58561dc14a982fb0843fddReid Kleckner      coff_symbol->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
467dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    } else {
468dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      const MCSymbolData &BaseData = Assembler.getSymbolData(*Base);
469cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines      if (BaseData.Fragment) {
470cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines        COFFSection *Sec =
471dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines            SectionMap[&BaseData.Fragment->getParent()->getSection()];
472cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines
473cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines        if (coff_symbol->Section && coff_symbol->Section != Sec)
474cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines          report_fatal_error("conflicting sections for symbol");
475cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines
476cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines        coff_symbol->Section = Sec;
477cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines      }
478dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    }
4794974b972e7dd94fad74ada4df32a12aba09c4de0Peter Collingbourne
4804974b972e7dd94fad74ada4df32a12aba09c4de0Peter Collingbourne    coff_symbol->MCData = &ResSymData;
4814974b972e7dd94fad74ada4df32a12aba09c4de0Peter Collingbourne  }
482801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer}
483801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
48436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines// Maximum offsets for different string table entry encodings.
48536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesstatic const unsigned Max6DecimalOffset = 999999;
48636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesstatic const unsigned Max7DecimalOffset = 9999999;
48736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesstatic const uint64_t MaxBase64Offset = 0xFFFFFFFFFULL; // 64^6, including 0
48836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
48936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines// Encode a string table entry offset in base 64, padded to 6 chars, and
49036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines// prefixed with a double slash: '//AAAAAA', '//AAAAAB', ...
49136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines// Buffer must be at least 8 bytes large. No terminating null appended.
49236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesstatic void encodeBase64StringEntry(char* Buffer, uint64_t Value) {
49336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  assert(Value > Max7DecimalOffset && Value <= MaxBase64Offset &&
49436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines         "Illegal section name encoding for value");
49536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
49636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  static const char Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
49736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                                 "abcdefghijklmnopqrstuvwxyz"
49836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                                 "0123456789+/";
49936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
50036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  Buffer[0] = '/';
50136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  Buffer[1] = '/';
50236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
50336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  char* Ptr = Buffer + 7;
50436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  for (unsigned i = 0; i < 6; ++i) {
50536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    unsigned Rem = Value % 64;
50636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    Value /= 64;
50736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    *(Ptr--) = Alphabet[Rem];
50836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  }
50936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines}
51036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
511a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer/// making a section real involves assigned it a number and putting
512a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer/// name into the string table if needed
513a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencervoid WinCOFFObjectWriter::MakeSectionReal(COFFSection &S, size_t Number) {
514a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer  if (S.Name.size() > COFF::NameSize) {
515c63dce3c59ac24b2656e06f7017cd4dce4bf733cNico Rieck    uint64_t StringTableEntry = Strings.insert(S.Name.c_str());
516c63dce3c59ac24b2656e06f7017cd4dce4bf733cNico Rieck
51736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    if (StringTableEntry <= Max6DecimalOffset) {
518c63dce3c59ac24b2656e06f7017cd4dce4bf733cNico Rieck      std::sprintf(S.Header.Name, "/%d", unsigned(StringTableEntry));
51936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    } else if (StringTableEntry <= Max7DecimalOffset) {
520c63dce3c59ac24b2656e06f7017cd4dce4bf733cNico Rieck      // With seven digits, we have to skip the terminating null. Because
521c63dce3c59ac24b2656e06f7017cd4dce4bf733cNico Rieck      // sprintf always appends it, we use a larger temporary buffer.
522c63dce3c59ac24b2656e06f7017cd4dce4bf733cNico Rieck      char buffer[9] = { };
523c63dce3c59ac24b2656e06f7017cd4dce4bf733cNico Rieck      std::sprintf(buffer, "/%d", unsigned(StringTableEntry));
524c63dce3c59ac24b2656e06f7017cd4dce4bf733cNico Rieck      std::memcpy(S.Header.Name, buffer, 8);
52536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    } else if (StringTableEntry <= MaxBase64Offset) {
52636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      // Starting with 10,000,000, offsets are encoded as base64.
52736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      encodeBase64StringEntry(S.Header.Name, StringTableEntry);
528c63dce3c59ac24b2656e06f7017cd4dce4bf733cNico Rieck    } else {
52936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      report_fatal_error("COFF string table is greater than 64 GB.");
530c63dce3c59ac24b2656e06f7017cd4dce4bf733cNico Rieck    }
531a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer  } else
532a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer    std::memcpy(S.Header.Name, S.Name.c_str(), S.Name.size());
533a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer
534a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer  S.Number = Number;
535a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer  S.Symbol->Data.SectionNumber = S.Number;
536a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer  S.Symbol->Aux[0].Aux.SectionDefinition.Number = S.Number;
537a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer}
538a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer
539a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencervoid WinCOFFObjectWriter::MakeSymbolReal(COFFSymbol &S, size_t Index) {
540a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer  if (S.Name.size() > COFF::NameSize) {
541a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer    size_t StringTableEntry = Strings.insert(S.Name.c_str());
542a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer
543a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer    S.set_name_offset(StringTableEntry);
544a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer  } else
545a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer    std::memcpy(S.Data.Name, S.Name.c_str(), S.Name.size());
546a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer  S.Index = Index;
547a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer}
548a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer
549a7b7a7d629c3101f6f6c87e6848e865734e0238cReid Klecknerbool WinCOFFObjectWriter::ExportSymbol(MCSymbolData const &SymbolData,
550a7b7a7d629c3101f6f6c87e6848e865734e0238cReid Kleckner                                       MCAssembler &Asm) {
551a7b7a7d629c3101f6f6c87e6848e865734e0238cReid Kleckner  // This doesn't seem to be right. Strings referred to from the .data section
552a7b7a7d629c3101f6f6c87e6848e865734e0238cReid Kleckner  // need symbols so they can be linked to code in the .text section right?
553a7b7a7d629c3101f6f6c87e6848e865734e0238cReid Kleckner
554cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  // return Asm.isSymbolLinkerVisible(SymbolData.getSymbol());
555a7b7a7d629c3101f6f6c87e6848e865734e0238cReid Kleckner
556a7b7a7d629c3101f6f6c87e6848e865734e0238cReid Kleckner  // For now, all non-variable symbols are exported,
557a7b7a7d629c3101f6f6c87e6848e865734e0238cReid Kleckner  // the linker will sort the rest out for us.
558a7b7a7d629c3101f6f6c87e6848e865734e0238cReid Kleckner  return SymbolData.isExternal() || !SymbolData.getSymbol().isVariable();
559a7b7a7d629c3101f6f6c87e6848e865734e0238cReid Kleckner}
560a7b7a7d629c3101f6f6c87e6848e865734e0238cReid Kleckner
561a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencerbool WinCOFFObjectWriter::IsPhysicalSection(COFFSection *S) {
562a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer  return (S->Header.Characteristics
563a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer         & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 0;
564801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer}
565801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
566801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer//------------------------------------------------------------------------------
567801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer// entity writing methods
568801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
569801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencervoid WinCOFFObjectWriter::WriteFileHeader(const COFF::header &Header) {
570801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  WriteLE16(Header.Machine);
571801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  WriteLE16(Header.NumberOfSections);
572801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  WriteLE32(Header.TimeDateStamp);
573801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  WriteLE32(Header.PointerToSymbolTable);
574801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  WriteLE32(Header.NumberOfSymbols);
575801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  WriteLE16(Header.SizeOfOptionalHeader);
576801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  WriteLE16(Header.Characteristics);
577801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer}
578801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
579dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hinesvoid WinCOFFObjectWriter::WriteSymbol(const COFFSymbol &S) {
580dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  WriteBytes(StringRef(S.Data.Name, COFF::NameSize));
581dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  WriteLE32(S.Data.Value);
582dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  WriteLE16(S.Data.SectionNumber);
583dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  WriteLE16(S.Data.Type);
584dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  Write8(S.Data.StorageClass);
585dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  Write8(S.Data.NumberOfAuxSymbols);
586dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  WriteAuxiliarySymbols(S.Aux);
587801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer}
588801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
589801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencervoid WinCOFFObjectWriter::WriteAuxiliarySymbols(
590801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer                                        const COFFSymbol::AuxiliarySymbols &S) {
591801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  for(COFFSymbol::AuxiliarySymbols::const_iterator i = S.begin(), e = S.end();
592801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer      i != e; ++i) {
593801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer    switch(i->AuxType) {
594801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer    case ATFunctionDefinition:
595801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer      WriteLE32(i->Aux.FunctionDefinition.TagIndex);
596801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer      WriteLE32(i->Aux.FunctionDefinition.TotalSize);
597801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer      WriteLE32(i->Aux.FunctionDefinition.PointerToLinenumber);
598801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer      WriteLE32(i->Aux.FunctionDefinition.PointerToNextFunction);
599801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer      WriteZeros(sizeof(i->Aux.FunctionDefinition.unused));
600801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer      break;
601801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer    case ATbfAndefSymbol:
602801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer      WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused1));
603801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer      WriteLE16(i->Aux.bfAndefSymbol.Linenumber);
604801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer      WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused2));
605801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer      WriteLE32(i->Aux.bfAndefSymbol.PointerToNextFunction);
606801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer      WriteZeros(sizeof(i->Aux.bfAndefSymbol.unused3));
607801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer      break;
608801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer    case ATWeakExternal:
609801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer      WriteLE32(i->Aux.WeakExternal.TagIndex);
610801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer      WriteLE32(i->Aux.WeakExternal.Characteristics);
611801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer      WriteZeros(sizeof(i->Aux.WeakExternal.unused));
612801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer      break;
613801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer    case ATFile:
614801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer      WriteBytes(StringRef(reinterpret_cast<const char *>(i->Aux.File.FileName),
615801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer                 sizeof(i->Aux.File.FileName)));
616801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer      break;
617801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer    case ATSectionDefinition:
618801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer      WriteLE32(i->Aux.SectionDefinition.Length);
619801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer      WriteLE16(i->Aux.SectionDefinition.NumberOfRelocations);
620801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer      WriteLE16(i->Aux.SectionDefinition.NumberOfLinenumbers);
621801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer      WriteLE32(i->Aux.SectionDefinition.CheckSum);
622801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer      WriteLE16(i->Aux.SectionDefinition.Number);
623801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer      Write8(i->Aux.SectionDefinition.Selection);
624801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer      WriteZeros(sizeof(i->Aux.SectionDefinition.unused));
625801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer      break;
626801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer    }
627801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  }
628801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer}
629801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
630801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencervoid WinCOFFObjectWriter::WriteSectionHeader(const COFF::section &S) {
631801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  WriteBytes(StringRef(S.Name, COFF::NameSize));
632801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
633801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  WriteLE32(S.VirtualSize);
634801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  WriteLE32(S.VirtualAddress);
635801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  WriteLE32(S.SizeOfRawData);
636801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  WriteLE32(S.PointerToRawData);
637801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  WriteLE32(S.PointerToRelocations);
638801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  WriteLE32(S.PointerToLineNumbers);
639801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  WriteLE16(S.NumberOfRelocations);
640801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  WriteLE16(S.NumberOfLineNumbers);
641801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  WriteLE32(S.Characteristics);
642801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer}
643801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
644801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencervoid WinCOFFObjectWriter::WriteRelocation(const COFF::relocation &R) {
645801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  WriteLE32(R.VirtualAddress);
646801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  WriteLE32(R.SymbolTableIndex);
647801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  WriteLE16(R.Type);
648b162290e39afd49d4c7d342333b331bc96232720Chris Lattner}
649b162290e39afd49d4c7d342333b331bc96232720Chris Lattner
650b162290e39afd49d4c7d342333b331bc96232720Chris Lattner////////////////////////////////////////////////////////////////////////////////
651b162290e39afd49d4c7d342333b331bc96232720Chris Lattner// MCObjectWriter interface implementations
652b162290e39afd49d4c7d342333b331bc96232720Chris Lattner
65385f2ecc697a8ca6c8cf08093054cbbb9d2060ccfRafael Espindolavoid WinCOFFObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
65485f2ecc697a8ca6c8cf08093054cbbb9d2060ccfRafael Espindola                                                   const MCAsmLayout &Layout) {
655801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  // "Define" each section & symbol. This creates section & symbol
656a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer  // entries in the staging area.
657801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
658dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  static_assert(sizeof(((COFF::AuxiliaryFile *)nullptr)->FileName) == COFF::SymbolSize,
659dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                "size mismatch for COFF::AuxiliaryFile::FileName");
660dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  for (auto FI = Asm.file_names_begin(), FE = Asm.file_names_end();
661dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines       FI != FE; ++FI) {
662dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    // round up to calculate the number of auxiliary symbols required
663dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    unsigned Count = (FI->size() + COFF::SymbolSize - 1) / COFF::SymbolSize;
664dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
665dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    COFFSymbol *file = createSymbol(".file");
666dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    file->Data.SectionNumber = COFF::IMAGE_SYM_DEBUG;
667dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    file->Data.StorageClass = COFF::IMAGE_SYM_CLASS_FILE;
668dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    file->Aux.resize(Count);
669dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
670dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    unsigned Offset = 0;
671dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    unsigned Length = FI->size();
672dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    for (auto & Aux : file->Aux) {
673dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      Aux.AuxType = ATFile;
674dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
675dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      if (Length > COFF::SymbolSize) {
676dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines        memcpy(Aux.Aux.File.FileName, FI->c_str() + Offset, COFF::SymbolSize);
677dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines        Length = Length - COFF::SymbolSize;
678dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      } else {
679dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines        memcpy(Aux.Aux.File.FileName, FI->c_str() + Offset, Length);
680dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines        memset(&Aux.Aux.File.FileName[Length], 0, COFF::SymbolSize - Length);
681dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines        Length = 0;
682dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      }
683801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
684dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      Offset = Offset + COFF::SymbolSize;
685a7b7a7d629c3101f6f6c87e6848e865734e0238cReid Kleckner    }
686a7b7a7d629c3101f6f6c87e6848e865734e0238cReid Kleckner  }
687dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
688dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  for (const auto & Section : Asm)
689dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    DefineSection(Section);
690dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
691dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  for (MCSymbolData &SD : Asm.symbols())
692dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    if (ExportSymbol(SD, Asm))
693dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      DefineSymbol(SD, Asm, Layout);
694b162290e39afd49d4c7d342333b331bc96232720Chris Lattner}
695b162290e39afd49d4c7d342333b331bc96232720Chris Lattner
696b162290e39afd49d4c7d342333b331bc96232720Chris Lattnervoid WinCOFFObjectWriter::RecordRelocation(const MCAssembler &Asm,
697b162290e39afd49d4c7d342333b331bc96232720Chris Lattner                                           const MCAsmLayout &Layout,
698b162290e39afd49d4c7d342333b331bc96232720Chris Lattner                                           const MCFragment *Fragment,
699b162290e39afd49d4c7d342333b331bc96232720Chris Lattner                                           const MCFixup &Fixup,
700b162290e39afd49d4c7d342333b331bc96232720Chris Lattner                                           MCValue Target,
70136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines                                           bool &IsPCRel,
702b162290e39afd49d4c7d342333b331bc96232720Chris Lattner                                           uint64_t &FixedValue) {
703dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  assert(Target.getSymA() && "Relocation must reference a symbol!");
70482c84fdd23669d23c02a07498c83b83702979829Michael J. Spencer
7056057eb7ab697fcd0feb3cdd55e9a497cfe0aff72Reid Kleckner  const MCSymbol &Symbol = Target.getSymA()->getSymbol();
7066057eb7ab697fcd0feb3cdd55e9a497cfe0aff72Reid Kleckner  const MCSymbol &A = Symbol.AliasedSymbol();
70736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  if (!Asm.hasSymbolData(A))
70836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    Asm.getContext().FatalError(
70936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines        Fixup.getLoc(),
71036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines        Twine("symbol '") + A.getName() + "' can not be undefined");
71136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
712dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  const MCSymbolData &A_SD = Asm.getSymbolData(A);
713801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
714801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  MCSectionData const *SectionData = Fragment->getParent();
715801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
71682c84fdd23669d23c02a07498c83b83702979829Michael J. Spencer  // Mark this symbol as requiring an entry in the symbol table.
7174cee2890a66974af506f2125243114cc14bd5556Michael J. Spencer  assert(SectionMap.find(&SectionData->getSection()) != SectionMap.end() &&
718801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer         "Section must already have been defined in ExecutePostLayoutBinding!");
7194cee2890a66974af506f2125243114cc14bd5556Michael J. Spencer  assert(SymbolMap.find(&A_SD.getSymbol()) != SymbolMap.end() &&
720801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer         "Symbol must already have been defined in ExecutePostLayoutBinding!");
721801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
7224cee2890a66974af506f2125243114cc14bd5556Michael J. Spencer  COFFSection *coff_section = SectionMap[&SectionData->getSection()];
7234cee2890a66974af506f2125243114cc14bd5556Michael J. Spencer  COFFSymbol *coff_symbol = SymbolMap[&A_SD.getSymbol()];
7243660a847f1820d73847539f3959dc069396f8e44Rafael Espindola  const MCSymbolRefExpr *SymB = Target.getSymB();
72536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  bool CrossSection = false;
72682c84fdd23669d23c02a07498c83b83702979829Michael J. Spencer
72736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  if (SymB) {
72836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    const MCSymbol *B = &SymB->getSymbol();
729dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    const MCSymbolData &B_SD = Asm.getSymbolData(*B);
73036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    if (!B_SD.getFragment())
73136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      Asm.getContext().FatalError(
73236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines          Fixup.getLoc(),
73336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines          Twine("symbol '") + B->getName() +
73436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines              "' can not be undefined in a subtraction expression");
73536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
73636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    if (!A_SD.getFragment())
73736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines      Asm.getContext().FatalError(
73836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines          Fixup.getLoc(),
73936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines          Twine("symbol '") + Symbol.getName() +
74036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines              "' can not be undefined in a subtraction expression");
74136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
74236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    CrossSection = &Symbol.getSection() != &B->getSection();
743801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
7441ac7fe0f4dae8a9266fa6ff31ea4939ec64a3e5eRafael Espindola    // Offset of the symbol in the section
7451ac7fe0f4dae8a9266fa6ff31ea4939ec64a3e5eRafael Espindola    int64_t a = Layout.getSymbolOffset(&B_SD);
74682c84fdd23669d23c02a07498c83b83702979829Michael J. Spencer
7471ac7fe0f4dae8a9266fa6ff31ea4939ec64a3e5eRafael Espindola    // Ofeset of the relocation in the section
7481ac7fe0f4dae8a9266fa6ff31ea4939ec64a3e5eRafael Espindola    int64_t b = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
7491ac7fe0f4dae8a9266fa6ff31ea4939ec64a3e5eRafael Espindola
7501ac7fe0f4dae8a9266fa6ff31ea4939ec64a3e5eRafael Espindola    FixedValue = b - a;
75182c84fdd23669d23c02a07498c83b83702979829Michael J. Spencer    // In the case where we have SymbA and SymB, we just need to store the delta
75282c84fdd23669d23c02a07498c83b83702979829Michael J. Spencer    // between the two symbols.  Update FixedValue to account for the delta, and
75382c84fdd23669d23c02a07498c83b83702979829Michael J. Spencer    // skip recording the relocation.
7543660a847f1820d73847539f3959dc069396f8e44Rafael Espindola    if (!CrossSection)
7553660a847f1820d73847539f3959dc069396f8e44Rafael Espindola      return;
75682c84fdd23669d23c02a07498c83b83702979829Michael J. Spencer  } else {
75782c84fdd23669d23c02a07498c83b83702979829Michael J. Spencer    FixedValue = Target.getConstant();
75882c84fdd23669d23c02a07498c83b83702979829Michael J. Spencer  }
759801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
760801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  COFFRelocation Reloc;
761801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
762425f634917542d7f09c189e2eb130752c6a12d2cDaniel Dunbar  Reloc.Data.SymbolTableIndex = 0;
763801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  Reloc.Data.VirtualAddress = Layout.getFragmentOffset(Fragment);
764a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer
765ea1104a4c376634ed8eb693f6c71e0ac51074949Michael J. Spencer  // Turn relocations for temporary symbols into section relocations.
7663660a847f1820d73847539f3959dc069396f8e44Rafael Espindola  if (coff_symbol->MCData->getSymbol().isTemporary() || CrossSection) {
767a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer    Reloc.Symb = coff_symbol->Section->Symbol;
768eb6e77f8cccd14cdba995ff8231f2c9faea9bfccMichael J. Spencer    FixedValue += Layout.getFragmentOffset(coff_symbol->MCData->Fragment)
769eb6e77f8cccd14cdba995ff8231f2c9faea9bfccMichael J. Spencer                + coff_symbol->MCData->getOffset();
770a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer  } else
771a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer    Reloc.Symb = coff_symbol;
772a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer
773a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer  ++Reloc.Symb->Relocations;
774801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
775801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  Reloc.Data.VirtualAddress += Fixup.getOffset();
77618d49acdab79d6f0966b47182b6c3a2ba3d9f80fNico Rieck  Reloc.Data.Type = TargetObjectWriter->getRelocType(Target, Fixup,
77718d49acdab79d6f0966b47182b6c3a2ba3d9f80fNico Rieck                                                     CrossSection);
778b156c5d3765637393eda28b04b7fc1e5c55675ebRafael Espindola
779b156c5d3765637393eda28b04b7fc1e5c55675ebRafael Espindola  // FIXME: Can anyone explain what this does other than adjust for the size
780b156c5d3765637393eda28b04b7fc1e5c55675ebRafael Espindola  // of the offset?
781dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  if ((Header.Machine == COFF::IMAGE_FILE_MACHINE_AMD64 &&
782dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines       Reloc.Data.Type == COFF::IMAGE_REL_AMD64_REL32) ||
783dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      (Header.Machine == COFF::IMAGE_FILE_MACHINE_I386 &&
784dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines       Reloc.Data.Type == COFF::IMAGE_REL_I386_REL32))
78582c84fdd23669d23c02a07498c83b83702979829Michael J. Spencer    FixedValue += 4;
786801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
787dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  if (Header.Machine == COFF::IMAGE_FILE_MACHINE_ARMNT) {
788dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    switch (Reloc.Data.Type) {
789dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    case COFF::IMAGE_REL_ARM_ABSOLUTE:
790dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    case COFF::IMAGE_REL_ARM_ADDR32:
791dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    case COFF::IMAGE_REL_ARM_ADDR32NB:
792dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    case COFF::IMAGE_REL_ARM_TOKEN:
793dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    case COFF::IMAGE_REL_ARM_SECTION:
794dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    case COFF::IMAGE_REL_ARM_SECREL:
795dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      break;
796dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    case COFF::IMAGE_REL_ARM_BRANCH11:
797dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    case COFF::IMAGE_REL_ARM_BLX11:
798dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      // IMAGE_REL_ARM_BRANCH11 and IMAGE_REL_ARM_BLX11 are only used for
799dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      // pre-ARMv7, which implicitly rules it out of ARMNT (it would be valid
800dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      // for Windows CE).
801dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    case COFF::IMAGE_REL_ARM_BRANCH24:
802dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    case COFF::IMAGE_REL_ARM_BLX24:
803dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    case COFF::IMAGE_REL_ARM_MOV32A:
804dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      // IMAGE_REL_ARM_BRANCH24, IMAGE_REL_ARM_BLX24, IMAGE_REL_ARM_MOV32A are
805dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      // only used for ARM mode code, which is documented as being unsupported
806dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      // by Windows on ARM.  Empirical proof indicates that masm is able to
807dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      // generate the relocations however the rest of the MSVC toolchain is
808dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      // unable to handle it.
809dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      llvm_unreachable("unsupported relocation");
810dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      break;
811dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    case COFF::IMAGE_REL_ARM_MOV32T:
812dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      break;
813dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    case COFF::IMAGE_REL_ARM_BRANCH20T:
814dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    case COFF::IMAGE_REL_ARM_BRANCH24T:
815dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    case COFF::IMAGE_REL_ARM_BLX23T:
816dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      // IMAGE_REL_BRANCH20T, IMAGE_REL_ARM_BRANCH24T, IMAGE_REL_ARM_BLX23T all
817dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      // perform a 4 byte adjustment to the relocation.  Relative branches are
818dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      // offset by 4 on ARM, however, because there is no RELA relocations, all
819dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      // branches are offset by 4.
820dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      FixedValue = FixedValue + 4;
821dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      break;
822dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    }
823dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  }
824dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
825dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  if (TargetObjectWriter->recordRelocation(Fixup))
826dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    coff_section->Relocations.push_back(Reloc);
827b162290e39afd49d4c7d342333b331bc96232720Chris Lattner}
828b162290e39afd49d4c7d342333b331bc96232720Chris Lattner
8298f413fa9c00dbaea000ddfe265ab5edd285ea3abRafael Espindolavoid WinCOFFObjectWriter::WriteObject(MCAssembler &Asm,
830b162290e39afd49d4c7d342333b331bc96232720Chris Lattner                                      const MCAsmLayout &Layout) {
831801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  // Assign symbol and section indexes and offsets.
832a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer  Header.NumberOfSections = 0;
833a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer
83480646283796b20c6a1b7d8eb69ce6f0478d54383Nico Rieck  DenseMap<COFFSection *, uint16_t> SectionIndices;
835dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  for (auto & Section : Sections) {
836cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines    size_t Number = ++Header.NumberOfSections;
837cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines    SectionIndices[Section.get()] = Number;
838cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines    MakeSectionReal(*Section, Number);
839a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer  }
840801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
841801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  Header.NumberOfSymbols = 0;
842801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
843dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  for (auto & Symbol : Symbols) {
844801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer    // Update section number & offset for symbols that have them.
845dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    if (Symbol->Section)
846dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      Symbol->Data.SectionNumber = Symbol->Section->Number;
847801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
848dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    if (Symbol->should_keep()) {
849dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      MakeSymbolReal(*Symbol, Header.NumberOfSymbols++);
850a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer
851a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer      // Update auxiliary symbol info.
852dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      Symbol->Data.NumberOfAuxSymbols = Symbol->Aux.size();
853dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      Header.NumberOfSymbols += Symbol->Data.NumberOfAuxSymbols;
854a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer    } else
855dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      Symbol->Index = -1;
856801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  }
857801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
858801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  // Fixup weak external references.
859dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  for (auto & Symbol : Symbols) {
860dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    if (Symbol->Other) {
861dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      assert(Symbol->Index != -1);
862dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      assert(Symbol->Aux.size() == 1 && "Symbol must contain one aux symbol!");
863dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      assert(Symbol->Aux[0].AuxType == ATWeakExternal &&
864801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer             "Symbol's aux symbol must be a Weak External!");
865dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      Symbol->Aux[0].Aux.WeakExternal.TagIndex = Symbol->Other->Index;
866801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer    }
867801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  }
868801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
86980646283796b20c6a1b7d8eb69ce6f0478d54383Nico Rieck  // Fixup associative COMDAT sections.
870dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  for (auto & Section : Sections) {
871dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    if (Section->Symbol->Aux[0].Aux.SectionDefinition.Selection !=
87280646283796b20c6a1b7d8eb69ce6f0478d54383Nico Rieck        COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
87380646283796b20c6a1b7d8eb69ce6f0478d54383Nico Rieck      continue;
87480646283796b20c6a1b7d8eb69ce6f0478d54383Nico Rieck
875dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    const MCSectionCOFF &MCSec =
876dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      static_cast<const MCSectionCOFF &>(Section->MCData->getSection());
87780646283796b20c6a1b7d8eb69ce6f0478d54383Nico Rieck
878cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines    const MCSymbol *COMDAT = MCSec.getCOMDATSymbol();
879cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines    assert(COMDAT);
880cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines    COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(COMDAT);
881cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines    assert(COMDATSymbol);
882cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines    COFFSection *Assoc = COMDATSymbol->Section;
883dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    if (!Assoc)
884cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines      report_fatal_error(
885cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines          Twine("Missing associated COMDAT section for section ") +
886cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines          MCSec.getSectionName());
88780646283796b20c6a1b7d8eb69ce6f0478d54383Nico Rieck
88880646283796b20c6a1b7d8eb69ce6f0478d54383Nico Rieck    // Skip this section if the associated section is unused.
88980646283796b20c6a1b7d8eb69ce6f0478d54383Nico Rieck    if (Assoc->Number == -1)
89080646283796b20c6a1b7d8eb69ce6f0478d54383Nico Rieck      continue;
89180646283796b20c6a1b7d8eb69ce6f0478d54383Nico Rieck
892dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    Section->Symbol->Aux[0].Aux.SectionDefinition.Number = SectionIndices[Assoc];
89380646283796b20c6a1b7d8eb69ce6f0478d54383Nico Rieck  }
89480646283796b20c6a1b7d8eb69ce6f0478d54383Nico Rieck
89580646283796b20c6a1b7d8eb69ce6f0478d54383Nico Rieck
896801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  // Assign file offsets to COFF object file structures.
897801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
898801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  unsigned offset = 0;
899801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
900801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer  offset += COFF::HeaderSize;
901a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer  offset += COFF::SectionSize * Header.NumberOfSections;
902801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
903dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  for (const auto & Section : Asm) {
904dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    COFFSection *Sec = SectionMap[&Section.getSection()];
905801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
906a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer    if (Sec->Number == -1)
907a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer      continue;
908a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer
909dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    Sec->Header.SizeOfRawData = Layout.getSectionAddressSize(&Section);
910801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
911a72d87899bc1bfdb17744aae2545a21b3630d3c1Michael J. Spencer    if (IsPhysicalSection(Sec)) {
912801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer      Sec->Header.PointerToRawData = offset;
913801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
914801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer      offset += Sec->Header.SizeOfRawData;
915801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer    }
916801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
917801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer    if (Sec->Relocations.size() > 0) {
918d03a29b69d7285ed2ca523d70a45174e33716727Michael J. Spencer      bool RelocationsOverflow = Sec->Relocations.size() >= 0xffff;
919d03a29b69d7285ed2ca523d70a45174e33716727Michael J. Spencer
920d03a29b69d7285ed2ca523d70a45174e33716727Michael J. Spencer      if (RelocationsOverflow) {
921d03a29b69d7285ed2ca523d70a45174e33716727Michael J. Spencer        // Signal overflow by setting NumberOfSections to max value. Actual
922d03a29b69d7285ed2ca523d70a45174e33716727Michael J. Spencer        // size is found in reloc #0. Microsoft tools understand this.
923d03a29b69d7285ed2ca523d70a45174e33716727Michael J. Spencer        Sec->Header.NumberOfRelocations = 0xffff;
924d03a29b69d7285ed2ca523d70a45174e33716727Michael J. Spencer      } else {
925d03a29b69d7285ed2ca523d70a45174e33716727Michael J. Spencer        Sec->Header.NumberOfRelocations = Sec->Relocations.size();
926d03a29b69d7285ed2ca523d70a45174e33716727Michael J. Spencer      }
927801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer      Sec->Header.PointerToRelocations = offset;
928801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
929d03a29b69d7285ed2ca523d70a45174e33716727Michael J. Spencer      if (RelocationsOverflow) {
930d03a29b69d7285ed2ca523d70a45174e33716727Michael J. Spencer        // Reloc #0 will contain actual count, so make room for it.
931d03a29b69d7285ed2ca523d70a45174e33716727Michael J. Spencer        offset += COFF::RelocationSize;
932d03a29b69d7285ed2ca523d70a45174e33716727Michael J. Spencer      }
933d03a29b69d7285ed2ca523d70a45174e33716727Michael J. Spencer
934801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer      offset += COFF::RelocationSize * Sec->Relocations.size();
935801a3591161789e9716b0ec519f0f950b2d0c2acMichael J. Spencer
936dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      for (auto & Relocation : Sec->Relocations) {
937dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines        assert(Relocation.Symb->Index != -1);
938