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