llvm-project/llvm/tools/llvm-pdbutil
Zachary Turner cafd476836 Fix emission of PDB string table.
This was originally reported as a bug with the symptom being "cvdump
crashes when printing an LLD-linked PDB that has an S_FILESTATIC record
in it". After some additional investigation, I determined that this was
a symptom of a larger problem, and in fact the real problem was in the
way we emitted the global PDB string table. As evidence of this, you can
take any lld-generated PDB, run cvdump -stringtable on it, and it would
return no results.

My hypothesis was that cvdump could not *find* the string table to begin
with. Normally it would do this by looking in the "named stream map",
finding the string /names, and using its value as the stream index. If
this lookup fails, then cvdump would fail to load the string table.

To test this hypothesis, I looked at the name stream map generated by a
link.exe PDB, and I emitted exactly those bytes into an LLD-generated
PDB. Suddenly, cvdump could read our string table!

This code has always been hacky and we knew there was something we
didn't understand. After all, there were some comments to the effect of
"we have to emit strings in a specific order, otherwise things don't
work". The key to fixing this was finally understanding this.

The way it works is that it makes use of a generic serializable hash map
that maps integers to other integers. In this case, the "key" is the
offset into a buffer, and the value is the stream number. If you index
into the buffer at the offset specified by a given key, you find the
name. The underlying cause of all these problems is that we were using
the identity function for the hash. i.e. if a string's offset in the
buffer was 12, the hash value was 12. Instead, we need to hash the
string *at that offset*. There is an additional catch, in that we have
to compute the hash as a uint32 and then truncate it to uint16.

Making this work is a little bit annoying, because we use the same hash
table in other places as well, and normally just using the identity
function for the hash function is actually what's desired. I'm not
totally happy with the template goo I came up with, but it works in any
case.

The reason we never found this bug through our own testing is because we
were building a /parallel/ hash table (in the form of an
llvm::StringMap<>) and doing all of our lookups and "real" hash table
work against that. I deleted all of that code and now everything goes
through the real hash table. Then, to test it, I added a unit test which
adds 7 strings and queries the associated values. I test every possible
insertion order permutation of these 7 strings, to verify that it really
does work as expected.

Differential Revision: https://reviews.llvm.org/D43326

llvm-svn: 325386
2018-02-16 20:46:04 +00:00
..
Analyze.cpp Remove some dead code / includes. 2017-06-16 23:42:15 +00:00
Analyze.h
BytesOutputStyle.cpp [llvm-pdbutil] Add support for dumping detailed module stats. 2017-08-21 14:53:25 +00:00
BytesOutputStyle.h [llvm-pdbutil] Add support for dumping detailed module stats. 2017-08-21 14:53:25 +00:00
CMakeLists.txt [llvm-pdbutil] Support dumping CodeView from object files. 2017-09-01 20:06:56 +00:00
Diff.cpp Fix emission of PDB string table. 2018-02-16 20:46:04 +00:00
Diff.h
DiffPrinter.cpp Resubmit "Add pdb-diff test." 2017-07-10 19:16:49 +00:00
DiffPrinter.h Resubmit "Add pdb-diff test." 2017-07-10 19:16:49 +00:00
DumpOutputStyle.cpp [PDB] Correctly link S_FILESTATIC records. 2018-01-05 19:12:40 +00:00
DumpOutputStyle.h [PDB] Correctly link S_FILESTATIC records. 2018-01-05 19:12:40 +00:00
FormatUtil.cpp [llvm-pdbutil] Print detailed S_UDT stats. 2017-08-31 20:43:22 +00:00
FormatUtil.h [llvm-pdbutil] Print detailed S_UDT stats. 2017-08-31 20:43:22 +00:00
InputFile.cpp [llvm-pdbutil] Remove unused variables. 2017-09-02 00:09:43 +00:00
InputFile.h [llvm-pdbutil] Support dumping CodeView from object files. 2017-09-01 20:06:56 +00:00
LLVMBuild.txt
LinePrinter.cpp [pdbutil] Add a command to dump the FPM. 2017-08-02 22:25:52 +00:00
LinePrinter.h [llvm-pdbutil] Support dumping CodeView from object files. 2017-09-01 20:06:56 +00:00
MinimalSymbolDumper.cpp [PDB] Correctly link S_FILESTATIC records. 2018-01-05 19:12:40 +00:00
MinimalSymbolDumper.h [PDB] Correctly link S_FILESTATIC records. 2018-01-05 19:12:40 +00:00
MinimalTypeDumper.cpp [llvm-pdbutil] Print detailed S_UDT stats. 2017-08-31 20:43:22 +00:00
MinimalTypeDumper.h [PDB] Finish and simplify TPI hashing 2017-07-18 00:33:45 +00:00
OutputStyle.h
PdbYaml.cpp Remove redundant includes from tools. 2017-12-13 21:31:10 +00:00
PdbYaml.h [codeview] Fix YAML for LF_TYPESERVER2 by hoisting PDB_UniqueId 2017-07-17 23:59:44 +00:00
PrettyBuiltinDumper.cpp Fix MSVC "not all control paths return a value" warning. 2018-01-17 18:16:28 +00:00
PrettyBuiltinDumper.h
PrettyClassDefinitionDumper.cpp
PrettyClassDefinitionDumper.h
PrettyClassLayoutGraphicalDumper.cpp Fix a null pointer dereference in llvm-pdbutil pretty. 2017-06-12 20:46:35 +00:00
PrettyClassLayoutGraphicalDumper.h
PrettyCompilandDumper.cpp
PrettyCompilandDumper.h
PrettyEnumDumper.cpp Enable llvm-pdbutil to list enumerations using native PDB reader 2017-08-04 22:37:58 +00:00
PrettyEnumDumper.h
PrettyExternalSymbolDumper.cpp
PrettyExternalSymbolDumper.h
PrettyFunctionDumper.cpp Fix pretty printing the unspecified param of a variadic function 2018-01-17 01:22:03 +00:00
PrettyFunctionDumper.h
PrettyTypeDumper.cpp
PrettyTypeDumper.h
PrettyTypedefDumper.cpp Remove redundant includes from tools. 2017-12-13 21:31:10 +00:00
PrettyTypedefDumper.h
PrettyVariableDumper.cpp
PrettyVariableDumper.h
StreamUtil.cpp [llvm-pdbutil] Add support for dumping detailed module stats. 2017-08-21 14:53:25 +00:00
StreamUtil.h [llvm-pdbutil] Add support for dumping detailed module stats. 2017-08-21 14:53:25 +00:00
YAMLOutputStyle.cpp Remove redundant includes from tools. 2017-12-13 21:31:10 +00:00
YAMLOutputStyle.h
llvm-pdbutil.cpp Remove redundant includes from tools. 2017-12-13 21:31:10 +00:00
llvm-pdbutil.h [llvm-pdbutil] Support dumping CodeView from object files. 2017-09-01 20:06:56 +00:00