Subversion Repositories Kolibri OS

Rev

Rev 5199 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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