2015-12-05 07:11:05 +08:00
|
|
|
//===- PDB.cpp ------------------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2015-12-05 07:11:05 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-09-16 06:24:51 +08:00
|
|
|
#include "PDB.h"
|
2016-11-12 08:00:51 +08:00
|
|
|
#include "Chunks.h"
|
2016-11-22 01:22:35 +08:00
|
|
|
#include "Config.h"
|
[LLD][COFF] Early dependency detection
We introduce a new class hierarchy for debug types merging (in DebugTypes.h). The end-goal is to parallelize the type merging - please see the plan in D59226.
Previously, dependency discovery was done on the fly, much later, during the type merging loop. Unfortunately, parallelizing the type merging requires the dependencies to be merged in first, before any dependent ObjFile, thus this early discovery.
The overall intention for this path is to discover debug information dependencies at a much earlier stage, when processing input files. Currently, two types of dependency are supported: PDB type servers (when compiling with MSVC /Zi) and precompiled headers OBJs (when compiling with MSVC /Yc and /Yu). Once discovered, an explicit link is added into the dependent ObjFile, through the new debug types class hierarchy introduced in DebugTypes.h.
Differential Revision: https://reviews.llvm.org/D59053
llvm-svn: 357383
2019-04-01 21:36:59 +08:00
|
|
|
#include "DebugTypes.h"
|
2017-10-21 03:48:26 +08:00
|
|
|
#include "Driver.h"
|
2016-11-12 08:00:51 +08:00
|
|
|
#include "SymbolTable.h"
|
|
|
|
#include "Symbols.h"
|
2019-04-03 04:43:19 +08:00
|
|
|
#include "TypeMerger.h"
|
2017-07-28 02:25:59 +08:00
|
|
|
#include "Writer.h"
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
#include "lld/Common/ErrorHandler.h"
|
2018-01-18 03:16:26 +08:00
|
|
|
#include "lld/Common/Timer.h"
|
2019-02-28 04:53:50 +08:00
|
|
|
#include "lld/Common/Threads.h"
|
2018-09-12 06:35:01 +08:00
|
|
|
#include "llvm/DebugInfo/CodeView/DebugFrameDataSubsection.h"
|
2017-06-20 01:21:45 +08:00
|
|
|
#include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h"
|
2017-12-15 02:07:04 +08:00
|
|
|
#include "llvm/DebugInfo/CodeView/GlobalTypeTableBuilder.h"
|
2017-05-20 03:26:58 +08:00
|
|
|
#include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h"
|
2017-12-01 02:39:50 +08:00
|
|
|
#include "llvm/DebugInfo/CodeView/MergingTypeTableBuilder.h"
|
2017-08-12 03:00:03 +08:00
|
|
|
#include "llvm/DebugInfo/CodeView/RecordName.h"
|
2017-08-09 02:34:44 +08:00
|
|
|
#include "llvm/DebugInfo/CodeView/SymbolDeserializer.h"
|
2018-12-07 01:49:15 +08:00
|
|
|
#include "llvm/DebugInfo/CodeView/SymbolRecordHelpers.h"
|
2017-07-11 05:01:37 +08:00
|
|
|
#include "llvm/DebugInfo/CodeView/SymbolSerializer.h"
|
2017-07-18 08:21:25 +08:00
|
|
|
#include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
|
[CodeView] Finish decoupling TypeDatabase from TypeDumper.
Previously the type dumper itself was passed around to a lot of different
places and manipulated in ways that were more appropriate on the type
database. For example, the entire TypeDumper was passed into the symbol
dumper, when all the symbol dumper wanted to do was lookup the name of a
TypeIndex so it could print it. That's what the TypeDatabase is for --
mapping type indices to names.
Another example is how if the user runs llvm-pdbdump with the option to
dump symbols but not types, we still have to visit all types so that we
can print minimal information about the type of a symbol, but just without
dumping full symbol records. The way we did this before is by hacking it
up so that we run everything through the type dumper with a null printer,
so that the output goes to /dev/null. But really, we don't need to dump
anything, all we want to do is build the type database. Since
TypeDatabaseVisitor now exists independently of TypeDumper, we can do
this. We just build a custom visitor callback pipeline that includes a
database visitor but not a dumper.
All the hackery around printers etc goes away. After this patch, we could
probably even delete the entire CVTypeDumper class since really all it is
at this point is a thin wrapper that hides the details of how to build a
useful visitation pipeline. It's not a priority though, so CVTypeDumper
remains for now.
After this patch we will be able to easily plug in a different style of
type dumper by only implementing the proper visitation methods to dump
one-line output and then sticking it on the pipeline.
Differential Revision: https://reviews.llvm.org/D28524
llvm-svn: 291724
2017-01-12 07:24:22 +08:00
|
|
|
#include "llvm/DebugInfo/CodeView/TypeDumpVisitor.h"
|
2017-06-22 01:25:56 +08:00
|
|
|
#include "llvm/DebugInfo/CodeView/TypeIndexDiscovery.h"
|
2017-01-12 11:09:25 +08:00
|
|
|
#include "llvm/DebugInfo/CodeView/TypeStreamMerger.h"
|
2016-09-16 12:32:33 +08:00
|
|
|
#include "llvm/DebugInfo/MSF/MSFBuilder.h"
|
2016-09-16 02:55:18 +08:00
|
|
|
#include "llvm/DebugInfo/MSF/MSFCommon.h"
|
2017-07-18 08:21:25 +08:00
|
|
|
#include "llvm/DebugInfo/PDB/GenericError.h"
|
2017-07-11 05:01:37 +08:00
|
|
|
#include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h"
|
2017-01-26 06:38:55 +08:00
|
|
|
#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
|
|
|
|
#include "llvm/DebugInfo/PDB/Native/DbiStreamBuilder.h"
|
2017-08-09 12:23:25 +08:00
|
|
|
#include "llvm/DebugInfo/PDB/Native/GSIStreamBuilder.h"
|
2017-01-26 06:38:55 +08:00
|
|
|
#include "llvm/DebugInfo/PDB/Native/InfoStream.h"
|
|
|
|
#include "llvm/DebugInfo/PDB/Native/InfoStreamBuilder.h"
|
2017-07-18 08:21:25 +08:00
|
|
|
#include "llvm/DebugInfo/PDB/Native/NativeSession.h"
|
2017-01-26 06:38:55 +08:00
|
|
|
#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
|
|
|
|
#include "llvm/DebugInfo/PDB/Native/PDBFileBuilder.h"
|
2017-05-03 04:19:42 +08:00
|
|
|
#include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h"
|
2017-07-20 01:26:07 +08:00
|
|
|
#include "llvm/DebugInfo/PDB/Native/TpiHashing.h"
|
2017-01-26 06:38:55 +08:00
|
|
|
#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
|
|
|
|
#include "llvm/DebugInfo/PDB/Native/TpiStreamBuilder.h"
|
2017-07-18 08:21:25 +08:00
|
|
|
#include "llvm/DebugInfo/PDB/PDB.h"
|
2016-11-01 05:09:21 +08:00
|
|
|
#include "llvm/Object/COFF.h"
|
2018-03-27 07:43:29 +08:00
|
|
|
#include "llvm/Object/CVDebugRecord.h"
|
2017-03-03 04:52:51 +08:00
|
|
|
#include "llvm/Support/BinaryByteStream.h"
|
2015-12-09 02:39:55 +08:00
|
|
|
#include "llvm/Support/Endian.h"
|
2018-11-06 03:20:47 +08:00
|
|
|
#include "llvm/Support/Errc.h"
|
2018-01-06 03:12:40 +08:00
|
|
|
#include "llvm/Support/FormatVariadic.h"
|
2017-08-08 04:23:45 +08:00
|
|
|
#include "llvm/Support/JamCRC.h"
|
2017-02-17 07:35:45 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2016-11-22 01:22:35 +08:00
|
|
|
#include "llvm/Support/ScopedPrinter.h"
|
2015-12-05 07:11:05 +08:00
|
|
|
#include <memory>
|
|
|
|
|
2016-09-16 06:24:51 +08:00
|
|
|
using namespace lld;
|
2016-11-12 08:00:51 +08:00
|
|
|
using namespace lld::coff;
|
2015-12-05 07:11:05 +08:00
|
|
|
using namespace llvm;
|
2016-11-22 01:22:35 +08:00
|
|
|
using namespace llvm::codeview;
|
2015-12-05 07:11:05 +08:00
|
|
|
|
2016-11-12 08:00:51 +08:00
|
|
|
using llvm::object::coff_section;
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
static ExitOnError exitOnErr;
|
2016-09-16 12:32:33 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
static Timer totalPdbLinkTimer("PDB Emission (Cumulative)", Timer::root());
|
2018-01-18 03:16:26 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
static Timer addObjectsTimer("Add Objects", totalPdbLinkTimer);
|
|
|
|
static Timer typeMergingTimer("Type Merging", addObjectsTimer);
|
|
|
|
static Timer symbolMergingTimer("Symbol Merging", addObjectsTimer);
|
|
|
|
static Timer globalsLayoutTimer("Globals Stream Layout", totalPdbLinkTimer);
|
|
|
|
static Timer tpiStreamLayoutTimer("TPI Stream Layout", totalPdbLinkTimer);
|
|
|
|
static Timer diskCommitTimer("Commit to Disk", totalPdbLinkTimer);
|
2018-01-18 03:16:26 +08:00
|
|
|
|
2017-07-14 08:14:58 +08:00
|
|
|
namespace {
|
2018-09-13 05:02:01 +08:00
|
|
|
class DebugSHandler;
|
|
|
|
|
2017-07-14 08:14:58 +08:00
|
|
|
class PDBLinker {
|
2018-09-13 05:02:01 +08:00
|
|
|
friend DebugSHandler;
|
|
|
|
|
2017-07-14 08:14:58 +08:00
|
|
|
public:
|
2019-07-11 13:40:30 +08:00
|
|
|
PDBLinker(SymbolTable *symtab)
|
|
|
|
: alloc(), symtab(symtab), builder(alloc), tMerger(alloc) {
|
2018-03-24 02:43:39 +08:00
|
|
|
// This isn't strictly necessary, but link.exe usually puts an empty string
|
|
|
|
// as the first "valid" string in the string table, so we do the same in
|
|
|
|
// order to maintain as much byte-for-byte compatibility as possible.
|
2019-07-11 13:40:30 +08:00
|
|
|
pdbStrTab.insert("");
|
2018-03-24 02:43:39 +08:00
|
|
|
}
|
2017-07-14 08:14:58 +08:00
|
|
|
|
|
|
|
/// Emit the basic PDB structure: initial streams, headers, etc.
|
2019-07-11 13:40:30 +08:00
|
|
|
void initialize(llvm::codeview::DebugInfo *buildId);
|
2017-07-14 08:14:58 +08:00
|
|
|
|
2018-03-24 03:57:25 +08:00
|
|
|
/// Add natvis files specified on the command line.
|
|
|
|
void addNatvisFiles();
|
|
|
|
|
2017-07-14 08:14:58 +08:00
|
|
|
/// Link CodeView from each object file in the symbol table into the PDB.
|
|
|
|
void addObjectsToPDB();
|
|
|
|
|
2019-03-30 04:25:34 +08:00
|
|
|
/// Link info for each import file in the symbol table into the PDB.
|
2019-07-11 13:40:30 +08:00
|
|
|
void addImportFilesToPDB(ArrayRef<OutputSection *> outputSections);
|
2019-03-30 04:25:34 +08:00
|
|
|
|
2018-11-06 03:20:47 +08:00
|
|
|
/// Link CodeView from a single object file into the target (output) PDB.
|
|
|
|
/// When a precompiled headers object is linked, its TPI map might be provided
|
|
|
|
/// externally.
|
2019-07-11 13:40:30 +08:00
|
|
|
void addObjFile(ObjFile *file, CVIndexMap *externIndexMap = nullptr);
|
2017-07-14 08:14:58 +08:00
|
|
|
|
2017-07-18 08:21:25 +08:00
|
|
|
/// Produce a mapping from the type and item indices used in the object
|
|
|
|
/// file to those in the destination PDB.
|
|
|
|
///
|
|
|
|
/// If the object file uses a type server PDB (compiled with /Zi), merge TPI
|
|
|
|
/// and IPI from the type server PDB and return a map for it. Each unique type
|
|
|
|
/// server PDB is merged at most once, so this may return an existing index
|
|
|
|
/// mapping.
|
|
|
|
///
|
|
|
|
/// If the object does not use a type server PDB (compiled with /Z7), we merge
|
|
|
|
/// all the type and item records from the .debug$S stream and fill in the
|
2019-07-16 16:26:38 +08:00
|
|
|
/// caller-provided objectIndexMap.
|
2019-07-11 13:40:30 +08:00
|
|
|
Expected<const CVIndexMap &> mergeDebugT(ObjFile *file,
|
|
|
|
CVIndexMap *objectIndexMap);
|
2017-07-18 08:21:25 +08:00
|
|
|
|
2018-11-06 03:20:47 +08:00
|
|
|
/// Reads and makes available a PDB.
|
2019-07-11 13:40:30 +08:00
|
|
|
Expected<const CVIndexMap &> maybeMergeTypeServerPDB(ObjFile *file);
|
2018-11-06 03:20:47 +08:00
|
|
|
|
|
|
|
/// Merges a precompiled headers TPI map into the current TPI map. The
|
|
|
|
/// precompiled headers object will also be loaded and remapped in the
|
|
|
|
/// process.
|
2019-07-11 13:40:30 +08:00
|
|
|
Error mergeInPrecompHeaderObj(ObjFile *file, CVIndexMap *objectIndexMap);
|
2018-11-06 03:20:47 +08:00
|
|
|
|
|
|
|
/// Reads and makes available a precompiled headers object.
|
|
|
|
///
|
|
|
|
/// This is a requirement for objects compiled with cl.exe /Yu. In that
|
|
|
|
/// case, the referenced object (which was compiled with /Yc) has to be loaded
|
|
|
|
/// first. This is mainly because the current object's TPI stream has external
|
|
|
|
/// references to the precompiled headers object.
|
|
|
|
///
|
|
|
|
/// If the precompiled headers object was already loaded, this function will
|
|
|
|
/// simply return its (remapped) TPI map.
|
2019-07-11 13:40:30 +08:00
|
|
|
Expected<const CVIndexMap &> aquirePrecompObj(ObjFile *file);
|
2018-11-06 03:20:47 +08:00
|
|
|
|
|
|
|
/// Adds a precompiled headers object signature -> TPI mapping.
|
|
|
|
std::pair<CVIndexMap &, bool /*already there*/>
|
2019-07-11 13:40:30 +08:00
|
|
|
registerPrecompiledHeaders(uint32_t signature);
|
2017-07-14 08:14:58 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
void mergeSymbolRecords(ObjFile *file, const CVIndexMap &indexMap,
|
|
|
|
std::vector<ulittle32_t *> &stringTableRefs,
|
|
|
|
BinaryStreamRef symData);
|
2018-11-14 07:44:39 +08:00
|
|
|
|
2017-07-14 08:14:58 +08:00
|
|
|
/// Add the section map and section contributions to the PDB.
|
2019-07-11 13:40:30 +08:00
|
|
|
void addSections(ArrayRef<OutputSection *> outputSections,
|
|
|
|
ArrayRef<uint8_t> sectionTable);
|
2017-08-04 05:15:09 +08:00
|
|
|
|
2018-09-16 02:37:22 +08:00
|
|
|
/// Write the PDB to disk and store the Guid generated for it in *Guid.
|
2019-07-11 13:40:30 +08:00
|
|
|
void commit(codeview::GUID *guid);
|
2017-07-14 08:14:58 +08:00
|
|
|
|
2019-03-15 02:45:08 +08:00
|
|
|
// Print statistics regarding the final PDB
|
|
|
|
void printStats();
|
|
|
|
|
2017-07-14 08:14:58 +08:00
|
|
|
private:
|
2019-07-11 13:40:30 +08:00
|
|
|
BumpPtrAllocator alloc;
|
2017-07-14 08:14:58 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
SymbolTable *symtab;
|
2017-07-14 08:14:58 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
pdb::PDBFileBuilder builder;
|
2017-07-14 08:14:58 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
TypeMerger tMerger;
|
2017-12-15 02:07:04 +08:00
|
|
|
|
2017-07-14 08:14:58 +08:00
|
|
|
/// PDBs use a single global string table for filenames in the file checksum
|
|
|
|
/// table.
|
2019-07-11 13:40:30 +08:00
|
|
|
DebugStringTableSubsection pdbStrTab;
|
2017-07-14 08:14:58 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
llvm::SmallString<128> nativePath;
|
2017-07-14 08:14:58 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
std::vector<pdb::SecMapEntry> sectionMap;
|
2017-07-18 08:21:25 +08:00
|
|
|
|
|
|
|
/// Type index mappings of type server PDBs that we've loaded so far.
|
2019-07-11 13:40:30 +08:00
|
|
|
std::map<codeview::GUID, CVIndexMap> typeServerIndexMappings;
|
2018-02-01 01:48:04 +08:00
|
|
|
|
2018-11-06 03:20:47 +08:00
|
|
|
/// Type index mappings of precompiled objects type map that we've loaded so
|
|
|
|
/// far.
|
2019-07-11 13:40:30 +08:00
|
|
|
std::map<uint32_t, CVIndexMap> precompTypeIndexMappings;
|
2018-11-06 03:20:47 +08:00
|
|
|
|
2019-03-15 02:45:08 +08:00
|
|
|
// For statistics
|
2019-07-11 13:40:30 +08:00
|
|
|
uint64_t globalSymbols = 0;
|
|
|
|
uint64_t moduleSymbols = 0;
|
|
|
|
uint64_t publicSymbols = 0;
|
2017-07-14 08:14:58 +08:00
|
|
|
};
|
2018-09-13 05:02:01 +08:00
|
|
|
|
|
|
|
class DebugSHandler {
|
2019-07-11 13:40:30 +08:00
|
|
|
PDBLinker &linker;
|
2018-09-13 05:02:01 +08:00
|
|
|
|
|
|
|
/// The object file whose .debug$S sections we're processing.
|
2019-07-11 13:40:30 +08:00
|
|
|
ObjFile &file;
|
2018-09-13 05:02:01 +08:00
|
|
|
|
|
|
|
/// The result of merging type indices.
|
2019-07-11 13:40:30 +08:00
|
|
|
const CVIndexMap &indexMap;
|
2018-09-13 05:02:01 +08:00
|
|
|
|
|
|
|
/// The DEBUG_S_STRINGTABLE subsection. These strings are referred to by
|
|
|
|
/// index from other records in the .debug$S section. All of these strings
|
|
|
|
/// need to be added to the global PDB string table, and all references to
|
|
|
|
/// these strings need to have their indices re-written to refer to the
|
|
|
|
/// global PDB string table.
|
2019-07-11 13:40:30 +08:00
|
|
|
DebugStringTableSubsectionRef cVStrTab;
|
2018-09-13 05:02:01 +08:00
|
|
|
|
|
|
|
/// The DEBUG_S_FILECHKSMS subsection. As above, these are referred to
|
|
|
|
/// by other records in the .debug$S section and need to be merged into the
|
|
|
|
/// PDB.
|
2019-07-11 13:40:30 +08:00
|
|
|
DebugChecksumsSubsectionRef checksums;
|
2018-09-13 05:02:01 +08:00
|
|
|
|
2019-06-04 02:15:38 +08:00
|
|
|
/// The DEBUG_S_INLINEELINES subsection. There can be only one of these per
|
|
|
|
/// object file.
|
2019-07-11 13:40:30 +08:00
|
|
|
DebugInlineeLinesSubsectionRef inlineeLines;
|
2019-06-04 02:15:38 +08:00
|
|
|
|
2018-09-13 05:02:01 +08:00
|
|
|
/// The DEBUG_S_FRAMEDATA subsection(s). There can be more than one of
|
|
|
|
/// these and they need not appear in any specific order. However, they
|
|
|
|
/// contain string table references which need to be re-written, so we
|
|
|
|
/// collect them all here and re-write them after all subsections have been
|
|
|
|
/// discovered and processed.
|
2019-07-11 13:40:30 +08:00
|
|
|
std::vector<DebugFrameDataSubsectionRef> newFpoFrames;
|
2018-09-13 05:02:01 +08:00
|
|
|
|
|
|
|
/// Pointers to raw memory that we determine have string table references
|
|
|
|
/// that need to be re-written. We first process all .debug$S subsections
|
|
|
|
/// to ensure that we can handle subsections written in any order, building
|
|
|
|
/// up this list as we go. At the end, we use the string table (which must
|
|
|
|
/// have been discovered by now else it is an error) to re-write these
|
|
|
|
/// references.
|
2019-07-11 13:40:30 +08:00
|
|
|
std::vector<ulittle32_t *> stringTableReferences;
|
2018-09-13 05:02:01 +08:00
|
|
|
|
|
|
|
public:
|
2019-07-11 13:40:30 +08:00
|
|
|
DebugSHandler(PDBLinker &linker, ObjFile &file, const CVIndexMap &indexMap)
|
|
|
|
: linker(linker), file(file), indexMap(indexMap) {}
|
2018-09-13 05:02:01 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
void handleDebugS(lld::coff::SectionChunk &debugS);
|
2019-06-04 02:15:38 +08:00
|
|
|
|
|
|
|
std::shared_ptr<DebugInlineeLinesSubsection>
|
2019-07-11 13:40:30 +08:00
|
|
|
mergeInlineeLines(DebugChecksumsSubsection *newChecksums);
|
2019-06-04 02:15:38 +08:00
|
|
|
|
2018-09-13 05:02:01 +08:00
|
|
|
void finish();
|
|
|
|
};
|
2017-07-14 08:14:58 +08:00
|
|
|
}
|
|
|
|
|
lld-link: Use /pdbsourcepath: for more places when present.
/pdbsourcepath: was added in https://reviews.llvm.org/D48882 to make it
possible to have relative paths in the debug info that clang-cl writes.
lld-link then makes the paths absolute at link time, which debuggers require.
This way, clang-cl's output is independent of the absolute path of the build
directory, which is useful for cacheability in distcc-like systems.
This patch extends /pdbsourcepath: (if passed) to also be used for:
1. The "cwd" stored in the env block in the pdb is /pdbsourcepath: if present
2. The "exe" stored in the env block in the pdb is made absolute relative
to /pdbsourcepath: instead of the cwd
3. The "pdb" stored in the env block in the pdb is made absolute relative
to /pdbsourcepath: instead of the cwd
4. For making absolute paths to .obj files referenced from the pdb
/pdbsourcepath: is now useful in three scenarios (the first one already working
before this change):
1. When building with full debug info, passing the real build dir to
/pdbsourcepath: allows having clang-cl's output to be independent
of the build directory path. This patch effectively doesn't change
behavior for this use case (assuming the cwd is the build dir).
2. When building without compile-time debug info but linking with /debug,
a fake fixed /pdbsourcepath: can be passed to get symbolized stacks
while making the pdb and exe independent of the current build dir.
For this two work, lld-link needs to be invoked with relative paths for
the lld-link invocation itself (for "exe"), for the pdb output name, the exe
output name (for "pdb"), and the obj input files, and no absolute path
must appear on the link command (for "cmd" in the pdb's env block).
Since no full debug info is present, it doesn't matter that the absolute
path doesn't exist on disk -- we only get symbols in stacks.
3. When building production builds with full debug info that don't have
local changes, and that get source indexed and their pdbs get uploaded
to a symbol server. /pdbsourcepath: again makes the build output independent
of the current directory, and the fixed path passed to /pdbsourcepath: can
be given the source indexing transform so that it gets mapped to a
repository path. This has the same requirements as 2.
This patch also makes it possible to create PDB files containing Windows-style
absolute paths when cross-compiling on a POSIX system.
Differential Revision: https://reviews.llvm.org/D53021
llvm-svn: 344061
2018-10-10 01:52:25 +08:00
|
|
|
// Visual Studio's debugger requires absolute paths in various places in the
|
|
|
|
// PDB to work without additional configuration:
|
|
|
|
// https://docs.microsoft.com/en-us/visualstudio/debugger/debug-source-files-common-properties-solution-property-pages-dialog-box
|
2019-07-11 13:40:30 +08:00
|
|
|
static void pdbMakeAbsolute(SmallVectorImpl<char> &fileName) {
|
2018-10-13 01:26:19 +08:00
|
|
|
// The default behavior is to produce paths that are valid within the context
|
|
|
|
// of the machine that you perform the link on. If the linker is running on
|
|
|
|
// a POSIX system, we will output absolute POSIX paths. If the linker is
|
|
|
|
// running on a Windows system, we will output absolute Windows paths. If the
|
|
|
|
// user desires any other kind of behavior, they should explicitly pass
|
|
|
|
// /pdbsourcepath, in which case we will treat the exact string the user
|
|
|
|
// passed in as the gospel and not normalize, canonicalize it.
|
2019-07-11 13:40:30 +08:00
|
|
|
if (sys::path::is_absolute(fileName, sys::path::Style::windows) ||
|
|
|
|
sys::path::is_absolute(fileName, sys::path::Style::posix))
|
lld-link: Use /pdbsourcepath: for more places when present.
/pdbsourcepath: was added in https://reviews.llvm.org/D48882 to make it
possible to have relative paths in the debug info that clang-cl writes.
lld-link then makes the paths absolute at link time, which debuggers require.
This way, clang-cl's output is independent of the absolute path of the build
directory, which is useful for cacheability in distcc-like systems.
This patch extends /pdbsourcepath: (if passed) to also be used for:
1. The "cwd" stored in the env block in the pdb is /pdbsourcepath: if present
2. The "exe" stored in the env block in the pdb is made absolute relative
to /pdbsourcepath: instead of the cwd
3. The "pdb" stored in the env block in the pdb is made absolute relative
to /pdbsourcepath: instead of the cwd
4. For making absolute paths to .obj files referenced from the pdb
/pdbsourcepath: is now useful in three scenarios (the first one already working
before this change):
1. When building with full debug info, passing the real build dir to
/pdbsourcepath: allows having clang-cl's output to be independent
of the build directory path. This patch effectively doesn't change
behavior for this use case (assuming the cwd is the build dir).
2. When building without compile-time debug info but linking with /debug,
a fake fixed /pdbsourcepath: can be passed to get symbolized stacks
while making the pdb and exe independent of the current build dir.
For this two work, lld-link needs to be invoked with relative paths for
the lld-link invocation itself (for "exe"), for the pdb output name, the exe
output name (for "pdb"), and the obj input files, and no absolute path
must appear on the link command (for "cmd" in the pdb's env block).
Since no full debug info is present, it doesn't matter that the absolute
path doesn't exist on disk -- we only get symbols in stacks.
3. When building production builds with full debug info that don't have
local changes, and that get source indexed and their pdbs get uploaded
to a symbol server. /pdbsourcepath: again makes the build output independent
of the current directory, and the fixed path passed to /pdbsourcepath: can
be given the source indexing transform so that it gets mapped to a
repository path. This has the same requirements as 2.
This patch also makes it possible to create PDB files containing Windows-style
absolute paths when cross-compiling on a POSIX system.
Differential Revision: https://reviews.llvm.org/D53021
llvm-svn: 344061
2018-10-10 01:52:25 +08:00
|
|
|
return;
|
2018-10-13 01:26:19 +08:00
|
|
|
|
|
|
|
// It's not absolute in any path syntax. Relative paths necessarily refer to
|
|
|
|
// the local file system, so we can make it native without ending up with a
|
|
|
|
// nonsensical path.
|
2019-07-11 13:40:30 +08:00
|
|
|
if (config->pdbSourcePath.empty()) {
|
|
|
|
sys::path::native(fileName);
|
|
|
|
sys::fs::make_absolute(fileName);
|
lld-link: Use /pdbsourcepath: for more places when present.
/pdbsourcepath: was added in https://reviews.llvm.org/D48882 to make it
possible to have relative paths in the debug info that clang-cl writes.
lld-link then makes the paths absolute at link time, which debuggers require.
This way, clang-cl's output is independent of the absolute path of the build
directory, which is useful for cacheability in distcc-like systems.
This patch extends /pdbsourcepath: (if passed) to also be used for:
1. The "cwd" stored in the env block in the pdb is /pdbsourcepath: if present
2. The "exe" stored in the env block in the pdb is made absolute relative
to /pdbsourcepath: instead of the cwd
3. The "pdb" stored in the env block in the pdb is made absolute relative
to /pdbsourcepath: instead of the cwd
4. For making absolute paths to .obj files referenced from the pdb
/pdbsourcepath: is now useful in three scenarios (the first one already working
before this change):
1. When building with full debug info, passing the real build dir to
/pdbsourcepath: allows having clang-cl's output to be independent
of the build directory path. This patch effectively doesn't change
behavior for this use case (assuming the cwd is the build dir).
2. When building without compile-time debug info but linking with /debug,
a fake fixed /pdbsourcepath: can be passed to get symbolized stacks
while making the pdb and exe independent of the current build dir.
For this two work, lld-link needs to be invoked with relative paths for
the lld-link invocation itself (for "exe"), for the pdb output name, the exe
output name (for "pdb"), and the obj input files, and no absolute path
must appear on the link command (for "cmd" in the pdb's env block).
Since no full debug info is present, it doesn't matter that the absolute
path doesn't exist on disk -- we only get symbols in stacks.
3. When building production builds with full debug info that don't have
local changes, and that get source indexed and their pdbs get uploaded
to a symbol server. /pdbsourcepath: again makes the build output independent
of the current directory, and the fixed path passed to /pdbsourcepath: can
be given the source indexing transform so that it gets mapped to a
repository path. This has the same requirements as 2.
This patch also makes it possible to create PDB files containing Windows-style
absolute paths when cross-compiling on a POSIX system.
Differential Revision: https://reviews.llvm.org/D53021
llvm-svn: 344061
2018-10-10 01:52:25 +08:00
|
|
|
return;
|
|
|
|
}
|
2018-10-13 01:26:19 +08:00
|
|
|
|
2019-02-06 08:50:35 +08:00
|
|
|
// Try to guess whether /PDBSOURCEPATH is a unix path or a windows path.
|
|
|
|
// Since PDB's are more of a Windows thing, we make this conservative and only
|
|
|
|
// decide that it's a unix path if we're fairly certain. Specifically, if
|
|
|
|
// it starts with a forward slash.
|
2019-07-11 13:40:30 +08:00
|
|
|
SmallString<128> absoluteFileName = config->pdbSourcePath;
|
|
|
|
sys::path::Style guessedStyle = absoluteFileName.startswith("/")
|
2019-02-06 08:50:35 +08:00
|
|
|
? sys::path::Style::posix
|
|
|
|
: sys::path::Style::windows;
|
2019-07-11 13:40:30 +08:00
|
|
|
sys::path::append(absoluteFileName, guessedStyle, fileName);
|
|
|
|
sys::path::native(absoluteFileName, guessedStyle);
|
|
|
|
sys::path::remove_dots(absoluteFileName, true, guessedStyle);
|
2019-02-06 08:50:35 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
fileName = std::move(absoluteFileName);
|
lld-link: Use /pdbsourcepath: for more places when present.
/pdbsourcepath: was added in https://reviews.llvm.org/D48882 to make it
possible to have relative paths in the debug info that clang-cl writes.
lld-link then makes the paths absolute at link time, which debuggers require.
This way, clang-cl's output is independent of the absolute path of the build
directory, which is useful for cacheability in distcc-like systems.
This patch extends /pdbsourcepath: (if passed) to also be used for:
1. The "cwd" stored in the env block in the pdb is /pdbsourcepath: if present
2. The "exe" stored in the env block in the pdb is made absolute relative
to /pdbsourcepath: instead of the cwd
3. The "pdb" stored in the env block in the pdb is made absolute relative
to /pdbsourcepath: instead of the cwd
4. For making absolute paths to .obj files referenced from the pdb
/pdbsourcepath: is now useful in three scenarios (the first one already working
before this change):
1. When building with full debug info, passing the real build dir to
/pdbsourcepath: allows having clang-cl's output to be independent
of the build directory path. This patch effectively doesn't change
behavior for this use case (assuming the cwd is the build dir).
2. When building without compile-time debug info but linking with /debug,
a fake fixed /pdbsourcepath: can be passed to get symbolized stacks
while making the pdb and exe independent of the current build dir.
For this two work, lld-link needs to be invoked with relative paths for
the lld-link invocation itself (for "exe"), for the pdb output name, the exe
output name (for "pdb"), and the obj input files, and no absolute path
must appear on the link command (for "cmd" in the pdb's env block).
Since no full debug info is present, it doesn't matter that the absolute
path doesn't exist on disk -- we only get symbols in stacks.
3. When building production builds with full debug info that don't have
local changes, and that get source indexed and their pdbs get uploaded
to a symbol server. /pdbsourcepath: again makes the build output independent
of the current directory, and the fixed path passed to /pdbsourcepath: can
be given the source indexing transform so that it gets mapped to a
repository path. This has the same requirements as 2.
This patch also makes it possible to create PDB files containing Windows-style
absolute paths when cross-compiling on a POSIX system.
Differential Revision: https://reviews.llvm.org/D53021
llvm-svn: 344061
2018-10-10 01:52:25 +08:00
|
|
|
}
|
|
|
|
|
2017-12-15 02:07:04 +08:00
|
|
|
// A COFF .debug$H section is currently a clang extension. This function checks
|
|
|
|
// if a .debug$H section is in a format that we expect / understand, so that we
|
|
|
|
// can ignore any sections which are coincidentally also named .debug$H but do
|
|
|
|
// not contain a format we recognize.
|
2019-07-11 13:40:30 +08:00
|
|
|
static bool canUseDebugH(ArrayRef<uint8_t> debugH) {
|
|
|
|
if (debugH.size() < sizeof(object::debug_h_header))
|
2017-12-15 02:07:04 +08:00
|
|
|
return false;
|
2019-07-11 13:40:30 +08:00
|
|
|
auto *header =
|
|
|
|
reinterpret_cast<const object::debug_h_header *>(debugH.data());
|
|
|
|
debugH = debugH.drop_front(sizeof(object::debug_h_header));
|
|
|
|
return header->Magic == COFF::DEBUG_HASHES_SECTION_MAGIC &&
|
|
|
|
header->Version == 0 &&
|
|
|
|
header->HashAlgorithm == uint16_t(GlobalTypeHashAlg::SHA1_8) &&
|
|
|
|
(debugH.size() % 8 == 0);
|
2017-12-15 02:07:04 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
static Optional<ArrayRef<uint8_t>> getDebugH(ObjFile *file) {
|
|
|
|
SectionChunk *sec =
|
|
|
|
SectionChunk::findByName(file->getDebugChunks(), ".debug$H");
|
|
|
|
if (!sec)
|
2017-12-15 02:07:04 +08:00
|
|
|
return llvm::None;
|
2019-07-11 13:40:30 +08:00
|
|
|
ArrayRef<uint8_t> contents = sec->getContents();
|
|
|
|
if (!canUseDebugH(contents))
|
2017-12-15 02:07:04 +08:00
|
|
|
return None;
|
2019-07-11 13:40:30 +08:00
|
|
|
return contents;
|
2017-12-15 02:07:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static ArrayRef<GloballyHashedType>
|
2019-07-11 13:40:30 +08:00
|
|
|
getHashesFromDebugH(ArrayRef<uint8_t> debugH) {
|
|
|
|
assert(canUseDebugH(debugH));
|
2017-12-15 02:07:04 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
debugH = debugH.drop_front(sizeof(object::debug_h_header));
|
|
|
|
uint32_t count = debugH.size() / sizeof(GloballyHashedType);
|
|
|
|
return {reinterpret_cast<const GloballyHashedType *>(debugH.data()), count};
|
2017-12-15 02:07:04 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
static void addTypeInfo(pdb::TpiStreamBuilder &tpiBuilder,
|
|
|
|
TypeCollection &typeTable) {
|
2017-03-25 01:26:38 +08:00
|
|
|
// Start the TPI or IPI stream header.
|
2019-07-11 13:40:30 +08:00
|
|
|
tpiBuilder.setVersionHeader(pdb::PdbTpiV80);
|
2017-03-25 01:26:38 +08:00
|
|
|
|
2017-07-20 01:26:07 +08:00
|
|
|
// Flatten the in memory type table and hash each type.
|
2019-07-11 13:40:30 +08:00
|
|
|
typeTable.ForEachRecord([&](TypeIndex ti, const CVType &type) {
|
|
|
|
auto hash = pdb::hashTypeRecord(type);
|
|
|
|
if (auto e = hash.takeError())
|
2017-07-20 01:26:07 +08:00
|
|
|
fatal("type hashing error");
|
2019-07-11 13:40:30 +08:00
|
|
|
tpiBuilder.addTypeRecord(type.RecordData, *hash);
|
2017-03-25 01:26:38 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-11-06 03:20:47 +08:00
|
|
|
Expected<const CVIndexMap &>
|
2019-07-11 13:40:30 +08:00
|
|
|
PDBLinker::mergeDebugT(ObjFile *file, CVIndexMap *objectIndexMap) {
|
|
|
|
ScopedTimer t(typeMergingTimer);
|
2018-01-18 03:16:26 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (!file->debugTypesObj)
|
|
|
|
return *objectIndexMap; // no Types stream
|
2018-11-06 03:20:47 +08:00
|
|
|
|
|
|
|
// Precompiled headers objects need to save the index map for further
|
|
|
|
// reference by other objects which use the precompiled headers.
|
2019-07-11 13:40:30 +08:00
|
|
|
if (file->debugTypesObj->kind == TpiSource::PCH) {
|
|
|
|
uint32_t pchSignature = file->pchSignature.getValueOr(0);
|
|
|
|
if (pchSignature == 0)
|
2018-11-06 03:20:47 +08:00
|
|
|
fatal("No signature found for the precompiled headers OBJ (" +
|
2019-07-11 13:40:30 +08:00
|
|
|
file->getName() + ")");
|
2018-11-06 03:20:47 +08:00
|
|
|
|
|
|
|
// When a precompiled headers object comes first on the command-line, we
|
|
|
|
// update the mapping here. Otherwise, if an object referencing the
|
|
|
|
// precompiled headers object comes first, the mapping is created in
|
|
|
|
// aquirePrecompObj(), thus we would skip this block.
|
2019-07-11 13:40:30 +08:00
|
|
|
if (!objectIndexMap->isPrecompiledTypeMap) {
|
|
|
|
auto r = registerPrecompiledHeaders(pchSignature);
|
|
|
|
if (r.second)
|
2018-11-06 03:20:47 +08:00
|
|
|
fatal(
|
|
|
|
"A precompiled headers OBJ with the same signature was already "
|
|
|
|
"provided! (" +
|
2019-07-11 13:40:30 +08:00
|
|
|
file->getName() + ")");
|
2018-11-06 03:20:47 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
objectIndexMap = &r.first;
|
2018-11-06 03:20:47 +08:00
|
|
|
}
|
|
|
|
}
|
2017-07-14 04:12:23 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (file->debugTypesObj->kind == TpiSource::UsingPDB) {
|
2018-11-06 03:20:47 +08:00
|
|
|
// Look through type servers. If we've already seen this type server,
|
|
|
|
// don't merge any type information.
|
2019-07-11 13:40:30 +08:00
|
|
|
return maybeMergeTypeServerPDB(file);
|
[LLD][COFF] Early dependency detection
We introduce a new class hierarchy for debug types merging (in DebugTypes.h). The end-goal is to parallelize the type merging - please see the plan in D59226.
Previously, dependency discovery was done on the fly, much later, during the type merging loop. Unfortunately, parallelizing the type merging requires the dependencies to be merged in first, before any dependent ObjFile, thus this early discovery.
The overall intention for this path is to discover debug information dependencies at a much earlier stage, when processing input files. Currently, two types of dependency are supported: PDB type servers (when compiling with MSVC /Zi) and precompiled headers OBJs (when compiling with MSVC /Yc and /Yu). Once discovered, an explicit link is added into the dependent ObjFile, through the new debug types class hierarchy introduced in DebugTypes.h.
Differential Revision: https://reviews.llvm.org/D59053
llvm-svn: 357383
2019-04-01 21:36:59 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
CVTypeArray &types = *file->debugTypes;
|
[LLD][COFF] Early dependency detection
We introduce a new class hierarchy for debug types merging (in DebugTypes.h). The end-goal is to parallelize the type merging - please see the plan in D59226.
Previously, dependency discovery was done on the fly, much later, during the type merging loop. Unfortunately, parallelizing the type merging requires the dependencies to be merged in first, before any dependent ObjFile, thus this early discovery.
The overall intention for this path is to discover debug information dependencies at a much earlier stage, when processing input files. Currently, two types of dependency are supported: PDB type servers (when compiling with MSVC /Zi) and precompiled headers OBJs (when compiling with MSVC /Yc and /Yu). Once discovered, an explicit link is added into the dependent ObjFile, through the new debug types class hierarchy introduced in DebugTypes.h.
Differential Revision: https://reviews.llvm.org/D59053
llvm-svn: 357383
2019-04-01 21:36:59 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (file->debugTypesObj->kind == TpiSource::UsingPCH) {
|
2018-11-06 03:20:47 +08:00
|
|
|
// This object was compiled with /Yu, so process the corresponding
|
|
|
|
// precompiled headers object (/Yc) first. Some type indices in the current
|
|
|
|
// object are referencing data in the precompiled headers object, so we need
|
|
|
|
// both to be loaded.
|
2019-07-11 13:40:30 +08:00
|
|
|
Error e = mergeInPrecompHeaderObj(file, objectIndexMap);
|
|
|
|
if (e)
|
|
|
|
return std::move(e);
|
[LLD][COFF] Early dependency detection
We introduce a new class hierarchy for debug types merging (in DebugTypes.h). The end-goal is to parallelize the type merging - please see the plan in D59226.
Previously, dependency discovery was done on the fly, much later, during the type merging loop. Unfortunately, parallelizing the type merging requires the dependencies to be merged in first, before any dependent ObjFile, thus this early discovery.
The overall intention for this path is to discover debug information dependencies at a much earlier stage, when processing input files. Currently, two types of dependency are supported: PDB type servers (when compiling with MSVC /Zi) and precompiled headers OBJs (when compiling with MSVC /Yc and /Yu). Once discovered, an explicit link is added into the dependent ObjFile, through the new debug types class hierarchy introduced in DebugTypes.h.
Differential Revision: https://reviews.llvm.org/D59053
llvm-svn: 357383
2019-04-01 21:36:59 +08:00
|
|
|
|
|
|
|
// Drop LF_PRECOMP record from the input stream, as it has been replaced
|
|
|
|
// with the precompiled headers Type stream in the mergeInPrecompHeaderObj()
|
|
|
|
// call above. Note that we can't just call Types.drop_front(), as we
|
|
|
|
// explicitly want to rebase the stream.
|
2019-07-11 13:40:30 +08:00
|
|
|
CVTypeArray::Iterator firstType = types.begin();
|
|
|
|
types.setUnderlyingStream(
|
|
|
|
types.getUnderlyingStream().drop_front(firstType->RecordData.size()));
|
2018-11-06 03:20:47 +08:00
|
|
|
}
|
2017-07-18 08:21:25 +08:00
|
|
|
|
2018-11-06 03:20:47 +08:00
|
|
|
// Fill in the temporary, caller-provided ObjectIndexMap.
|
2019-07-11 13:40:30 +08:00
|
|
|
if (config->debugGHashes) {
|
|
|
|
ArrayRef<GloballyHashedType> hashes;
|
|
|
|
std::vector<GloballyHashedType> ownedHashes;
|
|
|
|
if (Optional<ArrayRef<uint8_t>> debugH = getDebugH(file))
|
|
|
|
hashes = getHashesFromDebugH(*debugH);
|
2017-12-15 02:07:04 +08:00
|
|
|
else {
|
2019-07-11 13:40:30 +08:00
|
|
|
ownedHashes = GloballyHashedType::hashTypes(types);
|
|
|
|
hashes = ownedHashes;
|
2017-12-15 02:07:04 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (auto err = mergeTypeAndIdRecords(
|
|
|
|
tMerger.globalIDTable, tMerger.globalTypeTable,
|
|
|
|
objectIndexMap->tpiMap, types, hashes, file->pchSignature))
|
2017-12-15 02:07:04 +08:00
|
|
|
fatal("codeview::mergeTypeAndIdRecords failed: " +
|
2019-07-11 13:40:30 +08:00
|
|
|
toString(std::move(err)));
|
2017-12-15 02:07:04 +08:00
|
|
|
} else {
|
2019-07-11 13:40:30 +08:00
|
|
|
if (auto err = mergeTypeAndIdRecords(tMerger.iDTable, tMerger.typeTable,
|
|
|
|
objectIndexMap->tpiMap, types,
|
|
|
|
file->pchSignature))
|
2017-12-15 02:07:04 +08:00
|
|
|
fatal("codeview::mergeTypeAndIdRecords failed: " +
|
2019-07-11 13:40:30 +08:00
|
|
|
toString(std::move(err)));
|
2017-12-15 02:07:04 +08:00
|
|
|
}
|
2019-07-11 13:40:30 +08:00
|
|
|
return *objectIndexMap;
|
2017-07-18 08:21:25 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
Expected<const CVIndexMap &> PDBLinker::maybeMergeTypeServerPDB(ObjFile *file) {
|
|
|
|
Expected<llvm::pdb::NativeSession *> pdbSession = findTypeServerSource(file);
|
|
|
|
if (!pdbSession)
|
|
|
|
return pdbSession.takeError();
|
2018-03-01 02:09:18 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
pdb::PDBFile &pdbFile = pdbSession.get()->getPDBFile();
|
|
|
|
pdb::InfoStream &info = cantFail(pdbFile.getPDBInfoStream());
|
2019-05-29 04:57:56 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
auto it = typeServerIndexMappings.emplace(info.getGuid(), CVIndexMap());
|
|
|
|
CVIndexMap &indexMap = it.first->second;
|
|
|
|
if (!it.second)
|
|
|
|
return indexMap; // already merged
|
2019-05-29 04:57:56 +08:00
|
|
|
|
2019-06-03 20:39:47 +08:00
|
|
|
// Mark this map as a type server map.
|
2019-07-11 13:40:30 +08:00
|
|
|
indexMap.isTypeServerMap = true;
|
|
|
|
|
|
|
|
Expected<pdb::TpiStream &> expectedTpi = pdbFile.getPDBTpiStream();
|
|
|
|
if (auto e = expectedTpi.takeError())
|
|
|
|
fatal("Type server does not have TPI stream: " + toString(std::move(e)));
|
|
|
|
pdb::TpiStream *maybeIpi = nullptr;
|
|
|
|
if (pdbFile.hasPDBIpiStream()) {
|
|
|
|
Expected<pdb::TpiStream &> expectedIpi = pdbFile.getPDBIpiStream();
|
|
|
|
if (auto e = expectedIpi.takeError())
|
|
|
|
fatal("Error getting type server IPI stream: " + toString(std::move(e)));
|
|
|
|
maybeIpi = &*expectedIpi;
|
2019-06-13 06:33:16 +08:00
|
|
|
}
|
2017-12-15 02:07:04 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (config->debugGHashes) {
|
2017-12-15 02:07:04 +08:00
|
|
|
// PDBs do not actually store global hashes, so when merging a type server
|
|
|
|
// PDB we have to synthesize global hashes. To do this, we first synthesize
|
|
|
|
// global hashes for the TPI stream, since it is independent, then we
|
|
|
|
// synthesize hashes for the IPI stream, using the hashes for the TPI stream
|
|
|
|
// as inputs.
|
2019-07-11 13:40:30 +08:00
|
|
|
auto tpiHashes = GloballyHashedType::hashTypes(expectedTpi->typeArray());
|
|
|
|
Optional<uint32_t> endPrecomp;
|
2017-12-15 02:07:04 +08:00
|
|
|
// Merge TPI first, because the IPI stream will reference type indices.
|
2019-07-11 13:40:30 +08:00
|
|
|
if (auto err =
|
|
|
|
mergeTypeRecords(tMerger.globalTypeTable, indexMap.tpiMap,
|
|
|
|
expectedTpi->typeArray(), tpiHashes, endPrecomp))
|
|
|
|
fatal("codeview::mergeTypeRecords failed: " + toString(std::move(err)));
|
2017-12-15 02:07:04 +08:00
|
|
|
|
|
|
|
// Merge IPI.
|
2019-07-11 13:40:30 +08:00
|
|
|
if (maybeIpi) {
|
|
|
|
auto ipiHashes =
|
|
|
|
GloballyHashedType::hashIds(maybeIpi->typeArray(), tpiHashes);
|
|
|
|
if (auto err =
|
|
|
|
mergeIdRecords(tMerger.globalIDTable, indexMap.tpiMap,
|
|
|
|
indexMap.ipiMap, maybeIpi->typeArray(), ipiHashes))
|
|
|
|
fatal("codeview::mergeIdRecords failed: " + toString(std::move(err)));
|
2019-06-13 06:33:16 +08:00
|
|
|
}
|
2017-12-15 02:07:04 +08:00
|
|
|
} else {
|
|
|
|
// Merge TPI first, because the IPI stream will reference type indices.
|
2019-07-11 13:40:30 +08:00
|
|
|
if (auto err = mergeTypeRecords(tMerger.typeTable, indexMap.tpiMap,
|
|
|
|
expectedTpi->typeArray()))
|
|
|
|
fatal("codeview::mergeTypeRecords failed: " + toString(std::move(err)));
|
2017-12-15 02:07:04 +08:00
|
|
|
|
|
|
|
// Merge IPI.
|
2019-07-11 13:40:30 +08:00
|
|
|
if (maybeIpi) {
|
|
|
|
if (auto err = mergeIdRecords(tMerger.iDTable, indexMap.tpiMap,
|
|
|
|
indexMap.ipiMap, maybeIpi->typeArray()))
|
|
|
|
fatal("codeview::mergeIdRecords failed: " + toString(std::move(err)));
|
2019-06-13 06:33:16 +08:00
|
|
|
}
|
2017-12-15 02:07:04 +08:00
|
|
|
}
|
2017-07-18 08:21:25 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
return indexMap;
|
2017-06-20 01:21:45 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
Error PDBLinker::mergeInPrecompHeaderObj(ObjFile *file,
|
|
|
|
CVIndexMap *objectIndexMap) {
|
|
|
|
const PrecompRecord &precomp =
|
|
|
|
retrieveDependencyInfo<PrecompRecord>(file->debugTypesObj);
|
[LLD][COFF] Early dependency detection
We introduce a new class hierarchy for debug types merging (in DebugTypes.h). The end-goal is to parallelize the type merging - please see the plan in D59226.
Previously, dependency discovery was done on the fly, much later, during the type merging loop. Unfortunately, parallelizing the type merging requires the dependencies to be merged in first, before any dependent ObjFile, thus this early discovery.
The overall intention for this path is to discover debug information dependencies at a much earlier stage, when processing input files. Currently, two types of dependency are supported: PDB type servers (when compiling with MSVC /Zi) and precompiled headers OBJs (when compiling with MSVC /Yc and /Yu). Once discovered, an explicit link is added into the dependent ObjFile, through the new debug types class hierarchy introduced in DebugTypes.h.
Differential Revision: https://reviews.llvm.org/D59053
llvm-svn: 357383
2019-04-01 21:36:59 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
Expected<const CVIndexMap &> e = aquirePrecompObj(file);
|
|
|
|
if (!e)
|
|
|
|
return e.takeError();
|
2018-11-06 03:20:47 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
const CVIndexMap &precompIndexMap = *e;
|
|
|
|
assert(precompIndexMap.isPrecompiledTypeMap);
|
2018-11-06 03:20:47 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (precompIndexMap.tpiMap.empty())
|
[LLD][COFF] Early dependency detection
We introduce a new class hierarchy for debug types merging (in DebugTypes.h). The end-goal is to parallelize the type merging - please see the plan in D59226.
Previously, dependency discovery was done on the fly, much later, during the type merging loop. Unfortunately, parallelizing the type merging requires the dependencies to be merged in first, before any dependent ObjFile, thus this early discovery.
The overall intention for this path is to discover debug information dependencies at a much earlier stage, when processing input files. Currently, two types of dependency are supported: PDB type servers (when compiling with MSVC /Zi) and precompiled headers OBJs (when compiling with MSVC /Yc and /Yu). Once discovered, an explicit link is added into the dependent ObjFile, through the new debug types class hierarchy introduced in DebugTypes.h.
Differential Revision: https://reviews.llvm.org/D59053
llvm-svn: 357383
2019-04-01 21:36:59 +08:00
|
|
|
return Error::success();
|
2018-11-06 03:20:47 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
assert(precomp.getStartTypeIndex() == TypeIndex::FirstNonSimpleIndex);
|
|
|
|
assert(precomp.getTypesCount() <= precompIndexMap.tpiMap.size());
|
2018-11-06 03:20:47 +08:00
|
|
|
// Use the previously remapped index map from the precompiled headers.
|
2019-07-11 13:40:30 +08:00
|
|
|
objectIndexMap->tpiMap.append(precompIndexMap.tpiMap.begin(),
|
|
|
|
precompIndexMap.tpiMap.begin() +
|
|
|
|
precomp.getTypesCount());
|
[LLD][COFF] Early dependency detection
We introduce a new class hierarchy for debug types merging (in DebugTypes.h). The end-goal is to parallelize the type merging - please see the plan in D59226.
Previously, dependency discovery was done on the fly, much later, during the type merging loop. Unfortunately, parallelizing the type merging requires the dependencies to be merged in first, before any dependent ObjFile, thus this early discovery.
The overall intention for this path is to discover debug information dependencies at a much earlier stage, when processing input files. Currently, two types of dependency are supported: PDB type servers (when compiling with MSVC /Zi) and precompiled headers OBJs (when compiling with MSVC /Yc and /Yu). Once discovered, an explicit link is added into the dependent ObjFile, through the new debug types class hierarchy introduced in DebugTypes.h.
Differential Revision: https://reviews.llvm.org/D59053
llvm-svn: 357383
2019-04-01 21:36:59 +08:00
|
|
|
return Error::success();
|
2018-11-06 03:20:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool equals_path(StringRef path1, StringRef path2) {
|
|
|
|
#if defined(_WIN32)
|
|
|
|
return path1.equals_lower(path2);
|
|
|
|
#else
|
|
|
|
return path1.equals(path2);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-11-08 22:42:37 +08:00
|
|
|
// Find by name an OBJ provided on the command line
|
2019-07-11 13:40:30 +08:00
|
|
|
static ObjFile *findObjByName(StringRef fileNameOnly) {
|
|
|
|
SmallString<128> currentPath;
|
2018-11-06 03:20:47 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
for (ObjFile *f : ObjFile::instances) {
|
|
|
|
StringRef currentFileName = sys::path::filename(f->getName());
|
2018-11-06 03:20:47 +08:00
|
|
|
|
2018-11-08 22:42:37 +08:00
|
|
|
// Compare based solely on the file name (link.exe behavior)
|
2019-07-11 13:40:30 +08:00
|
|
|
if (equals_path(currentFileName, fileNameOnly))
|
|
|
|
return f;
|
2018-11-06 03:20:47 +08:00
|
|
|
}
|
2018-11-08 22:42:37 +08:00
|
|
|
return nullptr;
|
2018-11-06 03:20:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<CVIndexMap &, bool /*already there*/>
|
2019-07-11 13:40:30 +08:00
|
|
|
PDBLinker::registerPrecompiledHeaders(uint32_t signature) {
|
|
|
|
auto insertion = precompTypeIndexMappings.insert({signature, CVIndexMap()});
|
|
|
|
CVIndexMap &indexMap = insertion.first->second;
|
|
|
|
if (!insertion.second)
|
|
|
|
return {indexMap, true};
|
2018-11-06 03:20:47 +08:00
|
|
|
// Mark this map as a precompiled types map.
|
2019-07-11 13:40:30 +08:00
|
|
|
indexMap.isPrecompiledTypeMap = true;
|
|
|
|
return {indexMap, false};
|
2018-11-06 03:20:47 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
Expected<const CVIndexMap &> PDBLinker::aquirePrecompObj(ObjFile *file) {
|
|
|
|
const PrecompRecord &precomp =
|
|
|
|
retrieveDependencyInfo<PrecompRecord>(file->debugTypesObj);
|
[LLD][COFF] Early dependency detection
We introduce a new class hierarchy for debug types merging (in DebugTypes.h). The end-goal is to parallelize the type merging - please see the plan in D59226.
Previously, dependency discovery was done on the fly, much later, during the type merging loop. Unfortunately, parallelizing the type merging requires the dependencies to be merged in first, before any dependent ObjFile, thus this early discovery.
The overall intention for this path is to discover debug information dependencies at a much earlier stage, when processing input files. Currently, two types of dependency are supported: PDB type servers (when compiling with MSVC /Zi) and precompiled headers OBJs (when compiling with MSVC /Yc and /Yu). Once discovered, an explicit link is added into the dependent ObjFile, through the new debug types class hierarchy introduced in DebugTypes.h.
Differential Revision: https://reviews.llvm.org/D59053
llvm-svn: 357383
2019-04-01 21:36:59 +08:00
|
|
|
|
2018-11-06 03:20:47 +08:00
|
|
|
// First, check if we already loaded the precompiled headers object with this
|
|
|
|
// signature. Return the type index mapping if we've already seen it.
|
2019-07-11 13:40:30 +08:00
|
|
|
auto r = registerPrecompiledHeaders(precomp.getSignature());
|
|
|
|
if (r.second)
|
|
|
|
return r.first;
|
2018-11-06 03:20:47 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
CVIndexMap &indexMap = r.first;
|
2018-11-06 03:20:47 +08:00
|
|
|
|
|
|
|
// Cross-compile warning: given that Clang doesn't generate LF_PRECOMP
|
|
|
|
// records, we assume the OBJ comes from a Windows build of cl.exe. Thusly,
|
|
|
|
// the paths embedded in the OBJs are in the Windows format.
|
2019-07-11 13:40:30 +08:00
|
|
|
SmallString<128> precompFileName = sys::path::filename(
|
|
|
|
precomp.getPrecompFilePath(), sys::path::Style::windows);
|
2018-11-06 03:20:47 +08:00
|
|
|
|
|
|
|
// link.exe requires that a precompiled headers object must always be provided
|
|
|
|
// on the command-line, even if that's not necessary.
|
2019-07-11 13:40:30 +08:00
|
|
|
auto precompFile = findObjByName(precompFileName);
|
|
|
|
if (!precompFile)
|
2018-11-08 22:42:37 +08:00
|
|
|
return createFileError(
|
2019-07-11 13:40:30 +08:00
|
|
|
precompFileName.str(),
|
2018-11-08 22:42:37 +08:00
|
|
|
make_error<pdb::PDBError>(pdb::pdb_error_code::external_cmdline_ref));
|
2018-11-06 03:20:47 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
addObjFile(precompFile, &indexMap);
|
2018-11-06 03:20:47 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (!precompFile->pchSignature)
|
|
|
|
fatal(precompFile->getName() + " is not a precompiled headers object");
|
2018-11-06 03:20:47 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (precomp.getSignature() != precompFile->pchSignature.getValueOr(0))
|
2018-11-08 22:42:37 +08:00
|
|
|
return createFileError(
|
2019-07-11 13:40:30 +08:00
|
|
|
precomp.getPrecompFilePath().str(),
|
2018-11-08 22:42:37 +08:00
|
|
|
make_error<pdb::PDBError>(pdb::pdb_error_code::signature_out_of_date));
|
2018-11-06 03:20:47 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
return indexMap;
|
2018-11-06 03:20:47 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
static bool remapTypeIndex(TypeIndex &ti, ArrayRef<TypeIndex> typeIndexMap) {
|
|
|
|
if (ti.isSimple())
|
2017-06-22 01:25:56 +08:00
|
|
|
return true;
|
2019-07-11 13:40:30 +08:00
|
|
|
if (ti.toArrayIndex() >= typeIndexMap.size())
|
2017-06-22 01:25:56 +08:00
|
|
|
return false;
|
2019-07-11 13:40:30 +08:00
|
|
|
ti = typeIndexMap[ti.toArrayIndex()];
|
2017-06-22 01:25:56 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
static void remapTypesInSymbolRecord(ObjFile *file, SymbolKind symKind,
|
|
|
|
MutableArrayRef<uint8_t> recordBytes,
|
|
|
|
const CVIndexMap &indexMap,
|
|
|
|
ArrayRef<TiReference> typeRefs) {
|
|
|
|
MutableArrayRef<uint8_t> contents =
|
|
|
|
recordBytes.drop_front(sizeof(RecordPrefix));
|
|
|
|
for (const TiReference &ref : typeRefs) {
|
|
|
|
unsigned byteSize = ref.Count * sizeof(TypeIndex);
|
|
|
|
if (contents.size() < ref.Offset + byteSize)
|
2017-07-13 02:49:43 +08:00
|
|
|
fatal("symbol record too short");
|
2017-07-18 08:21:25 +08:00
|
|
|
|
|
|
|
// This can be an item index or a type index. Choose the appropriate map.
|
2019-07-11 13:40:30 +08:00
|
|
|
ArrayRef<TypeIndex> typeOrItemMap = indexMap.tpiMap;
|
|
|
|
bool isItemIndex = ref.Kind == TiRefKind::IndexRef;
|
|
|
|
if (isItemIndex && indexMap.isTypeServerMap)
|
|
|
|
typeOrItemMap = indexMap.ipiMap;
|
|
|
|
|
|
|
|
MutableArrayRef<TypeIndex> tIs(
|
|
|
|
reinterpret_cast<TypeIndex *>(contents.data() + ref.Offset), ref.Count);
|
|
|
|
for (TypeIndex &ti : tIs) {
|
|
|
|
if (!remapTypeIndex(ti, typeOrItemMap)) {
|
|
|
|
log("ignoring symbol record of kind 0x" + utohexstr(symKind) + " in " +
|
|
|
|
file->getName() + " with bad " + (isItemIndex ? "item" : "type") +
|
|
|
|
" index 0x" + utohexstr(ti.getIndex()));
|
|
|
|
ti = TypeIndex(SimpleTypeKind::NotTranslated);
|
2017-07-13 02:49:43 +08:00
|
|
|
continue;
|
2017-06-22 01:25:56 +08:00
|
|
|
}
|
2017-07-13 02:49:43 +08:00
|
|
|
}
|
2017-06-22 01:25:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-06 03:12:40 +08:00
|
|
|
static void
|
2019-07-11 13:40:30 +08:00
|
|
|
recordStringTableReferenceAtOffset(MutableArrayRef<uint8_t> contents,
|
|
|
|
uint32_t offset,
|
|
|
|
std::vector<ulittle32_t *> &strTableRefs) {
|
|
|
|
contents =
|
|
|
|
contents.drop_front(offset).take_front(sizeof(support::ulittle32_t));
|
|
|
|
ulittle32_t *index = reinterpret_cast<ulittle32_t *>(contents.data());
|
|
|
|
strTableRefs.push_back(index);
|
2018-01-06 03:12:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-07-11 13:40:30 +08:00
|
|
|
recordStringTableReferences(SymbolKind kind, MutableArrayRef<uint8_t> contents,
|
|
|
|
std::vector<ulittle32_t *> &strTableRefs) {
|
2018-01-06 03:12:40 +08:00
|
|
|
// For now we only handle S_FILESTATIC, but we may need the same logic for
|
|
|
|
// S_DEFRANGE and S_DEFRANGE_SUBFIELD. However, I cannot seem to generate any
|
|
|
|
// PDBs that contain these types of records, so because of the uncertainty
|
|
|
|
// they are omitted here until we can prove that it's necessary.
|
2019-07-11 13:40:30 +08:00
|
|
|
switch (kind) {
|
2018-01-06 03:12:40 +08:00
|
|
|
case SymbolKind::S_FILESTATIC:
|
|
|
|
// FileStaticSym::ModFileOffset
|
2019-07-11 13:40:30 +08:00
|
|
|
recordStringTableReferenceAtOffset(contents, 8, strTableRefs);
|
2018-01-06 03:12:40 +08:00
|
|
|
break;
|
|
|
|
case SymbolKind::S_DEFRANGE:
|
|
|
|
case SymbolKind::S_DEFRANGE_SUBFIELD:
|
|
|
|
log("Not fixing up string table reference in S_DEFRANGE / "
|
|
|
|
"S_DEFRANGE_SUBFIELD record");
|
|
|
|
break;
|
2018-01-06 03:28:39 +08:00
|
|
|
default:
|
|
|
|
break;
|
2018-01-06 03:12:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
static SymbolKind symbolKind(ArrayRef<uint8_t> recordData) {
|
|
|
|
const RecordPrefix *prefix =
|
|
|
|
reinterpret_cast<const RecordPrefix *>(recordData.data());
|
|
|
|
return static_cast<SymbolKind>(uint16_t(prefix->RecordKind));
|
2017-08-09 02:34:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// MSVC translates S_PROC_ID_END to S_END, and S_[LG]PROC32_ID to S_[LG]PROC32
|
2019-07-11 13:40:30 +08:00
|
|
|
static void translateIdSymbols(MutableArrayRef<uint8_t> &recordData,
|
|
|
|
TypeCollection &iDTable) {
|
|
|
|
RecordPrefix *prefix = reinterpret_cast<RecordPrefix *>(recordData.data());
|
2017-08-09 02:34:44 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
SymbolKind kind = symbolKind(recordData);
|
2017-08-09 02:34:44 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (kind == SymbolKind::S_PROC_ID_END) {
|
|
|
|
prefix->RecordKind = SymbolKind::S_END;
|
2017-08-09 02:34:44 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// In an object file, GPROC32_ID has an embedded reference which refers to the
|
|
|
|
// single object file type index namespace. This has already been translated
|
|
|
|
// to the PDB file's ID stream index space, but we need to convert this to a
|
|
|
|
// symbol that refers to the type stream index space. So we remap again from
|
|
|
|
// ID index space to type index space.
|
2019-07-11 13:40:30 +08:00
|
|
|
if (kind == SymbolKind::S_GPROC32_ID || kind == SymbolKind::S_LPROC32_ID) {
|
|
|
|
SmallVector<TiReference, 1> refs;
|
|
|
|
auto content = recordData.drop_front(sizeof(RecordPrefix));
|
|
|
|
CVSymbol sym(recordData);
|
|
|
|
discoverTypeIndicesInSymbol(sym, refs);
|
|
|
|
assert(refs.size() == 1);
|
|
|
|
assert(refs.front().Count == 1);
|
|
|
|
|
|
|
|
TypeIndex *ti =
|
|
|
|
reinterpret_cast<TypeIndex *>(content.data() + refs[0].Offset);
|
2019-07-16 16:26:38 +08:00
|
|
|
// `ti` is the index of a FuncIdRecord or MemberFuncIdRecord which lives in
|
2017-08-09 02:34:44 +08:00
|
|
|
// the IPI stream, whose `FunctionType` member refers to the TPI stream.
|
|
|
|
// Note that LF_FUNC_ID and LF_MEMFUNC_ID have the same record layout, and
|
|
|
|
// in both cases we just need the second type index.
|
2019-07-11 13:40:30 +08:00
|
|
|
if (!ti->isSimple() && !ti->isNoneType()) {
|
|
|
|
CVType funcIdData = iDTable.getType(*ti);
|
|
|
|
SmallVector<TypeIndex, 2> indices;
|
|
|
|
discoverTypeIndices(funcIdData, indices);
|
|
|
|
assert(indices.size() == 2);
|
|
|
|
*ti = indices[1];
|
2017-08-09 02:34:44 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
kind = (kind == SymbolKind::S_GPROC32_ID) ? SymbolKind::S_GPROC32
|
2017-08-09 02:34:44 +08:00
|
|
|
: SymbolKind::S_LPROC32;
|
2019-07-11 13:40:30 +08:00
|
|
|
prefix->RecordKind = uint16_t(kind);
|
2017-08-09 02:34:44 +08:00
|
|
|
}
|
2017-06-22 01:25:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Copy the symbol record. In a PDB, symbol records must be 4 byte aligned.
|
|
|
|
/// The object file may not be aligned.
|
[PDB] Add symbol records in bulk
Summary:
This speeds up linking clang.exe/pdb with /DEBUG:GHASH by 31%, from
12.9s to 9.8s.
Symbol records are typically small (16.7 bytes on average), but we
processed them one at a time. CVSymbol is a relatively "large" type. It
wraps an ArrayRef<uint8_t> with a kind an optional 32-bit hash, which we
don't need. Before this change, each DbiModuleDescriptorBuilder would
maintain an array of CVSymbols, and would write them individually with a
BinaryItemStream.
With this change, we now add symbols that happen to appear contiguously
in bulk. For each .debug$S section (roughly one per function), we
allocate two copies, one for relocation, and one for realignment
purposes. For runs of symbols that go in the module stream, which is
most symbols, we now add them as a single ArrayRef<uint8_t>, so the
vector DbiModuleDescriptorBuilder is roughly linear in the number of
.debug$S sections (O(# funcs)) instead of the number of symbol records
(very large).
Some stats on symbol sizes for the curious:
PDB size: 507M
sym bytes: 316,508,016
sym count: 18,954,971
sym byte avg: 16.7
As future work, we may be able to skip copying symbol records in the
linker for realignment purposes if we make LLVM write them aligned into
the object file. We need to double check that such symbol records are
still compatible with link.exe, but if so, it's definitely worth doing,
since my profile shows we spend 500ms in memcpy in the symbol merging
code. We could potentially cut that in half by saving a copy.
Alternatively, we could apply the relocations *after* we iterate the
symbols. This would require some careful re-engineering of the
relocation processing code, though.
Reviewers: zturner, aganea, ruiu
Subscribers: hiraditya, llvm-commits
Differential Revision: https://reviews.llvm.org/D54554
llvm-svn: 347687
2018-11-28 03:00:23 +08:00
|
|
|
static MutableArrayRef<uint8_t>
|
2019-07-11 13:40:30 +08:00
|
|
|
copyAndAlignSymbol(const CVSymbol &sym, MutableArrayRef<uint8_t> &alignedMem) {
|
|
|
|
size_t size = alignTo(sym.length(), alignOf(CodeViewContainer::Pdb));
|
|
|
|
assert(size >= 4 && "record too short");
|
|
|
|
assert(size <= MaxRecordLength && "record too long");
|
|
|
|
assert(alignedMem.size() >= size && "didn't preallocate enough");
|
2017-06-22 01:25:56 +08:00
|
|
|
|
|
|
|
// Copy the symbol record and zero out any padding bytes.
|
2019-07-11 13:40:30 +08:00
|
|
|
MutableArrayRef<uint8_t> newData = alignedMem.take_front(size);
|
|
|
|
alignedMem = alignedMem.drop_front(size);
|
|
|
|
memcpy(newData.data(), sym.data().data(), sym.length());
|
|
|
|
memset(newData.data() + sym.length(), 0, size - sym.length());
|
2017-06-22 01:25:56 +08:00
|
|
|
|
|
|
|
// Update the record prefix length. It should point to the beginning of the
|
2017-08-09 02:34:44 +08:00
|
|
|
// next record.
|
2019-07-11 13:40:30 +08:00
|
|
|
auto *prefix = reinterpret_cast<RecordPrefix *>(newData.data());
|
|
|
|
prefix->RecordLen = size - 2;
|
|
|
|
return newData;
|
2017-06-22 01:25:56 +08:00
|
|
|
}
|
|
|
|
|
2017-07-07 00:39:32 +08:00
|
|
|
struct ScopeRecord {
|
2019-07-11 13:40:30 +08:00
|
|
|
ulittle32_t ptrParent;
|
|
|
|
ulittle32_t ptrEnd;
|
2017-07-07 00:39:32 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct SymbolScope {
|
2019-07-11 13:40:30 +08:00
|
|
|
ScopeRecord *openingRecord;
|
|
|
|
uint32_t scopeOffset;
|
2017-07-07 00:39:32 +08:00
|
|
|
};
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
static void scopeStackOpen(SmallVectorImpl<SymbolScope> &stack,
|
|
|
|
uint32_t curOffset, CVSymbol &sym) {
|
|
|
|
assert(symbolOpensScope(sym.kind()));
|
|
|
|
SymbolScope s;
|
|
|
|
s.scopeOffset = curOffset;
|
|
|
|
s.openingRecord = const_cast<ScopeRecord *>(
|
|
|
|
reinterpret_cast<const ScopeRecord *>(sym.content().data()));
|
|
|
|
s.openingRecord->ptrParent = stack.empty() ? 0 : stack.back().scopeOffset;
|
|
|
|
stack.push_back(s);
|
2017-07-07 00:39:32 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
static void scopeStackClose(SmallVectorImpl<SymbolScope> &stack,
|
|
|
|
uint32_t curOffset, InputFile *file) {
|
|
|
|
if (stack.empty()) {
|
|
|
|
warn("symbol scopes are not balanced in " + file->getName());
|
2017-07-07 00:39:32 +08:00
|
|
|
return;
|
|
|
|
}
|
2019-07-11 13:40:30 +08:00
|
|
|
SymbolScope s = stack.pop_back_val();
|
|
|
|
s.openingRecord->ptrEnd = curOffset;
|
2017-07-07 00:39:32 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
static bool symbolGoesInModuleStream(const CVSymbol &sym, bool isGlobalScope) {
|
|
|
|
switch (sym.kind()) {
|
2017-08-12 03:00:03 +08:00
|
|
|
case SymbolKind::S_GDATA32:
|
|
|
|
case SymbolKind::S_CONSTANT:
|
|
|
|
// We really should not be seeing S_PROCREF and S_LPROCREF in the first place
|
|
|
|
// since they are synthesized by the linker in response to S_GPROC32 and
|
|
|
|
// S_LPROC32, but if we do see them, don't put them in the module stream I
|
|
|
|
// guess.
|
|
|
|
case SymbolKind::S_PROCREF:
|
|
|
|
case SymbolKind::S_LPROCREF:
|
|
|
|
return false;
|
2018-12-05 05:48:46 +08:00
|
|
|
// S_UDT records go in the module stream if it is not a global S_UDT.
|
|
|
|
case SymbolKind::S_UDT:
|
2019-07-11 13:40:30 +08:00
|
|
|
return !isGlobalScope;
|
2017-08-12 03:00:03 +08:00
|
|
|
// S_GDATA32 does not go in the module stream, but S_LDATA32 does.
|
|
|
|
case SymbolKind::S_LDATA32:
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
static bool symbolGoesInGlobalsStream(const CVSymbol &sym, bool isGlobalScope) {
|
|
|
|
switch (sym.kind()) {
|
2017-08-12 03:00:03 +08:00
|
|
|
case SymbolKind::S_CONSTANT:
|
|
|
|
case SymbolKind::S_GDATA32:
|
|
|
|
// S_LDATA32 goes in both the module stream and the globals stream.
|
|
|
|
case SymbolKind::S_LDATA32:
|
|
|
|
case SymbolKind::S_GPROC32:
|
|
|
|
case SymbolKind::S_LPROC32:
|
|
|
|
// We really should not be seeing S_PROCREF and S_LPROCREF in the first place
|
|
|
|
// since they are synthesized by the linker in response to S_GPROC32 and
|
|
|
|
// S_LPROC32, but if we do see them, copy them straight through.
|
|
|
|
case SymbolKind::S_PROCREF:
|
|
|
|
case SymbolKind::S_LPROCREF:
|
|
|
|
return true;
|
2018-12-05 05:48:46 +08:00
|
|
|
// S_UDT records go in the globals stream if it is a global S_UDT.
|
2017-08-15 02:44:58 +08:00
|
|
|
case SymbolKind::S_UDT:
|
2019-07-11 13:40:30 +08:00
|
|
|
return isGlobalScope;
|
2017-08-12 03:00:03 +08:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
static void addGlobalSymbol(pdb::GSIStreamBuilder &builder, uint16_t modIndex,
|
|
|
|
unsigned symOffset, const CVSymbol &sym) {
|
|
|
|
switch (sym.kind()) {
|
2017-08-12 03:00:03 +08:00
|
|
|
case SymbolKind::S_CONSTANT:
|
|
|
|
case SymbolKind::S_UDT:
|
|
|
|
case SymbolKind::S_GDATA32:
|
|
|
|
case SymbolKind::S_LDATA32:
|
|
|
|
case SymbolKind::S_PROCREF:
|
|
|
|
case SymbolKind::S_LPROCREF:
|
2019-07-11 13:40:30 +08:00
|
|
|
builder.addGlobalSymbol(sym);
|
2017-08-12 03:00:03 +08:00
|
|
|
break;
|
|
|
|
case SymbolKind::S_GPROC32:
|
|
|
|
case SymbolKind::S_LPROC32: {
|
2019-07-11 13:40:30 +08:00
|
|
|
SymbolRecordKind k = SymbolRecordKind::ProcRefSym;
|
|
|
|
if (sym.kind() == SymbolKind::S_LPROC32)
|
|
|
|
k = SymbolRecordKind::LocalProcRef;
|
|
|
|
ProcRefSym ps(k);
|
|
|
|
ps.Module = modIndex;
|
2017-08-12 03:00:03 +08:00
|
|
|
// For some reason, MSVC seems to add one to this value.
|
2019-07-11 13:40:30 +08:00
|
|
|
++ps.Module;
|
|
|
|
ps.Name = getSymbolName(sym);
|
|
|
|
ps.SumName = 0;
|
|
|
|
ps.SymOffset = symOffset;
|
|
|
|
builder.addGlobalSymbol(ps);
|
2017-08-12 03:00:03 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Invalid symbol kind!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
void PDBLinker::mergeSymbolRecords(ObjFile *file, const CVIndexMap &indexMap,
|
|
|
|
std::vector<ulittle32_t *> &stringTableRefs,
|
|
|
|
BinaryStreamRef symData) {
|
|
|
|
ArrayRef<uint8_t> symsBuffer;
|
|
|
|
cantFail(symData.readBytes(0, symData.getLength(), symsBuffer));
|
|
|
|
SmallVector<SymbolScope, 4> scopes;
|
2018-01-06 03:12:40 +08:00
|
|
|
|
[PDB] Add symbol records in bulk
Summary:
This speeds up linking clang.exe/pdb with /DEBUG:GHASH by 31%, from
12.9s to 9.8s.
Symbol records are typically small (16.7 bytes on average), but we
processed them one at a time. CVSymbol is a relatively "large" type. It
wraps an ArrayRef<uint8_t> with a kind an optional 32-bit hash, which we
don't need. Before this change, each DbiModuleDescriptorBuilder would
maintain an array of CVSymbols, and would write them individually with a
BinaryItemStream.
With this change, we now add symbols that happen to appear contiguously
in bulk. For each .debug$S section (roughly one per function), we
allocate two copies, one for relocation, and one for realignment
purposes. For runs of symbols that go in the module stream, which is
most symbols, we now add them as a single ArrayRef<uint8_t>, so the
vector DbiModuleDescriptorBuilder is roughly linear in the number of
.debug$S sections (O(# funcs)) instead of the number of symbol records
(very large).
Some stats on symbol sizes for the curious:
PDB size: 507M
sym bytes: 316,508,016
sym count: 18,954,971
sym byte avg: 16.7
As future work, we may be able to skip copying symbol records in the
linker for realignment purposes if we make LLVM write them aligned into
the object file. We need to double check that such symbol records are
still compatible with link.exe, but if so, it's definitely worth doing,
since my profile shows we spend 500ms in memcpy in the symbol merging
code. We could potentially cut that in half by saving a copy.
Alternatively, we could apply the relocations *after* we iterate the
symbols. This would require some careful re-engineering of the
relocation processing code, though.
Reviewers: zturner, aganea, ruiu
Subscribers: hiraditya, llvm-commits
Differential Revision: https://reviews.llvm.org/D54554
llvm-svn: 347687
2018-11-28 03:00:23 +08:00
|
|
|
// Iterate every symbol to check if any need to be realigned, and if so, how
|
|
|
|
// much space we need to allocate for them.
|
2019-07-11 13:40:30 +08:00
|
|
|
bool needsRealignment = false;
|
|
|
|
unsigned totalRealignedSize = 0;
|
|
|
|
auto ec = forEachCodeViewRecord<CVSymbol>(
|
|
|
|
symsBuffer, [&](CVSymbol sym) -> llvm::Error {
|
|
|
|
unsigned realignedSize =
|
|
|
|
alignTo(sym.length(), alignOf(CodeViewContainer::Pdb));
|
|
|
|
needsRealignment |= realignedSize != sym.length();
|
|
|
|
totalRealignedSize += realignedSize;
|
[PDB] Add symbol records in bulk
Summary:
This speeds up linking clang.exe/pdb with /DEBUG:GHASH by 31%, from
12.9s to 9.8s.
Symbol records are typically small (16.7 bytes on average), but we
processed them one at a time. CVSymbol is a relatively "large" type. It
wraps an ArrayRef<uint8_t> with a kind an optional 32-bit hash, which we
don't need. Before this change, each DbiModuleDescriptorBuilder would
maintain an array of CVSymbols, and would write them individually with a
BinaryItemStream.
With this change, we now add symbols that happen to appear contiguously
in bulk. For each .debug$S section (roughly one per function), we
allocate two copies, one for relocation, and one for realignment
purposes. For runs of symbols that go in the module stream, which is
most symbols, we now add them as a single ArrayRef<uint8_t>, so the
vector DbiModuleDescriptorBuilder is roughly linear in the number of
.debug$S sections (O(# funcs)) instead of the number of symbol records
(very large).
Some stats on symbol sizes for the curious:
PDB size: 507M
sym bytes: 316,508,016
sym count: 18,954,971
sym byte avg: 16.7
As future work, we may be able to skip copying symbol records in the
linker for realignment purposes if we make LLVM write them aligned into
the object file. We need to double check that such symbol records are
still compatible with link.exe, but if so, it's definitely worth doing,
since my profile shows we spend 500ms in memcpy in the symbol merging
code. We could potentially cut that in half by saving a copy.
Alternatively, we could apply the relocations *after* we iterate the
symbols. This would require some careful re-engineering of the
relocation processing code, though.
Reviewers: zturner, aganea, ruiu
Subscribers: hiraditya, llvm-commits
Differential Revision: https://reviews.llvm.org/D54554
llvm-svn: 347687
2018-11-28 03:00:23 +08:00
|
|
|
return Error::success();
|
|
|
|
});
|
|
|
|
|
|
|
|
// If any of the symbol record lengths was corrupt, ignore them all, warn
|
|
|
|
// about it, and move on.
|
2019-07-11 13:40:30 +08:00
|
|
|
if (ec) {
|
|
|
|
warn("corrupt symbol records in " + file->getName());
|
|
|
|
consumeError(std::move(ec));
|
[PDB] Add symbol records in bulk
Summary:
This speeds up linking clang.exe/pdb with /DEBUG:GHASH by 31%, from
12.9s to 9.8s.
Symbol records are typically small (16.7 bytes on average), but we
processed them one at a time. CVSymbol is a relatively "large" type. It
wraps an ArrayRef<uint8_t> with a kind an optional 32-bit hash, which we
don't need. Before this change, each DbiModuleDescriptorBuilder would
maintain an array of CVSymbols, and would write them individually with a
BinaryItemStream.
With this change, we now add symbols that happen to appear contiguously
in bulk. For each .debug$S section (roughly one per function), we
allocate two copies, one for relocation, and one for realignment
purposes. For runs of symbols that go in the module stream, which is
most symbols, we now add them as a single ArrayRef<uint8_t>, so the
vector DbiModuleDescriptorBuilder is roughly linear in the number of
.debug$S sections (O(# funcs)) instead of the number of symbol records
(very large).
Some stats on symbol sizes for the curious:
PDB size: 507M
sym bytes: 316,508,016
sym count: 18,954,971
sym byte avg: 16.7
As future work, we may be able to skip copying symbol records in the
linker for realignment purposes if we make LLVM write them aligned into
the object file. We need to double check that such symbol records are
still compatible with link.exe, but if so, it's definitely worth doing,
since my profile shows we spend 500ms in memcpy in the symbol merging
code. We could potentially cut that in half by saving a copy.
Alternatively, we could apply the relocations *after* we iterate the
symbols. This would require some careful re-engineering of the
relocation processing code, though.
Reviewers: zturner, aganea, ruiu
Subscribers: hiraditya, llvm-commits
Differential Revision: https://reviews.llvm.org/D54554
llvm-svn: 347687
2018-11-28 03:00:23 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If any symbol needed realignment, allocate enough contiguous memory for
|
|
|
|
// them all. Typically symbol subsections are small enough that this will not
|
|
|
|
// cause fragmentation.
|
2019-07-11 13:40:30 +08:00
|
|
|
MutableArrayRef<uint8_t> alignedSymbolMem;
|
|
|
|
if (needsRealignment) {
|
|
|
|
void *alignedData =
|
|
|
|
alloc.Allocate(totalRealignedSize, alignOf(CodeViewContainer::Pdb));
|
|
|
|
alignedSymbolMem = makeMutableArrayRef(
|
|
|
|
reinterpret_cast<uint8_t *>(alignedData), totalRealignedSize);
|
[PDB] Add symbol records in bulk
Summary:
This speeds up linking clang.exe/pdb with /DEBUG:GHASH by 31%, from
12.9s to 9.8s.
Symbol records are typically small (16.7 bytes on average), but we
processed them one at a time. CVSymbol is a relatively "large" type. It
wraps an ArrayRef<uint8_t> with a kind an optional 32-bit hash, which we
don't need. Before this change, each DbiModuleDescriptorBuilder would
maintain an array of CVSymbols, and would write them individually with a
BinaryItemStream.
With this change, we now add symbols that happen to appear contiguously
in bulk. For each .debug$S section (roughly one per function), we
allocate two copies, one for relocation, and one for realignment
purposes. For runs of symbols that go in the module stream, which is
most symbols, we now add them as a single ArrayRef<uint8_t>, so the
vector DbiModuleDescriptorBuilder is roughly linear in the number of
.debug$S sections (O(# funcs)) instead of the number of symbol records
(very large).
Some stats on symbol sizes for the curious:
PDB size: 507M
sym bytes: 316,508,016
sym count: 18,954,971
sym byte avg: 16.7
As future work, we may be able to skip copying symbol records in the
linker for realignment purposes if we make LLVM write them aligned into
the object file. We need to double check that such symbol records are
still compatible with link.exe, but if so, it's definitely worth doing,
since my profile shows we spend 500ms in memcpy in the symbol merging
code. We could potentially cut that in half by saving a copy.
Alternatively, we could apply the relocations *after* we iterate the
symbols. This would require some careful re-engineering of the
relocation processing code, though.
Reviewers: zturner, aganea, ruiu
Subscribers: hiraditya, llvm-commits
Differential Revision: https://reviews.llvm.org/D54554
llvm-svn: 347687
2018-11-28 03:00:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate again, this time doing the real work.
|
2019-07-11 13:40:30 +08:00
|
|
|
unsigned curSymOffset = file->moduleDBI->getNextSymbolOffset();
|
|
|
|
ArrayRef<uint8_t> bulkSymbols;
|
[PDB] Add symbol records in bulk
Summary:
This speeds up linking clang.exe/pdb with /DEBUG:GHASH by 31%, from
12.9s to 9.8s.
Symbol records are typically small (16.7 bytes on average), but we
processed them one at a time. CVSymbol is a relatively "large" type. It
wraps an ArrayRef<uint8_t> with a kind an optional 32-bit hash, which we
don't need. Before this change, each DbiModuleDescriptorBuilder would
maintain an array of CVSymbols, and would write them individually with a
BinaryItemStream.
With this change, we now add symbols that happen to appear contiguously
in bulk. For each .debug$S section (roughly one per function), we
allocate two copies, one for relocation, and one for realignment
purposes. For runs of symbols that go in the module stream, which is
most symbols, we now add them as a single ArrayRef<uint8_t>, so the
vector DbiModuleDescriptorBuilder is roughly linear in the number of
.debug$S sections (O(# funcs)) instead of the number of symbol records
(very large).
Some stats on symbol sizes for the curious:
PDB size: 507M
sym bytes: 316,508,016
sym count: 18,954,971
sym byte avg: 16.7
As future work, we may be able to skip copying symbol records in the
linker for realignment purposes if we make LLVM write them aligned into
the object file. We need to double check that such symbol records are
still compatible with link.exe, but if so, it's definitely worth doing,
since my profile shows we spend 500ms in memcpy in the symbol merging
code. We could potentially cut that in half by saving a copy.
Alternatively, we could apply the relocations *after* we iterate the
symbols. This would require some careful re-engineering of the
relocation processing code, though.
Reviewers: zturner, aganea, ruiu
Subscribers: hiraditya, llvm-commits
Differential Revision: https://reviews.llvm.org/D54554
llvm-svn: 347687
2018-11-28 03:00:23 +08:00
|
|
|
cantFail(forEachCodeViewRecord<CVSymbol>(
|
2019-07-11 13:40:30 +08:00
|
|
|
symsBuffer, [&](CVSymbol sym) -> llvm::Error {
|
[PDB] Add symbol records in bulk
Summary:
This speeds up linking clang.exe/pdb with /DEBUG:GHASH by 31%, from
12.9s to 9.8s.
Symbol records are typically small (16.7 bytes on average), but we
processed them one at a time. CVSymbol is a relatively "large" type. It
wraps an ArrayRef<uint8_t> with a kind an optional 32-bit hash, which we
don't need. Before this change, each DbiModuleDescriptorBuilder would
maintain an array of CVSymbols, and would write them individually with a
BinaryItemStream.
With this change, we now add symbols that happen to appear contiguously
in bulk. For each .debug$S section (roughly one per function), we
allocate two copies, one for relocation, and one for realignment
purposes. For runs of symbols that go in the module stream, which is
most symbols, we now add them as a single ArrayRef<uint8_t>, so the
vector DbiModuleDescriptorBuilder is roughly linear in the number of
.debug$S sections (O(# funcs)) instead of the number of symbol records
(very large).
Some stats on symbol sizes for the curious:
PDB size: 507M
sym bytes: 316,508,016
sym count: 18,954,971
sym byte avg: 16.7
As future work, we may be able to skip copying symbol records in the
linker for realignment purposes if we make LLVM write them aligned into
the object file. We need to double check that such symbol records are
still compatible with link.exe, but if so, it's definitely worth doing,
since my profile shows we spend 500ms in memcpy in the symbol merging
code. We could potentially cut that in half by saving a copy.
Alternatively, we could apply the relocations *after* we iterate the
symbols. This would require some careful re-engineering of the
relocation processing code, though.
Reviewers: zturner, aganea, ruiu
Subscribers: hiraditya, llvm-commits
Differential Revision: https://reviews.llvm.org/D54554
llvm-svn: 347687
2018-11-28 03:00:23 +08:00
|
|
|
// Align the record if required.
|
2019-07-11 13:40:30 +08:00
|
|
|
MutableArrayRef<uint8_t> recordBytes;
|
|
|
|
if (needsRealignment) {
|
|
|
|
recordBytes = copyAndAlignSymbol(sym, alignedSymbolMem);
|
|
|
|
sym = CVSymbol(recordBytes);
|
[PDB] Add symbol records in bulk
Summary:
This speeds up linking clang.exe/pdb with /DEBUG:GHASH by 31%, from
12.9s to 9.8s.
Symbol records are typically small (16.7 bytes on average), but we
processed them one at a time. CVSymbol is a relatively "large" type. It
wraps an ArrayRef<uint8_t> with a kind an optional 32-bit hash, which we
don't need. Before this change, each DbiModuleDescriptorBuilder would
maintain an array of CVSymbols, and would write them individually with a
BinaryItemStream.
With this change, we now add symbols that happen to appear contiguously
in bulk. For each .debug$S section (roughly one per function), we
allocate two copies, one for relocation, and one for realignment
purposes. For runs of symbols that go in the module stream, which is
most symbols, we now add them as a single ArrayRef<uint8_t>, so the
vector DbiModuleDescriptorBuilder is roughly linear in the number of
.debug$S sections (O(# funcs)) instead of the number of symbol records
(very large).
Some stats on symbol sizes for the curious:
PDB size: 507M
sym bytes: 316,508,016
sym count: 18,954,971
sym byte avg: 16.7
As future work, we may be able to skip copying symbol records in the
linker for realignment purposes if we make LLVM write them aligned into
the object file. We need to double check that such symbol records are
still compatible with link.exe, but if so, it's definitely worth doing,
since my profile shows we spend 500ms in memcpy in the symbol merging
code. We could potentially cut that in half by saving a copy.
Alternatively, we could apply the relocations *after* we iterate the
symbols. This would require some careful re-engineering of the
relocation processing code, though.
Reviewers: zturner, aganea, ruiu
Subscribers: hiraditya, llvm-commits
Differential Revision: https://reviews.llvm.org/D54554
llvm-svn: 347687
2018-11-28 03:00:23 +08:00
|
|
|
} else {
|
|
|
|
// Otherwise, we can actually mutate the symbol directly, since we
|
|
|
|
// copied it to apply relocations.
|
2019-07-11 13:40:30 +08:00
|
|
|
recordBytes = makeMutableArrayRef(
|
|
|
|
const_cast<uint8_t *>(sym.data().data()), sym.length());
|
[PDB] Add symbol records in bulk
Summary:
This speeds up linking clang.exe/pdb with /DEBUG:GHASH by 31%, from
12.9s to 9.8s.
Symbol records are typically small (16.7 bytes on average), but we
processed them one at a time. CVSymbol is a relatively "large" type. It
wraps an ArrayRef<uint8_t> with a kind an optional 32-bit hash, which we
don't need. Before this change, each DbiModuleDescriptorBuilder would
maintain an array of CVSymbols, and would write them individually with a
BinaryItemStream.
With this change, we now add symbols that happen to appear contiguously
in bulk. For each .debug$S section (roughly one per function), we
allocate two copies, one for relocation, and one for realignment
purposes. For runs of symbols that go in the module stream, which is
most symbols, we now add them as a single ArrayRef<uint8_t>, so the
vector DbiModuleDescriptorBuilder is roughly linear in the number of
.debug$S sections (O(# funcs)) instead of the number of symbol records
(very large).
Some stats on symbol sizes for the curious:
PDB size: 507M
sym bytes: 316,508,016
sym count: 18,954,971
sym byte avg: 16.7
As future work, we may be able to skip copying symbol records in the
linker for realignment purposes if we make LLVM write them aligned into
the object file. We need to double check that such symbol records are
still compatible with link.exe, but if so, it's definitely worth doing,
since my profile shows we spend 500ms in memcpy in the symbol merging
code. We could potentially cut that in half by saving a copy.
Alternatively, we could apply the relocations *after* we iterate the
symbols. This would require some careful re-engineering of the
relocation processing code, though.
Reviewers: zturner, aganea, ruiu
Subscribers: hiraditya, llvm-commits
Differential Revision: https://reviews.llvm.org/D54554
llvm-svn: 347687
2018-11-28 03:00:23 +08:00
|
|
|
}
|
|
|
|
|
2018-01-19 02:35:01 +08:00
|
|
|
// Discover type index references in the record. Skip it if we don't
|
|
|
|
// know where they are.
|
2019-07-11 13:40:30 +08:00
|
|
|
SmallVector<TiReference, 32> typeRefs;
|
|
|
|
if (!discoverTypeIndicesInSymbol(sym, typeRefs)) {
|
2018-01-19 02:35:01 +08:00
|
|
|
log("ignoring unknown symbol record with kind 0x" +
|
2019-07-11 13:40:30 +08:00
|
|
|
utohexstr(sym.kind()));
|
2018-01-19 02:35:01 +08:00
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-map all the type index references.
|
2019-07-11 13:40:30 +08:00
|
|
|
remapTypesInSymbolRecord(file, sym.kind(), recordBytes, indexMap,
|
|
|
|
typeRefs);
|
2018-01-19 02:35:01 +08:00
|
|
|
|
|
|
|
// An object file may have S_xxx_ID symbols, but these get converted to
|
|
|
|
// "real" symbols in a PDB.
|
2019-07-11 13:40:30 +08:00
|
|
|
translateIdSymbols(recordBytes, tMerger.getIDTable());
|
|
|
|
sym = CVSymbol(recordBytes);
|
2018-01-19 02:35:01 +08:00
|
|
|
|
|
|
|
// If this record refers to an offset in the object file's string table,
|
|
|
|
// add that item to the global PDB string table and re-write the index.
|
2019-07-11 13:40:30 +08:00
|
|
|
recordStringTableReferences(sym.kind(), recordBytes, stringTableRefs);
|
2018-01-19 02:35:01 +08:00
|
|
|
|
|
|
|
// Fill in "Parent" and "End" fields by maintaining a stack of scopes.
|
2019-07-11 13:40:30 +08:00
|
|
|
if (symbolOpensScope(sym.kind()))
|
|
|
|
scopeStackOpen(scopes, curSymOffset, sym);
|
|
|
|
else if (symbolEndsScope(sym.kind()))
|
|
|
|
scopeStackClose(scopes, curSymOffset, file);
|
2018-01-19 02:35:01 +08:00
|
|
|
|
|
|
|
// Add the symbol to the globals stream if necessary. Do this before
|
|
|
|
// adding the symbol to the module since we may need to get the next
|
|
|
|
// symbol offset, and writing to the module's symbol stream will update
|
|
|
|
// that offset.
|
2019-07-11 13:40:30 +08:00
|
|
|
if (symbolGoesInGlobalsStream(sym, scopes.empty())) {
|
|
|
|
addGlobalSymbol(builder.getGsiBuilder(),
|
|
|
|
file->moduleDBI->getModuleIndex(), curSymOffset, sym);
|
|
|
|
++globalSymbols;
|
2019-03-15 02:45:08 +08:00
|
|
|
}
|
[PDB] Add symbol records in bulk
Summary:
This speeds up linking clang.exe/pdb with /DEBUG:GHASH by 31%, from
12.9s to 9.8s.
Symbol records are typically small (16.7 bytes on average), but we
processed them one at a time. CVSymbol is a relatively "large" type. It
wraps an ArrayRef<uint8_t> with a kind an optional 32-bit hash, which we
don't need. Before this change, each DbiModuleDescriptorBuilder would
maintain an array of CVSymbols, and would write them individually with a
BinaryItemStream.
With this change, we now add symbols that happen to appear contiguously
in bulk. For each .debug$S section (roughly one per function), we
allocate two copies, one for relocation, and one for realignment
purposes. For runs of symbols that go in the module stream, which is
most symbols, we now add them as a single ArrayRef<uint8_t>, so the
vector DbiModuleDescriptorBuilder is roughly linear in the number of
.debug$S sections (O(# funcs)) instead of the number of symbol records
(very large).
Some stats on symbol sizes for the curious:
PDB size: 507M
sym bytes: 316,508,016
sym count: 18,954,971
sym byte avg: 16.7
As future work, we may be able to skip copying symbol records in the
linker for realignment purposes if we make LLVM write them aligned into
the object file. We need to double check that such symbol records are
still compatible with link.exe, but if so, it's definitely worth doing,
since my profile shows we spend 500ms in memcpy in the symbol merging
code. We could potentially cut that in half by saving a copy.
Alternatively, we could apply the relocations *after* we iterate the
symbols. This would require some careful re-engineering of the
relocation processing code, though.
Reviewers: zturner, aganea, ruiu
Subscribers: hiraditya, llvm-commits
Differential Revision: https://reviews.llvm.org/D54554
llvm-svn: 347687
2018-11-28 03:00:23 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (symbolGoesInModuleStream(sym, scopes.empty())) {
|
[PDB] Add symbol records in bulk
Summary:
This speeds up linking clang.exe/pdb with /DEBUG:GHASH by 31%, from
12.9s to 9.8s.
Symbol records are typically small (16.7 bytes on average), but we
processed them one at a time. CVSymbol is a relatively "large" type. It
wraps an ArrayRef<uint8_t> with a kind an optional 32-bit hash, which we
don't need. Before this change, each DbiModuleDescriptorBuilder would
maintain an array of CVSymbols, and would write them individually with a
BinaryItemStream.
With this change, we now add symbols that happen to appear contiguously
in bulk. For each .debug$S section (roughly one per function), we
allocate two copies, one for relocation, and one for realignment
purposes. For runs of symbols that go in the module stream, which is
most symbols, we now add them as a single ArrayRef<uint8_t>, so the
vector DbiModuleDescriptorBuilder is roughly linear in the number of
.debug$S sections (O(# funcs)) instead of the number of symbol records
(very large).
Some stats on symbol sizes for the curious:
PDB size: 507M
sym bytes: 316,508,016
sym count: 18,954,971
sym byte avg: 16.7
As future work, we may be able to skip copying symbol records in the
linker for realignment purposes if we make LLVM write them aligned into
the object file. We need to double check that such symbol records are
still compatible with link.exe, but if so, it's definitely worth doing,
since my profile shows we spend 500ms in memcpy in the symbol merging
code. We could potentially cut that in half by saving a copy.
Alternatively, we could apply the relocations *after* we iterate the
symbols. This would require some careful re-engineering of the
relocation processing code, though.
Reviewers: zturner, aganea, ruiu
Subscribers: hiraditya, llvm-commits
Differential Revision: https://reviews.llvm.org/D54554
llvm-svn: 347687
2018-11-28 03:00:23 +08:00
|
|
|
// Add symbols to the module in bulk. If this symbol is contiguous
|
|
|
|
// with the previous run of symbols to add, combine the ranges. If
|
|
|
|
// not, close the previous range of symbols and start a new one.
|
2019-07-11 13:40:30 +08:00
|
|
|
if (sym.data().data() == bulkSymbols.end()) {
|
|
|
|
bulkSymbols = makeArrayRef(bulkSymbols.data(),
|
|
|
|
bulkSymbols.size() + sym.length());
|
[PDB] Add symbol records in bulk
Summary:
This speeds up linking clang.exe/pdb with /DEBUG:GHASH by 31%, from
12.9s to 9.8s.
Symbol records are typically small (16.7 bytes on average), but we
processed them one at a time. CVSymbol is a relatively "large" type. It
wraps an ArrayRef<uint8_t> with a kind an optional 32-bit hash, which we
don't need. Before this change, each DbiModuleDescriptorBuilder would
maintain an array of CVSymbols, and would write them individually with a
BinaryItemStream.
With this change, we now add symbols that happen to appear contiguously
in bulk. For each .debug$S section (roughly one per function), we
allocate two copies, one for relocation, and one for realignment
purposes. For runs of symbols that go in the module stream, which is
most symbols, we now add them as a single ArrayRef<uint8_t>, so the
vector DbiModuleDescriptorBuilder is roughly linear in the number of
.debug$S sections (O(# funcs)) instead of the number of symbol records
(very large).
Some stats on symbol sizes for the curious:
PDB size: 507M
sym bytes: 316,508,016
sym count: 18,954,971
sym byte avg: 16.7
As future work, we may be able to skip copying symbol records in the
linker for realignment purposes if we make LLVM write them aligned into
the object file. We need to double check that such symbol records are
still compatible with link.exe, but if so, it's definitely worth doing,
since my profile shows we spend 500ms in memcpy in the symbol merging
code. We could potentially cut that in half by saving a copy.
Alternatively, we could apply the relocations *after* we iterate the
symbols. This would require some careful re-engineering of the
relocation processing code, though.
Reviewers: zturner, aganea, ruiu
Subscribers: hiraditya, llvm-commits
Differential Revision: https://reviews.llvm.org/D54554
llvm-svn: 347687
2018-11-28 03:00:23 +08:00
|
|
|
} else {
|
2019-07-11 13:40:30 +08:00
|
|
|
file->moduleDBI->addSymbolsInBulk(bulkSymbols);
|
|
|
|
bulkSymbols = recordBytes;
|
[PDB] Add symbol records in bulk
Summary:
This speeds up linking clang.exe/pdb with /DEBUG:GHASH by 31%, from
12.9s to 9.8s.
Symbol records are typically small (16.7 bytes on average), but we
processed them one at a time. CVSymbol is a relatively "large" type. It
wraps an ArrayRef<uint8_t> with a kind an optional 32-bit hash, which we
don't need. Before this change, each DbiModuleDescriptorBuilder would
maintain an array of CVSymbols, and would write them individually with a
BinaryItemStream.
With this change, we now add symbols that happen to appear contiguously
in bulk. For each .debug$S section (roughly one per function), we
allocate two copies, one for relocation, and one for realignment
purposes. For runs of symbols that go in the module stream, which is
most symbols, we now add them as a single ArrayRef<uint8_t>, so the
vector DbiModuleDescriptorBuilder is roughly linear in the number of
.debug$S sections (O(# funcs)) instead of the number of symbol records
(very large).
Some stats on symbol sizes for the curious:
PDB size: 507M
sym bytes: 316,508,016
sym count: 18,954,971
sym byte avg: 16.7
As future work, we may be able to skip copying symbol records in the
linker for realignment purposes if we make LLVM write them aligned into
the object file. We need to double check that such symbol records are
still compatible with link.exe, but if so, it's definitely worth doing,
since my profile shows we spend 500ms in memcpy in the symbol merging
code. We could potentially cut that in half by saving a copy.
Alternatively, we could apply the relocations *after* we iterate the
symbols. This would require some careful re-engineering of the
relocation processing code, though.
Reviewers: zturner, aganea, ruiu
Subscribers: hiraditya, llvm-commits
Differential Revision: https://reviews.llvm.org/D54554
llvm-svn: 347687
2018-11-28 03:00:23 +08:00
|
|
|
}
|
2019-07-11 13:40:30 +08:00
|
|
|
curSymOffset += sym.length();
|
|
|
|
++moduleSymbols;
|
[PDB] Add symbol records in bulk
Summary:
This speeds up linking clang.exe/pdb with /DEBUG:GHASH by 31%, from
12.9s to 9.8s.
Symbol records are typically small (16.7 bytes on average), but we
processed them one at a time. CVSymbol is a relatively "large" type. It
wraps an ArrayRef<uint8_t> with a kind an optional 32-bit hash, which we
don't need. Before this change, each DbiModuleDescriptorBuilder would
maintain an array of CVSymbols, and would write them individually with a
BinaryItemStream.
With this change, we now add symbols that happen to appear contiguously
in bulk. For each .debug$S section (roughly one per function), we
allocate two copies, one for relocation, and one for realignment
purposes. For runs of symbols that go in the module stream, which is
most symbols, we now add them as a single ArrayRef<uint8_t>, so the
vector DbiModuleDescriptorBuilder is roughly linear in the number of
.debug$S sections (O(# funcs)) instead of the number of symbol records
(very large).
Some stats on symbol sizes for the curious:
PDB size: 507M
sym bytes: 316,508,016
sym count: 18,954,971
sym byte avg: 16.7
As future work, we may be able to skip copying symbol records in the
linker for realignment purposes if we make LLVM write them aligned into
the object file. We need to double check that such symbol records are
still compatible with link.exe, but if so, it's definitely worth doing,
since my profile shows we spend 500ms in memcpy in the symbol merging
code. We could potentially cut that in half by saving a copy.
Alternatively, we could apply the relocations *after* we iterate the
symbols. This would require some careful re-engineering of the
relocation processing code, though.
Reviewers: zturner, aganea, ruiu
Subscribers: hiraditya, llvm-commits
Differential Revision: https://reviews.llvm.org/D54554
llvm-svn: 347687
2018-11-28 03:00:23 +08:00
|
|
|
}
|
2018-01-19 02:35:01 +08:00
|
|
|
return Error::success();
|
[PDB] Add symbol records in bulk
Summary:
This speeds up linking clang.exe/pdb with /DEBUG:GHASH by 31%, from
12.9s to 9.8s.
Symbol records are typically small (16.7 bytes on average), but we
processed them one at a time. CVSymbol is a relatively "large" type. It
wraps an ArrayRef<uint8_t> with a kind an optional 32-bit hash, which we
don't need. Before this change, each DbiModuleDescriptorBuilder would
maintain an array of CVSymbols, and would write them individually with a
BinaryItemStream.
With this change, we now add symbols that happen to appear contiguously
in bulk. For each .debug$S section (roughly one per function), we
allocate two copies, one for relocation, and one for realignment
purposes. For runs of symbols that go in the module stream, which is
most symbols, we now add them as a single ArrayRef<uint8_t>, so the
vector DbiModuleDescriptorBuilder is roughly linear in the number of
.debug$S sections (O(# funcs)) instead of the number of symbol records
(very large).
Some stats on symbol sizes for the curious:
PDB size: 507M
sym bytes: 316,508,016
sym count: 18,954,971
sym byte avg: 16.7
As future work, we may be able to skip copying symbol records in the
linker for realignment purposes if we make LLVM write them aligned into
the object file. We need to double check that such symbol records are
still compatible with link.exe, but if so, it's definitely worth doing,
since my profile shows we spend 500ms in memcpy in the symbol merging
code. We could potentially cut that in half by saving a copy.
Alternatively, we could apply the relocations *after* we iterate the
symbols. This would require some careful re-engineering of the
relocation processing code, though.
Reviewers: zturner, aganea, ruiu
Subscribers: hiraditya, llvm-commits
Differential Revision: https://reviews.llvm.org/D54554
llvm-svn: 347687
2018-11-28 03:00:23 +08:00
|
|
|
}));
|
|
|
|
|
|
|
|
// Add any remaining symbols we've accumulated.
|
2019-07-11 13:40:30 +08:00
|
|
|
file->moduleDBI->addSymbolsInBulk(bulkSymbols);
|
2017-06-22 01:25:56 +08:00
|
|
|
}
|
|
|
|
|
2018-09-13 05:02:01 +08:00
|
|
|
// Allocate memory for a .debug$S / .debug$F section and relocate it.
|
2019-07-11 13:40:30 +08:00
|
|
|
static ArrayRef<uint8_t> relocateDebugChunk(BumpPtrAllocator &alloc,
|
|
|
|
SectionChunk &debugChunk) {
|
|
|
|
uint8_t *buffer = alloc.Allocate<uint8_t>(debugChunk.getSize());
|
|
|
|
assert(debugChunk.getOutputSectionIdx() == 0 &&
|
2017-06-20 01:21:45 +08:00
|
|
|
"debug sections should not be in output sections");
|
2019-07-11 13:40:30 +08:00
|
|
|
debugChunk.writeTo(buffer);
|
|
|
|
return makeArrayRef(buffer, debugChunk.getSize());
|
2017-06-20 01:21:45 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
static pdb::SectionContrib createSectionContrib(const Chunk *c, uint32_t modi) {
|
|
|
|
OutputSection *os = c ? c->getOutputSection() : nullptr;
|
|
|
|
pdb::SectionContrib sc;
|
|
|
|
memset(&sc, 0, sizeof(sc));
|
|
|
|
sc.ISect = os ? os->sectionIndex : llvm::pdb::kInvalidStreamIndex;
|
|
|
|
sc.Off = c && os ? c->getRVA() - os->getRVA() : 0;
|
|
|
|
sc.Size = c ? c->getSize() : -1;
|
|
|
|
if (auto *secChunk = dyn_cast_or_null<SectionChunk>(c)) {
|
|
|
|
sc.Characteristics = secChunk->header->Characteristics;
|
|
|
|
sc.Imod = secChunk->file->moduleDBI->getModuleIndex();
|
|
|
|
ArrayRef<uint8_t> contents = secChunk->getContents();
|
|
|
|
JamCRC crc(0);
|
|
|
|
ArrayRef<char> charContents = makeArrayRef(
|
|
|
|
reinterpret_cast<const char *>(contents.data()), contents.size());
|
|
|
|
crc.update(charContents);
|
|
|
|
sc.DataCrc = crc.getCRC();
|
2018-04-21 02:00:46 +08:00
|
|
|
} else {
|
2019-07-11 13:40:30 +08:00
|
|
|
sc.Characteristics = os ? os->header.Characteristics : 0;
|
|
|
|
sc.Imod = modi;
|
2018-04-21 02:00:46 +08:00
|
|
|
}
|
2019-07-11 13:40:30 +08:00
|
|
|
sc.RelocCrc = 0; // FIXME
|
2018-04-21 02:00:46 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
return sc;
|
2018-04-21 02:00:46 +08:00
|
|
|
}
|
|
|
|
|
2018-09-12 06:35:01 +08:00
|
|
|
static uint32_t
|
2019-07-11 13:40:30 +08:00
|
|
|
translateStringTableIndex(uint32_t objIndex,
|
|
|
|
const DebugStringTableSubsectionRef &objStrTable,
|
|
|
|
DebugStringTableSubsection &pdbStrTable) {
|
|
|
|
auto expectedString = objStrTable.getString(objIndex);
|
|
|
|
if (!expectedString) {
|
2018-09-12 06:35:01 +08:00
|
|
|
warn("Invalid string table reference");
|
2019-07-11 13:40:30 +08:00
|
|
|
consumeError(expectedString.takeError());
|
2018-09-12 06:35:01 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
return pdbStrTable.insert(*expectedString);
|
2018-09-12 06:35:01 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
void DebugSHandler::handleDebugS(lld::coff::SectionChunk &debugS) {
|
|
|
|
DebugSubsectionArray subsections;
|
2018-09-13 05:02:01 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
ArrayRef<uint8_t> relocatedDebugContents = SectionChunk::consumeDebugMagic(
|
|
|
|
relocateDebugChunk(linker.alloc, debugS), debugS.getSectionName());
|
2018-09-13 05:02:01 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
BinaryStreamReader reader(relocatedDebugContents, support::little);
|
|
|
|
exitOnErr(reader.readArray(subsections, relocatedDebugContents.size()));
|
2018-09-13 05:02:01 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
for (const DebugSubsectionRecord &ss : subsections) {
|
2019-06-19 03:41:25 +08:00
|
|
|
// Ignore subsections with the 'ignore' bit. Some versions of the Visual C++
|
|
|
|
// runtime have subsections with this bit set.
|
2019-07-11 13:40:30 +08:00
|
|
|
if (uint32_t(ss.kind()) & codeview::SubsectionIgnoreFlag)
|
2019-06-19 03:41:25 +08:00
|
|
|
continue;
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
switch (ss.kind()) {
|
2018-09-13 05:02:01 +08:00
|
|
|
case DebugSubsectionKind::StringTable: {
|
2019-07-11 13:40:30 +08:00
|
|
|
assert(!cVStrTab.valid() &&
|
2018-09-13 05:02:01 +08:00
|
|
|
"Encountered multiple string table subsections!");
|
2019-07-11 13:40:30 +08:00
|
|
|
exitOnErr(cVStrTab.initialize(ss.getRecordData()));
|
2018-09-13 05:02:01 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DebugSubsectionKind::FileChecksums:
|
2019-07-11 13:40:30 +08:00
|
|
|
assert(!checksums.valid() &&
|
2018-09-13 05:02:01 +08:00
|
|
|
"Encountered multiple checksum subsections!");
|
2019-07-11 13:40:30 +08:00
|
|
|
exitOnErr(checksums.initialize(ss.getRecordData()));
|
2018-09-13 05:02:01 +08:00
|
|
|
break;
|
|
|
|
case DebugSubsectionKind::Lines:
|
|
|
|
// We can add the relocated line table directly to the PDB without
|
|
|
|
// modification because the file checksum offsets will stay the same.
|
2019-07-11 13:40:30 +08:00
|
|
|
file.moduleDBI->addDebugSubsection(ss);
|
2018-09-13 05:02:01 +08:00
|
|
|
break;
|
2019-06-04 02:15:38 +08:00
|
|
|
case DebugSubsectionKind::InlineeLines:
|
2019-07-11 13:40:30 +08:00
|
|
|
assert(!inlineeLines.valid() &&
|
2019-06-04 02:15:38 +08:00
|
|
|
"Encountered multiple inlinee lines subsections!");
|
2019-07-11 13:40:30 +08:00
|
|
|
exitOnErr(inlineeLines.initialize(ss.getRecordData()));
|
2019-06-04 02:15:38 +08:00
|
|
|
break;
|
2018-09-13 05:02:01 +08:00
|
|
|
case DebugSubsectionKind::FrameData: {
|
|
|
|
// We need to re-write string table indices here, so save off all
|
|
|
|
// frame data subsections until we've processed the entire list of
|
|
|
|
// subsections so that we can be sure we have the string table.
|
2019-07-11 13:40:30 +08:00
|
|
|
DebugFrameDataSubsectionRef fds;
|
|
|
|
exitOnErr(fds.initialize(ss.getRecordData()));
|
|
|
|
newFpoFrames.push_back(std::move(fds));
|
2018-09-13 05:02:01 +08:00
|
|
|
break;
|
|
|
|
}
|
2018-11-10 09:36:02 +08:00
|
|
|
case DebugSubsectionKind::Symbols: {
|
2019-07-11 13:40:30 +08:00
|
|
|
linker.mergeSymbolRecords(&file, indexMap, stringTableReferences,
|
|
|
|
ss.getRecordData());
|
2018-09-13 05:02:01 +08:00
|
|
|
break;
|
2018-11-10 09:36:02 +08:00
|
|
|
}
|
2019-06-04 02:15:38 +08:00
|
|
|
|
|
|
|
case DebugSubsectionKind::CrossScopeImports:
|
|
|
|
case DebugSubsectionKind::CrossScopeExports:
|
|
|
|
// These appear to relate to cross-module optimization, so we might use
|
|
|
|
// these for ThinLTO.
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DebugSubsectionKind::ILLines:
|
|
|
|
case DebugSubsectionKind::FuncMDTokenMap:
|
|
|
|
case DebugSubsectionKind::TypeMDTokenMap:
|
|
|
|
case DebugSubsectionKind::MergedAssemblyInput:
|
|
|
|
// These appear to relate to .Net assembly info.
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DebugSubsectionKind::CoffSymbolRVA:
|
|
|
|
// Unclear what this is for.
|
|
|
|
break;
|
|
|
|
|
2018-09-13 05:02:01 +08:00
|
|
|
default:
|
2019-06-04 02:15:38 +08:00
|
|
|
warn("ignoring unknown debug$S subsection kind 0x" +
|
2019-07-11 13:40:30 +08:00
|
|
|
utohexstr(uint32_t(ss.kind())) + " in file " + toString(&file));
|
2018-09-13 05:02:01 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-04 02:15:38 +08:00
|
|
|
static Expected<StringRef>
|
2019-07-11 13:40:30 +08:00
|
|
|
getFileName(const DebugStringTableSubsectionRef &strings,
|
|
|
|
const DebugChecksumsSubsectionRef &checksums, uint32_t fileID) {
|
|
|
|
auto iter = checksums.getArray().at(fileID);
|
|
|
|
if (iter == checksums.getArray().end())
|
2019-06-04 02:15:38 +08:00
|
|
|
return make_error<CodeViewError>(cv_error_code::no_records);
|
2019-07-11 13:40:30 +08:00
|
|
|
uint32_t offset = iter->FileNameOffset;
|
|
|
|
return strings.getString(offset);
|
2019-06-04 02:15:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<DebugInlineeLinesSubsection>
|
2019-07-11 13:40:30 +08:00
|
|
|
DebugSHandler::mergeInlineeLines(DebugChecksumsSubsection *newChecksums) {
|
|
|
|
auto newInlineeLines = std::make_shared<DebugInlineeLinesSubsection>(
|
|
|
|
*newChecksums, inlineeLines.hasExtraFiles());
|
|
|
|
|
|
|
|
for (const InlineeSourceLine &line : inlineeLines) {
|
|
|
|
TypeIndex inlinee = line.Header->Inlinee;
|
|
|
|
uint32_t fileID = line.Header->FileID;
|
|
|
|
uint32_t sourceLine = line.Header->SourceLineNum;
|
|
|
|
|
|
|
|
ArrayRef<TypeIndex> typeOrItemMap =
|
|
|
|
indexMap.isTypeServerMap ? indexMap.ipiMap : indexMap.tpiMap;
|
|
|
|
if (!remapTypeIndex(inlinee, typeOrItemMap)) {
|
|
|
|
log("ignoring inlinee line record in " + file.getName() +
|
|
|
|
" with bad inlinee index 0x" + utohexstr(inlinee.getIndex()));
|
2019-06-04 02:15:38 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
SmallString<128> filename =
|
|
|
|
exitOnErr(getFileName(cVStrTab, checksums, fileID));
|
|
|
|
pdbMakeAbsolute(filename);
|
|
|
|
newInlineeLines->addInlineSite(inlinee, filename, sourceLine);
|
2019-06-04 02:15:38 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (inlineeLines.hasExtraFiles()) {
|
|
|
|
for (uint32_t extraFileId : line.ExtraFiles) {
|
|
|
|
filename = exitOnErr(getFileName(cVStrTab, checksums, extraFileId));
|
|
|
|
pdbMakeAbsolute(filename);
|
|
|
|
newInlineeLines->addExtraFile(filename);
|
2019-06-04 02:15:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
return newInlineeLines;
|
2019-06-04 02:15:38 +08:00
|
|
|
}
|
|
|
|
|
2018-09-13 05:02:01 +08:00
|
|
|
void DebugSHandler::finish() {
|
2019-07-11 13:40:30 +08:00
|
|
|
pdb::DbiStreamBuilder &dbiBuilder = linker.builder.getDbiBuilder();
|
2018-09-13 05:02:01 +08:00
|
|
|
|
|
|
|
// We should have seen all debug subsections across the entire object file now
|
|
|
|
// which means that if a StringTable subsection and Checksums subsection were
|
|
|
|
// present, now is the time to handle them.
|
2019-07-11 13:40:30 +08:00
|
|
|
if (!cVStrTab.valid()) {
|
|
|
|
if (checksums.valid())
|
2018-09-13 05:02:01 +08:00
|
|
|
fatal(".debug$S sections with a checksums subsection must also contain a "
|
|
|
|
"string table subsection");
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (!stringTableReferences.empty())
|
2018-09-13 05:02:01 +08:00
|
|
|
warn("No StringTable subsection was encountered, but there are string "
|
|
|
|
"table references");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rewrite string table indices in the Fpo Data and symbol records to refer to
|
|
|
|
// the global PDB string table instead of the object file string table.
|
2019-07-11 13:40:30 +08:00
|
|
|
for (DebugFrameDataSubsectionRef &fds : newFpoFrames) {
|
|
|
|
const ulittle32_t *reloc = fds.getRelocPtr();
|
|
|
|
for (codeview::FrameData fd : fds) {
|
|
|
|
fd.RvaStart += *reloc;
|
|
|
|
fd.FrameFunc =
|
|
|
|
translateStringTableIndex(fd.FrameFunc, cVStrTab, linker.pdbStrTab);
|
|
|
|
dbiBuilder.addNewFpoData(fd);
|
2018-09-13 05:02:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
for (ulittle32_t *ref : stringTableReferences)
|
|
|
|
*ref = translateStringTableIndex(*ref, cVStrTab, linker.pdbStrTab);
|
2018-09-13 05:02:01 +08:00
|
|
|
|
|
|
|
// Make a new file checksum table that refers to offsets in the PDB-wide
|
|
|
|
// string table. Generally the string table subsection appears after the
|
|
|
|
// checksum table, so we have to do this after looping over all the
|
|
|
|
// subsections.
|
2019-07-11 13:40:30 +08:00
|
|
|
auto newChecksums = make_unique<DebugChecksumsSubsection>(linker.pdbStrTab);
|
|
|
|
for (FileChecksumEntry &fc : checksums) {
|
|
|
|
SmallString<128> filename =
|
|
|
|
exitOnErr(cVStrTab.getString(fc.FileNameOffset));
|
|
|
|
pdbMakeAbsolute(filename);
|
|
|
|
exitOnErr(dbiBuilder.addModuleSourceFile(*file.moduleDBI, filename));
|
|
|
|
newChecksums->addChecksum(filename, fc.Kind, fc.Checksum);
|
2018-09-13 05:02:01 +08:00
|
|
|
}
|
2019-06-04 02:15:38 +08:00
|
|
|
|
|
|
|
// Rewrite inlinee item indices if present.
|
2019-07-11 13:40:30 +08:00
|
|
|
if (inlineeLines.valid())
|
|
|
|
file.moduleDBI->addDebugSubsection(mergeInlineeLines(newChecksums.get()));
|
2019-06-04 02:15:38 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
file.moduleDBI->addDebugSubsection(std::move(newChecksums));
|
2018-09-13 05:02:01 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
void PDBLinker::addObjFile(ObjFile *file, CVIndexMap *externIndexMap) {
|
|
|
|
if (file->mergedIntoPDB)
|
2018-11-06 03:20:47 +08:00
|
|
|
return;
|
2019-07-11 13:40:30 +08:00
|
|
|
file->mergedIntoPDB = true;
|
2018-04-21 02:00:46 +08:00
|
|
|
|
2017-07-14 08:14:58 +08:00
|
|
|
// Before we can process symbol substreams from .debug$S, we need to process
|
|
|
|
// type information, file checksums, and the string table. Add type info to
|
|
|
|
// the PDB first, so that we can get the map from object file type and item
|
|
|
|
// indices to PDB type and item indices.
|
2019-07-11 13:40:30 +08:00
|
|
|
CVIndexMap objectIndexMap;
|
|
|
|
auto indexMapResult =
|
|
|
|
mergeDebugT(file, externIndexMap ? externIndexMap : &objectIndexMap);
|
2018-02-01 01:48:04 +08:00
|
|
|
|
|
|
|
// If the .debug$T sections fail to merge, assume there is no debug info.
|
2019-07-11 13:40:30 +08:00
|
|
|
if (!indexMapResult) {
|
|
|
|
if (!config->warnDebugInfoUnusable) {
|
|
|
|
consumeError(indexMapResult.takeError());
|
2019-01-12 03:10:01 +08:00
|
|
|
return;
|
2019-01-12 04:39:38 +08:00
|
|
|
}
|
2019-07-11 13:40:30 +08:00
|
|
|
warn("Cannot use debug info for '" + toString(file) + "' [LNK4099]\n" +
|
2018-09-10 21:51:21 +08:00
|
|
|
">>> failed to load reference " +
|
2019-07-11 13:40:30 +08:00
|
|
|
StringRef(toString(indexMapResult.takeError())));
|
2018-02-01 01:48:04 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
ScopedTimer t(symbolMergingTimer);
|
2018-01-18 03:16:26 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
pdb::DbiStreamBuilder &dbiBuilder = builder.getDbiBuilder();
|
|
|
|
DebugSHandler dsh(*this, *file, *indexMapResult);
|
2018-09-13 05:02:01 +08:00
|
|
|
// Now do all live .debug$S and .debug$F sections.
|
2019-07-11 13:40:30 +08:00
|
|
|
for (SectionChunk *debugChunk : file->getDebugChunks()) {
|
|
|
|
if (!debugChunk->live || debugChunk->getSize() == 0)
|
2017-07-14 08:14:58 +08:00
|
|
|
continue;
|
2017-06-20 01:21:45 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (debugChunk->getSectionName() == ".debug$S") {
|
|
|
|
dsh.handleDebugS(*debugChunk);
|
2017-07-14 08:14:58 +08:00
|
|
|
continue;
|
|
|
|
}
|
2017-06-20 01:21:45 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (debugChunk->getSectionName() == ".debug$F") {
|
|
|
|
ArrayRef<uint8_t> relocatedDebugContents =
|
|
|
|
relocateDebugChunk(alloc, *debugChunk);
|
2018-01-06 03:12:40 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
FixedStreamArray<object::FpoData> fpoRecords;
|
|
|
|
BinaryStreamReader reader(relocatedDebugContents, support::little);
|
|
|
|
uint32_t count = relocatedDebugContents.size() / sizeof(object::FpoData);
|
|
|
|
exitOnErr(reader.readArray(fpoRecords, count));
|
2018-01-06 03:12:40 +08:00
|
|
|
|
2018-09-13 05:02:01 +08:00
|
|
|
// These are already relocated and don't refer to the string table, so we
|
|
|
|
// can just copy it.
|
2019-07-11 13:40:30 +08:00
|
|
|
for (const object::FpoData &fd : fpoRecords)
|
|
|
|
dbiBuilder.addOldFpoData(fd);
|
2018-09-13 05:02:01 +08:00
|
|
|
continue;
|
2017-06-20 01:21:45 +08:00
|
|
|
}
|
2018-01-06 03:12:40 +08:00
|
|
|
}
|
|
|
|
|
2018-09-13 05:02:01 +08:00
|
|
|
// Do any post-processing now that all .debug$S sections have been processed.
|
2019-07-11 13:40:30 +08:00
|
|
|
dsh.finish();
|
2017-07-14 08:14:58 +08:00
|
|
|
}
|
2017-01-12 11:09:25 +08:00
|
|
|
|
2019-03-23 06:07:27 +08:00
|
|
|
// Add a module descriptor for every object file. We need to put an absolute
|
|
|
|
// path to the object into the PDB. If this is a plain object, we make its
|
|
|
|
// path absolute. If it's an object in an archive, we make the archive path
|
|
|
|
// absolute.
|
2019-07-11 13:40:30 +08:00
|
|
|
static void createModuleDBI(pdb::PDBFileBuilder &builder) {
|
|
|
|
pdb::DbiStreamBuilder &dbiBuilder = builder.getDbiBuilder();
|
|
|
|
SmallString<128> objName;
|
2019-03-23 06:07:27 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
for (ObjFile *file : ObjFile::instances) {
|
2019-03-23 06:07:27 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
bool inArchive = !file->parentName.empty();
|
|
|
|
objName = inArchive ? file->parentName : file->getName();
|
|
|
|
pdbMakeAbsolute(objName);
|
|
|
|
StringRef modName = inArchive ? file->getName() : StringRef(objName);
|
2019-03-23 06:07:27 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
file->moduleDBI = &exitOnErr(dbiBuilder.addModuleInfo(modName));
|
|
|
|
file->moduleDBI->setObjFileName(objName);
|
2019-03-23 06:07:27 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
ArrayRef<Chunk *> chunks = file->getChunks();
|
|
|
|
uint32_t modi = file->moduleDBI->getModuleIndex();
|
2019-03-23 06:07:27 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
for (Chunk *c : chunks) {
|
|
|
|
auto *secChunk = dyn_cast<SectionChunk>(c);
|
|
|
|
if (!secChunk || !secChunk->live)
|
2019-03-23 06:07:27 +08:00
|
|
|
continue;
|
2019-07-11 13:40:30 +08:00
|
|
|
pdb::SectionContrib sc = createSectionContrib(secChunk, modi);
|
|
|
|
file->moduleDBI->setFirstSectionContrib(sc);
|
2019-03-23 06:07:27 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
static PublicSym32 createPublic(Defined *def) {
|
|
|
|
PublicSym32 pub(SymbolKind::S_PUB32);
|
|
|
|
pub.Name = def->getName();
|
|
|
|
if (auto *d = dyn_cast<DefinedCOFF>(def)) {
|
|
|
|
if (d->getCOFFSymbol().isFunctionDefinition())
|
|
|
|
pub.Flags = PublicSymFlags::Function;
|
|
|
|
} else if (isa<DefinedImportThunk>(def)) {
|
|
|
|
pub.Flags = PublicSymFlags::Function;
|
2017-07-28 02:25:59 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
OutputSection *os = def->getChunk()->getOutputSection();
|
|
|
|
assert(os && "all publics should be in final image");
|
|
|
|
pub.Offset = def->getRVA() - os->getRVA();
|
|
|
|
pub.Segment = os->sectionIndex;
|
|
|
|
return pub;
|
2017-07-28 02:25:59 +08:00
|
|
|
}
|
|
|
|
|
2017-07-14 08:14:58 +08:00
|
|
|
// Add all object files to the PDB. Merge .debug$T sections into IpiData and
|
|
|
|
// TpiData.
|
|
|
|
void PDBLinker::addObjectsToPDB() {
|
2019-07-11 13:40:30 +08:00
|
|
|
ScopedTimer t1(addObjectsTimer);
|
2019-03-23 06:07:27 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
createModuleDBI(builder);
|
2019-03-23 06:07:27 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
for (ObjFile *file : ObjFile::instances)
|
|
|
|
addObjFile(file);
|
2017-07-14 08:14:58 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
builder.getStringTableBuilder().setStrings(pdbStrTab);
|
|
|
|
t1.stop();
|
2017-06-20 01:21:45 +08:00
|
|
|
|
2017-12-15 02:07:04 +08:00
|
|
|
// Construct TPI and IPI stream contents.
|
2019-07-11 13:40:30 +08:00
|
|
|
ScopedTimer t2(tpiStreamLayoutTimer);
|
|
|
|
addTypeInfo(builder.getTpiBuilder(), tMerger.getTypeTable());
|
|
|
|
addTypeInfo(builder.getIpiBuilder(), tMerger.getIDTable());
|
|
|
|
t2.stop();
|
2017-07-14 08:14:58 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
ScopedTimer t3(globalsLayoutTimer);
|
2017-08-12 03:00:03 +08:00
|
|
|
// Compute the public and global symbols.
|
2019-07-11 13:40:30 +08:00
|
|
|
auto &gsiBuilder = builder.getGsiBuilder();
|
|
|
|
std::vector<PublicSym32> publics;
|
|
|
|
symtab->forEachSymbol([&publics](Symbol *s) {
|
2017-07-28 02:25:59 +08:00
|
|
|
// Only emit defined, live symbols that have a chunk.
|
2019-07-11 13:40:30 +08:00
|
|
|
auto *def = dyn_cast<Defined>(s);
|
|
|
|
if (def && def->isLive() && def->getChunk())
|
|
|
|
publics.push_back(createPublic(def));
|
2017-07-28 02:25:59 +08:00
|
|
|
});
|
2017-07-14 08:14:58 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (!publics.empty()) {
|
|
|
|
publicSymbols = publics.size();
|
2017-07-28 02:25:59 +08:00
|
|
|
// Sort the public symbols and add them to the stream.
|
2019-07-11 13:40:30 +08:00
|
|
|
parallelSort(publics, [](const PublicSym32 &l, const PublicSym32 &r) {
|
|
|
|
return l.Name < r.Name;
|
2019-02-28 04:53:50 +08:00
|
|
|
});
|
2019-07-11 13:40:30 +08:00
|
|
|
for (const PublicSym32 &pub : publics)
|
|
|
|
gsiBuilder.addPublicSymbol(pub);
|
2017-07-28 02:25:59 +08:00
|
|
|
}
|
2017-01-12 11:09:25 +08:00
|
|
|
}
|
|
|
|
|
2019-03-15 02:45:08 +08:00
|
|
|
void PDBLinker::printStats() {
|
2019-07-11 13:40:30 +08:00
|
|
|
if (!config->showSummary)
|
2019-03-15 02:45:08 +08:00
|
|
|
return;
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
SmallString<256> buffer;
|
|
|
|
raw_svector_ostream stream(buffer);
|
2019-03-15 02:45:08 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
stream << center_justify("Summary", 80) << '\n'
|
2019-03-15 02:45:08 +08:00
|
|
|
<< std::string(80, '-') << '\n';
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
auto print = [&](uint64_t v, StringRef s) {
|
|
|
|
stream << format_decimal(v, 15) << " " << s << '\n';
|
2019-03-15 02:45:08 +08:00
|
|
|
};
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
print(ObjFile::instances.size(),
|
2019-03-15 02:45:08 +08:00
|
|
|
"Input OBJ files (expanded from all cmd-line inputs)");
|
2019-07-11 13:40:30 +08:00
|
|
|
print(typeServerIndexMappings.size(), "PDB type server dependencies");
|
|
|
|
print(precompTypeIndexMappings.size(), "Precomp OBJ dependencies");
|
|
|
|
print(tMerger.getTypeTable().size() + tMerger.getIDTable().size(),
|
2019-04-03 04:43:19 +08:00
|
|
|
"Merged TPI records");
|
2019-07-11 13:40:30 +08:00
|
|
|
print(pdbStrTab.size(), "Output PDB strings");
|
|
|
|
print(globalSymbols, "Global symbol records");
|
|
|
|
print(moduleSymbols, "Module symbol records");
|
|
|
|
print(publicSymbols, "Public symbol records");
|
2019-03-15 02:45:08 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
message(buffer);
|
2019-03-15 02:45:08 +08:00
|
|
|
}
|
|
|
|
|
2018-03-24 03:57:25 +08:00
|
|
|
void PDBLinker::addNatvisFiles() {
|
2019-07-11 13:40:30 +08:00
|
|
|
for (StringRef file : config->natvisFiles) {
|
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> dataOrErr =
|
|
|
|
MemoryBuffer::getFile(file);
|
|
|
|
if (!dataOrErr) {
|
|
|
|
warn("Cannot open input file: " + file);
|
2018-03-24 03:57:25 +08:00
|
|
|
continue;
|
|
|
|
}
|
2019-07-11 13:40:30 +08:00
|
|
|
builder.addInjectedSource(file, std::move(*dataOrErr));
|
2018-03-24 03:57:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
static codeview::CPUType toCodeViewMachine(COFF::MachineTypes machine) {
|
|
|
|
switch (machine) {
|
2018-04-17 02:17:13 +08:00
|
|
|
case COFF::IMAGE_FILE_MACHINE_AMD64:
|
|
|
|
return codeview::CPUType::X64;
|
|
|
|
case COFF::IMAGE_FILE_MACHINE_ARM:
|
|
|
|
return codeview::CPUType::ARM7;
|
|
|
|
case COFF::IMAGE_FILE_MACHINE_ARM64:
|
|
|
|
return codeview::CPUType::ARM64;
|
|
|
|
case COFF::IMAGE_FILE_MACHINE_ARMNT:
|
|
|
|
return codeview::CPUType::ARMNT;
|
|
|
|
case COFF::IMAGE_FILE_MACHINE_I386:
|
|
|
|
return codeview::CPUType::Intel80386;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unsupported CPU Type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-01 00:36:40 +08:00
|
|
|
// Mimic MSVC which surrounds arguments containing whitespace with quotes.
|
|
|
|
// Double double-quotes are handled, so that the resulting string can be
|
|
|
|
// executed again on the cmd-line.
|
2019-07-11 13:40:30 +08:00
|
|
|
static std::string quote(ArrayRef<StringRef> args) {
|
|
|
|
std::string r;
|
|
|
|
r.reserve(256);
|
|
|
|
for (StringRef a : args) {
|
|
|
|
if (!r.empty())
|
|
|
|
r.push_back(' ');
|
|
|
|
bool hasWS = a.find(' ') != StringRef::npos;
|
|
|
|
bool hasQ = a.find('"') != StringRef::npos;
|
|
|
|
if (hasWS || hasQ)
|
|
|
|
r.push_back('"');
|
|
|
|
if (hasQ) {
|
|
|
|
SmallVector<StringRef, 4> s;
|
|
|
|
a.split(s, '"');
|
|
|
|
r.append(join(s, "\"\""));
|
2018-12-01 00:36:40 +08:00
|
|
|
} else {
|
2019-07-11 13:40:30 +08:00
|
|
|
r.append(a);
|
2018-12-01 00:36:40 +08:00
|
|
|
}
|
2019-07-11 13:40:30 +08:00
|
|
|
if (hasWS || hasQ)
|
|
|
|
r.push_back('"');
|
2018-12-01 00:36:40 +08:00
|
|
|
}
|
2019-07-11 13:40:30 +08:00
|
|
|
return r;
|
2018-12-01 00:36:40 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
static void fillLinkerVerRecord(Compile3Sym &cs) {
|
|
|
|
cs.Machine = toCodeViewMachine(config->machine);
|
2017-08-12 05:14:01 +08:00
|
|
|
// Interestingly, if we set the string to 0.0.0.0, then when trying to view
|
|
|
|
// local variables WinDbg emits an error that private symbols are not present.
|
|
|
|
// By setting this to a valid MSVC linker version string, local variables are
|
|
|
|
// displayed properly. As such, even though it is not representative of
|
2017-08-12 04:46:47 +08:00
|
|
|
// LLVM's version information, we need this for compatibility.
|
2019-07-11 13:40:30 +08:00
|
|
|
cs.Flags = CompileSym3Flags::None;
|
|
|
|
cs.VersionBackendBuild = 25019;
|
|
|
|
cs.VersionBackendMajor = 14;
|
|
|
|
cs.VersionBackendMinor = 10;
|
|
|
|
cs.VersionBackendQFE = 0;
|
2017-08-12 04:46:47 +08:00
|
|
|
|
|
|
|
// MSVC also sets the frontend to 0.0.0.0 since this is specifically for the
|
|
|
|
// linker module (which is by definition a backend), so we don't need to do
|
|
|
|
// anything here. Also, it seems we can use "LLVM Linker" for the linker name
|
|
|
|
// without any problems. Only the backend version has to be hardcoded to a
|
|
|
|
// magic number.
|
2019-07-11 13:40:30 +08:00
|
|
|
cs.VersionFrontendBuild = 0;
|
|
|
|
cs.VersionFrontendMajor = 0;
|
|
|
|
cs.VersionFrontendMinor = 0;
|
|
|
|
cs.VersionFrontendQFE = 0;
|
|
|
|
cs.Version = "LLVM Linker";
|
|
|
|
cs.setLanguage(SourceLanguage::Link);
|
2019-03-30 04:25:34 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
static void addCommonLinkerModuleSymbols(StringRef path,
|
|
|
|
pdb::DbiModuleDescriptorBuilder &mod,
|
|
|
|
BumpPtrAllocator &allocator) {
|
|
|
|
ObjNameSym ons(SymbolRecordKind::ObjNameSym);
|
|
|
|
EnvBlockSym ebs(SymbolRecordKind::EnvBlockSym);
|
|
|
|
Compile3Sym cs(SymbolRecordKind::Compile3Sym);
|
|
|
|
fillLinkerVerRecord(cs);
|
2019-03-30 04:25:34 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
ons.Name = "* Linker *";
|
|
|
|
ons.Signature = 0;
|
2017-07-11 05:01:37 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
ArrayRef<StringRef> args = makeArrayRef(config->argv).drop_front();
|
|
|
|
std::string argStr = quote(args);
|
|
|
|
ebs.Fields.push_back("cwd");
|
2017-07-11 05:01:37 +08:00
|
|
|
SmallString<64> cwd;
|
2019-07-11 13:40:30 +08:00
|
|
|
if (config->pdbSourcePath.empty())
|
lld-link: Use /pdbsourcepath: for more places when present.
/pdbsourcepath: was added in https://reviews.llvm.org/D48882 to make it
possible to have relative paths in the debug info that clang-cl writes.
lld-link then makes the paths absolute at link time, which debuggers require.
This way, clang-cl's output is independent of the absolute path of the build
directory, which is useful for cacheability in distcc-like systems.
This patch extends /pdbsourcepath: (if passed) to also be used for:
1. The "cwd" stored in the env block in the pdb is /pdbsourcepath: if present
2. The "exe" stored in the env block in the pdb is made absolute relative
to /pdbsourcepath: instead of the cwd
3. The "pdb" stored in the env block in the pdb is made absolute relative
to /pdbsourcepath: instead of the cwd
4. For making absolute paths to .obj files referenced from the pdb
/pdbsourcepath: is now useful in three scenarios (the first one already working
before this change):
1. When building with full debug info, passing the real build dir to
/pdbsourcepath: allows having clang-cl's output to be independent
of the build directory path. This patch effectively doesn't change
behavior for this use case (assuming the cwd is the build dir).
2. When building without compile-time debug info but linking with /debug,
a fake fixed /pdbsourcepath: can be passed to get symbolized stacks
while making the pdb and exe independent of the current build dir.
For this two work, lld-link needs to be invoked with relative paths for
the lld-link invocation itself (for "exe"), for the pdb output name, the exe
output name (for "pdb"), and the obj input files, and no absolute path
must appear on the link command (for "cmd" in the pdb's env block).
Since no full debug info is present, it doesn't matter that the absolute
path doesn't exist on disk -- we only get symbols in stacks.
3. When building production builds with full debug info that don't have
local changes, and that get source indexed and their pdbs get uploaded
to a symbol server. /pdbsourcepath: again makes the build output independent
of the current directory, and the fixed path passed to /pdbsourcepath: can
be given the source indexing transform so that it gets mapped to a
repository path. This has the same requirements as 2.
This patch also makes it possible to create PDB files containing Windows-style
absolute paths when cross-compiling on a POSIX system.
Differential Revision: https://reviews.llvm.org/D53021
llvm-svn: 344061
2018-10-10 01:52:25 +08:00
|
|
|
sys::fs::current_path(cwd);
|
|
|
|
else
|
2019-07-11 13:40:30 +08:00
|
|
|
cwd = config->pdbSourcePath;
|
|
|
|
ebs.Fields.push_back(cwd);
|
|
|
|
ebs.Fields.push_back("exe");
|
|
|
|
SmallString<64> exe = config->argv[0];
|
lld-link: Use /pdbsourcepath: for more places when present.
/pdbsourcepath: was added in https://reviews.llvm.org/D48882 to make it
possible to have relative paths in the debug info that clang-cl writes.
lld-link then makes the paths absolute at link time, which debuggers require.
This way, clang-cl's output is independent of the absolute path of the build
directory, which is useful for cacheability in distcc-like systems.
This patch extends /pdbsourcepath: (if passed) to also be used for:
1. The "cwd" stored in the env block in the pdb is /pdbsourcepath: if present
2. The "exe" stored in the env block in the pdb is made absolute relative
to /pdbsourcepath: instead of the cwd
3. The "pdb" stored in the env block in the pdb is made absolute relative
to /pdbsourcepath: instead of the cwd
4. For making absolute paths to .obj files referenced from the pdb
/pdbsourcepath: is now useful in three scenarios (the first one already working
before this change):
1. When building with full debug info, passing the real build dir to
/pdbsourcepath: allows having clang-cl's output to be independent
of the build directory path. This patch effectively doesn't change
behavior for this use case (assuming the cwd is the build dir).
2. When building without compile-time debug info but linking with /debug,
a fake fixed /pdbsourcepath: can be passed to get symbolized stacks
while making the pdb and exe independent of the current build dir.
For this two work, lld-link needs to be invoked with relative paths for
the lld-link invocation itself (for "exe"), for the pdb output name, the exe
output name (for "pdb"), and the obj input files, and no absolute path
must appear on the link command (for "cmd" in the pdb's env block).
Since no full debug info is present, it doesn't matter that the absolute
path doesn't exist on disk -- we only get symbols in stacks.
3. When building production builds with full debug info that don't have
local changes, and that get source indexed and their pdbs get uploaded
to a symbol server. /pdbsourcepath: again makes the build output independent
of the current directory, and the fixed path passed to /pdbsourcepath: can
be given the source indexing transform so that it gets mapped to a
repository path. This has the same requirements as 2.
This patch also makes it possible to create PDB files containing Windows-style
absolute paths when cross-compiling on a POSIX system.
Differential Revision: https://reviews.llvm.org/D53021
llvm-svn: 344061
2018-10-10 01:52:25 +08:00
|
|
|
pdbMakeAbsolute(exe);
|
2019-07-11 13:40:30 +08:00
|
|
|
ebs.Fields.push_back(exe);
|
|
|
|
ebs.Fields.push_back("pdb");
|
|
|
|
ebs.Fields.push_back(path);
|
|
|
|
ebs.Fields.push_back("cmd");
|
|
|
|
ebs.Fields.push_back(argStr);
|
|
|
|
mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
|
|
|
|
ons, allocator, CodeViewContainer::Pdb));
|
|
|
|
mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
|
|
|
|
cs, allocator, CodeViewContainer::Pdb));
|
|
|
|
mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
|
|
|
|
ebs, allocator, CodeViewContainer::Pdb));
|
2017-07-11 05:01:37 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
static void addLinkerModuleCoffGroup(PartialSection *sec,
|
|
|
|
pdb::DbiModuleDescriptorBuilder &mod,
|
|
|
|
OutputSection &os,
|
|
|
|
BumpPtrAllocator &allocator) {
|
2019-03-30 04:25:34 +08:00
|
|
|
// If there's a section, there's at least one chunk
|
2019-07-11 13:40:30 +08:00
|
|
|
assert(!sec->chunks.empty());
|
|
|
|
const Chunk *firstChunk = *sec->chunks.begin();
|
|
|
|
const Chunk *lastChunk = *sec->chunks.rbegin();
|
2019-03-30 04:25:34 +08:00
|
|
|
|
|
|
|
// Emit COFF group
|
2019-07-11 13:40:30 +08:00
|
|
|
CoffGroupSym cgs(SymbolRecordKind::CoffGroupSym);
|
|
|
|
cgs.Name = sec->name;
|
|
|
|
cgs.Segment = os.sectionIndex;
|
|
|
|
cgs.Offset = firstChunk->getRVA() - os.getRVA();
|
|
|
|
cgs.Size = lastChunk->getRVA() + lastChunk->getSize() - firstChunk->getRVA();
|
|
|
|
cgs.Characteristics = sec->characteristics;
|
2019-03-30 04:25:34 +08:00
|
|
|
|
|
|
|
// Somehow .idata sections & sections groups in the debug symbol stream have
|
|
|
|
// the "write" flag set. However the section header for the corresponding
|
|
|
|
// .idata section doesn't have it.
|
2019-07-11 13:40:30 +08:00
|
|
|
if (cgs.Name.startswith(".idata"))
|
|
|
|
cgs.Characteristics |= llvm::COFF::IMAGE_SCN_MEM_WRITE;
|
2019-03-30 04:25:34 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
|
|
|
|
cgs, allocator, CodeViewContainer::Pdb));
|
2019-03-30 04:25:34 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
static void addLinkerModuleSectionSymbol(pdb::DbiModuleDescriptorBuilder &mod,
|
|
|
|
OutputSection &os,
|
|
|
|
BumpPtrAllocator &allocator) {
|
|
|
|
SectionSym sym(SymbolRecordKind::SectionSym);
|
|
|
|
sym.Alignment = 12; // 2^12 = 4KB
|
|
|
|
sym.Characteristics = os.header.Characteristics;
|
|
|
|
sym.Length = os.getVirtualSize();
|
|
|
|
sym.Name = os.name;
|
|
|
|
sym.Rva = os.getRVA();
|
|
|
|
sym.SectionNumber = os.sectionIndex;
|
|
|
|
mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
|
|
|
|
sym, allocator, CodeViewContainer::Pdb));
|
2019-03-30 04:25:34 +08:00
|
|
|
|
|
|
|
// Skip COFF groups in MinGW because it adds a significant footprint to the
|
|
|
|
// PDB, due to each function being in its own section
|
2019-07-11 13:40:30 +08:00
|
|
|
if (config->mingw)
|
2019-03-30 04:25:34 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Output COFF groups for individual chunks of this section.
|
2019-07-11 13:40:30 +08:00
|
|
|
for (PartialSection *sec : os.contribSections) {
|
|
|
|
addLinkerModuleCoffGroup(sec, mod, os, allocator);
|
2019-03-30 04:25:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add all import files as modules to the PDB.
|
2019-07-11 13:40:30 +08:00
|
|
|
void PDBLinker::addImportFilesToPDB(ArrayRef<OutputSection *> outputSections) {
|
|
|
|
if (ImportFile::instances.empty())
|
2019-03-30 04:25:34 +08:00
|
|
|
return;
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
std::map<std::string, llvm::pdb::DbiModuleDescriptorBuilder *> dllToModuleDbi;
|
2019-03-30 04:25:34 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
for (ImportFile *file : ImportFile::instances) {
|
|
|
|
if (!file->live)
|
2019-03-30 04:25:34 +08:00
|
|
|
continue;
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (!file->thunkSym)
|
2019-03-30 04:25:34 +08:00
|
|
|
continue;
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (!file->thunkLive)
|
2019-03-30 05:24:19 +08:00
|
|
|
continue;
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
std::string dll = StringRef(file->dllName).lower();
|
|
|
|
llvm::pdb::DbiModuleDescriptorBuilder *&mod = dllToModuleDbi[dll];
|
|
|
|
if (!mod) {
|
|
|
|
pdb::DbiStreamBuilder &dbiBuilder = builder.getDbiBuilder();
|
|
|
|
SmallString<128> libPath = file->parentName;
|
|
|
|
pdbMakeAbsolute(libPath);
|
|
|
|
sys::path::native(libPath);
|
2019-03-30 04:25:34 +08:00
|
|
|
|
|
|
|
// Name modules similar to MSVC's link.exe.
|
|
|
|
// The first module is the simple dll filename
|
2019-07-11 13:40:30 +08:00
|
|
|
llvm::pdb::DbiModuleDescriptorBuilder &firstMod =
|
|
|
|
exitOnErr(dbiBuilder.addModuleInfo(file->dllName));
|
|
|
|
firstMod.setObjFileName(libPath);
|
|
|
|
pdb::SectionContrib sc =
|
2019-03-30 04:25:34 +08:00
|
|
|
createSectionContrib(nullptr, llvm::pdb::kInvalidStreamIndex);
|
2019-07-11 13:40:30 +08:00
|
|
|
firstMod.setFirstSectionContrib(sc);
|
2019-03-30 04:25:34 +08:00
|
|
|
|
|
|
|
// The second module is where the import stream goes.
|
2019-07-11 13:40:30 +08:00
|
|
|
mod = &exitOnErr(dbiBuilder.addModuleInfo("Import:" + file->dllName));
|
|
|
|
mod->setObjFileName(libPath);
|
2019-03-30 04:25:34 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
DefinedImportThunk *thunk = cast<DefinedImportThunk>(file->thunkSym);
|
|
|
|
Chunk *thunkChunk = thunk->getChunk();
|
|
|
|
OutputSection *thunkOS = thunkChunk->getOutputSection();
|
2019-03-30 04:25:34 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
ObjNameSym ons(SymbolRecordKind::ObjNameSym);
|
|
|
|
Compile3Sym cs(SymbolRecordKind::Compile3Sym);
|
|
|
|
Thunk32Sym ts(SymbolRecordKind::Thunk32Sym);
|
|
|
|
ScopeEndSym es(SymbolRecordKind::ScopeEndSym);
|
2019-03-30 04:25:34 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
ons.Name = file->dllName;
|
|
|
|
ons.Signature = 0;
|
2019-03-30 04:25:34 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
fillLinkerVerRecord(cs);
|
2019-03-30 04:25:34 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
ts.Name = thunk->getName();
|
|
|
|
ts.Parent = 0;
|
|
|
|
ts.End = 0;
|
|
|
|
ts.Next = 0;
|
|
|
|
ts.Thunk = ThunkOrdinal::Standard;
|
|
|
|
ts.Length = thunkChunk->getSize();
|
|
|
|
ts.Segment = thunkOS->sectionIndex;
|
|
|
|
ts.Offset = thunkChunk->getRVA() - thunkOS->getRVA();
|
2019-03-30 04:25:34 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
mod->addSymbol(codeview::SymbolSerializer::writeOneSymbol(
|
|
|
|
ons, alloc, CodeViewContainer::Pdb));
|
|
|
|
mod->addSymbol(codeview::SymbolSerializer::writeOneSymbol(
|
|
|
|
cs, alloc, CodeViewContainer::Pdb));
|
2019-03-30 04:25:34 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
SmallVector<SymbolScope, 4> scopes;
|
|
|
|
CVSymbol newSym = codeview::SymbolSerializer::writeOneSymbol(
|
|
|
|
ts, alloc, CodeViewContainer::Pdb);
|
|
|
|
scopeStackOpen(scopes, mod->getNextSymbolOffset(), newSym);
|
2019-03-30 04:25:34 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
mod->addSymbol(newSym);
|
2019-03-30 04:25:34 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
newSym = codeview::SymbolSerializer::writeOneSymbol(es, alloc,
|
2019-03-30 04:25:34 +08:00
|
|
|
CodeViewContainer::Pdb);
|
2019-07-11 13:40:30 +08:00
|
|
|
scopeStackClose(scopes, mod->getNextSymbolOffset(), file);
|
2019-03-30 04:25:34 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
mod->addSymbol(newSym);
|
2019-03-30 04:25:34 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
pdb::SectionContrib sc =
|
|
|
|
createSectionContrib(thunk->getChunk(), mod->getModuleIndex());
|
|
|
|
mod->setFirstSectionContrib(sc);
|
2019-03-30 04:25:34 +08:00
|
|
|
}
|
2017-08-12 04:46:28 +08:00
|
|
|
}
|
|
|
|
|
2016-11-12 08:00:51 +08:00
|
|
|
// Creates a PDB file.
|
2019-07-11 13:40:30 +08:00
|
|
|
void coff::createPDB(SymbolTable *symtab,
|
|
|
|
ArrayRef<OutputSection *> outputSections,
|
|
|
|
ArrayRef<uint8_t> sectionTable,
|
|
|
|
llvm::codeview::DebugInfo *buildId) {
|
|
|
|
ScopedTimer t1(totalPdbLinkTimer);
|
|
|
|
PDBLinker pdb(symtab);
|
|
|
|
|
|
|
|
pdb.initialize(buildId);
|
|
|
|
pdb.addObjectsToPDB();
|
|
|
|
pdb.addImportFilesToPDB(outputSections);
|
|
|
|
pdb.addSections(outputSections, sectionTable);
|
|
|
|
pdb.addNatvisFiles();
|
|
|
|
|
|
|
|
ScopedTimer t2(diskCommitTimer);
|
|
|
|
codeview::GUID guid;
|
|
|
|
pdb.commit(&guid);
|
|
|
|
memcpy(&buildId->PDB70.Signature, &guid, 16);
|
|
|
|
|
|
|
|
t2.stop();
|
|
|
|
t1.stop();
|
|
|
|
pdb.printStats();
|
2017-07-14 08:14:58 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
void PDBLinker::initialize(llvm::codeview::DebugInfo *buildId) {
|
|
|
|
exitOnErr(builder.initialize(4096)); // 4096 is blocksize
|
2016-09-16 02:55:18 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
buildId->Signature.CVSignature = OMF::Signature::PDB70;
|
2018-09-16 02:37:22 +08:00
|
|
|
// Signature is set to a hash of the PDB contents when the PDB is done.
|
2019-07-11 13:40:30 +08:00
|
|
|
memset(buildId->PDB70.Signature, 0, 16);
|
|
|
|
buildId->PDB70.Age = 1;
|
2018-09-16 02:37:22 +08:00
|
|
|
|
2016-10-06 06:08:58 +08:00
|
|
|
// Create streams in MSF for predefined streams, namely
|
|
|
|
// PDB, TPI, DBI and IPI.
|
2019-07-11 13:40:30 +08:00
|
|
|
for (int i = 0; i < (int)pdb::kSpecialStreamCount; ++i)
|
|
|
|
exitOnErr(builder.getMsfBuilder().addStream(0));
|
2016-09-16 02:55:18 +08:00
|
|
|
|
2016-09-17 06:51:17 +08:00
|
|
|
// Add an Info stream.
|
2019-07-11 13:40:30 +08:00
|
|
|
auto &infoBuilder = builder.getInfoBuilder();
|
|
|
|
infoBuilder.setVersion(pdb::PdbRaw_ImplVer::PdbImplVC70);
|
|
|
|
infoBuilder.setHashPDBContentsToGUID(true);
|
2016-09-16 02:55:18 +08:00
|
|
|
|
2017-07-07 13:04:36 +08:00
|
|
|
// Add an empty DBI stream.
|
2019-07-11 13:40:30 +08:00
|
|
|
pdb::DbiStreamBuilder &dbiBuilder = builder.getDbiBuilder();
|
|
|
|
dbiBuilder.setAge(buildId->PDB70.Age);
|
|
|
|
dbiBuilder.setVersionHeader(pdb::PdbDbiV70);
|
|
|
|
dbiBuilder.setMachineType(config->machine);
|
2018-04-17 02:17:13 +08:00
|
|
|
// Technically we are not link.exe 14.11, but there are known cases where
|
|
|
|
// debugging tools on Windows expect Microsoft-specific version numbers or
|
|
|
|
// they fail to work at all. Since we know we produce PDBs that are
|
|
|
|
// compatible with LINK 14.11, we set that version number here.
|
2019-07-11 13:40:30 +08:00
|
|
|
dbiBuilder.setBuildNumber(14, 11);
|
2017-07-14 08:14:58 +08:00
|
|
|
}
|
2016-10-07 06:52:01 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
void PDBLinker::addSections(ArrayRef<OutputSection *> outputSections,
|
|
|
|
ArrayRef<uint8_t> sectionTable) {
|
2017-07-14 08:14:58 +08:00
|
|
|
// It's not entirely clear what this is, but the * Linker * module uses it.
|
2019-07-11 13:40:30 +08:00
|
|
|
pdb::DbiStreamBuilder &dbiBuilder = builder.getDbiBuilder();
|
|
|
|
nativePath = config->pdbPath;
|
|
|
|
pdbMakeAbsolute(nativePath);
|
|
|
|
uint32_t pdbFilePathNI = dbiBuilder.addECName(nativePath);
|
|
|
|
auto &linkerModule = exitOnErr(dbiBuilder.addModuleInfo("* Linker *"));
|
|
|
|
linkerModule.setPdbFilePathNI(pdbFilePathNI);
|
|
|
|
addCommonLinkerModuleSymbols(nativePath, linkerModule, alloc);
|
2016-11-16 09:10:46 +08:00
|
|
|
|
2017-08-04 05:15:09 +08:00
|
|
|
// Add section contributions. They must be ordered by ascending RVA.
|
2019-07-11 13:40:30 +08:00
|
|
|
for (OutputSection *os : outputSections) {
|
|
|
|
addLinkerModuleSectionSymbol(linkerModule, *os, alloc);
|
|
|
|
for (Chunk *c : os->chunks) {
|
|
|
|
pdb::SectionContrib sc =
|
|
|
|
createSectionContrib(c, linkerModule.getModuleIndex());
|
|
|
|
builder.getDbiBuilder().addSectionContrib(sc);
|
2018-04-21 02:00:46 +08:00
|
|
|
}
|
2017-08-12 04:46:28 +08:00
|
|
|
}
|
2017-08-04 05:15:09 +08:00
|
|
|
|
2019-03-19 03:13:23 +08:00
|
|
|
// The * Linker * first section contrib is only used along with /INCREMENTAL,
|
|
|
|
// to provide trampolines thunks for incremental function patching. Set this
|
|
|
|
// as "unused" because LLD doesn't support /INCREMENTAL link.
|
2019-07-11 13:40:30 +08:00
|
|
|
pdb::SectionContrib sc =
|
2019-03-19 03:13:23 +08:00
|
|
|
createSectionContrib(nullptr, llvm::pdb::kInvalidStreamIndex);
|
2019-07-11 13:40:30 +08:00
|
|
|
linkerModule.setFirstSectionContrib(sc);
|
2019-03-19 03:13:23 +08:00
|
|
|
|
2017-08-04 05:15:09 +08:00
|
|
|
// Add Section Map stream.
|
2019-07-11 13:40:30 +08:00
|
|
|
ArrayRef<object::coff_section> sections = {
|
|
|
|
(const object::coff_section *)sectionTable.data(),
|
|
|
|
sectionTable.size() / sizeof(object::coff_section)};
|
|
|
|
sectionMap = pdb::DbiStreamBuilder::createSectionMap(sections);
|
|
|
|
dbiBuilder.setSectionMap(sectionMap);
|
2017-08-04 05:15:09 +08:00
|
|
|
|
2016-10-12 03:45:07 +08:00
|
|
|
// Add COFF section header stream.
|
2019-07-11 13:40:30 +08:00
|
|
|
exitOnErr(
|
|
|
|
dbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, sectionTable));
|
2017-07-14 08:14:58 +08:00
|
|
|
}
|
2016-10-12 03:45:07 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
void PDBLinker::commit(codeview::GUID *guid) {
|
2016-09-27 07:53:55 +08:00
|
|
|
// Write to a file.
|
2019-07-11 13:40:30 +08:00
|
|
|
exitOnErr(builder.commit(config->pdbPath, guid));
|
2015-12-05 07:11:05 +08:00
|
|
|
}
|
2018-04-18 07:32:33 +08:00
|
|
|
|
|
|
|
static uint32_t getSecrelReloc() {
|
2019-07-11 13:40:30 +08:00
|
|
|
switch (config->machine) {
|
2018-04-18 07:32:33 +08:00
|
|
|
case AMD64:
|
|
|
|
return COFF::IMAGE_REL_AMD64_SECREL;
|
|
|
|
case I386:
|
|
|
|
return COFF::IMAGE_REL_I386_SECREL;
|
|
|
|
case ARMNT:
|
|
|
|
return COFF::IMAGE_REL_ARM_SECREL;
|
|
|
|
case ARM64:
|
|
|
|
return COFF::IMAGE_REL_ARM64_SECREL;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unknown machine type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to find a line table for the given offset Addr into the given chunk C.
|
|
|
|
// If a line table was found, the line table, the string and checksum tables
|
|
|
|
// that are used to interpret the line table, and the offset of Addr in the line
|
|
|
|
// table are stored in the output arguments. Returns whether a line table was
|
|
|
|
// found.
|
2019-07-11 13:40:30 +08:00
|
|
|
static bool findLineTable(const SectionChunk *c, uint32_t addr,
|
|
|
|
DebugStringTableSubsectionRef &cVStrTab,
|
|
|
|
DebugChecksumsSubsectionRef &checksums,
|
|
|
|
DebugLinesSubsectionRef &lines,
|
|
|
|
uint32_t &offsetInLinetable) {
|
|
|
|
ExitOnError exitOnErr;
|
|
|
|
uint32_t secrelReloc = getSecrelReloc();
|
|
|
|
|
|
|
|
for (SectionChunk *dbgC : c->file->getDebugChunks()) {
|
|
|
|
if (dbgC->getSectionName() != ".debug$S")
|
2018-04-18 07:32:33 +08:00
|
|
|
continue;
|
|
|
|
|
2019-07-16 16:26:38 +08:00
|
|
|
// Build a mapping of SECREL relocations in dbgC that refer to `c`.
|
2019-07-11 13:40:30 +08:00
|
|
|
DenseMap<uint32_t, uint32_t> secrels;
|
|
|
|
for (const coff_relocation &r : dbgC->getRelocs()) {
|
|
|
|
if (r.Type != secrelReloc)
|
2018-04-18 07:32:33 +08:00
|
|
|
continue;
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (auto *s = dyn_cast_or_null<DefinedRegular>(
|
|
|
|
c->file->getSymbols()[r.SymbolTableIndex]))
|
|
|
|
if (s->getChunk() == c)
|
|
|
|
secrels[r.VirtualAddress] = s->getValue();
|
2018-04-18 07:32:33 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
ArrayRef<uint8_t> contents =
|
|
|
|
SectionChunk::consumeDebugMagic(dbgC->getContents(), ".debug$S");
|
|
|
|
DebugSubsectionArray subsections;
|
|
|
|
BinaryStreamReader reader(contents, support::little);
|
|
|
|
exitOnErr(reader.readArray(subsections, contents.size()));
|
2018-04-18 07:32:33 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
for (const DebugSubsectionRecord &ss : subsections) {
|
|
|
|
switch (ss.kind()) {
|
2018-04-18 07:32:33 +08:00
|
|
|
case DebugSubsectionKind::StringTable: {
|
2019-07-11 13:40:30 +08:00
|
|
|
assert(!cVStrTab.valid() &&
|
2018-04-18 07:32:33 +08:00
|
|
|
"Encountered multiple string table subsections!");
|
2019-07-11 13:40:30 +08:00
|
|
|
exitOnErr(cVStrTab.initialize(ss.getRecordData()));
|
2018-04-18 07:32:33 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DebugSubsectionKind::FileChecksums:
|
2019-07-11 13:40:30 +08:00
|
|
|
assert(!checksums.valid() &&
|
2018-04-18 07:32:33 +08:00
|
|
|
"Encountered multiple checksum subsections!");
|
2019-07-11 13:40:30 +08:00
|
|
|
exitOnErr(checksums.initialize(ss.getRecordData()));
|
2018-04-18 07:32:33 +08:00
|
|
|
break;
|
|
|
|
case DebugSubsectionKind::Lines: {
|
2019-07-11 13:40:30 +08:00
|
|
|
ArrayRef<uint8_t> bytes;
|
|
|
|
auto ref = ss.getRecordData();
|
|
|
|
exitOnErr(ref.readLongestContiguousChunk(0, bytes));
|
|
|
|
size_t offsetInDbgC = bytes.data() - dbgC->getContents().data();
|
2018-04-18 07:32:33 +08:00
|
|
|
|
|
|
|
// Check whether this line table refers to C.
|
2019-07-11 13:40:30 +08:00
|
|
|
auto i = secrels.find(offsetInDbgC);
|
|
|
|
if (i == secrels.end())
|
2018-04-18 07:32:33 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
// Check whether this line table covers Addr in C.
|
2019-07-11 13:40:30 +08:00
|
|
|
DebugLinesSubsectionRef linesTmp;
|
|
|
|
exitOnErr(linesTmp.initialize(BinaryStreamReader(ref)));
|
|
|
|
uint32_t offsetInC = i->second + linesTmp.header()->RelocOffset;
|
|
|
|
if (addr < offsetInC || addr >= offsetInC + linesTmp.header()->CodeSize)
|
2018-04-18 07:32:33 +08:00
|
|
|
break;
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
assert(!lines.header() &&
|
2018-04-18 07:32:33 +08:00
|
|
|
"Encountered multiple line tables for function!");
|
2019-07-11 13:40:30 +08:00
|
|
|
exitOnErr(lines.initialize(BinaryStreamReader(ref)));
|
|
|
|
offsetInLinetable = addr - offsetInC;
|
2018-04-18 07:32:33 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (cVStrTab.valid() && checksums.valid() && lines.header())
|
2018-04-18 07:32:33 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use CodeView line tables to resolve a file and line number for the given
|
|
|
|
// offset into the given chunk and return them, or {"", 0} if a line table was
|
|
|
|
// not found.
|
2019-07-11 13:40:30 +08:00
|
|
|
std::pair<StringRef, uint32_t> coff::getFileLine(const SectionChunk *c,
|
|
|
|
uint32_t addr) {
|
|
|
|
ExitOnError exitOnErr;
|
2018-04-18 07:32:33 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
DebugStringTableSubsectionRef cVStrTab;
|
|
|
|
DebugChecksumsSubsectionRef checksums;
|
|
|
|
DebugLinesSubsectionRef lines;
|
|
|
|
uint32_t offsetInLinetable;
|
2018-04-18 07:32:33 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (!findLineTable(c, addr, cVStrTab, checksums, lines, offsetInLinetable))
|
2018-04-18 07:32:33 +08:00
|
|
|
return {"", 0};
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
Optional<uint32_t> nameIndex;
|
|
|
|
Optional<uint32_t> lineNumber;
|
|
|
|
for (LineColumnEntry &entry : lines) {
|
|
|
|
for (const LineNumberEntry &ln : entry.LineNumbers) {
|
|
|
|
LineInfo li(ln.Flags);
|
|
|
|
if (ln.Offset > offsetInLinetable) {
|
|
|
|
if (!nameIndex) {
|
|
|
|
nameIndex = entry.NameIndex;
|
|
|
|
lineNumber = li.getStartLine();
|
2019-01-05 05:49:22 +08:00
|
|
|
}
|
2019-07-11 13:40:30 +08:00
|
|
|
StringRef filename =
|
|
|
|
exitOnErr(getFileName(cVStrTab, checksums, *nameIndex));
|
|
|
|
return {filename, *lineNumber};
|
2018-04-18 07:32:33 +08:00
|
|
|
}
|
2019-07-11 13:40:30 +08:00
|
|
|
nameIndex = entry.NameIndex;
|
|
|
|
lineNumber = li.getStartLine();
|
2018-04-18 07:32:33 +08:00
|
|
|
}
|
|
|
|
}
|
2019-07-11 13:40:30 +08:00
|
|
|
if (!nameIndex)
|
2019-01-05 05:49:22 +08:00
|
|
|
return {"", 0};
|
2019-07-11 13:40:30 +08:00
|
|
|
StringRef filename = exitOnErr(getFileName(cVStrTab, checksums, *nameIndex));
|
|
|
|
return {filename, *lineNumber};
|
2018-04-18 07:32:33 +08:00
|
|
|
}
|