1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486
|
//===- InputFiles.cpp -----------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "InputFiles.h"
#include "COFFLinkerContext.h"
#include "Chunks.h"
#include "Config.h"
#include "DebugTypes.h"
#include "Driver.h"
#include "SymbolTable.h"
#include "Symbols.h"
#include "lld/Common/DWARF.h"
#include "llvm-c/lto.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Twine.h"
#include "llvm/BinaryFormat/COFF.h"
#include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h"
#include "llvm/DebugInfo/CodeView/SymbolDeserializer.h"
#include "llvm/DebugInfo/CodeView/SymbolRecord.h"
#include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
#include "llvm/DebugInfo/PDB/Native/NativeSession.h"
#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
#include "llvm/IR/Mangler.h"
#include "llvm/LTO/LTO.h"
#include "llvm/Object/Binary.h"
#include "llvm/Object/COFF.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Target/TargetOptions.h"
#include "llvm/TargetParser/Triple.h"
#include <cstring>
#include <optional>
#include <system_error>
#include <utility>
using namespace llvm;
using namespace llvm::COFF;
using namespace llvm::codeview;
using namespace llvm::object;
using namespace llvm::support::endian;
using namespace lld;
using namespace lld::coff;
using llvm::Triple;
using llvm::support::ulittle32_t;
// Returns the last element of a path, which is supposed to be a filename.
static StringRef getBasename(StringRef path) {
return sys::path::filename(path, sys::path::Style::windows);
}
// Returns a string in the format of "foo.obj" or "foo.obj(bar.lib)".
std::string lld::toString(const coff::InputFile *file) {
if (!file)
return "<internal>";
if (file->parentName.empty())
return std::string(file->getName());
return (getBasename(file->parentName) + "(" + getBasename(file->getName()) +
")")
.str();
}
const COFFSyncStream &coff::operator<<(const COFFSyncStream &s,
const InputFile *f) {
return s << toString(f);
}
/// Checks that Source is compatible with being a weak alias to Target.
/// If Source is Undefined and has no weak alias set, makes it a weak
/// alias to Target.
static void checkAndSetWeakAlias(SymbolTable &symtab, InputFile *f,
Symbol *source, Symbol *target,
bool isAntiDep) {
if (auto *u = dyn_cast<Undefined>(source)) {
if (u->weakAlias && u->weakAlias != target) {
// Ignore duplicated anti-dependency symbols.
if (isAntiDep)
return;
if (!u->isAntiDep) {
// Weak aliases as produced by GCC are named in the form
// .weak.<weaksymbol>.<othersymbol>, where <othersymbol> is the name
// of another symbol emitted near the weak symbol.
// Just use the definition from the first object file that defined
// this weak symbol.
if (symtab.ctx.config.allowDuplicateWeak)
return;
symtab.reportDuplicate(source, f);
}
}
u->setWeakAlias(target, isAntiDep);
}
}
static bool ignoredSymbolName(StringRef name) {
return name == "@feat.00" || name == "@comp.id";
}
static coff_symbol_generic *cloneSymbol(COFFSymbolRef sym) {
if (sym.isBigObj()) {
auto *copy = make<coff_symbol32>(
*reinterpret_cast<const coff_symbol32 *>(sym.getRawPtr()));
return reinterpret_cast<coff_symbol_generic *>(copy);
} else {
auto *copy = make<coff_symbol16>(
*reinterpret_cast<const coff_symbol16 *>(sym.getRawPtr()));
return reinterpret_cast<coff_symbol_generic *>(copy);
}
}
ArchiveFile::ArchiveFile(COFFLinkerContext &ctx, MemoryBufferRef m)
: InputFile(ctx.symtab, ArchiveKind, m) {}
void ArchiveFile::parse() {
COFFLinkerContext &ctx = symtab.ctx;
// Parse a MemoryBufferRef as an archive file.
file = CHECK(Archive::create(mb), this);
// Try to read symbols from ECSYMBOLS section on ARM64EC.
if (ctx.symtabEC) {
iterator_range<Archive::symbol_iterator> symbols =
CHECK(file->ec_symbols(), this);
if (!symbols.empty()) {
for (const Archive::Symbol &sym : symbols)
ctx.symtabEC->addLazyArchive(this, sym);
// Read both EC and native symbols on ARM64X.
if (!ctx.hybridSymtab)
return;
}
}
// Read the symbol table to construct Lazy objects.
for (const Archive::Symbol &sym : file->symbols())
ctx.symtab.addLazyArchive(this, sym);
}
// Returns a buffer pointing to a member file containing a given symbol.
void ArchiveFile::addMember(const Archive::Symbol &sym) {
const Archive::Child &c =
CHECK(sym.getMember(), "could not get the member for symbol " +
toCOFFString(symtab.ctx, sym));
// Return an empty buffer if we have already returned the same buffer.
// FIXME: Remove this once we resolve all defineds before all undefineds in
// ObjFile::initializeSymbols().
if (!seen.insert(c.getChildOffset()).second)
return;
symtab.ctx.driver.enqueueArchiveMember(c, sym, getName());
}
std::vector<MemoryBufferRef>
lld::coff::getArchiveMembers(COFFLinkerContext &ctx, Archive *file) {
std::vector<MemoryBufferRef> v;
Error err = Error::success();
// Thin archives refer to .o files, so --reproduces needs the .o files too.
bool addToTar = file->isThin() && ctx.driver.tar;
for (const Archive::Child &c : file->children(err)) {
MemoryBufferRef mbref =
CHECK(c.getMemoryBufferRef(),
file->getFileName() +
": could not get the buffer for a child of the archive");
if (addToTar) {
ctx.driver.tar->append(relativeToRoot(check(c.getFullName())),
mbref.getBuffer());
}
v.push_back(mbref);
}
if (err)
Fatal(ctx) << file->getFileName()
<< ": Archive::children failed: " << toString(std::move(err));
return v;
}
ObjFile::ObjFile(SymbolTable &symtab, COFFObjectFile *coffObj, bool lazy)
: InputFile(symtab, ObjectKind, coffObj->getMemoryBufferRef(), lazy),
coffObj(coffObj) {}
ObjFile *ObjFile::create(COFFLinkerContext &ctx, MemoryBufferRef m, bool lazy) {
// Parse a memory buffer as a COFF file.
Expected<std::unique_ptr<Binary>> bin = createBinary(m);
if (!bin)
Fatal(ctx) << "Could not parse " << m.getBufferIdentifier();
auto *obj = dyn_cast<COFFObjectFile>(bin->get());
if (!obj)
Fatal(ctx) << m.getBufferIdentifier() << " is not a COFF file";
bin->release();
return make<ObjFile>(ctx.getSymtab(MachineTypes(obj->getMachine())), obj,
lazy);
}
void ObjFile::parseLazy() {
// Native object file.
uint32_t numSymbols = coffObj->getNumberOfSymbols();
for (uint32_t i = 0; i < numSymbols; ++i) {
COFFSymbolRef coffSym = check(coffObj->getSymbol(i));
if (coffSym.isUndefined() || !coffSym.isExternal() ||
coffSym.isWeakExternal())
continue;
StringRef name = check(coffObj->getSymbolName(coffSym));
if (coffSym.isAbsolute() && ignoredSymbolName(name))
continue;
symtab.addLazyObject(this, name);
if (!lazy)
return;
i += coffSym.getNumberOfAuxSymbols();
}
}
struct ECMapEntry {
ulittle32_t src;
ulittle32_t dst;
ulittle32_t type;
};
void ObjFile::initializeECThunks() {
for (SectionChunk *chunk : hybmpChunks) {
if (chunk->getContents().size() % sizeof(ECMapEntry)) {
Err(symtab.ctx) << "Invalid .hybmp chunk size "
<< chunk->getContents().size();
continue;
}
const uint8_t *end =
chunk->getContents().data() + chunk->getContents().size();
for (const uint8_t *iter = chunk->getContents().data(); iter != end;
iter += sizeof(ECMapEntry)) {
auto entry = reinterpret_cast<const ECMapEntry *>(iter);
switch (entry->type) {
case Arm64ECThunkType::Entry:
symtab.addEntryThunk(getSymbol(entry->src), getSymbol(entry->dst));
break;
case Arm64ECThunkType::Exit:
symtab.addExitThunk(getSymbol(entry->src), getSymbol(entry->dst));
break;
case Arm64ECThunkType::GuestExit:
break;
default:
Warn(symtab.ctx) << "Ignoring unknown EC thunk type " << entry->type;
}
}
}
}
void ObjFile::parse() {
// Read section and symbol tables.
initializeChunks();
initializeSymbols();
initializeFlags();
initializeDependencies();
initializeECThunks();
}
const coff_section *ObjFile::getSection(uint32_t i) {
auto sec = coffObj->getSection(i);
if (!sec)
Fatal(symtab.ctx) << "getSection failed: #" << i << ": " << sec.takeError();
return *sec;
}
// We set SectionChunk pointers in the SparseChunks vector to this value
// temporarily to mark comdat sections as having an unknown resolution. As we
// walk the object file's symbol table, once we visit either a leader symbol or
// an associative section definition together with the parent comdat's leader,
// we set the pointer to either nullptr (to mark the section as discarded) or a
// valid SectionChunk for that section.
static SectionChunk *const pendingComdat = reinterpret_cast<SectionChunk *>(1);
void ObjFile::initializeChunks() {
uint32_t numSections = coffObj->getNumberOfSections();
sparseChunks.resize(numSections + 1);
for (uint32_t i = 1; i < numSections + 1; ++i) {
const coff_section *sec = getSection(i);
if (sec->Characteristics & IMAGE_SCN_LNK_COMDAT)
sparseChunks[i] = pendingComdat;
else
sparseChunks[i] = readSection(i, nullptr, "");
}
}
SectionChunk *ObjFile::readSection(uint32_t sectionNumber,
const coff_aux_section_definition *def,
StringRef leaderName) {
const coff_section *sec = getSection(sectionNumber);
StringRef name;
if (Expected<StringRef> e = coffObj->getSectionName(sec))
name = *e;
else
Fatal(symtab.ctx) << "getSectionName failed: #" << sectionNumber << ": "
<< e.takeError();
if (name == ".drectve") {
ArrayRef<uint8_t> data;
cantFail(coffObj->getSectionContents(sec, data));
directives = StringRef((const char *)data.data(), data.size());
return nullptr;
}
if (name == ".llvm_addrsig") {
addrsigSec = sec;
return nullptr;
}
if (name == ".llvm.call-graph-profile") {
callgraphSec = sec;
return nullptr;
}
// Object files may have DWARF debug info or MS CodeView debug info
// (or both).
//
// DWARF sections don't need any special handling from the perspective
// of the linker; they are just a data section containing relocations.
// We can just link them to complete debug info.
//
// CodeView needs linker support. We need to interpret debug info,
// and then write it to a separate .pdb file.
// Ignore DWARF debug info unless requested to be included.
if (!symtab.ctx.config.includeDwarfChunks && name.starts_with(".debug_"))
return nullptr;
if (sec->Characteristics & llvm::COFF::IMAGE_SCN_LNK_REMOVE)
return nullptr;
SectionChunk *c;
if (isArm64EC(getMachineType()))
c = make<SectionChunkEC>(this, sec);
else
c = make<SectionChunk>(this, sec);
if (def)
c->checksum = def->CheckSum;
// CodeView sections are stored to a different vector because they are not
// linked in the regular manner.
if (c->isCodeView())
debugChunks.push_back(c);
else if (name == ".gfids$y")
guardFidChunks.push_back(c);
else if (name == ".giats$y")
guardIATChunks.push_back(c);
else if (name == ".gljmp$y")
guardLJmpChunks.push_back(c);
else if (name == ".gehcont$y")
guardEHContChunks.push_back(c);
else if (name == ".sxdata")
sxDataChunks.push_back(c);
else if (isArm64EC(getMachineType()) && name == ".hybmp$x")
hybmpChunks.push_back(c);
else if (symtab.ctx.config.tailMerge && sec->NumberOfRelocations == 0 &&
name == ".rdata" && leaderName.starts_with("??_C@"))
// COFF sections that look like string literal sections (i.e. no
// relocations, in .rdata, leader symbol name matches the MSVC name mangling
// for string literals) are subject to string tail merging.
MergeChunk::addSection(symtab.ctx, c);
else if (name == ".rsrc" || name.starts_with(".rsrc$"))
resourceChunks.push_back(c);
else if (!(sec->Characteristics & llvm::COFF::IMAGE_SCN_LNK_INFO))
chunks.push_back(c);
return c;
}
void ObjFile::includeResourceChunks() {
chunks.insert(chunks.end(), resourceChunks.begin(), resourceChunks.end());
}
void ObjFile::readAssociativeDefinition(
COFFSymbolRef sym, const coff_aux_section_definition *def) {
readAssociativeDefinition(sym, def, def->getNumber(sym.isBigObj()));
}
void ObjFile::readAssociativeDefinition(COFFSymbolRef sym,
const coff_aux_section_definition *def,
uint32_t parentIndex) {
SectionChunk *parent = sparseChunks[parentIndex];
int32_t sectionNumber = sym.getSectionNumber();
auto diag = [&]() {
StringRef name = check(coffObj->getSymbolName(sym));
StringRef parentName;
const coff_section *parentSec = getSection(parentIndex);
if (Expected<StringRef> e = coffObj->getSectionName(parentSec))
parentName = *e;
Err(symtab.ctx) << toString(this) << ": associative comdat " << name
<< " (sec " << sectionNumber
<< ") has invalid reference to section " << parentName
<< " (sec " << parentIndex << ")";
};
if (parent == pendingComdat) {
// This can happen if an associative comdat refers to another associative
// comdat that appears after it (invalid per COFF spec) or to a section
// without any symbols.
diag();
return;
}
// Check whether the parent is prevailing. If it is, so are we, and we read
// the section; otherwise mark it as discarded.
if (parent) {
SectionChunk *c = readSection(sectionNumber, def, "");
sparseChunks[sectionNumber] = c;
if (c) {
c->selection = IMAGE_COMDAT_SELECT_ASSOCIATIVE;
parent->addAssociative(c);
}
} else {
sparseChunks[sectionNumber] = nullptr;
}
}
void ObjFile::recordPrevailingSymbolForMingw(
COFFSymbolRef sym, DenseMap<StringRef, uint32_t> &prevailingSectionMap) {
// For comdat symbols in executable sections, where this is the copy
// of the section chunk we actually include instead of discarding it,
// add the symbol to a map to allow using it for implicitly
// associating .[px]data$<func> sections to it.
// Use the suffix from the .text$<func> instead of the leader symbol
// name, for cases where the names differ (i386 mangling/decorations,
// cases where the leader is a weak symbol named .weak.func.default*).
int32_t sectionNumber = sym.getSectionNumber();
SectionChunk *sc = sparseChunks[sectionNumber];
if (sc && sc->getOutputCharacteristics() & IMAGE_SCN_MEM_EXECUTE) {
StringRef name = sc->getSectionName().split('$').second;
prevailingSectionMap[name] = sectionNumber;
}
}
void ObjFile::maybeAssociateSEHForMingw(
COFFSymbolRef sym, const coff_aux_section_definition *def,
const DenseMap<StringRef, uint32_t> &prevailingSectionMap) {
StringRef name = check(coffObj->getSymbolName(sym));
if (name.consume_front(".pdata$") || name.consume_front(".xdata$") ||
name.consume_front(".eh_frame$")) {
// For MinGW, treat .[px]data$<func> and .eh_frame$<func> as implicitly
// associative to the symbol <func>.
auto parentSym = prevailingSectionMap.find(name);
if (parentSym != prevailingSectionMap.end())
readAssociativeDefinition(sym, def, parentSym->second);
}
}
Symbol *ObjFile::createRegular(COFFSymbolRef sym) {
SectionChunk *sc = sparseChunks[sym.getSectionNumber()];
if (sym.isExternal()) {
StringRef name = check(coffObj->getSymbolName(sym));
if (sc)
return symtab.addRegular(this, name, sym.getGeneric(), sc,
sym.getValue());
// For MinGW symbols named .weak.* that point to a discarded section,
// don't create an Undefined symbol. If nothing ever refers to the symbol,
// everything should be fine. If something actually refers to the symbol
// (e.g. the undefined weak alias), linking will fail due to undefined
// references at the end.
if (symtab.ctx.config.mingw && name.starts_with(".weak."))
return nullptr;
return symtab.addUndefined(name, this, false);
}
if (sc) {
const coff_symbol_generic *symGen = sym.getGeneric();
if (sym.isSection()) {
auto *customSymGen = cloneSymbol(sym);
customSymGen->Value = 0;
symGen = customSymGen;
}
return make<DefinedRegular>(this, /*Name*/ "", /*IsCOMDAT*/ false,
/*IsExternal*/ false, symGen, sc);
}
return nullptr;
}
void ObjFile::initializeSymbols() {
uint32_t numSymbols = coffObj->getNumberOfSymbols();
symbols.resize(numSymbols);
SmallVector<std::pair<Symbol *, const coff_aux_weak_external *>, 8>
weakAliases;
std::vector<uint32_t> pendingIndexes;
pendingIndexes.reserve(numSymbols);
DenseMap<StringRef, uint32_t> prevailingSectionMap;
std::vector<const coff_aux_section_definition *> comdatDefs(
coffObj->getNumberOfSections() + 1);
COFFLinkerContext &ctx = symtab.ctx;
for (uint32_t i = 0; i < numSymbols; ++i) {
COFFSymbolRef coffSym = check(coffObj->getSymbol(i));
bool prevailingComdat;
if (coffSym.isUndefined()) {
symbols[i] = createUndefined(coffSym, false);
} else if (coffSym.isWeakExternal()) {
auto aux = coffSym.getAux<coff_aux_weak_external>();
bool overrideLazy = true;
// On ARM64EC, external function calls emit a pair of weak-dependency
// aliases: func to #func and #func to the func guess exit thunk
// (instead of a single undefined func symbol, which would be emitted on
// other targets). Allow such aliases to be overridden by lazy archive
// symbols, just as we would for undefined symbols.
if (isArm64EC(getMachineType()) &&
aux->Characteristics == IMAGE_WEAK_EXTERN_ANTI_DEPENDENCY) {
COFFSymbolRef targetSym = check(coffObj->getSymbol(aux->TagIndex));
if (!targetSym.isAnyUndefined()) {
// If the target is defined, it may be either a guess exit thunk or
// the actual implementation. If it's the latter, consider the alias
// to be part of the implementation and override potential lazy
// archive symbols.
StringRef targetName = check(coffObj->getSymbolName(targetSym));
StringRef name = check(coffObj->getSymbolName(coffSym));
std::optional<std::string> mangledName =
getArm64ECMangledFunctionName(name);
overrideLazy = mangledName == targetName;
} else {
overrideLazy = false;
}
}
symbols[i] = createUndefined(coffSym, overrideLazy);
weakAliases.emplace_back(symbols[i], aux);
} else if (std::optional<Symbol *> optSym =
createDefined(coffSym, comdatDefs, prevailingComdat)) {
symbols[i] = *optSym;
if (ctx.config.mingw && prevailingComdat)
recordPrevailingSymbolForMingw(coffSym, prevailingSectionMap);
} else {
// createDefined() returns std::nullopt if a symbol belongs to a section
// that was pending at the point when the symbol was read. This can happen
// in two cases:
// 1) section definition symbol for a comdat leader;
// 2) symbol belongs to a comdat section associated with another section.
// In both of these cases, we can expect the section to be resolved by
// the time we finish visiting the remaining symbols in the symbol
// table. So we postpone the handling of this symbol until that time.
pendingIndexes.push_back(i);
}
i += coffSym.getNumberOfAuxSymbols();
}
for (uint32_t i : pendingIndexes) {
COFFSymbolRef sym = check(coffObj->getSymbol(i));
if (const coff_aux_section_definition *def = sym.getSectionDefinition()) {
if (def->Selection == IMAGE_COMDAT_SELECT_ASSOCIATIVE)
readAssociativeDefinition(sym, def);
else if (ctx.config.mingw)
maybeAssociateSEHForMingw(sym, def, prevailingSectionMap);
}
if (sparseChunks[sym.getSectionNumber()] == pendingComdat) {
StringRef name = check(coffObj->getSymbolName(sym));
Log(ctx) << "comdat section " << name
<< " without leader and unassociated, discarding";
continue;
}
symbols[i] = createRegular(sym);
}
for (auto &kv : weakAliases) {
Symbol *sym = kv.first;
const coff_aux_weak_external *aux = kv.second;
checkAndSetWeakAlias(symtab, this, sym, symbols[aux->TagIndex],
aux->Characteristics ==
IMAGE_WEAK_EXTERN_ANTI_DEPENDENCY);
}
// Free the memory used by sparseChunks now that symbol loading is finished.
decltype(sparseChunks)().swap(sparseChunks);
}
Symbol *ObjFile::createUndefined(COFFSymbolRef sym, bool overrideLazy) {
StringRef name = check(coffObj->getSymbolName(sym));
Symbol *s = symtab.addUndefined(name, this, overrideLazy);
// Add an anti-dependency alias for undefined AMD64 symbols on the ARM64EC
// target.
if (symtab.isEC() && getMachineType() == AMD64) {
auto u = dyn_cast<Undefined>(s);
if (u && !u->weakAlias) {
if (std::optional<std::string> mangledName =
getArm64ECMangledFunctionName(name)) {
Symbol *m = symtab.addUndefined(saver().save(*mangledName), this,
/*overrideLazy=*/false);
u->setWeakAlias(m, /*antiDep=*/true);
}
}
}
return s;
}
static const coff_aux_section_definition *findSectionDef(COFFObjectFile *obj,
int32_t section) {
uint32_t numSymbols = obj->getNumberOfSymbols();
for (uint32_t i = 0; i < numSymbols; ++i) {
COFFSymbolRef sym = check(obj->getSymbol(i));
if (sym.getSectionNumber() != section)
continue;
if (const coff_aux_section_definition *def = sym.getSectionDefinition())
return def;
}
return nullptr;
}
void ObjFile::handleComdatSelection(
COFFSymbolRef sym, COMDATType &selection, bool &prevailing,
DefinedRegular *leader,
const llvm::object::coff_aux_section_definition *def) {
if (prevailing)
return;
// There's already an existing comdat for this symbol: `Leader`.
// Use the comdats's selection field to determine if the new
// symbol in `Sym` should be discarded, produce a duplicate symbol
// error, etc.
SectionChunk *leaderChunk = leader->getChunk();
COMDATType leaderSelection = leaderChunk->selection;
COFFLinkerContext &ctx = symtab.ctx;
assert(leader->data && "Comdat leader without SectionChunk?");
if (isa<BitcodeFile>(leader->file)) {
// If the leader is only a LTO symbol, we don't know e.g. its final size
// yet, so we can't do the full strict comdat selection checking yet.
selection = leaderSelection = IMAGE_COMDAT_SELECT_ANY;
}
if ((selection == IMAGE_COMDAT_SELECT_ANY &&
leaderSelection == IMAGE_COMDAT_SELECT_LARGEST) ||
(selection == IMAGE_COMDAT_SELECT_LARGEST &&
leaderSelection == IMAGE_COMDAT_SELECT_ANY)) {
// cl.exe picks "any" for vftables when building with /GR- and
// "largest" when building with /GR. To be able to link object files
// compiled with each flag, "any" and "largest" are merged as "largest".
leaderSelection = selection = IMAGE_COMDAT_SELECT_LARGEST;
}
// GCCs __declspec(selectany) doesn't actually pick "any" but "same size as".
// Clang on the other hand picks "any". To be able to link two object files
// with a __declspec(selectany) declaration, one compiled with gcc and the
// other with clang, we merge them as proper "same size as"
if (ctx.config.mingw && ((selection == IMAGE_COMDAT_SELECT_ANY &&
leaderSelection == IMAGE_COMDAT_SELECT_SAME_SIZE) ||
(selection == IMAGE_COMDAT_SELECT_SAME_SIZE &&
leaderSelection == IMAGE_COMDAT_SELECT_ANY))) {
leaderSelection = selection = IMAGE_COMDAT_SELECT_SAME_SIZE;
}
// Other than that, comdat selections must match. This is a bit more
// strict than link.exe which allows merging "any" and "largest" if "any"
// is the first symbol the linker sees, and it allows merging "largest"
// with everything (!) if "largest" is the first symbol the linker sees.
// Making this symmetric independent of which selection is seen first
// seems better though.
// (This behavior matches ModuleLinker::getComdatResult().)
if (selection != leaderSelection) {
Log(ctx) << "conflicting comdat type for " << leader << ": "
<< (int)leaderSelection << " in " << leader->getFile() << " and "
<< (int)selection << " in " << this;
symtab.reportDuplicate(leader, this);
return;
}
switch (selection) {
case IMAGE_COMDAT_SELECT_NODUPLICATES:
symtab.reportDuplicate(leader, this);
break;
case IMAGE_COMDAT_SELECT_ANY:
// Nothing to do.
break;
case IMAGE_COMDAT_SELECT_SAME_SIZE:
if (leaderChunk->getSize() != getSection(sym)->SizeOfRawData) {
if (!ctx.config.mingw) {
symtab.reportDuplicate(leader, this);
} else {
const coff_aux_section_definition *leaderDef = nullptr;
if (leaderChunk->file)
leaderDef = findSectionDef(leaderChunk->file->getCOFFObj(),
leaderChunk->getSectionNumber());
if (!leaderDef || leaderDef->Length != def->Length)
symtab.reportDuplicate(leader, this);
}
}
break;
case IMAGE_COMDAT_SELECT_EXACT_MATCH: {
SectionChunk newChunk(this, getSection(sym));
// link.exe only compares section contents here and doesn't complain
// if the two comdat sections have e.g. different alignment.
// Match that.
if (leaderChunk->getContents() != newChunk.getContents())
symtab.reportDuplicate(leader, this, &newChunk, sym.getValue());
break;
}
case IMAGE_COMDAT_SELECT_ASSOCIATIVE:
// createDefined() is never called for IMAGE_COMDAT_SELECT_ASSOCIATIVE.
// (This means lld-link doesn't produce duplicate symbol errors for
// associative comdats while link.exe does, but associate comdats
// are never extern in practice.)
llvm_unreachable("createDefined not called for associative comdats");
case IMAGE_COMDAT_SELECT_LARGEST:
if (leaderChunk->getSize() < getSection(sym)->SizeOfRawData) {
// Replace the existing comdat symbol with the new one.
StringRef name = check(coffObj->getSymbolName(sym));
// FIXME: This is incorrect: With /opt:noref, the previous sections
// make it into the final executable as well. Correct handling would
// be to undo reading of the whole old section that's being replaced,
// or doing one pass that determines what the final largest comdat
// is for all IMAGE_COMDAT_SELECT_LARGEST comdats and then reading
// only the largest one.
replaceSymbol<DefinedRegular>(leader, this, name, /*IsCOMDAT*/ true,
/*IsExternal*/ true, sym.getGeneric(),
nullptr);
prevailing = true;
}
break;
case IMAGE_COMDAT_SELECT_NEWEST:
llvm_unreachable("should have been rejected earlier");
}
}
std::optional<Symbol *> ObjFile::createDefined(
COFFSymbolRef sym,
std::vector<const coff_aux_section_definition *> &comdatDefs,
bool &prevailing) {
prevailing = false;
auto getName = [&]() { return check(coffObj->getSymbolName(sym)); };
if (sym.isCommon()) {
auto *c = make<CommonChunk>(sym);
chunks.push_back(c);
return symtab.addCommon(this, getName(), sym.getValue(), sym.getGeneric(),
c);
}
COFFLinkerContext &ctx = symtab.ctx;
if (sym.isAbsolute()) {
StringRef name = getName();
if (name == "@feat.00")
feat00Flags = sym.getValue();
// Skip special symbols.
if (ignoredSymbolName(name))
return nullptr;
if (sym.isExternal())
return symtab.addAbsolute(name, sym);
return make<DefinedAbsolute>(ctx, name, sym);
}
int32_t sectionNumber = sym.getSectionNumber();
if (sectionNumber == llvm::COFF::IMAGE_SYM_DEBUG)
return nullptr;
if (sym.isEmptySectionDeclaration()) {
// As there is no coff_section in the object file for these, make a
// new virtual one, with everything zeroed out (i.e. an empty section),
// with only the name and characteristics set.
StringRef name = getName();
auto *hdr = make<coff_section>();
memset(hdr, 0, sizeof(*hdr));
strncpy(hdr->Name, name.data(),
std::min(name.size(), (size_t)COFF::NameSize));
// The Value field in a section symbol may contain the characteristics,
// or it may be zero, where we make something up (that matches what is
// used in .idata sections in the regular object files in import libraries).
if (sym.getValue())
hdr->Characteristics = sym.getValue() | IMAGE_SCN_ALIGN_4BYTES;
else
hdr->Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA |
IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE |
IMAGE_SCN_ALIGN_4BYTES;
auto *sc = make<SectionChunk>(this, hdr);
chunks.push_back(sc);
auto *symGen = cloneSymbol(sym);
// Ignore the Value offset of these symbols, as it may be a bitmask.
symGen->Value = 0;
return make<DefinedRegular>(this, /*name=*/"", /*isCOMDAT=*/false,
/*isExternal=*/false, symGen, sc);
}
if (llvm::COFF::isReservedSectionNumber(sectionNumber))
Fatal(ctx) << toString(this) << ": " << getName()
<< " should not refer to special section "
<< Twine(sectionNumber);
if ((uint32_t)sectionNumber >= sparseChunks.size())
Fatal(ctx) << toString(this) << ": " << getName()
<< " should not refer to non-existent section "
<< Twine(sectionNumber);
// Comdat handling.
// A comdat symbol consists of two symbol table entries.
// The first symbol entry has the name of the section (e.g. .text), fixed
// values for the other fields, and one auxiliary record.
// The second symbol entry has the name of the comdat symbol, called the
// "comdat leader".
// When this function is called for the first symbol entry of a comdat,
// it sets comdatDefs and returns std::nullopt, and when it's called for the
// second symbol entry it reads comdatDefs and then sets it back to nullptr.
// Handle comdat leader.
if (const coff_aux_section_definition *def = comdatDefs[sectionNumber]) {
comdatDefs[sectionNumber] = nullptr;
DefinedRegular *leader;
if (sym.isExternal()) {
std::tie(leader, prevailing) =
symtab.addComdat(this, getName(), sym.getGeneric());
} else {
leader = make<DefinedRegular>(this, /*Name*/ "", /*IsCOMDAT*/ false,
/*IsExternal*/ false, sym.getGeneric());
prevailing = true;
}
if (def->Selection < (int)IMAGE_COMDAT_SELECT_NODUPLICATES ||
// Intentionally ends at IMAGE_COMDAT_SELECT_LARGEST: link.exe
// doesn't understand IMAGE_COMDAT_SELECT_NEWEST either.
def->Selection > (int)IMAGE_COMDAT_SELECT_LARGEST) {
Fatal(ctx) << "unknown comdat type "
<< std::to_string((int)def->Selection) << " for " << getName()
<< " in " << toString(this);
}
COMDATType selection = (COMDATType)def->Selection;
if (leader->isCOMDAT)
handleComdatSelection(sym, selection, prevailing, leader, def);
if (prevailing) {
SectionChunk *c = readSection(sectionNumber, def, getName());
sparseChunks[sectionNumber] = c;
if (!c)
return nullptr;
c->sym = cast<DefinedRegular>(leader);
c->selection = selection;
cast<DefinedRegular>(leader)->data = &c->repl;
} else {
sparseChunks[sectionNumber] = nullptr;
}
return leader;
}
// Prepare to handle the comdat leader symbol by setting the section's
// ComdatDefs pointer if we encounter a non-associative comdat.
if (sparseChunks[sectionNumber] == pendingComdat) {
if (const coff_aux_section_definition *def = sym.getSectionDefinition()) {
if (def->Selection != IMAGE_COMDAT_SELECT_ASSOCIATIVE)
comdatDefs[sectionNumber] = def;
}
return std::nullopt;
}
return createRegular(sym);
}
MachineTypes ObjFile::getMachineType() const {
return static_cast<MachineTypes>(coffObj->getMachine());
}
ArrayRef<uint8_t> ObjFile::getDebugSection(StringRef secName) {
if (SectionChunk *sec = SectionChunk::findByName(debugChunks, secName))
return sec->consumeDebugMagic();
return {};
}
// OBJ files systematically store critical information in a .debug$S stream,
// even if the TU was compiled with no debug info. At least two records are
// always there. S_OBJNAME stores a 32-bit signature, which is loaded into the
// PCHSignature member. S_COMPILE3 stores compile-time cmd-line flags. This is
// currently used to initialize the hotPatchable member.
void ObjFile::initializeFlags() {
ArrayRef<uint8_t> data = getDebugSection(".debug$S");
if (data.empty())
return;
DebugSubsectionArray subsections;
BinaryStreamReader reader(data, llvm::endianness::little);
ExitOnError exitOnErr;
exitOnErr(reader.readArray(subsections, data.size()));
for (const DebugSubsectionRecord &ss : subsections) {
if (ss.kind() != DebugSubsectionKind::Symbols)
continue;
unsigned offset = 0;
// Only parse the first two records. We are only looking for S_OBJNAME
// and S_COMPILE3, and they usually appear at the beginning of the
// stream.
for (unsigned i = 0; i < 2; ++i) {
Expected<CVSymbol> sym = readSymbolFromStream(ss.getRecordData(), offset);
if (!sym) {
consumeError(sym.takeError());
return;
}
if (sym->kind() == SymbolKind::S_COMPILE3) {
auto cs =
cantFail(SymbolDeserializer::deserializeAs<Compile3Sym>(sym.get()));
hotPatchable =
(cs.Flags & CompileSym3Flags::HotPatch) != CompileSym3Flags::None;
}
if (sym->kind() == SymbolKind::S_OBJNAME) {
auto objName = cantFail(SymbolDeserializer::deserializeAs<ObjNameSym>(
sym.get()));
if (objName.Signature)
pchSignature = objName.Signature;
}
offset += sym->length();
}
}
}
// Depending on the compilation flags, OBJs can refer to external files,
// necessary to merge this OBJ into the final PDB. We currently support two
// types of external files: Precomp/PCH OBJs, when compiling with /Yc and /Yu.
// And PDB type servers, when compiling with /Zi. This function extracts these
// dependencies and makes them available as a TpiSource interface (see
// DebugTypes.h). Both cases only happen with cl.exe: clang-cl produces regular
// output even with /Yc and /Yu and with /Zi.
void ObjFile::initializeDependencies() {
COFFLinkerContext &ctx = symtab.ctx;
if (!ctx.config.debug)
return;
bool isPCH = false;
ArrayRef<uint8_t> data = getDebugSection(".debug$P");
if (!data.empty())
isPCH = true;
else
data = getDebugSection(".debug$T");
// symbols but no types, make a plain, empty TpiSource anyway, because it
// simplifies adding the symbols later.
if (data.empty()) {
if (!debugChunks.empty())
debugTypesObj = makeTpiSource(ctx, this);
return;
}
// Get the first type record. It will indicate if this object uses a type
// server (/Zi) or a PCH file (/Yu).
CVTypeArray types;
BinaryStreamReader reader(data, llvm::endianness::little);
cantFail(reader.readArray(types, reader.getLength()));
CVTypeArray::Iterator firstType = types.begin();
if (firstType == types.end())
return;
// Remember the .debug$T or .debug$P section.
debugTypes = data;
// This object file is a PCH file that others will depend on.
if (isPCH) {
debugTypesObj = makePrecompSource(ctx, this);
return;
}
// This object file was compiled with /Zi. Enqueue the PDB dependency.
if (firstType->kind() == LF_TYPESERVER2) {
TypeServer2Record ts = cantFail(
TypeDeserializer::deserializeAs<TypeServer2Record>(firstType->data()));
debugTypesObj = makeUseTypeServerSource(ctx, this, ts);
enqueuePdbFile(ts.getName(), this);
return;
}
// This object was compiled with /Yu. It uses types from another object file
// with a matching signature.
if (firstType->kind() == LF_PRECOMP) {
PrecompRecord precomp = cantFail(
TypeDeserializer::deserializeAs<PrecompRecord>(firstType->data()));
// We're better off trusting the LF_PRECOMP signature. In some cases the
// S_OBJNAME record doesn't contain a valid PCH signature.
if (precomp.Signature)
pchSignature = precomp.Signature;
debugTypesObj = makeUsePrecompSource(ctx, this, precomp);
// Drop the LF_PRECOMP record from the input stream.
debugTypes = debugTypes.drop_front(firstType->RecordData.size());
return;
}
// This is a plain old object file.
debugTypesObj = makeTpiSource(ctx, this);
}
// The casing of the PDB path stamped in the OBJ can differ from the actual path
// on disk. With this, we ensure to always use lowercase as a key for the
// pdbInputFileInstances map, at least on Windows.
static std::string normalizePdbPath(StringRef path) {
#if defined(_WIN32)
return path.lower();
#else // LINUX
return std::string(path);
#endif
}
// If existing, return the actual PDB path on disk.
static std::optional<std::string>
findPdbPath(StringRef pdbPath, ObjFile *dependentFile, StringRef outputPath) {
// Ensure the file exists before anything else. In some cases, if the path
// points to a removable device, Driver::enqueuePath() would fail with an
// error (EAGAIN, "resource unavailable try again") which we want to skip
// silently.
if (llvm::sys::fs::exists(pdbPath))
return normalizePdbPath(pdbPath);
StringRef objPath = !dependentFile->parentName.empty()
? dependentFile->parentName
: dependentFile->getName();
// Currently, type server PDBs are only created by MSVC cl, which only runs
// on Windows, so we can assume type server paths are Windows style.
StringRef pdbName = sys::path::filename(pdbPath, sys::path::Style::windows);
// Check if the PDB is in the same folder as the OBJ.
SmallString<128> path;
sys::path::append(path, sys::path::parent_path(objPath), pdbName);
if (llvm::sys::fs::exists(path))
return normalizePdbPath(path);
// Check if the PDB is in the output folder.
path.clear();
sys::path::append(path, sys::path::parent_path(outputPath), pdbName);
if (llvm::sys::fs::exists(path))
return normalizePdbPath(path);
return std::nullopt;
}
PDBInputFile::PDBInputFile(COFFLinkerContext &ctx, MemoryBufferRef m)
: InputFile(ctx.symtab, PDBKind, m) {}
PDBInputFile::~PDBInputFile() = default;
PDBInputFile *PDBInputFile::findFromRecordPath(const COFFLinkerContext &ctx,
StringRef path,
ObjFile *fromFile) {
auto p = findPdbPath(path.str(), fromFile, ctx.config.outputFile);
if (!p)
return nullptr;
auto it = ctx.pdbInputFileInstances.find(*p);
if (it != ctx.pdbInputFileInstances.end())
return it->second;
return nullptr;
}
void PDBInputFile::parse() {
symtab.ctx.pdbInputFileInstances[mb.getBufferIdentifier().str()] = this;
std::unique_ptr<pdb::IPDBSession> thisSession;
Error E = pdb::NativeSession::createFromPdb(
MemoryBuffer::getMemBuffer(mb, false), thisSession);
if (E) {
loadErrorStr.emplace(toString(std::move(E)));
return; // fail silently at this point - the error will be handled later,
// when merging the debug type stream
}
session.reset(static_cast<pdb::NativeSession *>(thisSession.release()));
pdb::PDBFile &pdbFile = session->getPDBFile();
auto expectedInfo = pdbFile.getPDBInfoStream();
// All PDB Files should have an Info stream.
if (!expectedInfo) {
loadErrorStr.emplace(toString(expectedInfo.takeError()));
return;
}
debugTypesObj = makeTypeServerSource(symtab.ctx, this);
}
// Used only for DWARF debug info, which is not common (except in MinGW
// environments). This returns an optional pair of file name and line
// number for where the variable was defined.
std::optional<std::pair<StringRef, uint32_t>>
ObjFile::getVariableLocation(StringRef var) {
if (!dwarf) {
dwarf = make<DWARFCache>(DWARFContext::create(*getCOFFObj()));
if (!dwarf)
return std::nullopt;
}
if (symtab.machine == I386)
var.consume_front("_");
std::optional<std::pair<std::string, unsigned>> ret =
dwarf->getVariableLoc(var);
if (!ret)
return std::nullopt;
return std::make_pair(saver().save(ret->first), ret->second);
}
// Used only for DWARF debug info, which is not common (except in MinGW
// environments).
std::optional<DILineInfo> ObjFile::getDILineInfo(uint32_t offset,
uint32_t sectionIndex) {
if (!dwarf) {
dwarf = make<DWARFCache>(DWARFContext::create(*getCOFFObj()));
if (!dwarf)
return std::nullopt;
}
return dwarf->getDILineInfo(offset, sectionIndex);
}
void ObjFile::enqueuePdbFile(StringRef path, ObjFile *fromFile) {
auto p = findPdbPath(path.str(), fromFile, symtab.ctx.config.outputFile);
if (!p)
return;
auto it = symtab.ctx.pdbInputFileInstances.emplace(*p, nullptr);
if (!it.second)
return; // already scheduled for load
symtab.ctx.driver.enqueuePDB(*p);
}
ImportFile::ImportFile(COFFLinkerContext &ctx, MemoryBufferRef m)
: InputFile(ctx.getSymtab(getMachineType(m)), ImportKind, m),
live(!ctx.config.doGC) {}
MachineTypes ImportFile::getMachineType(MemoryBufferRef m) {
uint16_t machine =
reinterpret_cast<const coff_import_header *>(m.getBufferStart())->Machine;
return MachineTypes(machine);
}
bool ImportFile::isSameImport(const ImportFile *other) const {
if (!externalName.empty())
return other->externalName == externalName;
return hdr->OrdinalHint == other->hdr->OrdinalHint;
}
ImportThunkChunk *ImportFile::makeImportThunk() {
switch (hdr->Machine) {
case AMD64:
return make<ImportThunkChunkX64>(symtab.ctx, impSym);
case I386:
return make<ImportThunkChunkX86>(symtab.ctx, impSym);
case ARM64:
return make<ImportThunkChunkARM64>(symtab.ctx, impSym, ARM64);
case ARMNT:
return make<ImportThunkChunkARM>(symtab.ctx, impSym);
}
llvm_unreachable("unknown machine type");
}
void ImportFile::parse() {
const auto *hdr =
reinterpret_cast<const coff_import_header *>(mb.getBufferStart());
// Check if the total size is valid.
if (mb.getBufferSize() < sizeof(*hdr) ||
mb.getBufferSize() != sizeof(*hdr) + hdr->SizeOfData)
Fatal(symtab.ctx) << "broken import library";
// Read names and create an __imp_ symbol.
StringRef buf = mb.getBuffer().substr(sizeof(*hdr));
auto split = buf.split('\0');
buf = split.second;
StringRef name;
if (isArm64EC(hdr->Machine)) {
if (std::optional<std::string> demangledName =
getArm64ECDemangledFunctionName(split.first))
name = saver().save(*demangledName);
}
if (name.empty())
name = saver().save(split.first);
StringRef impName = saver().save("__imp_" + name);
dllName = buf.split('\0').first;
StringRef extName;
switch (hdr->getNameType()) {
case IMPORT_ORDINAL:
extName = "";
break;
case IMPORT_NAME:
extName = name;
break;
case IMPORT_NAME_NOPREFIX:
extName = ltrim1(name, "?@_");
break;
case IMPORT_NAME_UNDECORATE:
extName = ltrim1(name, "?@_");
extName = extName.substr(0, extName.find('@'));
break;
case IMPORT_NAME_EXPORTAS:
extName = buf.substr(dllName.size() + 1).split('\0').first;
break;
}
this->hdr = hdr;
externalName = extName;
bool isCode = hdr->getType() == llvm::COFF::IMPORT_CODE;
if (!symtab.isEC()) {
impSym = symtab.addImportData(impName, this, location);
} else {
// In addition to the regular IAT, ARM64EC also contains an auxiliary IAT,
// which holds addresses that are guaranteed to be callable directly from
// ARM64 code. Function symbol naming is swapped: __imp_ symbols refer to
// the auxiliary IAT, while __imp_aux_ symbols refer to the regular IAT. For
// data imports, the naming is reversed.
StringRef auxImpName = saver().save("__imp_aux_" + name);
if (isCode) {
impSym = symtab.addImportData(auxImpName, this, location);
impECSym = symtab.addImportData(impName, this, auxLocation);
} else {
impSym = symtab.addImportData(impName, this, location);
impECSym = symtab.addImportData(auxImpName, this, auxLocation);
}
if (!impECSym)
return;
StringRef auxImpCopyName = saver().save("__auximpcopy_" + name);
auxImpCopySym = symtab.addImportData(auxImpCopyName, this, auxCopyLocation);
if (!auxImpCopySym)
return;
}
// If this was a duplicate, we logged an error but may continue;
// in this case, impSym is nullptr.
if (!impSym)
return;
if (hdr->getType() == llvm::COFF::IMPORT_CONST)
static_cast<void>(symtab.addImportData(name, this, location));
// If type is function, we need to create a thunk which jump to an
// address pointed by the __imp_ symbol. (This allows you to call
// DLL functions just like regular non-DLL functions.)
if (isCode) {
if (!symtab.isEC()) {
thunkSym = symtab.addImportThunk(name, impSym, makeImportThunk());
} else {
thunkSym = symtab.addImportThunk(
name, impSym, make<ImportThunkChunkX64>(symtab.ctx, impSym));
if (std::optional<std::string> mangledName =
getArm64ECMangledFunctionName(name)) {
StringRef auxThunkName = saver().save(*mangledName);
auxThunkSym = symtab.addImportThunk(
auxThunkName, impECSym,
make<ImportThunkChunkARM64>(symtab.ctx, impECSym, ARM64EC));
}
StringRef impChkName = saver().save("__impchk_" + name);
impchkThunk = make<ImportThunkChunkARM64EC>(this);
impchkThunk->sym = symtab.addImportThunk(impChkName, impSym, impchkThunk);
symtab.ctx.driver.pullArm64ECIcallHelper();
}
}
}
BitcodeFile::BitcodeFile(SymbolTable &symtab, MemoryBufferRef mb,
std::unique_ptr<lto::InputFile> &o, bool lazy)
: InputFile(symtab, BitcodeKind, mb, lazy) {
obj.swap(o);
}
BitcodeFile *BitcodeFile::create(COFFLinkerContext &ctx, MemoryBufferRef mb,
StringRef archiveName,
uint64_t offsetInArchive, bool lazy) {
std::string path = mb.getBufferIdentifier().str();
if (ctx.config.thinLTOIndexOnly)
path = replaceThinLTOSuffix(mb.getBufferIdentifier(),
ctx.config.thinLTOObjectSuffixReplace.first,
ctx.config.thinLTOObjectSuffixReplace.second);
// ThinLTO assumes that all MemoryBufferRefs given to it have a unique
// name. If two archives define two members with the same name, this
// causes a collision which result in only one of the objects being taken
// into consideration at LTO time (which very likely causes undefined
// symbols later in the link stage). So we append file offset to make
// filename unique.
MemoryBufferRef mbref(mb.getBuffer(),
saver().save(archiveName.empty()
? path
: archiveName +
sys::path::filename(path) +
utostr(offsetInArchive)));
std::unique_ptr<lto::InputFile> obj = check(lto::InputFile::create(mbref));
return make<BitcodeFile>(ctx.getSymtab(getMachineType(obj.get())), mb, obj,
lazy);
}
BitcodeFile::~BitcodeFile() = default;
void BitcodeFile::parse() {
llvm::StringSaver &saver = lld::saver();
std::vector<std::pair<Symbol *, bool>> comdat(obj->getComdatTable().size());
for (size_t i = 0; i != obj->getComdatTable().size(); ++i)
// FIXME: Check nodeduplicate
comdat[i] =
symtab.addComdat(this, saver.save(obj->getComdatTable()[i].first));
for (const lto::InputFile::Symbol &objSym : obj->symbols()) {
StringRef symName = saver.save(objSym.getName());
int comdatIndex = objSym.getComdatIndex();
Symbol *sym;
SectionChunk *fakeSC = nullptr;
if (objSym.isExecutable())
fakeSC = &symtab.ctx.ltoTextSectionChunk.chunk;
else
fakeSC = &symtab.ctx.ltoDataSectionChunk.chunk;
if (objSym.isUndefined()) {
sym = symtab.addUndefined(symName, this, false);
if (objSym.isWeak())
sym->deferUndefined = true;
// If one LTO object file references (i.e. has an undefined reference to)
// a symbol with an __imp_ prefix, the LTO compilation itself sees it
// as unprefixed but with a dllimport attribute instead, and doesn't
// understand the relation to a concrete IR symbol with the __imp_ prefix.
//
// For such cases, mark the symbol as used in a regular object (i.e. the
// symbol must be retained) so that the linker can associate the
// references in the end. If the symbol is defined in an import library
// or in a regular object file, this has no effect, but if it is defined
// in another LTO object file, this makes sure it is kept, to fulfill
// the reference when linking the output of the LTO compilation.
if (symName.starts_with("__imp_"))
sym->isUsedInRegularObj = true;
} else if (objSym.isCommon()) {
sym = symtab.addCommon(this, symName, objSym.getCommonSize());
} else if (objSym.isWeak() && objSym.isIndirect()) {
// Weak external.
sym = symtab.addUndefined(symName, this, true);
std::string fallback = std::string(objSym.getCOFFWeakExternalFallback());
Symbol *alias = symtab.addUndefined(saver.save(fallback));
checkAndSetWeakAlias(symtab, this, sym, alias, false);
} else if (comdatIndex != -1) {
if (symName == obj->getComdatTable()[comdatIndex].first) {
sym = comdat[comdatIndex].first;
if (cast<DefinedRegular>(sym)->data == nullptr)
cast<DefinedRegular>(sym)->data = &fakeSC->repl;
} else if (comdat[comdatIndex].second) {
sym = symtab.addRegular(this, symName, nullptr, fakeSC);
} else {
sym = symtab.addUndefined(symName, this, false);
}
} else {
sym =
symtab.addRegular(this, symName, nullptr, fakeSC, 0, objSym.isWeak());
}
symbols.push_back(sym);
if (objSym.isUsed())
symtab.ctx.config.gcroot.push_back(sym);
}
directives = saver.save(obj->getCOFFLinkerOpts());
}
void BitcodeFile::parseLazy() {
for (const lto::InputFile::Symbol &sym : obj->symbols())
if (!sym.isUndefined()) {
symtab.addLazyObject(this, sym.getName());
if (!lazy)
return;
}
}
MachineTypes BitcodeFile::getMachineType(const llvm::lto::InputFile *obj) {
Triple t(obj->getTargetTriple());
switch (t.getArch()) {
case Triple::x86_64:
return AMD64;
case Triple::x86:
return I386;
case Triple::arm:
case Triple::thumb:
return ARMNT;
case Triple::aarch64:
return t.isWindowsArm64EC() ? ARM64EC : ARM64;
default:
return IMAGE_FILE_MACHINE_UNKNOWN;
}
}
std::string lld::coff::replaceThinLTOSuffix(StringRef path, StringRef suffix,
StringRef repl) {
if (path.consume_back(suffix))
return (path + repl).str();
return std::string(path);
}
static bool isRVACode(COFFObjectFile *coffObj, uint64_t rva, InputFile *file) {
for (size_t i = 1, e = coffObj->getNumberOfSections(); i <= e; i++) {
const coff_section *sec = CHECK(coffObj->getSection(i), file);
if (rva >= sec->VirtualAddress &&
rva <= sec->VirtualAddress + sec->VirtualSize) {
return (sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE) != 0;
}
}
return false;
}
void DLLFile::parse() {
// Parse a memory buffer as a PE-COFF executable.
std::unique_ptr<Binary> bin = CHECK(createBinary(mb), this);
if (auto *obj = dyn_cast<COFFObjectFile>(bin.get())) {
bin.release();
coffObj.reset(obj);
} else {
Err(symtab.ctx) << toString(this) << " is not a COFF file";
return;
}
if (!coffObj->getPE32Header() && !coffObj->getPE32PlusHeader()) {
Err(symtab.ctx) << toString(this) << " is not a PE-COFF executable";
return;
}
for (const auto &exp : coffObj->export_directories()) {
StringRef dllName, symbolName;
uint32_t exportRVA;
checkError(exp.getDllName(dllName));
checkError(exp.getSymbolName(symbolName));
checkError(exp.getExportRVA(exportRVA));
if (symbolName.empty())
continue;
bool code = isRVACode(coffObj.get(), exportRVA, this);
Symbol *s = make<Symbol>();
s->dllName = dllName;
s->symbolName = symbolName;
s->importType = code ? ImportType::IMPORT_CODE : ImportType::IMPORT_DATA;
s->nameType = ImportNameType::IMPORT_NAME;
if (coffObj->getMachine() == I386) {
s->symbolName = symbolName = saver().save("_" + symbolName);
s->nameType = ImportNameType::IMPORT_NAME_NOPREFIX;
}
StringRef impName = saver().save("__imp_" + symbolName);
symtab.addLazyDLLSymbol(this, s, impName);
if (code)
symtab.addLazyDLLSymbol(this, s, symbolName);
}
}
MachineTypes DLLFile::getMachineType() const {
if (coffObj)
return static_cast<MachineTypes>(coffObj->getMachine());
return IMAGE_FILE_MACHINE_UNKNOWN;
}
void DLLFile::makeImport(DLLFile::Symbol *s) {
if (!seen.insert(s->symbolName).second)
return;
size_t impSize = s->dllName.size() + s->symbolName.size() + 2; // +2 for NULs
size_t size = sizeof(coff_import_header) + impSize;
char *buf = bAlloc().Allocate<char>(size);
memset(buf, 0, size);
char *p = buf;
auto *imp = reinterpret_cast<coff_import_header *>(p);
p += sizeof(*imp);
imp->Sig2 = 0xFFFF;
imp->Machine = coffObj->getMachine();
imp->SizeOfData = impSize;
imp->OrdinalHint = 0; // Only linking by name
imp->TypeInfo = (s->nameType << 2) | s->importType;
// Write symbol name and DLL name.
memcpy(p, s->symbolName.data(), s->symbolName.size());
p += s->symbolName.size() + 1;
memcpy(p, s->dllName.data(), s->dllName.size());
MemoryBufferRef mbref = MemoryBufferRef(StringRef(buf, size), s->dllName);
ImportFile *impFile = make<ImportFile>(symtab.ctx, mbref);
symtab.ctx.driver.addFile(impFile);
}
|