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