Rizin
unix-like reverse engineering framework and cli tools
dbi.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2014-2020 inisider <inisider@gmail.com>
2 // SPDX-FileCopyrightText: 2021 Basstorm <basstorm@nyist.edu.cn>
3 // SPDX-License-Identifier: LGPL-3.0-only
4 
5 #include "pdb.h"
6 
8  if (!stream) {
9  return;
10  }
11  RzPdbDbiStreamExHdr *ex_hdr;
12  RzListIter *it;
13  rz_list_foreach (stream->ex_hdrs, it, ex_hdr) {
14  RZ_FREE(ex_hdr->ModuleName);
15  RZ_FREE(ex_hdr->ObjFileName);
16  RZ_FREE(ex_hdr);
17  }
18  rz_list_free(stream->ex_hdrs);
19  free(stream);
20 }
21 
23  return rz_buf_read_le32(buf, (ut32 *)&s->hdr.version_signature) &&
24  rz_buf_read_le32(buf, &s->hdr.version_header) &&
25  rz_buf_read_le32(buf, &s->hdr.age) &&
26  rz_buf_read_le16(buf, &s->hdr.global_stream_index) &&
27  rz_buf_read_le16(buf, &s->hdr.build_number) &&
28  rz_buf_read_le16(buf, &s->hdr.public_stream_index) &&
29  rz_buf_read_le16(buf, &s->hdr.pdb_dll_version) &&
30  rz_buf_read_le16(buf, &s->hdr.sym_record_stream) &&
31  rz_buf_read_le16(buf, &s->hdr.pdb_dll_rbld) &&
32  rz_buf_read_le32(buf, &s->hdr.mod_info_size) &&
33  rz_buf_read_le32(buf, &s->hdr.section_contribution_size) &&
34  rz_buf_read_le32(buf, &s->hdr.section_map_size) &&
35  rz_buf_read_le32(buf, &s->hdr.source_info_size) &&
36  rz_buf_read_le32(buf, &s->hdr.type_server_map_size) &&
37  rz_buf_read_le32(buf, &s->hdr.mfc_type_server_index) &&
38  rz_buf_read_le32(buf, &s->hdr.optional_dbg_header_size) &&
39  rz_buf_read_le32(buf, &s->hdr.ec_substream_size) &&
40  rz_buf_read_le16(buf, &s->hdr.flags) &&
41  rz_buf_read_le16(buf, &s->hdr.machine) &&
42  rz_buf_read_le32(buf, &s->hdr.padding);
43 }
44 
46  return rz_buf_read_le16(buf, &hdr->sec_con.Section) &&
48  rz_buf_read_le32(buf, (ut32 *)&hdr->sec_con.Offset) &&
49  rz_buf_read_le32(buf, (ut32 *)&hdr->sec_con.Size) &&
55 }
56 
58  s->ex_hdrs = rz_list_new();
59  if (!s->ex_hdrs) {
60  // free s-dbi
61  return false;
62  }
63  ut32 ex_size = s->hdr.mod_info_size;
64  ut32 read_len = 0;
65  bool result = true;
66  while (read_len < ex_size) {
67  ut32 initial_seek = rz_buf_tell(buf);
69  if (!hdr) {
70  result = false;
71  goto err;
72  }
73  if (!rz_buf_read_le32(buf, &hdr->unknown)) {
74  result = false;
75  goto err;
76  }
78  result = false;
79  goto err;
80  }
81  if (!rz_buf_read_le16(buf, &hdr->Flags) ||
83  result = false;
84  goto err;
85  }
86  if (!rz_buf_read_le32(buf, &hdr->SymByteSize) ||
87  !rz_buf_read_le32(buf, &hdr->C11ByteSize) ||
88  !rz_buf_read_le32(buf, &hdr->C13ByteSize)) {
89  result = false;
90  goto err;
91  }
92  if (!rz_buf_read_le16(buf, &hdr->SourceFileCount) ||
93  !rz_buf_read_le16(buf, (ut16 *)&hdr->Padding)) {
94  result = false;
95  goto err;
96  }
97  if (!rz_buf_read_le32(buf, &hdr->Unused2) ||
100  result = false;
101  goto err;
102  }
103 
105  ut32 str_length = strlen(hdr->ModuleName) + 1;
106  if (str_length) {
107  rz_buf_seek(buf, str_length, RZ_BUF_CUR);
108  }
109 
111  str_length = strlen(hdr->ObjFileName) + 1;
112  if (str_length) {
113  rz_buf_seek(buf, str_length, RZ_BUF_CUR);
114  }
115  read_len += rz_buf_tell(buf) - initial_seek;
116  if ((read_len % 4)) {
117  ut16 remain = 4 - (read_len % 4);
118  rz_buf_seek(buf, remain, RZ_BUF_CUR);
119  read_len += remain;
120  }
121  rz_list_append(s->ex_hdrs, hdr);
122  err:
123  if (!result) {
124  free(hdr);
125  return false;
126  }
127  }
128  if (read_len != ex_size) {
129  return false;
130  }
131  return true;
132 }
133 
135  if (!rz_buf_read_le16(buf, (ut16 *)&s->dbg_hdr.sn_fpo) ||
136  !rz_buf_read_le16(buf, (ut16 *)&s->dbg_hdr.sn_exception) ||
137  !rz_buf_read_le16(buf, (ut16 *)&s->dbg_hdr.sn_fixup) ||
138  !rz_buf_read_le16(buf, (ut16 *)&s->dbg_hdr.sn_omap_to_src) ||
139  !rz_buf_read_le16(buf, (ut16 *)&s->dbg_hdr.sn_omap_from_src) ||
140  !rz_buf_read_le16(buf, (ut16 *)&s->dbg_hdr.sn_section_hdr) ||
141  !rz_buf_read_le16(buf, (ut16 *)&s->dbg_hdr.sn_token_rid_map) ||
142  !rz_buf_read_le16(buf, (ut16 *)&s->dbg_hdr.sn_xdata) ||
143  !rz_buf_read_le16(buf, (ut16 *)&s->dbg_hdr.sn_pdata) ||
144  !rz_buf_read_le16(buf, (ut16 *)&s->dbg_hdr.sn_new_fpo) ||
145  !rz_buf_read_le16(buf, (ut16 *)&s->dbg_hdr.sn_section_hdr_orig)) {
146  return false;
147  }
148  return true;
149 }
150 
152  if (!pdb || !stream) {
153  return false;
154  }
155  pdb->s_dbi = RZ_NEW0(RzPdbDbiStream);
156  RzPdbDbiStream *s = pdb->s_dbi;
157  if (!s) {
158  RZ_LOG_ERROR("Error allocating memory.\n");
159  return false;
160  }
161  RzBuffer *buf = stream->stream_data;
162  // parse header
164  return false;
165  }
166 
167  // skip these streams
168  ut64 seek = s->hdr.section_contribution_size + s->hdr.section_map_size +
169  s->hdr.source_info_size + s->hdr.type_server_map_size +
170  s->hdr.ec_substream_size;
172  if (!parse_dbi_dbg_header(s, buf)) {
173  return false;
174  }
175  return true;
176 }
#define RZ_IPI
Definition: analysis_wasm.c:11
static bool err
Definition: armass.c:435
RZ_IPI bool parse_dbi_stream(RzPdb *pdb, RzPdbMsfStream *stream)
Definition: dbi.c:151
RZ_IPI void free_dbi_stream(RzPdbDbiStream *stream)
Definition: dbi.c:7
static bool parse_dbi_stream_header(RzPdbDbiStream *s, RzBuffer *buf)
Definition: dbi.c:22
static bool parse_dbi_stream_ex_header(RzPdbDbiStream *s, RzBuffer *buf)
Definition: dbi.c:57
static bool parse_dbi_stream_section_entry(RzPdbDbiStreamExHdr *hdr, RzBuffer *buf)
Definition: dbi.c:45
static bool parse_dbi_dbg_header(RzPdbDbiStream *s, RzBuffer *buf)
Definition: dbi.c:134
uint16_t ut16
uint32_t ut32
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf stream
Definition: ioapi.h:138
voidpf void * buf
Definition: ioapi.h:138
RZ_API RZ_OWN RzList * rz_list_new(void)
Returns a new initialized RzList pointer (free method is not initialized)
Definition: list.c:235
RZ_API RZ_BORROW RzListIter * rz_list_append(RZ_NONNULL RzList *list, void *data)
Appends at the end of the list a new element.
Definition: list.c:288
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137
static RzSocket * s
Definition: rtr.c:28
RZ_API ut64 rz_buf_tell(RZ_NONNULL RzBuffer *b)
Return the current cursor position.
Definition: buf.c:1238
RZ_API st64 rz_buf_seek(RZ_NONNULL RzBuffer *b, st64 addr, int whence)
Modify the current cursor position in the buffer.
Definition: buf.c:1166
#define RZ_BUF_CUR
Definition: rz_buf.h:15
RZ_API RZ_OWN char * rz_buf_get_string(RZ_NONNULL RzBuffer *b, ut64 addr)
Get a string from the buffer.
Definition: buf.c:628
#define rz_buf_read_le16(b, result)
Read a big endian or little endian (ut16, ut32, ut64) at the specified offset in the buffer and shift...
Definition: rz_buf.h:266
#define rz_buf_read_le32(b, result)
Definition: rz_buf.h:267
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define RZ_FREE(x)
Definition: rz_types.h:369
char Padding2[2]
Definition: dbi.h:24
ut16 Section
Definition: dbi.h:18
ut32 Characteristics
Definition: dbi.h:22
char Padding1[2]
Definition: dbi.h:19
ut16 ModuleIndex
Definition: dbi.h:23
ut32 DataCrc
Definition: dbi.h:25
ut32 RelocCrc
Definition: dbi.h:26
SectionContr sec_con
Definition: dbi.h:31
char * ModuleName
Definition: dbi.h:42
ut32 PdbFilePathNameIndex
Definition: dbi.h:41
ut32 SourceFileNameIndex
Definition: dbi.h:40
char Padding[2]
Definition: dbi.h:38
ut16 SourceFileCount
Definition: dbi.h:37
char * ObjFileName
Definition: dbi.h:43
ut16 ModuleSymStream
Definition: dbi.h:33
RzPdbDbiStream * s_dbi
Definition: rz_pdb.h:246
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static int seek(char *argv[])