Coverage Report

Created: 2018-01-17 21:32

/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
//                             The LLVM Linker
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
10
#include "InputFiles.h"
11
#include "Chunks.h"
12
#include "Config.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/Object/Binary.h"
24
#include "llvm/Object/COFF.h"
25
#include "llvm/Support/Casting.h"
26
#include "llvm/Support/Endian.h"
27
#include "llvm/Support/Error.h"
28
#include "llvm/Support/ErrorOr.h"
29
#include "llvm/Support/FileSystem.h"
30
#include "llvm/Target/TargetOptions.h"
31
#include <cstring>
32
#include <system_error>
33
#include <utility>
34
35
using namespace llvm;
36
using namespace llvm::COFF;
37
using namespace llvm::object;
38
using namespace llvm::support::endian;
39
40
using llvm::Triple;
41
using llvm::support::ulittle32_t;
42
43
namespace lld {
44
namespace coff {
45
46
std::vector<ObjFile *> ObjFile::Instances;
47
std::vector<ImportFile *> ImportFile::Instances;
48
std::vector<BitcodeFile *> BitcodeFile::Instances;
49
50
/// Checks that Source is compatible with being a weak alias to Target.
51
/// If Source is Undefined and has no weak alias set, makes it a weak
52
/// alias to Target.
53
static void checkAndSetWeakAlias(SymbolTable *Symtab, InputFile *F,
54
0
                                 Symbol *Source, Symbol *Target) {
55
0
  if (auto *U = dyn_cast<Undefined>(Source)) {
56
0
    if (U->WeakAlias && U->WeakAlias != Target)
57
0
      Symtab->reportDuplicate(Source, F);
58
0
    U->WeakAlias = Target;
59
0
  }
60
0
}
61
62
0
ArchiveFile::ArchiveFile(MemoryBufferRef M) : InputFile(ArchiveKind, M) {}
63
64
0
void ArchiveFile::parse() {
65
0
  // Parse a MemoryBufferRef as an archive file.
66
0
  File = CHECK(Archive::create(MB), this);
67
0
68
0
  // Read the symbol table to construct Lazy objects.
69
0
  for (const Archive::Symbol &Sym : File->symbols())
70
0
    Symtab->addLazy(this, Sym);
71
0
}
72
73
// Returns a buffer pointing to a member file containing a given symbol.
74
0
void ArchiveFile::addMember(const Archive::Symbol *Sym) {
75
0
  const Archive::Child &C =
76
0
      CHECK(Sym->getMember(),
77
0
            "could not get the member for symbol " + Sym->getName());
78
0
79
0
  // Return an empty buffer if we have already returned the same buffer.
80
0
  if (!Seen.insert(C.getChildOffset()).second)
81
0
    return;
82
0
83
0
  Driver->enqueueArchiveMember(C, Sym->getName(), getName());
84
0
}
85
86
0
std::vector<MemoryBufferRef> getArchiveMembers(Archive *File) {
87
0
  std::vector<MemoryBufferRef> V;
88
0
  Error Err = Error::success();
89
0
  for (const ErrorOr<Archive::Child> &COrErr : File->children(Err)) {
90
0
    Archive::Child C =
91
0
        CHECK(COrErr,
92
0
              File->getFileName() + ": could not get the child of the archive");
93
0
    MemoryBufferRef MBRef =
94
0
        CHECK(C.getMemoryBufferRef(),
95
0
              File->getFileName() +
96
0
                  ": could not get the buffer for a child of the archive");
97
0
    V.push_back(MBRef);
98
0
  }
99
0
  if (Err)
100
0
    fatal(File->getFileName() +
101
0
          ": Archive::children failed: " + toString(std::move(Err)));
102
0
  return V;
103
0
}
104
105
0
void ObjFile::parse() {
106
0
  // Parse a memory buffer as a COFF file.
107
0
  std::unique_ptr<Binary> Bin = CHECK(createBinary(MB), this);
108
0
109
0
  if (auto *Obj = dyn_cast<COFFObjectFile>(Bin.get())) {
110
0
    Bin.release();
111
0
    COFFObj.reset(Obj);
112
0
  } else {
113
0
    fatal(toString(this) + " is not a COFF file");
114
0
  }
115
0
116
0
  // Read section and symbol tables.
117
0
  initializeChunks();
118
0
  initializeSymbols();
119
0
}
120
121
// We set SectionChunk pointers in the SparseChunks vector to this value
122
// temporarily to mark comdat sections as having an unknown resolution. As we
123
// walk the object file's symbol table, once we visit either a leader symbol or
124
// an associative section definition together with the parent comdat's leader,
125
// we set the pointer to either nullptr (to mark the section as discarded) or a
126
// valid SectionChunk for that section.
127
static SectionChunk *const PendingComdat = reinterpret_cast<SectionChunk *>(1);
128
129
0
void ObjFile::initializeChunks() {
130
0
  uint32_t NumSections = COFFObj->getNumberOfSections();
131
0
  Chunks.reserve(NumSections);
132
0
  SparseChunks.resize(NumSections + 1);
133
0
  for (uint32_t I = 1; I < NumSections + 1; ++I) {
134
0
    const coff_section *Sec;
135
0
    if (auto EC = COFFObj->getSection(I, Sec))
136
0
      fatal("getSection failed: #" + Twine(I) + ": " + EC.message());
137
0
138
0
    if (Sec->Characteristics & IMAGE_SCN_LNK_COMDAT)
139
0
      SparseChunks[I] = PendingComdat;
140
0
    else
141
0
      SparseChunks[I] = readSection(I, nullptr);
142
0
  }
143
0
}
144
145
SectionChunk *ObjFile::readSection(uint32_t SectionNumber,
146
0
                                   const coff_aux_section_definition *Def) {
147
0
  const coff_section *Sec;
148
0
  StringRef Name;
149
0
  if (auto EC = COFFObj->getSection(SectionNumber, Sec))
150
0
    fatal("getSection failed: #" + Twine(SectionNumber) + ": " + EC.message());
151
0
  if (auto EC = COFFObj->getSectionName(Sec, Name))
152
0
    fatal("getSectionName failed: #" + Twine(SectionNumber) + ": " +
153
0
          EC.message());
154
0
  if (Name == ".sxdata") {
155
0
    ArrayRef<uint8_t> Data;
156
0
    COFFObj->getSectionContents(Sec, Data);
157
0
    if (Data.size() % 4 != 0)
158
0
      fatal(".sxdata must be an array of symbol table indices");
159
0
    SXData = {reinterpret_cast<const ulittle32_t *>(Data.data()),
160
0
              Data.size() / 4};
161
0
    return nullptr;
162
0
  }
163
0
  if (Name == ".drectve") {
164
0
    ArrayRef<uint8_t> Data;
165
0
    COFFObj->getSectionContents(Sec, Data);
166
0
    Directives = std::string((const char *)Data.data(), Data.size());
167
0
    return nullptr;
168
0
  }
169
0
170
0
  // Object files may have DWARF debug info or MS CodeView debug info
171
0
  // (or both).
172
0
  //
173
0
  // DWARF sections don't need any special handling from the perspective
174
0
  // of the linker; they are just a data section containing relocations.
175
0
  // We can just link them to complete debug info.
176
0
  //
177
0
  // CodeView needs a linker support. We need to interpret and debug
178
0
  // info, and then write it to a separate .pdb file.
179
0
180
0
  // Ignore debug info unless /debug is given.
181
0
  if (!Config->Debug && Name.startswith(".debug"))
182
0
    return nullptr;
183
0
184
0
  if (Sec->Characteristics & llvm::COFF::IMAGE_SCN_LNK_REMOVE)
185
0
    return nullptr;
186
0
  auto *C = make<SectionChunk>(this, Sec);
187
0
  if (Def)
188
0
    C->Checksum = Def->CheckSum;
189
0
190
0
  // CodeView sections are stored to a different vector because they are not
191
0
  // linked in the regular manner.
192
0
  if (C->isCodeView())
193
0
    DebugChunks.push_back(C);
194
0
  else
195
0
    Chunks.push_back(C);
196
0
197
0
  return C;
198
0
}
199
200
void ObjFile::readAssociativeDefinition(
201
0
    COFFSymbolRef Sym, const coff_aux_section_definition *Def) {
202
0
  SectionChunk *Parent = SparseChunks[Def->getNumber(Sym.isBigObj())];
203
0
204
0
  // If the parent is pending, it probably means that its section definition
205
0
  // appears after us in the symbol table. Leave the associated section as
206
0
  // pending; we will handle it during the second pass in initializeSymbols().
207
0
  if (Parent == PendingComdat)
208
0
    return;
209
0
210
0
  // Check whether the parent is prevailing. If it is, so are we, and we read
211
0
  // the section; otherwise mark it as discarded.
212
0
  int32_t SectionNumber = Sym.getSectionNumber();
213
0
  if (Parent) {
214
0
    SparseChunks[SectionNumber] = readSection(SectionNumber, Def);
215
0
    if (SparseChunks[SectionNumber])
216
0
      Parent->addAssociative(SparseChunks[SectionNumber]);
217
0
  } else {
218
0
    SparseChunks[SectionNumber] = nullptr;
219
0
  }
220
0
}
221
222
0
Symbol *ObjFile::createRegular(COFFSymbolRef Sym) {
223
0
  SectionChunk *SC = SparseChunks[Sym.getSectionNumber()];
224
0
  if (Sym.isExternal()) {
225
0
    StringRef Name;
226
0
    COFFObj->getSymbolName(Sym, Name);
227
0
    if (SC)
228
0
      return Symtab->addRegular(this, Name, Sym.getGeneric(), SC);
229
0
    return Symtab->addUndefined(Name, this, false);
230
0
  }
231
0
  if (SC)
232
0
    return make<DefinedRegular>(this, /*Name*/ "", false,
233
0
                                /*IsExternal*/ false, Sym.getGeneric(), SC);
234
0
  return nullptr;
235
0
}
236
237
0
void ObjFile::initializeSymbols() {
238
0
  uint32_t NumSymbols = COFFObj->getNumberOfSymbols();
239
0
  Symbols.resize(NumSymbols);
240
0
241
0
  SmallVector<std::pair<Symbol *, uint32_t>, 8> WeakAliases;
242
0
  std::vector<uint32_t> PendingIndexes;
243
0
  PendingIndexes.reserve(NumSymbols);
244
0
245
0
  std::vector<const coff_aux_section_definition *> ComdatDefs(
246
0
      COFFObj->getNumberOfSections() + 1);
247
0
248
0
  for (uint32_t I = 0; I < NumSymbols; ++I) {
249
0
    COFFSymbolRef COFFSym = check(COFFObj->getSymbol(I));
250
0
    if (COFFSym.isUndefined()) {
251
0
      Symbols[I] = createUndefined(COFFSym);
252
0
    } else if (COFFSym.isWeakExternal()) {
253
0
      Symbols[I] = createUndefined(COFFSym);
254
0
      uint32_t TagIndex = COFFSym.getAux<coff_aux_weak_external>()->TagIndex;
255
0
      WeakAliases.emplace_back(Symbols[I], TagIndex);
256
0
    } else if (Optional<Symbol *> OptSym = createDefined(COFFSym, ComdatDefs)) {
257
0
      Symbols[I] = *OptSym;
258
0
    } else {
259
0
      // createDefined() returns None if a symbol belongs to a section that
260
0
      // was pending at the point when the symbol was read. This can happen in
261
0
      // two cases:
262
0
      // 1) section definition symbol for a comdat leader;
263
0
      // 2) symbol belongs to a comdat section associated with a section whose
264
0
      //    section definition symbol appears later in the symbol table.
265
0
      // In both of these cases, we can expect the section to be resolved by
266
0
      // the time we finish visiting the remaining symbols in the symbol
267
0
      // table. So we postpone the handling of this symbol until that time.
268
0
      PendingIndexes.push_back(I);
269
0
    }
270
0
    I += COFFSym.getNumberOfAuxSymbols();
271
0
  }
272
0
273
0
  for (uint32_t I : PendingIndexes) {
274
0
    COFFSymbolRef Sym = check(COFFObj->getSymbol(I));
275
0
    if (auto *Def = Sym.getSectionDefinition())
276
0
      if (Def->Selection == IMAGE_COMDAT_SELECT_ASSOCIATIVE)
277
0
        readAssociativeDefinition(Sym, Def);
278
0
    Symbols[I] = createRegular(Sym);
279
0
  }
280
0
281
0
  for (auto &KV : WeakAliases) {
282
0
    Symbol *Sym = KV.first;
283
0
    uint32_t Idx = KV.second;
284
0
    checkAndSetWeakAlias(Symtab, this, Sym, Symbols[Idx]);
285
0
  }
286
0
}
287
288
0
Symbol *ObjFile::createUndefined(COFFSymbolRef Sym) {
289
0
  StringRef Name;
290
0
  COFFObj->getSymbolName(Sym, Name);
291
0
  return Symtab->addUndefined(Name, this, Sym.isWeakExternal());
292
0
}
293
294
Optional<Symbol *> ObjFile::createDefined(
295
    COFFSymbolRef Sym,
296
0
    std::vector<const coff_aux_section_definition *> &ComdatDefs) {
297
0
  StringRef Name;
298
0
  if (Sym.isCommon()) {
299
0
    auto *C = make<CommonChunk>(Sym);
300
0
    Chunks.push_back(C);
301
0
    COFFObj->getSymbolName(Sym, Name);
302
0
    Symbol *S =
303
0
        Symtab->addCommon(this, Name, Sym.getValue(), Sym.getGeneric(), C);
304
0
    return S;
305
0
  }
306
0
  if (Sym.isAbsolute()) {
307
0
    COFFObj->getSymbolName(Sym, Name);
308
0
    // Skip special symbols.
309
0
    if (Name == "@comp.id")
310
0
      return nullptr;
311
0
    // COFF spec 5.10.1. The .sxdata section.
312
0
    if (Name == "@feat.00") {
313
0
      if (Sym.getValue() & 1)
314
0
        SEHCompat = true;
315
0
      return nullptr;
316
0
    }
317
0
    if (Sym.isExternal())
318
0
      return Symtab->addAbsolute(Name, Sym);
319
0
    else
320
0
      return make<DefinedAbsolute>(Name, Sym);
321
0
  }
322
0
  int32_t SectionNumber = Sym.getSectionNumber();
323
0
  if (SectionNumber == llvm::COFF::IMAGE_SYM_DEBUG)
324
0
    return nullptr;
325
0
326
0
  // Reserved sections numbers don't have contents.
327
0
  if (llvm::COFF::isReservedSectionNumber(SectionNumber))
328
0
    fatal("broken object file: " + toString(this));
329
0
330
0
  // This symbol references a section which is not present in the section
331
0
  // header.
332
0
  if ((uint32_t)SectionNumber >= SparseChunks.size())
333
0
    fatal("broken object file: " + toString(this));
334
0
335
0
  // Handle comdat leader symbols.
336
0
  if (const coff_aux_section_definition *Def = ComdatDefs[SectionNumber]) {
337
0
    ComdatDefs[SectionNumber] = nullptr;
338
0
    Symbol *Leader;
339
0
    bool Prevailing;
340
0
    if (Sym.isExternal()) {
341
0
      COFFObj->getSymbolName(Sym, Name);
342
0
      std::tie(Leader, Prevailing) =
343
0
          Symtab->addComdat(this, Name, Sym.getGeneric());
344
0
    } else {
345
0
      Leader = make<DefinedRegular>(this, /*Name*/ "", false,
346
0
                                    /*IsExternal*/ false, Sym.getGeneric());
347
0
      Prevailing = true;
348
0
    }
349
0
    if (Prevailing) {
350
0
      SectionChunk *C = readSection(SectionNumber, Def);
351
0
      SparseChunks[SectionNumber] = C;
352
0
      C->Sym = cast<DefinedRegular>(Leader);
353
0
      cast<DefinedRegular>(Leader)->Data = &C->Repl;
354
0
    } else {
355
0
      SparseChunks[SectionNumber] = nullptr;
356
0
    }
357
0
    return Leader;
358
0
  }
359
0
360
0
  // Read associative section definitions and prepare to handle the comdat
361
0
  // leader symbol by setting the section's ComdatDefs pointer if we encounter a
362
0
  // non-associative comdat.
363
0
  if (SparseChunks[SectionNumber] == PendingComdat) {
364
0
    if (auto *Def = Sym.getSectionDefinition()) {
365
0
      if (Def->Selection == IMAGE_COMDAT_SELECT_ASSOCIATIVE)
366
0
        readAssociativeDefinition(Sym, Def);
367
0
      else
368
0
        ComdatDefs[SectionNumber] = Def;
369
0
    }
370
0
  }
371
0
372
0
  if (SparseChunks[SectionNumber] == PendingComdat)
373
0
    return None;
374
0
  return createRegular(Sym);
375
0
}
376
377
0
MachineTypes ObjFile::getMachineType() {
378
0
  if (COFFObj)
379
0
    return static_cast<MachineTypes>(COFFObj->getMachine());
380
0
  return IMAGE_FILE_MACHINE_UNKNOWN;
381
0
}
382
383
0
StringRef ltrim1(StringRef S, const char *Chars) {
384
0
  if (!S.empty() && strchr(Chars, S[0]))
385
0
    return S.substr(1);
386
0
  return S;
387
0
}
388
389
0
void ImportFile::parse() {
390
0
  const char *Buf = MB.getBufferStart();
391
0
  const char *End = MB.getBufferEnd();
392
0
  const auto *Hdr = reinterpret_cast<const coff_import_header *>(Buf);
393
0
394
0
  // Check if the total size is valid.
395
0
  if ((size_t)(End - Buf) != (sizeof(*Hdr) + Hdr->SizeOfData))
396
0
    fatal("broken import library");
397
0
398
0
  // Read names and create an __imp_ symbol.
399
0
  StringRef Name = Saver.save(StringRef(Buf + sizeof(*Hdr)));
400
0
  StringRef ImpName = Saver.save("__imp_" + Name);
401
0
  const char *NameStart = Buf + sizeof(coff_import_header) + Name.size() + 1;
402
0
  DLLName = StringRef(NameStart);
403
0
  StringRef ExtName;
404
0
  switch (Hdr->getNameType()) {
405
0
  case IMPORT_ORDINAL:
406
0
    ExtName = "";
407
0
    break;
408
0
  case IMPORT_NAME:
409
0
    ExtName = Name;
410
0
    break;
411
0
  case IMPORT_NAME_NOPREFIX:
412
0
    ExtName = ltrim1(Name, "?@_");
413
0
    break;
414
0
  case IMPORT_NAME_UNDECORATE:
415
0
    ExtName = ltrim1(Name, "?@_");
416
0
    ExtName = ExtName.substr(0, ExtName.find('@'));
417
0
    break;
418
0
  }
419
0
420
0
  this->Hdr = Hdr;
421
0
  ExternalName = ExtName;
422
0
423
0
  ImpSym = Symtab->addImportData(ImpName, this);
424
0
425
0
  if (Hdr->getType() == llvm::COFF::IMPORT_CONST)
426
0
    static_cast<void>(Symtab->addImportData(Name, this));
427
0
428
0
  // If type is function, we need to create a thunk which jump to an
429
0
  // address pointed by the __imp_ symbol. (This allows you to call
430
0
  // DLL functions just like regular non-DLL functions.)
431
0
  if (Hdr->getType() == llvm::COFF::IMPORT_CODE)
432
0
    ThunkSym = Symtab->addImportThunk(Name, ImpSym, Hdr->Machine);
433
0
}
434
435
0
void BitcodeFile::parse() {
436
0
  Obj = check(lto::InputFile::create(MemoryBufferRef(
437
0
      MB.getBuffer(), Saver.save(ParentName + MB.getBufferIdentifier()))));
438
0
  std::vector<std::pair<Symbol *, bool>> Comdat(Obj->getComdatTable().size());
439
0
  for (size_t I = 0; I != Obj->getComdatTable().size(); ++I)
440
0
    Comdat[I] = Symtab->addComdat(this, Saver.save(Obj->getComdatTable()[I]));
441
0
  for (const lto::InputFile::Symbol &ObjSym : Obj->symbols()) {
442
0
    StringRef SymName = Saver.save(ObjSym.getName());
443
0
    int ComdatIndex = ObjSym.getComdatIndex();
444
0
    Symbol *Sym;
445
0
    if (ObjSym.isUndefined()) {
446
0
      Sym = Symtab->addUndefined(SymName, this, false);
447
0
    } else if (ObjSym.isCommon()) {
448
0
      Sym = Symtab->addCommon(this, SymName, ObjSym.getCommonSize());
449
0
    } else if (ObjSym.isWeak() && ObjSym.isIndirect()) {
450
0
      // Weak external.
451
0
      Sym = Symtab->addUndefined(SymName, this, true);
452
0
      std::string Fallback = ObjSym.getCOFFWeakExternalFallback();
453
0
      Symbol *Alias = Symtab->addUndefined(Saver.save(Fallback));
454
0
      checkAndSetWeakAlias(Symtab, this, Sym, Alias);
455
0
    } else if (ComdatIndex != -1) {
456
0
      if (SymName == Obj->getComdatTable()[ComdatIndex])
457
0
        Sym = Comdat[ComdatIndex].first;
458
0
      else if (Comdat[ComdatIndex].second)
459
0
        Sym = Symtab->addRegular(this, SymName);
460
0
      else
461
0
        Sym = Symtab->addUndefined(SymName, this, false);
462
0
    } else {
463
0
      Sym = Symtab->addRegular(this, SymName);
464
0
    }
465
0
    SymbolBodies.push_back(Sym);
466
0
  }
467
0
  Directives = Obj->getCOFFLinkerOpts();
468
0
}
469
470
0
MachineTypes BitcodeFile::getMachineType() {
471
0
  switch (Triple(Obj->getTargetTriple()).getArch()) {
472
0
  case Triple::x86_64:
473
0
    return AMD64;
474
0
  case Triple::x86:
475
0
    return I386;
476
0
  case Triple::arm:
477
0
    return ARMNT;
478
0
  case Triple::aarch64:
479
0
    return ARM64;
480
0
  default:
481
0
    return IMAGE_FILE_MACHINE_UNKNOWN;
482
0
  }
483
0
}
484
} // namespace coff
485
} // namespace lld
486
487
// Returns the last element of a path, which is supposed to be a filename.
488
0
static StringRef getBasename(StringRef Path) {
489
0
  size_t Pos = Path.find_last_of("\\/");
490
0
  if (Pos == StringRef::npos)
491
0
    return Path;
492
0
  return Path.substr(Pos + 1);
493
0
}
494
495
// Returns a string in the format of "foo.obj" or "foo.obj(bar.lib)".
496
0
std::string lld::toString(const coff::InputFile *File) {
497
0
  if (!File)
498
0
    return "<internal>";
499
0
  if (File->ParentName.empty())
500
0
    return File->getName();
501
0
502
0
  return (getBasename(File->ParentName) + "(" + getBasename(File->getName()) +
503
0
          ")")
504
0
      .str();
505
0
}