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 /* FIXME: This is no longer used. */
43 long _bfd_chunksize = -1;
45 /* Return a new BFD. All BFD's are allocated through this routine. */
52 nbfd = (bfd *) bfd_zmalloc ((bfd_size_type) sizeof (bfd));
56 nbfd->memory = (PTR) objalloc_create ();
57 if (nbfd->memory == NULL)
59 bfd_set_error (bfd_error_no_memory);
64 nbfd->arch_info = &bfd_default_arch_struct;
66 nbfd->direction = no_direction;
67 nbfd->iostream = NULL;
69 if (!bfd_hash_table_init (&nbfd->section_htab, bfd_section_hash_newfunc))
74 nbfd->sections = (asection *) NULL;
75 nbfd->section_tail = &nbfd->sections;
76 nbfd->format = bfd_unknown;
77 nbfd->my_archive = (bfd *) NULL;
79 nbfd->opened_once = false;
80 nbfd->output_has_begun = false;
81 nbfd->section_count = 0;
82 nbfd->usrdata = (PTR) NULL;
83 nbfd->cacheable = false;
84 nbfd->flags = BFD_NO_FLAGS;
85 nbfd->mtime_set = false;
90 /* Allocate a new BFD as a member of archive OBFD. */
93 _bfd_new_bfd_contained_in (obfd)
98 nbfd = _bfd_new_bfd ();
101 nbfd->xvec = obfd->xvec;
102 nbfd->my_archive = obfd;
103 nbfd->direction = read_direction;
104 nbfd->target_defaulted = obfd->target_defaulted;
111 _bfd_delete_bfd (abfd)
114 bfd_hash_table_free (&abfd->section_htab);
115 objalloc_free ((struct objalloc *) abfd->memory);
121 Opening and closing BFDs
130 bfd *bfd_openr(const char *filename, const char *target);
133 Open the file @var{filename} (using <<fopen>>) with the target
134 @var{target}. Return a pointer to the created BFD.
136 Calls <<bfd_find_target>>, so @var{target} is interpreted as by
139 If <<NULL>> is returned then an error has occured. Possible errors
140 are <<bfd_error_no_memory>>, <<bfd_error_invalid_target>> or <<system_call>> error.
144 bfd_openr (filename, target)
145 const char *filename;
149 const bfd_target *target_vec;
151 nbfd = _bfd_new_bfd ();
155 target_vec = bfd_find_target (target, nbfd);
156 if (target_vec == NULL)
158 bfd_set_error (bfd_error_invalid_target);
159 _bfd_delete_bfd (nbfd);
163 nbfd->filename = filename;
164 nbfd->direction = read_direction;
166 if (bfd_open_file (nbfd) == NULL)
168 /* File didn't exist, or some such */
169 bfd_set_error (bfd_error_system_call);
170 _bfd_delete_bfd (nbfd);
177 /* Don't try to `optimize' this function:
179 o - We lock using stack space so that interrupting the locking
180 won't cause a storage leak.
181 o - We open the file stream last, since we don't want to have to
182 close it if anything goes wrong. Closing the stream means closing
183 the file descriptor too, even though we didn't open it.
190 bfd *bfd_fdopenr(const char *filename, const char *target, int fd);
193 <<bfd_fdopenr>> is to <<bfd_fopenr>> much like <<fdopen>> is to <<fopen>>.
194 It opens a BFD on a file already described by the @var{fd}
197 When the file is later <<bfd_close>>d, the file descriptor will be closed.
199 If the caller desires that this file descriptor be cached by BFD
200 (opened as needed, closed as needed to free descriptors for
201 other opens), with the supplied @var{fd} used as an initial
202 file descriptor (but subject to closure at any time), call
203 bfd_set_cacheable(bfd, 1) on the returned BFD. The default is to
204 assume no cacheing; the file descriptor will remain open until
205 <<bfd_close>>, and will not be affected by BFD operations on other
208 Possible errors are <<bfd_error_no_memory>>, <<bfd_error_invalid_target>> and <<bfd_error_system_call>>.
212 bfd_fdopenr (filename, target, fd)
213 const char *filename;
218 const bfd_target *target_vec;
221 bfd_set_error (bfd_error_system_call);
222 #if ! defined(HAVE_FCNTL) || ! defined(F_GETFL)
223 fdflags = O_RDWR; /* Assume full access */
225 fdflags = fcntl (fd, F_GETFL, NULL);
227 if (fdflags == -1) return NULL;
229 nbfd = _bfd_new_bfd ();
233 target_vec = bfd_find_target (target, nbfd);
234 if (target_vec == NULL)
236 bfd_set_error (bfd_error_invalid_target);
237 _bfd_delete_bfd (nbfd);
242 nbfd->iostream = (PTR) fopen (filename, FOPEN_RB);
244 /* (O_ACCMODE) parens are to avoid Ultrix header file bug */
245 switch (fdflags & (O_ACCMODE))
247 case O_RDONLY: nbfd->iostream = (PTR) fdopen (fd, FOPEN_RB); break;
248 case O_WRONLY: nbfd->iostream = (PTR) fdopen (fd, FOPEN_RUB); break;
249 case O_RDWR: nbfd->iostream = (PTR) fdopen (fd, FOPEN_RUB); break;
254 if (nbfd->iostream == NULL)
256 _bfd_delete_bfd (nbfd);
260 /* OK, put everything where it belongs */
262 nbfd->filename = filename;
264 /* As a special case we allow a FD open for read/write to
265 be written through, although doing so requires that we end
266 the previous clause with a preposition. */
267 /* (O_ACCMODE) parens are to avoid Ultrix header file bug */
268 switch (fdflags & (O_ACCMODE))
270 case O_RDONLY: nbfd->direction = read_direction; break;
271 case O_WRONLY: nbfd->direction = write_direction; break;
272 case O_RDWR: nbfd->direction = both_direction; break;
276 if (! bfd_cache_init (nbfd))
278 _bfd_delete_bfd (nbfd);
281 nbfd->opened_once = true;
291 bfd *bfd_openstreamr(const char *, const char *, PTR);
295 Open a BFD for read access on an existing stdio stream. When
296 the BFD is passed to <<bfd_close>>, the stream will be closed.
300 bfd_openstreamr (filename, target, streamarg)
301 const char *filename;
305 FILE *stream = (FILE *) streamarg;
307 const bfd_target *target_vec;
309 nbfd = _bfd_new_bfd ();
313 target_vec = bfd_find_target (target, nbfd);
314 if (target_vec == NULL)
316 bfd_set_error (bfd_error_invalid_target);
317 _bfd_delete_bfd (nbfd);
321 nbfd->iostream = (PTR) stream;
322 nbfd->filename = filename;
323 nbfd->direction = read_direction;
325 if (! bfd_cache_init (nbfd))
327 _bfd_delete_bfd (nbfd);
334 /** bfd_openw -- open for writing.
335 Returns a pointer to a freshly-allocated BFD on success, or NULL.
337 See comment by bfd_fdopenr before you try to modify this function. */
344 bfd *bfd_openw(const char *filename, const char *target);
347 Create a BFD, associated with file @var{filename}, using the
348 file format @var{target}, and return a pointer to it.
350 Possible errors are <<bfd_error_system_call>>, <<bfd_error_no_memory>>,
351 <<bfd_error_invalid_target>>.
355 bfd_openw (filename, target)
356 const char *filename;
360 const bfd_target *target_vec;
362 bfd_set_error (bfd_error_system_call);
364 /* nbfd has to point to head of malloc'ed block so that bfd_close may
365 reclaim it correctly. */
367 nbfd = _bfd_new_bfd ();
371 target_vec = bfd_find_target (target, nbfd);
372 if (target_vec == NULL)
374 _bfd_delete_bfd (nbfd);
378 nbfd->filename = filename;
379 nbfd->direction = write_direction;
381 if (bfd_open_file (nbfd) == NULL)
383 bfd_set_error (bfd_error_system_call); /* File not writeable, etc */
384 _bfd_delete_bfd (nbfd);
397 boolean bfd_close(bfd *abfd);
401 Close a BFD. If the BFD was open for writing,
402 then pending operations are completed and the file written out
403 and closed. If the created file is executable, then
404 <<chmod>> is called to mark it as such.
406 All memory attached to the BFD is released.
408 The file descriptor associated with the BFD is closed (even
409 if it was passed in to BFD by <<bfd_fdopenr>>).
412 <<true>> is returned if all is ok, otherwise <<false>>.
422 if (!bfd_read_p (abfd))
424 if (! BFD_SEND_FMT (abfd, _bfd_write_contents, (abfd)))
428 if (! BFD_SEND (abfd, _close_and_cleanup, (abfd)))
431 ret = bfd_cache_close (abfd);
433 /* If the file was open for writing and is now executable,
436 && abfd->direction == write_direction
437 && abfd->flags & EXEC_P)
441 if (stat (abfd->filename, &buf) == 0)
443 unsigned int mask = umask (0);
445 chmod (abfd->filename,
447 & (buf.st_mode | ((S_IXUSR | S_IXGRP | S_IXOTH) &~ mask))));
451 _bfd_delete_bfd (abfd);
461 boolean bfd_close_all_done(bfd *);
464 Close a BFD. Differs from <<bfd_close>>
465 since it does not complete any pending operations. This
466 routine would be used if the application had just used BFD for
467 swapping and didn't want to use any of the writing code.
469 If the created file is executable, then <<chmod>> is called
472 All memory attached to the BFD is released.
475 <<true>> is returned if all is ok, otherwise <<false>>.
480 bfd_close_all_done (abfd)
485 ret = bfd_cache_close (abfd);
487 /* If the file was open for writing and is now executable,
490 && abfd->direction == write_direction
491 && abfd->flags & EXEC_P)
495 if (stat (abfd->filename, &buf) == 0)
497 unsigned int mask = umask (0);
499 chmod (abfd->filename,
501 & (buf.st_mode | ((S_IXUSR | S_IXGRP | S_IXOTH) &~ mask))));
505 _bfd_delete_bfd (abfd);
515 bfd *bfd_create(const char *filename, bfd *templ);
518 Create a new BFD in the manner of
519 <<bfd_openw>>, but without opening a file. The new BFD
520 takes the target from the target used by @var{template}. The
521 format is always set to <<bfd_object>>.
526 bfd_create (filename, templ)
527 const char *filename;
532 nbfd = _bfd_new_bfd ();
535 nbfd->filename = filename;
537 nbfd->xvec = templ->xvec;
538 nbfd->direction = no_direction;
539 bfd_set_format (nbfd, bfd_object);
548 boolean bfd_make_writable(bfd *abfd);
551 Takes a BFD as created by <<bfd_create>> and converts it
552 into one like as returned by <<bfd_openw>>. It does this
553 by converting the BFD to BFD_IN_MEMORY. It's assumed that
554 you will call <<bfd_make_readable>> on this bfd later.
557 <<true>> is returned if all is ok, otherwise <<false>>.
561 bfd_make_writable(abfd)
564 struct bfd_in_memory *bim;
566 if (abfd->direction != no_direction)
568 bfd_set_error (bfd_error_invalid_operation);
572 bim = ((struct bfd_in_memory *)
573 bfd_malloc ((bfd_size_type) sizeof (struct bfd_in_memory)));
574 abfd->iostream = (PTR) bim;
575 /* bfd_bwrite will grow these as needed */
579 abfd->flags |= BFD_IN_MEMORY;
580 abfd->direction = write_direction;
591 boolean bfd_make_readable(bfd *abfd);
594 Takes a BFD as created by <<bfd_create>> and
595 <<bfd_make_writable>> and converts it into one like as
596 returned by <<bfd_openr>>. It does this by writing the
597 contents out to the memory buffer, then reversing the
601 <<true>> is returned if all is ok, otherwise <<false>>. */
604 bfd_make_readable(abfd)
607 if (abfd->direction != write_direction || !(abfd->flags & BFD_IN_MEMORY))
609 bfd_set_error (bfd_error_invalid_operation);
613 if (! BFD_SEND_FMT (abfd, _bfd_write_contents, (abfd)))
616 if (! BFD_SEND (abfd, _close_and_cleanup, (abfd)))
620 abfd->arch_info = &bfd_default_arch_struct;
623 abfd->format = bfd_unknown;
624 abfd->my_archive = (bfd *) NULL;
626 abfd->opened_once = false;
627 abfd->output_has_begun = false;
628 abfd->section_count = 0;
629 abfd->usrdata = (PTR) NULL;
630 abfd->cacheable = false;
631 abfd->flags = BFD_IN_MEMORY;
632 abfd->mtime_set = false;
634 abfd->target_defaulted = true;
635 abfd->direction = read_direction;
638 abfd->outsymbols = 0;
641 bfd_section_list_clear (abfd);
642 bfd_check_format(abfd, bfd_object);
652 PTR bfd_alloc (bfd *abfd, size_t wanted);
655 Allocate a block of @var{wanted} bytes of memory attached to
656 <<abfd>> and return a pointer to it.
661 bfd_alloc (abfd, size)
667 if (size != (unsigned long) size)
669 bfd_set_error (bfd_error_no_memory);
673 ret = objalloc_alloc (abfd->memory, (unsigned long) size);
675 bfd_set_error (bfd_error_no_memory);
680 bfd_zalloc (abfd, size)
686 res = bfd_alloc (abfd, size);
688 memset (res, 0, (size_t) size);
692 /* Free a block allocated for a BFD.
693 Note: Also frees all more recently allocated blocks! */
696 bfd_release (abfd, block)
700 objalloc_free_block ((struct objalloc *) abfd->memory, block);