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