1 //===- llvm/MC/WinCOFFObjectWriter.cpp ------------------------------------===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file contains an implementation of a Win32 COFF object file writer. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/ADT/DenseMap.h" 14 #include "llvm/ADT/DenseSet.h" 15 #include "llvm/ADT/STLExtras.h" 16 #include "llvm/ADT/SmallString.h" 17 #include "llvm/ADT/SmallVector.h" 18 #include "llvm/ADT/StringRef.h" 19 #include "llvm/ADT/Twine.h" 20 #include "llvm/BinaryFormat/COFF.h" 21 #include "llvm/MC/MCAsmLayout.h" 22 #include "llvm/MC/MCAssembler.h" 23 #include "llvm/MC/MCContext.h" 24 #include "llvm/MC/MCExpr.h" 25 #include "llvm/MC/MCFixup.h" 26 #include "llvm/MC/MCFragment.h" 27 #include "llvm/MC/MCObjectWriter.h" 28 #include "llvm/MC/MCSection.h" 29 #include "llvm/MC/MCSectionCOFF.h" 30 #include "llvm/MC/MCSymbol.h" 31 #include "llvm/MC/MCSymbolCOFF.h" 32 #include "llvm/MC/MCValue.h" 33 #include "llvm/MC/MCWinCOFFObjectWriter.h" 34 #include "llvm/MC/StringTableBuilder.h" 35 #include "llvm/Support/CRC.h" 36 #include "llvm/Support/Casting.h" 37 #include "llvm/Support/EndianStream.h" 38 #include "llvm/Support/ErrorHandling.h" 39 #include "llvm/Support/LEB128.h" 40 #include "llvm/Support/MathExtras.h" 41 #include "llvm/Support/raw_ostream.h" 42 #include <algorithm> 43 #include <cassert> 44 #include <cstddef> 45 #include <cstdint> 46 #include <cstring> 47 #include <ctime> 48 #include <memory> 49 #include <string> 50 #include <vector> 51 52 using namespace llvm; 53 using llvm::support::endian::write32le; 54 55 #define DEBUG_TYPE "WinCOFFObjectWriter" 56 57 namespace { 58 59 using name = SmallString<COFF::NameSize>; 60 61 enum AuxiliaryType { 62 ATWeakExternal, 63 ATFile, 64 ATSectionDefinition 65 }; 66 67 struct AuxSymbol { 68 AuxiliaryType AuxType; 69 COFF::Auxiliary Aux; 70 }; 71 72 class COFFSection; 73 74 class COFFSymbol { 75 public: 76 COFF::symbol Data = {}; 77 78 using AuxiliarySymbols = SmallVector<AuxSymbol, 1>; 79 80 name Name; 81 int Index; 82 AuxiliarySymbols Aux; 83 COFFSymbol *Other = nullptr; 84 COFFSection *Section = nullptr; 85 int Relocations = 0; 86 const MCSymbol *MC = nullptr; 87 88 COFFSymbol(StringRef Name) : Name(Name) {} 89 90 void set_name_offset(uint32_t Offset); 91 92 int64_t getIndex() const { return Index; } 93 void setIndex(int Value) { 94 Index = Value; 95 if (MC) 96 MC->setIndex(static_cast<uint32_t>(Value)); 97 } 98 }; 99 100 // This class contains staging data for a COFF relocation entry. 101 struct COFFRelocation { 102 COFF::relocation Data; 103 COFFSymbol *Symb = nullptr; 104 105 COFFRelocation() = default; 106 107 static size_t size() { return COFF::RelocationSize; } 108 }; 109 110 using relocations = std::vector<COFFRelocation>; 111 112 class COFFSection { 113 public: 114 COFF::section Header = {}; 115 116 std::string Name; 117 int Number; 118 MCSectionCOFF const *MCSection = nullptr; 119 COFFSymbol *Symbol = nullptr; 120 relocations Relocations; 121 122 COFFSection(StringRef Name) : Name(std::string(Name)) {} 123 }; 124 125 class WinCOFFObjectWriter : public MCObjectWriter { 126 public: 127 support::endian::Writer W; 128 129 using symbols = std::vector<std::unique_ptr<COFFSymbol>>; 130 using sections = std::vector<std::unique_ptr<COFFSection>>; 131 132 using symbol_map = DenseMap<MCSymbol const *, COFFSymbol *>; 133 using section_map = DenseMap<MCSection const *, COFFSection *>; 134 135 using symbol_list = DenseSet<COFFSymbol *>; 136 137 std::unique_ptr<MCWinCOFFObjectTargetWriter> TargetObjectWriter; 138 139 // Root level file contents. 140 COFF::header Header = {}; 141 sections Sections; 142 symbols Symbols; 143 StringTableBuilder Strings{StringTableBuilder::WinCOFF}; 144 145 // Maps used during object file creation. 146 section_map SectionMap; 147 symbol_map SymbolMap; 148 149 symbol_list WeakDefaults; 150 151 bool UseBigObj; 152 153 bool EmitAddrsigSection = false; 154 MCSectionCOFF *AddrsigSection; 155 std::vector<const MCSymbol *> AddrsigSyms; 156 157 WinCOFFObjectWriter(std::unique_ptr<MCWinCOFFObjectTargetWriter> MOTW, 158 raw_pwrite_stream &OS); 159 160 void reset() override { 161 memset(&Header, 0, sizeof(Header)); 162 Header.Machine = TargetObjectWriter->getMachine(); 163 Sections.clear(); 164 Symbols.clear(); 165 Strings.clear(); 166 SectionMap.clear(); 167 SymbolMap.clear(); 168 MCObjectWriter::reset(); 169 } 170 171 COFFSymbol *createSymbol(StringRef Name); 172 COFFSymbol *GetOrCreateCOFFSymbol(const MCSymbol *Symbol); 173 COFFSection *createSection(StringRef Name); 174 175 void defineSection(MCSectionCOFF const &Sec); 176 177 COFFSymbol *getLinkedSymbol(const MCSymbol &Symbol); 178 void DefineSymbol(const MCSymbol &Symbol, MCAssembler &Assembler, 179 const MCAsmLayout &Layout); 180 181 void SetSymbolName(COFFSymbol &S); 182 void SetSectionName(COFFSection &S); 183 184 bool IsPhysicalSection(COFFSection *S); 185 186 // Entity writing methods. 187 188 void WriteFileHeader(const COFF::header &Header); 189 void WriteSymbol(const COFFSymbol &S); 190 void WriteAuxiliarySymbols(const COFFSymbol::AuxiliarySymbols &S); 191 void writeSectionHeaders(); 192 void WriteRelocation(const COFF::relocation &R); 193 uint32_t writeSectionContents(MCAssembler &Asm, const MCAsmLayout &Layout, 194 const MCSection &MCSec); 195 void writeSection(MCAssembler &Asm, const MCAsmLayout &Layout, 196 const COFFSection &Sec, const MCSection &MCSec); 197 198 // MCObjectWriter interface implementation. 199 200 void executePostLayoutBinding(MCAssembler &Asm, 201 const MCAsmLayout &Layout) override; 202 203 bool isSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm, 204 const MCSymbol &SymA, 205 const MCFragment &FB, bool InSet, 206 bool IsPCRel) const override; 207 208 void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout, 209 const MCFragment *Fragment, const MCFixup &Fixup, 210 MCValue Target, uint64_t &FixedValue) override; 211 212 void createFileSymbols(MCAssembler &Asm); 213 void setWeakDefaultNames(); 214 void assignSectionNumbers(); 215 void assignFileOffsets(MCAssembler &Asm, const MCAsmLayout &Layout); 216 217 void emitAddrsigSection() override { EmitAddrsigSection = true; } 218 void addAddrsigSymbol(const MCSymbol *Sym) override { 219 AddrsigSyms.push_back(Sym); 220 } 221 222 uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override; 223 }; 224 225 } // end anonymous namespace 226 227 //------------------------------------------------------------------------------ 228 // Symbol class implementation 229 230 // In the case that the name does not fit within 8 bytes, the offset 231 // into the string table is stored in the last 4 bytes instead, leaving 232 // the first 4 bytes as 0. 233 void COFFSymbol::set_name_offset(uint32_t Offset) { 234 write32le(Data.Name + 0, 0); 235 write32le(Data.Name + 4, Offset); 236 } 237 238 //------------------------------------------------------------------------------ 239 // WinCOFFObjectWriter class implementation 240 241 WinCOFFObjectWriter::WinCOFFObjectWriter( 242 std::unique_ptr<MCWinCOFFObjectTargetWriter> MOTW, raw_pwrite_stream &OS) 243 : W(OS, support::little), TargetObjectWriter(std::move(MOTW)) { 244 Header.Machine = TargetObjectWriter->getMachine(); 245 } 246 247 COFFSymbol *WinCOFFObjectWriter::createSymbol(StringRef Name) { 248 Symbols.push_back(std::make_unique<COFFSymbol>(Name)); 249 return Symbols.back().get(); 250 } 251 252 COFFSymbol *WinCOFFObjectWriter::GetOrCreateCOFFSymbol(const MCSymbol *Symbol) { 253 COFFSymbol *&Ret = SymbolMap[Symbol]; 254 if (!Ret) 255 Ret = createSymbol(Symbol->getName()); 256 return Ret; 257 } 258 259 COFFSection *WinCOFFObjectWriter::createSection(StringRef Name) { 260 Sections.emplace_back(std::make_unique<COFFSection>(Name)); 261 return Sections.back().get(); 262 } 263 264 static uint32_t getAlignment(const MCSectionCOFF &Sec) { 265 switch (Sec.getAlignment()) { 266 case 1: 267 return COFF::IMAGE_SCN_ALIGN_1BYTES; 268 case 2: 269 return COFF::IMAGE_SCN_ALIGN_2BYTES; 270 case 4: 271 return COFF::IMAGE_SCN_ALIGN_4BYTES; 272 case 8: 273 return COFF::IMAGE_SCN_ALIGN_8BYTES; 274 case 16: 275 return COFF::IMAGE_SCN_ALIGN_16BYTES; 276 case 32: 277 return COFF::IMAGE_SCN_ALIGN_32BYTES; 278 case 64: 279 return COFF::IMAGE_SCN_ALIGN_64BYTES; 280 case 128: 281 return COFF::IMAGE_SCN_ALIGN_128BYTES; 282 case 256: 283 return COFF::IMAGE_SCN_ALIGN_256BYTES; 284 case 512: 285 return COFF::IMAGE_SCN_ALIGN_512BYTES; 286 case 1024: 287 return COFF::IMAGE_SCN_ALIGN_1024BYTES; 288 case 2048: 289 return COFF::IMAGE_SCN_ALIGN_2048BYTES; 290 case 4096: 291 return COFF::IMAGE_SCN_ALIGN_4096BYTES; 292 case 8192: 293 return COFF::IMAGE_SCN_ALIGN_8192BYTES; 294 } 295 llvm_unreachable("unsupported section alignment"); 296 } 297 298 /// This function takes a section data object from the assembler 299 /// and creates the associated COFF section staging object. 300 void WinCOFFObjectWriter::defineSection(const MCSectionCOFF &MCSec) { 301 COFFSection *Section = createSection(MCSec.getName()); 302 COFFSymbol *Symbol = createSymbol(MCSec.getName()); 303 Section->Symbol = Symbol; 304 Symbol->Section = Section; 305 Symbol->Data.StorageClass = COFF::IMAGE_SYM_CLASS_STATIC; 306 307 // Create a COMDAT symbol if needed. 308 if (MCSec.getSelection() != COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) { 309 if (const MCSymbol *S = MCSec.getCOMDATSymbol()) { 310 COFFSymbol *COMDATSymbol = GetOrCreateCOFFSymbol(S); 311 if (COMDATSymbol->Section) 312 report_fatal_error("two sections have the same comdat"); 313 COMDATSymbol->Section = Section; 314 } 315 } 316 317 // In this case the auxiliary symbol is a Section Definition. 318 Symbol->Aux.resize(1); 319 Symbol->Aux[0] = {}; 320 Symbol->Aux[0].AuxType = ATSectionDefinition; 321 Symbol->Aux[0].Aux.SectionDefinition.Selection = MCSec.getSelection(); 322 323 // Set section alignment. 324 Section->Header.Characteristics = MCSec.getCharacteristics(); 325 Section->Header.Characteristics |= getAlignment(MCSec); 326 327 // Bind internal COFF section to MC section. 328 Section->MCSection = &MCSec; 329 SectionMap[&MCSec] = Section; 330 } 331 332 static uint64_t getSymbolValue(const MCSymbol &Symbol, 333 const MCAsmLayout &Layout) { 334 if (Symbol.isCommon() && Symbol.isExternal()) 335 return Symbol.getCommonSize(); 336 337 uint64_t Res; 338 if (!Layout.getSymbolOffset(Symbol, Res)) 339 return 0; 340 341 return Res; 342 } 343 344 COFFSymbol *WinCOFFObjectWriter::getLinkedSymbol(const MCSymbol &Symbol) { 345 if (!Symbol.isVariable()) 346 return nullptr; 347 348 const MCSymbolRefExpr *SymRef = 349 dyn_cast<MCSymbolRefExpr>(Symbol.getVariableValue()); 350 if (!SymRef) 351 return nullptr; 352 353 const MCSymbol &Aliasee = SymRef->getSymbol(); 354 if (!Aliasee.isUndefined()) 355 return nullptr; 356 return GetOrCreateCOFFSymbol(&Aliasee); 357 } 358 359 /// This function takes a symbol data object from the assembler 360 /// and creates the associated COFF symbol staging object. 361 void WinCOFFObjectWriter::DefineSymbol(const MCSymbol &MCSym, 362 MCAssembler &Assembler, 363 const MCAsmLayout &Layout) { 364 COFFSymbol *Sym = GetOrCreateCOFFSymbol(&MCSym); 365 const MCSymbol *Base = Layout.getBaseSymbol(MCSym); 366 COFFSection *Sec = nullptr; 367 if (Base && Base->getFragment()) { 368 Sec = SectionMap[Base->getFragment()->getParent()]; 369 if (Sym->Section && Sym->Section != Sec) 370 report_fatal_error("conflicting sections for symbol"); 371 } 372 373 COFFSymbol *Local = nullptr; 374 if (cast<MCSymbolCOFF>(MCSym).isWeakExternal()) { 375 Sym->Data.StorageClass = COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL; 376 377 COFFSymbol *WeakDefault = getLinkedSymbol(MCSym); 378 if (!WeakDefault) { 379 std::string WeakName = (".weak." + MCSym.getName() + ".default").str(); 380 WeakDefault = createSymbol(WeakName); 381 if (!Sec) 382 WeakDefault->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE; 383 else 384 WeakDefault->Section = Sec; 385 WeakDefaults.insert(WeakDefault); 386 Local = WeakDefault; 387 } 388 389 Sym->Other = WeakDefault; 390 391 // Setup the Weak External auxiliary symbol. 392 Sym->Aux.resize(1); 393 memset(&Sym->Aux[0], 0, sizeof(Sym->Aux[0])); 394 Sym->Aux[0].AuxType = ATWeakExternal; 395 Sym->Aux[0].Aux.WeakExternal.TagIndex = 0; 396 Sym->Aux[0].Aux.WeakExternal.Characteristics = 397 COFF::IMAGE_WEAK_EXTERN_SEARCH_ALIAS; 398 } else { 399 if (!Base) 400 Sym->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE; 401 else 402 Sym->Section = Sec; 403 Local = Sym; 404 } 405 406 if (Local) { 407 Local->Data.Value = getSymbolValue(MCSym, Layout); 408 409 const MCSymbolCOFF &SymbolCOFF = cast<MCSymbolCOFF>(MCSym); 410 Local->Data.Type = SymbolCOFF.getType(); 411 Local->Data.StorageClass = SymbolCOFF.getClass(); 412 413 // If no storage class was specified in the streamer, define it here. 414 if (Local->Data.StorageClass == COFF::IMAGE_SYM_CLASS_NULL) { 415 bool IsExternal = MCSym.isExternal() || 416 (!MCSym.getFragment() && !MCSym.isVariable()); 417 418 Local->Data.StorageClass = IsExternal ? COFF::IMAGE_SYM_CLASS_EXTERNAL 419 : COFF::IMAGE_SYM_CLASS_STATIC; 420 } 421 } 422 423 Sym->MC = &MCSym; 424 } 425 426 // Maximum offsets for different string table entry encodings. 427 enum : unsigned { Max7DecimalOffset = 9999999U }; 428 enum : uint64_t { MaxBase64Offset = 0xFFFFFFFFFULL }; // 64^6, including 0 429 430 // Encode a string table entry offset in base 64, padded to 6 chars, and 431 // prefixed with a double slash: '//AAAAAA', '//AAAAAB', ... 432 // Buffer must be at least 8 bytes large. No terminating null appended. 433 static void encodeBase64StringEntry(char *Buffer, uint64_t Value) { 434 assert(Value > Max7DecimalOffset && Value <= MaxBase64Offset && 435 "Illegal section name encoding for value"); 436 437 static const char Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 438 "abcdefghijklmnopqrstuvwxyz" 439 "0123456789+/"; 440 441 Buffer[0] = '/'; 442 Buffer[1] = '/'; 443 444 char *Ptr = Buffer + 7; 445 for (unsigned i = 0; i < 6; ++i) { 446 unsigned Rem = Value % 64; 447 Value /= 64; 448 *(Ptr--) = Alphabet[Rem]; 449 } 450 } 451 452 void WinCOFFObjectWriter::SetSectionName(COFFSection &S) { 453 if (S.Name.size() <= COFF::NameSize) { 454 std::memcpy(S.Header.Name, S.Name.c_str(), S.Name.size()); 455 return; 456 } 457 458 uint64_t StringTableEntry = Strings.getOffset(S.Name); 459 if (StringTableEntry <= Max7DecimalOffset) { 460 SmallVector<char, COFF::NameSize> Buffer; 461 Twine('/').concat(Twine(StringTableEntry)).toVector(Buffer); 462 assert(Buffer.size() <= COFF::NameSize && Buffer.size() >= 2); 463 std::memcpy(S.Header.Name, Buffer.data(), Buffer.size()); 464 return; 465 } 466 if (StringTableEntry <= MaxBase64Offset) { 467 // Starting with 10,000,000, offsets are encoded as base64. 468 encodeBase64StringEntry(S.Header.Name, StringTableEntry); 469 return; 470 } 471 report_fatal_error("COFF string table is greater than 64 GB."); 472 } 473 474 void WinCOFFObjectWriter::SetSymbolName(COFFSymbol &S) { 475 if (S.Name.size() > COFF::NameSize) 476 S.set_name_offset(Strings.getOffset(S.Name)); 477 else 478 std::memcpy(S.Data.Name, S.Name.c_str(), S.Name.size()); 479 } 480 481 bool WinCOFFObjectWriter::IsPhysicalSection(COFFSection *S) { 482 return (S->Header.Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 483 0; 484 } 485 486 //------------------------------------------------------------------------------ 487 // entity writing methods 488 489 void WinCOFFObjectWriter::WriteFileHeader(const COFF::header &Header) { 490 if (UseBigObj) { 491 W.write<uint16_t>(COFF::IMAGE_FILE_MACHINE_UNKNOWN); 492 W.write<uint16_t>(0xFFFF); 493 W.write<uint16_t>(COFF::BigObjHeader::MinBigObjectVersion); 494 W.write<uint16_t>(Header.Machine); 495 W.write<uint32_t>(Header.TimeDateStamp); 496 W.OS.write(COFF::BigObjMagic, sizeof(COFF::BigObjMagic)); 497 W.write<uint32_t>(0); 498 W.write<uint32_t>(0); 499 W.write<uint32_t>(0); 500 W.write<uint32_t>(0); 501 W.write<uint32_t>(Header.NumberOfSections); 502 W.write<uint32_t>(Header.PointerToSymbolTable); 503 W.write<uint32_t>(Header.NumberOfSymbols); 504 } else { 505 W.write<uint16_t>(Header.Machine); 506 W.write<uint16_t>(static_cast<int16_t>(Header.NumberOfSections)); 507 W.write<uint32_t>(Header.TimeDateStamp); 508 W.write<uint32_t>(Header.PointerToSymbolTable); 509 W.write<uint32_t>(Header.NumberOfSymbols); 510 W.write<uint16_t>(Header.SizeOfOptionalHeader); 511 W.write<uint16_t>(Header.Characteristics); 512 } 513 } 514 515 void WinCOFFObjectWriter::WriteSymbol(const COFFSymbol &S) { 516 W.OS.write(S.Data.Name, COFF::NameSize); 517 W.write<uint32_t>(S.Data.Value); 518 if (UseBigObj) 519 W.write<uint32_t>(S.Data.SectionNumber); 520 else 521 W.write<uint16_t>(static_cast<int16_t>(S.Data.SectionNumber)); 522 W.write<uint16_t>(S.Data.Type); 523 W.OS << char(S.Data.StorageClass); 524 W.OS << char(S.Data.NumberOfAuxSymbols); 525 WriteAuxiliarySymbols(S.Aux); 526 } 527 528 void WinCOFFObjectWriter::WriteAuxiliarySymbols( 529 const COFFSymbol::AuxiliarySymbols &S) { 530 for (const AuxSymbol &i : S) { 531 switch (i.AuxType) { 532 case ATWeakExternal: 533 W.write<uint32_t>(i.Aux.WeakExternal.TagIndex); 534 W.write<uint32_t>(i.Aux.WeakExternal.Characteristics); 535 W.OS.write_zeros(sizeof(i.Aux.WeakExternal.unused)); 536 if (UseBigObj) 537 W.OS.write_zeros(COFF::Symbol32Size - COFF::Symbol16Size); 538 break; 539 case ATFile: 540 W.OS.write(reinterpret_cast<const char *>(&i.Aux), 541 UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size); 542 break; 543 case ATSectionDefinition: 544 W.write<uint32_t>(i.Aux.SectionDefinition.Length); 545 W.write<uint16_t>(i.Aux.SectionDefinition.NumberOfRelocations); 546 W.write<uint16_t>(i.Aux.SectionDefinition.NumberOfLinenumbers); 547 W.write<uint32_t>(i.Aux.SectionDefinition.CheckSum); 548 W.write<uint16_t>(static_cast<int16_t>(i.Aux.SectionDefinition.Number)); 549 W.OS << char(i.Aux.SectionDefinition.Selection); 550 W.OS.write_zeros(sizeof(i.Aux.SectionDefinition.unused)); 551 W.write<uint16_t>(static_cast<int16_t>(i.Aux.SectionDefinition.Number >> 16)); 552 if (UseBigObj) 553 W.OS.write_zeros(COFF::Symbol32Size - COFF::Symbol16Size); 554 break; 555 } 556 } 557 } 558 559 // Write the section header. 560 void WinCOFFObjectWriter::writeSectionHeaders() { 561 // Section numbers must be monotonically increasing in the section 562 // header, but our Sections array is not sorted by section number, 563 // so make a copy of Sections and sort it. 564 std::vector<COFFSection *> Arr; 565 for (auto &Section : Sections) 566 Arr.push_back(Section.get()); 567 llvm::sort(Arr, [](const COFFSection *A, const COFFSection *B) { 568 return A->Number < B->Number; 569 }); 570 571 for (auto &Section : Arr) { 572 if (Section->Number == -1) 573 continue; 574 575 COFF::section &S = Section->Header; 576 if (Section->Relocations.size() >= 0xffff) 577 S.Characteristics |= COFF::IMAGE_SCN_LNK_NRELOC_OVFL; 578 W.OS.write(S.Name, COFF::NameSize); 579 W.write<uint32_t>(S.VirtualSize); 580 W.write<uint32_t>(S.VirtualAddress); 581 W.write<uint32_t>(S.SizeOfRawData); 582 W.write<uint32_t>(S.PointerToRawData); 583 W.write<uint32_t>(S.PointerToRelocations); 584 W.write<uint32_t>(S.PointerToLineNumbers); 585 W.write<uint16_t>(S.NumberOfRelocations); 586 W.write<uint16_t>(S.NumberOfLineNumbers); 587 W.write<uint32_t>(S.Characteristics); 588 } 589 } 590 591 void WinCOFFObjectWriter::WriteRelocation(const COFF::relocation &R) { 592 W.write<uint32_t>(R.VirtualAddress); 593 W.write<uint32_t>(R.SymbolTableIndex); 594 W.write<uint16_t>(R.Type); 595 } 596 597 // Write MCSec's contents. What this function does is essentially 598 // "Asm.writeSectionData(&MCSec, Layout)", but it's a bit complicated 599 // because it needs to compute a CRC. 600 uint32_t WinCOFFObjectWriter::writeSectionContents(MCAssembler &Asm, 601 const MCAsmLayout &Layout, 602 const MCSection &MCSec) { 603 // Save the contents of the section to a temporary buffer, we need this 604 // to CRC the data before we dump it into the object file. 605 SmallVector<char, 128> Buf; 606 raw_svector_ostream VecOS(Buf); 607 Asm.writeSectionData(VecOS, &MCSec, Layout); 608 609 // Write the section contents to the object file. 610 W.OS << Buf; 611 612 // Calculate our CRC with an initial value of '0', this is not how 613 // JamCRC is specified but it aligns with the expected output. 614 JamCRC JC(/*Init=*/0); 615 JC.update(makeArrayRef(reinterpret_cast<uint8_t*>(Buf.data()), Buf.size())); 616 return JC.getCRC(); 617 } 618 619 void WinCOFFObjectWriter::writeSection(MCAssembler &Asm, 620 const MCAsmLayout &Layout, 621 const COFFSection &Sec, 622 const MCSection &MCSec) { 623 if (Sec.Number == -1) 624 return; 625 626 // Write the section contents. 627 if (Sec.Header.PointerToRawData != 0) { 628 assert(W.OS.tell() == Sec.Header.PointerToRawData && 629 "Section::PointerToRawData is insane!"); 630 631 uint32_t CRC = writeSectionContents(Asm, Layout, MCSec); 632 633 // Update the section definition auxiliary symbol to record the CRC. 634 COFFSection *Sec = SectionMap[&MCSec]; 635 COFFSymbol::AuxiliarySymbols &AuxSyms = Sec->Symbol->Aux; 636 assert(AuxSyms.size() == 1 && AuxSyms[0].AuxType == ATSectionDefinition); 637 AuxSymbol &SecDef = AuxSyms[0]; 638 SecDef.Aux.SectionDefinition.CheckSum = CRC; 639 } 640 641 // Write relocations for this section. 642 if (Sec.Relocations.empty()) { 643 assert(Sec.Header.PointerToRelocations == 0 && 644 "Section::PointerToRelocations is insane!"); 645 return; 646 } 647 648 assert(W.OS.tell() == Sec.Header.PointerToRelocations && 649 "Section::PointerToRelocations is insane!"); 650 651 if (Sec.Relocations.size() >= 0xffff) { 652 // In case of overflow, write actual relocation count as first 653 // relocation. Including the synthetic reloc itself (+ 1). 654 COFF::relocation R; 655 R.VirtualAddress = Sec.Relocations.size() + 1; 656 R.SymbolTableIndex = 0; 657 R.Type = 0; 658 WriteRelocation(R); 659 } 660 661 for (const auto &Relocation : Sec.Relocations) 662 WriteRelocation(Relocation.Data); 663 } 664 665 //////////////////////////////////////////////////////////////////////////////// 666 // MCObjectWriter interface implementations 667 668 void WinCOFFObjectWriter::executePostLayoutBinding(MCAssembler &Asm, 669 const MCAsmLayout &Layout) { 670 if (EmitAddrsigSection) { 671 AddrsigSection = Asm.getContext().getCOFFSection( 672 ".llvm_addrsig", COFF::IMAGE_SCN_LNK_REMOVE, 673 SectionKind::getMetadata()); 674 Asm.registerSection(*AddrsigSection); 675 } 676 677 // "Define" each section & symbol. This creates section & symbol 678 // entries in the staging area. 679 for (const auto &Section : Asm) 680 defineSection(static_cast<const MCSectionCOFF &>(Section)); 681 682 for (const MCSymbol &Symbol : Asm.symbols()) 683 if (!Symbol.isTemporary()) 684 DefineSymbol(Symbol, Asm, Layout); 685 } 686 687 bool WinCOFFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl( 688 const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB, 689 bool InSet, bool IsPCRel) const { 690 // Don't drop relocations between functions, even if they are in the same text 691 // section. Multiple Visual C++ linker features depend on having the 692 // relocations present. The /INCREMENTAL flag will cause these relocations to 693 // point to thunks, and the /GUARD:CF flag assumes that it can use relocations 694 // to approximate the set of all address taken functions. LLD's implementation 695 // of /GUARD:CF also relies on the existance of these relocations. 696 uint16_t Type = cast<MCSymbolCOFF>(SymA).getType(); 697 if ((Type >> COFF::SCT_COMPLEX_TYPE_SHIFT) == COFF::IMAGE_SYM_DTYPE_FUNCTION) 698 return false; 699 return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB, 700 InSet, IsPCRel); 701 } 702 703 void WinCOFFObjectWriter::recordRelocation(MCAssembler &Asm, 704 const MCAsmLayout &Layout, 705 const MCFragment *Fragment, 706 const MCFixup &Fixup, MCValue Target, 707 uint64_t &FixedValue) { 708 assert(Target.getSymA() && "Relocation must reference a symbol!"); 709 710 const MCSymbol &A = Target.getSymA()->getSymbol(); 711 if (!A.isRegistered()) { 712 Asm.getContext().reportError(Fixup.getLoc(), 713 Twine("symbol '") + A.getName() + 714 "' can not be undefined"); 715 return; 716 } 717 if (A.isTemporary() && A.isUndefined()) { 718 Asm.getContext().reportError(Fixup.getLoc(), 719 Twine("assembler label '") + A.getName() + 720 "' can not be undefined"); 721 return; 722 } 723 724 MCSection *MCSec = Fragment->getParent(); 725 726 // Mark this symbol as requiring an entry in the symbol table. 727 assert(SectionMap.find(MCSec) != SectionMap.end() && 728 "Section must already have been defined in executePostLayoutBinding!"); 729 730 COFFSection *Sec = SectionMap[MCSec]; 731 const MCSymbolRefExpr *SymB = Target.getSymB(); 732 733 if (SymB) { 734 const MCSymbol *B = &SymB->getSymbol(); 735 if (!B->getFragment()) { 736 Asm.getContext().reportError( 737 Fixup.getLoc(), 738 Twine("symbol '") + B->getName() + 739 "' can not be undefined in a subtraction expression"); 740 return; 741 } 742 743 // Offset of the symbol in the section 744 int64_t OffsetOfB = Layout.getSymbolOffset(*B); 745 746 // Offset of the relocation in the section 747 int64_t OffsetOfRelocation = 748 Layout.getFragmentOffset(Fragment) + Fixup.getOffset(); 749 750 FixedValue = (OffsetOfRelocation - OffsetOfB) + Target.getConstant(); 751 } else { 752 FixedValue = Target.getConstant(); 753 } 754 755 COFFRelocation Reloc; 756 757 Reloc.Data.SymbolTableIndex = 0; 758 Reloc.Data.VirtualAddress = Layout.getFragmentOffset(Fragment); 759 760 // Turn relocations for temporary symbols into section relocations. 761 if (A.isTemporary()) { 762 MCSection *TargetSection = &A.getSection(); 763 assert( 764 SectionMap.find(TargetSection) != SectionMap.end() && 765 "Section must already have been defined in executePostLayoutBinding!"); 766 Reloc.Symb = SectionMap[TargetSection]->Symbol; 767 FixedValue += Layout.getSymbolOffset(A); 768 } else { 769 assert( 770 SymbolMap.find(&A) != SymbolMap.end() && 771 "Symbol must already have been defined in executePostLayoutBinding!"); 772 Reloc.Symb = SymbolMap[&A]; 773 } 774 775 ++Reloc.Symb->Relocations; 776 777 Reloc.Data.VirtualAddress += Fixup.getOffset(); 778 Reloc.Data.Type = TargetObjectWriter->getRelocType( 779 Asm.getContext(), Target, Fixup, SymB, Asm.getBackend()); 780 781 // FIXME: Can anyone explain what this does other than adjust for the size 782 // of the offset? 783 if ((Header.Machine == COFF::IMAGE_FILE_MACHINE_AMD64 && 784 Reloc.Data.Type == COFF::IMAGE_REL_AMD64_REL32) || 785 (Header.Machine == COFF::IMAGE_FILE_MACHINE_I386 && 786 Reloc.Data.Type == COFF::IMAGE_REL_I386_REL32)) 787 FixedValue += 4; 788 789 if (Header.Machine == COFF::IMAGE_FILE_MACHINE_ARMNT) { 790 switch (Reloc.Data.Type) { 791 case COFF::IMAGE_REL_ARM_ABSOLUTE: 792 case COFF::IMAGE_REL_ARM_ADDR32: 793 case COFF::IMAGE_REL_ARM_ADDR32NB: 794 case COFF::IMAGE_REL_ARM_TOKEN: 795 case COFF::IMAGE_REL_ARM_SECTION: 796 case COFF::IMAGE_REL_ARM_SECREL: 797 break; 798 case COFF::IMAGE_REL_ARM_BRANCH11: 799 case COFF::IMAGE_REL_ARM_BLX11: 800 // IMAGE_REL_ARM_BRANCH11 and IMAGE_REL_ARM_BLX11 are only used for 801 // pre-ARMv7, which implicitly rules it out of ARMNT (it would be valid 802 // for Windows CE). 803 case COFF::IMAGE_REL_ARM_BRANCH24: 804 case COFF::IMAGE_REL_ARM_BLX24: 805 case COFF::IMAGE_REL_ARM_MOV32A: 806 // IMAGE_REL_ARM_BRANCH24, IMAGE_REL_ARM_BLX24, IMAGE_REL_ARM_MOV32A are 807 // only used for ARM mode code, which is documented as being unsupported 808 // by Windows on ARM. Empirical proof indicates that masm is able to 809 // generate the relocations however the rest of the MSVC toolchain is 810 // unable to handle it. 811 llvm_unreachable("unsupported relocation"); 812 break; 813 case COFF::IMAGE_REL_ARM_MOV32T: 814 break; 815 case COFF::IMAGE_REL_ARM_BRANCH20T: 816 case COFF::IMAGE_REL_ARM_BRANCH24T: 817 case COFF::IMAGE_REL_ARM_BLX23T: 818 // IMAGE_REL_BRANCH20T, IMAGE_REL_ARM_BRANCH24T, IMAGE_REL_ARM_BLX23T all 819 // perform a 4 byte adjustment to the relocation. Relative branches are 820 // offset by 4 on ARM, however, because there is no RELA relocations, all 821 // branches are offset by 4. 822 FixedValue = FixedValue + 4; 823 break; 824 } 825 } 826 827 // The fixed value never makes sense for section indices, ignore it. 828 if (Fixup.getKind() == FK_SecRel_2) 829 FixedValue = 0; 830 831 if (TargetObjectWriter->recordRelocation(Fixup)) 832 Sec->Relocations.push_back(Reloc); 833 } 834 835 static std::time_t getTime() { 836 std::time_t Now = time(nullptr); 837 if (Now < 0 || !isUInt<32>(Now)) 838 return UINT32_MAX; 839 return Now; 840 } 841 842 // Create .file symbols. 843 void WinCOFFObjectWriter::createFileSymbols(MCAssembler &Asm) { 844 for (const std::string &Name : Asm.getFileNames()) { 845 // round up to calculate the number of auxiliary symbols required 846 unsigned SymbolSize = UseBigObj ? COFF::Symbol32Size : COFF::Symbol16Size; 847 unsigned Count = (Name.size() + SymbolSize - 1) / SymbolSize; 848 849 COFFSymbol *File = createSymbol(".file"); 850 File->Data.SectionNumber = COFF::IMAGE_SYM_DEBUG; 851 File->Data.StorageClass = COFF::IMAGE_SYM_CLASS_FILE; 852 File->Aux.resize(Count); 853 854 unsigned Offset = 0; 855 unsigned Length = Name.size(); 856 for (auto &Aux : File->Aux) { 857 Aux.AuxType = ATFile; 858 859 if (Length > SymbolSize) { 860 memcpy(&Aux.Aux, Name.c_str() + Offset, SymbolSize); 861 Length = Length - SymbolSize; 862 } else { 863 memcpy(&Aux.Aux, Name.c_str() + Offset, Length); 864 memset((char *)&Aux.Aux + Length, 0, SymbolSize - Length); 865 break; 866 } 867 868 Offset += SymbolSize; 869 } 870 } 871 } 872 873 void WinCOFFObjectWriter::setWeakDefaultNames() { 874 if (WeakDefaults.empty()) 875 return; 876 877 // If multiple object files use a weak symbol (either with a regular 878 // defined default, or an absolute zero symbol as default), the defaults 879 // cause duplicate definitions unless their names are made unique. Look 880 // for a defined extern symbol, that isn't comdat - that should be unique 881 // unless there are other duplicate definitions. And if none is found, 882 // allow picking a comdat symbol, as that's still better than nothing. 883 884 COFFSymbol *Unique = nullptr; 885 for (bool AllowComdat : {false, true}) { 886 for (auto &Sym : Symbols) { 887 // Don't include the names of the defaults themselves 888 if (WeakDefaults.count(Sym.get())) 889 continue; 890 // Only consider external symbols 891 if (Sym->Data.StorageClass != COFF::IMAGE_SYM_CLASS_EXTERNAL) 892 continue; 893 // Only consider symbols defined in a section or that are absolute 894 if (!Sym->Section && Sym->Data.SectionNumber != COFF::IMAGE_SYM_ABSOLUTE) 895 continue; 896 if (!AllowComdat && Sym->Section && 897 Sym->Section->Header.Characteristics & COFF::IMAGE_SCN_LNK_COMDAT) 898 continue; 899 Unique = Sym.get(); 900 break; 901 } 902 if (Unique) 903 break; 904 } 905 // If we didn't find any unique symbol to use for the names, just skip this. 906 if (!Unique) 907 return; 908 for (auto *Sym : WeakDefaults) { 909 Sym->Name.append("."); 910 Sym->Name.append(Unique->Name); 911 } 912 } 913 914 static bool isAssociative(const COFFSection &Section) { 915 return Section.Symbol->Aux[0].Aux.SectionDefinition.Selection == 916 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE; 917 } 918 919 void WinCOFFObjectWriter::assignSectionNumbers() { 920 size_t I = 1; 921 auto Assign = [&](COFFSection &Section) { 922 Section.Number = I; 923 Section.Symbol->Data.SectionNumber = I; 924 Section.Symbol->Aux[0].Aux.SectionDefinition.Number = I; 925 ++I; 926 }; 927 928 // Although it is not explicitly requested by the Microsoft COFF spec, 929 // we should avoid emitting forward associative section references, 930 // because MSVC link.exe as of 2017 cannot handle that. 931 for (const std::unique_ptr<COFFSection> &Section : Sections) 932 if (!isAssociative(*Section)) 933 Assign(*Section); 934 for (const std::unique_ptr<COFFSection> &Section : Sections) 935 if (isAssociative(*Section)) 936 Assign(*Section); 937 } 938 939 // Assign file offsets to COFF object file structures. 940 void WinCOFFObjectWriter::assignFileOffsets(MCAssembler &Asm, 941 const MCAsmLayout &Layout) { 942 unsigned Offset = W.OS.tell(); 943 944 Offset += UseBigObj ? COFF::Header32Size : COFF::Header16Size; 945 Offset += COFF::SectionSize * Header.NumberOfSections; 946 947 for (const auto &Section : Asm) { 948 COFFSection *Sec = SectionMap[&Section]; 949 950 if (Sec->Number == -1) 951 continue; 952 953 Sec->Header.SizeOfRawData = Layout.getSectionAddressSize(&Section); 954 955 if (IsPhysicalSection(Sec)) { 956 Sec->Header.PointerToRawData = Offset; 957 Offset += Sec->Header.SizeOfRawData; 958 } 959 960 if (!Sec->Relocations.empty()) { 961 bool RelocationsOverflow = Sec->Relocations.size() >= 0xffff; 962 963 if (RelocationsOverflow) { 964 // Signal overflow by setting NumberOfRelocations to max value. Actual 965 // size is found in reloc #0. Microsoft tools understand this. 966 Sec->Header.NumberOfRelocations = 0xffff; 967 } else { 968 Sec->Header.NumberOfRelocations = Sec->Relocations.size(); 969 } 970 Sec->Header.PointerToRelocations = Offset; 971 972 if (RelocationsOverflow) { 973 // Reloc #0 will contain actual count, so make room for it. 974 Offset += COFF::RelocationSize; 975 } 976 977 Offset += COFF::RelocationSize * Sec->Relocations.size(); 978 979 for (auto &Relocation : Sec->Relocations) { 980 assert(Relocation.Symb->getIndex() != -1); 981 Relocation.Data.SymbolTableIndex = Relocation.Symb->getIndex(); 982 } 983 } 984 985 assert(Sec->Symbol->Aux.size() == 1 && 986 "Section's symbol must have one aux!"); 987 AuxSymbol &Aux = Sec->Symbol->Aux[0]; 988 assert(Aux.AuxType == ATSectionDefinition && 989 "Section's symbol's aux symbol must be a Section Definition!"); 990 Aux.Aux.SectionDefinition.Length = Sec->Header.SizeOfRawData; 991 Aux.Aux.SectionDefinition.NumberOfRelocations = 992 Sec->Header.NumberOfRelocations; 993 Aux.Aux.SectionDefinition.NumberOfLinenumbers = 994 Sec->Header.NumberOfLineNumbers; 995 } 996 997 Header.PointerToSymbolTable = Offset; 998 } 999 1000 uint64_t WinCOFFObjectWriter::writeObject(MCAssembler &Asm, 1001 const MCAsmLayout &Layout) { 1002 uint64_t StartOffset = W.OS.tell(); 1003 1004 if (Sections.size() > INT32_MAX) 1005 report_fatal_error( 1006 "PE COFF object files can't have more than 2147483647 sections"); 1007 1008 UseBigObj = Sections.size() > COFF::MaxNumberOfSections16; 1009 Header.NumberOfSections = Sections.size(); 1010 Header.NumberOfSymbols = 0; 1011 1012 setWeakDefaultNames(); 1013 assignSectionNumbers(); 1014 createFileSymbols(Asm); 1015 1016 for (auto &Symbol : Symbols) { 1017 // Update section number & offset for symbols that have them. 1018 if (Symbol->Section) 1019 Symbol->Data.SectionNumber = Symbol->Section->Number; 1020 Symbol->setIndex(Header.NumberOfSymbols++); 1021 // Update auxiliary symbol info. 1022 Symbol->Data.NumberOfAuxSymbols = Symbol->Aux.size(); 1023 Header.NumberOfSymbols += Symbol->Data.NumberOfAuxSymbols; 1024 } 1025 1026 // Build string table. 1027 for (const auto &S : Sections) 1028 if (S->Name.size() > COFF::NameSize) 1029 Strings.add(S->Name); 1030 for (const auto &S : Symbols) 1031 if (S->Name.size() > COFF::NameSize) 1032 Strings.add(S->Name); 1033 Strings.finalize(); 1034 1035 // Set names. 1036 for (const auto &S : Sections) 1037 SetSectionName(*S); 1038 for (auto &S : Symbols) 1039 SetSymbolName(*S); 1040 1041 // Fixup weak external references. 1042 for (auto &Symbol : Symbols) { 1043 if (Symbol->Other) { 1044 assert(Symbol->getIndex() != -1); 1045 assert(Symbol->Aux.size() == 1 && "Symbol must contain one aux symbol!"); 1046 assert(Symbol->Aux[0].AuxType == ATWeakExternal && 1047 "Symbol's aux symbol must be a Weak External!"); 1048 Symbol->Aux[0].Aux.WeakExternal.TagIndex = Symbol->Other->getIndex(); 1049 } 1050 } 1051 1052 // Fixup associative COMDAT sections. 1053 for (auto &Section : Sections) { 1054 if (Section->Symbol->Aux[0].Aux.SectionDefinition.Selection != 1055 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) 1056 continue; 1057 1058 const MCSectionCOFF &MCSec = *Section->MCSection; 1059 const MCSymbol *AssocMCSym = MCSec.getCOMDATSymbol(); 1060 assert(AssocMCSym); 1061 1062 // It's an error to try to associate with an undefined symbol or a symbol 1063 // without a section. 1064 if (!AssocMCSym->isInSection()) { 1065 Asm.getContext().reportError( 1066 SMLoc(), Twine("cannot make section ") + MCSec.getName() + 1067 Twine(" associative with sectionless symbol ") + 1068 AssocMCSym->getName()); 1069 continue; 1070 } 1071 1072 const auto *AssocMCSec = cast<MCSectionCOFF>(&AssocMCSym->getSection()); 1073 assert(SectionMap.count(AssocMCSec)); 1074 COFFSection *AssocSec = SectionMap[AssocMCSec]; 1075 1076 // Skip this section if the associated section is unused. 1077 if (AssocSec->Number == -1) 1078 continue; 1079 1080 Section->Symbol->Aux[0].Aux.SectionDefinition.Number = AssocSec->Number; 1081 } 1082 1083 // Create the contents of the .llvm_addrsig section. 1084 if (EmitAddrsigSection) { 1085 auto Frag = new MCDataFragment(AddrsigSection); 1086 Frag->setLayoutOrder(0); 1087 raw_svector_ostream OS(Frag->getContents()); 1088 for (const MCSymbol *S : AddrsigSyms) { 1089 if (!S->isTemporary()) { 1090 encodeULEB128(S->getIndex(), OS); 1091 continue; 1092 } 1093 1094 MCSection *TargetSection = &S->getSection(); 1095 assert(SectionMap.find(TargetSection) != SectionMap.end() && 1096 "Section must already have been defined in " 1097 "executePostLayoutBinding!"); 1098 encodeULEB128(SectionMap[TargetSection]->Symbol->getIndex(), OS); 1099 } 1100 } 1101 1102 assignFileOffsets(Asm, Layout); 1103 1104 // MS LINK expects to be able to use this timestamp to implement their 1105 // /INCREMENTAL feature. 1106 if (Asm.isIncrementalLinkerCompatible()) { 1107 Header.TimeDateStamp = getTime(); 1108 } else { 1109 // Have deterministic output if /INCREMENTAL isn't needed. Also matches GNU. 1110 Header.TimeDateStamp = 0; 1111 } 1112 1113 // Write it all to disk... 1114 WriteFileHeader(Header); 1115 writeSectionHeaders(); 1116 1117 // Write section contents. 1118 sections::iterator I = Sections.begin(); 1119 sections::iterator IE = Sections.end(); 1120 MCAssembler::iterator J = Asm.begin(); 1121 MCAssembler::iterator JE = Asm.end(); 1122 for (; I != IE && J != JE; ++I, ++J) 1123 writeSection(Asm, Layout, **I, *J); 1124 1125 assert(W.OS.tell() == Header.PointerToSymbolTable && 1126 "Header::PointerToSymbolTable is insane!"); 1127 1128 // Write a symbol table. 1129 for (auto &Symbol : Symbols) 1130 if (Symbol->getIndex() != -1) 1131 WriteSymbol(*Symbol); 1132 1133 // Write a string table, which completes the entire COFF file. 1134 Strings.write(W.OS); 1135 1136 return W.OS.tell() - StartOffset; 1137 } 1138 1139 MCWinCOFFObjectTargetWriter::MCWinCOFFObjectTargetWriter(unsigned Machine_) 1140 : Machine(Machine_) {} 1141 1142 // Pin the vtable to this file. 1143 void MCWinCOFFObjectTargetWriter::anchor() {} 1144 1145 //------------------------------------------------------------------------------ 1146 // WinCOFFObjectWriter factory function 1147 1148 std::unique_ptr<MCObjectWriter> llvm::createWinCOFFObjectWriter( 1149 std::unique_ptr<MCWinCOFFObjectTargetWriter> MOTW, raw_pwrite_stream &OS) { 1150 return std::make_unique<WinCOFFObjectWriter>(std::move(MOTW), OS); 1151 } 1152