1 /* Low-level I/O routines for BFDs.
3 Copyright (C) 1990-2022 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. */
34 #define S_IXUSR 0100 /* Execute by owner. */
37 #define S_IXGRP 0010 /* Execute by group. */
40 #define S_IXOTH 0001 /* Execute by others. */
48 _bfd_real_ftell (FILE *file
)
50 #if defined (HAVE_FTELLO64)
51 return ftello64 (file
);
52 #elif defined (HAVE_FTELLO)
60 _bfd_real_fseek (FILE *file
, file_ptr offset
, int whence
)
62 #if defined (HAVE_FSEEKO64)
63 return fseeko64 (file
, offset
, whence
);
64 #elif defined (HAVE_FSEEKO)
65 return fseeko (file
, offset
, whence
);
67 return fseek (file
, offset
, whence
);
71 /* Mark FILE as close-on-exec. Return FILE. FILE may be NULL, in
72 which case nothing is done. */
74 close_on_exec (FILE *file
)
76 #if defined (HAVE_FILENO) && defined (F_GETFD)
79 int fd
= fileno (file
);
80 int old
= fcntl (fd
, F_GETFD
, 0);
82 fcntl (fd
, F_SETFD
, old
| FD_CLOEXEC
);
89 _bfd_real_fopen (const char *filename
, const char *modes
)
94 /* On VMS, fopen allows file attributes as optional arguments.
95 We need to use them but we'd better to use the common prototype.
96 In fopen-vms.h, they are separated from the mode with a comma.
98 vms_attr
= strchr (modes
, ',');
101 /* Attributes found. Split. */
102 size_t modes_len
= strlen (modes
) + 1;
103 char attrs
[modes_len
+ 1];
107 memcpy (attrs
, modes
, modes_len
);
109 for (i
= 0; i
< 2; i
++)
111 at
[i
+ 1] = strchr (at
[i
], ',');
112 BFD_ASSERT (at
[i
+ 1] != NULL
);
113 *(at
[i
+ 1]++) = 0; /* Replace ',' with a nul, and skip it. */
115 return close_on_exec (fopen (filename
, at
[0], at
[1], at
[2]));
118 #elif defined (_WIN32)
119 /* PR 25713: Handle extra long path names possibly containing '..' and '.'. */
120 wchar_t ** lpFilePart
= {NULL
};
121 const wchar_t prefix
[] = L
"\\\\?\\";
122 const size_t partPathLen
= strlen (filename
) + 1;
124 /* Converting the partial path from ascii to unicode.
125 1) Get the length: Calling with lpWideCharStr set to null returns the length.
126 2) Convert the string: Calling with cbMultiByte set to -1 includes the terminating null. */
127 size_t partPathWSize
= MultiByteToWideChar (CP_UTF8
, 0, filename
, -1, NULL
, 0);
128 wchar_t * partPath
= calloc (partPathWSize
, sizeof(wchar_t));
131 MultiByteToWideChar (CP_UTF8
, 0, filename
, -1, partPath
, partPathWSize
);
133 /* Convert any UNIX style path separators into the DOS i.e. backslash separator. */
134 for (ix
= 0; ix
< partPathLen
; ix
++)
135 if (IS_UNIX_DIR_SEPARATOR(filename
[ix
]))
138 /* Getting the full path from the provided partial path.
140 2) Resolve the path. */
141 long fullPathWSize
= GetFullPathNameW (partPath
, 0, NULL
, lpFilePart
);
142 wchar_t * fullPath
= calloc (fullPathWSize
+ sizeof(prefix
) + 1, sizeof(wchar_t));
144 wcscpy (fullPath
, prefix
);
146 int prefixLen
= sizeof(prefix
) / sizeof(wchar_t);
147 wchar_t * fullPathOffset
= fullPath
+ prefixLen
- 1;
149 GetFullPathNameW (partPath
, fullPathWSize
, fullPathOffset
, lpFilePart
);
152 /* It is non-standard for modes to exceed 16 characters. */
155 MultiByteToWideChar (CP_UTF8
, 0, modes
, -1, modesW
, sizeof(modesW
));
157 FILE * file
= _wfopen (fullPath
, modesW
);
160 return close_on_exec (file
);
162 #elif defined (HAVE_FOPEN64)
163 return close_on_exec (fopen64 (filename
, modes
));
166 return close_on_exec (fopen (filename
, modes
));
176 The <<struct bfd_iovec>> contains the internal file I/O class.
177 Each <<BFD>> has an instance of this class and all file I/O is
178 routed through it (it is assumed that the instance implements
179 all methods listed below).
183 . {* To avoid problems with macros, a "b" rather than "f"
184 . prefix is prepended to each method name. *}
185 . {* Attempt to read/write NBYTES on ABFD's IOSTREAM storing/fetching
186 . bytes starting at PTR. Return the number of bytes actually
187 . transfered (a read past end-of-file returns less than NBYTES),
188 . or -1 (setting <<bfd_error>>) if an error occurs. *}
189 . file_ptr (*bread) (struct bfd *abfd, void *ptr, file_ptr nbytes);
190 . file_ptr (*bwrite) (struct bfd *abfd, const void *ptr,
192 . {* Return the current IOSTREAM file offset, or -1 (setting <<bfd_error>>
193 . if an error occurs. *}
194 . file_ptr (*btell) (struct bfd *abfd);
195 . {* For the following, on successful completion a value of 0 is returned.
196 . Otherwise, a value of -1 is returned (and <<bfd_error>> is set). *}
197 . int (*bseek) (struct bfd *abfd, file_ptr offset, int whence);
198 . int (*bclose) (struct bfd *abfd);
199 . int (*bflush) (struct bfd *abfd);
200 . int (*bstat) (struct bfd *abfd, struct stat *sb);
201 . {* Mmap a part of the files. ADDR, LEN, PROT, FLAGS and OFFSET are the usual
202 . mmap parameter, except that LEN and OFFSET do not need to be page
203 . aligned. Returns (void *)-1 on failure, mmapped address on success.
204 . Also write in MAP_ADDR the address of the page aligned buffer and in
205 . MAP_LEN the size mapped (a page multiple). Use unmap with MAP_ADDR and
206 . MAP_LEN to unmap. *}
207 . void *(*bmmap) (struct bfd *abfd, void *addr, bfd_size_type len,
208 . int prot, int flags, file_ptr offset,
209 . void **map_addr, bfd_size_type *map_len);
212 .extern const struct bfd_iovec _bfd_memory_iovec;
217 /* Return value is amount read. */
220 bfd_bread (void *ptr
, bfd_size_type size
, bfd
*abfd
)
223 bfd
*element_bfd
= abfd
;
224 ufile_ptr offset
= 0;
226 while (abfd
->my_archive
!= NULL
227 && !bfd_is_thin_archive (abfd
->my_archive
))
229 offset
+= abfd
->origin
;
230 abfd
= abfd
->my_archive
;
232 offset
+= abfd
->origin
;
234 /* If this is a non-thin archive element, don't read past the end of
236 if (element_bfd
->arelt_data
!= NULL
237 && element_bfd
->my_archive
!= NULL
238 && !bfd_is_thin_archive (element_bfd
->my_archive
))
240 bfd_size_type maxbytes
= arelt_size (element_bfd
);
242 if (abfd
->where
< offset
|| abfd
->where
- offset
>= maxbytes
)
244 bfd_set_error (bfd_error_invalid_operation
);
247 if (abfd
->where
- offset
+ size
> maxbytes
)
248 size
= maxbytes
- (abfd
->where
- offset
);
251 if (abfd
->iovec
== NULL
)
253 bfd_set_error (bfd_error_invalid_operation
);
257 nread
= abfd
->iovec
->bread (abfd
, ptr
, size
);
259 abfd
->where
+= nread
;
265 bfd_bwrite (const void *ptr
, bfd_size_type size
, bfd
*abfd
)
269 while (abfd
->my_archive
!= NULL
270 && !bfd_is_thin_archive (abfd
->my_archive
))
271 abfd
= abfd
->my_archive
;
273 if (abfd
->iovec
== NULL
)
275 bfd_set_error (bfd_error_invalid_operation
);
279 nwrote
= abfd
->iovec
->bwrite (abfd
, ptr
, size
);
281 abfd
->where
+= nwrote
;
282 if ((bfd_size_type
) nwrote
!= size
)
287 bfd_set_error (bfd_error_system_call
);
295 ufile_ptr offset
= 0;
298 while (abfd
->my_archive
!= NULL
299 && !bfd_is_thin_archive (abfd
->my_archive
))
301 offset
+= abfd
->origin
;
302 abfd
= abfd
->my_archive
;
304 offset
+= abfd
->origin
;
306 if (abfd
->iovec
== NULL
)
309 ptr
= abfd
->iovec
->btell (abfd
);
315 bfd_flush (bfd
*abfd
)
317 while (abfd
->my_archive
!= NULL
318 && !bfd_is_thin_archive (abfd
->my_archive
))
319 abfd
= abfd
->my_archive
;
321 if (abfd
->iovec
== NULL
)
324 return abfd
->iovec
->bflush (abfd
);
327 /* Returns 0 for success, negative value for failure (in which case
328 bfd_get_error can retrieve the error code). */
330 bfd_stat (bfd
*abfd
, struct stat
*statbuf
)
334 while (abfd
->my_archive
!= NULL
335 && !bfd_is_thin_archive (abfd
->my_archive
))
336 abfd
= abfd
->my_archive
;
338 if (abfd
->iovec
== NULL
)
340 bfd_set_error (bfd_error_invalid_operation
);
344 result
= abfd
->iovec
->bstat (abfd
, statbuf
);
346 bfd_set_error (bfd_error_system_call
);
350 /* Returns 0 for success, nonzero for failure (in which case bfd_get_error
351 can retrieve the error code). */
354 bfd_seek (bfd
*abfd
, file_ptr position
, int direction
)
357 ufile_ptr offset
= 0;
359 while (abfd
->my_archive
!= NULL
360 && !bfd_is_thin_archive (abfd
->my_archive
))
362 offset
+= abfd
->origin
;
363 abfd
= abfd
->my_archive
;
365 offset
+= abfd
->origin
;
367 if (abfd
->iovec
== NULL
)
369 bfd_set_error (bfd_error_invalid_operation
);
373 /* For the time being, a BFD may not seek to it's end. The problem
374 is that we don't easily have a way to recognize the end of an
375 element in an archive. */
376 BFD_ASSERT (direction
== SEEK_SET
|| direction
== SEEK_CUR
);
378 if (direction
!= SEEK_CUR
)
381 if ((direction
== SEEK_CUR
&& position
== 0)
382 || (direction
== SEEK_SET
&& (ufile_ptr
) position
== abfd
->where
))
385 result
= abfd
->iovec
->bseek (abfd
, position
, direction
);
388 /* An EINVAL error probably means that the file offset was
391 bfd_set_error (bfd_error_file_truncated
);
393 bfd_set_error (bfd_error_system_call
);
397 /* Adjust `where' field. */
398 if (direction
== SEEK_CUR
)
399 abfd
->where
+= position
;
401 abfd
->where
= position
;
412 long bfd_get_mtime (bfd *abfd);
415 Return the file modification time (as read from the file system, or
416 from the archive header for archive members).
421 bfd_get_mtime (bfd
*abfd
)
428 if (bfd_stat (abfd
, &buf
) != 0)
431 abfd
->mtime
= buf
.st_mtime
; /* Save value in case anyone wants it */
440 ufile_ptr bfd_get_size (bfd *abfd);
443 Return the file size (as read from file system) for the file
444 associated with BFD @var{abfd}.
446 The initial motivation for, and use of, this routine is not
447 so we can get the exact size of the object the BFD applies to, since
448 that might not be generally possible (archive members for example).
449 It would be ideal if someone could eventually modify
450 it so that such results were guaranteed.
452 Instead, we want to ask questions like "is this NNN byte sized
453 object I'm about to try read from file offset YYY reasonable?"
454 As as example of where we might do this, some object formats
455 use string tables for which the first <<sizeof (long)>> bytes of the
456 table contain the size of the table itself, including the size bytes.
457 If an application tries to read what it thinks is one of these
458 string tables, without some way to validate the size, and for
459 some reason the size is wrong (byte swapping error, wrong location
460 for the string table, etc.), the only clue is likely to be a read
461 error when it tries to read the table, or a "virtual memory
462 exhausted" error when it tries to allocate 15 bazillon bytes
463 of space for the 15 bazillon byte table it is about to read.
464 This function at least allows us to answer the question, "is the
467 A return value of zero indicates the file size is unknown.
471 bfd_get_size (bfd
*abfd
)
473 /* A size of 0 means we haven't yet called bfd_stat. A size of 1
474 means we have a cached value of 0, ie. unknown. */
475 if (abfd
->size
<= 1 || bfd_write_p (abfd
))
479 if (abfd
->size
== 1 && !bfd_write_p (abfd
))
482 if (bfd_stat (abfd
, &buf
) != 0
484 || buf
.st_size
- (ufile_ptr
) buf
.st_size
!= 0)
489 abfd
->size
= buf
.st_size
;
499 ufile_ptr bfd_get_file_size (bfd *abfd);
502 Return the file size (as read from file system) for the file
503 associated with BFD @var{abfd}. It supports both normal files
504 and archive elements.
509 bfd_get_file_size (bfd
*abfd
)
511 ufile_ptr file_size
, archive_size
= (ufile_ptr
) -1;
513 if (abfd
->my_archive
!= NULL
514 && !bfd_is_thin_archive (abfd
->my_archive
))
516 struct areltdata
*adata
= (struct areltdata
*) abfd
->arelt_data
;
519 archive_size
= adata
->parsed_size
;
520 /* If the archive is compressed we can't compare against
522 if (adata
->arch_header
!= NULL
523 && memcmp (((struct ar_hdr
*) adata
->arch_header
)->ar_fmag
,
526 abfd
= abfd
->my_archive
;
530 file_size
= bfd_get_size (abfd
);
531 if (archive_size
< file_size
)
541 void *bfd_mmap (bfd *abfd, void *addr, bfd_size_type len,
542 int prot, int flags, file_ptr offset,
543 void **map_addr, bfd_size_type *map_len);
546 Return mmap()ed region of the file, if possible and implemented.
547 LEN and OFFSET do not need to be page aligned. The page aligned
548 address and length are written to MAP_ADDR and MAP_LEN.
553 bfd_mmap (bfd
*abfd
, void *addr
, bfd_size_type len
,
554 int prot
, int flags
, file_ptr offset
,
555 void **map_addr
, bfd_size_type
*map_len
)
557 while (abfd
->my_archive
!= NULL
558 && !bfd_is_thin_archive (abfd
->my_archive
))
560 offset
+= abfd
->origin
;
561 abfd
= abfd
->my_archive
;
563 offset
+= abfd
->origin
;
565 if (abfd
->iovec
== NULL
)
567 bfd_set_error (bfd_error_invalid_operation
);
571 return abfd
->iovec
->bmmap (abfd
, addr
, len
, prot
, flags
, offset
,
575 /* Memory file I/O operations. */
578 memory_bread (bfd
*abfd
, void *ptr
, file_ptr size
)
580 struct bfd_in_memory
*bim
;
583 bim
= (struct bfd_in_memory
*) abfd
->iostream
;
585 if (abfd
->where
+ get
> bim
->size
)
587 if (bim
->size
< (bfd_size_type
) abfd
->where
)
590 get
= bim
->size
- abfd
->where
;
591 bfd_set_error (bfd_error_file_truncated
);
593 memcpy (ptr
, bim
->buffer
+ abfd
->where
, (size_t) get
);
598 memory_bwrite (bfd
*abfd
, const void *ptr
, file_ptr size
)
600 struct bfd_in_memory
*bim
= (struct bfd_in_memory
*) abfd
->iostream
;
602 if (abfd
->where
+ size
> bim
->size
)
604 bfd_size_type newsize
, oldsize
;
606 oldsize
= (bim
->size
+ 127) & ~(bfd_size_type
) 127;
607 bim
->size
= abfd
->where
+ size
;
608 /* Round up to cut down on memory fragmentation */
609 newsize
= (bim
->size
+ 127) & ~(bfd_size_type
) 127;
610 if (newsize
> oldsize
)
612 bim
->buffer
= (bfd_byte
*) bfd_realloc_or_free (bim
->buffer
, newsize
);
613 if (bim
->buffer
== NULL
)
618 if (newsize
> bim
->size
)
619 memset (bim
->buffer
+ bim
->size
, 0, newsize
- bim
->size
);
622 memcpy (bim
->buffer
+ abfd
->where
, ptr
, (size_t) size
);
627 memory_btell (bfd
*abfd
)
633 memory_bseek (bfd
*abfd
, file_ptr position
, int direction
)
636 struct bfd_in_memory
*bim
;
638 bim
= (struct bfd_in_memory
*) abfd
->iostream
;
640 if (direction
== SEEK_SET
)
643 nwhere
= abfd
->where
+ position
;
652 if ((bfd_size_type
)nwhere
> bim
->size
)
654 if (abfd
->direction
== write_direction
655 || abfd
->direction
== both_direction
)
657 bfd_size_type newsize
, oldsize
;
659 oldsize
= (bim
->size
+ 127) & ~(bfd_size_type
) 127;
661 /* Round up to cut down on memory fragmentation */
662 newsize
= (bim
->size
+ 127) & ~(bfd_size_type
) 127;
663 if (newsize
> oldsize
)
665 bim
->buffer
= (bfd_byte
*) bfd_realloc_or_free (bim
->buffer
, newsize
);
666 if (bim
->buffer
== NULL
)
672 memset (bim
->buffer
+ oldsize
, 0, newsize
- oldsize
);
677 abfd
->where
= bim
->size
;
679 bfd_set_error (bfd_error_file_truncated
);
687 memory_bclose (struct bfd
*abfd
)
689 struct bfd_in_memory
*bim
= (struct bfd_in_memory
*) abfd
->iostream
;
693 abfd
->iostream
= NULL
;
699 memory_bflush (bfd
*abfd ATTRIBUTE_UNUSED
)
705 memory_bstat (bfd
*abfd
, struct stat
*statbuf
)
707 struct bfd_in_memory
*bim
= (struct bfd_in_memory
*) abfd
->iostream
;
709 memset (statbuf
, 0, sizeof (*statbuf
));
710 statbuf
->st_size
= bim
->size
;
716 memory_bmmap (bfd
*abfd ATTRIBUTE_UNUSED
, void *addr ATTRIBUTE_UNUSED
,
717 bfd_size_type len ATTRIBUTE_UNUSED
, int prot ATTRIBUTE_UNUSED
,
718 int flags ATTRIBUTE_UNUSED
, file_ptr offset ATTRIBUTE_UNUSED
,
719 void **map_addr ATTRIBUTE_UNUSED
,
720 bfd_size_type
*map_len ATTRIBUTE_UNUSED
)
725 const struct bfd_iovec _bfd_memory_iovec
=
727 &memory_bread
, &memory_bwrite
, &memory_btell
, &memory_bseek
,
728 &memory_bclose
, &memory_bflush
, &memory_bstat
, &memory_bmmap