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