Rui Ueyama | 0fcd826 | 2016-05-20 19:55:17 +0000 | [diff] [blame] | 1 | //===- SymbolStream.cpp - PDB Symbol Stream Access ------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "llvm/DebugInfo/PDB/Raw/SymbolStream.h" |
| 11 | |
| 12 | #include "llvm/DebugInfo/CodeView/CodeView.h" |
| 13 | #include "llvm/DebugInfo/CodeView/TypeRecord.h" |
| 14 | #include "llvm/DebugInfo/PDB/Raw/MappedBlockStream.h" |
| 15 | #include "llvm/DebugInfo/PDB/Raw/RawConstants.h" |
| 16 | #include "llvm/DebugInfo/PDB/Raw/RawError.h" |
| 17 | #include "llvm/DebugInfo/PDB/Raw/StreamReader.h" |
| 18 | |
| 19 | #include "llvm/Support/Endian.h" |
| 20 | |
| 21 | using namespace llvm; |
| 22 | using namespace llvm::support; |
| 23 | using namespace llvm::pdb; |
| 24 | |
| 25 | // Symbol stream is an array of symbol records. Each record starts with |
| 26 | // length and type fields followed by type-specfic fields. |
| 27 | namespace { |
| 28 | struct SymbolHeader { |
| 29 | ulittle16_t Len; // Record length |
| 30 | ulittle16_t Type; |
| 31 | }; |
| 32 | |
| 33 | // For S_PUB32 symbol type. |
| 34 | struct DataSym32 { |
| 35 | ulittle32_t TypIndex; // Type index, or Metadata token if a managed symbol |
| 36 | ulittle32_t off; |
| 37 | ulittle16_t seg; |
Rui Ueyama | 2a58779 | 2016-05-20 22:59:05 +0000 | [diff] [blame] | 38 | char Name[1]; |
Rui Ueyama | 0fcd826 | 2016-05-20 19:55:17 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | // For S_PROCREF symbol type. |
| 42 | struct RefSym { |
| 43 | ulittle32_t SumName; // SUC of the name (?) |
| 44 | ulittle32_t SymOffset; // Offset of actual symbol in $$Symbols |
| 45 | ulittle16_t Mod; // Module containing the actual symbol |
Rui Ueyama | 2a58779 | 2016-05-20 22:59:05 +0000 | [diff] [blame] | 46 | char Name[1]; |
Rui Ueyama | 0fcd826 | 2016-05-20 19:55:17 +0000 | [diff] [blame] | 47 | }; |
| 48 | } |
| 49 | |
| 50 | SymbolStream::SymbolStream(PDBFile &File, uint32_t StreamNum) |
| 51 | : Stream(StreamNum, File) {} |
| 52 | |
| 53 | SymbolStream::~SymbolStream() {} |
| 54 | |
| 55 | Error SymbolStream::reload() { return Error::success(); } |
| 56 | |
Rui Ueyama | 0fcd826 | 2016-05-20 19:55:17 +0000 | [diff] [blame] | 57 | Expected<std::string> SymbolStream::getSymbolName(uint32_t Off) const { |
| 58 | StreamReader Reader(Stream); |
| 59 | Reader.setOffset(Off); |
| 60 | |
| 61 | // Read length field. |
| 62 | SymbolHeader Hdr; |
| 63 | if (Reader.readObject(&Hdr)) |
| 64 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 65 | "Corrupted symbol stream."); |
| 66 | |
| 67 | // Read the entire record. |
| 68 | std::vector<uint8_t> Buf(Hdr.Len - sizeof(Hdr.Type)); |
| 69 | if (Reader.readBytes(Buf)) |
| 70 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 71 | "Corrupted symbol stream."); |
| 72 | |
| 73 | switch (Hdr.Type) { |
| 74 | case codeview::S_PUB32: |
Rui Ueyama | 2a58779 | 2016-05-20 22:59:05 +0000 | [diff] [blame] | 75 | return reinterpret_cast<DataSym32 *>(Buf.data())->Name; |
Rui Ueyama | 0fcd826 | 2016-05-20 19:55:17 +0000 | [diff] [blame] | 76 | case codeview::S_PROCREF: |
Rui Ueyama | 2a58779 | 2016-05-20 22:59:05 +0000 | [diff] [blame] | 77 | return reinterpret_cast<RefSym *>(Buf.data())->Name; |
Rui Ueyama | 0fcd826 | 2016-05-20 19:55:17 +0000 | [diff] [blame] | 78 | default: |
| 79 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 80 | "Unknown symbol type"); |
| 81 | } |
| 82 | return Error::success(); |
| 83 | } |