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