1 //=--------- COFFLinkGraphBuilder.cpp - COFF LinkGraph builder ----------===// 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 // Generic COFF LinkGraph buliding code. 10 // 11 //===----------------------------------------------------------------------===// 12 #include "COFFLinkGraphBuilder.h" 13 14 #define DEBUG_TYPE "jitlink" 15 16 static const char *CommonSectionName = "__common"; 17 18 namespace llvm { 19 namespace jitlink { 20 21 static Triple createTripleWithCOFFFormat(Triple T) { 22 T.setObjectFormat(Triple::COFF); 23 return T; 24 } 25 26 COFFLinkGraphBuilder::COFFLinkGraphBuilder( 27 const object::COFFObjectFile &Obj, Triple TT, 28 LinkGraph::GetEdgeKindNameFunction GetEdgeKindName) 29 : Obj(Obj), 30 G(std::make_unique<LinkGraph>(Obj.getFileName().str(), 31 createTripleWithCOFFFormat(TT), 32 getPointerSize(Obj), getEndianness(Obj), 33 std::move(GetEdgeKindName))) { 34 LLVM_DEBUG({ 35 dbgs() << "Created COFFLinkGraphBuilder for \"" << Obj.getFileName() 36 << "\"\n"; 37 }); 38 } 39 40 COFFLinkGraphBuilder::~COFFLinkGraphBuilder() = default; 41 42 unsigned 43 COFFLinkGraphBuilder::getPointerSize(const object::COFFObjectFile &Obj) { 44 return Obj.getBytesInAddress(); 45 } 46 47 support::endianness 48 COFFLinkGraphBuilder::getEndianness(const object::COFFObjectFile &Obj) { 49 return Obj.isLittleEndian() ? support::little : support::big; 50 } 51 52 uint64_t COFFLinkGraphBuilder::getSectionSize(const object::COFFObjectFile &Obj, 53 const object::coff_section *Sec) { 54 // Consider the difference between executable form and object form. 55 // More information is inside COFFObjectFile::getSectionSize 56 if (Obj.getDOSHeader()) 57 return std::min(Sec->VirtualSize, Sec->SizeOfRawData); 58 return Sec->SizeOfRawData; 59 } 60 61 uint64_t 62 COFFLinkGraphBuilder::getSectionAddress(const object::COFFObjectFile &Obj, 63 const object::coff_section *Section) { 64 return Section->VirtualAddress + Obj.getImageBase(); 65 } 66 67 bool COFFLinkGraphBuilder::isComdatSection( 68 const object::coff_section *Section) { 69 return Section->Characteristics & COFF::IMAGE_SCN_LNK_COMDAT; 70 } 71 72 Section &COFFLinkGraphBuilder::getCommonSection() { 73 if (!CommonSection) 74 CommonSection = 75 &G->createSection(CommonSectionName, MemProt::Read | MemProt::Write); 76 return *CommonSection; 77 } 78 79 Expected<std::unique_ptr<LinkGraph>> COFFLinkGraphBuilder::buildGraph() { 80 if (!Obj.isRelocatableObject()) 81 return make_error<JITLinkError>("Object is not a relocatable COFF file"); 82 83 if (auto Err = graphifySections()) 84 return std::move(Err); 85 86 if (auto Err = graphifySymbols()) 87 return std::move(Err); 88 89 if (auto Err = addRelocations()) 90 return std::move(Err); 91 92 return std::move(G); 93 } 94 95 StringRef 96 COFFLinkGraphBuilder::getCOFFSectionName(COFFSectionIndex SectionIndex, 97 const object::coff_section *Sec, 98 object::COFFSymbolRef Sym) { 99 switch (SectionIndex) { 100 case COFF::IMAGE_SYM_UNDEFINED: { 101 if (Sym.getValue()) 102 return "(common)"; 103 else 104 return "(external)"; 105 } 106 case COFF::IMAGE_SYM_ABSOLUTE: 107 return "(absolute)"; 108 case COFF::IMAGE_SYM_DEBUG: { 109 // Used with .file symbol 110 return "(debug)"; 111 } 112 default: { 113 // Non reserved regular section numbers 114 if (Expected<StringRef> SecNameOrErr = Obj.getSectionName(Sec)) 115 return *SecNameOrErr; 116 } 117 } 118 return ""; 119 } 120 121 Error COFFLinkGraphBuilder::graphifySections() { 122 LLVM_DEBUG(dbgs() << " Creating graph sections...\n"); 123 124 GraphBlocks.resize(Obj.getNumberOfSections() + 1); 125 // For each section... 126 for (COFFSectionIndex SecIndex = 1; 127 SecIndex <= static_cast<COFFSectionIndex>(Obj.getNumberOfSections()); 128 SecIndex++) { 129 Expected<const object::coff_section *> Sec = Obj.getSection(SecIndex); 130 if (!Sec) 131 return Sec.takeError(); 132 133 StringRef SectionName; 134 if (Expected<StringRef> SecNameOrErr = Obj.getSectionName(*Sec)) 135 SectionName = *SecNameOrErr; 136 137 // FIXME: Skip debug info sections 138 139 LLVM_DEBUG({ 140 dbgs() << " " 141 << "Creating section for \"" << SectionName << "\"\n"; 142 }); 143 144 // FIXME: Revisit crash when dropping IMAGE_SCN_MEM_DISCARDABLE sections 145 146 // Get the section's memory protection flags. 147 MemProt Prot = MemProt::None; 148 if ((*Sec)->Characteristics & COFF::IMAGE_SCN_MEM_EXECUTE) 149 Prot |= MemProt::Exec; 150 if ((*Sec)->Characteristics & COFF::IMAGE_SCN_MEM_READ) 151 Prot |= MemProt::Read; 152 if ((*Sec)->Characteristics & COFF::IMAGE_SCN_MEM_WRITE) 153 Prot |= MemProt::Write; 154 155 // Look for existing sections first. 156 auto *GraphSec = G->findSectionByName(SectionName); 157 if (!GraphSec) 158 GraphSec = &G->createSection(SectionName, Prot); 159 if (GraphSec->getMemProt() != Prot) 160 return make_error<JITLinkError>("MemProt should match"); 161 162 Block *B = nullptr; 163 if ((*Sec)->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) 164 B = &G->createZeroFillBlock( 165 *GraphSec, getSectionSize(Obj, *Sec), 166 orc::ExecutorAddr(getSectionAddress(Obj, *Sec)), 167 (*Sec)->getAlignment(), 0); 168 else { 169 ArrayRef<uint8_t> Data; 170 if (auto Err = Obj.getSectionContents(*Sec, Data)) 171 return Err; 172 173 B = &G->createContentBlock( 174 *GraphSec, 175 ArrayRef<char>(reinterpret_cast<const char *>(Data.data()), 176 Data.size()), 177 orc::ExecutorAddr(getSectionAddress(Obj, *Sec)), 178 (*Sec)->getAlignment(), 0); 179 } 180 181 setGraphBlock(SecIndex, B); 182 } 183 184 return Error::success(); 185 } 186 187 Error COFFLinkGraphBuilder::graphifySymbols() { 188 LLVM_DEBUG(dbgs() << " Creating graph symbols...\n"); 189 190 SymbolSets.resize(Obj.getNumberOfSections() + 1); 191 PendingComdatExports.resize(Obj.getNumberOfSections() + 1); 192 GraphSymbols.resize(Obj.getNumberOfSymbols()); 193 194 for (COFFSymbolIndex SymIndex = 0; 195 SymIndex < static_cast<COFFSymbolIndex>(Obj.getNumberOfSymbols()); 196 SymIndex++) { 197 Expected<object::COFFSymbolRef> Sym = Obj.getSymbol(SymIndex); 198 if (!Sym) 199 return Sym.takeError(); 200 201 StringRef SymbolName; 202 if (Expected<StringRef> SymNameOrErr = Obj.getSymbolName(*Sym)) 203 SymbolName = *SymNameOrErr; 204 205 COFFSectionIndex SectionIndex = Sym->getSectionNumber(); 206 const object::coff_section *Sec = nullptr; 207 208 if (!COFF::isReservedSectionNumber(SectionIndex)) { 209 auto SecOrErr = Obj.getSection(SectionIndex); 210 if (!SecOrErr) 211 return make_error<JITLinkError>( 212 "Invalid COFF section number:" + formatv("{0:d}: ", SectionIndex) + 213 " (" + toString(SecOrErr.takeError()) + ")"); 214 Sec = *SecOrErr; 215 } 216 217 // Create jitlink symbol 218 jitlink::Symbol *GSym = nullptr; 219 if (Sym->isFileRecord()) 220 LLVM_DEBUG({ 221 dbgs() << " " << SymIndex << ": Skipping FileRecord symbol \"" 222 << SymbolName << "\" in " 223 << getCOFFSectionName(SectionIndex, Sec, *Sym) 224 << " (index: " << SectionIndex << ") \n"; 225 }); 226 else if (Sym->isUndefined()) { 227 LLVM_DEBUG({ 228 dbgs() << " " << SymIndex 229 << ": Creating external graph symbol for COFF symbol \"" 230 << SymbolName << "\" in " 231 << getCOFFSectionName(SectionIndex, Sec, *Sym) 232 << " (index: " << SectionIndex << ") \n"; 233 }); 234 GSym = 235 &G->addExternalSymbol(SymbolName, Sym->getValue(), Linkage::Strong); 236 } else if (Sym->isWeakExternal()) { 237 COFFSymbolIndex TagIndex = 238 Sym->getAux<object::coff_aux_weak_external>()->TagIndex; 239 assert(Sym->getAux<object::coff_aux_weak_external>()->Characteristics != 240 COFF::IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY && 241 "IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY is not supported."); 242 assert(Sym->getAux<object::coff_aux_weak_external>()->Characteristics != 243 COFF::IMAGE_WEAK_EXTERN_SEARCH_LIBRARY && 244 "IMAGE_WEAK_EXTERN_SEARCH_LIBRARY is not supported."); 245 WeakAliasRequests.push_back({SymIndex, TagIndex, SymbolName}); 246 } else { 247 Expected<jitlink::Symbol *> NewGSym = 248 createDefinedSymbol(SymIndex, SymbolName, *Sym, Sec); 249 if (!NewGSym) 250 return NewGSym.takeError(); 251 GSym = *NewGSym; 252 if (GSym) { 253 LLVM_DEBUG({ 254 dbgs() << " " << SymIndex 255 << ": Creating defined graph symbol for COFF symbol \"" 256 << SymbolName << "\" in " 257 << getCOFFSectionName(SectionIndex, Sec, *Sym) 258 << " (index: " << SectionIndex << ") \n"; 259 dbgs() << " " << *GSym << "\n"; 260 }); 261 } 262 } 263 264 // Register the symbol 265 if (GSym) 266 setGraphSymbol(SectionIndex, SymIndex, *GSym); 267 SymIndex += Sym->getNumberOfAuxSymbols(); 268 } 269 270 if (auto Err = flushWeakAliasRequests()) 271 return Err; 272 273 if (auto Err = calculateImplicitSizeOfSymbols()) 274 return Err; 275 276 return Error::success(); 277 } 278 279 Error COFFLinkGraphBuilder::flushWeakAliasRequests() { 280 // Export the weak external symbols and alias it 281 for (auto &WeakAlias : WeakAliasRequests) { 282 if (auto *Target = getGraphSymbol(WeakAlias.Target)) { 283 Expected<object::COFFSymbolRef> AliasSymbol = 284 Obj.getSymbol(WeakAlias.Alias); 285 if (!AliasSymbol) 286 return AliasSymbol.takeError(); 287 288 // FIXME: Support this when there's a way to handle this. 289 if (!Target->isDefined()) 290 return make_error<JITLinkError>("Weak external symbol with external " 291 "symbol as alternative not supported."); 292 293 jitlink::Symbol *NewSymbol = &G->addDefinedSymbol( 294 Target->getBlock(), Target->getOffset(), WeakAlias.SymbolName, 295 Target->getSize(), Linkage::Weak, Scope::Default, 296 Target->isCallable(), false); 297 setGraphSymbol(AliasSymbol->getSectionNumber(), WeakAlias.Alias, 298 *NewSymbol); 299 LLVM_DEBUG({ 300 dbgs() << " " << WeakAlias.Alias 301 << ": Creating weak external symbol for COFF symbol \"" 302 << WeakAlias.SymbolName << "\" in section " 303 << AliasSymbol->getSectionNumber() << "\n"; 304 dbgs() << " " << *NewSymbol << "\n"; 305 }); 306 } else 307 return make_error<JITLinkError>("Weak symbol alias requested but actual " 308 "symbol not found for symbol " + 309 formatv("{0:d}", WeakAlias.Alias)); 310 } 311 return Error::success(); 312 } 313 314 // In COFF, most of the defined symbols don't contain the size information. 315 // Hence, we calculate the "implicit" size of symbol by taking the delta of 316 // offsets of consecutive symbols within a block. We maintain a balanced tree 317 // set of symbols sorted by offset per each block in order to achieve 318 // logarithmic time complexity of sorted symbol insertion. Symbol is inserted to 319 // the set once it's processed in graphifySymbols. In this function, we iterate 320 // each collected symbol in sorted order and calculate the implicit size. 321 Error COFFLinkGraphBuilder::calculateImplicitSizeOfSymbols() { 322 for (COFFSectionIndex SecIndex = 1; 323 SecIndex <= static_cast<COFFSectionIndex>(Obj.getNumberOfSections()); 324 SecIndex++) { 325 auto &SymbolSet = SymbolSets[SecIndex]; 326 if (SymbolSet.empty()) 327 continue; 328 jitlink::Block *B = getGraphBlock(SecIndex); 329 orc::ExecutorAddrDiff LastOffset = B->getSize(); 330 orc::ExecutorAddrDiff LastDifferentOffset = B->getSize(); 331 orc::ExecutorAddrDiff LastSize = 0; 332 for (auto It = SymbolSet.rbegin(); It != SymbolSet.rend(); It++) { 333 orc::ExecutorAddrDiff Offset = It->first; 334 jitlink::Symbol *Symbol = It->second; 335 orc::ExecutorAddrDiff CandSize; 336 // Last offset can be same when aliasing happened 337 if (Symbol->getOffset() == LastOffset) 338 CandSize = LastSize; 339 else 340 CandSize = LastOffset - Offset; 341 342 LLVM_DEBUG({ 343 if (Offset + Symbol->getSize() > LastDifferentOffset) 344 dbgs() << " Overlapping symbol range generated for the following " 345 "symbol:" 346 << "\n" 347 << " " << *Symbol << "\n"; 348 }); 349 (void)LastDifferentOffset; 350 if (LastOffset != Offset) 351 LastDifferentOffset = Offset; 352 LastSize = CandSize; 353 LastOffset = Offset; 354 if (Symbol->getSize()) { 355 // Non empty symbol can happen in COMDAT symbol. 356 // We don't consider the possibility of overlapping symbol range that 357 // could be introduced by disparity between inferred symbol size and 358 // defined symbol size because symbol size information is currently only 359 // used by jitlink-check where we have control to not make overlapping 360 // ranges. 361 continue; 362 } 363 364 LLVM_DEBUG({ 365 if (!CandSize) 366 dbgs() << " Empty implicit symbol size generated for the following " 367 "symbol:" 368 << "\n" 369 << " " << *Symbol << "\n"; 370 }); 371 372 Symbol->setSize(CandSize); 373 } 374 } 375 return Error::success(); 376 } 377 378 Expected<Symbol *> COFFLinkGraphBuilder::createDefinedSymbol( 379 COFFSymbolIndex SymIndex, StringRef SymbolName, 380 object::COFFSymbolRef Symbol, const object::coff_section *Section) { 381 if (Symbol.isCommon()) { 382 // FIXME: correct alignment 383 return &G->addCommonSymbol(SymbolName, Scope::Default, getCommonSection(), 384 orc::ExecutorAddr(), Symbol.getValue(), 385 Symbol.getValue(), false); 386 } 387 if (Symbol.isAbsolute()) 388 return &G->addAbsoluteSymbol(SymbolName, 389 orc::ExecutorAddr(Symbol.getValue()), 0, 390 Linkage::Strong, Scope::Local, false); 391 392 if (llvm::COFF::isReservedSectionNumber(Symbol.getSectionNumber())) 393 return make_error<JITLinkError>( 394 "Reserved section number used in regular symbol " + 395 formatv("{0:d}", SymIndex)); 396 397 Block *B = getGraphBlock(Symbol.getSectionNumber()); 398 if (!B) { 399 LLVM_DEBUG({ 400 dbgs() << " " << SymIndex 401 << ": Skipping graph symbol since section was not created for " 402 "COFF symbol \"" 403 << SymbolName << "\" in section " << Symbol.getSectionNumber() 404 << "\n"; 405 }); 406 return nullptr; 407 } 408 409 if (Symbol.isExternal()) { 410 // This is not a comdat sequence, export the symbol as it is 411 if (!isComdatSection(Section)) { 412 413 return &G->addDefinedSymbol( 414 *B, Symbol.getValue(), SymbolName, 0, Linkage::Strong, Scope::Default, 415 Symbol.getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION, false); 416 } else { 417 if (!PendingComdatExports[Symbol.getSectionNumber()]) 418 return make_error<JITLinkError>("No pending COMDAT export for symbol " + 419 formatv("{0:d}", SymIndex)); 420 421 return exportCOMDATSymbol(SymIndex, SymbolName, Symbol); 422 } 423 } 424 425 if (Symbol.getStorageClass() == COFF::IMAGE_SYM_CLASS_STATIC || 426 Symbol.getStorageClass() == COFF::IMAGE_SYM_CLASS_LABEL) { 427 const object::coff_aux_section_definition *Definition = 428 Symbol.getSectionDefinition(); 429 if (!Definition || !isComdatSection(Section)) { 430 // Handle typical static symbol 431 return &G->addDefinedSymbol( 432 *B, Symbol.getValue(), SymbolName, 0, Linkage::Strong, Scope::Local, 433 Symbol.getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION, false); 434 } 435 if (Definition->Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE) { 436 auto Target = Definition->getNumber(Symbol.isBigObj()); 437 auto GSym = &G->addDefinedSymbol( 438 *B, Symbol.getValue(), SymbolName, 0, Linkage::Strong, Scope::Local, 439 Symbol.getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION, false); 440 getGraphBlock(Target)->addEdge(Edge::KeepAlive, 0, *GSym, 0); 441 return GSym; 442 } 443 if (PendingComdatExports[Symbol.getSectionNumber()]) 444 return make_error<JITLinkError>( 445 "COMDAT export request already exists before symbol " + 446 formatv("{0:d}", SymIndex)); 447 return createCOMDATExportRequest(SymIndex, Symbol, Definition); 448 } 449 return make_error<JITLinkError>("Unsupported storage class " + 450 formatv("{0:d}", Symbol.getStorageClass()) + 451 " in symbol " + formatv("{0:d}", SymIndex)); 452 } 453 454 // COMDAT handling: 455 // When IMAGE_SCN_LNK_COMDAT flag is set in the flags of a section, 456 // the section is called a COMDAT section. It contains two symbols 457 // in a sequence that specifes the behavior. First symbol is the section 458 // symbol which contains the size and name of the section. It also contains 459 // selection type that specifies how duplicate of the symbol is handled. 460 // Second symbol is COMDAT symbol which usually defines the external name and 461 // data type. 462 // 463 // Since two symbols always come in a specific order, we initiate pending COMDAT 464 // export request when we encounter the first symbol and actually exports it 465 // when we process the second symbol. 466 // 467 // Process the first symbol of COMDAT sequence. 468 Expected<Symbol *> COFFLinkGraphBuilder::createCOMDATExportRequest( 469 COFFSymbolIndex SymIndex, object::COFFSymbolRef Symbol, 470 const object::coff_aux_section_definition *Definition) { 471 Block *B = getGraphBlock(Symbol.getSectionNumber()); 472 Linkage L = Linkage::Strong; 473 switch (Definition->Selection) { 474 case COFF::IMAGE_COMDAT_SELECT_NODUPLICATES: { 475 L = Linkage::Strong; 476 break; 477 } 478 case COFF::IMAGE_COMDAT_SELECT_ANY: { 479 L = Linkage::Weak; 480 break; 481 } 482 case COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH: 483 case COFF::IMAGE_COMDAT_SELECT_SAME_SIZE: { 484 // FIXME: Implement size/content validation when LinkGraph is able to 485 // handle this. 486 L = Linkage::Weak; 487 break; 488 } 489 case COFF::IMAGE_COMDAT_SELECT_LARGEST: { 490 // FIXME: Support IMAGE_COMDAT_SELECT_LARGEST when LinkGraph is able to 491 // handle this. 492 return make_error<JITLinkError>( 493 "IMAGE_COMDAT_SELECT_LARGEST is not supported."); 494 } 495 case COFF::IMAGE_COMDAT_SELECT_NEWEST: { 496 // Even link.exe doesn't support this selection properly. 497 return make_error<JITLinkError>( 498 "IMAGE_COMDAT_SELECT_NEWEST is not supported."); 499 } 500 default: { 501 return make_error<JITLinkError>("Invalid comdat selection type: " + 502 formatv("{0:d}", Definition->Selection)); 503 } 504 } 505 PendingComdatExports[Symbol.getSectionNumber()] = {SymIndex, L}; 506 return &G->addAnonymousSymbol(*B, Symbol.getValue(), Definition->Length, 507 false, false); 508 } 509 510 // Process the second symbol of COMDAT sequence. 511 Expected<Symbol *> 512 COFFLinkGraphBuilder::exportCOMDATSymbol(COFFSymbolIndex SymIndex, 513 StringRef SymbolName, 514 object::COFFSymbolRef Symbol) { 515 auto &PendingComdatExport = PendingComdatExports[Symbol.getSectionNumber()]; 516 COFFSymbolIndex TargetIndex = PendingComdatExport->SymbolIndex; 517 Linkage L = PendingComdatExport->Linkage; 518 jitlink::Symbol *Target = getGraphSymbol(TargetIndex); 519 assert(Target && "COMDAT leaader is invalid."); 520 assert((llvm::count_if(G->defined_symbols(), 521 [&](const jitlink::Symbol *Sym) { 522 return Sym->getName() == SymbolName; 523 }) == 0) && 524 "Duplicate defined symbol"); 525 Target->setName(SymbolName); 526 Target->setLinkage(L); 527 Target->setCallable(Symbol.getComplexType() == 528 COFF::IMAGE_SYM_DTYPE_FUNCTION); 529 Target->setScope(Scope::Default); 530 LLVM_DEBUG({ 531 dbgs() << " " << SymIndex 532 << ": Exporting COMDAT graph symbol for COFF symbol \"" << SymbolName 533 << "\" in section " << Symbol.getSectionNumber() << "\n"; 534 dbgs() << " " << *Target << "\n"; 535 }); 536 PendingComdatExport = None; 537 return Target; 538 } 539 540 } // namespace jitlink 541 } // namespace llvm 542