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