]>
Commit | Line | Data |
---|---|---|
93509525 | 1 | /* Low-level I/O routines for BFDs. |
7c192733 | 2 | |
a2c58332 | 3 | Copyright (C) 1990-2022 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" | |
b570b954 | 28 | #include "aout/ar.h" |
0b8448af NC |
29 | #if defined (_WIN32) |
30 | #include <windows.h> | |
31 | #endif | |
93509525 | 32 | |
93509525 KD |
33 | #ifndef S_IXUSR |
34 | #define S_IXUSR 0100 /* Execute by owner. */ | |
35 | #endif | |
36 | #ifndef S_IXGRP | |
37 | #define S_IXGRP 0010 /* Execute by group. */ | |
38 | #endif | |
39 | #ifndef S_IXOTH | |
40 | #define S_IXOTH 0001 /* Execute by others. */ | |
41 | #endif | |
42 | ||
428b207a TT |
43 | #ifndef FD_CLOEXEC |
44 | #define FD_CLOEXEC 1 | |
45 | #endif | |
46 | ||
7c192733 | 47 | file_ptr |
c7c3d11b | 48 | _bfd_real_ftell (FILE *file) |
7c192733 AC |
49 | { |
50 | #if defined (HAVE_FTELLO64) | |
51 | return ftello64 (file); | |
52 | #elif defined (HAVE_FTELLO) | |
53 | return ftello (file); | |
54 | #else | |
55 | return ftell (file); | |
56 | #endif | |
57 | } | |
58 | ||
59 | int | |
c7c3d11b | 60 | _bfd_real_fseek (FILE *file, file_ptr offset, int whence) |
7c192733 AC |
61 | { |
62 | #if defined (HAVE_FSEEKO64) | |
63 | return fseeko64 (file, offset, whence); | |
64 | #elif defined (HAVE_FSEEKO) | |
65 | return fseeko (file, offset, whence); | |
66 | #else | |
67 | return fseek (file, offset, whence); | |
68 | #endif | |
69 | } | |
70 | ||
428b207a TT |
71 | /* Mark FILE as close-on-exec. Return FILE. FILE may be NULL, in |
72 | which case nothing is done. */ | |
73 | static FILE * | |
74 | close_on_exec (FILE *file) | |
75 | { | |
76 | #if defined (HAVE_FILENO) && defined (F_GETFD) | |
77 | if (file) | |
78 | { | |
79 | int fd = fileno (file); | |
80 | int old = fcntl (fd, F_GETFD, 0); | |
81 | if (old >= 0) | |
82 | fcntl (fd, F_SETFD, old | FD_CLOEXEC); | |
83 | } | |
84 | #endif | |
85 | return file; | |
86 | } | |
87 | ||
2e6f4fae | 88 | FILE * |
c7c3d11b | 89 | _bfd_real_fopen (const char *filename, const char *modes) |
2e6f4fae | 90 | { |
d387240a | 91 | #ifdef VMS |
d387240a TG |
92 | char *vms_attr; |
93 | ||
9aff4b7a | 94 | /* On VMS, fopen allows file attributes as optional arguments. |
d387240a TG |
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. | |
97 | Split here. */ | |
98 | vms_attr = strchr (modes, ','); | |
0b8448af | 99 | if (vms_attr != NULL) |
d387240a | 100 | { |
0c376465 TG |
101 | /* Attributes found. Split. */ |
102 | size_t modes_len = strlen (modes) + 1; | |
103 | char attrs[modes_len + 1]; | |
104 | char *at[3]; | |
105 | int i; | |
106 | ||
107 | memcpy (attrs, modes, modes_len); | |
108 | at[0] = attrs; | |
109 | for (i = 0; i < 2; i++) | |
110 | { | |
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. */ | |
114 | } | |
115 | return close_on_exec (fopen (filename, at[0], at[1], at[2])); | |
d387240a | 116 | } |
0b8448af NC |
117 | |
118 | #elif defined (_WIN32) | |
ba0eb22c NC |
119 | /* PR 25713: Handle extra long path names possibly containing '..' and '.'. */ |
120 | wchar_t ** lpFilePart = {NULL}; | |
121 | const wchar_t prefix[] = L"\\\\?\\"; | |
ba0eb22c | 122 | const size_t partPathLen = strlen (filename) + 1; |
17d60030 | 123 | |
cb7da2a6 | 124 | /* Converting the partial path from ascii to unicode. |
ba0eb22c NC |
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)); | |
129 | size_t ix; | |
17d60030 | 130 | |
ba0eb22c | 131 | MultiByteToWideChar (CP_UTF8, 0, filename, -1, partPath, partPathWSize); |
17d60030 | 132 | |
cb7da2a6 | 133 | /* Convert any UNIX style path separators into the DOS i.e. backslash separator. */ |
cb7da2a6 TS |
134 | for (ix = 0; ix < partPathLen; ix++) |
135 | if (IS_UNIX_DIR_SEPARATOR(filename[ix])) | |
136 | partPath[ix] = '\\'; | |
0b8448af | 137 | |
cb7da2a6 | 138 | /* Getting the full path from the provided partial path. |
ba0eb22c NC |
139 | 1) Get the length. |
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)); | |
cb7da2a6 TS |
143 | |
144 | wcscpy (fullPath, prefix); | |
ba0eb22c NC |
145 | |
146 | int prefixLen = sizeof(prefix) / sizeof(wchar_t); | |
147 | wchar_t * fullPathOffset = fullPath + prefixLen - 1; | |
148 | ||
cb7da2a6 TS |
149 | GetFullPathNameW (partPath, fullPathWSize, fullPathOffset, lpFilePart); |
150 | free (partPath); | |
151 | ||
152 | /* It is non-standard for modes to exceed 16 characters. */ | |
5f3fc928 | 153 | wchar_t modesW[16]; |
ba0eb22c | 154 | |
cb7da2a6 | 155 | MultiByteToWideChar (CP_UTF8, 0, modes, -1, modesW, sizeof(modesW)); |
cb7da2a6 | 156 | |
ba0eb22c | 157 | FILE * file = _wfopen (fullPath, modesW); |
cb7da2a6 TS |
158 | free (fullPath); |
159 | ||
160 | return close_on_exec (file); | |
0b8448af NC |
161 | |
162 | #elif defined (HAVE_FOPEN64) | |
428b207a | 163 | return close_on_exec (fopen64 (filename, modes)); |
0b8448af | 164 | |
cb7da2a6 | 165 | #else |
0b8448af | 166 | return close_on_exec (fopen (filename, modes)); |
cb7da2a6 | 167 | #endif |
2e6f4fae DJ |
168 | } |
169 | ||
40838a72 AC |
170 | /* |
171 | INTERNAL_DEFINITION | |
172 | struct bfd_iovec | |
93509525 | 173 | |
40838a72 | 174 | DESCRIPTION |
93509525 | 175 | |
40838a72 AC |
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). | |
180 | ||
181 | .struct bfd_iovec | |
182 | .{ | |
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, | |
07d6d2b8 | 191 | . file_ptr nbytes); |
40838a72 AC |
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. | |
07d6d2b8 | 196 | . Otherwise, a value of -1 is returned (and <<bfd_error>> is set). *} |
40838a72 | 197 | . int (*bseek) (struct bfd *abfd, file_ptr offset, int whence); |
405bf443 | 198 | . int (*bclose) (struct bfd *abfd); |
40838a72 AC |
199 | . int (*bflush) (struct bfd *abfd); |
200 | . int (*bstat) (struct bfd *abfd, struct stat *sb); | |
4c95ab76 TG |
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. *} | |
f07749bb | 207 | . void *(*bmmap) (struct bfd *abfd, void *addr, bfd_size_type len, |
07d6d2b8 AM |
208 | . int prot, int flags, file_ptr offset, |
209 | . void **map_addr, bfd_size_type *map_len); | |
40838a72 | 210 | .}; |
93509525 | 211 | |
65077aa8 TG |
212 | .extern const struct bfd_iovec _bfd_memory_iovec; |
213 | ||
40838a72 | 214 | */ |
93509525 | 215 | |
93509525 KD |
216 | |
217 | /* Return value is amount read. */ | |
218 | ||
219 | bfd_size_type | |
c58b9523 | 220 | bfd_bread (void *ptr, bfd_size_type size, bfd *abfd) |
93509525 | 221 | { |
5c4ce239 AM |
222 | file_ptr nread; |
223 | bfd *element_bfd = abfd; | |
224 | ufile_ptr offset = 0; | |
225 | ||
226 | while (abfd->my_archive != NULL | |
227 | && !bfd_is_thin_archive (abfd->my_archive)) | |
228 | { | |
229 | offset += abfd->origin; | |
230 | abfd = abfd->my_archive; | |
231 | } | |
4d095f5b | 232 | offset += abfd->origin; |
93509525 | 233 | |
c17eb63b | 234 | /* If this is a non-thin archive element, don't read past the end of |
1fb41da4 | 235 | this element. */ |
c17eb63b L |
236 | if (element_bfd->arelt_data != NULL |
237 | && element_bfd->my_archive != NULL | |
238 | && !bfd_is_thin_archive (element_bfd->my_archive)) | |
1fb41da4 | 239 | { |
5c4ce239 | 240 | bfd_size_type maxbytes = arelt_size (element_bfd); |
f1bb16f8 | 241 | |
5c4ce239 | 242 | if (abfd->where < offset || abfd->where - offset >= maxbytes) |
07d6d2b8 | 243 | { |
5c4ce239 AM |
244 | bfd_set_error (bfd_error_invalid_operation); |
245 | return -1; | |
07d6d2b8 | 246 | } |
5c4ce239 AM |
247 | if (abfd->where - offset + size > maxbytes) |
248 | size = maxbytes - (abfd->where - offset); | |
1fb41da4 AM |
249 | } |
250 | ||
5c4ce239 AM |
251 | if (abfd->iovec == NULL) |
252 | { | |
253 | bfd_set_error (bfd_error_invalid_operation); | |
254 | return -1; | |
255 | } | |
256 | ||
257 | nread = abfd->iovec->bread (abfd, ptr, size); | |
258 | if (nread != -1) | |
93509525 KD |
259 | abfd->where += nread; |
260 | ||
93509525 KD |
261 | return nread; |
262 | } | |
263 | ||
264 | bfd_size_type | |
c58b9523 | 265 | bfd_bwrite (const void *ptr, bfd_size_type size, bfd *abfd) |
93509525 | 266 | { |
5c4ce239 | 267 | file_ptr nwrote; |
93509525 | 268 | |
5c4ce239 AM |
269 | while (abfd->my_archive != NULL |
270 | && !bfd_is_thin_archive (abfd->my_archive)) | |
271 | abfd = abfd->my_archive; | |
69fd4758 | 272 | |
5c4ce239 AM |
273 | if (abfd->iovec == NULL) |
274 | { | |
275 | bfd_set_error (bfd_error_invalid_operation); | |
276 | return -1; | |
277 | } | |
278 | ||
279 | nwrote = abfd->iovec->bwrite (abfd, ptr, size); | |
280 | if (nwrote != -1) | |
93509525 | 281 | abfd->where += nwrote; |
5c4ce239 | 282 | if ((bfd_size_type) nwrote != size) |
93509525 KD |
283 | { |
284 | #ifdef ENOSPC | |
285 | errno = ENOSPC; | |
286 | #endif | |
287 | bfd_set_error (bfd_error_system_call); | |
288 | } | |
289 | return nwrote; | |
290 | } | |
291 | ||
7c192733 | 292 | file_ptr |
c58b9523 | 293 | bfd_tell (bfd *abfd) |
93509525 | 294 | { |
5c4ce239 | 295 | ufile_ptr offset = 0; |
93509525 KD |
296 | file_ptr ptr; |
297 | ||
5c4ce239 AM |
298 | while (abfd->my_archive != NULL |
299 | && !bfd_is_thin_archive (abfd->my_archive)) | |
69fd4758 | 300 | { |
5c4ce239 AM |
301 | offset += abfd->origin; |
302 | abfd = abfd->my_archive; | |
69fd4758 | 303 | } |
4d095f5b | 304 | offset += abfd->origin; |
93509525 | 305 | |
5c4ce239 AM |
306 | if (abfd->iovec == NULL) |
307 | return 0; | |
308 | ||
309 | ptr = abfd->iovec->btell (abfd); | |
93509525 | 310 | abfd->where = ptr; |
5c4ce239 | 311 | return ptr - offset; |
93509525 KD |
312 | } |
313 | ||
314 | int | |
c58b9523 | 315 | bfd_flush (bfd *abfd) |
93509525 | 316 | { |
5c4ce239 AM |
317 | while (abfd->my_archive != NULL |
318 | && !bfd_is_thin_archive (abfd->my_archive)) | |
319 | abfd = abfd->my_archive; | |
320 | ||
321 | if (abfd->iovec == NULL) | |
322 | return 0; | |
323 | ||
324 | return abfd->iovec->bflush (abfd); | |
93509525 KD |
325 | } |
326 | ||
327 | /* Returns 0 for success, negative value for failure (in which case | |
328 | bfd_get_error can retrieve the error code). */ | |
329 | int | |
c58b9523 | 330 | bfd_stat (bfd *abfd, struct stat *statbuf) |
93509525 | 331 | { |
93509525 KD |
332 | int result; |
333 | ||
5c4ce239 AM |
334 | while (abfd->my_archive != NULL |
335 | && !bfd_is_thin_archive (abfd->my_archive)) | |
336 | abfd = abfd->my_archive; | |
69fd4758 | 337 | |
5c4ce239 AM |
338 | if (abfd->iovec == NULL) |
339 | { | |
340 | bfd_set_error (bfd_error_invalid_operation); | |
341 | return -1; | |
342 | } | |
343 | ||
344 | result = abfd->iovec->bstat (abfd, statbuf); | |
93509525 KD |
345 | if (result < 0) |
346 | bfd_set_error (bfd_error_system_call); | |
347 | return result; | |
348 | } | |
349 | ||
350 | /* Returns 0 for success, nonzero for failure (in which case bfd_get_error | |
351 | can retrieve the error code). */ | |
352 | ||
353 | int | |
c58b9523 | 354 | bfd_seek (bfd *abfd, file_ptr position, int direction) |
93509525 KD |
355 | { |
356 | int result; | |
5c4ce239 | 357 | ufile_ptr offset = 0; |
93509525 | 358 | |
5c4ce239 AM |
359 | while (abfd->my_archive != NULL |
360 | && !bfd_is_thin_archive (abfd->my_archive)) | |
93509525 | 361 | { |
5c4ce239 AM |
362 | offset += abfd->origin; |
363 | abfd = abfd->my_archive; | |
93509525 | 364 | } |
4d095f5b | 365 | offset += abfd->origin; |
93509525 | 366 | |
5c4ce239 | 367 | if (abfd->iovec == NULL) |
660722b0 | 368 | { |
5c4ce239 AM |
369 | bfd_set_error (bfd_error_invalid_operation); |
370 | return -1; | |
660722b0 | 371 | } |
93509525 | 372 | |
5c4ce239 AM |
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); | |
377 | ||
378 | if (direction != SEEK_CUR) | |
379 | position += offset; | |
69fd4758 | 380 | |
ff91d2f0 AM |
381 | if ((direction == SEEK_CUR && position == 0) |
382 | || (direction == SEEK_SET && (ufile_ptr) position == abfd->where)) | |
383 | return 0; | |
384 | ||
5c4ce239 | 385 | result = abfd->iovec->bseek (abfd, position, direction); |
93509525 KD |
386 | if (result != 0) |
387 | { | |
93509525 | 388 | /* An EINVAL error probably means that the file offset was |
07d6d2b8 | 389 | absurd. */ |
5c4ce239 | 390 | if (errno == EINVAL) |
93509525 KD |
391 | bfd_set_error (bfd_error_file_truncated); |
392 | else | |
5c4ce239 | 393 | bfd_set_error (bfd_error_system_call); |
93509525 KD |
394 | } |
395 | else | |
396 | { | |
397 | /* Adjust `where' field. */ | |
5c4ce239 | 398 | if (direction == SEEK_CUR) |
93509525 | 399 | abfd->where += position; |
5c4ce239 AM |
400 | else |
401 | abfd->where = position; | |
93509525 | 402 | } |
5c4ce239 | 403 | |
93509525 KD |
404 | return result; |
405 | } | |
406 | ||
407 | /* | |
408 | FUNCTION | |
409 | bfd_get_mtime | |
410 | ||
411 | SYNOPSIS | |
c58b9523 | 412 | long bfd_get_mtime (bfd *abfd); |
93509525 KD |
413 | |
414 | DESCRIPTION | |
415 | Return the file modification time (as read from the file system, or | |
416 | from the archive header for archive members). | |
417 | ||
418 | */ | |
419 | ||
420 | long | |
c58b9523 | 421 | bfd_get_mtime (bfd *abfd) |
93509525 | 422 | { |
93509525 KD |
423 | struct stat buf; |
424 | ||
425 | if (abfd->mtime_set) | |
426 | return abfd->mtime; | |
427 | ||
5c4ce239 | 428 | if (bfd_stat (abfd, &buf) != 0) |
93509525 KD |
429 | return 0; |
430 | ||
431 | abfd->mtime = buf.st_mtime; /* Save value in case anyone wants it */ | |
432 | return buf.st_mtime; | |
433 | } | |
434 | ||
435 | /* | |
436 | FUNCTION | |
437 | bfd_get_size | |
438 | ||
439 | SYNOPSIS | |
47fdcf63 | 440 | ufile_ptr bfd_get_size (bfd *abfd); |
93509525 KD |
441 | |
442 | DESCRIPTION | |
443 | Return the file size (as read from file system) for the file | |
444 | associated with BFD @var{abfd}. | |
445 | ||
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. | |
451 | ||
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. | |
5c4491d3 | 464 | This function at least allows us to answer the question, "is the |
93509525 | 465 | size reasonable?". |
b03202e3 AM |
466 | |
467 | A return value of zero indicates the file size is unknown. | |
93509525 KD |
468 | */ |
469 | ||
47fdcf63 | 470 | ufile_ptr |
c58b9523 | 471 | bfd_get_size (bfd *abfd) |
93509525 | 472 | { |
b03202e3 AM |
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)) | |
476 | { | |
477 | struct stat buf; | |
93509525 | 478 | |
b03202e3 AM |
479 | if (abfd->size == 1 && !bfd_write_p (abfd)) |
480 | return 0; | |
93509525 | 481 | |
b03202e3 AM |
482 | if (bfd_stat (abfd, &buf) != 0 |
483 | || buf.st_size == 0 | |
484 | || buf.st_size - (ufile_ptr) buf.st_size != 0) | |
485 | { | |
486 | abfd->size = 1; | |
487 | return 0; | |
488 | } | |
489 | abfd->size = buf.st_size; | |
490 | } | |
491 | return abfd->size; | |
93509525 | 492 | } |
25b88f33 | 493 | |
8e2f54bc L |
494 | /* |
495 | FUNCTION | |
496 | bfd_get_file_size | |
497 | ||
498 | SYNOPSIS | |
47fdcf63 | 499 | ufile_ptr bfd_get_file_size (bfd *abfd); |
8e2f54bc L |
500 | |
501 | DESCRIPTION | |
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. | |
505 | ||
506 | */ | |
507 | ||
47fdcf63 | 508 | ufile_ptr |
8e2f54bc L |
509 | bfd_get_file_size (bfd *abfd) |
510 | { | |
b570b954 AM |
511 | ufile_ptr file_size, archive_size = (ufile_ptr) -1; |
512 | ||
8e2f54bc L |
513 | if (abfd->my_archive != NULL |
514 | && !bfd_is_thin_archive (abfd->my_archive)) | |
b570b954 AM |
515 | { |
516 | struct areltdata *adata = (struct areltdata *) abfd->arelt_data; | |
c01de193 AM |
517 | if (adata != NULL) |
518 | { | |
519 | archive_size = adata->parsed_size; | |
520 | /* If the archive is compressed we can't compare against | |
521 | file size. */ | |
522 | if (adata->arch_header != NULL | |
523 | && memcmp (((struct ar_hdr *) adata->arch_header)->ar_fmag, | |
524 | "Z\012", 2) == 0) | |
525 | return archive_size; | |
526 | abfd = abfd->my_archive; | |
527 | } | |
b570b954 | 528 | } |
8e2f54bc | 529 | |
b570b954 AM |
530 | file_size = bfd_get_size (abfd); |
531 | if (archive_size < file_size) | |
532 | return archive_size; | |
533 | return file_size; | |
8e2f54bc | 534 | } |
25b88f33 PP |
535 | |
536 | /* | |
537 | FUNCTION | |
538 | bfd_mmap | |
539 | ||
540 | SYNOPSIS | |
541 | void *bfd_mmap (bfd *abfd, void *addr, bfd_size_type len, | |
07d6d2b8 AM |
542 | int prot, int flags, file_ptr offset, |
543 | void **map_addr, bfd_size_type *map_len); | |
25b88f33 PP |
544 | |
545 | DESCRIPTION | |
546 | Return mmap()ed region of the file, if possible and implemented. | |
07d6d2b8 AM |
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. | |
25b88f33 PP |
549 | |
550 | */ | |
551 | ||
552 | void * | |
553 | bfd_mmap (bfd *abfd, void *addr, bfd_size_type len, | |
4c95ab76 | 554 | int prot, int flags, file_ptr offset, |
07d6d2b8 | 555 | void **map_addr, bfd_size_type *map_len) |
25b88f33 | 556 | { |
5c4ce239 AM |
557 | while (abfd->my_archive != NULL |
558 | && !bfd_is_thin_archive (abfd->my_archive)) | |
559 | { | |
560 | offset += abfd->origin; | |
561 | abfd = abfd->my_archive; | |
562 | } | |
4d095f5b | 563 | offset += abfd->origin; |
25b88f33 PP |
564 | |
565 | if (abfd->iovec == NULL) | |
5c4ce239 AM |
566 | { |
567 | bfd_set_error (bfd_error_invalid_operation); | |
568 | return (void *) -1; | |
569 | } | |
25b88f33 | 570 | |
4c95ab76 | 571 | return abfd->iovec->bmmap (abfd, addr, len, prot, flags, offset, |
07d6d2b8 | 572 | map_addr, map_len); |
25b88f33 | 573 | } |
65077aa8 TG |
574 | |
575 | /* Memory file I/O operations. */ | |
576 | ||
577 | static file_ptr | |
578 | memory_bread (bfd *abfd, void *ptr, file_ptr size) | |
579 | { | |
580 | struct bfd_in_memory *bim; | |
581 | bfd_size_type get; | |
582 | ||
583 | bim = (struct bfd_in_memory *) abfd->iostream; | |
584 | get = size; | |
585 | if (abfd->where + get > bim->size) | |
586 | { | |
587 | if (bim->size < (bfd_size_type) abfd->where) | |
07d6d2b8 | 588 | get = 0; |
65077aa8 | 589 | else |
07d6d2b8 | 590 | get = bim->size - abfd->where; |
65077aa8 TG |
591 | bfd_set_error (bfd_error_file_truncated); |
592 | } | |
593 | memcpy (ptr, bim->buffer + abfd->where, (size_t) get); | |
594 | return get; | |
595 | } | |
596 | ||
597 | static file_ptr | |
598 | memory_bwrite (bfd *abfd, const void *ptr, file_ptr size) | |
599 | { | |
600 | struct bfd_in_memory *bim = (struct bfd_in_memory *) abfd->iostream; | |
601 | ||
602 | if (abfd->where + size > bim->size) | |
603 | { | |
604 | bfd_size_type newsize, oldsize; | |
605 | ||
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) | |
07d6d2b8 AM |
611 | { |
612 | bim->buffer = (bfd_byte *) bfd_realloc_or_free (bim->buffer, newsize); | |
613 | if (bim->buffer == NULL) | |
614 | { | |
615 | bim->size = 0; | |
616 | return 0; | |
617 | } | |
618 | if (newsize > bim->size) | |
619 | memset (bim->buffer + bim->size, 0, newsize - bim->size); | |
620 | } | |
65077aa8 TG |
621 | } |
622 | memcpy (bim->buffer + abfd->where, ptr, (size_t) size); | |
623 | return size; | |
624 | } | |
625 | ||
626 | static file_ptr | |
627 | memory_btell (bfd *abfd) | |
628 | { | |
629 | return abfd->where; | |
630 | } | |
631 | ||
632 | static int | |
633 | memory_bseek (bfd *abfd, file_ptr position, int direction) | |
634 | { | |
635 | file_ptr nwhere; | |
636 | struct bfd_in_memory *bim; | |
637 | ||
638 | bim = (struct bfd_in_memory *) abfd->iostream; | |
639 | ||
640 | if (direction == SEEK_SET) | |
641 | nwhere = position; | |
642 | else | |
643 | nwhere = abfd->where + position; | |
644 | ||
645 | if (nwhere < 0) | |
646 | { | |
647 | abfd->where = 0; | |
648 | errno = EINVAL; | |
649 | return -1; | |
650 | } | |
651 | ||
652 | if ((bfd_size_type)nwhere > bim->size) | |
653 | { | |
654 | if (abfd->direction == write_direction | |
07d6d2b8 AM |
655 | || abfd->direction == both_direction) |
656 | { | |
657 | bfd_size_type newsize, oldsize; | |
658 | ||
659 | oldsize = (bim->size + 127) & ~(bfd_size_type) 127; | |
660 | bim->size = nwhere; | |
661 | /* Round up to cut down on memory fragmentation */ | |
662 | newsize = (bim->size + 127) & ~(bfd_size_type) 127; | |
663 | if (newsize > oldsize) | |
664 | { | |
665 | bim->buffer = (bfd_byte *) bfd_realloc_or_free (bim->buffer, newsize); | |
666 | if (bim->buffer == NULL) | |
667 | { | |
668 | errno = EINVAL; | |
669 | bim->size = 0; | |
670 | return -1; | |
671 | } | |
672 | memset (bim->buffer + oldsize, 0, newsize - oldsize); | |
673 | } | |
674 | } | |
65077aa8 | 675 | else |
07d6d2b8 AM |
676 | { |
677 | abfd->where = bim->size; | |
678 | errno = EINVAL; | |
679 | bfd_set_error (bfd_error_file_truncated); | |
680 | return -1; | |
681 | } | |
65077aa8 TG |
682 | } |
683 | return 0; | |
684 | } | |
685 | ||
405bf443 | 686 | static int |
65077aa8 TG |
687 | memory_bclose (struct bfd *abfd) |
688 | { | |
689 | struct bfd_in_memory *bim = (struct bfd_in_memory *) abfd->iostream; | |
690 | ||
c9594989 | 691 | free (bim->buffer); |
65077aa8 TG |
692 | free (bim); |
693 | abfd->iostream = NULL; | |
694 | ||
405bf443 | 695 | return 0; |
65077aa8 TG |
696 | } |
697 | ||
698 | static int | |
699 | memory_bflush (bfd *abfd ATTRIBUTE_UNUSED) | |
700 | { | |
701 | return 0; | |
702 | } | |
703 | ||
704 | static int | |
705 | memory_bstat (bfd *abfd, struct stat *statbuf) | |
706 | { | |
707 | struct bfd_in_memory *bim = (struct bfd_in_memory *) abfd->iostream; | |
708 | ||
b5dee4ea | 709 | memset (statbuf, 0, sizeof (*statbuf)); |
65077aa8 TG |
710 | statbuf->st_size = bim->size; |
711 | ||
712 | return 0; | |
713 | } | |
714 | ||
715 | static void * | |
716 | memory_bmmap (bfd *abfd ATTRIBUTE_UNUSED, void *addr ATTRIBUTE_UNUSED, | |
07d6d2b8 AM |
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) | |
65077aa8 TG |
721 | { |
722 | return (void *)-1; | |
723 | } | |
724 | ||
725 | const struct bfd_iovec _bfd_memory_iovec = | |
726 | { | |
727 | &memory_bread, &memory_bwrite, &memory_btell, &memory_bseek, | |
728 | &memory_bclose, &memory_bflush, &memory_bstat, &memory_bmmap | |
729 | }; |