/src/binutils-gdb/bfd/pdb.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* BFD back-end for PDB Multi-Stream Format archives. |
2 | | Copyright (C) 2022-2023 Free Software Foundation, Inc. |
3 | | |
4 | | This file is part of BFD, the Binary File Descriptor library. |
5 | | |
6 | | This program is free software; you can redistribute it and/or modify |
7 | | it under the terms of the GNU General Public License as published by |
8 | | the Free Software Foundation; either version 3 of the License, or |
9 | | (at your option) any later version. |
10 | | |
11 | | This program 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 |
14 | | GNU General Public License for more details. |
15 | | |
16 | | You should have received a copy of the GNU General Public License |
17 | | along with this program; if not, write to the Free Software |
18 | | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
19 | | MA 02110-1301, USA. */ |
20 | | |
21 | | /* This describes the MSF file archive format, which is used for the |
22 | | PDB debug info generated by MSVC. See https://llvm.org/docs/PDB/MsfFile.html |
23 | | for a full description of the format. */ |
24 | | |
25 | | #include "sysdep.h" |
26 | | #include "bfd.h" |
27 | | #include "libbfd.h" |
28 | | |
29 | | /* "Microsoft C/C++ MSF 7.00\r\n\x1a\x44\x53\0\0\0" */ |
30 | | static const uint8_t pdb_magic[] = |
31 | | { 0x4d, 0x69, 0x63, 0x72, 0x6f, 0x73, 0x6f, 0x66, |
32 | | 0x74, 0x20, 0x43, 0x2f, 0x43, 0x2b, 0x2b, 0x20, |
33 | | 0x4d, 0x53, 0x46, 0x20, 0x37, 0x2e, 0x30, 0x30, |
34 | | 0x0d, 0x0a, 0x1a, 0x44, 0x53, 0x00, 0x00, 0x00 }; |
35 | | |
36 | 0 | #define arch_eltdata(bfd) ((struct areltdata *) ((bfd)->arelt_data)) |
37 | | |
38 | | static bfd_cleanup |
39 | | pdb_archive_p (bfd *abfd) |
40 | 19.2k | { |
41 | 19.2k | int ret; |
42 | 19.2k | char magic[sizeof (pdb_magic)]; |
43 | | |
44 | 19.2k | ret = bfd_read (magic, sizeof (magic), abfd); |
45 | 19.2k | if (ret != sizeof (magic)) |
46 | 1.24k | { |
47 | 1.24k | bfd_set_error (bfd_error_wrong_format); |
48 | 1.24k | return NULL; |
49 | 1.24k | } |
50 | | |
51 | 17.9k | if (memcmp (magic, pdb_magic, sizeof (magic))) |
52 | 17.9k | { |
53 | 17.9k | bfd_set_error (bfd_error_wrong_format); |
54 | 17.9k | return NULL; |
55 | 17.9k | } |
56 | | |
57 | 1 | void *tdata = bfd_zalloc (abfd, sizeof (struct artdata)); |
58 | 1 | if (tdata == NULL) |
59 | 0 | return NULL; |
60 | 1 | bfd_ardata (abfd) = tdata; |
61 | | |
62 | 1 | return _bfd_no_cleanup; |
63 | 1 | } |
64 | | |
65 | | static bfd * |
66 | | pdb_get_elt_at_index (bfd *abfd, symindex sym_index) |
67 | 0 | { |
68 | 0 | char int_buf[sizeof (uint32_t)]; |
69 | 0 | uint32_t block_size, block_map_addr, block, num_files; |
70 | 0 | uint32_t first_dir_block, dir_offset, file_size, block_off, left; |
71 | 0 | char name[10]; |
72 | 0 | bfd *file; |
73 | 0 | char *buf; |
74 | | |
75 | | /* Get block_size. */ |
76 | |
|
77 | 0 | if (bfd_seek (abfd, sizeof (pdb_magic), SEEK_SET)) |
78 | 0 | return NULL; |
79 | | |
80 | 0 | if (bfd_read (int_buf, sizeof (uint32_t), abfd) != sizeof (uint32_t)) |
81 | 0 | { |
82 | 0 | bfd_set_error (bfd_error_malformed_archive); |
83 | 0 | return NULL; |
84 | 0 | } |
85 | | |
86 | 0 | block_size = bfd_getl32 (int_buf); |
87 | 0 | if ((block_size & -block_size) != block_size |
88 | 0 | || block_size < 512 |
89 | 0 | || block_size > 4096) |
90 | 0 | { |
91 | 0 | bfd_set_error (bfd_error_malformed_archive); |
92 | 0 | return NULL; |
93 | 0 | } |
94 | | |
95 | | /* Get block_map_addr. */ |
96 | | |
97 | 0 | if (bfd_seek (abfd, 4 * sizeof (uint32_t), SEEK_CUR)) |
98 | 0 | return NULL; |
99 | | |
100 | 0 | if (bfd_read (int_buf, sizeof (uint32_t), abfd) != sizeof (uint32_t)) |
101 | 0 | { |
102 | 0 | bfd_set_error (bfd_error_malformed_archive); |
103 | 0 | return NULL; |
104 | 0 | } |
105 | | |
106 | 0 | block_map_addr = bfd_getl32 (int_buf); |
107 | | |
108 | | /* Get num_files. */ |
109 | |
|
110 | 0 | if (bfd_seek (abfd, block_map_addr * block_size, SEEK_SET)) |
111 | 0 | return NULL; |
112 | | |
113 | 0 | if (bfd_read (int_buf, sizeof (uint32_t), abfd) != sizeof (uint32_t)) |
114 | 0 | { |
115 | 0 | bfd_set_error (bfd_error_malformed_archive); |
116 | 0 | return NULL; |
117 | 0 | } |
118 | | |
119 | 0 | first_dir_block = bfd_getl32 (int_buf); |
120 | |
|
121 | 0 | if (bfd_seek (abfd, first_dir_block * block_size, SEEK_SET)) |
122 | 0 | return NULL; |
123 | | |
124 | 0 | if (bfd_read (int_buf, sizeof (uint32_t), abfd) != sizeof (uint32_t)) |
125 | 0 | { |
126 | 0 | bfd_set_error (bfd_error_malformed_archive); |
127 | 0 | return NULL; |
128 | 0 | } |
129 | | |
130 | 0 | num_files = bfd_getl32 (int_buf); |
131 | |
|
132 | 0 | if (sym_index >= num_files) |
133 | 0 | { |
134 | 0 | bfd_set_error (bfd_error_no_more_archived_files); |
135 | 0 | return NULL; |
136 | 0 | } |
137 | | |
138 | | /* Read file size. */ |
139 | | |
140 | 0 | dir_offset = sizeof (uint32_t) * (sym_index + 1); |
141 | |
|
142 | 0 | if (dir_offset >= block_size) |
143 | 0 | { |
144 | 0 | uint32_t block_map_addr_off; |
145 | |
|
146 | 0 | block_map_addr_off = ((dir_offset / block_size) * sizeof (uint32_t)); |
147 | |
|
148 | 0 | if (bfd_seek (abfd, (block_map_addr * block_size) + block_map_addr_off, |
149 | 0 | SEEK_SET)) |
150 | 0 | return NULL; |
151 | | |
152 | 0 | if (bfd_read (int_buf, sizeof (uint32_t), abfd) != sizeof (uint32_t)) |
153 | 0 | { |
154 | 0 | bfd_set_error (bfd_error_malformed_archive); |
155 | 0 | return NULL; |
156 | 0 | } |
157 | | |
158 | 0 | block = bfd_getl32 (int_buf); |
159 | 0 | } |
160 | 0 | else |
161 | 0 | { |
162 | 0 | block = first_dir_block; |
163 | 0 | } |
164 | | |
165 | 0 | if (bfd_seek (abfd, (block * block_size) + (dir_offset % block_size), |
166 | 0 | SEEK_SET)) |
167 | 0 | return NULL; |
168 | | |
169 | 0 | if (bfd_read (int_buf, sizeof (uint32_t), abfd) != sizeof (uint32_t)) |
170 | 0 | { |
171 | 0 | bfd_set_error (bfd_error_malformed_archive); |
172 | 0 | return NULL; |
173 | 0 | } |
174 | | |
175 | 0 | file_size = bfd_getl32 (int_buf); |
176 | | |
177 | | /* Undocumented? Seen on PDBs created by MSVC 2022. */ |
178 | 0 | if (file_size == 0xffffffff) |
179 | 0 | file_size = 0; |
180 | | |
181 | | /* Create BFD. */ |
182 | | |
183 | | /* Four hex digits is enough - even though MSF allows for 32 bits, the |
184 | | PDB format itself only uses 16 bits for stream numbers. */ |
185 | 0 | sprintf (name, "%04lx", sym_index); |
186 | |
|
187 | 0 | file = bfd_create (name, abfd); |
188 | |
|
189 | 0 | if (!file) |
190 | 0 | return NULL; |
191 | | |
192 | 0 | if (!bfd_make_writable (file)) |
193 | 0 | goto fail; |
194 | | |
195 | 0 | file->arelt_data = |
196 | 0 | (struct areltdata *) bfd_zmalloc (sizeof (struct areltdata)); |
197 | |
|
198 | 0 | if (!file->arelt_data) |
199 | 0 | goto fail; |
200 | | |
201 | 0 | arch_eltdata (file)->parsed_size = file_size; |
202 | 0 | arch_eltdata (file)->key = sym_index; |
203 | |
|
204 | 0 | if (file_size == 0) |
205 | 0 | return file; |
206 | | |
207 | 0 | block_off = 0; |
208 | | |
209 | | /* Sum number of blocks in previous files. */ |
210 | |
|
211 | 0 | if (sym_index != 0) |
212 | 0 | { |
213 | 0 | dir_offset = sizeof (uint32_t); |
214 | |
|
215 | 0 | if (bfd_seek (abfd, (first_dir_block * block_size) + sizeof (uint32_t), |
216 | 0 | SEEK_SET)) |
217 | 0 | goto fail; |
218 | | |
219 | 0 | for (symindex i = 0; i < sym_index; i++) |
220 | 0 | { |
221 | 0 | uint32_t size, num_blocks; |
222 | |
|
223 | 0 | if ((dir_offset % block_size) == 0) |
224 | 0 | { |
225 | 0 | uint32_t block_map_addr_off; |
226 | |
|
227 | 0 | block_map_addr_off = |
228 | 0 | ((dir_offset / block_size) * sizeof (uint32_t)); |
229 | |
|
230 | 0 | if (bfd_seek |
231 | 0 | (abfd, (block_map_addr * block_size) + block_map_addr_off, |
232 | 0 | SEEK_SET)) |
233 | 0 | goto fail; |
234 | | |
235 | 0 | if (bfd_read (int_buf, sizeof (uint32_t), abfd) != |
236 | 0 | sizeof (uint32_t)) |
237 | 0 | { |
238 | 0 | bfd_set_error (bfd_error_malformed_archive); |
239 | 0 | goto fail; |
240 | 0 | } |
241 | | |
242 | 0 | block = bfd_getl32 (int_buf); |
243 | |
|
244 | 0 | if (bfd_seek (abfd, block * block_size, SEEK_SET)) |
245 | 0 | goto fail; |
246 | 0 | } |
247 | | |
248 | 0 | if (bfd_read (int_buf, sizeof (uint32_t), abfd) != |
249 | 0 | sizeof (uint32_t)) |
250 | 0 | { |
251 | 0 | bfd_set_error (bfd_error_malformed_archive); |
252 | 0 | goto fail; |
253 | 0 | } |
254 | | |
255 | 0 | size = bfd_getl32 (int_buf); |
256 | |
|
257 | 0 | if (size == 0xffffffff) |
258 | 0 | size = 0; |
259 | |
|
260 | 0 | num_blocks = (size + block_size - 1) / block_size; |
261 | 0 | block_off += num_blocks; |
262 | |
|
263 | 0 | dir_offset += sizeof (uint32_t); |
264 | 0 | } |
265 | 0 | } |
266 | | |
267 | | /* Read blocks, and write into new BFD. */ |
268 | | |
269 | 0 | dir_offset = sizeof (uint32_t) * (num_files + block_off + 1); |
270 | |
|
271 | 0 | if (dir_offset >= block_size) |
272 | 0 | { |
273 | 0 | uint32_t block_map_addr_off; |
274 | |
|
275 | 0 | block_map_addr_off = ((dir_offset / block_size) * sizeof (uint32_t)); |
276 | |
|
277 | 0 | if (bfd_seek (abfd, (block_map_addr * block_size) + block_map_addr_off, |
278 | 0 | SEEK_SET)) |
279 | 0 | goto fail; |
280 | | |
281 | 0 | if (bfd_read (int_buf, sizeof (uint32_t), abfd) != sizeof (uint32_t)) |
282 | 0 | { |
283 | 0 | bfd_set_error (bfd_error_malformed_archive); |
284 | 0 | goto fail; |
285 | 0 | } |
286 | | |
287 | 0 | block = bfd_getl32 (int_buf); |
288 | 0 | } |
289 | 0 | else |
290 | 0 | { |
291 | 0 | block = first_dir_block; |
292 | 0 | } |
293 | | |
294 | 0 | buf = bfd_malloc (block_size); |
295 | 0 | if (!buf) |
296 | 0 | goto fail; |
297 | | |
298 | 0 | left = file_size; |
299 | 0 | do |
300 | 0 | { |
301 | 0 | uint32_t file_block, to_read; |
302 | |
|
303 | 0 | if ((dir_offset % block_size) == 0 && left != file_size) |
304 | 0 | { |
305 | 0 | uint32_t block_map_addr_off; |
306 | |
|
307 | 0 | block_map_addr_off = |
308 | 0 | ((dir_offset / block_size) * sizeof (uint32_t)); |
309 | |
|
310 | 0 | if (bfd_seek |
311 | 0 | (abfd, (block_map_addr * block_size) + block_map_addr_off, |
312 | 0 | SEEK_SET)) |
313 | 0 | goto fail2; |
314 | | |
315 | 0 | if (bfd_read (int_buf, sizeof (uint32_t), abfd) != |
316 | 0 | sizeof (uint32_t)) |
317 | 0 | { |
318 | 0 | bfd_set_error (bfd_error_malformed_archive); |
319 | 0 | goto fail2; |
320 | 0 | } |
321 | | |
322 | 0 | block = bfd_getl32 (int_buf); |
323 | 0 | } |
324 | | |
325 | 0 | if (bfd_seek (abfd, (block * block_size) + (dir_offset % block_size), |
326 | 0 | SEEK_SET)) |
327 | 0 | goto fail2; |
328 | | |
329 | 0 | if (bfd_read (int_buf, sizeof (uint32_t), abfd) != sizeof (uint32_t)) |
330 | 0 | { |
331 | 0 | bfd_set_error (bfd_error_malformed_archive); |
332 | 0 | goto fail2; |
333 | 0 | } |
334 | | |
335 | 0 | file_block = bfd_getl32 (int_buf); |
336 | |
|
337 | 0 | if (bfd_seek (abfd, file_block * block_size, SEEK_SET)) |
338 | 0 | goto fail2; |
339 | | |
340 | 0 | to_read = left > block_size ? block_size : left; |
341 | |
|
342 | 0 | if (bfd_read (buf, to_read, abfd) != to_read) |
343 | 0 | { |
344 | 0 | bfd_set_error (bfd_error_malformed_archive); |
345 | 0 | goto fail2; |
346 | 0 | } |
347 | | |
348 | 0 | if (bfd_write (buf, to_read, file) != to_read) |
349 | 0 | goto fail2; |
350 | | |
351 | 0 | if (left > block_size) |
352 | 0 | left -= block_size; |
353 | 0 | else |
354 | 0 | break; |
355 | | |
356 | 0 | dir_offset += sizeof (uint32_t); |
357 | 0 | } |
358 | 0 | while (left > 0); |
359 | | |
360 | 0 | free (buf); |
361 | |
|
362 | 0 | return file; |
363 | | |
364 | 0 | fail2: |
365 | 0 | free (buf); |
366 | |
|
367 | 0 | fail: |
368 | 0 | bfd_close (file); |
369 | 0 | return NULL; |
370 | 0 | } |
371 | | |
372 | | static bfd * |
373 | | pdb_openr_next_archived_file (bfd *archive, bfd *last_file) |
374 | 0 | { |
375 | 0 | if (!last_file) |
376 | 0 | return pdb_get_elt_at_index (archive, 0); |
377 | 0 | else |
378 | 0 | return pdb_get_elt_at_index (archive, arch_eltdata (last_file)->key + 1); |
379 | 0 | } |
380 | | |
381 | | static int |
382 | | pdb_generic_stat_arch_elt (bfd *abfd, struct stat *buf) |
383 | 0 | { |
384 | 0 | buf->st_mtime = 0; |
385 | 0 | buf->st_uid = 0; |
386 | 0 | buf->st_gid = 0; |
387 | 0 | buf->st_mode = 0644; |
388 | 0 | buf->st_size = arch_eltdata (abfd)->parsed_size; |
389 | |
|
390 | 0 | return 0; |
391 | 0 | } |
392 | | |
393 | | static uint32_t |
394 | | pdb_allocate_block (uint32_t *num_blocks, uint32_t block_size) |
395 | 0 | { |
396 | 0 | uint32_t block; |
397 | |
|
398 | 0 | block = *num_blocks; |
399 | |
|
400 | 0 | (*num_blocks)++; |
401 | | |
402 | | /* If new interval, skip two blocks for free space map. */ |
403 | |
|
404 | 0 | if ((block % block_size) == 1) |
405 | 0 | { |
406 | 0 | block += 2; |
407 | 0 | (*num_blocks) += 2; |
408 | 0 | } |
409 | |
|
410 | 0 | return block; |
411 | 0 | } |
412 | | |
413 | | static bool |
414 | | pdb_write_directory (bfd *abfd, uint32_t block_size, uint32_t num_files, |
415 | | uint32_t block_map_addr, uint32_t * num_blocks) |
416 | 0 | { |
417 | 0 | char tmp[sizeof (uint32_t)]; |
418 | 0 | uint32_t block, left, block_map_off; |
419 | 0 | bfd *arelt; |
420 | 0 | char *buf; |
421 | | |
422 | | /* Allocate first block for directory. */ |
423 | |
|
424 | 0 | block = pdb_allocate_block (num_blocks, block_size); |
425 | 0 | left = block_size; |
426 | | |
427 | | /* Write allocated block no. at beginning of block map. */ |
428 | |
|
429 | 0 | if (bfd_seek (abfd, block_map_addr * block_size, SEEK_SET)) |
430 | 0 | return false; |
431 | | |
432 | 0 | bfd_putl32 (block, tmp); |
433 | |
|
434 | 0 | if (bfd_write (tmp, sizeof (uint32_t), abfd) != sizeof (uint32_t)) |
435 | 0 | return false; |
436 | | |
437 | 0 | block_map_off = sizeof (uint32_t); |
438 | | |
439 | | /* Write num_files at beginning of directory. */ |
440 | |
|
441 | 0 | if (bfd_seek (abfd, block * block_size, SEEK_SET)) |
442 | 0 | return false; |
443 | | |
444 | 0 | bfd_putl32 (num_files, tmp); |
445 | |
|
446 | 0 | if (bfd_write (tmp, sizeof (uint32_t), abfd) != sizeof (uint32_t)) |
447 | 0 | return false; |
448 | | |
449 | 0 | left -= sizeof (uint32_t); |
450 | | |
451 | | /* Write file sizes. */ |
452 | |
|
453 | 0 | arelt = abfd->archive_head; |
454 | 0 | while (arelt) |
455 | 0 | { |
456 | 0 | if (left == 0) |
457 | 0 | { |
458 | 0 | if (block_map_off == block_size) /* Too many blocks. */ |
459 | 0 | { |
460 | 0 | bfd_set_error (bfd_error_invalid_operation); |
461 | 0 | return false; |
462 | 0 | } |
463 | | |
464 | 0 | block = pdb_allocate_block (num_blocks, block_size); |
465 | 0 | left = block_size; |
466 | |
|
467 | 0 | if (bfd_seek |
468 | 0 | (abfd, (block_map_addr * block_size) + block_map_off, SEEK_SET)) |
469 | 0 | return false; |
470 | | |
471 | 0 | bfd_putl32 (block, tmp); |
472 | |
|
473 | 0 | if (bfd_write (tmp, sizeof (uint32_t), abfd) != sizeof (uint32_t)) |
474 | 0 | return false; |
475 | | |
476 | 0 | block_map_off += sizeof (uint32_t); |
477 | |
|
478 | 0 | if (bfd_seek (abfd, block * block_size, SEEK_SET)) |
479 | 0 | return false; |
480 | 0 | } |
481 | | |
482 | 0 | bfd_putl32 (bfd_get_size (arelt), tmp); |
483 | |
|
484 | 0 | if (bfd_write (tmp, sizeof (uint32_t), abfd) != sizeof (uint32_t)) |
485 | 0 | return false; |
486 | | |
487 | 0 | left -= sizeof (uint32_t); |
488 | |
|
489 | 0 | arelt = arelt->archive_next; |
490 | 0 | } |
491 | | |
492 | | /* Write blocks. */ |
493 | | |
494 | 0 | buf = bfd_malloc (block_size); |
495 | 0 | if (!buf) |
496 | 0 | return false; |
497 | | |
498 | 0 | arelt = abfd->archive_head; |
499 | 0 | while (arelt) |
500 | 0 | { |
501 | 0 | ufile_ptr size = bfd_get_size (arelt); |
502 | 0 | uint32_t req_blocks = (size + block_size - 1) / block_size; |
503 | |
|
504 | 0 | if (bfd_seek (arelt, 0, SEEK_SET)) |
505 | 0 | { |
506 | 0 | free (buf); |
507 | 0 | return false; |
508 | 0 | } |
509 | | |
510 | 0 | for (uint32_t i = 0; i < req_blocks; i++) |
511 | 0 | { |
512 | 0 | uint32_t file_block, to_read; |
513 | |
|
514 | 0 | if (left == 0) |
515 | 0 | { |
516 | 0 | if (block_map_off == block_size) /* Too many blocks. */ |
517 | 0 | { |
518 | 0 | bfd_set_error (bfd_error_invalid_operation); |
519 | 0 | free (buf); |
520 | 0 | return false; |
521 | 0 | } |
522 | | |
523 | 0 | block = pdb_allocate_block (num_blocks, block_size); |
524 | 0 | left = block_size; |
525 | |
|
526 | 0 | if (bfd_seek |
527 | 0 | (abfd, (block_map_addr * block_size) + block_map_off, |
528 | 0 | SEEK_SET)) |
529 | 0 | { |
530 | 0 | free (buf); |
531 | 0 | return false; |
532 | 0 | } |
533 | | |
534 | 0 | bfd_putl32 (block, tmp); |
535 | |
|
536 | 0 | if (bfd_write (tmp, sizeof (uint32_t), abfd) != |
537 | 0 | sizeof (uint32_t)) |
538 | 0 | { |
539 | 0 | free (buf); |
540 | 0 | return false; |
541 | 0 | } |
542 | | |
543 | 0 | block_map_off += sizeof (uint32_t); |
544 | |
|
545 | 0 | if (bfd_seek (abfd, block * block_size, SEEK_SET)) |
546 | 0 | { |
547 | 0 | free (buf); |
548 | 0 | return false; |
549 | 0 | } |
550 | 0 | } |
551 | | |
552 | | /* Allocate block and write number into directory. */ |
553 | | |
554 | 0 | file_block = pdb_allocate_block (num_blocks, block_size); |
555 | |
|
556 | 0 | bfd_putl32 (file_block, tmp); |
557 | |
|
558 | 0 | if (bfd_write (tmp, sizeof (uint32_t), abfd) != sizeof (uint32_t)) |
559 | 0 | { |
560 | 0 | free (buf); |
561 | 0 | return false; |
562 | 0 | } |
563 | | |
564 | 0 | left -= sizeof (uint32_t); |
565 | | |
566 | | /* Read file contents into buffer. */ |
567 | |
|
568 | 0 | to_read = size > block_size ? block_size : size; |
569 | |
|
570 | 0 | if (bfd_read (buf, to_read, arelt) != to_read) |
571 | 0 | { |
572 | 0 | free (buf); |
573 | 0 | return false; |
574 | 0 | } |
575 | | |
576 | 0 | size -= to_read; |
577 | |
|
578 | 0 | if (to_read < block_size) |
579 | 0 | memset (buf + to_read, 0, block_size - to_read); |
580 | |
|
581 | 0 | if (bfd_seek (abfd, file_block * block_size, SEEK_SET)) |
582 | 0 | { |
583 | 0 | free (buf); |
584 | 0 | return false; |
585 | 0 | } |
586 | | |
587 | | /* Write file contents into allocated block. */ |
588 | | |
589 | 0 | if (bfd_write (buf, block_size, abfd) != block_size) |
590 | 0 | { |
591 | 0 | free (buf); |
592 | 0 | return false; |
593 | 0 | } |
594 | | |
595 | 0 | if (bfd_seek |
596 | 0 | (abfd, (block * block_size) + block_size - left, SEEK_SET)) |
597 | 0 | { |
598 | 0 | free (buf); |
599 | 0 | return false; |
600 | 0 | } |
601 | 0 | } |
602 | | |
603 | 0 | arelt = arelt->archive_next; |
604 | 0 | } |
605 | | |
606 | 0 | memset (buf, 0, left); |
607 | |
|
608 | 0 | if (bfd_write (buf, left, abfd) != left) |
609 | 0 | { |
610 | 0 | free (buf); |
611 | 0 | return false; |
612 | 0 | } |
613 | | |
614 | 0 | free (buf); |
615 | |
|
616 | 0 | return true; |
617 | 0 | } |
618 | | |
619 | | static bool |
620 | | pdb_write_bitmap (bfd *abfd, uint32_t block_size, uint32_t num_blocks) |
621 | 0 | { |
622 | 0 | char *buf; |
623 | 0 | uint32_t num_intervals = (num_blocks + block_size - 1) / block_size; |
624 | |
|
625 | 0 | buf = bfd_malloc (block_size); |
626 | 0 | if (!buf) |
627 | 0 | return false; |
628 | | |
629 | 0 | num_blocks--; /* Superblock not included. */ |
630 | |
|
631 | 0 | for (uint32_t i = 0; i < num_intervals; i++) |
632 | 0 | { |
633 | 0 | if (bfd_seek (abfd, ((i * block_size) + 1) * block_size, SEEK_SET)) |
634 | 0 | { |
635 | 0 | free (buf); |
636 | 0 | return false; |
637 | 0 | } |
638 | | |
639 | | /* All of our blocks are contiguous, making our free block map simple. |
640 | | 0 = used, 1 = free. */ |
641 | | |
642 | 0 | if (num_blocks >= 8) |
643 | 0 | memset (buf, 0, |
644 | 0 | (num_blocks / 8) > |
645 | 0 | block_size ? block_size : (num_blocks / 8)); |
646 | |
|
647 | 0 | if (num_blocks < block_size * 8) |
648 | 0 | { |
649 | 0 | unsigned int off = num_blocks / 8; |
650 | |
|
651 | 0 | if (num_blocks % 8) |
652 | 0 | { |
653 | 0 | buf[off] = (1 << (8 - (num_blocks % 8))) - 1; |
654 | 0 | off++; |
655 | 0 | } |
656 | |
|
657 | 0 | if (off < block_size) |
658 | 0 | memset (buf + off, 0xff, block_size - off); |
659 | 0 | } |
660 | |
|
661 | 0 | if (num_blocks < block_size * 8) |
662 | 0 | num_blocks = 0; |
663 | 0 | else |
664 | 0 | num_blocks -= block_size * 8; |
665 | |
|
666 | 0 | if (bfd_write (buf, block_size, abfd) != block_size) |
667 | 0 | return false; |
668 | 0 | } |
669 | | |
670 | 0 | free (buf); |
671 | |
|
672 | 0 | return true; |
673 | 0 | } |
674 | | |
675 | | static bool |
676 | | pdb_write_contents (bfd *abfd) |
677 | 0 | { |
678 | 0 | char tmp[sizeof (uint32_t)]; |
679 | 0 | const uint32_t block_size = 0x400; |
680 | 0 | uint32_t block_map_addr; |
681 | 0 | uint32_t num_blocks; |
682 | 0 | uint32_t num_files = 0; |
683 | 0 | uint32_t num_directory_bytes = sizeof (uint32_t); |
684 | 0 | bfd *arelt; |
685 | |
|
686 | 0 | if (bfd_write (pdb_magic, sizeof (pdb_magic), abfd) != sizeof (pdb_magic)) |
687 | 0 | return false; |
688 | | |
689 | 0 | bfd_putl32 (block_size, tmp); |
690 | |
|
691 | 0 | if (bfd_write (tmp, sizeof (uint32_t), abfd) != sizeof (uint32_t)) |
692 | 0 | return false; |
693 | | |
694 | 0 | bfd_putl32 (1, tmp); /* Free block map block (always either 1 or 2). */ |
695 | |
|
696 | 0 | if (bfd_write (tmp, sizeof (uint32_t), abfd) != sizeof (uint32_t)) |
697 | 0 | return false; |
698 | | |
699 | 0 | arelt = abfd->archive_head; |
700 | |
|
701 | 0 | while (arelt) |
702 | 0 | { |
703 | 0 | uint32_t blocks_required = |
704 | 0 | (bfd_get_size (arelt) + block_size - 1) / block_size; |
705 | |
|
706 | 0 | num_directory_bytes += sizeof (uint32_t); /* Size. */ |
707 | 0 | num_directory_bytes += blocks_required * sizeof (uint32_t); /* Blocks. */ |
708 | |
|
709 | 0 | num_files++; |
710 | |
|
711 | 0 | arelt = arelt->archive_next; |
712 | 0 | } |
713 | | |
714 | | /* Superblock plus two bitmap blocks. */ |
715 | 0 | num_blocks = 3; |
716 | | |
717 | | /* Skip num_blocks for now. */ |
718 | 0 | if (bfd_seek (abfd, sizeof (uint32_t), SEEK_CUR)) |
719 | 0 | return false; |
720 | | |
721 | 0 | bfd_putl32 (num_directory_bytes, tmp); |
722 | |
|
723 | 0 | if (bfd_write (tmp, sizeof (uint32_t), abfd) != sizeof (uint32_t)) |
724 | 0 | return false; |
725 | | |
726 | | /* Skip unknown uint32_t (always 0?). */ |
727 | 0 | if (bfd_seek (abfd, sizeof (uint32_t), SEEK_CUR)) |
728 | 0 | return false; |
729 | | |
730 | 0 | block_map_addr = pdb_allocate_block (&num_blocks, block_size); |
731 | |
|
732 | 0 | bfd_putl32 (block_map_addr, tmp); |
733 | |
|
734 | 0 | if (bfd_write (tmp, sizeof (uint32_t), abfd) != sizeof (uint32_t)) |
735 | 0 | return false; |
736 | | |
737 | 0 | if (!pdb_write_directory |
738 | 0 | (abfd, block_size, num_files, block_map_addr, &num_blocks)) |
739 | 0 | return false; |
740 | | |
741 | 0 | if (!pdb_write_bitmap (abfd, block_size, num_blocks)) |
742 | 0 | return false; |
743 | | |
744 | | /* Write num_blocks now we know it. */ |
745 | | |
746 | 0 | if (bfd_seek |
747 | 0 | (abfd, sizeof (pdb_magic) + sizeof (uint32_t) + sizeof (uint32_t), |
748 | 0 | SEEK_SET)) |
749 | 0 | return false; |
750 | | |
751 | 0 | bfd_putl32 (num_blocks, tmp); |
752 | |
|
753 | 0 | if (bfd_write (tmp, sizeof (uint32_t), abfd) != sizeof (uint32_t)) |
754 | 0 | return false; |
755 | | |
756 | 0 | return true; |
757 | 0 | } |
758 | | |
759 | | #define pdb_bfd_free_cached_info _bfd_generic_bfd_free_cached_info |
760 | | #define pdb_new_section_hook _bfd_generic_new_section_hook |
761 | | #define pdb_get_section_contents _bfd_generic_get_section_contents |
762 | | #define pdb_get_section_contents_in_window _bfd_generic_get_section_contents_in_window |
763 | | #define pdb_close_and_cleanup _bfd_generic_close_and_cleanup |
764 | | |
765 | | #define pdb_slurp_armap _bfd_noarchive_slurp_armap |
766 | | #define pdb_slurp_extended_name_table _bfd_noarchive_slurp_extended_name_table |
767 | | #define pdb_construct_extended_name_table _bfd_noarchive_construct_extended_name_table |
768 | | #define pdb_truncate_arname _bfd_noarchive_truncate_arname |
769 | | #define pdb_write_armap _bfd_noarchive_write_armap |
770 | | #define pdb_read_ar_hdr _bfd_noarchive_read_ar_hdr |
771 | | #define pdb_write_ar_hdr _bfd_noarchive_write_ar_hdr |
772 | | #define pdb_update_armap_timestamp _bfd_noarchive_update_armap_timestamp |
773 | | |
774 | | const bfd_target pdb_vec = |
775 | | { |
776 | | "pdb", |
777 | | bfd_target_unknown_flavour, |
778 | | BFD_ENDIAN_LITTLE, /* target byte order */ |
779 | | BFD_ENDIAN_LITTLE, /* target headers byte order */ |
780 | | 0, /* object flags */ |
781 | | 0, /* section flags */ |
782 | | 0, /* leading underscore */ |
783 | | ' ', /* ar_pad_char */ |
784 | | 16, /* ar_max_namelen */ |
785 | | 0, /* match priority. */ |
786 | | TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */ |
787 | | bfd_getl64, bfd_getl_signed_64, bfd_putl64, |
788 | | bfd_getl32, bfd_getl_signed_32, bfd_putl32, |
789 | | bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* Data. */ |
790 | | bfd_getl64, bfd_getl_signed_64, bfd_putl64, |
791 | | bfd_getl32, bfd_getl_signed_32, bfd_putl32, |
792 | | bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* Hdrs. */ |
793 | | |
794 | | { /* bfd_check_format */ |
795 | | _bfd_dummy_target, |
796 | | _bfd_dummy_target, |
797 | | pdb_archive_p, |
798 | | _bfd_dummy_target |
799 | | }, |
800 | | { /* bfd_set_format */ |
801 | | _bfd_bool_bfd_false_error, |
802 | | _bfd_bool_bfd_false_error, |
803 | | _bfd_bool_bfd_true, |
804 | | _bfd_bool_bfd_false_error |
805 | | }, |
806 | | { /* bfd_write_contents */ |
807 | | _bfd_bool_bfd_true, |
808 | | _bfd_bool_bfd_false_error, |
809 | | pdb_write_contents, |
810 | | _bfd_bool_bfd_false_error |
811 | | }, |
812 | | |
813 | | BFD_JUMP_TABLE_GENERIC (pdb), |
814 | | BFD_JUMP_TABLE_COPY (_bfd_generic), |
815 | | BFD_JUMP_TABLE_CORE (_bfd_nocore), |
816 | | BFD_JUMP_TABLE_ARCHIVE (pdb), |
817 | | BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols), |
818 | | BFD_JUMP_TABLE_RELOCS (_bfd_norelocs), |
819 | | BFD_JUMP_TABLE_WRITE (_bfd_generic), |
820 | | BFD_JUMP_TABLE_LINK (_bfd_nolink), |
821 | | BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic), |
822 | | |
823 | | NULL, |
824 | | |
825 | | NULL |
826 | | }; |