sim: h8300: switch to common sim-resume
[deliverable/binutils-gdb.git] / bfd / bfdio.c
CommitLineData
93509525 1/* Low-level I/O routines for BFDs.
7c192733 2
b90efa5b 3 Copyright (C) 1990-2015 Free Software Foundation, Inc.
7c192733 4
93509525
KD
5 Written by Cygnus Support.
6
cd123cb7 7 This file is part of BFD, the Binary File Descriptor library.
93509525 8
cd123cb7
NC
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.
93509525 13
cd123cb7
NC
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.
93509525 18
cd123cb7
NC
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. */
93509525
KD
23
24#include "sysdep.h"
3db64b00 25#include <limits.h>
93509525
KD
26#include "bfd.h"
27#include "libbfd.h"
28
93509525
KD
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
38
428b207a
TT
39#ifndef FD_CLOEXEC
40#define FD_CLOEXEC 1
41#endif
42
7c192733
AC
43file_ptr
44real_ftell (FILE *file)
45{
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
53}
54
55int
56real_fseek (FILE *file, file_ptr offset, int whence)
57{
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
65}
66
428b207a
TT
67/* Mark FILE as close-on-exec. Return FILE. FILE may be NULL, in
68 which case nothing is done. */
69static FILE *
70close_on_exec (FILE *file)
71{
72#if defined (HAVE_FILENO) && defined (F_GETFD)
73 if (file)
74 {
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);
79 }
80#endif
81 return file;
82}
83
2e6f4fae
DJ
84FILE *
85real_fopen (const char *filename, const char *modes)
86{
d387240a 87#ifdef VMS
d387240a
TG
88 char *vms_attr;
89
9aff4b7a 90 /* On VMS, fopen allows file attributes as optional arguments.
d387240a
TG
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)
96 {
97 /* No attributes. */
98 return close_on_exec (fopen (filename, modes));
99 }
100 else
101 {
0c376465
TG
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;
107
108 memcpy (attrs, modes, modes_len);
109 at[0] = attrs;
110 for (i = 0; i < 2; i++)
111 {
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. */
115 }
116 return close_on_exec (fopen (filename, at[0], at[1], at[2]));
d387240a
TG
117 }
118#else /* !VMS */
2e6f4fae 119#if defined (HAVE_FOPEN64)
428b207a 120 return close_on_exec (fopen64 (filename, modes));
2e6f4fae 121#else
428b207a 122 return close_on_exec (fopen (filename, modes));
2e6f4fae 123#endif
d387240a 124#endif /* !VMS */
2e6f4fae
DJ
125}
126
40838a72
AC
127/*
128INTERNAL_DEFINITION
129 struct bfd_iovec
93509525 130
40838a72 131DESCRIPTION
93509525 132
40838a72
AC
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).
137
138.struct bfd_iovec
139.{
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);
405bf443 155. int (*bclose) (struct bfd *abfd);
40838a72
AC
156. int (*bflush) (struct bfd *abfd);
157. int (*bstat) (struct bfd *abfd, struct stat *sb);
4c95ab76
TG
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. *}
f07749bb 164. void *(*bmmap) (struct bfd *abfd, void *addr, bfd_size_type len,
4c95ab76
TG
165. int prot, int flags, file_ptr offset,
166. void **map_addr, bfd_size_type *map_len);
40838a72 167.};
93509525 168
65077aa8
TG
169.extern const struct bfd_iovec _bfd_memory_iovec;
170
40838a72 171*/
93509525 172
93509525
KD
173
174/* Return value is amount read. */
175
176bfd_size_type
c58b9523 177bfd_bread (void *ptr, bfd_size_type size, bfd *abfd)
93509525
KD
178{
179 size_t nread;
180
1fb41da4
AM
181 /* If this is an archive element, don't read past the end of
182 this element. */
183 if (abfd->arelt_data != NULL)
184 {
f1bb16f8
NC
185 bfd_size_type maxbytes = arelt_size (abfd);
186
f0b3dbfc
TG
187 if (abfd->where + size > maxbytes)
188 {
189 if (abfd->where >= maxbytes)
190 return 0;
191 size = maxbytes - abfd->where;
192 }
1fb41da4
AM
193 }
194
69fd4758
NC
195 if (abfd->iovec)
196 nread = abfd->iovec->bread (abfd, ptr, size);
197 else
198 nread = 0;
93509525
KD
199 if (nread != (size_t) -1)
200 abfd->where += nread;
201
93509525
KD
202 return nread;
203}
204
205bfd_size_type
c58b9523 206bfd_bwrite (const void *ptr, bfd_size_type size, bfd *abfd)
93509525
KD
207{
208 size_t nwrote;
209
69fd4758
NC
210 if (abfd->iovec)
211 nwrote = abfd->iovec->bwrite (abfd, ptr, size);
212 else
213 nwrote = 0;
214
93509525
KD
215 if (nwrote != (size_t) -1)
216 abfd->where += nwrote;
217 if (nwrote != size)
218 {
219#ifdef ENOSPC
220 errno = ENOSPC;
221#endif
222 bfd_set_error (bfd_error_system_call);
223 }
224 return nwrote;
225}
226
7c192733 227file_ptr
c58b9523 228bfd_tell (bfd *abfd)
93509525
KD
229{
230 file_ptr ptr;
231
69fd4758
NC
232 if (abfd->iovec)
233 {
660722b0 234 bfd *parent_bfd = abfd;
69fd4758
NC
235 ptr = abfd->iovec->btell (abfd);
236
660722b0
TG
237 while (parent_bfd->my_archive != NULL)
238 {
239 ptr -= parent_bfd->origin;
240 parent_bfd = parent_bfd->my_archive;
241 }
69fd4758
NC
242 }
243 else
244 ptr = 0;
93509525 245
93509525
KD
246 abfd->where = ptr;
247 return ptr;
248}
249
250int
c58b9523 251bfd_flush (bfd *abfd)
93509525 252{
69fd4758
NC
253 if (abfd->iovec)
254 return abfd->iovec->bflush (abfd);
255 return 0;
93509525
KD
256}
257
258/* Returns 0 for success, negative value for failure (in which case
259 bfd_get_error can retrieve the error code). */
260int
c58b9523 261bfd_stat (bfd *abfd, struct stat *statbuf)
93509525 262{
93509525
KD
263 int result;
264
69fd4758
NC
265 if (abfd->iovec)
266 result = abfd->iovec->bstat (abfd, statbuf);
267 else
268 result = -1;
269
93509525
KD
270 if (result < 0)
271 bfd_set_error (bfd_error_system_call);
272 return result;
273}
274
275/* Returns 0 for success, nonzero for failure (in which case bfd_get_error
276 can retrieve the error code). */
277
278int
c58b9523 279bfd_seek (bfd *abfd, file_ptr position, int direction)
93509525
KD
280{
281 int result;
7c192733 282 file_ptr file_position;
93509525
KD
283 /* For the time being, a BFD may not seek to it's end. The problem
284 is that we don't easily have a way to recognize the end of an
285 element in an archive. */
286
287 BFD_ASSERT (direction == SEEK_SET || direction == SEEK_CUR);
288
289 if (direction == SEEK_CUR && position == 0)
290 return 0;
291
93509525
KD
292 if (abfd->format != bfd_archive && abfd->my_archive == 0)
293 {
93509525
KD
294 if (direction == SEEK_SET && (bfd_vma) position == abfd->where)
295 return 0;
296 }
297 else
298 {
299 /* We need something smarter to optimize access to archives.
300 Currently, anything inside an archive is read via the file
301 handle for the archive. Which means that a bfd_seek on one
302 component affects the `current position' in the archive, as
303 well as in any other component.
304
305 It might be sufficient to put a spike through the cache
306 abstraction, and look to the archive for the file position,
307 but I think we should try for something cleaner.
308
309 In the meantime, no optimization for archives. */
310 }
311
93509525 312 file_position = position;
660722b0
TG
313 if (direction == SEEK_SET)
314 {
315 bfd *parent_bfd = abfd;
316
317 while (parent_bfd->my_archive != NULL)
318 {
319 file_position += parent_bfd->origin;
320 parent_bfd = parent_bfd->my_archive;
321 }
322 }
93509525 323
69fd4758
NC
324 if (abfd->iovec)
325 result = abfd->iovec->bseek (abfd, file_position, direction);
326 else
327 result = -1;
328
93509525
KD
329 if (result != 0)
330 {
331 int hold_errno = errno;
332
333 /* Force redetermination of `where' field. */
334 bfd_tell (abfd);
335
336 /* An EINVAL error probably means that the file offset was
337 absurd. */
338 if (hold_errno == EINVAL)
339 bfd_set_error (bfd_error_file_truncated);
340 else
341 {
342 bfd_set_error (bfd_error_system_call);
343 errno = hold_errno;
344 }
345 }
346 else
347 {
348 /* Adjust `where' field. */
349 if (direction == SEEK_SET)
350 abfd->where = position;
351 else
352 abfd->where += position;
353 }
354 return result;
355}
356
357/*
358FUNCTION
359 bfd_get_mtime
360
361SYNOPSIS
c58b9523 362 long bfd_get_mtime (bfd *abfd);
93509525
KD
363
364DESCRIPTION
365 Return the file modification time (as read from the file system, or
366 from the archive header for archive members).
367
368*/
369
370long
c58b9523 371bfd_get_mtime (bfd *abfd)
93509525 372{
93509525
KD
373 struct stat buf;
374
375 if (abfd->mtime_set)
376 return abfd->mtime;
377
69fd4758
NC
378 if (abfd->iovec == NULL)
379 return 0;
380
40838a72 381 if (abfd->iovec->bstat (abfd, &buf) != 0)
93509525
KD
382 return 0;
383
384 abfd->mtime = buf.st_mtime; /* Save value in case anyone wants it */
385 return buf.st_mtime;
386}
387
388/*
389FUNCTION
390 bfd_get_size
391
392SYNOPSIS
74633dd0 393 file_ptr bfd_get_size (bfd *abfd);
93509525
KD
394
395DESCRIPTION
396 Return the file size (as read from file system) for the file
397 associated with BFD @var{abfd}.
398
399 The initial motivation for, and use of, this routine is not
400 so we can get the exact size of the object the BFD applies to, since
401 that might not be generally possible (archive members for example).
402 It would be ideal if someone could eventually modify
403 it so that such results were guaranteed.
404
405 Instead, we want to ask questions like "is this NNN byte sized
406 object I'm about to try read from file offset YYY reasonable?"
407 As as example of where we might do this, some object formats
408 use string tables for which the first <<sizeof (long)>> bytes of the
409 table contain the size of the table itself, including the size bytes.
410 If an application tries to read what it thinks is one of these
411 string tables, without some way to validate the size, and for
412 some reason the size is wrong (byte swapping error, wrong location
413 for the string table, etc.), the only clue is likely to be a read
414 error when it tries to read the table, or a "virtual memory
415 exhausted" error when it tries to allocate 15 bazillon bytes
416 of space for the 15 bazillon byte table it is about to read.
5c4491d3 417 This function at least allows us to answer the question, "is the
93509525
KD
418 size reasonable?".
419*/
420
74633dd0 421file_ptr
c58b9523 422bfd_get_size (bfd *abfd)
93509525 423{
93509525
KD
424 struct stat buf;
425
69fd4758
NC
426 if (abfd->iovec == NULL)
427 return 0;
428
40838a72 429 if (abfd->iovec->bstat (abfd, &buf) != 0)
93509525
KD
430 return 0;
431
432 return buf.st_size;
433}
25b88f33
PP
434
435
436/*
437FUNCTION
438 bfd_mmap
439
440SYNOPSIS
441 void *bfd_mmap (bfd *abfd, void *addr, bfd_size_type len,
4c95ab76
TG
442 int prot, int flags, file_ptr offset,
443 void **map_addr, bfd_size_type *map_len);
25b88f33
PP
444
445DESCRIPTION
446 Return mmap()ed region of the file, if possible and implemented.
4c95ab76
TG
447 LEN and OFFSET do not need to be page aligned. The page aligned
448 address and length are written to MAP_ADDR and MAP_LEN.
25b88f33
PP
449
450*/
451
452void *
453bfd_mmap (bfd *abfd, void *addr, bfd_size_type len,
4c95ab76
TG
454 int prot, int flags, file_ptr offset,
455 void **map_addr, bfd_size_type *map_len)
25b88f33
PP
456{
457 void *ret = (void *)-1;
25b88f33
PP
458
459 if (abfd->iovec == NULL)
460 return ret;
461
4c95ab76
TG
462 return abfd->iovec->bmmap (abfd, addr, len, prot, flags, offset,
463 map_addr, map_len);
25b88f33 464}
65077aa8
TG
465
466/* Memory file I/O operations. */
467
468static file_ptr
469memory_bread (bfd *abfd, void *ptr, file_ptr size)
470{
471 struct bfd_in_memory *bim;
472 bfd_size_type get;
473
474 bim = (struct bfd_in_memory *) abfd->iostream;
475 get = size;
476 if (abfd->where + get > bim->size)
477 {
478 if (bim->size < (bfd_size_type) abfd->where)
479 get = 0;
480 else
481 get = bim->size - abfd->where;
482 bfd_set_error (bfd_error_file_truncated);
483 }
484 memcpy (ptr, bim->buffer + abfd->where, (size_t) get);
485 return get;
486}
487
488static file_ptr
489memory_bwrite (bfd *abfd, const void *ptr, file_ptr size)
490{
491 struct bfd_in_memory *bim = (struct bfd_in_memory *) abfd->iostream;
492
493 if (abfd->where + size > bim->size)
494 {
495 bfd_size_type newsize, oldsize;
496
497 oldsize = (bim->size + 127) & ~(bfd_size_type) 127;
498 bim->size = abfd->where + size;
499 /* Round up to cut down on memory fragmentation */
500 newsize = (bim->size + 127) & ~(bfd_size_type) 127;
501 if (newsize > oldsize)
502 {
503 bim->buffer = (bfd_byte *) bfd_realloc_or_free (bim->buffer, newsize);
504 if (bim->buffer == NULL)
505 {
506 bim->size = 0;
507 return 0;
508 }
509 if (newsize > bim->size)
510 memset (bim->buffer + bim->size, 0, newsize - bim->size);
511 }
512 }
513 memcpy (bim->buffer + abfd->where, ptr, (size_t) size);
514 return size;
515}
516
517static file_ptr
518memory_btell (bfd *abfd)
519{
520 return abfd->where;
521}
522
523static int
524memory_bseek (bfd *abfd, file_ptr position, int direction)
525{
526 file_ptr nwhere;
527 struct bfd_in_memory *bim;
528
529 bim = (struct bfd_in_memory *) abfd->iostream;
530
531 if (direction == SEEK_SET)
532 nwhere = position;
533 else
534 nwhere = abfd->where + position;
535
536 if (nwhere < 0)
537 {
538 abfd->where = 0;
539 errno = EINVAL;
540 return -1;
541 }
542
543 if ((bfd_size_type)nwhere > bim->size)
544 {
545 if (abfd->direction == write_direction
546 || abfd->direction == both_direction)
547 {
548 bfd_size_type newsize, oldsize;
549
550 oldsize = (bim->size + 127) & ~(bfd_size_type) 127;
551 bim->size = nwhere;
552 /* Round up to cut down on memory fragmentation */
553 newsize = (bim->size + 127) & ~(bfd_size_type) 127;
554 if (newsize > oldsize)
555 {
556 bim->buffer = (bfd_byte *) bfd_realloc_or_free (bim->buffer, newsize);
557 if (bim->buffer == NULL)
558 {
559 errno = EINVAL;
560 bim->size = 0;
561 return -1;
562 }
563 memset (bim->buffer + oldsize, 0, newsize - oldsize);
564 }
565 }
566 else
567 {
568 abfd->where = bim->size;
569 errno = EINVAL;
570 bfd_set_error (bfd_error_file_truncated);
571 return -1;
572 }
573 }
574 return 0;
575}
576
405bf443 577static int
65077aa8
TG
578memory_bclose (struct bfd *abfd)
579{
580 struct bfd_in_memory *bim = (struct bfd_in_memory *) abfd->iostream;
581
582 if (bim->buffer != NULL)
583 free (bim->buffer);
584 free (bim);
585 abfd->iostream = NULL;
586
405bf443 587 return 0;
65077aa8
TG
588}
589
590static int
591memory_bflush (bfd *abfd ATTRIBUTE_UNUSED)
592{
593 return 0;
594}
595
596static int
597memory_bstat (bfd *abfd, struct stat *statbuf)
598{
599 struct bfd_in_memory *bim = (struct bfd_in_memory *) abfd->iostream;
600
b5dee4ea 601 memset (statbuf, 0, sizeof (*statbuf));
65077aa8
TG
602 statbuf->st_size = bim->size;
603
604 return 0;
605}
606
607static void *
608memory_bmmap (bfd *abfd ATTRIBUTE_UNUSED, void *addr ATTRIBUTE_UNUSED,
609 bfd_size_type len ATTRIBUTE_UNUSED, int prot ATTRIBUTE_UNUSED,
4c95ab76
TG
610 int flags ATTRIBUTE_UNUSED, file_ptr offset ATTRIBUTE_UNUSED,
611 void **map_addr ATTRIBUTE_UNUSED,
612 bfd_size_type *map_len ATTRIBUTE_UNUSED)
65077aa8
TG
613{
614 return (void *)-1;
615}
616
617const struct bfd_iovec _bfd_memory_iovec =
618{
619 &memory_bread, &memory_bwrite, &memory_btell, &memory_bseek,
620 &memory_bclose, &memory_bflush, &memory_bstat, &memory_bmmap
621};
This page took 0.56883 seconds and 4 git commands to generate.