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