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