1 /* opncls.c -- open and close a BFD.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2000,
4 Free Software Foundation, Inc.
6 Written by Cygnus Support.
8 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
30 #define S_IXUSR 0100 /* Execute by owner. */
33 #define S_IXGRP 0010 /* Execute by group. */
36 #define S_IXOTH 0001 /* Execute by others. */
39 /* fdopen is a loser -- we should use stdio exclusively. Unfortunately
40 if we do that we can't use fcntl. */
42 /* Return a new BFD. All BFD's are allocated through this routine. */
49 nbfd
= (bfd
*) bfd_zmalloc ((bfd_size_type
) sizeof (bfd
));
53 nbfd
->memory
= (PTR
) objalloc_create ();
54 if (nbfd
->memory
== NULL
)
56 bfd_set_error (bfd_error_no_memory
);
61 nbfd
->arch_info
= &bfd_default_arch_struct
;
63 nbfd
->direction
= no_direction
;
64 nbfd
->iostream
= NULL
;
66 if (!bfd_hash_table_init (&nbfd
->section_htab
, bfd_section_hash_newfunc
))
71 nbfd
->sections
= (asection
*) NULL
;
72 nbfd
->section_tail
= &nbfd
->sections
;
73 nbfd
->format
= bfd_unknown
;
74 nbfd
->my_archive
= (bfd
*) NULL
;
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;
87 /* Allocate a new BFD as a member of archive OBFD. */
90 _bfd_new_bfd_contained_in (obfd
)
95 nbfd
= _bfd_new_bfd ();
96 nbfd
->xvec
= obfd
->xvec
;
97 nbfd
->my_archive
= obfd
;
98 nbfd
->direction
= read_direction
;
99 nbfd
->target_defaulted
= obfd
->target_defaulted
;
106 _bfd_delete_bfd (abfd
)
109 bfd_hash_table_free (&abfd
->section_htab
);
110 objalloc_free ((struct objalloc
*) abfd
->memory
);
116 Opening and closing BFDs
125 bfd *bfd_openr(const char *filename, const char *target);
128 Open the file @var{filename} (using <<fopen>>) with the target
129 @var{target}. Return a pointer to the created BFD.
131 Calls <<bfd_find_target>>, so @var{target} is interpreted as by
134 If <<NULL>> is returned then an error has occured. Possible errors
135 are <<bfd_error_no_memory>>, <<bfd_error_invalid_target>> or <<system_call>> error.
139 bfd_openr (filename
, target
)
140 const char *filename
;
144 const bfd_target
*target_vec
;
146 nbfd
= _bfd_new_bfd ();
150 target_vec
= bfd_find_target (target
, nbfd
);
151 if (target_vec
== NULL
)
153 bfd_set_error (bfd_error_invalid_target
);
154 _bfd_delete_bfd (nbfd
);
158 nbfd
->filename
= filename
;
159 nbfd
->direction
= read_direction
;
161 if (bfd_open_file (nbfd
) == NULL
)
163 /* File didn't exist, or some such. */
164 bfd_set_error (bfd_error_system_call
);
165 _bfd_delete_bfd (nbfd
);
172 /* Don't try to `optimize' this function:
174 o - We lock using stack space so that interrupting the locking
175 won't cause a storage leak.
176 o - We open the file stream last, since we don't want to have to
177 close it if anything goes wrong. Closing the stream means closing
178 the file descriptor too, even though we didn't open it. */
184 bfd *bfd_fdopenr(const char *filename, const char *target, int fd);
187 <<bfd_fdopenr>> is to <<bfd_fopenr>> much like <<fdopen>> is to <<fopen>>.
188 It opens a BFD on a file already described by the @var{fd}
191 When the file is later <<bfd_close>>d, the file descriptor will be closed.
193 If the caller desires that this file descriptor be cached by BFD
194 (opened as needed, closed as needed to free descriptors for
195 other opens), with the supplied @var{fd} used as an initial
196 file descriptor (but subject to closure at any time), call
197 bfd_set_cacheable(bfd, 1) on the returned BFD. The default is to
198 assume no cacheing; the file descriptor will remain open until
199 <<bfd_close>>, and will not be affected by BFD operations on other
202 Possible errors are <<bfd_error_no_memory>>, <<bfd_error_invalid_target>> and <<bfd_error_system_call>>.
206 bfd_fdopenr (filename
, target
, fd
)
207 const char *filename
;
212 const bfd_target
*target_vec
;
215 bfd_set_error (bfd_error_system_call
);
216 #if ! defined(HAVE_FCNTL) || ! defined(F_GETFL)
217 fdflags
= O_RDWR
; /* Assume full access. */
219 fdflags
= fcntl (fd
, F_GETFL
, NULL
);
221 if (fdflags
== -1) return NULL
;
223 nbfd
= _bfd_new_bfd ();
227 target_vec
= bfd_find_target (target
, nbfd
);
228 if (target_vec
== NULL
)
230 bfd_set_error (bfd_error_invalid_target
);
231 _bfd_delete_bfd (nbfd
);
236 nbfd
->iostream
= (PTR
) fopen (filename
, FOPEN_RB
);
238 /* (O_ACCMODE) parens are to avoid Ultrix header file bug. */
239 switch (fdflags
& (O_ACCMODE
))
241 case O_RDONLY
: nbfd
->iostream
= (PTR
) fdopen (fd
, FOPEN_RB
); break;
242 case O_WRONLY
: nbfd
->iostream
= (PTR
) fdopen (fd
, FOPEN_RUB
); break;
243 case O_RDWR
: nbfd
->iostream
= (PTR
) fdopen (fd
, FOPEN_RUB
); break;
248 if (nbfd
->iostream
== NULL
)
250 _bfd_delete_bfd (nbfd
);
254 /* OK, put everything where it belongs. */
255 nbfd
->filename
= filename
;
257 /* As a special case we allow a FD open for read/write to
258 be written through, although doing so requires that we end
259 the previous clause with a preposition. */
260 /* (O_ACCMODE) parens are to avoid Ultrix header file bug. */
261 switch (fdflags
& (O_ACCMODE
))
263 case O_RDONLY
: nbfd
->direction
= read_direction
; break;
264 case O_WRONLY
: nbfd
->direction
= write_direction
; break;
265 case O_RDWR
: nbfd
->direction
= both_direction
; break;
269 if (! bfd_cache_init (nbfd
))
271 _bfd_delete_bfd (nbfd
);
274 nbfd
->opened_once
= true;
284 bfd *bfd_openstreamr(const char *, const char *, PTR);
288 Open a BFD for read access on an existing stdio stream. When
289 the BFD is passed to <<bfd_close>>, the stream will be closed.
293 bfd_openstreamr (filename
, target
, streamarg
)
294 const char *filename
;
298 FILE *stream
= (FILE *) streamarg
;
300 const bfd_target
*target_vec
;
302 nbfd
= _bfd_new_bfd ();
306 target_vec
= bfd_find_target (target
, nbfd
);
307 if (target_vec
== NULL
)
309 bfd_set_error (bfd_error_invalid_target
);
310 _bfd_delete_bfd (nbfd
);
314 nbfd
->iostream
= (PTR
) stream
;
315 nbfd
->filename
= filename
;
316 nbfd
->direction
= read_direction
;
318 if (! bfd_cache_init (nbfd
))
320 _bfd_delete_bfd (nbfd
);
327 /* bfd_openw -- open for writing.
328 Returns a pointer to a freshly-allocated BFD on success, or NULL.
330 See comment by bfd_fdopenr before you try to modify this function. */
337 bfd *bfd_openw(const char *filename, const char *target);
340 Create a BFD, associated with file @var{filename}, using the
341 file format @var{target}, and return a pointer to it.
343 Possible errors are <<bfd_error_system_call>>, <<bfd_error_no_memory>>,
344 <<bfd_error_invalid_target>>.
348 bfd_openw (filename
, target
)
349 const char *filename
;
353 const bfd_target
*target_vec
;
355 bfd_set_error (bfd_error_system_call
);
357 /* nbfd has to point to head of malloc'ed block so that bfd_close may
358 reclaim it correctly. */
359 nbfd
= _bfd_new_bfd ();
363 target_vec
= bfd_find_target (target
, nbfd
);
364 if (target_vec
== NULL
)
366 _bfd_delete_bfd (nbfd
);
370 nbfd
->filename
= filename
;
371 nbfd
->direction
= write_direction
;
373 if (bfd_open_file (nbfd
) == NULL
)
375 /* File not writeable, etc. */
376 bfd_set_error (bfd_error_system_call
);
377 _bfd_delete_bfd (nbfd
);
390 boolean bfd_close(bfd *abfd);
394 Close a BFD. If the BFD was open for writing,
395 then pending operations are completed and the file written out
396 and closed. If the created file is executable, then
397 <<chmod>> is called to mark it as such.
399 All memory attached to the BFD is released.
401 The file descriptor associated with the BFD is closed (even
402 if it was passed in to BFD by <<bfd_fdopenr>>).
405 <<true>> is returned if all is ok, otherwise <<false>>.
415 if (bfd_write_p (abfd
))
417 if (! BFD_SEND_FMT (abfd
, _bfd_write_contents
, (abfd
)))
421 if (! BFD_SEND (abfd
, _close_and_cleanup
, (abfd
)))
424 ret
= bfd_cache_close (abfd
);
426 /* If the file was open for writing and is now executable,
429 && abfd
->direction
== write_direction
430 && abfd
->flags
& EXEC_P
)
434 if (stat (abfd
->filename
, &buf
) == 0)
436 unsigned int mask
= umask (0);
439 chmod (abfd
->filename
,
441 & (buf
.st_mode
| ((S_IXUSR
| S_IXGRP
| S_IXOTH
) &~ mask
))));
445 _bfd_delete_bfd (abfd
);
455 boolean bfd_close_all_done(bfd *);
458 Close a BFD. Differs from <<bfd_close>>
459 since it does not complete any pending operations. This
460 routine would be used if the application had just used BFD for
461 swapping and didn't want to use any of the writing code.
463 If the created file is executable, then <<chmod>> is called
466 All memory attached to the BFD is released.
469 <<true>> is returned if all is ok, otherwise <<false>>.
473 bfd_close_all_done (abfd
)
478 ret
= bfd_cache_close (abfd
);
480 /* If the file was open for writing and is now executable,
483 && abfd
->direction
== write_direction
484 && abfd
->flags
& EXEC_P
)
488 if (stat (abfd
->filename
, &buf
) == 0)
490 unsigned int mask
= umask (0);
493 chmod (abfd
->filename
,
495 & (buf
.st_mode
| ((S_IXUSR
| S_IXGRP
| S_IXOTH
) &~ mask
))));
499 _bfd_delete_bfd (abfd
);
509 bfd *bfd_create(const char *filename, bfd *templ);
512 Create a new BFD in the manner of
513 <<bfd_openw>>, but without opening a file. The new BFD
514 takes the target from the target used by @var{template}. The
515 format is always set to <<bfd_object>>.
519 bfd_create (filename
, templ
)
520 const char *filename
;
525 nbfd
= _bfd_new_bfd ();
528 nbfd
->filename
= filename
;
530 nbfd
->xvec
= templ
->xvec
;
531 nbfd
->direction
= no_direction
;
532 bfd_set_format (nbfd
, bfd_object
);
542 boolean bfd_make_writable(bfd *abfd);
545 Takes a BFD as created by <<bfd_create>> and converts it
546 into one like as returned by <<bfd_openw>>. It does this
547 by converting the BFD to BFD_IN_MEMORY. It's assumed that
548 you will call <<bfd_make_readable>> on this bfd later.
551 <<true>> is returned if all is ok, otherwise <<false>>.
555 bfd_make_writable(abfd
)
558 struct bfd_in_memory
*bim
;
560 if (abfd
->direction
!= no_direction
)
562 bfd_set_error (bfd_error_invalid_operation
);
566 bim
= ((struct bfd_in_memory
*)
567 bfd_malloc ((bfd_size_type
) sizeof (struct bfd_in_memory
)));
568 abfd
->iostream
= (PTR
) bim
;
569 /* bfd_bwrite will grow these as needed. */
573 abfd
->flags
|= BFD_IN_MEMORY
;
574 abfd
->direction
= write_direction
;
585 boolean bfd_make_readable(bfd *abfd);
588 Takes a BFD as created by <<bfd_create>> and
589 <<bfd_make_writable>> and converts it into one like as
590 returned by <<bfd_openr>>. It does this by writing the
591 contents out to the memory buffer, then reversing the
595 <<true>> is returned if all is ok, otherwise <<false>>. */
598 bfd_make_readable(abfd
)
601 if (abfd
->direction
!= write_direction
|| !(abfd
->flags
& BFD_IN_MEMORY
))
603 bfd_set_error (bfd_error_invalid_operation
);
607 if (! BFD_SEND_FMT (abfd
, _bfd_write_contents
, (abfd
)))
610 if (! BFD_SEND (abfd
, _close_and_cleanup
, (abfd
)))
614 abfd
->arch_info
= &bfd_default_arch_struct
;
617 abfd
->sections
= (asection
*) NULL
;
618 abfd
->format
= bfd_unknown
;
619 abfd
->my_archive
= (bfd
*) NULL
;
621 abfd
->opened_once
= false;
622 abfd
->output_has_begun
= false;
623 abfd
->section_count
= 0;
624 abfd
->usrdata
= (PTR
) NULL
;
625 abfd
->cacheable
= false;
626 abfd
->flags
= BFD_IN_MEMORY
;
627 abfd
->mtime_set
= false;
629 abfd
->target_defaulted
= true;
630 abfd
->direction
= read_direction
;
633 abfd
->outsymbols
= 0;
636 bfd_check_format(abfd
, bfd_object
);
646 PTR bfd_alloc (bfd *abfd, size_t wanted);
649 Allocate a block of @var{wanted} bytes of memory attached to
650 <<abfd>> and return a pointer to it.
655 bfd_alloc (abfd
, size
)
661 if (size
!= (unsigned long) size
)
663 bfd_set_error (bfd_error_no_memory
);
667 ret
= objalloc_alloc (abfd
->memory
, (unsigned long) size
);
669 bfd_set_error (bfd_error_no_memory
);
674 bfd_zalloc (abfd
, size
)
680 res
= bfd_alloc (abfd
, size
);
682 memset (res
, 0, (size_t) size
);
686 /* Free a block allocated for a BFD.
687 Note: Also frees all more recently allocated blocks! */
690 bfd_release (abfd
, block
)
694 objalloc_free_block ((struct objalloc
*) abfd
->memory
, block
);