Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/COFF/InputFiles.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- InputFiles.cpp -----------------------------------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "InputFiles.h"
10
#include "Chunks.h"
11
#include "Config.h"
12
#include "DebugTypes.h"
13
#include "Driver.h"
14
#include "SymbolTable.h"
15
#include "Symbols.h"
16
#include "lld/Common/ErrorHandler.h"
17
#include "lld/Common/Memory.h"
18
#include "llvm-c/lto.h"
19
#include "llvm/ADT/SmallVector.h"
20
#include "llvm/ADT/Triple.h"
21
#include "llvm/ADT/Twine.h"
22
#include "llvm/BinaryFormat/COFF.h"
23
#include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h"
24
#include "llvm/DebugInfo/CodeView/SymbolDeserializer.h"
25
#include "llvm/DebugInfo/CodeView/SymbolRecord.h"
26
#include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
27
#include "llvm/Object/Binary.h"
28
#include "llvm/Object/COFF.h"
29
#include "llvm/Support/Casting.h"
30
#include "llvm/Support/Endian.h"
31
#include "llvm/Support/Error.h"
32
#include "llvm/Support/ErrorOr.h"
33
#include "llvm/Support/FileSystem.h"
34
#include "llvm/Support/Path.h"
35
#include "llvm/Target/TargetOptions.h"
36
#include <cstring>
37
#include <system_error>
38
#include <utility>
39
40
using namespace llvm;
41
using namespace llvm::COFF;
42
using namespace llvm::codeview;
43
using namespace llvm::object;
44
using namespace llvm::support::endian;
45
46
using llvm::Triple;
47
using llvm::support::ulittle32_t;
48
49
namespace lld {
50
namespace coff {
51
52
std::vector<ObjFile *> ObjFile::instances;
53
std::vector<ImportFile *> ImportFile::instances;
54
std::vector<BitcodeFile *> BitcodeFile::instances;
55
56
/// Checks that Source is compatible with being a weak alias to Target.
57
/// If Source is Undefined and has no weak alias set, makes it a weak
58
/// alias to Target.
59
static void checkAndSetWeakAlias(SymbolTable *symtab, InputFile *f,
60
11
                                 Symbol *source, Symbol *target) {
61
11
  if (auto *u = dyn_cast<Undefined>(source)) {
62
10
    if (u->weakAlias && 
u->weakAlias != target2
) {
63
2
      // Weak aliases as produced by GCC are named in the form
64
2
      // .weak.<weaksymbol>.<othersymbol>, where <othersymbol> is the name
65
2
      // of another symbol emitted near the weak symbol.
66
2
      // Just use the definition from the first object file that defined
67
2
      // this weak symbol.
68
2
      if (config->mingw)
69
2
        return;
70
0
      symtab->reportDuplicate(source, f);
71
0
    }
72
10
    u->weakAlias = target;
73
8
  }
74
11
}
75
76
97
ArchiveFile::ArchiveFile(MemoryBufferRef m) : InputFile(ArchiveKind, m) {}
77
78
97
void ArchiveFile::parse() {
79
97
  // Parse a MemoryBufferRef as an archive file.
80
97
  file = CHECK(Archive::create(mb), this);
81
97
82
97
  // Read the symbol table to construct Lazy objects.
83
97
  for (const Archive::Symbol &sym : file->symbols())
84
508
    symtab->addLazy(this, sym);
85
97
}
86
87
// Returns a buffer pointing to a member file containing a given symbol.
88
157
void ArchiveFile::addMember(const Archive::Symbol &sym) {
89
157
  const Archive::Child &c =
90
157
      CHECK(sym.getMember(),
91
157
            "could not get the member for symbol " + toCOFFString(sym));
92
157
93
157
  // Return an empty buffer if we have already returned the same buffer.
94
157
  if (!seen.insert(c.getChildOffset()).second)
95
1
    return;
96
156
97
156
  driver->enqueueArchiveMember(c, sym, getName());
98
156
}
99
100
5
std::vector<MemoryBufferRef> getArchiveMembers(Archive *file) {
101
5
  std::vector<MemoryBufferRef> v;
102
5
  Error err = Error::success();
103
5
  for (const ErrorOr<Archive::Child> &cOrErr : file->children(err)) {
104
5
    Archive::Child c =
105
5
        CHECK(cOrErr,
106
5
              file->getFileName() + ": could not get the child of the archive");
107
5
    MemoryBufferRef mbref =
108
5
        CHECK(c.getMemoryBufferRef(),
109
5
              file->getFileName() +
110
5
                  ": could not get the buffer for a child of the archive");
111
5
    v.push_back(mbref);
112
5
  }
113
5
  if (err)
114
0
    fatal(file->getFileName() +
115
0
          ": Archive::children failed: " + toString(std::move(err)));
116
5
  return v;
117
5
}
118
119
828
void ObjFile::parse() {
120
828
  // Parse a memory buffer as a COFF file.
121
828
  std::unique_ptr<Binary> bin = CHECK(createBinary(mb), this);
122
828
123
828
  if (auto *obj = dyn_cast<COFFObjectFile>(bin.get())) {
124
828
    bin.release();
125
828
    coffObj.reset(obj);
126
828
  } else {
127
0
    fatal(toString(this) + " is not a COFF file");
128
0
  }
129
828
130
828
  // Read section and symbol tables.
131
828
  initializeChunks();
132
828
  initializeSymbols();
133
828
  initializeFlags();
134
828
  initializeDependencies();
135
828
}
136
137
6.02k
const coff_section* ObjFile::getSection(uint32_t i) {
138
6.02k
  const coff_section *sec;
139
6.02k
  if (auto ec = coffObj->getSection(i, sec))
140
0
    fatal("getSection failed: #" + Twine(i) + ": " + ec.message());
141
6.02k
  return sec;
142
6.02k
}
143
144
// We set SectionChunk pointers in the SparseChunks vector to this value
145
// temporarily to mark comdat sections as having an unknown resolution. As we
146
// walk the object file's symbol table, once we visit either a leader symbol or
147
// an associative section definition together with the parent comdat's leader,
148
// we set the pointer to either nullptr (to mark the section as discarded) or a
149
// valid SectionChunk for that section.
150
static SectionChunk *const pendingComdat = reinterpret_cast<SectionChunk *>(1);
151
152
828
void ObjFile::initializeChunks() {
153
828
  uint32_t numSections = coffObj->getNumberOfSections();
154
828
  chunks.reserve(numSections);
155
828
  sparseChunks.resize(numSections + 1);
156
3.86k
  for (uint32_t i = 1; i < numSections + 1; 
++i3.03k
) {
157
3.03k
    const coff_section *sec = getSection(i);
158
3.03k
    if (sec->Characteristics & IMAGE_SCN_LNK_COMDAT)
159
540
      sparseChunks[i] = pendingComdat;
160
2.49k
    else
161
2.49k
      sparseChunks[i] = readSection(i, nullptr, "");
162
3.03k
  }
163
828
}
164
165
SectionChunk *ObjFile::readSection(uint32_t sectionNumber,
166
                                   const coff_aux_section_definition *def,
167
2.97k
                                   StringRef leaderName) {
168
2.97k
  const coff_section *sec = getSection(sectionNumber);
169
2.97k
170
2.97k
  StringRef name;
171
2.97k
  if (Expected<StringRef> e = coffObj->getSectionName(sec))
172
2.97k
    name = *e;
173
0
  else
174
0
    fatal("getSectionName failed: #" + Twine(sectionNumber) + ": " +
175
0
          toString(e.takeError()));
176
2.97k
177
2.97k
  if (name == ".drectve") {
178
205
    ArrayRef<uint8_t> data;
179
205
    cantFail(coffObj->getSectionContents(sec, data));
180
205
    directives = StringRef((const char *)data.data(), data.size());
181
205
    return nullptr;
182
205
  }
183
2.77k
184
2.77k
  if (name == ".llvm_addrsig") {
185
9
    addrsigSec = sec;
186
9
    return nullptr;
187
9
  }
188
2.76k
189
2.76k
  // Object files may have DWARF debug info or MS CodeView debug info
190
2.76k
  // (or both).
191
2.76k
  //
192
2.76k
  // DWARF sections don't need any special handling from the perspective
193
2.76k
  // of the linker; they are just a data section containing relocations.
194
2.76k
  // We can just link them to complete debug info.
195
2.76k
  //
196
2.76k
  // CodeView needs linker support. We need to interpret debug info,
197
2.76k
  // and then write it to a separate .pdb file.
198
2.76k
199
2.76k
  // Ignore DWARF debug info unless /debug is given.
200
2.76k
  if (!config->debug && 
name.startswith(".debug_")2.07k
)
201
16
    return nullptr;
202
2.74k
203
2.74k
  if (sec->Characteristics & llvm::COFF::IMAGE_SCN_LNK_REMOVE)
204
27
    return nullptr;
205
2.71k
  auto *c = make<SectionChunk>(this, sec);
206
2.71k
  if (def)
207
483
    c->checksum = def->CheckSum;
208
2.71k
209
2.71k
  // link.exe uses the presence of .rsrc$01 for LNK4078, so match that.
210
2.71k
  if (name == ".rsrc$01")
211
11
    isResourceObjFile = true;
212
2.71k
213
2.71k
  // CodeView sections are stored to a different vector because they are not
214
2.71k
  // linked in the regular manner.
215
2.71k
  if (c->isCodeView())
216
295
    debugChunks.push_back(c);
217
2.42k
  else if (name == ".gfids$y")
218
12
    guardFidChunks.push_back(c);
219
2.41k
  else if (name == ".gljmp$y")
220
2
    guardLJmpChunks.push_back(c);
221
2.40k
  else if (name == ".sxdata")
222
12
    sXDataChunks.push_back(c);
223
2.39k
  else if (config->tailMerge && 
sec->NumberOfRelocations == 01.85k
&&
224
2.39k
           
name == ".rdata"1.51k
&&
leaderName.startswith("??_C@")44
)
225
12
    // COFF sections that look like string literal sections (i.e. no
226
12
    // relocations, in .rdata, leader symbol name matches the MSVC name mangling
227
12
    // for string literals) are subject to string tail merging.
228
12
    MergeChunk::addSection(c);
229
2.38k
  else
230
2.38k
    chunks.push_back(c);
231
2.71k
232
2.71k
  return c;
233
2.71k
}
234
235
void ObjFile::readAssociativeDefinition(
236
124
    COFFSymbolRef sym, const coff_aux_section_definition *def) {
237
124
  readAssociativeDefinition(sym, def, def->getNumber(sym.isBigObj()));
238
124
}
239
240
void ObjFile::readAssociativeDefinition(COFFSymbolRef sym,
241
                                        const coff_aux_section_definition *def,
242
130
                                        uint32_t parentIndex) {
243
130
  SectionChunk *parent = sparseChunks[parentIndex];
244
130
  int32_t sectionNumber = sym.getSectionNumber();
245
130
246
130
  auto diag = [&]() {
247
2
    StringRef name, parentName;
248
2
    coffObj->getSymbolName(sym, name);
249
2
250
2
    const coff_section *parentSec = getSection(parentIndex);
251
2
    if (Expected<StringRef> e = coffObj->getSectionName(parentSec))
252
2
      parentName = *e;
253
2
    error(toString(this) + ": associative comdat " + name + " (sec " +
254
2
          Twine(sectionNumber) + ") has invalid reference to section " +
255
2
          parentName + " (sec " + Twine(parentIndex) + ")");
256
2
  };
257
130
258
130
  if (parent == pendingComdat) {
259
2
    // This can happen if an associative comdat refers to another associative
260
2
    // comdat that appears after it (invalid per COFF spec) or to a section
261
2
    // without any symbols.
262
2
    diag();
263
2
    return;
264
2
  }
265
128
266
128
  // Check whether the parent is prevailing. If it is, so are we, and we read
267
128
  // the section; otherwise mark it as discarded.
268
128
  if (parent) {
269
117
    SectionChunk *c = readSection(sectionNumber, def, "");
270
117
    sparseChunks[sectionNumber] = c;
271
117
    if (c) {
272
117
      c->selection = IMAGE_COMDAT_SELECT_ASSOCIATIVE;
273
117
      parent->addAssociative(c);
274
117
    }
275
117
  } else {
276
11
    sparseChunks[sectionNumber] = nullptr;
277
11
  }
278
128
}
279
280
void ObjFile::recordPrevailingSymbolForMingw(
281
11
    COFFSymbolRef sym, DenseMap<StringRef, uint32_t> &prevailingSectionMap) {
282
11
  // For comdat symbols in executable sections, where this is the copy
283
11
  // of the section chunk we actually include instead of discarding it,
284
11
  // add the symbol to a map to allow using it for implicitly
285
11
  // associating .[px]data$<func> sections to it.
286
11
  int32_t sectionNumber = sym.getSectionNumber();
287
11
  SectionChunk *sc = sparseChunks[sectionNumber];
288
11
  if (sc && sc->getOutputCharacteristics() & IMAGE_SCN_MEM_EXECUTE) {
289
8
    StringRef name;
290
8
    coffObj->getSymbolName(sym, name);
291
8
    if (getMachineType() == I386)
292
4
      name.consume_front("_");
293
8
    prevailingSectionMap[name] = sectionNumber;
294
8
  }
295
11
}
296
297
void ObjFile::maybeAssociateSEHForMingw(
298
    COFFSymbolRef sym, const coff_aux_section_definition *def,
299
28
    const DenseMap<StringRef, uint32_t> &prevailingSectionMap) {
300
28
  StringRef name;
301
28
  coffObj->getSymbolName(sym, name);
302
28
  if (name.consume_front(".pdata$") || 
name.consume_front(".xdata$")26
||
303
28
      
name.consume_front(".eh_frame$")18
) {
304
12
    // For MinGW, treat .[px]data$<func> and .eh_frame$<func> as implicitly
305
12
    // associative to the symbol <func>.
306
12
    auto parentSym = prevailingSectionMap.find(name);
307
12
    if (parentSym != prevailingSectionMap.end())
308
6
      readAssociativeDefinition(sym, def, parentSym->second);
309
12
  }
310
28
}
311
312
4.48k
Symbol *ObjFile::createRegular(COFFSymbolRef sym) {
313
4.48k
  SectionChunk *sc = sparseChunks[sym.getSectionNumber()];
314
4.48k
  if (sym.isExternal()) {
315
1.07k
    StringRef name;
316
1.07k
    coffObj->getSymbolName(sym, name);
317
1.07k
    if (sc)
318
1.07k
      return symtab->addRegular(this, name, sym.getGeneric(), sc);
319
1
    // For MinGW symbols named .weak.* that point to a discarded section,
320
1
    // don't create an Undefined symbol. If nothing ever refers to the symbol,
321
1
    // everything should be fine. If something actually refers to the symbol
322
1
    // (e.g. the undefined weak alias), linking will fail due to undefined
323
1
    // references at the end.
324
1
    if (config->mingw && name.startswith(".weak."))
325
1
      return nullptr;
326
0
    return symtab->addUndefined(name, this, false);
327
0
  }
328
3.41k
  if (sc)
329
3.18k
    return make<DefinedRegular>(this, /*Name*/ "", /*IsCOMDAT*/ false,
330
3.18k
                                /*IsExternal*/ false, sym.getGeneric(), sc);
331
231
  return nullptr;
332
231
}
333
334
828
void ObjFile::initializeSymbols() {
335
828
  uint32_t numSymbols = coffObj->getNumberOfSymbols();
336
828
  symbols.resize(numSymbols);
337
828
338
828
  SmallVector<std::pair<Symbol *, uint32_t>, 8> weakAliases;
339
828
  std::vector<uint32_t> pendingIndexes;
340
828
  pendingIndexes.reserve(numSymbols);
341
828
342
828
  DenseMap<StringRef, uint32_t> prevailingSectionMap;
343
828
  std::vector<const coff_aux_section_definition *> comdatDefs(
344
828
      coffObj->getNumberOfSections() + 1);
345
828
346
6.55k
  for (uint32_t i = 0; i < numSymbols; 
++i5.72k
) {
347
5.72k
    COFFSymbolRef coffSym = check(coffObj->getSymbol(i));
348
5.72k
    bool prevailingComdat;
349
5.72k
    if (coffSym.isUndefined()) {
350
348
      symbols[i] = createUndefined(coffSym);
351
5.37k
    } else if (coffSym.isWeakExternal()) {
352
9
      symbols[i] = createUndefined(coffSym);
353
9
      uint32_t tagIndex = coffSym.getAux<coff_aux_weak_external>()->TagIndex;
354
9
      weakAliases.emplace_back(symbols[i], tagIndex);
355
5.36k
    } else if (Optional<Symbol *> optSym =
356
4.79k
                   createDefined(coffSym, comdatDefs, prevailingComdat)) {
357
4.79k
      symbols[i] = *optSym;
358
4.79k
      if (config->mingw && 
prevailingComdat452
)
359
11
        recordPrevailingSymbolForMingw(coffSym, prevailingSectionMap);
360
4.79k
    } else {
361
568
      // createDefined() returns None if a symbol belongs to a section that
362
568
      // was pending at the point when the symbol was read. This can happen in
363
568
      // two cases:
364
568
      // 1) section definition symbol for a comdat leader;
365
568
      // 2) symbol belongs to a comdat section associated with another section.
366
568
      // In both of these cases, we can expect the section to be resolved by
367
568
      // the time we finish visiting the remaining symbols in the symbol
368
568
      // table. So we postpone the handling of this symbol until that time.
369
568
      pendingIndexes.push_back(i);
370
568
    }
371
5.72k
    i += coffSym.getNumberOfAuxSymbols();
372
5.72k
  }
373
828
374
828
  for (uint32_t i : pendingIndexes) {
375
568
    COFFSymbolRef sym = check(coffObj->getSymbol(i));
376
568
    if (const coff_aux_section_definition *def = sym.getSectionDefinition()) {
377
540
      if (def->Selection == IMAGE_COMDAT_SELECT_ASSOCIATIVE)
378
124
        readAssociativeDefinition(sym, def);
379
416
      else if (config->mingw)
380
28
        maybeAssociateSEHForMingw(sym, def, prevailingSectionMap);
381
540
    }
382
568
    if (sparseChunks[sym.getSectionNumber()] == pendingComdat) {
383
12
      StringRef name;
384
12
      coffObj->getSymbolName(sym, name);
385
12
      log("comdat section " + name +
386
12
          " without leader and unassociated, discarding");
387
12
      continue;
388
12
    }
389
556
    symbols[i] = createRegular(sym);
390
556
  }
391
828
392
828
  for (auto &kv : weakAliases) {
393
9
    Symbol *sym = kv.first;
394
9
    uint32_t idx = kv.second;
395
9
    checkAndSetWeakAlias(symtab, this, sym, symbols[idx]);
396
9
  }
397
828
}
398
399
357
Symbol *ObjFile::createUndefined(COFFSymbolRef sym) {
400
357
  StringRef name;
401
357
  coffObj->getSymbolName(sym, name);
402
357
  return symtab->addUndefined(name, this, sym.isWeakExternal());
403
357
}
404
405
void ObjFile::handleComdatSelection(COFFSymbolRef sym, COMDATType &selection,
406
365
                                    bool &prevailing, DefinedRegular *leader) {
407
365
  if (prevailing)
408
324
    return;
409
41
  // There's already an existing comdat for this symbol: `Leader`.
410
41
  // Use the comdats's selection field to determine if the new
411
41
  // symbol in `Sym` should be discarded, produce a duplicate symbol
412
41
  // error, etc.
413
41
414
41
  SectionChunk *leaderChunk = nullptr;
415
41
  COMDATType leaderSelection = IMAGE_COMDAT_SELECT_ANY;
416
41
417
41
  if (leader->data) {
418
37
    leaderChunk = leader->getChunk();
419
37
    leaderSelection = leaderChunk->selection;
420
37
  } else {
421
4
    // FIXME: comdats from LTO files don't know their selection; treat them
422
4
    // as "any".
423
4
    selection = leaderSelection;
424
4
  }
425
41
426
41
  if ((selection == IMAGE_COMDAT_SELECT_ANY &&
427
41
       
leaderSelection == IMAGE_COMDAT_SELECT_LARGEST20
) ||
428
41
      
(40
selection == IMAGE_COMDAT_SELECT_LARGEST40
&&
429
40
       
leaderSelection == IMAGE_COMDAT_SELECT_ANY11
)) {
430
2
    // cl.exe picks "any" for vftables when building with /GR- and
431
2
    // "largest" when building with /GR. To be able to link object files
432
2
    // compiled with each flag, "any" and "largest" are merged as "largest".
433
2
    leaderSelection = selection = IMAGE_COMDAT_SELECT_LARGEST;
434
2
  }
435
41
436
41
  // Other than that, comdat selections must match.  This is a bit more
437
41
  // strict than link.exe which allows merging "any" and "largest" if "any"
438
41
  // is the first symbol the linker sees, and it allows merging "largest"
439
41
  // with everything (!) if "largest" is the first symbol the linker sees.
440
41
  // Making this symmetric independent of which selection is seen first
441
41
  // seems better though.
442
41
  // (This behavior matches ModuleLinker::getComdatResult().)
443
41
  if (selection != leaderSelection) {
444
5
    log(("conflicting comdat type for " + toString(*leader) + ": " +
445
5
         Twine((int)leaderSelection) + " in " + toString(leader->getFile()) +
446
5
         " and " + Twine((int)selection) + " in " + toString(this))
447
5
            .str());
448
5
    symtab->reportDuplicate(leader, this);
449
5
    return;
450
5
  }
451
36
452
36
  switch (selection) {
453
36
  case IMAGE_COMDAT_SELECT_NODUPLICATES:
454
1
    symtab->reportDuplicate(leader, this);
455
1
    break;
456
36
457
36
  case IMAGE_COMDAT_SELECT_ANY:
458
18
    // Nothing to do.
459
18
    break;
460
36
461
36
  case IMAGE_COMDAT_SELECT_SAME_SIZE:
462
2
    if (leaderChunk->getSize() != getSection(sym)->SizeOfRawData)
463
1
      symtab->reportDuplicate(leader, this);
464
2
    break;
465
36
466
36
  case IMAGE_COMDAT_SELECT_EXACT_MATCH: {
467
3
    SectionChunk newChunk(this, getSection(sym));
468
3
    // link.exe only compares section contents here and doesn't complain
469
3
    // if the two comdat sections have e.g. different alignment.
470
3
    // Match that.
471
3
    if (leaderChunk->getContents() != newChunk.getContents())
472
2
      symtab->reportDuplicate(leader, this);
473
3
    break;
474
36
  }
475
36
476
36
  case IMAGE_COMDAT_SELECT_ASSOCIATIVE:
477
0
    // createDefined() is never called for IMAGE_COMDAT_SELECT_ASSOCIATIVE.
478
0
    // (This means lld-link doesn't produce duplicate symbol errors for
479
0
    // associative comdats while link.exe does, but associate comdats
480
0
    // are never extern in practice.)
481
0
    llvm_unreachable("createDefined not called for associative comdats");
482
36
483
36
  case IMAGE_COMDAT_SELECT_LARGEST:
484
12
    if (leaderChunk->getSize() < getSection(sym)->SizeOfRawData) {
485
5
      // Replace the existing comdat symbol with the new one.
486
5
      StringRef name;
487
5
      coffObj->getSymbolName(sym, name);
488
5
      // FIXME: This is incorrect: With /opt:noref, the previous sections
489
5
      // make it into the final executable as well. Correct handling would
490
5
      // be to undo reading of the whole old section that's being replaced,
491
5
      // or doing one pass that determines what the final largest comdat
492
5
      // is for all IMAGE_COMDAT_SELECT_LARGEST comdats and then reading
493
5
      // only the largest one.
494
5
      replaceSymbol<DefinedRegular>(leader, this, name, /*IsCOMDAT*/ true,
495
5
                                    /*IsExternal*/ true, sym.getGeneric(),
496
5
                                    nullptr);
497
5
      prevailing = true;
498
5
    }
499
12
    break;
500
36
501
36
  case IMAGE_COMDAT_SELECT_NEWEST:
502
0
    llvm_unreachable("should have been rejected earlier");
503
36
  }
504
36
}
505
506
Optional<Symbol *> ObjFile::createDefined(
507
    COFFSymbolRef sym,
508
    std::vector<const coff_aux_section_definition *> &comdatDefs,
509
5.36k
    bool &prevailing) {
510
5.36k
  prevailing = false;
511
5.36k
  auto getName = [&]() {
512
1.10k
    StringRef s;
513
1.10k
    coffObj->getSymbolName(sym, s);
514
1.10k
    return s;
515
1.10k
  };
516
5.36k
517
5.36k
  if (sym.isCommon()) {
518
14
    auto *c = make<CommonChunk>(sym);
519
14
    chunks.push_back(c);
520
14
    return symtab->addCommon(this, getName(), sym.getValue(), sym.getGeneric(),
521
14
                             c);
522
14
  }
523
5.35k
524
5.35k
  if (sym.isAbsolute()) {
525
363
    StringRef name = getName();
526
363
527
363
    // Skip special symbols.
528
363
    if (name == "@comp.id")
529
103
      return nullptr;
530
260
    if (name == "@feat.00") {
531
248
      feat00Flags = sym.getValue();
532
248
      return nullptr;
533
248
    }
534
12
535
12
    if (sym.isExternal())
536
8
      return symtab->addAbsolute(name, sym);
537
4
    return make<DefinedAbsolute>(name, sym);
538
4
  }
539
4.98k
540
4.98k
  int32_t sectionNumber = sym.getSectionNumber();
541
4.98k
  if (sectionNumber == llvm::COFF::IMAGE_SYM_DEBUG)
542
87
    return nullptr;
543
4.90k
544
4.90k
  if (llvm::COFF::isReservedSectionNumber(sectionNumber))
545
0
    fatal(toString(this) + ": " + getName() +
546
0
          " should not refer to special section " + Twine(sectionNumber));
547
4.90k
548
4.90k
  if ((uint32_t)sectionNumber >= sparseChunks.size())
549
0
    fatal(toString(this) + ": " + getName() +
550
0
          " should not refer to non-existent section " + Twine(sectionNumber));
551
4.90k
552
4.90k
  // Comdat handling.
553
4.90k
  // A comdat symbol consists of two symbol table entries.
554
4.90k
  // The first symbol entry has the name of the section (e.g. .text), fixed
555
4.90k
  // values for the other fields, and one auxilliary record.
556
4.90k
  // The second symbol entry has the name of the comdat symbol, called the
557
4.90k
  // "comdat leader".
558
4.90k
  // When this function is called for the first symbol entry of a comdat,
559
4.90k
  // it sets comdatDefs and returns None, and when it's called for the second
560
4.90k
  // symbol entry it reads comdatDefs and then sets it back to nullptr.
561
4.90k
562
4.90k
  // Handle comdat leader.
563
4.90k
  if (const coff_aux_section_definition *def = comdatDefs[sectionNumber]) {
564
402
    comdatDefs[sectionNumber] = nullptr;
565
402
    DefinedRegular *leader;
566
402
567
402
    if (sym.isExternal()) {
568
365
      std::tie(leader, prevailing) =
569
365
          symtab->addComdat(this, getName(), sym.getGeneric());
570
365
    } else {
571
37
      leader = make<DefinedRegular>(this, /*Name*/ "", /*IsCOMDAT*/ false,
572
37
                                    /*IsExternal*/ false, sym.getGeneric());
573
37
      prevailing = true;
574
37
    }
575
402
576
402
    if (def->Selection < (int)IMAGE_COMDAT_SELECT_NODUPLICATES ||
577
402
        // Intentionally ends at IMAGE_COMDAT_SELECT_LARGEST: link.exe
578
402
        // doesn't understand IMAGE_COMDAT_SELECT_NEWEST either.
579
402
        def->Selection > (int)IMAGE_COMDAT_SELECT_LARGEST) {
580
0
      fatal("unknown comdat type " + std::to_string((int)def->Selection) +
581
0
            " for " + getName() + " in " + toString(this));
582
0
    }
583
402
    COMDATType selection = (COMDATType)def->Selection;
584
402
585
402
    if (leader->isCOMDAT)
586
365
      handleComdatSelection(sym, selection, prevailing, leader);
587
402
588
402
    if (prevailing) {
589
366
      SectionChunk *c = readSection(sectionNumber, def, getName());
590
366
      sparseChunks[sectionNumber] = c;
591
366
      c->sym = cast<DefinedRegular>(leader);
592
366
      c->selection = selection;
593
366
      cast<DefinedRegular>(leader)->data = &c->repl;
594
366
    } else {
595
36
      sparseChunks[sectionNumber] = nullptr;
596
36
    }
597
402
    return leader;
598
402
  }
599
4.49k
600
4.49k
  // Prepare to handle the comdat leader symbol by setting the section's
601
4.49k
  // ComdatDefs pointer if we encounter a non-associative comdat.
602
4.49k
  if (sparseChunks[sectionNumber] == pendingComdat) {
603
568
    if (const coff_aux_section_definition *def = sym.getSectionDefinition()) {
604
540
      if (def->Selection != IMAGE_COMDAT_SELECT_ASSOCIATIVE)
605
416
        comdatDefs[sectionNumber] = def;
606
540
    }
607
568
    return None;
608
568
  }
609
3.93k
610
3.93k
  return createRegular(sym);
611
3.93k
}
612
613
775
MachineTypes ObjFile::getMachineType() {
614
775
  if (coffObj)
615
775
    return static_cast<MachineTypes>(coffObj->getMachine());
616
0
  return IMAGE_FILE_MACHINE_UNKNOWN;
617
0
}
618
619
1.15k
ArrayRef<uint8_t> ObjFile::getDebugSection(StringRef secName) {
620
1.15k
  if (SectionChunk *sec = SectionChunk::findByName(debugChunks, secName))
621
232
    return sec->consumeDebugMagic();
622
922
  return {};
623
922
}
624
625
// OBJ files systematically store critical informations in a .debug$S stream,
626
// even if the TU was compiled with no debug info. At least two records are
627
// always there. S_OBJNAME stores a 32-bit signature, which is loaded into the
628
// PCHSignature member. S_COMPILE3 stores compile-time cmd-line flags. This is
629
// currently used to initialize the hotPatchable member.
630
828
void ObjFile::initializeFlags() {
631
828
  ArrayRef<uint8_t> data = getDebugSection(".debug$S");
632
828
  if (data.empty())
633
686
    return;
634
142
635
142
  DebugSubsectionArray subsections;
636
142
637
142
  BinaryStreamReader reader(data, support::little);
638
142
  ExitOnError exitOnErr;
639
142
  exitOnErr(reader.readArray(subsections, data.size()));
640
142
641
536
  for (const DebugSubsectionRecord &ss : subsections) {
642
536
    if (ss.kind() != DebugSubsectionKind::Symbols)
643
242
      continue;
644
294
645
294
    unsigned offset = 0;
646
294
647
294
    // Only parse the first two records. We are only looking for S_OBJNAME
648
294
    // and S_COMPILE3, and they usually appear at the beginning of the
649
294
    // stream.
650
783
    for (unsigned i = 0; i < 2; 
++i489
) {
651
588
      Expected<CVSymbol> sym = readSymbolFromStream(ss.getRecordData(), offset);
652
588
      if (!sym) {
653
99
        consumeError(sym.takeError());
654
99
        return;
655
99
      }
656
489
      if (sym->kind() == SymbolKind::S_COMPILE3) {
657
123
        auto cs =
658
123
            cantFail(SymbolDeserializer::deserializeAs<Compile3Sym>(sym.get()));
659
123
        hotPatchable =
660
123
            (cs.Flags & CompileSym3Flags::HotPatch) != CompileSym3Flags::None;
661
123
      }
662
489
      if (sym->kind() == SymbolKind::S_OBJNAME) {
663
100
        auto objName = cantFail(SymbolDeserializer::deserializeAs<ObjNameSym>(
664
100
            sym.get()));
665
100
        pchSignature = objName.Signature;
666
100
      }
667
489
      offset += sym->length();
668
489
    }
669
294
  }
670
142
}
671
672
// Depending on the compilation flags, OBJs can refer to external files,
673
// necessary to merge this OBJ into the final PDB. We currently support two
674
// types of external files: Precomp/PCH OBJs, when compiling with /Yc and /Yu.
675
// And PDB type servers, when compiling with /Zi. This function extracts these
676
// dependencies and makes them available as a TpiSource interface (see
677
// DebugTypes.h). Both cases only happen with cl.exe: clang-cl produces regular
678
// output even with /Yc and /Yu and with /Zi.
679
828
void ObjFile::initializeDependencies() {
680
828
  if (!config->debug)
681
663
    return;
682
165
683
165
  bool isPCH = false;
684
165
685
165
  ArrayRef<uint8_t> data = getDebugSection(".debug$P");
686
165
  if (!data.empty())
687
4
    isPCH = true;
688
161
  else
689
161
    data = getDebugSection(".debug$T");
690
165
691
165
  if (data.empty())
692
77
    return;
693
88
694
88
  CVTypeArray types;
695
88
  BinaryStreamReader reader(data, support::little);
696
88
  cantFail(reader.readArray(types, reader.getLength()));
697
88
698
88
  CVTypeArray::Iterator firstType = types.begin();
699
88
  if (firstType == types.end())
700
0
    return;
701
88
702
88
  debugTypes.emplace(types);
703
88
704
88
  if (isPCH) {
705
4
    debugTypesObj = makePrecompSource(this);
706
4
    return;
707
4
  }
708
84
709
84
  if (firstType->kind() == LF_TYPESERVER2) {
710
17
    TypeServer2Record ts = cantFail(
711
17
        TypeDeserializer::deserializeAs<TypeServer2Record>(firstType->data()));
712
17
    debugTypesObj = makeUseTypeServerSource(this, &ts);
713
17
    return;
714
17
  }
715
67
716
67
  if (firstType->kind() == LF_PRECOMP) {
717
8
    PrecompRecord precomp = cantFail(
718
8
        TypeDeserializer::deserializeAs<PrecompRecord>(firstType->data()));
719
8
    debugTypesObj = makeUsePrecompSource(this, &precomp);
720
8
    return;
721
8
  }
722
59
723
59
  debugTypesObj = makeTpiSource(this);
724
59
}
725
726
21
StringRef ltrim1(StringRef s, const char *chars) {
727
21
  if (!s.empty() && strchr(chars, s[0]))
728
21
    return s.substr(1);
729
0
  return s;
730
0
}
731
732
109
void ImportFile::parse() {
733
109
  const char *buf = mb.getBufferStart();
734
109
  const auto *hdr = reinterpret_cast<const coff_import_header *>(buf);
735
109
736
109
  // Check if the total size is valid.
737
109
  if (mb.getBufferSize() != sizeof(*hdr) + hdr->SizeOfData)
738
0
    fatal("broken import library");
739
109
740
109
  // Read names and create an __imp_ symbol.
741
109
  StringRef name = saver.save(StringRef(buf + sizeof(*hdr)));
742
109
  StringRef impName = saver.save("__imp_" + name);
743
109
  const char *nameStart = buf + sizeof(coff_import_header) + name.size() + 1;
744
109
  dllName = StringRef(nameStart);
745
109
  StringRef extName;
746
109
  switch (hdr->getNameType()) {
747
109
  case IMPORT_ORDINAL:
748
13
    extName = "";
749
13
    break;
750
109
  case IMPORT_NAME:
751
75
    extName = name;
752
75
    break;
753
109
  case IMPORT_NAME_NOPREFIX:
754
2
    extName = ltrim1(name, "?@_");
755
2
    break;
756
109
  case IMPORT_NAME_UNDECORATE:
757
19
    extName = ltrim1(name, "?@_");
758
19
    extName = extName.substr(0, extName.find('@'));
759
19
    break;
760
109
  }
761
109
762
109
  this->hdr = hdr;
763
109
  externalName = extName;
764
109
765
109
  impSym = symtab->addImportData(impName, this);
766
109
  // If this was a duplicate, we logged an error but may continue;
767
109
  // in this case, impSym is nullptr.
768
109
  if (!impSym)
769
2
    return;
770
107
771
107
  if (hdr->getType() == llvm::COFF::IMPORT_CONST)
772
1
    static_cast<void>(symtab->addImportData(name, this));
773
107
774
107
  // If type is function, we need to create a thunk which jump to an
775
107
  // address pointed by the __imp_ symbol. (This allows you to call
776
107
  // DLL functions just like regular non-DLL functions.)
777
107
  if (hdr->getType() == llvm::COFF::IMPORT_CODE)
778
99
    thunkSym = symtab->addImportThunk(
779
99
        name, cast_or_null<DefinedImportData>(impSym), hdr->Machine);
780
107
}
781
782
BitcodeFile::BitcodeFile(MemoryBufferRef mb, StringRef archiveName,
783
                         uint64_t offsetInArchive)
784
77
    : InputFile(BitcodeKind, mb) {
785
77
  std::string path = mb.getBufferIdentifier().str();
786
77
  if (config->thinLTOIndexOnly)
787
12
    path = replaceThinLTOSuffix(mb.getBufferIdentifier());
788
77
789
77
  // ThinLTO assumes that all MemoryBufferRefs given to it have a unique
790
77
  // name. If two archives define two members with the same name, this
791
77
  // causes a collision which result in only one of the objects being taken
792
77
  // into consideration at LTO time (which very likely causes undefined
793
77
  // symbols later in the link stage). So we append file offset to make
794
77
  // filename unique.
795
77
  MemoryBufferRef mbref(
796
77
      mb.getBuffer(),
797
77
      saver.save(archiveName + path +
798
77
                 (archiveName.empty() ? 
""65
:
utostr(offsetInArchive)12
)));
799
77
800
77
  obj = check(lto::InputFile::create(mbref));
801
77
}
802
803
77
void BitcodeFile::parse() {
804
77
  std::vector<std::pair<Symbol *, bool>> comdat(obj->getComdatTable().size());
805
106
  for (size_t i = 0; i != obj->getComdatTable().size(); 
++i29
)
806
29
    // FIXME: lto::InputFile doesn't keep enough data to do correct comdat
807
29
    // selection handling.
808
29
    comdat[i] = symtab->addComdat(this, saver.save(obj->getComdatTable()[i]));
809
145
  for (const lto::InputFile::Symbol &objSym : obj->symbols()) {
810
145
    StringRef symName = saver.save(objSym.getName());
811
145
    int comdatIndex = objSym.getComdatIndex();
812
145
    Symbol *sym;
813
145
    if (objSym.isUndefined()) {
814
29
      sym = symtab->addUndefined(symName, this, false);
815
116
    } else if (objSym.isCommon()) {
816
0
      sym = symtab->addCommon(this, symName, objSym.getCommonSize());
817
116
    } else if (objSym.isWeak() && 
objSym.isIndirect()22
) {
818
2
      // Weak external.
819
2
      sym = symtab->addUndefined(symName, this, true);
820
2
      std::string fallback = objSym.getCOFFWeakExternalFallback();
821
2
      Symbol *alias = symtab->addUndefined(saver.save(fallback));
822
2
      checkAndSetWeakAlias(symtab, this, sym, alias);
823
114
    } else if (comdatIndex != -1) {
824
29
      if (symName == obj->getComdatTable()[comdatIndex])
825
29
        sym = comdat[comdatIndex].first;
826
0
      else if (comdat[comdatIndex].second)
827
0
        sym = symtab->addRegular(this, symName);
828
0
      else
829
0
        sym = symtab->addUndefined(symName, this, false);
830
85
    } else {
831
85
      sym = symtab->addRegular(this, symName);
832
85
    }
833
145
    symbols.push_back(sym);
834
145
    if (objSym.isUsed())
835
1
      config->gcroot.push_back(sym);
836
145
  }
837
77
  directives = obj->getCOFFLinkerOpts();
838
77
}
839
840
77
MachineTypes BitcodeFile::getMachineType() {
841
77
  switch (Triple(obj->getTargetTriple()).getArch()) {
842
77
  case Triple::x86_64:
843
75
    return AMD64;
844
77
  case Triple::x86:
845
2
    return I386;
846
77
  case Triple::arm:
847
0
    return ARMNT;
848
77
  case Triple::aarch64:
849
0
    return ARM64;
850
77
  default:
851
0
    return IMAGE_FILE_MACHINE_UNKNOWN;
852
77
  }
853
77
}
854
855
12
std::string replaceThinLTOSuffix(StringRef path) {
856
12
  StringRef suffix = config->thinLTOObjectSuffixReplace.first;
857
12
  StringRef repl = config->thinLTOObjectSuffixReplace.second;
858
12
859
12
  if (path.consume_back(suffix))
860
11
    return (path + repl).str();
861
1
  return path;
862
1
}
863
} // namespace coff
864
} // namespace lld
865
866
// Returns the last element of a path, which is supposed to be a filename.
867
232
static StringRef getBasename(StringRef path) {
868
232
  return sys::path::filename(path, sys::path::Style::windows);
869
232
}
870
871
// Returns a string in the format of "foo.obj" or "foo.obj(bar.lib)".
872
1.45k
std::string lld::toString(const coff::InputFile *file) {
873
1.45k
  if (!file)
874
0
    return "<internal>";
875
1.45k
  if (file->parentName.empty() || 
file->kind() == coff::InputFile::ImportKind228
)
876
1.33k
    return file->getName();
877
116
878
116
  return (getBasename(file->parentName) + "(" + getBasename(file->getName()) +
879
116
          ")")
880
116
      .str();
881
116
}