Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 1 | //===- yaml2coff - Convert YAML to a COFF object file ---------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// |
| 9 | /// \file |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 10 | /// The COFF component of yaml2obj. |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 11 | /// |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
David Majnemer | ddf28f2 | 2014-03-19 04:47:47 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/STLExtras.h" |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/StringExtras.h" |
| 16 | #include "llvm/ADT/StringMap.h" |
| 17 | #include "llvm/ADT/StringSwitch.h" |
Zachary Turner | a8cfc29 | 2017-06-14 15:59:27 +0000 | [diff] [blame] | 18 | #include "llvm/DebugInfo/CodeView/DebugStringTableSubsection.h" |
| 19 | #include "llvm/DebugInfo/CodeView/StringsAndChecksums.h" |
David Majnemer | f69b0585 | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 20 | #include "llvm/Object/COFF.h" |
Chris Bieneman | 8ff0c11 | 2016-06-27 19:53:53 +0000 | [diff] [blame] | 21 | #include "llvm/ObjectYAML/ObjectYAML.h" |
Alex Brachet | c22d966 | 2019-08-07 02:44:49 +0000 | [diff] [blame] | 22 | #include "llvm/ObjectYAML/yaml2obj.h" |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Endian.h" |
| 24 | #include "llvm/Support/MemoryBuffer.h" |
| 25 | #include "llvm/Support/SourceMgr.h" |
Martin Storsjo | 93a7137 | 2019-01-07 20:55:33 +0000 | [diff] [blame] | 26 | #include "llvm/Support/WithColor.h" |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
| 28 | #include <vector> |
| 29 | |
| 30 | using namespace llvm; |
| 31 | |
Alex Brachet | c22d966 | 2019-08-07 02:44:49 +0000 | [diff] [blame] | 32 | namespace { |
| 33 | |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 34 | /// This parses a yaml stream that represents a COFF object file. |
| 35 | /// See docs/yaml2obj for the yaml scheema. |
| 36 | struct COFFParser { |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 37 | COFFParser(COFFYAML::Object &Obj, yaml::ErrorHandler EH) |
| 38 | : Obj(Obj), SectionTableStart(0), SectionTableSize(0), ErrHandler(EH) { |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 39 | // A COFF string table always starts with a 4 byte size field. Offsets into |
| 40 | // it include this size, so allocate it now. |
Will Dietz | 0b48c73 | 2013-10-12 21:29:16 +0000 | [diff] [blame] | 41 | StringTable.append(4, char(0)); |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 42 | } |
| 43 | |
David Majnemer | 2cbc138 | 2014-09-16 03:52:46 +0000 | [diff] [blame] | 44 | bool useBigObj() const { |
Aaron Ballman | 4934e4b | 2014-10-22 13:09:43 +0000 | [diff] [blame] | 45 | return static_cast<int32_t>(Obj.Sections.size()) > |
| 46 | COFF::MaxNumberOfSections16; |
David Majnemer | 2cbc138 | 2014-09-16 03:52:46 +0000 | [diff] [blame] | 47 | } |
| 48 | |
David Majnemer | f69b0585 | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 49 | bool isPE() const { return Obj.OptionalHeader.hasValue(); } |
| 50 | bool is64Bit() const { |
Martin Storsjo | 5db3d33 | 2018-11-27 20:47:38 +0000 | [diff] [blame] | 51 | return Obj.Header.Machine == COFF::IMAGE_FILE_MACHINE_AMD64 || |
| 52 | Obj.Header.Machine == COFF::IMAGE_FILE_MACHINE_ARM64; |
David Majnemer | f69b0585 | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | uint32_t getFileAlignment() const { |
| 56 | return Obj.OptionalHeader->Header.FileAlignment; |
| 57 | } |
| 58 | |
David Majnemer | 2cbc138 | 2014-09-16 03:52:46 +0000 | [diff] [blame] | 59 | unsigned getHeaderSize() const { |
| 60 | return useBigObj() ? COFF::Header32Size : COFF::Header16Size; |
| 61 | } |
| 62 | |
| 63 | unsigned getSymbolSize() const { |
| 64 | return useBigObj() ? COFF::Symbol32Size : COFF::Symbol16Size; |
| 65 | } |
| 66 | |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 67 | bool parseSections() { |
| 68 | for (std::vector<COFFYAML::Section>::iterator i = Obj.Sections.begin(), |
Alex Brachet | c22d966 | 2019-08-07 02:44:49 +0000 | [diff] [blame] | 69 | e = Obj.Sections.end(); |
| 70 | i != e; ++i) { |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 71 | COFFYAML::Section &Sec = *i; |
| 72 | |
| 73 | // If the name is less than 8 bytes, store it in place, otherwise |
| 74 | // store it in the string table. |
| 75 | StringRef Name = Sec.Name; |
| 76 | |
| 77 | if (Name.size() <= COFF::NameSize) { |
| 78 | std::copy(Name.begin(), Name.end(), Sec.Header.Name); |
| 79 | } else { |
| 80 | // Add string to the string table and format the index for output. |
| 81 | unsigned Index = getStringIndex(Name); |
| 82 | std::string str = utostr(Index); |
| 83 | if (str.size() > 7) { |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 84 | ErrHandler("string table got too large"); |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 85 | return false; |
| 86 | } |
| 87 | Sec.Header.Name[0] = '/'; |
| 88 | std::copy(str.begin(), str.end(), Sec.Header.Name + 1); |
| 89 | } |
| 90 | |
David Majnemer | 6f66f0a | 2016-03-17 05:43:26 +0000 | [diff] [blame] | 91 | if (Sec.Alignment) { |
| 92 | if (Sec.Alignment > 8192) { |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 93 | ErrHandler("section alignment is too large"); |
David Majnemer | 6f66f0a | 2016-03-17 05:43:26 +0000 | [diff] [blame] | 94 | return false; |
| 95 | } |
| 96 | if (!isPowerOf2_32(Sec.Alignment)) { |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 97 | ErrHandler("section alignment is not a power of 2"); |
David Majnemer | 6f66f0a | 2016-03-17 05:43:26 +0000 | [diff] [blame] | 98 | return false; |
| 99 | } |
| 100 | Sec.Header.Characteristics |= (Log2_32(Sec.Alignment) + 1) << 20; |
| 101 | } |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 102 | } |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | bool parseSymbols() { |
| 107 | for (std::vector<COFFYAML::Symbol>::iterator i = Obj.Symbols.begin(), |
Alex Brachet | c22d966 | 2019-08-07 02:44:49 +0000 | [diff] [blame] | 108 | e = Obj.Symbols.end(); |
| 109 | i != e; ++i) { |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 110 | COFFYAML::Symbol &Sym = *i; |
| 111 | |
| 112 | // If the name is less than 8 bytes, store it in place, otherwise |
| 113 | // store it in the string table. |
| 114 | StringRef Name = Sym.Name; |
| 115 | if (Name.size() <= COFF::NameSize) { |
| 116 | std::copy(Name.begin(), Name.end(), Sym.Header.Name); |
| 117 | } else { |
| 118 | // Add string to the string table and format the index for output. |
| 119 | unsigned Index = getStringIndex(Name); |
Alex Brachet | c22d966 | 2019-08-07 02:44:49 +0000 | [diff] [blame] | 120 | *reinterpret_cast<support::aligned_ulittle32_t *>(Sym.Header.Name + 4) = |
| 121 | Index; |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | Sym.Header.Type = Sym.SimpleType; |
| 125 | Sym.Header.Type |= Sym.ComplexType << COFF::SCT_COMPLEX_TYPE_SHIFT; |
| 126 | } |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | bool parse() { |
| 131 | if (!parseSections()) |
| 132 | return false; |
| 133 | if (!parseSymbols()) |
| 134 | return false; |
| 135 | return true; |
| 136 | } |
| 137 | |
| 138 | unsigned getStringIndex(StringRef Str) { |
| 139 | StringMap<unsigned>::iterator i = StringTableMap.find(Str); |
| 140 | if (i == StringTableMap.end()) { |
| 141 | unsigned Index = StringTable.size(); |
| 142 | StringTable.append(Str.begin(), Str.end()); |
| 143 | StringTable.push_back(0); |
| 144 | StringTableMap[Str] = Index; |
| 145 | return Index; |
| 146 | } |
| 147 | return i->second; |
| 148 | } |
| 149 | |
| 150 | COFFYAML::Object &Obj; |
| 151 | |
Zachary Turner | a8cfc29 | 2017-06-14 15:59:27 +0000 | [diff] [blame] | 152 | codeview::StringsAndChecksums StringsAndChecksums; |
| 153 | BumpPtrAllocator Allocator; |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 154 | StringMap<unsigned> StringTableMap; |
| 155 | std::string StringTable; |
David Majnemer | f69b0585 | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 156 | uint32_t SectionTableStart; |
| 157 | uint32_t SectionTableSize; |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 158 | |
| 159 | yaml::ErrorHandler ErrHandler; |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 160 | }; |
| 161 | |
Alex Brachet | c22d966 | 2019-08-07 02:44:49 +0000 | [diff] [blame] | 162 | enum { DOSStubSize = 128 }; |
| 163 | |
| 164 | } // end anonymous namespace |
| 165 | |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 166 | // Take a CP and assign addresses and sizes to everything. Returns false if the |
| 167 | // layout is not valid to do. |
David Majnemer | f69b0585 | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 168 | static bool layoutOptionalHeader(COFFParser &CP) { |
| 169 | if (!CP.isPE()) |
| 170 | return true; |
David Majnemer | 966064c | 2014-11-14 19:35:59 +0000 | [diff] [blame] | 171 | unsigned PEHeaderSize = CP.is64Bit() ? sizeof(object::pe32plus_header) |
| 172 | : sizeof(object::pe32_header); |
David Majnemer | f69b0585 | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 173 | CP.Obj.Header.SizeOfOptionalHeader = |
David Majnemer | 966064c | 2014-11-14 19:35:59 +0000 | [diff] [blame] | 174 | PEHeaderSize + |
| 175 | sizeof(object::data_directory) * (COFF::NUM_DATA_DIRECTORIES + 1); |
David Majnemer | f69b0585 | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 176 | return true; |
| 177 | } |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 178 | |
Zachary Turner | a8cfc29 | 2017-06-14 15:59:27 +0000 | [diff] [blame] | 179 | static yaml::BinaryRef |
| 180 | toDebugS(ArrayRef<CodeViewYAML::YAMLDebugSubsection> Subsections, |
| 181 | const codeview::StringsAndChecksums &SC, BumpPtrAllocator &Allocator) { |
| 182 | using namespace codeview; |
| 183 | ExitOnError Err("Error occurred writing .debug$S section"); |
| 184 | auto CVSS = |
| 185 | Err(CodeViewYAML::toCodeViewSubsectionList(Allocator, Subsections, SC)); |
| 186 | |
| 187 | std::vector<DebugSubsectionRecordBuilder> Builders; |
| 188 | uint32_t Size = sizeof(uint32_t); |
| 189 | for (auto &SS : CVSS) { |
| 190 | DebugSubsectionRecordBuilder B(SS, CodeViewContainer::ObjectFile); |
| 191 | Size += B.calculateSerializedLength(); |
| 192 | Builders.push_back(std::move(B)); |
| 193 | } |
| 194 | uint8_t *Buffer = Allocator.Allocate<uint8_t>(Size); |
| 195 | MutableArrayRef<uint8_t> Output(Buffer, Size); |
| 196 | BinaryStreamWriter Writer(Output, support::little); |
| 197 | |
| 198 | Err(Writer.writeInteger<uint32_t>(COFF::DEBUG_SECTION_MAGIC)); |
| 199 | for (const auto &B : Builders) { |
| 200 | Err(B.commit(Writer)); |
| 201 | } |
| 202 | return {Output}; |
| 203 | } |
| 204 | |
David Majnemer | f69b0585 | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 205 | // Take a CP and assign addresses and sizes to everything. Returns false if the |
| 206 | // layout is not valid to do. |
| 207 | static bool layoutCOFF(COFFParser &CP) { |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 208 | // The section table starts immediately after the header, including the |
| 209 | // optional header. |
David Majnemer | f69b0585 | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 210 | CP.SectionTableStart = |
| 211 | CP.getHeaderSize() + CP.Obj.Header.SizeOfOptionalHeader; |
David Majnemer | 646f47f | 2014-11-15 02:03:59 +0000 | [diff] [blame] | 212 | if (CP.isPE()) |
| 213 | CP.SectionTableStart += DOSStubSize + sizeof(COFF::PEMagic); |
David Majnemer | f69b0585 | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 214 | CP.SectionTableSize = COFF::SectionSize * CP.Obj.Sections.size(); |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 215 | |
David Majnemer | f69b0585 | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 216 | uint32_t CurrentSectionDataOffset = |
| 217 | CP.SectionTableStart + CP.SectionTableSize; |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 218 | |
Zachary Turner | a8cfc29 | 2017-06-14 15:59:27 +0000 | [diff] [blame] | 219 | for (COFFYAML::Section &S : CP.Obj.Sections) { |
| 220 | // We support specifying exactly one of SectionData or Subsections. So if |
| 221 | // there is already some SectionData, then we don't need to do any of this. |
| 222 | if (S.Name == ".debug$S" && S.SectionData.binary_size() == 0) { |
| 223 | CodeViewYAML::initializeStringsAndChecksums(S.DebugS, |
| 224 | CP.StringsAndChecksums); |
| 225 | if (CP.StringsAndChecksums.hasChecksums() && |
| 226 | CP.StringsAndChecksums.hasStrings()) |
| 227 | break; |
| 228 | } |
| 229 | } |
| 230 | |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 231 | // Assign each section data address consecutively. |
David Majnemer | f69b0585 | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 232 | for (COFFYAML::Section &S : CP.Obj.Sections) { |
Zachary Turner | a8cfc29 | 2017-06-14 15:59:27 +0000 | [diff] [blame] | 233 | if (S.Name == ".debug$S") { |
| 234 | if (S.SectionData.binary_size() == 0) { |
| 235 | assert(CP.StringsAndChecksums.hasStrings() && |
| 236 | "Object file does not have debug string table!"); |
| 237 | |
| 238 | S.SectionData = |
| 239 | toDebugS(S.DebugS, CP.StringsAndChecksums, CP.Allocator); |
| 240 | } |
| 241 | } else if (S.Name == ".debug$T") { |
| 242 | if (S.SectionData.binary_size() == 0) |
Alexandre Ganea | d9e9674 | 2018-04-09 20:17:56 +0000 | [diff] [blame] | 243 | S.SectionData = CodeViewYAML::toDebugT(S.DebugT, CP.Allocator, S.Name); |
| 244 | } else if (S.Name == ".debug$P") { |
| 245 | if (S.SectionData.binary_size() == 0) |
| 246 | S.SectionData = CodeViewYAML::toDebugT(S.DebugP, CP.Allocator, S.Name); |
Zachary Turner | c221dc7 | 2017-12-06 18:58:48 +0000 | [diff] [blame] | 247 | } else if (S.Name == ".debug$H") { |
| 248 | if (S.DebugH.hasValue() && S.SectionData.binary_size() == 0) |
| 249 | S.SectionData = CodeViewYAML::toDebugH(*S.DebugH, CP.Allocator); |
Zachary Turner | a8cfc29 | 2017-06-14 15:59:27 +0000 | [diff] [blame] | 250 | } |
| 251 | |
David Majnemer | f69b0585 | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 252 | if (S.SectionData.binary_size() > 0) { |
Rui Ueyama | da00f2f | 2016-01-14 21:06:47 +0000 | [diff] [blame] | 253 | CurrentSectionDataOffset = alignTo(CurrentSectionDataOffset, |
| 254 | CP.isPE() ? CP.getFileAlignment() : 4); |
David Majnemer | f69b0585 | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 255 | S.Header.SizeOfRawData = S.SectionData.binary_size(); |
| 256 | if (CP.isPE()) |
| 257 | S.Header.SizeOfRawData = |
Rui Ueyama | da00f2f | 2016-01-14 21:06:47 +0000 | [diff] [blame] | 258 | alignTo(S.Header.SizeOfRawData, CP.getFileAlignment()); |
David Majnemer | f69b0585 | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 259 | S.Header.PointerToRawData = CurrentSectionDataOffset; |
| 260 | CurrentSectionDataOffset += S.Header.SizeOfRawData; |
| 261 | if (!S.Relocations.empty()) { |
| 262 | S.Header.PointerToRelocations = CurrentSectionDataOffset; |
| 263 | S.Header.NumberOfRelocations = S.Relocations.size(); |
| 264 | CurrentSectionDataOffset += |
| 265 | S.Header.NumberOfRelocations * COFF::RelocationSize; |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 266 | } |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 267 | } else { |
Reid Kleckner | 7eb6b5f | 2019-05-10 21:53:44 +0000 | [diff] [blame] | 268 | // Leave SizeOfRawData unaltered. For .bss sections in object files, it |
| 269 | // carries the section size. |
David Majnemer | f69b0585 | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 270 | S.Header.PointerToRawData = 0; |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | |
| 274 | uint32_t SymbolTableStart = CurrentSectionDataOffset; |
| 275 | |
| 276 | // Calculate number of symbols. |
| 277 | uint32_t NumberOfSymbols = 0; |
| 278 | for (std::vector<COFFYAML::Symbol>::iterator i = CP.Obj.Symbols.begin(), |
| 279 | e = CP.Obj.Symbols.end(); |
Alex Brachet | c22d966 | 2019-08-07 02:44:49 +0000 | [diff] [blame] | 280 | i != e; ++i) { |
David Majnemer | ddf28f2 | 2014-03-19 04:47:47 +0000 | [diff] [blame] | 281 | uint32_t NumberOfAuxSymbols = 0; |
| 282 | if (i->FunctionDefinition) |
| 283 | NumberOfAuxSymbols += 1; |
| 284 | if (i->bfAndefSymbol) |
| 285 | NumberOfAuxSymbols += 1; |
| 286 | if (i->WeakExternal) |
| 287 | NumberOfAuxSymbols += 1; |
| 288 | if (!i->File.empty()) |
| 289 | NumberOfAuxSymbols += |
David Majnemer | 2cbc138 | 2014-09-16 03:52:46 +0000 | [diff] [blame] | 290 | (i->File.size() + CP.getSymbolSize() - 1) / CP.getSymbolSize(); |
David Majnemer | ddf28f2 | 2014-03-19 04:47:47 +0000 | [diff] [blame] | 291 | if (i->SectionDefinition) |
| 292 | NumberOfAuxSymbols += 1; |
| 293 | if (i->CLRToken) |
| 294 | NumberOfAuxSymbols += 1; |
| 295 | i->Header.NumberOfAuxSymbols = NumberOfAuxSymbols; |
| 296 | NumberOfSymbols += 1 + NumberOfAuxSymbols; |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | // Store all the allocated start addresses in the header. |
| 300 | CP.Obj.Header.NumberOfSections = CP.Obj.Sections.size(); |
| 301 | CP.Obj.Header.NumberOfSymbols = NumberOfSymbols; |
David Majnemer | f69b0585 | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 302 | if (NumberOfSymbols > 0 || CP.StringTable.size() > 4) |
| 303 | CP.Obj.Header.PointerToSymbolTable = SymbolTableStart; |
| 304 | else |
| 305 | CP.Obj.Header.PointerToSymbolTable = 0; |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 306 | |
Alex Brachet | c22d966 | 2019-08-07 02:44:49 +0000 | [diff] [blame] | 307 | *reinterpret_cast<support::ulittle32_t *>(&CP.StringTable[0]) = |
| 308 | CP.StringTable.size(); |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 309 | |
| 310 | return true; |
| 311 | } |
| 312 | |
Alex Brachet | c22d966 | 2019-08-07 02:44:49 +0000 | [diff] [blame] | 313 | template <typename value_type> struct binary_le_impl { |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 314 | value_type Value; |
| 315 | binary_le_impl(value_type V) : Value(V) {} |
| 316 | }; |
| 317 | |
| 318 | template <typename value_type> |
Alex Brachet | c22d966 | 2019-08-07 02:44:49 +0000 | [diff] [blame] | 319 | raw_ostream &operator<<(raw_ostream &OS, |
| 320 | const binary_le_impl<value_type> &BLE) { |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 321 | char Buffer[sizeof(BLE.Value)]; |
| 322 | support::endian::write<value_type, support::little, support::unaligned>( |
Alex Brachet | c22d966 | 2019-08-07 02:44:49 +0000 | [diff] [blame] | 323 | Buffer, BLE.Value); |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 324 | OS.write(Buffer, sizeof(BLE.Value)); |
| 325 | return OS; |
| 326 | } |
| 327 | |
| 328 | template <typename value_type> |
| 329 | binary_le_impl<value_type> binary_le(value_type V) { |
| 330 | return binary_le_impl<value_type>(V); |
| 331 | } |
| 332 | |
Benjamin Kramer | 79de6e6 | 2015-04-11 18:57:14 +0000 | [diff] [blame] | 333 | template <size_t NumBytes> struct zeros_impl {}; |
David Majnemer | ddf28f2 | 2014-03-19 04:47:47 +0000 | [diff] [blame] | 334 | |
| 335 | template <size_t NumBytes> |
| 336 | raw_ostream &operator<<(raw_ostream &OS, const zeros_impl<NumBytes> &) { |
| 337 | char Buffer[NumBytes]; |
| 338 | memset(Buffer, 0, sizeof(Buffer)); |
| 339 | OS.write(Buffer, sizeof(Buffer)); |
| 340 | return OS; |
| 341 | } |
| 342 | |
Alex Brachet | c22d966 | 2019-08-07 02:44:49 +0000 | [diff] [blame] | 343 | template <typename T> zeros_impl<sizeof(T)> zeros(const T &) { |
David Majnemer | ddf28f2 | 2014-03-19 04:47:47 +0000 | [diff] [blame] | 344 | return zeros_impl<sizeof(T)>(); |
| 345 | } |
| 346 | |
David Majnemer | f69b0585 | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 347 | template <typename T> |
Alex Brachet | c22d966 | 2019-08-07 02:44:49 +0000 | [diff] [blame] | 348 | static uint32_t initializeOptionalHeader(COFFParser &CP, uint16_t Magic, |
| 349 | T Header) { |
David Majnemer | f69b0585 | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 350 | memset(Header, 0, sizeof(*Header)); |
| 351 | Header->Magic = Magic; |
| 352 | Header->SectionAlignment = CP.Obj.OptionalHeader->Header.SectionAlignment; |
David Majnemer | 966064c | 2014-11-14 19:35:59 +0000 | [diff] [blame] | 353 | Header->FileAlignment = CP.Obj.OptionalHeader->Header.FileAlignment; |
David Majnemer | f69b0585 | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 354 | uint32_t SizeOfCode = 0, SizeOfInitializedData = 0, |
| 355 | SizeOfUninitializedData = 0; |
Rui Ueyama | da00f2f | 2016-01-14 21:06:47 +0000 | [diff] [blame] | 356 | uint32_t SizeOfHeaders = alignTo(CP.SectionTableStart + CP.SectionTableSize, |
| 357 | Header->FileAlignment); |
| 358 | uint32_t SizeOfImage = alignTo(SizeOfHeaders, Header->SectionAlignment); |
David Majnemer | 966064c | 2014-11-14 19:35:59 +0000 | [diff] [blame] | 359 | uint32_t BaseOfData = 0; |
David Majnemer | f69b0585 | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 360 | for (const COFFYAML::Section &S : CP.Obj.Sections) { |
| 361 | if (S.Header.Characteristics & COFF::IMAGE_SCN_CNT_CODE) |
| 362 | SizeOfCode += S.Header.SizeOfRawData; |
| 363 | if (S.Header.Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA) |
| 364 | SizeOfInitializedData += S.Header.SizeOfRawData; |
| 365 | if (S.Header.Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) |
| 366 | SizeOfUninitializedData += S.Header.SizeOfRawData; |
| 367 | if (S.Name.equals(".text")) |
David Majnemer | 966064c | 2014-11-14 19:35:59 +0000 | [diff] [blame] | 368 | Header->BaseOfCode = S.Header.VirtualAddress; // RVA |
| 369 | else if (S.Name.equals(".data")) |
| 370 | BaseOfData = S.Header.VirtualAddress; // RVA |
David Majnemer | f69b0585 | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 371 | if (S.Header.VirtualAddress) |
Rui Ueyama | da00f2f | 2016-01-14 21:06:47 +0000 | [diff] [blame] | 372 | SizeOfImage += alignTo(S.Header.VirtualSize, Header->SectionAlignment); |
David Majnemer | f69b0585 | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 373 | } |
| 374 | Header->SizeOfCode = SizeOfCode; |
| 375 | Header->SizeOfInitializedData = SizeOfInitializedData; |
| 376 | Header->SizeOfUninitializedData = SizeOfUninitializedData; |
| 377 | Header->AddressOfEntryPoint = |
| 378 | CP.Obj.OptionalHeader->Header.AddressOfEntryPoint; // RVA |
| 379 | Header->ImageBase = CP.Obj.OptionalHeader->Header.ImageBase; |
David Majnemer | f69b0585 | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 380 | Header->MajorOperatingSystemVersion = |
| 381 | CP.Obj.OptionalHeader->Header.MajorOperatingSystemVersion; |
| 382 | Header->MinorOperatingSystemVersion = |
| 383 | CP.Obj.OptionalHeader->Header.MinorOperatingSystemVersion; |
Alex Brachet | c22d966 | 2019-08-07 02:44:49 +0000 | [diff] [blame] | 384 | Header->MajorImageVersion = CP.Obj.OptionalHeader->Header.MajorImageVersion; |
| 385 | Header->MinorImageVersion = CP.Obj.OptionalHeader->Header.MinorImageVersion; |
David Majnemer | f69b0585 | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 386 | Header->MajorSubsystemVersion = |
| 387 | CP.Obj.OptionalHeader->Header.MajorSubsystemVersion; |
| 388 | Header->MinorSubsystemVersion = |
| 389 | CP.Obj.OptionalHeader->Header.MinorSubsystemVersion; |
| 390 | Header->SizeOfImage = SizeOfImage; |
| 391 | Header->SizeOfHeaders = SizeOfHeaders; |
| 392 | Header->Subsystem = CP.Obj.OptionalHeader->Header.Subsystem; |
| 393 | Header->DLLCharacteristics = CP.Obj.OptionalHeader->Header.DLLCharacteristics; |
| 394 | Header->SizeOfStackReserve = CP.Obj.OptionalHeader->Header.SizeOfStackReserve; |
| 395 | Header->SizeOfStackCommit = CP.Obj.OptionalHeader->Header.SizeOfStackCommit; |
| 396 | Header->SizeOfHeapReserve = CP.Obj.OptionalHeader->Header.SizeOfHeapReserve; |
| 397 | Header->SizeOfHeapCommit = CP.Obj.OptionalHeader->Header.SizeOfHeapCommit; |
| 398 | Header->NumberOfRvaAndSize = COFF::NUM_DATA_DIRECTORIES + 1; |
David Majnemer | 966064c | 2014-11-14 19:35:59 +0000 | [diff] [blame] | 399 | return BaseOfData; |
David Majnemer | f69b0585 | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | static bool writeCOFF(COFFParser &CP, raw_ostream &OS) { |
| 403 | if (CP.isPE()) { |
| 404 | // PE files start with a DOS stub. |
| 405 | object::dos_header DH; |
| 406 | memset(&DH, 0, sizeof(DH)); |
| 407 | |
| 408 | // DOS EXEs start with "MZ" magic. |
| 409 | DH.Magic[0] = 'M'; |
| 410 | DH.Magic[1] = 'Z'; |
| 411 | // Initializing the AddressOfRelocationTable is strictly optional but |
| 412 | // mollifies certain tools which expect it to have a value greater than |
| 413 | // 0x40. |
| 414 | DH.AddressOfRelocationTable = sizeof(DH); |
| 415 | // This is the address of the PE signature. |
David Majnemer | 646f47f | 2014-11-15 02:03:59 +0000 | [diff] [blame] | 416 | DH.AddressOfNewExeHeader = DOSStubSize; |
David Majnemer | f69b0585 | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 417 | |
| 418 | // Write out our DOS stub. |
| 419 | OS.write(reinterpret_cast<char *>(&DH), sizeof(DH)); |
| 420 | // Write padding until we reach the position of where our PE signature |
| 421 | // should live. |
Fangrui Song | de0462a | 2019-04-24 13:23:15 +0000 | [diff] [blame] | 422 | OS.write_zeros(DOSStubSize - sizeof(DH)); |
David Majnemer | f69b0585 | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 423 | // Write out the PE signature. |
| 424 | OS.write(COFF::PEMagic, sizeof(COFF::PEMagic)); |
| 425 | } |
David Majnemer | 2cbc138 | 2014-09-16 03:52:46 +0000 | [diff] [blame] | 426 | if (CP.useBigObj()) { |
| 427 | OS << binary_le(static_cast<uint16_t>(COFF::IMAGE_FILE_MACHINE_UNKNOWN)) |
| 428 | << binary_le(static_cast<uint16_t>(0xffff)) |
Alex Brachet | c22d966 | 2019-08-07 02:44:49 +0000 | [diff] [blame] | 429 | << binary_le( |
| 430 | static_cast<uint16_t>(COFF::BigObjHeader::MinBigObjectVersion)) |
David Majnemer | 2cbc138 | 2014-09-16 03:52:46 +0000 | [diff] [blame] | 431 | << binary_le(CP.Obj.Header.Machine) |
| 432 | << binary_le(CP.Obj.Header.TimeDateStamp); |
| 433 | OS.write(COFF::BigObjMagic, sizeof(COFF::BigObjMagic)); |
Alex Brachet | c22d966 | 2019-08-07 02:44:49 +0000 | [diff] [blame] | 434 | OS << zeros(uint32_t(0)) << zeros(uint32_t(0)) << zeros(uint32_t(0)) |
| 435 | << zeros(uint32_t(0)) << binary_le(CP.Obj.Header.NumberOfSections) |
David Majnemer | 2cbc138 | 2014-09-16 03:52:46 +0000 | [diff] [blame] | 436 | << binary_le(CP.Obj.Header.PointerToSymbolTable) |
| 437 | << binary_le(CP.Obj.Header.NumberOfSymbols); |
| 438 | } else { |
| 439 | OS << binary_le(CP.Obj.Header.Machine) |
| 440 | << binary_le(static_cast<int16_t>(CP.Obj.Header.NumberOfSections)) |
| 441 | << binary_le(CP.Obj.Header.TimeDateStamp) |
| 442 | << binary_le(CP.Obj.Header.PointerToSymbolTable) |
| 443 | << binary_le(CP.Obj.Header.NumberOfSymbols) |
| 444 | << binary_le(CP.Obj.Header.SizeOfOptionalHeader) |
| 445 | << binary_le(CP.Obj.Header.Characteristics); |
| 446 | } |
David Majnemer | f69b0585 | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 447 | if (CP.isPE()) { |
| 448 | if (CP.is64Bit()) { |
| 449 | object::pe32plus_header PEH; |
| 450 | initializeOptionalHeader(CP, COFF::PE32Header::PE32_PLUS, &PEH); |
| 451 | OS.write(reinterpret_cast<char *>(&PEH), sizeof(PEH)); |
| 452 | } else { |
| 453 | object::pe32_header PEH; |
Alex Brachet | c22d966 | 2019-08-07 02:44:49 +0000 | [diff] [blame] | 454 | uint32_t BaseOfData = |
| 455 | initializeOptionalHeader(CP, COFF::PE32Header::PE32, &PEH); |
David Majnemer | 966064c | 2014-11-14 19:35:59 +0000 | [diff] [blame] | 456 | PEH.BaseOfData = BaseOfData; |
David Majnemer | f69b0585 | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 457 | OS.write(reinterpret_cast<char *>(&PEH), sizeof(PEH)); |
| 458 | } |
| 459 | for (const Optional<COFF::DataDirectory> &DD : |
| 460 | CP.Obj.OptionalHeader->DataDirectories) { |
| 461 | if (!DD.hasValue()) { |
| 462 | OS << zeros(uint32_t(0)); |
| 463 | OS << zeros(uint32_t(0)); |
| 464 | } else { |
| 465 | OS << binary_le(DD->RelativeVirtualAddress); |
| 466 | OS << binary_le(DD->Size); |
| 467 | } |
| 468 | } |
| 469 | OS << zeros(uint32_t(0)); |
| 470 | OS << zeros(uint32_t(0)); |
| 471 | } |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 472 | |
David Majnemer | 646f47f | 2014-11-15 02:03:59 +0000 | [diff] [blame] | 473 | assert(OS.tell() == CP.SectionTableStart); |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 474 | // Output section table. |
| 475 | for (std::vector<COFFYAML::Section>::iterator i = CP.Obj.Sections.begin(), |
| 476 | e = CP.Obj.Sections.end(); |
Alex Brachet | c22d966 | 2019-08-07 02:44:49 +0000 | [diff] [blame] | 477 | i != e; ++i) { |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 478 | OS.write(i->Header.Name, COFF::NameSize); |
| 479 | OS << binary_le(i->Header.VirtualSize) |
| 480 | << binary_le(i->Header.VirtualAddress) |
| 481 | << binary_le(i->Header.SizeOfRawData) |
| 482 | << binary_le(i->Header.PointerToRawData) |
| 483 | << binary_le(i->Header.PointerToRelocations) |
| 484 | << binary_le(i->Header.PointerToLineNumbers) |
| 485 | << binary_le(i->Header.NumberOfRelocations) |
| 486 | << binary_le(i->Header.NumberOfLineNumbers) |
| 487 | << binary_le(i->Header.Characteristics); |
| 488 | } |
David Majnemer | 646f47f | 2014-11-15 02:03:59 +0000 | [diff] [blame] | 489 | assert(OS.tell() == CP.SectionTableStart + CP.SectionTableSize); |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 490 | |
Rafael Espindola | e2e741e | 2013-06-06 13:06:17 +0000 | [diff] [blame] | 491 | unsigned CurSymbol = 0; |
| 492 | StringMap<unsigned> SymbolTableIndexMap; |
| 493 | for (std::vector<COFFYAML::Symbol>::iterator I = CP.Obj.Symbols.begin(), |
| 494 | E = CP.Obj.Symbols.end(); |
| 495 | I != E; ++I) { |
| 496 | SymbolTableIndexMap[I->Name] = CurSymbol; |
| 497 | CurSymbol += 1 + I->Header.NumberOfAuxSymbols; |
| 498 | } |
| 499 | |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 500 | // Output section data. |
David Majnemer | f69b0585 | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 501 | for (const COFFYAML::Section &S : CP.Obj.Sections) { |
Reid Kleckner | 7eb6b5f | 2019-05-10 21:53:44 +0000 | [diff] [blame] | 502 | if (S.Header.SizeOfRawData == 0 || S.Header.PointerToRawData == 0) |
David Majnemer | f69b0585 | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 503 | continue; |
David Majnemer | 646f47f | 2014-11-15 02:03:59 +0000 | [diff] [blame] | 504 | assert(S.Header.PointerToRawData >= OS.tell()); |
Fangrui Song | de0462a | 2019-04-24 13:23:15 +0000 | [diff] [blame] | 505 | OS.write_zeros(S.Header.PointerToRawData - OS.tell()); |
David Majnemer | f69b0585 | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 506 | S.SectionData.writeAsBinary(OS); |
David Majnemer | 646f47f | 2014-11-15 02:03:59 +0000 | [diff] [blame] | 507 | assert(S.Header.SizeOfRawData >= S.SectionData.binary_size()); |
Fangrui Song | de0462a | 2019-04-24 13:23:15 +0000 | [diff] [blame] | 508 | OS.write_zeros(S.Header.SizeOfRawData - S.SectionData.binary_size()); |
David Majnemer | f69b0585 | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 509 | for (const COFFYAML::Relocation &R : S.Relocations) { |
Martin Storsjo | 93a7137 | 2019-01-07 20:55:33 +0000 | [diff] [blame] | 510 | uint32_t SymbolTableIndex; |
| 511 | if (R.SymbolTableIndex) { |
| 512 | if (!R.SymbolName.empty()) |
| 513 | WithColor::error() |
| 514 | << "Both SymbolName and SymbolTableIndex specified\n"; |
| 515 | SymbolTableIndex = *R.SymbolTableIndex; |
| 516 | } else { |
| 517 | SymbolTableIndex = SymbolTableIndexMap[R.SymbolName]; |
| 518 | } |
Alex Brachet | c22d966 | 2019-08-07 02:44:49 +0000 | [diff] [blame] | 519 | OS << binary_le(R.VirtualAddress) << binary_le(SymbolTableIndex) |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 520 | << binary_le(R.Type); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | // Output symbol table. |
| 525 | |
| 526 | for (std::vector<COFFYAML::Symbol>::const_iterator i = CP.Obj.Symbols.begin(), |
| 527 | e = CP.Obj.Symbols.end(); |
Alex Brachet | c22d966 | 2019-08-07 02:44:49 +0000 | [diff] [blame] | 528 | i != e; ++i) { |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 529 | OS.write(i->Header.Name, COFF::NameSize); |
David Majnemer | 2cbc138 | 2014-09-16 03:52:46 +0000 | [diff] [blame] | 530 | OS << binary_le(i->Header.Value); |
| 531 | if (CP.useBigObj()) |
Alex Brachet | c22d966 | 2019-08-07 02:44:49 +0000 | [diff] [blame] | 532 | OS << binary_le(i->Header.SectionNumber); |
David Majnemer | 2cbc138 | 2014-09-16 03:52:46 +0000 | [diff] [blame] | 533 | else |
Alex Brachet | c22d966 | 2019-08-07 02:44:49 +0000 | [diff] [blame] | 534 | OS << binary_le(static_cast<int16_t>(i->Header.SectionNumber)); |
| 535 | OS << binary_le(i->Header.Type) << binary_le(i->Header.StorageClass) |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 536 | << binary_le(i->Header.NumberOfAuxSymbols); |
David Majnemer | ddf28f2 | 2014-03-19 04:47:47 +0000 | [diff] [blame] | 537 | |
Fangrui Song | de0462a | 2019-04-24 13:23:15 +0000 | [diff] [blame] | 538 | if (i->FunctionDefinition) { |
David Majnemer | ddf28f2 | 2014-03-19 04:47:47 +0000 | [diff] [blame] | 539 | OS << binary_le(i->FunctionDefinition->TagIndex) |
| 540 | << binary_le(i->FunctionDefinition->TotalSize) |
| 541 | << binary_le(i->FunctionDefinition->PointerToLinenumber) |
| 542 | << binary_le(i->FunctionDefinition->PointerToNextFunction) |
Fangrui Song | de0462a | 2019-04-24 13:23:15 +0000 | [diff] [blame] | 543 | << zeros(i->FunctionDefinition->unused); |
| 544 | OS.write_zeros(CP.getSymbolSize() - COFF::Symbol16Size); |
| 545 | } |
| 546 | if (i->bfAndefSymbol) { |
David Majnemer | ddf28f2 | 2014-03-19 04:47:47 +0000 | [diff] [blame] | 547 | OS << zeros(i->bfAndefSymbol->unused1) |
| 548 | << binary_le(i->bfAndefSymbol->Linenumber) |
| 549 | << zeros(i->bfAndefSymbol->unused2) |
| 550 | << binary_le(i->bfAndefSymbol->PointerToNextFunction) |
Fangrui Song | de0462a | 2019-04-24 13:23:15 +0000 | [diff] [blame] | 551 | << zeros(i->bfAndefSymbol->unused3); |
| 552 | OS.write_zeros(CP.getSymbolSize() - COFF::Symbol16Size); |
| 553 | } |
| 554 | if (i->WeakExternal) { |
David Majnemer | ddf28f2 | 2014-03-19 04:47:47 +0000 | [diff] [blame] | 555 | OS << binary_le(i->WeakExternal->TagIndex) |
| 556 | << binary_le(i->WeakExternal->Characteristics) |
Fangrui Song | de0462a | 2019-04-24 13:23:15 +0000 | [diff] [blame] | 557 | << zeros(i->WeakExternal->unused); |
| 558 | OS.write_zeros(CP.getSymbolSize() - COFF::Symbol16Size); |
| 559 | } |
David Majnemer | ddf28f2 | 2014-03-19 04:47:47 +0000 | [diff] [blame] | 560 | if (!i->File.empty()) { |
David Majnemer | 2cbc138 | 2014-09-16 03:52:46 +0000 | [diff] [blame] | 561 | unsigned SymbolSize = CP.getSymbolSize(); |
David Majnemer | ddf28f2 | 2014-03-19 04:47:47 +0000 | [diff] [blame] | 562 | uint32_t NumberOfAuxRecords = |
David Majnemer | 2cbc138 | 2014-09-16 03:52:46 +0000 | [diff] [blame] | 563 | (i->File.size() + SymbolSize - 1) / SymbolSize; |
| 564 | uint32_t NumberOfAuxBytes = NumberOfAuxRecords * SymbolSize; |
David Majnemer | ddf28f2 | 2014-03-19 04:47:47 +0000 | [diff] [blame] | 565 | uint32_t NumZeros = NumberOfAuxBytes - i->File.size(); |
| 566 | OS.write(i->File.data(), i->File.size()); |
Fangrui Song | de0462a | 2019-04-24 13:23:15 +0000 | [diff] [blame] | 567 | OS.write_zeros(NumZeros); |
David Majnemer | ddf28f2 | 2014-03-19 04:47:47 +0000 | [diff] [blame] | 568 | } |
Fangrui Song | de0462a | 2019-04-24 13:23:15 +0000 | [diff] [blame] | 569 | if (i->SectionDefinition) { |
David Majnemer | ddf28f2 | 2014-03-19 04:47:47 +0000 | [diff] [blame] | 570 | OS << binary_le(i->SectionDefinition->Length) |
| 571 | << binary_le(i->SectionDefinition->NumberOfRelocations) |
| 572 | << binary_le(i->SectionDefinition->NumberOfLinenumbers) |
| 573 | << binary_le(i->SectionDefinition->CheckSum) |
David Majnemer | 4d57159 | 2014-09-15 19:42:42 +0000 | [diff] [blame] | 574 | << binary_le(static_cast<int16_t>(i->SectionDefinition->Number)) |
David Majnemer | ddf28f2 | 2014-03-19 04:47:47 +0000 | [diff] [blame] | 575 | << binary_le(i->SectionDefinition->Selection) |
David Majnemer | 4d57159 | 2014-09-15 19:42:42 +0000 | [diff] [blame] | 576 | << zeros(i->SectionDefinition->unused) |
Fangrui Song | de0462a | 2019-04-24 13:23:15 +0000 | [diff] [blame] | 577 | << binary_le(static_cast<int16_t>(i->SectionDefinition->Number >> 16)); |
| 578 | OS.write_zeros(CP.getSymbolSize() - COFF::Symbol16Size); |
| 579 | } |
| 580 | if (i->CLRToken) { |
Alex Brachet | c22d966 | 2019-08-07 02:44:49 +0000 | [diff] [blame] | 581 | OS << binary_le(i->CLRToken->AuxType) << zeros(i->CLRToken->unused1) |
David Majnemer | ddf28f2 | 2014-03-19 04:47:47 +0000 | [diff] [blame] | 582 | << binary_le(i->CLRToken->SymbolTableIndex) |
Fangrui Song | de0462a | 2019-04-24 13:23:15 +0000 | [diff] [blame] | 583 | << zeros(i->CLRToken->unused2); |
| 584 | OS.write_zeros(CP.getSymbolSize() - COFF::Symbol16Size); |
| 585 | } |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | // Output string table. |
David Majnemer | f69b0585 | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 589 | if (CP.Obj.Header.PointerToSymbolTable) |
| 590 | OS.write(&CP.StringTable[0], CP.StringTable.size()); |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 591 | return true; |
| 592 | } |
| 593 | |
Alex Brachet | c22d966 | 2019-08-07 02:44:49 +0000 | [diff] [blame] | 594 | namespace llvm { |
| 595 | namespace yaml { |
| 596 | |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 597 | bool yaml2coff(llvm::COFFYAML::Object &Doc, raw_ostream &Out, |
| 598 | ErrorHandler ErrHandler) { |
| 599 | COFFParser CP(Doc, ErrHandler); |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 600 | if (!CP.parse()) { |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 601 | ErrHandler("failed to parse YAML file"); |
George Rimar | 7da559f | 2019-09-13 09:12:38 +0000 | [diff] [blame] | 602 | return false; |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 603 | } |
| 604 | |
David Majnemer | f69b0585 | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 605 | if (!layoutOptionalHeader(CP)) { |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 606 | ErrHandler("failed to layout optional header for COFF file"); |
George Rimar | 7da559f | 2019-09-13 09:12:38 +0000 | [diff] [blame] | 607 | return false; |
David Majnemer | f69b0585 | 2014-11-14 08:15:42 +0000 | [diff] [blame] | 608 | } |
Zachary Turner | a8cfc29 | 2017-06-14 15:59:27 +0000 | [diff] [blame] | 609 | |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 610 | if (!layoutCOFF(CP)) { |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 611 | ErrHandler("failed to layout COFF file"); |
George Rimar | 7da559f | 2019-09-13 09:12:38 +0000 | [diff] [blame] | 612 | return false; |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 613 | } |
| 614 | if (!writeCOFF(CP, Out)) { |
George Rimar | 8501102 | 2019-09-13 16:00:16 +0000 | [diff] [blame] | 615 | ErrHandler("failed to write COFF file"); |
George Rimar | 7da559f | 2019-09-13 09:12:38 +0000 | [diff] [blame] | 616 | return false; |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 617 | } |
George Rimar | 7da559f | 2019-09-13 09:12:38 +0000 | [diff] [blame] | 618 | return true; |
Sean Silva | 3b76e40 | 2013-06-05 19:56:47 +0000 | [diff] [blame] | 619 | } |
Alex Brachet | c22d966 | 2019-08-07 02:44:49 +0000 | [diff] [blame] | 620 | |
| 621 | } // namespace yaml |
| 622 | } // namespace llvm |