blob: bd4280f34926339bb609ec04bdbe1f313d63c46e [file] [log] [blame]
Rui Ueyama0fcd8262016-05-20 19:55:17 +00001//===- 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
21using namespace llvm;
22using namespace llvm::support;
23using 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.
27namespace {
28struct SymbolHeader {
29 ulittle16_t Len; // Record length
30 ulittle16_t Type;
31};
32
33// For S_PUB32 symbol type.
34struct DataSym32 {
35 ulittle32_t TypIndex; // Type index, or Metadata token if a managed symbol
36 ulittle32_t off;
37 ulittle16_t seg;
Rui Ueyama2a587792016-05-20 22:59:05 +000038 char Name[1];
Rui Ueyama0fcd8262016-05-20 19:55:17 +000039};
40
41// For S_PROCREF symbol type.
42struct 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 Ueyama2a587792016-05-20 22:59:05 +000046 char Name[1];
Rui Ueyama0fcd8262016-05-20 19:55:17 +000047};
48}
49
50SymbolStream::SymbolStream(PDBFile &File, uint32_t StreamNum)
51 : Stream(StreamNum, File) {}
52
53SymbolStream::~SymbolStream() {}
54
55Error SymbolStream::reload() { return Error::success(); }
56
Rui Ueyama0fcd8262016-05-20 19:55:17 +000057Expected<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 Ueyama2a587792016-05-20 22:59:05 +000075 return reinterpret_cast<DataSym32 *>(Buf.data())->Name;
Rui Ueyama0fcd8262016-05-20 19:55:17 +000076 case codeview::S_PROCREF:
Rui Ueyama2a587792016-05-20 22:59:05 +000077 return reinterpret_cast<RefSym *>(Buf.data())->Name;
Rui Ueyama0fcd8262016-05-20 19:55:17 +000078 default:
79 return make_error<RawError>(raw_error_code::corrupt_file,
80 "Unknown symbol type");
81 }
82 return Error::success();
83}