Remove a use of pagination_enabled
[binutils-gdb.git] / bfd / bfdio.c
1 /* Low-level I/O routines for BFDs.
2
3 Copyright (C) 1990-2023 Free Software Foundation, Inc.
4
5 Written by Cygnus Support.
6
7 This file is part of BFD, the Binary File Descriptor library.
8
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.
13
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.
18
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. */
23
24 #include "sysdep.h"
25 #include <limits.h>
26 #include "bfd.h"
27 #include "libbfd.h"
28 #include "aout/ar.h"
29 #if defined (_WIN32)
30 #include <windows.h>
31 #include <locale.h>
32 #endif
33
34 #ifndef S_IXUSR
35 #define S_IXUSR 0100 /* Execute by owner. */
36 #endif
37 #ifndef S_IXGRP
38 #define S_IXGRP 0010 /* Execute by group. */
39 #endif
40 #ifndef S_IXOTH
41 #define S_IXOTH 0001 /* Execute by others. */
42 #endif
43
44 #ifndef FD_CLOEXEC
45 #define FD_CLOEXEC 1
46 #endif
47
48 file_ptr
49 _bfd_real_ftell (FILE *file)
50 {
51 #if defined (HAVE_FTELLO64)
52 return ftello64 (file);
53 #elif defined (HAVE_FTELLO)
54 return ftello (file);
55 #else
56 return ftell (file);
57 #endif
58 }
59
60 int
61 _bfd_real_fseek (FILE *file, file_ptr offset, int whence)
62 {
63 #if defined (HAVE_FSEEKO64)
64 return fseeko64 (file, offset, whence);
65 #elif defined (HAVE_FSEEKO)
66 return fseeko (file, offset, whence);
67 #else
68 return fseek (file, offset, whence);
69 #endif
70 }
71
72 /* Mark FILE as close-on-exec. Return FILE. FILE may be NULL, in
73 which case nothing is done. */
74 static FILE *
75 close_on_exec (FILE *file)
76 {
77 #if defined (HAVE_FILENO) && defined (F_GETFD)
78 if (file)
79 {
80 int fd = fileno (file);
81 int old = fcntl (fd, F_GETFD, 0);
82 if (old >= 0)
83 fcntl (fd, F_SETFD, old | FD_CLOEXEC);
84 }
85 #endif
86 return file;
87 }
88
89 FILE *
90 _bfd_real_fopen (const char *filename, const char *modes)
91 {
92 #ifdef VMS
93 char *vms_attr;
94
95 /* On VMS, fopen allows file attributes as optional arguments.
96 We need to use them but we'd better to use the common prototype.
97 In fopen-vms.h, they are separated from the mode with a comma.
98 Split here. */
99 vms_attr = strchr (modes, ',');
100 if (vms_attr != NULL)
101 {
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]));
117 }
118
119 #elif defined (_WIN32)
120 /* PR 25713: Handle extra long path names possibly containing '..' and '.'. */
121 wchar_t ** lpFilePart = {NULL};
122 const wchar_t prefix[] = L"\\\\?\\";
123 const size_t partPathLen = strlen (filename) + 1;
124 #ifdef __MINGW32__
125 #if !HAVE_DECL____LC_CODEPAGE_FUNC
126 /* This prototype was added to locale.h in version 9.0 of MinGW-w64. */
127 _CRTIMP unsigned int __cdecl ___lc_codepage_func (void);
128 #endif
129 const unsigned int cp = ___lc_codepage_func ();
130 #else
131 const unsigned int cp = CP_UTF8;
132 #endif
133
134 /* Converting the partial path from ascii to unicode.
135 1) Get the length: Calling with lpWideCharStr set to null returns the length.
136 2) Convert the string: Calling with cbMultiByte set to -1 includes the terminating null. */
137 size_t partPathWSize = MultiByteToWideChar (cp, 0, filename, -1, NULL, 0);
138 wchar_t * partPath = calloc (partPathWSize, sizeof(wchar_t));
139 size_t ix;
140
141 MultiByteToWideChar (cp, 0, filename, -1, partPath, partPathWSize);
142
143 /* Convert any UNIX style path separators into the DOS i.e. backslash separator. */
144 for (ix = 0; ix < partPathLen; ix++)
145 if (IS_UNIX_DIR_SEPARATOR(filename[ix]))
146 partPath[ix] = '\\';
147
148 /* Getting the full path from the provided partial path.
149 1) Get the length.
150 2) Resolve the path. */
151 long fullPathWSize = GetFullPathNameW (partPath, 0, NULL, lpFilePart);
152 wchar_t * fullPath = calloc (fullPathWSize + sizeof(prefix) + 1, sizeof(wchar_t));
153
154 wcscpy (fullPath, prefix);
155
156 int prefixLen = sizeof(prefix) / sizeof(wchar_t);
157
158 /* Do not add a prefix to the null device. */
159 if (stricmp (filename, "nul") == 0)
160 prefixLen = 1;
161
162 wchar_t * fullPathOffset = fullPath + prefixLen - 1;
163
164 GetFullPathNameW (partPath, fullPathWSize, fullPathOffset, lpFilePart);
165 free (partPath);
166
167 /* It is non-standard for modes to exceed 16 characters. */
168 wchar_t modesW[16];
169
170 MultiByteToWideChar (cp, 0, modes, -1, modesW, sizeof(modesW));
171
172 FILE * file = _wfopen (fullPath, modesW);
173 free (fullPath);
174
175 return close_on_exec (file);
176
177 #elif defined (HAVE_FOPEN64)
178 return close_on_exec (fopen64 (filename, modes));
179
180 #else
181 return close_on_exec (fopen (filename, modes));
182 #endif
183 }
184
185 /*
186 INTERNAL_DEFINITION
187 struct bfd_iovec
188
189 DESCRIPTION
190
191 The <<struct bfd_iovec>> contains the internal file I/O class.
192 Each <<BFD>> has an instance of this class and all file I/O is
193 routed through it (it is assumed that the instance implements
194 all methods listed below).
195
196 .struct bfd_iovec
197 .{
198 . {* To avoid problems with macros, a "b" rather than "f"
199 . prefix is prepended to each method name. *}
200 . {* Attempt to read/write NBYTES on ABFD's IOSTREAM storing/fetching
201 . bytes starting at PTR. Return the number of bytes actually
202 . transfered (a read past end-of-file returns less than NBYTES),
203 . or -1 (setting <<bfd_error>>) if an error occurs. *}
204 . file_ptr (*bread) (struct bfd *abfd, void *ptr, file_ptr nbytes);
205 . file_ptr (*bwrite) (struct bfd *abfd, const void *ptr,
206 . file_ptr nbytes);
207 . {* Return the current IOSTREAM file offset, or -1 (setting <<bfd_error>>
208 . if an error occurs. *}
209 . file_ptr (*btell) (struct bfd *abfd);
210 . {* For the following, on successful completion a value of 0 is returned.
211 . Otherwise, a value of -1 is returned (and <<bfd_error>> is set). *}
212 . int (*bseek) (struct bfd *abfd, file_ptr offset, int whence);
213 . int (*bclose) (struct bfd *abfd);
214 . int (*bflush) (struct bfd *abfd);
215 . int (*bstat) (struct bfd *abfd, struct stat *sb);
216 . {* Mmap a part of the files. ADDR, LEN, PROT, FLAGS and OFFSET are the usual
217 . mmap parameter, except that LEN and OFFSET do not need to be page
218 . aligned. Returns (void *)-1 on failure, mmapped address on success.
219 . Also write in MAP_ADDR the address of the page aligned buffer and in
220 . MAP_LEN the size mapped (a page multiple). Use unmap with MAP_ADDR and
221 . MAP_LEN to unmap. *}
222 . void *(*bmmap) (struct bfd *abfd, void *addr, bfd_size_type len,
223 . int prot, int flags, file_ptr offset,
224 . void **map_addr, bfd_size_type *map_len);
225 .};
226
227 .extern const struct bfd_iovec _bfd_memory_iovec;
228
229 */
230
231
232 /* Return value is amount read. */
233
234 bfd_size_type
235 bfd_bread (void *ptr, bfd_size_type size, bfd *abfd)
236 {
237 file_ptr nread;
238 bfd *element_bfd = abfd;
239 ufile_ptr offset = 0;
240
241 while (abfd->my_archive != NULL
242 && !bfd_is_thin_archive (abfd->my_archive))
243 {
244 offset += abfd->origin;
245 abfd = abfd->my_archive;
246 }
247 offset += abfd->origin;
248
249 /* If this is a non-thin archive element, don't read past the end of
250 this element. */
251 if (element_bfd->arelt_data != NULL
252 && element_bfd->my_archive != NULL
253 && !bfd_is_thin_archive (element_bfd->my_archive))
254 {
255 bfd_size_type maxbytes = arelt_size (element_bfd);
256
257 if (abfd->where < offset || abfd->where - offset >= maxbytes)
258 {
259 bfd_set_error (bfd_error_invalid_operation);
260 return -1;
261 }
262 if (abfd->where - offset + size > maxbytes)
263 size = maxbytes - (abfd->where - offset);
264 }
265
266 if (abfd->iovec == NULL)
267 {
268 bfd_set_error (bfd_error_invalid_operation);
269 return -1;
270 }
271
272 nread = abfd->iovec->bread (abfd, ptr, size);
273 if (nread != -1)
274 abfd->where += nread;
275
276 return nread;
277 }
278
279 bfd_size_type
280 bfd_bwrite (const void *ptr, bfd_size_type size, bfd *abfd)
281 {
282 file_ptr nwrote;
283
284 while (abfd->my_archive != NULL
285 && !bfd_is_thin_archive (abfd->my_archive))
286 abfd = abfd->my_archive;
287
288 if (abfd->iovec == NULL)
289 {
290 bfd_set_error (bfd_error_invalid_operation);
291 return -1;
292 }
293
294 nwrote = abfd->iovec->bwrite (abfd, ptr, size);
295 if (nwrote != -1)
296 abfd->where += nwrote;
297 if ((bfd_size_type) nwrote != size)
298 {
299 #ifdef ENOSPC
300 errno = ENOSPC;
301 #endif
302 bfd_set_error (bfd_error_system_call);
303 }
304 return nwrote;
305 }
306
307 file_ptr
308 bfd_tell (bfd *abfd)
309 {
310 ufile_ptr offset = 0;
311 file_ptr ptr;
312
313 while (abfd->my_archive != NULL
314 && !bfd_is_thin_archive (abfd->my_archive))
315 {
316 offset += abfd->origin;
317 abfd = abfd->my_archive;
318 }
319 offset += abfd->origin;
320
321 if (abfd->iovec == NULL)
322 return 0;
323
324 ptr = abfd->iovec->btell (abfd);
325 abfd->where = ptr;
326 return ptr - offset;
327 }
328
329 int
330 bfd_flush (bfd *abfd)
331 {
332 while (abfd->my_archive != NULL
333 && !bfd_is_thin_archive (abfd->my_archive))
334 abfd = abfd->my_archive;
335
336 if (abfd->iovec == NULL)
337 return 0;
338
339 return abfd->iovec->bflush (abfd);
340 }
341
342 /* Returns 0 for success, negative value for failure (in which case
343 bfd_get_error can retrieve the error code). */
344 int
345 bfd_stat (bfd *abfd, struct stat *statbuf)
346 {
347 int result;
348
349 while (abfd->my_archive != NULL
350 && !bfd_is_thin_archive (abfd->my_archive))
351 abfd = abfd->my_archive;
352
353 if (abfd->iovec == NULL)
354 {
355 bfd_set_error (bfd_error_invalid_operation);
356 return -1;
357 }
358
359 result = abfd->iovec->bstat (abfd, statbuf);
360 if (result < 0)
361 bfd_set_error (bfd_error_system_call);
362 return result;
363 }
364
365 /* Returns 0 for success, nonzero for failure (in which case bfd_get_error
366 can retrieve the error code). */
367
368 int
369 bfd_seek (bfd *abfd, file_ptr position, int direction)
370 {
371 int result;
372 ufile_ptr offset = 0;
373
374 while (abfd->my_archive != NULL
375 && !bfd_is_thin_archive (abfd->my_archive))
376 {
377 offset += abfd->origin;
378 abfd = abfd->my_archive;
379 }
380 offset += abfd->origin;
381
382 if (abfd->iovec == NULL)
383 {
384 bfd_set_error (bfd_error_invalid_operation);
385 return -1;
386 }
387
388 /* For the time being, a BFD may not seek to it's end. The problem
389 is that we don't easily have a way to recognize the end of an
390 element in an archive. */
391 BFD_ASSERT (direction == SEEK_SET || direction == SEEK_CUR);
392
393 if (direction != SEEK_CUR)
394 position += offset;
395
396 if ((direction == SEEK_CUR && position == 0)
397 || (direction == SEEK_SET && (ufile_ptr) position == abfd->where))
398 return 0;
399
400 result = abfd->iovec->bseek (abfd, position, direction);
401 if (result != 0)
402 {
403 /* An EINVAL error probably means that the file offset was
404 absurd. */
405 if (errno == EINVAL)
406 bfd_set_error (bfd_error_file_truncated);
407 else
408 bfd_set_error (bfd_error_system_call);
409 }
410 else
411 {
412 /* Adjust `where' field. */
413 if (direction == SEEK_CUR)
414 abfd->where += position;
415 else
416 abfd->where = position;
417 }
418
419 return result;
420 }
421
422 /*
423 FUNCTION
424 bfd_get_mtime
425
426 SYNOPSIS
427 long bfd_get_mtime (bfd *abfd);
428
429 DESCRIPTION
430 Return the file modification time (as read from the file system, or
431 from the archive header for archive members).
432
433 */
434
435 long
436 bfd_get_mtime (bfd *abfd)
437 {
438 struct stat buf;
439
440 if (abfd->mtime_set)
441 return abfd->mtime;
442
443 if (bfd_stat (abfd, &buf) != 0)
444 return 0;
445
446 abfd->mtime = buf.st_mtime; /* Save value in case anyone wants it */
447 return buf.st_mtime;
448 }
449
450 /*
451 FUNCTION
452 bfd_get_size
453
454 SYNOPSIS
455 ufile_ptr bfd_get_size (bfd *abfd);
456
457 DESCRIPTION
458 Return the file size (as read from file system) for the file
459 associated with BFD @var{abfd}.
460
461 The initial motivation for, and use of, this routine is not
462 so we can get the exact size of the object the BFD applies to, since
463 that might not be generally possible (archive members for example).
464 It would be ideal if someone could eventually modify
465 it so that such results were guaranteed.
466
467 Instead, we want to ask questions like "is this NNN byte sized
468 object I'm about to try read from file offset YYY reasonable?"
469 As as example of where we might do this, some object formats
470 use string tables for which the first <<sizeof (long)>> bytes of the
471 table contain the size of the table itself, including the size bytes.
472 If an application tries to read what it thinks is one of these
473 string tables, without some way to validate the size, and for
474 some reason the size is wrong (byte swapping error, wrong location
475 for the string table, etc.), the only clue is likely to be a read
476 error when it tries to read the table, or a "virtual memory
477 exhausted" error when it tries to allocate 15 bazillon bytes
478 of space for the 15 bazillon byte table it is about to read.
479 This function at least allows us to answer the question, "is the
480 size reasonable?".
481
482 A return value of zero indicates the file size is unknown.
483 */
484
485 ufile_ptr
486 bfd_get_size (bfd *abfd)
487 {
488 /* A size of 0 means we haven't yet called bfd_stat. A size of 1
489 means we have a cached value of 0, ie. unknown. */
490 if (abfd->size <= 1 || bfd_write_p (abfd))
491 {
492 struct stat buf;
493
494 if (abfd->size == 1 && !bfd_write_p (abfd))
495 return 0;
496
497 if (bfd_stat (abfd, &buf) != 0
498 || buf.st_size == 0
499 || buf.st_size - (ufile_ptr) buf.st_size != 0)
500 {
501 abfd->size = 1;
502 return 0;
503 }
504 abfd->size = buf.st_size;
505 }
506 return abfd->size;
507 }
508
509 /*
510 FUNCTION
511 bfd_get_file_size
512
513 SYNOPSIS
514 ufile_ptr bfd_get_file_size (bfd *abfd);
515
516 DESCRIPTION
517 Return the file size (as read from file system) for the file
518 associated with BFD @var{abfd}. It supports both normal files
519 and archive elements.
520
521 */
522
523 ufile_ptr
524 bfd_get_file_size (bfd *abfd)
525 {
526 ufile_ptr file_size, archive_size = (ufile_ptr) -1;
527
528 if (abfd->my_archive != NULL
529 && !bfd_is_thin_archive (abfd->my_archive))
530 {
531 struct areltdata *adata = (struct areltdata *) abfd->arelt_data;
532 if (adata != NULL)
533 {
534 archive_size = adata->parsed_size;
535 /* If the archive is compressed we can't compare against
536 file size. */
537 if (adata->arch_header != NULL
538 && memcmp (((struct ar_hdr *) adata->arch_header)->ar_fmag,
539 "Z\012", 2) == 0)
540 return archive_size;
541 abfd = abfd->my_archive;
542 }
543 }
544
545 file_size = bfd_get_size (abfd);
546 if (archive_size < file_size)
547 return archive_size;
548 return file_size;
549 }
550
551 /*
552 FUNCTION
553 bfd_mmap
554
555 SYNOPSIS
556 void *bfd_mmap (bfd *abfd, void *addr, bfd_size_type len,
557 int prot, int flags, file_ptr offset,
558 void **map_addr, bfd_size_type *map_len);
559
560 DESCRIPTION
561 Return mmap()ed region of the file, if possible and implemented.
562 LEN and OFFSET do not need to be page aligned. The page aligned
563 address and length are written to MAP_ADDR and MAP_LEN.
564
565 */
566
567 void *
568 bfd_mmap (bfd *abfd, void *addr, bfd_size_type len,
569 int prot, int flags, file_ptr offset,
570 void **map_addr, bfd_size_type *map_len)
571 {
572 while (abfd->my_archive != NULL
573 && !bfd_is_thin_archive (abfd->my_archive))
574 {
575 offset += abfd->origin;
576 abfd = abfd->my_archive;
577 }
578 offset += abfd->origin;
579
580 if (abfd->iovec == NULL)
581 {
582 bfd_set_error (bfd_error_invalid_operation);
583 return (void *) -1;
584 }
585
586 return abfd->iovec->bmmap (abfd, addr, len, prot, flags, offset,
587 map_addr, map_len);
588 }
589
590 /* Memory file I/O operations. */
591
592 static file_ptr
593 memory_bread (bfd *abfd, void *ptr, file_ptr size)
594 {
595 struct bfd_in_memory *bim;
596 bfd_size_type get;
597
598 bim = (struct bfd_in_memory *) abfd->iostream;
599 get = size;
600 if (abfd->where + get > bim->size)
601 {
602 if (bim->size < (bfd_size_type) abfd->where)
603 get = 0;
604 else
605 get = bim->size - abfd->where;
606 bfd_set_error (bfd_error_file_truncated);
607 }
608 memcpy (ptr, bim->buffer + abfd->where, (size_t) get);
609 return get;
610 }
611
612 static file_ptr
613 memory_bwrite (bfd *abfd, const void *ptr, file_ptr size)
614 {
615 struct bfd_in_memory *bim = (struct bfd_in_memory *) abfd->iostream;
616
617 if (abfd->where + size > bim->size)
618 {
619 bfd_size_type newsize, oldsize;
620
621 oldsize = (bim->size + 127) & ~(bfd_size_type) 127;
622 bim->size = abfd->where + size;
623 /* Round up to cut down on memory fragmentation */
624 newsize = (bim->size + 127) & ~(bfd_size_type) 127;
625 if (newsize > oldsize)
626 {
627 bim->buffer = (bfd_byte *) bfd_realloc_or_free (bim->buffer, newsize);
628 if (bim->buffer == NULL)
629 {
630 bim->size = 0;
631 return 0;
632 }
633 if (newsize > bim->size)
634 memset (bim->buffer + bim->size, 0, newsize - bim->size);
635 }
636 }
637 memcpy (bim->buffer + abfd->where, ptr, (size_t) size);
638 return size;
639 }
640
641 static file_ptr
642 memory_btell (bfd *abfd)
643 {
644 return abfd->where;
645 }
646
647 static int
648 memory_bseek (bfd *abfd, file_ptr position, int direction)
649 {
650 file_ptr nwhere;
651 struct bfd_in_memory *bim;
652
653 bim = (struct bfd_in_memory *) abfd->iostream;
654
655 if (direction == SEEK_SET)
656 nwhere = position;
657 else
658 nwhere = abfd->where + position;
659
660 if (nwhere < 0)
661 {
662 abfd->where = 0;
663 errno = EINVAL;
664 return -1;
665 }
666
667 if ((bfd_size_type)nwhere > bim->size)
668 {
669 if (abfd->direction == write_direction
670 || abfd->direction == both_direction)
671 {
672 bfd_size_type newsize, oldsize;
673
674 oldsize = (bim->size + 127) & ~(bfd_size_type) 127;
675 bim->size = nwhere;
676 /* Round up to cut down on memory fragmentation */
677 newsize = (bim->size + 127) & ~(bfd_size_type) 127;
678 if (newsize > oldsize)
679 {
680 bim->buffer = (bfd_byte *) bfd_realloc_or_free (bim->buffer, newsize);
681 if (bim->buffer == NULL)
682 {
683 errno = EINVAL;
684 bim->size = 0;
685 return -1;
686 }
687 memset (bim->buffer + oldsize, 0, newsize - oldsize);
688 }
689 }
690 else
691 {
692 abfd->where = bim->size;
693 errno = EINVAL;
694 bfd_set_error (bfd_error_file_truncated);
695 return -1;
696 }
697 }
698 return 0;
699 }
700
701 static int
702 memory_bclose (struct bfd *abfd)
703 {
704 struct bfd_in_memory *bim = (struct bfd_in_memory *) abfd->iostream;
705
706 free (bim->buffer);
707 free (bim);
708 abfd->iostream = NULL;
709
710 return 0;
711 }
712
713 static int
714 memory_bflush (bfd *abfd ATTRIBUTE_UNUSED)
715 {
716 return 0;
717 }
718
719 static int
720 memory_bstat (bfd *abfd, struct stat *statbuf)
721 {
722 struct bfd_in_memory *bim = (struct bfd_in_memory *) abfd->iostream;
723
724 memset (statbuf, 0, sizeof (*statbuf));
725 statbuf->st_size = bim->size;
726
727 return 0;
728 }
729
730 static void *
731 memory_bmmap (bfd *abfd ATTRIBUTE_UNUSED, void *addr ATTRIBUTE_UNUSED,
732 bfd_size_type len ATTRIBUTE_UNUSED, int prot ATTRIBUTE_UNUSED,
733 int flags ATTRIBUTE_UNUSED, file_ptr offset ATTRIBUTE_UNUSED,
734 void **map_addr ATTRIBUTE_UNUSED,
735 bfd_size_type *map_len ATTRIBUTE_UNUSED)
736 {
737 return (void *)-1;
738 }
739
740 const struct bfd_iovec _bfd_memory_iovec =
741 {
742 &memory_bread, &memory_bwrite, &memory_btell, &memory_bseek,
743 &memory_bclose, &memory_bflush, &memory_bstat, &memory_bmmap
744 };