* opncls.c (_bfd_new_bfd_contained_in): Check return value of
[deliverable/binutils-gdb.git] / bfd / opncls.c
1 /* opncls.c -- open and close a BFD.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2000,
3 2001, 2002
4 Free Software Foundation, Inc.
5
6 Written by Cygnus Support.
7
8 This file is part of BFD, the Binary File Descriptor library.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23
24 #include "bfd.h"
25 #include "sysdep.h"
26 #include "objalloc.h"
27 #include "libbfd.h"
28
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
39 /* fdopen is a loser -- we should use stdio exclusively. Unfortunately
40 if we do that we can't use fcntl. */
41
42 /* Return a new BFD. All BFD's are allocated through this routine. */
43
44 bfd *
45 _bfd_new_bfd ()
46 {
47 bfd *nbfd;
48
49 nbfd = (bfd *) bfd_zmalloc ((bfd_size_type) sizeof (bfd));
50 if (nbfd == NULL)
51 return NULL;
52
53 nbfd->memory = (PTR) objalloc_create ();
54 if (nbfd->memory == NULL)
55 {
56 bfd_set_error (bfd_error_no_memory);
57 free (nbfd);
58 return NULL;
59 }
60
61 nbfd->arch_info = &bfd_default_arch_struct;
62
63 nbfd->direction = no_direction;
64 nbfd->iostream = NULL;
65 nbfd->where = 0;
66 if (!bfd_hash_table_init (&nbfd->section_htab, bfd_section_hash_newfunc))
67 {
68 free (nbfd);
69 return NULL;
70 }
71 nbfd->sections = (asection *) NULL;
72 nbfd->section_tail = &nbfd->sections;
73 nbfd->format = bfd_unknown;
74 nbfd->my_archive = (bfd *) NULL;
75 nbfd->origin = 0;
76 nbfd->opened_once = false;
77 nbfd->output_has_begun = false;
78 nbfd->section_count = 0;
79 nbfd->usrdata = (PTR) NULL;
80 nbfd->cacheable = false;
81 nbfd->flags = BFD_NO_FLAGS;
82 nbfd->mtime_set = false;
83
84 return nbfd;
85 }
86
87 /* Allocate a new BFD as a member of archive OBFD. */
88
89 bfd *
90 _bfd_new_bfd_contained_in (obfd)
91 bfd *obfd;
92 {
93 bfd *nbfd;
94
95 nbfd = _bfd_new_bfd ();
96 if (nbfd == NULL)
97 return NULL;
98 nbfd->xvec = obfd->xvec;
99 nbfd->my_archive = obfd;
100 nbfd->direction = read_direction;
101 nbfd->target_defaulted = obfd->target_defaulted;
102 return nbfd;
103 }
104
105 /* Delete a BFD. */
106
107 void
108 _bfd_delete_bfd (abfd)
109 bfd *abfd;
110 {
111 bfd_hash_table_free (&abfd->section_htab);
112 objalloc_free ((struct objalloc *) abfd->memory);
113 free (abfd);
114 }
115
116 /*
117 SECTION
118 Opening and closing BFDs
119
120 */
121
122 /*
123 FUNCTION
124 bfd_openr
125
126 SYNOPSIS
127 bfd *bfd_openr(const char *filename, const char *target);
128
129 DESCRIPTION
130 Open the file @var{filename} (using <<fopen>>) with the target
131 @var{target}. Return a pointer to the created BFD.
132
133 Calls <<bfd_find_target>>, so @var{target} is interpreted as by
134 that function.
135
136 If <<NULL>> is returned then an error has occured. Possible errors
137 are <<bfd_error_no_memory>>, <<bfd_error_invalid_target>> or <<system_call>> error.
138 */
139
140 bfd *
141 bfd_openr (filename, target)
142 const char *filename;
143 const char *target;
144 {
145 bfd *nbfd;
146 const bfd_target *target_vec;
147
148 nbfd = _bfd_new_bfd ();
149 if (nbfd == NULL)
150 return NULL;
151
152 target_vec = bfd_find_target (target, nbfd);
153 if (target_vec == NULL)
154 {
155 bfd_set_error (bfd_error_invalid_target);
156 _bfd_delete_bfd (nbfd);
157 return NULL;
158 }
159
160 nbfd->filename = filename;
161 nbfd->direction = read_direction;
162
163 if (bfd_open_file (nbfd) == NULL)
164 {
165 /* File didn't exist, or some such. */
166 bfd_set_error (bfd_error_system_call);
167 _bfd_delete_bfd (nbfd);
168 return NULL;
169 }
170
171 return nbfd;
172 }
173
174 /* Don't try to `optimize' this function:
175
176 o - We lock using stack space so that interrupting the locking
177 won't cause a storage leak.
178 o - We open the file stream last, since we don't want to have to
179 close it if anything goes wrong. Closing the stream means closing
180 the file descriptor too, even though we didn't open it. */
181 /*
182 FUNCTION
183 bfd_fdopenr
184
185 SYNOPSIS
186 bfd *bfd_fdopenr(const char *filename, const char *target, int fd);
187
188 DESCRIPTION
189 <<bfd_fdopenr>> is to <<bfd_fopenr>> much like <<fdopen>> is to <<fopen>>.
190 It opens a BFD on a file already described by the @var{fd}
191 supplied.
192
193 When the file is later <<bfd_close>>d, the file descriptor will be closed.
194
195 If the caller desires that this file descriptor be cached by BFD
196 (opened as needed, closed as needed to free descriptors for
197 other opens), with the supplied @var{fd} used as an initial
198 file descriptor (but subject to closure at any time), call
199 bfd_set_cacheable(bfd, 1) on the returned BFD. The default is to
200 assume no cacheing; the file descriptor will remain open until
201 <<bfd_close>>, and will not be affected by BFD operations on other
202 files.
203
204 Possible errors are <<bfd_error_no_memory>>, <<bfd_error_invalid_target>> and <<bfd_error_system_call>>.
205 */
206
207 bfd *
208 bfd_fdopenr (filename, target, fd)
209 const char *filename;
210 const char *target;
211 int fd;
212 {
213 bfd *nbfd;
214 const bfd_target *target_vec;
215 int fdflags;
216
217 bfd_set_error (bfd_error_system_call);
218 #if ! defined(HAVE_FCNTL) || ! defined(F_GETFL)
219 fdflags = O_RDWR; /* Assume full access. */
220 #else
221 fdflags = fcntl (fd, F_GETFL, NULL);
222 #endif
223 if (fdflags == -1) return NULL;
224
225 nbfd = _bfd_new_bfd ();
226 if (nbfd == NULL)
227 return NULL;
228
229 target_vec = bfd_find_target (target, nbfd);
230 if (target_vec == NULL)
231 {
232 bfd_set_error (bfd_error_invalid_target);
233 _bfd_delete_bfd (nbfd);
234 return NULL;
235 }
236
237 #ifndef HAVE_FDOPEN
238 nbfd->iostream = (PTR) fopen (filename, FOPEN_RB);
239 #else
240 /* (O_ACCMODE) parens are to avoid Ultrix header file bug. */
241 switch (fdflags & (O_ACCMODE))
242 {
243 case O_RDONLY: nbfd->iostream = (PTR) fdopen (fd, FOPEN_RB); break;
244 case O_WRONLY: nbfd->iostream = (PTR) fdopen (fd, FOPEN_RUB); break;
245 case O_RDWR: nbfd->iostream = (PTR) fdopen (fd, FOPEN_RUB); break;
246 default: abort ();
247 }
248 #endif
249
250 if (nbfd->iostream == NULL)
251 {
252 _bfd_delete_bfd (nbfd);
253 return NULL;
254 }
255
256 /* OK, put everything where it belongs. */
257 nbfd->filename = filename;
258
259 /* As a special case we allow a FD open for read/write to
260 be written through, although doing so requires that we end
261 the previous clause with a preposition. */
262 /* (O_ACCMODE) parens are to avoid Ultrix header file bug. */
263 switch (fdflags & (O_ACCMODE))
264 {
265 case O_RDONLY: nbfd->direction = read_direction; break;
266 case O_WRONLY: nbfd->direction = write_direction; break;
267 case O_RDWR: nbfd->direction = both_direction; break;
268 default: abort ();
269 }
270
271 if (! bfd_cache_init (nbfd))
272 {
273 _bfd_delete_bfd (nbfd);
274 return NULL;
275 }
276 nbfd->opened_once = true;
277
278 return nbfd;
279 }
280
281 /*
282 FUNCTION
283 bfd_openstreamr
284
285 SYNOPSIS
286 bfd *bfd_openstreamr(const char *, const char *, PTR);
287
288 DESCRIPTION
289
290 Open a BFD for read access on an existing stdio stream. When
291 the BFD is passed to <<bfd_close>>, the stream will be closed.
292 */
293
294 bfd *
295 bfd_openstreamr (filename, target, streamarg)
296 const char *filename;
297 const char *target;
298 PTR streamarg;
299 {
300 FILE *stream = (FILE *) streamarg;
301 bfd *nbfd;
302 const bfd_target *target_vec;
303
304 nbfd = _bfd_new_bfd ();
305 if (nbfd == NULL)
306 return NULL;
307
308 target_vec = bfd_find_target (target, nbfd);
309 if (target_vec == NULL)
310 {
311 bfd_set_error (bfd_error_invalid_target);
312 _bfd_delete_bfd (nbfd);
313 return NULL;
314 }
315
316 nbfd->iostream = (PTR) stream;
317 nbfd->filename = filename;
318 nbfd->direction = read_direction;
319
320 if (! bfd_cache_init (nbfd))
321 {
322 _bfd_delete_bfd (nbfd);
323 return NULL;
324 }
325
326 return nbfd;
327 }
328 \f
329 /* bfd_openw -- open for writing.
330 Returns a pointer to a freshly-allocated BFD on success, or NULL.
331
332 See comment by bfd_fdopenr before you try to modify this function. */
333
334 /*
335 FUNCTION
336 bfd_openw
337
338 SYNOPSIS
339 bfd *bfd_openw(const char *filename, const char *target);
340
341 DESCRIPTION
342 Create a BFD, associated with file @var{filename}, using the
343 file format @var{target}, and return a pointer to it.
344
345 Possible errors are <<bfd_error_system_call>>, <<bfd_error_no_memory>>,
346 <<bfd_error_invalid_target>>.
347 */
348
349 bfd *
350 bfd_openw (filename, target)
351 const char *filename;
352 const char *target;
353 {
354 bfd *nbfd;
355 const bfd_target *target_vec;
356
357 bfd_set_error (bfd_error_system_call);
358
359 /* nbfd has to point to head of malloc'ed block so that bfd_close may
360 reclaim it correctly. */
361 nbfd = _bfd_new_bfd ();
362 if (nbfd == NULL)
363 return NULL;
364
365 target_vec = bfd_find_target (target, nbfd);
366 if (target_vec == NULL)
367 {
368 _bfd_delete_bfd (nbfd);
369 return NULL;
370 }
371
372 nbfd->filename = filename;
373 nbfd->direction = write_direction;
374
375 if (bfd_open_file (nbfd) == NULL)
376 {
377 /* File not writeable, etc. */
378 bfd_set_error (bfd_error_system_call);
379 _bfd_delete_bfd (nbfd);
380 return NULL;
381 }
382
383 return nbfd;
384 }
385
386 /*
387
388 FUNCTION
389 bfd_close
390
391 SYNOPSIS
392 boolean bfd_close(bfd *abfd);
393
394 DESCRIPTION
395
396 Close a BFD. If the BFD was open for writing,
397 then pending operations are completed and the file written out
398 and closed. If the created file is executable, then
399 <<chmod>> is called to mark it as such.
400
401 All memory attached to the BFD is released.
402
403 The file descriptor associated with the BFD is closed (even
404 if it was passed in to BFD by <<bfd_fdopenr>>).
405
406 RETURNS
407 <<true>> is returned if all is ok, otherwise <<false>>.
408 */
409
410
411 boolean
412 bfd_close (abfd)
413 bfd *abfd;
414 {
415 boolean ret;
416
417 if (bfd_write_p (abfd))
418 {
419 if (! BFD_SEND_FMT (abfd, _bfd_write_contents, (abfd)))
420 return false;
421 }
422
423 if (! BFD_SEND (abfd, _close_and_cleanup, (abfd)))
424 return false;
425
426 ret = bfd_cache_close (abfd);
427
428 /* If the file was open for writing and is now executable,
429 make it so. */
430 if (ret
431 && abfd->direction == write_direction
432 && abfd->flags & EXEC_P)
433 {
434 struct stat buf;
435
436 if (stat (abfd->filename, &buf) == 0)
437 {
438 unsigned int mask = umask (0);
439
440 umask (mask);
441 chmod (abfd->filename,
442 (0777
443 & (buf.st_mode | ((S_IXUSR | S_IXGRP | S_IXOTH) &~ mask))));
444 }
445 }
446
447 _bfd_delete_bfd (abfd);
448
449 return ret;
450 }
451
452 /*
453 FUNCTION
454 bfd_close_all_done
455
456 SYNOPSIS
457 boolean bfd_close_all_done(bfd *);
458
459 DESCRIPTION
460 Close a BFD. Differs from <<bfd_close>>
461 since it does not complete any pending operations. This
462 routine would be used if the application had just used BFD for
463 swapping and didn't want to use any of the writing code.
464
465 If the created file is executable, then <<chmod>> is called
466 to mark it as such.
467
468 All memory attached to the BFD is released.
469
470 RETURNS
471 <<true>> is returned if all is ok, otherwise <<false>>.
472 */
473
474 boolean
475 bfd_close_all_done (abfd)
476 bfd *abfd;
477 {
478 boolean ret;
479
480 ret = bfd_cache_close (abfd);
481
482 /* If the file was open for writing and is now executable,
483 make it so. */
484 if (ret
485 && abfd->direction == write_direction
486 && abfd->flags & EXEC_P)
487 {
488 struct stat buf;
489
490 if (stat (abfd->filename, &buf) == 0)
491 {
492 unsigned int mask = umask (0);
493
494 umask (mask);
495 chmod (abfd->filename,
496 (0777
497 & (buf.st_mode | ((S_IXUSR | S_IXGRP | S_IXOTH) &~ mask))));
498 }
499 }
500
501 _bfd_delete_bfd (abfd);
502
503 return ret;
504 }
505
506 /*
507 FUNCTION
508 bfd_create
509
510 SYNOPSIS
511 bfd *bfd_create(const char *filename, bfd *templ);
512
513 DESCRIPTION
514 Create a new BFD in the manner of
515 <<bfd_openw>>, but without opening a file. The new BFD
516 takes the target from the target used by @var{template}. The
517 format is always set to <<bfd_object>>.
518 */
519
520 bfd *
521 bfd_create (filename, templ)
522 const char *filename;
523 bfd *templ;
524 {
525 bfd *nbfd;
526
527 nbfd = _bfd_new_bfd ();
528 if (nbfd == NULL)
529 return NULL;
530 nbfd->filename = filename;
531 if (templ)
532 nbfd->xvec = templ->xvec;
533 nbfd->direction = no_direction;
534 bfd_set_format (nbfd, bfd_object);
535
536 return nbfd;
537 }
538
539 /*
540 FUNCTION
541 bfd_make_writable
542
543 SYNOPSIS
544 boolean bfd_make_writable(bfd *abfd);
545
546 DESCRIPTION
547 Takes a BFD as created by <<bfd_create>> and converts it
548 into one like as returned by <<bfd_openw>>. It does this
549 by converting the BFD to BFD_IN_MEMORY. It's assumed that
550 you will call <<bfd_make_readable>> on this bfd later.
551
552 RETURNS
553 <<true>> is returned if all is ok, otherwise <<false>>.
554 */
555
556 boolean
557 bfd_make_writable(abfd)
558 bfd *abfd;
559 {
560 struct bfd_in_memory *bim;
561
562 if (abfd->direction != no_direction)
563 {
564 bfd_set_error (bfd_error_invalid_operation);
565 return false;
566 }
567
568 bim = ((struct bfd_in_memory *)
569 bfd_malloc ((bfd_size_type) sizeof (struct bfd_in_memory)));
570 abfd->iostream = (PTR) bim;
571 /* bfd_bwrite will grow these as needed. */
572 bim->size = 0;
573 bim->buffer = 0;
574
575 abfd->flags |= BFD_IN_MEMORY;
576 abfd->direction = write_direction;
577 abfd->where = 0;
578
579 return true;
580 }
581
582 /*
583 FUNCTION
584 bfd_make_readable
585
586 SYNOPSIS
587 boolean bfd_make_readable(bfd *abfd);
588
589 DESCRIPTION
590 Takes a BFD as created by <<bfd_create>> and
591 <<bfd_make_writable>> and converts it into one like as
592 returned by <<bfd_openr>>. It does this by writing the
593 contents out to the memory buffer, then reversing the
594 direction.
595
596 RETURNS
597 <<true>> is returned if all is ok, otherwise <<false>>. */
598
599 boolean
600 bfd_make_readable(abfd)
601 bfd *abfd;
602 {
603 if (abfd->direction != write_direction || !(abfd->flags & BFD_IN_MEMORY))
604 {
605 bfd_set_error (bfd_error_invalid_operation);
606 return false;
607 }
608
609 if (! BFD_SEND_FMT (abfd, _bfd_write_contents, (abfd)))
610 return false;
611
612 if (! BFD_SEND (abfd, _close_and_cleanup, (abfd)))
613 return false;
614
615
616 abfd->arch_info = &bfd_default_arch_struct;
617
618 abfd->where = 0;
619 abfd->format = bfd_unknown;
620 abfd->my_archive = (bfd *) NULL;
621 abfd->origin = 0;
622 abfd->opened_once = false;
623 abfd->output_has_begun = false;
624 abfd->section_count = 0;
625 abfd->usrdata = (PTR) NULL;
626 abfd->cacheable = false;
627 abfd->flags = BFD_IN_MEMORY;
628 abfd->mtime_set = false;
629
630 abfd->target_defaulted = true;
631 abfd->direction = read_direction;
632 abfd->sections = 0;
633 abfd->symcount = 0;
634 abfd->outsymbols = 0;
635 abfd->tdata.any = 0;
636
637 bfd_section_list_clear (abfd);
638 bfd_check_format (abfd, bfd_object);
639
640 return true;
641 }
642
643 /*
644 INTERNAL_FUNCTION
645 bfd_alloc
646
647 SYNOPSIS
648 PTR bfd_alloc (bfd *abfd, size_t wanted);
649
650 DESCRIPTION
651 Allocate a block of @var{wanted} bytes of memory attached to
652 <<abfd>> and return a pointer to it.
653 */
654
655
656 PTR
657 bfd_alloc (abfd, size)
658 bfd *abfd;
659 bfd_size_type size;
660 {
661 PTR ret;
662
663 if (size != (unsigned long) size)
664 {
665 bfd_set_error (bfd_error_no_memory);
666 return NULL;
667 }
668
669 ret = objalloc_alloc (abfd->memory, (unsigned long) size);
670 if (ret == NULL)
671 bfd_set_error (bfd_error_no_memory);
672 return ret;
673 }
674
675 PTR
676 bfd_zalloc (abfd, size)
677 bfd *abfd;
678 bfd_size_type size;
679 {
680 PTR res;
681
682 res = bfd_alloc (abfd, size);
683 if (res)
684 memset (res, 0, (size_t) size);
685 return res;
686 }
687
688 /* Free a block allocated for a BFD.
689 Note: Also frees all more recently allocated blocks! */
690
691 void
692 bfd_release (abfd, block)
693 bfd *abfd;
694 PTR block;
695 {
696 objalloc_free_block ((struct objalloc *) abfd->memory, block);
697 }
This page took 0.044755 seconds and 4 git commands to generate.