171333Sitojun//===- InputFiles.cpp -----------------------------------------------------===//
278064Sume//
362656Skris// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
455505Sshin// See https://llvm.org/LICENSE.txt for license information.
555505Sshin// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6224144Shrs//
755505Sshin//===----------------------------------------------------------------------===//
8222732Shrs
955505Sshin#include "InputFiles.h"
1055505Sshin#include "COFFLinkerContext.h"
1155505Sshin#include "Chunks.h"
1255505Sshin#include "Config.h"
1355505Sshin#include "DebugTypes.h"
1455505Sshin#include "Driver.h"
1555505Sshin#include "SymbolTable.h"
1655505Sshin#include "Symbols.h"
1755505Sshin#include "lld/Common/DWARF.h"
1855505Sshin#include "llvm-c/lto.h"
1955505Sshin#include "llvm/ADT/SmallVector.h"
20222732Shrs#include "llvm/ADT/Twine.h"
2155505Sshin#include "llvm/BinaryFormat/COFF.h"
2255505Sshin#include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h"
2355505Sshin#include "llvm/DebugInfo/CodeView/SymbolDeserializer.h"
2455505Sshin#include "llvm/DebugInfo/CodeView/SymbolRecord.h"
2555505Sshin#include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
2655505Sshin#include "llvm/DebugInfo/PDB/Native/NativeSession.h"
2755505Sshin#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
2855505Sshin#include "llvm/LTO/LTO.h"
2955505Sshin#include "llvm/Object/Binary.h"
3055505Sshin#include "llvm/Object/COFF.h"
3155505Sshin#include "llvm/Support/Casting.h"
3255505Sshin#include "llvm/Support/Endian.h"
3355505Sshin#include "llvm/Support/Error.h"
3455505Sshin#include "llvm/Support/ErrorOr.h"
3555505Sshin#include "llvm/Support/FileSystem.h"
3655505Sshin#include "llvm/Support/Path.h"
3755505Sshin#include "llvm/Target/TargetOptions.h"
3855505Sshin#include "llvm/TargetParser/Triple.h"
39224144Shrs#include <cstring>
4055505Sshin#include <optional>
41224144Shrs#include <system_error>
42118787Sume#include <utility>
4355505Sshin
4455505Sshinusing namespace llvm;
45224144Shrsusing namespace llvm::COFF;
46224144Shrsusing namespace llvm::codeview;
4755505Sshinusing namespace llvm::object;
48224144Shrsusing namespace llvm::support::endian;
4955505Sshinusing namespace lld;
5055505Sshinusing namespace lld::coff;
51222732Shrs
5255505Sshinusing llvm::Triple;
5355505Sshinusing llvm::support::ulittle32_t;
5455505Sshin
55224144Shrs// Returns the last element of a path, which is supposed to be a filename.
56224144Shrsstatic StringRef getBasename(StringRef path) {
5755505Sshin  return sys::path::filename(path, sys::path::Style::windows);
5855505Sshin}
5955505Sshin
60222732Shrs// Returns a string in the format of "foo.obj" or "foo.obj(bar.lib)".
6155505Sshinstd::string lld::toString(const coff::InputFile *file) {
6255505Sshin  if (!file)
63222732Shrs    return "<internal>";
64222732Shrs  if (file->parentName.empty() || file->kind() == coff::InputFile::ImportKind)
65222732Shrs    return std::string(file->getName());
66222732Shrs
6755505Sshin  return (getBasename(file->parentName) + "(" + getBasename(file->getName()) +
68224144Shrs          ")")
69224144Shrs      .str();
70224144Shrs}
71224144Shrs
72224144Shrs/// Checks that Source is compatible with being a weak alias to Target.
7355505Sshin/// If Source is Undefined and has no weak alias set, makes it a weak
74224144Shrs/// alias to Target.
75224144Shrsstatic void checkAndSetWeakAlias(COFFLinkerContext &ctx, InputFile *f,
76224144Shrs                                 Symbol *source, Symbol *target) {
77224144Shrs  if (auto *u = dyn_cast<Undefined>(source)) {
78224144Shrs    if (u->weakAlias && u->weakAlias != target) {
7955505Sshin      // Weak aliases as produced by GCC are named in the form
80224144Shrs      // .weak.<weaksymbol>.<othersymbol>, where <othersymbol> is the name
81224144Shrs      // of another symbol emitted near the weak symbol.
82224144Shrs      // Just use the definition from the first object file that defined
83224144Shrs      // this weak symbol.
84224144Shrs      if (ctx.config.allowDuplicateWeak)
85224144Shrs        return;
86224144Shrs      ctx.symtab.reportDuplicate(source, f);
87224144Shrs    }
88224144Shrs    u->weakAlias = target;
89224144Shrs  }
90224144Shrs}
91224144Shrs
92224144Shrsstatic bool ignoredSymbolName(StringRef name) {
93224144Shrs  return name == "@feat.00" || name == "@comp.id";
94224144Shrs}
95224144Shrs
96224144ShrsArchiveFile::ArchiveFile(COFFLinkerContext &ctx, MemoryBufferRef m)
9755505Sshin    : InputFile(ctx, ArchiveKind, m) {}
9855505Sshin
9955505Sshinvoid ArchiveFile::parse() {
10055505Sshin  // Parse a MemoryBufferRef as an archive file.
101222732Shrs  file = CHECK(Archive::create(mb), this);
10255505Sshin
10355505Sshin  // Read the symbol table to construct Lazy objects.
10455505Sshin  for (const Archive::Symbol &sym : file->symbols())
10555505Sshin    ctx.symtab.addLazyArchive(this, sym);
10655505Sshin}
10755505Sshin
10855505Sshin// Returns a buffer pointing to a member file containing a given symbol.
10955505Sshinvoid ArchiveFile::addMember(const Archive::Symbol &sym) {
11055505Sshin  const Archive::Child &c =
11155505Sshin      CHECK(sym.getMember(),
11262656Skris            "could not get the member for symbol " + toCOFFString(ctx, sym));
11355505Sshin
11455505Sshin  // Return an empty buffer if we have already returned the same buffer.
11555505Sshin  if (!seen.insert(c.getChildOffset()).second)
116118664Sume    return;
117118664Sume
118308717Shrs  ctx.driver.enqueueArchiveMember(c, sym, getName());
119308717Shrs}
120222732Shrs
121118664Sumestd::vector<MemoryBufferRef> lld::coff::getArchiveMembers(Archive *file) {
122222732Shrs  std::vector<MemoryBufferRef> v;
12355505Sshin  Error err = Error::success();
12455505Sshin  for (const Archive::Child &c : file->children(err)) {
12555505Sshin    MemoryBufferRef mbref =
12655505Sshin        CHECK(c.getMemoryBufferRef(),
12755505Sshin              file->getFileName() +
12855505Sshin                  ": could not get the buffer for a child of the archive");
12955505Sshin    v.push_back(mbref);
13055505Sshin  }
13155505Sshin  if (err)
13255505Sshin    fatal(file->getFileName() +
133118664Sume          ": Archive::children failed: " + toString(std::move(err)));
134118664Sume  return v;
135308717Shrs}
136308717Shrs
137118664Sumevoid ObjFile::parseLazy() {
138118664Sume  // Native object file.
139118664Sume  std::unique_ptr<Binary> coffObjPtr = CHECK(createBinary(mb), this);
140118664Sume  COFFObjectFile *coffObj = cast<COFFObjectFile>(coffObjPtr.get());
141118664Sume  uint32_t numSymbols = coffObj->getNumberOfSymbols();
142118664Sume  for (uint32_t i = 0; i < numSymbols; ++i) {
143118664Sume    COFFSymbolRef coffSym = check(coffObj->getSymbol(i));
144118664Sume    if (coffSym.isUndefined() || !coffSym.isExternal() ||
14555505Sshin        coffSym.isWeakExternal())
14655505Sshin      continue;
14755505Sshin    StringRef name = check(coffObj->getSymbolName(coffSym));
14855505Sshin    if (coffSym.isAbsolute() && ignoredSymbolName(name))
14955505Sshin      continue;
15055505Sshin    ctx.symtab.addLazyObject(this, name);
151222732Shrs    i += coffSym.getNumberOfAuxSymbols();
15255505Sshin  }
15355505Sshin}
15455505Sshin
15555505Sshinvoid ObjFile::parse() {
15655505Sshin  // Parse a memory buffer as a COFF file.
157222732Shrs  std::unique_ptr<Binary> bin = CHECK(createBinary(mb), this);
15855505Sshin
159222732Shrs  if (auto *obj = dyn_cast<COFFObjectFile>(bin.get())) {
16055505Sshin    bin.release();
16155505Sshin    coffObj.reset(obj);
16262656Skris  } else {
16362656Skris    fatal(toString(this) + " is not a COFF file");
16462656Skris  }
16555505Sshin
16655505Sshin  // Read section and symbol tables.
16755505Sshin  initializeChunks();
16855505Sshin  initializeSymbols();
16955505Sshin  initializeFlags();
17055505Sshin  initializeDependencies();
17155505Sshin}
17255505Sshin
17355505Sshinconst coff_section *ObjFile::getSection(uint32_t i) {
17455505Sshin  auto sec = coffObj->getSection(i);
17555505Sshin  if (!sec)
17655505Sshin    fatal("getSection failed: #" + Twine(i) + ": " + toString(sec.takeError()));
17755505Sshin  return *sec;
17855505Sshin}
179222732Shrs
180222732Shrs// We set SectionChunk pointers in the SparseChunks vector to this value
18155505Sshin// temporarily to mark comdat sections as having an unknown resolution. As we
18255505Sshin// walk the object file's symbol table, once we visit either a leader symbol or
183222732Shrs// an associative section definition together with the parent comdat's leader,
184222732Shrs// we set the pointer to either nullptr (to mark the section as discarded) or a
185222732Shrs// valid SectionChunk for that section.
186222732Shrsstatic SectionChunk *const pendingComdat = reinterpret_cast<SectionChunk *>(1);
187222732Shrs
18855505Sshinvoid ObjFile::initializeChunks() {
18955505Sshin  uint32_t numSections = coffObj->getNumberOfSections();
19055505Sshin  sparseChunks.resize(numSections + 1);
191222732Shrs  for (uint32_t i = 1; i < numSections + 1; ++i) {
192222732Shrs    const coff_section *sec = getSection(i);
193222732Shrs    if (sec->Characteristics & IMAGE_SCN_LNK_COMDAT)
19455505Sshin      sparseChunks[i] = pendingComdat;
19555505Sshin    else
19655505Sshin      sparseChunks[i] = readSection(i, nullptr, "");
19755505Sshin  }
19855505Sshin}
19955505Sshin
20055505SshinSectionChunk *ObjFile::readSection(uint32_t sectionNumber,
20155505Sshin                                   const coff_aux_section_definition *def,
20255505Sshin                                   StringRef leaderName) {
20355505Sshin  const coff_section *sec = getSection(sectionNumber);
20455505Sshin
20555505Sshin  StringRef name;
20655505Sshin  if (Expected<StringRef> e = coffObj->getSectionName(sec))
20755505Sshin    name = *e;
20855505Sshin  else
20955505Sshin    fatal("getSectionName failed: #" + Twine(sectionNumber) + ": " +
21055505Sshin          toString(e.takeError()));
21155505Sshin
21255505Sshin  if (name == ".drectve") {
21355505Sshin    ArrayRef<uint8_t> data;
21455505Sshin    cantFail(coffObj->getSectionContents(sec, data));
21555505Sshin    directives = StringRef((const char *)data.data(), data.size());
21655505Sshin    return nullptr;
21755505Sshin  }
21855505Sshin
21955505Sshin  if (name == ".llvm_addrsig") {
22055505Sshin    addrsigSec = sec;
22155505Sshin    return nullptr;
22255505Sshin  }
22355505Sshin
22455505Sshin  if (name == ".llvm.call-graph-profile") {
22555505Sshin    callgraphSec = sec;
22655505Sshin    return nullptr;
22755505Sshin  }
22855505Sshin
22955505Sshin  // Object files may have DWARF debug info or MS CodeView debug info
23055505Sshin  // (or both).
23155505Sshin  //
23255505Sshin  // DWARF sections don't need any special handling from the perspective
23355505Sshin  // of the linker; they are just a data section containing relocations.
23455505Sshin  // We can just link them to complete debug info.
23555505Sshin  //
23655505Sshin  // CodeView needs linker support. We need to interpret debug info,
23755505Sshin  // and then write it to a separate .pdb file.
23855505Sshin
23955505Sshin  // Ignore DWARF debug info unless requested to be included.
24055505Sshin  if (!ctx.config.includeDwarfChunks && name.starts_with(".debug_"))
24155505Sshin    return nullptr;
24255505Sshin
24355505Sshin  if (sec->Characteristics & llvm::COFF::IMAGE_SCN_LNK_REMOVE)
24455505Sshin    return nullptr;
24555505Sshin  auto *c = make<SectionChunk>(this, sec);
24655505Sshin  if (def)
247222732Shrs    c->checksum = def->CheckSum;
24855505Sshin
24955505Sshin  // CodeView sections are stored to a different vector because they are not
25055505Sshin  // linked in the regular manner.
25155505Sshin  if (c->isCodeView())
25255505Sshin    debugChunks.push_back(c);
25355505Sshin  else if (name == ".gfids$y")
25455505Sshin    guardFidChunks.push_back(c);
255222732Shrs  else if (name == ".giats$y")
25655505Sshin    guardIATChunks.push_back(c);
25778064Sume  else if (name == ".gljmp$y")
25855505Sshin    guardLJmpChunks.push_back(c);
25955505Sshin  else if (name == ".gehcont$y")
26055505Sshin    guardEHContChunks.push_back(c);
26155505Sshin  else if (name == ".sxdata")
26255505Sshin    sxDataChunks.push_back(c);
26355505Sshin  else if (ctx.config.tailMerge && sec->NumberOfRelocations == 0 &&
26455505Sshin           name == ".rdata" && leaderName.starts_with("??_C@"))
26555505Sshin    // COFF sections that look like string literal sections (i.e. no
26655505Sshin    // relocations, in .rdata, leader symbol name matches the MSVC name mangling
26755505Sshin    // for string literals) are subject to string tail merging.
268222732Shrs    MergeChunk::addSection(ctx, c);
26955505Sshin  else if (name == ".rsrc" || name.starts_with(".rsrc$"))
27055505Sshin    resourceChunks.push_back(c);
27155505Sshin  else
27255505Sshin    chunks.push_back(c);
27355505Sshin
27455505Sshin  return c;
27555505Sshin}
27655505Sshin
27755505Sshinvoid ObjFile::includeResourceChunks() {
27855505Sshin  chunks.insert(chunks.end(), resourceChunks.begin(), resourceChunks.end());
27955505Sshin}
280222732Shrs
28155505Sshinvoid ObjFile::readAssociativeDefinition(
28255505Sshin    COFFSymbolRef sym, const coff_aux_section_definition *def) {
28355505Sshin  readAssociativeDefinition(sym, def, def->getNumber(sym.isBigObj()));
28455505Sshin}
28555505Sshin
28655505Sshinvoid ObjFile::readAssociativeDefinition(COFFSymbolRef sym,
28755505Sshin                                        const coff_aux_section_definition *def,
288224144Shrs                                        uint32_t parentIndex) {
289222732Shrs  SectionChunk *parent = sparseChunks[parentIndex];
29055505Sshin  int32_t sectionNumber = sym.getSectionNumber();
29155505Sshin
29255505Sshin  auto diag = [&]() {
29355505Sshin    StringRef name = check(coffObj->getSymbolName(sym));
294224144Shrs
295224144Shrs    StringRef parentName;
29667801Sume    const coff_section *parentSec = getSection(parentIndex);
29767801Sume    if (Expected<StringRef> e = coffObj->getSectionName(parentSec))
29867801Sume      parentName = *e;
29967801Sume    error(toString(this) + ": associative comdat " + name + " (sec " +
300224144Shrs          Twine(sectionNumber) + ") has invalid reference to section " +
30167801Sume          parentName + " (sec " + Twine(parentIndex) + ")");
30267801Sume  };
30367801Sume
30455505Sshin  if (parent == pendingComdat) {
30555505Sshin    // This can happen if an associative comdat refers to another associative
30655505Sshin    // comdat that appears after it (invalid per COFF spec) or to a section
30755505Sshin    // without any symbols.
30855505Sshin    diag();
30955505Sshin    return;
31055505Sshin  }
31155505Sshin
31255505Sshin  // Check whether the parent is prevailing. If it is, so are we, and we read
31355505Sshin  // the section; otherwise mark it as discarded.
31455505Sshin  if (parent) {
31555505Sshin    SectionChunk *c = readSection(sectionNumber, def, "");
31655505Sshin    sparseChunks[sectionNumber] = c;
31755505Sshin    if (c) {
31855505Sshin      c->selection = IMAGE_COMDAT_SELECT_ASSOCIATIVE;
31955505Sshin      parent->addAssociative(c);
32055505Sshin    }
32155505Sshin  } else {
32255505Sshin    sparseChunks[sectionNumber] = nullptr;
32355505Sshin  }
32455505Sshin}
32555505Sshin
32655505Sshinvoid ObjFile::recordPrevailingSymbolForMingw(
32755505Sshin    COFFSymbolRef sym, DenseMap<StringRef, uint32_t> &prevailingSectionMap) {
32855505Sshin  // For comdat symbols in executable sections, where this is the copy
32955505Sshin  // of the section chunk we actually include instead of discarding it,
33055505Sshin  // add the symbol to a map to allow using it for implicitly
33155505Sshin  // associating .[px]data$<func> sections to it.
33255505Sshin  // Use the suffix from the .text$<func> instead of the leader symbol
333222732Shrs  // name, for cases where the names differ (i386 mangling/decorations,
33455505Sshin  // cases where the leader is a weak symbol named .weak.func.default*).
33555505Sshin  int32_t sectionNumber = sym.getSectionNumber();
33655505Sshin  SectionChunk *sc = sparseChunks[sectionNumber];
337222732Shrs  if (sc && sc->getOutputCharacteristics() & IMAGE_SCN_MEM_EXECUTE) {
33855505Sshin    StringRef name = sc->getSectionName().split('$').second;
33955505Sshin    prevailingSectionMap[name] = sectionNumber;
340224144Shrs  }
341224144Shrs}
34255505Sshin
343224144Shrsvoid ObjFile::maybeAssociateSEHForMingw(
344224144Shrs    COFFSymbolRef sym, const coff_aux_section_definition *def,
34555505Sshin    const DenseMap<StringRef, uint32_t> &prevailingSectionMap) {
346224144Shrs  StringRef name = check(coffObj->getSymbolName(sym));
347224144Shrs  if (name.consume_front(".pdata$") || name.consume_front(".xdata$") ||
348224144Shrs      name.consume_front(".eh_frame$")) {
349224144Shrs    // For MinGW, treat .[px]data$<func> and .eh_frame$<func> as implicitly
350224144Shrs    // associative to the symbol <func>.
351224144Shrs    auto parentSym = prevailingSectionMap.find(name);
352224144Shrs    if (parentSym != prevailingSectionMap.end())
353224144Shrs      readAssociativeDefinition(sym, def, parentSym->second);
354224144Shrs  }
355224144Shrs}
356224144Shrs
357224144ShrsSymbol *ObjFile::createRegular(COFFSymbolRef sym) {
35855505Sshin  SectionChunk *sc = sparseChunks[sym.getSectionNumber()];
359224144Shrs  if (sym.isExternal()) {
360224144Shrs    StringRef name = check(coffObj->getSymbolName(sym));
361224144Shrs    if (sc)
362224144Shrs      return ctx.symtab.addRegular(this, name, sym.getGeneric(), sc,
36355505Sshin                                   sym.getValue());
364224144Shrs    // For MinGW symbols named .weak.* that point to a discarded section,
365224144Shrs    // don't create an Undefined symbol. If nothing ever refers to the symbol,
366290853Sdelphij    // everything should be fine. If something actually refers to the symbol
367224144Shrs    // (e.g. the undefined weak alias), linking will fail due to undefined
368224144Shrs    // references at the end.
369224144Shrs    if (ctx.config.mingw && name.starts_with(".weak."))
370224144Shrs      return nullptr;
371224144Shrs    return ctx.symtab.addUndefined(name, this, false);
372224144Shrs  }
373224144Shrs  if (sc)
374224144Shrs    return make<DefinedRegular>(this, /*Name*/ "", /*IsCOMDAT*/ false,
375224144Shrs                                /*IsExternal*/ false, sym.getGeneric(), sc);
376224144Shrs  return nullptr;
377224144Shrs}
378224144Shrs
37955505Sshinvoid ObjFile::initializeSymbols() {
38055505Sshin  uint32_t numSymbols = coffObj->getNumberOfSymbols();
38155505Sshin  symbols.resize(numSymbols);
382224144Shrs
38355505Sshin  SmallVector<std::pair<Symbol *, uint32_t>, 8> weakAliases;
384224144Shrs  std::vector<uint32_t> pendingIndexes;
385224144Shrs  pendingIndexes.reserve(numSymbols);
386224144Shrs
38755505Sshin  DenseMap<StringRef, uint32_t> prevailingSectionMap;
388224144Shrs  std::vector<const coff_aux_section_definition *> comdatDefs(
389224144Shrs      coffObj->getNumberOfSections() + 1);
390224144Shrs
391224144Shrs  for (uint32_t i = 0; i < numSymbols; ++i) {
392224144Shrs    COFFSymbolRef coffSym = check(coffObj->getSymbol(i));
393224144Shrs    bool prevailingComdat;
394224144Shrs    if (coffSym.isUndefined()) {
395300281Struckman      symbols[i] = createUndefined(coffSym);
396224144Shrs    } else if (coffSym.isWeakExternal()) {
397224144Shrs      symbols[i] = createUndefined(coffSym);
398224144Shrs      uint32_t tagIndex = coffSym.getAux<coff_aux_weak_external>()->TagIndex;
399224144Shrs      weakAliases.emplace_back(symbols[i], tagIndex);
400255156Shrs    } else if (std::optional<Symbol *> optSym =
401255156Shrs                   createDefined(coffSym, comdatDefs, prevailingComdat)) {
402224144Shrs      symbols[i] = *optSym;
403224144Shrs      if (ctx.config.mingw && prevailingComdat)
404224144Shrs        recordPrevailingSymbolForMingw(coffSym, prevailingSectionMap);
405224144Shrs    } else {
406224144Shrs      // createDefined() returns std::nullopt if a symbol belongs to a section
407224144Shrs      // that was pending at the point when the symbol was read. This can happen
40855505Sshin      // in two cases:
40955505Sshin      // 1) section definition symbol for a comdat leader;
410224144Shrs      // 2) symbol belongs to a comdat section associated with another section.
411224144Shrs      // In both of these cases, we can expect the section to be resolved by
41255505Sshin      // the time we finish visiting the remaining symbols in the symbol
413224144Shrs      // table. So we postpone the handling of this symbol until that time.
414224144Shrs      pendingIndexes.push_back(i);
415224144Shrs    }
416224144Shrs    i += coffSym.getNumberOfAuxSymbols();
417224144Shrs  }
418224144Shrs
419224144Shrs  for (uint32_t i : pendingIndexes) {
420224144Shrs    COFFSymbolRef sym = check(coffObj->getSymbol(i));
421224144Shrs    if (const coff_aux_section_definition *def = sym.getSectionDefinition()) {
42255505Sshin      if (def->Selection == IMAGE_COMDAT_SELECT_ASSOCIATIVE)
423224144Shrs        readAssociativeDefinition(sym, def);
42455505Sshin      else if (ctx.config.mingw)
425224144Shrs        maybeAssociateSEHForMingw(sym, def, prevailingSectionMap);
426224144Shrs    }
427224144Shrs    if (sparseChunks[sym.getSectionNumber()] == pendingComdat) {
428224144Shrs      StringRef name = check(coffObj->getSymbolName(sym));
42955505Sshin      log("comdat section " + name +
43055505Sshin          " without leader and unassociated, discarding");
431224144Shrs      continue;
432118660Sume    }
43355505Sshin    symbols[i] = createRegular(sym);
43455505Sshin  }
435224144Shrs
436224144Shrs  for (auto &kv : weakAliases) {
437224144Shrs    Symbol *sym = kv.first;
438224144Shrs    uint32_t idx = kv.second;
43955505Sshin    checkAndSetWeakAlias(ctx, this, sym, symbols[idx]);
44055505Sshin  }
44155505Sshin
442224144Shrs  // Free the memory used by sparseChunks now that symbol loading is finished.
443224144Shrs  decltype(sparseChunks)().swap(sparseChunks);
444224144Shrs}
445224144Shrs
446224144ShrsSymbol *ObjFile::createUndefined(COFFSymbolRef sym) {
44755505Sshin  StringRef name = check(coffObj->getSymbolName(sym));
448224144Shrs  return ctx.symtab.addUndefined(name, this, sym.isWeakExternal());
449224144Shrs}
45055505Sshin
451224144Shrsstatic const coff_aux_section_definition *findSectionDef(COFFObjectFile *obj,
452224144Shrs                                                         int32_t section) {
453224144Shrs  uint32_t numSymbols = obj->getNumberOfSymbols();
454224144Shrs  for (uint32_t i = 0; i < numSymbols; ++i) {
455224144Shrs    COFFSymbolRef sym = check(obj->getSymbol(i));
45655505Sshin    if (sym.getSectionNumber() != section)
457224144Shrs      continue;
458224144Shrs    if (const coff_aux_section_definition *def = sym.getSectionDefinition())
459224144Shrs      return def;
460224144Shrs  }
46155505Sshin  return nullptr;
46255505Sshin}
463224144Shrs
46455505Sshinvoid ObjFile::handleComdatSelection(
465224144Shrs    COFFSymbolRef sym, COMDATType &selection, bool &prevailing,
466224144Shrs    DefinedRegular *leader,
467224144Shrs    const llvm::object::coff_aux_section_definition *def) {
468224144Shrs  if (prevailing)
469224144Shrs    return;
470224144Shrs  // There's already an existing comdat for this symbol: `Leader`.
471224144Shrs  // Use the comdats's selection field to determine if the new
472224144Shrs  // symbol in `Sym` should be discarded, produce a duplicate symbol
473224144Shrs  // error, etc.
474224144Shrs
475224144Shrs  SectionChunk *leaderChunk = leader->getChunk();
476224144Shrs  COMDATType leaderSelection = leaderChunk->selection;
477224144Shrs
478224144Shrs  assert(leader->data && "Comdat leader without SectionChunk?");
479224144Shrs  if (isa<BitcodeFile>(leader->file)) {
480224144Shrs    // If the leader is only a LTO symbol, we don't know e.g. its final size
481224144Shrs    // yet, so we can't do the full strict comdat selection checking yet.
482224144Shrs    selection = leaderSelection = IMAGE_COMDAT_SELECT_ANY;
483224144Shrs  }
484224144Shrs
485224144Shrs  if ((selection == IMAGE_COMDAT_SELECT_ANY &&
486224144Shrs       leaderSelection == IMAGE_COMDAT_SELECT_LARGEST) ||
487224144Shrs      (selection == IMAGE_COMDAT_SELECT_LARGEST &&
488224144Shrs       leaderSelection == IMAGE_COMDAT_SELECT_ANY)) {
489224144Shrs    // cl.exe picks "any" for vftables when building with /GR- and
490224144Shrs    // "largest" when building with /GR. To be able to link object files
491224144Shrs    // compiled with each flag, "any" and "largest" are merged as "largest".
492224144Shrs    leaderSelection = selection = IMAGE_COMDAT_SELECT_LARGEST;
493224144Shrs  }
494224144Shrs
495224144Shrs  // GCCs __declspec(selectany) doesn't actually pick "any" but "same size as".
496224144Shrs  // Clang on the other hand picks "any". To be able to link two object files
497224144Shrs  // with a __declspec(selectany) declaration, one compiled with gcc and the
498224144Shrs  // other with clang, we merge them as proper "same size as"
499224144Shrs  if (ctx.config.mingw && ((selection == IMAGE_COMDAT_SELECT_ANY &&
500224144Shrs                            leaderSelection == IMAGE_COMDAT_SELECT_SAME_SIZE) ||
501224144Shrs                           (selection == IMAGE_COMDAT_SELECT_SAME_SIZE &&
502224144Shrs                            leaderSelection == IMAGE_COMDAT_SELECT_ANY))) {
503224144Shrs    leaderSelection = selection = IMAGE_COMDAT_SELECT_SAME_SIZE;
504224144Shrs  }
505224144Shrs
506224144Shrs  // Other than that, comdat selections must match.  This is a bit more
507224144Shrs  // strict than link.exe which allows merging "any" and "largest" if "any"
508224144Shrs  // is the first symbol the linker sees, and it allows merging "largest"
509224144Shrs  // with everything (!) if "largest" is the first symbol the linker sees.
510224144Shrs  // Making this symmetric independent of which selection is seen first
511224144Shrs  // seems better though.
512224144Shrs  // (This behavior matches ModuleLinker::getComdatResult().)
513224144Shrs  if (selection != leaderSelection) {
514224144Shrs    log(("conflicting comdat type for " + toString(ctx, *leader) + ": " +
515224144Shrs         Twine((int)leaderSelection) + " in " + toString(leader->getFile()) +
516224144Shrs         " and " + Twine((int)selection) + " in " + toString(this))
517224144Shrs            .str());
518224144Shrs    ctx.symtab.reportDuplicate(leader, this);
519224144Shrs    return;
520224144Shrs  }
521224144Shrs
522224144Shrs  switch (selection) {
523224144Shrs  case IMAGE_COMDAT_SELECT_NODUPLICATES:
524300281Struckman    ctx.symtab.reportDuplicate(leader, this);
525224144Shrs    break;
526224144Shrs
527224144Shrs  case IMAGE_COMDAT_SELECT_ANY:
528224144Shrs    // Nothing to do.
529224144Shrs    break;
530224144Shrs
531224144Shrs  case IMAGE_COMDAT_SELECT_SAME_SIZE:
532224144Shrs    if (leaderChunk->getSize() != getSection(sym)->SizeOfRawData) {
533224144Shrs      if (!ctx.config.mingw) {
534224144Shrs        ctx.symtab.reportDuplicate(leader, this);
535224144Shrs      } else {
536224144Shrs        const coff_aux_section_definition *leaderDef = nullptr;
537224144Shrs        if (leaderChunk->file)
538224144Shrs          leaderDef = findSectionDef(leaderChunk->file->getCOFFObj(),
539224144Shrs                                     leaderChunk->getSectionNumber());
540224144Shrs        if (!leaderDef || leaderDef->Length != def->Length)
541224144Shrs          ctx.symtab.reportDuplicate(leader, this);
542224144Shrs      }
543224144Shrs    }
544224144Shrs    break;
545224144Shrs
546224144Shrs  case IMAGE_COMDAT_SELECT_EXACT_MATCH: {
547224144Shrs    SectionChunk newChunk(this, getSection(sym));
548224144Shrs    // link.exe only compares section contents here and doesn't complain
549224144Shrs    // if the two comdat sections have e.g. different alignment.
550224144Shrs    // Match that.
551224144Shrs    if (leaderChunk->getContents() != newChunk.getContents())
552224144Shrs      ctx.symtab.reportDuplicate(leader, this, &newChunk, sym.getValue());
553224144Shrs    break;
554224144Shrs  }
555224144Shrs
556224144Shrs  case IMAGE_COMDAT_SELECT_ASSOCIATIVE:
557224144Shrs    // createDefined() is never called for IMAGE_COMDAT_SELECT_ASSOCIATIVE.
558224144Shrs    // (This means lld-link doesn't produce duplicate symbol errors for
559224144Shrs    // associative comdats while link.exe does, but associate comdats
560224144Shrs    // are never extern in practice.)
561224144Shrs    llvm_unreachable("createDefined not called for associative comdats");
562224144Shrs
563224144Shrs  case IMAGE_COMDAT_SELECT_LARGEST:
564224144Shrs    if (leaderChunk->getSize() < getSection(sym)->SizeOfRawData) {
565224144Shrs      // Replace the existing comdat symbol with the new one.
566224144Shrs      StringRef name = check(coffObj->getSymbolName(sym));
567224144Shrs      // FIXME: This is incorrect: With /opt:noref, the previous sections
568224144Shrs      // make it into the final executable as well. Correct handling would
569224144Shrs      // be to undo reading of the whole old section that's being replaced,
570224144Shrs      // or doing one pass that determines what the final largest comdat
571224144Shrs      // is for all IMAGE_COMDAT_SELECT_LARGEST comdats and then reading
572224144Shrs      // only the largest one.
573224144Shrs      replaceSymbol<DefinedRegular>(leader, this, name, /*IsCOMDAT*/ true,
57455505Sshin                                    /*IsExternal*/ true, sym.getGeneric(),
575224144Shrs                                    nullptr);
576224144Shrs      prevailing = true;
577224144Shrs    }
578224144Shrs    break;
579224144Shrs
58055505Sshin  case IMAGE_COMDAT_SELECT_NEWEST:
581224144Shrs    llvm_unreachable("should have been rejected earlier");
582224144Shrs  }
583224144Shrs}
584224144Shrs
585224144Shrsstd::optional<Symbol *> ObjFile::createDefined(
586224144Shrs    COFFSymbolRef sym,
58755505Sshin    std::vector<const coff_aux_section_definition *> &comdatDefs,
58855505Sshin    bool &prevailing) {
589224144Shrs  prevailing = false;
590224144Shrs  auto getName = [&]() { return check(coffObj->getSymbolName(sym)); };
591224144Shrs
592224144Shrs  if (sym.isCommon()) {
593224144Shrs    auto *c = make<CommonChunk>(sym);
594224144Shrs    chunks.push_back(c);
595224144Shrs    return ctx.symtab.addCommon(this, getName(), sym.getValue(),
596224144Shrs                                sym.getGeneric(), c);
597224144Shrs  }
59855505Sshin
59955505Sshin  if (sym.isAbsolute()) {
600224144Shrs    StringRef name = getName();
601224144Shrs
60255505Sshin    if (name == "@feat.00")
603224144Shrs      feat00Flags = sym.getValue();
604224144Shrs    // Skip special symbols.
605224144Shrs    if (ignoredSymbolName(name))
606224144Shrs      return nullptr;
607224144Shrs
608224144Shrs    if (sym.isExternal())
609224144Shrs      return ctx.symtab.addAbsolute(name, sym);
610224144Shrs    return make<DefinedAbsolute>(ctx, name, sym);
611224144Shrs  }
612224144Shrs
613224144Shrs  int32_t sectionNumber = sym.getSectionNumber();
614224144Shrs  if (sectionNumber == llvm::COFF::IMAGE_SYM_DEBUG)
615224144Shrs    return nullptr;
616224144Shrs
617224144Shrs  if (llvm::COFF::isReservedSectionNumber(sectionNumber))
618224144Shrs    fatal(toString(this) + ": " + getName() +
619224144Shrs          " should not refer to special section " + Twine(sectionNumber));
620224144Shrs
621224144Shrs  if ((uint32_t)sectionNumber >= sparseChunks.size())
622224144Shrs    fatal(toString(this) + ": " + getName() +
623224144Shrs          " should not refer to non-existent section " + Twine(sectionNumber));
624224144Shrs
625224144Shrs  // Comdat handling.
626224144Shrs  // A comdat symbol consists of two symbol table entries.
627224144Shrs  // The first symbol entry has the name of the section (e.g. .text), fixed
628224144Shrs  // values for the other fields, and one auxiliary record.
629224144Shrs  // The second symbol entry has the name of the comdat symbol, called the
630224144Shrs  // "comdat leader".
631224144Shrs  // When this function is called for the first symbol entry of a comdat,
632224144Shrs  // it sets comdatDefs and returns std::nullopt, and when it's called for the
633224144Shrs  // second symbol entry it reads comdatDefs and then sets it back to nullptr.
634224144Shrs
635224144Shrs  // Handle comdat leader.
636224144Shrs  if (const coff_aux_section_definition *def = comdatDefs[sectionNumber]) {
637224144Shrs    comdatDefs[sectionNumber] = nullptr;
638224144Shrs    DefinedRegular *leader;
639224144Shrs
640224144Shrs    if (sym.isExternal()) {
641224144Shrs      std::tie(leader, prevailing) =
642224144Shrs          ctx.symtab.addComdat(this, getName(), sym.getGeneric());
643224144Shrs    } else {
644224144Shrs      leader = make<DefinedRegular>(this, /*Name*/ "", /*IsCOMDAT*/ false,
645224144Shrs                                    /*IsExternal*/ false, sym.getGeneric());
646224144Shrs      prevailing = true;
647224144Shrs    }
648224144Shrs
649224144Shrs    if (def->Selection < (int)IMAGE_COMDAT_SELECT_NODUPLICATES ||
650224144Shrs        // Intentionally ends at IMAGE_COMDAT_SELECT_LARGEST: link.exe
651224144Shrs        // doesn't understand IMAGE_COMDAT_SELECT_NEWEST either.
652224144Shrs        def->Selection > (int)IMAGE_COMDAT_SELECT_LARGEST) {
653224144Shrs      fatal("unknown comdat type " + std::to_string((int)def->Selection) +
654224144Shrs            " for " + getName() + " in " + toString(this));
655224144Shrs    }
656224144Shrs    COMDATType selection = (COMDATType)def->Selection;
657224144Shrs
658224144Shrs    if (leader->isCOMDAT)
659224144Shrs      handleComdatSelection(sym, selection, prevailing, leader, def);
660224144Shrs
661224144Shrs    if (prevailing) {
662224144Shrs      SectionChunk *c = readSection(sectionNumber, def, getName());
663224144Shrs      sparseChunks[sectionNumber] = c;
664224144Shrs      if (!c)
665224144Shrs        return nullptr;
666224144Shrs      c->sym = cast<DefinedRegular>(leader);
667224144Shrs      c->selection = selection;
668224144Shrs      cast<DefinedRegular>(leader)->data = &c->repl;
669224144Shrs    } else {
670224144Shrs      sparseChunks[sectionNumber] = nullptr;
671222732Shrs    }
672224144Shrs    return leader;
673224144Shrs  }
674222732Shrs
675224144Shrs  // Prepare to handle the comdat leader symbol by setting the section's
676224144Shrs  // ComdatDefs pointer if we encounter a non-associative comdat.
677224144Shrs  if (sparseChunks[sectionNumber] == pendingComdat) {
678224144Shrs    if (const coff_aux_section_definition *def = sym.getSectionDefinition()) {
679224144Shrs      if (def->Selection != IMAGE_COMDAT_SELECT_ASSOCIATIVE)
680224144Shrs        comdatDefs[sectionNumber] = def;
681224144Shrs    }
682224144Shrs    return std::nullopt;
683224144Shrs  }
684224144Shrs
685224144Shrs  return createRegular(sym);
686224144Shrs}
687224144Shrs
688224144ShrsMachineTypes ObjFile::getMachineType() {
689224144Shrs  if (coffObj)
690224144Shrs    return static_cast<MachineTypes>(coffObj->getMachine());
691224144Shrs  return IMAGE_FILE_MACHINE_UNKNOWN;
692224144Shrs}
693224144Shrs
694224144ShrsArrayRef<uint8_t> ObjFile::getDebugSection(StringRef secName) {
695224144Shrs  if (SectionChunk *sec = SectionChunk::findByName(debugChunks, secName))
696224144Shrs    return sec->consumeDebugMagic();
697224144Shrs  return {};
698224144Shrs}
699224144Shrs
700224144Shrs// OBJ files systematically store critical information in a .debug$S stream,
701224144Shrs// even if the TU was compiled with no debug info. At least two records are
702224144Shrs// always there. S_OBJNAME stores a 32-bit signature, which is loaded into the
703224144Shrs// PCHSignature member. S_COMPILE3 stores compile-time cmd-line flags. This is
704224144Shrs// currently used to initialize the hotPatchable member.
705224144Shrsvoid ObjFile::initializeFlags() {
706224144Shrs  ArrayRef<uint8_t> data = getDebugSection(".debug$S");
707224144Shrs  if (data.empty())
70855505Sshin    return;
709224144Shrs
710224144Shrs  DebugSubsectionArray subsections;
711224144Shrs
71255505Sshin  BinaryStreamReader reader(data, llvm::endianness::little);
713224144Shrs  ExitOnError exitOnErr;
71455505Sshin  exitOnErr(reader.readArray(subsections, data.size()));
715224144Shrs
716224144Shrs  for (const DebugSubsectionRecord &ss : subsections) {
717224144Shrs    if (ss.kind() != DebugSubsectionKind::Symbols)
718224144Shrs      continue;
719224144Shrs
720224144Shrs    unsigned offset = 0;
721224144Shrs
722224144Shrs    // Only parse the first two records. We are only looking for S_OBJNAME
723224144Shrs    // and S_COMPILE3, and they usually appear at the beginning of the
724224144Shrs    // stream.
725224144Shrs    for (unsigned i = 0; i < 2; ++i) {
726224144Shrs      Expected<CVSymbol> sym = readSymbolFromStream(ss.getRecordData(), offset);
727224144Shrs      if (!sym) {
728224144Shrs        consumeError(sym.takeError());
729224144Shrs        return;
730224144Shrs      }
731224144Shrs      if (sym->kind() == SymbolKind::S_COMPILE3) {
732224144Shrs        auto cs =
733224144Shrs            cantFail(SymbolDeserializer::deserializeAs<Compile3Sym>(sym.get()));
734224144Shrs        hotPatchable =
735224144Shrs            (cs.Flags & CompileSym3Flags::HotPatch) != CompileSym3Flags::None;
736224144Shrs      }
737224144Shrs      if (sym->kind() == SymbolKind::S_OBJNAME) {
738224144Shrs        auto objName = cantFail(SymbolDeserializer::deserializeAs<ObjNameSym>(
739224144Shrs            sym.get()));
740224144Shrs        if (objName.Signature)
741224144Shrs          pchSignature = objName.Signature;
742224144Shrs      }
743224144Shrs      offset += sym->length();
744224144Shrs    }
745224144Shrs  }
746224144Shrs}
747224144Shrs
748224144Shrs// Depending on the compilation flags, OBJs can refer to external files,
749224144Shrs// necessary to merge this OBJ into the final PDB. We currently support two
750224144Shrs// types of external files: Precomp/PCH OBJs, when compiling with /Yc and /Yu.
751224144Shrs// And PDB type servers, when compiling with /Zi. This function extracts these
752224144Shrs// dependencies and makes them available as a TpiSource interface (see
753224144Shrs// DebugTypes.h). Both cases only happen with cl.exe: clang-cl produces regular
754// output even with /Yc and /Yu and with /Zi.
755void ObjFile::initializeDependencies() {
756  if (!ctx.config.debug)
757    return;
758
759  bool isPCH = false;
760
761  ArrayRef<uint8_t> data = getDebugSection(".debug$P");
762  if (!data.empty())
763    isPCH = true;
764  else
765    data = getDebugSection(".debug$T");
766
767  // symbols but no types, make a plain, empty TpiSource anyway, because it
768  // simplifies adding the symbols later.
769  if (data.empty()) {
770    if (!debugChunks.empty())
771      debugTypesObj = makeTpiSource(ctx, this);
772    return;
773  }
774
775  // Get the first type record. It will indicate if this object uses a type
776  // server (/Zi) or a PCH file (/Yu).
777  CVTypeArray types;
778  BinaryStreamReader reader(data, llvm::endianness::little);
779  cantFail(reader.readArray(types, reader.getLength()));
780  CVTypeArray::Iterator firstType = types.begin();
781  if (firstType == types.end())
782    return;
783
784  // Remember the .debug$T or .debug$P section.
785  debugTypes = data;
786
787  // This object file is a PCH file that others will depend on.
788  if (isPCH) {
789    debugTypesObj = makePrecompSource(ctx, this);
790    return;
791  }
792
793  // This object file was compiled with /Zi. Enqueue the PDB dependency.
794  if (firstType->kind() == LF_TYPESERVER2) {
795    TypeServer2Record ts = cantFail(
796        TypeDeserializer::deserializeAs<TypeServer2Record>(firstType->data()));
797    debugTypesObj = makeUseTypeServerSource(ctx, this, ts);
798    enqueuePdbFile(ts.getName(), this);
799    return;
800  }
801
802  // This object was compiled with /Yu. It uses types from another object file
803  // with a matching signature.
804  if (firstType->kind() == LF_PRECOMP) {
805    PrecompRecord precomp = cantFail(
806        TypeDeserializer::deserializeAs<PrecompRecord>(firstType->data()));
807    // We're better off trusting the LF_PRECOMP signature. In some cases the
808    // S_OBJNAME record doesn't contain a valid PCH signature.
809    if (precomp.Signature)
810      pchSignature = precomp.Signature;
811    debugTypesObj = makeUsePrecompSource(ctx, this, precomp);
812    // Drop the LF_PRECOMP record from the input stream.
813    debugTypes = debugTypes.drop_front(firstType->RecordData.size());
814    return;
815  }
816
817  // This is a plain old object file.
818  debugTypesObj = makeTpiSource(ctx, this);
819}
820
821// Make a PDB path assuming the PDB is in the same folder as the OBJ
822static std::string getPdbBaseName(ObjFile *file, StringRef tSPath) {
823  StringRef localPath =
824      !file->parentName.empty() ? file->parentName : file->getName();
825  SmallString<128> path = sys::path::parent_path(localPath);
826
827  // Currently, type server PDBs are only created by MSVC cl, which only runs
828  // on Windows, so we can assume type server paths are Windows style.
829  sys::path::append(path,
830                    sys::path::filename(tSPath, sys::path::Style::windows));
831  return std::string(path);
832}
833
834// The casing of the PDB path stamped in the OBJ can differ from the actual path
835// on disk. With this, we ensure to always use lowercase as a key for the
836// pdbInputFileInstances map, at least on Windows.
837static std::string normalizePdbPath(StringRef path) {
838#if defined(_WIN32)
839  return path.lower();
840#else // LINUX
841  return std::string(path);
842#endif
843}
844
845// If existing, return the actual PDB path on disk.
846static std::optional<std::string> findPdbPath(StringRef pdbPath,
847                                              ObjFile *dependentFile) {
848  // Ensure the file exists before anything else. In some cases, if the path
849  // points to a removable device, Driver::enqueuePath() would fail with an
850  // error (EAGAIN, "resource unavailable try again") which we want to skip
851  // silently.
852  if (llvm::sys::fs::exists(pdbPath))
853    return normalizePdbPath(pdbPath);
854  std::string ret = getPdbBaseName(dependentFile, pdbPath);
855  if (llvm::sys::fs::exists(ret))
856    return normalizePdbPath(ret);
857  return std::nullopt;
858}
859
860PDBInputFile::PDBInputFile(COFFLinkerContext &ctx, MemoryBufferRef m)
861    : InputFile(ctx, PDBKind, m) {}
862
863PDBInputFile::~PDBInputFile() = default;
864
865PDBInputFile *PDBInputFile::findFromRecordPath(const COFFLinkerContext &ctx,
866                                               StringRef path,
867                                               ObjFile *fromFile) {
868  auto p = findPdbPath(path.str(), fromFile);
869  if (!p)
870    return nullptr;
871  auto it = ctx.pdbInputFileInstances.find(*p);
872  if (it != ctx.pdbInputFileInstances.end())
873    return it->second;
874  return nullptr;
875}
876
877void PDBInputFile::parse() {
878  ctx.pdbInputFileInstances[mb.getBufferIdentifier().str()] = this;
879
880  std::unique_ptr<pdb::IPDBSession> thisSession;
881  Error E = pdb::NativeSession::createFromPdb(
882      MemoryBuffer::getMemBuffer(mb, false), thisSession);
883  if (E) {
884    loadErrorStr.emplace(toString(std::move(E)));
885    return; // fail silently at this point - the error will be handled later,
886            // when merging the debug type stream
887  }
888
889  session.reset(static_cast<pdb::NativeSession *>(thisSession.release()));
890
891  pdb::PDBFile &pdbFile = session->getPDBFile();
892  auto expectedInfo = pdbFile.getPDBInfoStream();
893  // All PDB Files should have an Info stream.
894  if (!expectedInfo) {
895    loadErrorStr.emplace(toString(expectedInfo.takeError()));
896    return;
897  }
898  debugTypesObj = makeTypeServerSource(ctx, this);
899}
900
901// Used only for DWARF debug info, which is not common (except in MinGW
902// environments). This returns an optional pair of file name and line
903// number for where the variable was defined.
904std::optional<std::pair<StringRef, uint32_t>>
905ObjFile::getVariableLocation(StringRef var) {
906  if (!dwarf) {
907    dwarf = make<DWARFCache>(DWARFContext::create(*getCOFFObj()));
908    if (!dwarf)
909      return std::nullopt;
910  }
911  if (ctx.config.machine == I386)
912    var.consume_front("_");
913  std::optional<std::pair<std::string, unsigned>> ret =
914      dwarf->getVariableLoc(var);
915  if (!ret)
916    return std::nullopt;
917  return std::make_pair(saver().save(ret->first), ret->second);
918}
919
920// Used only for DWARF debug info, which is not common (except in MinGW
921// environments).
922std::optional<DILineInfo> ObjFile::getDILineInfo(uint32_t offset,
923                                                 uint32_t sectionIndex) {
924  if (!dwarf) {
925    dwarf = make<DWARFCache>(DWARFContext::create(*getCOFFObj()));
926    if (!dwarf)
927      return std::nullopt;
928  }
929
930  return dwarf->getDILineInfo(offset, sectionIndex);
931}
932
933void ObjFile::enqueuePdbFile(StringRef path, ObjFile *fromFile) {
934  auto p = findPdbPath(path.str(), fromFile);
935  if (!p)
936    return;
937  auto it = ctx.pdbInputFileInstances.emplace(*p, nullptr);
938  if (!it.second)
939    return; // already scheduled for load
940  ctx.driver.enqueuePDB(*p);
941}
942
943ImportFile::ImportFile(COFFLinkerContext &ctx, MemoryBufferRef m)
944    : InputFile(ctx, ImportKind, m), live(!ctx.config.doGC), thunkLive(live) {}
945
946void ImportFile::parse() {
947  const char *buf = mb.getBufferStart();
948  const auto *hdr = reinterpret_cast<const coff_import_header *>(buf);
949
950  // Check if the total size is valid.
951  if (mb.getBufferSize() != sizeof(*hdr) + hdr->SizeOfData)
952    fatal("broken import library");
953
954  // Read names and create an __imp_ symbol.
955  StringRef name = saver().save(StringRef(buf + sizeof(*hdr)));
956  StringRef impName = saver().save("__imp_" + name);
957  const char *nameStart = buf + sizeof(coff_import_header) + name.size() + 1;
958  dllName = std::string(StringRef(nameStart));
959  StringRef extName;
960  switch (hdr->getNameType()) {
961  case IMPORT_ORDINAL:
962    extName = "";
963    break;
964  case IMPORT_NAME:
965    extName = name;
966    break;
967  case IMPORT_NAME_NOPREFIX:
968    extName = ltrim1(name, "?@_");
969    break;
970  case IMPORT_NAME_UNDECORATE:
971    extName = ltrim1(name, "?@_");
972    extName = extName.substr(0, extName.find('@'));
973    break;
974  }
975
976  this->hdr = hdr;
977  externalName = extName;
978
979  impSym = ctx.symtab.addImportData(impName, this);
980  // If this was a duplicate, we logged an error but may continue;
981  // in this case, impSym is nullptr.
982  if (!impSym)
983    return;
984
985  if (hdr->getType() == llvm::COFF::IMPORT_CONST)
986    static_cast<void>(ctx.symtab.addImportData(name, this));
987
988  // If type is function, we need to create a thunk which jump to an
989  // address pointed by the __imp_ symbol. (This allows you to call
990  // DLL functions just like regular non-DLL functions.)
991  if (hdr->getType() == llvm::COFF::IMPORT_CODE)
992    thunkSym = ctx.symtab.addImportThunk(
993        name, cast_or_null<DefinedImportData>(impSym), hdr->Machine);
994}
995
996BitcodeFile::BitcodeFile(COFFLinkerContext &ctx, MemoryBufferRef mb,
997                         StringRef archiveName, uint64_t offsetInArchive,
998                         bool lazy)
999    : InputFile(ctx, BitcodeKind, mb, lazy) {
1000  std::string path = mb.getBufferIdentifier().str();
1001  if (ctx.config.thinLTOIndexOnly)
1002    path = replaceThinLTOSuffix(mb.getBufferIdentifier(),
1003                                ctx.config.thinLTOObjectSuffixReplace.first,
1004                                ctx.config.thinLTOObjectSuffixReplace.second);
1005
1006  // ThinLTO assumes that all MemoryBufferRefs given to it have a unique
1007  // name. If two archives define two members with the same name, this
1008  // causes a collision which result in only one of the objects being taken
1009  // into consideration at LTO time (which very likely causes undefined
1010  // symbols later in the link stage). So we append file offset to make
1011  // filename unique.
1012  MemoryBufferRef mbref(mb.getBuffer(),
1013                        saver().save(archiveName.empty()
1014                                         ? path
1015                                         : archiveName +
1016                                               sys::path::filename(path) +
1017                                               utostr(offsetInArchive)));
1018
1019  obj = check(lto::InputFile::create(mbref));
1020}
1021
1022BitcodeFile::~BitcodeFile() = default;
1023
1024void BitcodeFile::parse() {
1025  llvm::StringSaver &saver = lld::saver();
1026
1027  std::vector<std::pair<Symbol *, bool>> comdat(obj->getComdatTable().size());
1028  for (size_t i = 0; i != obj->getComdatTable().size(); ++i)
1029    // FIXME: Check nodeduplicate
1030    comdat[i] =
1031        ctx.symtab.addComdat(this, saver.save(obj->getComdatTable()[i].first));
1032  for (const lto::InputFile::Symbol &objSym : obj->symbols()) {
1033    StringRef symName = saver.save(objSym.getName());
1034    int comdatIndex = objSym.getComdatIndex();
1035    Symbol *sym;
1036    SectionChunk *fakeSC = nullptr;
1037    if (objSym.isExecutable())
1038      fakeSC = &ctx.ltoTextSectionChunk.chunk;
1039    else
1040      fakeSC = &ctx.ltoDataSectionChunk.chunk;
1041    if (objSym.isUndefined()) {
1042      sym = ctx.symtab.addUndefined(symName, this, false);
1043      if (objSym.isWeak())
1044        sym->deferUndefined = true;
1045      // If one LTO object file references (i.e. has an undefined reference to)
1046      // a symbol with an __imp_ prefix, the LTO compilation itself sees it
1047      // as unprefixed but with a dllimport attribute instead, and doesn't
1048      // understand the relation to a concrete IR symbol with the __imp_ prefix.
1049      //
1050      // For such cases, mark the symbol as used in a regular object (i.e. the
1051      // symbol must be retained) so that the linker can associate the
1052      // references in the end. If the symbol is defined in an import library
1053      // or in a regular object file, this has no effect, but if it is defined
1054      // in another LTO object file, this makes sure it is kept, to fulfill
1055      // the reference when linking the output of the LTO compilation.
1056      if (symName.starts_with("__imp_"))
1057        sym->isUsedInRegularObj = true;
1058    } else if (objSym.isCommon()) {
1059      sym = ctx.symtab.addCommon(this, symName, objSym.getCommonSize());
1060    } else if (objSym.isWeak() && objSym.isIndirect()) {
1061      // Weak external.
1062      sym = ctx.symtab.addUndefined(symName, this, true);
1063      std::string fallback = std::string(objSym.getCOFFWeakExternalFallback());
1064      Symbol *alias = ctx.symtab.addUndefined(saver.save(fallback));
1065      checkAndSetWeakAlias(ctx, this, sym, alias);
1066    } else if (comdatIndex != -1) {
1067      if (symName == obj->getComdatTable()[comdatIndex].first) {
1068        sym = comdat[comdatIndex].first;
1069        if (cast<DefinedRegular>(sym)->data == nullptr)
1070          cast<DefinedRegular>(sym)->data = &fakeSC->repl;
1071      } else if (comdat[comdatIndex].second) {
1072        sym = ctx.symtab.addRegular(this, symName, nullptr, fakeSC);
1073      } else {
1074        sym = ctx.symtab.addUndefined(symName, this, false);
1075      }
1076    } else {
1077      sym = ctx.symtab.addRegular(this, symName, nullptr, fakeSC, 0,
1078                                  objSym.isWeak());
1079    }
1080    symbols.push_back(sym);
1081    if (objSym.isUsed())
1082      ctx.config.gcroot.push_back(sym);
1083  }
1084  directives = saver.save(obj->getCOFFLinkerOpts());
1085}
1086
1087void BitcodeFile::parseLazy() {
1088  for (const lto::InputFile::Symbol &sym : obj->symbols())
1089    if (!sym.isUndefined())
1090      ctx.symtab.addLazyObject(this, sym.getName());
1091}
1092
1093MachineTypes BitcodeFile::getMachineType() {
1094  switch (Triple(obj->getTargetTriple()).getArch()) {
1095  case Triple::x86_64:
1096    return AMD64;
1097  case Triple::x86:
1098    return I386;
1099  case Triple::arm:
1100  case Triple::thumb:
1101    return ARMNT;
1102  case Triple::aarch64:
1103    return ARM64;
1104  default:
1105    return IMAGE_FILE_MACHINE_UNKNOWN;
1106  }
1107}
1108
1109std::string lld::coff::replaceThinLTOSuffix(StringRef path, StringRef suffix,
1110                                            StringRef repl) {
1111  if (path.consume_back(suffix))
1112    return (path + repl).str();
1113  return std::string(path);
1114}
1115
1116static bool isRVACode(COFFObjectFile *coffObj, uint64_t rva, InputFile *file) {
1117  for (size_t i = 1, e = coffObj->getNumberOfSections(); i <= e; i++) {
1118    const coff_section *sec = CHECK(coffObj->getSection(i), file);
1119    if (rva >= sec->VirtualAddress &&
1120        rva <= sec->VirtualAddress + sec->VirtualSize) {
1121      return (sec->Characteristics & COFF::IMAGE_SCN_CNT_CODE) != 0;
1122    }
1123  }
1124  return false;
1125}
1126
1127void DLLFile::parse() {
1128  // Parse a memory buffer as a PE-COFF executable.
1129  std::unique_ptr<Binary> bin = CHECK(createBinary(mb), this);
1130
1131  if (auto *obj = dyn_cast<COFFObjectFile>(bin.get())) {
1132    bin.release();
1133    coffObj.reset(obj);
1134  } else {
1135    error(toString(this) + " is not a COFF file");
1136    return;
1137  }
1138
1139  if (!coffObj->getPE32Header() && !coffObj->getPE32PlusHeader()) {
1140    error(toString(this) + " is not a PE-COFF executable");
1141    return;
1142  }
1143
1144  for (const auto &exp : coffObj->export_directories()) {
1145    StringRef dllName, symbolName;
1146    uint32_t exportRVA;
1147    checkError(exp.getDllName(dllName));
1148    checkError(exp.getSymbolName(symbolName));
1149    checkError(exp.getExportRVA(exportRVA));
1150
1151    if (symbolName.empty())
1152      continue;
1153
1154    bool code = isRVACode(coffObj.get(), exportRVA, this);
1155
1156    Symbol *s = make<Symbol>();
1157    s->dllName = dllName;
1158    s->symbolName = symbolName;
1159    s->importType = code ? ImportType::IMPORT_CODE : ImportType::IMPORT_DATA;
1160    s->nameType = ImportNameType::IMPORT_NAME;
1161
1162    if (coffObj->getMachine() == I386) {
1163      s->symbolName = symbolName = saver().save("_" + symbolName);
1164      s->nameType = ImportNameType::IMPORT_NAME_NOPREFIX;
1165    }
1166
1167    StringRef impName = saver().save("__imp_" + symbolName);
1168    ctx.symtab.addLazyDLLSymbol(this, s, impName);
1169    if (code)
1170      ctx.symtab.addLazyDLLSymbol(this, s, symbolName);
1171  }
1172}
1173
1174MachineTypes DLLFile::getMachineType() {
1175  if (coffObj)
1176    return static_cast<MachineTypes>(coffObj->getMachine());
1177  return IMAGE_FILE_MACHINE_UNKNOWN;
1178}
1179
1180void DLLFile::makeImport(DLLFile::Symbol *s) {
1181  if (!seen.insert(s->symbolName).second)
1182    return;
1183
1184  size_t impSize = s->dllName.size() + s->symbolName.size() + 2; // +2 for NULs
1185  size_t size = sizeof(coff_import_header) + impSize;
1186  char *buf = bAlloc().Allocate<char>(size);
1187  memset(buf, 0, size);
1188  char *p = buf;
1189  auto *imp = reinterpret_cast<coff_import_header *>(p);
1190  p += sizeof(*imp);
1191  imp->Sig2 = 0xFFFF;
1192  imp->Machine = coffObj->getMachine();
1193  imp->SizeOfData = impSize;
1194  imp->OrdinalHint = 0; // Only linking by name
1195  imp->TypeInfo = (s->nameType << 2) | s->importType;
1196
1197  // Write symbol name and DLL name.
1198  memcpy(p, s->symbolName.data(), s->symbolName.size());
1199  p += s->symbolName.size() + 1;
1200  memcpy(p, s->dllName.data(), s->dllName.size());
1201  MemoryBufferRef mbref = MemoryBufferRef(StringRef(buf, size), s->dllName);
1202  ImportFile *impFile = make<ImportFile>(ctx, mbref);
1203  ctx.symtab.addFile(impFile);
1204}
1205