Coverage Report

Created: 2024-05-21 06:29

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