/src/binutils-gdb/bfd/opncls.c
Line | Count | Source |
1 | | /* opncls.c -- open and close a BFD. |
2 | | Copyright (C) 1990-2026 Free Software Foundation, Inc. |
3 | | |
4 | | Written by Cygnus Support. |
5 | | |
6 | | This file is part of BFD, the Binary File Descriptor library. |
7 | | |
8 | | This program is free software; you can redistribute it and/or modify |
9 | | it under the terms of the GNU General Public License as published by |
10 | | the Free Software Foundation; either version 3 of the License, or |
11 | | (at your option) any later version. |
12 | | |
13 | | This program is distributed in the hope that it will be useful, |
14 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | GNU General Public License for more details. |
17 | | |
18 | | You should have received a copy of the GNU General Public License |
19 | | along with this program; if not, write to the Free Software |
20 | | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
21 | | MA 02110-1301, USA. */ |
22 | | |
23 | | #include "sysdep.h" |
24 | | #include "bfd.h" |
25 | | #include "objalloc.h" |
26 | | #include "libbfd.h" |
27 | | #include "libiberty.h" |
28 | | #include "elf-bfd.h" |
29 | | |
30 | | #ifndef S_IXUSR |
31 | | #define S_IXUSR 0100 /* Execute by owner. */ |
32 | | #endif |
33 | | #ifndef S_IXGRP |
34 | | #define S_IXGRP 0010 /* Execute by group. */ |
35 | | #endif |
36 | | #ifndef S_IXOTH |
37 | | #define S_IXOTH 0001 /* Execute by others. */ |
38 | | #endif |
39 | | |
40 | | /* |
41 | | SECTION |
42 | | Opening and closing BFDs |
43 | | |
44 | | SUBSECTION |
45 | | Functions for opening and closing |
46 | | */ |
47 | | |
48 | | /* Counter used to initialize the unique bfd identifier. */ |
49 | | |
50 | | static unsigned int bfd_id_counter = 0; |
51 | | |
52 | | /* fdopen is a loser -- we should use stdio exclusively. Unfortunately |
53 | | if we do that we can't use fcntl. */ |
54 | | |
55 | | /* |
56 | | INTERNAL_FUNCTION |
57 | | _bfd_new_bfd |
58 | | |
59 | | SYNOPSIS |
60 | | bfd *_bfd_new_bfd (void); |
61 | | |
62 | | DESCRIPTION |
63 | | Return a new BFD. All BFD's are allocated through this routine. |
64 | | */ |
65 | | |
66 | | bfd * |
67 | | _bfd_new_bfd (void) |
68 | 9.35M | { |
69 | 9.35M | bfd *nbfd; |
70 | | |
71 | 9.35M | nbfd = (bfd *) bfd_zmalloc (sizeof (bfd)); |
72 | 9.35M | if (nbfd == NULL) |
73 | 0 | return NULL; |
74 | | |
75 | 9.35M | if (!bfd_lock ()) |
76 | 0 | goto loser; |
77 | 9.35M | nbfd->id = bfd_id_counter++; |
78 | 9.35M | if (!bfd_unlock ()) |
79 | 0 | goto loser; |
80 | | |
81 | 9.35M | nbfd->memory = objalloc_create (); |
82 | 9.35M | if (nbfd->memory == NULL) |
83 | 0 | { |
84 | 0 | bfd_set_error (bfd_error_no_memory); |
85 | 0 | goto loser; |
86 | 0 | } |
87 | | |
88 | 9.35M | nbfd->arch_info = &bfd_default_arch_struct; |
89 | | |
90 | 9.35M | if (!bfd_hash_table_init_n (& nbfd->section_htab, bfd_section_hash_newfunc, |
91 | 9.35M | sizeof (struct section_hash_entry), 13)) |
92 | 0 | { |
93 | 0 | objalloc_free (nbfd->memory); |
94 | 0 | goto loser; |
95 | 0 | } |
96 | | |
97 | 9.35M | nbfd->archive_plugin_fd = -1; |
98 | 9.35M | return nbfd; |
99 | | |
100 | 0 | loser: |
101 | 0 | free (nbfd); |
102 | 0 | return NULL; |
103 | 9.35M | } |
104 | | |
105 | | static const struct bfd_iovec opncls_iovec; |
106 | | |
107 | | /* |
108 | | INTERNAL_FUNCTION |
109 | | _bfd_new_bfd_contained_in |
110 | | |
111 | | SYNOPSIS |
112 | | bfd *_bfd_new_bfd_contained_in (bfd *); |
113 | | |
114 | | DESCRIPTION |
115 | | Allocate a new BFD as a member of archive OBFD. |
116 | | */ |
117 | | |
118 | | bfd * |
119 | | _bfd_new_bfd_contained_in (bfd *obfd) |
120 | 9.17M | { |
121 | 9.17M | bfd *nbfd; |
122 | | |
123 | | /* Nested archives in bims are unsupported. */ |
124 | 9.17M | if ((obfd->flags & BFD_IN_MEMORY) != 0) |
125 | 0 | { |
126 | 0 | bfd_set_error (bfd_error_malformed_archive); |
127 | 0 | return NULL; |
128 | 0 | } |
129 | 9.17M | nbfd = _bfd_new_bfd (); |
130 | 9.17M | if (nbfd == NULL) |
131 | 0 | return NULL; |
132 | 9.17M | nbfd->xvec = obfd->xvec; |
133 | 9.17M | nbfd->iovec = obfd->iovec; |
134 | 9.17M | if (obfd->iovec == &opncls_iovec) |
135 | 0 | nbfd->iostream = obfd->iostream; |
136 | 9.17M | nbfd->my_archive = obfd; |
137 | 9.17M | nbfd->direction = read_direction; |
138 | 9.17M | nbfd->target_defaulted = obfd->target_defaulted; |
139 | 9.17M | nbfd->lto_output = obfd->lto_output; |
140 | 9.17M | nbfd->no_export = obfd->no_export; |
141 | 9.17M | return nbfd; |
142 | 9.17M | } |
143 | | |
144 | | /* Delete a BFD. */ |
145 | | |
146 | | static void |
147 | | _bfd_delete_bfd (bfd *abfd) |
148 | 9.35M | { |
149 | | /* Give the target _bfd_free_cached_info a chance to free memory. */ |
150 | 9.35M | if (abfd->memory && abfd->xvec) |
151 | 9.35M | bfd_free_cached_info (abfd); |
152 | | |
153 | | /* The target _bfd_free_cached_info may not have done anything.. */ |
154 | 9.35M | bfd_hash_table_free (&abfd->section_htab); |
155 | 9.35M | if (abfd->memory) |
156 | 9.35M | objalloc_free (abfd->memory); |
157 | | |
158 | 9.35M | #ifdef USE_MMAP |
159 | 9.35M | struct bfd_mmapped *mmapped, *next; |
160 | 9.35M | for (mmapped = abfd->mmapped; mmapped != NULL; mmapped = next) |
161 | 1 | { |
162 | 1 | struct bfd_mmapped_entry *entries = mmapped->entries; |
163 | 1 | next = mmapped->next; |
164 | 7 | for (unsigned int i = 0; i < mmapped->next_entry; i++) |
165 | 6 | munmap (entries[i].addr, entries[i].size); |
166 | 1 | munmap (mmapped, _bfd_pagesize); |
167 | 1 | } |
168 | 9.35M | #endif |
169 | | |
170 | 9.35M | free (abfd->arelt_data); |
171 | 9.35M | free (abfd); |
172 | 9.35M | } |
173 | | |
174 | | /* |
175 | | INTERNAL_FUNCTION |
176 | | _bfd_free_cached_info |
177 | | |
178 | | SYNOPSIS |
179 | | bool _bfd_free_cached_info (bfd *); |
180 | | |
181 | | DESCRIPTION |
182 | | Free objalloc memory. |
183 | | */ |
184 | | |
185 | | bool |
186 | | _bfd_free_cached_info (bfd *abfd) |
187 | 9.07M | { |
188 | 9.07M | bfd_hash_table_free (&abfd->section_htab); |
189 | | |
190 | 9.07M | abfd->sections = NULL; |
191 | 9.07M | abfd->section_last = NULL; |
192 | 9.07M | abfd->section_count = 0; |
193 | 9.07M | abfd->outsymbols = NULL; |
194 | 9.07M | abfd->tdata.any = NULL; |
195 | 9.07M | abfd->usrdata = NULL; |
196 | 9.07M | abfd->format = bfd_unknown; |
197 | | |
198 | 9.07M | return true; |
199 | 9.07M | } |
200 | | |
201 | | /* |
202 | | FUNCTION |
203 | | bfd_fopen |
204 | | |
205 | | SYNOPSIS |
206 | | bfd *bfd_fopen (const char *filename, const char *target, |
207 | | const char *mode, int fd); |
208 | | |
209 | | DESCRIPTION |
210 | | Open the file @var{filename} with the target @var{target}. |
211 | | Return a pointer to the created BFD. If @var{fd} is not -1, |
212 | | then <<fdopen>> is used to open the file; otherwise, <<fopen>> |
213 | | is used. @var{mode} is passed directly to <<fopen>> or |
214 | | <<fdopen>>. |
215 | | |
216 | | Calls <<bfd_find_target>>, so @var{target} is interpreted as by |
217 | | that function. |
218 | | |
219 | | The new BFD is marked as cacheable iff @var{fd} is -1. |
220 | | |
221 | | If <<NULL>> is returned then an error has occured. Possible errors |
222 | | are <<bfd_error_no_memory>>, <<bfd_error_invalid_target>> or |
223 | | <<system_call>> error. |
224 | | |
225 | | On error, @var{fd} is always closed. |
226 | | |
227 | | A copy of the @var{filename} argument is stored in the newly created |
228 | | BFD. It can be accessed via the bfd_get_filename() macro. |
229 | | */ |
230 | | |
231 | | bfd * |
232 | | bfd_fopen (const char *filename, const char *target, const char *mode, int fd) |
233 | 151k | { |
234 | 151k | bfd *nbfd; |
235 | 151k | const bfd_target *target_vec; |
236 | | |
237 | 151k | nbfd = _bfd_new_bfd (); |
238 | 151k | if (nbfd == NULL) |
239 | 0 | { |
240 | 0 | if (fd != -1) |
241 | 0 | close (fd); |
242 | 0 | return NULL; |
243 | 0 | } |
244 | | |
245 | 151k | target_vec = bfd_find_target (target, nbfd); |
246 | 151k | if (target_vec == NULL) |
247 | 27 | { |
248 | 27 | if (fd != -1) |
249 | 0 | close (fd); |
250 | 27 | _bfd_delete_bfd (nbfd); |
251 | 27 | return NULL; |
252 | 27 | } |
253 | | |
254 | 151k | #ifdef HAVE_FDOPEN |
255 | 151k | if (fd != -1) |
256 | 21.1k | nbfd->iostream = fdopen (fd, mode); |
257 | 129k | else |
258 | 129k | #endif |
259 | 129k | nbfd->iostream = _bfd_real_fopen (filename, mode); |
260 | 151k | if (nbfd->iostream == NULL) |
261 | 2.16k | { |
262 | 2.16k | bfd_set_error (bfd_error_system_call); |
263 | 2.16k | if (fd != -1) |
264 | 0 | close (fd); |
265 | 2.16k | _bfd_delete_bfd (nbfd); |
266 | 2.16k | return NULL; |
267 | 2.16k | } |
268 | | |
269 | | /* OK, put everything where it belongs. */ |
270 | | |
271 | | /* PR 11983: Do not cache the original filename, but |
272 | | rather make a copy - the original might go away. */ |
273 | 148k | if (!bfd_set_filename (nbfd, filename)) |
274 | 0 | { |
275 | 0 | fclose (nbfd->iostream); |
276 | 0 | _bfd_delete_bfd (nbfd); |
277 | 0 | return NULL; |
278 | 0 | } |
279 | | |
280 | | /* Figure out whether the user is opening the file for reading, |
281 | | writing, or both, by looking at the MODE argument. */ |
282 | 148k | if ((mode[0] == 'r' || mode[0] == 'w' || mode[0] == 'a') |
283 | 148k | && mode[1] == '+') |
284 | 21.1k | nbfd->direction = both_direction; |
285 | 127k | else if (mode[0] == 'r') |
286 | 127k | nbfd->direction = read_direction; |
287 | 0 | else |
288 | 0 | nbfd->direction = write_direction; |
289 | | |
290 | 148k | if (!bfd_cache_init (nbfd)) |
291 | 0 | { |
292 | 0 | fclose (nbfd->iostream); |
293 | 0 | _bfd_delete_bfd (nbfd); |
294 | 0 | return NULL; |
295 | 0 | } |
296 | 148k | nbfd->opened_once = true; |
297 | | |
298 | | /* If we opened the file by name, mark it cacheable; we can close it |
299 | | and reopen it later. However, if a file descriptor was provided, |
300 | | then it may have been opened with special flags that make it |
301 | | unsafe to close and reopen the file. */ |
302 | 148k | if (fd == -1) |
303 | 127k | (void) bfd_set_cacheable (nbfd, true); |
304 | | |
305 | 148k | return nbfd; |
306 | 148k | } |
307 | | |
308 | | /* |
309 | | FUNCTION |
310 | | bfd_openr |
311 | | |
312 | | SYNOPSIS |
313 | | bfd *bfd_openr (const char *filename, const char *target); |
314 | | |
315 | | DESCRIPTION |
316 | | Open the file @var{filename} (using <<fopen>>) with the target |
317 | | @var{target}. Return a pointer to the created BFD. |
318 | | |
319 | | Calls <<bfd_find_target>>, so @var{target} is interpreted as by |
320 | | that function. |
321 | | |
322 | | If <<NULL>> is returned then an error has occured. Possible errors |
323 | | are <<bfd_error_no_memory>>, <<bfd_error_invalid_target>> or |
324 | | <<system_call>> error. |
325 | | |
326 | | A copy of the @var{filename} argument is stored in the newly created |
327 | | BFD. It can be accessed via the bfd_get_filename() macro. |
328 | | */ |
329 | | |
330 | | bfd * |
331 | | bfd_openr (const char *filename, const char *target) |
332 | 130k | { |
333 | 130k | return bfd_fopen (filename, target, FOPEN_RB, -1); |
334 | 130k | } |
335 | | |
336 | | /* Don't try to `optimize' this function: |
337 | | |
338 | | o - We lock using stack space so that interrupting the locking |
339 | | won't cause a storage leak. |
340 | | o - We open the file stream last, since we don't want to have to |
341 | | close it if anything goes wrong. Closing the stream means closing |
342 | | the file descriptor too, even though we didn't open it. */ |
343 | | /* |
344 | | FUNCTION |
345 | | bfd_fdopenr |
346 | | |
347 | | SYNOPSIS |
348 | | bfd *bfd_fdopenr (const char *filename, const char *target, int fd); |
349 | | |
350 | | DESCRIPTION |
351 | | <<bfd_fdopenr>> is to <<bfd_fopenr>> much like <<fdopen>> is to |
352 | | <<fopen>>. It opens a BFD on a file already described by the |
353 | | @var{fd} supplied. |
354 | | |
355 | | When the file is later <<bfd_close>>d, the file descriptor will |
356 | | be closed. If the caller desires that this file descriptor be |
357 | | cached by BFD (opened as needed, closed as needed to free |
358 | | descriptors for other opens), with the supplied @var{fd} used as |
359 | | an initial file descriptor (but subject to closure at any time), |
360 | | call bfd_set_cacheable(bfd, 1) on the returned BFD. The default |
361 | | is to assume no caching; the file descriptor will remain open |
362 | | until <<bfd_close>>, and will not be affected by BFD operations |
363 | | on other files. |
364 | | |
365 | | Possible errors are <<bfd_error_no_memory>>, |
366 | | <<bfd_error_invalid_target>> and <<bfd_error_system_call>>. |
367 | | |
368 | | On error, @var{fd} is closed. |
369 | | |
370 | | A copy of the @var{filename} argument is stored in the newly created |
371 | | BFD. It can be accessed via the bfd_get_filename() macro. |
372 | | */ |
373 | | |
374 | | bfd * |
375 | | bfd_fdopenr (const char *filename, const char *target, int fd) |
376 | 21.1k | { |
377 | 21.1k | const char *mode; |
378 | 21.1k | #if defined(HAVE_FCNTL) && defined(F_GETFL) |
379 | 21.1k | int fdflags; |
380 | 21.1k | #endif |
381 | | |
382 | | #if ! defined(HAVE_FCNTL) || ! defined(F_GETFL) |
383 | | mode = FOPEN_RUB; /* Assume full access. */ |
384 | | #else |
385 | 21.1k | fdflags = fcntl (fd, F_GETFL, NULL); |
386 | 21.1k | if (fdflags == -1) |
387 | 0 | { |
388 | 0 | int save = errno; |
389 | |
|
390 | 0 | close (fd); |
391 | 0 | errno = save; |
392 | 0 | bfd_set_error (bfd_error_system_call); |
393 | 0 | return NULL; |
394 | 0 | } |
395 | | |
396 | | /* (O_ACCMODE) parens are to avoid Ultrix header file bug. */ |
397 | 21.1k | switch (fdflags & (O_ACCMODE)) |
398 | 21.1k | { |
399 | 0 | case O_RDONLY: mode = FOPEN_RB; break; |
400 | 0 | case O_WRONLY: mode = FOPEN_RUB; break; |
401 | 21.1k | case O_RDWR: mode = FOPEN_RUB; break; |
402 | 0 | default: abort (); |
403 | 21.1k | } |
404 | 21.1k | #endif |
405 | | |
406 | 21.1k | return bfd_fopen (filename, target, mode, fd); |
407 | 21.1k | } |
408 | | |
409 | | /* |
410 | | FUNCTION |
411 | | bfd_fdopenw |
412 | | |
413 | | SYNOPSIS |
414 | | bfd *bfd_fdopenw (const char *filename, const char *target, int fd); |
415 | | |
416 | | DESCRIPTION |
417 | | <<bfd_fdopenw>> is exactly like <<bfd_fdopenr>> with the exception that |
418 | | the resulting BFD is suitable for output. |
419 | | */ |
420 | | |
421 | | bfd * |
422 | | bfd_fdopenw (const char *filename, const char *target, int fd) |
423 | 0 | { |
424 | 0 | bfd *out = bfd_fdopenr (filename, target, fd); |
425 | |
|
426 | 0 | if (out != NULL) |
427 | 0 | { |
428 | 0 | if (!bfd_write_p (out)) |
429 | 0 | { |
430 | 0 | close (fd); |
431 | 0 | _bfd_delete_bfd (out); |
432 | 0 | out = NULL; |
433 | 0 | bfd_set_error (bfd_error_invalid_operation); |
434 | 0 | } |
435 | 0 | else |
436 | 0 | out->direction = write_direction; |
437 | 0 | } |
438 | |
|
439 | 0 | return out; |
440 | 0 | } |
441 | | |
442 | | /* |
443 | | FUNCTION |
444 | | bfd_openstreamr |
445 | | |
446 | | SYNOPSIS |
447 | | bfd *bfd_openstreamr (const char * filename, const char * target, |
448 | | void * stream); |
449 | | |
450 | | DESCRIPTION |
451 | | Open a BFD for read access on an existing stdio stream. When |
452 | | the BFD is passed to <<bfd_close>>, the stream will be closed. |
453 | | |
454 | | A copy of the @var{filename} argument is stored in the newly created |
455 | | BFD. It can be accessed via the bfd_get_filename() macro. |
456 | | */ |
457 | | |
458 | | bfd * |
459 | | bfd_openstreamr (const char *filename, const char *target, void *streamarg) |
460 | 0 | { |
461 | 0 | FILE *stream = (FILE *) streamarg; |
462 | 0 | bfd *nbfd; |
463 | 0 | const bfd_target *target_vec; |
464 | |
|
465 | 0 | nbfd = _bfd_new_bfd (); |
466 | 0 | if (nbfd == NULL) |
467 | 0 | return NULL; |
468 | | |
469 | 0 | target_vec = bfd_find_target (target, nbfd); |
470 | 0 | if (target_vec == NULL) |
471 | 0 | { |
472 | 0 | _bfd_delete_bfd (nbfd); |
473 | 0 | return NULL; |
474 | 0 | } |
475 | | |
476 | 0 | nbfd->iostream = stream; |
477 | | /* PR 11983: Do not cache the original filename, but |
478 | | rather make a copy - the original might go away. */ |
479 | 0 | if (!bfd_set_filename (nbfd, filename)) |
480 | 0 | { |
481 | 0 | _bfd_delete_bfd (nbfd); |
482 | 0 | return NULL; |
483 | 0 | } |
484 | 0 | nbfd->direction = read_direction; |
485 | |
|
486 | 0 | if (! bfd_cache_init (nbfd)) |
487 | 0 | { |
488 | 0 | _bfd_delete_bfd (nbfd); |
489 | 0 | return NULL; |
490 | 0 | } |
491 | | |
492 | 0 | return nbfd; |
493 | 0 | } |
494 | | |
495 | | /* |
496 | | FUNCTION |
497 | | bfd_openr_fake_archive |
498 | | |
499 | | SYNOPSIS |
500 | | bfd *bfd_openr_fake_archive (bfd *fbfd); |
501 | | |
502 | | DESCRIPTION |
503 | | Open a list of BFDs starting from @var{fbfd} as an artificial |
504 | | archive. Subsequent members of the archive are indicated by each |
505 | | BFD's proxy_handle.abfd member, with the final one holding a NULL |
506 | | pointer there. The newly opened archive will necessarily also be |
507 | | a thin archive as there will be member files only to refer to and |
508 | | no containing archive file. |
509 | | */ |
510 | | |
511 | | bfd * |
512 | | bfd_openr_fake_archive (bfd *fbfd) |
513 | 0 | { |
514 | 0 | bfd *abfd, *nbfd; |
515 | |
|
516 | 0 | if (fbfd == NULL) |
517 | 0 | return NULL; |
518 | | |
519 | 0 | nbfd = _bfd_new_bfd (); |
520 | 0 | if (nbfd == NULL) |
521 | 0 | return NULL; |
522 | | |
523 | 0 | nbfd->xvec = fbfd->xvec; |
524 | 0 | bfd_set_format (nbfd, bfd_archive); |
525 | 0 | bfd_set_thin_archive (nbfd, true); |
526 | 0 | bfd_set_fake_archive (nbfd, true); |
527 | 0 | bfd_ardata (nbfd)->first_file.abfd = fbfd; |
528 | 0 | nbfd->direction = read_direction; |
529 | |
|
530 | 0 | for (abfd = fbfd; abfd != NULL; abfd = abfd->proxy_handle.abfd) |
531 | 0 | abfd->my_archive = nbfd; |
532 | |
|
533 | 0 | return nbfd; |
534 | 0 | } |
535 | | |
536 | | /* |
537 | | FUNCTION |
538 | | bfd_openr_iovec |
539 | | |
540 | | SYNOPSIS |
541 | | bfd *bfd_openr_iovec (const char *filename, const char *target, |
542 | | void *(*open_func) (struct bfd *nbfd, |
543 | | void *open_closure), |
544 | | void *open_closure, |
545 | | file_ptr (*pread_func) (struct bfd *nbfd, |
546 | | void *stream, |
547 | | void *buf, |
548 | | file_ptr nbytes, |
549 | | file_ptr offset), |
550 | | int (*close_func) (struct bfd *nbfd, |
551 | | void *stream), |
552 | | int (*stat_func) (struct bfd *abfd, |
553 | | void *stream, |
554 | | struct stat *sb)); |
555 | | |
556 | | DESCRIPTION |
557 | | Create and return a BFD backed by a read-only @var{stream}. |
558 | | The @var{stream} is created using @var{open_func}, accessed using |
559 | | @var{pread_func} and destroyed using @var{close_func}. |
560 | | |
561 | | Calls <<bfd_find_target>>, so @var{target} is interpreted as by |
562 | | that function. |
563 | | |
564 | | Calls @var{open_func} (which can call <<bfd_zalloc>> and |
565 | | <<bfd_get_filename>>) to obtain the read-only stream backing |
566 | | the BFD. @var{open_func} either succeeds returning the |
567 | | non-<<NULL>> @var{stream}, or fails returning <<NULL>> |
568 | | (setting <<bfd_error>>). |
569 | | |
570 | | Calls @var{pread_func} to request @var{nbytes} of data from |
571 | | @var{stream} starting at @var{offset} (e.g., via a call to |
572 | | <<bfd_read>>). @var{pread_func} either succeeds returning the |
573 | | number of bytes read (which can be less than @var{nbytes} when |
574 | | end-of-file), or fails returning -1 (setting <<bfd_error>>). |
575 | | |
576 | | Calls @var{close_func} when the BFD is later closed using |
577 | | <<bfd_close>>. @var{close_func} either succeeds returning 0, or |
578 | | fails returning -1 (setting <<bfd_error>>). |
579 | | |
580 | | Calls @var{stat_func} to fill in a stat structure for bfd_stat, |
581 | | bfd_get_size, and bfd_get_mtime calls. @var{stat_func} returns 0 |
582 | | on success, or returns -1 on failure (setting <<bfd_error>>). |
583 | | |
584 | | If <<bfd_openr_iovec>> returns <<NULL>> then an error has |
585 | | occurred. Possible errors are <<bfd_error_no_memory>>, |
586 | | <<bfd_error_invalid_target>> and <<bfd_error_system_call>>. |
587 | | |
588 | | A copy of the @var{filename} argument is stored in the newly created |
589 | | BFD. It can be accessed via the bfd_get_filename() macro. |
590 | | */ |
591 | | |
592 | | struct opncls |
593 | | { |
594 | | void *stream; |
595 | | file_ptr (*pread) (struct bfd *abfd, void *stream, void *buf, |
596 | | file_ptr nbytes, file_ptr offset); |
597 | | int (*close) (struct bfd *abfd, void *stream); |
598 | | int (*stat) (struct bfd *abfd, void *stream, struct stat *sb); |
599 | | file_ptr where; |
600 | | }; |
601 | | |
602 | | static file_ptr |
603 | | opncls_btell (struct bfd *abfd) |
604 | 0 | { |
605 | 0 | struct opncls *vec = (struct opncls *) abfd->iostream; |
606 | 0 | return vec->where; |
607 | 0 | } |
608 | | |
609 | | static int |
610 | | opncls_bseek (struct bfd *abfd, file_ptr offset, int whence) |
611 | 0 | { |
612 | 0 | struct opncls *vec = (struct opncls *) abfd->iostream; |
613 | 0 | switch (whence) |
614 | 0 | { |
615 | 0 | case SEEK_SET: vec->where = offset; break; |
616 | 0 | case SEEK_CUR: vec->where += offset; break; |
617 | 0 | case SEEK_END: return -1; |
618 | 0 | } |
619 | 0 | return 0; |
620 | 0 | } |
621 | | |
622 | | static file_ptr |
623 | | opncls_bread (struct bfd *abfd, void *buf, file_ptr nbytes) |
624 | 0 | { |
625 | 0 | struct opncls *vec = (struct opncls *) abfd->iostream; |
626 | 0 | file_ptr nread = (vec->pread) (abfd, vec->stream, buf, nbytes, vec->where); |
627 | |
|
628 | 0 | if (nread < 0) |
629 | 0 | return nread; |
630 | 0 | vec->where += nread; |
631 | 0 | return nread; |
632 | 0 | } |
633 | | |
634 | | static file_ptr |
635 | | opncls_bwrite (struct bfd *abfd ATTRIBUTE_UNUSED, |
636 | | const void *where ATTRIBUTE_UNUSED, |
637 | | file_ptr nbytes ATTRIBUTE_UNUSED) |
638 | 0 | { |
639 | 0 | return -1; |
640 | 0 | } |
641 | | |
642 | | static int |
643 | | opncls_bclose (struct bfd *abfd) |
644 | 0 | { |
645 | 0 | struct opncls *vec = (struct opncls *) abfd->iostream; |
646 | | /* Since the VEC's memory is bound to the bfd deleting the bfd will |
647 | | free it. */ |
648 | 0 | int status = 0; |
649 | |
|
650 | 0 | if (vec->close != NULL) |
651 | 0 | status = (vec->close) (abfd, vec->stream); |
652 | 0 | abfd->iostream = NULL; |
653 | 0 | return status; |
654 | 0 | } |
655 | | |
656 | | static int |
657 | | opncls_bflush (struct bfd *abfd ATTRIBUTE_UNUSED) |
658 | 0 | { |
659 | 0 | return 0; |
660 | 0 | } |
661 | | |
662 | | static int |
663 | | opncls_bstat (struct bfd *abfd, struct stat *sb) |
664 | 0 | { |
665 | 0 | struct opncls *vec = (struct opncls *) abfd->iostream; |
666 | |
|
667 | 0 | memset (sb, 0, sizeof (*sb)); |
668 | 0 | if (vec->stat == NULL) |
669 | 0 | return 0; |
670 | | |
671 | 0 | return (vec->stat) (abfd, vec->stream, sb); |
672 | 0 | } |
673 | | |
674 | | static void * |
675 | | opncls_bmmap (struct bfd *abfd ATTRIBUTE_UNUSED, |
676 | | void *addr ATTRIBUTE_UNUSED, |
677 | | size_t len ATTRIBUTE_UNUSED, |
678 | | int prot ATTRIBUTE_UNUSED, |
679 | | int flags ATTRIBUTE_UNUSED, |
680 | | file_ptr offset ATTRIBUTE_UNUSED, |
681 | | void **map_addr ATTRIBUTE_UNUSED, |
682 | | size_t *map_len ATTRIBUTE_UNUSED) |
683 | 0 | { |
684 | 0 | return MAP_FAILED; |
685 | 0 | } |
686 | | |
687 | | static const struct bfd_iovec opncls_iovec = |
688 | | { |
689 | | &opncls_bread, &opncls_bwrite, &opncls_btell, &opncls_bseek, |
690 | | &opncls_bclose, &opncls_bflush, &opncls_bstat, &opncls_bmmap |
691 | | }; |
692 | | |
693 | | bfd * |
694 | | bfd_openr_iovec (const char *filename, const char *target, |
695 | | void *(*open_p) (struct bfd *, void *), |
696 | | void *open_closure, |
697 | | file_ptr (*pread_p) (struct bfd *, void *, void *, |
698 | | file_ptr, file_ptr), |
699 | | int (*close_p) (struct bfd *, void *), |
700 | | int (*stat_p) (struct bfd *, void *, struct stat *)) |
701 | 0 | { |
702 | 0 | bfd *nbfd; |
703 | 0 | const bfd_target *target_vec; |
704 | 0 | struct opncls *vec; |
705 | 0 | void *stream; |
706 | |
|
707 | 0 | nbfd = _bfd_new_bfd (); |
708 | 0 | if (nbfd == NULL) |
709 | 0 | return NULL; |
710 | | |
711 | 0 | target_vec = bfd_find_target (target, nbfd); |
712 | 0 | if (target_vec == NULL) |
713 | 0 | { |
714 | 0 | _bfd_delete_bfd (nbfd); |
715 | 0 | return NULL; |
716 | 0 | } |
717 | | |
718 | | /* PR 11983: Do not cache the original filename, but |
719 | | rather make a copy - the original might go away. */ |
720 | 0 | if (!bfd_set_filename (nbfd, filename)) |
721 | 0 | { |
722 | 0 | _bfd_delete_bfd (nbfd); |
723 | 0 | return NULL; |
724 | 0 | } |
725 | 0 | nbfd->direction = read_direction; |
726 | | |
727 | | /* `open_p (...)' would get expanded by an the open(2) syscall macro. */ |
728 | 0 | stream = (*open_p) (nbfd, open_closure); |
729 | 0 | if (stream == NULL) |
730 | 0 | { |
731 | 0 | _bfd_delete_bfd (nbfd); |
732 | 0 | return NULL; |
733 | 0 | } |
734 | | |
735 | 0 | vec = (struct opncls *) bfd_zalloc (nbfd, sizeof (struct opncls)); |
736 | 0 | vec->stream = stream; |
737 | 0 | vec->pread = pread_p; |
738 | 0 | vec->close = close_p; |
739 | 0 | vec->stat = stat_p; |
740 | |
|
741 | 0 | nbfd->iovec = &opncls_iovec; |
742 | 0 | nbfd->iostream = vec; |
743 | |
|
744 | 0 | return nbfd; |
745 | 0 | } |
746 | | |
747 | | /* bfd_openw -- open for writing. |
748 | | Returns a pointer to a freshly-allocated BFD on success, or NULL. |
749 | | |
750 | | See comment by bfd_fdopenr before you try to modify this function. */ |
751 | | |
752 | | /* |
753 | | FUNCTION |
754 | | bfd_openw |
755 | | |
756 | | SYNOPSIS |
757 | | bfd *bfd_openw (const char *filename, const char *target); |
758 | | |
759 | | DESCRIPTION |
760 | | Create a BFD, associated with file @var{filename}, using the |
761 | | file format @var{target}, and return a pointer to it. |
762 | | |
763 | | Possible errors are <<bfd_error_system_call>>, <<bfd_error_no_memory>>, |
764 | | <<bfd_error_invalid_target>>. |
765 | | |
766 | | A copy of the @var{filename} argument is stored in the newly created |
767 | | BFD. It can be accessed via the bfd_get_filename() macro. |
768 | | */ |
769 | | |
770 | | bfd * |
771 | | bfd_openw (const char *filename, const char *target) |
772 | 24.3k | { |
773 | 24.3k | bfd *nbfd; |
774 | 24.3k | const bfd_target *target_vec; |
775 | | |
776 | | /* nbfd has to point to head of malloc'ed block so that bfd_close may |
777 | | reclaim it correctly. */ |
778 | 24.3k | nbfd = _bfd_new_bfd (); |
779 | 24.3k | if (nbfd == NULL) |
780 | 0 | return NULL; |
781 | | |
782 | 24.3k | target_vec = bfd_find_target (target, nbfd); |
783 | 24.3k | if (target_vec == NULL) |
784 | 0 | { |
785 | 0 | _bfd_delete_bfd (nbfd); |
786 | 0 | return NULL; |
787 | 0 | } |
788 | | |
789 | | /* PR 11983: Do not cache the original filename, but |
790 | | rather make a copy - the original might go away. */ |
791 | 24.3k | if (!bfd_set_filename (nbfd, filename)) |
792 | 0 | { |
793 | 0 | _bfd_delete_bfd (nbfd); |
794 | 0 | return NULL; |
795 | 0 | } |
796 | 24.3k | nbfd->direction = write_direction; |
797 | | |
798 | 24.3k | if (bfd_open_file (nbfd) == NULL) |
799 | 1.19k | { |
800 | | /* File not writeable, etc. */ |
801 | 1.19k | bfd_set_error (bfd_error_system_call); |
802 | 1.19k | _bfd_delete_bfd (nbfd); |
803 | 1.19k | return NULL; |
804 | 1.19k | } |
805 | | |
806 | 23.1k | return nbfd; |
807 | 24.3k | } |
808 | | |
809 | | /* |
810 | | FUNCTION |
811 | | bfd_elf_bfd_from_remote_memory |
812 | | |
813 | | SYNOPSIS |
814 | | bfd *bfd_elf_bfd_from_remote_memory |
815 | | (bfd *templ, bfd_vma ehdr_vma, bfd_size_type size, bfd_vma *loadbasep, |
816 | | int (*target_read_memory) |
817 | | (bfd_vma vma, bfd_byte *myaddr, bfd_size_type len)); |
818 | | |
819 | | DESCRIPTION |
820 | | Create a new BFD as if by bfd_openr. Rather than opening a |
821 | | file, reconstruct an ELF file by reading the segments out of |
822 | | remote memory based on the ELF file header at EHDR_VMA and the |
823 | | ELF program headers it points to. If non-zero, SIZE is the |
824 | | known extent of the object. If not null, *LOADBASEP is filled |
825 | | in with the difference between the VMAs from which the |
826 | | segments were read, and the VMAs the file headers (and hence |
827 | | BFD's idea of each section's VMA) put them at. |
828 | | |
829 | | The function TARGET_READ_MEMORY is called to copy LEN bytes |
830 | | from the remote memory at target address VMA into the local |
831 | | buffer at MYADDR; it should return zero on success or an |
832 | | errno code on failure. TEMPL must be a BFD for an ELF |
833 | | target with the word size and byte order found in the remote |
834 | | memory. |
835 | | */ |
836 | | |
837 | | bfd * |
838 | | bfd_elf_bfd_from_remote_memory |
839 | | (bfd *templ, |
840 | | bfd_vma ehdr_vma, |
841 | | bfd_size_type size, |
842 | | bfd_vma *loadbasep, |
843 | | int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type)) |
844 | 0 | { |
845 | 0 | if (bfd_get_flavour (templ) != bfd_target_elf_flavour) |
846 | 0 | { |
847 | 0 | bfd_set_error (bfd_error_invalid_operation); |
848 | 0 | return NULL; |
849 | 0 | } |
850 | 0 | return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory) |
851 | 0 | (templ, ehdr_vma, size, loadbasep, target_read_memory); |
852 | 0 | } |
853 | | |
854 | | static inline void |
855 | | _maybe_make_executable (bfd * abfd) |
856 | 9.34M | { |
857 | | /* If the file was open for writing and is now executable, |
858 | | make it so. */ |
859 | 9.34M | if (abfd->direction == write_direction |
860 | 26.5k | && (abfd->flags & (EXEC_P | DYNAMIC)) != 0) |
861 | 4.62k | { |
862 | 4.62k | struct stat buf; |
863 | | |
864 | 4.62k | if (stat (bfd_get_filename (abfd), &buf) == 0 |
865 | | /* Do not attempt to change non-regular files. This is |
866 | | here especially for configure scripts and kernel builds |
867 | | which run tests with "ld [...] -o /dev/null". */ |
868 | 4.62k | && S_ISREG(buf.st_mode)) |
869 | 4.62k | { |
870 | 4.62k | unsigned int mask = umask (0); |
871 | | |
872 | 4.62k | umask (mask); |
873 | 4.62k | chmod (bfd_get_filename (abfd), |
874 | 4.62k | (0777 |
875 | 4.62k | & (buf.st_mode | ((S_IXUSR | S_IXGRP | S_IXOTH) &~ mask)))); |
876 | 4.62k | } |
877 | 4.62k | } |
878 | 9.34M | } |
879 | | |
880 | | /* |
881 | | FUNCTION |
882 | | bfd_close |
883 | | |
884 | | SYNOPSIS |
885 | | bool bfd_close (bfd *abfd); |
886 | | |
887 | | DESCRIPTION |
888 | | Close a BFD. If the BFD was open for writing, then pending |
889 | | operations are completed and the file written out and closed. |
890 | | If the created file is executable, then <<chmod>> is called |
891 | | to mark it as such. |
892 | | |
893 | | All memory attached to the BFD is released. |
894 | | |
895 | | The file descriptor associated with the BFD is closed (even |
896 | | if it was passed in to BFD by <<bfd_fdopenr>>). |
897 | | |
898 | | <<TRUE>> is returned if all is ok, otherwise <<FALSE>>. |
899 | | */ |
900 | | |
901 | | bool |
902 | | bfd_close (bfd *abfd) |
903 | 9.32M | { |
904 | 9.32M | bool ret = (!bfd_write_p (abfd) |
905 | 28.9k | || BFD_SEND_FMT (abfd, _bfd_write_contents, (abfd))); |
906 | | |
907 | 9.32M | return bfd_close_all_done (abfd) && ret; |
908 | 9.32M | } |
909 | | |
910 | | /* |
911 | | FUNCTION |
912 | | bfd_close_all_done |
913 | | |
914 | | SYNOPSIS |
915 | | bool bfd_close_all_done (bfd *); |
916 | | |
917 | | DESCRIPTION |
918 | | Close a BFD. Differs from <<bfd_close>> since it does not |
919 | | complete any pending operations. This routine would be used |
920 | | if the application had just used BFD for swapping and didn't |
921 | | want to use any of the writing code. |
922 | | |
923 | | If the created file is executable, then <<chmod>> is called |
924 | | to mark it as such. |
925 | | |
926 | | All memory attached to the BFD is released. |
927 | | |
928 | | <<TRUE>> is returned if all is ok, otherwise <<FALSE>>. |
929 | | */ |
930 | | |
931 | | bool |
932 | | bfd_close_all_done (bfd *abfd) |
933 | 9.34M | { |
934 | 9.34M | bool ret = BFD_SEND (abfd, _close_and_cleanup, (abfd)); |
935 | | |
936 | 9.34M | if (abfd->iovec != NULL) |
937 | 9.34M | ret &= abfd->iovec->bclose (abfd) == 0; |
938 | | |
939 | 9.34M | if (ret) |
940 | 9.34M | _maybe_make_executable (abfd); |
941 | | |
942 | 9.34M | _bfd_delete_bfd (abfd); |
943 | | |
944 | 9.34M | return ret; |
945 | 9.34M | } |
946 | | |
947 | | /* |
948 | | FUNCTION |
949 | | bfd_create |
950 | | |
951 | | SYNOPSIS |
952 | | bfd *bfd_create (const char *filename, bfd *templ); |
953 | | |
954 | | DESCRIPTION |
955 | | Create a new BFD in the manner of <<bfd_openw>>, but without |
956 | | opening a file. The new BFD takes the target from the target |
957 | | used by @var{templ}. The format is always set to <<bfd_object>>. |
958 | | |
959 | | A copy of the @var{filename} argument is stored in the newly created |
960 | | BFD. It can be accessed via the bfd_get_filename() macro. |
961 | | */ |
962 | | |
963 | | bfd * |
964 | | bfd_create (const char *filename, bfd *templ) |
965 | 3.43k | { |
966 | 3.43k | bfd *nbfd; |
967 | | |
968 | 3.43k | nbfd = _bfd_new_bfd (); |
969 | 3.43k | if (nbfd == NULL) |
970 | 0 | return NULL; |
971 | | /* PR 11983: Do not cache the original filename, but |
972 | | rather make a copy - the original might go away. */ |
973 | 3.43k | if (!bfd_set_filename (nbfd, filename)) |
974 | 0 | { |
975 | 0 | _bfd_delete_bfd (nbfd); |
976 | 0 | return NULL; |
977 | 0 | } |
978 | 3.43k | if (templ) |
979 | 3.43k | nbfd->xvec = templ->xvec; |
980 | 3.43k | nbfd->direction = no_direction; |
981 | 3.43k | bfd_set_format (nbfd, bfd_object); |
982 | | |
983 | 3.43k | return nbfd; |
984 | 3.43k | } |
985 | | |
986 | | /* |
987 | | FUNCTION |
988 | | bfd_make_writable |
989 | | |
990 | | SYNOPSIS |
991 | | bool bfd_make_writable (bfd *abfd); |
992 | | |
993 | | DESCRIPTION |
994 | | Takes a BFD as created by <<bfd_create>> and converts it |
995 | | into one like as returned by <<bfd_openw>>. It does this |
996 | | by converting the BFD to BFD_IN_MEMORY. It's assumed that |
997 | | you will call <<bfd_make_readable>> on this bfd later. |
998 | | |
999 | | <<TRUE>> is returned if all is ok, otherwise <<FALSE>>. |
1000 | | */ |
1001 | | |
1002 | | bool |
1003 | | bfd_make_writable (bfd *abfd) |
1004 | 3.43k | { |
1005 | 3.43k | struct bfd_in_memory *bim; |
1006 | | |
1007 | 3.43k | if (abfd->direction != no_direction) |
1008 | 0 | { |
1009 | 0 | bfd_set_error (bfd_error_invalid_operation); |
1010 | 0 | return false; |
1011 | 0 | } |
1012 | | |
1013 | 3.43k | bim = (struct bfd_in_memory *) bfd_malloc (sizeof (struct bfd_in_memory)); |
1014 | 3.43k | if (bim == NULL) |
1015 | 0 | return false; /* bfd_error already set. */ |
1016 | 3.43k | abfd->iostream = bim; |
1017 | | /* bfd_write will grow these as needed. */ |
1018 | 3.43k | bim->size = 0; |
1019 | 3.43k | bim->buffer = 0; |
1020 | | |
1021 | 3.43k | abfd->flags |= BFD_IN_MEMORY; |
1022 | 3.43k | abfd->iovec = &_bfd_memory_iovec; |
1023 | 3.43k | abfd->origin = 0; |
1024 | 3.43k | abfd->direction = write_direction; |
1025 | 3.43k | abfd->where = 0; |
1026 | | |
1027 | 3.43k | return true; |
1028 | 3.43k | } |
1029 | | |
1030 | | /* |
1031 | | FUNCTION |
1032 | | bfd_make_readable |
1033 | | |
1034 | | SYNOPSIS |
1035 | | bool bfd_make_readable (bfd *abfd); |
1036 | | |
1037 | | DESCRIPTION |
1038 | | Takes a BFD as created by <<bfd_create>> and |
1039 | | <<bfd_make_writable>> and converts it into one like as |
1040 | | returned by <<bfd_openr>>. It does this by writing the |
1041 | | contents out to the memory buffer, then reversing the |
1042 | | direction. |
1043 | | |
1044 | | <<TRUE>> is returned if all is ok, otherwise <<FALSE>>. */ |
1045 | | |
1046 | | bool |
1047 | | bfd_make_readable (bfd *abfd) |
1048 | 0 | { |
1049 | 0 | if (abfd->direction != write_direction || !(abfd->flags & BFD_IN_MEMORY)) |
1050 | 0 | { |
1051 | 0 | bfd_set_error (bfd_error_invalid_operation); |
1052 | 0 | return false; |
1053 | 0 | } |
1054 | | |
1055 | 0 | if (!BFD_SEND_FMT (abfd, _bfd_write_contents, (abfd))) |
1056 | 0 | return false; |
1057 | | |
1058 | 0 | if (!BFD_SEND (abfd, _bfd_free_cached_info, (abfd))) |
1059 | 0 | return false; |
1060 | | |
1061 | 0 | if (!BFD_SEND (abfd, _close_and_cleanup, (abfd))) |
1062 | 0 | return false; |
1063 | | |
1064 | 0 | _bfd_free_cached_info (abfd); |
1065 | 0 | if (!bfd_hash_table_init_n (&abfd->section_htab, bfd_section_hash_newfunc, |
1066 | 0 | sizeof (struct section_hash_entry), 13)) |
1067 | 0 | return false; |
1068 | | |
1069 | 0 | abfd->arch_info = &bfd_default_arch_struct; |
1070 | |
|
1071 | 0 | abfd->where = 0; |
1072 | 0 | abfd->my_archive = NULL; |
1073 | 0 | abfd->origin = 0; |
1074 | 0 | abfd->opened_once = false; |
1075 | 0 | abfd->output_has_begun = false; |
1076 | 0 | abfd->cacheable = false; |
1077 | 0 | abfd->mtime_set = false; |
1078 | |
|
1079 | 0 | abfd->target_defaulted = true; |
1080 | 0 | abfd->direction = read_direction; |
1081 | 0 | abfd->symcount = 0; |
1082 | 0 | abfd->size = 0; |
1083 | |
|
1084 | 0 | bfd_check_format (abfd, bfd_object); |
1085 | |
|
1086 | 0 | return true; |
1087 | 0 | } |
1088 | | |
1089 | | /* |
1090 | | GNU Extension: separate debug-info files |
1091 | | |
1092 | | The idea here is that a special section called .gnu_debuglink might be |
1093 | | embedded in a binary file, which indicates that some *other* file |
1094 | | contains the real debugging information. This special section contains a |
1095 | | filename and CRC32 checksum, which we read and resolve to another file, |
1096 | | if it exists. |
1097 | | |
1098 | | This facilitates "optional" provision of debugging information, without |
1099 | | having to provide two complete copies of every binary object (with and |
1100 | | without debug symbols). */ |
1101 | | |
1102 | 3.61k | #define GNU_DEBUGLINK ".gnu_debuglink" |
1103 | 0 | #define GNU_DEBUGALTLINK ".gnu_debugaltlink" |
1104 | | |
1105 | | /* |
1106 | | FUNCTION |
1107 | | bfd_calc_gnu_debuglink_crc32 |
1108 | | |
1109 | | SYNOPSIS |
1110 | | uint32_t bfd_calc_gnu_debuglink_crc32 |
1111 | | (uint32_t crc, const bfd_byte *buf, bfd_size_type len); |
1112 | | |
1113 | | DESCRIPTION |
1114 | | Computes a CRC value as used in the .gnu_debuglink section. |
1115 | | Advances the previously computed @var{crc} value by computing |
1116 | | and adding in the crc32 for @var{len} bytes of @var{buf}. |
1117 | | |
1118 | | Return the updated CRC32 value. |
1119 | | */ |
1120 | | |
1121 | | uint32_t |
1122 | | bfd_calc_gnu_debuglink_crc32 (uint32_t crc, |
1123 | | const bfd_byte *buf, |
1124 | | bfd_size_type len) |
1125 | 0 | { |
1126 | 0 | static const uint32_t crc32_table[256] = |
1127 | 0 | { |
1128 | 0 | 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, |
1129 | 0 | 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, |
1130 | 0 | 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, |
1131 | 0 | 0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, |
1132 | 0 | 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856, |
1133 | 0 | 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, |
1134 | 0 | 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, |
1135 | 0 | 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, |
1136 | 0 | 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, |
1137 | 0 | 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a, |
1138 | 0 | 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599, |
1139 | 0 | 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, |
1140 | 0 | 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, |
1141 | 0 | 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, |
1142 | 0 | 0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0x0f00f934, 0x9609a88e, |
1143 | 0 | 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01, |
1144 | 0 | 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed, |
1145 | 0 | 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, |
1146 | 0 | 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, |
1147 | 0 | 0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, |
1148 | 0 | 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, |
1149 | 0 | 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5, |
1150 | 0 | 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010, |
1151 | 0 | 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, |
1152 | 0 | 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, |
1153 | 0 | 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, |
1154 | 0 | 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615, |
1155 | 0 | 0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8, |
1156 | 0 | 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344, |
1157 | 0 | 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, |
1158 | 0 | 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, |
1159 | 0 | 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, |
1160 | 0 | 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, |
1161 | 0 | 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c, |
1162 | 0 | 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef, |
1163 | 0 | 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, |
1164 | 0 | 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, |
1165 | 0 | 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, |
1166 | 0 | 0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c, |
1167 | 0 | 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713, |
1168 | 0 | 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b, |
1169 | 0 | 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, |
1170 | 0 | 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, |
1171 | 0 | 0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, |
1172 | 0 | 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278, |
1173 | 0 | 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7, |
1174 | 0 | 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66, |
1175 | 0 | 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, |
1176 | 0 | 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, |
1177 | 0 | 0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, |
1178 | 0 | 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, |
1179 | 0 | 0x2d02ef8d |
1180 | 0 | }; |
1181 | 0 | const bfd_byte *end; |
1182 | |
|
1183 | 0 | crc = ~crc & 0xffffffff; |
1184 | 0 | for (end = buf + len; buf < end; ++ buf) |
1185 | 0 | crc = crc32_table[(crc ^ *buf) & 0xff] ^ (crc >> 8); |
1186 | 0 | return ~crc & 0xffffffff; |
1187 | 0 | } |
1188 | | |
1189 | | |
1190 | | /* Extracts the filename and CRC32 value for any separate debug |
1191 | | information file associated with @var{abfd}. |
1192 | | |
1193 | | The @var{crc32_out} parameter is an untyped pointer because |
1194 | | this routine is used as a @code{get_func_type} function, but it |
1195 | | is expected to be a uint32_t pointer. |
1196 | | |
1197 | | Returns the filename of the associated debug information file, |
1198 | | or NULL if there is no such file. If the filename was found |
1199 | | then the contents of @var{crc32_out} are updated to hold the |
1200 | | corresponding CRC32 value for the file. |
1201 | | |
1202 | | The returned filename is allocated with @code{malloc}; freeing |
1203 | | it is the responsibility of the caller. */ |
1204 | | |
1205 | | static char * |
1206 | | bfd_get_debug_link_info_1 (bfd *abfd, void *crc32_out) |
1207 | 3.61k | { |
1208 | 3.61k | asection *sect; |
1209 | 3.61k | uint32_t *crc32 = crc32_out; |
1210 | 3.61k | bfd_byte *contents; |
1211 | 3.61k | unsigned int crc_offset; |
1212 | 3.61k | char *name; |
1213 | 3.61k | bfd_size_type size; |
1214 | | |
1215 | 3.61k | BFD_ASSERT (abfd); |
1216 | 3.61k | BFD_ASSERT (crc32_out); |
1217 | | |
1218 | 3.61k | sect = bfd_get_section_by_name (abfd, GNU_DEBUGLINK); |
1219 | | |
1220 | 3.61k | if (sect == NULL || (sect->flags & SEC_HAS_CONTENTS) == 0) |
1221 | 3.57k | return NULL; |
1222 | | |
1223 | 33 | size = bfd_section_size (sect); |
1224 | | |
1225 | | /* PR 22794: Make sure that the section has a reasonable size. */ |
1226 | 33 | if (size < 8) |
1227 | 3 | return NULL; |
1228 | | |
1229 | 30 | if (!bfd_malloc_and_get_section (abfd, sect, &contents)) |
1230 | 1 | return NULL; |
1231 | | |
1232 | | /* CRC value is stored after the filename, aligned up to 4 bytes. */ |
1233 | 29 | name = (char *) contents; |
1234 | | /* PR 17597: Avoid reading off the end of the buffer. */ |
1235 | 29 | crc_offset = strnlen (name, size) + 1; |
1236 | 29 | crc_offset = (crc_offset + 3) & ~3; |
1237 | 29 | if (crc_offset + 4 > size) |
1238 | 3 | { |
1239 | 3 | free (name); |
1240 | 3 | return NULL; |
1241 | 3 | } |
1242 | | |
1243 | 26 | *crc32 = bfd_get_32 (abfd, contents + crc_offset); |
1244 | 26 | return name; |
1245 | 29 | } |
1246 | | |
1247 | | |
1248 | | /* |
1249 | | FUNCTION |
1250 | | bfd_get_debug_link_info |
1251 | | |
1252 | | SYNOPSIS |
1253 | | char *bfd_get_debug_link_info (bfd *abfd, uint32_t *crc32_out); |
1254 | | |
1255 | | DESCRIPTION |
1256 | | Extracts the filename and CRC32 value for any separate debug |
1257 | | information file associated with @var{abfd}. |
1258 | | |
1259 | | Returns the filename of the associated debug information file, |
1260 | | or NULL if there is no such file. If the filename was found |
1261 | | then the contents of @var{crc32_out} are updated to hold the |
1262 | | corresponding CRC32 value for the file. |
1263 | | |
1264 | | The returned filename is allocated with @code{malloc}; freeing |
1265 | | it is the responsibility of the caller. |
1266 | | */ |
1267 | | |
1268 | | char * |
1269 | | bfd_get_debug_link_info (bfd *abfd, uint32_t *crc32_out) |
1270 | 0 | { |
1271 | 0 | return bfd_get_debug_link_info_1 (abfd, crc32_out); |
1272 | 0 | } |
1273 | | |
1274 | | /* |
1275 | | FUNCTION |
1276 | | bfd_get_alt_debug_link_info |
1277 | | |
1278 | | SYNOPSIS |
1279 | | char *bfd_get_alt_debug_link_info (bfd * abfd, |
1280 | | bfd_size_type *buildid_len, |
1281 | | bfd_byte **buildid_out); |
1282 | | |
1283 | | DESCRIPTION |
1284 | | Fetch the filename and BuildID value for any alternate debuginfo |
1285 | | associated with @var{abfd}. Return NULL if no such info found, |
1286 | | otherwise return filename and update @var{buildid_len} and |
1287 | | @var{buildid_out}. The returned filename and build_id are |
1288 | | allocated with @code{malloc}; freeing them is the responsibility |
1289 | | of the caller. |
1290 | | */ |
1291 | | |
1292 | | char * |
1293 | | bfd_get_alt_debug_link_info (bfd * abfd, bfd_size_type *buildid_len, |
1294 | | bfd_byte **buildid_out) |
1295 | 0 | { |
1296 | 0 | asection *sect; |
1297 | 0 | bfd_byte *contents; |
1298 | 0 | unsigned int buildid_offset; |
1299 | 0 | char *name; |
1300 | 0 | bfd_size_type size; |
1301 | |
|
1302 | 0 | BFD_ASSERT (abfd); |
1303 | 0 | BFD_ASSERT (buildid_len); |
1304 | 0 | BFD_ASSERT (buildid_out); |
1305 | |
|
1306 | 0 | sect = bfd_get_section_by_name (abfd, GNU_DEBUGALTLINK); |
1307 | |
|
1308 | 0 | if (sect == NULL || (sect->flags & SEC_HAS_CONTENTS) == 0) |
1309 | 0 | return NULL; |
1310 | | |
1311 | 0 | size = bfd_section_size (sect); |
1312 | 0 | if (size < 8) |
1313 | 0 | return NULL; |
1314 | | |
1315 | 0 | if (!bfd_malloc_and_get_section (abfd, sect, & contents)) |
1316 | 0 | return NULL; |
1317 | | |
1318 | | /* BuildID value is stored after the filename. */ |
1319 | 0 | name = (char *) contents; |
1320 | 0 | buildid_offset = strnlen (name, size) + 1; |
1321 | 0 | if (buildid_offset >= bfd_section_size (sect)) |
1322 | 0 | return NULL; |
1323 | | |
1324 | 0 | *buildid_len = size - buildid_offset; |
1325 | 0 | *buildid_out = bfd_malloc (*buildid_len); |
1326 | 0 | memcpy (*buildid_out, contents + buildid_offset, *buildid_len); |
1327 | |
|
1328 | 0 | return name; |
1329 | 0 | } |
1330 | | |
1331 | | /* Checks to see if @var{name} is a file and if its contents match |
1332 | | @var{crc32}, which is a pointer to a @code{uint32_t} |
1333 | | containing a CRC32. |
1334 | | |
1335 | | The @var{crc32_p} parameter is an untyped pointer because this |
1336 | | routine is used as a @code{check_func_type} function. */ |
1337 | | |
1338 | | static bool |
1339 | | separate_debug_file_exists (const char *name, void *crc32_p) |
1340 | 80 | { |
1341 | 80 | unsigned char buffer[8 * 1024]; |
1342 | 80 | uint32_t file_crc = 0; |
1343 | 80 | FILE *f; |
1344 | 80 | bfd_size_type count; |
1345 | 80 | uint32_t crc; |
1346 | | |
1347 | 80 | BFD_ASSERT (name); |
1348 | 80 | BFD_ASSERT (crc32_p); |
1349 | | |
1350 | 80 | crc = *(uint32_t *) crc32_p; |
1351 | | |
1352 | 80 | f = _bfd_real_fopen (name, FOPEN_RB); |
1353 | 80 | if (f == NULL) |
1354 | 80 | return false; |
1355 | | |
1356 | 0 | while ((count = fread (buffer, 1, sizeof (buffer), f)) > 0) |
1357 | 0 | file_crc = bfd_calc_gnu_debuglink_crc32 (file_crc, buffer, count); |
1358 | |
|
1359 | 0 | fclose (f); |
1360 | |
|
1361 | 0 | return crc == file_crc; |
1362 | 80 | } |
1363 | | |
1364 | | /* Checks to see if @var{name} is a file. */ |
1365 | | |
1366 | | static bool |
1367 | | separate_alt_debug_file_exists (const char *name, void *unused ATTRIBUTE_UNUSED) |
1368 | 0 | { |
1369 | 0 | FILE *f; |
1370 | |
|
1371 | 0 | BFD_ASSERT (name); |
1372 | |
|
1373 | 0 | f = _bfd_real_fopen (name, FOPEN_RB); |
1374 | 0 | if (f == NULL) |
1375 | 0 | return false; |
1376 | | |
1377 | 0 | fclose (f); |
1378 | |
|
1379 | 0 | return true; |
1380 | 0 | } |
1381 | | |
1382 | | /* Searches for a debug information file corresponding to @var{abfd}. |
1383 | | |
1384 | | The name of the separate debug info file is returned by the |
1385 | | @var{get} function. This function scans various fixed locations |
1386 | | in the filesystem, including the file tree rooted at @var{dir}. |
1387 | | If the @var{include_dirs} parameter is true then the directory |
1388 | | components of @var{abfd}'s filename will be included in the |
1389 | | searched locations. |
1390 | | |
1391 | | @var{data} is passed unmodified to the @var{get} and @var{check} |
1392 | | functions. It is generally used to implement build-id-like |
1393 | | matching in the callback functions. |
1394 | | |
1395 | | Returns the filename of the first file to be found which |
1396 | | receives a TRUE result from the @var{check} function. |
1397 | | Returns NULL if no valid file could be found. */ |
1398 | | |
1399 | | typedef char * (*get_func_type) (bfd *, void *); |
1400 | | typedef bool (*check_func_type) (const char *, void *); |
1401 | | |
1402 | | static char * |
1403 | | find_separate_debug_file (bfd *abfd, |
1404 | | const char *debug_file_directory, |
1405 | | bool include_dirs, |
1406 | | get_func_type get_func, |
1407 | | check_func_type check_func, |
1408 | | void *func_data) |
1409 | 7.22k | { |
1410 | 7.22k | char *base; |
1411 | 7.22k | char *dir; |
1412 | 7.22k | char *debugfile; |
1413 | 7.22k | char *canon_dir; |
1414 | 7.22k | size_t dirlen; |
1415 | 7.22k | size_t canon_dirlen; |
1416 | | |
1417 | 7.22k | BFD_ASSERT (abfd); |
1418 | 7.22k | if (debug_file_directory == NULL) |
1419 | 0 | debug_file_directory = "."; |
1420 | | |
1421 | | /* BFD may have been opened from a stream. */ |
1422 | 7.22k | if (bfd_get_filename (abfd) == NULL) |
1423 | 0 | { |
1424 | 0 | bfd_set_error (bfd_error_invalid_operation); |
1425 | 0 | return NULL; |
1426 | 0 | } |
1427 | | |
1428 | 7.22k | base = get_func (abfd, func_data); |
1429 | | |
1430 | 7.22k | if (base == NULL) |
1431 | 7.18k | return NULL; |
1432 | | |
1433 | 36 | if (base[0] == '\0') |
1434 | 10 | { |
1435 | 10 | free (base); |
1436 | 10 | bfd_set_error (bfd_error_no_debug_section); |
1437 | 10 | return NULL; |
1438 | 10 | } |
1439 | | |
1440 | 26 | if (include_dirs) |
1441 | 16 | { |
1442 | 16 | const char *fname = bfd_get_filename (abfd); |
1443 | 224 | for (dirlen = strlen (fname); dirlen > 0; dirlen--) |
1444 | 224 | if (IS_DIR_SEPARATOR (fname[dirlen - 1])) |
1445 | 16 | break; |
1446 | | |
1447 | 16 | dir = (char *) bfd_malloc (dirlen + 1); |
1448 | 16 | if (dir == NULL) |
1449 | 0 | { |
1450 | 0 | free (base); |
1451 | 0 | return NULL; |
1452 | 0 | } |
1453 | 16 | memcpy (dir, fname, dirlen); |
1454 | 16 | dir[dirlen] = '\0'; |
1455 | 16 | } |
1456 | 10 | else |
1457 | 10 | { |
1458 | 10 | dir = (char *) bfd_malloc (1); |
1459 | 10 | * dir = 0; |
1460 | 10 | dirlen = 0; |
1461 | 10 | } |
1462 | | |
1463 | | /* Compute the canonical name of the bfd object with all symbolic links |
1464 | | resolved, for use in the global debugfile directory. */ |
1465 | 26 | canon_dir = lrealpath (bfd_get_filename (abfd)); |
1466 | 364 | for (canon_dirlen = strlen (canon_dir); canon_dirlen > 0; canon_dirlen--) |
1467 | 364 | if (IS_DIR_SEPARATOR (canon_dir[canon_dirlen - 1])) |
1468 | 26 | break; |
1469 | 26 | canon_dir[canon_dirlen] = '\0'; |
1470 | | |
1471 | 26 | #ifndef EXTRA_DEBUG_ROOT1 |
1472 | 52 | #define EXTRA_DEBUG_ROOT1 "/usr/lib/debug" |
1473 | 26 | #endif |
1474 | 26 | #ifndef EXTRA_DEBUG_ROOT2 |
1475 | 52 | #define EXTRA_DEBUG_ROOT2 "/usr/lib/debug/usr" |
1476 | 26 | #endif |
1477 | | |
1478 | 26 | debugfile = (char *) |
1479 | 26 | bfd_malloc (strlen (debug_file_directory) + 1 |
1480 | 26 | + (canon_dirlen > dirlen ? canon_dirlen : dirlen) |
1481 | 26 | + strlen (".debug/") |
1482 | 26 | #ifdef EXTRA_DEBUG_ROOT1 |
1483 | 26 | + strlen (EXTRA_DEBUG_ROOT1) |
1484 | 26 | #endif |
1485 | 26 | #ifdef EXTRA_DEBUG_ROOT2 |
1486 | 26 | + strlen (EXTRA_DEBUG_ROOT2) |
1487 | 26 | #endif |
1488 | 26 | + strlen (base) |
1489 | 26 | + 1); |
1490 | 26 | if (debugfile == NULL) |
1491 | 0 | goto found; /* Actually this returns NULL. */ |
1492 | | |
1493 | | /* First try in the same directory as the original file. |
1494 | | |
1495 | | FIXME: Strictly speaking if we are using the build-id method, |
1496 | | (ie include_dirs == FALSE) then we should only check absolute |
1497 | | paths, not relative ones like this one (and the next one). |
1498 | | The check is left in however as this allows the binutils |
1499 | | testsuite to exercise this feature without having to install |
1500 | | a file into the root filesystem. (See binutils/testsuite/ |
1501 | | binutils-all/objdump.exp for the test). */ |
1502 | 26 | sprintf (debugfile, "%s%s", dir, base); |
1503 | 26 | if (check_func (debugfile, func_data)) |
1504 | 0 | goto found; |
1505 | | |
1506 | | /* Then try in a subdirectory called .debug. */ |
1507 | 26 | sprintf (debugfile, "%s.debug/%s", dir, base); |
1508 | 26 | if (check_func (debugfile, func_data)) |
1509 | 0 | goto found; |
1510 | | |
1511 | 26 | #ifdef EXTRA_DEBUG_ROOT1 |
1512 | | /* Try the first extra debug file root. */ |
1513 | 26 | sprintf (debugfile, "%s%s%s", EXTRA_DEBUG_ROOT1, |
1514 | 26 | include_dirs ? canon_dir : "/", base); |
1515 | 26 | if (check_func (debugfile, func_data)) |
1516 | 0 | goto found; |
1517 | 26 | #endif |
1518 | | |
1519 | 26 | #ifdef EXTRA_DEBUG_ROOT2 |
1520 | | /* Try the second extra debug file root. */ |
1521 | 26 | sprintf (debugfile, "%s%s%s", EXTRA_DEBUG_ROOT2, |
1522 | 26 | include_dirs ? canon_dir : "/", base); |
1523 | 26 | if (check_func (debugfile, func_data)) |
1524 | 0 | goto found; |
1525 | 26 | #endif |
1526 | | |
1527 | | /* Then try in the global debugfile directory. */ |
1528 | 26 | strcpy (debugfile, debug_file_directory); |
1529 | 26 | dirlen = strlen (debug_file_directory) - 1; |
1530 | 26 | if (include_dirs) |
1531 | 16 | { |
1532 | 16 | if (dirlen > 0 |
1533 | 16 | && debug_file_directory[dirlen] != '/' |
1534 | 16 | && canon_dir[0] != '/') |
1535 | 0 | strcat (debugfile, "/"); |
1536 | 16 | strcat (debugfile, canon_dir); |
1537 | 16 | } |
1538 | 10 | else |
1539 | 10 | { |
1540 | 10 | if (dirlen > 0 && debug_file_directory[dirlen] != '/') |
1541 | 10 | strcat (debugfile, "/"); |
1542 | 10 | } |
1543 | 26 | strcat (debugfile, base); |
1544 | | |
1545 | 26 | if (check_func (debugfile, func_data)) |
1546 | 0 | goto found; |
1547 | | |
1548 | | /* Failed to find the file. */ |
1549 | 26 | free (debugfile); |
1550 | 26 | debugfile = NULL; |
1551 | | |
1552 | 26 | found: |
1553 | 26 | free (base); |
1554 | 26 | free (dir); |
1555 | 26 | free (canon_dir); |
1556 | 26 | return debugfile; |
1557 | 26 | } |
1558 | | |
1559 | | /* |
1560 | | FUNCTION |
1561 | | bfd_follow_gnu_debuglink |
1562 | | |
1563 | | SYNOPSIS |
1564 | | char *bfd_follow_gnu_debuglink (bfd *abfd, const char *dir); |
1565 | | |
1566 | | DESCRIPTION |
1567 | | Takes a BFD and searches it for a .gnu_debuglink section. If this |
1568 | | section is found, it examines the section for the name and checksum |
1569 | | of a '.debug' file containing auxiliary debugging information. It |
1570 | | then searches the filesystem for this .debug file in some standard |
1571 | | locations, including the directory tree rooted at @var{dir}, and if |
1572 | | found returns the full filename. |
1573 | | |
1574 | | If @var{dir} is NULL, the search will take place starting at |
1575 | | the current directory. |
1576 | | |
1577 | | Returns <<NULL>> on any errors or failure to locate the .debug |
1578 | | file, otherwise a pointer to a heap-allocated string |
1579 | | containing the filename. The caller is responsible for |
1580 | | freeing this string. |
1581 | | */ |
1582 | | |
1583 | | char * |
1584 | | bfd_follow_gnu_debuglink (bfd *abfd, const char *dir) |
1585 | 3.61k | { |
1586 | 3.61k | uint32_t crc32; |
1587 | | |
1588 | 3.61k | return find_separate_debug_file (abfd, dir, true, |
1589 | 3.61k | bfd_get_debug_link_info_1, |
1590 | 3.61k | separate_debug_file_exists, &crc32); |
1591 | 3.61k | } |
1592 | | |
1593 | | /* Helper for bfd_follow_gnu_debugaltlink. It just returns the name |
1594 | | of the separate debug file. */ |
1595 | | |
1596 | | static char * |
1597 | | get_alt_debug_link_info_shim (bfd * abfd, void *unused ATTRIBUTE_UNUSED) |
1598 | 0 | { |
1599 | 0 | bfd_size_type len; |
1600 | 0 | bfd_byte *buildid = NULL; |
1601 | 0 | char *result = bfd_get_alt_debug_link_info (abfd, &len, &buildid); |
1602 | |
|
1603 | 0 | free (buildid); |
1604 | |
|
1605 | 0 | return result; |
1606 | 0 | } |
1607 | | |
1608 | | /* |
1609 | | FUNCTION |
1610 | | bfd_follow_gnu_debugaltlink |
1611 | | |
1612 | | SYNOPSIS |
1613 | | char *bfd_follow_gnu_debugaltlink (bfd *abfd, const char *dir); |
1614 | | |
1615 | | DESCRIPTION |
1616 | | Takes a BFD and searches it for a .gnu_debugaltlink section. If this |
1617 | | section is found, it examines the section for the name of a file |
1618 | | containing auxiliary debugging information. It then searches the |
1619 | | filesystem for this file in a set of standard locations, including |
1620 | | the directory tree rooted at @var{dir}, and if found returns the |
1621 | | full filename. |
1622 | | |
1623 | | If @var{dir} is NULL, the search will take place starting at |
1624 | | the current directory. |
1625 | | |
1626 | | Returns <<NULL>> on any errors or failure to locate the debug |
1627 | | file, otherwise a pointer to a heap-allocated string |
1628 | | containing the filename. The caller is responsible for |
1629 | | freeing this string. |
1630 | | */ |
1631 | | |
1632 | | char * |
1633 | | bfd_follow_gnu_debugaltlink (bfd *abfd, const char *dir) |
1634 | 0 | { |
1635 | 0 | return find_separate_debug_file (abfd, dir, true, |
1636 | 0 | get_alt_debug_link_info_shim, |
1637 | 0 | separate_alt_debug_file_exists, |
1638 | 0 | NULL); |
1639 | 0 | } |
1640 | | |
1641 | | /* |
1642 | | FUNCTION |
1643 | | bfd_create_gnu_debuglink_section |
1644 | | |
1645 | | SYNOPSIS |
1646 | | struct bfd_section *bfd_create_gnu_debuglink_section |
1647 | | (bfd *abfd, const char *filename); |
1648 | | |
1649 | | DESCRIPTION |
1650 | | Takes a @var{BFD} and adds a .gnu_debuglink section to it. The |
1651 | | section is sized to be big enough to contain a link to the specified |
1652 | | @var{filename}. |
1653 | | |
1654 | | A pointer to the new section is returned if all is ok. Otherwise |
1655 | | <<NULL>> is returned and bfd_error is set. |
1656 | | */ |
1657 | | |
1658 | | asection * |
1659 | | bfd_create_gnu_debuglink_section (bfd *abfd, const char *filename) |
1660 | 0 | { |
1661 | 0 | asection *sect; |
1662 | 0 | bfd_size_type debuglink_size; |
1663 | 0 | flagword flags; |
1664 | |
|
1665 | 0 | if (abfd == NULL || filename == NULL) |
1666 | 0 | { |
1667 | 0 | bfd_set_error (bfd_error_invalid_operation); |
1668 | 0 | return NULL; |
1669 | 0 | } |
1670 | | |
1671 | | /* Strip off any path components in filename. */ |
1672 | 0 | filename = lbasename (filename); |
1673 | |
|
1674 | 0 | sect = bfd_get_section_by_name (abfd, GNU_DEBUGLINK); |
1675 | 0 | if (sect) |
1676 | 0 | { |
1677 | | /* Section already exists. */ |
1678 | 0 | bfd_set_error (bfd_error_invalid_operation); |
1679 | 0 | return NULL; |
1680 | 0 | } |
1681 | | |
1682 | 0 | flags = SEC_HAS_CONTENTS | SEC_READONLY | SEC_DEBUGGING; |
1683 | 0 | sect = bfd_make_section_with_flags (abfd, GNU_DEBUGLINK, flags); |
1684 | 0 | if (sect == NULL) |
1685 | 0 | return NULL; |
1686 | | |
1687 | | /* Compute the size of the section. Allow for the CRC after the filename, |
1688 | | and padding so that it will start on a 4-byte boundary. */ |
1689 | 0 | debuglink_size = strlen (filename) + 1; |
1690 | 0 | debuglink_size += 3; |
1691 | 0 | debuglink_size &= ~3; |
1692 | 0 | debuglink_size += 4; |
1693 | |
|
1694 | 0 | if (!bfd_set_section_size (sect, debuglink_size)) |
1695 | | /* XXX Should we delete the section from the bfd ? */ |
1696 | 0 | return NULL; |
1697 | | |
1698 | | /* PR 21193: Ensure that the section has 4-byte alignment for the CRC. |
1699 | | Note - despite the name of the function being called, we are |
1700 | | setting an alignment power, not a byte alignment value. */ |
1701 | 0 | bfd_set_section_alignment (sect, 2); |
1702 | |
|
1703 | 0 | return sect; |
1704 | 0 | } |
1705 | | |
1706 | | |
1707 | | /* |
1708 | | FUNCTION |
1709 | | bfd_fill_in_gnu_debuglink_section |
1710 | | |
1711 | | SYNOPSIS |
1712 | | bool bfd_fill_in_gnu_debuglink_section |
1713 | | (bfd *abfd, struct bfd_section *sect, const char *filename); |
1714 | | |
1715 | | DESCRIPTION |
1716 | | Takes a @var{BFD} and containing a .gnu_debuglink section @var{SECT} |
1717 | | and fills in the contents of the section to contain a link to the |
1718 | | specified @var{filename}. The filename should be absolute or |
1719 | | relative to the current directory. |
1720 | | |
1721 | | <<TRUE>> is returned if all is ok. Otherwise <<FALSE>> is returned |
1722 | | and bfd_error is set. |
1723 | | */ |
1724 | | |
1725 | | bool |
1726 | | bfd_fill_in_gnu_debuglink_section (bfd *abfd, |
1727 | | struct bfd_section *sect, |
1728 | | const char *filename) |
1729 | 0 | { |
1730 | 0 | bfd_size_type debuglink_size; |
1731 | 0 | uint32_t crc32; |
1732 | 0 | char * contents; |
1733 | 0 | bfd_size_type crc_offset; |
1734 | 0 | FILE * handle; |
1735 | 0 | unsigned char buffer[8 * 1024]; |
1736 | 0 | size_t count; |
1737 | 0 | size_t filelen; |
1738 | |
|
1739 | 0 | if (abfd == NULL || sect == NULL || filename == NULL) |
1740 | 0 | { |
1741 | 0 | bfd_set_error (bfd_error_invalid_operation); |
1742 | 0 | return false; |
1743 | 0 | } |
1744 | | |
1745 | | /* Open the linked file so that we can compute a CRC. */ |
1746 | 0 | handle = _bfd_real_fopen (filename, FOPEN_RB); |
1747 | 0 | if (handle == NULL) |
1748 | 0 | { |
1749 | 0 | bfd_set_error (bfd_error_system_call); |
1750 | 0 | return false; |
1751 | 0 | } |
1752 | | |
1753 | 0 | crc32 = 0; |
1754 | 0 | while ((count = fread (buffer, 1, sizeof buffer, handle)) > 0) |
1755 | 0 | crc32 = bfd_calc_gnu_debuglink_crc32 (crc32, buffer, count); |
1756 | 0 | fclose (handle); |
1757 | | |
1758 | | /* Strip off any path components in filename, |
1759 | | now that we no longer need them. */ |
1760 | 0 | filename = lbasename (filename); |
1761 | |
|
1762 | 0 | filelen = strlen (filename); |
1763 | 0 | debuglink_size = filelen + 1; |
1764 | 0 | debuglink_size += 3; |
1765 | 0 | debuglink_size &= ~3; |
1766 | 0 | debuglink_size += 4; |
1767 | |
|
1768 | 0 | contents = bfd_malloc (debuglink_size); |
1769 | 0 | if (contents == NULL) |
1770 | 0 | { |
1771 | | /* XXX Should we delete the section from the bfd ? */ |
1772 | 0 | return false; |
1773 | 0 | } |
1774 | | |
1775 | 0 | crc_offset = debuglink_size - 4; |
1776 | 0 | memcpy (contents, filename, filelen); |
1777 | 0 | memset (contents + filelen, 0, crc_offset - filelen); |
1778 | |
|
1779 | 0 | bfd_put_32 (abfd, crc32, contents + crc_offset); |
1780 | |
|
1781 | 0 | bool ret = bfd_set_section_contents (abfd, sect, contents, 0, debuglink_size); |
1782 | 0 | free (contents); |
1783 | 0 | return ret; |
1784 | 0 | } |
1785 | | |
1786 | | /* Finds the build-id associated with @var{abfd}. If the build-id is |
1787 | | extracted from the note section then a build-id structure is built |
1788 | | for it, using memory allocated to @var{abfd}, and this is then |
1789 | | attached to the @var{abfd}. |
1790 | | |
1791 | | Returns a pointer to the build-id structure if a build-id could be |
1792 | | found. If no build-id is found NULL is returned and error code is |
1793 | | set. */ |
1794 | | |
1795 | | static struct bfd_build_id * |
1796 | | get_build_id (bfd *abfd) |
1797 | 3.61k | { |
1798 | 3.61k | struct bfd_build_id *build_id; |
1799 | 3.61k | Elf_Internal_Note inote; |
1800 | 3.61k | Elf_External_Note *enote; |
1801 | 3.61k | bfd_byte *contents; |
1802 | 3.61k | asection *sect; |
1803 | 3.61k | bfd_size_type size; |
1804 | | |
1805 | 3.61k | BFD_ASSERT (abfd); |
1806 | | |
1807 | 3.61k | if (abfd->build_id && abfd->build_id->size > 0) |
1808 | | /* Save some time by using the already computed build-id. */ |
1809 | 9 | return (struct bfd_build_id *) abfd->build_id; |
1810 | | |
1811 | 3.60k | sect = bfd_get_section_by_name (abfd, ".note.gnu.build-id"); |
1812 | 3.60k | if (sect == NULL |
1813 | 24 | || (sect->flags & SEC_HAS_CONTENTS) == 0) |
1814 | 3.58k | { |
1815 | 3.58k | bfd_set_error (bfd_error_no_debug_section); |
1816 | 3.58k | return NULL; |
1817 | 3.58k | } |
1818 | | |
1819 | 18 | size = bfd_section_size (sect); |
1820 | | /* FIXME: Should we support smaller build-id notes ? */ |
1821 | 18 | if (size < 0x24) |
1822 | 2 | { |
1823 | 2 | bfd_set_error (bfd_error_invalid_operation); |
1824 | 2 | return NULL; |
1825 | 2 | } |
1826 | | |
1827 | 16 | if (!bfd_malloc_and_get_section (abfd, sect, & contents)) |
1828 | 2 | return NULL; |
1829 | | |
1830 | | /* FIXME: Paranoia - allow for compressed build-id sections. |
1831 | | Maybe we should complain if this size is different from |
1832 | | the one obtained above... */ |
1833 | 14 | size = bfd_section_size (sect); |
1834 | 14 | if (size < sizeof (Elf_External_Note)) |
1835 | 0 | { |
1836 | 0 | bfd_set_error (bfd_error_invalid_operation); |
1837 | 0 | free (contents); |
1838 | 0 | return NULL; |
1839 | 0 | } |
1840 | | |
1841 | 14 | enote = (Elf_External_Note *) contents; |
1842 | 14 | inote.type = H_GET_32 (abfd, enote->type); |
1843 | 14 | inote.namesz = H_GET_32 (abfd, enote->namesz); |
1844 | 14 | inote.namedata = enote->name; |
1845 | 14 | inote.descsz = H_GET_32 (abfd, enote->descsz); |
1846 | 14 | inote.descdata = inote.namedata + BFD_ALIGN (inote.namesz, 4); |
1847 | | /* FIXME: Should we check for extra notes in this section ? */ |
1848 | | |
1849 | 14 | if (inote.descsz <= 0 |
1850 | 10 | || inote.type != NT_GNU_BUILD_ID |
1851 | 5 | || inote.namesz != 4 /* sizeof "GNU" */ |
1852 | 4 | || !startswith (inote.namedata, "GNU") |
1853 | 3 | || inote.descsz > 0x7ffffffe |
1854 | 2 | || size < (12 + BFD_ALIGN (inote.namesz, 4) + inote.descsz)) |
1855 | 13 | { |
1856 | 13 | free (contents); |
1857 | 13 | bfd_set_error (bfd_error_invalid_operation); |
1858 | 13 | return NULL; |
1859 | 13 | } |
1860 | | |
1861 | 1 | build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) + inote.descsz); |
1862 | 1 | if (build_id == NULL) |
1863 | 0 | { |
1864 | 0 | free (contents); |
1865 | 0 | return NULL; |
1866 | 0 | } |
1867 | | |
1868 | 1 | build_id->size = inote.descsz; |
1869 | 1 | memcpy (build_id->data, inote.descdata, inote.descsz); |
1870 | 1 | abfd->build_id = build_id; |
1871 | 1 | free (contents); |
1872 | | |
1873 | 1 | return build_id; |
1874 | 1 | } |
1875 | | |
1876 | | /* Searches @var{abfd} for a build-id, and then constructs a pathname |
1877 | | from it. The path is computed as .build-id/NN/NN+NN.debug where |
1878 | | NNNN+NN is the build-id value as a hexadecimal string. |
1879 | | |
1880 | | Returns the constructed filename or NULL upon error. It is the |
1881 | | caller's responsibility to free the memory used to hold the |
1882 | | filename. If a filename is returned then the @var{build_id_out_p} |
1883 | | parameter (which points to a @code{struct bfd_build_id} pointer) is |
1884 | | set to a pointer to the build_id structure. */ |
1885 | | |
1886 | | static char * |
1887 | | get_build_id_name (bfd *abfd, void *build_id_out_p) |
1888 | 3.61k | { |
1889 | 3.61k | struct bfd_build_id **build_id_out = build_id_out_p; |
1890 | 3.61k | struct bfd_build_id *build_id; |
1891 | 3.61k | char *name; |
1892 | 3.61k | char *n; |
1893 | 3.61k | bfd_size_type s; |
1894 | 3.61k | bfd_byte *d; |
1895 | | |
1896 | 3.61k | if (abfd == NULL || bfd_get_filename (abfd) == NULL || build_id_out == NULL) |
1897 | 0 | { |
1898 | 0 | bfd_set_error (bfd_error_invalid_operation); |
1899 | 0 | return NULL; |
1900 | 0 | } |
1901 | | |
1902 | 3.61k | build_id = get_build_id (abfd); |
1903 | 3.61k | if (build_id == NULL) |
1904 | 3.60k | return NULL; |
1905 | | |
1906 | | /* Compute the debug pathname corresponding to the build-id. */ |
1907 | 10 | name = bfd_malloc (strlen (".build-id/") + build_id->size * 2 + 2 + strlen (".debug")); |
1908 | 10 | if (name == NULL) |
1909 | 0 | { |
1910 | 0 | bfd_set_error (bfd_error_no_memory); |
1911 | 0 | return NULL; |
1912 | 0 | } |
1913 | 10 | n = name; |
1914 | 10 | d = build_id->data; |
1915 | 10 | s = build_id->size; |
1916 | | |
1917 | 10 | n += sprintf (n, ".build-id/"); |
1918 | 10 | n += sprintf (n, "%02x", (unsigned) *d++); s--; |
1919 | 10 | n += sprintf (n, "/"); |
1920 | 129 | while (s--) |
1921 | 119 | n += sprintf (n, "%02x", (unsigned) *d++); |
1922 | 10 | n += sprintf (n, ".debug"); |
1923 | | |
1924 | 10 | *build_id_out = build_id; |
1925 | 10 | return name; |
1926 | 10 | } |
1927 | | |
1928 | | /* Checks to see if @var{name} is a readable file and if its build-id |
1929 | | matches @var{buildid}. |
1930 | | |
1931 | | Returns TRUE if the file exists, is readable, and contains a |
1932 | | build-id which matches the build-id pointed at by @var{build_id_p} |
1933 | | (which is really a @code{struct bfd_build_id **}). */ |
1934 | | |
1935 | | static bool |
1936 | | check_build_id_file (const char *name, void *buildid_p) |
1937 | 50 | { |
1938 | 50 | struct bfd_build_id *orig_build_id; |
1939 | 50 | struct bfd_build_id *build_id; |
1940 | 50 | bfd * file; |
1941 | 50 | bool result; |
1942 | | |
1943 | 50 | BFD_ASSERT (name); |
1944 | 50 | BFD_ASSERT (buildid_p); |
1945 | | |
1946 | 50 | file = bfd_openr (name, NULL); |
1947 | 50 | if (file == NULL) |
1948 | 50 | return false; |
1949 | | |
1950 | | /* If the file is an archive, process all of its elements. */ |
1951 | 0 | if (! bfd_check_format (file, bfd_object)) |
1952 | 0 | { |
1953 | 0 | bfd_close (file); |
1954 | 0 | return false; |
1955 | 0 | } |
1956 | | |
1957 | 0 | build_id = get_build_id (file); |
1958 | 0 | if (build_id == NULL) |
1959 | 0 | { |
1960 | 0 | bfd_close (file); |
1961 | 0 | return false; |
1962 | 0 | } |
1963 | | |
1964 | 0 | orig_build_id = *(struct bfd_build_id **) buildid_p; |
1965 | |
|
1966 | 0 | result = build_id->size == orig_build_id->size |
1967 | 0 | && memcmp (build_id->data, orig_build_id->data, build_id->size) == 0; |
1968 | |
|
1969 | 0 | (void) bfd_close (file); |
1970 | |
|
1971 | 0 | return result; |
1972 | 0 | } |
1973 | | |
1974 | | /* |
1975 | | FUNCTION |
1976 | | bfd_follow_build_id_debuglink |
1977 | | |
1978 | | SYNOPSIS |
1979 | | char *bfd_follow_build_id_debuglink (bfd *abfd, const char *dir); |
1980 | | |
1981 | | DESCRIPTION |
1982 | | Takes @var{abfd} and searches it for a .note.gnu.build-id section. |
1983 | | If this section is found, it extracts the value of the NT_GNU_BUILD_ID |
1984 | | note, which should be a hexadecimal value @var{NNNN+NN} (for |
1985 | | 32+ hex digits). It then searches the filesystem for a file named |
1986 | | @var{.build-id/NN/NN+NN.debug} in a set of standard locations, |
1987 | | including the directory tree rooted at @var{dir}. The filename |
1988 | | of the first matching file to be found is returned. A matching |
1989 | | file should contain a .note.gnu.build-id section with the same |
1990 | | @var{NNNN+NN} note as @var{abfd}, although this check is currently |
1991 | | not implemented. |
1992 | | |
1993 | | If @var{dir} is NULL, the search will take place starting at |
1994 | | the current directory. |
1995 | | |
1996 | | Returns <<NULL>> on any errors or failure to locate the debug |
1997 | | file, otherwise a pointer to a heap-allocated string |
1998 | | containing the filename. The caller is responsible for |
1999 | | freeing this string. |
2000 | | */ |
2001 | | |
2002 | | char * |
2003 | | bfd_follow_build_id_debuglink (bfd *abfd, const char *dir) |
2004 | 3.61k | { |
2005 | 3.61k | struct bfd_build_id *build_id; |
2006 | | |
2007 | 3.61k | return find_separate_debug_file (abfd, dir, false, |
2008 | 3.61k | get_build_id_name, |
2009 | 3.61k | check_build_id_file, &build_id); |
2010 | 3.61k | } |
2011 | | |
2012 | | /* |
2013 | | FUNCTION |
2014 | | bfd_set_filename |
2015 | | |
2016 | | SYNOPSIS |
2017 | | const char *bfd_set_filename (bfd *abfd, const char *filename); |
2018 | | |
2019 | | DESCRIPTION |
2020 | | Set the filename of @var{abfd}, copying the FILENAME parameter to |
2021 | | bfd_alloc'd memory owned by @var{abfd}. Returns a pointer the |
2022 | | newly allocated name, or NULL if the allocation failed. |
2023 | | */ |
2024 | | |
2025 | | const char * |
2026 | | bfd_set_filename (bfd *abfd, const char *filename) |
2027 | 9.34M | { |
2028 | 9.34M | size_t len = strlen (filename) + 1; |
2029 | 9.34M | char *n = bfd_alloc (abfd, len); |
2030 | | |
2031 | 9.34M | if (n == NULL) |
2032 | 0 | return NULL; |
2033 | | |
2034 | 9.34M | if (abfd->filename != NULL) |
2035 | 0 | { |
2036 | | /* PR 29389. If we attempt to rename a file that has been closed due |
2037 | | to caching, then we will not be able to reopen it later on. */ |
2038 | 0 | if (abfd->iostream == NULL && (abfd->flags & BFD_CLOSED_BY_CACHE)) |
2039 | 0 | { |
2040 | 0 | bfd_set_error (bfd_error_invalid_operation); |
2041 | 0 | return NULL; |
2042 | 0 | } |
2043 | | |
2044 | | /* Similarly if we attempt to close a renamed file because the |
2045 | | cache is now full, we will not be able to reopen it later on. */ |
2046 | 0 | if (abfd->iostream != NULL) |
2047 | 0 | abfd->cacheable = 0; |
2048 | 0 | } |
2049 | | |
2050 | 9.34M | memcpy (n, filename, len); |
2051 | 9.34M | abfd->filename = n; |
2052 | | |
2053 | 9.34M | return n; |
2054 | 9.34M | } |
2055 | | |
2056 | | /* |
2057 | | FUNCTION |
2058 | | bfd_extract_object_only_section |
2059 | | |
2060 | | SYNOPSIS |
2061 | | const char *bfd_extract_object_only_section |
2062 | | (bfd *abfd); |
2063 | | |
2064 | | DESCRIPTION |
2065 | | |
2066 | | Takes a @var{ABFD} and extract the .gnu_object_only section into |
2067 | | a temporary file. |
2068 | | |
2069 | | RETURNS |
2070 | | The name of the temporary file is returned if all is ok. |
2071 | | Otherwise <<NULL>> is returned and bfd_error is set. |
2072 | | */ |
2073 | | |
2074 | | const char * |
2075 | | bfd_extract_object_only_section (bfd *abfd) |
2076 | 0 | { |
2077 | 0 | asection *sec = abfd->object_only_section; |
2078 | 0 | const char *name; |
2079 | 0 | FILE *file; |
2080 | 0 | bfd_byte *memhunk = NULL; |
2081 | 0 | size_t off, size; |
2082 | 0 | bfd_error_type err; |
2083 | | |
2084 | | /* Get a temporary object-only file. */ |
2085 | 0 | name = make_temp_file (".obj-only.o"); |
2086 | | |
2087 | | /* Open the object-only file. */ |
2088 | 0 | file = _bfd_real_fopen (name, FOPEN_WB); |
2089 | 0 | if (!bfd_get_full_section_contents (abfd, sec, &memhunk)) |
2090 | 0 | { |
2091 | 0 | err = bfd_get_error (); |
2092 | |
|
2093 | 0 | loser: |
2094 | 0 | free (memhunk); |
2095 | 0 | fclose (file); |
2096 | 0 | unlink (name); |
2097 | 0 | bfd_set_error (err); |
2098 | 0 | return NULL; |
2099 | 0 | } |
2100 | | |
2101 | 0 | off = 0; |
2102 | 0 | size = sec->size; |
2103 | 0 | while (off != size) |
2104 | 0 | { |
2105 | 0 | size_t written, nwrite = size - off; |
2106 | |
|
2107 | 0 | written = fwrite (memhunk + off, 1, nwrite, file); |
2108 | 0 | if (written < nwrite && ferror (file)) |
2109 | 0 | { |
2110 | 0 | err = bfd_error_system_call; |
2111 | 0 | goto loser; |
2112 | 0 | } |
2113 | | |
2114 | 0 | off += written; |
2115 | 0 | } |
2116 | | |
2117 | 0 | free (memhunk); |
2118 | 0 | fclose (file); |
2119 | 0 | return name; |
2120 | 0 | } |