blob: f9d255e1ad20ee09a9a9667c9a593e8ad1587e2e [file] [log] [blame]
Sean Silva3b76e402013-06-05 19:56:47 +00001//===- yaml2coff - Convert YAML to a COFF object file ---------------------===//
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/// \file
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000011/// The COFF component of yaml2obj.
Sean Silva3b76e402013-06-05 19:56:47 +000012///
13//===----------------------------------------------------------------------===//
14
15#include "yaml2obj.h"
David Majnemerddf28f22014-03-19 04:47:47 +000016#include "llvm/ADT/STLExtras.h"
Sean Silva3b76e402013-06-05 19:56:47 +000017#include "llvm/ADT/StringExtras.h"
18#include "llvm/ADT/StringMap.h"
19#include "llvm/ADT/StringSwitch.h"
Zachary Turnera8cfc292017-06-14 15:59:27 +000020#include "llvm/DebugInfo/CodeView/DebugStringTableSubsection.h"
21#include "llvm/DebugInfo/CodeView/StringsAndChecksums.h"
David Majnemerf69b05852014-11-14 08:15:42 +000022#include "llvm/Object/COFF.h"
Chris Bieneman8ff0c112016-06-27 19:53:53 +000023#include "llvm/ObjectYAML/ObjectYAML.h"
Sean Silva3b76e402013-06-05 19:56:47 +000024#include "llvm/Support/Endian.h"
25#include "llvm/Support/MemoryBuffer.h"
26#include "llvm/Support/SourceMgr.h"
27#include "llvm/Support/raw_ostream.h"
28#include <vector>
29
30using namespace llvm;
31
32/// This parses a yaml stream that represents a COFF object file.
33/// See docs/yaml2obj for the yaml scheema.
34struct COFFParser {
David Majnemerf69b05852014-11-14 08:15:42 +000035 COFFParser(COFFYAML::Object &Obj)
36 : Obj(Obj), SectionTableStart(0), SectionTableSize(0) {
Sean Silva3b76e402013-06-05 19:56:47 +000037 // A COFF string table always starts with a 4 byte size field. Offsets into
38 // it include this size, so allocate it now.
Will Dietz0b48c732013-10-12 21:29:16 +000039 StringTable.append(4, char(0));
Sean Silva3b76e402013-06-05 19:56:47 +000040 }
41
David Majnemer2cbc1382014-09-16 03:52:46 +000042 bool useBigObj() const {
Aaron Ballman4934e4b2014-10-22 13:09:43 +000043 return static_cast<int32_t>(Obj.Sections.size()) >
44 COFF::MaxNumberOfSections16;
David Majnemer2cbc1382014-09-16 03:52:46 +000045 }
46
David Majnemerf69b05852014-11-14 08:15:42 +000047 bool isPE() const { return Obj.OptionalHeader.hasValue(); }
48 bool is64Bit() const {
Martin Storsjo5db3d332018-11-27 20:47:38 +000049 return Obj.Header.Machine == COFF::IMAGE_FILE_MACHINE_AMD64 ||
50 Obj.Header.Machine == COFF::IMAGE_FILE_MACHINE_ARM64;
David Majnemerf69b05852014-11-14 08:15:42 +000051 }
52
53 uint32_t getFileAlignment() const {
54 return Obj.OptionalHeader->Header.FileAlignment;
55 }
56
David Majnemer2cbc1382014-09-16 03:52:46 +000057 unsigned getHeaderSize() const {
58 return useBigObj() ? COFF::Header32Size : COFF::Header16Size;
59 }
60
61 unsigned getSymbolSize() const {
62 return useBigObj() ? COFF::Symbol32Size : COFF::Symbol16Size;
63 }
64
Sean Silva3b76e402013-06-05 19:56:47 +000065 bool parseSections() {
66 for (std::vector<COFFYAML::Section>::iterator i = Obj.Sections.begin(),
67 e = Obj.Sections.end(); i != e; ++i) {
68 COFFYAML::Section &Sec = *i;
69
70 // If the name is less than 8 bytes, store it in place, otherwise
71 // store it in the string table.
72 StringRef Name = Sec.Name;
73
74 if (Name.size() <= COFF::NameSize) {
75 std::copy(Name.begin(), Name.end(), Sec.Header.Name);
76 } else {
77 // Add string to the string table and format the index for output.
78 unsigned Index = getStringIndex(Name);
79 std::string str = utostr(Index);
80 if (str.size() > 7) {
David Majnemer6f66f0a2016-03-17 05:43:26 +000081 errs() << "String table got too large\n";
Sean Silva3b76e402013-06-05 19:56:47 +000082 return false;
83 }
84 Sec.Header.Name[0] = '/';
85 std::copy(str.begin(), str.end(), Sec.Header.Name + 1);
86 }
87
David Majnemer6f66f0a2016-03-17 05:43:26 +000088 if (Sec.Alignment) {
89 if (Sec.Alignment > 8192) {
90 errs() << "Section alignment is too large\n";
91 return false;
92 }
93 if (!isPowerOf2_32(Sec.Alignment)) {
94 errs() << "Section alignment is not a power of 2\n";
95 return false;
96 }
97 Sec.Header.Characteristics |= (Log2_32(Sec.Alignment) + 1) << 20;
98 }
Sean Silva3b76e402013-06-05 19:56:47 +000099 }
100 return true;
101 }
102
103 bool parseSymbols() {
104 for (std::vector<COFFYAML::Symbol>::iterator i = Obj.Symbols.begin(),
105 e = Obj.Symbols.end(); i != e; ++i) {
106 COFFYAML::Symbol &Sym = *i;
107
108 // If the name is less than 8 bytes, store it in place, otherwise
109 // store it in the string table.
110 StringRef Name = Sym.Name;
111 if (Name.size() <= COFF::NameSize) {
112 std::copy(Name.begin(), Name.end(), Sym.Header.Name);
113 } else {
114 // Add string to the string table and format the index for output.
115 unsigned Index = getStringIndex(Name);
116 *reinterpret_cast<support::aligned_ulittle32_t*>(
117 Sym.Header.Name + 4) = Index;
118 }
119
120 Sym.Header.Type = Sym.SimpleType;
121 Sym.Header.Type |= Sym.ComplexType << COFF::SCT_COMPLEX_TYPE_SHIFT;
122 }
123 return true;
124 }
125
126 bool parse() {
127 if (!parseSections())
128 return false;
129 if (!parseSymbols())
130 return false;
131 return true;
132 }
133
134 unsigned getStringIndex(StringRef Str) {
135 StringMap<unsigned>::iterator i = StringTableMap.find(Str);
136 if (i == StringTableMap.end()) {
137 unsigned Index = StringTable.size();
138 StringTable.append(Str.begin(), Str.end());
139 StringTable.push_back(0);
140 StringTableMap[Str] = Index;
141 return Index;
142 }
143 return i->second;
144 }
145
146 COFFYAML::Object &Obj;
147
Zachary Turnera8cfc292017-06-14 15:59:27 +0000148 codeview::StringsAndChecksums StringsAndChecksums;
149 BumpPtrAllocator Allocator;
Sean Silva3b76e402013-06-05 19:56:47 +0000150 StringMap<unsigned> StringTableMap;
151 std::string StringTable;
David Majnemerf69b05852014-11-14 08:15:42 +0000152 uint32_t SectionTableStart;
153 uint32_t SectionTableSize;
Sean Silva3b76e402013-06-05 19:56:47 +0000154};
155
156// Take a CP and assign addresses and sizes to everything. Returns false if the
157// layout is not valid to do.
David Majnemerf69b05852014-11-14 08:15:42 +0000158static bool layoutOptionalHeader(COFFParser &CP) {
159 if (!CP.isPE())
160 return true;
David Majnemer966064c2014-11-14 19:35:59 +0000161 unsigned PEHeaderSize = CP.is64Bit() ? sizeof(object::pe32plus_header)
162 : sizeof(object::pe32_header);
David Majnemerf69b05852014-11-14 08:15:42 +0000163 CP.Obj.Header.SizeOfOptionalHeader =
David Majnemer966064c2014-11-14 19:35:59 +0000164 PEHeaderSize +
165 sizeof(object::data_directory) * (COFF::NUM_DATA_DIRECTORIES + 1);
David Majnemerf69b05852014-11-14 08:15:42 +0000166 return true;
167}
Sean Silva3b76e402013-06-05 19:56:47 +0000168
David Majnemer646f47f2014-11-15 02:03:59 +0000169namespace {
170enum { DOSStubSize = 128 };
171}
172
Zachary Turnera8cfc292017-06-14 15:59:27 +0000173static yaml::BinaryRef
174toDebugS(ArrayRef<CodeViewYAML::YAMLDebugSubsection> Subsections,
175 const codeview::StringsAndChecksums &SC, BumpPtrAllocator &Allocator) {
176 using namespace codeview;
177 ExitOnError Err("Error occurred writing .debug$S section");
178 auto CVSS =
179 Err(CodeViewYAML::toCodeViewSubsectionList(Allocator, Subsections, SC));
180
181 std::vector<DebugSubsectionRecordBuilder> Builders;
182 uint32_t Size = sizeof(uint32_t);
183 for (auto &SS : CVSS) {
184 DebugSubsectionRecordBuilder B(SS, CodeViewContainer::ObjectFile);
185 Size += B.calculateSerializedLength();
186 Builders.push_back(std::move(B));
187 }
188 uint8_t *Buffer = Allocator.Allocate<uint8_t>(Size);
189 MutableArrayRef<uint8_t> Output(Buffer, Size);
190 BinaryStreamWriter Writer(Output, support::little);
191
192 Err(Writer.writeInteger<uint32_t>(COFF::DEBUG_SECTION_MAGIC));
193 for (const auto &B : Builders) {
194 Err(B.commit(Writer));
195 }
196 return {Output};
197}
198
David Majnemerf69b05852014-11-14 08:15:42 +0000199// Take a CP and assign addresses and sizes to everything. Returns false if the
200// layout is not valid to do.
201static bool layoutCOFF(COFFParser &CP) {
Sean Silva3b76e402013-06-05 19:56:47 +0000202 // The section table starts immediately after the header, including the
203 // optional header.
David Majnemerf69b05852014-11-14 08:15:42 +0000204 CP.SectionTableStart =
205 CP.getHeaderSize() + CP.Obj.Header.SizeOfOptionalHeader;
David Majnemer646f47f2014-11-15 02:03:59 +0000206 if (CP.isPE())
207 CP.SectionTableStart += DOSStubSize + sizeof(COFF::PEMagic);
David Majnemerf69b05852014-11-14 08:15:42 +0000208 CP.SectionTableSize = COFF::SectionSize * CP.Obj.Sections.size();
Sean Silva3b76e402013-06-05 19:56:47 +0000209
David Majnemerf69b05852014-11-14 08:15:42 +0000210 uint32_t CurrentSectionDataOffset =
211 CP.SectionTableStart + CP.SectionTableSize;
Sean Silva3b76e402013-06-05 19:56:47 +0000212
Zachary Turnera8cfc292017-06-14 15:59:27 +0000213 for (COFFYAML::Section &S : CP.Obj.Sections) {
214 // We support specifying exactly one of SectionData or Subsections. So if
215 // there is already some SectionData, then we don't need to do any of this.
216 if (S.Name == ".debug$S" && S.SectionData.binary_size() == 0) {
217 CodeViewYAML::initializeStringsAndChecksums(S.DebugS,
218 CP.StringsAndChecksums);
219 if (CP.StringsAndChecksums.hasChecksums() &&
220 CP.StringsAndChecksums.hasStrings())
221 break;
222 }
223 }
224
Sean Silva3b76e402013-06-05 19:56:47 +0000225 // Assign each section data address consecutively.
David Majnemerf69b05852014-11-14 08:15:42 +0000226 for (COFFYAML::Section &S : CP.Obj.Sections) {
Zachary Turnera8cfc292017-06-14 15:59:27 +0000227 if (S.Name == ".debug$S") {
228 if (S.SectionData.binary_size() == 0) {
229 assert(CP.StringsAndChecksums.hasStrings() &&
230 "Object file does not have debug string table!");
231
232 S.SectionData =
233 toDebugS(S.DebugS, CP.StringsAndChecksums, CP.Allocator);
234 }
235 } else if (S.Name == ".debug$T") {
236 if (S.SectionData.binary_size() == 0)
Alexandre Ganead9e96742018-04-09 20:17:56 +0000237 S.SectionData = CodeViewYAML::toDebugT(S.DebugT, CP.Allocator, S.Name);
238 } else if (S.Name == ".debug$P") {
239 if (S.SectionData.binary_size() == 0)
240 S.SectionData = CodeViewYAML::toDebugT(S.DebugP, CP.Allocator, S.Name);
Zachary Turnerc221dc72017-12-06 18:58:48 +0000241 } else if (S.Name == ".debug$H") {
242 if (S.DebugH.hasValue() && S.SectionData.binary_size() == 0)
243 S.SectionData = CodeViewYAML::toDebugH(*S.DebugH, CP.Allocator);
Zachary Turnera8cfc292017-06-14 15:59:27 +0000244 }
245
David Majnemerf69b05852014-11-14 08:15:42 +0000246 if (S.SectionData.binary_size() > 0) {
Rui Ueyamada00f2f2016-01-14 21:06:47 +0000247 CurrentSectionDataOffset = alignTo(CurrentSectionDataOffset,
248 CP.isPE() ? CP.getFileAlignment() : 4);
David Majnemerf69b05852014-11-14 08:15:42 +0000249 S.Header.SizeOfRawData = S.SectionData.binary_size();
250 if (CP.isPE())
251 S.Header.SizeOfRawData =
Rui Ueyamada00f2f2016-01-14 21:06:47 +0000252 alignTo(S.Header.SizeOfRawData, CP.getFileAlignment());
David Majnemerf69b05852014-11-14 08:15:42 +0000253 S.Header.PointerToRawData = CurrentSectionDataOffset;
254 CurrentSectionDataOffset += S.Header.SizeOfRawData;
255 if (!S.Relocations.empty()) {
256 S.Header.PointerToRelocations = CurrentSectionDataOffset;
257 S.Header.NumberOfRelocations = S.Relocations.size();
258 CurrentSectionDataOffset +=
259 S.Header.NumberOfRelocations * COFF::RelocationSize;
Sean Silva3b76e402013-06-05 19:56:47 +0000260 }
Sean Silva3b76e402013-06-05 19:56:47 +0000261 } else {
David Majnemerf69b05852014-11-14 08:15:42 +0000262 S.Header.SizeOfRawData = 0;
263 S.Header.PointerToRawData = 0;
Sean Silva3b76e402013-06-05 19:56:47 +0000264 }
265 }
266
267 uint32_t SymbolTableStart = CurrentSectionDataOffset;
268
269 // Calculate number of symbols.
270 uint32_t NumberOfSymbols = 0;
271 for (std::vector<COFFYAML::Symbol>::iterator i = CP.Obj.Symbols.begin(),
272 e = CP.Obj.Symbols.end();
273 i != e; ++i) {
David Majnemerddf28f22014-03-19 04:47:47 +0000274 uint32_t NumberOfAuxSymbols = 0;
275 if (i->FunctionDefinition)
276 NumberOfAuxSymbols += 1;
277 if (i->bfAndefSymbol)
278 NumberOfAuxSymbols += 1;
279 if (i->WeakExternal)
280 NumberOfAuxSymbols += 1;
281 if (!i->File.empty())
282 NumberOfAuxSymbols +=
David Majnemer2cbc1382014-09-16 03:52:46 +0000283 (i->File.size() + CP.getSymbolSize() - 1) / CP.getSymbolSize();
David Majnemerddf28f22014-03-19 04:47:47 +0000284 if (i->SectionDefinition)
285 NumberOfAuxSymbols += 1;
286 if (i->CLRToken)
287 NumberOfAuxSymbols += 1;
288 i->Header.NumberOfAuxSymbols = NumberOfAuxSymbols;
289 NumberOfSymbols += 1 + NumberOfAuxSymbols;
Sean Silva3b76e402013-06-05 19:56:47 +0000290 }
291
292 // Store all the allocated start addresses in the header.
293 CP.Obj.Header.NumberOfSections = CP.Obj.Sections.size();
294 CP.Obj.Header.NumberOfSymbols = NumberOfSymbols;
David Majnemerf69b05852014-11-14 08:15:42 +0000295 if (NumberOfSymbols > 0 || CP.StringTable.size() > 4)
296 CP.Obj.Header.PointerToSymbolTable = SymbolTableStart;
297 else
298 CP.Obj.Header.PointerToSymbolTable = 0;
Sean Silva3b76e402013-06-05 19:56:47 +0000299
300 *reinterpret_cast<support::ulittle32_t *>(&CP.StringTable[0])
301 = CP.StringTable.size();
302
303 return true;
304}
305
306template <typename value_type>
307struct binary_le_impl {
308 value_type Value;
309 binary_le_impl(value_type V) : Value(V) {}
310};
311
312template <typename value_type>
313raw_ostream &operator <<( raw_ostream &OS
314 , const binary_le_impl<value_type> &BLE) {
315 char Buffer[sizeof(BLE.Value)];
316 support::endian::write<value_type, support::little, support::unaligned>(
317 Buffer, BLE.Value);
318 OS.write(Buffer, sizeof(BLE.Value));
319 return OS;
320}
321
322template <typename value_type>
323binary_le_impl<value_type> binary_le(value_type V) {
324 return binary_le_impl<value_type>(V);
325}
326
Benjamin Kramer79de6e62015-04-11 18:57:14 +0000327template <size_t NumBytes> struct zeros_impl {};
David Majnemerddf28f22014-03-19 04:47:47 +0000328
329template <size_t NumBytes>
330raw_ostream &operator<<(raw_ostream &OS, const zeros_impl<NumBytes> &) {
331 char Buffer[NumBytes];
332 memset(Buffer, 0, sizeof(Buffer));
333 OS.write(Buffer, sizeof(Buffer));
334 return OS;
335}
336
337template <typename T>
338zeros_impl<sizeof(T)> zeros(const T &) {
339 return zeros_impl<sizeof(T)>();
340}
341
David Majnemer2cbc1382014-09-16 03:52:46 +0000342struct num_zeros_impl {
343 size_t N;
344 num_zeros_impl(size_t N) : N(N) {}
345};
346
347raw_ostream &operator<<(raw_ostream &OS, const num_zeros_impl &NZI) {
348 for (size_t I = 0; I != NZI.N; ++I)
349 OS.write(0);
350 return OS;
351}
352
Benjamin Kramerf044d3f2015-03-09 16:23:46 +0000353static num_zeros_impl num_zeros(size_t N) {
David Majnemer2cbc1382014-09-16 03:52:46 +0000354 num_zeros_impl NZI(N);
355 return NZI;
356}
357
David Majnemerf69b05852014-11-14 08:15:42 +0000358template <typename T>
David Majnemer966064c2014-11-14 19:35:59 +0000359static uint32_t initializeOptionalHeader(COFFParser &CP, uint16_t Magic, T Header) {
David Majnemerf69b05852014-11-14 08:15:42 +0000360 memset(Header, 0, sizeof(*Header));
361 Header->Magic = Magic;
362 Header->SectionAlignment = CP.Obj.OptionalHeader->Header.SectionAlignment;
David Majnemer966064c2014-11-14 19:35:59 +0000363 Header->FileAlignment = CP.Obj.OptionalHeader->Header.FileAlignment;
David Majnemerf69b05852014-11-14 08:15:42 +0000364 uint32_t SizeOfCode = 0, SizeOfInitializedData = 0,
365 SizeOfUninitializedData = 0;
Rui Ueyamada00f2f2016-01-14 21:06:47 +0000366 uint32_t SizeOfHeaders = alignTo(CP.SectionTableStart + CP.SectionTableSize,
367 Header->FileAlignment);
368 uint32_t SizeOfImage = alignTo(SizeOfHeaders, Header->SectionAlignment);
David Majnemer966064c2014-11-14 19:35:59 +0000369 uint32_t BaseOfData = 0;
David Majnemerf69b05852014-11-14 08:15:42 +0000370 for (const COFFYAML::Section &S : CP.Obj.Sections) {
371 if (S.Header.Characteristics & COFF::IMAGE_SCN_CNT_CODE)
372 SizeOfCode += S.Header.SizeOfRawData;
373 if (S.Header.Characteristics & COFF::IMAGE_SCN_CNT_INITIALIZED_DATA)
374 SizeOfInitializedData += S.Header.SizeOfRawData;
375 if (S.Header.Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)
376 SizeOfUninitializedData += S.Header.SizeOfRawData;
377 if (S.Name.equals(".text"))
David Majnemer966064c2014-11-14 19:35:59 +0000378 Header->BaseOfCode = S.Header.VirtualAddress; // RVA
379 else if (S.Name.equals(".data"))
380 BaseOfData = S.Header.VirtualAddress; // RVA
David Majnemerf69b05852014-11-14 08:15:42 +0000381 if (S.Header.VirtualAddress)
Rui Ueyamada00f2f2016-01-14 21:06:47 +0000382 SizeOfImage += alignTo(S.Header.VirtualSize, Header->SectionAlignment);
David Majnemerf69b05852014-11-14 08:15:42 +0000383 }
384 Header->SizeOfCode = SizeOfCode;
385 Header->SizeOfInitializedData = SizeOfInitializedData;
386 Header->SizeOfUninitializedData = SizeOfUninitializedData;
387 Header->AddressOfEntryPoint =
388 CP.Obj.OptionalHeader->Header.AddressOfEntryPoint; // RVA
389 Header->ImageBase = CP.Obj.OptionalHeader->Header.ImageBase;
David Majnemerf69b05852014-11-14 08:15:42 +0000390 Header->MajorOperatingSystemVersion =
391 CP.Obj.OptionalHeader->Header.MajorOperatingSystemVersion;
392 Header->MinorOperatingSystemVersion =
393 CP.Obj.OptionalHeader->Header.MinorOperatingSystemVersion;
394 Header->MajorImageVersion =
395 CP.Obj.OptionalHeader->Header.MajorImageVersion;
396 Header->MinorImageVersion =
397 CP.Obj.OptionalHeader->Header.MinorImageVersion;
398 Header->MajorSubsystemVersion =
399 CP.Obj.OptionalHeader->Header.MajorSubsystemVersion;
400 Header->MinorSubsystemVersion =
401 CP.Obj.OptionalHeader->Header.MinorSubsystemVersion;
402 Header->SizeOfImage = SizeOfImage;
403 Header->SizeOfHeaders = SizeOfHeaders;
404 Header->Subsystem = CP.Obj.OptionalHeader->Header.Subsystem;
405 Header->DLLCharacteristics = CP.Obj.OptionalHeader->Header.DLLCharacteristics;
406 Header->SizeOfStackReserve = CP.Obj.OptionalHeader->Header.SizeOfStackReserve;
407 Header->SizeOfStackCommit = CP.Obj.OptionalHeader->Header.SizeOfStackCommit;
408 Header->SizeOfHeapReserve = CP.Obj.OptionalHeader->Header.SizeOfHeapReserve;
409 Header->SizeOfHeapCommit = CP.Obj.OptionalHeader->Header.SizeOfHeapCommit;
410 Header->NumberOfRvaAndSize = COFF::NUM_DATA_DIRECTORIES + 1;
David Majnemer966064c2014-11-14 19:35:59 +0000411 return BaseOfData;
David Majnemerf69b05852014-11-14 08:15:42 +0000412}
413
414static bool writeCOFF(COFFParser &CP, raw_ostream &OS) {
415 if (CP.isPE()) {
416 // PE files start with a DOS stub.
417 object::dos_header DH;
418 memset(&DH, 0, sizeof(DH));
419
420 // DOS EXEs start with "MZ" magic.
421 DH.Magic[0] = 'M';
422 DH.Magic[1] = 'Z';
423 // Initializing the AddressOfRelocationTable is strictly optional but
424 // mollifies certain tools which expect it to have a value greater than
425 // 0x40.
426 DH.AddressOfRelocationTable = sizeof(DH);
427 // This is the address of the PE signature.
David Majnemer646f47f2014-11-15 02:03:59 +0000428 DH.AddressOfNewExeHeader = DOSStubSize;
David Majnemerf69b05852014-11-14 08:15:42 +0000429
430 // Write out our DOS stub.
431 OS.write(reinterpret_cast<char *>(&DH), sizeof(DH));
432 // Write padding until we reach the position of where our PE signature
433 // should live.
David Majnemer646f47f2014-11-15 02:03:59 +0000434 OS << num_zeros(DOSStubSize - sizeof(DH));
David Majnemerf69b05852014-11-14 08:15:42 +0000435 // Write out the PE signature.
436 OS.write(COFF::PEMagic, sizeof(COFF::PEMagic));
437 }
David Majnemer2cbc1382014-09-16 03:52:46 +0000438 if (CP.useBigObj()) {
439 OS << binary_le(static_cast<uint16_t>(COFF::IMAGE_FILE_MACHINE_UNKNOWN))
440 << binary_le(static_cast<uint16_t>(0xffff))
441 << binary_le(static_cast<uint16_t>(COFF::BigObjHeader::MinBigObjectVersion))
442 << binary_le(CP.Obj.Header.Machine)
443 << binary_le(CP.Obj.Header.TimeDateStamp);
444 OS.write(COFF::BigObjMagic, sizeof(COFF::BigObjMagic));
445 OS << zeros(uint32_t(0))
446 << zeros(uint32_t(0))
447 << zeros(uint32_t(0))
448 << zeros(uint32_t(0))
449 << binary_le(CP.Obj.Header.NumberOfSections)
450 << binary_le(CP.Obj.Header.PointerToSymbolTable)
451 << binary_le(CP.Obj.Header.NumberOfSymbols);
452 } else {
453 OS << binary_le(CP.Obj.Header.Machine)
454 << binary_le(static_cast<int16_t>(CP.Obj.Header.NumberOfSections))
455 << binary_le(CP.Obj.Header.TimeDateStamp)
456 << binary_le(CP.Obj.Header.PointerToSymbolTable)
457 << binary_le(CP.Obj.Header.NumberOfSymbols)
458 << binary_le(CP.Obj.Header.SizeOfOptionalHeader)
459 << binary_le(CP.Obj.Header.Characteristics);
460 }
David Majnemerf69b05852014-11-14 08:15:42 +0000461 if (CP.isPE()) {
462 if (CP.is64Bit()) {
463 object::pe32plus_header PEH;
464 initializeOptionalHeader(CP, COFF::PE32Header::PE32_PLUS, &PEH);
465 OS.write(reinterpret_cast<char *>(&PEH), sizeof(PEH));
466 } else {
467 object::pe32_header PEH;
David Majnemer966064c2014-11-14 19:35:59 +0000468 uint32_t BaseOfData = initializeOptionalHeader(CP, COFF::PE32Header::PE32, &PEH);
469 PEH.BaseOfData = BaseOfData;
David Majnemerf69b05852014-11-14 08:15:42 +0000470 OS.write(reinterpret_cast<char *>(&PEH), sizeof(PEH));
471 }
472 for (const Optional<COFF::DataDirectory> &DD :
473 CP.Obj.OptionalHeader->DataDirectories) {
474 if (!DD.hasValue()) {
475 OS << zeros(uint32_t(0));
476 OS << zeros(uint32_t(0));
477 } else {
478 OS << binary_le(DD->RelativeVirtualAddress);
479 OS << binary_le(DD->Size);
480 }
481 }
482 OS << zeros(uint32_t(0));
483 OS << zeros(uint32_t(0));
484 }
Sean Silva3b76e402013-06-05 19:56:47 +0000485
David Majnemer646f47f2014-11-15 02:03:59 +0000486 assert(OS.tell() == CP.SectionTableStart);
Sean Silva3b76e402013-06-05 19:56:47 +0000487 // Output section table.
488 for (std::vector<COFFYAML::Section>::iterator i = CP.Obj.Sections.begin(),
489 e = CP.Obj.Sections.end();
490 i != e; ++i) {
491 OS.write(i->Header.Name, COFF::NameSize);
492 OS << binary_le(i->Header.VirtualSize)
493 << binary_le(i->Header.VirtualAddress)
494 << binary_le(i->Header.SizeOfRawData)
495 << binary_le(i->Header.PointerToRawData)
496 << binary_le(i->Header.PointerToRelocations)
497 << binary_le(i->Header.PointerToLineNumbers)
498 << binary_le(i->Header.NumberOfRelocations)
499 << binary_le(i->Header.NumberOfLineNumbers)
500 << binary_le(i->Header.Characteristics);
501 }
David Majnemer646f47f2014-11-15 02:03:59 +0000502 assert(OS.tell() == CP.SectionTableStart + CP.SectionTableSize);
Sean Silva3b76e402013-06-05 19:56:47 +0000503
Rafael Espindolae2e741e2013-06-06 13:06:17 +0000504 unsigned CurSymbol = 0;
505 StringMap<unsigned> SymbolTableIndexMap;
506 for (std::vector<COFFYAML::Symbol>::iterator I = CP.Obj.Symbols.begin(),
507 E = CP.Obj.Symbols.end();
508 I != E; ++I) {
509 SymbolTableIndexMap[I->Name] = CurSymbol;
510 CurSymbol += 1 + I->Header.NumberOfAuxSymbols;
511 }
512
Sean Silva3b76e402013-06-05 19:56:47 +0000513 // Output section data.
David Majnemerf69b05852014-11-14 08:15:42 +0000514 for (const COFFYAML::Section &S : CP.Obj.Sections) {
515 if (!S.Header.SizeOfRawData)
516 continue;
David Majnemer646f47f2014-11-15 02:03:59 +0000517 assert(S.Header.PointerToRawData >= OS.tell());
David Majnemerf69b05852014-11-14 08:15:42 +0000518 OS << num_zeros(S.Header.PointerToRawData - OS.tell());
519 S.SectionData.writeAsBinary(OS);
David Majnemer646f47f2014-11-15 02:03:59 +0000520 assert(S.Header.SizeOfRawData >= S.SectionData.binary_size());
David Majnemerf69b05852014-11-14 08:15:42 +0000521 OS << num_zeros(S.Header.SizeOfRawData - S.SectionData.binary_size());
522 for (const COFFYAML::Relocation &R : S.Relocations) {
Rafael Espindolae2e741e2013-06-06 13:06:17 +0000523 uint32_t SymbolTableIndex = SymbolTableIndexMap[R.SymbolName];
Sean Silva3b76e402013-06-05 19:56:47 +0000524 OS << binary_le(R.VirtualAddress)
Rafael Espindolae2e741e2013-06-06 13:06:17 +0000525 << binary_le(SymbolTableIndex)
Sean Silva3b76e402013-06-05 19:56:47 +0000526 << binary_le(R.Type);
527 }
528 }
529
530 // Output symbol table.
531
532 for (std::vector<COFFYAML::Symbol>::const_iterator i = CP.Obj.Symbols.begin(),
533 e = CP.Obj.Symbols.end();
534 i != e; ++i) {
535 OS.write(i->Header.Name, COFF::NameSize);
David Majnemer2cbc1382014-09-16 03:52:46 +0000536 OS << binary_le(i->Header.Value);
537 if (CP.useBigObj())
538 OS << binary_le(i->Header.SectionNumber);
539 else
540 OS << binary_le(static_cast<int16_t>(i->Header.SectionNumber));
541 OS << binary_le(i->Header.Type)
Sean Silva3b76e402013-06-05 19:56:47 +0000542 << binary_le(i->Header.StorageClass)
543 << binary_le(i->Header.NumberOfAuxSymbols);
David Majnemerddf28f22014-03-19 04:47:47 +0000544
545 if (i->FunctionDefinition)
546 OS << binary_le(i->FunctionDefinition->TagIndex)
547 << binary_le(i->FunctionDefinition->TotalSize)
548 << binary_le(i->FunctionDefinition->PointerToLinenumber)
549 << binary_le(i->FunctionDefinition->PointerToNextFunction)
David Majnemer2cbc1382014-09-16 03:52:46 +0000550 << zeros(i->FunctionDefinition->unused)
551 << num_zeros(CP.getSymbolSize() - COFF::Symbol16Size);
David Majnemerddf28f22014-03-19 04:47:47 +0000552 if (i->bfAndefSymbol)
553 OS << zeros(i->bfAndefSymbol->unused1)
554 << binary_le(i->bfAndefSymbol->Linenumber)
555 << zeros(i->bfAndefSymbol->unused2)
556 << binary_le(i->bfAndefSymbol->PointerToNextFunction)
David Majnemer2cbc1382014-09-16 03:52:46 +0000557 << zeros(i->bfAndefSymbol->unused3)
558 << num_zeros(CP.getSymbolSize() - COFF::Symbol16Size);
David Majnemerddf28f22014-03-19 04:47:47 +0000559 if (i->WeakExternal)
560 OS << binary_le(i->WeakExternal->TagIndex)
561 << binary_le(i->WeakExternal->Characteristics)
David Majnemer2cbc1382014-09-16 03:52:46 +0000562 << zeros(i->WeakExternal->unused)
563 << num_zeros(CP.getSymbolSize() - COFF::Symbol16Size);
David Majnemerddf28f22014-03-19 04:47:47 +0000564 if (!i->File.empty()) {
David Majnemer2cbc1382014-09-16 03:52:46 +0000565 unsigned SymbolSize = CP.getSymbolSize();
David Majnemerddf28f22014-03-19 04:47:47 +0000566 uint32_t NumberOfAuxRecords =
David Majnemer2cbc1382014-09-16 03:52:46 +0000567 (i->File.size() + SymbolSize - 1) / SymbolSize;
568 uint32_t NumberOfAuxBytes = NumberOfAuxRecords * SymbolSize;
David Majnemerddf28f22014-03-19 04:47:47 +0000569 uint32_t NumZeros = NumberOfAuxBytes - i->File.size();
570 OS.write(i->File.data(), i->File.size());
David Majnemer2cbc1382014-09-16 03:52:46 +0000571 OS << num_zeros(NumZeros);
David Majnemerddf28f22014-03-19 04:47:47 +0000572 }
573 if (i->SectionDefinition)
574 OS << binary_le(i->SectionDefinition->Length)
575 << binary_le(i->SectionDefinition->NumberOfRelocations)
576 << binary_le(i->SectionDefinition->NumberOfLinenumbers)
577 << binary_le(i->SectionDefinition->CheckSum)
David Majnemer4d571592014-09-15 19:42:42 +0000578 << binary_le(static_cast<int16_t>(i->SectionDefinition->Number))
David Majnemerddf28f22014-03-19 04:47:47 +0000579 << binary_le(i->SectionDefinition->Selection)
David Majnemer4d571592014-09-15 19:42:42 +0000580 << zeros(i->SectionDefinition->unused)
David Majnemer2cbc1382014-09-16 03:52:46 +0000581 << binary_le(static_cast<int16_t>(i->SectionDefinition->Number >> 16))
582 << num_zeros(CP.getSymbolSize() - COFF::Symbol16Size);
David Majnemerddf28f22014-03-19 04:47:47 +0000583 if (i->CLRToken)
584 OS << binary_le(i->CLRToken->AuxType)
585 << zeros(i->CLRToken->unused1)
586 << binary_le(i->CLRToken->SymbolTableIndex)
David Majnemer2cbc1382014-09-16 03:52:46 +0000587 << zeros(i->CLRToken->unused2)
588 << num_zeros(CP.getSymbolSize() - COFF::Symbol16Size);
Sean Silva3b76e402013-06-05 19:56:47 +0000589 }
590
591 // Output string table.
David Majnemerf69b05852014-11-14 08:15:42 +0000592 if (CP.Obj.Header.PointerToSymbolTable)
593 OS.write(&CP.StringTable[0], CP.StringTable.size());
Sean Silva3b76e402013-06-05 19:56:47 +0000594 return true;
595}
596
Chris Bieneman8ff0c112016-06-27 19:53:53 +0000597int yaml2coff(llvm::COFFYAML::Object &Doc, raw_ostream &Out) {
Sean Silva3b76e402013-06-05 19:56:47 +0000598 COFFParser CP(Doc);
599 if (!CP.parse()) {
600 errs() << "yaml2obj: Failed to parse YAML file!\n";
601 return 1;
602 }
603
David Majnemerf69b05852014-11-14 08:15:42 +0000604 if (!layoutOptionalHeader(CP)) {
605 errs() << "yaml2obj: Failed to layout optional header for COFF file!\n";
606 return 1;
607 }
Zachary Turnera8cfc292017-06-14 15:59:27 +0000608
Sean Silva3b76e402013-06-05 19:56:47 +0000609 if (!layoutCOFF(CP)) {
610 errs() << "yaml2obj: Failed to layout COFF file!\n";
611 return 1;
612 }
613 if (!writeCOFF(CP, Out)) {
614 errs() << "yaml2obj: Failed to write COFF file!\n";
615 return 1;
616 }
617 return 0;
618}