xref: /openbsd-src/gnu/usr.bin/binutils-2.17/bfd/cache.c (revision 3d8817e467ea46cf4772788d6804dd293abfb01a)
1*3d8817e4Smiod /* BFD library -- caching of file descriptors.
2*3d8817e4Smiod 
3*3d8817e4Smiod    Copyright 1990, 1991, 1992, 1993, 1994, 1996, 2000, 2001, 2002,
4*3d8817e4Smiod    2003, 2004, 2005 Free Software Foundation, Inc.
5*3d8817e4Smiod 
6*3d8817e4Smiod    Hacked by Steve Chamberlain of Cygnus Support (steve@cygnus.com).
7*3d8817e4Smiod 
8*3d8817e4Smiod This file is part of BFD, the Binary File Descriptor library.
9*3d8817e4Smiod 
10*3d8817e4Smiod This program is free software; you can redistribute it and/or modify
11*3d8817e4Smiod it under the terms of the GNU General Public License as published by
12*3d8817e4Smiod the Free Software Foundation; either version 2 of the License, or
13*3d8817e4Smiod (at your option) any later version.
14*3d8817e4Smiod 
15*3d8817e4Smiod This program is distributed in the hope that it will be useful,
16*3d8817e4Smiod but WITHOUT ANY WARRANTY; without even the implied warranty of
17*3d8817e4Smiod MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*3d8817e4Smiod GNU General Public License for more details.
19*3d8817e4Smiod 
20*3d8817e4Smiod You should have received a copy of the GNU General Public License
21*3d8817e4Smiod along with this program; if not, write to the Free Software
22*3d8817e4Smiod Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
23*3d8817e4Smiod 
24*3d8817e4Smiod /*
25*3d8817e4Smiod SECTION
26*3d8817e4Smiod 	File caching
27*3d8817e4Smiod 
28*3d8817e4Smiod 	The file caching mechanism is embedded within BFD and allows
29*3d8817e4Smiod 	the application to open as many BFDs as it wants without
30*3d8817e4Smiod 	regard to the underlying operating system's file descriptor
31*3d8817e4Smiod 	limit (often as low as 20 open files).  The module in
32*3d8817e4Smiod 	<<cache.c>> maintains a least recently used list of
33*3d8817e4Smiod 	<<BFD_CACHE_MAX_OPEN>> files, and exports the name
34*3d8817e4Smiod 	<<bfd_cache_lookup>>, which runs around and makes sure that
35*3d8817e4Smiod 	the required BFD is open. If not, then it chooses a file to
36*3d8817e4Smiod 	close, closes it and opens the one wanted, returning its file
37*3d8817e4Smiod 	handle.
38*3d8817e4Smiod 
39*3d8817e4Smiod SUBSECTION
40*3d8817e4Smiod 	Caching functions
41*3d8817e4Smiod */
42*3d8817e4Smiod 
43*3d8817e4Smiod #include "bfd.h"
44*3d8817e4Smiod #include "sysdep.h"
45*3d8817e4Smiod #include "libbfd.h"
46*3d8817e4Smiod #include "libiberty.h"
47*3d8817e4Smiod 
48*3d8817e4Smiod /* In some cases we can optimize cache operation when reopening files.
49*3d8817e4Smiod    For instance, a flush is entirely unnecessary if the file is already
50*3d8817e4Smiod    closed, so a flush would use CACHE_NO_OPEN.  Similarly, a seek using
51*3d8817e4Smiod    SEEK_SET or SEEK_END need not first seek to the current position.
52*3d8817e4Smiod    For stat we ignore seek errors, just in case the file has changed
53*3d8817e4Smiod    while we weren't looking.  If it has, then it's possible that the
54*3d8817e4Smiod    file is shorter and we don't want a seek error to prevent us doing
55*3d8817e4Smiod    the stat.  */
56*3d8817e4Smiod enum cache_flag {
57*3d8817e4Smiod   CACHE_NORMAL = 0,
58*3d8817e4Smiod   CACHE_NO_OPEN = 1,
59*3d8817e4Smiod   CACHE_NO_SEEK = 2,
60*3d8817e4Smiod   CACHE_NO_SEEK_ERROR = 4
61*3d8817e4Smiod };
62*3d8817e4Smiod 
63*3d8817e4Smiod /* The maximum number of files which the cache will keep open at
64*3d8817e4Smiod    one time.  */
65*3d8817e4Smiod 
66*3d8817e4Smiod #define BFD_CACHE_MAX_OPEN 10
67*3d8817e4Smiod 
68*3d8817e4Smiod /* The number of BFD files we have open.  */
69*3d8817e4Smiod 
70*3d8817e4Smiod static int open_files;
71*3d8817e4Smiod 
72*3d8817e4Smiod /* Zero, or a pointer to the topmost BFD on the chain.  This is
73*3d8817e4Smiod    used by the <<bfd_cache_lookup>> macro in @file{libbfd.h} to
74*3d8817e4Smiod    determine when it can avoid a function call.  */
75*3d8817e4Smiod 
76*3d8817e4Smiod static bfd *bfd_last_cache = NULL;
77*3d8817e4Smiod 
78*3d8817e4Smiod /* Insert a BFD into the cache.  */
79*3d8817e4Smiod 
80*3d8817e4Smiod static void
insert(bfd * abfd)81*3d8817e4Smiod insert (bfd *abfd)
82*3d8817e4Smiod {
83*3d8817e4Smiod   if (bfd_last_cache == NULL)
84*3d8817e4Smiod     {
85*3d8817e4Smiod       abfd->lru_next = abfd;
86*3d8817e4Smiod       abfd->lru_prev = abfd;
87*3d8817e4Smiod     }
88*3d8817e4Smiod   else
89*3d8817e4Smiod     {
90*3d8817e4Smiod       abfd->lru_next = bfd_last_cache;
91*3d8817e4Smiod       abfd->lru_prev = bfd_last_cache->lru_prev;
92*3d8817e4Smiod       abfd->lru_prev->lru_next = abfd;
93*3d8817e4Smiod       abfd->lru_next->lru_prev = abfd;
94*3d8817e4Smiod     }
95*3d8817e4Smiod   bfd_last_cache = abfd;
96*3d8817e4Smiod }
97*3d8817e4Smiod 
98*3d8817e4Smiod /* Remove a BFD from the cache.  */
99*3d8817e4Smiod 
100*3d8817e4Smiod static void
snip(bfd * abfd)101*3d8817e4Smiod snip (bfd *abfd)
102*3d8817e4Smiod {
103*3d8817e4Smiod   abfd->lru_prev->lru_next = abfd->lru_next;
104*3d8817e4Smiod   abfd->lru_next->lru_prev = abfd->lru_prev;
105*3d8817e4Smiod   if (abfd == bfd_last_cache)
106*3d8817e4Smiod     {
107*3d8817e4Smiod       bfd_last_cache = abfd->lru_next;
108*3d8817e4Smiod       if (abfd == bfd_last_cache)
109*3d8817e4Smiod 	bfd_last_cache = NULL;
110*3d8817e4Smiod     }
111*3d8817e4Smiod }
112*3d8817e4Smiod 
113*3d8817e4Smiod /* Close a BFD and remove it from the cache.  */
114*3d8817e4Smiod 
115*3d8817e4Smiod static bfd_boolean
bfd_cache_delete(bfd * abfd)116*3d8817e4Smiod bfd_cache_delete (bfd *abfd)
117*3d8817e4Smiod {
118*3d8817e4Smiod   bfd_boolean ret;
119*3d8817e4Smiod 
120*3d8817e4Smiod   if (fclose ((FILE *) abfd->iostream) == 0)
121*3d8817e4Smiod     ret = TRUE;
122*3d8817e4Smiod   else
123*3d8817e4Smiod     {
124*3d8817e4Smiod       ret = FALSE;
125*3d8817e4Smiod       bfd_set_error (bfd_error_system_call);
126*3d8817e4Smiod     }
127*3d8817e4Smiod 
128*3d8817e4Smiod   snip (abfd);
129*3d8817e4Smiod 
130*3d8817e4Smiod   abfd->iostream = NULL;
131*3d8817e4Smiod   --open_files;
132*3d8817e4Smiod 
133*3d8817e4Smiod   return ret;
134*3d8817e4Smiod }
135*3d8817e4Smiod 
136*3d8817e4Smiod /* We need to open a new file, and the cache is full.  Find the least
137*3d8817e4Smiod    recently used cacheable BFD and close it.  */
138*3d8817e4Smiod 
139*3d8817e4Smiod static bfd_boolean
close_one(void)140*3d8817e4Smiod close_one (void)
141*3d8817e4Smiod {
142*3d8817e4Smiod   register bfd *kill;
143*3d8817e4Smiod 
144*3d8817e4Smiod   if (bfd_last_cache == NULL)
145*3d8817e4Smiod     kill = NULL;
146*3d8817e4Smiod   else
147*3d8817e4Smiod     {
148*3d8817e4Smiod       for (kill = bfd_last_cache->lru_prev;
149*3d8817e4Smiod 	   ! kill->cacheable;
150*3d8817e4Smiod 	   kill = kill->lru_prev)
151*3d8817e4Smiod 	{
152*3d8817e4Smiod 	  if (kill == bfd_last_cache)
153*3d8817e4Smiod 	    {
154*3d8817e4Smiod 	      kill = NULL;
155*3d8817e4Smiod 	      break;
156*3d8817e4Smiod 	    }
157*3d8817e4Smiod 	}
158*3d8817e4Smiod     }
159*3d8817e4Smiod 
160*3d8817e4Smiod   if (kill == NULL)
161*3d8817e4Smiod     {
162*3d8817e4Smiod       /* There are no open cacheable BFD's.  */
163*3d8817e4Smiod       return TRUE;
164*3d8817e4Smiod     }
165*3d8817e4Smiod 
166*3d8817e4Smiod   kill->where = real_ftell ((FILE *) kill->iostream);
167*3d8817e4Smiod 
168*3d8817e4Smiod   /* Save the file st_mtime.  This is a hack so that gdb can detect when
169*3d8817e4Smiod      an executable has been deleted and recreated.  The only thing that
170*3d8817e4Smiod      makes this reasonable is that st_mtime doesn't change when a file
171*3d8817e4Smiod      is unlinked, so saving st_mtime makes BFD's file cache operation
172*3d8817e4Smiod      a little more transparent for this particular usage pattern.  If we
173*3d8817e4Smiod      hadn't closed the file then we would not have lost the original
174*3d8817e4Smiod      contents, st_mtime etc.  Of course, if something is writing to an
175*3d8817e4Smiod      existing file, then this is the wrong thing to do.
176*3d8817e4Smiod      FIXME: gdb should save these times itself on first opening a file,
177*3d8817e4Smiod      and this hack be removed.  */
178*3d8817e4Smiod   if (kill->direction == no_direction || kill->direction == read_direction)
179*3d8817e4Smiod     {
180*3d8817e4Smiod       bfd_get_mtime (kill);
181*3d8817e4Smiod       kill->mtime_set = TRUE;
182*3d8817e4Smiod     }
183*3d8817e4Smiod 
184*3d8817e4Smiod   return bfd_cache_delete (kill);
185*3d8817e4Smiod }
186*3d8817e4Smiod 
187*3d8817e4Smiod /* Check to see if the required BFD is the same as the last one
188*3d8817e4Smiod    looked up. If so, then it can use the stream in the BFD with
189*3d8817e4Smiod    impunity, since it can't have changed since the last lookup;
190*3d8817e4Smiod    otherwise, it has to perform the complicated lookup function.  */
191*3d8817e4Smiod 
192*3d8817e4Smiod #define bfd_cache_lookup(x, flag) \
193*3d8817e4Smiod   ((x) == bfd_last_cache			\
194*3d8817e4Smiod    ? (FILE *) (bfd_last_cache->iostream)	\
195*3d8817e4Smiod    : bfd_cache_lookup_worker (x, flag))
196*3d8817e4Smiod 
197*3d8817e4Smiod /* Called when the macro <<bfd_cache_lookup>> fails to find a
198*3d8817e4Smiod    quick answer.  Find a file descriptor for @var{abfd}.  If
199*3d8817e4Smiod    necessary, it open it.  If there are already more than
200*3d8817e4Smiod    <<BFD_CACHE_MAX_OPEN>> files open, it tries to close one first, to
201*3d8817e4Smiod    avoid running out of file descriptors.  It will return NULL
202*3d8817e4Smiod    if it is unable to (re)open the @var{abfd}.  */
203*3d8817e4Smiod 
204*3d8817e4Smiod static FILE *
bfd_cache_lookup_worker(bfd * abfd,enum cache_flag flag)205*3d8817e4Smiod bfd_cache_lookup_worker (bfd *abfd, enum cache_flag flag)
206*3d8817e4Smiod {
207*3d8817e4Smiod   bfd *orig_bfd = abfd;
208*3d8817e4Smiod   if ((abfd->flags & BFD_IN_MEMORY) != 0)
209*3d8817e4Smiod     abort ();
210*3d8817e4Smiod 
211*3d8817e4Smiod   if (abfd->my_archive)
212*3d8817e4Smiod     abfd = abfd->my_archive;
213*3d8817e4Smiod 
214*3d8817e4Smiod   if (abfd->iostream != NULL)
215*3d8817e4Smiod     {
216*3d8817e4Smiod       /* Move the file to the start of the cache.  */
217*3d8817e4Smiod       if (abfd != bfd_last_cache)
218*3d8817e4Smiod 	{
219*3d8817e4Smiod 	  snip (abfd);
220*3d8817e4Smiod 	  insert (abfd);
221*3d8817e4Smiod 	}
222*3d8817e4Smiod       return (FILE *) abfd->iostream;
223*3d8817e4Smiod     }
224*3d8817e4Smiod 
225*3d8817e4Smiod   if (flag & CACHE_NO_OPEN)
226*3d8817e4Smiod     return NULL;
227*3d8817e4Smiod 
228*3d8817e4Smiod   if (bfd_open_file (abfd) == NULL)
229*3d8817e4Smiod     ;
230*3d8817e4Smiod   else if (!(flag & CACHE_NO_SEEK)
231*3d8817e4Smiod 	   && real_fseek ((FILE *) abfd->iostream, abfd->where, SEEK_SET) != 0
232*3d8817e4Smiod 	   && !(flag & CACHE_NO_SEEK_ERROR))
233*3d8817e4Smiod     bfd_set_error (bfd_error_system_call);
234*3d8817e4Smiod   else
235*3d8817e4Smiod     return (FILE *) abfd->iostream;
236*3d8817e4Smiod 
237*3d8817e4Smiod   (*_bfd_error_handler) (_("reopening %B: %s\n"),
238*3d8817e4Smiod 			 orig_bfd, bfd_errmsg (bfd_get_error ()));
239*3d8817e4Smiod   return NULL;
240*3d8817e4Smiod }
241*3d8817e4Smiod 
242*3d8817e4Smiod static file_ptr
cache_btell(struct bfd * abfd)243*3d8817e4Smiod cache_btell (struct bfd *abfd)
244*3d8817e4Smiod {
245*3d8817e4Smiod   FILE *f = bfd_cache_lookup (abfd, CACHE_NO_OPEN);
246*3d8817e4Smiod   if (f == NULL)
247*3d8817e4Smiod     return abfd->where;
248*3d8817e4Smiod   return real_ftell (f);
249*3d8817e4Smiod }
250*3d8817e4Smiod 
251*3d8817e4Smiod static int
cache_bseek(struct bfd * abfd,file_ptr offset,int whence)252*3d8817e4Smiod cache_bseek (struct bfd *abfd, file_ptr offset, int whence)
253*3d8817e4Smiod {
254*3d8817e4Smiod   FILE *f = bfd_cache_lookup (abfd, whence != SEEK_CUR ? CACHE_NO_SEEK : 0);
255*3d8817e4Smiod   if (f == NULL)
256*3d8817e4Smiod     return -1;
257*3d8817e4Smiod   return real_fseek (f, offset, whence);
258*3d8817e4Smiod }
259*3d8817e4Smiod 
260*3d8817e4Smiod /* Note that archive entries don't have streams; they share their parent's.
261*3d8817e4Smiod    This allows someone to play with the iostream behind BFD's back.
262*3d8817e4Smiod 
263*3d8817e4Smiod    Also, note that the origin pointer points to the beginning of a file's
264*3d8817e4Smiod    contents (0 for non-archive elements).  For archive entries this is the
265*3d8817e4Smiod    first octet in the file, NOT the beginning of the archive header.  */
266*3d8817e4Smiod 
267*3d8817e4Smiod static file_ptr
cache_bread(struct bfd * abfd,void * buf,file_ptr nbytes)268*3d8817e4Smiod cache_bread (struct bfd *abfd, void *buf, file_ptr nbytes)
269*3d8817e4Smiod {
270*3d8817e4Smiod   FILE *f;
271*3d8817e4Smiod   file_ptr nread;
272*3d8817e4Smiod   /* FIXME - this looks like an optimization, but it's really to cover
273*3d8817e4Smiod      up for a feature of some OSs (not solaris - sigh) that
274*3d8817e4Smiod      ld/pe-dll.c takes advantage of (apparently) when it creates BFDs
275*3d8817e4Smiod      internally and tries to link against them.  BFD seems to be smart
276*3d8817e4Smiod      enough to realize there are no symbol records in the "file" that
277*3d8817e4Smiod      doesn't exist but attempts to read them anyway.  On Solaris,
278*3d8817e4Smiod      attempting to read zero bytes from a NULL file results in a core
279*3d8817e4Smiod      dump, but on other platforms it just returns zero bytes read.
280*3d8817e4Smiod      This makes it to something reasonable. - DJ */
281*3d8817e4Smiod   if (nbytes == 0)
282*3d8817e4Smiod     return 0;
283*3d8817e4Smiod 
284*3d8817e4Smiod   f = bfd_cache_lookup (abfd, 0);
285*3d8817e4Smiod   if (f == NULL)
286*3d8817e4Smiod     return 0;
287*3d8817e4Smiod 
288*3d8817e4Smiod #if defined (__VAX) && defined (VMS)
289*3d8817e4Smiod   /* Apparently fread on Vax VMS does not keep the record length
290*3d8817e4Smiod      information.  */
291*3d8817e4Smiod   nread = read (fileno (f), buf, nbytes);
292*3d8817e4Smiod   /* Set bfd_error if we did not read as much data as we expected.  If
293*3d8817e4Smiod      the read failed due to an error set the bfd_error_system_call,
294*3d8817e4Smiod      else set bfd_error_file_truncated.  */
295*3d8817e4Smiod   if (nread == (file_ptr)-1)
296*3d8817e4Smiod     {
297*3d8817e4Smiod       bfd_set_error (bfd_error_system_call);
298*3d8817e4Smiod       return -1;
299*3d8817e4Smiod     }
300*3d8817e4Smiod #else
301*3d8817e4Smiod   nread = fread (buf, 1, nbytes, f);
302*3d8817e4Smiod   /* Set bfd_error if we did not read as much data as we expected.  If
303*3d8817e4Smiod      the read failed due to an error set the bfd_error_system_call,
304*3d8817e4Smiod      else set bfd_error_file_truncated.  */
305*3d8817e4Smiod   if (nread < nbytes && ferror (f))
306*3d8817e4Smiod     {
307*3d8817e4Smiod       bfd_set_error (bfd_error_system_call);
308*3d8817e4Smiod       return -1;
309*3d8817e4Smiod     }
310*3d8817e4Smiod #endif
311*3d8817e4Smiod   return nread;
312*3d8817e4Smiod }
313*3d8817e4Smiod 
314*3d8817e4Smiod static file_ptr
cache_bwrite(struct bfd * abfd,const void * where,file_ptr nbytes)315*3d8817e4Smiod cache_bwrite (struct bfd *abfd, const void *where, file_ptr nbytes)
316*3d8817e4Smiod {
317*3d8817e4Smiod   file_ptr nwrite;
318*3d8817e4Smiod   FILE *f = bfd_cache_lookup (abfd, 0);
319*3d8817e4Smiod   if (f == NULL)
320*3d8817e4Smiod     return 0;
321*3d8817e4Smiod   nwrite = fwrite (where, 1, nbytes, f);
322*3d8817e4Smiod   if (nwrite < nbytes && ferror (f))
323*3d8817e4Smiod     {
324*3d8817e4Smiod       bfd_set_error (bfd_error_system_call);
325*3d8817e4Smiod       return -1;
326*3d8817e4Smiod     }
327*3d8817e4Smiod   return nwrite;
328*3d8817e4Smiod }
329*3d8817e4Smiod 
330*3d8817e4Smiod static int
cache_bclose(struct bfd * abfd)331*3d8817e4Smiod cache_bclose (struct bfd *abfd)
332*3d8817e4Smiod {
333*3d8817e4Smiod   return bfd_cache_close (abfd);
334*3d8817e4Smiod }
335*3d8817e4Smiod 
336*3d8817e4Smiod static int
cache_bflush(struct bfd * abfd)337*3d8817e4Smiod cache_bflush (struct bfd *abfd)
338*3d8817e4Smiod {
339*3d8817e4Smiod   int sts;
340*3d8817e4Smiod   FILE *f = bfd_cache_lookup (abfd, CACHE_NO_OPEN);
341*3d8817e4Smiod   if (f == NULL)
342*3d8817e4Smiod     return 0;
343*3d8817e4Smiod   sts = fflush (f);
344*3d8817e4Smiod   if (sts < 0)
345*3d8817e4Smiod     bfd_set_error (bfd_error_system_call);
346*3d8817e4Smiod   return sts;
347*3d8817e4Smiod }
348*3d8817e4Smiod 
349*3d8817e4Smiod static int
cache_bstat(struct bfd * abfd,struct stat * sb)350*3d8817e4Smiod cache_bstat (struct bfd *abfd, struct stat *sb)
351*3d8817e4Smiod {
352*3d8817e4Smiod   int sts;
353*3d8817e4Smiod   FILE *f = bfd_cache_lookup (abfd, CACHE_NO_SEEK_ERROR);
354*3d8817e4Smiod   if (f == NULL)
355*3d8817e4Smiod     return -1;
356*3d8817e4Smiod   sts = fstat (fileno (f), sb);
357*3d8817e4Smiod   if (sts < 0)
358*3d8817e4Smiod     bfd_set_error (bfd_error_system_call);
359*3d8817e4Smiod   return sts;
360*3d8817e4Smiod }
361*3d8817e4Smiod 
362*3d8817e4Smiod static const struct bfd_iovec cache_iovec = {
363*3d8817e4Smiod   &cache_bread, &cache_bwrite, &cache_btell, &cache_bseek,
364*3d8817e4Smiod   &cache_bclose, &cache_bflush, &cache_bstat
365*3d8817e4Smiod };
366*3d8817e4Smiod 
367*3d8817e4Smiod /*
368*3d8817e4Smiod INTERNAL_FUNCTION
369*3d8817e4Smiod 	bfd_cache_init
370*3d8817e4Smiod 
371*3d8817e4Smiod SYNOPSIS
372*3d8817e4Smiod 	bfd_boolean bfd_cache_init (bfd *abfd);
373*3d8817e4Smiod 
374*3d8817e4Smiod DESCRIPTION
375*3d8817e4Smiod 	Add a newly opened BFD to the cache.
376*3d8817e4Smiod */
377*3d8817e4Smiod 
378*3d8817e4Smiod bfd_boolean
bfd_cache_init(bfd * abfd)379*3d8817e4Smiod bfd_cache_init (bfd *abfd)
380*3d8817e4Smiod {
381*3d8817e4Smiod   BFD_ASSERT (abfd->iostream != NULL);
382*3d8817e4Smiod   if (open_files >= BFD_CACHE_MAX_OPEN)
383*3d8817e4Smiod     {
384*3d8817e4Smiod       if (! close_one ())
385*3d8817e4Smiod 	return FALSE;
386*3d8817e4Smiod     }
387*3d8817e4Smiod   abfd->iovec = &cache_iovec;
388*3d8817e4Smiod   insert (abfd);
389*3d8817e4Smiod   ++open_files;
390*3d8817e4Smiod   return TRUE;
391*3d8817e4Smiod }
392*3d8817e4Smiod 
393*3d8817e4Smiod /*
394*3d8817e4Smiod INTERNAL_FUNCTION
395*3d8817e4Smiod 	bfd_cache_close
396*3d8817e4Smiod 
397*3d8817e4Smiod SYNOPSIS
398*3d8817e4Smiod 	bfd_boolean bfd_cache_close (bfd *abfd);
399*3d8817e4Smiod 
400*3d8817e4Smiod DESCRIPTION
401*3d8817e4Smiod 	Remove the BFD @var{abfd} from the cache. If the attached file is open,
402*3d8817e4Smiod 	then close it too.
403*3d8817e4Smiod 
404*3d8817e4Smiod RETURNS
405*3d8817e4Smiod 	<<FALSE>> is returned if closing the file fails, <<TRUE>> is
406*3d8817e4Smiod 	returned if all is well.
407*3d8817e4Smiod */
408*3d8817e4Smiod 
409*3d8817e4Smiod bfd_boolean
bfd_cache_close(bfd * abfd)410*3d8817e4Smiod bfd_cache_close (bfd *abfd)
411*3d8817e4Smiod {
412*3d8817e4Smiod   if (abfd->iovec != &cache_iovec)
413*3d8817e4Smiod     return TRUE;
414*3d8817e4Smiod 
415*3d8817e4Smiod   if (abfd->iostream == NULL)
416*3d8817e4Smiod     /* Previously closed.  */
417*3d8817e4Smiod     return TRUE;
418*3d8817e4Smiod 
419*3d8817e4Smiod   return bfd_cache_delete (abfd);
420*3d8817e4Smiod }
421*3d8817e4Smiod 
422*3d8817e4Smiod /*
423*3d8817e4Smiod FUNCTION
424*3d8817e4Smiod 	bfd_cache_close_all
425*3d8817e4Smiod 
426*3d8817e4Smiod SYNOPSIS
427*3d8817e4Smiod 	bfd_boolean bfd_cache_close_all (void);
428*3d8817e4Smiod 
429*3d8817e4Smiod DESCRIPTION
430*3d8817e4Smiod 	Remove all BFDs from the cache. If the attached file is open,
431*3d8817e4Smiod 	then close it too.
432*3d8817e4Smiod 
433*3d8817e4Smiod RETURNS
434*3d8817e4Smiod 	<<FALSE>> is returned if closing one of the file fails, <<TRUE>> is
435*3d8817e4Smiod 	returned if all is well.
436*3d8817e4Smiod */
437*3d8817e4Smiod 
438*3d8817e4Smiod bfd_boolean
bfd_cache_close_all()439*3d8817e4Smiod bfd_cache_close_all ()
440*3d8817e4Smiod {
441*3d8817e4Smiod   bfd_boolean ret = TRUE;
442*3d8817e4Smiod 
443*3d8817e4Smiod   while (bfd_last_cache != NULL)
444*3d8817e4Smiod     ret &= bfd_cache_close (bfd_last_cache);
445*3d8817e4Smiod 
446*3d8817e4Smiod   return ret;
447*3d8817e4Smiod }
448*3d8817e4Smiod 
449*3d8817e4Smiod /*
450*3d8817e4Smiod INTERNAL_FUNCTION
451*3d8817e4Smiod 	bfd_open_file
452*3d8817e4Smiod 
453*3d8817e4Smiod SYNOPSIS
454*3d8817e4Smiod 	FILE* bfd_open_file (bfd *abfd);
455*3d8817e4Smiod 
456*3d8817e4Smiod DESCRIPTION
457*3d8817e4Smiod 	Call the OS to open a file for @var{abfd}.  Return the <<FILE *>>
458*3d8817e4Smiod 	(possibly <<NULL>>) that results from this operation.  Set up the
459*3d8817e4Smiod 	BFD so that future accesses know the file is open. If the <<FILE *>>
460*3d8817e4Smiod 	returned is <<NULL>>, then it won't have been put in the
461*3d8817e4Smiod 	cache, so it won't have to be removed from it.
462*3d8817e4Smiod */
463*3d8817e4Smiod 
464*3d8817e4Smiod FILE *
bfd_open_file(bfd * abfd)465*3d8817e4Smiod bfd_open_file (bfd *abfd)
466*3d8817e4Smiod {
467*3d8817e4Smiod   abfd->cacheable = TRUE;	/* Allow it to be closed later.  */
468*3d8817e4Smiod 
469*3d8817e4Smiod   if (open_files >= BFD_CACHE_MAX_OPEN)
470*3d8817e4Smiod     {
471*3d8817e4Smiod       if (! close_one ())
472*3d8817e4Smiod 	return NULL;
473*3d8817e4Smiod     }
474*3d8817e4Smiod 
475*3d8817e4Smiod   switch (abfd->direction)
476*3d8817e4Smiod     {
477*3d8817e4Smiod     case read_direction:
478*3d8817e4Smiod     case no_direction:
479*3d8817e4Smiod       abfd->iostream = (PTR) real_fopen (abfd->filename, FOPEN_RB);
480*3d8817e4Smiod       break;
481*3d8817e4Smiod     case both_direction:
482*3d8817e4Smiod     case write_direction:
483*3d8817e4Smiod       if (abfd->opened_once)
484*3d8817e4Smiod 	{
485*3d8817e4Smiod 	  abfd->iostream = (PTR) real_fopen (abfd->filename, FOPEN_RUB);
486*3d8817e4Smiod 	  if (abfd->iostream == NULL)
487*3d8817e4Smiod 	    abfd->iostream = (PTR) real_fopen (abfd->filename, FOPEN_WUB);
488*3d8817e4Smiod 	}
489*3d8817e4Smiod       else
490*3d8817e4Smiod 	{
491*3d8817e4Smiod 	  /* Create the file.
492*3d8817e4Smiod 
493*3d8817e4Smiod 	     Some operating systems won't let us overwrite a running
494*3d8817e4Smiod 	     binary.  For them, we want to unlink the file first.
495*3d8817e4Smiod 
496*3d8817e4Smiod 	     However, gcc 2.95 will create temporary files using
497*3d8817e4Smiod 	     O_EXCL and tight permissions to prevent other users from
498*3d8817e4Smiod 	     substituting other .o files during the compilation.  gcc
499*3d8817e4Smiod 	     will then tell the assembler to use the newly created
500*3d8817e4Smiod 	     file as an output file.  If we unlink the file here, we
501*3d8817e4Smiod 	     open a brief window when another user could still
502*3d8817e4Smiod 	     substitute a file.
503*3d8817e4Smiod 
504*3d8817e4Smiod 	     So we unlink the output file if and only if it has
505*3d8817e4Smiod 	     non-zero size.  */
506*3d8817e4Smiod #ifndef __MSDOS__
507*3d8817e4Smiod 	  /* Don't do this for MSDOS: it doesn't care about overwriting
508*3d8817e4Smiod 	     a running binary, but if this file is already open by
509*3d8817e4Smiod 	     another BFD, we will be in deep trouble if we delete an
510*3d8817e4Smiod 	     open file.  In fact, objdump does just that if invoked with
511*3d8817e4Smiod 	     the --info option.  */
512*3d8817e4Smiod 	  struct stat s;
513*3d8817e4Smiod 
514*3d8817e4Smiod 	  if (stat (abfd->filename, &s) == 0 && s.st_size != 0)
515*3d8817e4Smiod 	    unlink_if_ordinary (abfd->filename);
516*3d8817e4Smiod #endif
517*3d8817e4Smiod 	  abfd->iostream = (PTR) real_fopen (abfd->filename, FOPEN_WUB);
518*3d8817e4Smiod 	  abfd->opened_once = TRUE;
519*3d8817e4Smiod 	}
520*3d8817e4Smiod       break;
521*3d8817e4Smiod     }
522*3d8817e4Smiod 
523*3d8817e4Smiod   if (abfd->iostream == NULL)
524*3d8817e4Smiod     bfd_set_error (bfd_error_system_call);
525*3d8817e4Smiod   else
526*3d8817e4Smiod     {
527*3d8817e4Smiod       if (! bfd_cache_init (abfd))
528*3d8817e4Smiod 	return NULL;
529*3d8817e4Smiod     }
530*3d8817e4Smiod 
531*3d8817e4Smiod   return (FILE *) abfd->iostream;
532*3d8817e4Smiod }
533