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