Add a fast_hash function in common-utils
[binutils-gdb.git] / bfd / bfdio.c
blob92dd9803e70a51fbc5fc634ebb8d8746261673aa
1 /* Low-level I/O routines for BFDs.
3 Copyright (C) 1990-2019 Free Software Foundation, Inc.
5 Written by Cygnus Support.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
24 #include "sysdep.h"
25 #include <limits.h>
26 #include "bfd.h"
27 #include "libbfd.h"
29 #ifndef S_IXUSR
30 #define S_IXUSR 0100 /* Execute by owner. */
31 #endif
32 #ifndef S_IXGRP
33 #define S_IXGRP 0010 /* Execute by group. */
34 #endif
35 #ifndef S_IXOTH
36 #define S_IXOTH 0001 /* Execute by others. */
37 #endif
39 #ifndef FD_CLOEXEC
40 #define FD_CLOEXEC 1
41 #endif
43 file_ptr
44 _bfd_real_ftell (FILE *file)
46 #if defined (HAVE_FTELLO64)
47 return ftello64 (file);
48 #elif defined (HAVE_FTELLO)
49 return ftello (file);
50 #else
51 return ftell (file);
52 #endif
55 int
56 _bfd_real_fseek (FILE *file, file_ptr offset, int whence)
58 #if defined (HAVE_FSEEKO64)
59 return fseeko64 (file, offset, whence);
60 #elif defined (HAVE_FSEEKO)
61 return fseeko (file, offset, whence);
62 #else
63 return fseek (file, offset, whence);
64 #endif
67 /* Mark FILE as close-on-exec. Return FILE. FILE may be NULL, in
68 which case nothing is done. */
69 static FILE *
70 close_on_exec (FILE *file)
72 #if defined (HAVE_FILENO) && defined (F_GETFD)
73 if (file)
75 int fd = fileno (file);
76 int old = fcntl (fd, F_GETFD, 0);
77 if (old >= 0)
78 fcntl (fd, F_SETFD, old | FD_CLOEXEC);
80 #endif
81 return file;
84 FILE *
85 _bfd_real_fopen (const char *filename, const char *modes)
87 #ifdef VMS
88 char *vms_attr;
90 /* On VMS, fopen allows file attributes as optional arguments.
91 We need to use them but we'd better to use the common prototype.
92 In fopen-vms.h, they are separated from the mode with a comma.
93 Split here. */
94 vms_attr = strchr (modes, ',');
95 if (vms_attr == NULL)
97 /* No attributes. */
98 return close_on_exec (fopen (filename, modes));
100 else
102 /* Attributes found. Split. */
103 size_t modes_len = strlen (modes) + 1;
104 char attrs[modes_len + 1];
105 char *at[3];
106 int i;
108 memcpy (attrs, modes, modes_len);
109 at[0] = attrs;
110 for (i = 0; i < 2; i++)
112 at[i + 1] = strchr (at[i], ',');
113 BFD_ASSERT (at[i + 1] != NULL);
114 *(at[i + 1]++) = 0; /* Replace ',' with a nul, and skip it. */
116 return close_on_exec (fopen (filename, at[0], at[1], at[2]));
118 #else /* !VMS */
119 #if defined (HAVE_FOPEN64)
120 return close_on_exec (fopen64 (filename, modes));
121 #else
122 return close_on_exec (fopen (filename, modes));
123 #endif
124 #endif /* !VMS */
128 INTERNAL_DEFINITION
129 struct bfd_iovec
131 DESCRIPTION
133 The <<struct bfd_iovec>> contains the internal file I/O class.
134 Each <<BFD>> has an instance of this class and all file I/O is
135 routed through it (it is assumed that the instance implements
136 all methods listed below).
138 .struct bfd_iovec
140 . {* To avoid problems with macros, a "b" rather than "f"
141 . prefix is prepended to each method name. *}
142 . {* Attempt to read/write NBYTES on ABFD's IOSTREAM storing/fetching
143 . bytes starting at PTR. Return the number of bytes actually
144 . transfered (a read past end-of-file returns less than NBYTES),
145 . or -1 (setting <<bfd_error>>) if an error occurs. *}
146 . file_ptr (*bread) (struct bfd *abfd, void *ptr, file_ptr nbytes);
147 . file_ptr (*bwrite) (struct bfd *abfd, const void *ptr,
148 . file_ptr nbytes);
149 . {* Return the current IOSTREAM file offset, or -1 (setting <<bfd_error>>
150 . if an error occurs. *}
151 . file_ptr (*btell) (struct bfd *abfd);
152 . {* For the following, on successful completion a value of 0 is returned.
153 . Otherwise, a value of -1 is returned (and <<bfd_error>> is set). *}
154 . int (*bseek) (struct bfd *abfd, file_ptr offset, int whence);
155 . int (*bclose) (struct bfd *abfd);
156 . int (*bflush) (struct bfd *abfd);
157 . int (*bstat) (struct bfd *abfd, struct stat *sb);
158 . {* Mmap a part of the files. ADDR, LEN, PROT, FLAGS and OFFSET are the usual
159 . mmap parameter, except that LEN and OFFSET do not need to be page
160 . aligned. Returns (void *)-1 on failure, mmapped address on success.
161 . Also write in MAP_ADDR the address of the page aligned buffer and in
162 . MAP_LEN the size mapped (a page multiple). Use unmap with MAP_ADDR and
163 . MAP_LEN to unmap. *}
164 . void *(*bmmap) (struct bfd *abfd, void *addr, bfd_size_type len,
165 . int prot, int flags, file_ptr offset,
166 . void **map_addr, bfd_size_type *map_len);
169 .extern const struct bfd_iovec _bfd_memory_iovec;
174 /* Return value is amount read. */
176 bfd_size_type
177 bfd_bread (void *ptr, bfd_size_type size, bfd *abfd)
179 file_ptr nread;
180 bfd *element_bfd = abfd;
181 ufile_ptr offset = 0;
183 while (abfd->my_archive != NULL
184 && !bfd_is_thin_archive (abfd->my_archive))
186 offset += abfd->origin;
187 abfd = abfd->my_archive;
190 /* If this is an archive element, don't read past the end of
191 this element. */
192 if (element_bfd->arelt_data != NULL)
194 bfd_size_type maxbytes = arelt_size (element_bfd);
196 if (abfd->where < offset || abfd->where - offset >= maxbytes)
198 bfd_set_error (bfd_error_invalid_operation);
199 return -1;
201 if (abfd->where - offset + size > maxbytes)
202 size = maxbytes - (abfd->where - offset);
205 if (abfd->iovec == NULL)
207 bfd_set_error (bfd_error_invalid_operation);
208 return -1;
211 nread = abfd->iovec->bread (abfd, ptr, size);
212 if (nread != -1)
213 abfd->where += nread;
215 return nread;
218 bfd_size_type
219 bfd_bwrite (const void *ptr, bfd_size_type size, bfd *abfd)
221 file_ptr nwrote;
223 while (abfd->my_archive != NULL
224 && !bfd_is_thin_archive (abfd->my_archive))
225 abfd = abfd->my_archive;
227 if (abfd->iovec == NULL)
229 bfd_set_error (bfd_error_invalid_operation);
230 return -1;
233 nwrote = abfd->iovec->bwrite (abfd, ptr, size);
234 if (nwrote != -1)
235 abfd->where += nwrote;
236 if ((bfd_size_type) nwrote != size)
238 #ifdef ENOSPC
239 errno = ENOSPC;
240 #endif
241 bfd_set_error (bfd_error_system_call);
243 return nwrote;
246 file_ptr
247 bfd_tell (bfd *abfd)
249 ufile_ptr offset = 0;
250 file_ptr ptr;
252 while (abfd->my_archive != NULL
253 && !bfd_is_thin_archive (abfd->my_archive))
255 offset += abfd->origin;
256 abfd = abfd->my_archive;
259 if (abfd->iovec == NULL)
260 return 0;
262 ptr = abfd->iovec->btell (abfd);
263 abfd->where = ptr;
264 return ptr - offset;
268 bfd_flush (bfd *abfd)
270 while (abfd->my_archive != NULL
271 && !bfd_is_thin_archive (abfd->my_archive))
272 abfd = abfd->my_archive;
274 if (abfd->iovec == NULL)
275 return 0;
277 return abfd->iovec->bflush (abfd);
280 /* Returns 0 for success, negative value for failure (in which case
281 bfd_get_error can retrieve the error code). */
283 bfd_stat (bfd *abfd, struct stat *statbuf)
285 int result;
287 while (abfd->my_archive != NULL
288 && !bfd_is_thin_archive (abfd->my_archive))
289 abfd = abfd->my_archive;
291 if (abfd->iovec == NULL)
293 bfd_set_error (bfd_error_invalid_operation);
294 return -1;
297 result = abfd->iovec->bstat (abfd, statbuf);
298 if (result < 0)
299 bfd_set_error (bfd_error_system_call);
300 return result;
303 /* Returns 0 for success, nonzero for failure (in which case bfd_get_error
304 can retrieve the error code). */
307 bfd_seek (bfd *abfd, file_ptr position, int direction)
309 int result;
310 ufile_ptr offset = 0;
312 while (abfd->my_archive != NULL
313 && !bfd_is_thin_archive (abfd->my_archive))
315 offset += abfd->origin;
316 abfd = abfd->my_archive;
319 if (abfd->iovec == NULL)
321 bfd_set_error (bfd_error_invalid_operation);
322 return -1;
325 /* For the time being, a BFD may not seek to it's end. The problem
326 is that we don't easily have a way to recognize the end of an
327 element in an archive. */
328 BFD_ASSERT (direction == SEEK_SET || direction == SEEK_CUR);
330 if (direction != SEEK_CUR)
331 position += offset;
333 if ((direction == SEEK_CUR && position == 0)
334 || (direction == SEEK_SET && (ufile_ptr) position == abfd->where))
335 return 0;
337 result = abfd->iovec->bseek (abfd, position, direction);
338 if (result != 0)
340 /* An EINVAL error probably means that the file offset was
341 absurd. */
342 if (errno == EINVAL)
343 bfd_set_error (bfd_error_file_truncated);
344 else
345 bfd_set_error (bfd_error_system_call);
347 else
349 /* Adjust `where' field. */
350 if (direction == SEEK_CUR)
351 abfd->where += position;
352 else
353 abfd->where = position;
356 return result;
360 FUNCTION
361 bfd_get_mtime
363 SYNOPSIS
364 long bfd_get_mtime (bfd *abfd);
366 DESCRIPTION
367 Return the file modification time (as read from the file system, or
368 from the archive header for archive members).
372 long
373 bfd_get_mtime (bfd *abfd)
375 struct stat buf;
377 if (abfd->mtime_set)
378 return abfd->mtime;
380 if (bfd_stat (abfd, &buf) != 0)
381 return 0;
383 abfd->mtime = buf.st_mtime; /* Save value in case anyone wants it */
384 return buf.st_mtime;
388 FUNCTION
389 bfd_get_size
391 SYNOPSIS
392 ufile_ptr bfd_get_size (bfd *abfd);
394 DESCRIPTION
395 Return the file size (as read from file system) for the file
396 associated with BFD @var{abfd}.
398 The initial motivation for, and use of, this routine is not
399 so we can get the exact size of the object the BFD applies to, since
400 that might not be generally possible (archive members for example).
401 It would be ideal if someone could eventually modify
402 it so that such results were guaranteed.
404 Instead, we want to ask questions like "is this NNN byte sized
405 object I'm about to try read from file offset YYY reasonable?"
406 As as example of where we might do this, some object formats
407 use string tables for which the first <<sizeof (long)>> bytes of the
408 table contain the size of the table itself, including the size bytes.
409 If an application tries to read what it thinks is one of these
410 string tables, without some way to validate the size, and for
411 some reason the size is wrong (byte swapping error, wrong location
412 for the string table, etc.), the only clue is likely to be a read
413 error when it tries to read the table, or a "virtual memory
414 exhausted" error when it tries to allocate 15 bazillon bytes
415 of space for the 15 bazillon byte table it is about to read.
416 This function at least allows us to answer the question, "is the
417 size reasonable?".
420 ufile_ptr
421 bfd_get_size (bfd *abfd)
423 struct stat buf;
425 if (bfd_stat (abfd, &buf) != 0)
426 return 0;
428 return buf.st_size;
432 FUNCTION
433 bfd_get_file_size
435 SYNOPSIS
436 ufile_ptr bfd_get_file_size (bfd *abfd);
438 DESCRIPTION
439 Return the file size (as read from file system) for the file
440 associated with BFD @var{abfd}. It supports both normal files
441 and archive elements.
445 ufile_ptr
446 bfd_get_file_size (bfd *abfd)
448 if (abfd->my_archive != NULL
449 && !bfd_is_thin_archive (abfd->my_archive))
450 return arelt_size (abfd);
452 return bfd_get_size (abfd);
456 FUNCTION
457 bfd_mmap
459 SYNOPSIS
460 void *bfd_mmap (bfd *abfd, void *addr, bfd_size_type len,
461 int prot, int flags, file_ptr offset,
462 void **map_addr, bfd_size_type *map_len);
464 DESCRIPTION
465 Return mmap()ed region of the file, if possible and implemented.
466 LEN and OFFSET do not need to be page aligned. The page aligned
467 address and length are written to MAP_ADDR and MAP_LEN.
471 void *
472 bfd_mmap (bfd *abfd, void *addr, bfd_size_type len,
473 int prot, int flags, file_ptr offset,
474 void **map_addr, bfd_size_type *map_len)
476 while (abfd->my_archive != NULL
477 && !bfd_is_thin_archive (abfd->my_archive))
479 offset += abfd->origin;
480 abfd = abfd->my_archive;
483 if (abfd->iovec == NULL)
485 bfd_set_error (bfd_error_invalid_operation);
486 return (void *) -1;
489 return abfd->iovec->bmmap (abfd, addr, len, prot, flags, offset,
490 map_addr, map_len);
493 /* Memory file I/O operations. */
495 static file_ptr
496 memory_bread (bfd *abfd, void *ptr, file_ptr size)
498 struct bfd_in_memory *bim;
499 bfd_size_type get;
501 bim = (struct bfd_in_memory *) abfd->iostream;
502 get = size;
503 if (abfd->where + get > bim->size)
505 if (bim->size < (bfd_size_type) abfd->where)
506 get = 0;
507 else
508 get = bim->size - abfd->where;
509 bfd_set_error (bfd_error_file_truncated);
511 memcpy (ptr, bim->buffer + abfd->where, (size_t) get);
512 return get;
515 static file_ptr
516 memory_bwrite (bfd *abfd, const void *ptr, file_ptr size)
518 struct bfd_in_memory *bim = (struct bfd_in_memory *) abfd->iostream;
520 if (abfd->where + size > bim->size)
522 bfd_size_type newsize, oldsize;
524 oldsize = (bim->size + 127) & ~(bfd_size_type) 127;
525 bim->size = abfd->where + size;
526 /* Round up to cut down on memory fragmentation */
527 newsize = (bim->size + 127) & ~(bfd_size_type) 127;
528 if (newsize > oldsize)
530 bim->buffer = (bfd_byte *) bfd_realloc_or_free (bim->buffer, newsize);
531 if (bim->buffer == NULL)
533 bim->size = 0;
534 return 0;
536 if (newsize > bim->size)
537 memset (bim->buffer + bim->size, 0, newsize - bim->size);
540 memcpy (bim->buffer + abfd->where, ptr, (size_t) size);
541 return size;
544 static file_ptr
545 memory_btell (bfd *abfd)
547 return abfd->where;
550 static int
551 memory_bseek (bfd *abfd, file_ptr position, int direction)
553 file_ptr nwhere;
554 struct bfd_in_memory *bim;
556 bim = (struct bfd_in_memory *) abfd->iostream;
558 if (direction == SEEK_SET)
559 nwhere = position;
560 else
561 nwhere = abfd->where + position;
563 if (nwhere < 0)
565 abfd->where = 0;
566 errno = EINVAL;
567 return -1;
570 if ((bfd_size_type)nwhere > bim->size)
572 if (abfd->direction == write_direction
573 || abfd->direction == both_direction)
575 bfd_size_type newsize, oldsize;
577 oldsize = (bim->size + 127) & ~(bfd_size_type) 127;
578 bim->size = nwhere;
579 /* Round up to cut down on memory fragmentation */
580 newsize = (bim->size + 127) & ~(bfd_size_type) 127;
581 if (newsize > oldsize)
583 bim->buffer = (bfd_byte *) bfd_realloc_or_free (bim->buffer, newsize);
584 if (bim->buffer == NULL)
586 errno = EINVAL;
587 bim->size = 0;
588 return -1;
590 memset (bim->buffer + oldsize, 0, newsize - oldsize);
593 else
595 abfd->where = bim->size;
596 errno = EINVAL;
597 bfd_set_error (bfd_error_file_truncated);
598 return -1;
601 return 0;
604 static int
605 memory_bclose (struct bfd *abfd)
607 struct bfd_in_memory *bim = (struct bfd_in_memory *) abfd->iostream;
609 if (bim->buffer != NULL)
610 free (bim->buffer);
611 free (bim);
612 abfd->iostream = NULL;
614 return 0;
617 static int
618 memory_bflush (bfd *abfd ATTRIBUTE_UNUSED)
620 return 0;
623 static int
624 memory_bstat (bfd *abfd, struct stat *statbuf)
626 struct bfd_in_memory *bim = (struct bfd_in_memory *) abfd->iostream;
628 memset (statbuf, 0, sizeof (*statbuf));
629 statbuf->st_size = bim->size;
631 return 0;
634 static void *
635 memory_bmmap (bfd *abfd ATTRIBUTE_UNUSED, void *addr ATTRIBUTE_UNUSED,
636 bfd_size_type len ATTRIBUTE_UNUSED, int prot ATTRIBUTE_UNUSED,
637 int flags ATTRIBUTE_UNUSED, file_ptr offset ATTRIBUTE_UNUSED,
638 void **map_addr ATTRIBUTE_UNUSED,
639 bfd_size_type *map_len ATTRIBUTE_UNUSED)
641 return (void *)-1;
644 const struct bfd_iovec _bfd_memory_iovec =
646 &memory_bread, &memory_bwrite, &memory_btell, &memory_bseek,
647 &memory_bclose, &memory_bflush, &memory_bstat, &memory_bmmap