xref: /openbmc/qemu/contrib/elf2dmp/pdb.c (revision 2df1eb27)
1 /*
2  * Copyright (c) 2018 Virtuozzo International GmbH
3  *
4  * Based on source of Wine project
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20 
21 #include "qemu/osdep.h"
22 
23 #include "pdb.h"
24 #include "err.h"
25 
26 static uint32_t pdb_get_file_size(const struct pdb_reader *r, unsigned idx)
27 {
28     if (idx >= r->ds.toc->num_files) {
29         return 0;
30     }
31 
32     return r->ds.toc->file_size[idx];
33 }
34 
35 static pdb_seg *get_seg_by_num(struct pdb_reader *r, size_t n)
36 {
37     size_t i = 0;
38     char *ptr;
39 
40     for (ptr = r->segs; (ptr < r->segs + r->segs_size); ) {
41         i++;
42         ptr += 8;
43         if (i == n) {
44             break;
45         }
46         ptr += sizeof(pdb_seg);
47     }
48 
49     return (pdb_seg *)ptr;
50 }
51 
52 uint64_t pdb_find_public_v3_symbol(struct pdb_reader *r, const char *name)
53 {
54     size_t size = pdb_get_file_size(r, r->symbols->gsym_file);
55     int length;
56     const union codeview_symbol *sym;
57     const uint8_t *root = r->modimage;
58     size_t i;
59 
60     for (i = 0; i < size; i += length) {
61         sym = (const void *)(root + i);
62         length = sym->generic.len + 2;
63 
64         if (!sym->generic.id || length < 4) {
65             break;
66         }
67 
68         if (sym->generic.id == S_PUB_V3 &&
69                 !strcmp(name, sym->public_v3.name)) {
70             pdb_seg *segment = get_seg_by_num(r, sym->public_v3.segment);
71             uint32_t sect_rva = segment->dword[1];
72             uint64_t rva = sect_rva + sym->public_v3.offset;
73 
74             printf("%s: 0x%016x(%d:\'%.8s\') + 0x%08x = 0x%09"PRIx64"\n", name,
75                     sect_rva, sym->public_v3.segment,
76                     ((char *)segment - 8), sym->public_v3.offset, rva);
77             return rva;
78         }
79     }
80 
81     return 0;
82 }
83 
84 uint64_t pdb_resolve(uint64_t img_base, struct pdb_reader *r, const char *name)
85 {
86     uint64_t rva = pdb_find_public_v3_symbol(r, name);
87 
88     if (!rva) {
89         return 0;
90     }
91 
92     return img_base + rva;
93 }
94 
95 static void pdb_reader_ds_exit(struct pdb_reader *r)
96 {
97     g_free(r->ds.toc);
98 }
99 
100 static void pdb_exit_symbols(struct pdb_reader *r)
101 {
102     g_free(r->modimage);
103     g_free(r->symbols);
104 }
105 
106 static void pdb_exit_segments(struct pdb_reader *r)
107 {
108     g_free(r->segs);
109 }
110 
111 static void *pdb_ds_read(const PDB_DS_HEADER *header,
112         const uint32_t *block_list, int size)
113 {
114     int i, nBlocks;
115     uint8_t *buffer;
116 
117     if (!size) {
118         return NULL;
119     }
120 
121     nBlocks = (size + header->block_size - 1) / header->block_size;
122 
123     buffer = g_malloc(nBlocks * header->block_size);
124 
125     for (i = 0; i < nBlocks; i++) {
126         memcpy(buffer + i * header->block_size, (const char *)header +
127                 block_list[i] * header->block_size, header->block_size);
128     }
129 
130     return buffer;
131 }
132 
133 static void *pdb_ds_read_file(struct pdb_reader* r, uint32_t file_number)
134 {
135     const uint32_t *block_list;
136     uint32_t block_size;
137     const uint32_t *file_size;
138     size_t i;
139 
140     if (!r->ds.toc || file_number >= r->ds.toc->num_files) {
141         return NULL;
142     }
143 
144     file_size = r->ds.toc->file_size;
145     r->file_used[file_number / 32] |= 1 << (file_number % 32);
146 
147     if (file_size[file_number] == 0 || file_size[file_number] == 0xFFFFFFFF) {
148         return NULL;
149     }
150 
151     block_list = file_size + r->ds.toc->num_files;
152     block_size = r->ds.header->block_size;
153 
154     for (i = 0; i < file_number; i++) {
155         block_list += (file_size[i] + block_size - 1) / block_size;
156     }
157 
158     return pdb_ds_read(r->ds.header, block_list, file_size[file_number]);
159 }
160 
161 static int pdb_init_segments(struct pdb_reader *r)
162 {
163     unsigned stream_idx = r->segments;
164 
165     r->segs = pdb_ds_read_file(r, stream_idx);
166     if (!r->segs) {
167         return 1;
168     }
169 
170     r->segs_size = pdb_get_file_size(r, stream_idx);
171     if (!r->segs_size) {
172         return 1;
173     }
174 
175     return 0;
176 }
177 
178 static int pdb_init_symbols(struct pdb_reader *r)
179 {
180     int err = 0;
181     PDB_SYMBOLS *symbols;
182 
183     symbols = pdb_ds_read_file(r, 3);
184     if (!symbols) {
185         return 1;
186     }
187 
188     r->symbols = symbols;
189 
190     r->segments = *(uint16_t *)((const char *)symbols + sizeof(PDB_SYMBOLS) +
191             symbols->module_size + symbols->offset_size +
192             symbols->hash_size + symbols->srcmodule_size +
193             symbols->pdbimport_size + symbols->unknown2_size +
194             offsetof(PDB_STREAM_INDEXES, segments));
195 
196     /* Read global symbol table */
197     r->modimage = pdb_ds_read_file(r, symbols->gsym_file);
198     if (!r->modimage) {
199         err = 1;
200         goto out_symbols;
201     }
202 
203     return 0;
204 
205 out_symbols:
206     g_free(symbols);
207 
208     return err;
209 }
210 
211 static int pdb_reader_ds_init(struct pdb_reader *r, PDB_DS_HEADER *hdr)
212 {
213     if (hdr->block_size == 0) {
214         return 1;
215     }
216 
217     memset(r->file_used, 0, sizeof(r->file_used));
218     r->ds.header = hdr;
219     r->ds.toc = pdb_ds_read(hdr, (uint32_t *)((uint8_t *)hdr +
220                 hdr->toc_page * hdr->block_size), hdr->toc_size);
221 
222     if (!r->ds.toc) {
223         return 1;
224     }
225 
226     return 0;
227 }
228 
229 static int pdb_reader_init(struct pdb_reader *r, void *data)
230 {
231     int err = 0;
232     const char pdb7[] = "Microsoft C/C++ MSF 7.00";
233 
234     if (memcmp(data, pdb7, sizeof(pdb7) - 1)) {
235         return 1;
236     }
237 
238     if (pdb_reader_ds_init(r, data)) {
239         return 1;
240     }
241 
242     r->ds.root = pdb_ds_read_file(r, 1);
243     if (!r->ds.root) {
244         err = 1;
245         goto out_ds;
246     }
247 
248     if (pdb_init_symbols(r)) {
249         err = 1;
250         goto out_root;
251     }
252 
253     if (pdb_init_segments(r)) {
254         err = 1;
255         goto out_sym;
256     }
257 
258     return 0;
259 
260 out_sym:
261     pdb_exit_symbols(r);
262 out_root:
263     g_free(r->ds.root);
264 out_ds:
265     pdb_reader_ds_exit(r);
266 
267     return err;
268 }
269 
270 static void pdb_reader_exit(struct pdb_reader *r)
271 {
272     pdb_exit_segments(r);
273     pdb_exit_symbols(r);
274     g_free(r->ds.root);
275     pdb_reader_ds_exit(r);
276 }
277 
278 int pdb_init_from_file(const char *name, struct pdb_reader *reader)
279 {
280     GError *gerr = NULL;
281     int err = 0;
282     void *map;
283 
284     reader->gmf = g_mapped_file_new(name, TRUE, &gerr);
285     if (gerr) {
286         eprintf("Failed to map PDB file \'%s\'\n", name);
287         g_error_free(gerr);
288         return 1;
289     }
290 
291     reader->file_size = g_mapped_file_get_length(reader->gmf);
292     map = g_mapped_file_get_contents(reader->gmf);
293     if (pdb_reader_init(reader, map)) {
294         err = 1;
295         goto out_unmap;
296     }
297 
298     return 0;
299 
300 out_unmap:
301     g_mapped_file_unref(reader->gmf);
302 
303     return err;
304 }
305 
306 void pdb_exit(struct pdb_reader *reader)
307 {
308     g_mapped_file_unref(reader->gmf);
309     pdb_reader_exit(reader);
310 }
311